cfi_cmdset_0001.c 70 KB

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