cfi_cmdset_0002.c 84 KB

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