cfi_cmdset_0002.c 49 KB

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