cfi_cmdset_0002.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  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. * 25/09/2008 Christopher Moore: TopBottom fixup for many Macronix with CFI V1.0
  17. *
  18. * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
  19. *
  20. * This code is GPL
  21. */
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/init.h>
  27. #include <asm/io.h>
  28. #include <asm/byteorder.h>
  29. #include <linux/errno.h>
  30. #include <linux/slab.h>
  31. #include <linux/delay.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/reboot.h>
  34. #include <linux/mtd/map.h>
  35. #include <linux/mtd/mtd.h>
  36. #include <linux/mtd/cfi.h>
  37. #include <linux/mtd/xip.h>
  38. #define AMD_BOOTLOC_BUG
  39. #define FORCE_WORD_WRITE 0
  40. #define MAX_WORD_RETRIES 3
  41. #define SST49LF004B 0x0060
  42. #define SST49LF040B 0x0050
  43. #define SST49LF008A 0x005a
  44. #define AT49BV6416 0x00d6
  45. static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  46. static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  47. static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  48. static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
  49. static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
  50. static void cfi_amdstd_sync (struct mtd_info *);
  51. static int cfi_amdstd_suspend (struct mtd_info *);
  52. static void cfi_amdstd_resume (struct mtd_info *);
  53. static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *);
  54. static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  55. static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  56. size_t *retlen, const u_char *buf);
  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, uint64_t len);
  64. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_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)
  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. pr_debug("%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
  126. map->name, cfi->mfr, cfi->id);
  127. /* AFAICS all 29LV400 with a bottom boot block have a device ID
  128. * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
  129. * These were badly detected as they have the 0x80 bit set
  130. * so treat them as a special case.
  131. */
  132. if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
  133. /* Macronix added CFI to their 2nd generation
  134. * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
  135. * Fujitsu, Spansion, EON, ESI and older Macronix)
  136. * has CFI.
  137. *
  138. * Therefore also check the manufacturer.
  139. * This reduces the risk of false detection due to
  140. * the 8-bit device ID.
  141. */
  142. (cfi->mfr == CFI_MFR_MACRONIX)) {
  143. pr_debug("%s: Macronix MX29LV400C with bottom boot block"
  144. " detected\n", map->name);
  145. extp->TopBottom = 2; /* bottom boot */
  146. } else
  147. if (cfi->id & 0x80) {
  148. printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
  149. extp->TopBottom = 3; /* top boot */
  150. } else {
  151. extp->TopBottom = 2; /* bottom boot */
  152. }
  153. pr_debug("%s: AMD CFI PRI V%c.%c has no boot block field;"
  154. " deduced %s from Device ID\n", map->name, major, minor,
  155. extp->TopBottom == 2 ? "bottom" : "top");
  156. }
  157. }
  158. #endif
  159. static void fixup_use_write_buffers(struct mtd_info *mtd)
  160. {
  161. struct map_info *map = mtd->priv;
  162. struct cfi_private *cfi = map->fldrv_priv;
  163. if (cfi->cfiq->BufWriteTimeoutTyp) {
  164. pr_debug("Using buffer write method\n" );
  165. mtd->_write = cfi_amdstd_write_buffers;
  166. }
  167. }
  168. /* Atmel chips don't use the same PRI format as AMD chips */
  169. static void fixup_convert_atmel_pri(struct mtd_info *mtd)
  170. {
  171. struct map_info *map = mtd->priv;
  172. struct cfi_private *cfi = map->fldrv_priv;
  173. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  174. struct cfi_pri_atmel atmel_pri;
  175. memcpy(&atmel_pri, extp, sizeof(atmel_pri));
  176. memset((char *)extp + 5, 0, sizeof(*extp) - 5);
  177. if (atmel_pri.Features & 0x02)
  178. extp->EraseSuspend = 2;
  179. /* Some chips got it backwards... */
  180. if (cfi->id == AT49BV6416) {
  181. if (atmel_pri.BottomBoot)
  182. extp->TopBottom = 3;
  183. else
  184. extp->TopBottom = 2;
  185. } else {
  186. if (atmel_pri.BottomBoot)
  187. extp->TopBottom = 2;
  188. else
  189. extp->TopBottom = 3;
  190. }
  191. /* burst write mode not supported */
  192. cfi->cfiq->BufWriteTimeoutTyp = 0;
  193. cfi->cfiq->BufWriteTimeoutMax = 0;
  194. }
  195. static void fixup_use_secsi(struct mtd_info *mtd)
  196. {
  197. /* Setup for chips with a secsi area */
  198. mtd->_read_user_prot_reg = cfi_amdstd_secsi_read;
  199. mtd->_read_fact_prot_reg = cfi_amdstd_secsi_read;
  200. }
  201. static void fixup_use_erase_chip(struct mtd_info *mtd)
  202. {
  203. struct map_info *map = mtd->priv;
  204. struct cfi_private *cfi = map->fldrv_priv;
  205. if ((cfi->cfiq->NumEraseRegions == 1) &&
  206. ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
  207. mtd->_erase = cfi_amdstd_erase_chip;
  208. }
  209. }
  210. /*
  211. * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
  212. * locked by default.
  213. */
  214. static void fixup_use_atmel_lock(struct mtd_info *mtd)
  215. {
  216. mtd->_lock = cfi_atmel_lock;
  217. mtd->_unlock = cfi_atmel_unlock;
  218. mtd->flags |= MTD_POWERUP_LOCK;
  219. }
  220. static void fixup_old_sst_eraseregion(struct mtd_info *mtd)
  221. {
  222. struct map_info *map = mtd->priv;
  223. struct cfi_private *cfi = map->fldrv_priv;
  224. /*
  225. * These flashes report two separate eraseblock regions based on the
  226. * sector_erase-size and block_erase-size, although they both operate on the
  227. * same memory. This is not allowed according to CFI, so we just pick the
  228. * sector_erase-size.
  229. */
  230. cfi->cfiq->NumEraseRegions = 1;
  231. }
  232. static void fixup_sst39vf(struct mtd_info *mtd)
  233. {
  234. struct map_info *map = mtd->priv;
  235. struct cfi_private *cfi = map->fldrv_priv;
  236. fixup_old_sst_eraseregion(mtd);
  237. cfi->addr_unlock1 = 0x5555;
  238. cfi->addr_unlock2 = 0x2AAA;
  239. }
  240. static void fixup_sst39vf_rev_b(struct mtd_info *mtd)
  241. {
  242. struct map_info *map = mtd->priv;
  243. struct cfi_private *cfi = map->fldrv_priv;
  244. fixup_old_sst_eraseregion(mtd);
  245. cfi->addr_unlock1 = 0x555;
  246. cfi->addr_unlock2 = 0x2AA;
  247. cfi->sector_erase_cmd = CMD(0x50);
  248. }
  249. static void fixup_sst38vf640x_sectorsize(struct mtd_info *mtd)
  250. {
  251. struct map_info *map = mtd->priv;
  252. struct cfi_private *cfi = map->fldrv_priv;
  253. fixup_sst39vf_rev_b(mtd);
  254. /*
  255. * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where
  256. * it should report a size of 8KBytes (0x0020*256).
  257. */
  258. cfi->cfiq->EraseRegionInfo[0] = 0x002003ff;
  259. pr_warning("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n", mtd->name);
  260. }
  261. static void fixup_s29gl064n_sectors(struct mtd_info *mtd)
  262. {
  263. struct map_info *map = mtd->priv;
  264. struct cfi_private *cfi = map->fldrv_priv;
  265. if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
  266. cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
  267. pr_warning("%s: Bad S29GL064N CFI data; adjust from 64 to 128 sectors\n", mtd->name);
  268. }
  269. }
  270. static void fixup_s29gl032n_sectors(struct mtd_info *mtd)
  271. {
  272. struct map_info *map = mtd->priv;
  273. struct cfi_private *cfi = map->fldrv_priv;
  274. if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
  275. cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
  276. pr_warning("%s: Bad S29GL032N CFI data; adjust from 127 to 63 sectors\n", mtd->name);
  277. }
  278. }
  279. static void fixup_s29ns512p_sectors(struct mtd_info *mtd)
  280. {
  281. struct map_info *map = mtd->priv;
  282. struct cfi_private *cfi = map->fldrv_priv;
  283. /*
  284. * S29NS512P flash uses more than 8bits to report number of sectors,
  285. * which is not permitted by CFI.
  286. */
  287. cfi->cfiq->EraseRegionInfo[0] = 0x020001ff;
  288. pr_warning("%s: Bad S29NS512P CFI data; adjust to 512 sectors\n", mtd->name);
  289. }
  290. /* Used to fix CFI-Tables of chips without Extended Query Tables */
  291. static struct cfi_fixup cfi_nopri_fixup_table[] = {
  292. { CFI_MFR_SST, 0x234a, fixup_sst39vf }, /* SST39VF1602 */
  293. { CFI_MFR_SST, 0x234b, fixup_sst39vf }, /* SST39VF1601 */
  294. { CFI_MFR_SST, 0x235a, fixup_sst39vf }, /* SST39VF3202 */
  295. { CFI_MFR_SST, 0x235b, fixup_sst39vf }, /* SST39VF3201 */
  296. { CFI_MFR_SST, 0x235c, fixup_sst39vf_rev_b }, /* SST39VF3202B */
  297. { CFI_MFR_SST, 0x235d, fixup_sst39vf_rev_b }, /* SST39VF3201B */
  298. { CFI_MFR_SST, 0x236c, fixup_sst39vf_rev_b }, /* SST39VF6402B */
  299. { CFI_MFR_SST, 0x236d, fixup_sst39vf_rev_b }, /* SST39VF6401B */
  300. { 0, 0, NULL }
  301. };
  302. static struct cfi_fixup cfi_fixup_table[] = {
  303. { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
  304. #ifdef AMD_BOOTLOC_BUG
  305. { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock },
  306. { CFI_MFR_AMIC, CFI_ID_ANY, fixup_amd_bootblock },
  307. { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock },
  308. #endif
  309. { CFI_MFR_AMD, 0x0050, fixup_use_secsi },
  310. { CFI_MFR_AMD, 0x0053, fixup_use_secsi },
  311. { CFI_MFR_AMD, 0x0055, fixup_use_secsi },
  312. { CFI_MFR_AMD, 0x0056, fixup_use_secsi },
  313. { CFI_MFR_AMD, 0x005C, fixup_use_secsi },
  314. { CFI_MFR_AMD, 0x005F, fixup_use_secsi },
  315. { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors },
  316. { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors },
  317. { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors },
  318. { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors },
  319. { CFI_MFR_AMD, 0x3f00, fixup_s29ns512p_sectors },
  320. { CFI_MFR_SST, 0x536a, fixup_sst38vf640x_sectorsize }, /* SST38VF6402 */
  321. { CFI_MFR_SST, 0x536b, fixup_sst38vf640x_sectorsize }, /* SST38VF6401 */
  322. { CFI_MFR_SST, 0x536c, fixup_sst38vf640x_sectorsize }, /* SST38VF6404 */
  323. { CFI_MFR_SST, 0x536d, fixup_sst38vf640x_sectorsize }, /* SST38VF6403 */
  324. #if !FORCE_WORD_WRITE
  325. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
  326. #endif
  327. { 0, 0, NULL }
  328. };
  329. static struct cfi_fixup jedec_fixup_table[] = {
  330. { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock },
  331. { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock },
  332. { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock },
  333. { 0, 0, NULL }
  334. };
  335. static struct cfi_fixup fixup_table[] = {
  336. /* The CFI vendor ids and the JEDEC vendor IDs appear
  337. * to be common. It is like the devices id's are as
  338. * well. This table is to pick all cases where
  339. * we know that is the case.
  340. */
  341. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip },
  342. { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock },
  343. { 0, 0, NULL }
  344. };
  345. static void cfi_fixup_major_minor(struct cfi_private *cfi,
  346. struct cfi_pri_amdstd *extp)
  347. {
  348. if (cfi->mfr == CFI_MFR_SAMSUNG) {
  349. if ((extp->MajorVersion == '0' && extp->MinorVersion == '0') ||
  350. (extp->MajorVersion == '3' && extp->MinorVersion == '3')) {
  351. /*
  352. * Samsung K8P2815UQB and K8D6x16UxM chips
  353. * report major=0 / minor=0.
  354. * K8D3x16UxC chips report major=3 / minor=3.
  355. */
  356. printk(KERN_NOTICE " Fixing Samsung's Amd/Fujitsu"
  357. " Extended Query version to 1.%c\n",
  358. extp->MinorVersion);
  359. extp->MajorVersion = '1';
  360. }
  361. }
  362. /*
  363. * SST 38VF640x chips report major=0xFF / minor=0xFF.
  364. */
  365. if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) {
  366. extp->MajorVersion = '1';
  367. extp->MinorVersion = '0';
  368. }
  369. }
  370. static int is_m29ew(struct cfi_private *cfi)
  371. {
  372. if (cfi->mfr == CFI_MFR_INTEL &&
  373. ((cfi->device_type == CFI_DEVICETYPE_X8 && (cfi->id & 0xff) == 0x7e) ||
  374. (cfi->device_type == CFI_DEVICETYPE_X16 && cfi->id == 0x227e)))
  375. return 1;
  376. return 0;
  377. }
  378. /*
  379. * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 20:
  380. * Some revisions of the M29EW suffer from erase suspend hang ups. In
  381. * particular, it can occur when the sequence
  382. * Erase Confirm -> Suspend -> Program -> Resume
  383. * causes a lockup due to internal timing issues. The consequence is that the
  384. * erase cannot be resumed without inserting a dummy command after programming
  385. * and prior to resuming. [...] The work-around is to issue a dummy write cycle
  386. * that writes an F0 command code before the RESUME command.
  387. */
  388. static void cfi_fixup_m29ew_erase_suspend(struct map_info *map,
  389. unsigned long adr)
  390. {
  391. struct cfi_private *cfi = map->fldrv_priv;
  392. /* before resume, insert a dummy 0xF0 cycle for Micron M29EW devices */
  393. if (is_m29ew(cfi))
  394. map_write(map, CMD(0xF0), adr);
  395. }
  396. /*
  397. * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 22:
  398. *
  399. * Some revisions of the M29EW (for example, A1 and A2 step revisions)
  400. * are affected by a problem that could cause a hang up when an ERASE SUSPEND
  401. * command is issued after an ERASE RESUME operation without waiting for a
  402. * minimum delay. The result is that once the ERASE seems to be completed
  403. * (no bits are toggling), the contents of the Flash memory block on which
  404. * the erase was ongoing could be inconsistent with the expected values
  405. * (typically, the array value is stuck to the 0xC0, 0xC4, 0x80, or 0x84
  406. * values), causing a consequent failure of the ERASE operation.
  407. * The occurrence of this issue could be high, especially when file system
  408. * operations on the Flash are intensive. As a result, it is recommended
  409. * that a patch be applied. Intensive file system operations can cause many
  410. * calls to the garbage routine to free Flash space (also by erasing physical
  411. * Flash blocks) and as a result, many consecutive SUSPEND and RESUME
  412. * commands can occur. The problem disappears when a delay is inserted after
  413. * the RESUME command by using the udelay() function available in Linux.
  414. * The DELAY value must be tuned based on the customer's platform.
  415. * The maximum value that fixes the problem in all cases is 500us.
  416. * But, in our experience, a delay of 30 µs to 50 µs is sufficient
  417. * in most cases.
  418. * We have chosen 500µs because this latency is acceptable.
  419. */
  420. static void cfi_fixup_m29ew_delay_after_resume(struct cfi_private *cfi)
  421. {
  422. /*
  423. * Resolving the Delay After Resume Issue see Micron TN-13-07
  424. * Worst case delay must be 500µs but 30-50µs should be ok as well
  425. */
  426. if (is_m29ew(cfi))
  427. cfi_udelay(500);
  428. }
  429. struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
  430. {
  431. struct cfi_private *cfi = map->fldrv_priv;
  432. struct mtd_info *mtd;
  433. int i;
  434. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  435. if (!mtd) {
  436. printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
  437. return NULL;
  438. }
  439. mtd->priv = map;
  440. mtd->type = MTD_NORFLASH;
  441. /* Fill in the default mtd operations */
  442. mtd->_erase = cfi_amdstd_erase_varsize;
  443. mtd->_write = cfi_amdstd_write_words;
  444. mtd->_read = cfi_amdstd_read;
  445. mtd->_sync = cfi_amdstd_sync;
  446. mtd->_suspend = cfi_amdstd_suspend;
  447. mtd->_resume = cfi_amdstd_resume;
  448. mtd->flags = MTD_CAP_NORFLASH;
  449. mtd->name = map->name;
  450. mtd->writesize = 1;
  451. mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  452. pr_debug("MTD %s(): write buffer size %d\n", __func__,
  453. mtd->writebufsize);
  454. mtd->_panic_write = cfi_amdstd_panic_write;
  455. mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
  456. if (cfi->cfi_mode==CFI_MODE_CFI){
  457. unsigned char bootloc;
  458. __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
  459. struct cfi_pri_amdstd *extp;
  460. extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
  461. if (extp) {
  462. /*
  463. * It's a real CFI chip, not one for which the probe
  464. * routine faked a CFI structure.
  465. */
  466. cfi_fixup_major_minor(cfi, extp);
  467. /*
  468. * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4, 1.5
  469. * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19
  470. * http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
  471. * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
  472. * http://www.spansion.com/Support/Datasheets/S29GL_128S_01GS_00_02_e.pdf
  473. */
  474. if (extp->MajorVersion != '1' ||
  475. (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '5'))) {
  476. printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
  477. "version %c.%c (%#02x/%#02x).\n",
  478. extp->MajorVersion, extp->MinorVersion,
  479. extp->MajorVersion, extp->MinorVersion);
  480. kfree(extp);
  481. kfree(mtd);
  482. return NULL;
  483. }
  484. printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n",
  485. extp->MajorVersion, extp->MinorVersion);
  486. /* Install our own private info structure */
  487. cfi->cmdset_priv = extp;
  488. /* Apply cfi device specific fixups */
  489. cfi_fixup(mtd, cfi_fixup_table);
  490. #ifdef DEBUG_CFI_FEATURES
  491. /* Tell the user about it in lots of lovely detail */
  492. cfi_tell_features(extp);
  493. #endif
  494. bootloc = extp->TopBottom;
  495. if ((bootloc < 2) || (bootloc > 5)) {
  496. printk(KERN_WARNING "%s: CFI contains unrecognised boot "
  497. "bank location (%d). Assuming bottom.\n",
  498. map->name, bootloc);
  499. bootloc = 2;
  500. }
  501. if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
  502. printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
  503. for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
  504. int j = (cfi->cfiq->NumEraseRegions-1)-i;
  505. __u32 swap;
  506. swap = cfi->cfiq->EraseRegionInfo[i];
  507. cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
  508. cfi->cfiq->EraseRegionInfo[j] = swap;
  509. }
  510. }
  511. /* Set the default CFI lock/unlock addresses */
  512. cfi->addr_unlock1 = 0x555;
  513. cfi->addr_unlock2 = 0x2aa;
  514. }
  515. cfi_fixup(mtd, cfi_nopri_fixup_table);
  516. if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
  517. kfree(mtd);
  518. return NULL;
  519. }
  520. } /* CFI mode */
  521. else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
  522. /* Apply jedec specific fixups */
  523. cfi_fixup(mtd, jedec_fixup_table);
  524. }
  525. /* Apply generic fixups */
  526. cfi_fixup(mtd, fixup_table);
  527. for (i=0; i< cfi->numchips; i++) {
  528. cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
  529. cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
  530. cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
  531. cfi->chips[i].ref_point_counter = 0;
  532. init_waitqueue_head(&(cfi->chips[i].wq));
  533. }
  534. map->fldrv = &cfi_amdstd_chipdrv;
  535. return cfi_amdstd_setup(mtd);
  536. }
  537. struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
  538. struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
  539. EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
  540. EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
  541. EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
  542. static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
  543. {
  544. struct map_info *map = mtd->priv;
  545. struct cfi_private *cfi = map->fldrv_priv;
  546. unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
  547. unsigned long offset = 0;
  548. int i,j;
  549. printk(KERN_NOTICE "number of %s chips: %d\n",
  550. (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
  551. /* Select the correct geometry setup */
  552. mtd->size = devsize * cfi->numchips;
  553. mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
  554. mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
  555. * mtd->numeraseregions, GFP_KERNEL);
  556. if (!mtd->eraseregions) {
  557. printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
  558. goto setup_err;
  559. }
  560. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  561. unsigned long ernum, ersize;
  562. ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
  563. ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
  564. if (mtd->erasesize < ersize) {
  565. mtd->erasesize = ersize;
  566. }
  567. for (j=0; j<cfi->numchips; j++) {
  568. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
  569. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
  570. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
  571. }
  572. offset += (ersize * ernum);
  573. }
  574. if (offset != devsize) {
  575. /* Argh */
  576. printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
  577. goto setup_err;
  578. }
  579. __module_get(THIS_MODULE);
  580. register_reboot_notifier(&mtd->reboot_notifier);
  581. return mtd;
  582. setup_err:
  583. kfree(mtd->eraseregions);
  584. kfree(mtd);
  585. kfree(cfi->cmdset_priv);
  586. kfree(cfi->cfiq);
  587. return NULL;
  588. }
  589. /*
  590. * Return true if the chip is ready.
  591. *
  592. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  593. * non-suspended sector) and is indicated by no toggle bits toggling.
  594. *
  595. * Note that anything more complicated than checking if no bits are toggling
  596. * (including checking DQ5 for an error status) is tricky to get working
  597. * correctly and is therefore not done (particularly with interleaved chips
  598. * as each chip must be checked independently of the others).
  599. */
  600. static int __xipram chip_ready(struct map_info *map, unsigned long addr)
  601. {
  602. map_word d, t;
  603. d = map_read(map, addr);
  604. t = map_read(map, addr);
  605. return map_word_equal(map, d, t);
  606. }
  607. /*
  608. * Return true if the chip is ready and has the correct value.
  609. *
  610. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  611. * non-suspended sector) and it is indicated by no bits toggling.
  612. *
  613. * Error are indicated by toggling bits or bits held with the wrong value,
  614. * or with bits toggling.
  615. *
  616. * Note that anything more complicated than checking if no bits are toggling
  617. * (including checking DQ5 for an error status) is tricky to get working
  618. * correctly and is therefore not done (particularly with interleaved chips
  619. * as each chip must be checked independently of the others).
  620. *
  621. */
  622. static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
  623. {
  624. map_word oldd, curd;
  625. oldd = map_read(map, addr);
  626. curd = map_read(map, addr);
  627. return map_word_equal(map, oldd, curd) &&
  628. map_word_equal(map, curd, expected);
  629. }
  630. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  631. {
  632. DECLARE_WAITQUEUE(wait, current);
  633. struct cfi_private *cfi = map->fldrv_priv;
  634. unsigned long timeo;
  635. struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
  636. resettime:
  637. timeo = jiffies + HZ;
  638. retry:
  639. switch (chip->state) {
  640. case FL_STATUS:
  641. for (;;) {
  642. if (chip_ready(map, adr))
  643. break;
  644. if (time_after(jiffies, timeo)) {
  645. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  646. return -EIO;
  647. }
  648. mutex_unlock(&chip->mutex);
  649. cfi_udelay(1);
  650. mutex_lock(&chip->mutex);
  651. /* Someone else might have been playing with it. */
  652. goto retry;
  653. }
  654. case FL_READY:
  655. case FL_CFI_QUERY:
  656. case FL_JEDEC_QUERY:
  657. return 0;
  658. case FL_ERASING:
  659. if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
  660. !(mode == FL_READY || mode == FL_POINT ||
  661. (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
  662. goto sleep;
  663. /* We could check to see if we're trying to access the sector
  664. * that is currently being erased. However, no user will try
  665. * anything like that so we just wait for the timeout. */
  666. /* Erase suspend */
  667. /* It's harmless to issue the Erase-Suspend and Erase-Resume
  668. * commands when the erase algorithm isn't in progress. */
  669. map_write(map, CMD(0xB0), chip->in_progress_block_addr);
  670. chip->oldstate = FL_ERASING;
  671. chip->state = FL_ERASE_SUSPENDING;
  672. chip->erase_suspended = 1;
  673. for (;;) {
  674. if (chip_ready(map, adr))
  675. break;
  676. if (time_after(jiffies, timeo)) {
  677. /* Should have suspended the erase by now.
  678. * Send an Erase-Resume command as either
  679. * there was an error (so leave the erase
  680. * routine to recover from it) or we trying to
  681. * use the erase-in-progress sector. */
  682. put_chip(map, chip, adr);
  683. printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
  684. return -EIO;
  685. }
  686. mutex_unlock(&chip->mutex);
  687. cfi_udelay(1);
  688. mutex_lock(&chip->mutex);
  689. /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
  690. So we can just loop here. */
  691. }
  692. chip->state = FL_READY;
  693. return 0;
  694. case FL_XIP_WHILE_ERASING:
  695. if (mode != FL_READY && mode != FL_POINT &&
  696. (!cfip || !(cfip->EraseSuspend&2)))
  697. goto sleep;
  698. chip->oldstate = chip->state;
  699. chip->state = FL_READY;
  700. return 0;
  701. case FL_SHUTDOWN:
  702. /* The machine is rebooting */
  703. return -EIO;
  704. case FL_POINT:
  705. /* Only if there's no operation suspended... */
  706. if (mode == FL_READY && chip->oldstate == FL_READY)
  707. return 0;
  708. default:
  709. sleep:
  710. set_current_state(TASK_UNINTERRUPTIBLE);
  711. add_wait_queue(&chip->wq, &wait);
  712. mutex_unlock(&chip->mutex);
  713. schedule();
  714. remove_wait_queue(&chip->wq, &wait);
  715. mutex_lock(&chip->mutex);
  716. goto resettime;
  717. }
  718. }
  719. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
  720. {
  721. struct cfi_private *cfi = map->fldrv_priv;
  722. switch(chip->oldstate) {
  723. case FL_ERASING:
  724. cfi_fixup_m29ew_erase_suspend(map,
  725. chip->in_progress_block_addr);
  726. map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
  727. cfi_fixup_m29ew_delay_after_resume(cfi);
  728. chip->oldstate = FL_READY;
  729. chip->state = FL_ERASING;
  730. break;
  731. case FL_XIP_WHILE_ERASING:
  732. chip->state = chip->oldstate;
  733. chip->oldstate = FL_READY;
  734. break;
  735. case FL_READY:
  736. case FL_STATUS:
  737. break;
  738. default:
  739. printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
  740. }
  741. wake_up(&chip->wq);
  742. }
  743. #ifdef CONFIG_MTD_XIP
  744. /*
  745. * No interrupt what so ever can be serviced while the flash isn't in array
  746. * mode. This is ensured by the xip_disable() and xip_enable() functions
  747. * enclosing any code path where the flash is known not to be in array mode.
  748. * And within a XIP disabled code path, only functions marked with __xipram
  749. * may be called and nothing else (it's a good thing to inspect generated
  750. * assembly to make sure inline functions were actually inlined and that gcc
  751. * didn't emit calls to its own support functions). Also configuring MTD CFI
  752. * support to a single buswidth and a single interleave is also recommended.
  753. */
  754. static void xip_disable(struct map_info *map, struct flchip *chip,
  755. unsigned long adr)
  756. {
  757. /* TODO: chips with no XIP use should ignore and return */
  758. (void) map_read(map, adr); /* ensure mmu mapping is up to date */
  759. local_irq_disable();
  760. }
  761. static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
  762. unsigned long adr)
  763. {
  764. struct cfi_private *cfi = map->fldrv_priv;
  765. if (chip->state != FL_POINT && chip->state != FL_READY) {
  766. map_write(map, CMD(0xf0), adr);
  767. chip->state = FL_READY;
  768. }
  769. (void) map_read(map, adr);
  770. xip_iprefetch();
  771. local_irq_enable();
  772. }
  773. /*
  774. * When a delay is required for the flash operation to complete, the
  775. * xip_udelay() function is polling for both the given timeout and pending
  776. * (but still masked) hardware interrupts. Whenever there is an interrupt
  777. * pending then the flash erase operation is suspended, array mode restored
  778. * and interrupts unmasked. Task scheduling might also happen at that
  779. * point. The CPU eventually returns from the interrupt or the call to
  780. * schedule() and the suspended flash operation is resumed for the remaining
  781. * of the delay period.
  782. *
  783. * Warning: this function _will_ fool interrupt latency tracing tools.
  784. */
  785. static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
  786. unsigned long adr, int usec)
  787. {
  788. struct cfi_private *cfi = map->fldrv_priv;
  789. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  790. map_word status, OK = CMD(0x80);
  791. unsigned long suspended, start = xip_currtime();
  792. flstate_t oldstate;
  793. do {
  794. cpu_relax();
  795. if (xip_irqpending() && extp &&
  796. ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
  797. (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
  798. /*
  799. * Let's suspend the erase operation when supported.
  800. * Note that we currently don't try to suspend
  801. * interleaved chips if there is already another
  802. * operation suspended (imagine what happens
  803. * when one chip was already done with the current
  804. * operation while another chip suspended it, then
  805. * we resume the whole thing at once). Yes, it
  806. * can happen!
  807. */
  808. map_write(map, CMD(0xb0), adr);
  809. usec -= xip_elapsed_since(start);
  810. suspended = xip_currtime();
  811. do {
  812. if (xip_elapsed_since(suspended) > 100000) {
  813. /*
  814. * The chip doesn't want to suspend
  815. * after waiting for 100 msecs.
  816. * This is a critical error but there
  817. * is not much we can do here.
  818. */
  819. return;
  820. }
  821. status = map_read(map, adr);
  822. } while (!map_word_andequal(map, status, OK, OK));
  823. /* Suspend succeeded */
  824. oldstate = chip->state;
  825. if (!map_word_bitsset(map, status, CMD(0x40)))
  826. break;
  827. chip->state = FL_XIP_WHILE_ERASING;
  828. chip->erase_suspended = 1;
  829. map_write(map, CMD(0xf0), adr);
  830. (void) map_read(map, adr);
  831. xip_iprefetch();
  832. local_irq_enable();
  833. mutex_unlock(&chip->mutex);
  834. xip_iprefetch();
  835. cond_resched();
  836. /*
  837. * We're back. However someone else might have
  838. * decided to go write to the chip if we are in
  839. * a suspended erase state. If so let's wait
  840. * until it's done.
  841. */
  842. mutex_lock(&chip->mutex);
  843. while (chip->state != FL_XIP_WHILE_ERASING) {
  844. DECLARE_WAITQUEUE(wait, current);
  845. set_current_state(TASK_UNINTERRUPTIBLE);
  846. add_wait_queue(&chip->wq, &wait);
  847. mutex_unlock(&chip->mutex);
  848. schedule();
  849. remove_wait_queue(&chip->wq, &wait);
  850. mutex_lock(&chip->mutex);
  851. }
  852. /* Disallow XIP again */
  853. local_irq_disable();
  854. /* Correct Erase Suspend Hangups for M29EW */
  855. cfi_fixup_m29ew_erase_suspend(map, adr);
  856. /* Resume the write or erase operation */
  857. map_write(map, cfi->sector_erase_cmd, adr);
  858. chip->state = oldstate;
  859. start = xip_currtime();
  860. } else if (usec >= 1000000/HZ) {
  861. /*
  862. * Try to save on CPU power when waiting delay
  863. * is at least a system timer tick period.
  864. * No need to be extremely accurate here.
  865. */
  866. xip_cpu_idle();
  867. }
  868. status = map_read(map, adr);
  869. } while (!map_word_andequal(map, status, OK, OK)
  870. && xip_elapsed_since(start) < usec);
  871. }
  872. #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
  873. /*
  874. * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
  875. * the flash is actively programming or erasing since we have to poll for
  876. * the operation to complete anyway. We can't do that in a generic way with
  877. * a XIP setup so do it before the actual flash operation in this case
  878. * and stub it out from INVALIDATE_CACHE_UDELAY.
  879. */
  880. #define XIP_INVAL_CACHED_RANGE(map, from, size) \
  881. INVALIDATE_CACHED_RANGE(map, from, size)
  882. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  883. UDELAY(map, chip, adr, usec)
  884. /*
  885. * Extra notes:
  886. *
  887. * Activating this XIP support changes the way the code works a bit. For
  888. * example the code to suspend the current process when concurrent access
  889. * happens is never executed because xip_udelay() will always return with the
  890. * same chip state as it was entered with. This is why there is no care for
  891. * the presence of add_wait_queue() or schedule() calls from within a couple
  892. * xip_disable()'d areas of code, like in do_erase_oneblock for example.
  893. * The queueing and scheduling are always happening within xip_udelay().
  894. *
  895. * Similarly, get_chip() and put_chip() just happen to always be executed
  896. * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
  897. * is in array mode, therefore never executing many cases therein and not
  898. * causing any problem with XIP.
  899. */
  900. #else
  901. #define xip_disable(map, chip, adr)
  902. #define xip_enable(map, chip, adr)
  903. #define XIP_INVAL_CACHED_RANGE(x...)
  904. #define UDELAY(map, chip, adr, usec) \
  905. do { \
  906. mutex_unlock(&chip->mutex); \
  907. cfi_udelay(usec); \
  908. mutex_lock(&chip->mutex); \
  909. } while (0)
  910. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  911. do { \
  912. mutex_unlock(&chip->mutex); \
  913. INVALIDATE_CACHED_RANGE(map, adr, len); \
  914. cfi_udelay(usec); \
  915. mutex_lock(&chip->mutex); \
  916. } while (0)
  917. #endif
  918. static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  919. {
  920. unsigned long cmd_addr;
  921. struct cfi_private *cfi = map->fldrv_priv;
  922. int ret;
  923. adr += chip->start;
  924. /* Ensure cmd read/writes are aligned. */
  925. cmd_addr = adr & ~(map_bankwidth(map)-1);
  926. mutex_lock(&chip->mutex);
  927. ret = get_chip(map, chip, cmd_addr, FL_READY);
  928. if (ret) {
  929. mutex_unlock(&chip->mutex);
  930. return ret;
  931. }
  932. if (chip->state != FL_POINT && chip->state != FL_READY) {
  933. map_write(map, CMD(0xf0), cmd_addr);
  934. chip->state = FL_READY;
  935. }
  936. map_copy_from(map, buf, adr, len);
  937. put_chip(map, chip, cmd_addr);
  938. mutex_unlock(&chip->mutex);
  939. return 0;
  940. }
  941. static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  942. {
  943. struct map_info *map = mtd->priv;
  944. struct cfi_private *cfi = map->fldrv_priv;
  945. unsigned long ofs;
  946. int chipnum;
  947. int ret = 0;
  948. /* ofs: offset within the first chip that the first read should start */
  949. chipnum = (from >> cfi->chipshift);
  950. ofs = from - (chipnum << cfi->chipshift);
  951. while (len) {
  952. unsigned long thislen;
  953. if (chipnum >= cfi->numchips)
  954. break;
  955. if ((len + ofs -1) >> cfi->chipshift)
  956. thislen = (1<<cfi->chipshift) - ofs;
  957. else
  958. thislen = len;
  959. ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  960. if (ret)
  961. break;
  962. *retlen += thislen;
  963. len -= thislen;
  964. buf += thislen;
  965. ofs = 0;
  966. chipnum++;
  967. }
  968. return ret;
  969. }
  970. static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  971. {
  972. DECLARE_WAITQUEUE(wait, current);
  973. unsigned long timeo = jiffies + HZ;
  974. struct cfi_private *cfi = map->fldrv_priv;
  975. retry:
  976. mutex_lock(&chip->mutex);
  977. if (chip->state != FL_READY){
  978. set_current_state(TASK_UNINTERRUPTIBLE);
  979. add_wait_queue(&chip->wq, &wait);
  980. mutex_unlock(&chip->mutex);
  981. schedule();
  982. remove_wait_queue(&chip->wq, &wait);
  983. timeo = jiffies + HZ;
  984. goto retry;
  985. }
  986. adr += chip->start;
  987. chip->state = FL_READY;
  988. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  989. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  990. cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  991. map_copy_from(map, buf, adr, len);
  992. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  993. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  994. cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  995. cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  996. wake_up(&chip->wq);
  997. mutex_unlock(&chip->mutex);
  998. return 0;
  999. }
  1000. static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  1001. {
  1002. struct map_info *map = mtd->priv;
  1003. struct cfi_private *cfi = map->fldrv_priv;
  1004. unsigned long ofs;
  1005. int chipnum;
  1006. int ret = 0;
  1007. /* ofs: offset within the first chip that the first read should start */
  1008. /* 8 secsi bytes per chip */
  1009. chipnum=from>>3;
  1010. ofs=from & 7;
  1011. while (len) {
  1012. unsigned long thislen;
  1013. if (chipnum >= cfi->numchips)
  1014. break;
  1015. if ((len + ofs -1) >> 3)
  1016. thislen = (1<<3) - ofs;
  1017. else
  1018. thislen = len;
  1019. ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  1020. if (ret)
  1021. break;
  1022. *retlen += thislen;
  1023. len -= thislen;
  1024. buf += thislen;
  1025. ofs = 0;
  1026. chipnum++;
  1027. }
  1028. return ret;
  1029. }
  1030. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
  1031. {
  1032. struct cfi_private *cfi = map->fldrv_priv;
  1033. unsigned long timeo = jiffies + HZ;
  1034. /*
  1035. * We use a 1ms + 1 jiffies generic timeout for writes (most devices
  1036. * have a max write time of a few hundreds usec). However, we should
  1037. * use the maximum timeout value given by the chip at probe time
  1038. * instead. Unfortunately, struct flchip does have a field for
  1039. * maximum timeout, only for typical which can be far too short
  1040. * depending of the conditions. The ' + 1' is to avoid having a
  1041. * timeout of 0 jiffies if HZ is smaller than 1000.
  1042. */
  1043. unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
  1044. int ret = 0;
  1045. map_word oldd;
  1046. int retry_cnt = 0;
  1047. adr += chip->start;
  1048. mutex_lock(&chip->mutex);
  1049. ret = get_chip(map, chip, adr, FL_WRITING);
  1050. if (ret) {
  1051. mutex_unlock(&chip->mutex);
  1052. return ret;
  1053. }
  1054. pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1055. __func__, adr, datum.x[0] );
  1056. /*
  1057. * Check for a NOP for the case when the datum to write is already
  1058. * present - it saves time and works around buggy chips that corrupt
  1059. * data at other locations when 0xff is written to a location that
  1060. * already contains 0xff.
  1061. */
  1062. oldd = map_read(map, adr);
  1063. if (map_word_equal(map, oldd, datum)) {
  1064. pr_debug("MTD %s(): NOP\n",
  1065. __func__);
  1066. goto op_done;
  1067. }
  1068. XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
  1069. ENABLE_VPP(map);
  1070. xip_disable(map, chip, adr);
  1071. retry:
  1072. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1073. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1074. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1075. map_write(map, datum, adr);
  1076. chip->state = FL_WRITING;
  1077. INVALIDATE_CACHE_UDELAY(map, chip,
  1078. adr, map_bankwidth(map),
  1079. chip->word_write_time);
  1080. /* See comment above for timeout value. */
  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. mutex_unlock(&chip->mutex);
  1089. schedule();
  1090. remove_wait_queue(&chip->wq, &wait);
  1091. timeo = jiffies + (HZ / 2); /* FIXME */
  1092. mutex_lock(&chip->mutex);
  1093. continue;
  1094. }
  1095. if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
  1096. xip_enable(map, chip, adr);
  1097. printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
  1098. xip_disable(map, chip, adr);
  1099. break;
  1100. }
  1101. if (chip_ready(map, adr))
  1102. break;
  1103. /* Latency issues. Drop the lock, wait a while and retry */
  1104. UDELAY(map, chip, adr, 1);
  1105. }
  1106. /* Did we succeed? */
  1107. if (!chip_good(map, adr, datum)) {
  1108. /* reset on all failures. */
  1109. map_write( map, CMD(0xF0), chip->start );
  1110. /* FIXME - should have reset delay before continuing */
  1111. if (++retry_cnt <= MAX_WORD_RETRIES)
  1112. goto retry;
  1113. ret = -EIO;
  1114. }
  1115. xip_enable(map, chip, adr);
  1116. op_done:
  1117. chip->state = FL_READY;
  1118. DISABLE_VPP(map);
  1119. put_chip(map, chip, adr);
  1120. mutex_unlock(&chip->mutex);
  1121. return ret;
  1122. }
  1123. static int cfi_amdstd_write_words(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 ret = 0;
  1129. int chipnum;
  1130. unsigned long ofs, chipstart;
  1131. DECLARE_WAITQUEUE(wait, current);
  1132. chipnum = to >> cfi->chipshift;
  1133. ofs = to - (chipnum << cfi->chipshift);
  1134. chipstart = cfi->chips[chipnum].start;
  1135. /* If it's not bus-aligned, do the first byte write */
  1136. if (ofs & (map_bankwidth(map)-1)) {
  1137. unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
  1138. int i = ofs - bus_ofs;
  1139. int n = 0;
  1140. map_word tmp_buf;
  1141. retry:
  1142. mutex_lock(&cfi->chips[chipnum].mutex);
  1143. if (cfi->chips[chipnum].state != FL_READY) {
  1144. set_current_state(TASK_UNINTERRUPTIBLE);
  1145. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1146. mutex_unlock(&cfi->chips[chipnum].mutex);
  1147. schedule();
  1148. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1149. goto retry;
  1150. }
  1151. /* Load 'tmp_buf' with old contents of flash */
  1152. tmp_buf = map_read(map, bus_ofs+chipstart);
  1153. mutex_unlock(&cfi->chips[chipnum].mutex);
  1154. /* Number of bytes to copy from buffer */
  1155. n = min_t(int, len, map_bankwidth(map)-i);
  1156. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1157. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1158. bus_ofs, tmp_buf);
  1159. if (ret)
  1160. return ret;
  1161. ofs += n;
  1162. buf += n;
  1163. (*retlen) += n;
  1164. len -= n;
  1165. if (ofs >> cfi->chipshift) {
  1166. chipnum ++;
  1167. ofs = 0;
  1168. if (chipnum == cfi->numchips)
  1169. return 0;
  1170. }
  1171. }
  1172. /* We are now aligned, write as much as possible */
  1173. while(len >= map_bankwidth(map)) {
  1174. map_word datum;
  1175. datum = map_word_load(map, buf);
  1176. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1177. ofs, datum);
  1178. if (ret)
  1179. return ret;
  1180. ofs += map_bankwidth(map);
  1181. buf += map_bankwidth(map);
  1182. (*retlen) += map_bankwidth(map);
  1183. len -= map_bankwidth(map);
  1184. if (ofs >> cfi->chipshift) {
  1185. chipnum ++;
  1186. ofs = 0;
  1187. if (chipnum == cfi->numchips)
  1188. return 0;
  1189. chipstart = cfi->chips[chipnum].start;
  1190. }
  1191. }
  1192. /* Write the trailing bytes if any */
  1193. if (len & (map_bankwidth(map)-1)) {
  1194. map_word tmp_buf;
  1195. retry1:
  1196. mutex_lock(&cfi->chips[chipnum].mutex);
  1197. if (cfi->chips[chipnum].state != FL_READY) {
  1198. set_current_state(TASK_UNINTERRUPTIBLE);
  1199. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1200. mutex_unlock(&cfi->chips[chipnum].mutex);
  1201. schedule();
  1202. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1203. goto retry1;
  1204. }
  1205. tmp_buf = map_read(map, ofs + chipstart);
  1206. mutex_unlock(&cfi->chips[chipnum].mutex);
  1207. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1208. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1209. ofs, tmp_buf);
  1210. if (ret)
  1211. return ret;
  1212. (*retlen) += len;
  1213. }
  1214. return 0;
  1215. }
  1216. /*
  1217. * FIXME: interleaved mode not tested, and probably not supported!
  1218. */
  1219. static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
  1220. unsigned long adr, const u_char *buf,
  1221. int len)
  1222. {
  1223. struct cfi_private *cfi = map->fldrv_priv;
  1224. unsigned long timeo = jiffies + HZ;
  1225. /* see comments in do_write_oneword() regarding uWriteTimeo. */
  1226. unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
  1227. int ret = -EIO;
  1228. unsigned long cmd_adr;
  1229. int z, words;
  1230. map_word datum;
  1231. adr += chip->start;
  1232. cmd_adr = adr;
  1233. mutex_lock(&chip->mutex);
  1234. ret = get_chip(map, chip, adr, FL_WRITING);
  1235. if (ret) {
  1236. mutex_unlock(&chip->mutex);
  1237. return ret;
  1238. }
  1239. datum = map_word_load(map, buf);
  1240. pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1241. __func__, adr, datum.x[0] );
  1242. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1243. ENABLE_VPP(map);
  1244. xip_disable(map, chip, cmd_adr);
  1245. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1246. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1247. /* Write Buffer Load */
  1248. map_write(map, CMD(0x25), cmd_adr);
  1249. chip->state = FL_WRITING_TO_BUFFER;
  1250. /* Write length of data to come */
  1251. words = len / map_bankwidth(map);
  1252. map_write(map, CMD(words - 1), cmd_adr);
  1253. /* Write data */
  1254. z = 0;
  1255. while(z < words * map_bankwidth(map)) {
  1256. datum = map_word_load(map, buf);
  1257. map_write(map, datum, adr + z);
  1258. z += map_bankwidth(map);
  1259. buf += map_bankwidth(map);
  1260. }
  1261. z -= map_bankwidth(map);
  1262. adr += z;
  1263. /* Write Buffer Program Confirm: GO GO GO */
  1264. map_write(map, CMD(0x29), cmd_adr);
  1265. chip->state = FL_WRITING;
  1266. INVALIDATE_CACHE_UDELAY(map, chip,
  1267. adr, map_bankwidth(map),
  1268. chip->word_write_time);
  1269. timeo = jiffies + uWriteTimeout;
  1270. for (;;) {
  1271. if (chip->state != FL_WRITING) {
  1272. /* Someone's suspended the write. Sleep */
  1273. DECLARE_WAITQUEUE(wait, current);
  1274. set_current_state(TASK_UNINTERRUPTIBLE);
  1275. add_wait_queue(&chip->wq, &wait);
  1276. mutex_unlock(&chip->mutex);
  1277. schedule();
  1278. remove_wait_queue(&chip->wq, &wait);
  1279. timeo = jiffies + (HZ / 2); /* FIXME */
  1280. mutex_lock(&chip->mutex);
  1281. continue;
  1282. }
  1283. if (time_after(jiffies, timeo) && !chip_ready(map, adr))
  1284. break;
  1285. if (chip_ready(map, adr)) {
  1286. xip_enable(map, chip, adr);
  1287. goto op_done;
  1288. }
  1289. /* Latency issues. Drop the lock, wait a while and retry */
  1290. UDELAY(map, chip, adr, 1);
  1291. }
  1292. /*
  1293. * Recovery from write-buffer programming failures requires
  1294. * the write-to-buffer-reset sequence. Since the last part
  1295. * of the sequence also works as a normal reset, we can run
  1296. * the same commands regardless of why we are here.
  1297. * See e.g.
  1298. * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf
  1299. */
  1300. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1301. cfi->device_type, NULL);
  1302. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1303. cfi->device_type, NULL);
  1304. cfi_send_gen_cmd(0xF0, cfi->addr_unlock1, chip->start, map, cfi,
  1305. cfi->device_type, NULL);
  1306. xip_enable(map, chip, adr);
  1307. /* FIXME - should have reset delay before continuing */
  1308. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1309. __func__ );
  1310. ret = -EIO;
  1311. op_done:
  1312. chip->state = FL_READY;
  1313. DISABLE_VPP(map);
  1314. put_chip(map, chip, adr);
  1315. mutex_unlock(&chip->mutex);
  1316. return ret;
  1317. }
  1318. static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
  1319. size_t *retlen, const u_char *buf)
  1320. {
  1321. struct map_info *map = mtd->priv;
  1322. struct cfi_private *cfi = map->fldrv_priv;
  1323. int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1324. int ret = 0;
  1325. int chipnum;
  1326. unsigned long ofs;
  1327. chipnum = to >> cfi->chipshift;
  1328. ofs = to - (chipnum << cfi->chipshift);
  1329. /* If it's not bus-aligned, do the first word write */
  1330. if (ofs & (map_bankwidth(map)-1)) {
  1331. size_t local_len = (-ofs)&(map_bankwidth(map)-1);
  1332. if (local_len > len)
  1333. local_len = len;
  1334. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1335. local_len, retlen, buf);
  1336. if (ret)
  1337. return ret;
  1338. ofs += local_len;
  1339. buf += local_len;
  1340. len -= local_len;
  1341. if (ofs >> cfi->chipshift) {
  1342. chipnum ++;
  1343. ofs = 0;
  1344. if (chipnum == cfi->numchips)
  1345. return 0;
  1346. }
  1347. }
  1348. /* Write buffer is worth it only if more than one word to write... */
  1349. while (len >= map_bankwidth(map) * 2) {
  1350. /* We must not cross write block boundaries */
  1351. int size = wbufsize - (ofs & (wbufsize-1));
  1352. if (size > len)
  1353. size = len;
  1354. if (size % map_bankwidth(map))
  1355. size -= size % map_bankwidth(map);
  1356. ret = do_write_buffer(map, &cfi->chips[chipnum],
  1357. ofs, buf, size);
  1358. if (ret)
  1359. return ret;
  1360. ofs += size;
  1361. buf += size;
  1362. (*retlen) += size;
  1363. len -= size;
  1364. if (ofs >> cfi->chipshift) {
  1365. chipnum ++;
  1366. ofs = 0;
  1367. if (chipnum == cfi->numchips)
  1368. return 0;
  1369. }
  1370. }
  1371. if (len) {
  1372. size_t retlen_dregs = 0;
  1373. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1374. len, &retlen_dregs, buf);
  1375. *retlen += retlen_dregs;
  1376. return ret;
  1377. }
  1378. return 0;
  1379. }
  1380. /*
  1381. * Wait for the flash chip to become ready to write data
  1382. *
  1383. * This is only called during the panic_write() path. When panic_write()
  1384. * is called, the kernel is in the process of a panic, and will soon be
  1385. * dead. Therefore we don't take any locks, and attempt to get access
  1386. * to the chip as soon as possible.
  1387. */
  1388. static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
  1389. unsigned long adr)
  1390. {
  1391. struct cfi_private *cfi = map->fldrv_priv;
  1392. int retries = 10;
  1393. int i;
  1394. /*
  1395. * If the driver thinks the chip is idle, and no toggle bits
  1396. * are changing, then the chip is actually idle for sure.
  1397. */
  1398. if (chip->state == FL_READY && chip_ready(map, adr))
  1399. return 0;
  1400. /*
  1401. * Try several times to reset the chip and then wait for it
  1402. * to become idle. The upper limit of a few milliseconds of
  1403. * delay isn't a big problem: the kernel is dying anyway. It
  1404. * is more important to save the messages.
  1405. */
  1406. while (retries > 0) {
  1407. const unsigned long timeo = (HZ / 1000) + 1;
  1408. /* send the reset command */
  1409. map_write(map, CMD(0xF0), chip->start);
  1410. /* wait for the chip to become ready */
  1411. for (i = 0; i < jiffies_to_usecs(timeo); i++) {
  1412. if (chip_ready(map, adr))
  1413. return 0;
  1414. udelay(1);
  1415. }
  1416. }
  1417. /* the chip never became ready */
  1418. return -EBUSY;
  1419. }
  1420. /*
  1421. * Write out one word of data to a single flash chip during a kernel panic
  1422. *
  1423. * This is only called during the panic_write() path. When panic_write()
  1424. * is called, the kernel is in the process of a panic, and will soon be
  1425. * dead. Therefore we don't take any locks, and attempt to get access
  1426. * to the chip as soon as possible.
  1427. *
  1428. * The implementation of this routine is intentionally similar to
  1429. * do_write_oneword(), in order to ease code maintenance.
  1430. */
  1431. static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
  1432. unsigned long adr, map_word datum)
  1433. {
  1434. const unsigned long uWriteTimeout = (HZ / 1000) + 1;
  1435. struct cfi_private *cfi = map->fldrv_priv;
  1436. int retry_cnt = 0;
  1437. map_word oldd;
  1438. int ret = 0;
  1439. int i;
  1440. adr += chip->start;
  1441. ret = cfi_amdstd_panic_wait(map, chip, adr);
  1442. if (ret)
  1443. return ret;
  1444. pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
  1445. __func__, adr, datum.x[0]);
  1446. /*
  1447. * Check for a NOP for the case when the datum to write is already
  1448. * present - it saves time and works around buggy chips that corrupt
  1449. * data at other locations when 0xff is written to a location that
  1450. * already contains 0xff.
  1451. */
  1452. oldd = map_read(map, adr);
  1453. if (map_word_equal(map, oldd, datum)) {
  1454. pr_debug("MTD %s(): NOP\n", __func__);
  1455. goto op_done;
  1456. }
  1457. ENABLE_VPP(map);
  1458. retry:
  1459. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1460. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1461. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1462. map_write(map, datum, adr);
  1463. for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
  1464. if (chip_ready(map, adr))
  1465. break;
  1466. udelay(1);
  1467. }
  1468. if (!chip_good(map, adr, datum)) {
  1469. /* reset on all failures. */
  1470. map_write(map, CMD(0xF0), chip->start);
  1471. /* FIXME - should have reset delay before continuing */
  1472. if (++retry_cnt <= MAX_WORD_RETRIES)
  1473. goto retry;
  1474. ret = -EIO;
  1475. }
  1476. op_done:
  1477. DISABLE_VPP(map);
  1478. return ret;
  1479. }
  1480. /*
  1481. * Write out some data during a kernel panic
  1482. *
  1483. * This is used by the mtdoops driver to save the dying messages from a
  1484. * kernel which has panic'd.
  1485. *
  1486. * This routine ignores all of the locking used throughout the rest of the
  1487. * driver, in order to ensure that the data gets written out no matter what
  1488. * state this driver (and the flash chip itself) was in when the kernel crashed.
  1489. *
  1490. * The implementation of this routine is intentionally similar to
  1491. * cfi_amdstd_write_words(), in order to ease code maintenance.
  1492. */
  1493. static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  1494. size_t *retlen, const u_char *buf)
  1495. {
  1496. struct map_info *map = mtd->priv;
  1497. struct cfi_private *cfi = map->fldrv_priv;
  1498. unsigned long ofs, chipstart;
  1499. int ret = 0;
  1500. int chipnum;
  1501. chipnum = to >> cfi->chipshift;
  1502. ofs = to - (chipnum << cfi->chipshift);
  1503. chipstart = cfi->chips[chipnum].start;
  1504. /* If it's not bus aligned, do the first byte write */
  1505. if (ofs & (map_bankwidth(map) - 1)) {
  1506. unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
  1507. int i = ofs - bus_ofs;
  1508. int n = 0;
  1509. map_word tmp_buf;
  1510. ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
  1511. if (ret)
  1512. return ret;
  1513. /* Load 'tmp_buf' with old contents of flash */
  1514. tmp_buf = map_read(map, bus_ofs + chipstart);
  1515. /* Number of bytes to copy from buffer */
  1516. n = min_t(int, len, map_bankwidth(map) - i);
  1517. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1518. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1519. bus_ofs, tmp_buf);
  1520. if (ret)
  1521. return ret;
  1522. ofs += n;
  1523. buf += n;
  1524. (*retlen) += n;
  1525. len -= n;
  1526. if (ofs >> cfi->chipshift) {
  1527. chipnum++;
  1528. ofs = 0;
  1529. if (chipnum == cfi->numchips)
  1530. return 0;
  1531. }
  1532. }
  1533. /* We are now aligned, write as much as possible */
  1534. while (len >= map_bankwidth(map)) {
  1535. map_word datum;
  1536. datum = map_word_load(map, buf);
  1537. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1538. ofs, datum);
  1539. if (ret)
  1540. return ret;
  1541. ofs += map_bankwidth(map);
  1542. buf += map_bankwidth(map);
  1543. (*retlen) += map_bankwidth(map);
  1544. len -= map_bankwidth(map);
  1545. if (ofs >> cfi->chipshift) {
  1546. chipnum++;
  1547. ofs = 0;
  1548. if (chipnum == cfi->numchips)
  1549. return 0;
  1550. chipstart = cfi->chips[chipnum].start;
  1551. }
  1552. }
  1553. /* Write the trailing bytes if any */
  1554. if (len & (map_bankwidth(map) - 1)) {
  1555. map_word tmp_buf;
  1556. ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
  1557. if (ret)
  1558. return ret;
  1559. tmp_buf = map_read(map, ofs + chipstart);
  1560. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1561. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1562. ofs, tmp_buf);
  1563. if (ret)
  1564. return ret;
  1565. (*retlen) += len;
  1566. }
  1567. return 0;
  1568. }
  1569. /*
  1570. * Handle devices with one erase region, that only implement
  1571. * the chip erase command.
  1572. */
  1573. static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
  1574. {
  1575. struct cfi_private *cfi = map->fldrv_priv;
  1576. unsigned long timeo = jiffies + HZ;
  1577. unsigned long int adr;
  1578. DECLARE_WAITQUEUE(wait, current);
  1579. int ret = 0;
  1580. adr = cfi->addr_unlock1;
  1581. mutex_lock(&chip->mutex);
  1582. ret = get_chip(map, chip, adr, FL_WRITING);
  1583. if (ret) {
  1584. mutex_unlock(&chip->mutex);
  1585. return ret;
  1586. }
  1587. pr_debug("MTD %s(): ERASE 0x%.8lx\n",
  1588. __func__, chip->start );
  1589. XIP_INVAL_CACHED_RANGE(map, adr, map->size);
  1590. ENABLE_VPP(map);
  1591. xip_disable(map, chip, adr);
  1592. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1593. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1594. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1595. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1596. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1597. cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1598. chip->state = FL_ERASING;
  1599. chip->erase_suspended = 0;
  1600. chip->in_progress_block_addr = adr;
  1601. INVALIDATE_CACHE_UDELAY(map, chip,
  1602. adr, map->size,
  1603. chip->erase_time*500);
  1604. timeo = jiffies + (HZ*20);
  1605. for (;;) {
  1606. if (chip->state != FL_ERASING) {
  1607. /* Someone's suspended the erase. Sleep */
  1608. set_current_state(TASK_UNINTERRUPTIBLE);
  1609. add_wait_queue(&chip->wq, &wait);
  1610. mutex_unlock(&chip->mutex);
  1611. schedule();
  1612. remove_wait_queue(&chip->wq, &wait);
  1613. mutex_lock(&chip->mutex);
  1614. continue;
  1615. }
  1616. if (chip->erase_suspended) {
  1617. /* This erase was suspended and resumed.
  1618. Adjust the timeout */
  1619. timeo = jiffies + (HZ*20); /* FIXME */
  1620. chip->erase_suspended = 0;
  1621. }
  1622. if (chip_ready(map, adr))
  1623. break;
  1624. if (time_after(jiffies, timeo)) {
  1625. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1626. __func__ );
  1627. break;
  1628. }
  1629. /* Latency issues. Drop the lock, wait a while and retry */
  1630. UDELAY(map, chip, adr, 1000000/HZ);
  1631. }
  1632. /* Did we succeed? */
  1633. if (!chip_good(map, adr, map_word_ff(map))) {
  1634. /* reset on all failures. */
  1635. map_write( map, CMD(0xF0), chip->start );
  1636. /* FIXME - should have reset delay before continuing */
  1637. ret = -EIO;
  1638. }
  1639. chip->state = FL_READY;
  1640. xip_enable(map, chip, adr);
  1641. DISABLE_VPP(map);
  1642. put_chip(map, chip, adr);
  1643. mutex_unlock(&chip->mutex);
  1644. return ret;
  1645. }
  1646. static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
  1647. {
  1648. struct cfi_private *cfi = map->fldrv_priv;
  1649. unsigned long timeo = jiffies + HZ;
  1650. DECLARE_WAITQUEUE(wait, current);
  1651. int ret = 0;
  1652. adr += chip->start;
  1653. mutex_lock(&chip->mutex);
  1654. ret = get_chip(map, chip, adr, FL_ERASING);
  1655. if (ret) {
  1656. mutex_unlock(&chip->mutex);
  1657. return ret;
  1658. }
  1659. pr_debug("MTD %s(): ERASE 0x%.8lx\n",
  1660. __func__, adr );
  1661. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1662. ENABLE_VPP(map);
  1663. xip_disable(map, chip, adr);
  1664. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1665. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1666. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1667. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1668. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1669. map_write(map, cfi->sector_erase_cmd, adr);
  1670. chip->state = FL_ERASING;
  1671. chip->erase_suspended = 0;
  1672. chip->in_progress_block_addr = adr;
  1673. INVALIDATE_CACHE_UDELAY(map, chip,
  1674. adr, len,
  1675. chip->erase_time*500);
  1676. timeo = jiffies + (HZ*20);
  1677. for (;;) {
  1678. if (chip->state != FL_ERASING) {
  1679. /* Someone's suspended the erase. Sleep */
  1680. set_current_state(TASK_UNINTERRUPTIBLE);
  1681. add_wait_queue(&chip->wq, &wait);
  1682. mutex_unlock(&chip->mutex);
  1683. schedule();
  1684. remove_wait_queue(&chip->wq, &wait);
  1685. mutex_lock(&chip->mutex);
  1686. continue;
  1687. }
  1688. if (chip->erase_suspended) {
  1689. /* This erase was suspended and resumed.
  1690. Adjust the timeout */
  1691. timeo = jiffies + (HZ*20); /* FIXME */
  1692. chip->erase_suspended = 0;
  1693. }
  1694. if (chip_ready(map, adr)) {
  1695. xip_enable(map, chip, adr);
  1696. break;
  1697. }
  1698. if (time_after(jiffies, timeo)) {
  1699. xip_enable(map, chip, adr);
  1700. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1701. __func__ );
  1702. break;
  1703. }
  1704. /* Latency issues. Drop the lock, wait a while and retry */
  1705. UDELAY(map, chip, adr, 1000000/HZ);
  1706. }
  1707. /* Did we succeed? */
  1708. if (!chip_good(map, adr, map_word_ff(map))) {
  1709. /* reset on all failures. */
  1710. map_write( map, CMD(0xF0), chip->start );
  1711. /* FIXME - should have reset delay before continuing */
  1712. ret = -EIO;
  1713. }
  1714. chip->state = FL_READY;
  1715. DISABLE_VPP(map);
  1716. put_chip(map, chip, adr);
  1717. mutex_unlock(&chip->mutex);
  1718. return ret;
  1719. }
  1720. static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
  1721. {
  1722. unsigned long ofs, len;
  1723. int ret;
  1724. ofs = instr->addr;
  1725. len = instr->len;
  1726. ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
  1727. if (ret)
  1728. return ret;
  1729. instr->state = MTD_ERASE_DONE;
  1730. mtd_erase_callback(instr);
  1731. return 0;
  1732. }
  1733. static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
  1734. {
  1735. struct map_info *map = mtd->priv;
  1736. struct cfi_private *cfi = map->fldrv_priv;
  1737. int ret = 0;
  1738. if (instr->addr != 0)
  1739. return -EINVAL;
  1740. if (instr->len != mtd->size)
  1741. return -EINVAL;
  1742. ret = do_erase_chip(map, &cfi->chips[0]);
  1743. if (ret)
  1744. return ret;
  1745. instr->state = MTD_ERASE_DONE;
  1746. mtd_erase_callback(instr);
  1747. return 0;
  1748. }
  1749. static int do_atmel_lock(struct map_info *map, struct flchip *chip,
  1750. unsigned long adr, int len, void *thunk)
  1751. {
  1752. struct cfi_private *cfi = map->fldrv_priv;
  1753. int ret;
  1754. mutex_lock(&chip->mutex);
  1755. ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
  1756. if (ret)
  1757. goto out_unlock;
  1758. chip->state = FL_LOCKING;
  1759. pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
  1760. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1761. cfi->device_type, NULL);
  1762. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1763. cfi->device_type, NULL);
  1764. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
  1765. cfi->device_type, NULL);
  1766. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1767. cfi->device_type, NULL);
  1768. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1769. cfi->device_type, NULL);
  1770. map_write(map, CMD(0x40), chip->start + adr);
  1771. chip->state = FL_READY;
  1772. put_chip(map, chip, adr + chip->start);
  1773. ret = 0;
  1774. out_unlock:
  1775. mutex_unlock(&chip->mutex);
  1776. return ret;
  1777. }
  1778. static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
  1779. unsigned long adr, int len, void *thunk)
  1780. {
  1781. struct cfi_private *cfi = map->fldrv_priv;
  1782. int ret;
  1783. mutex_lock(&chip->mutex);
  1784. ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
  1785. if (ret)
  1786. goto out_unlock;
  1787. chip->state = FL_UNLOCKING;
  1788. pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
  1789. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1790. cfi->device_type, NULL);
  1791. map_write(map, CMD(0x70), adr);
  1792. chip->state = FL_READY;
  1793. put_chip(map, chip, adr + chip->start);
  1794. ret = 0;
  1795. out_unlock:
  1796. mutex_unlock(&chip->mutex);
  1797. return ret;
  1798. }
  1799. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1800. {
  1801. return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
  1802. }
  1803. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1804. {
  1805. return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
  1806. }
  1807. static void cfi_amdstd_sync (struct mtd_info *mtd)
  1808. {
  1809. struct map_info *map = mtd->priv;
  1810. struct cfi_private *cfi = map->fldrv_priv;
  1811. int i;
  1812. struct flchip *chip;
  1813. int ret = 0;
  1814. DECLARE_WAITQUEUE(wait, current);
  1815. for (i=0; !ret && i<cfi->numchips; i++) {
  1816. chip = &cfi->chips[i];
  1817. retry:
  1818. mutex_lock(&chip->mutex);
  1819. switch(chip->state) {
  1820. case FL_READY:
  1821. case FL_STATUS:
  1822. case FL_CFI_QUERY:
  1823. case FL_JEDEC_QUERY:
  1824. chip->oldstate = chip->state;
  1825. chip->state = FL_SYNCING;
  1826. /* No need to wake_up() on this state change -
  1827. * as the whole point is that nobody can do anything
  1828. * with the chip now anyway.
  1829. */
  1830. case FL_SYNCING:
  1831. mutex_unlock(&chip->mutex);
  1832. break;
  1833. default:
  1834. /* Not an idle state */
  1835. set_current_state(TASK_UNINTERRUPTIBLE);
  1836. add_wait_queue(&chip->wq, &wait);
  1837. mutex_unlock(&chip->mutex);
  1838. schedule();
  1839. remove_wait_queue(&chip->wq, &wait);
  1840. goto retry;
  1841. }
  1842. }
  1843. /* Unlock the chips again */
  1844. for (i--; i >=0; i--) {
  1845. chip = &cfi->chips[i];
  1846. mutex_lock(&chip->mutex);
  1847. if (chip->state == FL_SYNCING) {
  1848. chip->state = chip->oldstate;
  1849. wake_up(&chip->wq);
  1850. }
  1851. mutex_unlock(&chip->mutex);
  1852. }
  1853. }
  1854. static int cfi_amdstd_suspend(struct mtd_info *mtd)
  1855. {
  1856. struct map_info *map = mtd->priv;
  1857. struct cfi_private *cfi = map->fldrv_priv;
  1858. int i;
  1859. struct flchip *chip;
  1860. int ret = 0;
  1861. for (i=0; !ret && i<cfi->numchips; i++) {
  1862. chip = &cfi->chips[i];
  1863. mutex_lock(&chip->mutex);
  1864. switch(chip->state) {
  1865. case FL_READY:
  1866. case FL_STATUS:
  1867. case FL_CFI_QUERY:
  1868. case FL_JEDEC_QUERY:
  1869. chip->oldstate = chip->state;
  1870. chip->state = FL_PM_SUSPENDED;
  1871. /* No need to wake_up() on this state change -
  1872. * as the whole point is that nobody can do anything
  1873. * with the chip now anyway.
  1874. */
  1875. case FL_PM_SUSPENDED:
  1876. break;
  1877. default:
  1878. ret = -EAGAIN;
  1879. break;
  1880. }
  1881. mutex_unlock(&chip->mutex);
  1882. }
  1883. /* Unlock the chips again */
  1884. if (ret) {
  1885. for (i--; i >=0; i--) {
  1886. chip = &cfi->chips[i];
  1887. mutex_lock(&chip->mutex);
  1888. if (chip->state == FL_PM_SUSPENDED) {
  1889. chip->state = chip->oldstate;
  1890. wake_up(&chip->wq);
  1891. }
  1892. mutex_unlock(&chip->mutex);
  1893. }
  1894. }
  1895. return ret;
  1896. }
  1897. static void cfi_amdstd_resume(struct mtd_info *mtd)
  1898. {
  1899. struct map_info *map = mtd->priv;
  1900. struct cfi_private *cfi = map->fldrv_priv;
  1901. int i;
  1902. struct flchip *chip;
  1903. for (i=0; i<cfi->numchips; i++) {
  1904. chip = &cfi->chips[i];
  1905. mutex_lock(&chip->mutex);
  1906. if (chip->state == FL_PM_SUSPENDED) {
  1907. chip->state = FL_READY;
  1908. map_write(map, CMD(0xF0), chip->start);
  1909. wake_up(&chip->wq);
  1910. }
  1911. else
  1912. printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
  1913. mutex_unlock(&chip->mutex);
  1914. }
  1915. }
  1916. /*
  1917. * Ensure that the flash device is put back into read array mode before
  1918. * unloading the driver or rebooting. On some systems, rebooting while
  1919. * the flash is in query/program/erase mode will prevent the CPU from
  1920. * fetching the bootloader code, requiring a hard reset or power cycle.
  1921. */
  1922. static int cfi_amdstd_reset(struct mtd_info *mtd)
  1923. {
  1924. struct map_info *map = mtd->priv;
  1925. struct cfi_private *cfi = map->fldrv_priv;
  1926. int i, ret;
  1927. struct flchip *chip;
  1928. for (i = 0; i < cfi->numchips; i++) {
  1929. chip = &cfi->chips[i];
  1930. mutex_lock(&chip->mutex);
  1931. ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
  1932. if (!ret) {
  1933. map_write(map, CMD(0xF0), chip->start);
  1934. chip->state = FL_SHUTDOWN;
  1935. put_chip(map, chip, chip->start);
  1936. }
  1937. mutex_unlock(&chip->mutex);
  1938. }
  1939. return 0;
  1940. }
  1941. static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
  1942. void *v)
  1943. {
  1944. struct mtd_info *mtd;
  1945. mtd = container_of(nb, struct mtd_info, reboot_notifier);
  1946. cfi_amdstd_reset(mtd);
  1947. return NOTIFY_DONE;
  1948. }
  1949. static void cfi_amdstd_destroy(struct mtd_info *mtd)
  1950. {
  1951. struct map_info *map = mtd->priv;
  1952. struct cfi_private *cfi = map->fldrv_priv;
  1953. cfi_amdstd_reset(mtd);
  1954. unregister_reboot_notifier(&mtd->reboot_notifier);
  1955. kfree(cfi->cmdset_priv);
  1956. kfree(cfi->cfiq);
  1957. kfree(cfi);
  1958. kfree(mtd->eraseregions);
  1959. }
  1960. MODULE_LICENSE("GPL");
  1961. MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
  1962. MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
  1963. MODULE_ALIAS("cfi_cmdset_0006");
  1964. MODULE_ALIAS("cfi_cmdset_0701");