cfi_cmdset_0020.c 37 KB

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