2 * Copyright (c) 2006 M. Warner Losh
3 * Copyright (c) 2011-2012 Ian Lepore
4 * Copyright (c) 2012 Marius Strobl <marius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <geom/geom_disk.h>
46 #include <dev/spibus/spi.h>
47 #include "spibus_if.h"
49 #include "opt_platform.h"
52 #include <dev/fdt/fdt_common.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 #include <dev/ofw/openfirm.h>
56 static struct ofw_compat_data compat_data[] = {
58 { "atmel,dataflash", 1 },
63 /* This is the information returned by the MANUFACTURER_ID command. */
64 struct at45d_mfg_info {
65 uint32_t jedec_id; /* Mfg ID, DevId1, DevId2, ExtLen */
66 uint16_t ext_id; /* ExtId1, ExtId2 */
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.
73 struct at45d_flash_ident
87 struct bio_queue_head bio_queue;
99 #define TSTATE_STOPPED 0
100 #define TSTATE_STOPPING 1
101 #define TSTATE_RUNNING 2
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), \
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);
112 /* bus entry points */
113 static device_attach_t at45d_attach;
114 static device_detach_t at45d_detach;
115 static device_probe_t at45d_probe;
118 static int at45d_close(struct disk *dp);
119 static int at45d_open(struct disk *dp);
120 static int at45d_getattr(struct bio *bp);
121 static void at45d_strategy(struct bio *bp);
122 static void at45d_task(void *arg);
124 /* helper routines */
125 static void at45d_delayed_attach(void *xsc);
126 static int at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp);
127 static int at45d_get_status(device_t dev, uint8_t *status);
128 static int at45d_wait_ready(device_t dev, uint8_t *status);
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
137 #define STATUS_READY (1u << 7)
138 #define STATUS_CMPFAIL (1u << 6)
139 #define STATUS_PAGE2N (1u << 0)
142 * Metadata for supported chips.
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.
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.
154 static 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 },
168 at45d_get_status(device_t dev, uint8_t *status)
170 uint8_t rxBuf[8], txBuf[8];
171 struct spi_command cmd;
174 memset(&cmd, 0, sizeof(cmd));
175 memset(txBuf, 0, sizeof(txBuf));
176 memset(rxBuf, 0, sizeof(rxBuf));
178 txBuf[0] = STATUS_REGISTER_READ;
181 cmd.rx_cmd_sz = cmd.tx_cmd_sz = 2;
182 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
188 at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp)
190 uint8_t rxBuf[8], txBuf[8];
191 struct spi_command cmd;
194 memset(&cmd, 0, sizeof(cmd));
195 memset(txBuf, 0, sizeof(txBuf));
196 memset(rxBuf, 0, sizeof(rxBuf));
198 txBuf[0] = MANUFACTURER_ID;
201 cmd.tx_cmd_sz = cmd.rx_cmd_sz = 7;
202 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
206 resp->jedec_id = be32dec(rxBuf + 1);
207 resp->ext_id = be16dec(rxBuf + 5);
213 at45d_wait_ready(device_t dev, uint8_t *status)
215 struct timeval now, tout;
218 getmicrouptime(&tout);
221 getmicrouptime(&now);
222 if (now.tv_sec > tout.tv_sec)
225 err = at45d_get_status(dev, status);
226 } while (err == 0 && !(*status & STATUS_READY));
231 at45d_probe(device_t dev)
236 if (!ofw_bus_status_okay(dev))
239 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
242 rv = BUS_PROBE_DEFAULT;
244 rv = BUS_PROBE_NOWILDCARD;
247 device_set_desc(dev, "AT45D Flash Family");
252 at45d_attach(device_t dev)
254 struct at45d_softc *sc;
256 sc = device_get_softc(dev);
260 config_intrhook_oneshot(at45d_delayed_attach, sc);
265 at45d_detach(device_t dev)
267 struct at45d_softc *sc;
270 sc = device_get_softc(dev);
274 if (sc->taskstate == TSTATE_RUNNING) {
275 sc->taskstate = TSTATE_STOPPING;
277 while (err == 0 && sc->taskstate != TSTATE_STOPPED) {
278 err = msleep(sc, &sc->sc_mtx, 0, "at45dt", hz * 3);
280 sc->taskstate = TSTATE_RUNNING;
281 device_printf(sc->dev,
282 "Failed to stop queue task\n");
288 if (err == 0 && sc->taskstate == TSTATE_STOPPED) {
290 disk_destroy(sc->disk);
291 bioq_flush(&sc->bio_queue, NULL, ENXIO);
292 free(sc->dummybuf, M_DEVBUF);
294 AT45D_LOCK_DESTROY(sc);
300 at45d_delayed_attach(void *xsc)
302 struct at45d_softc *sc;
303 struct at45d_mfg_info mfginfo;
304 const struct at45d_flash_ident *ident;
315 if (at45d_wait_ready(sc->dev, &status) != 0) {
316 device_printf(sc->dev, "Error waiting for device-ready.\n");
319 if (at45d_get_mfg_info(sc->dev, &mfginfo) != 0) {
320 device_printf(sc->dev, "Failed to get ID.\n");
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) {
330 if (i == nitems(at45d_flash_devices)) {
331 device_printf(sc->dev, "JEDEC 0x%x not in list.\n", jedec);
335 sc->pagecount = ident->pagecount;
336 sc->pageoffset = ident->pageoffset;
337 if (ident->pagesize2n != 0 && (status & STATUS_PAGE2N)) {
339 pagesize = ident->pagesize2n;
341 pagesize = ident->pagesize;
342 sc->pagesize = pagesize;
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).
350 sectorsize = pagesize;
354 if (OF_getencprop(ofw_bus_get_node(sc->dev),
355 "freebsd,sectorsize", &size, sizeof(size)) > 0)
359 resource_int_value(device_get_name(sc->dev), device_get_unit(sc->dev),
360 "sectorsize", §orsize);
362 if ((sectorsize % pagesize) != 0) {
363 device_printf(sc->dev, "Invalid sectorsize %d, "
364 "must be a multiple of %d\n", sectorsize, pagesize);
368 sc->dummybuf = malloc(pagesize, M_DEVBUF, M_WAITOK | M_ZERO);
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);
392 at45d_open(struct disk *dp)
399 at45d_close(struct disk *dp)
406 at45d_getattr(struct bio *bp)
408 struct at45d_softc *sc;
411 * This function exists to support geom_flashmap and fdt_slicer.
414 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
416 if (strcmp(bp->bio_attribute, "SPI::device") != 0)
418 sc = bp->bio_disk->d_drv1;
419 if (bp->bio_length != sizeof(sc->dev))
421 bcopy(&sc->dev, bp->bio_data, sizeof(sc->dev));
426 at45d_strategy(struct bio *bp)
428 struct at45d_softc *sc;
430 sc = (struct at45d_softc *)bp->bio_disk->d_drv1;
432 bioq_disksort(&sc->bio_queue, bp);
438 at45d_task(void *arg)
440 uint8_t rxBuf[8], txBuf[8];
441 struct at45d_softc *sc;
443 struct spi_command cmd;
447 u_int addr, berr, err, offset, page;
450 sc = (struct at45d_softc*)arg;
452 pdev = device_get_parent(dev);
453 memset(&cmd, 0, sizeof(cmd));
454 memset(txBuf, 0, sizeof(txBuf));
455 memset(rxBuf, 0, sizeof(rxBuf));
462 if (sc->taskstate == TSTATE_STOPPING) {
463 sc->taskstate = TSTATE_STOPPED;
468 bp = bioq_takefirst(&sc->bio_queue);
470 msleep(sc, &sc->sc_mtx, PRIBIO, "at45dq", 0);
471 } while (bp == NULL);
476 len = resid = bp->bio_bcount;
477 page = bp->bio_offset / sc->pagesize;
478 offset = bp->bio_offset % sc->pagesize;
480 switch (bp->bio_cmd) {
482 txBuf[0] = CONTINUOUS_ARRAY_READ;
483 cmd.tx_cmd_sz = cmd.rx_cmd_sz = 8;
484 cmd.tx_data = sc->dummybuf;
488 cmd.tx_cmd_sz = cmd.rx_cmd_sz = 4;
490 cmd.rx_data = sc->dummybuf;
491 if (resid + offset > sc->pagesize)
492 len = sc->pagesize - offset;
500 * NB: for BIO_READ, this loop is only traversed once.
503 if (page > sc->pagecount) {
507 addr = page << sc->pageoffset;
508 if (bp->bio_cmd == BIO_WRITE) {
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.
515 if (len != sc->pagesize) {
516 txBuf[0] = PAGE_TO_BUFFER_TRANSFER;
517 txBuf[1] = ((addr >> 16) & 0xff);
518 txBuf[2] = ((addr >> 8) & 0xff);
520 cmd.tx_data_sz = cmd.rx_data_sz = 0;
521 err = SPIBUS_TRANSFER(pdev, dev, &cmd);
523 err = at45d_wait_ready(dev,
530 txBuf[0] = PROGRAM_THROUGH_BUFFER;
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);
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);
551 cmd.tx_data_sz = cmd.rx_data_sz = 0;
552 err = SPIBUS_TRANSFER(pdev, dev, &cmd);
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,
567 if (resid > sc->pagesize)
571 if (bp->bio_cmd == BIO_READ)
578 bp->bio_flags |= BIO_ERROR;
579 bp->bio_error = berr;
581 bp->bio_resid = resid;
586 static devclass_t at45d_devclass;
588 static 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),
597 static driver_t at45d_driver = {
600 sizeof(struct at45d_softc),
603 DRIVER_MODULE(at45d, spibus, at45d_driver, at45d_devclass, NULL, NULL);
604 MODULE_DEPEND(at45d, spibus, 1, 1, 1);
606 MODULE_DEPEND(at45d, fdt_slicer, 1, 1, 1);