cfi_cmdset_0002.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  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/module.h>
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/sched.h>
  27. #include <linux/init.h>
  28. #include <asm/io.h>
  29. #include <asm/byteorder.h>
  30. #include <linux/errno.h>
  31. #include <linux/slab.h>
  32. #include <linux/delay.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/mtd/compatmac.h>
  35. #include <linux/mtd/map.h>
  36. #include <linux/mtd/mtd.h>
  37. #include <linux/mtd/cfi.h>
  38. #include <linux/mtd/xip.h>
  39. #define AMD_BOOTLOC_BUG
  40. #define FORCE_WORD_WRITE 0
  41. #define MAX_WORD_RETRIES 3
  42. #define MANUFACTURER_AMD 0x0001
  43. #define MANUFACTURER_SST 0x00BF
  44. #define SST49LF004B 0x0060
  45. #define SST49LF008A 0x005a
  46. static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  47. static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  48. static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  49. static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
  50. static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
  51. static void cfi_amdstd_sync (struct mtd_info *);
  52. static int cfi_amdstd_suspend (struct mtd_info *);
  53. static void cfi_amdstd_resume (struct mtd_info *);
  54. static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  55. static void cfi_amdstd_destroy(struct mtd_info *);
  56. struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
  57. static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
  58. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
  59. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
  60. #include "fwh_lock.h"
  61. static struct mtd_chip_driver cfi_amdstd_chipdrv = {
  62. .probe = NULL, /* Not usable directly */
  63. .destroy = cfi_amdstd_destroy,
  64. .name = "cfi_cmdset_0002",
  65. .module = THIS_MODULE
  66. };
  67. /* #define DEBUG_CFI_FEATURES */
  68. #ifdef DEBUG_CFI_FEATURES
  69. static void cfi_tell_features(struct cfi_pri_amdstd *extp)
  70. {
  71. const char* erase_suspend[3] = {
  72. "Not supported", "Read only", "Read/write"
  73. };
  74. const char* top_bottom[6] = {
  75. "No WP", "8x8KiB sectors at top & bottom, no WP",
  76. "Bottom boot", "Top boot",
  77. "Uniform, Bottom WP", "Uniform, Top WP"
  78. };
  79. printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
  80. printk(" Address sensitive unlock: %s\n",
  81. (extp->SiliconRevision & 1) ? "Not required" : "Required");
  82. if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
  83. printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
  84. else
  85. printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
  86. if (extp->BlkProt == 0)
  87. printk(" Block protection: Not supported\n");
  88. else
  89. printk(" Block protection: %d sectors per group\n", extp->BlkProt);
  90. printk(" Temporary block unprotect: %s\n",
  91. extp->TmpBlkUnprotect ? "Supported" : "Not supported");
  92. printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
  93. printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
  94. printk(" Burst mode: %s\n",
  95. extp->BurstMode ? "Supported" : "Not supported");
  96. if (extp->PageMode == 0)
  97. printk(" Page mode: Not supported\n");
  98. else
  99. printk(" Page mode: %d word page\n", extp->PageMode << 2);
  100. printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
  101. extp->VppMin >> 4, extp->VppMin & 0xf);
  102. printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
  103. extp->VppMax >> 4, extp->VppMax & 0xf);
  104. if (extp->TopBottom < ARRAY_SIZE(top_bottom))
  105. printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
  106. else
  107. printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
  108. }
  109. #endif
  110. #ifdef AMD_BOOTLOC_BUG
  111. /* Wheee. Bring me the head of someone at AMD. */
  112. static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
  113. {
  114. struct map_info *map = mtd->priv;
  115. struct cfi_private *cfi = map->fldrv_priv;
  116. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  117. __u8 major = extp->MajorVersion;
  118. __u8 minor = extp->MinorVersion;
  119. if (((major << 8) | minor) < 0x3131) {
  120. /* CFI version 1.0 => don't trust bootloc */
  121. if (cfi->id & 0x80) {
  122. printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
  123. extp->TopBottom = 3; /* top boot */
  124. } else {
  125. extp->TopBottom = 2; /* bottom boot */
  126. }
  127. }
  128. }
  129. #endif
  130. static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
  131. {
  132. struct map_info *map = mtd->priv;
  133. struct cfi_private *cfi = map->fldrv_priv;
  134. if (cfi->cfiq->BufWriteTimeoutTyp) {
  135. DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
  136. mtd->write = cfi_amdstd_write_buffers;
  137. }
  138. }
  139. static void fixup_use_secsi(struct mtd_info *mtd, void *param)
  140. {
  141. /* Setup for chips with a secsi area */
  142. mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
  143. mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
  144. }
  145. static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
  146. {
  147. struct map_info *map = mtd->priv;
  148. struct cfi_private *cfi = map->fldrv_priv;
  149. if ((cfi->cfiq->NumEraseRegions == 1) &&
  150. ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
  151. mtd->erase = cfi_amdstd_erase_chip;
  152. }
  153. }
  154. static struct cfi_fixup cfi_fixup_table[] = {
  155. #ifdef AMD_BOOTLOC_BUG
  156. { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
  157. #endif
  158. { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
  159. { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
  160. { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
  161. { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
  162. { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
  163. { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
  164. #if !FORCE_WORD_WRITE
  165. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
  166. #endif
  167. { 0, 0, NULL, NULL }
  168. };
  169. static struct cfi_fixup jedec_fixup_table[] = {
  170. { MANUFACTURER_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
  171. { MANUFACTURER_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
  172. { 0, 0, NULL, NULL }
  173. };
  174. static struct cfi_fixup fixup_table[] = {
  175. /* The CFI vendor ids and the JEDEC vendor IDs appear
  176. * to be common. It is like the devices id's are as
  177. * well. This table is to pick all cases where
  178. * we know that is the case.
  179. */
  180. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
  181. { 0, 0, NULL, NULL }
  182. };
  183. struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
  184. {
  185. struct cfi_private *cfi = map->fldrv_priv;
  186. struct mtd_info *mtd;
  187. int i;
  188. mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
  189. if (!mtd) {
  190. printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
  191. return NULL;
  192. }
  193. memset(mtd, 0, sizeof(*mtd));
  194. mtd->priv = map;
  195. mtd->type = MTD_NORFLASH;
  196. /* Fill in the default mtd operations */
  197. mtd->erase = cfi_amdstd_erase_varsize;
  198. mtd->write = cfi_amdstd_write_words;
  199. mtd->read = cfi_amdstd_read;
  200. mtd->sync = cfi_amdstd_sync;
  201. mtd->suspend = cfi_amdstd_suspend;
  202. mtd->resume = cfi_amdstd_resume;
  203. mtd->flags = MTD_CAP_NORFLASH;
  204. mtd->name = map->name;
  205. mtd->writesize = 1;
  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. EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
  283. static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
  284. {
  285. struct map_info *map = mtd->priv;
  286. struct cfi_private *cfi = map->fldrv_priv;
  287. unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
  288. unsigned long offset = 0;
  289. int i,j;
  290. printk(KERN_NOTICE "number of %s chips: %d\n",
  291. (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
  292. /* Select the correct geometry setup */
  293. mtd->size = devsize * cfi->numchips;
  294. mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
  295. mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
  296. * mtd->numeraseregions, GFP_KERNEL);
  297. if (!mtd->eraseregions) {
  298. printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
  299. goto setup_err;
  300. }
  301. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  302. unsigned long ernum, ersize;
  303. ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
  304. ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
  305. if (mtd->erasesize < ersize) {
  306. mtd->erasesize = ersize;
  307. }
  308. for (j=0; j<cfi->numchips; j++) {
  309. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
  310. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
  311. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
  312. }
  313. offset += (ersize * ernum);
  314. }
  315. if (offset != devsize) {
  316. /* Argh */
  317. printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
  318. goto setup_err;
  319. }
  320. #if 0
  321. // debug
  322. for (i=0; i<mtd->numeraseregions;i++){
  323. printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
  324. i,mtd->eraseregions[i].offset,
  325. mtd->eraseregions[i].erasesize,
  326. mtd->eraseregions[i].numblocks);
  327. }
  328. #endif
  329. /* FIXME: erase-suspend-program is broken. See
  330. http://lists.infradead.org/pipermail/linux-mtd/2003-December/009001.html */
  331. printk(KERN_NOTICE "cfi_cmdset_0002: Disabling erase-suspend-program due to code brokenness.\n");
  332. __module_get(THIS_MODULE);
  333. return mtd;
  334. setup_err:
  335. if(mtd) {
  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. MODULE_LICENSE("GPL");
  1447. MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
  1448. MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");