cfi_cmdset_0020.c 38 KB

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