cfi_cmdset_0002.c 51 KB

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