cfi_cmdset_0002.c 46 KB

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