cfi_cmdset_0002.c 79 KB

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