cfi_cmdset_0002.c 52 KB

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