cfi_cmdset_0002.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  1. /*
  2. * Common Flash Interface support:
  3. * AMD & Fujitsu Standard Vendor Command Set (ID 0x0002)
  4. *
  5. * Copyright (C) 2000 Crossnet Co. <info@crossnet.co.jp>
  6. * Copyright (C) 2004 Arcom Control Systems Ltd <linux@arcom.com>
  7. * Copyright (C) 2005 MontaVista Software Inc. <source@mvista.com>
  8. *
  9. * 2_by_8 routines added by Simon Munton
  10. *
  11. * 4_by_16 work by Carolyn J. Smith
  12. *
  13. * XIP support hooks by Vitaly Wool (based on code for Intel flash
  14. * by Nicolas Pitre)
  15. *
  16. * 25/09/2008 Christopher Moore: TopBottom fixup for many Macronix with CFI V1.0
  17. *
  18. * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
  19. *
  20. * This code is GPL
  21. */
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/init.h>
  27. #include <asm/io.h>
  28. #include <asm/byteorder.h>
  29. #include <linux/errno.h>
  30. #include <linux/slab.h>
  31. #include <linux/delay.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/reboot.h>
  34. #include <linux/mtd/map.h>
  35. #include <linux/mtd/mtd.h>
  36. #include <linux/mtd/cfi.h>
  37. #include <linux/mtd/xip.h>
  38. #define AMD_BOOTLOC_BUG
  39. #define FORCE_WORD_WRITE 0
  40. #define MAX_WORD_RETRIES 3
  41. #define SST49LF004B 0x0060
  42. #define SST49LF040B 0x0050
  43. #define SST49LF008A 0x005a
  44. #define AT49BV6416 0x00d6
  45. static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  46. static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  47. static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  48. static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
  49. static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
  50. static void cfi_amdstd_sync (struct mtd_info *);
  51. static int cfi_amdstd_suspend (struct mtd_info *);
  52. static void cfi_amdstd_resume (struct mtd_info *);
  53. static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *);
  54. static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  55. static void cfi_amdstd_destroy(struct mtd_info *);
  56. struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
  57. static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
  58. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
  59. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
  60. #include "fwh_lock.h"
  61. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  62. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  63. static struct mtd_chip_driver cfi_amdstd_chipdrv = {
  64. .probe = NULL, /* Not usable directly */
  65. .destroy = cfi_amdstd_destroy,
  66. .name = "cfi_cmdset_0002",
  67. .module = THIS_MODULE
  68. };
  69. /* #define DEBUG_CFI_FEATURES */
  70. #ifdef DEBUG_CFI_FEATURES
  71. static void cfi_tell_features(struct cfi_pri_amdstd *extp)
  72. {
  73. const char* erase_suspend[3] = {
  74. "Not supported", "Read only", "Read/write"
  75. };
  76. const char* top_bottom[6] = {
  77. "No WP", "8x8KiB sectors at top & bottom, no WP",
  78. "Bottom boot", "Top boot",
  79. "Uniform, Bottom WP", "Uniform, Top WP"
  80. };
  81. printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
  82. printk(" Address sensitive unlock: %s\n",
  83. (extp->SiliconRevision & 1) ? "Not required" : "Required");
  84. if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
  85. printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
  86. else
  87. printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
  88. if (extp->BlkProt == 0)
  89. printk(" Block protection: Not supported\n");
  90. else
  91. printk(" Block protection: %d sectors per group\n", extp->BlkProt);
  92. printk(" Temporary block unprotect: %s\n",
  93. extp->TmpBlkUnprotect ? "Supported" : "Not supported");
  94. printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
  95. printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
  96. printk(" Burst mode: %s\n",
  97. extp->BurstMode ? "Supported" : "Not supported");
  98. if (extp->PageMode == 0)
  99. printk(" Page mode: Not supported\n");
  100. else
  101. printk(" Page mode: %d word page\n", extp->PageMode << 2);
  102. printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
  103. extp->VppMin >> 4, extp->VppMin & 0xf);
  104. printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
  105. extp->VppMax >> 4, extp->VppMax & 0xf);
  106. if (extp->TopBottom < ARRAY_SIZE(top_bottom))
  107. printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
  108. else
  109. printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
  110. }
  111. #endif
  112. #ifdef AMD_BOOTLOC_BUG
  113. /* Wheee. Bring me the head of someone at AMD. */
  114. static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
  115. {
  116. struct map_info *map = mtd->priv;
  117. struct cfi_private *cfi = map->fldrv_priv;
  118. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  119. __u8 major = extp->MajorVersion;
  120. __u8 minor = extp->MinorVersion;
  121. if (((major << 8) | minor) < 0x3131) {
  122. /* CFI version 1.0 => don't trust bootloc */
  123. DEBUG(MTD_DEBUG_LEVEL1,
  124. "%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
  125. map->name, cfi->mfr, cfi->id);
  126. /* AFAICS all 29LV400 with a bottom boot block have a device ID
  127. * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
  128. * These were badly detected as they have the 0x80 bit set
  129. * so treat them as a special case.
  130. */
  131. if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
  132. /* Macronix added CFI to their 2nd generation
  133. * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
  134. * Fujitsu, Spansion, EON, ESI and older Macronix)
  135. * has CFI.
  136. *
  137. * Therefore also check the manufacturer.
  138. * This reduces the risk of false detection due to
  139. * the 8-bit device ID.
  140. */
  141. (cfi->mfr == CFI_MFR_MACRONIX)) {
  142. DEBUG(MTD_DEBUG_LEVEL1,
  143. "%s: Macronix MX29LV400C with bottom boot block"
  144. " detected\n", map->name);
  145. extp->TopBottom = 2; /* bottom boot */
  146. } else
  147. if (cfi->id & 0x80) {
  148. printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
  149. extp->TopBottom = 3; /* top boot */
  150. } else {
  151. extp->TopBottom = 2; /* bottom boot */
  152. }
  153. DEBUG(MTD_DEBUG_LEVEL1,
  154. "%s: AMD CFI PRI V%c.%c has no boot block field;"
  155. " deduced %s from Device ID\n", map->name, major, minor,
  156. extp->TopBottom == 2 ? "bottom" : "top");
  157. }
  158. }
  159. #endif
  160. static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
  161. {
  162. struct map_info *map = mtd->priv;
  163. struct cfi_private *cfi = map->fldrv_priv;
  164. if (cfi->cfiq->BufWriteTimeoutTyp) {
  165. DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
  166. mtd->write = cfi_amdstd_write_buffers;
  167. }
  168. }
  169. /* Atmel chips don't use the same PRI format as AMD chips */
  170. static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
  171. {
  172. struct map_info *map = mtd->priv;
  173. struct cfi_private *cfi = map->fldrv_priv;
  174. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  175. struct cfi_pri_atmel atmel_pri;
  176. memcpy(&atmel_pri, extp, sizeof(atmel_pri));
  177. memset((char *)extp + 5, 0, sizeof(*extp) - 5);
  178. if (atmel_pri.Features & 0x02)
  179. extp->EraseSuspend = 2;
  180. /* Some chips got it backwards... */
  181. if (cfi->id == AT49BV6416) {
  182. if (atmel_pri.BottomBoot)
  183. extp->TopBottom = 3;
  184. else
  185. extp->TopBottom = 2;
  186. } else {
  187. if (atmel_pri.BottomBoot)
  188. extp->TopBottom = 2;
  189. else
  190. extp->TopBottom = 3;
  191. }
  192. /* burst write mode not supported */
  193. cfi->cfiq->BufWriteTimeoutTyp = 0;
  194. cfi->cfiq->BufWriteTimeoutMax = 0;
  195. }
  196. static void fixup_use_secsi(struct mtd_info *mtd, void *param)
  197. {
  198. /* Setup for chips with a secsi area */
  199. mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
  200. mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
  201. }
  202. static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
  203. {
  204. struct map_info *map = mtd->priv;
  205. struct cfi_private *cfi = map->fldrv_priv;
  206. if ((cfi->cfiq->NumEraseRegions == 1) &&
  207. ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
  208. mtd->erase = cfi_amdstd_erase_chip;
  209. }
  210. }
  211. /*
  212. * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
  213. * locked by default.
  214. */
  215. static void fixup_use_atmel_lock(struct mtd_info *mtd, void *param)
  216. {
  217. mtd->lock = cfi_atmel_lock;
  218. mtd->unlock = cfi_atmel_unlock;
  219. mtd->flags |= MTD_POWERUP_LOCK;
  220. }
  221. static void fixup_old_sst_eraseregion(struct mtd_info *mtd)
  222. {
  223. struct map_info *map = mtd->priv;
  224. struct cfi_private *cfi = map->fldrv_priv;
  225. /*
  226. * These flashes report two seperate eraseblock regions based on the
  227. * sector_erase-size and block_erase-size, although they both operate on the
  228. * same memory. This is not allowed according to CFI, so we just pick the
  229. * sector_erase-size.
  230. */
  231. cfi->cfiq->NumEraseRegions = 1;
  232. }
  233. static void fixup_sst39vf(struct mtd_info *mtd, void *param)
  234. {
  235. struct map_info *map = mtd->priv;
  236. struct cfi_private *cfi = map->fldrv_priv;
  237. fixup_old_sst_eraseregion(mtd);
  238. cfi->addr_unlock1 = 0x5555;
  239. cfi->addr_unlock2 = 0x2AAA;
  240. }
  241. static void fixup_sst39vf_rev_b(struct mtd_info *mtd, void *param)
  242. {
  243. struct map_info *map = mtd->priv;
  244. struct cfi_private *cfi = map->fldrv_priv;
  245. fixup_old_sst_eraseregion(mtd);
  246. cfi->addr_unlock1 = 0x555;
  247. cfi->addr_unlock2 = 0x2AA;
  248. cfi->sector_erase_cmd = CMD(0x50);
  249. }
  250. static void fixup_sst38vf640x_sectorsize(struct mtd_info *mtd, void *param)
  251. {
  252. struct map_info *map = mtd->priv;
  253. struct cfi_private *cfi = map->fldrv_priv;
  254. fixup_sst39vf_rev_b(mtd, param);
  255. /*
  256. * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where
  257. * it should report a size of 8KBytes (0x0020*256).
  258. */
  259. cfi->cfiq->EraseRegionInfo[0] = 0x002003ff;
  260. pr_warning("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n", mtd->name);
  261. }
  262. static void fixup_s29gl064n_sectors(struct mtd_info *mtd, void *param)
  263. {
  264. struct map_info *map = mtd->priv;
  265. struct cfi_private *cfi = map->fldrv_priv;
  266. if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
  267. cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
  268. pr_warning("%s: Bad S29GL064N CFI data, adjust from 64 to 128 sectors\n", mtd->name);
  269. }
  270. }
  271. static void fixup_s29gl032n_sectors(struct mtd_info *mtd, void *param)
  272. {
  273. struct map_info *map = mtd->priv;
  274. struct cfi_private *cfi = map->fldrv_priv;
  275. if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
  276. cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
  277. pr_warning("%s: Bad S29GL032N CFI data, adjust from 127 to 63 sectors\n", mtd->name);
  278. }
  279. }
  280. /* Used to fix CFI-Tables of chips without Extended Query Tables */
  281. static struct cfi_fixup cfi_nopri_fixup_table[] = {
  282. { CFI_MFR_SST, 0x234A, fixup_sst39vf, NULL, }, /* SST39VF1602 */
  283. { CFI_MFR_SST, 0x234B, fixup_sst39vf, NULL, }, /* SST39VF1601 */
  284. { CFI_MFR_SST, 0x235A, fixup_sst39vf, NULL, }, /* SST39VF3202 */
  285. { CFI_MFR_SST, 0x235B, fixup_sst39vf, NULL, }, /* SST39VF3201 */
  286. { CFI_MFR_SST, 0x235C, fixup_sst39vf_rev_b, NULL, }, /* SST39VF3202B */
  287. { CFI_MFR_SST, 0x235D, fixup_sst39vf_rev_b, NULL, }, /* SST39VF3201B */
  288. { CFI_MFR_SST, 0x236C, fixup_sst39vf_rev_b, NULL, }, /* SST39VF6402B */
  289. { CFI_MFR_SST, 0x236D, fixup_sst39vf_rev_b, NULL, }, /* SST39VF6401B */
  290. { 0, 0, NULL, NULL }
  291. };
  292. static struct cfi_fixup cfi_fixup_table[] = {
  293. { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
  294. #ifdef AMD_BOOTLOC_BUG
  295. { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
  296. { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock, NULL },
  297. #endif
  298. { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
  299. { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
  300. { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
  301. { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
  302. { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
  303. { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
  304. { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors, NULL, },
  305. { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors, NULL, },
  306. { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors, NULL, },
  307. { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors, NULL, },
  308. { CFI_MFR_SST, 0x536A, fixup_sst38vf640x_sectorsize, NULL, }, /* SST38VF6402 */
  309. { CFI_MFR_SST, 0x536B, fixup_sst38vf640x_sectorsize, NULL, }, /* SST38VF6401 */
  310. { CFI_MFR_SST, 0x536C, fixup_sst38vf640x_sectorsize, NULL, }, /* SST38VF6404 */
  311. { CFI_MFR_SST, 0x536D, fixup_sst38vf640x_sectorsize, NULL, }, /* SST38VF6403 */
  312. #if !FORCE_WORD_WRITE
  313. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
  314. #endif
  315. { 0, 0, NULL, NULL }
  316. };
  317. static struct cfi_fixup jedec_fixup_table[] = {
  318. { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
  319. { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock, NULL, },
  320. { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
  321. { 0, 0, NULL, NULL }
  322. };
  323. static struct cfi_fixup fixup_table[] = {
  324. /* The CFI vendor ids and the JEDEC vendor IDs appear
  325. * to be common. It is like the devices id's are as
  326. * well. This table is to pick all cases where
  327. * we know that is the case.
  328. */
  329. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
  330. { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock, NULL },
  331. { 0, 0, NULL, NULL }
  332. };
  333. static void cfi_fixup_major_minor(struct cfi_private *cfi,
  334. struct cfi_pri_amdstd *extp)
  335. {
  336. if (cfi->mfr == CFI_MFR_SAMSUNG && cfi->id == 0x257e &&
  337. extp->MajorVersion == '0')
  338. extp->MajorVersion = '1';
  339. /*
  340. * SST 38VF640x chips report major=0xFF / minor=0xFF.
  341. */
  342. if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) {
  343. extp->MajorVersion = '1';
  344. extp->MinorVersion = '0';
  345. }
  346. }
  347. struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
  348. {
  349. struct cfi_private *cfi = map->fldrv_priv;
  350. struct mtd_info *mtd;
  351. int i;
  352. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  353. if (!mtd) {
  354. printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
  355. return NULL;
  356. }
  357. mtd->priv = map;
  358. mtd->type = MTD_NORFLASH;
  359. /* Fill in the default mtd operations */
  360. mtd->erase = cfi_amdstd_erase_varsize;
  361. mtd->write = cfi_amdstd_write_words;
  362. mtd->read = cfi_amdstd_read;
  363. mtd->sync = cfi_amdstd_sync;
  364. mtd->suspend = cfi_amdstd_suspend;
  365. mtd->resume = cfi_amdstd_resume;
  366. mtd->flags = MTD_CAP_NORFLASH;
  367. mtd->name = map->name;
  368. mtd->writesize = 1;
  369. mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
  370. if (cfi->cfi_mode==CFI_MODE_CFI){
  371. unsigned char bootloc;
  372. __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
  373. struct cfi_pri_amdstd *extp;
  374. extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
  375. if (extp) {
  376. /*
  377. * It's a real CFI chip, not one for which the probe
  378. * routine faked a CFI structure.
  379. */
  380. cfi_fixup_major_minor(cfi, extp);
  381. /*
  382. * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4
  383. * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19
  384. * http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
  385. * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
  386. */
  387. if (extp->MajorVersion != '1' ||
  388. (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '4'))) {
  389. printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
  390. "version %c.%c (%#02x/%#02x).\n",
  391. extp->MajorVersion, extp->MinorVersion,
  392. extp->MajorVersion, extp->MinorVersion);
  393. kfree(extp);
  394. kfree(mtd);
  395. return NULL;
  396. }
  397. printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n",
  398. extp->MajorVersion, extp->MinorVersion);
  399. /* Install our own private info structure */
  400. cfi->cmdset_priv = extp;
  401. /* Apply cfi device specific fixups */
  402. cfi_fixup(mtd, cfi_fixup_table);
  403. #ifdef DEBUG_CFI_FEATURES
  404. /* Tell the user about it in lots of lovely detail */
  405. cfi_tell_features(extp);
  406. #endif
  407. bootloc = extp->TopBottom;
  408. if ((bootloc < 2) || (bootloc > 5)) {
  409. printk(KERN_WARNING "%s: CFI contains unrecognised boot "
  410. "bank location (%d). Assuming bottom.\n",
  411. map->name, bootloc);
  412. bootloc = 2;
  413. }
  414. if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
  415. printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
  416. for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
  417. int j = (cfi->cfiq->NumEraseRegions-1)-i;
  418. __u32 swap;
  419. swap = cfi->cfiq->EraseRegionInfo[i];
  420. cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
  421. cfi->cfiq->EraseRegionInfo[j] = swap;
  422. }
  423. }
  424. /* Set the default CFI lock/unlock addresses */
  425. cfi->addr_unlock1 = 0x555;
  426. cfi->addr_unlock2 = 0x2aa;
  427. }
  428. cfi_fixup(mtd, cfi_nopri_fixup_table);
  429. if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
  430. kfree(mtd);
  431. return NULL;
  432. }
  433. } /* CFI mode */
  434. else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
  435. /* Apply jedec specific fixups */
  436. cfi_fixup(mtd, jedec_fixup_table);
  437. }
  438. /* Apply generic fixups */
  439. cfi_fixup(mtd, fixup_table);
  440. for (i=0; i< cfi->numchips; i++) {
  441. cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
  442. cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
  443. cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
  444. cfi->chips[i].ref_point_counter = 0;
  445. init_waitqueue_head(&(cfi->chips[i].wq));
  446. }
  447. map->fldrv = &cfi_amdstd_chipdrv;
  448. return cfi_amdstd_setup(mtd);
  449. }
  450. struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
  451. struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
  452. EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
  453. EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
  454. EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
  455. static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
  456. {
  457. struct map_info *map = mtd->priv;
  458. struct cfi_private *cfi = map->fldrv_priv;
  459. unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
  460. unsigned long offset = 0;
  461. int i,j;
  462. printk(KERN_NOTICE "number of %s chips: %d\n",
  463. (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
  464. /* Select the correct geometry setup */
  465. mtd->size = devsize * cfi->numchips;
  466. mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
  467. mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
  468. * mtd->numeraseregions, GFP_KERNEL);
  469. if (!mtd->eraseregions) {
  470. printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
  471. goto setup_err;
  472. }
  473. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  474. unsigned long ernum, ersize;
  475. ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
  476. ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
  477. if (mtd->erasesize < ersize) {
  478. mtd->erasesize = ersize;
  479. }
  480. for (j=0; j<cfi->numchips; j++) {
  481. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
  482. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
  483. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
  484. }
  485. offset += (ersize * ernum);
  486. }
  487. if (offset != devsize) {
  488. /* Argh */
  489. printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
  490. goto setup_err;
  491. }
  492. __module_get(THIS_MODULE);
  493. register_reboot_notifier(&mtd->reboot_notifier);
  494. return mtd;
  495. setup_err:
  496. kfree(mtd->eraseregions);
  497. kfree(mtd);
  498. kfree(cfi->cmdset_priv);
  499. kfree(cfi->cfiq);
  500. return NULL;
  501. }
  502. /*
  503. * Return true if the chip is ready.
  504. *
  505. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  506. * non-suspended sector) and is indicated by no toggle bits toggling.
  507. *
  508. * Note that anything more complicated than checking if no bits are toggling
  509. * (including checking DQ5 for an error status) is tricky to get working
  510. * correctly and is therefore not done (particulary with interleaved chips
  511. * as each chip must be checked independantly of the others).
  512. */
  513. static int __xipram chip_ready(struct map_info *map, unsigned long addr)
  514. {
  515. map_word d, t;
  516. d = map_read(map, addr);
  517. t = map_read(map, addr);
  518. return map_word_equal(map, d, t);
  519. }
  520. /*
  521. * Return true if the chip is ready and has the correct value.
  522. *
  523. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  524. * non-suspended sector) and it is indicated by no bits toggling.
  525. *
  526. * Error are indicated by toggling bits or bits held with the wrong value,
  527. * or with bits toggling.
  528. *
  529. * Note that anything more complicated than checking if no bits are toggling
  530. * (including checking DQ5 for an error status) is tricky to get working
  531. * correctly and is therefore not done (particulary with interleaved chips
  532. * as each chip must be checked independantly of the others).
  533. *
  534. */
  535. static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
  536. {
  537. map_word oldd, curd;
  538. oldd = map_read(map, addr);
  539. curd = map_read(map, addr);
  540. return map_word_equal(map, oldd, curd) &&
  541. map_word_equal(map, curd, expected);
  542. }
  543. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  544. {
  545. DECLARE_WAITQUEUE(wait, current);
  546. struct cfi_private *cfi = map->fldrv_priv;
  547. unsigned long timeo;
  548. struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
  549. resettime:
  550. timeo = jiffies + HZ;
  551. retry:
  552. switch (chip->state) {
  553. case FL_STATUS:
  554. for (;;) {
  555. if (chip_ready(map, adr))
  556. break;
  557. if (time_after(jiffies, timeo)) {
  558. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  559. return -EIO;
  560. }
  561. mutex_unlock(&chip->mutex);
  562. cfi_udelay(1);
  563. mutex_lock(&chip->mutex);
  564. /* Someone else might have been playing with it. */
  565. goto retry;
  566. }
  567. case FL_READY:
  568. case FL_CFI_QUERY:
  569. case FL_JEDEC_QUERY:
  570. return 0;
  571. case FL_ERASING:
  572. if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
  573. !(mode == FL_READY || mode == FL_POINT ||
  574. (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
  575. goto sleep;
  576. /* We could check to see if we're trying to access the sector
  577. * that is currently being erased. However, no user will try
  578. * anything like that so we just wait for the timeout. */
  579. /* Erase suspend */
  580. /* It's harmless to issue the Erase-Suspend and Erase-Resume
  581. * commands when the erase algorithm isn't in progress. */
  582. map_write(map, CMD(0xB0), chip->in_progress_block_addr);
  583. chip->oldstate = FL_ERASING;
  584. chip->state = FL_ERASE_SUSPENDING;
  585. chip->erase_suspended = 1;
  586. for (;;) {
  587. if (chip_ready(map, adr))
  588. break;
  589. if (time_after(jiffies, timeo)) {
  590. /* Should have suspended the erase by now.
  591. * Send an Erase-Resume command as either
  592. * there was an error (so leave the erase
  593. * routine to recover from it) or we trying to
  594. * use the erase-in-progress sector. */
  595. map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
  596. chip->state = FL_ERASING;
  597. chip->oldstate = FL_READY;
  598. printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
  599. return -EIO;
  600. }
  601. mutex_unlock(&chip->mutex);
  602. cfi_udelay(1);
  603. mutex_lock(&chip->mutex);
  604. /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
  605. So we can just loop here. */
  606. }
  607. chip->state = FL_READY;
  608. return 0;
  609. case FL_XIP_WHILE_ERASING:
  610. if (mode != FL_READY && mode != FL_POINT &&
  611. (!cfip || !(cfip->EraseSuspend&2)))
  612. goto sleep;
  613. chip->oldstate = chip->state;
  614. chip->state = FL_READY;
  615. return 0;
  616. case FL_SHUTDOWN:
  617. /* The machine is rebooting */
  618. return -EIO;
  619. case FL_POINT:
  620. /* Only if there's no operation suspended... */
  621. if (mode == FL_READY && chip->oldstate == FL_READY)
  622. return 0;
  623. default:
  624. sleep:
  625. set_current_state(TASK_UNINTERRUPTIBLE);
  626. add_wait_queue(&chip->wq, &wait);
  627. mutex_unlock(&chip->mutex);
  628. schedule();
  629. remove_wait_queue(&chip->wq, &wait);
  630. mutex_lock(&chip->mutex);
  631. goto resettime;
  632. }
  633. }
  634. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
  635. {
  636. struct cfi_private *cfi = map->fldrv_priv;
  637. switch(chip->oldstate) {
  638. case FL_ERASING:
  639. chip->state = chip->oldstate;
  640. map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
  641. chip->oldstate = FL_READY;
  642. chip->state = FL_ERASING;
  643. break;
  644. case FL_XIP_WHILE_ERASING:
  645. chip->state = chip->oldstate;
  646. chip->oldstate = FL_READY;
  647. break;
  648. case FL_READY:
  649. case FL_STATUS:
  650. /* We should really make set_vpp() count, rather than doing this */
  651. DISABLE_VPP(map);
  652. break;
  653. default:
  654. printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
  655. }
  656. wake_up(&chip->wq);
  657. }
  658. #ifdef CONFIG_MTD_XIP
  659. /*
  660. * No interrupt what so ever can be serviced while the flash isn't in array
  661. * mode. This is ensured by the xip_disable() and xip_enable() functions
  662. * enclosing any code path where the flash is known not to be in array mode.
  663. * And within a XIP disabled code path, only functions marked with __xipram
  664. * may be called and nothing else (it's a good thing to inspect generated
  665. * assembly to make sure inline functions were actually inlined and that gcc
  666. * didn't emit calls to its own support functions). Also configuring MTD CFI
  667. * support to a single buswidth and a single interleave is also recommended.
  668. */
  669. static void xip_disable(struct map_info *map, struct flchip *chip,
  670. unsigned long adr)
  671. {
  672. /* TODO: chips with no XIP use should ignore and return */
  673. (void) map_read(map, adr); /* ensure mmu mapping is up to date */
  674. local_irq_disable();
  675. }
  676. static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
  677. unsigned long adr)
  678. {
  679. struct cfi_private *cfi = map->fldrv_priv;
  680. if (chip->state != FL_POINT && chip->state != FL_READY) {
  681. map_write(map, CMD(0xf0), adr);
  682. chip->state = FL_READY;
  683. }
  684. (void) map_read(map, adr);
  685. xip_iprefetch();
  686. local_irq_enable();
  687. }
  688. /*
  689. * When a delay is required for the flash operation to complete, the
  690. * xip_udelay() function is polling for both the given timeout and pending
  691. * (but still masked) hardware interrupts. Whenever there is an interrupt
  692. * pending then the flash erase operation is suspended, array mode restored
  693. * and interrupts unmasked. Task scheduling might also happen at that
  694. * point. The CPU eventually returns from the interrupt or the call to
  695. * schedule() and the suspended flash operation is resumed for the remaining
  696. * of the delay period.
  697. *
  698. * Warning: this function _will_ fool interrupt latency tracing tools.
  699. */
  700. static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
  701. unsigned long adr, int usec)
  702. {
  703. struct cfi_private *cfi = map->fldrv_priv;
  704. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  705. map_word status, OK = CMD(0x80);
  706. unsigned long suspended, start = xip_currtime();
  707. flstate_t oldstate;
  708. do {
  709. cpu_relax();
  710. if (xip_irqpending() && extp &&
  711. ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
  712. (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
  713. /*
  714. * Let's suspend the erase operation when supported.
  715. * Note that we currently don't try to suspend
  716. * interleaved chips if there is already another
  717. * operation suspended (imagine what happens
  718. * when one chip was already done with the current
  719. * operation while another chip suspended it, then
  720. * we resume the whole thing at once). Yes, it
  721. * can happen!
  722. */
  723. map_write(map, CMD(0xb0), adr);
  724. usec -= xip_elapsed_since(start);
  725. suspended = xip_currtime();
  726. do {
  727. if (xip_elapsed_since(suspended) > 100000) {
  728. /*
  729. * The chip doesn't want to suspend
  730. * after waiting for 100 msecs.
  731. * This is a critical error but there
  732. * is not much we can do here.
  733. */
  734. return;
  735. }
  736. status = map_read(map, adr);
  737. } while (!map_word_andequal(map, status, OK, OK));
  738. /* Suspend succeeded */
  739. oldstate = chip->state;
  740. if (!map_word_bitsset(map, status, CMD(0x40)))
  741. break;
  742. chip->state = FL_XIP_WHILE_ERASING;
  743. chip->erase_suspended = 1;
  744. map_write(map, CMD(0xf0), adr);
  745. (void) map_read(map, adr);
  746. xip_iprefetch();
  747. local_irq_enable();
  748. mutex_unlock(&chip->mutex);
  749. xip_iprefetch();
  750. cond_resched();
  751. /*
  752. * We're back. However someone else might have
  753. * decided to go write to the chip if we are in
  754. * a suspended erase state. If so let's wait
  755. * until it's done.
  756. */
  757. mutex_lock(&chip->mutex);
  758. while (chip->state != FL_XIP_WHILE_ERASING) {
  759. DECLARE_WAITQUEUE(wait, current);
  760. set_current_state(TASK_UNINTERRUPTIBLE);
  761. add_wait_queue(&chip->wq, &wait);
  762. mutex_unlock(&chip->mutex);
  763. schedule();
  764. remove_wait_queue(&chip->wq, &wait);
  765. mutex_lock(&chip->mutex);
  766. }
  767. /* Disallow XIP again */
  768. local_irq_disable();
  769. /* Resume the write or erase operation */
  770. map_write(map, cfi->sector_erase_cmd, adr);
  771. chip->state = oldstate;
  772. start = xip_currtime();
  773. } else if (usec >= 1000000/HZ) {
  774. /*
  775. * Try to save on CPU power when waiting delay
  776. * is at least a system timer tick period.
  777. * No need to be extremely accurate here.
  778. */
  779. xip_cpu_idle();
  780. }
  781. status = map_read(map, adr);
  782. } while (!map_word_andequal(map, status, OK, OK)
  783. && xip_elapsed_since(start) < usec);
  784. }
  785. #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
  786. /*
  787. * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
  788. * the flash is actively programming or erasing since we have to poll for
  789. * the operation to complete anyway. We can't do that in a generic way with
  790. * a XIP setup so do it before the actual flash operation in this case
  791. * and stub it out from INVALIDATE_CACHE_UDELAY.
  792. */
  793. #define XIP_INVAL_CACHED_RANGE(map, from, size) \
  794. INVALIDATE_CACHED_RANGE(map, from, size)
  795. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  796. UDELAY(map, chip, adr, usec)
  797. /*
  798. * Extra notes:
  799. *
  800. * Activating this XIP support changes the way the code works a bit. For
  801. * example the code to suspend the current process when concurrent access
  802. * happens is never executed because xip_udelay() will always return with the
  803. * same chip state as it was entered with. This is why there is no care for
  804. * the presence of add_wait_queue() or schedule() calls from within a couple
  805. * xip_disable()'d areas of code, like in do_erase_oneblock for example.
  806. * The queueing and scheduling are always happening within xip_udelay().
  807. *
  808. * Similarly, get_chip() and put_chip() just happen to always be executed
  809. * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
  810. * is in array mode, therefore never executing many cases therein and not
  811. * causing any problem with XIP.
  812. */
  813. #else
  814. #define xip_disable(map, chip, adr)
  815. #define xip_enable(map, chip, adr)
  816. #define XIP_INVAL_CACHED_RANGE(x...)
  817. #define UDELAY(map, chip, adr, usec) \
  818. do { \
  819. mutex_unlock(&chip->mutex); \
  820. cfi_udelay(usec); \
  821. mutex_lock(&chip->mutex); \
  822. } while (0)
  823. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  824. do { \
  825. mutex_unlock(&chip->mutex); \
  826. INVALIDATE_CACHED_RANGE(map, adr, len); \
  827. cfi_udelay(usec); \
  828. mutex_lock(&chip->mutex); \
  829. } while (0)
  830. #endif
  831. static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  832. {
  833. unsigned long cmd_addr;
  834. struct cfi_private *cfi = map->fldrv_priv;
  835. int ret;
  836. adr += chip->start;
  837. /* Ensure cmd read/writes are aligned. */
  838. cmd_addr = adr & ~(map_bankwidth(map)-1);
  839. mutex_lock(&chip->mutex);
  840. ret = get_chip(map, chip, cmd_addr, FL_READY);
  841. if (ret) {
  842. mutex_unlock(&chip->mutex);
  843. return ret;
  844. }
  845. if (chip->state != FL_POINT && chip->state != FL_READY) {
  846. map_write(map, CMD(0xf0), cmd_addr);
  847. chip->state = FL_READY;
  848. }
  849. map_copy_from(map, buf, adr, len);
  850. put_chip(map, chip, cmd_addr);
  851. mutex_unlock(&chip->mutex);
  852. return 0;
  853. }
  854. static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  855. {
  856. struct map_info *map = mtd->priv;
  857. struct cfi_private *cfi = map->fldrv_priv;
  858. unsigned long ofs;
  859. int chipnum;
  860. int ret = 0;
  861. /* ofs: offset within the first chip that the first read should start */
  862. chipnum = (from >> cfi->chipshift);
  863. ofs = from - (chipnum << cfi->chipshift);
  864. *retlen = 0;
  865. while (len) {
  866. unsigned long thislen;
  867. if (chipnum >= cfi->numchips)
  868. break;
  869. if ((len + ofs -1) >> cfi->chipshift)
  870. thislen = (1<<cfi->chipshift) - ofs;
  871. else
  872. thislen = len;
  873. ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  874. if (ret)
  875. break;
  876. *retlen += thislen;
  877. len -= thislen;
  878. buf += thislen;
  879. ofs = 0;
  880. chipnum++;
  881. }
  882. return ret;
  883. }
  884. static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  885. {
  886. DECLARE_WAITQUEUE(wait, current);
  887. unsigned long timeo = jiffies + HZ;
  888. struct cfi_private *cfi = map->fldrv_priv;
  889. retry:
  890. mutex_lock(&chip->mutex);
  891. if (chip->state != FL_READY){
  892. set_current_state(TASK_UNINTERRUPTIBLE);
  893. add_wait_queue(&chip->wq, &wait);
  894. mutex_unlock(&chip->mutex);
  895. schedule();
  896. remove_wait_queue(&chip->wq, &wait);
  897. timeo = jiffies + HZ;
  898. goto retry;
  899. }
  900. adr += chip->start;
  901. chip->state = FL_READY;
  902. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  903. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  904. cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  905. map_copy_from(map, buf, adr, len);
  906. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  907. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  908. cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  909. cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  910. wake_up(&chip->wq);
  911. mutex_unlock(&chip->mutex);
  912. return 0;
  913. }
  914. static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  915. {
  916. struct map_info *map = mtd->priv;
  917. struct cfi_private *cfi = map->fldrv_priv;
  918. unsigned long ofs;
  919. int chipnum;
  920. int ret = 0;
  921. /* ofs: offset within the first chip that the first read should start */
  922. /* 8 secsi bytes per chip */
  923. chipnum=from>>3;
  924. ofs=from & 7;
  925. *retlen = 0;
  926. while (len) {
  927. unsigned long thislen;
  928. if (chipnum >= cfi->numchips)
  929. break;
  930. if ((len + ofs -1) >> 3)
  931. thislen = (1<<3) - ofs;
  932. else
  933. thislen = len;
  934. ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  935. if (ret)
  936. break;
  937. *retlen += thislen;
  938. len -= thislen;
  939. buf += thislen;
  940. ofs = 0;
  941. chipnum++;
  942. }
  943. return ret;
  944. }
  945. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
  946. {
  947. struct cfi_private *cfi = map->fldrv_priv;
  948. unsigned long timeo = jiffies + HZ;
  949. /*
  950. * We use a 1ms + 1 jiffies generic timeout for writes (most devices
  951. * have a max write time of a few hundreds usec). However, we should
  952. * use the maximum timeout value given by the chip at probe time
  953. * instead. Unfortunately, struct flchip does have a field for
  954. * maximum timeout, only for typical which can be far too short
  955. * depending of the conditions. The ' + 1' is to avoid having a
  956. * timeout of 0 jiffies if HZ is smaller than 1000.
  957. */
  958. unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
  959. int ret = 0;
  960. map_word oldd;
  961. int retry_cnt = 0;
  962. adr += chip->start;
  963. mutex_lock(&chip->mutex);
  964. ret = get_chip(map, chip, adr, FL_WRITING);
  965. if (ret) {
  966. mutex_unlock(&chip->mutex);
  967. return ret;
  968. }
  969. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  970. __func__, adr, datum.x[0] );
  971. /*
  972. * Check for a NOP for the case when the datum to write is already
  973. * present - it saves time and works around buggy chips that corrupt
  974. * data at other locations when 0xff is written to a location that
  975. * already contains 0xff.
  976. */
  977. oldd = map_read(map, adr);
  978. if (map_word_equal(map, oldd, datum)) {
  979. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
  980. __func__);
  981. goto op_done;
  982. }
  983. XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
  984. ENABLE_VPP(map);
  985. xip_disable(map, chip, adr);
  986. retry:
  987. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  988. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  989. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  990. map_write(map, datum, adr);
  991. chip->state = FL_WRITING;
  992. INVALIDATE_CACHE_UDELAY(map, chip,
  993. adr, map_bankwidth(map),
  994. chip->word_write_time);
  995. /* See comment above for timeout value. */
  996. timeo = jiffies + uWriteTimeout;
  997. for (;;) {
  998. if (chip->state != FL_WRITING) {
  999. /* Someone's suspended the write. Sleep */
  1000. DECLARE_WAITQUEUE(wait, current);
  1001. set_current_state(TASK_UNINTERRUPTIBLE);
  1002. add_wait_queue(&chip->wq, &wait);
  1003. mutex_unlock(&chip->mutex);
  1004. schedule();
  1005. remove_wait_queue(&chip->wq, &wait);
  1006. timeo = jiffies + (HZ / 2); /* FIXME */
  1007. mutex_lock(&chip->mutex);
  1008. continue;
  1009. }
  1010. if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
  1011. xip_enable(map, chip, adr);
  1012. printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
  1013. xip_disable(map, chip, adr);
  1014. break;
  1015. }
  1016. if (chip_ready(map, adr))
  1017. break;
  1018. /* Latency issues. Drop the lock, wait a while and retry */
  1019. UDELAY(map, chip, adr, 1);
  1020. }
  1021. /* Did we succeed? */
  1022. if (!chip_good(map, adr, datum)) {
  1023. /* reset on all failures. */
  1024. map_write( map, CMD(0xF0), chip->start );
  1025. /* FIXME - should have reset delay before continuing */
  1026. if (++retry_cnt <= MAX_WORD_RETRIES)
  1027. goto retry;
  1028. ret = -EIO;
  1029. }
  1030. xip_enable(map, chip, adr);
  1031. op_done:
  1032. chip->state = FL_READY;
  1033. put_chip(map, chip, adr);
  1034. mutex_unlock(&chip->mutex);
  1035. return ret;
  1036. }
  1037. static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
  1038. size_t *retlen, const u_char *buf)
  1039. {
  1040. struct map_info *map = mtd->priv;
  1041. struct cfi_private *cfi = map->fldrv_priv;
  1042. int ret = 0;
  1043. int chipnum;
  1044. unsigned long ofs, chipstart;
  1045. DECLARE_WAITQUEUE(wait, current);
  1046. *retlen = 0;
  1047. if (!len)
  1048. return 0;
  1049. chipnum = to >> cfi->chipshift;
  1050. ofs = to - (chipnum << cfi->chipshift);
  1051. chipstart = cfi->chips[chipnum].start;
  1052. /* If it's not bus-aligned, do the first byte write */
  1053. if (ofs & (map_bankwidth(map)-1)) {
  1054. unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
  1055. int i = ofs - bus_ofs;
  1056. int n = 0;
  1057. map_word tmp_buf;
  1058. retry:
  1059. mutex_lock(&cfi->chips[chipnum].mutex);
  1060. if (cfi->chips[chipnum].state != FL_READY) {
  1061. set_current_state(TASK_UNINTERRUPTIBLE);
  1062. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1063. mutex_unlock(&cfi->chips[chipnum].mutex);
  1064. schedule();
  1065. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1066. goto retry;
  1067. }
  1068. /* Load 'tmp_buf' with old contents of flash */
  1069. tmp_buf = map_read(map, bus_ofs+chipstart);
  1070. mutex_unlock(&cfi->chips[chipnum].mutex);
  1071. /* Number of bytes to copy from buffer */
  1072. n = min_t(int, len, map_bankwidth(map)-i);
  1073. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1074. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1075. bus_ofs, tmp_buf);
  1076. if (ret)
  1077. return ret;
  1078. ofs += n;
  1079. buf += n;
  1080. (*retlen) += n;
  1081. len -= n;
  1082. if (ofs >> cfi->chipshift) {
  1083. chipnum ++;
  1084. ofs = 0;
  1085. if (chipnum == cfi->numchips)
  1086. return 0;
  1087. }
  1088. }
  1089. /* We are now aligned, write as much as possible */
  1090. while(len >= map_bankwidth(map)) {
  1091. map_word datum;
  1092. datum = map_word_load(map, buf);
  1093. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1094. ofs, datum);
  1095. if (ret)
  1096. return ret;
  1097. ofs += map_bankwidth(map);
  1098. buf += map_bankwidth(map);
  1099. (*retlen) += map_bankwidth(map);
  1100. len -= map_bankwidth(map);
  1101. if (ofs >> cfi->chipshift) {
  1102. chipnum ++;
  1103. ofs = 0;
  1104. if (chipnum == cfi->numchips)
  1105. return 0;
  1106. chipstart = cfi->chips[chipnum].start;
  1107. }
  1108. }
  1109. /* Write the trailing bytes if any */
  1110. if (len & (map_bankwidth(map)-1)) {
  1111. map_word tmp_buf;
  1112. retry1:
  1113. mutex_lock(&cfi->chips[chipnum].mutex);
  1114. if (cfi->chips[chipnum].state != FL_READY) {
  1115. set_current_state(TASK_UNINTERRUPTIBLE);
  1116. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1117. mutex_unlock(&cfi->chips[chipnum].mutex);
  1118. schedule();
  1119. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1120. goto retry1;
  1121. }
  1122. tmp_buf = map_read(map, ofs + chipstart);
  1123. mutex_unlock(&cfi->chips[chipnum].mutex);
  1124. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1125. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1126. ofs, tmp_buf);
  1127. if (ret)
  1128. return ret;
  1129. (*retlen) += len;
  1130. }
  1131. return 0;
  1132. }
  1133. /*
  1134. * FIXME: interleaved mode not tested, and probably not supported!
  1135. */
  1136. static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
  1137. unsigned long adr, const u_char *buf,
  1138. int len)
  1139. {
  1140. struct cfi_private *cfi = map->fldrv_priv;
  1141. unsigned long timeo = jiffies + HZ;
  1142. /* see comments in do_write_oneword() regarding uWriteTimeo. */
  1143. unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
  1144. int ret = -EIO;
  1145. unsigned long cmd_adr;
  1146. int z, words;
  1147. map_word datum;
  1148. adr += chip->start;
  1149. cmd_adr = adr;
  1150. mutex_lock(&chip->mutex);
  1151. ret = get_chip(map, chip, adr, FL_WRITING);
  1152. if (ret) {
  1153. mutex_unlock(&chip->mutex);
  1154. return ret;
  1155. }
  1156. datum = map_word_load(map, buf);
  1157. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1158. __func__, adr, datum.x[0] );
  1159. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1160. ENABLE_VPP(map);
  1161. xip_disable(map, chip, cmd_adr);
  1162. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1163. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1164. /* Write Buffer Load */
  1165. map_write(map, CMD(0x25), cmd_adr);
  1166. chip->state = FL_WRITING_TO_BUFFER;
  1167. /* Write length of data to come */
  1168. words = len / map_bankwidth(map);
  1169. map_write(map, CMD(words - 1), cmd_adr);
  1170. /* Write data */
  1171. z = 0;
  1172. while(z < words * map_bankwidth(map)) {
  1173. datum = map_word_load(map, buf);
  1174. map_write(map, datum, adr + z);
  1175. z += map_bankwidth(map);
  1176. buf += map_bankwidth(map);
  1177. }
  1178. z -= map_bankwidth(map);
  1179. adr += z;
  1180. /* Write Buffer Program Confirm: GO GO GO */
  1181. map_write(map, CMD(0x29), cmd_adr);
  1182. chip->state = FL_WRITING;
  1183. INVALIDATE_CACHE_UDELAY(map, chip,
  1184. adr, map_bankwidth(map),
  1185. chip->word_write_time);
  1186. timeo = jiffies + uWriteTimeout;
  1187. for (;;) {
  1188. if (chip->state != FL_WRITING) {
  1189. /* Someone's suspended the write. Sleep */
  1190. DECLARE_WAITQUEUE(wait, current);
  1191. set_current_state(TASK_UNINTERRUPTIBLE);
  1192. add_wait_queue(&chip->wq, &wait);
  1193. mutex_unlock(&chip->mutex);
  1194. schedule();
  1195. remove_wait_queue(&chip->wq, &wait);
  1196. timeo = jiffies + (HZ / 2); /* FIXME */
  1197. mutex_lock(&chip->mutex);
  1198. continue;
  1199. }
  1200. if (time_after(jiffies, timeo) && !chip_ready(map, adr))
  1201. break;
  1202. if (chip_ready(map, adr)) {
  1203. xip_enable(map, chip, adr);
  1204. goto op_done;
  1205. }
  1206. /* Latency issues. Drop the lock, wait a while and retry */
  1207. UDELAY(map, chip, adr, 1);
  1208. }
  1209. /* reset on all failures. */
  1210. map_write( map, CMD(0xF0), chip->start );
  1211. xip_enable(map, chip, adr);
  1212. /* FIXME - should have reset delay before continuing */
  1213. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1214. __func__ );
  1215. ret = -EIO;
  1216. op_done:
  1217. chip->state = FL_READY;
  1218. put_chip(map, chip, adr);
  1219. mutex_unlock(&chip->mutex);
  1220. return ret;
  1221. }
  1222. static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
  1223. size_t *retlen, const u_char *buf)
  1224. {
  1225. struct map_info *map = mtd->priv;
  1226. struct cfi_private *cfi = map->fldrv_priv;
  1227. int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1228. int ret = 0;
  1229. int chipnum;
  1230. unsigned long ofs;
  1231. *retlen = 0;
  1232. if (!len)
  1233. return 0;
  1234. chipnum = to >> cfi->chipshift;
  1235. ofs = to - (chipnum << cfi->chipshift);
  1236. /* If it's not bus-aligned, do the first word write */
  1237. if (ofs & (map_bankwidth(map)-1)) {
  1238. size_t local_len = (-ofs)&(map_bankwidth(map)-1);
  1239. if (local_len > len)
  1240. local_len = len;
  1241. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1242. local_len, retlen, buf);
  1243. if (ret)
  1244. return ret;
  1245. ofs += local_len;
  1246. buf += local_len;
  1247. len -= local_len;
  1248. if (ofs >> cfi->chipshift) {
  1249. chipnum ++;
  1250. ofs = 0;
  1251. if (chipnum == cfi->numchips)
  1252. return 0;
  1253. }
  1254. }
  1255. /* Write buffer is worth it only if more than one word to write... */
  1256. while (len >= map_bankwidth(map) * 2) {
  1257. /* We must not cross write block boundaries */
  1258. int size = wbufsize - (ofs & (wbufsize-1));
  1259. if (size > len)
  1260. size = len;
  1261. if (size % map_bankwidth(map))
  1262. size -= size % map_bankwidth(map);
  1263. ret = do_write_buffer(map, &cfi->chips[chipnum],
  1264. ofs, buf, size);
  1265. if (ret)
  1266. return ret;
  1267. ofs += size;
  1268. buf += size;
  1269. (*retlen) += size;
  1270. len -= size;
  1271. if (ofs >> cfi->chipshift) {
  1272. chipnum ++;
  1273. ofs = 0;
  1274. if (chipnum == cfi->numchips)
  1275. return 0;
  1276. }
  1277. }
  1278. if (len) {
  1279. size_t retlen_dregs = 0;
  1280. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1281. len, &retlen_dregs, buf);
  1282. *retlen += retlen_dregs;
  1283. return ret;
  1284. }
  1285. return 0;
  1286. }
  1287. /*
  1288. * Handle devices with one erase region, that only implement
  1289. * the chip erase command.
  1290. */
  1291. static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
  1292. {
  1293. struct cfi_private *cfi = map->fldrv_priv;
  1294. unsigned long timeo = jiffies + HZ;
  1295. unsigned long int adr;
  1296. DECLARE_WAITQUEUE(wait, current);
  1297. int ret = 0;
  1298. adr = cfi->addr_unlock1;
  1299. mutex_lock(&chip->mutex);
  1300. ret = get_chip(map, chip, adr, FL_WRITING);
  1301. if (ret) {
  1302. mutex_unlock(&chip->mutex);
  1303. return ret;
  1304. }
  1305. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
  1306. __func__, chip->start );
  1307. XIP_INVAL_CACHED_RANGE(map, adr, map->size);
  1308. ENABLE_VPP(map);
  1309. xip_disable(map, chip, adr);
  1310. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1311. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1312. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1313. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1314. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1315. cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1316. chip->state = FL_ERASING;
  1317. chip->erase_suspended = 0;
  1318. chip->in_progress_block_addr = adr;
  1319. INVALIDATE_CACHE_UDELAY(map, chip,
  1320. adr, map->size,
  1321. chip->erase_time*500);
  1322. timeo = jiffies + (HZ*20);
  1323. for (;;) {
  1324. if (chip->state != FL_ERASING) {
  1325. /* Someone's suspended the erase. Sleep */
  1326. set_current_state(TASK_UNINTERRUPTIBLE);
  1327. add_wait_queue(&chip->wq, &wait);
  1328. mutex_unlock(&chip->mutex);
  1329. schedule();
  1330. remove_wait_queue(&chip->wq, &wait);
  1331. mutex_lock(&chip->mutex);
  1332. continue;
  1333. }
  1334. if (chip->erase_suspended) {
  1335. /* This erase was suspended and resumed.
  1336. Adjust the timeout */
  1337. timeo = jiffies + (HZ*20); /* FIXME */
  1338. chip->erase_suspended = 0;
  1339. }
  1340. if (chip_ready(map, adr))
  1341. break;
  1342. if (time_after(jiffies, timeo)) {
  1343. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1344. __func__ );
  1345. break;
  1346. }
  1347. /* Latency issues. Drop the lock, wait a while and retry */
  1348. UDELAY(map, chip, adr, 1000000/HZ);
  1349. }
  1350. /* Did we succeed? */
  1351. if (!chip_good(map, adr, map_word_ff(map))) {
  1352. /* reset on all failures. */
  1353. map_write( map, CMD(0xF0), chip->start );
  1354. /* FIXME - should have reset delay before continuing */
  1355. ret = -EIO;
  1356. }
  1357. chip->state = FL_READY;
  1358. xip_enable(map, chip, adr);
  1359. put_chip(map, chip, adr);
  1360. mutex_unlock(&chip->mutex);
  1361. return ret;
  1362. }
  1363. static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
  1364. {
  1365. struct cfi_private *cfi = map->fldrv_priv;
  1366. unsigned long timeo = jiffies + HZ;
  1367. DECLARE_WAITQUEUE(wait, current);
  1368. int ret = 0;
  1369. adr += chip->start;
  1370. mutex_lock(&chip->mutex);
  1371. ret = get_chip(map, chip, adr, FL_ERASING);
  1372. if (ret) {
  1373. mutex_unlock(&chip->mutex);
  1374. return ret;
  1375. }
  1376. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
  1377. __func__, adr );
  1378. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1379. ENABLE_VPP(map);
  1380. xip_disable(map, chip, adr);
  1381. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1382. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1383. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1384. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1385. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1386. map_write(map, cfi->sector_erase_cmd, adr);
  1387. chip->state = FL_ERASING;
  1388. chip->erase_suspended = 0;
  1389. chip->in_progress_block_addr = adr;
  1390. INVALIDATE_CACHE_UDELAY(map, chip,
  1391. adr, len,
  1392. chip->erase_time*500);
  1393. timeo = jiffies + (HZ*20);
  1394. for (;;) {
  1395. if (chip->state != FL_ERASING) {
  1396. /* Someone's suspended the erase. Sleep */
  1397. set_current_state(TASK_UNINTERRUPTIBLE);
  1398. add_wait_queue(&chip->wq, &wait);
  1399. mutex_unlock(&chip->mutex);
  1400. schedule();
  1401. remove_wait_queue(&chip->wq, &wait);
  1402. mutex_lock(&chip->mutex);
  1403. continue;
  1404. }
  1405. if (chip->erase_suspended) {
  1406. /* This erase was suspended and resumed.
  1407. Adjust the timeout */
  1408. timeo = jiffies + (HZ*20); /* FIXME */
  1409. chip->erase_suspended = 0;
  1410. }
  1411. if (chip_ready(map, adr)) {
  1412. xip_enable(map, chip, adr);
  1413. break;
  1414. }
  1415. if (time_after(jiffies, timeo)) {
  1416. xip_enable(map, chip, adr);
  1417. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1418. __func__ );
  1419. break;
  1420. }
  1421. /* Latency issues. Drop the lock, wait a while and retry */
  1422. UDELAY(map, chip, adr, 1000000/HZ);
  1423. }
  1424. /* Did we succeed? */
  1425. if (!chip_good(map, adr, map_word_ff(map))) {
  1426. /* reset on all failures. */
  1427. map_write( map, CMD(0xF0), chip->start );
  1428. /* FIXME - should have reset delay before continuing */
  1429. ret = -EIO;
  1430. }
  1431. chip->state = FL_READY;
  1432. put_chip(map, chip, adr);
  1433. mutex_unlock(&chip->mutex);
  1434. return ret;
  1435. }
  1436. static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
  1437. {
  1438. unsigned long ofs, len;
  1439. int ret;
  1440. ofs = instr->addr;
  1441. len = instr->len;
  1442. ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
  1443. if (ret)
  1444. return ret;
  1445. instr->state = MTD_ERASE_DONE;
  1446. mtd_erase_callback(instr);
  1447. return 0;
  1448. }
  1449. static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
  1450. {
  1451. struct map_info *map = mtd->priv;
  1452. struct cfi_private *cfi = map->fldrv_priv;
  1453. int ret = 0;
  1454. if (instr->addr != 0)
  1455. return -EINVAL;
  1456. if (instr->len != mtd->size)
  1457. return -EINVAL;
  1458. ret = do_erase_chip(map, &cfi->chips[0]);
  1459. if (ret)
  1460. return ret;
  1461. instr->state = MTD_ERASE_DONE;
  1462. mtd_erase_callback(instr);
  1463. return 0;
  1464. }
  1465. static int do_atmel_lock(struct map_info *map, struct flchip *chip,
  1466. unsigned long adr, int len, void *thunk)
  1467. {
  1468. struct cfi_private *cfi = map->fldrv_priv;
  1469. int ret;
  1470. mutex_lock(&chip->mutex);
  1471. ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
  1472. if (ret)
  1473. goto out_unlock;
  1474. chip->state = FL_LOCKING;
  1475. DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
  1476. __func__, adr, len);
  1477. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1478. cfi->device_type, NULL);
  1479. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1480. cfi->device_type, NULL);
  1481. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
  1482. cfi->device_type, NULL);
  1483. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1484. cfi->device_type, NULL);
  1485. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1486. cfi->device_type, NULL);
  1487. map_write(map, CMD(0x40), chip->start + adr);
  1488. chip->state = FL_READY;
  1489. put_chip(map, chip, adr + chip->start);
  1490. ret = 0;
  1491. out_unlock:
  1492. mutex_unlock(&chip->mutex);
  1493. return ret;
  1494. }
  1495. static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
  1496. unsigned long adr, int len, void *thunk)
  1497. {
  1498. struct cfi_private *cfi = map->fldrv_priv;
  1499. int ret;
  1500. mutex_lock(&chip->mutex);
  1501. ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
  1502. if (ret)
  1503. goto out_unlock;
  1504. chip->state = FL_UNLOCKING;
  1505. DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
  1506. __func__, adr, len);
  1507. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1508. cfi->device_type, NULL);
  1509. map_write(map, CMD(0x70), adr);
  1510. chip->state = FL_READY;
  1511. put_chip(map, chip, adr + chip->start);
  1512. ret = 0;
  1513. out_unlock:
  1514. mutex_unlock(&chip->mutex);
  1515. return ret;
  1516. }
  1517. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1518. {
  1519. return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
  1520. }
  1521. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1522. {
  1523. return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
  1524. }
  1525. static void cfi_amdstd_sync (struct mtd_info *mtd)
  1526. {
  1527. struct map_info *map = mtd->priv;
  1528. struct cfi_private *cfi = map->fldrv_priv;
  1529. int i;
  1530. struct flchip *chip;
  1531. int ret = 0;
  1532. DECLARE_WAITQUEUE(wait, current);
  1533. for (i=0; !ret && i<cfi->numchips; i++) {
  1534. chip = &cfi->chips[i];
  1535. retry:
  1536. mutex_lock(&chip->mutex);
  1537. switch(chip->state) {
  1538. case FL_READY:
  1539. case FL_STATUS:
  1540. case FL_CFI_QUERY:
  1541. case FL_JEDEC_QUERY:
  1542. chip->oldstate = chip->state;
  1543. chip->state = FL_SYNCING;
  1544. /* No need to wake_up() on this state change -
  1545. * as the whole point is that nobody can do anything
  1546. * with the chip now anyway.
  1547. */
  1548. case FL_SYNCING:
  1549. mutex_unlock(&chip->mutex);
  1550. break;
  1551. default:
  1552. /* Not an idle state */
  1553. set_current_state(TASK_UNINTERRUPTIBLE);
  1554. add_wait_queue(&chip->wq, &wait);
  1555. mutex_unlock(&chip->mutex);
  1556. schedule();
  1557. remove_wait_queue(&chip->wq, &wait);
  1558. goto retry;
  1559. }
  1560. }
  1561. /* Unlock the chips again */
  1562. for (i--; i >=0; i--) {
  1563. chip = &cfi->chips[i];
  1564. mutex_lock(&chip->mutex);
  1565. if (chip->state == FL_SYNCING) {
  1566. chip->state = chip->oldstate;
  1567. wake_up(&chip->wq);
  1568. }
  1569. mutex_unlock(&chip->mutex);
  1570. }
  1571. }
  1572. static int cfi_amdstd_suspend(struct mtd_info *mtd)
  1573. {
  1574. struct map_info *map = mtd->priv;
  1575. struct cfi_private *cfi = map->fldrv_priv;
  1576. int i;
  1577. struct flchip *chip;
  1578. int ret = 0;
  1579. for (i=0; !ret && i<cfi->numchips; i++) {
  1580. chip = &cfi->chips[i];
  1581. mutex_lock(&chip->mutex);
  1582. switch(chip->state) {
  1583. case FL_READY:
  1584. case FL_STATUS:
  1585. case FL_CFI_QUERY:
  1586. case FL_JEDEC_QUERY:
  1587. chip->oldstate = chip->state;
  1588. chip->state = FL_PM_SUSPENDED;
  1589. /* No need to wake_up() on this state change -
  1590. * as the whole point is that nobody can do anything
  1591. * with the chip now anyway.
  1592. */
  1593. case FL_PM_SUSPENDED:
  1594. break;
  1595. default:
  1596. ret = -EAGAIN;
  1597. break;
  1598. }
  1599. mutex_unlock(&chip->mutex);
  1600. }
  1601. /* Unlock the chips again */
  1602. if (ret) {
  1603. for (i--; i >=0; i--) {
  1604. chip = &cfi->chips[i];
  1605. mutex_lock(&chip->mutex);
  1606. if (chip->state == FL_PM_SUSPENDED) {
  1607. chip->state = chip->oldstate;
  1608. wake_up(&chip->wq);
  1609. }
  1610. mutex_unlock(&chip->mutex);
  1611. }
  1612. }
  1613. return ret;
  1614. }
  1615. static void cfi_amdstd_resume(struct mtd_info *mtd)
  1616. {
  1617. struct map_info *map = mtd->priv;
  1618. struct cfi_private *cfi = map->fldrv_priv;
  1619. int i;
  1620. struct flchip *chip;
  1621. for (i=0; i<cfi->numchips; i++) {
  1622. chip = &cfi->chips[i];
  1623. mutex_lock(&chip->mutex);
  1624. if (chip->state == FL_PM_SUSPENDED) {
  1625. chip->state = FL_READY;
  1626. map_write(map, CMD(0xF0), chip->start);
  1627. wake_up(&chip->wq);
  1628. }
  1629. else
  1630. printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
  1631. mutex_unlock(&chip->mutex);
  1632. }
  1633. }
  1634. /*
  1635. * Ensure that the flash device is put back into read array mode before
  1636. * unloading the driver or rebooting. On some systems, rebooting while
  1637. * the flash is in query/program/erase mode will prevent the CPU from
  1638. * fetching the bootloader code, requiring a hard reset or power cycle.
  1639. */
  1640. static int cfi_amdstd_reset(struct mtd_info *mtd)
  1641. {
  1642. struct map_info *map = mtd->priv;
  1643. struct cfi_private *cfi = map->fldrv_priv;
  1644. int i, ret;
  1645. struct flchip *chip;
  1646. for (i = 0; i < cfi->numchips; i++) {
  1647. chip = &cfi->chips[i];
  1648. mutex_lock(&chip->mutex);
  1649. ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
  1650. if (!ret) {
  1651. map_write(map, CMD(0xF0), chip->start);
  1652. chip->state = FL_SHUTDOWN;
  1653. put_chip(map, chip, chip->start);
  1654. }
  1655. mutex_unlock(&chip->mutex);
  1656. }
  1657. return 0;
  1658. }
  1659. static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
  1660. void *v)
  1661. {
  1662. struct mtd_info *mtd;
  1663. mtd = container_of(nb, struct mtd_info, reboot_notifier);
  1664. cfi_amdstd_reset(mtd);
  1665. return NOTIFY_DONE;
  1666. }
  1667. static void cfi_amdstd_destroy(struct mtd_info *mtd)
  1668. {
  1669. struct map_info *map = mtd->priv;
  1670. struct cfi_private *cfi = map->fldrv_priv;
  1671. cfi_amdstd_reset(mtd);
  1672. unregister_reboot_notifier(&mtd->reboot_notifier);
  1673. kfree(cfi->cmdset_priv);
  1674. kfree(cfi->cfiq);
  1675. kfree(cfi);
  1676. kfree(mtd->eraseregions);
  1677. }
  1678. MODULE_LICENSE("GPL");
  1679. MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
  1680. MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
  1681. MODULE_ALIAS("cfi_cmdset_0006");
  1682. MODULE_ALIAS("cfi_cmdset_0701");