cfi_cmdset_0002.c 70 KB

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