2 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
3 * Copyright (c) 2009 Oleksandr Tymoshenko. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include "opt_platform.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <geom/geom_disk.h>
46 #include <dev/fdt/fdt_common.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 #include <dev/ofw/openfirm.h>
51 #include <dev/spibus/spi.h>
52 #include "spibus_if.h"
54 #include <dev/flash/mx25lreg.h>
57 #define FL_ERASE_4K 0x01
58 #define FL_ERASE_32K 0x02
59 #define FL_ENABLE_4B_ADDR 0x04
60 #define FL_DISABLE_4B_ADDR 0x08
63 * Define the sectorsize to be a smaller size rather than the flash
64 * sector size. Trying to run FFS off of a 64k flash sector size
65 * results in a completely un-usable system.
67 #define MX25L_SECTORSIZE 512
69 struct mx25l_flash_ident
72 uint8_t manufacturer_id;
74 unsigned int sectorsize;
75 unsigned int sectorcount;
82 uint8_t sc_manufacturer_id;
83 uint16_t sc_device_id;
84 unsigned int sc_sectorsize;
88 struct bio_queue_head sc_bio_queue;
89 unsigned int sc_flags;
92 #define M25PXX_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
93 #define M25PXX_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
94 #define M25PXX_LOCK_INIT(_sc) \
95 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
97 #define M25PXX_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
98 #define M25PXX_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
99 #define M25PXX_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
102 static int mx25l_open(struct disk *dp);
103 static int mx25l_close(struct disk *dp);
104 static int mx25l_ioctl(struct disk *, u_long, void *, int, struct thread *);
105 static void mx25l_strategy(struct bio *bp);
106 static int mx25l_getattr(struct bio *bp);
107 static void mx25l_task(void *arg);
109 struct mx25l_flash_ident flash_devices[] = {
110 { "en25f32", 0x1c, 0x3116, 64 * 1024, 64, FL_NONE },
111 { "en25p32", 0x1c, 0x2016, 64 * 1024, 64, FL_NONE },
112 { "en25p64", 0x1c, 0x2017, 64 * 1024, 128, FL_NONE },
113 { "en25q32", 0x1c, 0x3016, 64 * 1024, 64, FL_NONE },
114 { "en25q64", 0x1c, 0x3017, 64 * 1024, 128, FL_ERASE_4K },
115 { "m25p32", 0x20, 0x2016, 64 * 1024, 64, FL_NONE },
116 { "m25p64", 0x20, 0x2017, 64 * 1024, 128, FL_NONE },
117 { "mx25ll32", 0xc2, 0x2016, 64 * 1024, 64, FL_NONE },
118 { "mx25ll64", 0xc2, 0x2017, 64 * 1024, 128, FL_NONE },
119 { "mx25ll128", 0xc2, 0x2018, 64 * 1024, 256, FL_ERASE_4K | FL_ERASE_32K },
120 { "mx25ll256", 0xc2, 0x2019, 64 * 1024, 512, FL_ERASE_4K | FL_ERASE_32K | FL_ENABLE_4B_ADDR },
121 { "s25fl032", 0x01, 0x0215, 64 * 1024, 64, FL_NONE },
122 { "s25fl064", 0x01, 0x0216, 64 * 1024, 128, FL_NONE },
123 { "s25fl128", 0x01, 0x2018, 64 * 1024, 256, FL_NONE },
124 { "s25fl256s", 0x01, 0x0219, 64 * 1024, 512, FL_NONE },
125 { "SST25VF032B", 0xbf, 0x254a, 64 * 1024, 64, FL_ERASE_4K | FL_ERASE_32K },
127 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
128 { "w25x32", 0xef, 0x3016, 64 * 1024, 64, FL_ERASE_4K },
129 { "w25x64", 0xef, 0x3017, 64 * 1024, 128, FL_ERASE_4K },
130 { "w25q32", 0xef, 0x4016, 64 * 1024, 64, FL_ERASE_4K },
131 { "w25q64", 0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
132 { "w25q64bv", 0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
133 { "w25q128", 0xef, 0x4018, 64 * 1024, 256, FL_ERASE_4K },
134 { "w25q256", 0xef, 0x4019, 64 * 1024, 512, FL_ERASE_4K },
137 { "at25df641", 0x1f, 0x4800, 64 * 1024, 128, FL_ERASE_4K },
140 { "gd25q64", 0xc8, 0x4017, 64 * 1024, 128, FL_ERASE_4K },
144 mx25l_get_status(device_t dev)
146 uint8_t txBuf[2], rxBuf[2];
147 struct spi_command cmd;
150 memset(&cmd, 0, sizeof(cmd));
151 memset(txBuf, 0, sizeof(txBuf));
152 memset(rxBuf, 0, sizeof(rxBuf));
154 txBuf[0] = CMD_READ_STATUS;
159 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
164 mx25l_wait_for_device_ready(device_t dev)
166 while ((mx25l_get_status(dev) & STATUS_WIP))
170 static struct mx25l_flash_ident*
171 mx25l_get_device_ident(struct mx25l_softc *sc)
173 device_t dev = sc->sc_dev;
174 uint8_t txBuf[8], rxBuf[8];
175 struct spi_command cmd;
176 uint8_t manufacturer_id;
180 memset(&cmd, 0, sizeof(cmd));
181 memset(txBuf, 0, sizeof(txBuf));
182 memset(rxBuf, 0, sizeof(rxBuf));
184 txBuf[0] = CMD_READ_IDENT;
188 * Some compatible devices has extended two-bytes ID
189 * We'll use only manufacturer/deviceid atm
193 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
197 manufacturer_id = rxBuf[1];
198 dev_id = (rxBuf[2] << 8) | (rxBuf[3]);
201 i < nitems(flash_devices); i++) {
202 if ((flash_devices[i].manufacturer_id == manufacturer_id) &&
203 (flash_devices[i].device_id == dev_id))
204 return &flash_devices[i];
207 printf("Unknown SPI flash device. Vendor: %02x, device id: %04x\n",
208 manufacturer_id, dev_id);
213 mx25l_set_writable(device_t dev, int writable)
215 uint8_t txBuf[1], rxBuf[1];
216 struct spi_command cmd;
219 memset(&cmd, 0, sizeof(cmd));
220 memset(txBuf, 0, sizeof(txBuf));
221 memset(rxBuf, 0, sizeof(rxBuf));
223 txBuf[0] = writable ? CMD_WRITE_ENABLE : CMD_WRITE_DISABLE;
228 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
232 mx25l_erase_cmd(device_t dev, off_t sector, uint8_t ecmd)
234 struct mx25l_softc *sc;
235 uint8_t txBuf[5], rxBuf[5];
236 struct spi_command cmd;
239 sc = device_get_softc(dev);
241 mx25l_wait_for_device_ready(dev);
242 mx25l_set_writable(dev, 1);
244 memset(&cmd, 0, sizeof(cmd));
245 memset(txBuf, 0, sizeof(txBuf));
246 memset(rxBuf, 0, sizeof(rxBuf));
251 if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
254 txBuf[1] = ((sector >> 24) & 0xff);
255 txBuf[2] = ((sector >> 16) & 0xff);
256 txBuf[3] = ((sector >> 8) & 0xff);
257 txBuf[4] = (sector & 0xff);
261 txBuf[1] = ((sector >> 16) & 0xff);
262 txBuf[2] = ((sector >> 8) & 0xff);
263 txBuf[3] = (sector & 0xff);
265 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
269 mx25l_write(device_t dev, off_t offset, caddr_t data, off_t count)
271 struct mx25l_softc *sc;
272 uint8_t txBuf[8], rxBuf[8];
273 struct spi_command cmd;
275 long bytes_to_write, bytes_writen;
279 pdev = device_get_parent(dev);
280 sc = device_get_softc(dev);
282 if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
291 write_offset = offset;
294 * Use the erase sectorsize here since blocks are fully erased
295 * first before they're written to.
297 if (count % sc->sc_sectorsize != 0 || offset % sc->sc_sectorsize != 0)
301 * Assume here that we write per-sector only
302 * and sector size should be 256 bytes aligned
304 KASSERT(write_offset % FLASH_PAGE_SIZE == 0,
305 ("offset for BIO_WRITE is not page size (%d bytes) aligned",
309 * Maximum write size for CMD_PAGE_PROGRAM is
310 * FLASH_PAGE_SIZE, so split data to chunks
311 * FLASH_PAGE_SIZE bytes eash and write them
314 while (bytes_writen < count) {
316 * If we crossed sector boundary - erase next sector
318 if (((offset + bytes_writen) % sc->sc_sectorsize) == 0)
319 mx25l_erase_cmd(dev, offset + bytes_writen, CMD_SECTOR_ERASE);
321 txBuf[0] = CMD_PAGE_PROGRAM;
322 if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
323 txBuf[1] = ((write_offset >> 24) & 0xff);
324 txBuf[2] = ((write_offset >> 16) & 0xff);
325 txBuf[3] = ((write_offset >> 8) & 0xff);
326 txBuf[4] = (write_offset & 0xff);
328 txBuf[1] = ((write_offset >> 16) & 0xff);
329 txBuf[2] = ((write_offset >> 8) & 0xff);
330 txBuf[3] = (write_offset & 0xff);
333 bytes_to_write = MIN(FLASH_PAGE_SIZE,
334 count - bytes_writen);
337 cmd.tx_data = data + bytes_writen;
338 cmd.tx_data_sz = bytes_to_write;
339 cmd.rx_data = data + bytes_writen;
340 cmd.rx_data_sz = bytes_to_write;
343 * Eash completed write operation resets WEL
344 * (write enable latch) to disabled state,
345 * so we re-enable it here
347 mx25l_wait_for_device_ready(dev);
348 mx25l_set_writable(dev, 1);
350 err = SPIBUS_TRANSFER(pdev, dev, &cmd);
354 bytes_writen += bytes_to_write;
355 write_offset += bytes_to_write;
362 mx25l_read(device_t dev, off_t offset, caddr_t data, off_t count)
364 struct mx25l_softc *sc;
365 uint8_t txBuf[8], rxBuf[8];
366 struct spi_command cmd;
370 pdev = device_get_parent(dev);
371 sc = device_get_softc(dev);
374 * Enforce the disk read sectorsize not the erase sectorsize.
375 * In this way, smaller read IO is possible,dramatically
376 * speeding up filesystem/geom_compress access.
378 if (count % sc->sc_disk->d_sectorsize != 0
379 || offset % sc->sc_disk->d_sectorsize != 0)
382 txBuf[0] = CMD_FAST_READ;
383 if (sc->sc_flags & FL_ENABLE_4B_ADDR) {
387 txBuf[1] = ((offset >> 24) & 0xff);
388 txBuf[2] = ((offset >> 16) & 0xff);
389 txBuf[3] = ((offset >> 8) & 0xff);
390 txBuf[4] = (offset & 0xff);
397 txBuf[1] = ((offset >> 16) & 0xff);
398 txBuf[2] = ((offset >> 8) & 0xff);
399 txBuf[3] = (offset & 0xff);
407 cmd.tx_data_sz = count;
409 cmd.rx_data_sz = count;
411 err = SPIBUS_TRANSFER(pdev, dev, &cmd);
417 mx25l_set_4b_mode(device_t dev, uint8_t command)
419 uint8_t txBuf[1], rxBuf[1];
420 struct spi_command cmd;
424 memset(&cmd, 0, sizeof(cmd));
425 memset(txBuf, 0, sizeof(txBuf));
426 memset(rxBuf, 0, sizeof(rxBuf));
428 pdev = device_get_parent(dev);
430 cmd.tx_cmd_sz = cmd.rx_cmd_sz = 1;
437 err = SPIBUS_TRANSFER(pdev, dev, &cmd);
439 mx25l_wait_for_device_ready(dev);
445 static struct ofw_compat_data compat_data[] = {
447 { "jedec,spi-nor", 1 },
453 mx25l_probe(device_t dev)
458 if (!ofw_bus_status_okay(dev))
461 /* First try to match the compatible property to the compat_data */
462 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 1)
466 * Next, try to find a compatible device using the names in the
467 * flash_devices structure
469 for (i = 0; i < nitems(flash_devices); i++)
470 if (ofw_bus_is_compatible(dev, flash_devices[i].name))
476 device_set_desc(dev, "M25Pxx Flash Family");
482 mx25l_attach(device_t dev)
484 struct mx25l_softc *sc;
485 struct mx25l_flash_ident *ident;
487 sc = device_get_softc(dev);
489 M25PXX_LOCK_INIT(sc);
491 ident = mx25l_get_device_ident(sc);
495 mx25l_wait_for_device_ready(sc->sc_dev);
497 sc->sc_disk = disk_alloc();
498 sc->sc_disk->d_open = mx25l_open;
499 sc->sc_disk->d_close = mx25l_close;
500 sc->sc_disk->d_strategy = mx25l_strategy;
501 sc->sc_disk->d_getattr = mx25l_getattr;
502 sc->sc_disk->d_ioctl = mx25l_ioctl;
503 sc->sc_disk->d_name = "flash/spi";
504 sc->sc_disk->d_drv1 = sc;
505 sc->sc_disk->d_maxsize = DFLTPHYS;
506 sc->sc_disk->d_sectorsize = MX25L_SECTORSIZE;
507 sc->sc_disk->d_mediasize = ident->sectorsize * ident->sectorcount;
508 sc->sc_disk->d_unit = device_get_unit(sc->sc_dev);
509 sc->sc_disk->d_dump = NULL; /* NB: no dumps */
510 /* Sectorsize for erase operations */
511 sc->sc_sectorsize = ident->sectorsize;
512 sc->sc_flags = ident->flags;
514 if (sc->sc_flags & FL_ENABLE_4B_ADDR)
515 mx25l_set_4b_mode(dev, CMD_ENTER_4B_MODE);
517 if (sc->sc_flags & FL_DISABLE_4B_ADDR)
518 mx25l_set_4b_mode(dev, CMD_EXIT_4B_MODE);
520 /* NB: use stripesize to hold the erase/region size for RedBoot */
521 sc->sc_disk->d_stripesize = ident->sectorsize;
523 disk_create(sc->sc_disk, DISK_VERSION);
524 bioq_init(&sc->sc_bio_queue);
526 kproc_create(&mx25l_task, sc, &sc->sc_p, 0, 0, "task: mx25l flash");
527 device_printf(sc->sc_dev, "%s, sector %d bytes, %d sectors\n",
528 ident->name, ident->sectorsize, ident->sectorcount);
534 mx25l_detach(device_t dev)
541 mx25l_open(struct disk *dp)
547 mx25l_close(struct disk *dp)
554 mx25l_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
562 mx25l_strategy(struct bio *bp)
564 struct mx25l_softc *sc;
566 sc = (struct mx25l_softc *)bp->bio_disk->d_drv1;
568 bioq_disksort(&sc->sc_bio_queue, bp);
574 mx25l_getattr(struct bio *bp)
576 struct mx25l_softc *sc;
579 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
582 sc = bp->bio_disk->d_drv1;
585 if (strcmp(bp->bio_attribute, "SPI::device") == 0) {
586 if (bp->bio_length != sizeof(dev))
588 bcopy(&dev, bp->bio_data, sizeof(dev));
595 mx25l_task(void *arg)
597 struct mx25l_softc *sc = (struct mx25l_softc*)arg;
605 bp = bioq_first(&sc->sc_bio_queue);
607 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
608 } while (bp == NULL);
609 bioq_remove(&sc->sc_bio_queue, bp);
612 switch (bp->bio_cmd) {
614 bp->bio_error = mx25l_read(dev, bp->bio_offset,
615 bp->bio_data, bp->bio_bcount);
618 bp->bio_error = mx25l_write(dev, bp->bio_offset,
619 bp->bio_data, bp->bio_bcount);
622 bp->bio_error = EINVAL;
630 static devclass_t mx25l_devclass;
632 static device_method_t mx25l_methods[] = {
633 /* Device interface */
634 DEVMETHOD(device_probe, mx25l_probe),
635 DEVMETHOD(device_attach, mx25l_attach),
636 DEVMETHOD(device_detach, mx25l_detach),
641 static driver_t mx25l_driver = {
644 sizeof(struct mx25l_softc),
647 DRIVER_MODULE(mx25l, spibus, mx25l_driver, mx25l_devclass, 0, 0);