cfi_cmdset_0002.c 79 KB

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