cfi_cmdset_0002.c 51 KB

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