cfi_cmdset_0002.c 49 KB

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