cfi_cmdset_0002.c 46 KB

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