cfi_cmdset_0002.c 55 KB

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