cfi_cmdset_0002.c 52 KB

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