cfi_cmdset_0002.c 49 KB

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