cfi_cmdset_0002.c 61 KB

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