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