cfi_cmdset_0002.c 49 KB

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