1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2006 M. Warner Losh
5 * Copyright (c) 2011-2012 Ian Lepore
6 * Copyright (c) 2012 Marius Strobl <marius@FreeBSD.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD$");
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/bio.h>
36#include <sys/bus.h>
37#include <sys/conf.h>
38#include <sys/endian.h>
39#include <sys/kernel.h>
40#include <sys/kthread.h>
41#include <sys/lock.h>
42#include <sys/mbuf.h>
43#include <sys/malloc.h>
44#include <sys/module.h>
45#include <sys/mutex.h>
46#include <geom/geom_disk.h>
47
48#include <dev/spibus/spi.h>
49#include "spibus_if.h"
50
51#include "opt_platform.h"
52
53#ifdef FDT
54#include <dev/fdt/fdt_common.h>
55#include <dev/ofw/ofw_bus_subr.h>
56#include <dev/ofw/openfirm.h>
57
58static struct ofw_compat_data compat_data[] = {
59	{ "atmel,at45",		1 },
60	{ "atmel,dataflash",	1 },
61	{ NULL,			0 },
62};
63#endif
64
65/* This is the information returned by the MANUFACTURER_ID command. */
66struct at45d_mfg_info {
67	uint32_t	jedec_id; /* Mfg ID, DevId1, DevId2, ExtLen */
68	uint16_t	ext_id;   /* ExtId1, ExtId2 */
69};
70
71/*
72 * This is an entry in our table of metadata describing the chips.  We match on
73 * both jedec id and extended id info returned by the MANUFACTURER_ID command.
74 */
75struct at45d_flash_ident
76{
77	const char	*name;
78	uint32_t	jedec;
79	uint16_t	extid;
80	uint16_t	extmask;
81	uint16_t	pagecount;
82	uint16_t	pageoffset;
83	uint16_t	pagesize;
84	uint16_t	pagesize2n;
85};
86
87struct at45d_softc
88{
89	struct bio_queue_head	bio_queue;
90	struct mtx		sc_mtx;
91	struct disk		*disk;
92	struct proc		*p;
93	device_t		dev;
94	u_int			taskstate;
95	uint16_t		pagecount;
96	uint16_t		pageoffset;
97	uint16_t		pagesize;
98	void			*dummybuf;
99};
100
101#define	TSTATE_STOPPED	0
102#define	TSTATE_STOPPING	1
103#define	TSTATE_RUNNING	2
104
105#define	AT45D_LOCK(_sc)			mtx_lock(&(_sc)->sc_mtx)
106#define	AT45D_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
107#define	AT45D_LOCK_INIT(_sc) \
108	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
109	    "at45d", MTX_DEF)
110#define	AT45D_LOCK_DESTROY(_sc)		mtx_destroy(&_sc->sc_mtx);
111#define	AT45D_ASSERT_LOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_OWNED);
112#define	AT45D_ASSERT_UNLOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
113
114/* bus entry points */
115static device_attach_t at45d_attach;
116static device_detach_t at45d_detach;
117static device_probe_t at45d_probe;
118
119/* disk routines */
120static int at45d_close(struct disk *dp);
121static int at45d_open(struct disk *dp);
122static int at45d_getattr(struct bio *bp);
123static void at45d_strategy(struct bio *bp);
124static void at45d_task(void *arg);
125
126/* helper routines */
127static void at45d_delayed_attach(void *xsc);
128static int at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp);
129static int at45d_get_status(device_t dev, uint8_t *status);
130static int at45d_wait_ready(device_t dev, uint8_t *status);
131
132#define	PAGE_TO_BUFFER_TRANSFER		0x53
133#define	PAGE_TO_BUFFER_COMPARE		0x60
134#define	PROGRAM_THROUGH_BUFFER		0x82
135#define	MANUFACTURER_ID			0x9f
136#define	STATUS_REGISTER_READ		0xd7
137#define	CONTINUOUS_ARRAY_READ		0xe8
138
139#define	STATUS_READY			(1u << 7)
140#define	STATUS_CMPFAIL			(1u << 6)
141#define	STATUS_PAGE2N			(1u << 0)
142
143/*
144 * Metadata for supported chips.
145 *
146 * The jedec id in this table includes the extended id length byte.  A match is
147 * based on both jedec id and extended id matching.  The chip's extended id (not
148 * present in most chips) is ANDed with ExtMask and the result is compared to
149 * ExtId.  If a chip only returns 1 ext id byte it will be in the upper 8 bits
150 * of ExtId in this table.
151 *
152 * A sectorsize2n != 0 is used to indicate that a device optionally supports
153 * 2^N byte pages.  If support for the latter is enabled, the sector offset
154 * has to be reduced by one.
155 */
156static const struct at45d_flash_ident at45d_flash_devices[] = {
157	/* Part Name    Jedec ID    ExtId   ExtMask PgCnt Offs PgSz PgSz2n */
158	{ "AT45DB011B", 0x1f220000, 0x0000, 0x0000,   512,  9,  264,  256 },
159	{ "AT45DB021B", 0x1f230000, 0x0000, 0x0000,  1024,  9,  264,  256 },
160	{ "AT45DB041x", 0x1f240000, 0x0000, 0x0000,  2028,  9,  264,  256 },
161	{ "AT45DB081B", 0x1f250000, 0x0000, 0x0000,  4096,  9,  264,  256 },
162	{ "AT45DB161x", 0x1f260000, 0x0000, 0x0000,  4096, 10,  528,  512 },
163	{ "AT45DB321x", 0x1f270000, 0x0000, 0x0000,  8192, 10,  528,    0 },
164	{ "AT45DB321x", 0x1f270100, 0x0000, 0x0000,  8192, 10,  528,  512 },
165	{ "AT45DB641E", 0x1f280001, 0x0000, 0xff00, 32768,  9,  264,  256 },
166	{ "AT45DB642x", 0x1f280000, 0x0000, 0x0000,  8192, 11, 1056, 1024 },
167};
168
169static int
170at45d_get_status(device_t dev, uint8_t *status)
171{
172	uint8_t rxBuf[8], txBuf[8];
173	struct spi_command cmd;
174	int err;
175
176	memset(&cmd, 0, sizeof(cmd));
177	memset(txBuf, 0, sizeof(txBuf));
178	memset(rxBuf, 0, sizeof(rxBuf));
179
180	txBuf[0] = STATUS_REGISTER_READ;
181	cmd.tx_cmd = txBuf;
182	cmd.rx_cmd = rxBuf;
183	cmd.rx_cmd_sz = cmd.tx_cmd_sz = 2;
184	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
185	*status = rxBuf[1];
186	return (err);
187}
188
189static int
190at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp)
191{
192	uint8_t rxBuf[8], txBuf[8];
193	struct spi_command cmd;
194	int err;
195
196	memset(&cmd, 0, sizeof(cmd));
197	memset(txBuf, 0, sizeof(txBuf));
198	memset(rxBuf, 0, sizeof(rxBuf));
199
200	txBuf[0] = MANUFACTURER_ID;
201	cmd.tx_cmd = &txBuf;
202	cmd.rx_cmd = &rxBuf;
203	cmd.tx_cmd_sz = cmd.rx_cmd_sz = 7;
204	err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
205	if (err)
206		return (err);
207
208	resp->jedec_id = be32dec(rxBuf + 1);
209	resp->ext_id   = be16dec(rxBuf + 5);
210
211	return (0);
212}
213
214static int
215at45d_wait_ready(device_t dev, uint8_t *status)
216{
217	struct timeval now, tout;
218	int err;
219
220	getmicrouptime(&tout);
221	tout.tv_sec += 3;
222	do {
223		getmicrouptime(&now);
224		if (now.tv_sec > tout.tv_sec)
225			err = ETIMEDOUT;
226		else
227			err = at45d_get_status(dev, status);
228	} while (err == 0 && !(*status & STATUS_READY));
229	return (err);
230}
231
232static int
233at45d_probe(device_t dev)
234{
235	int rv;
236
237#ifdef FDT
238	if (!ofw_bus_status_okay(dev))
239		return (ENXIO);
240
241	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
242		return (ENXIO);
243
244	rv = BUS_PROBE_DEFAULT;
245#else
246	rv = BUS_PROBE_NOWILDCARD;
247#endif
248
249	device_set_desc(dev, "AT45D Flash Family");
250	return (rv);
251}
252
253static int
254at45d_attach(device_t dev)
255{
256	struct at45d_softc *sc;
257
258	sc = device_get_softc(dev);
259	sc->dev = dev;
260	AT45D_LOCK_INIT(sc);
261
262	config_intrhook_oneshot(at45d_delayed_attach, sc);
263	return (0);
264}
265
266static int
267at45d_detach(device_t dev)
268{
269	struct at45d_softc *sc;
270	int err;
271
272	sc = device_get_softc(dev);
273	err = 0;
274
275	AT45D_LOCK(sc);
276	if (sc->taskstate == TSTATE_RUNNING) {
277		sc->taskstate = TSTATE_STOPPING;
278		wakeup(sc);
279		while (err == 0 && sc->taskstate != TSTATE_STOPPED) {
280			err = msleep(sc, &sc->sc_mtx, 0, "at45dt", hz * 3);
281			if (err != 0) {
282				sc->taskstate = TSTATE_RUNNING;
283				device_printf(sc->dev,
284				    "Failed to stop queue task\n");
285			}
286		}
287	}
288	AT45D_UNLOCK(sc);
289
290	if (err == 0 && sc->taskstate == TSTATE_STOPPED) {
291		if (sc->disk) {
292			disk_destroy(sc->disk);
293			bioq_flush(&sc->bio_queue, NULL, ENXIO);
294			free(sc->dummybuf, M_DEVBUF);
295		}
296		AT45D_LOCK_DESTROY(sc);
297	}
298	return (err);
299}
300
301static void
302at45d_delayed_attach(void *xsc)
303{
304	struct at45d_softc *sc;
305	struct at45d_mfg_info mfginfo;
306	const struct at45d_flash_ident *ident;
307	u_int i;
308	int sectorsize;
309	uint32_t jedec;
310	uint16_t pagesize;
311	uint8_t status;
312
313	sc = xsc;
314	ident = NULL;
315	jedec = 0;
316
317	if (at45d_wait_ready(sc->dev, &status) != 0) {
318		device_printf(sc->dev, "Error waiting for device-ready.\n");
319		return;
320	}
321	if (at45d_get_mfg_info(sc->dev, &mfginfo) != 0) {
322		device_printf(sc->dev, "Failed to get ID.\n");
323		return;
324	}
325	for (i = 0; i < nitems(at45d_flash_devices); i++) {
326		ident = &at45d_flash_devices[i];
327		if (mfginfo.jedec_id == ident->jedec &&
328		    (mfginfo.ext_id & ident->extmask) == ident->extid) {
329			break;
330		}
331	}
332	if (i == nitems(at45d_flash_devices)) {
333		device_printf(sc->dev, "JEDEC 0x%x not in list.\n", jedec);
334		return;
335	}
336
337	sc->pagecount = ident->pagecount;
338	sc->pageoffset = ident->pageoffset;
339	if (ident->pagesize2n != 0 && (status & STATUS_PAGE2N)) {
340		sc->pageoffset -= 1;
341		pagesize = ident->pagesize2n;
342	} else
343		pagesize = ident->pagesize;
344	sc->pagesize = pagesize;
345
346	/*
347	 * By default we set up a disk with a sector size that matches the
348	 * device page size.  If there is a device hint or fdt property
349	 * requesting a different size, use that, as long as it is a multiple of
350	 * the device page size).
351	 */
352	sectorsize = pagesize;
353#ifdef FDT
354	{
355		pcell_t size;
356		if (OF_getencprop(ofw_bus_get_node(sc->dev),
357		    "freebsd,sectorsize", &size, sizeof(size)) > 0)
358			sectorsize = size;
359	}
360#endif
361	resource_int_value(device_get_name(sc->dev), device_get_unit(sc->dev),
362	    "sectorsize", &sectorsize);
363
364	if ((sectorsize % pagesize) != 0) {
365		device_printf(sc->dev, "Invalid sectorsize %d, "
366		    "must be a multiple of %d\n", sectorsize, pagesize);
367		return;
368	}
369
370	sc->dummybuf = malloc(pagesize, M_DEVBUF, M_WAITOK | M_ZERO);
371
372	sc->disk = disk_alloc();
373	sc->disk->d_open = at45d_open;
374	sc->disk->d_close = at45d_close;
375	sc->disk->d_strategy = at45d_strategy;
376	sc->disk->d_getattr = at45d_getattr;
377	sc->disk->d_name = "flash/at45d";
378	sc->disk->d_drv1 = sc;
379	sc->disk->d_maxsize = DFLTPHYS;
380	sc->disk->d_sectorsize = sectorsize;
381	sc->disk->d_mediasize = pagesize * ident->pagecount;
382	sc->disk->d_unit = device_get_unit(sc->dev);
383	disk_create(sc->disk, DISK_VERSION);
384	bioq_init(&sc->bio_queue);
385	kproc_create(&at45d_task, sc, &sc->p, 0, 0, "task: at45d flash");
386	sc->taskstate = TSTATE_RUNNING;
387	device_printf(sc->dev,
388	    "%s, %d bytes per page, %d pages; %d KBytes; disk sector size %d\n",
389	    ident->name, pagesize, ident->pagecount,
390	    (pagesize * ident->pagecount) / 1024, sectorsize);
391}
392
393static int
394at45d_open(struct disk *dp)
395{
396
397	return (0);
398}
399
400static int
401at45d_close(struct disk *dp)
402{
403
404	return (0);
405}
406
407static int
408at45d_getattr(struct bio *bp)
409{
410	struct at45d_softc *sc;
411
412	/*
413	 * This function exists to support geom_flashmap and fdt_slicer.
414	 */
415
416	if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
417		return (ENXIO);
418	if (strcmp(bp->bio_attribute, "SPI::device") != 0)
419		return (-1);
420	sc = bp->bio_disk->d_drv1;
421	if (bp->bio_length != sizeof(sc->dev))
422		return (EFAULT);
423	bcopy(&sc->dev, bp->bio_data, sizeof(sc->dev));
424	return (0);
425}
426
427static void
428at45d_strategy(struct bio *bp)
429{
430	struct at45d_softc *sc;
431
432	sc = (struct at45d_softc *)bp->bio_disk->d_drv1;
433	AT45D_LOCK(sc);
434	bioq_disksort(&sc->bio_queue, bp);
435	wakeup(sc);
436	AT45D_UNLOCK(sc);
437}
438
439static void
440at45d_task(void *arg)
441{
442	uint8_t rxBuf[8], txBuf[8];
443	struct at45d_softc *sc;
444	struct bio *bp;
445	struct spi_command cmd;
446	device_t dev, pdev;
447	caddr_t buf;
448	u_long len, resid;
449	u_int addr, berr, err, offset, page;
450	uint8_t status;
451
452	sc = (struct at45d_softc*)arg;
453	dev = sc->dev;
454	pdev = device_get_parent(dev);
455	memset(&cmd, 0, sizeof(cmd));
456	memset(txBuf, 0, sizeof(txBuf));
457	memset(rxBuf, 0, sizeof(rxBuf));
458	cmd.tx_cmd = txBuf;
459	cmd.rx_cmd = rxBuf;
460
461	for (;;) {
462		AT45D_LOCK(sc);
463		do {
464			if (sc->taskstate == TSTATE_STOPPING) {
465				sc->taskstate = TSTATE_STOPPED;
466				AT45D_UNLOCK(sc);
467				wakeup(sc);
468				kproc_exit(0);
469			}
470			bp = bioq_takefirst(&sc->bio_queue);
471			if (bp == NULL)
472				msleep(sc, &sc->sc_mtx, PRIBIO, "at45dq", 0);
473		} while (bp == NULL);
474		AT45D_UNLOCK(sc);
475
476		berr = 0;
477		buf = bp->bio_data;
478		len = resid = bp->bio_bcount;
479		page = bp->bio_offset / sc->pagesize;
480		offset = bp->bio_offset % sc->pagesize;
481
482		switch (bp->bio_cmd) {
483		case BIO_READ:
484			txBuf[0] = CONTINUOUS_ARRAY_READ;
485			cmd.tx_cmd_sz = cmd.rx_cmd_sz = 8;
486			cmd.tx_data = sc->dummybuf;
487			cmd.rx_data = buf;
488			break;
489		case BIO_WRITE:
490			cmd.tx_cmd_sz = cmd.rx_cmd_sz = 4;
491			cmd.tx_data = buf;
492			cmd.rx_data = sc->dummybuf;
493			if (resid + offset > sc->pagesize)
494				len = sc->pagesize - offset;
495			break;
496		default:
497			berr = EINVAL;
498			goto out;
499		}
500
501		/*
502		 * NB: for BIO_READ, this loop is only traversed once.
503		 */
504		while (resid > 0) {
505			if (page > sc->pagecount) {
506				berr = EINVAL;
507				goto out;
508			}
509			addr = page << sc->pageoffset;
510			if (bp->bio_cmd == BIO_WRITE) {
511				/*
512				 * If writing less than a full page, transfer
513				 * the existing page to the buffer, so that our
514				 * PROGRAM_THROUGH_BUFFER below will preserve
515				 * the parts of the page we're not writing.
516				 */
517				if (len != sc->pagesize) {
518					txBuf[0] = PAGE_TO_BUFFER_TRANSFER;
519					txBuf[1] = ((addr >> 16) & 0xff);
520					txBuf[2] = ((addr >> 8) & 0xff);
521					txBuf[3] = 0;
522					cmd.tx_data_sz = cmd.rx_data_sz = 0;
523					err = SPIBUS_TRANSFER(pdev, dev, &cmd);
524					if (err == 0)
525						err = at45d_wait_ready(dev,
526						    &status);
527					if (err != 0) {
528						berr = EIO;
529						goto out;
530					}
531				}
532				txBuf[0] = PROGRAM_THROUGH_BUFFER;
533			}
534
535			addr += offset;
536			txBuf[1] = ((addr >> 16) & 0xff);
537			txBuf[2] = ((addr >> 8) & 0xff);
538			txBuf[3] = (addr & 0xff);
539			cmd.tx_data_sz = cmd.rx_data_sz = len;
540			err = SPIBUS_TRANSFER(pdev, dev, &cmd);
541			if (err == 0 && bp->bio_cmd != BIO_READ)
542				err = at45d_wait_ready(dev, &status);
543			if (err != 0) {
544				berr = EIO;
545				goto out;
546			}
547			if (bp->bio_cmd == BIO_WRITE) {
548				addr = page << sc->pageoffset;
549				txBuf[0] = PAGE_TO_BUFFER_COMPARE;
550				txBuf[1] = ((addr >> 16) & 0xff);
551				txBuf[2] = ((addr >> 8) & 0xff);
552				txBuf[3] = 0;
553				cmd.tx_data_sz = cmd.rx_data_sz = 0;
554				err = SPIBUS_TRANSFER(pdev, dev, &cmd);
555				if (err == 0)
556					err = at45d_wait_ready(dev, &status);
557				if (err != 0 || (status & STATUS_CMPFAIL)) {
558					device_printf(dev, "comparing page "
559					    "%d failed (status=0x%x)\n", page,
560					    status);
561					berr = EIO;
562					goto out;
563				}
564			}
565			page++;
566			buf += len;
567			offset = 0;
568			resid -= len;
569			if (resid > sc->pagesize)
570				len = sc->pagesize;
571			else
572				len = resid;
573			if (bp->bio_cmd == BIO_READ)
574				cmd.rx_data = buf;
575			else
576				cmd.tx_data = buf;
577		}
578 out:
579		if (berr != 0) {
580			bp->bio_flags |= BIO_ERROR;
581			bp->bio_error = berr;
582		}
583		bp->bio_resid = resid;
584		biodone(bp);
585	}
586}
587
588static devclass_t at45d_devclass;
589
590static device_method_t at45d_methods[] = {
591	/* Device interface */
592	DEVMETHOD(device_probe,		at45d_probe),
593	DEVMETHOD(device_attach,	at45d_attach),
594	DEVMETHOD(device_detach,	at45d_detach),
595
596	DEVMETHOD_END
597};
598
599static driver_t at45d_driver = {
600	"at45d",
601	at45d_methods,
602	sizeof(struct at45d_softc),
603};
604
605DRIVER_MODULE(at45d, spibus, at45d_driver, at45d_devclass, NULL, NULL);
606MODULE_DEPEND(at45d, spibus, 1, 1, 1);
607#ifdef FDT
608MODULE_DEPEND(at45d, fdt_slicer, 1, 1, 1);
609SPIBUS_FDT_PNP_INFO(compat_data);
610#endif
611
612