cfi_cmdset_0002.c 55 KB

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