2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
37 #include <sys/endian.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <geom/geom_disk.h>
47 #include <dev/spibus/spi.h>
48 #include "spibus_if.h"
50 #include "opt_platform.h"
53 #include <dev/fdt/fdt_common.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55 #include <dev/ofw/openfirm.h>
57 static struct ofw_compat_data compat_data[] = {
59 { "atmel,dataflash", 1 },
64 /* This is the information returned by the MANUFACTURER_ID command. */
65 struct at45d_mfg_info {
66 uint32_t jedec_id; /* Mfg ID, DevId1, DevId2, ExtLen */
67 uint16_t ext_id; /* ExtId1, ExtId2 */
71 * This is an entry in our table of metadata describing the chips. We match on
72 * both jedec id and extended id info returned by the MANUFACTURER_ID command.
74 struct at45d_flash_ident
88 struct bio_queue_head bio_queue;
100 #define TSTATE_STOPPED 0
101 #define TSTATE_STOPPING 1
102 #define TSTATE_RUNNING 2
104 #define AT45D_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
105 #define AT45D_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
106 #define AT45D_LOCK_INIT(_sc) \
107 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
109 #define AT45D_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
110 #define AT45D_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
111 #define AT45D_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
113 /* bus entry points */
114 static device_attach_t at45d_attach;
115 static device_detach_t at45d_detach;
116 static device_probe_t at45d_probe;
119 static int at45d_close(struct disk *dp);
120 static int at45d_open(struct disk *dp);
121 static int at45d_getattr(struct bio *bp);
122 static void at45d_strategy(struct bio *bp);
123 static void at45d_task(void *arg);
125 /* helper routines */
126 static void at45d_delayed_attach(void *xsc);
127 static int at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp);
128 static int at45d_get_status(device_t dev, uint8_t *status);
129 static int at45d_wait_ready(device_t dev, uint8_t *status);
131 #define PAGE_TO_BUFFER_TRANSFER 0x53
132 #define PAGE_TO_BUFFER_COMPARE 0x60
133 #define PROGRAM_THROUGH_BUFFER 0x82
134 #define MANUFACTURER_ID 0x9f
135 #define STATUS_REGISTER_READ 0xd7
136 #define CONTINUOUS_ARRAY_READ 0xe8
138 #define STATUS_READY (1u << 7)
139 #define STATUS_CMPFAIL (1u << 6)
140 #define STATUS_PAGE2N (1u << 0)
143 * Metadata for supported chips.
145 * The jedec id in this table includes the extended id length byte. A match is
146 * based on both jedec id and extended id matching. The chip's extended id (not
147 * present in most chips) is ANDed with ExtMask and the result is compared to
148 * ExtId. If a chip only returns 1 ext id byte it will be in the upper 8 bits
149 * of ExtId in this table.
151 * A sectorsize2n != 0 is used to indicate that a device optionally supports
152 * 2^N byte pages. If support for the latter is enabled, the sector offset
153 * has to be reduced by one.
155 static const struct at45d_flash_ident at45d_flash_devices[] = {
156 /* Part Name Jedec ID ExtId ExtMask PgCnt Offs PgSz PgSz2n */
157 { "AT45DB011B", 0x1f220000, 0x0000, 0x0000, 512, 9, 264, 256 },
158 { "AT45DB021B", 0x1f230000, 0x0000, 0x0000, 1024, 9, 264, 256 },
159 { "AT45DB041x", 0x1f240000, 0x0000, 0x0000, 2028, 9, 264, 256 },
160 { "AT45DB081B", 0x1f250000, 0x0000, 0x0000, 4096, 9, 264, 256 },
161 { "AT45DB161x", 0x1f260000, 0x0000, 0x0000, 4096, 10, 528, 512 },
162 { "AT45DB321x", 0x1f270000, 0x0000, 0x0000, 8192, 10, 528, 0 },
163 { "AT45DB321x", 0x1f270100, 0x0000, 0x0000, 8192, 10, 528, 512 },
164 { "AT45DB641E", 0x1f280001, 0x0000, 0xff00, 32768, 9, 264, 256 },
165 { "AT45DB642x", 0x1f280000, 0x0000, 0x0000, 8192, 11, 1056, 1024 },
169 at45d_get_status(device_t dev, uint8_t *status)
171 uint8_t rxBuf[8], txBuf[8];
172 struct spi_command cmd;
175 memset(&cmd, 0, sizeof(cmd));
176 memset(txBuf, 0, sizeof(txBuf));
177 memset(rxBuf, 0, sizeof(rxBuf));
179 txBuf[0] = STATUS_REGISTER_READ;
182 cmd.rx_cmd_sz = cmd.tx_cmd_sz = 2;
183 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
189 at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp)
191 uint8_t rxBuf[8], txBuf[8];
192 struct spi_command cmd;
195 memset(&cmd, 0, sizeof(cmd));
196 memset(txBuf, 0, sizeof(txBuf));
197 memset(rxBuf, 0, sizeof(rxBuf));
199 txBuf[0] = MANUFACTURER_ID;
202 cmd.tx_cmd_sz = cmd.rx_cmd_sz = 7;
203 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
207 resp->jedec_id = be32dec(rxBuf + 1);
208 resp->ext_id = be16dec(rxBuf + 5);
214 at45d_wait_ready(device_t dev, uint8_t *status)
216 struct timeval now, tout;
219 getmicrouptime(&tout);
222 getmicrouptime(&now);
223 if (now.tv_sec > tout.tv_sec)
226 err = at45d_get_status(dev, status);
227 } while (err == 0 && !(*status & STATUS_READY));
232 at45d_probe(device_t dev)
237 if (!ofw_bus_status_okay(dev))
240 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
243 rv = BUS_PROBE_DEFAULT;
245 rv = BUS_PROBE_NOWILDCARD;
248 device_set_desc(dev, "AT45D Flash Family");
253 at45d_attach(device_t dev)
255 struct at45d_softc *sc;
257 sc = device_get_softc(dev);
261 config_intrhook_oneshot(at45d_delayed_attach, sc);
266 at45d_detach(device_t dev)
268 struct at45d_softc *sc;
271 sc = device_get_softc(dev);
275 if (sc->taskstate == TSTATE_RUNNING) {
276 sc->taskstate = TSTATE_STOPPING;
278 while (err == 0 && sc->taskstate != TSTATE_STOPPED) {
279 err = msleep(sc, &sc->sc_mtx, 0, "at45dt", hz * 3);
281 sc->taskstate = TSTATE_RUNNING;
282 device_printf(sc->dev,
283 "Failed to stop queue task\n");
289 if (err == 0 && sc->taskstate == TSTATE_STOPPED) {
291 disk_destroy(sc->disk);
292 bioq_flush(&sc->bio_queue, NULL, ENXIO);
293 free(sc->dummybuf, M_DEVBUF);
295 AT45D_LOCK_DESTROY(sc);
301 at45d_delayed_attach(void *xsc)
303 struct at45d_softc *sc;
304 struct at45d_mfg_info mfginfo;
305 const struct at45d_flash_ident *ident;
316 if (at45d_wait_ready(sc->dev, &status) != 0) {
317 device_printf(sc->dev, "Error waiting for device-ready.\n");
320 if (at45d_get_mfg_info(sc->dev, &mfginfo) != 0) {
321 device_printf(sc->dev, "Failed to get ID.\n");
324 for (i = 0; i < nitems(at45d_flash_devices); i++) {
325 ident = &at45d_flash_devices[i];
326 if (mfginfo.jedec_id == ident->jedec &&
327 (mfginfo.ext_id & ident->extmask) == ident->extid) {
331 if (i == nitems(at45d_flash_devices)) {
332 device_printf(sc->dev, "JEDEC 0x%x not in list.\n", jedec);
336 sc->pagecount = ident->pagecount;
337 sc->pageoffset = ident->pageoffset;
338 if (ident->pagesize2n != 0 && (status & STATUS_PAGE2N)) {
340 pagesize = ident->pagesize2n;
342 pagesize = ident->pagesize;
343 sc->pagesize = pagesize;
346 * By default we set up a disk with a sector size that matches the
347 * device page size. If there is a device hint or fdt property
348 * requesting a different size, use that, as long as it is a multiple of
349 * the device page size).
351 sectorsize = pagesize;
355 if (OF_getencprop(ofw_bus_get_node(sc->dev),
356 "freebsd,sectorsize", &size, sizeof(size)) > 0)
360 resource_int_value(device_get_name(sc->dev), device_get_unit(sc->dev),
361 "sectorsize", §orsize);
363 if ((sectorsize % pagesize) != 0) {
364 device_printf(sc->dev, "Invalid sectorsize %d, "
365 "must be a multiple of %d\n", sectorsize, pagesize);
369 sc->dummybuf = malloc(pagesize, M_DEVBUF, M_WAITOK | M_ZERO);
371 sc->disk = disk_alloc();
372 sc->disk->d_open = at45d_open;
373 sc->disk->d_close = at45d_close;
374 sc->disk->d_strategy = at45d_strategy;
375 sc->disk->d_getattr = at45d_getattr;
376 sc->disk->d_name = "flash/at45d";
377 sc->disk->d_drv1 = sc;
378 sc->disk->d_maxsize = DFLTPHYS;
379 sc->disk->d_sectorsize = sectorsize;
380 sc->disk->d_mediasize = pagesize * ident->pagecount;
381 sc->disk->d_unit = device_get_unit(sc->dev);
382 disk_create(sc->disk, DISK_VERSION);
383 bioq_init(&sc->bio_queue);
384 kproc_create(&at45d_task, sc, &sc->p, 0, 0, "task: at45d flash");
385 sc->taskstate = TSTATE_RUNNING;
386 device_printf(sc->dev,
387 "%s, %d bytes per page, %d pages; %d KBytes; disk sector size %d\n",
388 ident->name, pagesize, ident->pagecount,
389 (pagesize * ident->pagecount) / 1024, sectorsize);
393 at45d_open(struct disk *dp)
400 at45d_close(struct disk *dp)
407 at45d_getattr(struct bio *bp)
409 struct at45d_softc *sc;
412 * This function exists to support geom_flashmap and fdt_slicer.
415 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
417 if (strcmp(bp->bio_attribute, "SPI::device") != 0)
419 sc = bp->bio_disk->d_drv1;
420 if (bp->bio_length != sizeof(sc->dev))
422 bcopy(&sc->dev, bp->bio_data, sizeof(sc->dev));
427 at45d_strategy(struct bio *bp)
429 struct at45d_softc *sc;
431 sc = (struct at45d_softc *)bp->bio_disk->d_drv1;
433 bioq_disksort(&sc->bio_queue, bp);
439 at45d_task(void *arg)
441 uint8_t rxBuf[8], txBuf[8];
442 struct at45d_softc *sc;
444 struct spi_command cmd;
448 u_int addr, berr, err, offset, page;
451 sc = (struct at45d_softc*)arg;
453 pdev = device_get_parent(dev);
454 memset(&cmd, 0, sizeof(cmd));
455 memset(txBuf, 0, sizeof(txBuf));
456 memset(rxBuf, 0, sizeof(rxBuf));
463 if (sc->taskstate == TSTATE_STOPPING) {
464 sc->taskstate = TSTATE_STOPPED;
469 bp = bioq_takefirst(&sc->bio_queue);
471 msleep(sc, &sc->sc_mtx, PRIBIO, "at45dq", 0);
472 } while (bp == NULL);
477 len = resid = bp->bio_bcount;
478 page = bp->bio_offset / sc->pagesize;
479 offset = bp->bio_offset % sc->pagesize;
481 switch (bp->bio_cmd) {
483 txBuf[0] = CONTINUOUS_ARRAY_READ;
484 cmd.tx_cmd_sz = cmd.rx_cmd_sz = 8;
485 cmd.tx_data = sc->dummybuf;
489 cmd.tx_cmd_sz = cmd.rx_cmd_sz = 4;
491 cmd.rx_data = sc->dummybuf;
492 if (resid + offset > sc->pagesize)
493 len = sc->pagesize - offset;
501 * NB: for BIO_READ, this loop is only traversed once.
504 if (page > sc->pagecount) {
508 addr = page << sc->pageoffset;
509 if (bp->bio_cmd == BIO_WRITE) {
511 * If writing less than a full page, transfer
512 * the existing page to the buffer, so that our
513 * PROGRAM_THROUGH_BUFFER below will preserve
514 * the parts of the page we're not writing.
516 if (len != sc->pagesize) {
517 txBuf[0] = PAGE_TO_BUFFER_TRANSFER;
518 txBuf[1] = ((addr >> 16) & 0xff);
519 txBuf[2] = ((addr >> 8) & 0xff);
521 cmd.tx_data_sz = cmd.rx_data_sz = 0;
522 err = SPIBUS_TRANSFER(pdev, dev, &cmd);
524 err = at45d_wait_ready(dev,
531 txBuf[0] = PROGRAM_THROUGH_BUFFER;
535 txBuf[1] = ((addr >> 16) & 0xff);
536 txBuf[2] = ((addr >> 8) & 0xff);
537 txBuf[3] = (addr & 0xff);
538 cmd.tx_data_sz = cmd.rx_data_sz = len;
539 err = SPIBUS_TRANSFER(pdev, dev, &cmd);
540 if (err == 0 && bp->bio_cmd != BIO_READ)
541 err = at45d_wait_ready(dev, &status);
546 if (bp->bio_cmd == BIO_WRITE) {
547 addr = page << sc->pageoffset;
548 txBuf[0] = PAGE_TO_BUFFER_COMPARE;
549 txBuf[1] = ((addr >> 16) & 0xff);
550 txBuf[2] = ((addr >> 8) & 0xff);
552 cmd.tx_data_sz = cmd.rx_data_sz = 0;
553 err = SPIBUS_TRANSFER(pdev, dev, &cmd);
555 err = at45d_wait_ready(dev, &status);
556 if (err != 0 || (status & STATUS_CMPFAIL)) {
557 device_printf(dev, "comparing page "
558 "%d failed (status=0x%x)\n", page,
568 if (resid > sc->pagesize)
572 if (bp->bio_cmd == BIO_READ)
579 bp->bio_flags |= BIO_ERROR;
580 bp->bio_error = berr;
582 bp->bio_resid = resid;
587 static devclass_t at45d_devclass;
589 static device_method_t at45d_methods[] = {
590 /* Device interface */
591 DEVMETHOD(device_probe, at45d_probe),
592 DEVMETHOD(device_attach, at45d_attach),
593 DEVMETHOD(device_detach, at45d_detach),
598 static driver_t at45d_driver = {
601 sizeof(struct at45d_softc),
604 DRIVER_MODULE(at45d, spibus, at45d_driver, at45d_devclass, NULL, NULL);
605 MODULE_DEPEND(at45d, spibus, 1, 1, 1);
607 MODULE_DEPEND(at45d, fdt_slicer, 1, 1, 1);
608 SPIBUS_FDT_PNP_INFO(compat_data);