cfi_cmdset_0002.c 84 KB

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