Index: head/sys/dev/flash/at45d.c =================================================================== --- head/sys/dev/flash/at45d.c (revision 344980) +++ head/sys/dev/flash/at45d.c (revision 344981) @@ -1,612 +1,611 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2006 M. Warner Losh * Copyright (c) 2011-2012 Ian Lepore * Copyright (c) 2012 Marius Strobl * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "spibus_if.h" #include "opt_platform.h" #ifdef FDT #include #include #include static struct ofw_compat_data compat_data[] = { { "atmel,at45", 1 }, { "atmel,dataflash", 1 }, { NULL, 0 }, }; #endif /* This is the information returned by the MANUFACTURER_ID command. */ struct at45d_mfg_info { uint32_t jedec_id; /* Mfg ID, DevId1, DevId2, ExtLen */ uint16_t ext_id; /* ExtId1, ExtId2 */ }; /* * This is an entry in our table of metadata describing the chips. We match on * both jedec id and extended id info returned by the MANUFACTURER_ID command. */ struct at45d_flash_ident { const char *name; uint32_t jedec; uint16_t extid; uint16_t extmask; uint16_t pagecount; uint16_t pageoffset; uint16_t pagesize; uint16_t pagesize2n; }; struct at45d_softc { struct bio_queue_head bio_queue; struct mtx sc_mtx; struct disk *disk; struct proc *p; device_t dev; u_int taskstate; uint16_t pagecount; uint16_t pageoffset; uint16_t pagesize; void *dummybuf; }; #define TSTATE_STOPPED 0 #define TSTATE_STOPPING 1 #define TSTATE_RUNNING 2 #define AT45D_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) #define AT45D_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) #define AT45D_LOCK_INIT(_sc) \ mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ "at45d", MTX_DEF) #define AT45D_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); #define AT45D_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); #define AT45D_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); /* bus entry points */ static device_attach_t at45d_attach; static device_detach_t at45d_detach; static device_probe_t at45d_probe; /* disk routines */ static int at45d_close(struct disk *dp); static int at45d_open(struct disk *dp); static int at45d_getattr(struct bio *bp); static void at45d_strategy(struct bio *bp); static void at45d_task(void *arg); /* helper routines */ static void at45d_delayed_attach(void *xsc); static int at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp); static int at45d_get_status(device_t dev, uint8_t *status); static int at45d_wait_ready(device_t dev, uint8_t *status); #define PAGE_TO_BUFFER_TRANSFER 0x53 #define PAGE_TO_BUFFER_COMPARE 0x60 #define PROGRAM_THROUGH_BUFFER 0x82 #define MANUFACTURER_ID 0x9f #define STATUS_REGISTER_READ 0xd7 #define CONTINUOUS_ARRAY_READ 0xe8 #define STATUS_READY (1u << 7) #define STATUS_CMPFAIL (1u << 6) #define STATUS_PAGE2N (1u << 0) /* * Metadata for supported chips. * * The jedec id in this table includes the extended id length byte. A match is * based on both jedec id and extended id matching. The chip's extended id (not * present in most chips) is ANDed with ExtMask and the result is compared to * ExtId. If a chip only returns 1 ext id byte it will be in the upper 8 bits * of ExtId in this table. * * A sectorsize2n != 0 is used to indicate that a device optionally supports * 2^N byte pages. If support for the latter is enabled, the sector offset * has to be reduced by one. */ static const struct at45d_flash_ident at45d_flash_devices[] = { /* Part Name Jedec ID ExtId ExtMask PgCnt Offs PgSz PgSz2n */ { "AT45DB011B", 0x1f220000, 0x0000, 0x0000, 512, 9, 264, 256 }, { "AT45DB021B", 0x1f230000, 0x0000, 0x0000, 1024, 9, 264, 256 }, { "AT45DB041x", 0x1f240000, 0x0000, 0x0000, 2028, 9, 264, 256 }, { "AT45DB081B", 0x1f250000, 0x0000, 0x0000, 4096, 9, 264, 256 }, { "AT45DB161x", 0x1f260000, 0x0000, 0x0000, 4096, 10, 528, 512 }, { "AT45DB321x", 0x1f270000, 0x0000, 0x0000, 8192, 10, 528, 0 }, { "AT45DB321x", 0x1f270100, 0x0000, 0x0000, 8192, 10, 528, 512 }, { "AT45DB641E", 0x1f280001, 0x0000, 0xff00, 32768, 9, 264, 256 }, { "AT45DB642x", 0x1f280000, 0x0000, 0x0000, 8192, 11, 1056, 1024 }, }; static int at45d_get_status(device_t dev, uint8_t *status) { uint8_t rxBuf[8], txBuf[8]; struct spi_command cmd; int err; memset(&cmd, 0, sizeof(cmd)); memset(txBuf, 0, sizeof(txBuf)); memset(rxBuf, 0, sizeof(rxBuf)); txBuf[0] = STATUS_REGISTER_READ; cmd.tx_cmd = txBuf; cmd.rx_cmd = rxBuf; cmd.rx_cmd_sz = cmd.tx_cmd_sz = 2; err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd); *status = rxBuf[1]; return (err); } static int at45d_get_mfg_info(device_t dev, struct at45d_mfg_info *resp) { uint8_t rxBuf[8], txBuf[8]; struct spi_command cmd; int err; memset(&cmd, 0, sizeof(cmd)); memset(txBuf, 0, sizeof(txBuf)); memset(rxBuf, 0, sizeof(rxBuf)); txBuf[0] = MANUFACTURER_ID; cmd.tx_cmd = &txBuf; cmd.rx_cmd = &rxBuf; cmd.tx_cmd_sz = cmd.rx_cmd_sz = 7; err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd); if (err) return (err); resp->jedec_id = be32dec(rxBuf + 1); resp->ext_id = be16dec(rxBuf + 5); return (0); } static int at45d_wait_ready(device_t dev, uint8_t *status) { struct timeval now, tout; int err; getmicrouptime(&tout); tout.tv_sec += 3; do { getmicrouptime(&now); if (now.tv_sec > tout.tv_sec) err = ETIMEDOUT; else err = at45d_get_status(dev, status); } while (err == 0 && !(*status & STATUS_READY)); return (err); } static int at45d_probe(device_t dev) { int rv; #ifdef FDT if (!ofw_bus_status_okay(dev)) return (ENXIO); if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) return (ENXIO); rv = BUS_PROBE_DEFAULT; #else rv = BUS_PROBE_NOWILDCARD; #endif device_set_desc(dev, "AT45D Flash Family"); return (rv); } static int at45d_attach(device_t dev) { struct at45d_softc *sc; sc = device_get_softc(dev); sc->dev = dev; AT45D_LOCK_INIT(sc); config_intrhook_oneshot(at45d_delayed_attach, sc); return (0); } static int at45d_detach(device_t dev) { struct at45d_softc *sc; int err; sc = device_get_softc(dev); err = 0; AT45D_LOCK(sc); if (sc->taskstate == TSTATE_RUNNING) { sc->taskstate = TSTATE_STOPPING; wakeup(sc); while (err == 0 && sc->taskstate != TSTATE_STOPPED) { err = msleep(sc, &sc->sc_mtx, 0, "at45dt", hz * 3); if (err != 0) { sc->taskstate = TSTATE_RUNNING; device_printf(sc->dev, "Failed to stop queue task\n"); } } } AT45D_UNLOCK(sc); if (err == 0 && sc->taskstate == TSTATE_STOPPED) { if (sc->disk) { disk_destroy(sc->disk); bioq_flush(&sc->bio_queue, NULL, ENXIO); free(sc->dummybuf, M_DEVBUF); } AT45D_LOCK_DESTROY(sc); } return (err); } static void at45d_delayed_attach(void *xsc) { struct at45d_softc *sc; struct at45d_mfg_info mfginfo; const struct at45d_flash_ident *ident; u_int i; int sectorsize; uint32_t jedec; uint16_t pagesize; uint8_t status; sc = xsc; ident = NULL; jedec = 0; if (at45d_wait_ready(sc->dev, &status) != 0) { device_printf(sc->dev, "Error waiting for device-ready.\n"); return; } if (at45d_get_mfg_info(sc->dev, &mfginfo) != 0) { device_printf(sc->dev, "Failed to get ID.\n"); return; } for (i = 0; i < nitems(at45d_flash_devices); i++) { ident = &at45d_flash_devices[i]; if (mfginfo.jedec_id == ident->jedec && (mfginfo.ext_id & ident->extmask) == ident->extid) { break; } } if (i == nitems(at45d_flash_devices)) { device_printf(sc->dev, "JEDEC 0x%x not in list.\n", jedec); return; } sc->pagecount = ident->pagecount; sc->pageoffset = ident->pageoffset; if (ident->pagesize2n != 0 && (status & STATUS_PAGE2N)) { sc->pageoffset -= 1; pagesize = ident->pagesize2n; } else pagesize = ident->pagesize; sc->pagesize = pagesize; /* * By default we set up a disk with a sector size that matches the * device page size. If there is a device hint or fdt property * requesting a different size, use that, as long as it is a multiple of * the device page size). */ sectorsize = pagesize; #ifdef FDT { pcell_t size; if (OF_getencprop(ofw_bus_get_node(sc->dev), "freebsd,sectorsize", &size, sizeof(size)) > 0) sectorsize = size; } #endif resource_int_value(device_get_name(sc->dev), device_get_unit(sc->dev), "sectorsize", §orsize); if ((sectorsize % pagesize) != 0) { device_printf(sc->dev, "Invalid sectorsize %d, " "must be a multiple of %d\n", sectorsize, pagesize); return; } sc->dummybuf = malloc(pagesize, M_DEVBUF, M_WAITOK | M_ZERO); sc->disk = disk_alloc(); sc->disk->d_open = at45d_open; sc->disk->d_close = at45d_close; sc->disk->d_strategy = at45d_strategy; sc->disk->d_getattr = at45d_getattr; sc->disk->d_name = "flash/at45d"; sc->disk->d_drv1 = sc; sc->disk->d_maxsize = DFLTPHYS; sc->disk->d_sectorsize = sectorsize; sc->disk->d_mediasize = pagesize * ident->pagecount; sc->disk->d_unit = device_get_unit(sc->dev); disk_create(sc->disk, DISK_VERSION); - disk_add_alias(sc->disk, "flash/spi"); bioq_init(&sc->bio_queue); kproc_create(&at45d_task, sc, &sc->p, 0, 0, "task: at45d flash"); sc->taskstate = TSTATE_RUNNING; device_printf(sc->dev, "%s, %d bytes per page, %d pages; %d KBytes; disk sector size %d\n", ident->name, pagesize, ident->pagecount, (pagesize * ident->pagecount) / 1024, sectorsize); } static int at45d_open(struct disk *dp) { return (0); } static int at45d_close(struct disk *dp) { return (0); } static int at45d_getattr(struct bio *bp) { struct at45d_softc *sc; /* * This function exists to support geom_flashmap and fdt_slicer. */ if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL) return (ENXIO); if (strcmp(bp->bio_attribute, "SPI::device") != 0) return (-1); sc = bp->bio_disk->d_drv1; if (bp->bio_length != sizeof(sc->dev)) return (EFAULT); bcopy(&sc->dev, bp->bio_data, sizeof(sc->dev)); return (0); } static void at45d_strategy(struct bio *bp) { struct at45d_softc *sc; sc = (struct at45d_softc *)bp->bio_disk->d_drv1; AT45D_LOCK(sc); bioq_disksort(&sc->bio_queue, bp); wakeup(sc); AT45D_UNLOCK(sc); } static void at45d_task(void *arg) { uint8_t rxBuf[8], txBuf[8]; struct at45d_softc *sc; struct bio *bp; struct spi_command cmd; device_t dev, pdev; caddr_t buf; u_long len, resid; u_int addr, berr, err, offset, page; uint8_t status; sc = (struct at45d_softc*)arg; dev = sc->dev; pdev = device_get_parent(dev); memset(&cmd, 0, sizeof(cmd)); memset(txBuf, 0, sizeof(txBuf)); memset(rxBuf, 0, sizeof(rxBuf)); cmd.tx_cmd = txBuf; cmd.rx_cmd = rxBuf; for (;;) { AT45D_LOCK(sc); do { if (sc->taskstate == TSTATE_STOPPING) { sc->taskstate = TSTATE_STOPPED; AT45D_UNLOCK(sc); wakeup(sc); kproc_exit(0); } bp = bioq_takefirst(&sc->bio_queue); if (bp == NULL) msleep(sc, &sc->sc_mtx, PRIBIO, "at45dq", 0); } while (bp == NULL); AT45D_UNLOCK(sc); berr = 0; buf = bp->bio_data; len = resid = bp->bio_bcount; page = bp->bio_offset / sc->pagesize; offset = bp->bio_offset % sc->pagesize; switch (bp->bio_cmd) { case BIO_READ: txBuf[0] = CONTINUOUS_ARRAY_READ; cmd.tx_cmd_sz = cmd.rx_cmd_sz = 8; cmd.tx_data = sc->dummybuf; cmd.rx_data = buf; break; case BIO_WRITE: cmd.tx_cmd_sz = cmd.rx_cmd_sz = 4; cmd.tx_data = buf; cmd.rx_data = sc->dummybuf; if (resid + offset > sc->pagesize) len = sc->pagesize - offset; break; default: berr = EINVAL; goto out; } /* * NB: for BIO_READ, this loop is only traversed once. */ while (resid > 0) { if (page > sc->pagecount) { berr = EINVAL; goto out; } addr = page << sc->pageoffset; if (bp->bio_cmd == BIO_WRITE) { /* * If writing less than a full page, transfer * the existing page to the buffer, so that our * PROGRAM_THROUGH_BUFFER below will preserve * the parts of the page we're not writing. */ if (len != sc->pagesize) { txBuf[0] = PAGE_TO_BUFFER_TRANSFER; txBuf[1] = ((addr >> 16) & 0xff); txBuf[2] = ((addr >> 8) & 0xff); txBuf[3] = 0; cmd.tx_data_sz = cmd.rx_data_sz = 0; err = SPIBUS_TRANSFER(pdev, dev, &cmd); if (err == 0) err = at45d_wait_ready(dev, &status); if (err != 0) { berr = EIO; goto out; } } txBuf[0] = PROGRAM_THROUGH_BUFFER; } addr += offset; txBuf[1] = ((addr >> 16) & 0xff); txBuf[2] = ((addr >> 8) & 0xff); txBuf[3] = (addr & 0xff); cmd.tx_data_sz = cmd.rx_data_sz = len; err = SPIBUS_TRANSFER(pdev, dev, &cmd); if (err == 0 && bp->bio_cmd != BIO_READ) err = at45d_wait_ready(dev, &status); if (err != 0) { berr = EIO; goto out; } if (bp->bio_cmd == BIO_WRITE) { addr = page << sc->pageoffset; txBuf[0] = PAGE_TO_BUFFER_COMPARE; txBuf[1] = ((addr >> 16) & 0xff); txBuf[2] = ((addr >> 8) & 0xff); txBuf[3] = 0; cmd.tx_data_sz = cmd.rx_data_sz = 0; err = SPIBUS_TRANSFER(pdev, dev, &cmd); if (err == 0) err = at45d_wait_ready(dev, &status); if (err != 0 || (status & STATUS_CMPFAIL)) { device_printf(dev, "comparing page " "%d failed (status=0x%x)\n", page, status); berr = EIO; goto out; } } page++; buf += len; offset = 0; resid -= len; if (resid > sc->pagesize) len = sc->pagesize; else len = resid; if (bp->bio_cmd == BIO_READ) cmd.rx_data = buf; else cmd.tx_data = buf; } out: if (berr != 0) { bp->bio_flags |= BIO_ERROR; bp->bio_error = berr; } bp->bio_resid = resid; biodone(bp); } } static devclass_t at45d_devclass; static device_method_t at45d_methods[] = { /* Device interface */ DEVMETHOD(device_probe, at45d_probe), DEVMETHOD(device_attach, at45d_attach), DEVMETHOD(device_detach, at45d_detach), DEVMETHOD_END }; static driver_t at45d_driver = { "at45d", at45d_methods, sizeof(struct at45d_softc), }; DRIVER_MODULE(at45d, spibus, at45d_driver, at45d_devclass, NULL, NULL); MODULE_DEPEND(at45d, spibus, 1, 1, 1); #ifdef FDT MODULE_DEPEND(at45d, fdt_slicer, 1, 1, 1); SPIBUS_PNP_INFO(compat_data); #endif Index: head/sys/dev/flash/mx25l.c =================================================================== --- head/sys/dev/flash/mx25l.c (revision 344980) +++ head/sys/dev/flash/mx25l.c (revision 344981) @@ -1,691 +1,690 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2006 M. Warner Losh. * Copyright (c) 2009 Oleksandr Tymoshenko. All rights reserved. * Copyright (c) 2018 Ian Lepore. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "opt_platform.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef FDT #include #include #include #endif #include #include "spibus_if.h" #include #define FL_NONE 0x00 #define FL_ERASE_4K 0x01 #define FL_ERASE_32K 0x02 #define FL_ENABLE_4B_ADDR 0x04 #define FL_DISABLE_4B_ADDR 0x08 /* * Define the sectorsize to be a smaller size rather than the flash * sector size. Trying to run FFS off of a 64k flash sector size * results in a completely un-usable system. */ #define MX25L_SECTORSIZE 512 struct mx25l_flash_ident { const char *name; uint8_t manufacturer_id; uint16_t device_id; unsigned int sectorsize; unsigned int sectorcount; unsigned int flags; }; struct mx25l_softc { device_t sc_dev; device_t sc_parent; uint8_t sc_manufacturer_id; uint16_t sc_device_id; unsigned int sc_erasesize; struct mtx sc_mtx; struct disk *sc_disk; struct proc *sc_p; struct bio_queue_head sc_bio_queue; unsigned int sc_flags; unsigned int sc_taskstate; uint8_t sc_dummybuf[FLASH_PAGE_SIZE]; }; #define TSTATE_STOPPED 0 #define TSTATE_STOPPING 1 #define TSTATE_RUNNING 2 #define M25PXX_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) #define M25PXX_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) #define M25PXX_LOCK_INIT(_sc) \ mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \ "mx25l", MTX_DEF) #define M25PXX_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); #define M25PXX_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); #define M25PXX_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); /* disk routines */ static int mx25l_open(struct disk *dp); static int mx25l_close(struct disk *dp); static int mx25l_ioctl(struct disk *, u_long, void *, int, struct thread *); static void mx25l_strategy(struct bio *bp); static int mx25l_getattr(struct bio *bp); static void mx25l_task(void *arg); static struct mx25l_flash_ident flash_devices[] = { { "en25f32", 0x1c, 0x3116, 64 * 1024, 64, FL_NONE }, { "en25p32", 0x1c, 0x2016, 64 * 1024, 64, FL_NONE }, { "en25p64", 0x1c, 0x2017, 64 * 1024, 128, FL_NONE }, { "en25q32", 0x1c, 0x3016, 64 * 1024, 64, FL_NONE }, { "en25q64", 0x1c, 0x3017, 64 * 1024, 128, FL_ERASE_4K }, { "m25p32", 0x20, 0x2016, 64 * 1024, 64, FL_NONE }, { "m25p64", 0x20, 0x2017, 64 * 1024, 128, FL_NONE }, { "mx25l1606e", 0xc2, 0x2015, 64 * 1024, 32, FL_ERASE_4K}, { "mx25ll32", 0xc2, 0x2016, 64 * 1024, 64, FL_NONE }, { "mx25ll64", 0xc2, 0x2017, 64 * 1024, 128, FL_NONE }, { "mx25ll128", 0xc2, 0x2018, 64 * 1024, 256, FL_ERASE_4K | FL_ERASE_32K }, { "mx25ll256", 0xc2, 0x2019, 64 * 1024, 512, FL_ERASE_4K | FL_ERASE_32K | FL_ENABLE_4B_ADDR }, { "s25fl032", 0x01, 0x0215, 64 * 1024, 64, FL_NONE }, { "s25fl064", 0x01, 0x0216, 64 * 1024, 128, FL_NONE }, { "s25fl128", 0x01, 0x2018, 64 * 1024, 256, FL_NONE }, { "s25fl256s", 0x01, 0x0219, 64 * 1024, 512, FL_NONE }, { "SST25VF010A", 0xbf, 0x2549, 4 * 1024, 32, FL_ERASE_4K | FL_ERASE_32K }, { "SST25VF032B", 0xbf, 0x254a, 64 * 1024, 64, FL_ERASE_4K | FL_ERASE_32K }, /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */ { "w25x32", 0xef, 0x3016, 64 * 1024, 64, FL_ERASE_4K }, { "w25x64", 0xef, 0x3017, 64 * 1024, 128, FL_ERASE_4K }, { "w25q32", 0xef, 0x4016, 64 * 1024, 64, FL_ERASE_4K }, { "w25q64", 0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K }, { "w25q64bv", 0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K }, { "w25q128", 0xef, 0x4018, 64 * 1024, 256, FL_ERASE_4K }, { "w25q256", 0xef, 0x4019, 64 * 1024, 512, FL_ERASE_4K }, /* Atmel */ { "at25df641", 0x1f, 0x4800, 64 * 1024, 128, FL_ERASE_4K }, /* GigaDevice */ { "gd25q64", 0xc8, 0x4017, 64 * 1024, 128, FL_ERASE_4K }, }; static int mx25l_wait_for_device_ready(struct mx25l_softc *sc) { uint8_t txBuf[2], rxBuf[2]; struct spi_command cmd; int err; memset(&cmd, 0, sizeof(cmd)); do { txBuf[0] = CMD_READ_STATUS; cmd.tx_cmd = txBuf; cmd.rx_cmd = rxBuf; cmd.rx_cmd_sz = 2; cmd.tx_cmd_sz = 2; err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd); } while (err == 0 && (rxBuf[1] & STATUS_WIP)); return (err); } static struct mx25l_flash_ident* mx25l_get_device_ident(struct mx25l_softc *sc) { uint8_t txBuf[8], rxBuf[8]; struct spi_command cmd; uint8_t manufacturer_id; uint16_t dev_id; int err, i; memset(&cmd, 0, sizeof(cmd)); memset(txBuf, 0, sizeof(txBuf)); memset(rxBuf, 0, sizeof(rxBuf)); txBuf[0] = CMD_READ_IDENT; cmd.tx_cmd = &txBuf; cmd.rx_cmd = &rxBuf; /* * Some compatible devices has extended two-bytes ID * We'll use only manufacturer/deviceid atm */ cmd.tx_cmd_sz = 4; cmd.rx_cmd_sz = 4; err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd); if (err) return (NULL); manufacturer_id = rxBuf[1]; dev_id = (rxBuf[2] << 8) | (rxBuf[3]); for (i = 0; i < nitems(flash_devices); i++) { if ((flash_devices[i].manufacturer_id == manufacturer_id) && (flash_devices[i].device_id == dev_id)) return &flash_devices[i]; } device_printf(sc->sc_dev, "Unknown SPI flash device. Vendor: %02x, device id: %04x\n", manufacturer_id, dev_id); return (NULL); } static int mx25l_set_writable(struct mx25l_softc *sc, int writable) { uint8_t txBuf[1], rxBuf[1]; struct spi_command cmd; int err; memset(&cmd, 0, sizeof(cmd)); memset(txBuf, 0, sizeof(txBuf)); memset(rxBuf, 0, sizeof(rxBuf)); txBuf[0] = writable ? CMD_WRITE_ENABLE : CMD_WRITE_DISABLE; cmd.tx_cmd = txBuf; cmd.rx_cmd = rxBuf; cmd.rx_cmd_sz = 1; cmd.tx_cmd_sz = 1; err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd); return (err); } static int mx25l_erase_cmd(struct mx25l_softc *sc, off_t sector) { uint8_t txBuf[5], rxBuf[5]; struct spi_command cmd; int err; if ((err = mx25l_set_writable(sc, 1)) != 0) return (err); memset(&cmd, 0, sizeof(cmd)); memset(txBuf, 0, sizeof(txBuf)); memset(rxBuf, 0, sizeof(rxBuf)); cmd.tx_cmd = txBuf; cmd.rx_cmd = rxBuf; if (sc->sc_flags & FL_ERASE_4K) txBuf[0] = CMD_BLOCK_4K_ERASE; else if (sc->sc_flags & FL_ERASE_32K) txBuf[0] = CMD_BLOCK_32K_ERASE; else txBuf[0] = CMD_SECTOR_ERASE; if (sc->sc_flags & FL_ENABLE_4B_ADDR) { cmd.rx_cmd_sz = 5; cmd.tx_cmd_sz = 5; txBuf[1] = ((sector >> 24) & 0xff); txBuf[2] = ((sector >> 16) & 0xff); txBuf[3] = ((sector >> 8) & 0xff); txBuf[4] = (sector & 0xff); } else { cmd.rx_cmd_sz = 4; cmd.tx_cmd_sz = 4; txBuf[1] = ((sector >> 16) & 0xff); txBuf[2] = ((sector >> 8) & 0xff); txBuf[3] = (sector & 0xff); } if ((err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd)) != 0) return (err); err = mx25l_wait_for_device_ready(sc); return (err); } static int mx25l_write(struct mx25l_softc *sc, off_t offset, caddr_t data, off_t count) { uint8_t txBuf[8], rxBuf[8]; struct spi_command cmd; off_t bytes_to_write; int err = 0; if (sc->sc_flags & FL_ENABLE_4B_ADDR) { cmd.tx_cmd_sz = 5; cmd.rx_cmd_sz = 5; } else { cmd.tx_cmd_sz = 4; cmd.rx_cmd_sz = 4; } /* * Writes must be aligned to the erase sectorsize, since blocks are * fully erased before they're written to. */ if (count % sc->sc_erasesize != 0 || offset % sc->sc_erasesize != 0) return (EIO); /* * Maximum write size for CMD_PAGE_PROGRAM is FLASH_PAGE_SIZE, so loop * to write chunks of FLASH_PAGE_SIZE bytes each. */ while (count != 0) { /* If we crossed a sector boundary, erase the next sector. */ if (((offset) % sc->sc_erasesize) == 0) { err = mx25l_erase_cmd(sc, offset); if (err) break; } txBuf[0] = CMD_PAGE_PROGRAM; if (sc->sc_flags & FL_ENABLE_4B_ADDR) { txBuf[1] = (offset >> 24) & 0xff; txBuf[2] = (offset >> 16) & 0xff; txBuf[3] = (offset >> 8) & 0xff; txBuf[4] = offset & 0xff; } else { txBuf[1] = (offset >> 16) & 0xff; txBuf[2] = (offset >> 8) & 0xff; txBuf[3] = offset & 0xff; } bytes_to_write = MIN(FLASH_PAGE_SIZE, count); cmd.tx_cmd = txBuf; cmd.rx_cmd = rxBuf; cmd.tx_data = data; cmd.rx_data = sc->sc_dummybuf; cmd.tx_data_sz = (uint32_t)bytes_to_write; cmd.rx_data_sz = (uint32_t)bytes_to_write; /* * Each completed write operation resets WEL (write enable * latch) to disabled state, so we re-enable it here. */ if ((err = mx25l_wait_for_device_ready(sc)) != 0) break; if ((err = mx25l_set_writable(sc, 1)) != 0) break; err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd); if (err != 0) break; err = mx25l_wait_for_device_ready(sc); if (err) break; data += bytes_to_write; offset += bytes_to_write; count -= bytes_to_write; } return (err); } static int mx25l_read(struct mx25l_softc *sc, off_t offset, caddr_t data, off_t count) { uint8_t txBuf[8], rxBuf[8]; struct spi_command cmd; int err = 0; /* * Enforce that reads are aligned to the disk sectorsize, not the * erase sectorsize. In this way, smaller read IO is possible, * dramatically speeding up filesystem/geom_compress access. */ if (count % sc->sc_disk->d_sectorsize != 0 || offset % sc->sc_disk->d_sectorsize != 0) return (EIO); txBuf[0] = CMD_FAST_READ; if (sc->sc_flags & FL_ENABLE_4B_ADDR) { cmd.tx_cmd_sz = 6; cmd.rx_cmd_sz = 6; txBuf[1] = (offset >> 24) & 0xff; txBuf[2] = (offset >> 16) & 0xff; txBuf[3] = (offset >> 8) & 0xff; txBuf[4] = offset & 0xff; /* Dummy byte */ txBuf[5] = 0; } else { cmd.tx_cmd_sz = 5; cmd.rx_cmd_sz = 5; txBuf[1] = (offset >> 16) & 0xff; txBuf[2] = (offset >> 8) & 0xff; txBuf[3] = offset & 0xff; /* Dummy byte */ txBuf[4] = 0; } cmd.tx_cmd = txBuf; cmd.rx_cmd = rxBuf; cmd.tx_data = data; cmd.rx_data = data; cmd.tx_data_sz = count; cmd.rx_data_sz = count; err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd); return (err); } static int mx25l_set_4b_mode(struct mx25l_softc *sc, uint8_t command) { uint8_t txBuf[1], rxBuf[1]; struct spi_command cmd; int err; memset(&cmd, 0, sizeof(cmd)); memset(txBuf, 0, sizeof(txBuf)); memset(rxBuf, 0, sizeof(rxBuf)); cmd.tx_cmd_sz = cmd.rx_cmd_sz = 1; cmd.tx_cmd = txBuf; cmd.rx_cmd = rxBuf; txBuf[0] = command; if ((err = SPIBUS_TRANSFER(sc->sc_parent, sc->sc_dev, &cmd)) == 0) err = mx25l_wait_for_device_ready(sc); return (err); } #ifdef FDT static struct ofw_compat_data compat_data[] = { { "st,m25p", 1 }, { "jedec,spi-nor", 1 }, { NULL, 0 }, }; #endif static int mx25l_probe(device_t dev) { #ifdef FDT int i; if (!ofw_bus_status_okay(dev)) return (ENXIO); /* First try to match the compatible property to the compat_data */ if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 1) goto found; /* * Next, try to find a compatible device using the names in the * flash_devices structure */ for (i = 0; i < nitems(flash_devices); i++) if (ofw_bus_is_compatible(dev, flash_devices[i].name)) goto found; return (ENXIO); found: #endif device_set_desc(dev, "M25Pxx Flash Family"); return (0); } static int mx25l_attach(device_t dev) { struct mx25l_softc *sc; struct mx25l_flash_ident *ident; int err; sc = device_get_softc(dev); sc->sc_dev = dev; sc->sc_parent = device_get_parent(sc->sc_dev); M25PXX_LOCK_INIT(sc); ident = mx25l_get_device_ident(sc); if (ident == NULL) return (ENXIO); if ((err = mx25l_wait_for_device_ready(sc)) != 0) return (err); sc->sc_flags = ident->flags; if (sc->sc_flags & FL_ERASE_4K) sc->sc_erasesize = 4 * 1024; else if (sc->sc_flags & FL_ERASE_32K) sc->sc_erasesize = 32 * 1024; else sc->sc_erasesize = ident->sectorsize; if (sc->sc_flags & FL_ENABLE_4B_ADDR) { if ((err = mx25l_set_4b_mode(sc, CMD_ENTER_4B_MODE)) != 0) return (err); } else if (sc->sc_flags & FL_DISABLE_4B_ADDR) { if ((err = mx25l_set_4b_mode(sc, CMD_EXIT_4B_MODE)) != 0) return (err); } sc->sc_disk = disk_alloc(); sc->sc_disk->d_open = mx25l_open; sc->sc_disk->d_close = mx25l_close; sc->sc_disk->d_strategy = mx25l_strategy; sc->sc_disk->d_getattr = mx25l_getattr; sc->sc_disk->d_ioctl = mx25l_ioctl; - sc->sc_disk->d_name = "flash/mx25l"; + sc->sc_disk->d_name = "flash/spi"; sc->sc_disk->d_drv1 = sc; sc->sc_disk->d_maxsize = DFLTPHYS; sc->sc_disk->d_sectorsize = MX25L_SECTORSIZE; sc->sc_disk->d_mediasize = ident->sectorsize * ident->sectorcount; sc->sc_disk->d_stripesize = sc->sc_erasesize; sc->sc_disk->d_unit = device_get_unit(sc->sc_dev); sc->sc_disk->d_dump = NULL; /* NB: no dumps */ strlcpy(sc->sc_disk->d_descr, ident->name, sizeof(sc->sc_disk->d_descr)); disk_create(sc->sc_disk, DISK_VERSION); - disk_add_alias(sc->sc_disk, "flash/spi"); bioq_init(&sc->sc_bio_queue); kproc_create(&mx25l_task, sc, &sc->sc_p, 0, 0, "task: mx25l flash"); sc->sc_taskstate = TSTATE_RUNNING; device_printf(sc->sc_dev, "device type %s, size %dK in %d sectors of %dK, erase size %dK\n", ident->name, ident->sectorcount * ident->sectorsize / 1024, ident->sectorcount, ident->sectorsize / 1024, sc->sc_erasesize / 1024); return (0); } static int mx25l_detach(device_t dev) { struct mx25l_softc *sc; int err; sc = device_get_softc(dev); err = 0; M25PXX_LOCK(sc); if (sc->sc_taskstate == TSTATE_RUNNING) { sc->sc_taskstate = TSTATE_STOPPING; wakeup(sc); while (err == 0 && sc->sc_taskstate != TSTATE_STOPPED) { err = msleep(sc, &sc->sc_mtx, 0, "mx25dt", hz * 3); if (err != 0) { sc->sc_taskstate = TSTATE_RUNNING; device_printf(sc->sc_dev, "Failed to stop queue task\n"); } } } M25PXX_UNLOCK(sc); if (err == 0 && sc->sc_taskstate == TSTATE_STOPPED) { disk_destroy(sc->sc_disk); bioq_flush(&sc->sc_bio_queue, NULL, ENXIO); M25PXX_LOCK_DESTROY(sc); } return (err); } static int mx25l_open(struct disk *dp) { return (0); } static int mx25l_close(struct disk *dp) { return (0); } static int mx25l_ioctl(struct disk *dp, u_long cmd, void *data, int fflag, struct thread *td) { return (EINVAL); } static void mx25l_strategy(struct bio *bp) { struct mx25l_softc *sc; sc = (struct mx25l_softc *)bp->bio_disk->d_drv1; M25PXX_LOCK(sc); bioq_disksort(&sc->sc_bio_queue, bp); wakeup(sc); M25PXX_UNLOCK(sc); } static int mx25l_getattr(struct bio *bp) { struct mx25l_softc *sc; device_t dev; if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL) return (ENXIO); sc = bp->bio_disk->d_drv1; dev = sc->sc_dev; if (strcmp(bp->bio_attribute, "SPI::device") == 0) { if (bp->bio_length != sizeof(dev)) return (EFAULT); bcopy(&dev, bp->bio_data, sizeof(dev)); } else return (-1); return (0); } static void mx25l_task(void *arg) { struct mx25l_softc *sc = (struct mx25l_softc*)arg; struct bio *bp; device_t dev; for (;;) { dev = sc->sc_dev; M25PXX_LOCK(sc); do { if (sc->sc_taskstate == TSTATE_STOPPING) { sc->sc_taskstate = TSTATE_STOPPED; M25PXX_UNLOCK(sc); wakeup(sc); kproc_exit(0); } bp = bioq_first(&sc->sc_bio_queue); if (bp == NULL) msleep(sc, &sc->sc_mtx, PRIBIO, "mx25jq", 0); } while (bp == NULL); bioq_remove(&sc->sc_bio_queue, bp); M25PXX_UNLOCK(sc); switch (bp->bio_cmd) { case BIO_READ: bp->bio_error = mx25l_read(sc, bp->bio_offset, bp->bio_data, bp->bio_bcount); break; case BIO_WRITE: bp->bio_error = mx25l_write(sc, bp->bio_offset, bp->bio_data, bp->bio_bcount); break; default: bp->bio_error = EINVAL; } biodone(bp); } } static devclass_t mx25l_devclass; static device_method_t mx25l_methods[] = { /* Device interface */ DEVMETHOD(device_probe, mx25l_probe), DEVMETHOD(device_attach, mx25l_attach), DEVMETHOD(device_detach, mx25l_detach), { 0, 0 } }; static driver_t mx25l_driver = { "mx25l", mx25l_methods, sizeof(struct mx25l_softc), }; DRIVER_MODULE(mx25l, spibus, mx25l_driver, mx25l_devclass, 0, 0); MODULE_DEPEND(mx25l, spibus, 1, 1, 1); #ifdef FDT MODULE_DEPEND(mx25l, fdt_slicer, 1, 1, 1); SPIBUS_PNP_INFO(compat_data); #endif