2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Oleksandr Tymoshenko. All rights reserved.
5 * Copyright (c) 2018 Ian Lepore. 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 "opt_platform.h"
34 #include <sys/param.h>
35 #include <sys/systm.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>
49 #include <dev/fdt/fdt_common.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 #include <dev/ofw/openfirm.h>
54 #include <dev/spibus/spi.h>
55 #include "spibus_if.h"
57 #include <dev/flash/mx25lreg.h>
60 #define FL_ERASE_4K 0x01
61 #define FL_ERASE_32K 0x02
62 #define FL_ENABLE_4B_ADDR 0x04
63 #define FL_DISABLE_4B_ADDR 0x08
66 * Define the sectorsize to be a smaller size rather than the flash
67 * sector size. Trying to run FFS off of a 64k flash sector size
68 * results in a completely un-usable system.
70 #define MX25L_SECTORSIZE 512
72 struct mx25l_flash_ident
75 uint8_t manufacturer_id;
77 unsigned int sectorsize;
78 unsigned int sectorcount;
86 uint8_t sc_manufacturer_id;
87 uint16_t sc_device_id;
88 unsigned int sc_erasesize;
92 struct bio_queue_head sc_bio_queue;
93 unsigned int sc_flags;
94 unsigned int sc_taskstate;
95 uint8_t sc_dummybuf[FLASH_PAGE_SIZE];
98 #define TSTATE_STOPPED 0
99 #define TSTATE_STOPPING 1
100 #define TSTATE_RUNNING 2
102 #define M25PXX_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
103 #define M25PXX_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
104 #define M25PXX_LOCK_INIT(_sc) \
105 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
107 #define M25PXX_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
108 #define M25PXX_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
109 #define M25PXX_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
112 static int mx25l_open(struct disk *dp);
113 static int mx25l_close(struct disk *dp);
114 static int mx25l_ioctl(struct disk *, u_long, void *, int, struct thread *);
115 static void mx25l_strategy(struct bio *bp);
116 static int mx25l_getattr(struct bio *bp);
117 static void mx25l_task(void *arg);
119 static struct mx25l_flash_ident flash_devices[] = {
120 { "en25f32", 0x1c, 0x3116, 64 * 1024, 64, FL_NONE },
121 { "en25p32", 0x1c, 0x2016, 64 * 1024, 64, FL_NONE },
122 { "en25p64", 0x1c, 0x2017, 64 * 1024, 128, FL_NONE },
123 { "en25q32", 0x1c, 0x3016, 64 * 1024, 64, FL_NONE },
124 { "en25q64", 0x1c, 0x3017, 64 * 1024, 128, FL_ERASE_4K },
125 { "m25p32", 0x20, 0x2016, 64 * 1024, 64, FL_NONE },
126 { "m25p64", 0x20, 0x2017, 64 * 1024, 128, FL_NONE },
127 { "mx25l1606e", 0xc2, 0x2015, 64 * 1024, 32, FL_ERASE_4K},
128 { "mx25ll32", 0xc2, 0x2016, 64 * 1024, 64, FL_NONE },
129 { "mx25ll64", 0xc2, 0x2017, 64 * 1024, 128, FL_NONE },
130 { "mx25ll128", 0xc2, 0x2018, 64 * 1024, 256, FL_ERASE_4K | FL_ERASE_32K },
131 { "mx25ll256", 0xc2, 0x2019, 64 * 1024, 512, FL_ERASE_4K | FL_ERASE_32K | FL_ENABLE_4B_ADDR },
132 { "s25fl032", 0x01, 0x0215, 64 * 1024, 64, FL_NONE },
133 { "s25fl064", 0x01, 0x0216, 64 * 1024, 128, FL_NONE },
134 { "s25fl128", 0x01, 0x2018, 64 * 1024, 256, FL_NONE },
135 { "s25fl256s", 0x01, 0x0219, 64 * 1024, 512, FL_NONE },
136 { "s25fl512s", 0x01, 0x0220, 64 * 1024, 1024, FL_NONE },
137 { "SST25VF010A", 0xbf, 0x2549, 4 * 1024, 32, FL_ERASE_4K | FL_ERASE_32K },
138 { "SST25VF032B", 0xbf, 0x254a, 64 * 1024, 64, FL_ERASE_4K | FL_ERASE_32K },
140 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
141 { "w25x32", 0xef, 0x3016, 64 * 1024, 64, FL_ERASE_4K },
142 { "w25x64", 0xef, 0x3017, 64 * 1024, 128, FL_ERASE_4K },
143 { "w25q32", 0xef, 0x4016, 64 * 1024, 64, FL_ERASE_4K },
144 { "w25q64", 0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
145 { "w25q64bv", 0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
146 { "w25q128", 0xef, 0x4018, 64 * 1024, 256, FL_ERASE_4K },
147 { "w25q256", 0xef, 0x4019, 64 * 1024, 512, FL_ERASE_4K },
150 { "at25df641", 0x1f, 0x4800, 64 * 1024, 128, FL_ERASE_4K },
153 { "gd25q64", 0xc8, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
154 { "gd25q128", 0xc8, 0x4018, 64 * 1024, 256, FL_ERASE_4K },
156 /* Integrated Silicon Solution */
157 { "is25wp256", 0x9d, 0x7019, 64 * 1024, 512, FL_ERASE_4K | FL_ENABLE_4B_ADDR},
161 mx25l_wait_for_device_ready(struct mx25l_softc *sc)
163 uint8_t txBuf[2], rxBuf[2];
164 struct spi_command cmd;
167 memset(&cmd, 0, sizeof(cmd));
170 txBuf[0] = CMD_READ_STATUS;
175 err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
176 } while (err == 0 && (rxBuf[1] & STATUS_WIP));
181 static struct mx25l_flash_ident*
182 mx25l_get_device_ident(struct mx25l_softc *sc)
184 uint8_t txBuf[8], rxBuf[8];
185 struct spi_command cmd;
186 uint8_t manufacturer_id;
190 memset(&cmd, 0, sizeof(cmd));
191 memset(txBuf, 0, sizeof(txBuf));
192 memset(rxBuf, 0, sizeof(rxBuf));
194 txBuf[0] = CMD_READ_IDENT;
198 * Some compatible devices has extended two-bytes ID
199 * We'll use only manufacturer/deviceid atm
203 err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
207 manufacturer_id = rxBuf[1];
208 dev_id = (rxBuf[2] << 8) | (rxBuf[3]);
210 for (i = 0; i < nitems(flash_devices); i++) {
211 if ((flash_devices[i].manufacturer_id == manufacturer_id) &&
212 (flash_devices[i].device_id == dev_id))
213 return &flash_devices[i];
216 device_printf(sc->sc_dev,
217 "Unknown SPI flash device. Vendor: %02x, device id: %04x\n",
218 manufacturer_id, dev_id);
223 mx25l_set_writable(struct mx25l_softc *sc, int writable)
225 uint8_t txBuf[1], rxBuf[1];
226 struct spi_command cmd;
229 memset(&cmd, 0, sizeof(cmd));
230 memset(txBuf, 0, sizeof(txBuf));
231 memset(rxBuf, 0, sizeof(rxBuf));
233 txBuf[0] = writable ? CMD_WRITE_ENABLE : CMD_WRITE_DISABLE;
238 err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
243 mx25l_erase_cmd(struct mx25l_softc *sc, off_t sector)
245 uint8_t txBuf[5], rxBuf[5];
246 struct spi_command cmd;
249 if ((err = mx25l_set_writable(sc, 1)) != 0)
252 memset(&cmd, 0, sizeof(cmd));
253 memset(txBuf, 0, sizeof(txBuf));
254 memset(rxBuf, 0, sizeof(rxBuf));
259 if (sc->sc_flags & FL_ERASE_4K)
260 txBuf[0] = CMD_BLOCK_4K_ERASE;
261 else if (sc->sc_flags & FL_ERASE_32K)
262 txBuf[0] = CMD_BLOCK_32K_ERASE;
264 txBuf[0] = CMD_SECTOR_ERASE;
266 if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
269 txBuf[1] = ((sector >> 24) & 0xff);
270 txBuf[2] = ((sector >> 16) & 0xff);
271 txBuf[3] = ((sector >> 8) & 0xff);
272 txBuf[4] = (sector & 0xff);
276 txBuf[1] = ((sector >> 16) & 0xff);
277 txBuf[2] = ((sector >> 8) & 0xff);
278 txBuf[3] = (sector & 0xff);
280 if ((err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd)) != 0)
282 err = mx25l_wait_for_device_ready(sc);
287 mx25l_write(struct mx25l_softc *sc, off_t offset, caddr_t data, off_t count)
289 uint8_t txBuf[8], rxBuf[8];
290 struct spi_command cmd;
291 off_t bytes_to_write;
294 if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
303 * Writes must be aligned to the erase sectorsize, since blocks are
304 * fully erased before they're written to.
306 if (count % sc->sc_erasesize != 0 || offset % sc->sc_erasesize != 0)
310 * Maximum write size for CMD_PAGE_PROGRAM is FLASH_PAGE_SIZE, so loop
311 * to write chunks of FLASH_PAGE_SIZE bytes each.
314 /* If we crossed a sector boundary, erase the next sector. */
315 if (((offset) % sc->sc_erasesize) == 0) {
316 err = mx25l_erase_cmd(sc, offset);
321 txBuf[0] = CMD_PAGE_PROGRAM;
322 if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
323 txBuf[1] = (offset >> 24) & 0xff;
324 txBuf[2] = (offset >> 16) & 0xff;
325 txBuf[3] = (offset >> 8) & 0xff;
326 txBuf[4] = offset & 0xff;
328 txBuf[1] = (offset >> 16) & 0xff;
329 txBuf[2] = (offset >> 8) & 0xff;
330 txBuf[3] = offset & 0xff;
333 bytes_to_write = MIN(FLASH_PAGE_SIZE, count);
337 cmd.rx_data = sc->sc_dummybuf;
338 cmd.tx_data_sz = (uint32_t)bytes_to_write;
339 cmd.rx_data_sz = (uint32_t)bytes_to_write;
342 * Each completed write operation resets WEL (write enable
343 * latch) to disabled state, so we re-enable it here.
345 if ((err = mx25l_wait_for_device_ready(sc)) != 0)
347 if ((err = mx25l_set_writable(sc, 1)) != 0)
350 err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
353 err = mx25l_wait_for_device_ready(sc);
357 data += bytes_to_write;
358 offset += bytes_to_write;
359 count -= bytes_to_write;
366 mx25l_read(struct mx25l_softc *sc, off_t offset, caddr_t data, off_t count)
368 uint8_t txBuf[8], rxBuf[8];
369 struct spi_command cmd;
373 * Enforce that reads are aligned to the disk sectorsize, not the
374 * erase sectorsize. In this way, smaller read IO is possible,
375 * dramatically speeding up filesystem/geom_compress access.
377 if (count % sc->sc_disk->d_sectorsize != 0 ||
378 offset % sc->sc_disk->d_sectorsize != 0)
381 txBuf[0] = CMD_FAST_READ;
382 if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
386 txBuf[1] = (offset >> 24) & 0xff;
387 txBuf[2] = (offset >> 16) & 0xff;
388 txBuf[3] = (offset >> 8) & 0xff;
389 txBuf[4] = offset & 0xff;
396 txBuf[1] = (offset >> 16) & 0xff;
397 txBuf[2] = (offset >> 8) & 0xff;
398 txBuf[3] = offset & 0xff;
407 cmd.tx_data_sz = count;
408 cmd.rx_data_sz = count;
410 err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd);
415 mx25l_set_4b_mode(struct mx25l_softc *sc, uint8_t command)
417 uint8_t txBuf[1], rxBuf[1];
418 struct spi_command cmd;
421 memset(&cmd, 0, sizeof(cmd));
422 memset(txBuf, 0, sizeof(txBuf));
423 memset(rxBuf, 0, sizeof(rxBuf));
425 cmd.tx_cmd_sz = cmd.rx_cmd_sz = 1;
432 if ((err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd)) == 0)
433 err = mx25l_wait_for_device_ready(sc);
439 static struct ofw_compat_data compat_data[] = {
441 { "jedec,spi-nor", 1 },
447 mx25l_probe(device_t dev)
452 if (!ofw_bus_status_okay(dev))
455 /* First try to match the compatible property to the compat_data */
456 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 1)
460 * Next, try to find a compatible device using the names in the
461 * flash_devices structure
463 for (i = 0; i < nitems(flash_devices); i++)
464 if (ofw_bus_is_compatible(dev, flash_devices[i].name))
470 device_set_desc(dev, "M25Pxx Flash Family");
476 mx25l_attach(device_t dev)
478 struct mx25l_softc *sc;
479 struct mx25l_flash_ident *ident;
482 sc = device_get_softc(dev);
484 sc->sc_parent = device_get_parent(sc->sc_dev);
486 M25PXX_LOCK_INIT(sc);
488 ident = mx25l_get_device_ident(sc);
492 if ((err = mx25l_wait_for_device_ready(sc)) != 0)
495 sc->sc_flags = ident->flags;
497 if (sc->sc_flags & FL_ERASE_4K)
498 sc->sc_erasesize = 4 * 1024;
499 else if (sc->sc_flags & FL_ERASE_32K)
500 sc->sc_erasesize = 32 * 1024;
502 sc->sc_erasesize = ident->sectorsize;
504 if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
505 if ((err = mx25l_set_4b_mode(sc, CMD_ENTER_4B_MODE)) != 0)
507 } else if (sc->sc_flags & FL_DISABLE_4B_ADDR) {
508 if ((err = mx25l_set_4b_mode(sc, CMD_EXIT_4B_MODE)) != 0)
512 sc->sc_disk = disk_alloc();
513 sc->sc_disk->d_open = mx25l_open;
514 sc->sc_disk->d_close = mx25l_close;
515 sc->sc_disk->d_strategy = mx25l_strategy;
516 sc->sc_disk->d_getattr = mx25l_getattr;
517 sc->sc_disk->d_ioctl = mx25l_ioctl;
518 sc->sc_disk->d_name = "flash/spi";
519 sc->sc_disk->d_drv1 = sc;
520 sc->sc_disk->d_maxsize = DFLTPHYS;
521 sc->sc_disk->d_sectorsize = MX25L_SECTORSIZE;
522 sc->sc_disk->d_mediasize = ident->sectorsize * ident->sectorcount;
523 sc->sc_disk->d_stripesize = sc->sc_erasesize;
524 sc->sc_disk->d_unit = device_get_unit(sc->sc_dev);
525 sc->sc_disk->d_dump = NULL; /* NB: no dumps */
526 strlcpy(sc->sc_disk->d_descr, ident->name,
527 sizeof(sc->sc_disk->d_descr));
529 disk_create(sc->sc_disk, DISK_VERSION);
530 bioq_init(&sc->sc_bio_queue);
532 kproc_create(&mx25l_task, sc, &sc->sc_p, 0, 0, "task: mx25l flash");
533 sc->sc_taskstate = TSTATE_RUNNING;
535 device_printf(sc->sc_dev,
536 "device type %s, size %dK in %d sectors of %dK, erase size %dK\n",
538 ident->sectorcount * ident->sectorsize / 1024,
539 ident->sectorcount, ident->sectorsize / 1024,
540 sc->sc_erasesize / 1024);
546 mx25l_detach(device_t dev)
548 struct mx25l_softc *sc;
551 sc = device_get_softc(dev);
555 if (sc->sc_taskstate == TSTATE_RUNNING) {
556 sc->sc_taskstate = TSTATE_STOPPING;
558 while (err == 0 && sc->sc_taskstate != TSTATE_STOPPED) {
559 err = msleep(sc, &sc->sc_mtx, 0, "mx25dt", hz * 3);
561 sc->sc_taskstate = TSTATE_RUNNING;
562 device_printf(sc->sc_dev,
563 "Failed to stop queue task\n");
569 if (err == 0 && sc->sc_taskstate == TSTATE_STOPPED) {
570 disk_destroy(sc->sc_disk);
571 bioq_flush(&sc->sc_bio_queue, NULL, ENXIO);
572 M25PXX_LOCK_DESTROY(sc);
578 mx25l_open(struct disk *dp)
584 mx25l_close(struct disk *dp)
591 mx25l_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
599 mx25l_strategy(struct bio *bp)
601 struct mx25l_softc *sc;
603 sc = (struct mx25l_softc *)bp->bio_disk->d_drv1;
605 bioq_disksort(&sc->sc_bio_queue, bp);
611 mx25l_getattr(struct bio *bp)
613 struct mx25l_softc *sc;
616 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
619 sc = bp->bio_disk->d_drv1;
622 if (strcmp(bp->bio_attribute, "SPI::device") == 0) {
623 if (bp->bio_length != sizeof(dev))
625 bcopy(&dev, bp->bio_data, sizeof(dev));
632 mx25l_task(void *arg)
634 struct mx25l_softc *sc = (struct mx25l_softc*)arg;
642 if (sc->sc_taskstate == TSTATE_STOPPING) {
643 sc->sc_taskstate = TSTATE_STOPPED;
648 bp = bioq_first(&sc->sc_bio_queue);
650 msleep(sc, &sc->sc_mtx, PRIBIO, "mx25jq", 0);
651 } while (bp == NULL);
652 bioq_remove(&sc->sc_bio_queue, bp);
655 switch (bp->bio_cmd) {
657 bp->bio_error = mx25l_read(sc, bp->bio_offset,
658 bp->bio_data, bp->bio_bcount);
661 bp->bio_error = mx25l_write(sc, bp->bio_offset,
662 bp->bio_data, bp->bio_bcount);
665 bp->bio_error = EOPNOTSUPP;
673 static devclass_t mx25l_devclass;
675 static device_method_t mx25l_methods[] = {
676 /* Device interface */
677 DEVMETHOD(device_probe, mx25l_probe),
678 DEVMETHOD(device_attach, mx25l_attach),
679 DEVMETHOD(device_detach, mx25l_detach),
684 static driver_t mx25l_driver = {
687 sizeof(struct mx25l_softc),
690 DRIVER_MODULE(mx25l, spibus, mx25l_driver, mx25l_devclass, 0, 0);
691 MODULE_DEPEND(mx25l, spibus, 1, 1, 1);
693 MODULE_DEPEND(mx25l, fdt_slicer, 1, 1, 1);
694 SPIBUS_FDT_PNP_INFO(compat_data);