1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Common Flash Interface support: 4 * ST Advanced Architecture Command Set (ID 0x0020) 5 * 6 * (C) 2000 Red Hat. 7 * 8 * 10/10/2000 Nicolas Pitre <nico@fluxnic.net> 9 * - completely revamped method functions so they are aware and 10 * independent of the flash geometry (buswidth, interleave, etc.) 11 * - scalability vs code size is completely set at compile-time 12 * (see include/linux/mtd/cfi.h for selection) 13 * - optimized write buffer method 14 * 06/21/2002 Joern Engel <joern@wh.fh-wedel.de> and others 15 * - modified Intel Command Set 0x0001 to support ST Advanced Architecture 16 * (command set 0x0020) 17 * - added a writev function 18 * 07/13/2005 Joern Engel <joern@wh.fh-wedel.de> 19 * - Plugged memory leak in cfi_staa_writev(). 20 */ 21 22 #include <linux/module.h> 23 #include <linux/types.h> 24 #include <linux/kernel.h> 25 #include <linux/sched.h> 26 #include <asm/io.h> 27 #include <asm/byteorder.h> 28 29 #include <linux/errno.h> 30 #include <linux/slab.h> 31 #include <linux/delay.h> 32 #include <linux/interrupt.h> 33 #include <linux/mtd/map.h> 34 #include <linux/mtd/cfi.h> 35 #include <linux/mtd/mtd.h> 36 37 38 static int cfi_staa_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *); 39 static int cfi_staa_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *); 40 static int cfi_staa_writev(struct mtd_info *mtd, const struct kvec *vecs, 41 unsigned long count, loff_t to, size_t *retlen); 42 static int cfi_staa_erase_varsize(struct mtd_info *, struct erase_info *); 43 static void cfi_staa_sync (struct mtd_info *); 44 static int cfi_staa_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len); 45 static int cfi_staa_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len); 46 static int cfi_staa_suspend (struct mtd_info *); 47 static void cfi_staa_resume (struct mtd_info *); 48 49 static void cfi_staa_destroy(struct mtd_info *); 50 51 struct mtd_info *cfi_cmdset_0020(struct map_info *, int); 52 53 static struct mtd_info *cfi_staa_setup (struct map_info *); 54 55 static struct mtd_chip_driver cfi_staa_chipdrv = { 56 .probe = NULL, /* Not usable directly */ 57 .destroy = cfi_staa_destroy, 58 .name = "cfi_cmdset_0020", 59 .module = THIS_MODULE 60 }; 61 62 /* #define DEBUG_LOCK_BITS */ 63 //#define DEBUG_CFI_FEATURES 64 65 #ifdef DEBUG_CFI_FEATURES 66 static void cfi_tell_features(struct cfi_pri_intelext *extp) 67 { 68 int i; 69 printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport); 70 printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported"); 71 printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported"); 72 printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported"); 73 printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported"); 74 printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported"); 75 printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported"); 76 printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported"); 77 printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported"); 78 printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported"); 79 for (i=9; i<32; i++) { 80 if (extp->FeatureSupport & (1<<i)) 81 printk(" - Unknown Bit %X: supported\n", i); 82 } 83 84 printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport); 85 printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported"); 86 for (i=1; i<8; i++) { 87 if (extp->SuspendCmdSupport & (1<<i)) 88 printk(" - Unknown Bit %X: supported\n", i); 89 } 90 91 printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask); 92 printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no"); 93 printk(" - Valid Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no"); 94 for (i=2; i<16; i++) { 95 if (extp->BlkStatusRegMask & (1<<i)) 96 printk(" - Unknown Bit %X Active: yes\n",i); 97 } 98 99 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n", 100 extp->VccOptimal >> 8, extp->VccOptimal & 0xf); 101 if (extp->VppOptimal) 102 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n", 103 extp->VppOptimal >> 8, extp->VppOptimal & 0xf); 104 } 105 #endif 106 107 /* This routine is made available to other mtd code via 108 * inter_module_register. It must only be accessed through 109 * inter_module_get which will bump the use count of this module. The 110 * addresses passed back in cfi are valid as long as the use count of 111 * this module is non-zero, i.e. between inter_module_get and 112 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000. 113 */ 114 struct mtd_info *cfi_cmdset_0020(struct map_info *map, int primary) 115 { 116 struct cfi_private *cfi = map->fldrv_priv; 117 int i; 118 119 if (cfi->cfi_mode) { 120 /* 121 * It's a real CFI chip, not one for which the probe 122 * routine faked a CFI structure. So we read the feature 123 * table from it. 124 */ 125 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR; 126 struct cfi_pri_intelext *extp; 127 128 extp = (struct cfi_pri_intelext*)cfi_read_pri(map, adr, sizeof(*extp), "ST Microelectronics"); 129 if (!extp) 130 return NULL; 131 132 if (extp->MajorVersion != '1' || 133 (extp->MinorVersion < '0' || extp->MinorVersion > '3')) { 134 printk(KERN_ERR " Unknown ST Microelectronics" 135 " Extended Query version %c.%c.\n", 136 extp->MajorVersion, extp->MinorVersion); 137 kfree(extp); 138 return NULL; 139 } 140 141 /* Do some byteswapping if necessary */ 142 extp->FeatureSupport = cfi32_to_cpu(map, extp->FeatureSupport); 143 extp->BlkStatusRegMask = cfi32_to_cpu(map, 144 extp->BlkStatusRegMask); 145 146 #ifdef DEBUG_CFI_FEATURES 147 /* Tell the user about it in lots of lovely detail */ 148 cfi_tell_features(extp); 149 #endif 150 151 /* Install our own private info structure */ 152 cfi->cmdset_priv = extp; 153 } 154 155 for (i=0; i< cfi->numchips; i++) { 156 cfi->chips[i].word_write_time = 128; 157 cfi->chips[i].buffer_write_time = 128; 158 cfi->chips[i].erase_time = 1024; 159 cfi->chips[i].ref_point_counter = 0; 160 init_waitqueue_head(&(cfi->chips[i].wq)); 161 } 162 163 return cfi_staa_setup(map); 164 } 165 EXPORT_SYMBOL_GPL(cfi_cmdset_0020); 166 167 static struct mtd_info *cfi_staa_setup(struct map_info *map) 168 { 169 struct cfi_private *cfi = map->fldrv_priv; 170 struct mtd_info *mtd; 171 unsigned long offset = 0; 172 int i,j; 173 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave; 174 175 mtd = kzalloc_obj(*mtd); 176 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips); 177 if (!mtd) 178 goto free_cmdset_priv; 179 180 mtd->priv = map; 181 mtd->type = MTD_NORFLASH; 182 mtd->size = devsize * cfi->numchips; 183 184 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips; 185 mtd->eraseregions = kmalloc_objs(struct mtd_erase_region_info, 186 mtd->numeraseregions); 187 if (!mtd->eraseregions) 188 goto free_mtd; 189 190 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) { 191 unsigned long ernum, ersize; 192 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave; 193 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1; 194 195 if (mtd->erasesize < ersize) { 196 mtd->erasesize = ersize; 197 } 198 for (j=0; j<cfi->numchips; j++) { 199 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset; 200 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize; 201 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum; 202 } 203 offset += (ersize * ernum); 204 } 205 206 if (offset != devsize) { 207 /* Argh */ 208 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize); 209 kfree(mtd->eraseregions); 210 goto free_mtd; 211 } 212 213 for (i=0; i<mtd->numeraseregions;i++){ 214 printk(KERN_DEBUG "%d: offset=0x%llx,size=0x%x,blocks=%d\n", 215 i, (unsigned long long)mtd->eraseregions[i].offset, 216 mtd->eraseregions[i].erasesize, 217 mtd->eraseregions[i].numblocks); 218 } 219 220 /* Also select the correct geometry setup too */ 221 mtd->_erase = cfi_staa_erase_varsize; 222 mtd->_read = cfi_staa_read; 223 mtd->_write = cfi_staa_write_buffers; 224 mtd->_writev = cfi_staa_writev; 225 mtd->_sync = cfi_staa_sync; 226 mtd->_lock = cfi_staa_lock; 227 mtd->_unlock = cfi_staa_unlock; 228 mtd->_suspend = cfi_staa_suspend; 229 mtd->_resume = cfi_staa_resume; 230 mtd->flags = MTD_CAP_NORFLASH & ~MTD_BIT_WRITEABLE; 231 mtd->writesize = 8; /* FIXME: Should be 0 for STMicro flashes w/out ECC */ 232 mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize; 233 map->fldrv = &cfi_staa_chipdrv; 234 __module_get(THIS_MODULE); 235 mtd->name = map->name; 236 return mtd; 237 238 free_mtd: 239 kfree(mtd); 240 free_cmdset_priv: 241 kfree(cfi->cmdset_priv); 242 return NULL; 243 } 244 245 246 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf) 247 { 248 map_word status, status_OK; 249 unsigned long timeo; 250 DECLARE_WAITQUEUE(wait, current); 251 int suspended = 0; 252 unsigned long cmd_addr; 253 struct cfi_private *cfi = map->fldrv_priv; 254 255 adr += chip->start; 256 257 /* Ensure cmd read/writes are aligned. */ 258 cmd_addr = adr & ~(map_bankwidth(map)-1); 259 260 /* Let's determine this according to the interleave only once */ 261 status_OK = CMD(0x80); 262 263 timeo = jiffies + HZ; 264 retry: 265 mutex_lock(&chip->mutex); 266 267 /* Check that the chip's ready to talk to us. 268 * If it's in FL_ERASING state, suspend it and make it talk now. 269 */ 270 switch (chip->state) { 271 case FL_ERASING: 272 if (!(((struct cfi_pri_intelext *)cfi->cmdset_priv)->FeatureSupport & 2)) 273 goto sleep; /* We don't support erase suspend */ 274 275 map_write (map, CMD(0xb0), cmd_addr); 276 /* If the flash has finished erasing, then 'erase suspend' 277 * appears to make some (28F320) flash devices switch to 278 * 'read' mode. Make sure that we switch to 'read status' 279 * mode so we get the right data. --rmk 280 */ 281 map_write(map, CMD(0x70), cmd_addr); 282 chip->oldstate = FL_ERASING; 283 chip->state = FL_ERASE_SUSPENDING; 284 // printk("Erase suspending at 0x%lx\n", cmd_addr); 285 for (;;) { 286 status = map_read(map, cmd_addr); 287 if (map_word_andequal(map, status, status_OK, status_OK)) 288 break; 289 290 if (time_after(jiffies, timeo)) { 291 /* Urgh */ 292 map_write(map, CMD(0xd0), cmd_addr); 293 /* make sure we're in 'read status' mode */ 294 map_write(map, CMD(0x70), cmd_addr); 295 chip->state = FL_ERASING; 296 wake_up(&chip->wq); 297 mutex_unlock(&chip->mutex); 298 printk(KERN_ERR "Chip not ready after erase " 299 "suspended: status = 0x%lx\n", status.x[0]); 300 return -EIO; 301 } 302 303 mutex_unlock(&chip->mutex); 304 cfi_udelay(1); 305 mutex_lock(&chip->mutex); 306 } 307 308 suspended = 1; 309 map_write(map, CMD(0xff), cmd_addr); 310 chip->state = FL_READY; 311 break; 312 313 #if 0 314 case FL_WRITING: 315 /* Not quite yet */ 316 #endif 317 318 case FL_READY: 319 break; 320 321 case FL_CFI_QUERY: 322 case FL_JEDEC_QUERY: 323 map_write(map, CMD(0x70), cmd_addr); 324 chip->state = FL_STATUS; 325 fallthrough; 326 case FL_STATUS: 327 status = map_read(map, cmd_addr); 328 if (map_word_andequal(map, status, status_OK, status_OK)) { 329 map_write(map, CMD(0xff), cmd_addr); 330 chip->state = FL_READY; 331 break; 332 } 333 334 /* Urgh. Chip not yet ready to talk to us. */ 335 if (time_after(jiffies, timeo)) { 336 mutex_unlock(&chip->mutex); 337 printk(KERN_ERR "waiting for chip to be ready timed out in read. WSM status = %lx\n", status.x[0]); 338 return -EIO; 339 } 340 341 /* Latency issues. Drop the lock, wait a while and retry */ 342 mutex_unlock(&chip->mutex); 343 cfi_udelay(1); 344 goto retry; 345 346 default: 347 sleep: 348 /* Stick ourselves on a wait queue to be woken when 349 someone changes the status */ 350 set_current_state(TASK_UNINTERRUPTIBLE); 351 add_wait_queue(&chip->wq, &wait); 352 mutex_unlock(&chip->mutex); 353 schedule(); 354 remove_wait_queue(&chip->wq, &wait); 355 timeo = jiffies + HZ; 356 goto retry; 357 } 358 359 map_copy_from(map, buf, adr, len); 360 361 if (suspended) { 362 chip->state = chip->oldstate; 363 /* What if one interleaved chip has finished and the 364 other hasn't? The old code would leave the finished 365 one in READY mode. That's bad, and caused -EROFS 366 errors to be returned from do_erase_oneblock because 367 that's the only bit it checked for at the time. 368 As the state machine appears to explicitly allow 369 sending the 0x70 (Read Status) command to an erasing 370 chip and expecting it to be ignored, that's what we 371 do. */ 372 map_write(map, CMD(0xd0), cmd_addr); 373 map_write(map, CMD(0x70), cmd_addr); 374 } 375 376 wake_up(&chip->wq); 377 mutex_unlock(&chip->mutex); 378 return 0; 379 } 380 381 static int cfi_staa_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf) 382 { 383 struct map_info *map = mtd->priv; 384 struct cfi_private *cfi = map->fldrv_priv; 385 unsigned long ofs; 386 int chipnum; 387 int ret = 0; 388 389 /* ofs: offset within the first chip that the first read should start */ 390 chipnum = (from >> cfi->chipshift); 391 ofs = from - (chipnum << cfi->chipshift); 392 393 while (len) { 394 unsigned long thislen; 395 396 if (chipnum >= cfi->numchips) 397 break; 398 399 if ((len + ofs -1) >> cfi->chipshift) 400 thislen = (1<<cfi->chipshift) - ofs; 401 else 402 thislen = len; 403 404 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf); 405 if (ret) 406 break; 407 408 *retlen += thislen; 409 len -= thislen; 410 buf += thislen; 411 412 ofs = 0; 413 chipnum++; 414 } 415 return ret; 416 } 417 418 static int do_write_buffer(struct map_info *map, struct flchip *chip, 419 unsigned long adr, const u_char *buf, int len) 420 { 421 struct cfi_private *cfi = map->fldrv_priv; 422 map_word status, status_OK; 423 unsigned long cmd_adr, timeo; 424 DECLARE_WAITQUEUE(wait, current); 425 int wbufsize, z; 426 427 /* M58LW064A requires bus alignment for buffer wriets -- saw */ 428 if (adr & (map_bankwidth(map)-1)) 429 return -EINVAL; 430 431 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize; 432 adr += chip->start; 433 cmd_adr = adr & ~(wbufsize-1); 434 435 /* Let's determine this according to the interleave only once */ 436 status_OK = CMD(0x80); 437 438 timeo = jiffies + HZ; 439 retry: 440 441 #ifdef DEBUG_CFI_FEATURES 442 printk("%s: chip->state[%d]\n", __func__, chip->state); 443 #endif 444 mutex_lock(&chip->mutex); 445 446 /* Check that the chip's ready to talk to us. 447 * Later, we can actually think about interrupting it 448 * if it's in FL_ERASING state. 449 * Not just yet, though. 450 */ 451 switch (chip->state) { 452 case FL_READY: 453 break; 454 455 case FL_CFI_QUERY: 456 case FL_JEDEC_QUERY: 457 map_write(map, CMD(0x70), cmd_adr); 458 chip->state = FL_STATUS; 459 #ifdef DEBUG_CFI_FEATURES 460 printk("%s: 1 status[%x]\n", __func__, map_read(map, cmd_adr)); 461 #endif 462 fallthrough; 463 case FL_STATUS: 464 status = map_read(map, cmd_adr); 465 if (map_word_andequal(map, status, status_OK, status_OK)) 466 break; 467 /* Urgh. Chip not yet ready to talk to us. */ 468 if (time_after(jiffies, timeo)) { 469 mutex_unlock(&chip->mutex); 470 printk(KERN_ERR "waiting for chip to be ready timed out in buffer write Xstatus = %lx, status = %lx\n", 471 status.x[0], map_read(map, cmd_adr).x[0]); 472 return -EIO; 473 } 474 475 /* Latency issues. Drop the lock, wait a while and retry */ 476 mutex_unlock(&chip->mutex); 477 cfi_udelay(1); 478 goto retry; 479 480 default: 481 /* Stick ourselves on a wait queue to be woken when 482 someone changes the status */ 483 set_current_state(TASK_UNINTERRUPTIBLE); 484 add_wait_queue(&chip->wq, &wait); 485 mutex_unlock(&chip->mutex); 486 schedule(); 487 remove_wait_queue(&chip->wq, &wait); 488 timeo = jiffies + HZ; 489 goto retry; 490 } 491 492 ENABLE_VPP(map); 493 map_write(map, CMD(0xe8), cmd_adr); 494 chip->state = FL_WRITING_TO_BUFFER; 495 496 z = 0; 497 for (;;) { 498 status = map_read(map, cmd_adr); 499 if (map_word_andequal(map, status, status_OK, status_OK)) 500 break; 501 502 mutex_unlock(&chip->mutex); 503 cfi_udelay(1); 504 mutex_lock(&chip->mutex); 505 506 if (++z > 100) { 507 /* Argh. Not ready for write to buffer */ 508 DISABLE_VPP(map); 509 map_write(map, CMD(0x70), cmd_adr); 510 chip->state = FL_STATUS; 511 mutex_unlock(&chip->mutex); 512 printk(KERN_ERR "Chip not ready for buffer write. Xstatus = %lx\n", status.x[0]); 513 return -EIO; 514 } 515 } 516 517 /* Write length of data to come */ 518 map_write(map, CMD(len/map_bankwidth(map)-1), cmd_adr ); 519 520 /* Write data */ 521 for (z = 0; z < len; 522 z += map_bankwidth(map), buf += map_bankwidth(map)) { 523 map_word d; 524 d = map_word_load(map, buf); 525 map_write(map, d, adr+z); 526 } 527 /* GO GO GO */ 528 map_write(map, CMD(0xd0), cmd_adr); 529 chip->state = FL_WRITING; 530 531 mutex_unlock(&chip->mutex); 532 cfi_udelay(chip->buffer_write_time); 533 mutex_lock(&chip->mutex); 534 535 timeo = jiffies + (HZ/2); 536 z = 0; 537 for (;;) { 538 if (chip->state != FL_WRITING) { 539 /* Someone's suspended the write. Sleep */ 540 set_current_state(TASK_UNINTERRUPTIBLE); 541 add_wait_queue(&chip->wq, &wait); 542 mutex_unlock(&chip->mutex); 543 schedule(); 544 remove_wait_queue(&chip->wq, &wait); 545 timeo = jiffies + (HZ / 2); /* FIXME */ 546 mutex_lock(&chip->mutex); 547 continue; 548 } 549 550 status = map_read(map, cmd_adr); 551 if (map_word_andequal(map, status, status_OK, status_OK)) 552 break; 553 554 /* OK Still waiting */ 555 if (time_after(jiffies, timeo)) { 556 /* clear status */ 557 map_write(map, CMD(0x50), cmd_adr); 558 /* put back into read status register mode */ 559 map_write(map, CMD(0x70), adr); 560 chip->state = FL_STATUS; 561 DISABLE_VPP(map); 562 mutex_unlock(&chip->mutex); 563 printk(KERN_ERR "waiting for chip to be ready timed out in bufwrite\n"); 564 return -EIO; 565 } 566 567 /* Latency issues. Drop the lock, wait a while and retry */ 568 mutex_unlock(&chip->mutex); 569 cfi_udelay(1); 570 z++; 571 mutex_lock(&chip->mutex); 572 } 573 if (!z) { 574 chip->buffer_write_time--; 575 if (!chip->buffer_write_time) 576 chip->buffer_write_time++; 577 } 578 if (z > 1) 579 chip->buffer_write_time++; 580 581 /* Done and happy. */ 582 DISABLE_VPP(map); 583 chip->state = FL_STATUS; 584 585 /* check for errors: 'lock bit', 'VPP', 'dead cell'/'unerased cell' or 'incorrect cmd' -- saw */ 586 if (map_word_bitsset(map, status, CMD(0x3a))) { 587 #ifdef DEBUG_CFI_FEATURES 588 printk("%s: 2 status[%lx]\n", __func__, status.x[0]); 589 #endif 590 /* clear status */ 591 map_write(map, CMD(0x50), cmd_adr); 592 /* put back into read status register mode */ 593 map_write(map, CMD(0x70), adr); 594 wake_up(&chip->wq); 595 mutex_unlock(&chip->mutex); 596 return map_word_bitsset(map, status, CMD(0x02)) ? -EROFS : -EIO; 597 } 598 wake_up(&chip->wq); 599 mutex_unlock(&chip->mutex); 600 601 return 0; 602 } 603 604 static int cfi_staa_write_buffers (struct mtd_info *mtd, loff_t to, 605 size_t len, size_t *retlen, const u_char *buf) 606 { 607 struct map_info *map = mtd->priv; 608 struct cfi_private *cfi = map->fldrv_priv; 609 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize; 610 int ret; 611 int chipnum; 612 unsigned long ofs; 613 614 chipnum = to >> cfi->chipshift; 615 ofs = to - (chipnum << cfi->chipshift); 616 617 #ifdef DEBUG_CFI_FEATURES 618 printk("%s: map_bankwidth(map)[%x]\n", __func__, map_bankwidth(map)); 619 printk("%s: chipnum[%x] wbufsize[%x]\n", __func__, chipnum, wbufsize); 620 printk("%s: ofs[%x] len[%x]\n", __func__, ofs, len); 621 #endif 622 623 /* Write buffer is worth it only if more than one word to write... */ 624 while (len > 0) { 625 /* We must not cross write block boundaries */ 626 int size = wbufsize - (ofs & (wbufsize-1)); 627 628 if (size > len) 629 size = len; 630 631 ret = do_write_buffer(map, &cfi->chips[chipnum], 632 ofs, buf, size); 633 if (ret) 634 return ret; 635 636 ofs += size; 637 buf += size; 638 (*retlen) += size; 639 len -= size; 640 641 if (ofs >> cfi->chipshift) { 642 chipnum ++; 643 ofs = 0; 644 if (chipnum == cfi->numchips) 645 return 0; 646 } 647 } 648 649 return 0; 650 } 651 652 /* 653 * Writev for ECC-Flashes is a little more complicated. We need to maintain 654 * a small buffer for this. 655 * XXX: If the buffer size is not a multiple of 2, this will break 656 */ 657 #define ECCBUF_SIZE (mtd->writesize) 658 #define ECCBUF_DIV(x) ((x) & ~(ECCBUF_SIZE - 1)) 659 #define ECCBUF_MOD(x) ((x) & (ECCBUF_SIZE - 1)) 660 static int 661 cfi_staa_writev(struct mtd_info *mtd, const struct kvec *vecs, 662 unsigned long count, loff_t to, size_t *retlen) 663 { 664 unsigned long i; 665 size_t totlen = 0, thislen; 666 int ret = 0; 667 size_t buflen = 0; 668 char *buffer; 669 670 if (!ECCBUF_SIZE) { 671 /* We should fall back to a general writev implementation. 672 * Until that is written, just break. 673 */ 674 return -EIO; 675 } 676 buffer = kmalloc(ECCBUF_SIZE, GFP_KERNEL); 677 if (!buffer) 678 return -ENOMEM; 679 680 for (i=0; i<count; i++) { 681 size_t elem_len = vecs[i].iov_len; 682 void *elem_base = vecs[i].iov_base; 683 if (!elem_len) /* FIXME: Might be unnecessary. Check that */ 684 continue; 685 if (buflen) { /* cut off head */ 686 if (buflen + elem_len < ECCBUF_SIZE) { /* just accumulate */ 687 memcpy(buffer+buflen, elem_base, elem_len); 688 buflen += elem_len; 689 continue; 690 } 691 memcpy(buffer+buflen, elem_base, ECCBUF_SIZE-buflen); 692 ret = mtd_write(mtd, to, ECCBUF_SIZE, &thislen, 693 buffer); 694 totlen += thislen; 695 if (ret || thislen != ECCBUF_SIZE) 696 goto write_error; 697 elem_len -= thislen-buflen; 698 elem_base += thislen-buflen; 699 to += ECCBUF_SIZE; 700 } 701 if (ECCBUF_DIV(elem_len)) { /* write clean aligned data */ 702 ret = mtd_write(mtd, to, ECCBUF_DIV(elem_len), 703 &thislen, elem_base); 704 totlen += thislen; 705 if (ret || thislen != ECCBUF_DIV(elem_len)) 706 goto write_error; 707 to += thislen; 708 } 709 buflen = ECCBUF_MOD(elem_len); /* cut off tail */ 710 if (buflen) { 711 memset(buffer, 0xff, ECCBUF_SIZE); 712 memcpy(buffer, elem_base + thislen, buflen); 713 } 714 } 715 if (buflen) { /* flush last page, even if not full */ 716 /* This is sometimes intended behaviour, really */ 717 ret = mtd_write(mtd, to, buflen, &thislen, buffer); 718 totlen += thislen; 719 if (ret || thislen != ECCBUF_SIZE) 720 goto write_error; 721 } 722 write_error: 723 if (retlen) 724 *retlen = totlen; 725 kfree(buffer); 726 return ret; 727 } 728 729 730 static inline int do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr) 731 { 732 struct cfi_private *cfi = map->fldrv_priv; 733 map_word status, status_OK; 734 unsigned long timeo; 735 int retries = 3; 736 DECLARE_WAITQUEUE(wait, current); 737 int ret = 0; 738 739 adr += chip->start; 740 741 /* Let's determine this according to the interleave only once */ 742 status_OK = CMD(0x80); 743 744 timeo = jiffies + HZ; 745 retry: 746 mutex_lock(&chip->mutex); 747 748 /* Check that the chip's ready to talk to us. */ 749 switch (chip->state) { 750 case FL_CFI_QUERY: 751 case FL_JEDEC_QUERY: 752 case FL_READY: 753 map_write(map, CMD(0x70), adr); 754 chip->state = FL_STATUS; 755 fallthrough; 756 case FL_STATUS: 757 status = map_read(map, adr); 758 if (map_word_andequal(map, status, status_OK, status_OK)) 759 break; 760 761 /* Urgh. Chip not yet ready to talk to us. */ 762 if (time_after(jiffies, timeo)) { 763 mutex_unlock(&chip->mutex); 764 printk(KERN_ERR "waiting for chip to be ready timed out in erase\n"); 765 return -EIO; 766 } 767 768 /* Latency issues. Drop the lock, wait a while and retry */ 769 mutex_unlock(&chip->mutex); 770 cfi_udelay(1); 771 goto retry; 772 773 default: 774 /* Stick ourselves on a wait queue to be woken when 775 someone changes the status */ 776 set_current_state(TASK_UNINTERRUPTIBLE); 777 add_wait_queue(&chip->wq, &wait); 778 mutex_unlock(&chip->mutex); 779 schedule(); 780 remove_wait_queue(&chip->wq, &wait); 781 timeo = jiffies + HZ; 782 goto retry; 783 } 784 785 ENABLE_VPP(map); 786 /* Clear the status register first */ 787 map_write(map, CMD(0x50), adr); 788 789 /* Now erase */ 790 map_write(map, CMD(0x20), adr); 791 map_write(map, CMD(0xD0), adr); 792 chip->state = FL_ERASING; 793 794 mutex_unlock(&chip->mutex); 795 msleep(1000); 796 mutex_lock(&chip->mutex); 797 798 /* FIXME. Use a timer to check this, and return immediately. */ 799 /* Once the state machine's known to be working I'll do that */ 800 801 timeo = jiffies + (HZ*20); 802 for (;;) { 803 if (chip->state != FL_ERASING) { 804 /* Someone's suspended the erase. Sleep */ 805 set_current_state(TASK_UNINTERRUPTIBLE); 806 add_wait_queue(&chip->wq, &wait); 807 mutex_unlock(&chip->mutex); 808 schedule(); 809 remove_wait_queue(&chip->wq, &wait); 810 timeo = jiffies + (HZ*20); /* FIXME */ 811 mutex_lock(&chip->mutex); 812 continue; 813 } 814 815 status = map_read(map, adr); 816 if (map_word_andequal(map, status, status_OK, status_OK)) 817 break; 818 819 /* OK Still waiting */ 820 if (time_after(jiffies, timeo)) { 821 map_write(map, CMD(0x70), adr); 822 chip->state = FL_STATUS; 823 printk(KERN_ERR "waiting for erase to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]); 824 DISABLE_VPP(map); 825 mutex_unlock(&chip->mutex); 826 return -EIO; 827 } 828 829 /* Latency issues. Drop the lock, wait a while and retry */ 830 mutex_unlock(&chip->mutex); 831 cfi_udelay(1); 832 mutex_lock(&chip->mutex); 833 } 834 835 DISABLE_VPP(map); 836 ret = 0; 837 838 /* We've broken this before. It doesn't hurt to be safe */ 839 map_write(map, CMD(0x70), adr); 840 chip->state = FL_STATUS; 841 status = map_read(map, adr); 842 843 /* check for lock bit */ 844 if (map_word_bitsset(map, status, CMD(0x3a))) { 845 unsigned char chipstatus = status.x[0]; 846 if (!map_word_equal(map, status, CMD(chipstatus))) { 847 int i, w; 848 for (w=0; w<map_words(map); w++) { 849 for (i = 0; i<cfi_interleave(cfi); i++) { 850 chipstatus |= status.x[w] >> (cfi->device_type * 8); 851 } 852 } 853 printk(KERN_WARNING "Status is not identical for all chips: 0x%lx. Merging to give 0x%02x\n", 854 status.x[0], chipstatus); 855 } 856 /* Reset the error bits */ 857 map_write(map, CMD(0x50), adr); 858 map_write(map, CMD(0x70), adr); 859 860 if ((chipstatus & 0x30) == 0x30) { 861 printk(KERN_NOTICE "Chip reports improper command sequence: status 0x%x\n", chipstatus); 862 ret = -EIO; 863 } else if (chipstatus & 0x02) { 864 /* Protection bit set */ 865 ret = -EROFS; 866 } else if (chipstatus & 0x8) { 867 /* Voltage */ 868 printk(KERN_WARNING "Chip reports voltage low on erase: status 0x%x\n", chipstatus); 869 ret = -EIO; 870 } else if (chipstatus & 0x20) { 871 if (retries--) { 872 printk(KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x. Retrying...\n", adr, chipstatus); 873 timeo = jiffies + HZ; 874 chip->state = FL_STATUS; 875 mutex_unlock(&chip->mutex); 876 goto retry; 877 } 878 printk(KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x\n", adr, chipstatus); 879 ret = -EIO; 880 } 881 } 882 883 wake_up(&chip->wq); 884 mutex_unlock(&chip->mutex); 885 return ret; 886 } 887 888 static int cfi_staa_erase_varsize(struct mtd_info *mtd, 889 struct erase_info *instr) 890 { struct map_info *map = mtd->priv; 891 struct cfi_private *cfi = map->fldrv_priv; 892 unsigned long adr, len; 893 int chipnum, ret; 894 int i, first; 895 struct mtd_erase_region_info *regions = mtd->eraseregions; 896 897 /* Check that both start and end of the requested erase are 898 * aligned with the erasesize at the appropriate addresses. 899 */ 900 901 i = 0; 902 903 /* Skip all erase regions which are ended before the start of 904 the requested erase. Actually, to save on the calculations, 905 we skip to the first erase region which starts after the 906 start of the requested erase, and then go back one. 907 */ 908 909 while (i < mtd->numeraseregions && instr->addr >= regions[i].offset) 910 i++; 911 i--; 912 913 /* OK, now i is pointing at the erase region in which this 914 erase request starts. Check the start of the requested 915 erase range is aligned with the erase size which is in 916 effect here. 917 */ 918 919 if (instr->addr & (regions[i].erasesize-1)) 920 return -EINVAL; 921 922 /* Remember the erase region we start on */ 923 first = i; 924 925 /* Next, check that the end of the requested erase is aligned 926 * with the erase region at that address. 927 */ 928 929 while (i<mtd->numeraseregions && (instr->addr + instr->len) >= regions[i].offset) 930 i++; 931 932 /* As before, drop back one to point at the region in which 933 the address actually falls 934 */ 935 i--; 936 937 if ((instr->addr + instr->len) & (regions[i].erasesize-1)) 938 return -EINVAL; 939 940 chipnum = instr->addr >> cfi->chipshift; 941 adr = instr->addr - (chipnum << cfi->chipshift); 942 len = instr->len; 943 944 i=first; 945 946 while(len) { 947 ret = do_erase_oneblock(map, &cfi->chips[chipnum], adr); 948 949 if (ret) 950 return ret; 951 952 adr += regions[i].erasesize; 953 len -= regions[i].erasesize; 954 955 if (adr % (1<< cfi->chipshift) == (((unsigned long)regions[i].offset + (regions[i].erasesize * regions[i].numblocks)) %( 1<< cfi->chipshift))) 956 i++; 957 958 if (adr >> cfi->chipshift) { 959 adr = 0; 960 chipnum++; 961 962 if (chipnum >= cfi->numchips) 963 break; 964 } 965 } 966 967 return 0; 968 } 969 970 static void cfi_staa_sync (struct mtd_info *mtd) 971 { 972 struct map_info *map = mtd->priv; 973 struct cfi_private *cfi = map->fldrv_priv; 974 int i; 975 struct flchip *chip; 976 int ret = 0; 977 DECLARE_WAITQUEUE(wait, current); 978 979 for (i=0; !ret && i<cfi->numchips; i++) { 980 chip = &cfi->chips[i]; 981 982 retry: 983 mutex_lock(&chip->mutex); 984 985 switch(chip->state) { 986 case FL_READY: 987 case FL_STATUS: 988 case FL_CFI_QUERY: 989 case FL_JEDEC_QUERY: 990 chip->oldstate = chip->state; 991 chip->state = FL_SYNCING; 992 /* No need to wake_up() on this state change - 993 * as the whole point is that nobody can do anything 994 * with the chip now anyway. 995 */ 996 fallthrough; 997 case FL_SYNCING: 998 mutex_unlock(&chip->mutex); 999 break; 1000 1001 default: 1002 /* Not an idle state */ 1003 set_current_state(TASK_UNINTERRUPTIBLE); 1004 add_wait_queue(&chip->wq, &wait); 1005 1006 mutex_unlock(&chip->mutex); 1007 schedule(); 1008 remove_wait_queue(&chip->wq, &wait); 1009 1010 goto retry; 1011 } 1012 } 1013 1014 /* Unlock the chips again */ 1015 1016 for (i--; i >=0; i--) { 1017 chip = &cfi->chips[i]; 1018 1019 mutex_lock(&chip->mutex); 1020 1021 if (chip->state == FL_SYNCING) { 1022 chip->state = chip->oldstate; 1023 wake_up(&chip->wq); 1024 } 1025 mutex_unlock(&chip->mutex); 1026 } 1027 } 1028 1029 static inline int do_lock_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr) 1030 { 1031 struct cfi_private *cfi = map->fldrv_priv; 1032 map_word status, status_OK; 1033 unsigned long timeo = jiffies + HZ; 1034 DECLARE_WAITQUEUE(wait, current); 1035 1036 adr += chip->start; 1037 1038 /* Let's determine this according to the interleave only once */ 1039 status_OK = CMD(0x80); 1040 1041 timeo = jiffies + HZ; 1042 retry: 1043 mutex_lock(&chip->mutex); 1044 1045 /* Check that the chip's ready to talk to us. */ 1046 switch (chip->state) { 1047 case FL_CFI_QUERY: 1048 case FL_JEDEC_QUERY: 1049 case FL_READY: 1050 map_write(map, CMD(0x70), adr); 1051 chip->state = FL_STATUS; 1052 fallthrough; 1053 case FL_STATUS: 1054 status = map_read(map, adr); 1055 if (map_word_andequal(map, status, status_OK, status_OK)) 1056 break; 1057 1058 /* Urgh. Chip not yet ready to talk to us. */ 1059 if (time_after(jiffies, timeo)) { 1060 mutex_unlock(&chip->mutex); 1061 printk(KERN_ERR "waiting for chip to be ready timed out in lock\n"); 1062 return -EIO; 1063 } 1064 1065 /* Latency issues. Drop the lock, wait a while and retry */ 1066 mutex_unlock(&chip->mutex); 1067 cfi_udelay(1); 1068 goto retry; 1069 1070 default: 1071 /* Stick ourselves on a wait queue to be woken when 1072 someone changes the status */ 1073 set_current_state(TASK_UNINTERRUPTIBLE); 1074 add_wait_queue(&chip->wq, &wait); 1075 mutex_unlock(&chip->mutex); 1076 schedule(); 1077 remove_wait_queue(&chip->wq, &wait); 1078 timeo = jiffies + HZ; 1079 goto retry; 1080 } 1081 1082 ENABLE_VPP(map); 1083 map_write(map, CMD(0x60), adr); 1084 map_write(map, CMD(0x01), adr); 1085 chip->state = FL_LOCKING; 1086 1087 mutex_unlock(&chip->mutex); 1088 msleep(1000); 1089 mutex_lock(&chip->mutex); 1090 1091 /* FIXME. Use a timer to check this, and return immediately. */ 1092 /* Once the state machine's known to be working I'll do that */ 1093 1094 timeo = jiffies + (HZ*2); 1095 for (;;) { 1096 1097 status = map_read(map, adr); 1098 if (map_word_andequal(map, status, status_OK, status_OK)) 1099 break; 1100 1101 /* OK Still waiting */ 1102 if (time_after(jiffies, timeo)) { 1103 map_write(map, CMD(0x70), adr); 1104 chip->state = FL_STATUS; 1105 printk(KERN_ERR "waiting for lock to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]); 1106 DISABLE_VPP(map); 1107 mutex_unlock(&chip->mutex); 1108 return -EIO; 1109 } 1110 1111 /* Latency issues. Drop the lock, wait a while and retry */ 1112 mutex_unlock(&chip->mutex); 1113 cfi_udelay(1); 1114 mutex_lock(&chip->mutex); 1115 } 1116 1117 /* Done and happy. */ 1118 chip->state = FL_STATUS; 1119 DISABLE_VPP(map); 1120 wake_up(&chip->wq); 1121 mutex_unlock(&chip->mutex); 1122 return 0; 1123 } 1124 static int cfi_staa_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 1125 { 1126 struct map_info *map = mtd->priv; 1127 struct cfi_private *cfi = map->fldrv_priv; 1128 unsigned long adr; 1129 int chipnum, ret; 1130 #ifdef DEBUG_LOCK_BITS 1131 int ofs_factor = cfi->interleave * cfi->device_type; 1132 #endif 1133 1134 if (ofs & (mtd->erasesize - 1)) 1135 return -EINVAL; 1136 1137 if (len & (mtd->erasesize -1)) 1138 return -EINVAL; 1139 1140 chipnum = ofs >> cfi->chipshift; 1141 adr = ofs - (chipnum << cfi->chipshift); 1142 1143 while(len) { 1144 1145 #ifdef DEBUG_LOCK_BITS 1146 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL); 1147 printk("before lock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor))); 1148 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL); 1149 #endif 1150 1151 ret = do_lock_oneblock(map, &cfi->chips[chipnum], adr); 1152 1153 #ifdef DEBUG_LOCK_BITS 1154 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL); 1155 printk("after lock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor))); 1156 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL); 1157 #endif 1158 1159 if (ret) 1160 return ret; 1161 1162 adr += mtd->erasesize; 1163 len -= mtd->erasesize; 1164 1165 if (adr >> cfi->chipshift) { 1166 adr = 0; 1167 chipnum++; 1168 1169 if (chipnum >= cfi->numchips) 1170 break; 1171 } 1172 } 1173 return 0; 1174 } 1175 static inline int do_unlock_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr) 1176 { 1177 struct cfi_private *cfi = map->fldrv_priv; 1178 map_word status, status_OK; 1179 unsigned long timeo = jiffies + HZ; 1180 DECLARE_WAITQUEUE(wait, current); 1181 1182 adr += chip->start; 1183 1184 /* Let's determine this according to the interleave only once */ 1185 status_OK = CMD(0x80); 1186 1187 timeo = jiffies + HZ; 1188 retry: 1189 mutex_lock(&chip->mutex); 1190 1191 /* Check that the chip's ready to talk to us. */ 1192 switch (chip->state) { 1193 case FL_CFI_QUERY: 1194 case FL_JEDEC_QUERY: 1195 case FL_READY: 1196 map_write(map, CMD(0x70), adr); 1197 chip->state = FL_STATUS; 1198 fallthrough; 1199 case FL_STATUS: 1200 status = map_read(map, adr); 1201 if (map_word_andequal(map, status, status_OK, status_OK)) 1202 break; 1203 1204 /* Urgh. Chip not yet ready to talk to us. */ 1205 if (time_after(jiffies, timeo)) { 1206 mutex_unlock(&chip->mutex); 1207 printk(KERN_ERR "waiting for chip to be ready timed out in unlock\n"); 1208 return -EIO; 1209 } 1210 1211 /* Latency issues. Drop the lock, wait a while and retry */ 1212 mutex_unlock(&chip->mutex); 1213 cfi_udelay(1); 1214 goto retry; 1215 1216 default: 1217 /* Stick ourselves on a wait queue to be woken when 1218 someone changes the status */ 1219 set_current_state(TASK_UNINTERRUPTIBLE); 1220 add_wait_queue(&chip->wq, &wait); 1221 mutex_unlock(&chip->mutex); 1222 schedule(); 1223 remove_wait_queue(&chip->wq, &wait); 1224 timeo = jiffies + HZ; 1225 goto retry; 1226 } 1227 1228 ENABLE_VPP(map); 1229 map_write(map, CMD(0x60), adr); 1230 map_write(map, CMD(0xD0), adr); 1231 chip->state = FL_UNLOCKING; 1232 1233 mutex_unlock(&chip->mutex); 1234 msleep(1000); 1235 mutex_lock(&chip->mutex); 1236 1237 /* FIXME. Use a timer to check this, and return immediately. */ 1238 /* Once the state machine's known to be working I'll do that */ 1239 1240 timeo = jiffies + (HZ*2); 1241 for (;;) { 1242 1243 status = map_read(map, adr); 1244 if (map_word_andequal(map, status, status_OK, status_OK)) 1245 break; 1246 1247 /* OK Still waiting */ 1248 if (time_after(jiffies, timeo)) { 1249 map_write(map, CMD(0x70), adr); 1250 chip->state = FL_STATUS; 1251 printk(KERN_ERR "waiting for unlock to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]); 1252 DISABLE_VPP(map); 1253 mutex_unlock(&chip->mutex); 1254 return -EIO; 1255 } 1256 1257 /* Latency issues. Drop the unlock, wait a while and retry */ 1258 mutex_unlock(&chip->mutex); 1259 cfi_udelay(1); 1260 mutex_lock(&chip->mutex); 1261 } 1262 1263 /* Done and happy. */ 1264 chip->state = FL_STATUS; 1265 DISABLE_VPP(map); 1266 wake_up(&chip->wq); 1267 mutex_unlock(&chip->mutex); 1268 return 0; 1269 } 1270 static int cfi_staa_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 1271 { 1272 struct map_info *map = mtd->priv; 1273 struct cfi_private *cfi = map->fldrv_priv; 1274 unsigned long adr; 1275 int chipnum, ret; 1276 #ifdef DEBUG_LOCK_BITS 1277 int ofs_factor = cfi->interleave * cfi->device_type; 1278 #endif 1279 1280 chipnum = ofs >> cfi->chipshift; 1281 adr = ofs - (chipnum << cfi->chipshift); 1282 1283 #ifdef DEBUG_LOCK_BITS 1284 { 1285 unsigned long temp_adr = adr; 1286 unsigned long temp_len = len; 1287 1288 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL); 1289 while (temp_len) { 1290 printk("before unlock %x: block status register is %x\n",temp_adr,cfi_read_query(map, temp_adr+(2*ofs_factor))); 1291 temp_adr += mtd->erasesize; 1292 temp_len -= mtd->erasesize; 1293 } 1294 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL); 1295 } 1296 #endif 1297 1298 ret = do_unlock_oneblock(map, &cfi->chips[chipnum], adr); 1299 1300 #ifdef DEBUG_LOCK_BITS 1301 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL); 1302 printk("after unlock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor))); 1303 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL); 1304 #endif 1305 1306 return ret; 1307 } 1308 1309 static int cfi_staa_suspend(struct mtd_info *mtd) 1310 { 1311 struct map_info *map = mtd->priv; 1312 struct cfi_private *cfi = map->fldrv_priv; 1313 int i; 1314 struct flchip *chip; 1315 int ret = 0; 1316 1317 for (i=0; !ret && i<cfi->numchips; i++) { 1318 chip = &cfi->chips[i]; 1319 1320 mutex_lock(&chip->mutex); 1321 1322 switch(chip->state) { 1323 case FL_READY: 1324 case FL_STATUS: 1325 case FL_CFI_QUERY: 1326 case FL_JEDEC_QUERY: 1327 chip->oldstate = chip->state; 1328 chip->state = FL_PM_SUSPENDED; 1329 /* No need to wake_up() on this state change - 1330 * as the whole point is that nobody can do anything 1331 * with the chip now anyway. 1332 */ 1333 break; 1334 1335 case FL_PM_SUSPENDED: 1336 break; 1337 1338 default: 1339 ret = -EAGAIN; 1340 break; 1341 } 1342 mutex_unlock(&chip->mutex); 1343 } 1344 1345 /* Unlock the chips again */ 1346 1347 if (ret) { 1348 for (i--; i >=0; i--) { 1349 chip = &cfi->chips[i]; 1350 1351 mutex_lock(&chip->mutex); 1352 1353 if (chip->state == FL_PM_SUSPENDED) { 1354 /* No need to force it into a known state here, 1355 because we're returning failure, and it didn't 1356 get power cycled */ 1357 chip->state = chip->oldstate; 1358 wake_up(&chip->wq); 1359 } 1360 mutex_unlock(&chip->mutex); 1361 } 1362 } 1363 1364 return ret; 1365 } 1366 1367 static void cfi_staa_resume(struct mtd_info *mtd) 1368 { 1369 struct map_info *map = mtd->priv; 1370 struct cfi_private *cfi = map->fldrv_priv; 1371 int i; 1372 struct flchip *chip; 1373 1374 for (i=0; i<cfi->numchips; i++) { 1375 1376 chip = &cfi->chips[i]; 1377 1378 mutex_lock(&chip->mutex); 1379 1380 /* Go to known state. Chip may have been power cycled */ 1381 if (chip->state == FL_PM_SUSPENDED) { 1382 map_write(map, CMD(0xFF), 0); 1383 chip->state = FL_READY; 1384 wake_up(&chip->wq); 1385 } 1386 1387 mutex_unlock(&chip->mutex); 1388 } 1389 } 1390 1391 static void cfi_staa_destroy(struct mtd_info *mtd) 1392 { 1393 struct map_info *map = mtd->priv; 1394 struct cfi_private *cfi = map->fldrv_priv; 1395 kfree(cfi->cmdset_priv); 1396 kfree(cfi); 1397 } 1398 1399 MODULE_DESCRIPTION("MTD chip driver for ST Advanced Architecture Command Set (ID 0x0020)"); 1400 MODULE_LICENSE("GPL"); 1401