1 /*- 2 * Copyright (c) 2006 M. Warner Losh. All rights reserved. 3 * Copyright (c) 2009 Oleksandr Tymoshenko. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bio.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/kernel.h> 35 #include <sys/kthread.h> 36 #include <sys/lock.h> 37 #include <sys/mbuf.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/mutex.h> 41 #include <geom/geom_disk.h> 42 43 #include <dev/spibus/spi.h> 44 #include "spibus_if.h" 45 46 #include <dev/flash/mx25lreg.h> 47 48 #define FL_NONE 0x00 49 #define FL_ERASE_4K 0x01 50 #define FL_ERASE_32K 0x02 51 52 /* 53 * Define the sectorsize to be a smaller size rather than the flash 54 * sector size. Trying to run FFS off of a 64k flash sector size 55 * results in a completely un-usable system. 56 */ 57 #define MX25L_SECTORSIZE 512 58 59 struct mx25l_flash_ident 60 { 61 const char *name; 62 uint8_t manufacturer_id; 63 uint16_t device_id; 64 unsigned int sectorsize; 65 unsigned int sectorcount; 66 unsigned int flags; 67 }; 68 69 struct mx25l_softc 70 { 71 device_t sc_dev; 72 uint8_t sc_manufacturer_id; 73 uint16_t sc_device_id; 74 unsigned int sc_sectorsize; 75 struct mtx sc_mtx; 76 struct disk *sc_disk; 77 struct proc *sc_p; 78 struct bio_queue_head sc_bio_queue; 79 unsigned int sc_flags; 80 }; 81 82 #define M25PXX_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 83 #define M25PXX_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 84 #define M25PXX_LOCK_INIT(_sc) \ 85 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \ 86 "mx25l", MTX_DEF) 87 #define M25PXX_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 88 #define M25PXX_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 89 #define M25PXX_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 90 91 /* disk routines */ 92 static int mx25l_open(struct disk *dp); 93 static int mx25l_close(struct disk *dp); 94 static int mx25l_ioctl(struct disk *, u_long, void *, int, struct thread *); 95 static void mx25l_strategy(struct bio *bp); 96 static void mx25l_task(void *arg); 97 98 struct mx25l_flash_ident flash_devices[] = { 99 { "mx25ll32", 0xc2, 0x2016, 64 * 1024, 64, FL_NONE }, 100 { "m25p64", 0x20, 0x2017, 64 * 1024, 128, FL_NONE }, 101 { "mx25ll64", 0xc2, 0x2017, 64 * 1024, 128, FL_NONE }, 102 { "mx25ll128", 0xc2, 0x2018, 64 * 1024, 256, FL_ERASE_4K | FL_ERASE_32K }, 103 { "s25fl128", 0x01, 0x2018, 64 * 1024, 256, FL_NONE }, 104 { "s25s1032", 0x01, 0x0215, 64 * 1024, 64, FL_NONE }, 105 { "s25sl064a", 0x01, 0x0216, 64 * 1024, 128, FL_NONE }, 106 { "w25q64bv", 0xef, 0x4017, 64 * 1024, 128, FL_ERASE_4K }, 107 }; 108 109 static uint8_t 110 mx25l_get_status(device_t dev) 111 { 112 uint8_t txBuf[2], rxBuf[2]; 113 struct spi_command cmd; 114 int err; 115 116 memset(&cmd, 0, sizeof(cmd)); 117 memset(txBuf, 0, sizeof(txBuf)); 118 memset(rxBuf, 0, sizeof(rxBuf)); 119 120 txBuf[0] = CMD_READ_STATUS; 121 cmd.tx_cmd = txBuf; 122 cmd.rx_cmd = rxBuf; 123 cmd.rx_cmd_sz = 2; 124 cmd.tx_cmd_sz = 2; 125 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd); 126 return (rxBuf[1]); 127 } 128 129 static void 130 mx25l_wait_for_device_ready(device_t dev) 131 { 132 while ((mx25l_get_status(dev) & STATUS_WIP)) 133 continue; 134 } 135 136 static struct mx25l_flash_ident* 137 mx25l_get_device_ident(struct mx25l_softc *sc) 138 { 139 device_t dev = sc->sc_dev; 140 uint8_t txBuf[8], rxBuf[8]; 141 struct spi_command cmd; 142 uint8_t manufacturer_id; 143 uint16_t dev_id; 144 int err, i; 145 146 memset(&cmd, 0, sizeof(cmd)); 147 memset(txBuf, 0, sizeof(txBuf)); 148 memset(rxBuf, 0, sizeof(rxBuf)); 149 150 txBuf[0] = CMD_READ_IDENT; 151 cmd.tx_cmd = &txBuf; 152 cmd.rx_cmd = &rxBuf; 153 /* 154 * Some compatible devices has extended two-bytes ID 155 * We'll use only manufacturer/deviceid atm 156 */ 157 cmd.tx_cmd_sz = 4; 158 cmd.rx_cmd_sz = 4; 159 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd); 160 if (err) 161 return (NULL); 162 163 manufacturer_id = rxBuf[1]; 164 dev_id = (rxBuf[2] << 8) | (rxBuf[3]); 165 166 for (i = 0; 167 i < sizeof(flash_devices)/sizeof(struct mx25l_flash_ident); i++) { 168 if ((flash_devices[i].manufacturer_id == manufacturer_id) && 169 (flash_devices[i].device_id == dev_id)) 170 return &flash_devices[i]; 171 } 172 173 printf("Unknown SPI flash device. Vendor: %02x, device id: %04x\n", 174 manufacturer_id, dev_id); 175 return (NULL); 176 } 177 178 static void 179 mx25l_set_writable(device_t dev, int writable) 180 { 181 uint8_t txBuf[1], rxBuf[1]; 182 struct spi_command cmd; 183 int err; 184 185 memset(&cmd, 0, sizeof(cmd)); 186 memset(txBuf, 0, sizeof(txBuf)); 187 memset(rxBuf, 0, sizeof(rxBuf)); 188 189 txBuf[0] = writable ? CMD_WRITE_ENABLE : CMD_WRITE_DISABLE; 190 cmd.tx_cmd = txBuf; 191 cmd.rx_cmd = rxBuf; 192 cmd.rx_cmd_sz = 1; 193 cmd.tx_cmd_sz = 1; 194 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd); 195 } 196 197 static void 198 mx25l_erase_cmd(device_t dev, off_t sector, uint8_t ecmd) 199 { 200 uint8_t txBuf[4], rxBuf[4]; 201 struct spi_command cmd; 202 int err; 203 204 mx25l_wait_for_device_ready(dev); 205 mx25l_set_writable(dev, 1); 206 207 memset(&cmd, 0, sizeof(cmd)); 208 memset(txBuf, 0, sizeof(txBuf)); 209 memset(rxBuf, 0, sizeof(rxBuf)); 210 211 txBuf[0] = ecmd; 212 cmd.tx_cmd = txBuf; 213 cmd.rx_cmd = rxBuf; 214 cmd.rx_cmd_sz = 4; 215 cmd.tx_cmd_sz = 4; 216 txBuf[1] = ((sector >> 16) & 0xff); 217 txBuf[2] = ((sector >> 8) & 0xff); 218 txBuf[3] = (sector & 0xff); 219 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd); 220 } 221 222 static int 223 mx25l_write(device_t dev, off_t offset, caddr_t data, off_t count) 224 { 225 struct mx25l_softc *sc; 226 uint8_t txBuf[8], rxBuf[8]; 227 struct spi_command cmd; 228 off_t write_offset; 229 long bytes_to_write, bytes_writen; 230 device_t pdev; 231 int err = 0; 232 233 pdev = device_get_parent(dev); 234 sc = device_get_softc(dev); 235 236 cmd.tx_cmd_sz = 4; 237 cmd.rx_cmd_sz = 4; 238 239 bytes_writen = 0; 240 write_offset = offset; 241 242 /* 243 * Use the erase sectorsize here since blocks are fully erased 244 * first before they're written to. 245 */ 246 if (count % sc->sc_sectorsize != 0 || offset % sc->sc_sectorsize != 0) 247 return (EIO); 248 249 /* 250 * Assume here that we write per-sector only 251 * and sector size should be 256 bytes aligned 252 */ 253 KASSERT(write_offset % FLASH_PAGE_SIZE == 0, 254 ("offset for BIO_WRITE is not page size (%d bytes) aligned", 255 FLASH_PAGE_SIZE)); 256 257 /* 258 * Maximum write size for CMD_PAGE_PROGRAM is 259 * FLASH_PAGE_SIZE, so split data to chunks 260 * FLASH_PAGE_SIZE bytes eash and write them 261 * one by one 262 */ 263 while (bytes_writen < count) { 264 /* 265 * If we crossed sector boundary - erase next sector 266 */ 267 if (((offset + bytes_writen) % sc->sc_sectorsize) == 0) 268 mx25l_erase_cmd(dev, offset + bytes_writen, CMD_SECTOR_ERASE); 269 270 txBuf[0] = CMD_PAGE_PROGRAM; 271 txBuf[1] = ((write_offset >> 16) & 0xff); 272 txBuf[2] = ((write_offset >> 8) & 0xff); 273 txBuf[3] = (write_offset & 0xff); 274 275 bytes_to_write = MIN(FLASH_PAGE_SIZE, 276 count - bytes_writen); 277 cmd.tx_cmd = txBuf; 278 cmd.rx_cmd = rxBuf; 279 cmd.tx_data = data + bytes_writen; 280 cmd.tx_data_sz = bytes_to_write; 281 cmd.rx_data = data + bytes_writen; 282 cmd.rx_data_sz = bytes_to_write; 283 284 /* 285 * Eash completed write operation resets WEL 286 * (write enable latch) to disabled state, 287 * so we re-enable it here 288 */ 289 mx25l_wait_for_device_ready(dev); 290 mx25l_set_writable(dev, 1); 291 292 err = SPIBUS_TRANSFER(pdev, dev, &cmd); 293 if (err) 294 break; 295 296 bytes_writen += bytes_to_write; 297 write_offset += bytes_to_write; 298 } 299 300 return (err); 301 } 302 303 static int 304 mx25l_read(device_t dev, off_t offset, caddr_t data, off_t count) 305 { 306 struct mx25l_softc *sc; 307 uint8_t txBuf[8], rxBuf[8]; 308 struct spi_command cmd; 309 device_t pdev; 310 int err = 0; 311 312 pdev = device_get_parent(dev); 313 sc = device_get_softc(dev); 314 315 /* 316 * Enforce the disk read sectorsize not the erase sectorsize. 317 * In this way, smaller read IO is possible,dramatically 318 * speeding up filesystem/geom_compress access. 319 */ 320 if (count % sc->sc_disk->d_sectorsize != 0 321 || offset % sc->sc_disk->d_sectorsize != 0) 322 return (EIO); 323 324 txBuf[0] = CMD_FAST_READ; 325 cmd.tx_cmd_sz = 5; 326 cmd.rx_cmd_sz = 5; 327 328 txBuf[1] = ((offset >> 16) & 0xff); 329 txBuf[2] = ((offset >> 8) & 0xff); 330 txBuf[3] = (offset & 0xff); 331 /* Dummy byte */ 332 txBuf[4] = 0; 333 334 cmd.tx_cmd = txBuf; 335 cmd.rx_cmd = rxBuf; 336 cmd.tx_data = data; 337 cmd.tx_data_sz = count; 338 cmd.rx_data = data; 339 cmd.rx_data_sz = count; 340 341 err = SPIBUS_TRANSFER(pdev, dev, &cmd); 342 343 return (err); 344 } 345 346 static int 347 mx25l_probe(device_t dev) 348 { 349 device_set_desc(dev, "M25Pxx Flash Family"); 350 return (0); 351 } 352 353 static int 354 mx25l_attach(device_t dev) 355 { 356 struct mx25l_softc *sc; 357 struct mx25l_flash_ident *ident; 358 359 sc = device_get_softc(dev); 360 sc->sc_dev = dev; 361 M25PXX_LOCK_INIT(sc); 362 363 ident = mx25l_get_device_ident(sc); 364 if (ident == NULL) 365 return (ENXIO); 366 367 mx25l_wait_for_device_ready(sc->sc_dev); 368 369 sc->sc_disk = disk_alloc(); 370 sc->sc_disk->d_open = mx25l_open; 371 sc->sc_disk->d_close = mx25l_close; 372 sc->sc_disk->d_strategy = mx25l_strategy; 373 sc->sc_disk->d_ioctl = mx25l_ioctl; 374 sc->sc_disk->d_name = "flash/spi"; 375 sc->sc_disk->d_drv1 = sc; 376 sc->sc_disk->d_maxsize = DFLTPHYS; 377 sc->sc_disk->d_sectorsize = MX25L_SECTORSIZE; 378 sc->sc_disk->d_mediasize = ident->sectorsize * ident->sectorcount; 379 sc->sc_disk->d_unit = device_get_unit(sc->sc_dev); 380 sc->sc_disk->d_dump = NULL; /* NB: no dumps */ 381 /* Sectorsize for erase operations */ 382 sc->sc_sectorsize = ident->sectorsize; 383 sc->sc_flags = ident->flags; 384 385 /* NB: use stripesize to hold the erase/region size for RedBoot */ 386 sc->sc_disk->d_stripesize = ident->sectorsize; 387 388 disk_create(sc->sc_disk, DISK_VERSION); 389 bioq_init(&sc->sc_bio_queue); 390 391 kproc_create(&mx25l_task, sc, &sc->sc_p, 0, 0, "task: mx25l flash"); 392 device_printf(sc->sc_dev, "%s, sector %d bytes, %d sectors\n", 393 ident->name, ident->sectorsize, ident->sectorcount); 394 395 return (0); 396 } 397 398 static int 399 mx25l_detach(device_t dev) 400 { 401 402 return (EIO); 403 } 404 405 static int 406 mx25l_open(struct disk *dp) 407 { 408 return (0); 409 } 410 411 static int 412 mx25l_close(struct disk *dp) 413 { 414 415 return (0); 416 } 417 418 static int 419 mx25l_ioctl(struct disk *dp, u_long cmd, void *data, int fflag, 420 struct thread *td) 421 { 422 423 return (EINVAL); 424 } 425 426 static void 427 mx25l_strategy(struct bio *bp) 428 { 429 struct mx25l_softc *sc; 430 431 sc = (struct mx25l_softc *)bp->bio_disk->d_drv1; 432 M25PXX_LOCK(sc); 433 bioq_disksort(&sc->sc_bio_queue, bp); 434 wakeup(sc); 435 M25PXX_UNLOCK(sc); 436 } 437 438 static void 439 mx25l_task(void *arg) 440 { 441 struct mx25l_softc *sc = (struct mx25l_softc*)arg; 442 struct bio *bp; 443 device_t dev; 444 445 for (;;) { 446 dev = sc->sc_dev; 447 M25PXX_LOCK(sc); 448 do { 449 bp = bioq_first(&sc->sc_bio_queue); 450 if (bp == NULL) 451 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0); 452 } while (bp == NULL); 453 bioq_remove(&sc->sc_bio_queue, bp); 454 M25PXX_UNLOCK(sc); 455 456 switch (bp->bio_cmd) { 457 case BIO_READ: 458 bp->bio_error = mx25l_read(dev, bp->bio_offset, 459 bp->bio_data, bp->bio_bcount); 460 break; 461 case BIO_WRITE: 462 bp->bio_error = mx25l_write(dev, bp->bio_offset, 463 bp->bio_data, bp->bio_bcount); 464 break; 465 default: 466 bp->bio_error = EINVAL; 467 } 468 469 470 biodone(bp); 471 } 472 } 473 474 static devclass_t mx25l_devclass; 475 476 static device_method_t mx25l_methods[] = { 477 /* Device interface */ 478 DEVMETHOD(device_probe, mx25l_probe), 479 DEVMETHOD(device_attach, mx25l_attach), 480 DEVMETHOD(device_detach, mx25l_detach), 481 482 { 0, 0 } 483 }; 484 485 static driver_t mx25l_driver = { 486 "mx25l", 487 mx25l_methods, 488 sizeof(struct mx25l_softc), 489 }; 490 491 DRIVER_MODULE(mx25l, spibus, mx25l_driver, mx25l_devclass, 0, 0); 492