2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 M. Warner Losh
5 * Copyright (c) 2011-2012 Ian Lepore
6 * Copyright (c) 2012 Marius Strobl <marius@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/kthread.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <geom/geom_disk.h>
48 #include <dev/spibus/spi.h>
49 #include "spibus_if.h"
51 #include "opt_platform.h"
54 #include <dev/fdt/fdt_common.h>
55 #include <dev/ofw/ofw_bus_subr.h>
56 #include <dev/ofw/openfirm.h>
58 static struct ofw_compat_data compat_data[] = {
60 { "atmel,dataflash", 1 },
65 /* This is the information returned by the MANUFACTURER_ID command. */
66 struct at45d_mfg_info {
67 uint32_t jedec_id; /* Mfg ID, DevId1, DevId2, ExtLen */
68 uint16_t ext_id; /* ExtId1, ExtId2 */
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.
75 struct at45d_flash_ident
89 struct bio_queue_head bio_queue;
101 #define TSTATE_STOPPED 0
102 #define TSTATE_STOPPING 1
103 #define TSTATE_RUNNING 2
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), \
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);
114 /* bus entry points */
115 static device_attach_t at45d_attach;
116 static device_detach_t at45d_detach;
117 static device_probe_t at45d_probe;
120 static int at45d_close(struct disk *dp);
121 static int at45d_open(struct disk *dp);
122 static int at45d_getattr(struct bio *bp);
123 static void at45d_strategy(struct bio *bp);
124 static void at45d_task(void *arg);
126 /* helper routines */
127 static void at45d_delayed_attach(void *xsc);
128 static int at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp);
129 static int at45d_get_status(device_t dev, uint8_t *status);
130 static int at45d_wait_ready(device_t dev, uint8_t *status);
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
139 #define STATUS_READY (1u << 7)
140 #define STATUS_CMPFAIL (1u << 6)
141 #define STATUS_PAGE2N (1u << 0)
144 * Metadata for supported chips.
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.
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.
156 static 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 },
170 at45d_get_status(device_t dev, uint8_t *status)
172 uint8_t rxBuf[8], txBuf[8];
173 struct spi_command cmd;
176 memset(&cmd, 0, sizeof(cmd));
177 memset(txBuf, 0, sizeof(txBuf));
178 memset(rxBuf, 0, sizeof(rxBuf));
180 txBuf[0] = STATUS_REGISTER_READ;
183 cmd.rx_cmd_sz = cmd.tx_cmd_sz = 2;
184 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
190 at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp)
192 uint8_t rxBuf[8], txBuf[8];
193 struct spi_command cmd;
196 memset(&cmd, 0, sizeof(cmd));
197 memset(txBuf, 0, sizeof(txBuf));
198 memset(rxBuf, 0, sizeof(rxBuf));
200 txBuf[0] = MANUFACTURER_ID;
203 cmd.tx_cmd_sz = cmd.rx_cmd_sz = 7;
204 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
208 resp->jedec_id = be32dec(rxBuf + 1);
209 resp->ext_id = be16dec(rxBuf + 5);
215 at45d_wait_ready(device_t dev, uint8_t *status)
217 struct timeval now, tout;
220 getmicrouptime(&tout);
223 getmicrouptime(&now);
224 if (now.tv_sec > tout.tv_sec)
227 err = at45d_get_status(dev, status);
228 } while (err == 0 && !(*status & STATUS_READY));
233 at45d_probe(device_t dev)
238 if (!ofw_bus_status_okay(dev))
241 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
244 rv = BUS_PROBE_DEFAULT;
246 rv = BUS_PROBE_NOWILDCARD;
249 device_set_desc(dev, "AT45D Flash Family");
254 at45d_attach(device_t dev)
256 struct at45d_softc *sc;
258 sc = device_get_softc(dev);
262 config_intrhook_oneshot(at45d_delayed_attach, sc);
267 at45d_detach(device_t dev)
269 struct at45d_softc *sc;
272 sc = device_get_softc(dev);
276 if (sc->taskstate == TSTATE_RUNNING) {
277 sc->taskstate = TSTATE_STOPPING;
279 while (err == 0 && sc->taskstate != TSTATE_STOPPED) {
280 err = msleep(sc, &sc->sc_mtx, 0, "at45dt", hz * 3);
282 sc->taskstate = TSTATE_RUNNING;
283 device_printf(sc->dev,
284 "Failed to stop queue task\n");
290 if (err == 0 && sc->taskstate == TSTATE_STOPPED) {
292 disk_destroy(sc->disk);
293 bioq_flush(&sc->bio_queue, NULL, ENXIO);
294 free(sc->dummybuf, M_DEVBUF);
296 AT45D_LOCK_DESTROY(sc);
302 at45d_delayed_attach(void *xsc)
304 struct at45d_softc *sc;
305 struct at45d_mfg_info mfginfo;
306 const struct at45d_flash_ident *ident;
317 if (at45d_wait_ready(sc->dev, &status) != 0) {
318 device_printf(sc->dev, "Error waiting for device-ready.\n");
321 if (at45d_get_mfg_info(sc->dev, &mfginfo) != 0) {
322 device_printf(sc->dev, "Failed to get ID.\n");
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) {
332 if (i == nitems(at45d_flash_devices)) {
333 device_printf(sc->dev, "JEDEC 0x%x not in list.\n", jedec);
337 sc->pagecount = ident->pagecount;
338 sc->pageoffset = ident->pageoffset;
339 if (ident->pagesize2n != 0 && (status & STATUS_PAGE2N)) {
341 pagesize = ident->pagesize2n;
343 pagesize = ident->pagesize;
344 sc->pagesize = pagesize;
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).
352 sectorsize = pagesize;
356 if (OF_getencprop(ofw_bus_get_node(sc->dev),
357 "freebsd,sectorsize", &size, sizeof(size)) > 0)
361 resource_int_value(device_get_name(sc->dev), device_get_unit(sc->dev),
362 "sectorsize", §orsize);
364 if ((sectorsize % pagesize) != 0) {
365 device_printf(sc->dev, "Invalid sectorsize %d, "
366 "must be a multiple of %d\n", sectorsize, pagesize);
370 sc->dummybuf = malloc(pagesize, M_DEVBUF, M_WAITOK | M_ZERO);
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);
394 at45d_open(struct disk *dp)
401 at45d_close(struct disk *dp)
408 at45d_getattr(struct bio *bp)
410 struct at45d_softc *sc;
413 * This function exists to support geom_flashmap and fdt_slicer.
416 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
418 if (strcmp(bp->bio_attribute, "SPI::device") != 0)
420 sc = bp->bio_disk->d_drv1;
421 if (bp->bio_length != sizeof(sc->dev))
423 bcopy(&sc->dev, bp->bio_data, sizeof(sc->dev));
428 at45d_strategy(struct bio *bp)
430 struct at45d_softc *sc;
432 sc = (struct at45d_softc *)bp->bio_disk->d_drv1;
434 bioq_disksort(&sc->bio_queue, bp);
440 at45d_task(void *arg)
442 uint8_t rxBuf[8], txBuf[8];
443 struct at45d_softc *sc;
445 struct spi_command cmd;
449 u_int addr, berr, err, offset, page;
452 sc = (struct at45d_softc*)arg;
454 pdev = device_get_parent(dev);
455 memset(&cmd, 0, sizeof(cmd));
456 memset(txBuf, 0, sizeof(txBuf));
457 memset(rxBuf, 0, sizeof(rxBuf));
464 if (sc->taskstate == TSTATE_STOPPING) {
465 sc->taskstate = TSTATE_STOPPED;
470 bp = bioq_takefirst(&sc->bio_queue);
472 msleep(sc, &sc->sc_mtx, PRIBIO, "at45dq", 0);
473 } while (bp == NULL);
478 len = resid = bp->bio_bcount;
479 page = bp->bio_offset / sc->pagesize;
480 offset = bp->bio_offset % sc->pagesize;
482 switch (bp->bio_cmd) {
484 txBuf[0] = CONTINUOUS_ARRAY_READ;
485 cmd.tx_cmd_sz = cmd.rx_cmd_sz = 8;
486 cmd.tx_data = sc->dummybuf;
490 cmd.tx_cmd_sz = cmd.rx_cmd_sz = 4;
492 cmd.rx_data = sc->dummybuf;
493 if (resid + offset > sc->pagesize)
494 len = sc->pagesize - offset;
502 * NB: for BIO_READ, this loop is only traversed once.
505 if (page > sc->pagecount) {
509 addr = page << sc->pageoffset;
510 if (bp->bio_cmd == BIO_WRITE) {
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.
517 if (len != sc->pagesize) {
518 txBuf[0] = PAGE_TO_BUFFER_TRANSFER;
519 txBuf[1] = ((addr >> 16) & 0xff);
520 txBuf[2] = ((addr >> 8) & 0xff);
522 cmd.tx_data_sz = cmd.rx_data_sz = 0;
523 err = SPIBUS_TRANSFER(pdev, dev, &cmd);
525 err = at45d_wait_ready(dev,
532 txBuf[0] = PROGRAM_THROUGH_BUFFER;
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);
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);
553 cmd.tx_data_sz = cmd.rx_data_sz = 0;
554 err = SPIBUS_TRANSFER(pdev, dev, &cmd);
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,
569 if (resid > sc->pagesize)
573 if (bp->bio_cmd == BIO_READ)
580 bp->bio_flags |= BIO_ERROR;
581 bp->bio_error = berr;
583 bp->bio_resid = resid;
588 static devclass_t at45d_devclass;
590 static 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),
599 static driver_t at45d_driver = {
602 sizeof(struct at45d_softc),
605 DRIVER_MODULE(at45d, spibus, at45d_driver, at45d_devclass, NULL, NULL);
606 MODULE_DEPEND(at45d, spibus, 1, 1, 1);
608 MODULE_DEPEND(at45d, fdt_slicer, 1, 1, 1);
609 SPIBUS_FDT_PNP_INFO(compat_data);