cfi_cmdset_0001.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690
  1. /*
  2. * Common Flash Interface support:
  3. * Intel Extended Vendor Command Set (ID 0x0001)
  4. *
  5. * (C) 2000 Red Hat. GPL'd
  6. *
  7. *
  8. * 10/10/2000 Nicolas Pitre <nico@fluxnic.net>
  9. * - completely revamped method functions so they are aware and
  10. * independent of the flash geometry (buswidth, interleave, etc.)
  11. * - scalability vs code size is completely set at compile-time
  12. * (see include/linux/mtd/cfi.h for selection)
  13. * - optimized write buffer method
  14. * 02/05/2002 Christopher Hoover <ch@hpl.hp.com>/<ch@murgatroid.com>
  15. * - reworked lock/unlock/erase support for var size flash
  16. * 21/03/2007 Rodolfo Giometti <giometti@linux.it>
  17. * - auto unlock sectors on resume for auto locking flash on power up
  18. */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <asm/io.h>
  24. #include <asm/byteorder.h>
  25. #include <linux/errno.h>
  26. #include <linux/slab.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/reboot.h>
  30. #include <linux/bitmap.h>
  31. #include <linux/mtd/xip.h>
  32. #include <linux/mtd/map.h>
  33. #include <linux/mtd/mtd.h>
  34. #include <linux/mtd/cfi.h>
  35. /* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */
  36. /* #define CMDSET0001_DISABLE_WRITE_SUSPEND */
  37. // debugging, turns off buffer write mode if set to 1
  38. #define FORCE_WORD_WRITE 0
  39. /* Intel chips */
  40. #define I82802AB 0x00ad
  41. #define I82802AC 0x00ac
  42. #define PF38F4476 0x881c
  43. #define M28F00AP30 0x8963
  44. /* STMicroelectronics chips */
  45. #define M50LPW080 0x002F
  46. #define M50FLW080A 0x0080
  47. #define M50FLW080B 0x0081
  48. /* Atmel chips */
  49. #define AT49BV640D 0x02de
  50. #define AT49BV640DT 0x02db
  51. /* Sharp chips */
  52. #define LH28F640BFHE_PTTL90 0x00b0
  53. #define LH28F640BFHE_PBTL90 0x00b1
  54. #define LH28F640BFHE_PTTL70A 0x00b2
  55. #define LH28F640BFHE_PBTL70A 0x00b3
  56. static int cfi_intelext_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  57. static int cfi_intelext_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  58. static int cfi_intelext_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  59. static int cfi_intelext_writev(struct mtd_info *, const struct kvec *, unsigned long, loff_t, size_t *);
  60. static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *);
  61. static void cfi_intelext_sync (struct mtd_info *);
  62. static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  63. static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  64. static int cfi_intelext_is_locked(struct mtd_info *mtd, loff_t ofs,
  65. uint64_t len);
  66. #ifdef CONFIG_MTD_OTP
  67. static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  68. static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  69. static int cfi_intelext_write_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  70. static int cfi_intelext_lock_user_prot_reg (struct mtd_info *, loff_t, size_t);
  71. static int cfi_intelext_get_fact_prot_info(struct mtd_info *, size_t,
  72. size_t *, struct otp_info *);
  73. static int cfi_intelext_get_user_prot_info(struct mtd_info *, size_t,
  74. size_t *, struct otp_info *);
  75. #endif
  76. static int cfi_intelext_suspend (struct mtd_info *);
  77. static void cfi_intelext_resume (struct mtd_info *);
  78. static int cfi_intelext_reboot (struct notifier_block *, unsigned long, void *);
  79. static void cfi_intelext_destroy(struct mtd_info *);
  80. struct mtd_info *cfi_cmdset_0001(struct map_info *, int);
  81. static struct mtd_info *cfi_intelext_setup (struct mtd_info *);
  82. static int cfi_intelext_partition_fixup(struct mtd_info *, struct cfi_private **);
  83. static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len,
  84. size_t *retlen, void **virt, resource_size_t *phys);
  85. static int cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
  86. static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
  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. /*
  91. * *********** SETUP AND PROBE BITS ***********
  92. */
  93. static struct mtd_chip_driver cfi_intelext_chipdrv = {
  94. .probe = NULL, /* Not usable directly */
  95. .destroy = cfi_intelext_destroy,
  96. .name = "cfi_cmdset_0001",
  97. .module = THIS_MODULE
  98. };
  99. /* #define DEBUG_LOCK_BITS */
  100. /* #define DEBUG_CFI_FEATURES */
  101. #ifdef DEBUG_CFI_FEATURES
  102. static void cfi_tell_features(struct cfi_pri_intelext *extp)
  103. {
  104. int i;
  105. printk(" Extended Query version %c.%c\n", extp->MajorVersion, extp->MinorVersion);
  106. printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport);
  107. printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported");
  108. printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported");
  109. printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported");
  110. printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported");
  111. printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported");
  112. printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported");
  113. printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported");
  114. printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported");
  115. printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported");
  116. printk(" - Simultaneous operations: %s\n", extp->FeatureSupport&512?"supported":"unsupported");
  117. printk(" - Extended Flash Array: %s\n", extp->FeatureSupport&1024?"supported":"unsupported");
  118. for (i=11; i<32; i++) {
  119. if (extp->FeatureSupport & (1<<i))
  120. printk(" - Unknown Bit %X: supported\n", i);
  121. }
  122. printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport);
  123. printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported");
  124. for (i=1; i<8; i++) {
  125. if (extp->SuspendCmdSupport & (1<<i))
  126. printk(" - Unknown Bit %X: supported\n", i);
  127. }
  128. printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask);
  129. printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no");
  130. printk(" - Lock-Down Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no");
  131. for (i=2; i<3; i++) {
  132. if (extp->BlkStatusRegMask & (1<<i))
  133. printk(" - Unknown Bit %X Active: yes\n",i);
  134. }
  135. printk(" - EFA Lock Bit: %s\n", extp->BlkStatusRegMask&16?"yes":"no");
  136. printk(" - EFA Lock-Down Bit: %s\n", extp->BlkStatusRegMask&32?"yes":"no");
  137. for (i=6; i<16; i++) {
  138. if (extp->BlkStatusRegMask & (1<<i))
  139. printk(" - Unknown Bit %X Active: yes\n",i);
  140. }
  141. printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
  142. extp->VccOptimal >> 4, extp->VccOptimal & 0xf);
  143. if (extp->VppOptimal)
  144. printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
  145. extp->VppOptimal >> 4, extp->VppOptimal & 0xf);
  146. }
  147. #endif
  148. /* Atmel chips don't use the same PRI format as Intel chips */
  149. static void fixup_convert_atmel_pri(struct mtd_info *mtd)
  150. {
  151. struct map_info *map = mtd->priv;
  152. struct cfi_private *cfi = map->fldrv_priv;
  153. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  154. struct cfi_pri_atmel atmel_pri;
  155. uint32_t features = 0;
  156. /* Reverse byteswapping */
  157. extp->FeatureSupport = cpu_to_le32(extp->FeatureSupport);
  158. extp->BlkStatusRegMask = cpu_to_le16(extp->BlkStatusRegMask);
  159. extp->ProtRegAddr = cpu_to_le16(extp->ProtRegAddr);
  160. memcpy(&atmel_pri, extp, sizeof(atmel_pri));
  161. memset((char *)extp + 5, 0, sizeof(*extp) - 5);
  162. printk(KERN_ERR "atmel Features: %02x\n", atmel_pri.Features);
  163. if (atmel_pri.Features & 0x01) /* chip erase supported */
  164. features |= (1<<0);
  165. if (atmel_pri.Features & 0x02) /* erase suspend supported */
  166. features |= (1<<1);
  167. if (atmel_pri.Features & 0x04) /* program suspend supported */
  168. features |= (1<<2);
  169. if (atmel_pri.Features & 0x08) /* simultaneous operations supported */
  170. features |= (1<<9);
  171. if (atmel_pri.Features & 0x20) /* page mode read supported */
  172. features |= (1<<7);
  173. if (atmel_pri.Features & 0x40) /* queued erase supported */
  174. features |= (1<<4);
  175. if (atmel_pri.Features & 0x80) /* Protection bits supported */
  176. features |= (1<<6);
  177. extp->FeatureSupport = features;
  178. /* burst write mode not supported */
  179. cfi->cfiq->BufWriteTimeoutTyp = 0;
  180. cfi->cfiq->BufWriteTimeoutMax = 0;
  181. }
  182. static void fixup_at49bv640dx_lock(struct mtd_info *mtd)
  183. {
  184. struct map_info *map = mtd->priv;
  185. struct cfi_private *cfi = map->fldrv_priv;
  186. struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
  187. cfip->FeatureSupport |= (1 << 5);
  188. mtd->flags |= MTD_POWERUP_LOCK;
  189. }
  190. #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
  191. /* Some Intel Strata Flash prior to FPO revision C has bugs in this area */
  192. static void fixup_intel_strataflash(struct mtd_info *mtd)
  193. {
  194. struct map_info *map = mtd->priv;
  195. struct cfi_private *cfi = map->fldrv_priv;
  196. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  197. printk(KERN_WARNING "cfi_cmdset_0001: Suspend "
  198. "erase on write disabled.\n");
  199. extp->SuspendCmdSupport &= ~1;
  200. }
  201. #endif
  202. #ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
  203. static void fixup_no_write_suspend(struct mtd_info *mtd)
  204. {
  205. struct map_info *map = mtd->priv;
  206. struct cfi_private *cfi = map->fldrv_priv;
  207. struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
  208. if (cfip && (cfip->FeatureSupport&4)) {
  209. cfip->FeatureSupport &= ~4;
  210. printk(KERN_WARNING "cfi_cmdset_0001: write suspend disabled\n");
  211. }
  212. }
  213. #endif
  214. static void fixup_st_m28w320ct(struct mtd_info *mtd)
  215. {
  216. struct map_info *map = mtd->priv;
  217. struct cfi_private *cfi = map->fldrv_priv;
  218. cfi->cfiq->BufWriteTimeoutTyp = 0; /* Not supported */
  219. cfi->cfiq->BufWriteTimeoutMax = 0; /* Not supported */
  220. }
  221. static void fixup_st_m28w320cb(struct mtd_info *mtd)
  222. {
  223. struct map_info *map = mtd->priv;
  224. struct cfi_private *cfi = map->fldrv_priv;
  225. /* Note this is done after the region info is endian swapped */
  226. cfi->cfiq->EraseRegionInfo[1] =
  227. (cfi->cfiq->EraseRegionInfo[1] & 0xffff0000) | 0x3e;
  228. };
  229. static int is_LH28F640BF(struct cfi_private *cfi)
  230. {
  231. /* Sharp LH28F640BF Family */
  232. if (cfi->mfr == CFI_MFR_SHARP && (
  233. cfi->id == LH28F640BFHE_PTTL90 || cfi->id == LH28F640BFHE_PBTL90 ||
  234. cfi->id == LH28F640BFHE_PTTL70A || cfi->id == LH28F640BFHE_PBTL70A))
  235. return 1;
  236. return 0;
  237. }
  238. static void fixup_LH28F640BF(struct mtd_info *mtd)
  239. {
  240. struct map_info *map = mtd->priv;
  241. struct cfi_private *cfi = map->fldrv_priv;
  242. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  243. /* Reset the Partition Configuration Register on LH28F640BF
  244. * to a single partition (PCR = 0x000): PCR is embedded into A0-A15. */
  245. if (is_LH28F640BF(cfi)) {
  246. printk(KERN_INFO "Reset Partition Config. Register: 1 Partition of 4 planes\n");
  247. map_write(map, CMD(0x60), 0);
  248. map_write(map, CMD(0x04), 0);
  249. /* We have set one single partition thus
  250. * Simultaneous Operations are not allowed */
  251. printk(KERN_INFO "cfi_cmdset_0001: Simultaneous Operations disabled\n");
  252. extp->FeatureSupport &= ~512;
  253. }
  254. }
  255. static void fixup_use_point(struct mtd_info *mtd)
  256. {
  257. struct map_info *map = mtd->priv;
  258. if (!mtd->_point && map_is_linear(map)) {
  259. mtd->_point = cfi_intelext_point;
  260. mtd->_unpoint = cfi_intelext_unpoint;
  261. }
  262. }
  263. static void fixup_use_write_buffers(struct mtd_info *mtd)
  264. {
  265. struct map_info *map = mtd->priv;
  266. struct cfi_private *cfi = map->fldrv_priv;
  267. if (cfi->cfiq->BufWriteTimeoutTyp) {
  268. printk(KERN_INFO "Using buffer write method\n" );
  269. mtd->_write = cfi_intelext_write_buffers;
  270. mtd->_writev = cfi_intelext_writev;
  271. }
  272. }
  273. /*
  274. * Some chips power-up with all sectors locked by default.
  275. */
  276. static void fixup_unlock_powerup_lock(struct mtd_info *mtd)
  277. {
  278. struct map_info *map = mtd->priv;
  279. struct cfi_private *cfi = map->fldrv_priv;
  280. struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
  281. if (cfip->FeatureSupport&32) {
  282. printk(KERN_INFO "Using auto-unlock on power-up/resume\n" );
  283. mtd->flags |= MTD_POWERUP_LOCK;
  284. }
  285. }
  286. static struct cfi_fixup cfi_fixup_table[] = {
  287. { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
  288. { CFI_MFR_ATMEL, AT49BV640D, fixup_at49bv640dx_lock },
  289. { CFI_MFR_ATMEL, AT49BV640DT, fixup_at49bv640dx_lock },
  290. #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
  291. { CFI_MFR_ANY, CFI_ID_ANY, fixup_intel_strataflash },
  292. #endif
  293. #ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
  294. { CFI_MFR_ANY, CFI_ID_ANY, fixup_no_write_suspend },
  295. #endif
  296. #if !FORCE_WORD_WRITE
  297. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
  298. #endif
  299. { CFI_MFR_ST, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct },
  300. { CFI_MFR_ST, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb },
  301. { CFI_MFR_INTEL, CFI_ID_ANY, fixup_unlock_powerup_lock },
  302. { CFI_MFR_SHARP, CFI_ID_ANY, fixup_unlock_powerup_lock },
  303. { CFI_MFR_SHARP, CFI_ID_ANY, fixup_LH28F640BF },
  304. { 0, 0, NULL }
  305. };
  306. static struct cfi_fixup jedec_fixup_table[] = {
  307. { CFI_MFR_INTEL, I82802AB, fixup_use_fwh_lock },
  308. { CFI_MFR_INTEL, I82802AC, fixup_use_fwh_lock },
  309. { CFI_MFR_ST, M50LPW080, fixup_use_fwh_lock },
  310. { CFI_MFR_ST, M50FLW080A, fixup_use_fwh_lock },
  311. { CFI_MFR_ST, M50FLW080B, fixup_use_fwh_lock },
  312. { 0, 0, NULL }
  313. };
  314. static struct cfi_fixup fixup_table[] = {
  315. /* The CFI vendor ids and the JEDEC vendor IDs appear
  316. * to be common. It is like the devices id's are as
  317. * well. This table is to pick all cases where
  318. * we know that is the case.
  319. */
  320. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_point },
  321. { 0, 0, NULL }
  322. };
  323. static void cfi_fixup_major_minor(struct cfi_private *cfi,
  324. struct cfi_pri_intelext *extp)
  325. {
  326. if (cfi->mfr == CFI_MFR_INTEL &&
  327. cfi->id == PF38F4476 && extp->MinorVersion == '3')
  328. extp->MinorVersion = '1';
  329. }
  330. static int cfi_is_micron_28F00AP30(struct cfi_private *cfi, struct flchip *chip)
  331. {
  332. /*
  333. * Micron(was Numonyx) 1Gbit bottom boot are buggy w.r.t
  334. * Erase Supend for their small Erase Blocks(0x8000)
  335. */
  336. if (cfi->mfr == CFI_MFR_INTEL && cfi->id == M28F00AP30)
  337. return 1;
  338. return 0;
  339. }
  340. static inline struct cfi_pri_intelext *
  341. read_pri_intelext(struct map_info *map, __u16 adr)
  342. {
  343. struct cfi_private *cfi = map->fldrv_priv;
  344. struct cfi_pri_intelext *extp;
  345. unsigned int extra_size = 0;
  346. unsigned int extp_size = sizeof(*extp);
  347. again:
  348. extp = (struct cfi_pri_intelext *)cfi_read_pri(map, adr, extp_size, "Intel/Sharp");
  349. if (!extp)
  350. return NULL;
  351. cfi_fixup_major_minor(cfi, extp);
  352. if (extp->MajorVersion != '1' ||
  353. (extp->MinorVersion < '0' || extp->MinorVersion > '5')) {
  354. printk(KERN_ERR " Unknown Intel/Sharp Extended Query "
  355. "version %c.%c.\n", extp->MajorVersion,
  356. extp->MinorVersion);
  357. kfree(extp);
  358. return NULL;
  359. }
  360. /* Do some byteswapping if necessary */
  361. extp->FeatureSupport = le32_to_cpu(extp->FeatureSupport);
  362. extp->BlkStatusRegMask = le16_to_cpu(extp->BlkStatusRegMask);
  363. extp->ProtRegAddr = le16_to_cpu(extp->ProtRegAddr);
  364. if (extp->MinorVersion >= '0') {
  365. extra_size = 0;
  366. /* Protection Register info */
  367. if (extp->NumProtectionFields)
  368. extra_size += (extp->NumProtectionFields - 1) *
  369. sizeof(struct cfi_intelext_otpinfo);
  370. }
  371. if (extp->MinorVersion >= '1') {
  372. /* Burst Read info */
  373. extra_size += 2;
  374. if (extp_size < sizeof(*extp) + extra_size)
  375. goto need_more;
  376. extra_size += extp->extra[extra_size - 1];
  377. }
  378. if (extp->MinorVersion >= '3') {
  379. int nb_parts, i;
  380. /* Number of hardware-partitions */
  381. extra_size += 1;
  382. if (extp_size < sizeof(*extp) + extra_size)
  383. goto need_more;
  384. nb_parts = extp->extra[extra_size - 1];
  385. /* skip the sizeof(partregion) field in CFI 1.4 */
  386. if (extp->MinorVersion >= '4')
  387. extra_size += 2;
  388. for (i = 0; i < nb_parts; i++) {
  389. struct cfi_intelext_regioninfo *rinfo;
  390. rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[extra_size];
  391. extra_size += sizeof(*rinfo);
  392. if (extp_size < sizeof(*extp) + extra_size)
  393. goto need_more;
  394. rinfo->NumIdentPartitions=le16_to_cpu(rinfo->NumIdentPartitions);
  395. extra_size += (rinfo->NumBlockTypes - 1)
  396. * sizeof(struct cfi_intelext_blockinfo);
  397. }
  398. if (extp->MinorVersion >= '4')
  399. extra_size += sizeof(struct cfi_intelext_programming_regioninfo);
  400. if (extp_size < sizeof(*extp) + extra_size) {
  401. need_more:
  402. extp_size = sizeof(*extp) + extra_size;
  403. kfree(extp);
  404. if (extp_size > 4096) {
  405. printk(KERN_ERR
  406. "%s: cfi_pri_intelext is too fat\n",
  407. __func__);
  408. return NULL;
  409. }
  410. goto again;
  411. }
  412. }
  413. return extp;
  414. }
  415. struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
  416. {
  417. struct cfi_private *cfi = map->fldrv_priv;
  418. struct mtd_info *mtd;
  419. int i;
  420. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  421. if (!mtd)
  422. return NULL;
  423. mtd->priv = map;
  424. mtd->type = MTD_NORFLASH;
  425. /* Fill in the default mtd operations */
  426. mtd->_erase = cfi_intelext_erase_varsize;
  427. mtd->_read = cfi_intelext_read;
  428. mtd->_write = cfi_intelext_write_words;
  429. mtd->_sync = cfi_intelext_sync;
  430. mtd->_lock = cfi_intelext_lock;
  431. mtd->_unlock = cfi_intelext_unlock;
  432. mtd->_is_locked = cfi_intelext_is_locked;
  433. mtd->_suspend = cfi_intelext_suspend;
  434. mtd->_resume = cfi_intelext_resume;
  435. mtd->flags = MTD_CAP_NORFLASH;
  436. mtd->name = map->name;
  437. mtd->writesize = 1;
  438. mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  439. mtd->reboot_notifier.notifier_call = cfi_intelext_reboot;
  440. if (cfi->cfi_mode == CFI_MODE_CFI) {
  441. /*
  442. * It's a real CFI chip, not one for which the probe
  443. * routine faked a CFI structure. So we read the feature
  444. * table from it.
  445. */
  446. __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
  447. struct cfi_pri_intelext *extp;
  448. extp = read_pri_intelext(map, adr);
  449. if (!extp) {
  450. kfree(mtd);
  451. return NULL;
  452. }
  453. /* Install our own private info structure */
  454. cfi->cmdset_priv = extp;
  455. cfi_fixup(mtd, cfi_fixup_table);
  456. #ifdef DEBUG_CFI_FEATURES
  457. /* Tell the user about it in lots of lovely detail */
  458. cfi_tell_features(extp);
  459. #endif
  460. if(extp->SuspendCmdSupport & 1) {
  461. printk(KERN_NOTICE "cfi_cmdset_0001: Erase suspend on write enabled\n");
  462. }
  463. }
  464. else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
  465. /* Apply jedec specific fixups */
  466. cfi_fixup(mtd, jedec_fixup_table);
  467. }
  468. /* Apply generic fixups */
  469. cfi_fixup(mtd, fixup_table);
  470. for (i=0; i< cfi->numchips; i++) {
  471. if (cfi->cfiq->WordWriteTimeoutTyp)
  472. cfi->chips[i].word_write_time =
  473. 1<<cfi->cfiq->WordWriteTimeoutTyp;
  474. else
  475. cfi->chips[i].word_write_time = 50000;
  476. if (cfi->cfiq->BufWriteTimeoutTyp)
  477. cfi->chips[i].buffer_write_time =
  478. 1<<cfi->cfiq->BufWriteTimeoutTyp;
  479. /* No default; if it isn't specified, we won't use it */
  480. if (cfi->cfiq->BlockEraseTimeoutTyp)
  481. cfi->chips[i].erase_time =
  482. 1000<<cfi->cfiq->BlockEraseTimeoutTyp;
  483. else
  484. cfi->chips[i].erase_time = 2000000;
  485. if (cfi->cfiq->WordWriteTimeoutTyp &&
  486. cfi->cfiq->WordWriteTimeoutMax)
  487. cfi->chips[i].word_write_time_max =
  488. 1<<(cfi->cfiq->WordWriteTimeoutTyp +
  489. cfi->cfiq->WordWriteTimeoutMax);
  490. else
  491. cfi->chips[i].word_write_time_max = 50000 * 8;
  492. if (cfi->cfiq->BufWriteTimeoutTyp &&
  493. cfi->cfiq->BufWriteTimeoutMax)
  494. cfi->chips[i].buffer_write_time_max =
  495. 1<<(cfi->cfiq->BufWriteTimeoutTyp +
  496. cfi->cfiq->BufWriteTimeoutMax);
  497. if (cfi->cfiq->BlockEraseTimeoutTyp &&
  498. cfi->cfiq->BlockEraseTimeoutMax)
  499. cfi->chips[i].erase_time_max =
  500. 1000<<(cfi->cfiq->BlockEraseTimeoutTyp +
  501. cfi->cfiq->BlockEraseTimeoutMax);
  502. else
  503. cfi->chips[i].erase_time_max = 2000000 * 8;
  504. cfi->chips[i].ref_point_counter = 0;
  505. init_waitqueue_head(&(cfi->chips[i].wq));
  506. }
  507. map->fldrv = &cfi_intelext_chipdrv;
  508. return cfi_intelext_setup(mtd);
  509. }
  510. struct mtd_info *cfi_cmdset_0003(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
  511. struct mtd_info *cfi_cmdset_0200(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
  512. EXPORT_SYMBOL_GPL(cfi_cmdset_0001);
  513. EXPORT_SYMBOL_GPL(cfi_cmdset_0003);
  514. EXPORT_SYMBOL_GPL(cfi_cmdset_0200);
  515. static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
  516. {
  517. struct map_info *map = mtd->priv;
  518. struct cfi_private *cfi = map->fldrv_priv;
  519. unsigned long offset = 0;
  520. int i,j;
  521. unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
  522. //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
  523. mtd->size = devsize * cfi->numchips;
  524. mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
  525. mtd->eraseregions = kcalloc(mtd->numeraseregions,
  526. sizeof(struct mtd_erase_region_info),
  527. GFP_KERNEL);
  528. if (!mtd->eraseregions)
  529. goto setup_err;
  530. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  531. unsigned long ernum, ersize;
  532. ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
  533. ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
  534. if (mtd->erasesize < ersize) {
  535. mtd->erasesize = ersize;
  536. }
  537. for (j=0; j<cfi->numchips; j++) {
  538. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
  539. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
  540. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
  541. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap = kmalloc(ernum / 8 + 1, GFP_KERNEL);
  542. if (!mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap)
  543. goto setup_err;
  544. }
  545. offset += (ersize * ernum);
  546. }
  547. if (offset != devsize) {
  548. /* Argh */
  549. printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
  550. goto setup_err;
  551. }
  552. for (i=0; i<mtd->numeraseregions;i++){
  553. printk(KERN_DEBUG "erase region %d: offset=0x%llx,size=0x%x,blocks=%d\n",
  554. i,(unsigned long long)mtd->eraseregions[i].offset,
  555. mtd->eraseregions[i].erasesize,
  556. mtd->eraseregions[i].numblocks);
  557. }
  558. #ifdef CONFIG_MTD_OTP
  559. mtd->_read_fact_prot_reg = cfi_intelext_read_fact_prot_reg;
  560. mtd->_read_user_prot_reg = cfi_intelext_read_user_prot_reg;
  561. mtd->_write_user_prot_reg = cfi_intelext_write_user_prot_reg;
  562. mtd->_lock_user_prot_reg = cfi_intelext_lock_user_prot_reg;
  563. mtd->_get_fact_prot_info = cfi_intelext_get_fact_prot_info;
  564. mtd->_get_user_prot_info = cfi_intelext_get_user_prot_info;
  565. #endif
  566. /* This function has the potential to distort the reality
  567. a bit and therefore should be called last. */
  568. if (cfi_intelext_partition_fixup(mtd, &cfi) != 0)
  569. goto setup_err;
  570. __module_get(THIS_MODULE);
  571. register_reboot_notifier(&mtd->reboot_notifier);
  572. return mtd;
  573. setup_err:
  574. if (mtd->eraseregions)
  575. for (i=0; i<cfi->cfiq->NumEraseRegions; i++)
  576. for (j=0; j<cfi->numchips; j++)
  577. kfree(mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap);
  578. kfree(mtd->eraseregions);
  579. kfree(mtd);
  580. kfree(cfi->cmdset_priv);
  581. return NULL;
  582. }
  583. static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
  584. struct cfi_private **pcfi)
  585. {
  586. struct map_info *map = mtd->priv;
  587. struct cfi_private *cfi = *pcfi;
  588. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  589. /*
  590. * Probing of multi-partition flash chips.
  591. *
  592. * To support multiple partitions when available, we simply arrange
  593. * for each of them to have their own flchip structure even if they
  594. * are on the same physical chip. This means completely recreating
  595. * a new cfi_private structure right here which is a blatent code
  596. * layering violation, but this is still the least intrusive
  597. * arrangement at this point. This can be rearranged in the future
  598. * if someone feels motivated enough. --nico
  599. */
  600. if (extp && extp->MajorVersion == '1' && extp->MinorVersion >= '3'
  601. && extp->FeatureSupport & (1 << 9)) {
  602. int offs = 0;
  603. struct cfi_private *newcfi;
  604. struct flchip *chip;
  605. struct flchip_shared *shared;
  606. int numregions, numparts, partshift, numvirtchips, i, j;
  607. /* Protection Register info */
  608. if (extp->NumProtectionFields)
  609. offs = (extp->NumProtectionFields - 1) *
  610. sizeof(struct cfi_intelext_otpinfo);
  611. /* Burst Read info */
  612. offs += extp->extra[offs+1]+2;
  613. /* Number of partition regions */
  614. numregions = extp->extra[offs];
  615. offs += 1;
  616. /* skip the sizeof(partregion) field in CFI 1.4 */
  617. if (extp->MinorVersion >= '4')
  618. offs += 2;
  619. /* Number of hardware partitions */
  620. numparts = 0;
  621. for (i = 0; i < numregions; i++) {
  622. struct cfi_intelext_regioninfo *rinfo;
  623. rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[offs];
  624. numparts += rinfo->NumIdentPartitions;
  625. offs += sizeof(*rinfo)
  626. + (rinfo->NumBlockTypes - 1) *
  627. sizeof(struct cfi_intelext_blockinfo);
  628. }
  629. if (!numparts)
  630. numparts = 1;
  631. /* Programming Region info */
  632. if (extp->MinorVersion >= '4') {
  633. struct cfi_intelext_programming_regioninfo *prinfo;
  634. prinfo = (struct cfi_intelext_programming_regioninfo *)&extp->extra[offs];
  635. mtd->writesize = cfi->interleave << prinfo->ProgRegShift;
  636. mtd->flags &= ~MTD_BIT_WRITEABLE;
  637. printk(KERN_DEBUG "%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
  638. map->name, mtd->writesize,
  639. cfi->interleave * prinfo->ControlValid,
  640. cfi->interleave * prinfo->ControlInvalid);
  641. }
  642. /*
  643. * All functions below currently rely on all chips having
  644. * the same geometry so we'll just assume that all hardware
  645. * partitions are of the same size too.
  646. */
  647. partshift = cfi->chipshift - __ffs(numparts);
  648. if ((1 << partshift) < mtd->erasesize) {
  649. printk( KERN_ERR
  650. "%s: bad number of hw partitions (%d)\n",
  651. __func__, numparts);
  652. return -EINVAL;
  653. }
  654. numvirtchips = cfi->numchips * numparts;
  655. newcfi = kmalloc(struct_size(newcfi, chips, numvirtchips),
  656. GFP_KERNEL);
  657. if (!newcfi)
  658. return -ENOMEM;
  659. shared = kmalloc_array(cfi->numchips,
  660. sizeof(struct flchip_shared),
  661. GFP_KERNEL);
  662. if (!shared) {
  663. kfree(newcfi);
  664. return -ENOMEM;
  665. }
  666. memcpy(newcfi, cfi, sizeof(struct cfi_private));
  667. newcfi->numchips = numvirtchips;
  668. newcfi->chipshift = partshift;
  669. chip = &newcfi->chips[0];
  670. for (i = 0; i < cfi->numchips; i++) {
  671. shared[i].writing = shared[i].erasing = NULL;
  672. mutex_init(&shared[i].lock);
  673. for (j = 0; j < numparts; j++) {
  674. *chip = cfi->chips[i];
  675. chip->start += j << partshift;
  676. chip->priv = &shared[i];
  677. /* those should be reset too since
  678. they create memory references. */
  679. init_waitqueue_head(&chip->wq);
  680. mutex_init(&chip->mutex);
  681. chip++;
  682. }
  683. }
  684. printk(KERN_DEBUG "%s: %d set(s) of %d interleaved chips "
  685. "--> %d partitions of %d KiB\n",
  686. map->name, cfi->numchips, cfi->interleave,
  687. newcfi->numchips, 1<<(newcfi->chipshift-10));
  688. map->fldrv_priv = newcfi;
  689. *pcfi = newcfi;
  690. kfree(cfi);
  691. }
  692. return 0;
  693. }
  694. /*
  695. * *********** CHIP ACCESS FUNCTIONS ***********
  696. */
  697. static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  698. {
  699. DECLARE_WAITQUEUE(wait, current);
  700. struct cfi_private *cfi = map->fldrv_priv;
  701. map_word status, status_OK = CMD(0x80), status_PWS = CMD(0x01);
  702. struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
  703. unsigned long timeo = jiffies + HZ;
  704. /* Prevent setting state FL_SYNCING for chip in suspended state. */
  705. if (mode == FL_SYNCING && chip->oldstate != FL_READY)
  706. goto sleep;
  707. switch (chip->state) {
  708. case FL_STATUS:
  709. for (;;) {
  710. status = map_read(map, adr);
  711. if (map_word_andequal(map, status, status_OK, status_OK))
  712. break;
  713. /* At this point we're fine with write operations
  714. in other partitions as they don't conflict. */
  715. if (chip->priv && map_word_andequal(map, status, status_PWS, status_PWS))
  716. break;
  717. mutex_unlock(&chip->mutex);
  718. cfi_udelay(1);
  719. mutex_lock(&chip->mutex);
  720. /* Someone else might have been playing with it. */
  721. return -EAGAIN;
  722. }
  723. fallthrough;
  724. case FL_READY:
  725. case FL_CFI_QUERY:
  726. case FL_JEDEC_QUERY:
  727. return 0;
  728. case FL_ERASING:
  729. if (!cfip ||
  730. !(cfip->FeatureSupport & 2) ||
  731. !(mode == FL_READY || mode == FL_POINT ||
  732. (mode == FL_WRITING && (cfip->SuspendCmdSupport & 1))))
  733. goto sleep;
  734. /* Do not allow suspend iff read/write to EB address */
  735. if ((adr & chip->in_progress_block_mask) ==
  736. chip->in_progress_block_addr)
  737. goto sleep;
  738. /* do not suspend small EBs, buggy Micron Chips */
  739. if (cfi_is_micron_28F00AP30(cfi, chip) &&
  740. (chip->in_progress_block_mask == ~(0x8000-1)))
  741. goto sleep;
  742. /* Erase suspend */
  743. map_write(map, CMD(0xB0), chip->in_progress_block_addr);
  744. /* If the flash has finished erasing, then 'erase suspend'
  745. * appears to make some (28F320) flash devices switch to
  746. * 'read' mode. Make sure that we switch to 'read status'
  747. * mode so we get the right data. --rmk
  748. */
  749. map_write(map, CMD(0x70), chip->in_progress_block_addr);
  750. chip->oldstate = FL_ERASING;
  751. chip->state = FL_ERASE_SUSPENDING;
  752. chip->erase_suspended = 1;
  753. for (;;) {
  754. status = map_read(map, chip->in_progress_block_addr);
  755. if (map_word_andequal(map, status, status_OK, status_OK))
  756. break;
  757. if (time_after(jiffies, timeo)) {
  758. /* Urgh. Resume and pretend we weren't here.
  759. * Make sure we're in 'read status' mode if it had finished */
  760. put_chip(map, chip, adr);
  761. printk(KERN_ERR "%s: Chip not ready after erase "
  762. "suspended: status = 0x%lx\n", map->name, status.x[0]);
  763. return -EIO;
  764. }
  765. mutex_unlock(&chip->mutex);
  766. cfi_udelay(1);
  767. mutex_lock(&chip->mutex);
  768. /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
  769. So we can just loop here. */
  770. }
  771. chip->state = FL_STATUS;
  772. return 0;
  773. case FL_XIP_WHILE_ERASING:
  774. if (mode != FL_READY && mode != FL_POINT &&
  775. (mode != FL_WRITING || !cfip || !(cfip->SuspendCmdSupport&1)))
  776. goto sleep;
  777. chip->oldstate = chip->state;
  778. chip->state = FL_READY;
  779. return 0;
  780. case FL_SHUTDOWN:
  781. /* The machine is rebooting now,so no one can get chip anymore */
  782. return -EIO;
  783. case FL_POINT:
  784. /* Only if there's no operation suspended... */
  785. if (mode == FL_READY && chip->oldstate == FL_READY)
  786. return 0;
  787. fallthrough;
  788. default:
  789. sleep:
  790. set_current_state(TASK_UNINTERRUPTIBLE);
  791. add_wait_queue(&chip->wq, &wait);
  792. mutex_unlock(&chip->mutex);
  793. schedule();
  794. remove_wait_queue(&chip->wq, &wait);
  795. mutex_lock(&chip->mutex);
  796. return -EAGAIN;
  797. }
  798. }
  799. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  800. {
  801. int ret;
  802. DECLARE_WAITQUEUE(wait, current);
  803. retry:
  804. if (chip->priv &&
  805. (mode == FL_WRITING || mode == FL_ERASING || mode == FL_OTP_WRITE
  806. || mode == FL_SHUTDOWN) && chip->state != FL_SYNCING) {
  807. /*
  808. * OK. We have possibility for contention on the write/erase
  809. * operations which are global to the real chip and not per
  810. * partition. So let's fight it over in the partition which
  811. * currently has authority on the operation.
  812. *
  813. * The rules are as follows:
  814. *
  815. * - any write operation must own shared->writing.
  816. *
  817. * - any erase operation must own _both_ shared->writing and
  818. * shared->erasing.
  819. *
  820. * - contention arbitration is handled in the owner's context.
  821. *
  822. * The 'shared' struct can be read and/or written only when
  823. * its lock is taken.
  824. */
  825. struct flchip_shared *shared = chip->priv;
  826. struct flchip *contender;
  827. mutex_lock(&shared->lock);
  828. contender = shared->writing;
  829. if (contender && contender != chip) {
  830. /*
  831. * The engine to perform desired operation on this
  832. * partition is already in use by someone else.
  833. * Let's fight over it in the context of the chip
  834. * currently using it. If it is possible to suspend,
  835. * that other partition will do just that, otherwise
  836. * it'll happily send us to sleep. In any case, when
  837. * get_chip returns success we're clear to go ahead.
  838. */
  839. ret = mutex_trylock(&contender->mutex);
  840. mutex_unlock(&shared->lock);
  841. if (!ret)
  842. goto retry;
  843. mutex_unlock(&chip->mutex);
  844. ret = chip_ready(map, contender, contender->start, mode);
  845. mutex_lock(&chip->mutex);
  846. if (ret == -EAGAIN) {
  847. mutex_unlock(&contender->mutex);
  848. goto retry;
  849. }
  850. if (ret) {
  851. mutex_unlock(&contender->mutex);
  852. return ret;
  853. }
  854. mutex_lock(&shared->lock);
  855. /* We should not own chip if it is already
  856. * in FL_SYNCING state. Put contender and retry. */
  857. if (chip->state == FL_SYNCING) {
  858. put_chip(map, contender, contender->start);
  859. mutex_unlock(&contender->mutex);
  860. goto retry;
  861. }
  862. mutex_unlock(&contender->mutex);
  863. }
  864. /* Check if we already have suspended erase
  865. * on this chip. Sleep. */
  866. if (mode == FL_ERASING && shared->erasing
  867. && shared->erasing->oldstate == FL_ERASING) {
  868. mutex_unlock(&shared->lock);
  869. set_current_state(TASK_UNINTERRUPTIBLE);
  870. add_wait_queue(&chip->wq, &wait);
  871. mutex_unlock(&chip->mutex);
  872. schedule();
  873. remove_wait_queue(&chip->wq, &wait);
  874. mutex_lock(&chip->mutex);
  875. goto retry;
  876. }
  877. /* We now own it */
  878. shared->writing = chip;
  879. if (mode == FL_ERASING)
  880. shared->erasing = chip;
  881. mutex_unlock(&shared->lock);
  882. }
  883. ret = chip_ready(map, chip, adr, mode);
  884. if (ret == -EAGAIN)
  885. goto retry;
  886. return ret;
  887. }
  888. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
  889. {
  890. struct cfi_private *cfi = map->fldrv_priv;
  891. if (chip->priv) {
  892. struct flchip_shared *shared = chip->priv;
  893. mutex_lock(&shared->lock);
  894. if (shared->writing == chip && chip->oldstate == FL_READY) {
  895. /* We own the ability to write, but we're done */
  896. shared->writing = shared->erasing;
  897. if (shared->writing && shared->writing != chip) {
  898. /* give back ownership to who we loaned it from */
  899. struct flchip *loaner = shared->writing;
  900. mutex_lock(&loaner->mutex);
  901. mutex_unlock(&shared->lock);
  902. mutex_unlock(&chip->mutex);
  903. put_chip(map, loaner, loaner->start);
  904. mutex_lock(&chip->mutex);
  905. mutex_unlock(&loaner->mutex);
  906. wake_up(&chip->wq);
  907. return;
  908. }
  909. shared->erasing = NULL;
  910. shared->writing = NULL;
  911. } else if (shared->erasing == chip && shared->writing != chip) {
  912. /*
  913. * We own the ability to erase without the ability
  914. * to write, which means the erase was suspended
  915. * and some other partition is currently writing.
  916. * Don't let the switch below mess things up since
  917. * we don't have ownership to resume anything.
  918. */
  919. mutex_unlock(&shared->lock);
  920. wake_up(&chip->wq);
  921. return;
  922. }
  923. mutex_unlock(&shared->lock);
  924. }
  925. switch(chip->oldstate) {
  926. case FL_ERASING:
  927. /* What if one interleaved chip has finished and the
  928. other hasn't? The old code would leave the finished
  929. one in READY mode. That's bad, and caused -EROFS
  930. errors to be returned from do_erase_oneblock because
  931. that's the only bit it checked for at the time.
  932. As the state machine appears to explicitly allow
  933. sending the 0x70 (Read Status) command to an erasing
  934. chip and expecting it to be ignored, that's what we
  935. do. */
  936. map_write(map, CMD(0xd0), chip->in_progress_block_addr);
  937. map_write(map, CMD(0x70), chip->in_progress_block_addr);
  938. chip->oldstate = FL_READY;
  939. chip->state = FL_ERASING;
  940. break;
  941. case FL_XIP_WHILE_ERASING:
  942. chip->state = chip->oldstate;
  943. chip->oldstate = FL_READY;
  944. break;
  945. case FL_READY:
  946. case FL_STATUS:
  947. case FL_JEDEC_QUERY:
  948. break;
  949. default:
  950. printk(KERN_ERR "%s: put_chip() called with oldstate %d!!\n", map->name, chip->oldstate);
  951. }
  952. wake_up(&chip->wq);
  953. }
  954. #ifdef CONFIG_MTD_XIP
  955. /*
  956. * No interrupt what so ever can be serviced while the flash isn't in array
  957. * mode. This is ensured by the xip_disable() and xip_enable() functions
  958. * enclosing any code path where the flash is known not to be in array mode.
  959. * And within a XIP disabled code path, only functions marked with __xipram
  960. * may be called and nothing else (it's a good thing to inspect generated
  961. * assembly to make sure inline functions were actually inlined and that gcc
  962. * didn't emit calls to its own support functions). Also configuring MTD CFI
  963. * support to a single buswidth and a single interleave is also recommended.
  964. */
  965. static void xip_disable(struct map_info *map, struct flchip *chip,
  966. unsigned long adr)
  967. {
  968. /* TODO: chips with no XIP use should ignore and return */
  969. (void) map_read(map, adr); /* ensure mmu mapping is up to date */
  970. local_irq_disable();
  971. }
  972. static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
  973. unsigned long adr)
  974. {
  975. struct cfi_private *cfi = map->fldrv_priv;
  976. if (chip->state != FL_POINT && chip->state != FL_READY) {
  977. map_write(map, CMD(0xff), adr);
  978. chip->state = FL_READY;
  979. }
  980. (void) map_read(map, adr);
  981. xip_iprefetch();
  982. local_irq_enable();
  983. }
  984. /*
  985. * When a delay is required for the flash operation to complete, the
  986. * xip_wait_for_operation() function is polling for both the given timeout
  987. * and pending (but still masked) hardware interrupts. Whenever there is an
  988. * interrupt pending then the flash erase or write operation is suspended,
  989. * array mode restored and interrupts unmasked. Task scheduling might also
  990. * happen at that point. The CPU eventually returns from the interrupt or
  991. * the call to schedule() and the suspended flash operation is resumed for
  992. * the remaining of the delay period.
  993. *
  994. * Warning: this function _will_ fool interrupt latency tracing tools.
  995. */
  996. static int __xipram xip_wait_for_operation(
  997. struct map_info *map, struct flchip *chip,
  998. unsigned long adr, unsigned int chip_op_time_max)
  999. {
  1000. struct cfi_private *cfi = map->fldrv_priv;
  1001. struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
  1002. map_word status, OK = CMD(0x80);
  1003. unsigned long usec, suspended, start, done;
  1004. flstate_t oldstate, newstate;
  1005. start = xip_currtime();
  1006. usec = chip_op_time_max;
  1007. if (usec == 0)
  1008. usec = 500000;
  1009. done = 0;
  1010. do {
  1011. cpu_relax();
  1012. if (xip_irqpending() && cfip &&
  1013. ((chip->state == FL_ERASING && (cfip->FeatureSupport&2)) ||
  1014. (chip->state == FL_WRITING && (cfip->FeatureSupport&4))) &&
  1015. (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
  1016. /*
  1017. * Let's suspend the erase or write operation when
  1018. * supported. Note that we currently don't try to
  1019. * suspend interleaved chips if there is already
  1020. * another operation suspended (imagine what happens
  1021. * when one chip was already done with the current
  1022. * operation while another chip suspended it, then
  1023. * we resume the whole thing at once). Yes, it
  1024. * can happen!
  1025. */
  1026. usec -= done;
  1027. map_write(map, CMD(0xb0), adr);
  1028. map_write(map, CMD(0x70), adr);
  1029. suspended = xip_currtime();
  1030. do {
  1031. if (xip_elapsed_since(suspended) > 100000) {
  1032. /*
  1033. * The chip doesn't want to suspend
  1034. * after waiting for 100 msecs.
  1035. * This is a critical error but there
  1036. * is not much we can do here.
  1037. */
  1038. return -EIO;
  1039. }
  1040. status = map_read(map, adr);
  1041. } while (!map_word_andequal(map, status, OK, OK));
  1042. /* Suspend succeeded */
  1043. oldstate = chip->state;
  1044. if (oldstate == FL_ERASING) {
  1045. if (!map_word_bitsset(map, status, CMD(0x40)))
  1046. break;
  1047. newstate = FL_XIP_WHILE_ERASING;
  1048. chip->erase_suspended = 1;
  1049. } else {
  1050. if (!map_word_bitsset(map, status, CMD(0x04)))
  1051. break;
  1052. newstate = FL_XIP_WHILE_WRITING;
  1053. chip->write_suspended = 1;
  1054. }
  1055. chip->state = newstate;
  1056. map_write(map, CMD(0xff), adr);
  1057. (void) map_read(map, adr);
  1058. xip_iprefetch();
  1059. local_irq_enable();
  1060. mutex_unlock(&chip->mutex);
  1061. xip_iprefetch();
  1062. cond_resched();
  1063. /*
  1064. * We're back. However someone else might have
  1065. * decided to go write to the chip if we are in
  1066. * a suspended erase state. If so let's wait
  1067. * until it's done.
  1068. */
  1069. mutex_lock(&chip->mutex);
  1070. while (chip->state != newstate) {
  1071. DECLARE_WAITQUEUE(wait, current);
  1072. set_current_state(TASK_UNINTERRUPTIBLE);
  1073. add_wait_queue(&chip->wq, &wait);
  1074. mutex_unlock(&chip->mutex);
  1075. schedule();
  1076. remove_wait_queue(&chip->wq, &wait);
  1077. mutex_lock(&chip->mutex);
  1078. }
  1079. /* Disallow XIP again */
  1080. local_irq_disable();
  1081. /* Resume the write or erase operation */
  1082. map_write(map, CMD(0xd0), adr);
  1083. map_write(map, CMD(0x70), adr);
  1084. chip->state = oldstate;
  1085. start = xip_currtime();
  1086. } else if (usec >= 1000000/HZ) {
  1087. /*
  1088. * Try to save on CPU power when waiting delay
  1089. * is at least a system timer tick period.
  1090. * No need to be extremely accurate here.
  1091. */
  1092. xip_cpu_idle();
  1093. }
  1094. status = map_read(map, adr);
  1095. done = xip_elapsed_since(start);
  1096. } while (!map_word_andequal(map, status, OK, OK)
  1097. && done < usec);
  1098. return (done >= usec) ? -ETIME : 0;
  1099. }
  1100. /*
  1101. * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
  1102. * the flash is actively programming or erasing since we have to poll for
  1103. * the operation to complete anyway. We can't do that in a generic way with
  1104. * a XIP setup so do it before the actual flash operation in this case
  1105. * and stub it out from INVAL_CACHE_AND_WAIT.
  1106. */
  1107. #define XIP_INVAL_CACHED_RANGE(map, from, size) \
  1108. INVALIDATE_CACHED_RANGE(map, from, size)
  1109. #define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, usec, usec_max) \
  1110. xip_wait_for_operation(map, chip, cmd_adr, usec_max)
  1111. #else
  1112. #define xip_disable(map, chip, adr)
  1113. #define xip_enable(map, chip, adr)
  1114. #define XIP_INVAL_CACHED_RANGE(x...)
  1115. #define INVAL_CACHE_AND_WAIT inval_cache_and_wait_for_operation
  1116. static int inval_cache_and_wait_for_operation(
  1117. struct map_info *map, struct flchip *chip,
  1118. unsigned long cmd_adr, unsigned long inval_adr, int inval_len,
  1119. unsigned int chip_op_time, unsigned int chip_op_time_max)
  1120. {
  1121. struct cfi_private *cfi = map->fldrv_priv;
  1122. map_word status, status_OK = CMD(0x80);
  1123. int chip_state = chip->state;
  1124. unsigned int timeo, sleep_time, reset_timeo;
  1125. mutex_unlock(&chip->mutex);
  1126. if (inval_len)
  1127. INVALIDATE_CACHED_RANGE(map, inval_adr, inval_len);
  1128. mutex_lock(&chip->mutex);
  1129. timeo = chip_op_time_max;
  1130. if (!timeo)
  1131. timeo = 500000;
  1132. reset_timeo = timeo;
  1133. sleep_time = chip_op_time / 2;
  1134. for (;;) {
  1135. if (chip->state != chip_state) {
  1136. /* Someone's suspended the operation: sleep */
  1137. DECLARE_WAITQUEUE(wait, current);
  1138. set_current_state(TASK_UNINTERRUPTIBLE);
  1139. add_wait_queue(&chip->wq, &wait);
  1140. mutex_unlock(&chip->mutex);
  1141. schedule();
  1142. remove_wait_queue(&chip->wq, &wait);
  1143. mutex_lock(&chip->mutex);
  1144. continue;
  1145. }
  1146. status = map_read(map, cmd_adr);
  1147. if (map_word_andequal(map, status, status_OK, status_OK))
  1148. break;
  1149. if (chip->erase_suspended && chip_state == FL_ERASING) {
  1150. /* Erase suspend occurred while sleep: reset timeout */
  1151. timeo = reset_timeo;
  1152. chip->erase_suspended = 0;
  1153. }
  1154. if (chip->write_suspended && chip_state == FL_WRITING) {
  1155. /* Write suspend occurred while sleep: reset timeout */
  1156. timeo = reset_timeo;
  1157. chip->write_suspended = 0;
  1158. }
  1159. if (!timeo) {
  1160. map_write(map, CMD(0x70), cmd_adr);
  1161. chip->state = FL_STATUS;
  1162. return -ETIME;
  1163. }
  1164. /* OK Still waiting. Drop the lock, wait a while and retry. */
  1165. mutex_unlock(&chip->mutex);
  1166. if (sleep_time >= 1000000/HZ) {
  1167. /*
  1168. * Half of the normal delay still remaining
  1169. * can be performed with a sleeping delay instead
  1170. * of busy waiting.
  1171. */
  1172. msleep(sleep_time/1000);
  1173. timeo -= sleep_time;
  1174. sleep_time = 1000000/HZ;
  1175. } else {
  1176. udelay(1);
  1177. cond_resched();
  1178. timeo--;
  1179. }
  1180. mutex_lock(&chip->mutex);
  1181. }
  1182. /* Done and happy. */
  1183. chip->state = FL_STATUS;
  1184. return 0;
  1185. }
  1186. #endif
  1187. #define WAIT_TIMEOUT(map, chip, adr, udelay, udelay_max) \
  1188. INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, udelay, udelay_max);
  1189. static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
  1190. {
  1191. unsigned long cmd_addr;
  1192. struct cfi_private *cfi = map->fldrv_priv;
  1193. int ret;
  1194. adr += chip->start;
  1195. /* Ensure cmd read/writes are aligned. */
  1196. cmd_addr = adr & ~(map_bankwidth(map)-1);
  1197. mutex_lock(&chip->mutex);
  1198. ret = get_chip(map, chip, cmd_addr, FL_POINT);
  1199. if (!ret) {
  1200. if (chip->state != FL_POINT && chip->state != FL_READY)
  1201. map_write(map, CMD(0xff), cmd_addr);
  1202. chip->state = FL_POINT;
  1203. chip->ref_point_counter++;
  1204. }
  1205. mutex_unlock(&chip->mutex);
  1206. return ret;
  1207. }
  1208. static int cfi_intelext_point(struct mtd_info *mtd, loff_t from, size_t len,
  1209. size_t *retlen, void **virt, resource_size_t *phys)
  1210. {
  1211. struct map_info *map = mtd->priv;
  1212. struct cfi_private *cfi = map->fldrv_priv;
  1213. unsigned long ofs, last_end = 0;
  1214. int chipnum;
  1215. int ret;
  1216. if (!map->virt)
  1217. return -EINVAL;
  1218. /* Now lock the chip(s) to POINT state */
  1219. /* ofs: offset within the first chip that the first read should start */
  1220. chipnum = (from >> cfi->chipshift);
  1221. ofs = from - (chipnum << cfi->chipshift);
  1222. *virt = map->virt + cfi->chips[chipnum].start + ofs;
  1223. if (phys)
  1224. *phys = map->phys + cfi->chips[chipnum].start + ofs;
  1225. while (len) {
  1226. unsigned long thislen;
  1227. if (chipnum >= cfi->numchips)
  1228. break;
  1229. /* We cannot point across chips that are virtually disjoint */
  1230. if (!last_end)
  1231. last_end = cfi->chips[chipnum].start;
  1232. else if (cfi->chips[chipnum].start != last_end)
  1233. break;
  1234. if ((len + ofs -1) >> cfi->chipshift)
  1235. thislen = (1<<cfi->chipshift) - ofs;
  1236. else
  1237. thislen = len;
  1238. ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen);
  1239. if (ret)
  1240. break;
  1241. *retlen += thislen;
  1242. len -= thislen;
  1243. ofs = 0;
  1244. last_end += 1 << cfi->chipshift;
  1245. chipnum++;
  1246. }
  1247. return 0;
  1248. }
  1249. static int cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  1250. {
  1251. struct map_info *map = mtd->priv;
  1252. struct cfi_private *cfi = map->fldrv_priv;
  1253. unsigned long ofs;
  1254. int chipnum, err = 0;
  1255. /* Now unlock the chip(s) POINT state */
  1256. /* ofs: offset within the first chip that the first read should start */
  1257. chipnum = (from >> cfi->chipshift);
  1258. ofs = from - (chipnum << cfi->chipshift);
  1259. while (len && !err) {
  1260. unsigned long thislen;
  1261. struct flchip *chip;
  1262. chip = &cfi->chips[chipnum];
  1263. if (chipnum >= cfi->numchips)
  1264. break;
  1265. if ((len + ofs -1) >> cfi->chipshift)
  1266. thislen = (1<<cfi->chipshift) - ofs;
  1267. else
  1268. thislen = len;
  1269. mutex_lock(&chip->mutex);
  1270. if (chip->state == FL_POINT) {
  1271. chip->ref_point_counter--;
  1272. if(chip->ref_point_counter == 0)
  1273. chip->state = FL_READY;
  1274. } else {
  1275. printk(KERN_ERR "%s: Error: unpoint called on non pointed region\n", map->name);
  1276. err = -EINVAL;
  1277. }
  1278. put_chip(map, chip, chip->start);
  1279. mutex_unlock(&chip->mutex);
  1280. len -= thislen;
  1281. ofs = 0;
  1282. chipnum++;
  1283. }
  1284. return err;
  1285. }
  1286. static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  1287. {
  1288. unsigned long cmd_addr;
  1289. struct cfi_private *cfi = map->fldrv_priv;
  1290. int ret;
  1291. adr += chip->start;
  1292. /* Ensure cmd read/writes are aligned. */
  1293. cmd_addr = adr & ~(map_bankwidth(map)-1);
  1294. mutex_lock(&chip->mutex);
  1295. ret = get_chip(map, chip, cmd_addr, FL_READY);
  1296. if (ret) {
  1297. mutex_unlock(&chip->mutex);
  1298. return ret;
  1299. }
  1300. if (chip->state != FL_POINT && chip->state != FL_READY) {
  1301. map_write(map, CMD(0xff), cmd_addr);
  1302. chip->state = FL_READY;
  1303. }
  1304. map_copy_from(map, buf, adr, len);
  1305. put_chip(map, chip, cmd_addr);
  1306. mutex_unlock(&chip->mutex);
  1307. return 0;
  1308. }
  1309. static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  1310. {
  1311. struct map_info *map = mtd->priv;
  1312. struct cfi_private *cfi = map->fldrv_priv;
  1313. unsigned long ofs;
  1314. int chipnum;
  1315. int ret = 0;
  1316. /* ofs: offset within the first chip that the first read should start */
  1317. chipnum = (from >> cfi->chipshift);
  1318. ofs = from - (chipnum << cfi->chipshift);
  1319. while (len) {
  1320. unsigned long thislen;
  1321. if (chipnum >= cfi->numchips)
  1322. break;
  1323. if ((len + ofs -1) >> cfi->chipshift)
  1324. thislen = (1<<cfi->chipshift) - ofs;
  1325. else
  1326. thislen = len;
  1327. ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  1328. if (ret)
  1329. break;
  1330. *retlen += thislen;
  1331. len -= thislen;
  1332. buf += thislen;
  1333. ofs = 0;
  1334. chipnum++;
  1335. }
  1336. return ret;
  1337. }
  1338. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
  1339. unsigned long adr, map_word datum, int mode)
  1340. {
  1341. struct cfi_private *cfi = map->fldrv_priv;
  1342. map_word status, write_cmd;
  1343. int ret;
  1344. adr += chip->start;
  1345. switch (mode) {
  1346. case FL_WRITING:
  1347. write_cmd = (cfi->cfiq->P_ID != P_ID_INTEL_PERFORMANCE) ? CMD(0x40) : CMD(0x41);
  1348. break;
  1349. case FL_OTP_WRITE:
  1350. write_cmd = CMD(0xc0);
  1351. break;
  1352. default:
  1353. return -EINVAL;
  1354. }
  1355. mutex_lock(&chip->mutex);
  1356. ret = get_chip(map, chip, adr, mode);
  1357. if (ret) {
  1358. mutex_unlock(&chip->mutex);
  1359. return ret;
  1360. }
  1361. XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
  1362. ENABLE_VPP(map);
  1363. xip_disable(map, chip, adr);
  1364. map_write(map, write_cmd, adr);
  1365. map_write(map, datum, adr);
  1366. chip->state = mode;
  1367. ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
  1368. adr, map_bankwidth(map),
  1369. chip->word_write_time,
  1370. chip->word_write_time_max);
  1371. if (ret) {
  1372. xip_enable(map, chip, adr);
  1373. printk(KERN_ERR "%s: word write error (status timeout)\n", map->name);
  1374. goto out;
  1375. }
  1376. /* check for errors */
  1377. status = map_read(map, adr);
  1378. if (map_word_bitsset(map, status, CMD(0x1a))) {
  1379. unsigned long chipstatus = MERGESTATUS(status);
  1380. /* reset status */
  1381. map_write(map, CMD(0x50), adr);
  1382. map_write(map, CMD(0x70), adr);
  1383. xip_enable(map, chip, adr);
  1384. if (chipstatus & 0x02) {
  1385. ret = -EROFS;
  1386. } else if (chipstatus & 0x08) {
  1387. printk(KERN_ERR "%s: word write error (bad VPP)\n", map->name);
  1388. ret = -EIO;
  1389. } else {
  1390. printk(KERN_ERR "%s: word write error (status 0x%lx)\n", map->name, chipstatus);
  1391. ret = -EINVAL;
  1392. }
  1393. goto out;
  1394. }
  1395. xip_enable(map, chip, adr);
  1396. out: DISABLE_VPP(map);
  1397. put_chip(map, chip, adr);
  1398. mutex_unlock(&chip->mutex);
  1399. return ret;
  1400. }
  1401. static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
  1402. {
  1403. struct map_info *map = mtd->priv;
  1404. struct cfi_private *cfi = map->fldrv_priv;
  1405. int ret;
  1406. int chipnum;
  1407. unsigned long ofs;
  1408. chipnum = to >> cfi->chipshift;
  1409. ofs = to - (chipnum << cfi->chipshift);
  1410. /* If it's not bus-aligned, do the first byte write */
  1411. if (ofs & (map_bankwidth(map)-1)) {
  1412. unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
  1413. int gap = ofs - bus_ofs;
  1414. int n;
  1415. map_word datum;
  1416. n = min_t(int, len, map_bankwidth(map)-gap);
  1417. datum = map_word_ff(map);
  1418. datum = map_word_load_partial(map, datum, buf, gap, n);
  1419. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1420. bus_ofs, datum, FL_WRITING);
  1421. if (ret)
  1422. return ret;
  1423. len -= n;
  1424. ofs += n;
  1425. buf += n;
  1426. (*retlen) += n;
  1427. if (ofs >> cfi->chipshift) {
  1428. chipnum ++;
  1429. ofs = 0;
  1430. if (chipnum == cfi->numchips)
  1431. return 0;
  1432. }
  1433. }
  1434. while(len >= map_bankwidth(map)) {
  1435. map_word datum = map_word_load(map, buf);
  1436. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1437. ofs, datum, FL_WRITING);
  1438. if (ret)
  1439. return ret;
  1440. ofs += map_bankwidth(map);
  1441. buf += map_bankwidth(map);
  1442. (*retlen) += map_bankwidth(map);
  1443. len -= map_bankwidth(map);
  1444. if (ofs >> cfi->chipshift) {
  1445. chipnum ++;
  1446. ofs = 0;
  1447. if (chipnum == cfi->numchips)
  1448. return 0;
  1449. }
  1450. }
  1451. if (len & (map_bankwidth(map)-1)) {
  1452. map_word datum;
  1453. datum = map_word_ff(map);
  1454. datum = map_word_load_partial(map, datum, buf, 0, len);
  1455. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1456. ofs, datum, FL_WRITING);
  1457. if (ret)
  1458. return ret;
  1459. (*retlen) += len;
  1460. }
  1461. return 0;
  1462. }
  1463. static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
  1464. unsigned long adr, const struct kvec **pvec,
  1465. unsigned long *pvec_seek, int len)
  1466. {
  1467. struct cfi_private *cfi = map->fldrv_priv;
  1468. map_word status, write_cmd, datum;
  1469. unsigned long cmd_adr;
  1470. int ret, wbufsize, word_gap, words;
  1471. const struct kvec *vec;
  1472. unsigned long vec_seek;
  1473. unsigned long initial_adr;
  1474. int initial_len = len;
  1475. wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1476. adr += chip->start;
  1477. initial_adr = adr;
  1478. cmd_adr = adr & ~(wbufsize-1);
  1479. /* Sharp LH28F640BF chips need the first address for the
  1480. * Page Buffer Program command. See Table 5 of
  1481. * LH28F320BF, LH28F640BF, LH28F128BF Series (Appendix FUM00701) */
  1482. if (is_LH28F640BF(cfi))
  1483. cmd_adr = adr;
  1484. /* Let's determine this according to the interleave only once */
  1485. write_cmd = (cfi->cfiq->P_ID != P_ID_INTEL_PERFORMANCE) ? CMD(0xe8) : CMD(0xe9);
  1486. mutex_lock(&chip->mutex);
  1487. ret = get_chip(map, chip, cmd_adr, FL_WRITING);
  1488. if (ret) {
  1489. mutex_unlock(&chip->mutex);
  1490. return ret;
  1491. }
  1492. XIP_INVAL_CACHED_RANGE(map, initial_adr, initial_len);
  1493. ENABLE_VPP(map);
  1494. xip_disable(map, chip, cmd_adr);
  1495. /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
  1496. [...], the device will not accept any more Write to Buffer commands".
  1497. So we must check here and reset those bits if they're set. Otherwise
  1498. we're just pissing in the wind */
  1499. if (chip->state != FL_STATUS) {
  1500. map_write(map, CMD(0x70), cmd_adr);
  1501. chip->state = FL_STATUS;
  1502. }
  1503. status = map_read(map, cmd_adr);
  1504. if (map_word_bitsset(map, status, CMD(0x30))) {
  1505. xip_enable(map, chip, cmd_adr);
  1506. printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]);
  1507. xip_disable(map, chip, cmd_adr);
  1508. map_write(map, CMD(0x50), cmd_adr);
  1509. map_write(map, CMD(0x70), cmd_adr);
  1510. }
  1511. chip->state = FL_WRITING_TO_BUFFER;
  1512. map_write(map, write_cmd, cmd_adr);
  1513. ret = WAIT_TIMEOUT(map, chip, cmd_adr, 0, 0);
  1514. if (ret) {
  1515. /* Argh. Not ready for write to buffer */
  1516. map_word Xstatus = map_read(map, cmd_adr);
  1517. map_write(map, CMD(0x70), cmd_adr);
  1518. chip->state = FL_STATUS;
  1519. status = map_read(map, cmd_adr);
  1520. map_write(map, CMD(0x50), cmd_adr);
  1521. map_write(map, CMD(0x70), cmd_adr);
  1522. xip_enable(map, chip, cmd_adr);
  1523. printk(KERN_ERR "%s: Chip not ready for buffer write. Xstatus = %lx, status = %lx\n",
  1524. map->name, Xstatus.x[0], status.x[0]);
  1525. goto out;
  1526. }
  1527. /* Figure out the number of words to write */
  1528. word_gap = (-adr & (map_bankwidth(map)-1));
  1529. words = DIV_ROUND_UP(len - word_gap, map_bankwidth(map));
  1530. if (!word_gap) {
  1531. words--;
  1532. } else {
  1533. word_gap = map_bankwidth(map) - word_gap;
  1534. adr -= word_gap;
  1535. datum = map_word_ff(map);
  1536. }
  1537. /* Write length of data to come */
  1538. map_write(map, CMD(words), cmd_adr );
  1539. /* Write data */
  1540. vec = *pvec;
  1541. vec_seek = *pvec_seek;
  1542. do {
  1543. int n = map_bankwidth(map) - word_gap;
  1544. if (n > vec->iov_len - vec_seek)
  1545. n = vec->iov_len - vec_seek;
  1546. if (n > len)
  1547. n = len;
  1548. if (!word_gap && len < map_bankwidth(map))
  1549. datum = map_word_ff(map);
  1550. datum = map_word_load_partial(map, datum,
  1551. vec->iov_base + vec_seek,
  1552. word_gap, n);
  1553. len -= n;
  1554. word_gap += n;
  1555. if (!len || word_gap == map_bankwidth(map)) {
  1556. map_write(map, datum, adr);
  1557. adr += map_bankwidth(map);
  1558. word_gap = 0;
  1559. }
  1560. vec_seek += n;
  1561. if (vec_seek == vec->iov_len) {
  1562. vec++;
  1563. vec_seek = 0;
  1564. }
  1565. } while (len);
  1566. *pvec = vec;
  1567. *pvec_seek = vec_seek;
  1568. /* GO GO GO */
  1569. map_write(map, CMD(0xd0), cmd_adr);
  1570. chip->state = FL_WRITING;
  1571. ret = INVAL_CACHE_AND_WAIT(map, chip, cmd_adr,
  1572. initial_adr, initial_len,
  1573. chip->buffer_write_time,
  1574. chip->buffer_write_time_max);
  1575. if (ret) {
  1576. map_write(map, CMD(0x70), cmd_adr);
  1577. chip->state = FL_STATUS;
  1578. xip_enable(map, chip, cmd_adr);
  1579. printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name);
  1580. goto out;
  1581. }
  1582. /* check for errors */
  1583. status = map_read(map, cmd_adr);
  1584. if (map_word_bitsset(map, status, CMD(0x1a))) {
  1585. unsigned long chipstatus = MERGESTATUS(status);
  1586. /* reset status */
  1587. map_write(map, CMD(0x50), cmd_adr);
  1588. map_write(map, CMD(0x70), cmd_adr);
  1589. xip_enable(map, chip, cmd_adr);
  1590. if (chipstatus & 0x02) {
  1591. ret = -EROFS;
  1592. } else if (chipstatus & 0x08) {
  1593. printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name);
  1594. ret = -EIO;
  1595. } else {
  1596. printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
  1597. ret = -EINVAL;
  1598. }
  1599. goto out;
  1600. }
  1601. xip_enable(map, chip, cmd_adr);
  1602. out: DISABLE_VPP(map);
  1603. put_chip(map, chip, cmd_adr);
  1604. mutex_unlock(&chip->mutex);
  1605. return ret;
  1606. }
  1607. static int cfi_intelext_writev (struct mtd_info *mtd, const struct kvec *vecs,
  1608. unsigned long count, loff_t to, size_t *retlen)
  1609. {
  1610. struct map_info *map = mtd->priv;
  1611. struct cfi_private *cfi = map->fldrv_priv;
  1612. int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1613. int ret;
  1614. int chipnum;
  1615. unsigned long ofs, vec_seek, i;
  1616. size_t len = 0;
  1617. for (i = 0; i < count; i++)
  1618. len += vecs[i].iov_len;
  1619. if (!len)
  1620. return 0;
  1621. chipnum = to >> cfi->chipshift;
  1622. ofs = to - (chipnum << cfi->chipshift);
  1623. vec_seek = 0;
  1624. do {
  1625. /* We must not cross write block boundaries */
  1626. int size = wbufsize - (ofs & (wbufsize-1));
  1627. if (size > len)
  1628. size = len;
  1629. ret = do_write_buffer(map, &cfi->chips[chipnum],
  1630. ofs, &vecs, &vec_seek, size);
  1631. if (ret)
  1632. return ret;
  1633. ofs += size;
  1634. (*retlen) += size;
  1635. len -= size;
  1636. if (ofs >> cfi->chipshift) {
  1637. chipnum ++;
  1638. ofs = 0;
  1639. if (chipnum == cfi->numchips)
  1640. return 0;
  1641. }
  1642. /* Be nice and reschedule with the chip in a usable state for other
  1643. processes. */
  1644. cond_resched();
  1645. } while (len);
  1646. return 0;
  1647. }
  1648. static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to,
  1649. size_t len, size_t *retlen, const u_char *buf)
  1650. {
  1651. struct kvec vec;
  1652. vec.iov_base = (void *) buf;
  1653. vec.iov_len = len;
  1654. return cfi_intelext_writev(mtd, &vec, 1, to, retlen);
  1655. }
  1656. static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip,
  1657. unsigned long adr, int len, void *thunk)
  1658. {
  1659. struct cfi_private *cfi = map->fldrv_priv;
  1660. map_word status;
  1661. int retries = 3;
  1662. int ret;
  1663. adr += chip->start;
  1664. retry:
  1665. mutex_lock(&chip->mutex);
  1666. ret = get_chip(map, chip, adr, FL_ERASING);
  1667. if (ret) {
  1668. mutex_unlock(&chip->mutex);
  1669. return ret;
  1670. }
  1671. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1672. ENABLE_VPP(map);
  1673. xip_disable(map, chip, adr);
  1674. /* Clear the status register first */
  1675. map_write(map, CMD(0x50), adr);
  1676. /* Now erase */
  1677. map_write(map, CMD(0x20), adr);
  1678. map_write(map, CMD(0xD0), adr);
  1679. chip->state = FL_ERASING;
  1680. chip->erase_suspended = 0;
  1681. chip->in_progress_block_addr = adr;
  1682. chip->in_progress_block_mask = ~(len - 1);
  1683. ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
  1684. adr, len,
  1685. chip->erase_time,
  1686. chip->erase_time_max);
  1687. if (ret) {
  1688. map_write(map, CMD(0x70), adr);
  1689. chip->state = FL_STATUS;
  1690. xip_enable(map, chip, adr);
  1691. printk(KERN_ERR "%s: block erase error: (status timeout)\n", map->name);
  1692. goto out;
  1693. }
  1694. /* We've broken this before. It doesn't hurt to be safe */
  1695. map_write(map, CMD(0x70), adr);
  1696. chip->state = FL_STATUS;
  1697. status = map_read(map, adr);
  1698. /* check for errors */
  1699. if (map_word_bitsset(map, status, CMD(0x3a))) {
  1700. unsigned long chipstatus = MERGESTATUS(status);
  1701. /* Reset the error bits */
  1702. map_write(map, CMD(0x50), adr);
  1703. map_write(map, CMD(0x70), adr);
  1704. xip_enable(map, chip, adr);
  1705. if ((chipstatus & 0x30) == 0x30) {
  1706. printk(KERN_ERR "%s: block erase error: (bad command sequence, status 0x%lx)\n", map->name, chipstatus);
  1707. ret = -EINVAL;
  1708. } else if (chipstatus & 0x02) {
  1709. /* Protection bit set */
  1710. ret = -EROFS;
  1711. } else if (chipstatus & 0x8) {
  1712. /* Voltage */
  1713. printk(KERN_ERR "%s: block erase error: (bad VPP)\n", map->name);
  1714. ret = -EIO;
  1715. } else if (chipstatus & 0x20 && retries--) {
  1716. printk(KERN_DEBUG "block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr, chipstatus);
  1717. DISABLE_VPP(map);
  1718. put_chip(map, chip, adr);
  1719. mutex_unlock(&chip->mutex);
  1720. goto retry;
  1721. } else {
  1722. printk(KERN_ERR "%s: block erase failed at 0x%08lx (status 0x%lx)\n", map->name, adr, chipstatus);
  1723. ret = -EIO;
  1724. }
  1725. goto out;
  1726. }
  1727. xip_enable(map, chip, adr);
  1728. out: DISABLE_VPP(map);
  1729. put_chip(map, chip, adr);
  1730. mutex_unlock(&chip->mutex);
  1731. return ret;
  1732. }
  1733. static int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
  1734. {
  1735. return cfi_varsize_frob(mtd, do_erase_oneblock, instr->addr,
  1736. instr->len, NULL);
  1737. }
  1738. static void cfi_intelext_sync (struct mtd_info *mtd)
  1739. {
  1740. struct map_info *map = mtd->priv;
  1741. struct cfi_private *cfi = map->fldrv_priv;
  1742. int i;
  1743. struct flchip *chip;
  1744. int ret = 0;
  1745. for (i=0; !ret && i<cfi->numchips; i++) {
  1746. chip = &cfi->chips[i];
  1747. mutex_lock(&chip->mutex);
  1748. ret = get_chip(map, chip, chip->start, FL_SYNCING);
  1749. if (!ret) {
  1750. chip->oldstate = chip->state;
  1751. chip->state = FL_SYNCING;
  1752. /* No need to wake_up() on this state change -
  1753. * as the whole point is that nobody can do anything
  1754. * with the chip now anyway.
  1755. */
  1756. }
  1757. mutex_unlock(&chip->mutex);
  1758. }
  1759. /* Unlock the chips again */
  1760. for (i--; i >=0; i--) {
  1761. chip = &cfi->chips[i];
  1762. mutex_lock(&chip->mutex);
  1763. if (chip->state == FL_SYNCING) {
  1764. chip->state = chip->oldstate;
  1765. chip->oldstate = FL_READY;
  1766. wake_up(&chip->wq);
  1767. }
  1768. mutex_unlock(&chip->mutex);
  1769. }
  1770. }
  1771. static int __xipram do_getlockstatus_oneblock(struct map_info *map,
  1772. struct flchip *chip,
  1773. unsigned long adr,
  1774. int len, void *thunk)
  1775. {
  1776. struct cfi_private *cfi = map->fldrv_priv;
  1777. int status, ofs_factor = cfi->interleave * cfi->device_type;
  1778. adr += chip->start;
  1779. xip_disable(map, chip, adr+(2*ofs_factor));
  1780. map_write(map, CMD(0x90), adr+(2*ofs_factor));
  1781. chip->state = FL_JEDEC_QUERY;
  1782. status = cfi_read_query(map, adr+(2*ofs_factor));
  1783. xip_enable(map, chip, 0);
  1784. return status;
  1785. }
  1786. #ifdef DEBUG_LOCK_BITS
  1787. static int __xipram do_printlockstatus_oneblock(struct map_info *map,
  1788. struct flchip *chip,
  1789. unsigned long adr,
  1790. int len, void *thunk)
  1791. {
  1792. printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
  1793. adr, do_getlockstatus_oneblock(map, chip, adr, len, thunk));
  1794. return 0;
  1795. }
  1796. #endif
  1797. #define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
  1798. #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
  1799. static int __xipram do_xxlock_oneblock(struct map_info *map, struct flchip *chip,
  1800. unsigned long adr, int len, void *thunk)
  1801. {
  1802. struct cfi_private *cfi = map->fldrv_priv;
  1803. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  1804. int mdelay;
  1805. int ret;
  1806. adr += chip->start;
  1807. mutex_lock(&chip->mutex);
  1808. ret = get_chip(map, chip, adr, FL_LOCKING);
  1809. if (ret) {
  1810. mutex_unlock(&chip->mutex);
  1811. return ret;
  1812. }
  1813. ENABLE_VPP(map);
  1814. xip_disable(map, chip, adr);
  1815. map_write(map, CMD(0x60), adr);
  1816. if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
  1817. map_write(map, CMD(0x01), adr);
  1818. chip->state = FL_LOCKING;
  1819. } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
  1820. map_write(map, CMD(0xD0), adr);
  1821. chip->state = FL_UNLOCKING;
  1822. } else
  1823. BUG();
  1824. /*
  1825. * If Instant Individual Block Locking supported then no need
  1826. * to delay.
  1827. */
  1828. /*
  1829. * Unlocking may take up to 1.4 seconds on some Intel flashes. So
  1830. * lets use a max of 1.5 seconds (1500ms) as timeout.
  1831. *
  1832. * See "Clear Block Lock-Bits Time" on page 40 in
  1833. * "3 Volt Intel StrataFlash Memory" 28F128J3,28F640J3,28F320J3 manual
  1834. * from February 2003
  1835. */
  1836. mdelay = (!extp || !(extp->FeatureSupport & (1 << 5))) ? 1500 : 0;
  1837. ret = WAIT_TIMEOUT(map, chip, adr, mdelay, mdelay * 1000);
  1838. if (ret) {
  1839. map_write(map, CMD(0x70), adr);
  1840. chip->state = FL_STATUS;
  1841. xip_enable(map, chip, adr);
  1842. printk(KERN_ERR "%s: block unlock error: (status timeout)\n", map->name);
  1843. goto out;
  1844. }
  1845. xip_enable(map, chip, adr);
  1846. out: DISABLE_VPP(map);
  1847. put_chip(map, chip, adr);
  1848. mutex_unlock(&chip->mutex);
  1849. return ret;
  1850. }
  1851. static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1852. {
  1853. int ret;
  1854. #ifdef DEBUG_LOCK_BITS
  1855. printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
  1856. __func__, ofs, len);
  1857. cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
  1858. ofs, len, NULL);
  1859. #endif
  1860. ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
  1861. ofs, len, DO_XXLOCK_ONEBLOCK_LOCK);
  1862. #ifdef DEBUG_LOCK_BITS
  1863. printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
  1864. __func__, ret);
  1865. cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
  1866. ofs, len, NULL);
  1867. #endif
  1868. return ret;
  1869. }
  1870. static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1871. {
  1872. int ret;
  1873. #ifdef DEBUG_LOCK_BITS
  1874. printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
  1875. __func__, ofs, len);
  1876. cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
  1877. ofs, len, NULL);
  1878. #endif
  1879. ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
  1880. ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK);
  1881. #ifdef DEBUG_LOCK_BITS
  1882. printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
  1883. __func__, ret);
  1884. cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
  1885. ofs, len, NULL);
  1886. #endif
  1887. return ret;
  1888. }
  1889. static int cfi_intelext_is_locked(struct mtd_info *mtd, loff_t ofs,
  1890. uint64_t len)
  1891. {
  1892. return cfi_varsize_frob(mtd, do_getlockstatus_oneblock,
  1893. ofs, len, NULL) ? 1 : 0;
  1894. }
  1895. #ifdef CONFIG_MTD_OTP
  1896. typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
  1897. u_long data_offset, u_char *buf, u_int size,
  1898. u_long prot_offset, u_int groupno, u_int groupsize);
  1899. static int __xipram
  1900. do_otp_read(struct map_info *map, struct flchip *chip, u_long offset,
  1901. u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
  1902. {
  1903. struct cfi_private *cfi = map->fldrv_priv;
  1904. int ret;
  1905. mutex_lock(&chip->mutex);
  1906. ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY);
  1907. if (ret) {
  1908. mutex_unlock(&chip->mutex);
  1909. return ret;
  1910. }
  1911. /* let's ensure we're not reading back cached data from array mode */
  1912. INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
  1913. xip_disable(map, chip, chip->start);
  1914. if (chip->state != FL_JEDEC_QUERY) {
  1915. map_write(map, CMD(0x90), chip->start);
  1916. chip->state = FL_JEDEC_QUERY;
  1917. }
  1918. map_copy_from(map, buf, chip->start + offset, size);
  1919. xip_enable(map, chip, chip->start);
  1920. /* then ensure we don't keep OTP data in the cache */
  1921. INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
  1922. put_chip(map, chip, chip->start);
  1923. mutex_unlock(&chip->mutex);
  1924. return 0;
  1925. }
  1926. static int
  1927. do_otp_write(struct map_info *map, struct flchip *chip, u_long offset,
  1928. u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
  1929. {
  1930. int ret;
  1931. while (size) {
  1932. unsigned long bus_ofs = offset & ~(map_bankwidth(map)-1);
  1933. int gap = offset - bus_ofs;
  1934. int n = min_t(int, size, map_bankwidth(map)-gap);
  1935. map_word datum = map_word_ff(map);
  1936. datum = map_word_load_partial(map, datum, buf, gap, n);
  1937. ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
  1938. if (ret)
  1939. return ret;
  1940. offset += n;
  1941. buf += n;
  1942. size -= n;
  1943. }
  1944. return 0;
  1945. }
  1946. static int
  1947. do_otp_lock(struct map_info *map, struct flchip *chip, u_long offset,
  1948. u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
  1949. {
  1950. struct cfi_private *cfi = map->fldrv_priv;
  1951. map_word datum;
  1952. /* make sure area matches group boundaries */
  1953. if (size != grpsz)
  1954. return -EXDEV;
  1955. datum = map_word_ff(map);
  1956. datum = map_word_clr(map, datum, CMD(1 << grpno));
  1957. return do_write_oneword(map, chip, prot, datum, FL_OTP_WRITE);
  1958. }
  1959. static int cfi_intelext_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1960. size_t *retlen, u_char *buf,
  1961. otp_op_t action, int user_regs)
  1962. {
  1963. struct map_info *map = mtd->priv;
  1964. struct cfi_private *cfi = map->fldrv_priv;
  1965. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  1966. struct flchip *chip;
  1967. struct cfi_intelext_otpinfo *otp;
  1968. u_long devsize, reg_prot_offset, data_offset;
  1969. u_int chip_num, chip_step, field, reg_fact_size, reg_user_size;
  1970. u_int groups, groupno, groupsize, reg_fact_groups, reg_user_groups;
  1971. int ret;
  1972. *retlen = 0;
  1973. /* Check that we actually have some OTP registers */
  1974. if (!extp || !(extp->FeatureSupport & 64) || !extp->NumProtectionFields)
  1975. return -ENODATA;
  1976. /* we need real chips here not virtual ones */
  1977. devsize = (1 << cfi->cfiq->DevSize) * cfi->interleave;
  1978. chip_step = devsize >> cfi->chipshift;
  1979. chip_num = 0;
  1980. /* Some chips have OTP located in the _top_ partition only.
  1981. For example: Intel 28F256L18T (T means top-parameter device) */
  1982. if (cfi->mfr == CFI_MFR_INTEL) {
  1983. switch (cfi->id) {
  1984. case 0x880b:
  1985. case 0x880c:
  1986. case 0x880d:
  1987. chip_num = chip_step - 1;
  1988. }
  1989. }
  1990. for ( ; chip_num < cfi->numchips; chip_num += chip_step) {
  1991. chip = &cfi->chips[chip_num];
  1992. otp = (struct cfi_intelext_otpinfo *)&extp->extra[0];
  1993. /* first OTP region */
  1994. field = 0;
  1995. reg_prot_offset = extp->ProtRegAddr;
  1996. reg_fact_groups = 1;
  1997. reg_fact_size = 1 << extp->FactProtRegSize;
  1998. reg_user_groups = 1;
  1999. reg_user_size = 1 << extp->UserProtRegSize;
  2000. while (len > 0) {
  2001. /* flash geometry fixup */
  2002. data_offset = reg_prot_offset + 1;
  2003. data_offset *= cfi->interleave * cfi->device_type;
  2004. reg_prot_offset *= cfi->interleave * cfi->device_type;
  2005. reg_fact_size *= cfi->interleave;
  2006. reg_user_size *= cfi->interleave;
  2007. if (user_regs) {
  2008. groups = reg_user_groups;
  2009. groupsize = reg_user_size;
  2010. /* skip over factory reg area */
  2011. groupno = reg_fact_groups;
  2012. data_offset += reg_fact_groups * reg_fact_size;
  2013. } else {
  2014. groups = reg_fact_groups;
  2015. groupsize = reg_fact_size;
  2016. groupno = 0;
  2017. }
  2018. while (len > 0 && groups > 0) {
  2019. if (!action) {
  2020. /*
  2021. * Special case: if action is NULL
  2022. * we fill buf with otp_info records.
  2023. */
  2024. struct otp_info *otpinfo;
  2025. map_word lockword;
  2026. len -= sizeof(struct otp_info);
  2027. if (len <= 0)
  2028. return -ENOSPC;
  2029. ret = do_otp_read(map, chip,
  2030. reg_prot_offset,
  2031. (u_char *)&lockword,
  2032. map_bankwidth(map),
  2033. 0, 0, 0);
  2034. if (ret)
  2035. return ret;
  2036. otpinfo = (struct otp_info *)buf;
  2037. otpinfo->start = from;
  2038. otpinfo->length = groupsize;
  2039. otpinfo->locked =
  2040. !map_word_bitsset(map, lockword,
  2041. CMD(1 << groupno));
  2042. from += groupsize;
  2043. buf += sizeof(*otpinfo);
  2044. *retlen += sizeof(*otpinfo);
  2045. } else if (from >= groupsize) {
  2046. from -= groupsize;
  2047. data_offset += groupsize;
  2048. } else {
  2049. int size = groupsize;
  2050. data_offset += from;
  2051. size -= from;
  2052. from = 0;
  2053. if (size > len)
  2054. size = len;
  2055. ret = action(map, chip, data_offset,
  2056. buf, size, reg_prot_offset,
  2057. groupno, groupsize);
  2058. if (ret < 0)
  2059. return ret;
  2060. buf += size;
  2061. len -= size;
  2062. *retlen += size;
  2063. data_offset += size;
  2064. }
  2065. groupno++;
  2066. groups--;
  2067. }
  2068. /* next OTP region */
  2069. if (++field == extp->NumProtectionFields)
  2070. break;
  2071. reg_prot_offset = otp->ProtRegAddr;
  2072. reg_fact_groups = otp->FactGroups;
  2073. reg_fact_size = 1 << otp->FactProtRegSize;
  2074. reg_user_groups = otp->UserGroups;
  2075. reg_user_size = 1 << otp->UserProtRegSize;
  2076. otp++;
  2077. }
  2078. }
  2079. return 0;
  2080. }
  2081. static int cfi_intelext_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  2082. size_t len, size_t *retlen,
  2083. u_char *buf)
  2084. {
  2085. return cfi_intelext_otp_walk(mtd, from, len, retlen,
  2086. buf, do_otp_read, 0);
  2087. }
  2088. static int cfi_intelext_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2089. size_t len, size_t *retlen,
  2090. u_char *buf)
  2091. {
  2092. return cfi_intelext_otp_walk(mtd, from, len, retlen,
  2093. buf, do_otp_read, 1);
  2094. }
  2095. static int cfi_intelext_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2096. size_t len, size_t *retlen,
  2097. u_char *buf)
  2098. {
  2099. return cfi_intelext_otp_walk(mtd, from, len, retlen,
  2100. buf, do_otp_write, 1);
  2101. }
  2102. static int cfi_intelext_lock_user_prot_reg(struct mtd_info *mtd,
  2103. loff_t from, size_t len)
  2104. {
  2105. size_t retlen;
  2106. return cfi_intelext_otp_walk(mtd, from, len, &retlen,
  2107. NULL, do_otp_lock, 1);
  2108. }
  2109. static int cfi_intelext_get_fact_prot_info(struct mtd_info *mtd, size_t len,
  2110. size_t *retlen, struct otp_info *buf)
  2111. {
  2112. return cfi_intelext_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
  2113. NULL, 0);
  2114. }
  2115. static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd, size_t len,
  2116. size_t *retlen, struct otp_info *buf)
  2117. {
  2118. return cfi_intelext_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
  2119. NULL, 1);
  2120. }
  2121. #endif
  2122. static void cfi_intelext_save_locks(struct mtd_info *mtd)
  2123. {
  2124. struct mtd_erase_region_info *region;
  2125. int block, status, i;
  2126. unsigned long adr;
  2127. size_t len;
  2128. for (i = 0; i < mtd->numeraseregions; i++) {
  2129. region = &mtd->eraseregions[i];
  2130. if (!region->lockmap)
  2131. continue;
  2132. for (block = 0; block < region->numblocks; block++){
  2133. len = region->erasesize;
  2134. adr = region->offset + block * len;
  2135. status = cfi_varsize_frob(mtd,
  2136. do_getlockstatus_oneblock, adr, len, NULL);
  2137. if (status)
  2138. set_bit(block, region->lockmap);
  2139. else
  2140. clear_bit(block, region->lockmap);
  2141. }
  2142. }
  2143. }
  2144. static int cfi_intelext_suspend(struct mtd_info *mtd)
  2145. {
  2146. struct map_info *map = mtd->priv;
  2147. struct cfi_private *cfi = map->fldrv_priv;
  2148. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  2149. int i;
  2150. struct flchip *chip;
  2151. int ret = 0;
  2152. if ((mtd->flags & MTD_POWERUP_LOCK)
  2153. && extp && (extp->FeatureSupport & (1 << 5)))
  2154. cfi_intelext_save_locks(mtd);
  2155. for (i=0; !ret && i<cfi->numchips; i++) {
  2156. chip = &cfi->chips[i];
  2157. mutex_lock(&chip->mutex);
  2158. switch (chip->state) {
  2159. case FL_READY:
  2160. case FL_STATUS:
  2161. case FL_CFI_QUERY:
  2162. case FL_JEDEC_QUERY:
  2163. if (chip->oldstate == FL_READY) {
  2164. /* place the chip in a known state before suspend */
  2165. map_write(map, CMD(0xFF), cfi->chips[i].start);
  2166. chip->oldstate = chip->state;
  2167. chip->state = FL_PM_SUSPENDED;
  2168. /* No need to wake_up() on this state change -
  2169. * as the whole point is that nobody can do anything
  2170. * with the chip now anyway.
  2171. */
  2172. } else {
  2173. /* There seems to be an operation pending. We must wait for it. */
  2174. printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate);
  2175. ret = -EAGAIN;
  2176. }
  2177. break;
  2178. default:
  2179. /* Should we actually wait? Once upon a time these routines weren't
  2180. allowed to. Or should we return -EAGAIN, because the upper layers
  2181. ought to have already shut down anything which was using the device
  2182. anyway? The latter for now. */
  2183. printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->state);
  2184. ret = -EAGAIN;
  2185. case FL_PM_SUSPENDED:
  2186. break;
  2187. }
  2188. mutex_unlock(&chip->mutex);
  2189. }
  2190. /* Unlock the chips again */
  2191. if (ret) {
  2192. for (i--; i >=0; i--) {
  2193. chip = &cfi->chips[i];
  2194. mutex_lock(&chip->mutex);
  2195. if (chip->state == FL_PM_SUSPENDED) {
  2196. /* No need to force it into a known state here,
  2197. because we're returning failure, and it didn't
  2198. get power cycled */
  2199. chip->state = chip->oldstate;
  2200. chip->oldstate = FL_READY;
  2201. wake_up(&chip->wq);
  2202. }
  2203. mutex_unlock(&chip->mutex);
  2204. }
  2205. }
  2206. return ret;
  2207. }
  2208. static void cfi_intelext_restore_locks(struct mtd_info *mtd)
  2209. {
  2210. struct mtd_erase_region_info *region;
  2211. int block, i;
  2212. unsigned long adr;
  2213. size_t len;
  2214. for (i = 0; i < mtd->numeraseregions; i++) {
  2215. region = &mtd->eraseregions[i];
  2216. if (!region->lockmap)
  2217. continue;
  2218. for_each_clear_bit(block, region->lockmap, region->numblocks) {
  2219. len = region->erasesize;
  2220. adr = region->offset + block * len;
  2221. cfi_intelext_unlock(mtd, adr, len);
  2222. }
  2223. }
  2224. }
  2225. static void cfi_intelext_resume(struct mtd_info *mtd)
  2226. {
  2227. struct map_info *map = mtd->priv;
  2228. struct cfi_private *cfi = map->fldrv_priv;
  2229. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  2230. int i;
  2231. struct flchip *chip;
  2232. for (i=0; i<cfi->numchips; i++) {
  2233. chip = &cfi->chips[i];
  2234. mutex_lock(&chip->mutex);
  2235. /* Go to known state. Chip may have been power cycled */
  2236. if (chip->state == FL_PM_SUSPENDED) {
  2237. /* Refresh LH28F640BF Partition Config. Register */
  2238. fixup_LH28F640BF(mtd);
  2239. map_write(map, CMD(0xFF), cfi->chips[i].start);
  2240. chip->oldstate = chip->state = FL_READY;
  2241. wake_up(&chip->wq);
  2242. }
  2243. mutex_unlock(&chip->mutex);
  2244. }
  2245. if ((mtd->flags & MTD_POWERUP_LOCK)
  2246. && extp && (extp->FeatureSupport & (1 << 5)))
  2247. cfi_intelext_restore_locks(mtd);
  2248. }
  2249. static int cfi_intelext_reset(struct mtd_info *mtd)
  2250. {
  2251. struct map_info *map = mtd->priv;
  2252. struct cfi_private *cfi = map->fldrv_priv;
  2253. int i, ret;
  2254. for (i=0; i < cfi->numchips; i++) {
  2255. struct flchip *chip = &cfi->chips[i];
  2256. /* force the completion of any ongoing operation
  2257. and switch to array mode so any bootloader in
  2258. flash is accessible for soft reboot. */
  2259. mutex_lock(&chip->mutex);
  2260. ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
  2261. if (!ret) {
  2262. map_write(map, CMD(0xff), chip->start);
  2263. chip->state = FL_SHUTDOWN;
  2264. put_chip(map, chip, chip->start);
  2265. }
  2266. mutex_unlock(&chip->mutex);
  2267. }
  2268. return 0;
  2269. }
  2270. static int cfi_intelext_reboot(struct notifier_block *nb, unsigned long val,
  2271. void *v)
  2272. {
  2273. struct mtd_info *mtd;
  2274. mtd = container_of(nb, struct mtd_info, reboot_notifier);
  2275. cfi_intelext_reset(mtd);
  2276. return NOTIFY_DONE;
  2277. }
  2278. static void cfi_intelext_destroy(struct mtd_info *mtd)
  2279. {
  2280. struct map_info *map = mtd->priv;
  2281. struct cfi_private *cfi = map->fldrv_priv;
  2282. struct mtd_erase_region_info *region;
  2283. int i;
  2284. cfi_intelext_reset(mtd);
  2285. unregister_reboot_notifier(&mtd->reboot_notifier);
  2286. kfree(cfi->cmdset_priv);
  2287. kfree(cfi->cfiq);
  2288. kfree(cfi->chips[0].priv);
  2289. kfree(cfi);
  2290. for (i = 0; i < mtd->numeraseregions; i++) {
  2291. region = &mtd->eraseregions[i];
  2292. kfree(region->lockmap);
  2293. }
  2294. kfree(mtd->eraseregions);
  2295. }
  2296. MODULE_LICENSE("GPL");
  2297. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
  2298. MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");
  2299. MODULE_ALIAS("cfi_cmdset_0003");
  2300. MODULE_ALIAS("cfi_cmdset_0200");