cfi_cmdset_0001.c 68 KB

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