cfi_cmdset_0002.c 79 KB

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