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