cfi_cmdset_0020.c 37 KB

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