cfi_cmdset_0002.c 79 KB

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