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 { "s25sl064a", 0x01, 0x0216, 64 * 1024, 128, FL_NONE }, 105 }; 106 107 static uint8_t 108 mx25l_get_status(device_t dev) 109 { 110 uint8_t txBuf[2], rxBuf[2]; 111 struct spi_command cmd; 112 int err; 113 114 memset(&cmd, 0, sizeof(cmd)); 115 memset(txBuf, 0, sizeof(txBuf)); 116 memset(rxBuf, 0, sizeof(rxBuf)); 117 118 txBuf[0] = CMD_READ_STATUS; 119 cmd.tx_cmd = txBuf; 120 cmd.rx_cmd = rxBuf; 121 cmd.rx_cmd_sz = 2; 122 cmd.tx_cmd_sz = 2; 123 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd); 124 return (rxBuf[1]); 125 } 126 127 static void 128 mx25l_wait_for_device_ready(device_t dev) 129 { 130 while ((mx25l_get_status(dev) & STATUS_WIP)) 131 continue; 132 } 133 134 static struct mx25l_flash_ident* 135 mx25l_get_device_ident(struct mx25l_softc *sc) 136 { 137 device_t dev = sc->sc_dev; 138 uint8_t txBuf[8], rxBuf[8]; 139 struct spi_command cmd; 140 uint8_t manufacturer_id; 141 uint16_t dev_id; 142 int err, i; 143 144 memset(&cmd, 0, sizeof(cmd)); 145 memset(txBuf, 0, sizeof(txBuf)); 146 memset(rxBuf, 0, sizeof(rxBuf)); 147 148 txBuf[0] = CMD_READ_IDENT; 149 cmd.tx_cmd = &txBuf; 150 cmd.rx_cmd = &rxBuf; 151 /* 152 * Some compatible devices has extended two-bytes ID 153 * We'll use only manufacturer/deviceid atm 154 */ 155 cmd.tx_cmd_sz = 4; 156 cmd.rx_cmd_sz = 4; 157 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd); 158 if (err) 159 return (NULL); 160 161 manufacturer_id = rxBuf[1]; 162 dev_id = (rxBuf[2] << 8) | (rxBuf[3]); 163 164 for (i = 0; 165 i < sizeof(flash_devices)/sizeof(struct mx25l_flash_ident); i++) { 166 if ((flash_devices[i].manufacturer_id == manufacturer_id) && 167 (flash_devices[i].device_id == dev_id)) 168 return &flash_devices[i]; 169 } 170 171 printf("Unknown SPI flash device. Vendor: %02x, device id: %04x\n", 172 manufacturer_id, dev_id); 173 return (NULL); 174 } 175 176 static void 177 mx25l_set_writable(device_t dev, int writable) 178 { 179 uint8_t txBuf[1], rxBuf[1]; 180 struct spi_command cmd; 181 int err; 182 183 memset(&cmd, 0, sizeof(cmd)); 184 memset(txBuf, 0, sizeof(txBuf)); 185 memset(rxBuf, 0, sizeof(rxBuf)); 186 187 txBuf[0] = writable ? CMD_WRITE_ENABLE : CMD_WRITE_DISABLE; 188 cmd.tx_cmd = txBuf; 189 cmd.rx_cmd = rxBuf; 190 cmd.rx_cmd_sz = 1; 191 cmd.tx_cmd_sz = 1; 192 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd); 193 } 194 195 static void 196 mx25l_erase_cmd(device_t dev, off_t sector, uint8_t ecmd) 197 { 198 uint8_t txBuf[4], rxBuf[4]; 199 struct spi_command cmd; 200 int err; 201 202 mx25l_wait_for_device_ready(dev); 203 mx25l_set_writable(dev, 1); 204 205 memset(&cmd, 0, sizeof(cmd)); 206 memset(txBuf, 0, sizeof(txBuf)); 207 memset(rxBuf, 0, sizeof(rxBuf)); 208 209 txBuf[0] = ecmd; 210 cmd.tx_cmd = txBuf; 211 cmd.rx_cmd = rxBuf; 212 cmd.rx_cmd_sz = 4; 213 cmd.tx_cmd_sz = 4; 214 txBuf[1] = ((sector >> 16) & 0xff); 215 txBuf[2] = ((sector >> 8) & 0xff); 216 txBuf[3] = (sector & 0xff); 217 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd); 218 } 219 220 static int 221 mx25l_write(device_t dev, off_t offset, caddr_t data, off_t count) 222 { 223 struct mx25l_softc *sc; 224 uint8_t txBuf[8], rxBuf[8]; 225 struct spi_command cmd; 226 off_t write_offset; 227 long bytes_to_write, bytes_writen; 228 device_t pdev; 229 int err = 0; 230 231 pdev = device_get_parent(dev); 232 sc = device_get_softc(dev); 233 234 cmd.tx_cmd_sz = 4; 235 cmd.rx_cmd_sz = 4; 236 237 bytes_writen = 0; 238 write_offset = offset; 239 240 /* 241 * Use the erase sectorsize here since blocks are fully erased 242 * first before they're written to. 243 */ 244 if (count % sc->sc_sectorsize != 0 || offset % sc->sc_sectorsize != 0) 245 return (EIO); 246 247 /* 248 * Assume here that we write per-sector only 249 * and sector size should be 256 bytes aligned 250 */ 251 KASSERT(write_offset % FLASH_PAGE_SIZE == 0, 252 ("offset for BIO_WRITE is not page size (%d bytes) aligned", 253 FLASH_PAGE_SIZE)); 254 255 /* 256 * Maximum write size for CMD_PAGE_PROGRAM is 257 * FLASH_PAGE_SIZE, so split data to chunks 258 * FLASH_PAGE_SIZE bytes eash and write them 259 * one by one 260 */ 261 while (bytes_writen < count) { 262 /* 263 * If we crossed sector boundary - erase next sector 264 */ 265 if (((offset + bytes_writen) % sc->sc_sectorsize) == 0) 266 mx25l_erase_cmd(dev, offset + bytes_writen, CMD_SECTOR_ERASE); 267 268 txBuf[0] = CMD_PAGE_PROGRAM; 269 txBuf[1] = ((write_offset >> 16) & 0xff); 270 txBuf[2] = ((write_offset >> 8) & 0xff); 271 txBuf[3] = (write_offset & 0xff); 272 273 bytes_to_write = MIN(FLASH_PAGE_SIZE, 274 count - bytes_writen); 275 cmd.tx_cmd = txBuf; 276 cmd.rx_cmd = rxBuf; 277 cmd.tx_data = data + bytes_writen; 278 cmd.tx_data_sz = bytes_to_write; 279 cmd.rx_data = data + bytes_writen; 280 cmd.rx_data_sz = bytes_to_write; 281 282 /* 283 * Eash completed write operation resets WEL 284 * (write enable latch) to disabled state, 285 * so we re-enable it here 286 */ 287 mx25l_wait_for_device_ready(dev); 288 mx25l_set_writable(dev, 1); 289 290 err = SPIBUS_TRANSFER(pdev, dev, &cmd); 291 if (err) 292 break; 293 294 bytes_writen += bytes_to_write; 295 write_offset += bytes_to_write; 296 } 297 298 return (err); 299 } 300 301 static int 302 mx25l_read(device_t dev, off_t offset, caddr_t data, off_t count) 303 { 304 struct mx25l_softc *sc; 305 uint8_t txBuf[8], rxBuf[8]; 306 struct spi_command cmd; 307 device_t pdev; 308 int err = 0; 309 310 pdev = device_get_parent(dev); 311 sc = device_get_softc(dev); 312 313 /* 314 * Enforce the disk read sectorsize not the erase sectorsize. 315 * In this way, smaller read IO is possible,dramatically 316 * speeding up filesystem/geom_compress access. 317 */ 318 if (count % sc->sc_disk->d_sectorsize != 0 319 || offset % sc->sc_disk->d_sectorsize != 0) 320 return (EIO); 321 322 txBuf[0] = CMD_FAST_READ; 323 cmd.tx_cmd_sz = 5; 324 cmd.rx_cmd_sz = 5; 325 326 txBuf[1] = ((offset >> 16) & 0xff); 327 txBuf[2] = ((offset >> 8) & 0xff); 328 txBuf[3] = (offset & 0xff); 329 /* Dummy byte */ 330 txBuf[4] = 0; 331 332 cmd.tx_cmd = txBuf; 333 cmd.rx_cmd = rxBuf; 334 cmd.tx_data = data; 335 cmd.tx_data_sz = count; 336 cmd.rx_data = data; 337 cmd.rx_data_sz = count; 338 339 err = SPIBUS_TRANSFER(pdev, dev, &cmd); 340 341 return (err); 342 } 343 344 static int 345 mx25l_probe(device_t dev) 346 { 347 device_set_desc(dev, "M25Pxx Flash Family"); 348 return (0); 349 } 350 351 static int 352 mx25l_attach(device_t dev) 353 { 354 struct mx25l_softc *sc; 355 struct mx25l_flash_ident *ident; 356 357 sc = device_get_softc(dev); 358 sc->sc_dev = dev; 359 M25PXX_LOCK_INIT(sc); 360 361 ident = mx25l_get_device_ident(sc); 362 if (ident == NULL) 363 return (ENXIO); 364 365 mx25l_wait_for_device_ready(sc->sc_dev); 366 367 sc->sc_disk = disk_alloc(); 368 sc->sc_disk->d_open = mx25l_open; 369 sc->sc_disk->d_close = mx25l_close; 370 sc->sc_disk->d_strategy = mx25l_strategy; 371 sc->sc_disk->d_ioctl = mx25l_ioctl; 372 sc->sc_disk->d_name = "flash/spi"; 373 sc->sc_disk->d_drv1 = sc; 374 sc->sc_disk->d_maxsize = DFLTPHYS; 375 sc->sc_disk->d_sectorsize = MX25L_SECTORSIZE; 376 sc->sc_disk->d_mediasize = ident->sectorsize * ident->sectorcount; 377 sc->sc_disk->d_unit = device_get_unit(sc->sc_dev); 378 sc->sc_disk->d_dump = NULL; /* NB: no dumps */ 379 /* Sectorsize for erase operations */ 380 sc->sc_sectorsize = ident->sectorsize; 381 sc->sc_flags = ident->flags; 382 383 /* NB: use stripesize to hold the erase/region size for RedBoot */ 384 sc->sc_disk->d_stripesize = ident->sectorsize; 385 386 disk_create(sc->sc_disk, DISK_VERSION); 387 bioq_init(&sc->sc_bio_queue); 388 389 kproc_create(&mx25l_task, sc, &sc->sc_p, 0, 0, "task: mx25l flash"); 390 device_printf(sc->sc_dev, "%s, sector %d bytes, %d sectors\n", 391 ident->name, ident->sectorsize, ident->sectorcount); 392 393 return (0); 394 } 395 396 static int 397 mx25l_detach(device_t dev) 398 { 399 400 return (EIO); 401 } 402 403 static int 404 mx25l_open(struct disk *dp) 405 { 406 return (0); 407 } 408 409 static int 410 mx25l_close(struct disk *dp) 411 { 412 413 return (0); 414 } 415 416 static int 417 mx25l_ioctl(struct disk *dp, u_long cmd, void *data, int fflag, 418 struct thread *td) 419 { 420 421 return (EINVAL); 422 } 423 424 static void 425 mx25l_strategy(struct bio *bp) 426 { 427 struct mx25l_softc *sc; 428 429 sc = (struct mx25l_softc *)bp->bio_disk->d_drv1; 430 M25PXX_LOCK(sc); 431 bioq_disksort(&sc->sc_bio_queue, bp); 432 wakeup(sc); 433 M25PXX_UNLOCK(sc); 434 } 435 436 static void 437 mx25l_task(void *arg) 438 { 439 struct mx25l_softc *sc = (struct mx25l_softc*)arg; 440 struct bio *bp; 441 device_t dev; 442 443 for (;;) { 444 dev = sc->sc_dev; 445 M25PXX_LOCK(sc); 446 do { 447 bp = bioq_first(&sc->sc_bio_queue); 448 if (bp == NULL) 449 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0); 450 } while (bp == NULL); 451 bioq_remove(&sc->sc_bio_queue, bp); 452 M25PXX_UNLOCK(sc); 453 454 switch (bp->bio_cmd) { 455 case BIO_READ: 456 bp->bio_error = mx25l_read(dev, bp->bio_offset, 457 bp->bio_data, bp->bio_bcount); 458 break; 459 case BIO_WRITE: 460 bp->bio_error = mx25l_write(dev, bp->bio_offset, 461 bp->bio_data, bp->bio_bcount); 462 break; 463 default: 464 bp->bio_error = EINVAL; 465 } 466 467 468 biodone(bp); 469 } 470 } 471 472 static devclass_t mx25l_devclass; 473 474 static device_method_t mx25l_methods[] = { 475 /* Device interface */ 476 DEVMETHOD(device_probe, mx25l_probe), 477 DEVMETHOD(device_attach, mx25l_attach), 478 DEVMETHOD(device_detach, mx25l_detach), 479 480 { 0, 0 } 481 }; 482 483 static driver_t mx25l_driver = { 484 "mx25l", 485 mx25l_methods, 486 sizeof(struct mx25l_softc), 487 }; 488 489 DRIVER_MODULE(mx25l, spibus, mx25l_driver, mx25l_devclass, 0, 0); 490