cfi_cmdset_0002.c 54 KB

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