cfi_cmdset_0002.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946
  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 <asm/io.h>
  27. #include <asm/byteorder.h>
  28. #include <linux/errno.h>
  29. #include <linux/slab.h>
  30. #include <linux/delay.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/reboot.h>
  33. #include <linux/of.h>
  34. #include <linux/of_platform.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_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_get_fact_prot_info(struct mtd_info *, size_t,
  56. size_t *, struct otp_info *);
  57. static int cfi_amdstd_get_user_prot_info(struct mtd_info *, size_t,
  58. size_t *, struct otp_info *);
  59. static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  60. static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *, loff_t, size_t,
  61. size_t *, u_char *);
  62. static int cfi_amdstd_read_user_prot_reg(struct mtd_info *, loff_t, size_t,
  63. size_t *, u_char *);
  64. static int cfi_amdstd_write_user_prot_reg(struct mtd_info *, loff_t, size_t,
  65. size_t *, u_char *);
  66. static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *, loff_t, size_t);
  67. static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  68. size_t *retlen, const u_char *buf);
  69. static void cfi_amdstd_destroy(struct mtd_info *);
  70. struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
  71. static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
  72. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
  73. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
  74. #include "fwh_lock.h"
  75. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  76. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  77. static int cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  78. static int cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  79. static int cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  80. static struct mtd_chip_driver cfi_amdstd_chipdrv = {
  81. .probe = NULL, /* Not usable directly */
  82. .destroy = cfi_amdstd_destroy,
  83. .name = "cfi_cmdset_0002",
  84. .module = THIS_MODULE
  85. };
  86. /* #define DEBUG_CFI_FEATURES */
  87. #ifdef DEBUG_CFI_FEATURES
  88. static void cfi_tell_features(struct cfi_pri_amdstd *extp)
  89. {
  90. const char* erase_suspend[3] = {
  91. "Not supported", "Read only", "Read/write"
  92. };
  93. const char* top_bottom[6] = {
  94. "No WP", "8x8KiB sectors at top & bottom, no WP",
  95. "Bottom boot", "Top boot",
  96. "Uniform, Bottom WP", "Uniform, Top WP"
  97. };
  98. printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
  99. printk(" Address sensitive unlock: %s\n",
  100. (extp->SiliconRevision & 1) ? "Not required" : "Required");
  101. if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
  102. printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
  103. else
  104. printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
  105. if (extp->BlkProt == 0)
  106. printk(" Block protection: Not supported\n");
  107. else
  108. printk(" Block protection: %d sectors per group\n", extp->BlkProt);
  109. printk(" Temporary block unprotect: %s\n",
  110. extp->TmpBlkUnprotect ? "Supported" : "Not supported");
  111. printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
  112. printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
  113. printk(" Burst mode: %s\n",
  114. extp->BurstMode ? "Supported" : "Not supported");
  115. if (extp->PageMode == 0)
  116. printk(" Page mode: Not supported\n");
  117. else
  118. printk(" Page mode: %d word page\n", extp->PageMode << 2);
  119. printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
  120. extp->VppMin >> 4, extp->VppMin & 0xf);
  121. printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
  122. extp->VppMax >> 4, extp->VppMax & 0xf);
  123. if (extp->TopBottom < ARRAY_SIZE(top_bottom))
  124. printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
  125. else
  126. printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
  127. }
  128. #endif
  129. #ifdef AMD_BOOTLOC_BUG
  130. /* Wheee. Bring me the head of someone at AMD. */
  131. static void fixup_amd_bootblock(struct mtd_info *mtd)
  132. {
  133. struct map_info *map = mtd->priv;
  134. struct cfi_private *cfi = map->fldrv_priv;
  135. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  136. __u8 major = extp->MajorVersion;
  137. __u8 minor = extp->MinorVersion;
  138. if (((major << 8) | minor) < 0x3131) {
  139. /* CFI version 1.0 => don't trust bootloc */
  140. pr_debug("%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
  141. map->name, cfi->mfr, cfi->id);
  142. /* AFAICS all 29LV400 with a bottom boot block have a device ID
  143. * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
  144. * These were badly detected as they have the 0x80 bit set
  145. * so treat them as a special case.
  146. */
  147. if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
  148. /* Macronix added CFI to their 2nd generation
  149. * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
  150. * Fujitsu, Spansion, EON, ESI and older Macronix)
  151. * has CFI.
  152. *
  153. * Therefore also check the manufacturer.
  154. * This reduces the risk of false detection due to
  155. * the 8-bit device ID.
  156. */
  157. (cfi->mfr == CFI_MFR_MACRONIX)) {
  158. pr_debug("%s: Macronix MX29LV400C with bottom boot block"
  159. " detected\n", map->name);
  160. extp->TopBottom = 2; /* bottom boot */
  161. } else
  162. if (cfi->id & 0x80) {
  163. printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
  164. extp->TopBottom = 3; /* top boot */
  165. } else {
  166. extp->TopBottom = 2; /* bottom boot */
  167. }
  168. pr_debug("%s: AMD CFI PRI V%c.%c has no boot block field;"
  169. " deduced %s from Device ID\n", map->name, major, minor,
  170. extp->TopBottom == 2 ? "bottom" : "top");
  171. }
  172. }
  173. #endif
  174. static void fixup_use_write_buffers(struct mtd_info *mtd)
  175. {
  176. struct map_info *map = mtd->priv;
  177. struct cfi_private *cfi = map->fldrv_priv;
  178. if (cfi->cfiq->BufWriteTimeoutTyp) {
  179. pr_debug("Using buffer write method\n" );
  180. mtd->_write = cfi_amdstd_write_buffers;
  181. }
  182. }
  183. /* Atmel chips don't use the same PRI format as AMD chips */
  184. static void fixup_convert_atmel_pri(struct mtd_info *mtd)
  185. {
  186. struct map_info *map = mtd->priv;
  187. struct cfi_private *cfi = map->fldrv_priv;
  188. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  189. struct cfi_pri_atmel atmel_pri;
  190. memcpy(&atmel_pri, extp, sizeof(atmel_pri));
  191. memset((char *)extp + 5, 0, sizeof(*extp) - 5);
  192. if (atmel_pri.Features & 0x02)
  193. extp->EraseSuspend = 2;
  194. /* Some chips got it backwards... */
  195. if (cfi->id == AT49BV6416) {
  196. if (atmel_pri.BottomBoot)
  197. extp->TopBottom = 3;
  198. else
  199. extp->TopBottom = 2;
  200. } else {
  201. if (atmel_pri.BottomBoot)
  202. extp->TopBottom = 2;
  203. else
  204. extp->TopBottom = 3;
  205. }
  206. /* burst write mode not supported */
  207. cfi->cfiq->BufWriteTimeoutTyp = 0;
  208. cfi->cfiq->BufWriteTimeoutMax = 0;
  209. }
  210. static void fixup_use_secsi(struct mtd_info *mtd)
  211. {
  212. /* Setup for chips with a secsi area */
  213. mtd->_read_user_prot_reg = cfi_amdstd_secsi_read;
  214. mtd->_read_fact_prot_reg = cfi_amdstd_secsi_read;
  215. }
  216. static void fixup_use_erase_chip(struct mtd_info *mtd)
  217. {
  218. struct map_info *map = mtd->priv;
  219. struct cfi_private *cfi = map->fldrv_priv;
  220. if ((cfi->cfiq->NumEraseRegions == 1) &&
  221. ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
  222. mtd->_erase = cfi_amdstd_erase_chip;
  223. }
  224. }
  225. /*
  226. * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
  227. * locked by default.
  228. */
  229. static void fixup_use_atmel_lock(struct mtd_info *mtd)
  230. {
  231. mtd->_lock = cfi_atmel_lock;
  232. mtd->_unlock = cfi_atmel_unlock;
  233. mtd->flags |= MTD_POWERUP_LOCK;
  234. }
  235. static void fixup_old_sst_eraseregion(struct mtd_info *mtd)
  236. {
  237. struct map_info *map = mtd->priv;
  238. struct cfi_private *cfi = map->fldrv_priv;
  239. /*
  240. * These flashes report two separate eraseblock regions based on the
  241. * sector_erase-size and block_erase-size, although they both operate on the
  242. * same memory. This is not allowed according to CFI, so we just pick the
  243. * sector_erase-size.
  244. */
  245. cfi->cfiq->NumEraseRegions = 1;
  246. }
  247. static void fixup_sst39vf(struct mtd_info *mtd)
  248. {
  249. struct map_info *map = mtd->priv;
  250. struct cfi_private *cfi = map->fldrv_priv;
  251. fixup_old_sst_eraseregion(mtd);
  252. cfi->addr_unlock1 = 0x5555;
  253. cfi->addr_unlock2 = 0x2AAA;
  254. }
  255. static void fixup_sst39vf_rev_b(struct mtd_info *mtd)
  256. {
  257. struct map_info *map = mtd->priv;
  258. struct cfi_private *cfi = map->fldrv_priv;
  259. fixup_old_sst_eraseregion(mtd);
  260. cfi->addr_unlock1 = 0x555;
  261. cfi->addr_unlock2 = 0x2AA;
  262. cfi->sector_erase_cmd = CMD(0x50);
  263. }
  264. static void fixup_sst38vf640x_sectorsize(struct mtd_info *mtd)
  265. {
  266. struct map_info *map = mtd->priv;
  267. struct cfi_private *cfi = map->fldrv_priv;
  268. fixup_sst39vf_rev_b(mtd);
  269. /*
  270. * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where
  271. * it should report a size of 8KBytes (0x0020*256).
  272. */
  273. cfi->cfiq->EraseRegionInfo[0] = 0x002003ff;
  274. pr_warning("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n", mtd->name);
  275. }
  276. static void fixup_s29gl064n_sectors(struct mtd_info *mtd)
  277. {
  278. struct map_info *map = mtd->priv;
  279. struct cfi_private *cfi = map->fldrv_priv;
  280. if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
  281. cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
  282. pr_warning("%s: Bad S29GL064N CFI data; adjust from 64 to 128 sectors\n", mtd->name);
  283. }
  284. }
  285. static void fixup_s29gl032n_sectors(struct mtd_info *mtd)
  286. {
  287. struct map_info *map = mtd->priv;
  288. struct cfi_private *cfi = map->fldrv_priv;
  289. if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
  290. cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
  291. pr_warning("%s: Bad S29GL032N CFI data; adjust from 127 to 63 sectors\n", mtd->name);
  292. }
  293. }
  294. static void fixup_s29ns512p_sectors(struct mtd_info *mtd)
  295. {
  296. struct map_info *map = mtd->priv;
  297. struct cfi_private *cfi = map->fldrv_priv;
  298. /*
  299. * S29NS512P flash uses more than 8bits to report number of sectors,
  300. * which is not permitted by CFI.
  301. */
  302. cfi->cfiq->EraseRegionInfo[0] = 0x020001ff;
  303. pr_warning("%s: Bad S29NS512P CFI data; adjust to 512 sectors\n", mtd->name);
  304. }
  305. /* Used to fix CFI-Tables of chips without Extended Query Tables */
  306. static struct cfi_fixup cfi_nopri_fixup_table[] = {
  307. { CFI_MFR_SST, 0x234a, fixup_sst39vf }, /* SST39VF1602 */
  308. { CFI_MFR_SST, 0x234b, fixup_sst39vf }, /* SST39VF1601 */
  309. { CFI_MFR_SST, 0x235a, fixup_sst39vf }, /* SST39VF3202 */
  310. { CFI_MFR_SST, 0x235b, fixup_sst39vf }, /* SST39VF3201 */
  311. { CFI_MFR_SST, 0x235c, fixup_sst39vf_rev_b }, /* SST39VF3202B */
  312. { CFI_MFR_SST, 0x235d, fixup_sst39vf_rev_b }, /* SST39VF3201B */
  313. { CFI_MFR_SST, 0x236c, fixup_sst39vf_rev_b }, /* SST39VF6402B */
  314. { CFI_MFR_SST, 0x236d, fixup_sst39vf_rev_b }, /* SST39VF6401B */
  315. { 0, 0, NULL }
  316. };
  317. static struct cfi_fixup cfi_fixup_table[] = {
  318. { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
  319. #ifdef AMD_BOOTLOC_BUG
  320. { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock },
  321. { CFI_MFR_AMIC, CFI_ID_ANY, fixup_amd_bootblock },
  322. { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock },
  323. #endif
  324. { CFI_MFR_AMD, 0x0050, fixup_use_secsi },
  325. { CFI_MFR_AMD, 0x0053, fixup_use_secsi },
  326. { CFI_MFR_AMD, 0x0055, fixup_use_secsi },
  327. { CFI_MFR_AMD, 0x0056, fixup_use_secsi },
  328. { CFI_MFR_AMD, 0x005C, fixup_use_secsi },
  329. { CFI_MFR_AMD, 0x005F, fixup_use_secsi },
  330. { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors },
  331. { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors },
  332. { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors },
  333. { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors },
  334. { CFI_MFR_AMD, 0x3f00, fixup_s29ns512p_sectors },
  335. { CFI_MFR_SST, 0x536a, fixup_sst38vf640x_sectorsize }, /* SST38VF6402 */
  336. { CFI_MFR_SST, 0x536b, fixup_sst38vf640x_sectorsize }, /* SST38VF6401 */
  337. { CFI_MFR_SST, 0x536c, fixup_sst38vf640x_sectorsize }, /* SST38VF6404 */
  338. { CFI_MFR_SST, 0x536d, fixup_sst38vf640x_sectorsize }, /* SST38VF6403 */
  339. #if !FORCE_WORD_WRITE
  340. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
  341. #endif
  342. { 0, 0, NULL }
  343. };
  344. static struct cfi_fixup jedec_fixup_table[] = {
  345. { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock },
  346. { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock },
  347. { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock },
  348. { 0, 0, NULL }
  349. };
  350. static struct cfi_fixup fixup_table[] = {
  351. /* The CFI vendor ids and the JEDEC vendor IDs appear
  352. * to be common. It is like the devices id's are as
  353. * well. This table is to pick all cases where
  354. * we know that is the case.
  355. */
  356. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip },
  357. { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock },
  358. { 0, 0, NULL }
  359. };
  360. static void cfi_fixup_major_minor(struct cfi_private *cfi,
  361. struct cfi_pri_amdstd *extp)
  362. {
  363. if (cfi->mfr == CFI_MFR_SAMSUNG) {
  364. if ((extp->MajorVersion == '0' && extp->MinorVersion == '0') ||
  365. (extp->MajorVersion == '3' && extp->MinorVersion == '3')) {
  366. /*
  367. * Samsung K8P2815UQB and K8D6x16UxM chips
  368. * report major=0 / minor=0.
  369. * K8D3x16UxC chips report major=3 / minor=3.
  370. */
  371. printk(KERN_NOTICE " Fixing Samsung's Amd/Fujitsu"
  372. " Extended Query version to 1.%c\n",
  373. extp->MinorVersion);
  374. extp->MajorVersion = '1';
  375. }
  376. }
  377. /*
  378. * SST 38VF640x chips report major=0xFF / minor=0xFF.
  379. */
  380. if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) {
  381. extp->MajorVersion = '1';
  382. extp->MinorVersion = '0';
  383. }
  384. }
  385. static int is_m29ew(struct cfi_private *cfi)
  386. {
  387. if (cfi->mfr == CFI_MFR_INTEL &&
  388. ((cfi->device_type == CFI_DEVICETYPE_X8 && (cfi->id & 0xff) == 0x7e) ||
  389. (cfi->device_type == CFI_DEVICETYPE_X16 && cfi->id == 0x227e)))
  390. return 1;
  391. return 0;
  392. }
  393. /*
  394. * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 20:
  395. * Some revisions of the M29EW suffer from erase suspend hang ups. In
  396. * particular, it can occur when the sequence
  397. * Erase Confirm -> Suspend -> Program -> Resume
  398. * causes a lockup due to internal timing issues. The consequence is that the
  399. * erase cannot be resumed without inserting a dummy command after programming
  400. * and prior to resuming. [...] The work-around is to issue a dummy write cycle
  401. * that writes an F0 command code before the RESUME command.
  402. */
  403. static void cfi_fixup_m29ew_erase_suspend(struct map_info *map,
  404. unsigned long adr)
  405. {
  406. struct cfi_private *cfi = map->fldrv_priv;
  407. /* before resume, insert a dummy 0xF0 cycle for Micron M29EW devices */
  408. if (is_m29ew(cfi))
  409. map_write(map, CMD(0xF0), adr);
  410. }
  411. /*
  412. * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 22:
  413. *
  414. * Some revisions of the M29EW (for example, A1 and A2 step revisions)
  415. * are affected by a problem that could cause a hang up when an ERASE SUSPEND
  416. * command is issued after an ERASE RESUME operation without waiting for a
  417. * minimum delay. The result is that once the ERASE seems to be completed
  418. * (no bits are toggling), the contents of the Flash memory block on which
  419. * the erase was ongoing could be inconsistent with the expected values
  420. * (typically, the array value is stuck to the 0xC0, 0xC4, 0x80, or 0x84
  421. * values), causing a consequent failure of the ERASE operation.
  422. * The occurrence of this issue could be high, especially when file system
  423. * operations on the Flash are intensive. As a result, it is recommended
  424. * that a patch be applied. Intensive file system operations can cause many
  425. * calls to the garbage routine to free Flash space (also by erasing physical
  426. * Flash blocks) and as a result, many consecutive SUSPEND and RESUME
  427. * commands can occur. The problem disappears when a delay is inserted after
  428. * the RESUME command by using the udelay() function available in Linux.
  429. * The DELAY value must be tuned based on the customer's platform.
  430. * The maximum value that fixes the problem in all cases is 500us.
  431. * But, in our experience, a delay of 30 µs to 50 µs is sufficient
  432. * in most cases.
  433. * We have chosen 500µs because this latency is acceptable.
  434. */
  435. static void cfi_fixup_m29ew_delay_after_resume(struct cfi_private *cfi)
  436. {
  437. /*
  438. * Resolving the Delay After Resume Issue see Micron TN-13-07
  439. * Worst case delay must be 500µs but 30-50µs should be ok as well
  440. */
  441. if (is_m29ew(cfi))
  442. cfi_udelay(500);
  443. }
  444. struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
  445. {
  446. struct cfi_private *cfi = map->fldrv_priv;
  447. struct device_node __maybe_unused *np = map->device_node;
  448. struct mtd_info *mtd;
  449. int i;
  450. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  451. if (!mtd)
  452. return NULL;
  453. mtd->priv = map;
  454. mtd->type = MTD_NORFLASH;
  455. /* Fill in the default mtd operations */
  456. mtd->_erase = cfi_amdstd_erase_varsize;
  457. mtd->_write = cfi_amdstd_write_words;
  458. mtd->_read = cfi_amdstd_read;
  459. mtd->_sync = cfi_amdstd_sync;
  460. mtd->_suspend = cfi_amdstd_suspend;
  461. mtd->_resume = cfi_amdstd_resume;
  462. mtd->_read_user_prot_reg = cfi_amdstd_read_user_prot_reg;
  463. mtd->_read_fact_prot_reg = cfi_amdstd_read_fact_prot_reg;
  464. mtd->_get_fact_prot_info = cfi_amdstd_get_fact_prot_info;
  465. mtd->_get_user_prot_info = cfi_amdstd_get_user_prot_info;
  466. mtd->_write_user_prot_reg = cfi_amdstd_write_user_prot_reg;
  467. mtd->_lock_user_prot_reg = cfi_amdstd_lock_user_prot_reg;
  468. mtd->flags = MTD_CAP_NORFLASH;
  469. mtd->name = map->name;
  470. mtd->writesize = 1;
  471. mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  472. pr_debug("MTD %s(): write buffer size %d\n", __func__,
  473. mtd->writebufsize);
  474. mtd->_panic_write = cfi_amdstd_panic_write;
  475. mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
  476. if (cfi->cfi_mode==CFI_MODE_CFI){
  477. unsigned char bootloc;
  478. __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
  479. struct cfi_pri_amdstd *extp;
  480. extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
  481. if (extp) {
  482. /*
  483. * It's a real CFI chip, not one for which the probe
  484. * routine faked a CFI structure.
  485. */
  486. cfi_fixup_major_minor(cfi, extp);
  487. /*
  488. * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4, 1.5
  489. * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19
  490. * http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
  491. * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
  492. * http://www.spansion.com/Support/Datasheets/S29GL_128S_01GS_00_02_e.pdf
  493. */
  494. if (extp->MajorVersion != '1' ||
  495. (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '5'))) {
  496. printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
  497. "version %c.%c (%#02x/%#02x).\n",
  498. extp->MajorVersion, extp->MinorVersion,
  499. extp->MajorVersion, extp->MinorVersion);
  500. kfree(extp);
  501. kfree(mtd);
  502. return NULL;
  503. }
  504. printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n",
  505. extp->MajorVersion, extp->MinorVersion);
  506. /* Install our own private info structure */
  507. cfi->cmdset_priv = extp;
  508. /* Apply cfi device specific fixups */
  509. cfi_fixup(mtd, cfi_fixup_table);
  510. #ifdef DEBUG_CFI_FEATURES
  511. /* Tell the user about it in lots of lovely detail */
  512. cfi_tell_features(extp);
  513. #endif
  514. #ifdef CONFIG_OF
  515. if (np && of_property_read_bool(
  516. np, "use-advanced-sector-protection")
  517. && extp->BlkProtUnprot == 8) {
  518. printk(KERN_INFO " Advanced Sector Protection (PPB Locking) supported\n");
  519. mtd->_lock = cfi_ppb_lock;
  520. mtd->_unlock = cfi_ppb_unlock;
  521. mtd->_is_locked = cfi_ppb_is_locked;
  522. }
  523. #endif
  524. bootloc = extp->TopBottom;
  525. if ((bootloc < 2) || (bootloc > 5)) {
  526. printk(KERN_WARNING "%s: CFI contains unrecognised boot "
  527. "bank location (%d). Assuming bottom.\n",
  528. map->name, bootloc);
  529. bootloc = 2;
  530. }
  531. if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
  532. printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
  533. for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
  534. int j = (cfi->cfiq->NumEraseRegions-1)-i;
  535. swap(cfi->cfiq->EraseRegionInfo[i],
  536. cfi->cfiq->EraseRegionInfo[j]);
  537. }
  538. }
  539. /* Set the default CFI lock/unlock addresses */
  540. cfi->addr_unlock1 = 0x555;
  541. cfi->addr_unlock2 = 0x2aa;
  542. }
  543. cfi_fixup(mtd, cfi_nopri_fixup_table);
  544. if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
  545. kfree(mtd);
  546. return NULL;
  547. }
  548. } /* CFI mode */
  549. else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
  550. /* Apply jedec specific fixups */
  551. cfi_fixup(mtd, jedec_fixup_table);
  552. }
  553. /* Apply generic fixups */
  554. cfi_fixup(mtd, fixup_table);
  555. for (i=0; i< cfi->numchips; i++) {
  556. cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
  557. cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
  558. cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
  559. /*
  560. * First calculate the timeout max according to timeout field
  561. * of struct cfi_ident that probed from chip's CFI aera, if
  562. * available. Specify a minimum of 2000us, in case the CFI data
  563. * is wrong.
  564. */
  565. if (cfi->cfiq->BufWriteTimeoutTyp &&
  566. cfi->cfiq->BufWriteTimeoutMax)
  567. cfi->chips[i].buffer_write_time_max =
  568. 1 << (cfi->cfiq->BufWriteTimeoutTyp +
  569. cfi->cfiq->BufWriteTimeoutMax);
  570. else
  571. cfi->chips[i].buffer_write_time_max = 0;
  572. cfi->chips[i].buffer_write_time_max =
  573. max(cfi->chips[i].buffer_write_time_max, 2000);
  574. cfi->chips[i].ref_point_counter = 0;
  575. init_waitqueue_head(&(cfi->chips[i].wq));
  576. }
  577. map->fldrv = &cfi_amdstd_chipdrv;
  578. return cfi_amdstd_setup(mtd);
  579. }
  580. struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
  581. struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
  582. EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
  583. EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
  584. EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
  585. static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
  586. {
  587. struct map_info *map = mtd->priv;
  588. struct cfi_private *cfi = map->fldrv_priv;
  589. unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
  590. unsigned long offset = 0;
  591. int i,j;
  592. printk(KERN_NOTICE "number of %s chips: %d\n",
  593. (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
  594. /* Select the correct geometry setup */
  595. mtd->size = devsize * cfi->numchips;
  596. mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
  597. mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
  598. * mtd->numeraseregions, GFP_KERNEL);
  599. if (!mtd->eraseregions)
  600. goto setup_err;
  601. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  602. unsigned long ernum, ersize;
  603. ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
  604. ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
  605. if (mtd->erasesize < ersize) {
  606. mtd->erasesize = ersize;
  607. }
  608. for (j=0; j<cfi->numchips; j++) {
  609. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
  610. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
  611. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
  612. }
  613. offset += (ersize * ernum);
  614. }
  615. if (offset != devsize) {
  616. /* Argh */
  617. printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
  618. goto setup_err;
  619. }
  620. __module_get(THIS_MODULE);
  621. register_reboot_notifier(&mtd->reboot_notifier);
  622. return mtd;
  623. setup_err:
  624. kfree(mtd->eraseregions);
  625. kfree(mtd);
  626. kfree(cfi->cmdset_priv);
  627. kfree(cfi->cfiq);
  628. return NULL;
  629. }
  630. /*
  631. * Return true if the chip is ready.
  632. *
  633. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  634. * non-suspended sector) and is indicated by no toggle bits toggling.
  635. *
  636. * Note that anything more complicated than checking if no bits are toggling
  637. * (including checking DQ5 for an error status) is tricky to get working
  638. * correctly and is therefore not done (particularly with interleaved chips
  639. * as each chip must be checked independently of the others).
  640. */
  641. static int __xipram chip_ready(struct map_info *map, unsigned long addr)
  642. {
  643. map_word d, t;
  644. d = map_read(map, addr);
  645. t = map_read(map, addr);
  646. return map_word_equal(map, d, t);
  647. }
  648. /*
  649. * Return true if the chip is ready and has the correct value.
  650. *
  651. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  652. * non-suspended sector) and it is indicated by no bits toggling.
  653. *
  654. * Error are indicated by toggling bits or bits held with the wrong value,
  655. * or with bits toggling.
  656. *
  657. * Note that anything more complicated than checking if no bits are toggling
  658. * (including checking DQ5 for an error status) is tricky to get working
  659. * correctly and is therefore not done (particularly with interleaved chips
  660. * as each chip must be checked independently of the others).
  661. *
  662. */
  663. static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
  664. {
  665. map_word oldd, curd;
  666. oldd = map_read(map, addr);
  667. curd = map_read(map, addr);
  668. return map_word_equal(map, oldd, curd) &&
  669. map_word_equal(map, curd, expected);
  670. }
  671. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  672. {
  673. DECLARE_WAITQUEUE(wait, current);
  674. struct cfi_private *cfi = map->fldrv_priv;
  675. unsigned long timeo;
  676. struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
  677. resettime:
  678. timeo = jiffies + HZ;
  679. retry:
  680. switch (chip->state) {
  681. case FL_STATUS:
  682. for (;;) {
  683. if (chip_ready(map, adr))
  684. break;
  685. if (time_after(jiffies, timeo)) {
  686. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  687. return -EIO;
  688. }
  689. mutex_unlock(&chip->mutex);
  690. cfi_udelay(1);
  691. mutex_lock(&chip->mutex);
  692. /* Someone else might have been playing with it. */
  693. goto retry;
  694. }
  695. case FL_READY:
  696. case FL_CFI_QUERY:
  697. case FL_JEDEC_QUERY:
  698. return 0;
  699. case FL_ERASING:
  700. if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
  701. !(mode == FL_READY || mode == FL_POINT ||
  702. (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
  703. goto sleep;
  704. /* Do not allow suspend iff read/write to EB address */
  705. if ((adr & chip->in_progress_block_mask) ==
  706. chip->in_progress_block_addr)
  707. goto sleep;
  708. /* Erase suspend */
  709. /* It's harmless to issue the Erase-Suspend and Erase-Resume
  710. * commands when the erase algorithm isn't in progress. */
  711. map_write(map, CMD(0xB0), chip->in_progress_block_addr);
  712. chip->oldstate = FL_ERASING;
  713. chip->state = FL_ERASE_SUSPENDING;
  714. chip->erase_suspended = 1;
  715. for (;;) {
  716. if (chip_ready(map, adr))
  717. break;
  718. if (time_after(jiffies, timeo)) {
  719. /* Should have suspended the erase by now.
  720. * Send an Erase-Resume command as either
  721. * there was an error (so leave the erase
  722. * routine to recover from it) or we trying to
  723. * use the erase-in-progress sector. */
  724. put_chip(map, chip, adr);
  725. printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
  726. return -EIO;
  727. }
  728. mutex_unlock(&chip->mutex);
  729. cfi_udelay(1);
  730. mutex_lock(&chip->mutex);
  731. /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
  732. So we can just loop here. */
  733. }
  734. chip->state = FL_READY;
  735. return 0;
  736. case FL_XIP_WHILE_ERASING:
  737. if (mode != FL_READY && mode != FL_POINT &&
  738. (!cfip || !(cfip->EraseSuspend&2)))
  739. goto sleep;
  740. chip->oldstate = chip->state;
  741. chip->state = FL_READY;
  742. return 0;
  743. case FL_SHUTDOWN:
  744. /* The machine is rebooting */
  745. return -EIO;
  746. case FL_POINT:
  747. /* Only if there's no operation suspended... */
  748. if (mode == FL_READY && chip->oldstate == FL_READY)
  749. return 0;
  750. default:
  751. sleep:
  752. set_current_state(TASK_UNINTERRUPTIBLE);
  753. add_wait_queue(&chip->wq, &wait);
  754. mutex_unlock(&chip->mutex);
  755. schedule();
  756. remove_wait_queue(&chip->wq, &wait);
  757. mutex_lock(&chip->mutex);
  758. goto resettime;
  759. }
  760. }
  761. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
  762. {
  763. struct cfi_private *cfi = map->fldrv_priv;
  764. switch(chip->oldstate) {
  765. case FL_ERASING:
  766. cfi_fixup_m29ew_erase_suspend(map,
  767. chip->in_progress_block_addr);
  768. map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
  769. cfi_fixup_m29ew_delay_after_resume(cfi);
  770. chip->oldstate = FL_READY;
  771. chip->state = FL_ERASING;
  772. break;
  773. case FL_XIP_WHILE_ERASING:
  774. chip->state = chip->oldstate;
  775. chip->oldstate = FL_READY;
  776. break;
  777. case FL_READY:
  778. case FL_STATUS:
  779. break;
  780. default:
  781. printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
  782. }
  783. wake_up(&chip->wq);
  784. }
  785. #ifdef CONFIG_MTD_XIP
  786. /*
  787. * No interrupt what so ever can be serviced while the flash isn't in array
  788. * mode. This is ensured by the xip_disable() and xip_enable() functions
  789. * enclosing any code path where the flash is known not to be in array mode.
  790. * And within a XIP disabled code path, only functions marked with __xipram
  791. * may be called and nothing else (it's a good thing to inspect generated
  792. * assembly to make sure inline functions were actually inlined and that gcc
  793. * didn't emit calls to its own support functions). Also configuring MTD CFI
  794. * support to a single buswidth and a single interleave is also recommended.
  795. */
  796. static void xip_disable(struct map_info *map, struct flchip *chip,
  797. unsigned long adr)
  798. {
  799. /* TODO: chips with no XIP use should ignore and return */
  800. (void) map_read(map, adr); /* ensure mmu mapping is up to date */
  801. local_irq_disable();
  802. }
  803. static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
  804. unsigned long adr)
  805. {
  806. struct cfi_private *cfi = map->fldrv_priv;
  807. if (chip->state != FL_POINT && chip->state != FL_READY) {
  808. map_write(map, CMD(0xf0), adr);
  809. chip->state = FL_READY;
  810. }
  811. (void) map_read(map, adr);
  812. xip_iprefetch();
  813. local_irq_enable();
  814. }
  815. /*
  816. * When a delay is required for the flash operation to complete, the
  817. * xip_udelay() function is polling for both the given timeout and pending
  818. * (but still masked) hardware interrupts. Whenever there is an interrupt
  819. * pending then the flash erase operation is suspended, array mode restored
  820. * and interrupts unmasked. Task scheduling might also happen at that
  821. * point. The CPU eventually returns from the interrupt or the call to
  822. * schedule() and the suspended flash operation is resumed for the remaining
  823. * of the delay period.
  824. *
  825. * Warning: this function _will_ fool interrupt latency tracing tools.
  826. */
  827. static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
  828. unsigned long adr, int usec)
  829. {
  830. struct cfi_private *cfi = map->fldrv_priv;
  831. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  832. map_word status, OK = CMD(0x80);
  833. unsigned long suspended, start = xip_currtime();
  834. flstate_t oldstate;
  835. do {
  836. cpu_relax();
  837. if (xip_irqpending() && extp &&
  838. ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
  839. (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
  840. /*
  841. * Let's suspend the erase operation when supported.
  842. * Note that we currently don't try to suspend
  843. * interleaved chips if there is already another
  844. * operation suspended (imagine what happens
  845. * when one chip was already done with the current
  846. * operation while another chip suspended it, then
  847. * we resume the whole thing at once). Yes, it
  848. * can happen!
  849. */
  850. map_write(map, CMD(0xb0), adr);
  851. usec -= xip_elapsed_since(start);
  852. suspended = xip_currtime();
  853. do {
  854. if (xip_elapsed_since(suspended) > 100000) {
  855. /*
  856. * The chip doesn't want to suspend
  857. * after waiting for 100 msecs.
  858. * This is a critical error but there
  859. * is not much we can do here.
  860. */
  861. return;
  862. }
  863. status = map_read(map, adr);
  864. } while (!map_word_andequal(map, status, OK, OK));
  865. /* Suspend succeeded */
  866. oldstate = chip->state;
  867. if (!map_word_bitsset(map, status, CMD(0x40)))
  868. break;
  869. chip->state = FL_XIP_WHILE_ERASING;
  870. chip->erase_suspended = 1;
  871. map_write(map, CMD(0xf0), adr);
  872. (void) map_read(map, adr);
  873. xip_iprefetch();
  874. local_irq_enable();
  875. mutex_unlock(&chip->mutex);
  876. xip_iprefetch();
  877. cond_resched();
  878. /*
  879. * We're back. However someone else might have
  880. * decided to go write to the chip if we are in
  881. * a suspended erase state. If so let's wait
  882. * until it's done.
  883. */
  884. mutex_lock(&chip->mutex);
  885. while (chip->state != FL_XIP_WHILE_ERASING) {
  886. DECLARE_WAITQUEUE(wait, current);
  887. set_current_state(TASK_UNINTERRUPTIBLE);
  888. add_wait_queue(&chip->wq, &wait);
  889. mutex_unlock(&chip->mutex);
  890. schedule();
  891. remove_wait_queue(&chip->wq, &wait);
  892. mutex_lock(&chip->mutex);
  893. }
  894. /* Disallow XIP again */
  895. local_irq_disable();
  896. /* Correct Erase Suspend Hangups for M29EW */
  897. cfi_fixup_m29ew_erase_suspend(map, adr);
  898. /* Resume the write or erase operation */
  899. map_write(map, cfi->sector_erase_cmd, adr);
  900. chip->state = oldstate;
  901. start = xip_currtime();
  902. } else if (usec >= 1000000/HZ) {
  903. /*
  904. * Try to save on CPU power when waiting delay
  905. * is at least a system timer tick period.
  906. * No need to be extremely accurate here.
  907. */
  908. xip_cpu_idle();
  909. }
  910. status = map_read(map, adr);
  911. } while (!map_word_andequal(map, status, OK, OK)
  912. && xip_elapsed_since(start) < usec);
  913. }
  914. #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
  915. /*
  916. * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
  917. * the flash is actively programming or erasing since we have to poll for
  918. * the operation to complete anyway. We can't do that in a generic way with
  919. * a XIP setup so do it before the actual flash operation in this case
  920. * and stub it out from INVALIDATE_CACHE_UDELAY.
  921. */
  922. #define XIP_INVAL_CACHED_RANGE(map, from, size) \
  923. INVALIDATE_CACHED_RANGE(map, from, size)
  924. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  925. UDELAY(map, chip, adr, usec)
  926. /*
  927. * Extra notes:
  928. *
  929. * Activating this XIP support changes the way the code works a bit. For
  930. * example the code to suspend the current process when concurrent access
  931. * happens is never executed because xip_udelay() will always return with the
  932. * same chip state as it was entered with. This is why there is no care for
  933. * the presence of add_wait_queue() or schedule() calls from within a couple
  934. * xip_disable()'d areas of code, like in do_erase_oneblock for example.
  935. * The queueing and scheduling are always happening within xip_udelay().
  936. *
  937. * Similarly, get_chip() and put_chip() just happen to always be executed
  938. * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
  939. * is in array mode, therefore never executing many cases therein and not
  940. * causing any problem with XIP.
  941. */
  942. #else
  943. #define xip_disable(map, chip, adr)
  944. #define xip_enable(map, chip, adr)
  945. #define XIP_INVAL_CACHED_RANGE(x...)
  946. #define UDELAY(map, chip, adr, usec) \
  947. do { \
  948. mutex_unlock(&chip->mutex); \
  949. cfi_udelay(usec); \
  950. mutex_lock(&chip->mutex); \
  951. } while (0)
  952. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  953. do { \
  954. mutex_unlock(&chip->mutex); \
  955. INVALIDATE_CACHED_RANGE(map, adr, len); \
  956. cfi_udelay(usec); \
  957. mutex_lock(&chip->mutex); \
  958. } while (0)
  959. #endif
  960. static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  961. {
  962. unsigned long cmd_addr;
  963. struct cfi_private *cfi = map->fldrv_priv;
  964. int ret;
  965. adr += chip->start;
  966. /* Ensure cmd read/writes are aligned. */
  967. cmd_addr = adr & ~(map_bankwidth(map)-1);
  968. mutex_lock(&chip->mutex);
  969. ret = get_chip(map, chip, cmd_addr, FL_READY);
  970. if (ret) {
  971. mutex_unlock(&chip->mutex);
  972. return ret;
  973. }
  974. if (chip->state != FL_POINT && chip->state != FL_READY) {
  975. map_write(map, CMD(0xf0), cmd_addr);
  976. chip->state = FL_READY;
  977. }
  978. map_copy_from(map, buf, adr, len);
  979. put_chip(map, chip, cmd_addr);
  980. mutex_unlock(&chip->mutex);
  981. return 0;
  982. }
  983. static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  984. {
  985. struct map_info *map = mtd->priv;
  986. struct cfi_private *cfi = map->fldrv_priv;
  987. unsigned long ofs;
  988. int chipnum;
  989. int ret = 0;
  990. /* ofs: offset within the first chip that the first read should start */
  991. chipnum = (from >> cfi->chipshift);
  992. ofs = from - (chipnum << cfi->chipshift);
  993. while (len) {
  994. unsigned long thislen;
  995. if (chipnum >= cfi->numchips)
  996. break;
  997. if ((len + ofs -1) >> cfi->chipshift)
  998. thislen = (1<<cfi->chipshift) - ofs;
  999. else
  1000. thislen = len;
  1001. ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  1002. if (ret)
  1003. break;
  1004. *retlen += thislen;
  1005. len -= thislen;
  1006. buf += thislen;
  1007. ofs = 0;
  1008. chipnum++;
  1009. }
  1010. return ret;
  1011. }
  1012. typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
  1013. loff_t adr, size_t len, u_char *buf, size_t grouplen);
  1014. static inline void otp_enter(struct map_info *map, struct flchip *chip,
  1015. loff_t adr, size_t len)
  1016. {
  1017. struct cfi_private *cfi = map->fldrv_priv;
  1018. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1019. cfi->device_type, NULL);
  1020. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1021. cfi->device_type, NULL);
  1022. cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi,
  1023. cfi->device_type, NULL);
  1024. INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
  1025. }
  1026. static inline void otp_exit(struct map_info *map, struct flchip *chip,
  1027. loff_t adr, size_t len)
  1028. {
  1029. struct cfi_private *cfi = map->fldrv_priv;
  1030. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1031. cfi->device_type, NULL);
  1032. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1033. cfi->device_type, NULL);
  1034. cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi,
  1035. cfi->device_type, NULL);
  1036. cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi,
  1037. cfi->device_type, NULL);
  1038. INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
  1039. }
  1040. static inline int do_read_secsi_onechip(struct map_info *map,
  1041. struct flchip *chip, loff_t adr,
  1042. size_t len, u_char *buf,
  1043. size_t grouplen)
  1044. {
  1045. DECLARE_WAITQUEUE(wait, current);
  1046. unsigned long timeo = jiffies + HZ;
  1047. retry:
  1048. mutex_lock(&chip->mutex);
  1049. if (chip->state != FL_READY){
  1050. set_current_state(TASK_UNINTERRUPTIBLE);
  1051. add_wait_queue(&chip->wq, &wait);
  1052. mutex_unlock(&chip->mutex);
  1053. schedule();
  1054. remove_wait_queue(&chip->wq, &wait);
  1055. timeo = jiffies + HZ;
  1056. goto retry;
  1057. }
  1058. adr += chip->start;
  1059. chip->state = FL_READY;
  1060. otp_enter(map, chip, adr, len);
  1061. map_copy_from(map, buf, adr, len);
  1062. otp_exit(map, chip, adr, len);
  1063. wake_up(&chip->wq);
  1064. mutex_unlock(&chip->mutex);
  1065. return 0;
  1066. }
  1067. static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  1068. {
  1069. struct map_info *map = mtd->priv;
  1070. struct cfi_private *cfi = map->fldrv_priv;
  1071. unsigned long ofs;
  1072. int chipnum;
  1073. int ret = 0;
  1074. /* ofs: offset within the first chip that the first read should start */
  1075. /* 8 secsi bytes per chip */
  1076. chipnum=from>>3;
  1077. ofs=from & 7;
  1078. while (len) {
  1079. unsigned long thislen;
  1080. if (chipnum >= cfi->numchips)
  1081. break;
  1082. if ((len + ofs -1) >> 3)
  1083. thislen = (1<<3) - ofs;
  1084. else
  1085. thislen = len;
  1086. ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs,
  1087. thislen, buf, 0);
  1088. if (ret)
  1089. break;
  1090. *retlen += thislen;
  1091. len -= thislen;
  1092. buf += thislen;
  1093. ofs = 0;
  1094. chipnum++;
  1095. }
  1096. return ret;
  1097. }
  1098. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
  1099. unsigned long adr, map_word datum,
  1100. int mode);
  1101. static int do_otp_write(struct map_info *map, struct flchip *chip, loff_t adr,
  1102. size_t len, u_char *buf, size_t grouplen)
  1103. {
  1104. int ret;
  1105. while (len) {
  1106. unsigned long bus_ofs = adr & ~(map_bankwidth(map)-1);
  1107. int gap = adr - bus_ofs;
  1108. int n = min_t(int, len, map_bankwidth(map) - gap);
  1109. map_word datum = map_word_ff(map);
  1110. if (n != map_bankwidth(map)) {
  1111. /* partial write of a word, load old contents */
  1112. otp_enter(map, chip, bus_ofs, map_bankwidth(map));
  1113. datum = map_read(map, bus_ofs);
  1114. otp_exit(map, chip, bus_ofs, map_bankwidth(map));
  1115. }
  1116. datum = map_word_load_partial(map, datum, buf, gap, n);
  1117. ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
  1118. if (ret)
  1119. return ret;
  1120. adr += n;
  1121. buf += n;
  1122. len -= n;
  1123. }
  1124. return 0;
  1125. }
  1126. static int do_otp_lock(struct map_info *map, struct flchip *chip, loff_t adr,
  1127. size_t len, u_char *buf, size_t grouplen)
  1128. {
  1129. struct cfi_private *cfi = map->fldrv_priv;
  1130. uint8_t lockreg;
  1131. unsigned long timeo;
  1132. int ret;
  1133. /* make sure area matches group boundaries */
  1134. if ((adr != 0) || (len != grouplen))
  1135. return -EINVAL;
  1136. mutex_lock(&chip->mutex);
  1137. ret = get_chip(map, chip, chip->start, FL_LOCKING);
  1138. if (ret) {
  1139. mutex_unlock(&chip->mutex);
  1140. return ret;
  1141. }
  1142. chip->state = FL_LOCKING;
  1143. /* Enter lock register command */
  1144. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1145. cfi->device_type, NULL);
  1146. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1147. cfi->device_type, NULL);
  1148. cfi_send_gen_cmd(0x40, cfi->addr_unlock1, chip->start, map, cfi,
  1149. cfi->device_type, NULL);
  1150. /* read lock register */
  1151. lockreg = cfi_read_query(map, 0);
  1152. /* set bit 0 to protect extended memory block */
  1153. lockreg &= ~0x01;
  1154. /* set bit 0 to protect extended memory block */
  1155. /* write lock register */
  1156. map_write(map, CMD(0xA0), chip->start);
  1157. map_write(map, CMD(lockreg), chip->start);
  1158. /* wait for chip to become ready */
  1159. timeo = jiffies + msecs_to_jiffies(2);
  1160. for (;;) {
  1161. if (chip_ready(map, adr))
  1162. break;
  1163. if (time_after(jiffies, timeo)) {
  1164. pr_err("Waiting for chip to be ready timed out.\n");
  1165. ret = -EIO;
  1166. break;
  1167. }
  1168. UDELAY(map, chip, 0, 1);
  1169. }
  1170. /* exit protection commands */
  1171. map_write(map, CMD(0x90), chip->start);
  1172. map_write(map, CMD(0x00), chip->start);
  1173. chip->state = FL_READY;
  1174. put_chip(map, chip, chip->start);
  1175. mutex_unlock(&chip->mutex);
  1176. return ret;
  1177. }
  1178. static int cfi_amdstd_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1179. size_t *retlen, u_char *buf,
  1180. otp_op_t action, int user_regs)
  1181. {
  1182. struct map_info *map = mtd->priv;
  1183. struct cfi_private *cfi = map->fldrv_priv;
  1184. int ofs_factor = cfi->interleave * cfi->device_type;
  1185. unsigned long base;
  1186. int chipnum;
  1187. struct flchip *chip;
  1188. uint8_t otp, lockreg;
  1189. int ret;
  1190. size_t user_size, factory_size, otpsize;
  1191. loff_t user_offset, factory_offset, otpoffset;
  1192. int user_locked = 0, otplocked;
  1193. *retlen = 0;
  1194. for (chipnum = 0; chipnum < cfi->numchips; chipnum++) {
  1195. chip = &cfi->chips[chipnum];
  1196. factory_size = 0;
  1197. user_size = 0;
  1198. /* Micron M29EW family */
  1199. if (is_m29ew(cfi)) {
  1200. base = chip->start;
  1201. /* check whether secsi area is factory locked
  1202. or user lockable */
  1203. mutex_lock(&chip->mutex);
  1204. ret = get_chip(map, chip, base, FL_CFI_QUERY);
  1205. if (ret) {
  1206. mutex_unlock(&chip->mutex);
  1207. return ret;
  1208. }
  1209. cfi_qry_mode_on(base, map, cfi);
  1210. otp = cfi_read_query(map, base + 0x3 * ofs_factor);
  1211. cfi_qry_mode_off(base, map, cfi);
  1212. put_chip(map, chip, base);
  1213. mutex_unlock(&chip->mutex);
  1214. if (otp & 0x80) {
  1215. /* factory locked */
  1216. factory_offset = 0;
  1217. factory_size = 0x100;
  1218. } else {
  1219. /* customer lockable */
  1220. user_offset = 0;
  1221. user_size = 0x100;
  1222. mutex_lock(&chip->mutex);
  1223. ret = get_chip(map, chip, base, FL_LOCKING);
  1224. if (ret) {
  1225. mutex_unlock(&chip->mutex);
  1226. return ret;
  1227. }
  1228. /* Enter lock register command */
  1229. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1,
  1230. chip->start, map, cfi,
  1231. cfi->device_type, NULL);
  1232. cfi_send_gen_cmd(0x55, cfi->addr_unlock2,
  1233. chip->start, map, cfi,
  1234. cfi->device_type, NULL);
  1235. cfi_send_gen_cmd(0x40, cfi->addr_unlock1,
  1236. chip->start, map, cfi,
  1237. cfi->device_type, NULL);
  1238. /* read lock register */
  1239. lockreg = cfi_read_query(map, 0);
  1240. /* exit protection commands */
  1241. map_write(map, CMD(0x90), chip->start);
  1242. map_write(map, CMD(0x00), chip->start);
  1243. put_chip(map, chip, chip->start);
  1244. mutex_unlock(&chip->mutex);
  1245. user_locked = ((lockreg & 0x01) == 0x00);
  1246. }
  1247. }
  1248. otpsize = user_regs ? user_size : factory_size;
  1249. if (!otpsize)
  1250. continue;
  1251. otpoffset = user_regs ? user_offset : factory_offset;
  1252. otplocked = user_regs ? user_locked : 1;
  1253. if (!action) {
  1254. /* return otpinfo */
  1255. struct otp_info *otpinfo;
  1256. len -= sizeof(*otpinfo);
  1257. if (len <= 0)
  1258. return -ENOSPC;
  1259. otpinfo = (struct otp_info *)buf;
  1260. otpinfo->start = from;
  1261. otpinfo->length = otpsize;
  1262. otpinfo->locked = otplocked;
  1263. buf += sizeof(*otpinfo);
  1264. *retlen += sizeof(*otpinfo);
  1265. from += otpsize;
  1266. } else if ((from < otpsize) && (len > 0)) {
  1267. size_t size;
  1268. size = (len < otpsize - from) ? len : otpsize - from;
  1269. ret = action(map, chip, otpoffset + from, size, buf,
  1270. otpsize);
  1271. if (ret < 0)
  1272. return ret;
  1273. buf += size;
  1274. len -= size;
  1275. *retlen += size;
  1276. from = 0;
  1277. } else {
  1278. from -= otpsize;
  1279. }
  1280. }
  1281. return 0;
  1282. }
  1283. static int cfi_amdstd_get_fact_prot_info(struct mtd_info *mtd, size_t len,
  1284. size_t *retlen, struct otp_info *buf)
  1285. {
  1286. return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
  1287. NULL, 0);
  1288. }
  1289. static int cfi_amdstd_get_user_prot_info(struct mtd_info *mtd, size_t len,
  1290. size_t *retlen, struct otp_info *buf)
  1291. {
  1292. return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
  1293. NULL, 1);
  1294. }
  1295. static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1296. size_t len, size_t *retlen,
  1297. u_char *buf)
  1298. {
  1299. return cfi_amdstd_otp_walk(mtd, from, len, retlen,
  1300. buf, do_read_secsi_onechip, 0);
  1301. }
  1302. static int cfi_amdstd_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1303. size_t len, size_t *retlen,
  1304. u_char *buf)
  1305. {
  1306. return cfi_amdstd_otp_walk(mtd, from, len, retlen,
  1307. buf, do_read_secsi_onechip, 1);
  1308. }
  1309. static int cfi_amdstd_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1310. size_t len, size_t *retlen,
  1311. u_char *buf)
  1312. {
  1313. return cfi_amdstd_otp_walk(mtd, from, len, retlen, buf,
  1314. do_otp_write, 1);
  1315. }
  1316. static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1317. size_t len)
  1318. {
  1319. size_t retlen;
  1320. return cfi_amdstd_otp_walk(mtd, from, len, &retlen, NULL,
  1321. do_otp_lock, 1);
  1322. }
  1323. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
  1324. unsigned long adr, map_word datum,
  1325. int mode)
  1326. {
  1327. struct cfi_private *cfi = map->fldrv_priv;
  1328. unsigned long timeo = jiffies + HZ;
  1329. /*
  1330. * We use a 1ms + 1 jiffies generic timeout for writes (most devices
  1331. * have a max write time of a few hundreds usec). However, we should
  1332. * use the maximum timeout value given by the chip at probe time
  1333. * instead. Unfortunately, struct flchip does have a field for
  1334. * maximum timeout, only for typical which can be far too short
  1335. * depending of the conditions. The ' + 1' is to avoid having a
  1336. * timeout of 0 jiffies if HZ is smaller than 1000.
  1337. */
  1338. unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
  1339. int ret = 0;
  1340. map_word oldd;
  1341. int retry_cnt = 0;
  1342. adr += chip->start;
  1343. mutex_lock(&chip->mutex);
  1344. ret = get_chip(map, chip, adr, mode);
  1345. if (ret) {
  1346. mutex_unlock(&chip->mutex);
  1347. return ret;
  1348. }
  1349. pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1350. __func__, adr, datum.x[0] );
  1351. if (mode == FL_OTP_WRITE)
  1352. otp_enter(map, chip, adr, map_bankwidth(map));
  1353. /*
  1354. * Check for a NOP for the case when the datum to write is already
  1355. * present - it saves time and works around buggy chips that corrupt
  1356. * data at other locations when 0xff is written to a location that
  1357. * already contains 0xff.
  1358. */
  1359. oldd = map_read(map, adr);
  1360. if (map_word_equal(map, oldd, datum)) {
  1361. pr_debug("MTD %s(): NOP\n",
  1362. __func__);
  1363. goto op_done;
  1364. }
  1365. XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
  1366. ENABLE_VPP(map);
  1367. xip_disable(map, chip, adr);
  1368. retry:
  1369. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1370. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1371. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1372. map_write(map, datum, adr);
  1373. chip->state = mode;
  1374. INVALIDATE_CACHE_UDELAY(map, chip,
  1375. adr, map_bankwidth(map),
  1376. chip->word_write_time);
  1377. /* See comment above for timeout value. */
  1378. timeo = jiffies + uWriteTimeout;
  1379. for (;;) {
  1380. if (chip->state != mode) {
  1381. /* Someone's suspended the write. Sleep */
  1382. DECLARE_WAITQUEUE(wait, current);
  1383. set_current_state(TASK_UNINTERRUPTIBLE);
  1384. add_wait_queue(&chip->wq, &wait);
  1385. mutex_unlock(&chip->mutex);
  1386. schedule();
  1387. remove_wait_queue(&chip->wq, &wait);
  1388. timeo = jiffies + (HZ / 2); /* FIXME */
  1389. mutex_lock(&chip->mutex);
  1390. continue;
  1391. }
  1392. if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
  1393. xip_enable(map, chip, adr);
  1394. printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
  1395. xip_disable(map, chip, adr);
  1396. break;
  1397. }
  1398. if (chip_ready(map, adr))
  1399. break;
  1400. /* Latency issues. Drop the lock, wait a while and retry */
  1401. UDELAY(map, chip, adr, 1);
  1402. }
  1403. /* Did we succeed? */
  1404. if (!chip_good(map, adr, datum)) {
  1405. /* reset on all failures. */
  1406. map_write( map, CMD(0xF0), chip->start );
  1407. /* FIXME - should have reset delay before continuing */
  1408. if (++retry_cnt <= MAX_RETRIES)
  1409. goto retry;
  1410. ret = -EIO;
  1411. }
  1412. xip_enable(map, chip, adr);
  1413. op_done:
  1414. if (mode == FL_OTP_WRITE)
  1415. otp_exit(map, chip, adr, map_bankwidth(map));
  1416. chip->state = FL_READY;
  1417. DISABLE_VPP(map);
  1418. put_chip(map, chip, adr);
  1419. mutex_unlock(&chip->mutex);
  1420. return ret;
  1421. }
  1422. static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
  1423. size_t *retlen, const u_char *buf)
  1424. {
  1425. struct map_info *map = mtd->priv;
  1426. struct cfi_private *cfi = map->fldrv_priv;
  1427. int ret = 0;
  1428. int chipnum;
  1429. unsigned long ofs, chipstart;
  1430. DECLARE_WAITQUEUE(wait, current);
  1431. chipnum = to >> cfi->chipshift;
  1432. ofs = to - (chipnum << cfi->chipshift);
  1433. chipstart = cfi->chips[chipnum].start;
  1434. /* If it's not bus-aligned, do the first byte write */
  1435. if (ofs & (map_bankwidth(map)-1)) {
  1436. unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
  1437. int i = ofs - bus_ofs;
  1438. int n = 0;
  1439. map_word tmp_buf;
  1440. retry:
  1441. mutex_lock(&cfi->chips[chipnum].mutex);
  1442. if (cfi->chips[chipnum].state != FL_READY) {
  1443. set_current_state(TASK_UNINTERRUPTIBLE);
  1444. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1445. mutex_unlock(&cfi->chips[chipnum].mutex);
  1446. schedule();
  1447. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1448. goto retry;
  1449. }
  1450. /* Load 'tmp_buf' with old contents of flash */
  1451. tmp_buf = map_read(map, bus_ofs+chipstart);
  1452. mutex_unlock(&cfi->chips[chipnum].mutex);
  1453. /* Number of bytes to copy from buffer */
  1454. n = min_t(int, len, map_bankwidth(map)-i);
  1455. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1456. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1457. bus_ofs, tmp_buf, FL_WRITING);
  1458. if (ret)
  1459. return ret;
  1460. ofs += n;
  1461. buf += n;
  1462. (*retlen) += n;
  1463. len -= n;
  1464. if (ofs >> cfi->chipshift) {
  1465. chipnum ++;
  1466. ofs = 0;
  1467. if (chipnum == cfi->numchips)
  1468. return 0;
  1469. }
  1470. }
  1471. /* We are now aligned, write as much as possible */
  1472. while(len >= map_bankwidth(map)) {
  1473. map_word datum;
  1474. datum = map_word_load(map, buf);
  1475. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1476. ofs, datum, FL_WRITING);
  1477. if (ret)
  1478. return ret;
  1479. ofs += map_bankwidth(map);
  1480. buf += map_bankwidth(map);
  1481. (*retlen) += map_bankwidth(map);
  1482. len -= map_bankwidth(map);
  1483. if (ofs >> cfi->chipshift) {
  1484. chipnum ++;
  1485. ofs = 0;
  1486. if (chipnum == cfi->numchips)
  1487. return 0;
  1488. chipstart = cfi->chips[chipnum].start;
  1489. }
  1490. }
  1491. /* Write the trailing bytes if any */
  1492. if (len & (map_bankwidth(map)-1)) {
  1493. map_word tmp_buf;
  1494. retry1:
  1495. mutex_lock(&cfi->chips[chipnum].mutex);
  1496. if (cfi->chips[chipnum].state != FL_READY) {
  1497. set_current_state(TASK_UNINTERRUPTIBLE);
  1498. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1499. mutex_unlock(&cfi->chips[chipnum].mutex);
  1500. schedule();
  1501. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1502. goto retry1;
  1503. }
  1504. tmp_buf = map_read(map, ofs + chipstart);
  1505. mutex_unlock(&cfi->chips[chipnum].mutex);
  1506. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1507. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1508. ofs, tmp_buf, FL_WRITING);
  1509. if (ret)
  1510. return ret;
  1511. (*retlen) += len;
  1512. }
  1513. return 0;
  1514. }
  1515. /*
  1516. * FIXME: interleaved mode not tested, and probably not supported!
  1517. */
  1518. static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
  1519. unsigned long adr, const u_char *buf,
  1520. int len)
  1521. {
  1522. struct cfi_private *cfi = map->fldrv_priv;
  1523. unsigned long timeo = jiffies + HZ;
  1524. /*
  1525. * Timeout is calculated according to CFI data, if available.
  1526. * See more comments in cfi_cmdset_0002().
  1527. */
  1528. unsigned long uWriteTimeout =
  1529. usecs_to_jiffies(chip->buffer_write_time_max);
  1530. int ret = -EIO;
  1531. unsigned long cmd_adr;
  1532. int z, words;
  1533. map_word datum;
  1534. adr += chip->start;
  1535. cmd_adr = adr;
  1536. mutex_lock(&chip->mutex);
  1537. ret = get_chip(map, chip, adr, FL_WRITING);
  1538. if (ret) {
  1539. mutex_unlock(&chip->mutex);
  1540. return ret;
  1541. }
  1542. datum = map_word_load(map, buf);
  1543. pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1544. __func__, adr, datum.x[0] );
  1545. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1546. ENABLE_VPP(map);
  1547. xip_disable(map, chip, cmd_adr);
  1548. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1549. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1550. /* Write Buffer Load */
  1551. map_write(map, CMD(0x25), cmd_adr);
  1552. chip->state = FL_WRITING_TO_BUFFER;
  1553. /* Write length of data to come */
  1554. words = len / map_bankwidth(map);
  1555. map_write(map, CMD(words - 1), cmd_adr);
  1556. /* Write data */
  1557. z = 0;
  1558. while(z < words * map_bankwidth(map)) {
  1559. datum = map_word_load(map, buf);
  1560. map_write(map, datum, adr + z);
  1561. z += map_bankwidth(map);
  1562. buf += map_bankwidth(map);
  1563. }
  1564. z -= map_bankwidth(map);
  1565. adr += z;
  1566. /* Write Buffer Program Confirm: GO GO GO */
  1567. map_write(map, CMD(0x29), cmd_adr);
  1568. chip->state = FL_WRITING;
  1569. INVALIDATE_CACHE_UDELAY(map, chip,
  1570. adr, map_bankwidth(map),
  1571. chip->word_write_time);
  1572. timeo = jiffies + uWriteTimeout;
  1573. for (;;) {
  1574. if (chip->state != FL_WRITING) {
  1575. /* Someone's suspended the write. Sleep */
  1576. DECLARE_WAITQUEUE(wait, current);
  1577. set_current_state(TASK_UNINTERRUPTIBLE);
  1578. add_wait_queue(&chip->wq, &wait);
  1579. mutex_unlock(&chip->mutex);
  1580. schedule();
  1581. remove_wait_queue(&chip->wq, &wait);
  1582. timeo = jiffies + (HZ / 2); /* FIXME */
  1583. mutex_lock(&chip->mutex);
  1584. continue;
  1585. }
  1586. if (time_after(jiffies, timeo) && !chip_ready(map, adr))
  1587. break;
  1588. if (chip_good(map, adr, datum)) {
  1589. xip_enable(map, chip, adr);
  1590. goto op_done;
  1591. }
  1592. /* Latency issues. Drop the lock, wait a while and retry */
  1593. UDELAY(map, chip, adr, 1);
  1594. }
  1595. /*
  1596. * Recovery from write-buffer programming failures requires
  1597. * the write-to-buffer-reset sequence. Since the last part
  1598. * of the sequence also works as a normal reset, we can run
  1599. * the same commands regardless of why we are here.
  1600. * See e.g.
  1601. * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf
  1602. */
  1603. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1604. cfi->device_type, NULL);
  1605. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1606. cfi->device_type, NULL);
  1607. cfi_send_gen_cmd(0xF0, cfi->addr_unlock1, chip->start, map, cfi,
  1608. cfi->device_type, NULL);
  1609. xip_enable(map, chip, adr);
  1610. /* FIXME - should have reset delay before continuing */
  1611. printk(KERN_WARNING "MTD %s(): software timeout, address:0x%.8lx.\n",
  1612. __func__, adr);
  1613. ret = -EIO;
  1614. op_done:
  1615. chip->state = FL_READY;
  1616. DISABLE_VPP(map);
  1617. put_chip(map, chip, adr);
  1618. mutex_unlock(&chip->mutex);
  1619. return ret;
  1620. }
  1621. static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
  1622. size_t *retlen, const u_char *buf)
  1623. {
  1624. struct map_info *map = mtd->priv;
  1625. struct cfi_private *cfi = map->fldrv_priv;
  1626. int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1627. int ret = 0;
  1628. int chipnum;
  1629. unsigned long ofs;
  1630. chipnum = to >> cfi->chipshift;
  1631. ofs = to - (chipnum << cfi->chipshift);
  1632. /* If it's not bus-aligned, do the first word write */
  1633. if (ofs & (map_bankwidth(map)-1)) {
  1634. size_t local_len = (-ofs)&(map_bankwidth(map)-1);
  1635. if (local_len > len)
  1636. local_len = len;
  1637. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1638. local_len, retlen, buf);
  1639. if (ret)
  1640. return ret;
  1641. ofs += local_len;
  1642. buf += local_len;
  1643. len -= local_len;
  1644. if (ofs >> cfi->chipshift) {
  1645. chipnum ++;
  1646. ofs = 0;
  1647. if (chipnum == cfi->numchips)
  1648. return 0;
  1649. }
  1650. }
  1651. /* Write buffer is worth it only if more than one word to write... */
  1652. while (len >= map_bankwidth(map) * 2) {
  1653. /* We must not cross write block boundaries */
  1654. int size = wbufsize - (ofs & (wbufsize-1));
  1655. if (size > len)
  1656. size = len;
  1657. if (size % map_bankwidth(map))
  1658. size -= size % map_bankwidth(map);
  1659. ret = do_write_buffer(map, &cfi->chips[chipnum],
  1660. ofs, buf, size);
  1661. if (ret)
  1662. return ret;
  1663. ofs += size;
  1664. buf += size;
  1665. (*retlen) += size;
  1666. len -= size;
  1667. if (ofs >> cfi->chipshift) {
  1668. chipnum ++;
  1669. ofs = 0;
  1670. if (chipnum == cfi->numchips)
  1671. return 0;
  1672. }
  1673. }
  1674. if (len) {
  1675. size_t retlen_dregs = 0;
  1676. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1677. len, &retlen_dregs, buf);
  1678. *retlen += retlen_dregs;
  1679. return ret;
  1680. }
  1681. return 0;
  1682. }
  1683. /*
  1684. * Wait for the flash chip to become ready to write data
  1685. *
  1686. * This is only called during the panic_write() path. When panic_write()
  1687. * is called, the kernel is in the process of a panic, and will soon be
  1688. * dead. Therefore we don't take any locks, and attempt to get access
  1689. * to the chip as soon as possible.
  1690. */
  1691. static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
  1692. unsigned long adr)
  1693. {
  1694. struct cfi_private *cfi = map->fldrv_priv;
  1695. int retries = 10;
  1696. int i;
  1697. /*
  1698. * If the driver thinks the chip is idle, and no toggle bits
  1699. * are changing, then the chip is actually idle for sure.
  1700. */
  1701. if (chip->state == FL_READY && chip_ready(map, adr))
  1702. return 0;
  1703. /*
  1704. * Try several times to reset the chip and then wait for it
  1705. * to become idle. The upper limit of a few milliseconds of
  1706. * delay isn't a big problem: the kernel is dying anyway. It
  1707. * is more important to save the messages.
  1708. */
  1709. while (retries > 0) {
  1710. const unsigned long timeo = (HZ / 1000) + 1;
  1711. /* send the reset command */
  1712. map_write(map, CMD(0xF0), chip->start);
  1713. /* wait for the chip to become ready */
  1714. for (i = 0; i < jiffies_to_usecs(timeo); i++) {
  1715. if (chip_ready(map, adr))
  1716. return 0;
  1717. udelay(1);
  1718. }
  1719. retries--;
  1720. }
  1721. /* the chip never became ready */
  1722. return -EBUSY;
  1723. }
  1724. /*
  1725. * Write out one word of data to a single flash chip during a kernel panic
  1726. *
  1727. * This is only called during the panic_write() path. When panic_write()
  1728. * is called, the kernel is in the process of a panic, and will soon be
  1729. * dead. Therefore we don't take any locks, and attempt to get access
  1730. * to the chip as soon as possible.
  1731. *
  1732. * The implementation of this routine is intentionally similar to
  1733. * do_write_oneword(), in order to ease code maintenance.
  1734. */
  1735. static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
  1736. unsigned long adr, map_word datum)
  1737. {
  1738. const unsigned long uWriteTimeout = (HZ / 1000) + 1;
  1739. struct cfi_private *cfi = map->fldrv_priv;
  1740. int retry_cnt = 0;
  1741. map_word oldd;
  1742. int ret = 0;
  1743. int i;
  1744. adr += chip->start;
  1745. ret = cfi_amdstd_panic_wait(map, chip, adr);
  1746. if (ret)
  1747. return ret;
  1748. pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
  1749. __func__, adr, datum.x[0]);
  1750. /*
  1751. * Check for a NOP for the case when the datum to write is already
  1752. * present - it saves time and works around buggy chips that corrupt
  1753. * data at other locations when 0xff is written to a location that
  1754. * already contains 0xff.
  1755. */
  1756. oldd = map_read(map, adr);
  1757. if (map_word_equal(map, oldd, datum)) {
  1758. pr_debug("MTD %s(): NOP\n", __func__);
  1759. goto op_done;
  1760. }
  1761. ENABLE_VPP(map);
  1762. retry:
  1763. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1764. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1765. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1766. map_write(map, datum, adr);
  1767. for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
  1768. if (chip_ready(map, adr))
  1769. break;
  1770. udelay(1);
  1771. }
  1772. if (!chip_good(map, adr, datum)) {
  1773. /* reset on all failures. */
  1774. map_write(map, CMD(0xF0), chip->start);
  1775. /* FIXME - should have reset delay before continuing */
  1776. if (++retry_cnt <= MAX_RETRIES)
  1777. goto retry;
  1778. ret = -EIO;
  1779. }
  1780. op_done:
  1781. DISABLE_VPP(map);
  1782. return ret;
  1783. }
  1784. /*
  1785. * Write out some data during a kernel panic
  1786. *
  1787. * This is used by the mtdoops driver to save the dying messages from a
  1788. * kernel which has panic'd.
  1789. *
  1790. * This routine ignores all of the locking used throughout the rest of the
  1791. * driver, in order to ensure that the data gets written out no matter what
  1792. * state this driver (and the flash chip itself) was in when the kernel crashed.
  1793. *
  1794. * The implementation of this routine is intentionally similar to
  1795. * cfi_amdstd_write_words(), in order to ease code maintenance.
  1796. */
  1797. static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  1798. size_t *retlen, const u_char *buf)
  1799. {
  1800. struct map_info *map = mtd->priv;
  1801. struct cfi_private *cfi = map->fldrv_priv;
  1802. unsigned long ofs, chipstart;
  1803. int ret = 0;
  1804. int chipnum;
  1805. chipnum = to >> cfi->chipshift;
  1806. ofs = to - (chipnum << cfi->chipshift);
  1807. chipstart = cfi->chips[chipnum].start;
  1808. /* If it's not bus aligned, do the first byte write */
  1809. if (ofs & (map_bankwidth(map) - 1)) {
  1810. unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
  1811. int i = ofs - bus_ofs;
  1812. int n = 0;
  1813. map_word tmp_buf;
  1814. ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
  1815. if (ret)
  1816. return ret;
  1817. /* Load 'tmp_buf' with old contents of flash */
  1818. tmp_buf = map_read(map, bus_ofs + chipstart);
  1819. /* Number of bytes to copy from buffer */
  1820. n = min_t(int, len, map_bankwidth(map) - i);
  1821. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1822. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1823. bus_ofs, tmp_buf);
  1824. if (ret)
  1825. return ret;
  1826. ofs += n;
  1827. buf += n;
  1828. (*retlen) += n;
  1829. len -= n;
  1830. if (ofs >> cfi->chipshift) {
  1831. chipnum++;
  1832. ofs = 0;
  1833. if (chipnum == cfi->numchips)
  1834. return 0;
  1835. }
  1836. }
  1837. /* We are now aligned, write as much as possible */
  1838. while (len >= map_bankwidth(map)) {
  1839. map_word datum;
  1840. datum = map_word_load(map, buf);
  1841. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1842. ofs, datum);
  1843. if (ret)
  1844. return ret;
  1845. ofs += map_bankwidth(map);
  1846. buf += map_bankwidth(map);
  1847. (*retlen) += map_bankwidth(map);
  1848. len -= map_bankwidth(map);
  1849. if (ofs >> cfi->chipshift) {
  1850. chipnum++;
  1851. ofs = 0;
  1852. if (chipnum == cfi->numchips)
  1853. return 0;
  1854. chipstart = cfi->chips[chipnum].start;
  1855. }
  1856. }
  1857. /* Write the trailing bytes if any */
  1858. if (len & (map_bankwidth(map) - 1)) {
  1859. map_word tmp_buf;
  1860. ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
  1861. if (ret)
  1862. return ret;
  1863. tmp_buf = map_read(map, ofs + chipstart);
  1864. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1865. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1866. ofs, tmp_buf);
  1867. if (ret)
  1868. return ret;
  1869. (*retlen) += len;
  1870. }
  1871. return 0;
  1872. }
  1873. /*
  1874. * Handle devices with one erase region, that only implement
  1875. * the chip erase command.
  1876. */
  1877. static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
  1878. {
  1879. struct cfi_private *cfi = map->fldrv_priv;
  1880. unsigned long timeo = jiffies + HZ;
  1881. unsigned long int adr;
  1882. DECLARE_WAITQUEUE(wait, current);
  1883. int ret = 0;
  1884. int retry_cnt = 0;
  1885. adr = cfi->addr_unlock1;
  1886. mutex_lock(&chip->mutex);
  1887. ret = get_chip(map, chip, adr, FL_WRITING);
  1888. if (ret) {
  1889. mutex_unlock(&chip->mutex);
  1890. return ret;
  1891. }
  1892. pr_debug("MTD %s(): ERASE 0x%.8lx\n",
  1893. __func__, chip->start );
  1894. XIP_INVAL_CACHED_RANGE(map, adr, map->size);
  1895. ENABLE_VPP(map);
  1896. xip_disable(map, chip, adr);
  1897. retry:
  1898. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1899. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1900. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1901. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1902. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1903. cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1904. chip->state = FL_ERASING;
  1905. chip->erase_suspended = 0;
  1906. chip->in_progress_block_addr = adr;
  1907. chip->in_progress_block_mask = ~(map->size - 1);
  1908. INVALIDATE_CACHE_UDELAY(map, chip,
  1909. adr, map->size,
  1910. chip->erase_time*500);
  1911. timeo = jiffies + (HZ*20);
  1912. for (;;) {
  1913. if (chip->state != FL_ERASING) {
  1914. /* Someone's suspended the erase. Sleep */
  1915. set_current_state(TASK_UNINTERRUPTIBLE);
  1916. add_wait_queue(&chip->wq, &wait);
  1917. mutex_unlock(&chip->mutex);
  1918. schedule();
  1919. remove_wait_queue(&chip->wq, &wait);
  1920. mutex_lock(&chip->mutex);
  1921. continue;
  1922. }
  1923. if (chip->erase_suspended) {
  1924. /* This erase was suspended and resumed.
  1925. Adjust the timeout */
  1926. timeo = jiffies + (HZ*20); /* FIXME */
  1927. chip->erase_suspended = 0;
  1928. }
  1929. if (chip_good(map, adr, map_word_ff(map)))
  1930. break;
  1931. if (time_after(jiffies, timeo)) {
  1932. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1933. __func__ );
  1934. ret = -EIO;
  1935. break;
  1936. }
  1937. /* Latency issues. Drop the lock, wait a while and retry */
  1938. UDELAY(map, chip, adr, 1000000/HZ);
  1939. }
  1940. /* Did we succeed? */
  1941. if (ret) {
  1942. /* reset on all failures. */
  1943. map_write( map, CMD(0xF0), chip->start );
  1944. /* FIXME - should have reset delay before continuing */
  1945. if (++retry_cnt <= MAX_RETRIES) {
  1946. ret = 0;
  1947. goto retry;
  1948. }
  1949. }
  1950. chip->state = FL_READY;
  1951. xip_enable(map, chip, adr);
  1952. DISABLE_VPP(map);
  1953. put_chip(map, chip, adr);
  1954. mutex_unlock(&chip->mutex);
  1955. return ret;
  1956. }
  1957. static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
  1958. {
  1959. struct cfi_private *cfi = map->fldrv_priv;
  1960. unsigned long timeo = jiffies + HZ;
  1961. DECLARE_WAITQUEUE(wait, current);
  1962. int ret = 0;
  1963. int retry_cnt = 0;
  1964. adr += chip->start;
  1965. mutex_lock(&chip->mutex);
  1966. ret = get_chip(map, chip, adr, FL_ERASING);
  1967. if (ret) {
  1968. mutex_unlock(&chip->mutex);
  1969. return ret;
  1970. }
  1971. pr_debug("MTD %s(): ERASE 0x%.8lx\n",
  1972. __func__, adr );
  1973. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1974. ENABLE_VPP(map);
  1975. xip_disable(map, chip, adr);
  1976. retry:
  1977. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1978. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1979. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1980. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1981. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1982. map_write(map, cfi->sector_erase_cmd, adr);
  1983. chip->state = FL_ERASING;
  1984. chip->erase_suspended = 0;
  1985. chip->in_progress_block_addr = adr;
  1986. chip->in_progress_block_mask = ~(len - 1);
  1987. INVALIDATE_CACHE_UDELAY(map, chip,
  1988. adr, len,
  1989. chip->erase_time*500);
  1990. timeo = jiffies + (HZ*20);
  1991. for (;;) {
  1992. if (chip->state != FL_ERASING) {
  1993. /* Someone's suspended the erase. Sleep */
  1994. set_current_state(TASK_UNINTERRUPTIBLE);
  1995. add_wait_queue(&chip->wq, &wait);
  1996. mutex_unlock(&chip->mutex);
  1997. schedule();
  1998. remove_wait_queue(&chip->wq, &wait);
  1999. mutex_lock(&chip->mutex);
  2000. continue;
  2001. }
  2002. if (chip->erase_suspended) {
  2003. /* This erase was suspended and resumed.
  2004. Adjust the timeout */
  2005. timeo = jiffies + (HZ*20); /* FIXME */
  2006. chip->erase_suspended = 0;
  2007. }
  2008. if (chip_good(map, adr, map_word_ff(map))) {
  2009. xip_enable(map, chip, adr);
  2010. break;
  2011. }
  2012. if (time_after(jiffies, timeo)) {
  2013. xip_enable(map, chip, adr);
  2014. printk(KERN_WARNING "MTD %s(): software timeout\n",
  2015. __func__ );
  2016. ret = -EIO;
  2017. break;
  2018. }
  2019. /* Latency issues. Drop the lock, wait a while and retry */
  2020. UDELAY(map, chip, adr, 1000000/HZ);
  2021. }
  2022. /* Did we succeed? */
  2023. if (ret) {
  2024. /* reset on all failures. */
  2025. map_write( map, CMD(0xF0), chip->start );
  2026. /* FIXME - should have reset delay before continuing */
  2027. if (++retry_cnt <= MAX_RETRIES) {
  2028. ret = 0;
  2029. goto retry;
  2030. }
  2031. }
  2032. chip->state = FL_READY;
  2033. DISABLE_VPP(map);
  2034. put_chip(map, chip, adr);
  2035. mutex_unlock(&chip->mutex);
  2036. return ret;
  2037. }
  2038. static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
  2039. {
  2040. unsigned long ofs, len;
  2041. int ret;
  2042. ofs = instr->addr;
  2043. len = instr->len;
  2044. ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
  2045. if (ret)
  2046. return ret;
  2047. instr->state = MTD_ERASE_DONE;
  2048. mtd_erase_callback(instr);
  2049. return 0;
  2050. }
  2051. static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
  2052. {
  2053. struct map_info *map = mtd->priv;
  2054. struct cfi_private *cfi = map->fldrv_priv;
  2055. int ret = 0;
  2056. if (instr->addr != 0)
  2057. return -EINVAL;
  2058. if (instr->len != mtd->size)
  2059. return -EINVAL;
  2060. ret = do_erase_chip(map, &cfi->chips[0]);
  2061. if (ret)
  2062. return ret;
  2063. instr->state = MTD_ERASE_DONE;
  2064. mtd_erase_callback(instr);
  2065. return 0;
  2066. }
  2067. static int do_atmel_lock(struct map_info *map, struct flchip *chip,
  2068. unsigned long adr, int len, void *thunk)
  2069. {
  2070. struct cfi_private *cfi = map->fldrv_priv;
  2071. int ret;
  2072. mutex_lock(&chip->mutex);
  2073. ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
  2074. if (ret)
  2075. goto out_unlock;
  2076. chip->state = FL_LOCKING;
  2077. pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
  2078. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2079. cfi->device_type, NULL);
  2080. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2081. cfi->device_type, NULL);
  2082. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
  2083. cfi->device_type, NULL);
  2084. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2085. cfi->device_type, NULL);
  2086. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2087. cfi->device_type, NULL);
  2088. map_write(map, CMD(0x40), chip->start + adr);
  2089. chip->state = FL_READY;
  2090. put_chip(map, chip, adr + chip->start);
  2091. ret = 0;
  2092. out_unlock:
  2093. mutex_unlock(&chip->mutex);
  2094. return ret;
  2095. }
  2096. static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
  2097. unsigned long adr, int len, void *thunk)
  2098. {
  2099. struct cfi_private *cfi = map->fldrv_priv;
  2100. int ret;
  2101. mutex_lock(&chip->mutex);
  2102. ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
  2103. if (ret)
  2104. goto out_unlock;
  2105. chip->state = FL_UNLOCKING;
  2106. pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
  2107. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2108. cfi->device_type, NULL);
  2109. map_write(map, CMD(0x70), adr);
  2110. chip->state = FL_READY;
  2111. put_chip(map, chip, adr + chip->start);
  2112. ret = 0;
  2113. out_unlock:
  2114. mutex_unlock(&chip->mutex);
  2115. return ret;
  2116. }
  2117. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2118. {
  2119. return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
  2120. }
  2121. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2122. {
  2123. return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
  2124. }
  2125. /*
  2126. * Advanced Sector Protection - PPB (Persistent Protection Bit) locking
  2127. */
  2128. struct ppb_lock {
  2129. struct flchip *chip;
  2130. unsigned long adr;
  2131. int locked;
  2132. };
  2133. #define MAX_SECTORS 512
  2134. #define DO_XXLOCK_ONEBLOCK_LOCK ((void *)1)
  2135. #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *)2)
  2136. #define DO_XXLOCK_ONEBLOCK_GETLOCK ((void *)3)
  2137. static int __maybe_unused do_ppb_xxlock(struct map_info *map,
  2138. struct flchip *chip,
  2139. unsigned long adr, int len, void *thunk)
  2140. {
  2141. struct cfi_private *cfi = map->fldrv_priv;
  2142. unsigned long timeo;
  2143. int ret;
  2144. adr += chip->start;
  2145. mutex_lock(&chip->mutex);
  2146. ret = get_chip(map, chip, adr, FL_LOCKING);
  2147. if (ret) {
  2148. mutex_unlock(&chip->mutex);
  2149. return ret;
  2150. }
  2151. pr_debug("MTD %s(): XXLOCK 0x%08lx len %d\n", __func__, adr, len);
  2152. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2153. cfi->device_type, NULL);
  2154. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2155. cfi->device_type, NULL);
  2156. /* PPB entry command */
  2157. cfi_send_gen_cmd(0xC0, cfi->addr_unlock1, chip->start, map, cfi,
  2158. cfi->device_type, NULL);
  2159. if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
  2160. chip->state = FL_LOCKING;
  2161. map_write(map, CMD(0xA0), adr);
  2162. map_write(map, CMD(0x00), adr);
  2163. } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
  2164. /*
  2165. * Unlocking of one specific sector is not supported, so we
  2166. * have to unlock all sectors of this device instead
  2167. */
  2168. chip->state = FL_UNLOCKING;
  2169. map_write(map, CMD(0x80), chip->start);
  2170. map_write(map, CMD(0x30), chip->start);
  2171. } else if (thunk == DO_XXLOCK_ONEBLOCK_GETLOCK) {
  2172. chip->state = FL_JEDEC_QUERY;
  2173. /* Return locked status: 0->locked, 1->unlocked */
  2174. ret = !cfi_read_query(map, adr);
  2175. } else
  2176. BUG();
  2177. /*
  2178. * Wait for some time as unlocking of all sectors takes quite long
  2179. */
  2180. timeo = jiffies + msecs_to_jiffies(2000); /* 2s max (un)locking */
  2181. for (;;) {
  2182. if (chip_ready(map, adr))
  2183. break;
  2184. if (time_after(jiffies, timeo)) {
  2185. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  2186. ret = -EIO;
  2187. break;
  2188. }
  2189. UDELAY(map, chip, adr, 1);
  2190. }
  2191. /* Exit BC commands */
  2192. map_write(map, CMD(0x90), chip->start);
  2193. map_write(map, CMD(0x00), chip->start);
  2194. chip->state = FL_READY;
  2195. put_chip(map, chip, adr);
  2196. mutex_unlock(&chip->mutex);
  2197. return ret;
  2198. }
  2199. static int __maybe_unused cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs,
  2200. uint64_t len)
  2201. {
  2202. return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2203. DO_XXLOCK_ONEBLOCK_LOCK);
  2204. }
  2205. static int __maybe_unused cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs,
  2206. uint64_t len)
  2207. {
  2208. struct mtd_erase_region_info *regions = mtd->eraseregions;
  2209. struct map_info *map = mtd->priv;
  2210. struct cfi_private *cfi = map->fldrv_priv;
  2211. struct ppb_lock *sect;
  2212. unsigned long adr;
  2213. loff_t offset;
  2214. uint64_t length;
  2215. int chipnum;
  2216. int i;
  2217. int sectors;
  2218. int ret;
  2219. /*
  2220. * PPB unlocking always unlocks all sectors of the flash chip.
  2221. * We need to re-lock all previously locked sectors. So lets
  2222. * first check the locking status of all sectors and save
  2223. * it for future use.
  2224. */
  2225. sect = kzalloc(MAX_SECTORS * sizeof(struct ppb_lock), GFP_KERNEL);
  2226. if (!sect)
  2227. return -ENOMEM;
  2228. /*
  2229. * This code to walk all sectors is a slightly modified version
  2230. * of the cfi_varsize_frob() code.
  2231. */
  2232. i = 0;
  2233. chipnum = 0;
  2234. adr = 0;
  2235. sectors = 0;
  2236. offset = 0;
  2237. length = mtd->size;
  2238. while (length) {
  2239. int size = regions[i].erasesize;
  2240. /*
  2241. * Only test sectors that shall not be unlocked. The other
  2242. * sectors shall be unlocked, so lets keep their locking
  2243. * status at "unlocked" (locked=0) for the final re-locking.
  2244. */
  2245. if ((offset < ofs) || (offset >= (ofs + len))) {
  2246. sect[sectors].chip = &cfi->chips[chipnum];
  2247. sect[sectors].adr = adr;
  2248. sect[sectors].locked = do_ppb_xxlock(
  2249. map, &cfi->chips[chipnum], adr, 0,
  2250. DO_XXLOCK_ONEBLOCK_GETLOCK);
  2251. }
  2252. adr += size;
  2253. offset += size;
  2254. length -= size;
  2255. if (offset == regions[i].offset + size * regions[i].numblocks)
  2256. i++;
  2257. if (adr >> cfi->chipshift) {
  2258. if (offset >= (ofs + len))
  2259. break;
  2260. adr = 0;
  2261. chipnum++;
  2262. if (chipnum >= cfi->numchips)
  2263. break;
  2264. }
  2265. sectors++;
  2266. if (sectors >= MAX_SECTORS) {
  2267. printk(KERN_ERR "Only %d sectors for PPB locking supported!\n",
  2268. MAX_SECTORS);
  2269. kfree(sect);
  2270. return -EINVAL;
  2271. }
  2272. }
  2273. /* Now unlock the whole chip */
  2274. ret = cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2275. DO_XXLOCK_ONEBLOCK_UNLOCK);
  2276. if (ret) {
  2277. kfree(sect);
  2278. return ret;
  2279. }
  2280. /*
  2281. * PPB unlocking always unlocks all sectors of the flash chip.
  2282. * We need to re-lock all previously locked sectors.
  2283. */
  2284. for (i = 0; i < sectors; i++) {
  2285. if (sect[i].locked)
  2286. do_ppb_xxlock(map, sect[i].chip, sect[i].adr, 0,
  2287. DO_XXLOCK_ONEBLOCK_LOCK);
  2288. }
  2289. kfree(sect);
  2290. return ret;
  2291. }
  2292. static int __maybe_unused cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs,
  2293. uint64_t len)
  2294. {
  2295. return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2296. DO_XXLOCK_ONEBLOCK_GETLOCK) ? 1 : 0;
  2297. }
  2298. static void cfi_amdstd_sync (struct mtd_info *mtd)
  2299. {
  2300. struct map_info *map = mtd->priv;
  2301. struct cfi_private *cfi = map->fldrv_priv;
  2302. int i;
  2303. struct flchip *chip;
  2304. int ret = 0;
  2305. DECLARE_WAITQUEUE(wait, current);
  2306. for (i=0; !ret && i<cfi->numchips; i++) {
  2307. chip = &cfi->chips[i];
  2308. retry:
  2309. mutex_lock(&chip->mutex);
  2310. switch(chip->state) {
  2311. case FL_READY:
  2312. case FL_STATUS:
  2313. case FL_CFI_QUERY:
  2314. case FL_JEDEC_QUERY:
  2315. chip->oldstate = chip->state;
  2316. chip->state = FL_SYNCING;
  2317. /* No need to wake_up() on this state change -
  2318. * as the whole point is that nobody can do anything
  2319. * with the chip now anyway.
  2320. */
  2321. case FL_SYNCING:
  2322. mutex_unlock(&chip->mutex);
  2323. break;
  2324. default:
  2325. /* Not an idle state */
  2326. set_current_state(TASK_UNINTERRUPTIBLE);
  2327. add_wait_queue(&chip->wq, &wait);
  2328. mutex_unlock(&chip->mutex);
  2329. schedule();
  2330. remove_wait_queue(&chip->wq, &wait);
  2331. goto retry;
  2332. }
  2333. }
  2334. /* Unlock the chips again */
  2335. for (i--; i >=0; i--) {
  2336. chip = &cfi->chips[i];
  2337. mutex_lock(&chip->mutex);
  2338. if (chip->state == FL_SYNCING) {
  2339. chip->state = chip->oldstate;
  2340. wake_up(&chip->wq);
  2341. }
  2342. mutex_unlock(&chip->mutex);
  2343. }
  2344. }
  2345. static int cfi_amdstd_suspend(struct mtd_info *mtd)
  2346. {
  2347. struct map_info *map = mtd->priv;
  2348. struct cfi_private *cfi = map->fldrv_priv;
  2349. int i;
  2350. struct flchip *chip;
  2351. int ret = 0;
  2352. for (i=0; !ret && i<cfi->numchips; i++) {
  2353. chip = &cfi->chips[i];
  2354. mutex_lock(&chip->mutex);
  2355. switch(chip->state) {
  2356. case FL_READY:
  2357. case FL_STATUS:
  2358. case FL_CFI_QUERY:
  2359. case FL_JEDEC_QUERY:
  2360. chip->oldstate = chip->state;
  2361. chip->state = FL_PM_SUSPENDED;
  2362. /* No need to wake_up() on this state change -
  2363. * as the whole point is that nobody can do anything
  2364. * with the chip now anyway.
  2365. */
  2366. case FL_PM_SUSPENDED:
  2367. break;
  2368. default:
  2369. ret = -EAGAIN;
  2370. break;
  2371. }
  2372. mutex_unlock(&chip->mutex);
  2373. }
  2374. /* Unlock the chips again */
  2375. if (ret) {
  2376. for (i--; i >=0; i--) {
  2377. chip = &cfi->chips[i];
  2378. mutex_lock(&chip->mutex);
  2379. if (chip->state == FL_PM_SUSPENDED) {
  2380. chip->state = chip->oldstate;
  2381. wake_up(&chip->wq);
  2382. }
  2383. mutex_unlock(&chip->mutex);
  2384. }
  2385. }
  2386. return ret;
  2387. }
  2388. static void cfi_amdstd_resume(struct mtd_info *mtd)
  2389. {
  2390. struct map_info *map = mtd->priv;
  2391. struct cfi_private *cfi = map->fldrv_priv;
  2392. int i;
  2393. struct flchip *chip;
  2394. for (i=0; i<cfi->numchips; i++) {
  2395. chip = &cfi->chips[i];
  2396. mutex_lock(&chip->mutex);
  2397. if (chip->state == FL_PM_SUSPENDED) {
  2398. chip->state = FL_READY;
  2399. map_write(map, CMD(0xF0), chip->start);
  2400. wake_up(&chip->wq);
  2401. }
  2402. else
  2403. printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
  2404. mutex_unlock(&chip->mutex);
  2405. }
  2406. }
  2407. /*
  2408. * Ensure that the flash device is put back into read array mode before
  2409. * unloading the driver or rebooting. On some systems, rebooting while
  2410. * the flash is in query/program/erase mode will prevent the CPU from
  2411. * fetching the bootloader code, requiring a hard reset or power cycle.
  2412. */
  2413. static int cfi_amdstd_reset(struct mtd_info *mtd)
  2414. {
  2415. struct map_info *map = mtd->priv;
  2416. struct cfi_private *cfi = map->fldrv_priv;
  2417. int i, ret;
  2418. struct flchip *chip;
  2419. for (i = 0; i < cfi->numchips; i++) {
  2420. chip = &cfi->chips[i];
  2421. mutex_lock(&chip->mutex);
  2422. ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
  2423. if (!ret) {
  2424. map_write(map, CMD(0xF0), chip->start);
  2425. chip->state = FL_SHUTDOWN;
  2426. put_chip(map, chip, chip->start);
  2427. }
  2428. mutex_unlock(&chip->mutex);
  2429. }
  2430. return 0;
  2431. }
  2432. static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
  2433. void *v)
  2434. {
  2435. struct mtd_info *mtd;
  2436. mtd = container_of(nb, struct mtd_info, reboot_notifier);
  2437. cfi_amdstd_reset(mtd);
  2438. return NOTIFY_DONE;
  2439. }
  2440. static void cfi_amdstd_destroy(struct mtd_info *mtd)
  2441. {
  2442. struct map_info *map = mtd->priv;
  2443. struct cfi_private *cfi = map->fldrv_priv;
  2444. cfi_amdstd_reset(mtd);
  2445. unregister_reboot_notifier(&mtd->reboot_notifier);
  2446. kfree(cfi->cmdset_priv);
  2447. kfree(cfi->cfiq);
  2448. kfree(cfi);
  2449. kfree(mtd->eraseregions);
  2450. }
  2451. MODULE_LICENSE("GPL");
  2452. MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
  2453. MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
  2454. MODULE_ALIAS("cfi_cmdset_0006");
  2455. MODULE_ALIAS("cfi_cmdset_0701");