cfi_cmdset_0002.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924
  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 <asm/io.h>
  27. #include <asm/byteorder.h>
  28. #include <linux/errno.h>
  29. #include <linux/slab.h>
  30. #include <linux/delay.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/reboot.h>
  33. #include <linux/of.h>
  34. #include <linux/of_platform.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_RETRIES 3
  42. #define SST49LF004B 0x0060
  43. #define SST49LF040B 0x0050
  44. #define SST49LF008A 0x005a
  45. #define AT49BV6416 0x00d6
  46. static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  47. static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  48. static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  49. static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
  50. static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
  51. static void cfi_amdstd_sync (struct mtd_info *);
  52. static int cfi_amdstd_suspend (struct mtd_info *);
  53. static void cfi_amdstd_resume (struct mtd_info *);
  54. static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *);
  55. static int cfi_amdstd_get_fact_prot_info(struct mtd_info *, size_t,
  56. size_t *, struct otp_info *);
  57. static int cfi_amdstd_get_user_prot_info(struct mtd_info *, size_t,
  58. size_t *, struct otp_info *);
  59. static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  60. static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *, loff_t, size_t,
  61. size_t *, u_char *);
  62. static int cfi_amdstd_read_user_prot_reg(struct mtd_info *, loff_t, size_t,
  63. size_t *, u_char *);
  64. static int cfi_amdstd_write_user_prot_reg(struct mtd_info *, loff_t, size_t,
  65. size_t *, u_char *);
  66. static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *, loff_t, size_t);
  67. static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  68. size_t *retlen, const u_char *buf);
  69. static void cfi_amdstd_destroy(struct mtd_info *);
  70. struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
  71. static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
  72. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
  73. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
  74. #include "fwh_lock.h"
  75. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  76. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  77. static int cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  78. static int cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  79. static int cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  80. static struct mtd_chip_driver cfi_amdstd_chipdrv = {
  81. .probe = NULL, /* Not usable directly */
  82. .destroy = cfi_amdstd_destroy,
  83. .name = "cfi_cmdset_0002",
  84. .module = THIS_MODULE
  85. };
  86. /* #define DEBUG_CFI_FEATURES */
  87. #ifdef DEBUG_CFI_FEATURES
  88. static void cfi_tell_features(struct cfi_pri_amdstd *extp)
  89. {
  90. const char* erase_suspend[3] = {
  91. "Not supported", "Read only", "Read/write"
  92. };
  93. const char* top_bottom[6] = {
  94. "No WP", "8x8KiB sectors at top & bottom, no WP",
  95. "Bottom boot", "Top boot",
  96. "Uniform, Bottom WP", "Uniform, Top WP"
  97. };
  98. printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
  99. printk(" Address sensitive unlock: %s\n",
  100. (extp->SiliconRevision & 1) ? "Not required" : "Required");
  101. if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
  102. printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
  103. else
  104. printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
  105. if (extp->BlkProt == 0)
  106. printk(" Block protection: Not supported\n");
  107. else
  108. printk(" Block protection: %d sectors per group\n", extp->BlkProt);
  109. printk(" Temporary block unprotect: %s\n",
  110. extp->TmpBlkUnprotect ? "Supported" : "Not supported");
  111. printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
  112. printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
  113. printk(" Burst mode: %s\n",
  114. extp->BurstMode ? "Supported" : "Not supported");
  115. if (extp->PageMode == 0)
  116. printk(" Page mode: Not supported\n");
  117. else
  118. printk(" Page mode: %d word page\n", extp->PageMode << 2);
  119. printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
  120. extp->VppMin >> 4, extp->VppMin & 0xf);
  121. printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
  122. extp->VppMax >> 4, extp->VppMax & 0xf);
  123. if (extp->TopBottom < ARRAY_SIZE(top_bottom))
  124. printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
  125. else
  126. printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
  127. }
  128. #endif
  129. #ifdef AMD_BOOTLOC_BUG
  130. /* Wheee. Bring me the head of someone at AMD. */
  131. static void fixup_amd_bootblock(struct mtd_info *mtd)
  132. {
  133. struct map_info *map = mtd->priv;
  134. struct cfi_private *cfi = map->fldrv_priv;
  135. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  136. __u8 major = extp->MajorVersion;
  137. __u8 minor = extp->MinorVersion;
  138. if (((major << 8) | minor) < 0x3131) {
  139. /* CFI version 1.0 => don't trust bootloc */
  140. pr_debug("%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
  141. map->name, cfi->mfr, cfi->id);
  142. /* AFAICS all 29LV400 with a bottom boot block have a device ID
  143. * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
  144. * These were badly detected as they have the 0x80 bit set
  145. * so treat them as a special case.
  146. */
  147. if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
  148. /* Macronix added CFI to their 2nd generation
  149. * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
  150. * Fujitsu, Spansion, EON, ESI and older Macronix)
  151. * has CFI.
  152. *
  153. * Therefore also check the manufacturer.
  154. * This reduces the risk of false detection due to
  155. * the 8-bit device ID.
  156. */
  157. (cfi->mfr == CFI_MFR_MACRONIX)) {
  158. pr_debug("%s: Macronix MX29LV400C with bottom boot block"
  159. " detected\n", map->name);
  160. extp->TopBottom = 2; /* bottom boot */
  161. } else
  162. if (cfi->id & 0x80) {
  163. printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
  164. extp->TopBottom = 3; /* top boot */
  165. } else {
  166. extp->TopBottom = 2; /* bottom boot */
  167. }
  168. pr_debug("%s: AMD CFI PRI V%c.%c has no boot block field;"
  169. " deduced %s from Device ID\n", map->name, major, minor,
  170. extp->TopBottom == 2 ? "bottom" : "top");
  171. }
  172. }
  173. #endif
  174. static void fixup_use_write_buffers(struct mtd_info *mtd)
  175. {
  176. struct map_info *map = mtd->priv;
  177. struct cfi_private *cfi = map->fldrv_priv;
  178. if (cfi->cfiq->BufWriteTimeoutTyp) {
  179. pr_debug("Using buffer write method\n");
  180. mtd->_write = cfi_amdstd_write_buffers;
  181. }
  182. }
  183. /* Atmel chips don't use the same PRI format as AMD chips */
  184. static void fixup_convert_atmel_pri(struct mtd_info *mtd)
  185. {
  186. struct map_info *map = mtd->priv;
  187. struct cfi_private *cfi = map->fldrv_priv;
  188. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  189. struct cfi_pri_atmel atmel_pri;
  190. memcpy(&atmel_pri, extp, sizeof(atmel_pri));
  191. memset((char *)extp + 5, 0, sizeof(*extp) - 5);
  192. if (atmel_pri.Features & 0x02)
  193. extp->EraseSuspend = 2;
  194. /* Some chips got it backwards... */
  195. if (cfi->id == AT49BV6416) {
  196. if (atmel_pri.BottomBoot)
  197. extp->TopBottom = 3;
  198. else
  199. extp->TopBottom = 2;
  200. } else {
  201. if (atmel_pri.BottomBoot)
  202. extp->TopBottom = 2;
  203. else
  204. extp->TopBottom = 3;
  205. }
  206. /* burst write mode not supported */
  207. cfi->cfiq->BufWriteTimeoutTyp = 0;
  208. cfi->cfiq->BufWriteTimeoutMax = 0;
  209. }
  210. static void fixup_use_secsi(struct mtd_info *mtd)
  211. {
  212. /* Setup for chips with a secsi area */
  213. mtd->_read_user_prot_reg = cfi_amdstd_secsi_read;
  214. mtd->_read_fact_prot_reg = cfi_amdstd_secsi_read;
  215. }
  216. static void fixup_use_erase_chip(struct mtd_info *mtd)
  217. {
  218. struct map_info *map = mtd->priv;
  219. struct cfi_private *cfi = map->fldrv_priv;
  220. if ((cfi->cfiq->NumEraseRegions == 1) &&
  221. ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
  222. mtd->_erase = cfi_amdstd_erase_chip;
  223. }
  224. }
  225. /*
  226. * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
  227. * locked by default.
  228. */
  229. static void fixup_use_atmel_lock(struct mtd_info *mtd)
  230. {
  231. mtd->_lock = cfi_atmel_lock;
  232. mtd->_unlock = cfi_atmel_unlock;
  233. mtd->flags |= MTD_POWERUP_LOCK;
  234. }
  235. static void fixup_old_sst_eraseregion(struct mtd_info *mtd)
  236. {
  237. struct map_info *map = mtd->priv;
  238. struct cfi_private *cfi = map->fldrv_priv;
  239. /*
  240. * These flashes report two separate eraseblock regions based on the
  241. * sector_erase-size and block_erase-size, although they both operate on the
  242. * same memory. This is not allowed according to CFI, so we just pick the
  243. * sector_erase-size.
  244. */
  245. cfi->cfiq->NumEraseRegions = 1;
  246. }
  247. static void fixup_sst39vf(struct mtd_info *mtd)
  248. {
  249. struct map_info *map = mtd->priv;
  250. struct cfi_private *cfi = map->fldrv_priv;
  251. fixup_old_sst_eraseregion(mtd);
  252. cfi->addr_unlock1 = 0x5555;
  253. cfi->addr_unlock2 = 0x2AAA;
  254. }
  255. static void fixup_sst39vf_rev_b(struct mtd_info *mtd)
  256. {
  257. struct map_info *map = mtd->priv;
  258. struct cfi_private *cfi = map->fldrv_priv;
  259. fixup_old_sst_eraseregion(mtd);
  260. cfi->addr_unlock1 = 0x555;
  261. cfi->addr_unlock2 = 0x2AA;
  262. cfi->sector_erase_cmd = CMD(0x50);
  263. }
  264. static void fixup_sst38vf640x_sectorsize(struct mtd_info *mtd)
  265. {
  266. struct map_info *map = mtd->priv;
  267. struct cfi_private *cfi = map->fldrv_priv;
  268. fixup_sst39vf_rev_b(mtd);
  269. /*
  270. * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where
  271. * it should report a size of 8KBytes (0x0020*256).
  272. */
  273. cfi->cfiq->EraseRegionInfo[0] = 0x002003ff;
  274. pr_warn("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n",
  275. mtd->name);
  276. }
  277. static void fixup_s29gl064n_sectors(struct mtd_info *mtd)
  278. {
  279. struct map_info *map = mtd->priv;
  280. struct cfi_private *cfi = map->fldrv_priv;
  281. if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
  282. cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
  283. pr_warn("%s: Bad S29GL064N CFI data; adjust from 64 to 128 sectors\n",
  284. mtd->name);
  285. }
  286. }
  287. static void fixup_s29gl032n_sectors(struct mtd_info *mtd)
  288. {
  289. struct map_info *map = mtd->priv;
  290. struct cfi_private *cfi = map->fldrv_priv;
  291. if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
  292. cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
  293. pr_warn("%s: Bad S29GL032N CFI data; adjust from 127 to 63 sectors\n",
  294. mtd->name);
  295. }
  296. }
  297. static void fixup_s29ns512p_sectors(struct mtd_info *mtd)
  298. {
  299. struct map_info *map = mtd->priv;
  300. struct cfi_private *cfi = map->fldrv_priv;
  301. /*
  302. * S29NS512P flash uses more than 8bits to report number of sectors,
  303. * which is not permitted by CFI.
  304. */
  305. cfi->cfiq->EraseRegionInfo[0] = 0x020001ff;
  306. pr_warn("%s: Bad S29NS512P CFI data; adjust to 512 sectors\n",
  307. mtd->name);
  308. }
  309. /* Used to fix CFI-Tables of chips without Extended Query Tables */
  310. static struct cfi_fixup cfi_nopri_fixup_table[] = {
  311. { CFI_MFR_SST, 0x234a, fixup_sst39vf }, /* SST39VF1602 */
  312. { CFI_MFR_SST, 0x234b, fixup_sst39vf }, /* SST39VF1601 */
  313. { CFI_MFR_SST, 0x235a, fixup_sst39vf }, /* SST39VF3202 */
  314. { CFI_MFR_SST, 0x235b, fixup_sst39vf }, /* SST39VF3201 */
  315. { CFI_MFR_SST, 0x235c, fixup_sst39vf_rev_b }, /* SST39VF3202B */
  316. { CFI_MFR_SST, 0x235d, fixup_sst39vf_rev_b }, /* SST39VF3201B */
  317. { CFI_MFR_SST, 0x236c, fixup_sst39vf_rev_b }, /* SST39VF6402B */
  318. { CFI_MFR_SST, 0x236d, fixup_sst39vf_rev_b }, /* SST39VF6401B */
  319. { 0, 0, NULL }
  320. };
  321. static struct cfi_fixup cfi_fixup_table[] = {
  322. { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
  323. #ifdef AMD_BOOTLOC_BUG
  324. { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock },
  325. { CFI_MFR_AMIC, CFI_ID_ANY, fixup_amd_bootblock },
  326. { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock },
  327. #endif
  328. { CFI_MFR_AMD, 0x0050, fixup_use_secsi },
  329. { CFI_MFR_AMD, 0x0053, fixup_use_secsi },
  330. { CFI_MFR_AMD, 0x0055, fixup_use_secsi },
  331. { CFI_MFR_AMD, 0x0056, fixup_use_secsi },
  332. { CFI_MFR_AMD, 0x005C, fixup_use_secsi },
  333. { CFI_MFR_AMD, 0x005F, fixup_use_secsi },
  334. { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors },
  335. { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors },
  336. { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors },
  337. { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors },
  338. { CFI_MFR_AMD, 0x3f00, fixup_s29ns512p_sectors },
  339. { CFI_MFR_SST, 0x536a, fixup_sst38vf640x_sectorsize }, /* SST38VF6402 */
  340. { CFI_MFR_SST, 0x536b, fixup_sst38vf640x_sectorsize }, /* SST38VF6401 */
  341. { CFI_MFR_SST, 0x536c, fixup_sst38vf640x_sectorsize }, /* SST38VF6404 */
  342. { CFI_MFR_SST, 0x536d, fixup_sst38vf640x_sectorsize }, /* SST38VF6403 */
  343. #if !FORCE_WORD_WRITE
  344. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
  345. #endif
  346. { 0, 0, NULL }
  347. };
  348. static struct cfi_fixup jedec_fixup_table[] = {
  349. { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock },
  350. { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock },
  351. { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock },
  352. { 0, 0, NULL }
  353. };
  354. static struct cfi_fixup fixup_table[] = {
  355. /* The CFI vendor ids and the JEDEC vendor IDs appear
  356. * to be common. It is like the devices id's are as
  357. * well. This table is to pick all cases where
  358. * we know that is the case.
  359. */
  360. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip },
  361. { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock },
  362. { 0, 0, NULL }
  363. };
  364. static void cfi_fixup_major_minor(struct cfi_private *cfi,
  365. struct cfi_pri_amdstd *extp)
  366. {
  367. if (cfi->mfr == CFI_MFR_SAMSUNG) {
  368. if ((extp->MajorVersion == '0' && extp->MinorVersion == '0') ||
  369. (extp->MajorVersion == '3' && extp->MinorVersion == '3')) {
  370. /*
  371. * Samsung K8P2815UQB and K8D6x16UxM chips
  372. * report major=0 / minor=0.
  373. * K8D3x16UxC chips report major=3 / minor=3.
  374. */
  375. printk(KERN_NOTICE " Fixing Samsung's Amd/Fujitsu"
  376. " Extended Query version to 1.%c\n",
  377. extp->MinorVersion);
  378. extp->MajorVersion = '1';
  379. }
  380. }
  381. /*
  382. * SST 38VF640x chips report major=0xFF / minor=0xFF.
  383. */
  384. if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) {
  385. extp->MajorVersion = '1';
  386. extp->MinorVersion = '0';
  387. }
  388. }
  389. static int is_m29ew(struct cfi_private *cfi)
  390. {
  391. if (cfi->mfr == CFI_MFR_INTEL &&
  392. ((cfi->device_type == CFI_DEVICETYPE_X8 && (cfi->id & 0xff) == 0x7e) ||
  393. (cfi->device_type == CFI_DEVICETYPE_X16 && cfi->id == 0x227e)))
  394. return 1;
  395. return 0;
  396. }
  397. /*
  398. * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 20:
  399. * Some revisions of the M29EW suffer from erase suspend hang ups. In
  400. * particular, it can occur when the sequence
  401. * Erase Confirm -> Suspend -> Program -> Resume
  402. * causes a lockup due to internal timing issues. The consequence is that the
  403. * erase cannot be resumed without inserting a dummy command after programming
  404. * and prior to resuming. [...] The work-around is to issue a dummy write cycle
  405. * that writes an F0 command code before the RESUME command.
  406. */
  407. static void cfi_fixup_m29ew_erase_suspend(struct map_info *map,
  408. unsigned long adr)
  409. {
  410. struct cfi_private *cfi = map->fldrv_priv;
  411. /* before resume, insert a dummy 0xF0 cycle for Micron M29EW devices */
  412. if (is_m29ew(cfi))
  413. map_write(map, CMD(0xF0), adr);
  414. }
  415. /*
  416. * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 22:
  417. *
  418. * Some revisions of the M29EW (for example, A1 and A2 step revisions)
  419. * are affected by a problem that could cause a hang up when an ERASE SUSPEND
  420. * command is issued after an ERASE RESUME operation without waiting for a
  421. * minimum delay. The result is that once the ERASE seems to be completed
  422. * (no bits are toggling), the contents of the Flash memory block on which
  423. * the erase was ongoing could be inconsistent with the expected values
  424. * (typically, the array value is stuck to the 0xC0, 0xC4, 0x80, or 0x84
  425. * values), causing a consequent failure of the ERASE operation.
  426. * The occurrence of this issue could be high, especially when file system
  427. * operations on the Flash are intensive. As a result, it is recommended
  428. * that a patch be applied. Intensive file system operations can cause many
  429. * calls to the garbage routine to free Flash space (also by erasing physical
  430. * Flash blocks) and as a result, many consecutive SUSPEND and RESUME
  431. * commands can occur. The problem disappears when a delay is inserted after
  432. * the RESUME command by using the udelay() function available in Linux.
  433. * The DELAY value must be tuned based on the customer's platform.
  434. * The maximum value that fixes the problem in all cases is 500us.
  435. * But, in our experience, a delay of 30 µs to 50 µs is sufficient
  436. * in most cases.
  437. * We have chosen 500µs because this latency is acceptable.
  438. */
  439. static void cfi_fixup_m29ew_delay_after_resume(struct cfi_private *cfi)
  440. {
  441. /*
  442. * Resolving the Delay After Resume Issue see Micron TN-13-07
  443. * Worst case delay must be 500µs but 30-50µs should be ok as well
  444. */
  445. if (is_m29ew(cfi))
  446. cfi_udelay(500);
  447. }
  448. struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
  449. {
  450. struct cfi_private *cfi = map->fldrv_priv;
  451. struct device_node __maybe_unused *np = map->device_node;
  452. struct mtd_info *mtd;
  453. int i;
  454. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  455. if (!mtd)
  456. return NULL;
  457. mtd->priv = map;
  458. mtd->type = MTD_NORFLASH;
  459. /* Fill in the default mtd operations */
  460. mtd->_erase = cfi_amdstd_erase_varsize;
  461. mtd->_write = cfi_amdstd_write_words;
  462. mtd->_read = cfi_amdstd_read;
  463. mtd->_sync = cfi_amdstd_sync;
  464. mtd->_suspend = cfi_amdstd_suspend;
  465. mtd->_resume = cfi_amdstd_resume;
  466. mtd->_read_user_prot_reg = cfi_amdstd_read_user_prot_reg;
  467. mtd->_read_fact_prot_reg = cfi_amdstd_read_fact_prot_reg;
  468. mtd->_get_fact_prot_info = cfi_amdstd_get_fact_prot_info;
  469. mtd->_get_user_prot_info = cfi_amdstd_get_user_prot_info;
  470. mtd->_write_user_prot_reg = cfi_amdstd_write_user_prot_reg;
  471. mtd->_lock_user_prot_reg = cfi_amdstd_lock_user_prot_reg;
  472. mtd->flags = MTD_CAP_NORFLASH;
  473. mtd->name = map->name;
  474. mtd->writesize = 1;
  475. mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  476. pr_debug("MTD %s(): write buffer size %d\n", __func__,
  477. mtd->writebufsize);
  478. mtd->_panic_write = cfi_amdstd_panic_write;
  479. mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
  480. if (cfi->cfi_mode==CFI_MODE_CFI){
  481. unsigned char bootloc;
  482. __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
  483. struct cfi_pri_amdstd *extp;
  484. extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
  485. if (extp) {
  486. /*
  487. * It's a real CFI chip, not one for which the probe
  488. * routine faked a CFI structure.
  489. */
  490. cfi_fixup_major_minor(cfi, extp);
  491. /*
  492. * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4, 1.5
  493. * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19
  494. * http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
  495. * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
  496. * http://www.spansion.com/Support/Datasheets/S29GL_128S_01GS_00_02_e.pdf
  497. */
  498. if (extp->MajorVersion != '1' ||
  499. (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '5'))) {
  500. printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
  501. "version %c.%c (%#02x/%#02x).\n",
  502. extp->MajorVersion, extp->MinorVersion,
  503. extp->MajorVersion, extp->MinorVersion);
  504. kfree(extp);
  505. kfree(mtd);
  506. return NULL;
  507. }
  508. printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n",
  509. extp->MajorVersion, extp->MinorVersion);
  510. /* Install our own private info structure */
  511. cfi->cmdset_priv = extp;
  512. /* Apply cfi device specific fixups */
  513. cfi_fixup(mtd, cfi_fixup_table);
  514. #ifdef DEBUG_CFI_FEATURES
  515. /* Tell the user about it in lots of lovely detail */
  516. cfi_tell_features(extp);
  517. #endif
  518. #ifdef CONFIG_OF
  519. if (np && of_property_read_bool(
  520. np, "use-advanced-sector-protection")
  521. && extp->BlkProtUnprot == 8) {
  522. printk(KERN_INFO " Advanced Sector Protection (PPB Locking) supported\n");
  523. mtd->_lock = cfi_ppb_lock;
  524. mtd->_unlock = cfi_ppb_unlock;
  525. mtd->_is_locked = cfi_ppb_is_locked;
  526. }
  527. #endif
  528. bootloc = extp->TopBottom;
  529. if ((bootloc < 2) || (bootloc > 5)) {
  530. printk(KERN_WARNING "%s: CFI contains unrecognised boot "
  531. "bank location (%d). Assuming bottom.\n",
  532. map->name, bootloc);
  533. bootloc = 2;
  534. }
  535. if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
  536. printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
  537. for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
  538. int j = (cfi->cfiq->NumEraseRegions-1)-i;
  539. swap(cfi->cfiq->EraseRegionInfo[i],
  540. cfi->cfiq->EraseRegionInfo[j]);
  541. }
  542. }
  543. /* Set the default CFI lock/unlock addresses */
  544. cfi->addr_unlock1 = 0x555;
  545. cfi->addr_unlock2 = 0x2aa;
  546. }
  547. cfi_fixup(mtd, cfi_nopri_fixup_table);
  548. if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
  549. kfree(mtd);
  550. return NULL;
  551. }
  552. } /* CFI mode */
  553. else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
  554. /* Apply jedec specific fixups */
  555. cfi_fixup(mtd, jedec_fixup_table);
  556. }
  557. /* Apply generic fixups */
  558. cfi_fixup(mtd, fixup_table);
  559. for (i=0; i< cfi->numchips; i++) {
  560. cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
  561. cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
  562. cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
  563. /*
  564. * First calculate the timeout max according to timeout field
  565. * of struct cfi_ident that probed from chip's CFI aera, if
  566. * available. Specify a minimum of 2000us, in case the CFI data
  567. * is wrong.
  568. */
  569. if (cfi->cfiq->BufWriteTimeoutTyp &&
  570. cfi->cfiq->BufWriteTimeoutMax)
  571. cfi->chips[i].buffer_write_time_max =
  572. 1 << (cfi->cfiq->BufWriteTimeoutTyp +
  573. cfi->cfiq->BufWriteTimeoutMax);
  574. else
  575. cfi->chips[i].buffer_write_time_max = 0;
  576. cfi->chips[i].buffer_write_time_max =
  577. max(cfi->chips[i].buffer_write_time_max, 2000);
  578. cfi->chips[i].ref_point_counter = 0;
  579. init_waitqueue_head(&(cfi->chips[i].wq));
  580. }
  581. map->fldrv = &cfi_amdstd_chipdrv;
  582. return cfi_amdstd_setup(mtd);
  583. }
  584. struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
  585. struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
  586. EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
  587. EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
  588. EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
  589. static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
  590. {
  591. struct map_info *map = mtd->priv;
  592. struct cfi_private *cfi = map->fldrv_priv;
  593. unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
  594. unsigned long offset = 0;
  595. int i,j;
  596. printk(KERN_NOTICE "number of %s chips: %d\n",
  597. (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
  598. /* Select the correct geometry setup */
  599. mtd->size = devsize * cfi->numchips;
  600. mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
  601. mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
  602. * mtd->numeraseregions, GFP_KERNEL);
  603. if (!mtd->eraseregions)
  604. goto setup_err;
  605. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  606. unsigned long ernum, ersize;
  607. ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
  608. ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
  609. if (mtd->erasesize < ersize) {
  610. mtd->erasesize = ersize;
  611. }
  612. for (j=0; j<cfi->numchips; j++) {
  613. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
  614. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
  615. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
  616. }
  617. offset += (ersize * ernum);
  618. }
  619. if (offset != devsize) {
  620. /* Argh */
  621. printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
  622. goto setup_err;
  623. }
  624. __module_get(THIS_MODULE);
  625. register_reboot_notifier(&mtd->reboot_notifier);
  626. return mtd;
  627. setup_err:
  628. kfree(mtd->eraseregions);
  629. kfree(mtd);
  630. kfree(cfi->cmdset_priv);
  631. kfree(cfi->cfiq);
  632. return NULL;
  633. }
  634. /*
  635. * Return true if the chip is ready.
  636. *
  637. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  638. * non-suspended sector) and is indicated by no toggle bits toggling.
  639. *
  640. * Note that anything more complicated than checking if no bits are toggling
  641. * (including checking DQ5 for an error status) is tricky to get working
  642. * correctly and is therefore not done (particularly with interleaved chips
  643. * as each chip must be checked independently of the others).
  644. */
  645. static int __xipram chip_ready(struct map_info *map, unsigned long addr)
  646. {
  647. map_word d, t;
  648. d = map_read(map, addr);
  649. t = map_read(map, addr);
  650. return map_word_equal(map, d, t);
  651. }
  652. /*
  653. * Return true if the chip is ready and has the correct value.
  654. *
  655. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  656. * non-suspended sector) and it is indicated by no bits toggling.
  657. *
  658. * Error are indicated by toggling bits or bits held with the wrong value,
  659. * or with bits toggling.
  660. *
  661. * Note that anything more complicated than checking if no bits are toggling
  662. * (including checking DQ5 for an error status) is tricky to get working
  663. * correctly and is therefore not done (particularly with interleaved chips
  664. * as each chip must be checked independently of the others).
  665. *
  666. */
  667. static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
  668. {
  669. map_word oldd, curd;
  670. oldd = map_read(map, addr);
  671. curd = map_read(map, addr);
  672. return map_word_equal(map, oldd, curd) &&
  673. map_word_equal(map, curd, expected);
  674. }
  675. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  676. {
  677. DECLARE_WAITQUEUE(wait, current);
  678. struct cfi_private *cfi = map->fldrv_priv;
  679. unsigned long timeo;
  680. struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
  681. resettime:
  682. timeo = jiffies + HZ;
  683. retry:
  684. switch (chip->state) {
  685. case FL_STATUS:
  686. for (;;) {
  687. if (chip_ready(map, adr))
  688. break;
  689. if (time_after(jiffies, timeo)) {
  690. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  691. return -EIO;
  692. }
  693. mutex_unlock(&chip->mutex);
  694. cfi_udelay(1);
  695. mutex_lock(&chip->mutex);
  696. /* Someone else might have been playing with it. */
  697. goto retry;
  698. }
  699. case FL_READY:
  700. case FL_CFI_QUERY:
  701. case FL_JEDEC_QUERY:
  702. return 0;
  703. case FL_ERASING:
  704. if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
  705. !(mode == FL_READY || mode == FL_POINT ||
  706. (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
  707. goto sleep;
  708. /* Do not allow suspend iff read/write to EB address */
  709. if ((adr & chip->in_progress_block_mask) ==
  710. chip->in_progress_block_addr)
  711. goto sleep;
  712. /* Erase suspend */
  713. /* It's harmless to issue the Erase-Suspend and Erase-Resume
  714. * commands when the erase algorithm isn't in progress. */
  715. map_write(map, CMD(0xB0), chip->in_progress_block_addr);
  716. chip->oldstate = FL_ERASING;
  717. chip->state = FL_ERASE_SUSPENDING;
  718. chip->erase_suspended = 1;
  719. for (;;) {
  720. if (chip_ready(map, adr))
  721. break;
  722. if (time_after(jiffies, timeo)) {
  723. /* Should have suspended the erase by now.
  724. * Send an Erase-Resume command as either
  725. * there was an error (so leave the erase
  726. * routine to recover from it) or we trying to
  727. * use the erase-in-progress sector. */
  728. put_chip(map, chip, adr);
  729. printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
  730. return -EIO;
  731. }
  732. mutex_unlock(&chip->mutex);
  733. cfi_udelay(1);
  734. mutex_lock(&chip->mutex);
  735. /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
  736. So we can just loop here. */
  737. }
  738. chip->state = FL_READY;
  739. return 0;
  740. case FL_XIP_WHILE_ERASING:
  741. if (mode != FL_READY && mode != FL_POINT &&
  742. (!cfip || !(cfip->EraseSuspend&2)))
  743. goto sleep;
  744. chip->oldstate = chip->state;
  745. chip->state = FL_READY;
  746. return 0;
  747. case FL_SHUTDOWN:
  748. /* The machine is rebooting */
  749. return -EIO;
  750. case FL_POINT:
  751. /* Only if there's no operation suspended... */
  752. if (mode == FL_READY && chip->oldstate == FL_READY)
  753. return 0;
  754. default:
  755. sleep:
  756. set_current_state(TASK_UNINTERRUPTIBLE);
  757. add_wait_queue(&chip->wq, &wait);
  758. mutex_unlock(&chip->mutex);
  759. schedule();
  760. remove_wait_queue(&chip->wq, &wait);
  761. mutex_lock(&chip->mutex);
  762. goto resettime;
  763. }
  764. }
  765. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
  766. {
  767. struct cfi_private *cfi = map->fldrv_priv;
  768. switch(chip->oldstate) {
  769. case FL_ERASING:
  770. cfi_fixup_m29ew_erase_suspend(map,
  771. chip->in_progress_block_addr);
  772. map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
  773. cfi_fixup_m29ew_delay_after_resume(cfi);
  774. chip->oldstate = FL_READY;
  775. chip->state = FL_ERASING;
  776. break;
  777. case FL_XIP_WHILE_ERASING:
  778. chip->state = chip->oldstate;
  779. chip->oldstate = FL_READY;
  780. break;
  781. case FL_READY:
  782. case FL_STATUS:
  783. break;
  784. default:
  785. printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
  786. }
  787. wake_up(&chip->wq);
  788. }
  789. #ifdef CONFIG_MTD_XIP
  790. /*
  791. * No interrupt what so ever can be serviced while the flash isn't in array
  792. * mode. This is ensured by the xip_disable() and xip_enable() functions
  793. * enclosing any code path where the flash is known not to be in array mode.
  794. * And within a XIP disabled code path, only functions marked with __xipram
  795. * may be called and nothing else (it's a good thing to inspect generated
  796. * assembly to make sure inline functions were actually inlined and that gcc
  797. * didn't emit calls to its own support functions). Also configuring MTD CFI
  798. * support to a single buswidth and a single interleave is also recommended.
  799. */
  800. static void xip_disable(struct map_info *map, struct flchip *chip,
  801. unsigned long adr)
  802. {
  803. /* TODO: chips with no XIP use should ignore and return */
  804. (void) map_read(map, adr); /* ensure mmu mapping is up to date */
  805. local_irq_disable();
  806. }
  807. static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
  808. unsigned long adr)
  809. {
  810. struct cfi_private *cfi = map->fldrv_priv;
  811. if (chip->state != FL_POINT && chip->state != FL_READY) {
  812. map_write(map, CMD(0xf0), adr);
  813. chip->state = FL_READY;
  814. }
  815. (void) map_read(map, adr);
  816. xip_iprefetch();
  817. local_irq_enable();
  818. }
  819. /*
  820. * When a delay is required for the flash operation to complete, the
  821. * xip_udelay() function is polling for both the given timeout and pending
  822. * (but still masked) hardware interrupts. Whenever there is an interrupt
  823. * pending then the flash erase operation is suspended, array mode restored
  824. * and interrupts unmasked. Task scheduling might also happen at that
  825. * point. The CPU eventually returns from the interrupt or the call to
  826. * schedule() and the suspended flash operation is resumed for the remaining
  827. * of the delay period.
  828. *
  829. * Warning: this function _will_ fool interrupt latency tracing tools.
  830. */
  831. static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
  832. unsigned long adr, int usec)
  833. {
  834. struct cfi_private *cfi = map->fldrv_priv;
  835. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  836. map_word status, OK = CMD(0x80);
  837. unsigned long suspended, start = xip_currtime();
  838. flstate_t oldstate;
  839. do {
  840. cpu_relax();
  841. if (xip_irqpending() && extp &&
  842. ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
  843. (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
  844. /*
  845. * Let's suspend the erase operation when supported.
  846. * Note that we currently don't try to suspend
  847. * interleaved chips if there is already another
  848. * operation suspended (imagine what happens
  849. * when one chip was already done with the current
  850. * operation while another chip suspended it, then
  851. * we resume the whole thing at once). Yes, it
  852. * can happen!
  853. */
  854. map_write(map, CMD(0xb0), adr);
  855. usec -= xip_elapsed_since(start);
  856. suspended = xip_currtime();
  857. do {
  858. if (xip_elapsed_since(suspended) > 100000) {
  859. /*
  860. * The chip doesn't want to suspend
  861. * after waiting for 100 msecs.
  862. * This is a critical error but there
  863. * is not much we can do here.
  864. */
  865. return;
  866. }
  867. status = map_read(map, adr);
  868. } while (!map_word_andequal(map, status, OK, OK));
  869. /* Suspend succeeded */
  870. oldstate = chip->state;
  871. if (!map_word_bitsset(map, status, CMD(0x40)))
  872. break;
  873. chip->state = FL_XIP_WHILE_ERASING;
  874. chip->erase_suspended = 1;
  875. map_write(map, CMD(0xf0), adr);
  876. (void) map_read(map, adr);
  877. xip_iprefetch();
  878. local_irq_enable();
  879. mutex_unlock(&chip->mutex);
  880. xip_iprefetch();
  881. cond_resched();
  882. /*
  883. * We're back. However someone else might have
  884. * decided to go write to the chip if we are in
  885. * a suspended erase state. If so let's wait
  886. * until it's done.
  887. */
  888. mutex_lock(&chip->mutex);
  889. while (chip->state != FL_XIP_WHILE_ERASING) {
  890. DECLARE_WAITQUEUE(wait, current);
  891. set_current_state(TASK_UNINTERRUPTIBLE);
  892. add_wait_queue(&chip->wq, &wait);
  893. mutex_unlock(&chip->mutex);
  894. schedule();
  895. remove_wait_queue(&chip->wq, &wait);
  896. mutex_lock(&chip->mutex);
  897. }
  898. /* Disallow XIP again */
  899. local_irq_disable();
  900. /* Correct Erase Suspend Hangups for M29EW */
  901. cfi_fixup_m29ew_erase_suspend(map, adr);
  902. /* Resume the write or erase operation */
  903. map_write(map, cfi->sector_erase_cmd, adr);
  904. chip->state = oldstate;
  905. start = xip_currtime();
  906. } else if (usec >= 1000000/HZ) {
  907. /*
  908. * Try to save on CPU power when waiting delay
  909. * is at least a system timer tick period.
  910. * No need to be extremely accurate here.
  911. */
  912. xip_cpu_idle();
  913. }
  914. status = map_read(map, adr);
  915. } while (!map_word_andequal(map, status, OK, OK)
  916. && xip_elapsed_since(start) < usec);
  917. }
  918. #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
  919. /*
  920. * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
  921. * the flash is actively programming or erasing since we have to poll for
  922. * the operation to complete anyway. We can't do that in a generic way with
  923. * a XIP setup so do it before the actual flash operation in this case
  924. * and stub it out from INVALIDATE_CACHE_UDELAY.
  925. */
  926. #define XIP_INVAL_CACHED_RANGE(map, from, size) \
  927. INVALIDATE_CACHED_RANGE(map, from, size)
  928. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  929. UDELAY(map, chip, adr, usec)
  930. /*
  931. * Extra notes:
  932. *
  933. * Activating this XIP support changes the way the code works a bit. For
  934. * example the code to suspend the current process when concurrent access
  935. * happens is never executed because xip_udelay() will always return with the
  936. * same chip state as it was entered with. This is why there is no care for
  937. * the presence of add_wait_queue() or schedule() calls from within a couple
  938. * xip_disable()'d areas of code, like in do_erase_oneblock for example.
  939. * The queueing and scheduling are always happening within xip_udelay().
  940. *
  941. * Similarly, get_chip() and put_chip() just happen to always be executed
  942. * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
  943. * is in array mode, therefore never executing many cases therein and not
  944. * causing any problem with XIP.
  945. */
  946. #else
  947. #define xip_disable(map, chip, adr)
  948. #define xip_enable(map, chip, adr)
  949. #define XIP_INVAL_CACHED_RANGE(x...)
  950. #define UDELAY(map, chip, adr, usec) \
  951. do { \
  952. mutex_unlock(&chip->mutex); \
  953. cfi_udelay(usec); \
  954. mutex_lock(&chip->mutex); \
  955. } while (0)
  956. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  957. do { \
  958. mutex_unlock(&chip->mutex); \
  959. INVALIDATE_CACHED_RANGE(map, adr, len); \
  960. cfi_udelay(usec); \
  961. mutex_lock(&chip->mutex); \
  962. } while (0)
  963. #endif
  964. static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  965. {
  966. unsigned long cmd_addr;
  967. struct cfi_private *cfi = map->fldrv_priv;
  968. int ret;
  969. adr += chip->start;
  970. /* Ensure cmd read/writes are aligned. */
  971. cmd_addr = adr & ~(map_bankwidth(map)-1);
  972. mutex_lock(&chip->mutex);
  973. ret = get_chip(map, chip, cmd_addr, FL_READY);
  974. if (ret) {
  975. mutex_unlock(&chip->mutex);
  976. return ret;
  977. }
  978. if (chip->state != FL_POINT && chip->state != FL_READY) {
  979. map_write(map, CMD(0xf0), cmd_addr);
  980. chip->state = FL_READY;
  981. }
  982. map_copy_from(map, buf, adr, len);
  983. put_chip(map, chip, cmd_addr);
  984. mutex_unlock(&chip->mutex);
  985. return 0;
  986. }
  987. static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  988. {
  989. struct map_info *map = mtd->priv;
  990. struct cfi_private *cfi = map->fldrv_priv;
  991. unsigned long ofs;
  992. int chipnum;
  993. int ret = 0;
  994. /* ofs: offset within the first chip that the first read should start */
  995. chipnum = (from >> cfi->chipshift);
  996. ofs = from - (chipnum << cfi->chipshift);
  997. while (len) {
  998. unsigned long thislen;
  999. if (chipnum >= cfi->numchips)
  1000. break;
  1001. if ((len + ofs -1) >> cfi->chipshift)
  1002. thislen = (1<<cfi->chipshift) - ofs;
  1003. else
  1004. thislen = len;
  1005. ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  1006. if (ret)
  1007. break;
  1008. *retlen += thislen;
  1009. len -= thislen;
  1010. buf += thislen;
  1011. ofs = 0;
  1012. chipnum++;
  1013. }
  1014. return ret;
  1015. }
  1016. typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
  1017. loff_t adr, size_t len, u_char *buf, size_t grouplen);
  1018. static inline void otp_enter(struct map_info *map, struct flchip *chip,
  1019. loff_t adr, size_t len)
  1020. {
  1021. struct cfi_private *cfi = map->fldrv_priv;
  1022. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1023. cfi->device_type, NULL);
  1024. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1025. cfi->device_type, NULL);
  1026. cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi,
  1027. cfi->device_type, NULL);
  1028. INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
  1029. }
  1030. static inline void otp_exit(struct map_info *map, struct flchip *chip,
  1031. loff_t adr, size_t len)
  1032. {
  1033. struct cfi_private *cfi = map->fldrv_priv;
  1034. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1035. cfi->device_type, NULL);
  1036. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1037. cfi->device_type, NULL);
  1038. cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi,
  1039. cfi->device_type, NULL);
  1040. cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi,
  1041. cfi->device_type, NULL);
  1042. INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
  1043. }
  1044. static inline int do_read_secsi_onechip(struct map_info *map,
  1045. struct flchip *chip, loff_t adr,
  1046. size_t len, u_char *buf,
  1047. size_t grouplen)
  1048. {
  1049. DECLARE_WAITQUEUE(wait, current);
  1050. unsigned long timeo = jiffies + HZ;
  1051. retry:
  1052. mutex_lock(&chip->mutex);
  1053. if (chip->state != FL_READY){
  1054. set_current_state(TASK_UNINTERRUPTIBLE);
  1055. add_wait_queue(&chip->wq, &wait);
  1056. mutex_unlock(&chip->mutex);
  1057. schedule();
  1058. remove_wait_queue(&chip->wq, &wait);
  1059. timeo = jiffies + HZ;
  1060. goto retry;
  1061. }
  1062. adr += chip->start;
  1063. chip->state = FL_READY;
  1064. otp_enter(map, chip, adr, len);
  1065. map_copy_from(map, buf, adr, len);
  1066. otp_exit(map, chip, adr, len);
  1067. wake_up(&chip->wq);
  1068. mutex_unlock(&chip->mutex);
  1069. return 0;
  1070. }
  1071. static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  1072. {
  1073. struct map_info *map = mtd->priv;
  1074. struct cfi_private *cfi = map->fldrv_priv;
  1075. unsigned long ofs;
  1076. int chipnum;
  1077. int ret = 0;
  1078. /* ofs: offset within the first chip that the first read should start */
  1079. /* 8 secsi bytes per chip */
  1080. chipnum=from>>3;
  1081. ofs=from & 7;
  1082. while (len) {
  1083. unsigned long thislen;
  1084. if (chipnum >= cfi->numchips)
  1085. break;
  1086. if ((len + ofs -1) >> 3)
  1087. thislen = (1<<3) - ofs;
  1088. else
  1089. thislen = len;
  1090. ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs,
  1091. thislen, buf, 0);
  1092. if (ret)
  1093. break;
  1094. *retlen += thislen;
  1095. len -= thislen;
  1096. buf += thislen;
  1097. ofs = 0;
  1098. chipnum++;
  1099. }
  1100. return ret;
  1101. }
  1102. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
  1103. unsigned long adr, map_word datum,
  1104. int mode);
  1105. static int do_otp_write(struct map_info *map, struct flchip *chip, loff_t adr,
  1106. size_t len, u_char *buf, size_t grouplen)
  1107. {
  1108. int ret;
  1109. while (len) {
  1110. unsigned long bus_ofs = adr & ~(map_bankwidth(map)-1);
  1111. int gap = adr - bus_ofs;
  1112. int n = min_t(int, len, map_bankwidth(map) - gap);
  1113. map_word datum = map_word_ff(map);
  1114. if (n != map_bankwidth(map)) {
  1115. /* partial write of a word, load old contents */
  1116. otp_enter(map, chip, bus_ofs, map_bankwidth(map));
  1117. datum = map_read(map, bus_ofs);
  1118. otp_exit(map, chip, bus_ofs, map_bankwidth(map));
  1119. }
  1120. datum = map_word_load_partial(map, datum, buf, gap, n);
  1121. ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
  1122. if (ret)
  1123. return ret;
  1124. adr += n;
  1125. buf += n;
  1126. len -= n;
  1127. }
  1128. return 0;
  1129. }
  1130. static int do_otp_lock(struct map_info *map, struct flchip *chip, loff_t adr,
  1131. size_t len, u_char *buf, size_t grouplen)
  1132. {
  1133. struct cfi_private *cfi = map->fldrv_priv;
  1134. uint8_t lockreg;
  1135. unsigned long timeo;
  1136. int ret;
  1137. /* make sure area matches group boundaries */
  1138. if ((adr != 0) || (len != grouplen))
  1139. return -EINVAL;
  1140. mutex_lock(&chip->mutex);
  1141. ret = get_chip(map, chip, chip->start, FL_LOCKING);
  1142. if (ret) {
  1143. mutex_unlock(&chip->mutex);
  1144. return ret;
  1145. }
  1146. chip->state = FL_LOCKING;
  1147. /* Enter lock register command */
  1148. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1149. cfi->device_type, NULL);
  1150. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1151. cfi->device_type, NULL);
  1152. cfi_send_gen_cmd(0x40, cfi->addr_unlock1, chip->start, map, cfi,
  1153. cfi->device_type, NULL);
  1154. /* read lock register */
  1155. lockreg = cfi_read_query(map, 0);
  1156. /* set bit 0 to protect extended memory block */
  1157. lockreg &= ~0x01;
  1158. /* set bit 0 to protect extended memory block */
  1159. /* write lock register */
  1160. map_write(map, CMD(0xA0), chip->start);
  1161. map_write(map, CMD(lockreg), chip->start);
  1162. /* wait for chip to become ready */
  1163. timeo = jiffies + msecs_to_jiffies(2);
  1164. for (;;) {
  1165. if (chip_ready(map, adr))
  1166. break;
  1167. if (time_after(jiffies, timeo)) {
  1168. pr_err("Waiting for chip to be ready timed out.\n");
  1169. ret = -EIO;
  1170. break;
  1171. }
  1172. UDELAY(map, chip, 0, 1);
  1173. }
  1174. /* exit protection commands */
  1175. map_write(map, CMD(0x90), chip->start);
  1176. map_write(map, CMD(0x00), chip->start);
  1177. chip->state = FL_READY;
  1178. put_chip(map, chip, chip->start);
  1179. mutex_unlock(&chip->mutex);
  1180. return ret;
  1181. }
  1182. static int cfi_amdstd_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1183. size_t *retlen, u_char *buf,
  1184. otp_op_t action, int user_regs)
  1185. {
  1186. struct map_info *map = mtd->priv;
  1187. struct cfi_private *cfi = map->fldrv_priv;
  1188. int ofs_factor = cfi->interleave * cfi->device_type;
  1189. unsigned long base;
  1190. int chipnum;
  1191. struct flchip *chip;
  1192. uint8_t otp, lockreg;
  1193. int ret;
  1194. size_t user_size, factory_size, otpsize;
  1195. loff_t user_offset, factory_offset, otpoffset;
  1196. int user_locked = 0, otplocked;
  1197. *retlen = 0;
  1198. for (chipnum = 0; chipnum < cfi->numchips; chipnum++) {
  1199. chip = &cfi->chips[chipnum];
  1200. factory_size = 0;
  1201. user_size = 0;
  1202. /* Micron M29EW family */
  1203. if (is_m29ew(cfi)) {
  1204. base = chip->start;
  1205. /* check whether secsi area is factory locked
  1206. or user lockable */
  1207. mutex_lock(&chip->mutex);
  1208. ret = get_chip(map, chip, base, FL_CFI_QUERY);
  1209. if (ret) {
  1210. mutex_unlock(&chip->mutex);
  1211. return ret;
  1212. }
  1213. cfi_qry_mode_on(base, map, cfi);
  1214. otp = cfi_read_query(map, base + 0x3 * ofs_factor);
  1215. cfi_qry_mode_off(base, map, cfi);
  1216. put_chip(map, chip, base);
  1217. mutex_unlock(&chip->mutex);
  1218. if (otp & 0x80) {
  1219. /* factory locked */
  1220. factory_offset = 0;
  1221. factory_size = 0x100;
  1222. } else {
  1223. /* customer lockable */
  1224. user_offset = 0;
  1225. user_size = 0x100;
  1226. mutex_lock(&chip->mutex);
  1227. ret = get_chip(map, chip, base, FL_LOCKING);
  1228. if (ret) {
  1229. mutex_unlock(&chip->mutex);
  1230. return ret;
  1231. }
  1232. /* Enter lock register command */
  1233. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1,
  1234. chip->start, map, cfi,
  1235. cfi->device_type, NULL);
  1236. cfi_send_gen_cmd(0x55, cfi->addr_unlock2,
  1237. chip->start, map, cfi,
  1238. cfi->device_type, NULL);
  1239. cfi_send_gen_cmd(0x40, cfi->addr_unlock1,
  1240. chip->start, map, cfi,
  1241. cfi->device_type, NULL);
  1242. /* read lock register */
  1243. lockreg = cfi_read_query(map, 0);
  1244. /* exit protection commands */
  1245. map_write(map, CMD(0x90), chip->start);
  1246. map_write(map, CMD(0x00), chip->start);
  1247. put_chip(map, chip, chip->start);
  1248. mutex_unlock(&chip->mutex);
  1249. user_locked = ((lockreg & 0x01) == 0x00);
  1250. }
  1251. }
  1252. otpsize = user_regs ? user_size : factory_size;
  1253. if (!otpsize)
  1254. continue;
  1255. otpoffset = user_regs ? user_offset : factory_offset;
  1256. otplocked = user_regs ? user_locked : 1;
  1257. if (!action) {
  1258. /* return otpinfo */
  1259. struct otp_info *otpinfo;
  1260. len -= sizeof(*otpinfo);
  1261. if (len <= 0)
  1262. return -ENOSPC;
  1263. otpinfo = (struct otp_info *)buf;
  1264. otpinfo->start = from;
  1265. otpinfo->length = otpsize;
  1266. otpinfo->locked = otplocked;
  1267. buf += sizeof(*otpinfo);
  1268. *retlen += sizeof(*otpinfo);
  1269. from += otpsize;
  1270. } else if ((from < otpsize) && (len > 0)) {
  1271. size_t size;
  1272. size = (len < otpsize - from) ? len : otpsize - from;
  1273. ret = action(map, chip, otpoffset + from, size, buf,
  1274. otpsize);
  1275. if (ret < 0)
  1276. return ret;
  1277. buf += size;
  1278. len -= size;
  1279. *retlen += size;
  1280. from = 0;
  1281. } else {
  1282. from -= otpsize;
  1283. }
  1284. }
  1285. return 0;
  1286. }
  1287. static int cfi_amdstd_get_fact_prot_info(struct mtd_info *mtd, size_t len,
  1288. size_t *retlen, struct otp_info *buf)
  1289. {
  1290. return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
  1291. NULL, 0);
  1292. }
  1293. static int cfi_amdstd_get_user_prot_info(struct mtd_info *mtd, size_t len,
  1294. size_t *retlen, struct otp_info *buf)
  1295. {
  1296. return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
  1297. NULL, 1);
  1298. }
  1299. static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1300. size_t len, size_t *retlen,
  1301. u_char *buf)
  1302. {
  1303. return cfi_amdstd_otp_walk(mtd, from, len, retlen,
  1304. buf, do_read_secsi_onechip, 0);
  1305. }
  1306. static int cfi_amdstd_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1307. size_t len, size_t *retlen,
  1308. u_char *buf)
  1309. {
  1310. return cfi_amdstd_otp_walk(mtd, from, len, retlen,
  1311. buf, do_read_secsi_onechip, 1);
  1312. }
  1313. static int cfi_amdstd_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1314. size_t len, size_t *retlen,
  1315. u_char *buf)
  1316. {
  1317. return cfi_amdstd_otp_walk(mtd, from, len, retlen, buf,
  1318. do_otp_write, 1);
  1319. }
  1320. static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1321. size_t len)
  1322. {
  1323. size_t retlen;
  1324. return cfi_amdstd_otp_walk(mtd, from, len, &retlen, NULL,
  1325. do_otp_lock, 1);
  1326. }
  1327. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
  1328. unsigned long adr, map_word datum,
  1329. int mode)
  1330. {
  1331. struct cfi_private *cfi = map->fldrv_priv;
  1332. unsigned long timeo = jiffies + HZ;
  1333. /*
  1334. * We use a 1ms + 1 jiffies generic timeout for writes (most devices
  1335. * have a max write time of a few hundreds usec). However, we should
  1336. * use the maximum timeout value given by the chip at probe time
  1337. * instead. Unfortunately, struct flchip does have a field for
  1338. * maximum timeout, only for typical which can be far too short
  1339. * depending of the conditions. The ' + 1' is to avoid having a
  1340. * timeout of 0 jiffies if HZ is smaller than 1000.
  1341. */
  1342. unsigned long uWriteTimeout = (HZ / 1000) + 1;
  1343. int ret = 0;
  1344. map_word oldd;
  1345. int retry_cnt = 0;
  1346. adr += chip->start;
  1347. mutex_lock(&chip->mutex);
  1348. ret = get_chip(map, chip, adr, mode);
  1349. if (ret) {
  1350. mutex_unlock(&chip->mutex);
  1351. return ret;
  1352. }
  1353. pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1354. __func__, adr, datum.x[0]);
  1355. if (mode == FL_OTP_WRITE)
  1356. otp_enter(map, chip, adr, map_bankwidth(map));
  1357. /*
  1358. * Check for a NOP for the case when the datum to write is already
  1359. * present - it saves time and works around buggy chips that corrupt
  1360. * data at other locations when 0xff is written to a location that
  1361. * already contains 0xff.
  1362. */
  1363. oldd = map_read(map, adr);
  1364. if (map_word_equal(map, oldd, datum)) {
  1365. pr_debug("MTD %s(): NOP\n",
  1366. __func__);
  1367. goto op_done;
  1368. }
  1369. XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
  1370. ENABLE_VPP(map);
  1371. xip_disable(map, chip, adr);
  1372. retry:
  1373. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1374. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1375. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1376. map_write(map, datum, adr);
  1377. chip->state = mode;
  1378. INVALIDATE_CACHE_UDELAY(map, chip,
  1379. adr, map_bankwidth(map),
  1380. chip->word_write_time);
  1381. /* See comment above for timeout value. */
  1382. timeo = jiffies + uWriteTimeout;
  1383. for (;;) {
  1384. if (chip->state != mode) {
  1385. /* Someone's suspended the write. Sleep */
  1386. DECLARE_WAITQUEUE(wait, current);
  1387. set_current_state(TASK_UNINTERRUPTIBLE);
  1388. add_wait_queue(&chip->wq, &wait);
  1389. mutex_unlock(&chip->mutex);
  1390. schedule();
  1391. remove_wait_queue(&chip->wq, &wait);
  1392. timeo = jiffies + (HZ / 2); /* FIXME */
  1393. mutex_lock(&chip->mutex);
  1394. continue;
  1395. }
  1396. if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
  1397. xip_enable(map, chip, adr);
  1398. printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
  1399. xip_disable(map, chip, adr);
  1400. break;
  1401. }
  1402. if (chip_ready(map, adr))
  1403. break;
  1404. /* Latency issues. Drop the lock, wait a while and retry */
  1405. UDELAY(map, chip, adr, 1);
  1406. }
  1407. /* Did we succeed? */
  1408. if (!chip_good(map, adr, datum)) {
  1409. /* reset on all failures. */
  1410. map_write(map, CMD(0xF0), chip->start);
  1411. /* FIXME - should have reset delay before continuing */
  1412. if (++retry_cnt <= MAX_RETRIES)
  1413. goto retry;
  1414. ret = -EIO;
  1415. }
  1416. xip_enable(map, chip, adr);
  1417. op_done:
  1418. if (mode == FL_OTP_WRITE)
  1419. otp_exit(map, chip, adr, map_bankwidth(map));
  1420. chip->state = FL_READY;
  1421. DISABLE_VPP(map);
  1422. put_chip(map, chip, adr);
  1423. mutex_unlock(&chip->mutex);
  1424. return ret;
  1425. }
  1426. static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
  1427. size_t *retlen, const u_char *buf)
  1428. {
  1429. struct map_info *map = mtd->priv;
  1430. struct cfi_private *cfi = map->fldrv_priv;
  1431. int ret = 0;
  1432. int chipnum;
  1433. unsigned long ofs, chipstart;
  1434. DECLARE_WAITQUEUE(wait, current);
  1435. chipnum = to >> cfi->chipshift;
  1436. ofs = to - (chipnum << cfi->chipshift);
  1437. chipstart = cfi->chips[chipnum].start;
  1438. /* If it's not bus-aligned, do the first byte write */
  1439. if (ofs & (map_bankwidth(map)-1)) {
  1440. unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
  1441. int i = ofs - bus_ofs;
  1442. int n = 0;
  1443. map_word tmp_buf;
  1444. retry:
  1445. mutex_lock(&cfi->chips[chipnum].mutex);
  1446. if (cfi->chips[chipnum].state != FL_READY) {
  1447. set_current_state(TASK_UNINTERRUPTIBLE);
  1448. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1449. mutex_unlock(&cfi->chips[chipnum].mutex);
  1450. schedule();
  1451. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1452. goto retry;
  1453. }
  1454. /* Load 'tmp_buf' with old contents of flash */
  1455. tmp_buf = map_read(map, bus_ofs+chipstart);
  1456. mutex_unlock(&cfi->chips[chipnum].mutex);
  1457. /* Number of bytes to copy from buffer */
  1458. n = min_t(int, len, map_bankwidth(map)-i);
  1459. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1460. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1461. bus_ofs, tmp_buf, FL_WRITING);
  1462. if (ret)
  1463. return ret;
  1464. ofs += n;
  1465. buf += n;
  1466. (*retlen) += n;
  1467. len -= n;
  1468. if (ofs >> cfi->chipshift) {
  1469. chipnum ++;
  1470. ofs = 0;
  1471. if (chipnum == cfi->numchips)
  1472. return 0;
  1473. }
  1474. }
  1475. /* We are now aligned, write as much as possible */
  1476. while(len >= map_bankwidth(map)) {
  1477. map_word datum;
  1478. datum = map_word_load(map, buf);
  1479. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1480. ofs, datum, FL_WRITING);
  1481. if (ret)
  1482. return ret;
  1483. ofs += map_bankwidth(map);
  1484. buf += map_bankwidth(map);
  1485. (*retlen) += map_bankwidth(map);
  1486. len -= map_bankwidth(map);
  1487. if (ofs >> cfi->chipshift) {
  1488. chipnum ++;
  1489. ofs = 0;
  1490. if (chipnum == cfi->numchips)
  1491. return 0;
  1492. chipstart = cfi->chips[chipnum].start;
  1493. }
  1494. }
  1495. /* Write the trailing bytes if any */
  1496. if (len & (map_bankwidth(map)-1)) {
  1497. map_word tmp_buf;
  1498. retry1:
  1499. mutex_lock(&cfi->chips[chipnum].mutex);
  1500. if (cfi->chips[chipnum].state != FL_READY) {
  1501. set_current_state(TASK_UNINTERRUPTIBLE);
  1502. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1503. mutex_unlock(&cfi->chips[chipnum].mutex);
  1504. schedule();
  1505. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1506. goto retry1;
  1507. }
  1508. tmp_buf = map_read(map, ofs + chipstart);
  1509. mutex_unlock(&cfi->chips[chipnum].mutex);
  1510. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1511. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1512. ofs, tmp_buf, FL_WRITING);
  1513. if (ret)
  1514. return ret;
  1515. (*retlen) += len;
  1516. }
  1517. return 0;
  1518. }
  1519. /*
  1520. * FIXME: interleaved mode not tested, and probably not supported!
  1521. */
  1522. static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
  1523. unsigned long adr, const u_char *buf,
  1524. int len)
  1525. {
  1526. struct cfi_private *cfi = map->fldrv_priv;
  1527. unsigned long timeo = jiffies + HZ;
  1528. /*
  1529. * Timeout is calculated according to CFI data, if available.
  1530. * See more comments in cfi_cmdset_0002().
  1531. */
  1532. unsigned long uWriteTimeout =
  1533. usecs_to_jiffies(chip->buffer_write_time_max);
  1534. int ret = -EIO;
  1535. unsigned long cmd_adr;
  1536. int z, words;
  1537. map_word datum;
  1538. adr += chip->start;
  1539. cmd_adr = adr;
  1540. mutex_lock(&chip->mutex);
  1541. ret = get_chip(map, chip, adr, FL_WRITING);
  1542. if (ret) {
  1543. mutex_unlock(&chip->mutex);
  1544. return ret;
  1545. }
  1546. datum = map_word_load(map, buf);
  1547. pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1548. __func__, adr, datum.x[0]);
  1549. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1550. ENABLE_VPP(map);
  1551. xip_disable(map, chip, cmd_adr);
  1552. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1553. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1554. /* Write Buffer Load */
  1555. map_write(map, CMD(0x25), cmd_adr);
  1556. chip->state = FL_WRITING_TO_BUFFER;
  1557. /* Write length of data to come */
  1558. words = len / map_bankwidth(map);
  1559. map_write(map, CMD(words - 1), cmd_adr);
  1560. /* Write data */
  1561. z = 0;
  1562. while(z < words * map_bankwidth(map)) {
  1563. datum = map_word_load(map, buf);
  1564. map_write(map, datum, adr + z);
  1565. z += map_bankwidth(map);
  1566. buf += map_bankwidth(map);
  1567. }
  1568. z -= map_bankwidth(map);
  1569. adr += z;
  1570. /* Write Buffer Program Confirm: GO GO GO */
  1571. map_write(map, CMD(0x29), cmd_adr);
  1572. chip->state = FL_WRITING;
  1573. INVALIDATE_CACHE_UDELAY(map, chip,
  1574. adr, map_bankwidth(map),
  1575. chip->word_write_time);
  1576. timeo = jiffies + uWriteTimeout;
  1577. for (;;) {
  1578. if (chip->state != FL_WRITING) {
  1579. /* Someone's suspended the write. Sleep */
  1580. DECLARE_WAITQUEUE(wait, current);
  1581. set_current_state(TASK_UNINTERRUPTIBLE);
  1582. add_wait_queue(&chip->wq, &wait);
  1583. mutex_unlock(&chip->mutex);
  1584. schedule();
  1585. remove_wait_queue(&chip->wq, &wait);
  1586. timeo = jiffies + (HZ / 2); /* FIXME */
  1587. mutex_lock(&chip->mutex);
  1588. continue;
  1589. }
  1590. if (time_after(jiffies, timeo) && !chip_ready(map, adr))
  1591. break;
  1592. if (chip_good(map, adr, datum)) {
  1593. xip_enable(map, chip, adr);
  1594. goto op_done;
  1595. }
  1596. /* Latency issues. Drop the lock, wait a while and retry */
  1597. UDELAY(map, chip, adr, 1);
  1598. }
  1599. /*
  1600. * Recovery from write-buffer programming failures requires
  1601. * the write-to-buffer-reset sequence. Since the last part
  1602. * of the sequence also works as a normal reset, we can run
  1603. * the same commands regardless of why we are here.
  1604. * See e.g.
  1605. * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf
  1606. */
  1607. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1608. cfi->device_type, NULL);
  1609. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1610. cfi->device_type, NULL);
  1611. cfi_send_gen_cmd(0xF0, cfi->addr_unlock1, chip->start, map, cfi,
  1612. cfi->device_type, NULL);
  1613. xip_enable(map, chip, adr);
  1614. /* FIXME - should have reset delay before continuing */
  1615. printk(KERN_WARNING "MTD %s(): software timeout, address:0x%.8lx.\n",
  1616. __func__, adr);
  1617. ret = -EIO;
  1618. op_done:
  1619. chip->state = FL_READY;
  1620. DISABLE_VPP(map);
  1621. put_chip(map, chip, adr);
  1622. mutex_unlock(&chip->mutex);
  1623. return ret;
  1624. }
  1625. static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
  1626. size_t *retlen, const u_char *buf)
  1627. {
  1628. struct map_info *map = mtd->priv;
  1629. struct cfi_private *cfi = map->fldrv_priv;
  1630. int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1631. int ret = 0;
  1632. int chipnum;
  1633. unsigned long ofs;
  1634. chipnum = to >> cfi->chipshift;
  1635. ofs = to - (chipnum << cfi->chipshift);
  1636. /* If it's not bus-aligned, do the first word write */
  1637. if (ofs & (map_bankwidth(map)-1)) {
  1638. size_t local_len = (-ofs)&(map_bankwidth(map)-1);
  1639. if (local_len > len)
  1640. local_len = len;
  1641. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1642. local_len, retlen, buf);
  1643. if (ret)
  1644. return ret;
  1645. ofs += local_len;
  1646. buf += local_len;
  1647. len -= local_len;
  1648. if (ofs >> cfi->chipshift) {
  1649. chipnum ++;
  1650. ofs = 0;
  1651. if (chipnum == cfi->numchips)
  1652. return 0;
  1653. }
  1654. }
  1655. /* Write buffer is worth it only if more than one word to write... */
  1656. while (len >= map_bankwidth(map) * 2) {
  1657. /* We must not cross write block boundaries */
  1658. int size = wbufsize - (ofs & (wbufsize-1));
  1659. if (size > len)
  1660. size = len;
  1661. if (size % map_bankwidth(map))
  1662. size -= size % map_bankwidth(map);
  1663. ret = do_write_buffer(map, &cfi->chips[chipnum],
  1664. ofs, buf, size);
  1665. if (ret)
  1666. return ret;
  1667. ofs += size;
  1668. buf += size;
  1669. (*retlen) += size;
  1670. len -= size;
  1671. if (ofs >> cfi->chipshift) {
  1672. chipnum ++;
  1673. ofs = 0;
  1674. if (chipnum == cfi->numchips)
  1675. return 0;
  1676. }
  1677. }
  1678. if (len) {
  1679. size_t retlen_dregs = 0;
  1680. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1681. len, &retlen_dregs, buf);
  1682. *retlen += retlen_dregs;
  1683. return ret;
  1684. }
  1685. return 0;
  1686. }
  1687. /*
  1688. * Wait for the flash chip to become ready to write data
  1689. *
  1690. * This is only called during the panic_write() path. When panic_write()
  1691. * is called, the kernel is in the process of a panic, and will soon be
  1692. * dead. Therefore we don't take any locks, and attempt to get access
  1693. * to the chip as soon as possible.
  1694. */
  1695. static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
  1696. unsigned long adr)
  1697. {
  1698. struct cfi_private *cfi = map->fldrv_priv;
  1699. int retries = 10;
  1700. int i;
  1701. /*
  1702. * If the driver thinks the chip is idle, and no toggle bits
  1703. * are changing, then the chip is actually idle for sure.
  1704. */
  1705. if (chip->state == FL_READY && chip_ready(map, adr))
  1706. return 0;
  1707. /*
  1708. * Try several times to reset the chip and then wait for it
  1709. * to become idle. The upper limit of a few milliseconds of
  1710. * delay isn't a big problem: the kernel is dying anyway. It
  1711. * is more important to save the messages.
  1712. */
  1713. while (retries > 0) {
  1714. const unsigned long timeo = (HZ / 1000) + 1;
  1715. /* send the reset command */
  1716. map_write(map, CMD(0xF0), chip->start);
  1717. /* wait for the chip to become ready */
  1718. for (i = 0; i < jiffies_to_usecs(timeo); i++) {
  1719. if (chip_ready(map, adr))
  1720. return 0;
  1721. udelay(1);
  1722. }
  1723. retries--;
  1724. }
  1725. /* the chip never became ready */
  1726. return -EBUSY;
  1727. }
  1728. /*
  1729. * Write out one word of data to a single flash chip during a kernel panic
  1730. *
  1731. * This is only called during the panic_write() path. When panic_write()
  1732. * is called, the kernel is in the process of a panic, and will soon be
  1733. * dead. Therefore we don't take any locks, and attempt to get access
  1734. * to the chip as soon as possible.
  1735. *
  1736. * The implementation of this routine is intentionally similar to
  1737. * do_write_oneword(), in order to ease code maintenance.
  1738. */
  1739. static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
  1740. unsigned long adr, map_word datum)
  1741. {
  1742. const unsigned long uWriteTimeout = (HZ / 1000) + 1;
  1743. struct cfi_private *cfi = map->fldrv_priv;
  1744. int retry_cnt = 0;
  1745. map_word oldd;
  1746. int ret = 0;
  1747. int i;
  1748. adr += chip->start;
  1749. ret = cfi_amdstd_panic_wait(map, chip, adr);
  1750. if (ret)
  1751. return ret;
  1752. pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
  1753. __func__, adr, datum.x[0]);
  1754. /*
  1755. * Check for a NOP for the case when the datum to write is already
  1756. * present - it saves time and works around buggy chips that corrupt
  1757. * data at other locations when 0xff is written to a location that
  1758. * already contains 0xff.
  1759. */
  1760. oldd = map_read(map, adr);
  1761. if (map_word_equal(map, oldd, datum)) {
  1762. pr_debug("MTD %s(): NOP\n", __func__);
  1763. goto op_done;
  1764. }
  1765. ENABLE_VPP(map);
  1766. retry:
  1767. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1768. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1769. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1770. map_write(map, datum, adr);
  1771. for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
  1772. if (chip_ready(map, adr))
  1773. break;
  1774. udelay(1);
  1775. }
  1776. if (!chip_good(map, adr, datum)) {
  1777. /* reset on all failures. */
  1778. map_write(map, CMD(0xF0), chip->start);
  1779. /* FIXME - should have reset delay before continuing */
  1780. if (++retry_cnt <= MAX_RETRIES)
  1781. goto retry;
  1782. ret = -EIO;
  1783. }
  1784. op_done:
  1785. DISABLE_VPP(map);
  1786. return ret;
  1787. }
  1788. /*
  1789. * Write out some data during a kernel panic
  1790. *
  1791. * This is used by the mtdoops driver to save the dying messages from a
  1792. * kernel which has panic'd.
  1793. *
  1794. * This routine ignores all of the locking used throughout the rest of the
  1795. * driver, in order to ensure that the data gets written out no matter what
  1796. * state this driver (and the flash chip itself) was in when the kernel crashed.
  1797. *
  1798. * The implementation of this routine is intentionally similar to
  1799. * cfi_amdstd_write_words(), in order to ease code maintenance.
  1800. */
  1801. static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  1802. size_t *retlen, const u_char *buf)
  1803. {
  1804. struct map_info *map = mtd->priv;
  1805. struct cfi_private *cfi = map->fldrv_priv;
  1806. unsigned long ofs, chipstart;
  1807. int ret = 0;
  1808. int chipnum;
  1809. chipnum = to >> cfi->chipshift;
  1810. ofs = to - (chipnum << cfi->chipshift);
  1811. chipstart = cfi->chips[chipnum].start;
  1812. /* If it's not bus aligned, do the first byte write */
  1813. if (ofs & (map_bankwidth(map) - 1)) {
  1814. unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
  1815. int i = ofs - bus_ofs;
  1816. int n = 0;
  1817. map_word tmp_buf;
  1818. ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
  1819. if (ret)
  1820. return ret;
  1821. /* Load 'tmp_buf' with old contents of flash */
  1822. tmp_buf = map_read(map, bus_ofs + chipstart);
  1823. /* Number of bytes to copy from buffer */
  1824. n = min_t(int, len, map_bankwidth(map) - i);
  1825. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1826. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1827. bus_ofs, tmp_buf);
  1828. if (ret)
  1829. return ret;
  1830. ofs += n;
  1831. buf += n;
  1832. (*retlen) += n;
  1833. len -= n;
  1834. if (ofs >> cfi->chipshift) {
  1835. chipnum++;
  1836. ofs = 0;
  1837. if (chipnum == cfi->numchips)
  1838. return 0;
  1839. }
  1840. }
  1841. /* We are now aligned, write as much as possible */
  1842. while (len >= map_bankwidth(map)) {
  1843. map_word datum;
  1844. datum = map_word_load(map, buf);
  1845. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1846. ofs, datum);
  1847. if (ret)
  1848. return ret;
  1849. ofs += map_bankwidth(map);
  1850. buf += map_bankwidth(map);
  1851. (*retlen) += map_bankwidth(map);
  1852. len -= map_bankwidth(map);
  1853. if (ofs >> cfi->chipshift) {
  1854. chipnum++;
  1855. ofs = 0;
  1856. if (chipnum == cfi->numchips)
  1857. return 0;
  1858. chipstart = cfi->chips[chipnum].start;
  1859. }
  1860. }
  1861. /* Write the trailing bytes if any */
  1862. if (len & (map_bankwidth(map) - 1)) {
  1863. map_word tmp_buf;
  1864. ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
  1865. if (ret)
  1866. return ret;
  1867. tmp_buf = map_read(map, ofs + chipstart);
  1868. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1869. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1870. ofs, tmp_buf);
  1871. if (ret)
  1872. return ret;
  1873. (*retlen) += len;
  1874. }
  1875. return 0;
  1876. }
  1877. /*
  1878. * Handle devices with one erase region, that only implement
  1879. * the chip erase command.
  1880. */
  1881. static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
  1882. {
  1883. struct cfi_private *cfi = map->fldrv_priv;
  1884. unsigned long timeo = jiffies + HZ;
  1885. unsigned long int adr;
  1886. DECLARE_WAITQUEUE(wait, current);
  1887. int ret = 0;
  1888. int retry_cnt = 0;
  1889. adr = cfi->addr_unlock1;
  1890. mutex_lock(&chip->mutex);
  1891. ret = get_chip(map, chip, adr, FL_WRITING);
  1892. if (ret) {
  1893. mutex_unlock(&chip->mutex);
  1894. return ret;
  1895. }
  1896. pr_debug("MTD %s(): ERASE 0x%.8lx\n",
  1897. __func__, chip->start);
  1898. XIP_INVAL_CACHED_RANGE(map, adr, map->size);
  1899. ENABLE_VPP(map);
  1900. xip_disable(map, chip, adr);
  1901. retry:
  1902. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1903. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1904. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1905. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1906. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1907. cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1908. chip->state = FL_ERASING;
  1909. chip->erase_suspended = 0;
  1910. chip->in_progress_block_addr = adr;
  1911. chip->in_progress_block_mask = ~(map->size - 1);
  1912. INVALIDATE_CACHE_UDELAY(map, chip,
  1913. adr, map->size,
  1914. chip->erase_time*500);
  1915. timeo = jiffies + (HZ*20);
  1916. for (;;) {
  1917. if (chip->state != FL_ERASING) {
  1918. /* Someone's suspended the erase. Sleep */
  1919. set_current_state(TASK_UNINTERRUPTIBLE);
  1920. add_wait_queue(&chip->wq, &wait);
  1921. mutex_unlock(&chip->mutex);
  1922. schedule();
  1923. remove_wait_queue(&chip->wq, &wait);
  1924. mutex_lock(&chip->mutex);
  1925. continue;
  1926. }
  1927. if (chip->erase_suspended) {
  1928. /* This erase was suspended and resumed.
  1929. Adjust the timeout */
  1930. timeo = jiffies + (HZ*20); /* FIXME */
  1931. chip->erase_suspended = 0;
  1932. }
  1933. if (chip_good(map, adr, map_word_ff(map)))
  1934. break;
  1935. if (time_after(jiffies, timeo)) {
  1936. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1937. __func__);
  1938. ret = -EIO;
  1939. break;
  1940. }
  1941. /* Latency issues. Drop the lock, wait a while and retry */
  1942. UDELAY(map, chip, adr, 1000000/HZ);
  1943. }
  1944. /* Did we succeed? */
  1945. if (ret) {
  1946. /* reset on all failures. */
  1947. map_write(map, CMD(0xF0), chip->start);
  1948. /* FIXME - should have reset delay before continuing */
  1949. if (++retry_cnt <= MAX_RETRIES) {
  1950. ret = 0;
  1951. goto retry;
  1952. }
  1953. }
  1954. chip->state = FL_READY;
  1955. xip_enable(map, chip, adr);
  1956. DISABLE_VPP(map);
  1957. put_chip(map, chip, adr);
  1958. mutex_unlock(&chip->mutex);
  1959. return ret;
  1960. }
  1961. static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
  1962. {
  1963. struct cfi_private *cfi = map->fldrv_priv;
  1964. unsigned long timeo = jiffies + HZ;
  1965. DECLARE_WAITQUEUE(wait, current);
  1966. int ret = 0;
  1967. int retry_cnt = 0;
  1968. adr += chip->start;
  1969. mutex_lock(&chip->mutex);
  1970. ret = get_chip(map, chip, adr, FL_ERASING);
  1971. if (ret) {
  1972. mutex_unlock(&chip->mutex);
  1973. return ret;
  1974. }
  1975. pr_debug("MTD %s(): ERASE 0x%.8lx\n",
  1976. __func__, adr);
  1977. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1978. ENABLE_VPP(map);
  1979. xip_disable(map, chip, adr);
  1980. retry:
  1981. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1982. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1983. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1984. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1985. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1986. map_write(map, cfi->sector_erase_cmd, adr);
  1987. chip->state = FL_ERASING;
  1988. chip->erase_suspended = 0;
  1989. chip->in_progress_block_addr = adr;
  1990. chip->in_progress_block_mask = ~(len - 1);
  1991. INVALIDATE_CACHE_UDELAY(map, chip,
  1992. adr, len,
  1993. chip->erase_time*500);
  1994. timeo = jiffies + (HZ*20);
  1995. for (;;) {
  1996. if (chip->state != FL_ERASING) {
  1997. /* Someone's suspended the erase. Sleep */
  1998. set_current_state(TASK_UNINTERRUPTIBLE);
  1999. add_wait_queue(&chip->wq, &wait);
  2000. mutex_unlock(&chip->mutex);
  2001. schedule();
  2002. remove_wait_queue(&chip->wq, &wait);
  2003. mutex_lock(&chip->mutex);
  2004. continue;
  2005. }
  2006. if (chip->erase_suspended) {
  2007. /* This erase was suspended and resumed.
  2008. Adjust the timeout */
  2009. timeo = jiffies + (HZ*20); /* FIXME */
  2010. chip->erase_suspended = 0;
  2011. }
  2012. if (chip_good(map, adr, map_word_ff(map)))
  2013. break;
  2014. if (time_after(jiffies, timeo)) {
  2015. printk(KERN_WARNING "MTD %s(): software timeout\n",
  2016. __func__);
  2017. ret = -EIO;
  2018. break;
  2019. }
  2020. /* Latency issues. Drop the lock, wait a while and retry */
  2021. UDELAY(map, chip, adr, 1000000/HZ);
  2022. }
  2023. /* Did we succeed? */
  2024. if (ret) {
  2025. /* reset on all failures. */
  2026. map_write(map, CMD(0xF0), chip->start);
  2027. /* FIXME - should have reset delay before continuing */
  2028. if (++retry_cnt <= MAX_RETRIES) {
  2029. ret = 0;
  2030. goto retry;
  2031. }
  2032. }
  2033. chip->state = FL_READY;
  2034. xip_enable(map, chip, adr);
  2035. DISABLE_VPP(map);
  2036. put_chip(map, chip, adr);
  2037. mutex_unlock(&chip->mutex);
  2038. return ret;
  2039. }
  2040. static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
  2041. {
  2042. return cfi_varsize_frob(mtd, do_erase_oneblock, instr->addr,
  2043. instr->len, NULL);
  2044. }
  2045. static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
  2046. {
  2047. struct map_info *map = mtd->priv;
  2048. struct cfi_private *cfi = map->fldrv_priv;
  2049. if (instr->addr != 0)
  2050. return -EINVAL;
  2051. if (instr->len != mtd->size)
  2052. return -EINVAL;
  2053. return do_erase_chip(map, &cfi->chips[0]);
  2054. }
  2055. static int do_atmel_lock(struct map_info *map, struct flchip *chip,
  2056. unsigned long adr, int len, void *thunk)
  2057. {
  2058. struct cfi_private *cfi = map->fldrv_priv;
  2059. int ret;
  2060. mutex_lock(&chip->mutex);
  2061. ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
  2062. if (ret)
  2063. goto out_unlock;
  2064. chip->state = FL_LOCKING;
  2065. pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
  2066. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2067. cfi->device_type, NULL);
  2068. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2069. cfi->device_type, NULL);
  2070. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
  2071. cfi->device_type, NULL);
  2072. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2073. cfi->device_type, NULL);
  2074. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2075. cfi->device_type, NULL);
  2076. map_write(map, CMD(0x40), chip->start + adr);
  2077. chip->state = FL_READY;
  2078. put_chip(map, chip, adr + chip->start);
  2079. ret = 0;
  2080. out_unlock:
  2081. mutex_unlock(&chip->mutex);
  2082. return ret;
  2083. }
  2084. static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
  2085. unsigned long adr, int len, void *thunk)
  2086. {
  2087. struct cfi_private *cfi = map->fldrv_priv;
  2088. int ret;
  2089. mutex_lock(&chip->mutex);
  2090. ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
  2091. if (ret)
  2092. goto out_unlock;
  2093. chip->state = FL_UNLOCKING;
  2094. pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
  2095. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2096. cfi->device_type, NULL);
  2097. map_write(map, CMD(0x70), adr);
  2098. chip->state = FL_READY;
  2099. put_chip(map, chip, adr + chip->start);
  2100. ret = 0;
  2101. out_unlock:
  2102. mutex_unlock(&chip->mutex);
  2103. return ret;
  2104. }
  2105. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2106. {
  2107. return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
  2108. }
  2109. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2110. {
  2111. return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
  2112. }
  2113. /*
  2114. * Advanced Sector Protection - PPB (Persistent Protection Bit) locking
  2115. */
  2116. struct ppb_lock {
  2117. struct flchip *chip;
  2118. loff_t offset;
  2119. int locked;
  2120. };
  2121. #define MAX_SECTORS 512
  2122. #define DO_XXLOCK_ONEBLOCK_LOCK ((void *)1)
  2123. #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *)2)
  2124. #define DO_XXLOCK_ONEBLOCK_GETLOCK ((void *)3)
  2125. static int __maybe_unused do_ppb_xxlock(struct map_info *map,
  2126. struct flchip *chip,
  2127. unsigned long adr, int len, void *thunk)
  2128. {
  2129. struct cfi_private *cfi = map->fldrv_priv;
  2130. unsigned long timeo;
  2131. int ret;
  2132. mutex_lock(&chip->mutex);
  2133. ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
  2134. if (ret) {
  2135. mutex_unlock(&chip->mutex);
  2136. return ret;
  2137. }
  2138. pr_debug("MTD %s(): XXLOCK 0x%08lx len %d\n", __func__, adr, len);
  2139. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2140. cfi->device_type, NULL);
  2141. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2142. cfi->device_type, NULL);
  2143. /* PPB entry command */
  2144. cfi_send_gen_cmd(0xC0, cfi->addr_unlock1, chip->start, map, cfi,
  2145. cfi->device_type, NULL);
  2146. if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
  2147. chip->state = FL_LOCKING;
  2148. map_write(map, CMD(0xA0), chip->start + adr);
  2149. map_write(map, CMD(0x00), chip->start + adr);
  2150. } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
  2151. /*
  2152. * Unlocking of one specific sector is not supported, so we
  2153. * have to unlock all sectors of this device instead
  2154. */
  2155. chip->state = FL_UNLOCKING;
  2156. map_write(map, CMD(0x80), chip->start);
  2157. map_write(map, CMD(0x30), chip->start);
  2158. } else if (thunk == DO_XXLOCK_ONEBLOCK_GETLOCK) {
  2159. chip->state = FL_JEDEC_QUERY;
  2160. /* Return locked status: 0->locked, 1->unlocked */
  2161. ret = !cfi_read_query(map, adr);
  2162. } else
  2163. BUG();
  2164. /*
  2165. * Wait for some time as unlocking of all sectors takes quite long
  2166. */
  2167. timeo = jiffies + msecs_to_jiffies(2000); /* 2s max (un)locking */
  2168. for (;;) {
  2169. if (chip_ready(map, adr))
  2170. break;
  2171. if (time_after(jiffies, timeo)) {
  2172. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  2173. ret = -EIO;
  2174. break;
  2175. }
  2176. UDELAY(map, chip, adr, 1);
  2177. }
  2178. /* Exit BC commands */
  2179. map_write(map, CMD(0x90), chip->start);
  2180. map_write(map, CMD(0x00), chip->start);
  2181. chip->state = FL_READY;
  2182. put_chip(map, chip, adr + chip->start);
  2183. mutex_unlock(&chip->mutex);
  2184. return ret;
  2185. }
  2186. static int __maybe_unused cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs,
  2187. uint64_t len)
  2188. {
  2189. return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2190. DO_XXLOCK_ONEBLOCK_LOCK);
  2191. }
  2192. static int __maybe_unused cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs,
  2193. uint64_t len)
  2194. {
  2195. struct mtd_erase_region_info *regions = mtd->eraseregions;
  2196. struct map_info *map = mtd->priv;
  2197. struct cfi_private *cfi = map->fldrv_priv;
  2198. struct ppb_lock *sect;
  2199. unsigned long adr;
  2200. loff_t offset;
  2201. uint64_t length;
  2202. int chipnum;
  2203. int i;
  2204. int sectors;
  2205. int ret;
  2206. /*
  2207. * PPB unlocking always unlocks all sectors of the flash chip.
  2208. * We need to re-lock all previously locked sectors. So lets
  2209. * first check the locking status of all sectors and save
  2210. * it for future use.
  2211. */
  2212. sect = kzalloc(MAX_SECTORS * sizeof(struct ppb_lock), GFP_KERNEL);
  2213. if (!sect)
  2214. return -ENOMEM;
  2215. /*
  2216. * This code to walk all sectors is a slightly modified version
  2217. * of the cfi_varsize_frob() code.
  2218. */
  2219. i = 0;
  2220. chipnum = 0;
  2221. adr = 0;
  2222. sectors = 0;
  2223. offset = 0;
  2224. length = mtd->size;
  2225. while (length) {
  2226. int size = regions[i].erasesize;
  2227. /*
  2228. * Only test sectors that shall not be unlocked. The other
  2229. * sectors shall be unlocked, so lets keep their locking
  2230. * status at "unlocked" (locked=0) for the final re-locking.
  2231. */
  2232. if ((adr < ofs) || (adr >= (ofs + len))) {
  2233. sect[sectors].chip = &cfi->chips[chipnum];
  2234. sect[sectors].offset = offset;
  2235. sect[sectors].locked = do_ppb_xxlock(
  2236. map, &cfi->chips[chipnum], adr, 0,
  2237. DO_XXLOCK_ONEBLOCK_GETLOCK);
  2238. }
  2239. adr += size;
  2240. offset += size;
  2241. length -= size;
  2242. if (offset == regions[i].offset + size * regions[i].numblocks)
  2243. i++;
  2244. if (adr >> cfi->chipshift) {
  2245. adr = 0;
  2246. chipnum++;
  2247. if (chipnum >= cfi->numchips)
  2248. break;
  2249. }
  2250. sectors++;
  2251. if (sectors >= MAX_SECTORS) {
  2252. printk(KERN_ERR "Only %d sectors for PPB locking supported!\n",
  2253. MAX_SECTORS);
  2254. kfree(sect);
  2255. return -EINVAL;
  2256. }
  2257. }
  2258. /* Now unlock the whole chip */
  2259. ret = cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2260. DO_XXLOCK_ONEBLOCK_UNLOCK);
  2261. if (ret) {
  2262. kfree(sect);
  2263. return ret;
  2264. }
  2265. /*
  2266. * PPB unlocking always unlocks all sectors of the flash chip.
  2267. * We need to re-lock all previously locked sectors.
  2268. */
  2269. for (i = 0; i < sectors; i++) {
  2270. if (sect[i].locked)
  2271. do_ppb_xxlock(map, sect[i].chip, sect[i].offset, 0,
  2272. DO_XXLOCK_ONEBLOCK_LOCK);
  2273. }
  2274. kfree(sect);
  2275. return ret;
  2276. }
  2277. static int __maybe_unused cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs,
  2278. uint64_t len)
  2279. {
  2280. return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2281. DO_XXLOCK_ONEBLOCK_GETLOCK) ? 1 : 0;
  2282. }
  2283. static void cfi_amdstd_sync (struct mtd_info *mtd)
  2284. {
  2285. struct map_info *map = mtd->priv;
  2286. struct cfi_private *cfi = map->fldrv_priv;
  2287. int i;
  2288. struct flchip *chip;
  2289. int ret = 0;
  2290. DECLARE_WAITQUEUE(wait, current);
  2291. for (i=0; !ret && i<cfi->numchips; i++) {
  2292. chip = &cfi->chips[i];
  2293. retry:
  2294. mutex_lock(&chip->mutex);
  2295. switch(chip->state) {
  2296. case FL_READY:
  2297. case FL_STATUS:
  2298. case FL_CFI_QUERY:
  2299. case FL_JEDEC_QUERY:
  2300. chip->oldstate = chip->state;
  2301. chip->state = FL_SYNCING;
  2302. /* No need to wake_up() on this state change -
  2303. * as the whole point is that nobody can do anything
  2304. * with the chip now anyway.
  2305. */
  2306. case FL_SYNCING:
  2307. mutex_unlock(&chip->mutex);
  2308. break;
  2309. default:
  2310. /* Not an idle state */
  2311. set_current_state(TASK_UNINTERRUPTIBLE);
  2312. add_wait_queue(&chip->wq, &wait);
  2313. mutex_unlock(&chip->mutex);
  2314. schedule();
  2315. remove_wait_queue(&chip->wq, &wait);
  2316. goto retry;
  2317. }
  2318. }
  2319. /* Unlock the chips again */
  2320. for (i--; i >=0; i--) {
  2321. chip = &cfi->chips[i];
  2322. mutex_lock(&chip->mutex);
  2323. if (chip->state == FL_SYNCING) {
  2324. chip->state = chip->oldstate;
  2325. wake_up(&chip->wq);
  2326. }
  2327. mutex_unlock(&chip->mutex);
  2328. }
  2329. }
  2330. static int cfi_amdstd_suspend(struct mtd_info *mtd)
  2331. {
  2332. struct map_info *map = mtd->priv;
  2333. struct cfi_private *cfi = map->fldrv_priv;
  2334. int i;
  2335. struct flchip *chip;
  2336. int ret = 0;
  2337. for (i=0; !ret && i<cfi->numchips; i++) {
  2338. chip = &cfi->chips[i];
  2339. mutex_lock(&chip->mutex);
  2340. switch(chip->state) {
  2341. case FL_READY:
  2342. case FL_STATUS:
  2343. case FL_CFI_QUERY:
  2344. case FL_JEDEC_QUERY:
  2345. chip->oldstate = chip->state;
  2346. chip->state = FL_PM_SUSPENDED;
  2347. /* No need to wake_up() on this state change -
  2348. * as the whole point is that nobody can do anything
  2349. * with the chip now anyway.
  2350. */
  2351. case FL_PM_SUSPENDED:
  2352. break;
  2353. default:
  2354. ret = -EAGAIN;
  2355. break;
  2356. }
  2357. mutex_unlock(&chip->mutex);
  2358. }
  2359. /* Unlock the chips again */
  2360. if (ret) {
  2361. for (i--; i >=0; i--) {
  2362. chip = &cfi->chips[i];
  2363. mutex_lock(&chip->mutex);
  2364. if (chip->state == FL_PM_SUSPENDED) {
  2365. chip->state = chip->oldstate;
  2366. wake_up(&chip->wq);
  2367. }
  2368. mutex_unlock(&chip->mutex);
  2369. }
  2370. }
  2371. return ret;
  2372. }
  2373. static void cfi_amdstd_resume(struct mtd_info *mtd)
  2374. {
  2375. struct map_info *map = mtd->priv;
  2376. struct cfi_private *cfi = map->fldrv_priv;
  2377. int i;
  2378. struct flchip *chip;
  2379. for (i=0; i<cfi->numchips; i++) {
  2380. chip = &cfi->chips[i];
  2381. mutex_lock(&chip->mutex);
  2382. if (chip->state == FL_PM_SUSPENDED) {
  2383. chip->state = FL_READY;
  2384. map_write(map, CMD(0xF0), chip->start);
  2385. wake_up(&chip->wq);
  2386. }
  2387. else
  2388. printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
  2389. mutex_unlock(&chip->mutex);
  2390. }
  2391. }
  2392. /*
  2393. * Ensure that the flash device is put back into read array mode before
  2394. * unloading the driver or rebooting. On some systems, rebooting while
  2395. * the flash is in query/program/erase mode will prevent the CPU from
  2396. * fetching the bootloader code, requiring a hard reset or power cycle.
  2397. */
  2398. static int cfi_amdstd_reset(struct mtd_info *mtd)
  2399. {
  2400. struct map_info *map = mtd->priv;
  2401. struct cfi_private *cfi = map->fldrv_priv;
  2402. int i, ret;
  2403. struct flchip *chip;
  2404. for (i = 0; i < cfi->numchips; i++) {
  2405. chip = &cfi->chips[i];
  2406. mutex_lock(&chip->mutex);
  2407. ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
  2408. if (!ret) {
  2409. map_write(map, CMD(0xF0), chip->start);
  2410. chip->state = FL_SHUTDOWN;
  2411. put_chip(map, chip, chip->start);
  2412. }
  2413. mutex_unlock(&chip->mutex);
  2414. }
  2415. return 0;
  2416. }
  2417. static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
  2418. void *v)
  2419. {
  2420. struct mtd_info *mtd;
  2421. mtd = container_of(nb, struct mtd_info, reboot_notifier);
  2422. cfi_amdstd_reset(mtd);
  2423. return NOTIFY_DONE;
  2424. }
  2425. static void cfi_amdstd_destroy(struct mtd_info *mtd)
  2426. {
  2427. struct map_info *map = mtd->priv;
  2428. struct cfi_private *cfi = map->fldrv_priv;
  2429. cfi_amdstd_reset(mtd);
  2430. unregister_reboot_notifier(&mtd->reboot_notifier);
  2431. kfree(cfi->cmdset_priv);
  2432. kfree(cfi->cfiq);
  2433. kfree(cfi);
  2434. kfree(mtd->eraseregions);
  2435. }
  2436. MODULE_LICENSE("GPL");
  2437. MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
  2438. MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
  2439. MODULE_ALIAS("cfi_cmdset_0006");
  2440. MODULE_ALIAS("cfi_cmdset_0701");