cfi_cmdset_0002.c 46 KB

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