cfi_cmdset_0002.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. /*
  2. * Common Flash Interface support:
  3. * AMD & Fujitsu Standard Vendor Command Set (ID 0x0002)
  4. *
  5. * Copyright (C) 2000 Crossnet Co. <info@crossnet.co.jp>
  6. * Copyright (C) 2004 Arcom Control Systems Ltd <linux@arcom.com>
  7. * Copyright (C) 2005 MontaVista Software Inc. <source@mvista.com>
  8. *
  9. * 2_by_8 routines added by Simon Munton
  10. *
  11. * 4_by_16 work by Carolyn J. Smith
  12. *
  13. * XIP support hooks by Vitaly Wool (based on code for Intel flash
  14. * by Nicolas Pitre)
  15. *
  16. * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
  17. *
  18. * This code is GPL
  19. *
  20. * $Id: cfi_cmdset_0002.c,v 1.122 2005/11/07 11:14:22 gleixner Exp $
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/sched.h>
  27. #include <linux/init.h>
  28. #include <asm/io.h>
  29. #include <asm/byteorder.h>
  30. #include <linux/errno.h>
  31. #include <linux/slab.h>
  32. #include <linux/delay.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/mtd/compatmac.h>
  35. #include <linux/mtd/map.h>
  36. #include <linux/mtd/mtd.h>
  37. #include <linux/mtd/cfi.h>
  38. #include <linux/mtd/xip.h>
  39. #define AMD_BOOTLOC_BUG
  40. #define FORCE_WORD_WRITE 0
  41. #define MAX_WORD_RETRIES 3
  42. #define MANUFACTURER_AMD 0x0001
  43. #define MANUFACTURER_ATMEL 0x001F
  44. #define MANUFACTURER_SST 0x00BF
  45. #define SST49LF004B 0x0060
  46. #define SST49LF040B 0x0050
  47. #define SST49LF008A 0x005a
  48. #define AT49BV6416 0x00d6
  49. static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  50. static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  51. static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  52. static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
  53. static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
  54. static void cfi_amdstd_sync (struct mtd_info *);
  55. static int cfi_amdstd_suspend (struct mtd_info *);
  56. static void cfi_amdstd_resume (struct mtd_info *);
  57. static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  58. static void cfi_amdstd_destroy(struct mtd_info *);
  59. struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
  60. static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
  61. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
  62. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
  63. #include "fwh_lock.h"
  64. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, size_t len);
  65. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, size_t len);
  66. static struct mtd_chip_driver cfi_amdstd_chipdrv = {
  67. .probe = NULL, /* Not usable directly */
  68. .destroy = cfi_amdstd_destroy,
  69. .name = "cfi_cmdset_0002",
  70. .module = THIS_MODULE
  71. };
  72. /* #define DEBUG_CFI_FEATURES */
  73. #ifdef DEBUG_CFI_FEATURES
  74. static void cfi_tell_features(struct cfi_pri_amdstd *extp)
  75. {
  76. const char* erase_suspend[3] = {
  77. "Not supported", "Read only", "Read/write"
  78. };
  79. const char* top_bottom[6] = {
  80. "No WP", "8x8KiB sectors at top & bottom, no WP",
  81. "Bottom boot", "Top boot",
  82. "Uniform, Bottom WP", "Uniform, Top WP"
  83. };
  84. printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
  85. printk(" Address sensitive unlock: %s\n",
  86. (extp->SiliconRevision & 1) ? "Not required" : "Required");
  87. if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
  88. printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
  89. else
  90. printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
  91. if (extp->BlkProt == 0)
  92. printk(" Block protection: Not supported\n");
  93. else
  94. printk(" Block protection: %d sectors per group\n", extp->BlkProt);
  95. printk(" Temporary block unprotect: %s\n",
  96. extp->TmpBlkUnprotect ? "Supported" : "Not supported");
  97. printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
  98. printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
  99. printk(" Burst mode: %s\n",
  100. extp->BurstMode ? "Supported" : "Not supported");
  101. if (extp->PageMode == 0)
  102. printk(" Page mode: Not supported\n");
  103. else
  104. printk(" Page mode: %d word page\n", extp->PageMode << 2);
  105. printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
  106. extp->VppMin >> 4, extp->VppMin & 0xf);
  107. printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
  108. extp->VppMax >> 4, extp->VppMax & 0xf);
  109. if (extp->TopBottom < ARRAY_SIZE(top_bottom))
  110. printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
  111. else
  112. printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
  113. }
  114. #endif
  115. #ifdef AMD_BOOTLOC_BUG
  116. /* Wheee. Bring me the head of someone at AMD. */
  117. static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
  118. {
  119. struct map_info *map = mtd->priv;
  120. struct cfi_private *cfi = map->fldrv_priv;
  121. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  122. __u8 major = extp->MajorVersion;
  123. __u8 minor = extp->MinorVersion;
  124. if (((major << 8) | minor) < 0x3131) {
  125. /* CFI version 1.0 => don't trust bootloc */
  126. if (cfi->id & 0x80) {
  127. printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
  128. extp->TopBottom = 3; /* top boot */
  129. } else {
  130. extp->TopBottom = 2; /* bottom boot */
  131. }
  132. }
  133. }
  134. #endif
  135. static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
  136. {
  137. struct map_info *map = mtd->priv;
  138. struct cfi_private *cfi = map->fldrv_priv;
  139. if (cfi->cfiq->BufWriteTimeoutTyp) {
  140. DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
  141. mtd->write = cfi_amdstd_write_buffers;
  142. }
  143. }
  144. /* Atmel chips don't use the same PRI format as AMD chips */
  145. static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
  146. {
  147. struct map_info *map = mtd->priv;
  148. struct cfi_private *cfi = map->fldrv_priv;
  149. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  150. struct cfi_pri_atmel atmel_pri;
  151. memcpy(&atmel_pri, extp, sizeof(atmel_pri));
  152. memset((char *)extp + 5, 0, sizeof(*extp) - 5);
  153. if (atmel_pri.Features & 0x02)
  154. extp->EraseSuspend = 2;
  155. if (atmel_pri.BottomBoot)
  156. extp->TopBottom = 2;
  157. else
  158. extp->TopBottom = 3;
  159. /* burst write mode not supported */
  160. cfi->cfiq->BufWriteTimeoutTyp = 0;
  161. cfi->cfiq->BufWriteTimeoutMax = 0;
  162. }
  163. static void fixup_use_secsi(struct mtd_info *mtd, void *param)
  164. {
  165. /* Setup for chips with a secsi area */
  166. mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
  167. mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
  168. }
  169. static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
  170. {
  171. struct map_info *map = mtd->priv;
  172. struct cfi_private *cfi = map->fldrv_priv;
  173. if ((cfi->cfiq->NumEraseRegions == 1) &&
  174. ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
  175. mtd->erase = cfi_amdstd_erase_chip;
  176. }
  177. }
  178. /*
  179. * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
  180. * locked by default.
  181. */
  182. static void fixup_use_atmel_lock(struct mtd_info *mtd, void *param)
  183. {
  184. mtd->lock = cfi_atmel_lock;
  185. mtd->unlock = cfi_atmel_unlock;
  186. mtd->flags |= MTD_POWERUP_LOCK;
  187. }
  188. static struct cfi_fixup cfi_fixup_table[] = {
  189. { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
  190. #ifdef AMD_BOOTLOC_BUG
  191. { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
  192. #endif
  193. { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
  194. { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
  195. { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
  196. { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
  197. { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
  198. { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
  199. #if !FORCE_WORD_WRITE
  200. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
  201. #endif
  202. { 0, 0, NULL, NULL }
  203. };
  204. static struct cfi_fixup jedec_fixup_table[] = {
  205. { MANUFACTURER_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
  206. { MANUFACTURER_SST, SST49LF040B, fixup_use_fwh_lock, NULL, },
  207. { MANUFACTURER_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
  208. { 0, 0, NULL, NULL }
  209. };
  210. static struct cfi_fixup fixup_table[] = {
  211. /* The CFI vendor ids and the JEDEC vendor IDs appear
  212. * to be common. It is like the devices id's are as
  213. * well. This table is to pick all cases where
  214. * we know that is the case.
  215. */
  216. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
  217. { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock, NULL },
  218. { 0, 0, NULL, NULL }
  219. };
  220. struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
  221. {
  222. struct cfi_private *cfi = map->fldrv_priv;
  223. struct mtd_info *mtd;
  224. int i;
  225. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  226. if (!mtd) {
  227. printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
  228. return NULL;
  229. }
  230. mtd->priv = map;
  231. mtd->type = MTD_NORFLASH;
  232. /* Fill in the default mtd operations */
  233. mtd->erase = cfi_amdstd_erase_varsize;
  234. mtd->write = cfi_amdstd_write_words;
  235. mtd->read = cfi_amdstd_read;
  236. mtd->sync = cfi_amdstd_sync;
  237. mtd->suspend = cfi_amdstd_suspend;
  238. mtd->resume = cfi_amdstd_resume;
  239. mtd->flags = MTD_CAP_NORFLASH;
  240. mtd->name = map->name;
  241. mtd->writesize = 1;
  242. if (cfi->cfi_mode==CFI_MODE_CFI){
  243. unsigned char bootloc;
  244. /*
  245. * It's a real CFI chip, not one for which the probe
  246. * routine faked a CFI structure. So we read the feature
  247. * table from it.
  248. */
  249. __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
  250. struct cfi_pri_amdstd *extp;
  251. extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
  252. if (!extp) {
  253. kfree(mtd);
  254. return NULL;
  255. }
  256. if (extp->MajorVersion != '1' ||
  257. (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
  258. printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
  259. "version %c.%c.\n", extp->MajorVersion,
  260. extp->MinorVersion);
  261. kfree(extp);
  262. kfree(mtd);
  263. return NULL;
  264. }
  265. /* Install our own private info structure */
  266. cfi->cmdset_priv = extp;
  267. /* Apply cfi device specific fixups */
  268. cfi_fixup(mtd, cfi_fixup_table);
  269. #ifdef DEBUG_CFI_FEATURES
  270. /* Tell the user about it in lots of lovely detail */
  271. cfi_tell_features(extp);
  272. #endif
  273. bootloc = extp->TopBottom;
  274. if ((bootloc != 2) && (bootloc != 3)) {
  275. printk(KERN_WARNING "%s: CFI does not contain boot "
  276. "bank location. Assuming top.\n", map->name);
  277. bootloc = 2;
  278. }
  279. if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
  280. printk(KERN_WARNING "%s: Swapping erase regions for broken CFI table.\n", map->name);
  281. for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
  282. int j = (cfi->cfiq->NumEraseRegions-1)-i;
  283. __u32 swap;
  284. swap = cfi->cfiq->EraseRegionInfo[i];
  285. cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
  286. cfi->cfiq->EraseRegionInfo[j] = swap;
  287. }
  288. }
  289. /* Set the default CFI lock/unlock addresses */
  290. cfi->addr_unlock1 = 0x555;
  291. cfi->addr_unlock2 = 0x2aa;
  292. /* Modify the unlock address if we are in compatibility mode */
  293. if ( /* x16 in x8 mode */
  294. ((cfi->device_type == CFI_DEVICETYPE_X8) &&
  295. (cfi->cfiq->InterfaceDesc ==
  296. CFI_INTERFACE_X8_BY_X16_ASYNC)) ||
  297. /* x32 in x16 mode */
  298. ((cfi->device_type == CFI_DEVICETYPE_X16) &&
  299. (cfi->cfiq->InterfaceDesc ==
  300. CFI_INTERFACE_X16_BY_X32_ASYNC)))
  301. {
  302. cfi->addr_unlock1 = 0xaaa;
  303. cfi->addr_unlock2 = 0x555;
  304. }
  305. } /* CFI mode */
  306. else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
  307. /* Apply jedec specific fixups */
  308. cfi_fixup(mtd, jedec_fixup_table);
  309. }
  310. /* Apply generic fixups */
  311. cfi_fixup(mtd, fixup_table);
  312. for (i=0; i< cfi->numchips; i++) {
  313. cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
  314. cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
  315. cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
  316. cfi->chips[i].ref_point_counter = 0;
  317. init_waitqueue_head(&(cfi->chips[i].wq));
  318. }
  319. map->fldrv = &cfi_amdstd_chipdrv;
  320. return cfi_amdstd_setup(mtd);
  321. }
  322. EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
  323. static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
  324. {
  325. struct map_info *map = mtd->priv;
  326. struct cfi_private *cfi = map->fldrv_priv;
  327. unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
  328. unsigned long offset = 0;
  329. int i,j;
  330. printk(KERN_NOTICE "number of %s chips: %d\n",
  331. (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
  332. /* Select the correct geometry setup */
  333. mtd->size = devsize * cfi->numchips;
  334. mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
  335. mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
  336. * mtd->numeraseregions, GFP_KERNEL);
  337. if (!mtd->eraseregions) {
  338. printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
  339. goto setup_err;
  340. }
  341. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  342. unsigned long ernum, ersize;
  343. ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
  344. ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
  345. if (mtd->erasesize < ersize) {
  346. mtd->erasesize = ersize;
  347. }
  348. for (j=0; j<cfi->numchips; j++) {
  349. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
  350. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
  351. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
  352. }
  353. offset += (ersize * ernum);
  354. }
  355. if (offset != devsize) {
  356. /* Argh */
  357. printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
  358. goto setup_err;
  359. }
  360. #if 0
  361. // debug
  362. for (i=0; i<mtd->numeraseregions;i++){
  363. printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
  364. i,mtd->eraseregions[i].offset,
  365. mtd->eraseregions[i].erasesize,
  366. mtd->eraseregions[i].numblocks);
  367. }
  368. #endif
  369. /* FIXME: erase-suspend-program is broken. See
  370. http://lists.infradead.org/pipermail/linux-mtd/2003-December/009001.html */
  371. printk(KERN_NOTICE "cfi_cmdset_0002: Disabling erase-suspend-program due to code brokenness.\n");
  372. __module_get(THIS_MODULE);
  373. return mtd;
  374. setup_err:
  375. if(mtd) {
  376. kfree(mtd->eraseregions);
  377. kfree(mtd);
  378. }
  379. kfree(cfi->cmdset_priv);
  380. kfree(cfi->cfiq);
  381. return NULL;
  382. }
  383. /*
  384. * Return true if the chip is ready.
  385. *
  386. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  387. * non-suspended sector) and is indicated by no toggle bits toggling.
  388. *
  389. * Note that anything more complicated than checking if no bits are toggling
  390. * (including checking DQ5 for an error status) is tricky to get working
  391. * correctly and is therefore not done (particulary with interleaved chips
  392. * as each chip must be checked independantly of the others).
  393. */
  394. static int __xipram chip_ready(struct map_info *map, unsigned long addr)
  395. {
  396. map_word d, t;
  397. d = map_read(map, addr);
  398. t = map_read(map, addr);
  399. return map_word_equal(map, d, t);
  400. }
  401. /*
  402. * Return true if the chip is ready and has the correct value.
  403. *
  404. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  405. * non-suspended sector) and it is indicated by no bits toggling.
  406. *
  407. * Error are indicated by toggling bits or bits held with the wrong value,
  408. * or with bits toggling.
  409. *
  410. * Note that anything more complicated than checking if no bits are toggling
  411. * (including checking DQ5 for an error status) is tricky to get working
  412. * correctly and is therefore not done (particulary with interleaved chips
  413. * as each chip must be checked independantly of the others).
  414. *
  415. */
  416. static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
  417. {
  418. map_word oldd, curd;
  419. oldd = map_read(map, addr);
  420. curd = map_read(map, addr);
  421. return map_word_equal(map, oldd, curd) &&
  422. map_word_equal(map, curd, expected);
  423. }
  424. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  425. {
  426. DECLARE_WAITQUEUE(wait, current);
  427. struct cfi_private *cfi = map->fldrv_priv;
  428. unsigned long timeo;
  429. struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
  430. resettime:
  431. timeo = jiffies + HZ;
  432. retry:
  433. switch (chip->state) {
  434. case FL_STATUS:
  435. for (;;) {
  436. if (chip_ready(map, adr))
  437. break;
  438. if (time_after(jiffies, timeo)) {
  439. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  440. spin_unlock(chip->mutex);
  441. return -EIO;
  442. }
  443. spin_unlock(chip->mutex);
  444. cfi_udelay(1);
  445. spin_lock(chip->mutex);
  446. /* Someone else might have been playing with it. */
  447. goto retry;
  448. }
  449. case FL_READY:
  450. case FL_CFI_QUERY:
  451. case FL_JEDEC_QUERY:
  452. return 0;
  453. case FL_ERASING:
  454. if (mode == FL_WRITING) /* FIXME: Erase-suspend-program appears broken. */
  455. goto sleep;
  456. if (!( mode == FL_READY
  457. || mode == FL_POINT
  458. || !cfip
  459. || (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))
  460. || (mode == FL_WRITING && (cfip->EraseSuspend & 0x1)
  461. )))
  462. goto sleep;
  463. /* We could check to see if we're trying to access the sector
  464. * that is currently being erased. However, no user will try
  465. * anything like that so we just wait for the timeout. */
  466. /* Erase suspend */
  467. /* It's harmless to issue the Erase-Suspend and Erase-Resume
  468. * commands when the erase algorithm isn't in progress. */
  469. map_write(map, CMD(0xB0), chip->in_progress_block_addr);
  470. chip->oldstate = FL_ERASING;
  471. chip->state = FL_ERASE_SUSPENDING;
  472. chip->erase_suspended = 1;
  473. for (;;) {
  474. if (chip_ready(map, adr))
  475. break;
  476. if (time_after(jiffies, timeo)) {
  477. /* Should have suspended the erase by now.
  478. * Send an Erase-Resume command as either
  479. * there was an error (so leave the erase
  480. * routine to recover from it) or we trying to
  481. * use the erase-in-progress sector. */
  482. map_write(map, CMD(0x30), chip->in_progress_block_addr);
  483. chip->state = FL_ERASING;
  484. chip->oldstate = FL_READY;
  485. printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
  486. return -EIO;
  487. }
  488. spin_unlock(chip->mutex);
  489. cfi_udelay(1);
  490. spin_lock(chip->mutex);
  491. /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
  492. So we can just loop here. */
  493. }
  494. chip->state = FL_READY;
  495. return 0;
  496. case FL_XIP_WHILE_ERASING:
  497. if (mode != FL_READY && mode != FL_POINT &&
  498. (!cfip || !(cfip->EraseSuspend&2)))
  499. goto sleep;
  500. chip->oldstate = chip->state;
  501. chip->state = FL_READY;
  502. return 0;
  503. case FL_POINT:
  504. /* Only if there's no operation suspended... */
  505. if (mode == FL_READY && chip->oldstate == FL_READY)
  506. return 0;
  507. default:
  508. sleep:
  509. set_current_state(TASK_UNINTERRUPTIBLE);
  510. add_wait_queue(&chip->wq, &wait);
  511. spin_unlock(chip->mutex);
  512. schedule();
  513. remove_wait_queue(&chip->wq, &wait);
  514. spin_lock(chip->mutex);
  515. goto resettime;
  516. }
  517. }
  518. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
  519. {
  520. struct cfi_private *cfi = map->fldrv_priv;
  521. switch(chip->oldstate) {
  522. case FL_ERASING:
  523. chip->state = chip->oldstate;
  524. map_write(map, CMD(0x30), chip->in_progress_block_addr);
  525. chip->oldstate = FL_READY;
  526. chip->state = FL_ERASING;
  527. break;
  528. case FL_XIP_WHILE_ERASING:
  529. chip->state = chip->oldstate;
  530. chip->oldstate = FL_READY;
  531. break;
  532. case FL_READY:
  533. case FL_STATUS:
  534. /* We should really make set_vpp() count, rather than doing this */
  535. DISABLE_VPP(map);
  536. break;
  537. default:
  538. printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
  539. }
  540. wake_up(&chip->wq);
  541. }
  542. #ifdef CONFIG_MTD_XIP
  543. /*
  544. * No interrupt what so ever can be serviced while the flash isn't in array
  545. * mode. This is ensured by the xip_disable() and xip_enable() functions
  546. * enclosing any code path where the flash is known not to be in array mode.
  547. * And within a XIP disabled code path, only functions marked with __xipram
  548. * may be called and nothing else (it's a good thing to inspect generated
  549. * assembly to make sure inline functions were actually inlined and that gcc
  550. * didn't emit calls to its own support functions). Also configuring MTD CFI
  551. * support to a single buswidth and a single interleave is also recommended.
  552. */
  553. static void xip_disable(struct map_info *map, struct flchip *chip,
  554. unsigned long adr)
  555. {
  556. /* TODO: chips with no XIP use should ignore and return */
  557. (void) map_read(map, adr); /* ensure mmu mapping is up to date */
  558. local_irq_disable();
  559. }
  560. static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
  561. unsigned long adr)
  562. {
  563. struct cfi_private *cfi = map->fldrv_priv;
  564. if (chip->state != FL_POINT && chip->state != FL_READY) {
  565. map_write(map, CMD(0xf0), adr);
  566. chip->state = FL_READY;
  567. }
  568. (void) map_read(map, adr);
  569. xip_iprefetch();
  570. local_irq_enable();
  571. }
  572. /*
  573. * When a delay is required for the flash operation to complete, the
  574. * xip_udelay() function is polling for both the given timeout and pending
  575. * (but still masked) hardware interrupts. Whenever there is an interrupt
  576. * pending then the flash erase operation is suspended, array mode restored
  577. * and interrupts unmasked. Task scheduling might also happen at that
  578. * point. The CPU eventually returns from the interrupt or the call to
  579. * schedule() and the suspended flash operation is resumed for the remaining
  580. * of the delay period.
  581. *
  582. * Warning: this function _will_ fool interrupt latency tracing tools.
  583. */
  584. static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
  585. unsigned long adr, int usec)
  586. {
  587. struct cfi_private *cfi = map->fldrv_priv;
  588. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  589. map_word status, OK = CMD(0x80);
  590. unsigned long suspended, start = xip_currtime();
  591. flstate_t oldstate;
  592. do {
  593. cpu_relax();
  594. if (xip_irqpending() && extp &&
  595. ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
  596. (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
  597. /*
  598. * Let's suspend the erase operation when supported.
  599. * Note that we currently don't try to suspend
  600. * interleaved chips if there is already another
  601. * operation suspended (imagine what happens
  602. * when one chip was already done with the current
  603. * operation while another chip suspended it, then
  604. * we resume the whole thing at once). Yes, it
  605. * can happen!
  606. */
  607. map_write(map, CMD(0xb0), adr);
  608. usec -= xip_elapsed_since(start);
  609. suspended = xip_currtime();
  610. do {
  611. if (xip_elapsed_since(suspended) > 100000) {
  612. /*
  613. * The chip doesn't want to suspend
  614. * after waiting for 100 msecs.
  615. * This is a critical error but there
  616. * is not much we can do here.
  617. */
  618. return;
  619. }
  620. status = map_read(map, adr);
  621. } while (!map_word_andequal(map, status, OK, OK));
  622. /* Suspend succeeded */
  623. oldstate = chip->state;
  624. if (!map_word_bitsset(map, status, CMD(0x40)))
  625. break;
  626. chip->state = FL_XIP_WHILE_ERASING;
  627. chip->erase_suspended = 1;
  628. map_write(map, CMD(0xf0), adr);
  629. (void) map_read(map, adr);
  630. asm volatile (".rep 8; nop; .endr");
  631. local_irq_enable();
  632. spin_unlock(chip->mutex);
  633. asm volatile (".rep 8; nop; .endr");
  634. cond_resched();
  635. /*
  636. * We're back. However someone else might have
  637. * decided to go write to the chip if we are in
  638. * a suspended erase state. If so let's wait
  639. * until it's done.
  640. */
  641. spin_lock(chip->mutex);
  642. while (chip->state != FL_XIP_WHILE_ERASING) {
  643. DECLARE_WAITQUEUE(wait, current);
  644. set_current_state(TASK_UNINTERRUPTIBLE);
  645. add_wait_queue(&chip->wq, &wait);
  646. spin_unlock(chip->mutex);
  647. schedule();
  648. remove_wait_queue(&chip->wq, &wait);
  649. spin_lock(chip->mutex);
  650. }
  651. /* Disallow XIP again */
  652. local_irq_disable();
  653. /* Resume the write or erase operation */
  654. map_write(map, CMD(0x30), adr);
  655. chip->state = oldstate;
  656. start = xip_currtime();
  657. } else if (usec >= 1000000/HZ) {
  658. /*
  659. * Try to save on CPU power when waiting delay
  660. * is at least a system timer tick period.
  661. * No need to be extremely accurate here.
  662. */
  663. xip_cpu_idle();
  664. }
  665. status = map_read(map, adr);
  666. } while (!map_word_andequal(map, status, OK, OK)
  667. && xip_elapsed_since(start) < usec);
  668. }
  669. #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
  670. /*
  671. * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
  672. * the flash is actively programming or erasing since we have to poll for
  673. * the operation to complete anyway. We can't do that in a generic way with
  674. * a XIP setup so do it before the actual flash operation in this case
  675. * and stub it out from INVALIDATE_CACHE_UDELAY.
  676. */
  677. #define XIP_INVAL_CACHED_RANGE(map, from, size) \
  678. INVALIDATE_CACHED_RANGE(map, from, size)
  679. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  680. UDELAY(map, chip, adr, usec)
  681. /*
  682. * Extra notes:
  683. *
  684. * Activating this XIP support changes the way the code works a bit. For
  685. * example the code to suspend the current process when concurrent access
  686. * happens is never executed because xip_udelay() will always return with the
  687. * same chip state as it was entered with. This is why there is no care for
  688. * the presence of add_wait_queue() or schedule() calls from within a couple
  689. * xip_disable()'d areas of code, like in do_erase_oneblock for example.
  690. * The queueing and scheduling are always happening within xip_udelay().
  691. *
  692. * Similarly, get_chip() and put_chip() just happen to always be executed
  693. * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
  694. * is in array mode, therefore never executing many cases therein and not
  695. * causing any problem with XIP.
  696. */
  697. #else
  698. #define xip_disable(map, chip, adr)
  699. #define xip_enable(map, chip, adr)
  700. #define XIP_INVAL_CACHED_RANGE(x...)
  701. #define UDELAY(map, chip, adr, usec) \
  702. do { \
  703. spin_unlock(chip->mutex); \
  704. cfi_udelay(usec); \
  705. spin_lock(chip->mutex); \
  706. } while (0)
  707. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  708. do { \
  709. spin_unlock(chip->mutex); \
  710. INVALIDATE_CACHED_RANGE(map, adr, len); \
  711. cfi_udelay(usec); \
  712. spin_lock(chip->mutex); \
  713. } while (0)
  714. #endif
  715. static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  716. {
  717. unsigned long cmd_addr;
  718. struct cfi_private *cfi = map->fldrv_priv;
  719. int ret;
  720. adr += chip->start;
  721. /* Ensure cmd read/writes are aligned. */
  722. cmd_addr = adr & ~(map_bankwidth(map)-1);
  723. spin_lock(chip->mutex);
  724. ret = get_chip(map, chip, cmd_addr, FL_READY);
  725. if (ret) {
  726. spin_unlock(chip->mutex);
  727. return ret;
  728. }
  729. if (chip->state != FL_POINT && chip->state != FL_READY) {
  730. map_write(map, CMD(0xf0), cmd_addr);
  731. chip->state = FL_READY;
  732. }
  733. map_copy_from(map, buf, adr, len);
  734. put_chip(map, chip, cmd_addr);
  735. spin_unlock(chip->mutex);
  736. return 0;
  737. }
  738. static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  739. {
  740. struct map_info *map = mtd->priv;
  741. struct cfi_private *cfi = map->fldrv_priv;
  742. unsigned long ofs;
  743. int chipnum;
  744. int ret = 0;
  745. /* ofs: offset within the first chip that the first read should start */
  746. chipnum = (from >> cfi->chipshift);
  747. ofs = from - (chipnum << cfi->chipshift);
  748. *retlen = 0;
  749. while (len) {
  750. unsigned long thislen;
  751. if (chipnum >= cfi->numchips)
  752. break;
  753. if ((len + ofs -1) >> cfi->chipshift)
  754. thislen = (1<<cfi->chipshift) - ofs;
  755. else
  756. thislen = len;
  757. ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  758. if (ret)
  759. break;
  760. *retlen += thislen;
  761. len -= thislen;
  762. buf += thislen;
  763. ofs = 0;
  764. chipnum++;
  765. }
  766. return ret;
  767. }
  768. static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  769. {
  770. DECLARE_WAITQUEUE(wait, current);
  771. unsigned long timeo = jiffies + HZ;
  772. struct cfi_private *cfi = map->fldrv_priv;
  773. retry:
  774. spin_lock(chip->mutex);
  775. if (chip->state != FL_READY){
  776. #if 0
  777. printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state);
  778. #endif
  779. set_current_state(TASK_UNINTERRUPTIBLE);
  780. add_wait_queue(&chip->wq, &wait);
  781. spin_unlock(chip->mutex);
  782. schedule();
  783. remove_wait_queue(&chip->wq, &wait);
  784. #if 0
  785. if(signal_pending(current))
  786. return -EINTR;
  787. #endif
  788. timeo = jiffies + HZ;
  789. goto retry;
  790. }
  791. adr += chip->start;
  792. chip->state = FL_READY;
  793. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  794. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  795. cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  796. map_copy_from(map, buf, adr, len);
  797. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  798. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  799. cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  800. cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  801. wake_up(&chip->wq);
  802. spin_unlock(chip->mutex);
  803. return 0;
  804. }
  805. static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  806. {
  807. struct map_info *map = mtd->priv;
  808. struct cfi_private *cfi = map->fldrv_priv;
  809. unsigned long ofs;
  810. int chipnum;
  811. int ret = 0;
  812. /* ofs: offset within the first chip that the first read should start */
  813. /* 8 secsi bytes per chip */
  814. chipnum=from>>3;
  815. ofs=from & 7;
  816. *retlen = 0;
  817. while (len) {
  818. unsigned long thislen;
  819. if (chipnum >= cfi->numchips)
  820. break;
  821. if ((len + ofs -1) >> 3)
  822. thislen = (1<<3) - ofs;
  823. else
  824. thislen = len;
  825. ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  826. if (ret)
  827. break;
  828. *retlen += thislen;
  829. len -= thislen;
  830. buf += thislen;
  831. ofs = 0;
  832. chipnum++;
  833. }
  834. return ret;
  835. }
  836. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
  837. {
  838. struct cfi_private *cfi = map->fldrv_priv;
  839. unsigned long timeo = jiffies + HZ;
  840. /*
  841. * We use a 1ms + 1 jiffies generic timeout for writes (most devices
  842. * have a max write time of a few hundreds usec). However, we should
  843. * use the maximum timeout value given by the chip at probe time
  844. * instead. Unfortunately, struct flchip does have a field for
  845. * maximum timeout, only for typical which can be far too short
  846. * depending of the conditions. The ' + 1' is to avoid having a
  847. * timeout of 0 jiffies if HZ is smaller than 1000.
  848. */
  849. unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
  850. int ret = 0;
  851. map_word oldd;
  852. int retry_cnt = 0;
  853. adr += chip->start;
  854. spin_lock(chip->mutex);
  855. ret = get_chip(map, chip, adr, FL_WRITING);
  856. if (ret) {
  857. spin_unlock(chip->mutex);
  858. return ret;
  859. }
  860. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  861. __func__, adr, datum.x[0] );
  862. /*
  863. * Check for a NOP for the case when the datum to write is already
  864. * present - it saves time and works around buggy chips that corrupt
  865. * data at other locations when 0xff is written to a location that
  866. * already contains 0xff.
  867. */
  868. oldd = map_read(map, adr);
  869. if (map_word_equal(map, oldd, datum)) {
  870. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
  871. __func__);
  872. goto op_done;
  873. }
  874. XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
  875. ENABLE_VPP(map);
  876. xip_disable(map, chip, adr);
  877. retry:
  878. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  879. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  880. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  881. map_write(map, datum, adr);
  882. chip->state = FL_WRITING;
  883. INVALIDATE_CACHE_UDELAY(map, chip,
  884. adr, map_bankwidth(map),
  885. chip->word_write_time);
  886. /* See comment above for timeout value. */
  887. timeo = jiffies + uWriteTimeout;
  888. for (;;) {
  889. if (chip->state != FL_WRITING) {
  890. /* Someone's suspended the write. Sleep */
  891. DECLARE_WAITQUEUE(wait, current);
  892. set_current_state(TASK_UNINTERRUPTIBLE);
  893. add_wait_queue(&chip->wq, &wait);
  894. spin_unlock(chip->mutex);
  895. schedule();
  896. remove_wait_queue(&chip->wq, &wait);
  897. timeo = jiffies + (HZ / 2); /* FIXME */
  898. spin_lock(chip->mutex);
  899. continue;
  900. }
  901. if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
  902. xip_enable(map, chip, adr);
  903. printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
  904. xip_disable(map, chip, adr);
  905. break;
  906. }
  907. if (chip_ready(map, adr))
  908. break;
  909. /* Latency issues. Drop the lock, wait a while and retry */
  910. UDELAY(map, chip, adr, 1);
  911. }
  912. /* Did we succeed? */
  913. if (!chip_good(map, adr, datum)) {
  914. /* reset on all failures. */
  915. map_write( map, CMD(0xF0), chip->start );
  916. /* FIXME - should have reset delay before continuing */
  917. if (++retry_cnt <= MAX_WORD_RETRIES)
  918. goto retry;
  919. ret = -EIO;
  920. }
  921. xip_enable(map, chip, adr);
  922. op_done:
  923. chip->state = FL_READY;
  924. put_chip(map, chip, adr);
  925. spin_unlock(chip->mutex);
  926. return ret;
  927. }
  928. static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
  929. size_t *retlen, const u_char *buf)
  930. {
  931. struct map_info *map = mtd->priv;
  932. struct cfi_private *cfi = map->fldrv_priv;
  933. int ret = 0;
  934. int chipnum;
  935. unsigned long ofs, chipstart;
  936. DECLARE_WAITQUEUE(wait, current);
  937. *retlen = 0;
  938. if (!len)
  939. return 0;
  940. chipnum = to >> cfi->chipshift;
  941. ofs = to - (chipnum << cfi->chipshift);
  942. chipstart = cfi->chips[chipnum].start;
  943. /* If it's not bus-aligned, do the first byte write */
  944. if (ofs & (map_bankwidth(map)-1)) {
  945. unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
  946. int i = ofs - bus_ofs;
  947. int n = 0;
  948. map_word tmp_buf;
  949. retry:
  950. spin_lock(cfi->chips[chipnum].mutex);
  951. if (cfi->chips[chipnum].state != FL_READY) {
  952. #if 0
  953. printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
  954. #endif
  955. set_current_state(TASK_UNINTERRUPTIBLE);
  956. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  957. spin_unlock(cfi->chips[chipnum].mutex);
  958. schedule();
  959. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  960. #if 0
  961. if(signal_pending(current))
  962. return -EINTR;
  963. #endif
  964. goto retry;
  965. }
  966. /* Load 'tmp_buf' with old contents of flash */
  967. tmp_buf = map_read(map, bus_ofs+chipstart);
  968. spin_unlock(cfi->chips[chipnum].mutex);
  969. /* Number of bytes to copy from buffer */
  970. n = min_t(int, len, map_bankwidth(map)-i);
  971. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  972. ret = do_write_oneword(map, &cfi->chips[chipnum],
  973. bus_ofs, tmp_buf);
  974. if (ret)
  975. return ret;
  976. ofs += n;
  977. buf += n;
  978. (*retlen) += n;
  979. len -= n;
  980. if (ofs >> cfi->chipshift) {
  981. chipnum ++;
  982. ofs = 0;
  983. if (chipnum == cfi->numchips)
  984. return 0;
  985. }
  986. }
  987. /* We are now aligned, write as much as possible */
  988. while(len >= map_bankwidth(map)) {
  989. map_word datum;
  990. datum = map_word_load(map, buf);
  991. ret = do_write_oneword(map, &cfi->chips[chipnum],
  992. ofs, datum);
  993. if (ret)
  994. return ret;
  995. ofs += map_bankwidth(map);
  996. buf += map_bankwidth(map);
  997. (*retlen) += map_bankwidth(map);
  998. len -= map_bankwidth(map);
  999. if (ofs >> cfi->chipshift) {
  1000. chipnum ++;
  1001. ofs = 0;
  1002. if (chipnum == cfi->numchips)
  1003. return 0;
  1004. chipstart = cfi->chips[chipnum].start;
  1005. }
  1006. }
  1007. /* Write the trailing bytes if any */
  1008. if (len & (map_bankwidth(map)-1)) {
  1009. map_word tmp_buf;
  1010. retry1:
  1011. spin_lock(cfi->chips[chipnum].mutex);
  1012. if (cfi->chips[chipnum].state != FL_READY) {
  1013. #if 0
  1014. printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
  1015. #endif
  1016. set_current_state(TASK_UNINTERRUPTIBLE);
  1017. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1018. spin_unlock(cfi->chips[chipnum].mutex);
  1019. schedule();
  1020. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1021. #if 0
  1022. if(signal_pending(current))
  1023. return -EINTR;
  1024. #endif
  1025. goto retry1;
  1026. }
  1027. tmp_buf = map_read(map, ofs + chipstart);
  1028. spin_unlock(cfi->chips[chipnum].mutex);
  1029. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1030. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1031. ofs, tmp_buf);
  1032. if (ret)
  1033. return ret;
  1034. (*retlen) += len;
  1035. }
  1036. return 0;
  1037. }
  1038. /*
  1039. * FIXME: interleaved mode not tested, and probably not supported!
  1040. */
  1041. static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
  1042. unsigned long adr, const u_char *buf,
  1043. int len)
  1044. {
  1045. struct cfi_private *cfi = map->fldrv_priv;
  1046. unsigned long timeo = jiffies + HZ;
  1047. /* see comments in do_write_oneword() regarding uWriteTimeo. */
  1048. unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
  1049. int ret = -EIO;
  1050. unsigned long cmd_adr;
  1051. int z, words;
  1052. map_word datum;
  1053. adr += chip->start;
  1054. cmd_adr = adr;
  1055. spin_lock(chip->mutex);
  1056. ret = get_chip(map, chip, adr, FL_WRITING);
  1057. if (ret) {
  1058. spin_unlock(chip->mutex);
  1059. return ret;
  1060. }
  1061. datum = map_word_load(map, buf);
  1062. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1063. __func__, adr, datum.x[0] );
  1064. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1065. ENABLE_VPP(map);
  1066. xip_disable(map, chip, cmd_adr);
  1067. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1068. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1069. //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1070. /* Write Buffer Load */
  1071. map_write(map, CMD(0x25), cmd_adr);
  1072. chip->state = FL_WRITING_TO_BUFFER;
  1073. /* Write length of data to come */
  1074. words = len / map_bankwidth(map);
  1075. map_write(map, CMD(words - 1), cmd_adr);
  1076. /* Write data */
  1077. z = 0;
  1078. while(z < words * map_bankwidth(map)) {
  1079. datum = map_word_load(map, buf);
  1080. map_write(map, datum, adr + z);
  1081. z += map_bankwidth(map);
  1082. buf += map_bankwidth(map);
  1083. }
  1084. z -= map_bankwidth(map);
  1085. adr += z;
  1086. /* Write Buffer Program Confirm: GO GO GO */
  1087. map_write(map, CMD(0x29), cmd_adr);
  1088. chip->state = FL_WRITING;
  1089. INVALIDATE_CACHE_UDELAY(map, chip,
  1090. adr, map_bankwidth(map),
  1091. chip->word_write_time);
  1092. timeo = jiffies + uWriteTimeout;
  1093. for (;;) {
  1094. if (chip->state != FL_WRITING) {
  1095. /* Someone's suspended the write. Sleep */
  1096. DECLARE_WAITQUEUE(wait, current);
  1097. set_current_state(TASK_UNINTERRUPTIBLE);
  1098. add_wait_queue(&chip->wq, &wait);
  1099. spin_unlock(chip->mutex);
  1100. schedule();
  1101. remove_wait_queue(&chip->wq, &wait);
  1102. timeo = jiffies + (HZ / 2); /* FIXME */
  1103. spin_lock(chip->mutex);
  1104. continue;
  1105. }
  1106. if (time_after(jiffies, timeo) && !chip_ready(map, adr))
  1107. break;
  1108. if (chip_ready(map, adr)) {
  1109. xip_enable(map, chip, adr);
  1110. goto op_done;
  1111. }
  1112. /* Latency issues. Drop the lock, wait a while and retry */
  1113. UDELAY(map, chip, adr, 1);
  1114. }
  1115. /* reset on all failures. */
  1116. map_write( map, CMD(0xF0), chip->start );
  1117. xip_enable(map, chip, adr);
  1118. /* FIXME - should have reset delay before continuing */
  1119. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1120. __func__ );
  1121. ret = -EIO;
  1122. op_done:
  1123. chip->state = FL_READY;
  1124. put_chip(map, chip, adr);
  1125. spin_unlock(chip->mutex);
  1126. return ret;
  1127. }
  1128. static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
  1129. size_t *retlen, const u_char *buf)
  1130. {
  1131. struct map_info *map = mtd->priv;
  1132. struct cfi_private *cfi = map->fldrv_priv;
  1133. int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1134. int ret = 0;
  1135. int chipnum;
  1136. unsigned long ofs;
  1137. *retlen = 0;
  1138. if (!len)
  1139. return 0;
  1140. chipnum = to >> cfi->chipshift;
  1141. ofs = to - (chipnum << cfi->chipshift);
  1142. /* If it's not bus-aligned, do the first word write */
  1143. if (ofs & (map_bankwidth(map)-1)) {
  1144. size_t local_len = (-ofs)&(map_bankwidth(map)-1);
  1145. if (local_len > len)
  1146. local_len = len;
  1147. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1148. local_len, retlen, buf);
  1149. if (ret)
  1150. return ret;
  1151. ofs += local_len;
  1152. buf += local_len;
  1153. len -= local_len;
  1154. if (ofs >> cfi->chipshift) {
  1155. chipnum ++;
  1156. ofs = 0;
  1157. if (chipnum == cfi->numchips)
  1158. return 0;
  1159. }
  1160. }
  1161. /* Write buffer is worth it only if more than one word to write... */
  1162. while (len >= map_bankwidth(map) * 2) {
  1163. /* We must not cross write block boundaries */
  1164. int size = wbufsize - (ofs & (wbufsize-1));
  1165. if (size > len)
  1166. size = len;
  1167. if (size % map_bankwidth(map))
  1168. size -= size % map_bankwidth(map);
  1169. ret = do_write_buffer(map, &cfi->chips[chipnum],
  1170. ofs, buf, size);
  1171. if (ret)
  1172. return ret;
  1173. ofs += size;
  1174. buf += size;
  1175. (*retlen) += size;
  1176. len -= size;
  1177. if (ofs >> cfi->chipshift) {
  1178. chipnum ++;
  1179. ofs = 0;
  1180. if (chipnum == cfi->numchips)
  1181. return 0;
  1182. }
  1183. }
  1184. if (len) {
  1185. size_t retlen_dregs = 0;
  1186. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1187. len, &retlen_dregs, buf);
  1188. *retlen += retlen_dregs;
  1189. return ret;
  1190. }
  1191. return 0;
  1192. }
  1193. /*
  1194. * Handle devices with one erase region, that only implement
  1195. * the chip erase command.
  1196. */
  1197. static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
  1198. {
  1199. struct cfi_private *cfi = map->fldrv_priv;
  1200. unsigned long timeo = jiffies + HZ;
  1201. unsigned long int adr;
  1202. DECLARE_WAITQUEUE(wait, current);
  1203. int ret = 0;
  1204. adr = cfi->addr_unlock1;
  1205. spin_lock(chip->mutex);
  1206. ret = get_chip(map, chip, adr, FL_WRITING);
  1207. if (ret) {
  1208. spin_unlock(chip->mutex);
  1209. return ret;
  1210. }
  1211. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
  1212. __func__, chip->start );
  1213. XIP_INVAL_CACHED_RANGE(map, adr, map->size);
  1214. ENABLE_VPP(map);
  1215. xip_disable(map, chip, adr);
  1216. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1217. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1218. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1219. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1220. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1221. cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1222. chip->state = FL_ERASING;
  1223. chip->erase_suspended = 0;
  1224. chip->in_progress_block_addr = adr;
  1225. INVALIDATE_CACHE_UDELAY(map, chip,
  1226. adr, map->size,
  1227. chip->erase_time*500);
  1228. timeo = jiffies + (HZ*20);
  1229. for (;;) {
  1230. if (chip->state != FL_ERASING) {
  1231. /* Someone's suspended the erase. Sleep */
  1232. set_current_state(TASK_UNINTERRUPTIBLE);
  1233. add_wait_queue(&chip->wq, &wait);
  1234. spin_unlock(chip->mutex);
  1235. schedule();
  1236. remove_wait_queue(&chip->wq, &wait);
  1237. spin_lock(chip->mutex);
  1238. continue;
  1239. }
  1240. if (chip->erase_suspended) {
  1241. /* This erase was suspended and resumed.
  1242. Adjust the timeout */
  1243. timeo = jiffies + (HZ*20); /* FIXME */
  1244. chip->erase_suspended = 0;
  1245. }
  1246. if (chip_ready(map, adr))
  1247. break;
  1248. if (time_after(jiffies, timeo)) {
  1249. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1250. __func__ );
  1251. break;
  1252. }
  1253. /* Latency issues. Drop the lock, wait a while and retry */
  1254. UDELAY(map, chip, adr, 1000000/HZ);
  1255. }
  1256. /* Did we succeed? */
  1257. if (!chip_good(map, adr, map_word_ff(map))) {
  1258. /* reset on all failures. */
  1259. map_write( map, CMD(0xF0), chip->start );
  1260. /* FIXME - should have reset delay before continuing */
  1261. ret = -EIO;
  1262. }
  1263. chip->state = FL_READY;
  1264. xip_enable(map, chip, adr);
  1265. put_chip(map, chip, adr);
  1266. spin_unlock(chip->mutex);
  1267. return ret;
  1268. }
  1269. static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
  1270. {
  1271. struct cfi_private *cfi = map->fldrv_priv;
  1272. unsigned long timeo = jiffies + HZ;
  1273. DECLARE_WAITQUEUE(wait, current);
  1274. int ret = 0;
  1275. adr += chip->start;
  1276. spin_lock(chip->mutex);
  1277. ret = get_chip(map, chip, adr, FL_ERASING);
  1278. if (ret) {
  1279. spin_unlock(chip->mutex);
  1280. return ret;
  1281. }
  1282. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
  1283. __func__, adr );
  1284. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1285. ENABLE_VPP(map);
  1286. xip_disable(map, chip, adr);
  1287. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1288. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1289. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1290. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1291. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1292. map_write(map, CMD(0x30), adr);
  1293. chip->state = FL_ERASING;
  1294. chip->erase_suspended = 0;
  1295. chip->in_progress_block_addr = adr;
  1296. INVALIDATE_CACHE_UDELAY(map, chip,
  1297. adr, len,
  1298. chip->erase_time*500);
  1299. timeo = jiffies + (HZ*20);
  1300. for (;;) {
  1301. if (chip->state != FL_ERASING) {
  1302. /* Someone's suspended the erase. Sleep */
  1303. set_current_state(TASK_UNINTERRUPTIBLE);
  1304. add_wait_queue(&chip->wq, &wait);
  1305. spin_unlock(chip->mutex);
  1306. schedule();
  1307. remove_wait_queue(&chip->wq, &wait);
  1308. spin_lock(chip->mutex);
  1309. continue;
  1310. }
  1311. if (chip->erase_suspended) {
  1312. /* This erase was suspended and resumed.
  1313. Adjust the timeout */
  1314. timeo = jiffies + (HZ*20); /* FIXME */
  1315. chip->erase_suspended = 0;
  1316. }
  1317. if (chip_ready(map, adr)) {
  1318. xip_enable(map, chip, adr);
  1319. break;
  1320. }
  1321. if (time_after(jiffies, timeo)) {
  1322. xip_enable(map, chip, adr);
  1323. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1324. __func__ );
  1325. break;
  1326. }
  1327. /* Latency issues. Drop the lock, wait a while and retry */
  1328. UDELAY(map, chip, adr, 1000000/HZ);
  1329. }
  1330. /* Did we succeed? */
  1331. if (!chip_good(map, adr, map_word_ff(map))) {
  1332. /* reset on all failures. */
  1333. map_write( map, CMD(0xF0), chip->start );
  1334. /* FIXME - should have reset delay before continuing */
  1335. ret = -EIO;
  1336. }
  1337. chip->state = FL_READY;
  1338. put_chip(map, chip, adr);
  1339. spin_unlock(chip->mutex);
  1340. return ret;
  1341. }
  1342. static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
  1343. {
  1344. unsigned long ofs, len;
  1345. int ret;
  1346. ofs = instr->addr;
  1347. len = instr->len;
  1348. ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
  1349. if (ret)
  1350. return ret;
  1351. instr->state = MTD_ERASE_DONE;
  1352. mtd_erase_callback(instr);
  1353. return 0;
  1354. }
  1355. static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
  1356. {
  1357. struct map_info *map = mtd->priv;
  1358. struct cfi_private *cfi = map->fldrv_priv;
  1359. int ret = 0;
  1360. if (instr->addr != 0)
  1361. return -EINVAL;
  1362. if (instr->len != mtd->size)
  1363. return -EINVAL;
  1364. ret = do_erase_chip(map, &cfi->chips[0]);
  1365. if (ret)
  1366. return ret;
  1367. instr->state = MTD_ERASE_DONE;
  1368. mtd_erase_callback(instr);
  1369. return 0;
  1370. }
  1371. static int do_atmel_lock(struct map_info *map, struct flchip *chip,
  1372. unsigned long adr, int len, void *thunk)
  1373. {
  1374. struct cfi_private *cfi = map->fldrv_priv;
  1375. int ret;
  1376. spin_lock(chip->mutex);
  1377. ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
  1378. if (ret)
  1379. goto out_unlock;
  1380. chip->state = FL_LOCKING;
  1381. DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
  1382. __func__, adr, len);
  1383. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1384. cfi->device_type, NULL);
  1385. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1386. cfi->device_type, NULL);
  1387. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
  1388. cfi->device_type, NULL);
  1389. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1390. cfi->device_type, NULL);
  1391. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1392. cfi->device_type, NULL);
  1393. map_write(map, CMD(0x40), chip->start + adr);
  1394. chip->state = FL_READY;
  1395. put_chip(map, chip, adr + chip->start);
  1396. ret = 0;
  1397. out_unlock:
  1398. spin_unlock(chip->mutex);
  1399. return ret;
  1400. }
  1401. static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
  1402. unsigned long adr, int len, void *thunk)
  1403. {
  1404. struct cfi_private *cfi = map->fldrv_priv;
  1405. int ret;
  1406. spin_lock(chip->mutex);
  1407. ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
  1408. if (ret)
  1409. goto out_unlock;
  1410. chip->state = FL_UNLOCKING;
  1411. DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
  1412. __func__, adr, len);
  1413. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1414. cfi->device_type, NULL);
  1415. map_write(map, CMD(0x70), adr);
  1416. chip->state = FL_READY;
  1417. put_chip(map, chip, adr + chip->start);
  1418. ret = 0;
  1419. out_unlock:
  1420. spin_unlock(chip->mutex);
  1421. return ret;
  1422. }
  1423. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1424. {
  1425. return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
  1426. }
  1427. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1428. {
  1429. return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
  1430. }
  1431. static void cfi_amdstd_sync (struct mtd_info *mtd)
  1432. {
  1433. struct map_info *map = mtd->priv;
  1434. struct cfi_private *cfi = map->fldrv_priv;
  1435. int i;
  1436. struct flchip *chip;
  1437. int ret = 0;
  1438. DECLARE_WAITQUEUE(wait, current);
  1439. for (i=0; !ret && i<cfi->numchips; i++) {
  1440. chip = &cfi->chips[i];
  1441. retry:
  1442. spin_lock(chip->mutex);
  1443. switch(chip->state) {
  1444. case FL_READY:
  1445. case FL_STATUS:
  1446. case FL_CFI_QUERY:
  1447. case FL_JEDEC_QUERY:
  1448. chip->oldstate = chip->state;
  1449. chip->state = FL_SYNCING;
  1450. /* No need to wake_up() on this state change -
  1451. * as the whole point is that nobody can do anything
  1452. * with the chip now anyway.
  1453. */
  1454. case FL_SYNCING:
  1455. spin_unlock(chip->mutex);
  1456. break;
  1457. default:
  1458. /* Not an idle state */
  1459. set_current_state(TASK_UNINTERRUPTIBLE);
  1460. add_wait_queue(&chip->wq, &wait);
  1461. spin_unlock(chip->mutex);
  1462. schedule();
  1463. remove_wait_queue(&chip->wq, &wait);
  1464. goto retry;
  1465. }
  1466. }
  1467. /* Unlock the chips again */
  1468. for (i--; i >=0; i--) {
  1469. chip = &cfi->chips[i];
  1470. spin_lock(chip->mutex);
  1471. if (chip->state == FL_SYNCING) {
  1472. chip->state = chip->oldstate;
  1473. wake_up(&chip->wq);
  1474. }
  1475. spin_unlock(chip->mutex);
  1476. }
  1477. }
  1478. static int cfi_amdstd_suspend(struct mtd_info *mtd)
  1479. {
  1480. struct map_info *map = mtd->priv;
  1481. struct cfi_private *cfi = map->fldrv_priv;
  1482. int i;
  1483. struct flchip *chip;
  1484. int ret = 0;
  1485. for (i=0; !ret && i<cfi->numchips; i++) {
  1486. chip = &cfi->chips[i];
  1487. spin_lock(chip->mutex);
  1488. switch(chip->state) {
  1489. case FL_READY:
  1490. case FL_STATUS:
  1491. case FL_CFI_QUERY:
  1492. case FL_JEDEC_QUERY:
  1493. chip->oldstate = chip->state;
  1494. chip->state = FL_PM_SUSPENDED;
  1495. /* No need to wake_up() on this state change -
  1496. * as the whole point is that nobody can do anything
  1497. * with the chip now anyway.
  1498. */
  1499. case FL_PM_SUSPENDED:
  1500. break;
  1501. default:
  1502. ret = -EAGAIN;
  1503. break;
  1504. }
  1505. spin_unlock(chip->mutex);
  1506. }
  1507. /* Unlock the chips again */
  1508. if (ret) {
  1509. for (i--; i >=0; i--) {
  1510. chip = &cfi->chips[i];
  1511. spin_lock(chip->mutex);
  1512. if (chip->state == FL_PM_SUSPENDED) {
  1513. chip->state = chip->oldstate;
  1514. wake_up(&chip->wq);
  1515. }
  1516. spin_unlock(chip->mutex);
  1517. }
  1518. }
  1519. return ret;
  1520. }
  1521. static void cfi_amdstd_resume(struct mtd_info *mtd)
  1522. {
  1523. struct map_info *map = mtd->priv;
  1524. struct cfi_private *cfi = map->fldrv_priv;
  1525. int i;
  1526. struct flchip *chip;
  1527. for (i=0; i<cfi->numchips; i++) {
  1528. chip = &cfi->chips[i];
  1529. spin_lock(chip->mutex);
  1530. if (chip->state == FL_PM_SUSPENDED) {
  1531. chip->state = FL_READY;
  1532. map_write(map, CMD(0xF0), chip->start);
  1533. wake_up(&chip->wq);
  1534. }
  1535. else
  1536. printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
  1537. spin_unlock(chip->mutex);
  1538. }
  1539. }
  1540. static void cfi_amdstd_destroy(struct mtd_info *mtd)
  1541. {
  1542. struct map_info *map = mtd->priv;
  1543. struct cfi_private *cfi = map->fldrv_priv;
  1544. kfree(cfi->cmdset_priv);
  1545. kfree(cfi->cfiq);
  1546. kfree(cfi);
  1547. kfree(mtd->eraseregions);
  1548. }
  1549. MODULE_LICENSE("GPL");
  1550. MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
  1551. MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");