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