cfi_cmdset_0002.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114
  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(sizeof(*mtd), GFP_KERNEL);
  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_array(mtd->numeraseregions,
  672. sizeof(struct mtd_erase_region_info),
  673. GFP_KERNEL);
  674. if (!mtd->eraseregions)
  675. goto setup_err;
  676. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  677. unsigned long ernum, ersize;
  678. ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
  679. ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
  680. if (mtd->erasesize < ersize) {
  681. mtd->erasesize = ersize;
  682. }
  683. for (j=0; j<cfi->numchips; j++) {
  684. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
  685. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
  686. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
  687. }
  688. offset += (ersize * ernum);
  689. }
  690. if (offset != devsize) {
  691. /* Argh */
  692. printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
  693. goto setup_err;
  694. }
  695. __module_get(THIS_MODULE);
  696. register_reboot_notifier(&mtd->reboot_notifier);
  697. return mtd;
  698. setup_err:
  699. kfree(mtd->eraseregions);
  700. kfree(mtd);
  701. kfree(cfi->cmdset_priv);
  702. return NULL;
  703. }
  704. /*
  705. * Return true if the chip is ready and has the correct value.
  706. *
  707. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  708. * non-suspended sector) and is indicated by no toggle bits toggling.
  709. *
  710. * Error are indicated by toggling bits or bits held with the wrong value,
  711. * or with bits toggling.
  712. *
  713. * Note that anything more complicated than checking if no bits are toggling
  714. * (including checking DQ5 for an error status) is tricky to get working
  715. * correctly and is therefore not done (particularly with interleaved chips
  716. * as each chip must be checked independently of the others).
  717. */
  718. static int __xipram chip_ready(struct map_info *map, struct flchip *chip,
  719. unsigned long addr, map_word *expected)
  720. {
  721. struct cfi_private *cfi = map->fldrv_priv;
  722. map_word oldd, curd;
  723. int ret;
  724. if (cfi_use_status_reg(cfi)) {
  725. map_word ready = CMD(CFI_SR_DRB);
  726. /*
  727. * For chips that support status register, check device
  728. * ready bit
  729. */
  730. cfi_send_gen_cmd(0x70, cfi->addr_unlock1, chip->start, map, cfi,
  731. cfi->device_type, NULL);
  732. curd = map_read(map, addr);
  733. return map_word_andequal(map, curd, ready, ready);
  734. }
  735. oldd = map_read(map, addr);
  736. curd = map_read(map, addr);
  737. ret = map_word_equal(map, oldd, curd);
  738. if (!ret || !expected)
  739. return ret;
  740. return map_word_equal(map, curd, *expected);
  741. }
  742. static int __xipram chip_good(struct map_info *map, struct flchip *chip,
  743. unsigned long addr, map_word *expected)
  744. {
  745. struct cfi_private *cfi = map->fldrv_priv;
  746. map_word *datum = expected;
  747. if (cfi->quirks & CFI_QUIRK_DQ_TRUE_DATA)
  748. datum = NULL;
  749. return chip_ready(map, chip, addr, datum);
  750. }
  751. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  752. {
  753. DECLARE_WAITQUEUE(wait, current);
  754. struct cfi_private *cfi = map->fldrv_priv;
  755. unsigned long timeo;
  756. struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
  757. resettime:
  758. timeo = jiffies + HZ;
  759. retry:
  760. switch (chip->state) {
  761. case FL_STATUS:
  762. for (;;) {
  763. if (chip_ready(map, chip, adr, NULL))
  764. break;
  765. if (time_after(jiffies, timeo)) {
  766. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  767. return -EIO;
  768. }
  769. mutex_unlock(&chip->mutex);
  770. cfi_udelay(1);
  771. mutex_lock(&chip->mutex);
  772. /* Someone else might have been playing with it. */
  773. goto retry;
  774. }
  775. return 0;
  776. case FL_READY:
  777. case FL_CFI_QUERY:
  778. case FL_JEDEC_QUERY:
  779. return 0;
  780. case FL_ERASING:
  781. if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
  782. !(mode == FL_READY || mode == FL_POINT ||
  783. (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
  784. goto sleep;
  785. /* Do not allow suspend iff read/write to EB address */
  786. if ((adr & chip->in_progress_block_mask) ==
  787. chip->in_progress_block_addr)
  788. goto sleep;
  789. /* Erase suspend */
  790. /* It's harmless to issue the Erase-Suspend and Erase-Resume
  791. * commands when the erase algorithm isn't in progress. */
  792. map_write(map, CMD(0xB0), chip->in_progress_block_addr);
  793. chip->oldstate = FL_ERASING;
  794. chip->state = FL_ERASE_SUSPENDING;
  795. chip->erase_suspended = 1;
  796. for (;;) {
  797. if (chip_ready(map, chip, adr, NULL))
  798. break;
  799. if (time_after(jiffies, timeo)) {
  800. /* Should have suspended the erase by now.
  801. * Send an Erase-Resume command as either
  802. * there was an error (so leave the erase
  803. * routine to recover from it) or we trying to
  804. * use the erase-in-progress sector. */
  805. put_chip(map, chip, adr);
  806. printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
  807. return -EIO;
  808. }
  809. mutex_unlock(&chip->mutex);
  810. cfi_udelay(1);
  811. mutex_lock(&chip->mutex);
  812. /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
  813. So we can just loop here. */
  814. }
  815. chip->state = FL_READY;
  816. return 0;
  817. case FL_XIP_WHILE_ERASING:
  818. if (mode != FL_READY && mode != FL_POINT &&
  819. (!cfip || !(cfip->EraseSuspend&2)))
  820. goto sleep;
  821. chip->oldstate = chip->state;
  822. chip->state = FL_READY;
  823. return 0;
  824. case FL_SHUTDOWN:
  825. /* The machine is rebooting */
  826. return -EIO;
  827. case FL_POINT:
  828. /* Only if there's no operation suspended... */
  829. if (mode == FL_READY && chip->oldstate == FL_READY)
  830. return 0;
  831. fallthrough;
  832. default:
  833. sleep:
  834. set_current_state(TASK_UNINTERRUPTIBLE);
  835. add_wait_queue(&chip->wq, &wait);
  836. mutex_unlock(&chip->mutex);
  837. schedule();
  838. remove_wait_queue(&chip->wq, &wait);
  839. mutex_lock(&chip->mutex);
  840. goto resettime;
  841. }
  842. }
  843. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
  844. {
  845. struct cfi_private *cfi = map->fldrv_priv;
  846. switch(chip->oldstate) {
  847. case FL_ERASING:
  848. cfi_fixup_m29ew_erase_suspend(map,
  849. chip->in_progress_block_addr);
  850. map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
  851. cfi_fixup_m29ew_delay_after_resume(cfi);
  852. chip->oldstate = FL_READY;
  853. chip->state = FL_ERASING;
  854. break;
  855. case FL_XIP_WHILE_ERASING:
  856. chip->state = chip->oldstate;
  857. chip->oldstate = FL_READY;
  858. break;
  859. case FL_READY:
  860. case FL_STATUS:
  861. break;
  862. default:
  863. printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
  864. }
  865. wake_up(&chip->wq);
  866. }
  867. #ifdef CONFIG_MTD_XIP
  868. /*
  869. * No interrupt what so ever can be serviced while the flash isn't in array
  870. * mode. This is ensured by the xip_disable() and xip_enable() functions
  871. * enclosing any code path where the flash is known not to be in array mode.
  872. * And within a XIP disabled code path, only functions marked with __xipram
  873. * may be called and nothing else (it's a good thing to inspect generated
  874. * assembly to make sure inline functions were actually inlined and that gcc
  875. * didn't emit calls to its own support functions). Also configuring MTD CFI
  876. * support to a single buswidth and a single interleave is also recommended.
  877. */
  878. static void xip_disable(struct map_info *map, struct flchip *chip,
  879. unsigned long adr)
  880. {
  881. /* TODO: chips with no XIP use should ignore and return */
  882. (void) map_read(map, adr); /* ensure mmu mapping is up to date */
  883. local_irq_disable();
  884. }
  885. static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
  886. unsigned long adr)
  887. {
  888. struct cfi_private *cfi = map->fldrv_priv;
  889. if (chip->state != FL_POINT && chip->state != FL_READY) {
  890. map_write(map, CMD(0xf0), adr);
  891. chip->state = FL_READY;
  892. }
  893. (void) map_read(map, adr);
  894. xip_iprefetch();
  895. local_irq_enable();
  896. }
  897. /*
  898. * When a delay is required for the flash operation to complete, the
  899. * xip_udelay() function is polling for both the given timeout and pending
  900. * (but still masked) hardware interrupts. Whenever there is an interrupt
  901. * pending then the flash erase operation is suspended, array mode restored
  902. * and interrupts unmasked. Task scheduling might also happen at that
  903. * point. The CPU eventually returns from the interrupt or the call to
  904. * schedule() and the suspended flash operation is resumed for the remaining
  905. * of the delay period.
  906. *
  907. * Warning: this function _will_ fool interrupt latency tracing tools.
  908. */
  909. static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
  910. unsigned long adr, int usec)
  911. {
  912. struct cfi_private *cfi = map->fldrv_priv;
  913. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  914. map_word status, OK = CMD(0x80);
  915. unsigned long suspended, start = xip_currtime();
  916. flstate_t oldstate;
  917. do {
  918. cpu_relax();
  919. if (xip_irqpending() && extp &&
  920. ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
  921. (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
  922. /*
  923. * Let's suspend the erase operation when supported.
  924. * Note that we currently don't try to suspend
  925. * interleaved chips if there is already another
  926. * operation suspended (imagine what happens
  927. * when one chip was already done with the current
  928. * operation while another chip suspended it, then
  929. * we resume the whole thing at once). Yes, it
  930. * can happen!
  931. */
  932. map_write(map, CMD(0xb0), adr);
  933. usec -= xip_elapsed_since(start);
  934. suspended = xip_currtime();
  935. do {
  936. if (xip_elapsed_since(suspended) > 100000) {
  937. /*
  938. * The chip doesn't want to suspend
  939. * after waiting for 100 msecs.
  940. * This is a critical error but there
  941. * is not much we can do here.
  942. */
  943. return;
  944. }
  945. status = map_read(map, adr);
  946. } while (!map_word_andequal(map, status, OK, OK));
  947. /* Suspend succeeded */
  948. oldstate = chip->state;
  949. if (!map_word_bitsset(map, status, CMD(0x40)))
  950. break;
  951. chip->state = FL_XIP_WHILE_ERASING;
  952. chip->erase_suspended = 1;
  953. map_write(map, CMD(0xf0), adr);
  954. (void) map_read(map, adr);
  955. xip_iprefetch();
  956. local_irq_enable();
  957. mutex_unlock(&chip->mutex);
  958. xip_iprefetch();
  959. cond_resched();
  960. /*
  961. * We're back. However someone else might have
  962. * decided to go write to the chip if we are in
  963. * a suspended erase state. If so let's wait
  964. * until it's done.
  965. */
  966. mutex_lock(&chip->mutex);
  967. while (chip->state != FL_XIP_WHILE_ERASING) {
  968. DECLARE_WAITQUEUE(wait, current);
  969. set_current_state(TASK_UNINTERRUPTIBLE);
  970. add_wait_queue(&chip->wq, &wait);
  971. mutex_unlock(&chip->mutex);
  972. schedule();
  973. remove_wait_queue(&chip->wq, &wait);
  974. mutex_lock(&chip->mutex);
  975. }
  976. /* Disallow XIP again */
  977. local_irq_disable();
  978. /* Correct Erase Suspend Hangups for M29EW */
  979. cfi_fixup_m29ew_erase_suspend(map, adr);
  980. /* Resume the write or erase operation */
  981. map_write(map, cfi->sector_erase_cmd, adr);
  982. chip->state = oldstate;
  983. start = xip_currtime();
  984. } else if (usec >= 1000000/HZ) {
  985. /*
  986. * Try to save on CPU power when waiting delay
  987. * is at least a system timer tick period.
  988. * No need to be extremely accurate here.
  989. */
  990. xip_cpu_idle();
  991. }
  992. status = map_read(map, adr);
  993. } while (!map_word_andequal(map, status, OK, OK)
  994. && xip_elapsed_since(start) < usec);
  995. }
  996. #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
  997. /*
  998. * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
  999. * the flash is actively programming or erasing since we have to poll for
  1000. * the operation to complete anyway. We can't do that in a generic way with
  1001. * a XIP setup so do it before the actual flash operation in this case
  1002. * and stub it out from INVALIDATE_CACHE_UDELAY.
  1003. */
  1004. #define XIP_INVAL_CACHED_RANGE(map, from, size) \
  1005. INVALIDATE_CACHED_RANGE(map, from, size)
  1006. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  1007. UDELAY(map, chip, adr, usec)
  1008. /*
  1009. * Extra notes:
  1010. *
  1011. * Activating this XIP support changes the way the code works a bit. For
  1012. * example the code to suspend the current process when concurrent access
  1013. * happens is never executed because xip_udelay() will always return with the
  1014. * same chip state as it was entered with. This is why there is no care for
  1015. * the presence of add_wait_queue() or schedule() calls from within a couple
  1016. * xip_disable()'d areas of code, like in do_erase_oneblock for example.
  1017. * The queueing and scheduling are always happening within xip_udelay().
  1018. *
  1019. * Similarly, get_chip() and put_chip() just happen to always be executed
  1020. * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
  1021. * is in array mode, therefore never executing many cases therein and not
  1022. * causing any problem with XIP.
  1023. */
  1024. #else
  1025. #define xip_disable(map, chip, adr)
  1026. #define xip_enable(map, chip, adr)
  1027. #define XIP_INVAL_CACHED_RANGE(x...)
  1028. #define UDELAY(map, chip, adr, usec) \
  1029. do { \
  1030. mutex_unlock(&chip->mutex); \
  1031. cfi_udelay(usec); \
  1032. mutex_lock(&chip->mutex); \
  1033. } while (0)
  1034. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  1035. do { \
  1036. mutex_unlock(&chip->mutex); \
  1037. INVALIDATE_CACHED_RANGE(map, adr, len); \
  1038. cfi_udelay(usec); \
  1039. mutex_lock(&chip->mutex); \
  1040. } while (0)
  1041. #endif
  1042. static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  1043. {
  1044. unsigned long cmd_addr;
  1045. struct cfi_private *cfi = map->fldrv_priv;
  1046. int ret;
  1047. adr += chip->start;
  1048. /* Ensure cmd read/writes are aligned. */
  1049. cmd_addr = adr & ~(map_bankwidth(map)-1);
  1050. mutex_lock(&chip->mutex);
  1051. ret = get_chip(map, chip, cmd_addr, FL_READY);
  1052. if (ret) {
  1053. mutex_unlock(&chip->mutex);
  1054. return ret;
  1055. }
  1056. if (chip->state != FL_POINT && chip->state != FL_READY) {
  1057. map_write(map, CMD(0xf0), cmd_addr);
  1058. chip->state = FL_READY;
  1059. }
  1060. map_copy_from(map, buf, adr, len);
  1061. put_chip(map, chip, cmd_addr);
  1062. mutex_unlock(&chip->mutex);
  1063. return 0;
  1064. }
  1065. static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  1066. {
  1067. struct map_info *map = mtd->priv;
  1068. struct cfi_private *cfi = map->fldrv_priv;
  1069. unsigned long ofs;
  1070. int chipnum;
  1071. int ret = 0;
  1072. /* ofs: offset within the first chip that the first read should start */
  1073. chipnum = (from >> cfi->chipshift);
  1074. ofs = from - (chipnum << cfi->chipshift);
  1075. while (len) {
  1076. unsigned long thislen;
  1077. if (chipnum >= cfi->numchips)
  1078. break;
  1079. if ((len + ofs -1) >> cfi->chipshift)
  1080. thislen = (1<<cfi->chipshift) - ofs;
  1081. else
  1082. thislen = len;
  1083. ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  1084. if (ret)
  1085. break;
  1086. *retlen += thislen;
  1087. len -= thislen;
  1088. buf += thislen;
  1089. ofs = 0;
  1090. chipnum++;
  1091. }
  1092. return ret;
  1093. }
  1094. typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
  1095. loff_t adr, size_t len, u_char *buf, size_t grouplen);
  1096. static inline void otp_enter(struct map_info *map, struct flchip *chip,
  1097. loff_t adr, size_t len)
  1098. {
  1099. struct cfi_private *cfi = map->fldrv_priv;
  1100. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1101. cfi->device_type, NULL);
  1102. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1103. cfi->device_type, NULL);
  1104. cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi,
  1105. cfi->device_type, NULL);
  1106. INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
  1107. }
  1108. static inline void otp_exit(struct map_info *map, struct flchip *chip,
  1109. loff_t adr, size_t len)
  1110. {
  1111. struct cfi_private *cfi = map->fldrv_priv;
  1112. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1113. cfi->device_type, NULL);
  1114. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1115. cfi->device_type, NULL);
  1116. cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi,
  1117. cfi->device_type, NULL);
  1118. cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi,
  1119. cfi->device_type, NULL);
  1120. INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
  1121. }
  1122. static inline int do_read_secsi_onechip(struct map_info *map,
  1123. struct flchip *chip, loff_t adr,
  1124. size_t len, u_char *buf,
  1125. size_t grouplen)
  1126. {
  1127. DECLARE_WAITQUEUE(wait, current);
  1128. retry:
  1129. mutex_lock(&chip->mutex);
  1130. if (chip->state != FL_READY){
  1131. set_current_state(TASK_UNINTERRUPTIBLE);
  1132. add_wait_queue(&chip->wq, &wait);
  1133. mutex_unlock(&chip->mutex);
  1134. schedule();
  1135. remove_wait_queue(&chip->wq, &wait);
  1136. goto retry;
  1137. }
  1138. adr += chip->start;
  1139. chip->state = FL_READY;
  1140. otp_enter(map, chip, adr, len);
  1141. map_copy_from(map, buf, adr, len);
  1142. otp_exit(map, chip, adr, len);
  1143. wake_up(&chip->wq);
  1144. mutex_unlock(&chip->mutex);
  1145. return 0;
  1146. }
  1147. static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  1148. {
  1149. struct map_info *map = mtd->priv;
  1150. struct cfi_private *cfi = map->fldrv_priv;
  1151. unsigned long ofs;
  1152. int chipnum;
  1153. int ret = 0;
  1154. /* ofs: offset within the first chip that the first read should start */
  1155. /* 8 secsi bytes per chip */
  1156. chipnum=from>>3;
  1157. ofs=from & 7;
  1158. while (len) {
  1159. unsigned long thislen;
  1160. if (chipnum >= cfi->numchips)
  1161. break;
  1162. if ((len + ofs -1) >> 3)
  1163. thislen = (1<<3) - ofs;
  1164. else
  1165. thislen = len;
  1166. ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs,
  1167. thislen, buf, 0);
  1168. if (ret)
  1169. break;
  1170. *retlen += thislen;
  1171. len -= thislen;
  1172. buf += thislen;
  1173. ofs = 0;
  1174. chipnum++;
  1175. }
  1176. return ret;
  1177. }
  1178. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
  1179. unsigned long adr, map_word datum,
  1180. int mode);
  1181. static int do_otp_write(struct map_info *map, struct flchip *chip, loff_t adr,
  1182. size_t len, u_char *buf, size_t grouplen)
  1183. {
  1184. int ret;
  1185. while (len) {
  1186. unsigned long bus_ofs = adr & ~(map_bankwidth(map)-1);
  1187. int gap = adr - bus_ofs;
  1188. int n = min_t(int, len, map_bankwidth(map) - gap);
  1189. map_word datum = map_word_ff(map);
  1190. if (n != map_bankwidth(map)) {
  1191. /* partial write of a word, load old contents */
  1192. otp_enter(map, chip, bus_ofs, map_bankwidth(map));
  1193. datum = map_read(map, bus_ofs);
  1194. otp_exit(map, chip, bus_ofs, map_bankwidth(map));
  1195. }
  1196. datum = map_word_load_partial(map, datum, buf, gap, n);
  1197. ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
  1198. if (ret)
  1199. return ret;
  1200. adr += n;
  1201. buf += n;
  1202. len -= n;
  1203. }
  1204. return 0;
  1205. }
  1206. static int do_otp_lock(struct map_info *map, struct flchip *chip, loff_t adr,
  1207. size_t len, u_char *buf, size_t grouplen)
  1208. {
  1209. struct cfi_private *cfi = map->fldrv_priv;
  1210. uint8_t lockreg;
  1211. unsigned long timeo;
  1212. int ret;
  1213. /* make sure area matches group boundaries */
  1214. if ((adr != 0) || (len != grouplen))
  1215. return -EINVAL;
  1216. mutex_lock(&chip->mutex);
  1217. ret = get_chip(map, chip, chip->start, FL_LOCKING);
  1218. if (ret) {
  1219. mutex_unlock(&chip->mutex);
  1220. return ret;
  1221. }
  1222. chip->state = FL_LOCKING;
  1223. /* Enter lock register command */
  1224. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1225. cfi->device_type, NULL);
  1226. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1227. cfi->device_type, NULL);
  1228. cfi_send_gen_cmd(0x40, cfi->addr_unlock1, chip->start, map, cfi,
  1229. cfi->device_type, NULL);
  1230. /* read lock register */
  1231. lockreg = cfi_read_query(map, 0);
  1232. /* set bit 0 to protect extended memory block */
  1233. lockreg &= ~0x01;
  1234. /* set bit 0 to protect extended memory block */
  1235. /* write lock register */
  1236. map_write(map, CMD(0xA0), chip->start);
  1237. map_write(map, CMD(lockreg), chip->start);
  1238. /* wait for chip to become ready */
  1239. timeo = jiffies + msecs_to_jiffies(2);
  1240. for (;;) {
  1241. if (chip_ready(map, chip, adr, NULL))
  1242. break;
  1243. if (time_after(jiffies, timeo)) {
  1244. pr_err("Waiting for chip to be ready timed out.\n");
  1245. ret = -EIO;
  1246. break;
  1247. }
  1248. UDELAY(map, chip, 0, 1);
  1249. }
  1250. /* exit protection commands */
  1251. map_write(map, CMD(0x90), chip->start);
  1252. map_write(map, CMD(0x00), chip->start);
  1253. chip->state = FL_READY;
  1254. put_chip(map, chip, chip->start);
  1255. mutex_unlock(&chip->mutex);
  1256. return ret;
  1257. }
  1258. static int cfi_amdstd_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1259. size_t *retlen, u_char *buf,
  1260. otp_op_t action, int user_regs)
  1261. {
  1262. struct map_info *map = mtd->priv;
  1263. struct cfi_private *cfi = map->fldrv_priv;
  1264. int ofs_factor = cfi->interleave * cfi->device_type;
  1265. unsigned long base;
  1266. int chipnum;
  1267. struct flchip *chip;
  1268. uint8_t otp, lockreg;
  1269. int ret;
  1270. size_t user_size, factory_size, otpsize;
  1271. loff_t user_offset, factory_offset, otpoffset;
  1272. int user_locked = 0, otplocked;
  1273. *retlen = 0;
  1274. for (chipnum = 0; chipnum < cfi->numchips; chipnum++) {
  1275. chip = &cfi->chips[chipnum];
  1276. factory_size = 0;
  1277. user_size = 0;
  1278. /* Micron M29EW family */
  1279. if (is_m29ew(cfi)) {
  1280. base = chip->start;
  1281. /* check whether secsi area is factory locked
  1282. or user lockable */
  1283. mutex_lock(&chip->mutex);
  1284. ret = get_chip(map, chip, base, FL_CFI_QUERY);
  1285. if (ret) {
  1286. mutex_unlock(&chip->mutex);
  1287. return ret;
  1288. }
  1289. cfi_qry_mode_on(base, map, cfi);
  1290. otp = cfi_read_query(map, base + 0x3 * ofs_factor);
  1291. cfi_qry_mode_off(base, map, cfi);
  1292. put_chip(map, chip, base);
  1293. mutex_unlock(&chip->mutex);
  1294. if (otp & 0x80) {
  1295. /* factory locked */
  1296. factory_offset = 0;
  1297. factory_size = 0x100;
  1298. } else {
  1299. /* customer lockable */
  1300. user_offset = 0;
  1301. user_size = 0x100;
  1302. mutex_lock(&chip->mutex);
  1303. ret = get_chip(map, chip, base, FL_LOCKING);
  1304. if (ret) {
  1305. mutex_unlock(&chip->mutex);
  1306. return ret;
  1307. }
  1308. /* Enter lock register command */
  1309. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1,
  1310. chip->start, map, cfi,
  1311. cfi->device_type, NULL);
  1312. cfi_send_gen_cmd(0x55, cfi->addr_unlock2,
  1313. chip->start, map, cfi,
  1314. cfi->device_type, NULL);
  1315. cfi_send_gen_cmd(0x40, cfi->addr_unlock1,
  1316. chip->start, map, cfi,
  1317. cfi->device_type, NULL);
  1318. /* read lock register */
  1319. lockreg = cfi_read_query(map, 0);
  1320. /* exit protection commands */
  1321. map_write(map, CMD(0x90), chip->start);
  1322. map_write(map, CMD(0x00), chip->start);
  1323. put_chip(map, chip, chip->start);
  1324. mutex_unlock(&chip->mutex);
  1325. user_locked = ((lockreg & 0x01) == 0x00);
  1326. }
  1327. }
  1328. otpsize = user_regs ? user_size : factory_size;
  1329. if (!otpsize)
  1330. continue;
  1331. otpoffset = user_regs ? user_offset : factory_offset;
  1332. otplocked = user_regs ? user_locked : 1;
  1333. if (!action) {
  1334. /* return otpinfo */
  1335. struct otp_info *otpinfo;
  1336. len -= sizeof(*otpinfo);
  1337. if (len <= 0)
  1338. return -ENOSPC;
  1339. otpinfo = (struct otp_info *)buf;
  1340. otpinfo->start = from;
  1341. otpinfo->length = otpsize;
  1342. otpinfo->locked = otplocked;
  1343. buf += sizeof(*otpinfo);
  1344. *retlen += sizeof(*otpinfo);
  1345. from += otpsize;
  1346. } else if ((from < otpsize) && (len > 0)) {
  1347. size_t size;
  1348. size = (len < otpsize - from) ? len : otpsize - from;
  1349. ret = action(map, chip, otpoffset + from, size, buf,
  1350. otpsize);
  1351. if (ret < 0)
  1352. return ret;
  1353. buf += size;
  1354. len -= size;
  1355. *retlen += size;
  1356. from = 0;
  1357. } else {
  1358. from -= otpsize;
  1359. }
  1360. }
  1361. return 0;
  1362. }
  1363. static int cfi_amdstd_get_fact_prot_info(struct mtd_info *mtd, size_t len,
  1364. size_t *retlen, struct otp_info *buf)
  1365. {
  1366. return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
  1367. NULL, 0);
  1368. }
  1369. static int cfi_amdstd_get_user_prot_info(struct mtd_info *mtd, size_t len,
  1370. size_t *retlen, struct otp_info *buf)
  1371. {
  1372. return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
  1373. NULL, 1);
  1374. }
  1375. static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1376. size_t len, size_t *retlen,
  1377. u_char *buf)
  1378. {
  1379. return cfi_amdstd_otp_walk(mtd, from, len, retlen,
  1380. buf, do_read_secsi_onechip, 0);
  1381. }
  1382. static int cfi_amdstd_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1383. size_t len, size_t *retlen,
  1384. u_char *buf)
  1385. {
  1386. return cfi_amdstd_otp_walk(mtd, from, len, retlen,
  1387. buf, do_read_secsi_onechip, 1);
  1388. }
  1389. static int cfi_amdstd_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1390. size_t len, size_t *retlen,
  1391. const u_char *buf)
  1392. {
  1393. return cfi_amdstd_otp_walk(mtd, from, len, retlen, (u_char *)buf,
  1394. do_otp_write, 1);
  1395. }
  1396. static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1397. size_t len)
  1398. {
  1399. size_t retlen;
  1400. return cfi_amdstd_otp_walk(mtd, from, len, &retlen, NULL,
  1401. do_otp_lock, 1);
  1402. }
  1403. static int __xipram do_write_oneword_once(struct map_info *map,
  1404. struct flchip *chip,
  1405. unsigned long adr, map_word datum,
  1406. int mode, struct cfi_private *cfi)
  1407. {
  1408. unsigned long timeo;
  1409. /*
  1410. * We use a 1ms + 1 jiffies generic timeout for writes (most devices
  1411. * have a max write time of a few hundreds usec). However, we should
  1412. * use the maximum timeout value given by the chip at probe time
  1413. * instead. Unfortunately, struct flchip does have a field for
  1414. * maximum timeout, only for typical which can be far too short
  1415. * depending of the conditions. The ' + 1' is to avoid having a
  1416. * timeout of 0 jiffies if HZ is smaller than 1000.
  1417. */
  1418. unsigned long uWriteTimeout = (HZ / 1000) + 1;
  1419. int ret = 0;
  1420. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1421. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1422. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1423. map_write(map, datum, adr);
  1424. chip->state = mode;
  1425. INVALIDATE_CACHE_UDELAY(map, chip,
  1426. adr, map_bankwidth(map),
  1427. chip->word_write_time);
  1428. /* See comment above for timeout value. */
  1429. timeo = jiffies + uWriteTimeout;
  1430. for (;;) {
  1431. if (chip->state != mode) {
  1432. /* Someone's suspended the write. Sleep */
  1433. DECLARE_WAITQUEUE(wait, current);
  1434. set_current_state(TASK_UNINTERRUPTIBLE);
  1435. add_wait_queue(&chip->wq, &wait);
  1436. mutex_unlock(&chip->mutex);
  1437. schedule();
  1438. remove_wait_queue(&chip->wq, &wait);
  1439. timeo = jiffies + (HZ / 2); /* FIXME */
  1440. mutex_lock(&chip->mutex);
  1441. continue;
  1442. }
  1443. /*
  1444. * We check "time_after" and "!chip_good" before checking
  1445. * "chip_good" to avoid the failure due to scheduling.
  1446. */
  1447. if (time_after(jiffies, timeo) &&
  1448. !chip_good(map, chip, adr, &datum)) {
  1449. xip_enable(map, chip, adr);
  1450. printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
  1451. xip_disable(map, chip, adr);
  1452. ret = -EIO;
  1453. break;
  1454. }
  1455. if (chip_good(map, chip, adr, &datum)) {
  1456. if (cfi_check_err_status(map, chip, adr))
  1457. ret = -EIO;
  1458. break;
  1459. }
  1460. /* Latency issues. Drop the lock, wait a while and retry */
  1461. UDELAY(map, chip, adr, 1);
  1462. }
  1463. return ret;
  1464. }
  1465. static int __xipram do_write_oneword_start(struct map_info *map,
  1466. struct flchip *chip,
  1467. unsigned long adr, int mode)
  1468. {
  1469. int ret;
  1470. mutex_lock(&chip->mutex);
  1471. ret = get_chip(map, chip, adr, mode);
  1472. if (ret) {
  1473. mutex_unlock(&chip->mutex);
  1474. return ret;
  1475. }
  1476. if (mode == FL_OTP_WRITE)
  1477. otp_enter(map, chip, adr, map_bankwidth(map));
  1478. return ret;
  1479. }
  1480. static void __xipram do_write_oneword_done(struct map_info *map,
  1481. struct flchip *chip,
  1482. unsigned long adr, int mode)
  1483. {
  1484. if (mode == FL_OTP_WRITE)
  1485. otp_exit(map, chip, adr, map_bankwidth(map));
  1486. chip->state = FL_READY;
  1487. DISABLE_VPP(map);
  1488. put_chip(map, chip, adr);
  1489. mutex_unlock(&chip->mutex);
  1490. }
  1491. static int __xipram do_write_oneword_retry(struct map_info *map,
  1492. struct flchip *chip,
  1493. unsigned long adr, map_word datum,
  1494. int mode)
  1495. {
  1496. struct cfi_private *cfi = map->fldrv_priv;
  1497. int ret = 0;
  1498. map_word oldd;
  1499. int retry_cnt = 0;
  1500. /*
  1501. * Check for a NOP for the case when the datum to write is already
  1502. * present - it saves time and works around buggy chips that corrupt
  1503. * data at other locations when 0xff is written to a location that
  1504. * already contains 0xff.
  1505. */
  1506. oldd = map_read(map, adr);
  1507. if (map_word_equal(map, oldd, datum)) {
  1508. pr_debug("MTD %s(): NOP\n", __func__);
  1509. return ret;
  1510. }
  1511. XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
  1512. ENABLE_VPP(map);
  1513. xip_disable(map, chip, adr);
  1514. retry:
  1515. ret = do_write_oneword_once(map, chip, adr, datum, mode, cfi);
  1516. if (ret) {
  1517. /* reset on all failures. */
  1518. map_write(map, CMD(0xF0), chip->start);
  1519. /* FIXME - should have reset delay before continuing */
  1520. if (++retry_cnt <= MAX_RETRIES) {
  1521. ret = 0;
  1522. goto retry;
  1523. }
  1524. }
  1525. xip_enable(map, chip, adr);
  1526. return ret;
  1527. }
  1528. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
  1529. unsigned long adr, map_word datum,
  1530. int mode)
  1531. {
  1532. int ret;
  1533. adr += chip->start;
  1534. pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n", __func__, adr,
  1535. datum.x[0]);
  1536. ret = do_write_oneword_start(map, chip, adr, mode);
  1537. if (ret)
  1538. return ret;
  1539. ret = do_write_oneword_retry(map, chip, adr, datum, mode);
  1540. do_write_oneword_done(map, chip, adr, mode);
  1541. return ret;
  1542. }
  1543. static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
  1544. size_t *retlen, const u_char *buf)
  1545. {
  1546. struct map_info *map = mtd->priv;
  1547. struct cfi_private *cfi = map->fldrv_priv;
  1548. int ret;
  1549. int chipnum;
  1550. unsigned long ofs, chipstart;
  1551. DECLARE_WAITQUEUE(wait, current);
  1552. chipnum = to >> cfi->chipshift;
  1553. ofs = to - (chipnum << cfi->chipshift);
  1554. chipstart = cfi->chips[chipnum].start;
  1555. /* If it's not bus-aligned, do the first byte write */
  1556. if (ofs & (map_bankwidth(map)-1)) {
  1557. unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
  1558. int i = ofs - bus_ofs;
  1559. int n = 0;
  1560. map_word tmp_buf;
  1561. retry:
  1562. mutex_lock(&cfi->chips[chipnum].mutex);
  1563. if (cfi->chips[chipnum].state != FL_READY) {
  1564. set_current_state(TASK_UNINTERRUPTIBLE);
  1565. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1566. mutex_unlock(&cfi->chips[chipnum].mutex);
  1567. schedule();
  1568. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1569. goto retry;
  1570. }
  1571. /* Load 'tmp_buf' with old contents of flash */
  1572. tmp_buf = map_read(map, bus_ofs+chipstart);
  1573. mutex_unlock(&cfi->chips[chipnum].mutex);
  1574. /* Number of bytes to copy from buffer */
  1575. n = min_t(int, len, map_bankwidth(map)-i);
  1576. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1577. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1578. bus_ofs, tmp_buf, FL_WRITING);
  1579. if (ret)
  1580. return ret;
  1581. ofs += n;
  1582. buf += n;
  1583. (*retlen) += n;
  1584. len -= n;
  1585. if (ofs >> cfi->chipshift) {
  1586. chipnum ++;
  1587. ofs = 0;
  1588. if (chipnum == cfi->numchips)
  1589. return 0;
  1590. }
  1591. }
  1592. /* We are now aligned, write as much as possible */
  1593. while(len >= map_bankwidth(map)) {
  1594. map_word datum;
  1595. datum = map_word_load(map, buf);
  1596. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1597. ofs, datum, FL_WRITING);
  1598. if (ret)
  1599. return ret;
  1600. ofs += map_bankwidth(map);
  1601. buf += map_bankwidth(map);
  1602. (*retlen) += map_bankwidth(map);
  1603. len -= map_bankwidth(map);
  1604. if (ofs >> cfi->chipshift) {
  1605. chipnum ++;
  1606. ofs = 0;
  1607. if (chipnum == cfi->numchips)
  1608. return 0;
  1609. chipstart = cfi->chips[chipnum].start;
  1610. }
  1611. }
  1612. /* Write the trailing bytes if any */
  1613. if (len & (map_bankwidth(map)-1)) {
  1614. map_word tmp_buf;
  1615. retry1:
  1616. mutex_lock(&cfi->chips[chipnum].mutex);
  1617. if (cfi->chips[chipnum].state != FL_READY) {
  1618. set_current_state(TASK_UNINTERRUPTIBLE);
  1619. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1620. mutex_unlock(&cfi->chips[chipnum].mutex);
  1621. schedule();
  1622. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1623. goto retry1;
  1624. }
  1625. tmp_buf = map_read(map, ofs + chipstart);
  1626. mutex_unlock(&cfi->chips[chipnum].mutex);
  1627. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1628. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1629. ofs, tmp_buf, FL_WRITING);
  1630. if (ret)
  1631. return ret;
  1632. (*retlen) += len;
  1633. }
  1634. return 0;
  1635. }
  1636. #if !FORCE_WORD_WRITE
  1637. static int __xipram do_write_buffer_wait(struct map_info *map,
  1638. struct flchip *chip, unsigned long adr,
  1639. map_word datum)
  1640. {
  1641. unsigned long timeo;
  1642. unsigned long u_write_timeout;
  1643. int ret = 0;
  1644. /*
  1645. * Timeout is calculated according to CFI data, if available.
  1646. * See more comments in cfi_cmdset_0002().
  1647. */
  1648. u_write_timeout = usecs_to_jiffies(chip->buffer_write_time_max);
  1649. timeo = jiffies + u_write_timeout;
  1650. for (;;) {
  1651. if (chip->state != FL_WRITING) {
  1652. /* Someone's suspended the write. Sleep */
  1653. DECLARE_WAITQUEUE(wait, current);
  1654. set_current_state(TASK_UNINTERRUPTIBLE);
  1655. add_wait_queue(&chip->wq, &wait);
  1656. mutex_unlock(&chip->mutex);
  1657. schedule();
  1658. remove_wait_queue(&chip->wq, &wait);
  1659. timeo = jiffies + (HZ / 2); /* FIXME */
  1660. mutex_lock(&chip->mutex);
  1661. continue;
  1662. }
  1663. /*
  1664. * We check "time_after" and "!chip_good" before checking
  1665. * "chip_good" to avoid the failure due to scheduling.
  1666. */
  1667. if (time_after(jiffies, timeo) &&
  1668. !chip_good(map, chip, adr, &datum)) {
  1669. pr_err("MTD %s(): software timeout, address:0x%.8lx.\n",
  1670. __func__, adr);
  1671. ret = -EIO;
  1672. break;
  1673. }
  1674. if (chip_good(map, chip, adr, &datum)) {
  1675. if (cfi_check_err_status(map, chip, adr))
  1676. ret = -EIO;
  1677. break;
  1678. }
  1679. /* Latency issues. Drop the lock, wait a while and retry */
  1680. UDELAY(map, chip, adr, 1);
  1681. }
  1682. return ret;
  1683. }
  1684. static void __xipram do_write_buffer_reset(struct map_info *map,
  1685. struct flchip *chip,
  1686. struct cfi_private *cfi)
  1687. {
  1688. /*
  1689. * Recovery from write-buffer programming failures requires
  1690. * the write-to-buffer-reset sequence. Since the last part
  1691. * of the sequence also works as a normal reset, we can run
  1692. * the same commands regardless of why we are here.
  1693. * See e.g.
  1694. * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf
  1695. */
  1696. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1697. cfi->device_type, NULL);
  1698. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1699. cfi->device_type, NULL);
  1700. cfi_send_gen_cmd(0xF0, cfi->addr_unlock1, chip->start, map, cfi,
  1701. cfi->device_type, NULL);
  1702. /* FIXME - should have reset delay before continuing */
  1703. }
  1704. /*
  1705. * FIXME: interleaved mode not tested, and probably not supported!
  1706. */
  1707. static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
  1708. unsigned long adr, const u_char *buf,
  1709. int len)
  1710. {
  1711. struct cfi_private *cfi = map->fldrv_priv;
  1712. int ret;
  1713. unsigned long cmd_adr;
  1714. int z, words;
  1715. map_word datum;
  1716. adr += chip->start;
  1717. cmd_adr = adr;
  1718. mutex_lock(&chip->mutex);
  1719. ret = get_chip(map, chip, adr, FL_WRITING);
  1720. if (ret) {
  1721. mutex_unlock(&chip->mutex);
  1722. return ret;
  1723. }
  1724. datum = map_word_load(map, buf);
  1725. pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1726. __func__, adr, datum.x[0]);
  1727. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1728. ENABLE_VPP(map);
  1729. xip_disable(map, chip, cmd_adr);
  1730. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1731. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1732. /* Write Buffer Load */
  1733. map_write(map, CMD(0x25), cmd_adr);
  1734. chip->state = FL_WRITING_TO_BUFFER;
  1735. /* Write length of data to come */
  1736. words = len / map_bankwidth(map);
  1737. map_write(map, CMD(words - 1), cmd_adr);
  1738. /* Write data */
  1739. z = 0;
  1740. while(z < words * map_bankwidth(map)) {
  1741. datum = map_word_load(map, buf);
  1742. map_write(map, datum, adr + z);
  1743. z += map_bankwidth(map);
  1744. buf += map_bankwidth(map);
  1745. }
  1746. z -= map_bankwidth(map);
  1747. adr += z;
  1748. /* Write Buffer Program Confirm: GO GO GO */
  1749. map_write(map, CMD(0x29), cmd_adr);
  1750. chip->state = FL_WRITING;
  1751. INVALIDATE_CACHE_UDELAY(map, chip,
  1752. adr, map_bankwidth(map),
  1753. chip->word_write_time);
  1754. ret = do_write_buffer_wait(map, chip, adr, datum);
  1755. if (ret)
  1756. do_write_buffer_reset(map, chip, cfi);
  1757. xip_enable(map, chip, adr);
  1758. chip->state = FL_READY;
  1759. DISABLE_VPP(map);
  1760. put_chip(map, chip, adr);
  1761. mutex_unlock(&chip->mutex);
  1762. return ret;
  1763. }
  1764. static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
  1765. size_t *retlen, const u_char *buf)
  1766. {
  1767. struct map_info *map = mtd->priv;
  1768. struct cfi_private *cfi = map->fldrv_priv;
  1769. int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1770. int ret;
  1771. int chipnum;
  1772. unsigned long ofs;
  1773. chipnum = to >> cfi->chipshift;
  1774. ofs = to - (chipnum << cfi->chipshift);
  1775. /* If it's not bus-aligned, do the first word write */
  1776. if (ofs & (map_bankwidth(map)-1)) {
  1777. size_t local_len = (-ofs)&(map_bankwidth(map)-1);
  1778. if (local_len > len)
  1779. local_len = len;
  1780. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1781. local_len, retlen, buf);
  1782. if (ret)
  1783. return ret;
  1784. ofs += local_len;
  1785. buf += local_len;
  1786. len -= local_len;
  1787. if (ofs >> cfi->chipshift) {
  1788. chipnum ++;
  1789. ofs = 0;
  1790. if (chipnum == cfi->numchips)
  1791. return 0;
  1792. }
  1793. }
  1794. /* Write buffer is worth it only if more than one word to write... */
  1795. while (len >= map_bankwidth(map) * 2) {
  1796. /* We must not cross write block boundaries */
  1797. int size = wbufsize - (ofs & (wbufsize-1));
  1798. if (size > len)
  1799. size = len;
  1800. if (size % map_bankwidth(map))
  1801. size -= size % map_bankwidth(map);
  1802. ret = do_write_buffer(map, &cfi->chips[chipnum],
  1803. ofs, buf, size);
  1804. if (ret)
  1805. return ret;
  1806. ofs += size;
  1807. buf += size;
  1808. (*retlen) += size;
  1809. len -= size;
  1810. if (ofs >> cfi->chipshift) {
  1811. chipnum ++;
  1812. ofs = 0;
  1813. if (chipnum == cfi->numchips)
  1814. return 0;
  1815. }
  1816. }
  1817. if (len) {
  1818. size_t retlen_dregs = 0;
  1819. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1820. len, &retlen_dregs, buf);
  1821. *retlen += retlen_dregs;
  1822. return ret;
  1823. }
  1824. return 0;
  1825. }
  1826. #endif /* !FORCE_WORD_WRITE */
  1827. /*
  1828. * Wait for the flash chip to become ready to write data
  1829. *
  1830. * This is only called during the panic_write() path. When panic_write()
  1831. * is called, the kernel is in the process of a panic, and will soon be
  1832. * dead. Therefore we don't take any locks, and attempt to get access
  1833. * to the chip as soon as possible.
  1834. */
  1835. static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
  1836. unsigned long adr)
  1837. {
  1838. struct cfi_private *cfi = map->fldrv_priv;
  1839. int retries = 10;
  1840. int i;
  1841. /*
  1842. * If the driver thinks the chip is idle, and no toggle bits
  1843. * are changing, then the chip is actually idle for sure.
  1844. */
  1845. if (chip->state == FL_READY && chip_ready(map, chip, adr, NULL))
  1846. return 0;
  1847. /*
  1848. * Try several times to reset the chip and then wait for it
  1849. * to become idle. The upper limit of a few milliseconds of
  1850. * delay isn't a big problem: the kernel is dying anyway. It
  1851. * is more important to save the messages.
  1852. */
  1853. while (retries > 0) {
  1854. const unsigned long timeo = (HZ / 1000) + 1;
  1855. /* send the reset command */
  1856. map_write(map, CMD(0xF0), chip->start);
  1857. /* wait for the chip to become ready */
  1858. for (i = 0; i < jiffies_to_usecs(timeo); i++) {
  1859. if (chip_ready(map, chip, adr, NULL))
  1860. return 0;
  1861. udelay(1);
  1862. }
  1863. retries--;
  1864. }
  1865. /* the chip never became ready */
  1866. return -EBUSY;
  1867. }
  1868. /*
  1869. * Write out one word of data to a single flash chip during a kernel panic
  1870. *
  1871. * This is only called during the panic_write() path. When panic_write()
  1872. * is called, the kernel is in the process of a panic, and will soon be
  1873. * dead. Therefore we don't take any locks, and attempt to get access
  1874. * to the chip as soon as possible.
  1875. *
  1876. * The implementation of this routine is intentionally similar to
  1877. * do_write_oneword(), in order to ease code maintenance.
  1878. */
  1879. static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
  1880. unsigned long adr, map_word datum)
  1881. {
  1882. const unsigned long uWriteTimeout = (HZ / 1000) + 1;
  1883. struct cfi_private *cfi = map->fldrv_priv;
  1884. int retry_cnt = 0;
  1885. map_word oldd;
  1886. int ret;
  1887. int i;
  1888. adr += chip->start;
  1889. ret = cfi_amdstd_panic_wait(map, chip, adr);
  1890. if (ret)
  1891. return ret;
  1892. pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
  1893. __func__, adr, datum.x[0]);
  1894. /*
  1895. * Check for a NOP for the case when the datum to write is already
  1896. * present - it saves time and works around buggy chips that corrupt
  1897. * data at other locations when 0xff is written to a location that
  1898. * already contains 0xff.
  1899. */
  1900. oldd = map_read(map, adr);
  1901. if (map_word_equal(map, oldd, datum)) {
  1902. pr_debug("MTD %s(): NOP\n", __func__);
  1903. goto op_done;
  1904. }
  1905. ENABLE_VPP(map);
  1906. retry:
  1907. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1908. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1909. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1910. map_write(map, datum, adr);
  1911. for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
  1912. if (chip_ready(map, chip, adr, NULL))
  1913. break;
  1914. udelay(1);
  1915. }
  1916. if (!chip_ready(map, chip, adr, &datum) ||
  1917. cfi_check_err_status(map, chip, adr)) {
  1918. /* reset on all failures. */
  1919. map_write(map, CMD(0xF0), chip->start);
  1920. /* FIXME - should have reset delay before continuing */
  1921. if (++retry_cnt <= MAX_RETRIES)
  1922. goto retry;
  1923. ret = -EIO;
  1924. }
  1925. op_done:
  1926. DISABLE_VPP(map);
  1927. return ret;
  1928. }
  1929. /*
  1930. * Write out some data during a kernel panic
  1931. *
  1932. * This is used by the mtdoops driver to save the dying messages from a
  1933. * kernel which has panic'd.
  1934. *
  1935. * This routine ignores all of the locking used throughout the rest of the
  1936. * driver, in order to ensure that the data gets written out no matter what
  1937. * state this driver (and the flash chip itself) was in when the kernel crashed.
  1938. *
  1939. * The implementation of this routine is intentionally similar to
  1940. * cfi_amdstd_write_words(), in order to ease code maintenance.
  1941. */
  1942. static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  1943. size_t *retlen, const u_char *buf)
  1944. {
  1945. struct map_info *map = mtd->priv;
  1946. struct cfi_private *cfi = map->fldrv_priv;
  1947. unsigned long ofs, chipstart;
  1948. int ret;
  1949. int chipnum;
  1950. chipnum = to >> cfi->chipshift;
  1951. ofs = to - (chipnum << cfi->chipshift);
  1952. chipstart = cfi->chips[chipnum].start;
  1953. /* If it's not bus aligned, do the first byte write */
  1954. if (ofs & (map_bankwidth(map) - 1)) {
  1955. unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
  1956. int i = ofs - bus_ofs;
  1957. int n = 0;
  1958. map_word tmp_buf;
  1959. ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
  1960. if (ret)
  1961. return ret;
  1962. /* Load 'tmp_buf' with old contents of flash */
  1963. tmp_buf = map_read(map, bus_ofs + chipstart);
  1964. /* Number of bytes to copy from buffer */
  1965. n = min_t(int, len, map_bankwidth(map) - i);
  1966. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1967. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1968. bus_ofs, tmp_buf);
  1969. if (ret)
  1970. return ret;
  1971. ofs += n;
  1972. buf += n;
  1973. (*retlen) += n;
  1974. len -= n;
  1975. if (ofs >> cfi->chipshift) {
  1976. chipnum++;
  1977. ofs = 0;
  1978. if (chipnum == cfi->numchips)
  1979. return 0;
  1980. }
  1981. }
  1982. /* We are now aligned, write as much as possible */
  1983. while (len >= map_bankwidth(map)) {
  1984. map_word datum;
  1985. datum = map_word_load(map, buf);
  1986. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1987. ofs, datum);
  1988. if (ret)
  1989. return ret;
  1990. ofs += map_bankwidth(map);
  1991. buf += map_bankwidth(map);
  1992. (*retlen) += map_bankwidth(map);
  1993. len -= map_bankwidth(map);
  1994. if (ofs >> cfi->chipshift) {
  1995. chipnum++;
  1996. ofs = 0;
  1997. if (chipnum == cfi->numchips)
  1998. return 0;
  1999. chipstart = cfi->chips[chipnum].start;
  2000. }
  2001. }
  2002. /* Write the trailing bytes if any */
  2003. if (len & (map_bankwidth(map) - 1)) {
  2004. map_word tmp_buf;
  2005. ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
  2006. if (ret)
  2007. return ret;
  2008. tmp_buf = map_read(map, ofs + chipstart);
  2009. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  2010. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  2011. ofs, tmp_buf);
  2012. if (ret)
  2013. return ret;
  2014. (*retlen) += len;
  2015. }
  2016. return 0;
  2017. }
  2018. /*
  2019. * Handle devices with one erase region, that only implement
  2020. * the chip erase command.
  2021. */
  2022. static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
  2023. {
  2024. struct cfi_private *cfi = map->fldrv_priv;
  2025. unsigned long timeo;
  2026. unsigned long int adr;
  2027. DECLARE_WAITQUEUE(wait, current);
  2028. int ret;
  2029. int retry_cnt = 0;
  2030. map_word datum = map_word_ff(map);
  2031. adr = cfi->addr_unlock1;
  2032. mutex_lock(&chip->mutex);
  2033. ret = get_chip(map, chip, adr, FL_ERASING);
  2034. if (ret) {
  2035. mutex_unlock(&chip->mutex);
  2036. return ret;
  2037. }
  2038. pr_debug("MTD %s(): ERASE 0x%.8lx\n",
  2039. __func__, chip->start);
  2040. XIP_INVAL_CACHED_RANGE(map, adr, map->size);
  2041. ENABLE_VPP(map);
  2042. xip_disable(map, chip, adr);
  2043. retry:
  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(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  2047. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  2048. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  2049. cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  2050. chip->state = FL_ERASING;
  2051. chip->erase_suspended = 0;
  2052. chip->in_progress_block_addr = adr;
  2053. chip->in_progress_block_mask = ~(map->size - 1);
  2054. INVALIDATE_CACHE_UDELAY(map, chip,
  2055. adr, map->size,
  2056. chip->erase_time*500);
  2057. timeo = jiffies + (HZ*20);
  2058. for (;;) {
  2059. if (chip->state != FL_ERASING) {
  2060. /* Someone's suspended the erase. Sleep */
  2061. set_current_state(TASK_UNINTERRUPTIBLE);
  2062. add_wait_queue(&chip->wq, &wait);
  2063. mutex_unlock(&chip->mutex);
  2064. schedule();
  2065. remove_wait_queue(&chip->wq, &wait);
  2066. mutex_lock(&chip->mutex);
  2067. continue;
  2068. }
  2069. if (chip->erase_suspended) {
  2070. /* This erase was suspended and resumed.
  2071. Adjust the timeout */
  2072. timeo = jiffies + (HZ*20); /* FIXME */
  2073. chip->erase_suspended = 0;
  2074. }
  2075. if (chip_ready(map, chip, adr, &datum)) {
  2076. if (cfi_check_err_status(map, chip, adr))
  2077. ret = -EIO;
  2078. break;
  2079. }
  2080. if (time_after(jiffies, timeo)) {
  2081. printk(KERN_WARNING "MTD %s(): software timeout\n",
  2082. __func__);
  2083. ret = -EIO;
  2084. break;
  2085. }
  2086. /* Latency issues. Drop the lock, wait a while and retry */
  2087. UDELAY(map, chip, adr, 1000000/HZ);
  2088. }
  2089. /* Did we succeed? */
  2090. if (ret) {
  2091. /* reset on all failures. */
  2092. map_write(map, CMD(0xF0), chip->start);
  2093. /* FIXME - should have reset delay before continuing */
  2094. if (++retry_cnt <= MAX_RETRIES) {
  2095. ret = 0;
  2096. goto retry;
  2097. }
  2098. }
  2099. chip->state = FL_READY;
  2100. xip_enable(map, chip, adr);
  2101. DISABLE_VPP(map);
  2102. put_chip(map, chip, adr);
  2103. mutex_unlock(&chip->mutex);
  2104. return ret;
  2105. }
  2106. static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
  2107. {
  2108. struct cfi_private *cfi = map->fldrv_priv;
  2109. unsigned long timeo;
  2110. DECLARE_WAITQUEUE(wait, current);
  2111. int ret;
  2112. int retry_cnt = 0;
  2113. map_word datum = map_word_ff(map);
  2114. adr += chip->start;
  2115. mutex_lock(&chip->mutex);
  2116. ret = get_chip(map, chip, adr, FL_ERASING);
  2117. if (ret) {
  2118. mutex_unlock(&chip->mutex);
  2119. return ret;
  2120. }
  2121. pr_debug("MTD %s(): ERASE 0x%.8lx\n",
  2122. __func__, adr);
  2123. XIP_INVAL_CACHED_RANGE(map, adr, len);
  2124. ENABLE_VPP(map);
  2125. xip_disable(map, chip, adr);
  2126. retry:
  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. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  2130. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  2131. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  2132. map_write(map, cfi->sector_erase_cmd, adr);
  2133. chip->state = FL_ERASING;
  2134. chip->erase_suspended = 0;
  2135. chip->in_progress_block_addr = adr;
  2136. chip->in_progress_block_mask = ~(len - 1);
  2137. INVALIDATE_CACHE_UDELAY(map, chip,
  2138. adr, len,
  2139. chip->erase_time*500);
  2140. timeo = jiffies + (HZ*20);
  2141. for (;;) {
  2142. if (chip->state != FL_ERASING) {
  2143. /* Someone's suspended the erase. Sleep */
  2144. set_current_state(TASK_UNINTERRUPTIBLE);
  2145. add_wait_queue(&chip->wq, &wait);
  2146. mutex_unlock(&chip->mutex);
  2147. schedule();
  2148. remove_wait_queue(&chip->wq, &wait);
  2149. mutex_lock(&chip->mutex);
  2150. continue;
  2151. }
  2152. if (chip->erase_suspended) {
  2153. /* This erase was suspended and resumed.
  2154. Adjust the timeout */
  2155. timeo = jiffies + (HZ*20); /* FIXME */
  2156. chip->erase_suspended = 0;
  2157. }
  2158. if (chip_ready(map, chip, adr, &datum)) {
  2159. if (cfi_check_err_status(map, chip, adr))
  2160. ret = -EIO;
  2161. break;
  2162. }
  2163. if (time_after(jiffies, timeo)) {
  2164. printk(KERN_WARNING "MTD %s(): software timeout\n",
  2165. __func__);
  2166. ret = -EIO;
  2167. break;
  2168. }
  2169. /* Latency issues. Drop the lock, wait a while and retry */
  2170. UDELAY(map, chip, adr, 1000000/HZ);
  2171. }
  2172. /* Did we succeed? */
  2173. if (ret) {
  2174. /* reset on all failures. */
  2175. map_write(map, CMD(0xF0), chip->start);
  2176. /* FIXME - should have reset delay before continuing */
  2177. if (++retry_cnt <= MAX_RETRIES) {
  2178. ret = 0;
  2179. goto retry;
  2180. }
  2181. }
  2182. chip->state = FL_READY;
  2183. xip_enable(map, chip, adr);
  2184. DISABLE_VPP(map);
  2185. put_chip(map, chip, adr);
  2186. mutex_unlock(&chip->mutex);
  2187. return ret;
  2188. }
  2189. static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
  2190. {
  2191. return cfi_varsize_frob(mtd, do_erase_oneblock, instr->addr,
  2192. instr->len, NULL);
  2193. }
  2194. static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
  2195. {
  2196. struct map_info *map = mtd->priv;
  2197. struct cfi_private *cfi = map->fldrv_priv;
  2198. if (instr->addr != 0)
  2199. return -EINVAL;
  2200. if (instr->len != mtd->size)
  2201. return -EINVAL;
  2202. return do_erase_chip(map, &cfi->chips[0]);
  2203. }
  2204. static int do_atmel_lock(struct map_info *map, struct flchip *chip,
  2205. unsigned long adr, int len, void *thunk)
  2206. {
  2207. struct cfi_private *cfi = map->fldrv_priv;
  2208. int ret;
  2209. mutex_lock(&chip->mutex);
  2210. ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
  2211. if (ret)
  2212. goto out_unlock;
  2213. chip->state = FL_LOCKING;
  2214. pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
  2215. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2216. cfi->device_type, NULL);
  2217. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2218. cfi->device_type, NULL);
  2219. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
  2220. cfi->device_type, NULL);
  2221. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2222. cfi->device_type, NULL);
  2223. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2224. cfi->device_type, NULL);
  2225. map_write(map, CMD(0x40), chip->start + adr);
  2226. chip->state = FL_READY;
  2227. put_chip(map, chip, adr + chip->start);
  2228. ret = 0;
  2229. out_unlock:
  2230. mutex_unlock(&chip->mutex);
  2231. return ret;
  2232. }
  2233. static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
  2234. unsigned long adr, int len, void *thunk)
  2235. {
  2236. struct cfi_private *cfi = map->fldrv_priv;
  2237. int ret;
  2238. mutex_lock(&chip->mutex);
  2239. ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
  2240. if (ret)
  2241. goto out_unlock;
  2242. chip->state = FL_UNLOCKING;
  2243. pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
  2244. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2245. cfi->device_type, NULL);
  2246. map_write(map, CMD(0x70), adr);
  2247. chip->state = FL_READY;
  2248. put_chip(map, chip, adr + chip->start);
  2249. ret = 0;
  2250. out_unlock:
  2251. mutex_unlock(&chip->mutex);
  2252. return ret;
  2253. }
  2254. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2255. {
  2256. return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
  2257. }
  2258. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2259. {
  2260. return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
  2261. }
  2262. /*
  2263. * Advanced Sector Protection - PPB (Persistent Protection Bit) locking
  2264. */
  2265. struct ppb_lock {
  2266. struct flchip *chip;
  2267. unsigned long adr;
  2268. int locked;
  2269. };
  2270. #define DO_XXLOCK_ONEBLOCK_LOCK ((void *)1)
  2271. #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *)2)
  2272. #define DO_XXLOCK_ONEBLOCK_GETLOCK ((void *)3)
  2273. static int __maybe_unused do_ppb_xxlock(struct map_info *map,
  2274. struct flchip *chip,
  2275. unsigned long adr, int len, void *thunk)
  2276. {
  2277. struct cfi_private *cfi = map->fldrv_priv;
  2278. unsigned long timeo;
  2279. int ret;
  2280. adr += chip->start;
  2281. mutex_lock(&chip->mutex);
  2282. ret = get_chip(map, chip, adr, FL_LOCKING);
  2283. if (ret) {
  2284. mutex_unlock(&chip->mutex);
  2285. return ret;
  2286. }
  2287. pr_debug("MTD %s(): XXLOCK 0x%08lx len %d\n", __func__, adr, len);
  2288. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2289. cfi->device_type, NULL);
  2290. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2291. cfi->device_type, NULL);
  2292. /* PPB entry command */
  2293. cfi_send_gen_cmd(0xC0, cfi->addr_unlock1, chip->start, map, cfi,
  2294. cfi->device_type, NULL);
  2295. if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
  2296. chip->state = FL_LOCKING;
  2297. map_write(map, CMD(0xA0), adr);
  2298. map_write(map, CMD(0x00), adr);
  2299. } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
  2300. /*
  2301. * Unlocking of one specific sector is not supported, so we
  2302. * have to unlock all sectors of this device instead
  2303. */
  2304. chip->state = FL_UNLOCKING;
  2305. map_write(map, CMD(0x80), chip->start);
  2306. map_write(map, CMD(0x30), chip->start);
  2307. } else if (thunk == DO_XXLOCK_ONEBLOCK_GETLOCK) {
  2308. chip->state = FL_JEDEC_QUERY;
  2309. /* Return locked status: 0->locked, 1->unlocked */
  2310. ret = !cfi_read_query(map, adr);
  2311. } else
  2312. BUG();
  2313. /*
  2314. * Wait for some time as unlocking of all sectors takes quite long
  2315. */
  2316. timeo = jiffies + msecs_to_jiffies(2000); /* 2s max (un)locking */
  2317. for (;;) {
  2318. if (chip_ready(map, chip, adr, NULL))
  2319. break;
  2320. if (time_after(jiffies, timeo)) {
  2321. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  2322. ret = -EIO;
  2323. break;
  2324. }
  2325. UDELAY(map, chip, adr, 1);
  2326. }
  2327. /* Exit BC commands */
  2328. map_write(map, CMD(0x90), chip->start);
  2329. map_write(map, CMD(0x00), chip->start);
  2330. chip->state = FL_READY;
  2331. put_chip(map, chip, adr);
  2332. mutex_unlock(&chip->mutex);
  2333. return ret;
  2334. }
  2335. static int __maybe_unused cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs,
  2336. uint64_t len)
  2337. {
  2338. return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2339. DO_XXLOCK_ONEBLOCK_LOCK);
  2340. }
  2341. static int __maybe_unused cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs,
  2342. uint64_t len)
  2343. {
  2344. struct mtd_erase_region_info *regions = mtd->eraseregions;
  2345. struct map_info *map = mtd->priv;
  2346. struct cfi_private *cfi = map->fldrv_priv;
  2347. struct ppb_lock *sect;
  2348. unsigned long adr;
  2349. loff_t offset;
  2350. uint64_t length;
  2351. int chipnum;
  2352. int i;
  2353. int sectors;
  2354. int ret;
  2355. int max_sectors;
  2356. /*
  2357. * PPB unlocking always unlocks all sectors of the flash chip.
  2358. * We need to re-lock all previously locked sectors. So lets
  2359. * first check the locking status of all sectors and save
  2360. * it for future use.
  2361. */
  2362. max_sectors = 0;
  2363. for (i = 0; i < mtd->numeraseregions; i++)
  2364. max_sectors += regions[i].numblocks;
  2365. sect = kcalloc(max_sectors, sizeof(struct ppb_lock), GFP_KERNEL);
  2366. if (!sect)
  2367. return -ENOMEM;
  2368. /*
  2369. * This code to walk all sectors is a slightly modified version
  2370. * of the cfi_varsize_frob() code.
  2371. */
  2372. i = 0;
  2373. chipnum = 0;
  2374. adr = 0;
  2375. sectors = 0;
  2376. offset = 0;
  2377. length = mtd->size;
  2378. while (length) {
  2379. int size = regions[i].erasesize;
  2380. /*
  2381. * Only test sectors that shall not be unlocked. The other
  2382. * sectors shall be unlocked, so lets keep their locking
  2383. * status at "unlocked" (locked=0) for the final re-locking.
  2384. */
  2385. if ((offset < ofs) || (offset >= (ofs + len))) {
  2386. sect[sectors].chip = &cfi->chips[chipnum];
  2387. sect[sectors].adr = adr;
  2388. sect[sectors].locked = do_ppb_xxlock(
  2389. map, &cfi->chips[chipnum], adr, 0,
  2390. DO_XXLOCK_ONEBLOCK_GETLOCK);
  2391. }
  2392. adr += size;
  2393. offset += size;
  2394. length -= size;
  2395. if (offset == regions[i].offset + size * regions[i].numblocks)
  2396. i++;
  2397. if (adr >> cfi->chipshift) {
  2398. if (offset >= (ofs + len))
  2399. break;
  2400. adr = 0;
  2401. chipnum++;
  2402. if (chipnum >= cfi->numchips)
  2403. break;
  2404. }
  2405. sectors++;
  2406. if (sectors >= max_sectors) {
  2407. printk(KERN_ERR "Only %d sectors for PPB locking supported!\n",
  2408. max_sectors);
  2409. kfree(sect);
  2410. return -EINVAL;
  2411. }
  2412. }
  2413. /* Now unlock the whole chip */
  2414. ret = cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2415. DO_XXLOCK_ONEBLOCK_UNLOCK);
  2416. if (ret) {
  2417. kfree(sect);
  2418. return ret;
  2419. }
  2420. /*
  2421. * PPB unlocking always unlocks all sectors of the flash chip.
  2422. * We need to re-lock all previously locked sectors.
  2423. */
  2424. for (i = 0; i < sectors; i++) {
  2425. if (sect[i].locked)
  2426. do_ppb_xxlock(map, sect[i].chip, sect[i].adr, 0,
  2427. DO_XXLOCK_ONEBLOCK_LOCK);
  2428. }
  2429. kfree(sect);
  2430. return ret;
  2431. }
  2432. static int __maybe_unused cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs,
  2433. uint64_t len)
  2434. {
  2435. return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2436. DO_XXLOCK_ONEBLOCK_GETLOCK) ? 1 : 0;
  2437. }
  2438. static void cfi_amdstd_sync (struct mtd_info *mtd)
  2439. {
  2440. struct map_info *map = mtd->priv;
  2441. struct cfi_private *cfi = map->fldrv_priv;
  2442. int i;
  2443. struct flchip *chip;
  2444. int ret = 0;
  2445. DECLARE_WAITQUEUE(wait, current);
  2446. for (i=0; !ret && i<cfi->numchips; i++) {
  2447. chip = &cfi->chips[i];
  2448. retry:
  2449. mutex_lock(&chip->mutex);
  2450. switch(chip->state) {
  2451. case FL_READY:
  2452. case FL_STATUS:
  2453. case FL_CFI_QUERY:
  2454. case FL_JEDEC_QUERY:
  2455. chip->oldstate = chip->state;
  2456. chip->state = FL_SYNCING;
  2457. /* No need to wake_up() on this state change -
  2458. * as the whole point is that nobody can do anything
  2459. * with the chip now anyway.
  2460. */
  2461. fallthrough;
  2462. case FL_SYNCING:
  2463. mutex_unlock(&chip->mutex);
  2464. break;
  2465. default:
  2466. /* Not an idle state */
  2467. set_current_state(TASK_UNINTERRUPTIBLE);
  2468. add_wait_queue(&chip->wq, &wait);
  2469. mutex_unlock(&chip->mutex);
  2470. schedule();
  2471. remove_wait_queue(&chip->wq, &wait);
  2472. goto retry;
  2473. }
  2474. }
  2475. /* Unlock the chips again */
  2476. for (i--; i >=0; i--) {
  2477. chip = &cfi->chips[i];
  2478. mutex_lock(&chip->mutex);
  2479. if (chip->state == FL_SYNCING) {
  2480. chip->state = chip->oldstate;
  2481. wake_up(&chip->wq);
  2482. }
  2483. mutex_unlock(&chip->mutex);
  2484. }
  2485. }
  2486. static int cfi_amdstd_suspend(struct mtd_info *mtd)
  2487. {
  2488. struct map_info *map = mtd->priv;
  2489. struct cfi_private *cfi = map->fldrv_priv;
  2490. int i;
  2491. struct flchip *chip;
  2492. int ret = 0;
  2493. for (i=0; !ret && i<cfi->numchips; i++) {
  2494. chip = &cfi->chips[i];
  2495. mutex_lock(&chip->mutex);
  2496. switch(chip->state) {
  2497. case FL_READY:
  2498. case FL_STATUS:
  2499. case FL_CFI_QUERY:
  2500. case FL_JEDEC_QUERY:
  2501. chip->oldstate = chip->state;
  2502. chip->state = FL_PM_SUSPENDED;
  2503. /* No need to wake_up() on this state change -
  2504. * as the whole point is that nobody can do anything
  2505. * with the chip now anyway.
  2506. */
  2507. break;
  2508. case FL_PM_SUSPENDED:
  2509. break;
  2510. default:
  2511. ret = -EAGAIN;
  2512. break;
  2513. }
  2514. mutex_unlock(&chip->mutex);
  2515. }
  2516. /* Unlock the chips again */
  2517. if (ret) {
  2518. for (i--; i >=0; i--) {
  2519. chip = &cfi->chips[i];
  2520. mutex_lock(&chip->mutex);
  2521. if (chip->state == FL_PM_SUSPENDED) {
  2522. chip->state = chip->oldstate;
  2523. wake_up(&chip->wq);
  2524. }
  2525. mutex_unlock(&chip->mutex);
  2526. }
  2527. }
  2528. return ret;
  2529. }
  2530. static void cfi_amdstd_resume(struct mtd_info *mtd)
  2531. {
  2532. struct map_info *map = mtd->priv;
  2533. struct cfi_private *cfi = map->fldrv_priv;
  2534. int i;
  2535. struct flchip *chip;
  2536. for (i=0; i<cfi->numchips; i++) {
  2537. chip = &cfi->chips[i];
  2538. mutex_lock(&chip->mutex);
  2539. if (chip->state == FL_PM_SUSPENDED) {
  2540. chip->state = FL_READY;
  2541. map_write(map, CMD(0xF0), chip->start);
  2542. wake_up(&chip->wq);
  2543. }
  2544. else
  2545. printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
  2546. mutex_unlock(&chip->mutex);
  2547. }
  2548. }
  2549. /*
  2550. * Ensure that the flash device is put back into read array mode before
  2551. * unloading the driver or rebooting. On some systems, rebooting while
  2552. * the flash is in query/program/erase mode will prevent the CPU from
  2553. * fetching the bootloader code, requiring a hard reset or power cycle.
  2554. */
  2555. static int cfi_amdstd_reset(struct mtd_info *mtd)
  2556. {
  2557. struct map_info *map = mtd->priv;
  2558. struct cfi_private *cfi = map->fldrv_priv;
  2559. int i, ret;
  2560. struct flchip *chip;
  2561. for (i = 0; i < cfi->numchips; i++) {
  2562. chip = &cfi->chips[i];
  2563. mutex_lock(&chip->mutex);
  2564. ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
  2565. if (!ret) {
  2566. map_write(map, CMD(0xF0), chip->start);
  2567. chip->state = FL_SHUTDOWN;
  2568. put_chip(map, chip, chip->start);
  2569. }
  2570. mutex_unlock(&chip->mutex);
  2571. }
  2572. return 0;
  2573. }
  2574. static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
  2575. void *v)
  2576. {
  2577. struct mtd_info *mtd;
  2578. mtd = container_of(nb, struct mtd_info, reboot_notifier);
  2579. cfi_amdstd_reset(mtd);
  2580. return NOTIFY_DONE;
  2581. }
  2582. static void cfi_amdstd_destroy(struct mtd_info *mtd)
  2583. {
  2584. struct map_info *map = mtd->priv;
  2585. struct cfi_private *cfi = map->fldrv_priv;
  2586. cfi_amdstd_reset(mtd);
  2587. unregister_reboot_notifier(&mtd->reboot_notifier);
  2588. kfree(cfi->cmdset_priv);
  2589. kfree(cfi->cfiq);
  2590. kfree(cfi);
  2591. kfree(mtd->eraseregions);
  2592. }
  2593. MODULE_LICENSE("GPL");
  2594. MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
  2595. MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
  2596. MODULE_ALIAS("cfi_cmdset_0006");
  2597. MODULE_ALIAS("cfi_cmdset_0701");