cfi_cmdset_0002.c 55 KB

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