cfi_cmdset_0002.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  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_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
  419. struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
  420. EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
  421. EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
  422. EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
  423. static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
  424. {
  425. struct map_info *map = mtd->priv;
  426. struct cfi_private *cfi = map->fldrv_priv;
  427. unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
  428. unsigned long offset = 0;
  429. int i,j;
  430. printk(KERN_NOTICE "number of %s chips: %d\n",
  431. (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
  432. /* Select the correct geometry setup */
  433. mtd->size = devsize * cfi->numchips;
  434. mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
  435. mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
  436. * mtd->numeraseregions, GFP_KERNEL);
  437. if (!mtd->eraseregions) {
  438. printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
  439. goto setup_err;
  440. }
  441. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  442. unsigned long ernum, ersize;
  443. ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
  444. ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
  445. if (mtd->erasesize < ersize) {
  446. mtd->erasesize = ersize;
  447. }
  448. for (j=0; j<cfi->numchips; j++) {
  449. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
  450. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
  451. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
  452. }
  453. offset += (ersize * ernum);
  454. }
  455. if (offset != devsize) {
  456. /* Argh */
  457. printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
  458. goto setup_err;
  459. }
  460. #if 0
  461. // debug
  462. for (i=0; i<mtd->numeraseregions;i++){
  463. printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
  464. i,mtd->eraseregions[i].offset,
  465. mtd->eraseregions[i].erasesize,
  466. mtd->eraseregions[i].numblocks);
  467. }
  468. #endif
  469. __module_get(THIS_MODULE);
  470. register_reboot_notifier(&mtd->reboot_notifier);
  471. return mtd;
  472. setup_err:
  473. kfree(mtd->eraseregions);
  474. kfree(mtd);
  475. kfree(cfi->cmdset_priv);
  476. kfree(cfi->cfiq);
  477. return NULL;
  478. }
  479. /*
  480. * Return true if the chip is ready.
  481. *
  482. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  483. * non-suspended sector) and is indicated by no toggle bits toggling.
  484. *
  485. * Note that anything more complicated than checking if no bits are toggling
  486. * (including checking DQ5 for an error status) is tricky to get working
  487. * correctly and is therefore not done (particulary with interleaved chips
  488. * as each chip must be checked independantly of the others).
  489. */
  490. static int __xipram chip_ready(struct map_info *map, unsigned long addr)
  491. {
  492. map_word d, t;
  493. d = map_read(map, addr);
  494. t = map_read(map, addr);
  495. return map_word_equal(map, d, t);
  496. }
  497. /*
  498. * Return true if the chip is ready and has the correct value.
  499. *
  500. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  501. * non-suspended sector) and it is indicated by no bits toggling.
  502. *
  503. * Error are indicated by toggling bits or bits held with the wrong value,
  504. * or with bits toggling.
  505. *
  506. * Note that anything more complicated than checking if no bits are toggling
  507. * (including checking DQ5 for an error status) is tricky to get working
  508. * correctly and is therefore not done (particulary with interleaved chips
  509. * as each chip must be checked independantly of the others).
  510. *
  511. */
  512. static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
  513. {
  514. map_word oldd, curd;
  515. oldd = map_read(map, addr);
  516. curd = map_read(map, addr);
  517. return map_word_equal(map, oldd, curd) &&
  518. map_word_equal(map, curd, expected);
  519. }
  520. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  521. {
  522. DECLARE_WAITQUEUE(wait, current);
  523. struct cfi_private *cfi = map->fldrv_priv;
  524. unsigned long timeo;
  525. struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
  526. resettime:
  527. timeo = jiffies + HZ;
  528. retry:
  529. switch (chip->state) {
  530. case FL_STATUS:
  531. for (;;) {
  532. if (chip_ready(map, adr))
  533. break;
  534. if (time_after(jiffies, timeo)) {
  535. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  536. return -EIO;
  537. }
  538. mutex_unlock(&chip->mutex);
  539. cfi_udelay(1);
  540. mutex_lock(&chip->mutex);
  541. /* Someone else might have been playing with it. */
  542. goto retry;
  543. }
  544. case FL_READY:
  545. case FL_CFI_QUERY:
  546. case FL_JEDEC_QUERY:
  547. return 0;
  548. case FL_ERASING:
  549. if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
  550. !(mode == FL_READY || mode == FL_POINT ||
  551. (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
  552. goto sleep;
  553. /* We could check to see if we're trying to access the sector
  554. * that is currently being erased. However, no user will try
  555. * anything like that so we just wait for the timeout. */
  556. /* Erase suspend */
  557. /* It's harmless to issue the Erase-Suspend and Erase-Resume
  558. * commands when the erase algorithm isn't in progress. */
  559. map_write(map, CMD(0xB0), chip->in_progress_block_addr);
  560. chip->oldstate = FL_ERASING;
  561. chip->state = FL_ERASE_SUSPENDING;
  562. chip->erase_suspended = 1;
  563. for (;;) {
  564. if (chip_ready(map, adr))
  565. break;
  566. if (time_after(jiffies, timeo)) {
  567. /* Should have suspended the erase by now.
  568. * Send an Erase-Resume command as either
  569. * there was an error (so leave the erase
  570. * routine to recover from it) or we trying to
  571. * use the erase-in-progress sector. */
  572. map_write(map, CMD(0x30), chip->in_progress_block_addr);
  573. chip->state = FL_ERASING;
  574. chip->oldstate = FL_READY;
  575. printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
  576. return -EIO;
  577. }
  578. mutex_unlock(&chip->mutex);
  579. cfi_udelay(1);
  580. mutex_lock(&chip->mutex);
  581. /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
  582. So we can just loop here. */
  583. }
  584. chip->state = FL_READY;
  585. return 0;
  586. case FL_XIP_WHILE_ERASING:
  587. if (mode != FL_READY && mode != FL_POINT &&
  588. (!cfip || !(cfip->EraseSuspend&2)))
  589. goto sleep;
  590. chip->oldstate = chip->state;
  591. chip->state = FL_READY;
  592. return 0;
  593. case FL_SHUTDOWN:
  594. /* The machine is rebooting */
  595. return -EIO;
  596. case FL_POINT:
  597. /* Only if there's no operation suspended... */
  598. if (mode == FL_READY && chip->oldstate == FL_READY)
  599. return 0;
  600. default:
  601. sleep:
  602. set_current_state(TASK_UNINTERRUPTIBLE);
  603. add_wait_queue(&chip->wq, &wait);
  604. mutex_unlock(&chip->mutex);
  605. schedule();
  606. remove_wait_queue(&chip->wq, &wait);
  607. mutex_lock(&chip->mutex);
  608. goto resettime;
  609. }
  610. }
  611. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
  612. {
  613. struct cfi_private *cfi = map->fldrv_priv;
  614. switch(chip->oldstate) {
  615. case FL_ERASING:
  616. chip->state = chip->oldstate;
  617. map_write(map, CMD(0x30), chip->in_progress_block_addr);
  618. chip->oldstate = FL_READY;
  619. chip->state = FL_ERASING;
  620. break;
  621. case FL_XIP_WHILE_ERASING:
  622. chip->state = chip->oldstate;
  623. chip->oldstate = FL_READY;
  624. break;
  625. case FL_READY:
  626. case FL_STATUS:
  627. /* We should really make set_vpp() count, rather than doing this */
  628. DISABLE_VPP(map);
  629. break;
  630. default:
  631. printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
  632. }
  633. wake_up(&chip->wq);
  634. }
  635. #ifdef CONFIG_MTD_XIP
  636. /*
  637. * No interrupt what so ever can be serviced while the flash isn't in array
  638. * mode. This is ensured by the xip_disable() and xip_enable() functions
  639. * enclosing any code path where the flash is known not to be in array mode.
  640. * And within a XIP disabled code path, only functions marked with __xipram
  641. * may be called and nothing else (it's a good thing to inspect generated
  642. * assembly to make sure inline functions were actually inlined and that gcc
  643. * didn't emit calls to its own support functions). Also configuring MTD CFI
  644. * support to a single buswidth and a single interleave is also recommended.
  645. */
  646. static void xip_disable(struct map_info *map, struct flchip *chip,
  647. unsigned long adr)
  648. {
  649. /* TODO: chips with no XIP use should ignore and return */
  650. (void) map_read(map, adr); /* ensure mmu mapping is up to date */
  651. local_irq_disable();
  652. }
  653. static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
  654. unsigned long adr)
  655. {
  656. struct cfi_private *cfi = map->fldrv_priv;
  657. if (chip->state != FL_POINT && chip->state != FL_READY) {
  658. map_write(map, CMD(0xf0), adr);
  659. chip->state = FL_READY;
  660. }
  661. (void) map_read(map, adr);
  662. xip_iprefetch();
  663. local_irq_enable();
  664. }
  665. /*
  666. * When a delay is required for the flash operation to complete, the
  667. * xip_udelay() function is polling for both the given timeout and pending
  668. * (but still masked) hardware interrupts. Whenever there is an interrupt
  669. * pending then the flash erase operation is suspended, array mode restored
  670. * and interrupts unmasked. Task scheduling might also happen at that
  671. * point. The CPU eventually returns from the interrupt or the call to
  672. * schedule() and the suspended flash operation is resumed for the remaining
  673. * of the delay period.
  674. *
  675. * Warning: this function _will_ fool interrupt latency tracing tools.
  676. */
  677. static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
  678. unsigned long adr, int usec)
  679. {
  680. struct cfi_private *cfi = map->fldrv_priv;
  681. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  682. map_word status, OK = CMD(0x80);
  683. unsigned long suspended, start = xip_currtime();
  684. flstate_t oldstate;
  685. do {
  686. cpu_relax();
  687. if (xip_irqpending() && extp &&
  688. ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
  689. (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
  690. /*
  691. * Let's suspend the erase operation when supported.
  692. * Note that we currently don't try to suspend
  693. * interleaved chips if there is already another
  694. * operation suspended (imagine what happens
  695. * when one chip was already done with the current
  696. * operation while another chip suspended it, then
  697. * we resume the whole thing at once). Yes, it
  698. * can happen!
  699. */
  700. map_write(map, CMD(0xb0), adr);
  701. usec -= xip_elapsed_since(start);
  702. suspended = xip_currtime();
  703. do {
  704. if (xip_elapsed_since(suspended) > 100000) {
  705. /*
  706. * The chip doesn't want to suspend
  707. * after waiting for 100 msecs.
  708. * This is a critical error but there
  709. * is not much we can do here.
  710. */
  711. return;
  712. }
  713. status = map_read(map, adr);
  714. } while (!map_word_andequal(map, status, OK, OK));
  715. /* Suspend succeeded */
  716. oldstate = chip->state;
  717. if (!map_word_bitsset(map, status, CMD(0x40)))
  718. break;
  719. chip->state = FL_XIP_WHILE_ERASING;
  720. chip->erase_suspended = 1;
  721. map_write(map, CMD(0xf0), adr);
  722. (void) map_read(map, adr);
  723. xip_iprefetch();
  724. local_irq_enable();
  725. mutex_unlock(&chip->mutex);
  726. xip_iprefetch();
  727. cond_resched();
  728. /*
  729. * We're back. However someone else might have
  730. * decided to go write to the chip if we are in
  731. * a suspended erase state. If so let's wait
  732. * until it's done.
  733. */
  734. mutex_lock(&chip->mutex);
  735. while (chip->state != FL_XIP_WHILE_ERASING) {
  736. DECLARE_WAITQUEUE(wait, current);
  737. set_current_state(TASK_UNINTERRUPTIBLE);
  738. add_wait_queue(&chip->wq, &wait);
  739. mutex_unlock(&chip->mutex);
  740. schedule();
  741. remove_wait_queue(&chip->wq, &wait);
  742. mutex_lock(&chip->mutex);
  743. }
  744. /* Disallow XIP again */
  745. local_irq_disable();
  746. /* Resume the write or erase operation */
  747. map_write(map, CMD(0x30), adr);
  748. chip->state = oldstate;
  749. start = xip_currtime();
  750. } else if (usec >= 1000000/HZ) {
  751. /*
  752. * Try to save on CPU power when waiting delay
  753. * is at least a system timer tick period.
  754. * No need to be extremely accurate here.
  755. */
  756. xip_cpu_idle();
  757. }
  758. status = map_read(map, adr);
  759. } while (!map_word_andequal(map, status, OK, OK)
  760. && xip_elapsed_since(start) < usec);
  761. }
  762. #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
  763. /*
  764. * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
  765. * the flash is actively programming or erasing since we have to poll for
  766. * the operation to complete anyway. We can't do that in a generic way with
  767. * a XIP setup so do it before the actual flash operation in this case
  768. * and stub it out from INVALIDATE_CACHE_UDELAY.
  769. */
  770. #define XIP_INVAL_CACHED_RANGE(map, from, size) \
  771. INVALIDATE_CACHED_RANGE(map, from, size)
  772. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  773. UDELAY(map, chip, adr, usec)
  774. /*
  775. * Extra notes:
  776. *
  777. * Activating this XIP support changes the way the code works a bit. For
  778. * example the code to suspend the current process when concurrent access
  779. * happens is never executed because xip_udelay() will always return with the
  780. * same chip state as it was entered with. This is why there is no care for
  781. * the presence of add_wait_queue() or schedule() calls from within a couple
  782. * xip_disable()'d areas of code, like in do_erase_oneblock for example.
  783. * The queueing and scheduling are always happening within xip_udelay().
  784. *
  785. * Similarly, get_chip() and put_chip() just happen to always be executed
  786. * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
  787. * is in array mode, therefore never executing many cases therein and not
  788. * causing any problem with XIP.
  789. */
  790. #else
  791. #define xip_disable(map, chip, adr)
  792. #define xip_enable(map, chip, adr)
  793. #define XIP_INVAL_CACHED_RANGE(x...)
  794. #define UDELAY(map, chip, adr, usec) \
  795. do { \
  796. mutex_unlock(&chip->mutex); \
  797. cfi_udelay(usec); \
  798. mutex_lock(&chip->mutex); \
  799. } while (0)
  800. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  801. do { \
  802. mutex_unlock(&chip->mutex); \
  803. INVALIDATE_CACHED_RANGE(map, adr, len); \
  804. cfi_udelay(usec); \
  805. mutex_lock(&chip->mutex); \
  806. } while (0)
  807. #endif
  808. static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  809. {
  810. unsigned long cmd_addr;
  811. struct cfi_private *cfi = map->fldrv_priv;
  812. int ret;
  813. adr += chip->start;
  814. /* Ensure cmd read/writes are aligned. */
  815. cmd_addr = adr & ~(map_bankwidth(map)-1);
  816. mutex_lock(&chip->mutex);
  817. ret = get_chip(map, chip, cmd_addr, FL_READY);
  818. if (ret) {
  819. mutex_unlock(&chip->mutex);
  820. return ret;
  821. }
  822. if (chip->state != FL_POINT && chip->state != FL_READY) {
  823. map_write(map, CMD(0xf0), cmd_addr);
  824. chip->state = FL_READY;
  825. }
  826. map_copy_from(map, buf, adr, len);
  827. put_chip(map, chip, cmd_addr);
  828. mutex_unlock(&chip->mutex);
  829. return 0;
  830. }
  831. static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  832. {
  833. struct map_info *map = mtd->priv;
  834. struct cfi_private *cfi = map->fldrv_priv;
  835. unsigned long ofs;
  836. int chipnum;
  837. int ret = 0;
  838. /* ofs: offset within the first chip that the first read should start */
  839. chipnum = (from >> cfi->chipshift);
  840. ofs = from - (chipnum << cfi->chipshift);
  841. *retlen = 0;
  842. while (len) {
  843. unsigned long thislen;
  844. if (chipnum >= cfi->numchips)
  845. break;
  846. if ((len + ofs -1) >> cfi->chipshift)
  847. thislen = (1<<cfi->chipshift) - ofs;
  848. else
  849. thislen = len;
  850. ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  851. if (ret)
  852. break;
  853. *retlen += thislen;
  854. len -= thislen;
  855. buf += thislen;
  856. ofs = 0;
  857. chipnum++;
  858. }
  859. return ret;
  860. }
  861. static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  862. {
  863. DECLARE_WAITQUEUE(wait, current);
  864. unsigned long timeo = jiffies + HZ;
  865. struct cfi_private *cfi = map->fldrv_priv;
  866. retry:
  867. mutex_lock(&chip->mutex);
  868. if (chip->state != FL_READY){
  869. #if 0
  870. printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state);
  871. #endif
  872. set_current_state(TASK_UNINTERRUPTIBLE);
  873. add_wait_queue(&chip->wq, &wait);
  874. mutex_unlock(&chip->mutex);
  875. schedule();
  876. remove_wait_queue(&chip->wq, &wait);
  877. #if 0
  878. if(signal_pending(current))
  879. return -EINTR;
  880. #endif
  881. timeo = jiffies + HZ;
  882. goto retry;
  883. }
  884. adr += chip->start;
  885. chip->state = FL_READY;
  886. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  887. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  888. cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  889. map_copy_from(map, buf, adr, len);
  890. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  891. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  892. cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  893. cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  894. wake_up(&chip->wq);
  895. mutex_unlock(&chip->mutex);
  896. return 0;
  897. }
  898. static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  899. {
  900. struct map_info *map = mtd->priv;
  901. struct cfi_private *cfi = map->fldrv_priv;
  902. unsigned long ofs;
  903. int chipnum;
  904. int ret = 0;
  905. /* ofs: offset within the first chip that the first read should start */
  906. /* 8 secsi bytes per chip */
  907. chipnum=from>>3;
  908. ofs=from & 7;
  909. *retlen = 0;
  910. while (len) {
  911. unsigned long thislen;
  912. if (chipnum >= cfi->numchips)
  913. break;
  914. if ((len + ofs -1) >> 3)
  915. thislen = (1<<3) - ofs;
  916. else
  917. thislen = len;
  918. ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  919. if (ret)
  920. break;
  921. *retlen += thislen;
  922. len -= thislen;
  923. buf += thislen;
  924. ofs = 0;
  925. chipnum++;
  926. }
  927. return ret;
  928. }
  929. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
  930. {
  931. struct cfi_private *cfi = map->fldrv_priv;
  932. unsigned long timeo = jiffies + HZ;
  933. /*
  934. * We use a 1ms + 1 jiffies generic timeout for writes (most devices
  935. * have a max write time of a few hundreds usec). However, we should
  936. * use the maximum timeout value given by the chip at probe time
  937. * instead. Unfortunately, struct flchip does have a field for
  938. * maximum timeout, only for typical which can be far too short
  939. * depending of the conditions. The ' + 1' is to avoid having a
  940. * timeout of 0 jiffies if HZ is smaller than 1000.
  941. */
  942. unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
  943. int ret = 0;
  944. map_word oldd;
  945. int retry_cnt = 0;
  946. adr += chip->start;
  947. mutex_lock(&chip->mutex);
  948. ret = get_chip(map, chip, adr, FL_WRITING);
  949. if (ret) {
  950. mutex_unlock(&chip->mutex);
  951. return ret;
  952. }
  953. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  954. __func__, adr, datum.x[0] );
  955. /*
  956. * Check for a NOP for the case when the datum to write is already
  957. * present - it saves time and works around buggy chips that corrupt
  958. * data at other locations when 0xff is written to a location that
  959. * already contains 0xff.
  960. */
  961. oldd = map_read(map, adr);
  962. if (map_word_equal(map, oldd, datum)) {
  963. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
  964. __func__);
  965. goto op_done;
  966. }
  967. XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
  968. ENABLE_VPP(map);
  969. xip_disable(map, chip, adr);
  970. retry:
  971. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  972. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  973. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  974. map_write(map, datum, adr);
  975. chip->state = FL_WRITING;
  976. INVALIDATE_CACHE_UDELAY(map, chip,
  977. adr, map_bankwidth(map),
  978. chip->word_write_time);
  979. /* See comment above for timeout value. */
  980. timeo = jiffies + uWriteTimeout;
  981. for (;;) {
  982. if (chip->state != FL_WRITING) {
  983. /* Someone's suspended the write. Sleep */
  984. DECLARE_WAITQUEUE(wait, current);
  985. set_current_state(TASK_UNINTERRUPTIBLE);
  986. add_wait_queue(&chip->wq, &wait);
  987. mutex_unlock(&chip->mutex);
  988. schedule();
  989. remove_wait_queue(&chip->wq, &wait);
  990. timeo = jiffies + (HZ / 2); /* FIXME */
  991. mutex_lock(&chip->mutex);
  992. continue;
  993. }
  994. if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
  995. xip_enable(map, chip, adr);
  996. printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
  997. xip_disable(map, chip, adr);
  998. break;
  999. }
  1000. if (chip_ready(map, adr))
  1001. break;
  1002. /* Latency issues. Drop the lock, wait a while and retry */
  1003. UDELAY(map, chip, adr, 1);
  1004. }
  1005. /* Did we succeed? */
  1006. if (!chip_good(map, adr, datum)) {
  1007. /* reset on all failures. */
  1008. map_write( map, CMD(0xF0), chip->start );
  1009. /* FIXME - should have reset delay before continuing */
  1010. if (++retry_cnt <= MAX_WORD_RETRIES)
  1011. goto retry;
  1012. ret = -EIO;
  1013. }
  1014. xip_enable(map, chip, adr);
  1015. op_done:
  1016. chip->state = FL_READY;
  1017. put_chip(map, chip, adr);
  1018. mutex_unlock(&chip->mutex);
  1019. return ret;
  1020. }
  1021. static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
  1022. size_t *retlen, const u_char *buf)
  1023. {
  1024. struct map_info *map = mtd->priv;
  1025. struct cfi_private *cfi = map->fldrv_priv;
  1026. int ret = 0;
  1027. int chipnum;
  1028. unsigned long ofs, chipstart;
  1029. DECLARE_WAITQUEUE(wait, current);
  1030. *retlen = 0;
  1031. if (!len)
  1032. return 0;
  1033. chipnum = to >> cfi->chipshift;
  1034. ofs = to - (chipnum << cfi->chipshift);
  1035. chipstart = cfi->chips[chipnum].start;
  1036. /* If it's not bus-aligned, do the first byte write */
  1037. if (ofs & (map_bankwidth(map)-1)) {
  1038. unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
  1039. int i = ofs - bus_ofs;
  1040. int n = 0;
  1041. map_word tmp_buf;
  1042. retry:
  1043. mutex_lock(&cfi->chips[chipnum].mutex);
  1044. if (cfi->chips[chipnum].state != FL_READY) {
  1045. #if 0
  1046. printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
  1047. #endif
  1048. set_current_state(TASK_UNINTERRUPTIBLE);
  1049. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1050. mutex_unlock(&cfi->chips[chipnum].mutex);
  1051. schedule();
  1052. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1053. #if 0
  1054. if(signal_pending(current))
  1055. return -EINTR;
  1056. #endif
  1057. goto retry;
  1058. }
  1059. /* Load 'tmp_buf' with old contents of flash */
  1060. tmp_buf = map_read(map, bus_ofs+chipstart);
  1061. mutex_unlock(&cfi->chips[chipnum].mutex);
  1062. /* Number of bytes to copy from buffer */
  1063. n = min_t(int, len, map_bankwidth(map)-i);
  1064. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1065. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1066. bus_ofs, tmp_buf);
  1067. if (ret)
  1068. return ret;
  1069. ofs += n;
  1070. buf += n;
  1071. (*retlen) += n;
  1072. len -= n;
  1073. if (ofs >> cfi->chipshift) {
  1074. chipnum ++;
  1075. ofs = 0;
  1076. if (chipnum == cfi->numchips)
  1077. return 0;
  1078. }
  1079. }
  1080. /* We are now aligned, write as much as possible */
  1081. while(len >= map_bankwidth(map)) {
  1082. map_word datum;
  1083. datum = map_word_load(map, buf);
  1084. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1085. ofs, datum);
  1086. if (ret)
  1087. return ret;
  1088. ofs += map_bankwidth(map);
  1089. buf += map_bankwidth(map);
  1090. (*retlen) += map_bankwidth(map);
  1091. len -= map_bankwidth(map);
  1092. if (ofs >> cfi->chipshift) {
  1093. chipnum ++;
  1094. ofs = 0;
  1095. if (chipnum == cfi->numchips)
  1096. return 0;
  1097. chipstart = cfi->chips[chipnum].start;
  1098. }
  1099. }
  1100. /* Write the trailing bytes if any */
  1101. if (len & (map_bankwidth(map)-1)) {
  1102. map_word tmp_buf;
  1103. retry1:
  1104. mutex_lock(&cfi->chips[chipnum].mutex);
  1105. if (cfi->chips[chipnum].state != FL_READY) {
  1106. #if 0
  1107. printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
  1108. #endif
  1109. set_current_state(TASK_UNINTERRUPTIBLE);
  1110. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1111. mutex_unlock(&cfi->chips[chipnum].mutex);
  1112. schedule();
  1113. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1114. #if 0
  1115. if(signal_pending(current))
  1116. return -EINTR;
  1117. #endif
  1118. goto retry1;
  1119. }
  1120. tmp_buf = map_read(map, ofs + chipstart);
  1121. mutex_unlock(&cfi->chips[chipnum].mutex);
  1122. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1123. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1124. ofs, tmp_buf);
  1125. if (ret)
  1126. return ret;
  1127. (*retlen) += len;
  1128. }
  1129. return 0;
  1130. }
  1131. /*
  1132. * FIXME: interleaved mode not tested, and probably not supported!
  1133. */
  1134. static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
  1135. unsigned long adr, const u_char *buf,
  1136. int len)
  1137. {
  1138. struct cfi_private *cfi = map->fldrv_priv;
  1139. unsigned long timeo = jiffies + HZ;
  1140. /* see comments in do_write_oneword() regarding uWriteTimeo. */
  1141. unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
  1142. int ret = -EIO;
  1143. unsigned long cmd_adr;
  1144. int z, words;
  1145. map_word datum;
  1146. adr += chip->start;
  1147. cmd_adr = adr;
  1148. mutex_lock(&chip->mutex);
  1149. ret = get_chip(map, chip, adr, FL_WRITING);
  1150. if (ret) {
  1151. mutex_unlock(&chip->mutex);
  1152. return ret;
  1153. }
  1154. datum = map_word_load(map, buf);
  1155. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1156. __func__, adr, datum.x[0] );
  1157. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1158. ENABLE_VPP(map);
  1159. xip_disable(map, chip, cmd_adr);
  1160. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1161. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1162. //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1163. /* Write Buffer Load */
  1164. map_write(map, CMD(0x25), cmd_adr);
  1165. chip->state = FL_WRITING_TO_BUFFER;
  1166. /* Write length of data to come */
  1167. words = len / map_bankwidth(map);
  1168. map_write(map, CMD(words - 1), cmd_adr);
  1169. /* Write data */
  1170. z = 0;
  1171. while(z < words * map_bankwidth(map)) {
  1172. datum = map_word_load(map, buf);
  1173. map_write(map, datum, adr + z);
  1174. z += map_bankwidth(map);
  1175. buf += map_bankwidth(map);
  1176. }
  1177. z -= map_bankwidth(map);
  1178. adr += z;
  1179. /* Write Buffer Program Confirm: GO GO GO */
  1180. map_write(map, CMD(0x29), cmd_adr);
  1181. chip->state = FL_WRITING;
  1182. INVALIDATE_CACHE_UDELAY(map, chip,
  1183. adr, map_bankwidth(map),
  1184. chip->word_write_time);
  1185. timeo = jiffies + uWriteTimeout;
  1186. for (;;) {
  1187. if (chip->state != FL_WRITING) {
  1188. /* Someone's suspended the write. Sleep */
  1189. DECLARE_WAITQUEUE(wait, current);
  1190. set_current_state(TASK_UNINTERRUPTIBLE);
  1191. add_wait_queue(&chip->wq, &wait);
  1192. mutex_unlock(&chip->mutex);
  1193. schedule();
  1194. remove_wait_queue(&chip->wq, &wait);
  1195. timeo = jiffies + (HZ / 2); /* FIXME */
  1196. mutex_lock(&chip->mutex);
  1197. continue;
  1198. }
  1199. if (time_after(jiffies, timeo) && !chip_ready(map, adr))
  1200. break;
  1201. if (chip_ready(map, adr)) {
  1202. xip_enable(map, chip, adr);
  1203. goto op_done;
  1204. }
  1205. /* Latency issues. Drop the lock, wait a while and retry */
  1206. UDELAY(map, chip, adr, 1);
  1207. }
  1208. /* reset on all failures. */
  1209. map_write( map, CMD(0xF0), chip->start );
  1210. xip_enable(map, chip, adr);
  1211. /* FIXME - should have reset delay before continuing */
  1212. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1213. __func__ );
  1214. ret = -EIO;
  1215. op_done:
  1216. chip->state = FL_READY;
  1217. put_chip(map, chip, adr);
  1218. mutex_unlock(&chip->mutex);
  1219. return ret;
  1220. }
  1221. static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
  1222. size_t *retlen, const u_char *buf)
  1223. {
  1224. struct map_info *map = mtd->priv;
  1225. struct cfi_private *cfi = map->fldrv_priv;
  1226. int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1227. int ret = 0;
  1228. int chipnum;
  1229. unsigned long ofs;
  1230. *retlen = 0;
  1231. if (!len)
  1232. return 0;
  1233. chipnum = to >> cfi->chipshift;
  1234. ofs = to - (chipnum << cfi->chipshift);
  1235. /* If it's not bus-aligned, do the first word write */
  1236. if (ofs & (map_bankwidth(map)-1)) {
  1237. size_t local_len = (-ofs)&(map_bankwidth(map)-1);
  1238. if (local_len > len)
  1239. local_len = len;
  1240. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1241. local_len, retlen, buf);
  1242. if (ret)
  1243. return ret;
  1244. ofs += local_len;
  1245. buf += local_len;
  1246. len -= local_len;
  1247. if (ofs >> cfi->chipshift) {
  1248. chipnum ++;
  1249. ofs = 0;
  1250. if (chipnum == cfi->numchips)
  1251. return 0;
  1252. }
  1253. }
  1254. /* Write buffer is worth it only if more than one word to write... */
  1255. while (len >= map_bankwidth(map) * 2) {
  1256. /* We must not cross write block boundaries */
  1257. int size = wbufsize - (ofs & (wbufsize-1));
  1258. if (size > len)
  1259. size = len;
  1260. if (size % map_bankwidth(map))
  1261. size -= size % map_bankwidth(map);
  1262. ret = do_write_buffer(map, &cfi->chips[chipnum],
  1263. ofs, buf, size);
  1264. if (ret)
  1265. return ret;
  1266. ofs += size;
  1267. buf += size;
  1268. (*retlen) += size;
  1269. len -= size;
  1270. if (ofs >> cfi->chipshift) {
  1271. chipnum ++;
  1272. ofs = 0;
  1273. if (chipnum == cfi->numchips)
  1274. return 0;
  1275. }
  1276. }
  1277. if (len) {
  1278. size_t retlen_dregs = 0;
  1279. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1280. len, &retlen_dregs, buf);
  1281. *retlen += retlen_dregs;
  1282. return ret;
  1283. }
  1284. return 0;
  1285. }
  1286. /*
  1287. * Handle devices with one erase region, that only implement
  1288. * the chip erase command.
  1289. */
  1290. static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
  1291. {
  1292. struct cfi_private *cfi = map->fldrv_priv;
  1293. unsigned long timeo = jiffies + HZ;
  1294. unsigned long int adr;
  1295. DECLARE_WAITQUEUE(wait, current);
  1296. int ret = 0;
  1297. adr = cfi->addr_unlock1;
  1298. mutex_lock(&chip->mutex);
  1299. ret = get_chip(map, chip, adr, FL_WRITING);
  1300. if (ret) {
  1301. mutex_unlock(&chip->mutex);
  1302. return ret;
  1303. }
  1304. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
  1305. __func__, chip->start );
  1306. XIP_INVAL_CACHED_RANGE(map, adr, map->size);
  1307. ENABLE_VPP(map);
  1308. xip_disable(map, chip, adr);
  1309. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1310. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1311. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1312. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1313. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1314. cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1315. chip->state = FL_ERASING;
  1316. chip->erase_suspended = 0;
  1317. chip->in_progress_block_addr = adr;
  1318. INVALIDATE_CACHE_UDELAY(map, chip,
  1319. adr, map->size,
  1320. chip->erase_time*500);
  1321. timeo = jiffies + (HZ*20);
  1322. for (;;) {
  1323. if (chip->state != FL_ERASING) {
  1324. /* Someone's suspended the erase. Sleep */
  1325. set_current_state(TASK_UNINTERRUPTIBLE);
  1326. add_wait_queue(&chip->wq, &wait);
  1327. mutex_unlock(&chip->mutex);
  1328. schedule();
  1329. remove_wait_queue(&chip->wq, &wait);
  1330. mutex_lock(&chip->mutex);
  1331. continue;
  1332. }
  1333. if (chip->erase_suspended) {
  1334. /* This erase was suspended and resumed.
  1335. Adjust the timeout */
  1336. timeo = jiffies + (HZ*20); /* FIXME */
  1337. chip->erase_suspended = 0;
  1338. }
  1339. if (chip_ready(map, adr))
  1340. break;
  1341. if (time_after(jiffies, timeo)) {
  1342. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1343. __func__ );
  1344. break;
  1345. }
  1346. /* Latency issues. Drop the lock, wait a while and retry */
  1347. UDELAY(map, chip, adr, 1000000/HZ);
  1348. }
  1349. /* Did we succeed? */
  1350. if (!chip_good(map, adr, map_word_ff(map))) {
  1351. /* reset on all failures. */
  1352. map_write( map, CMD(0xF0), chip->start );
  1353. /* FIXME - should have reset delay before continuing */
  1354. ret = -EIO;
  1355. }
  1356. chip->state = FL_READY;
  1357. xip_enable(map, chip, adr);
  1358. put_chip(map, chip, adr);
  1359. mutex_unlock(&chip->mutex);
  1360. return ret;
  1361. }
  1362. static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
  1363. {
  1364. struct cfi_private *cfi = map->fldrv_priv;
  1365. unsigned long timeo = jiffies + HZ;
  1366. DECLARE_WAITQUEUE(wait, current);
  1367. int ret = 0;
  1368. adr += chip->start;
  1369. mutex_lock(&chip->mutex);
  1370. ret = get_chip(map, chip, adr, FL_ERASING);
  1371. if (ret) {
  1372. mutex_unlock(&chip->mutex);
  1373. return ret;
  1374. }
  1375. DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
  1376. __func__, adr );
  1377. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1378. ENABLE_VPP(map);
  1379. xip_disable(map, chip, adr);
  1380. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1381. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1382. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1383. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1384. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1385. map_write(map, CMD(0x30), adr);
  1386. chip->state = FL_ERASING;
  1387. chip->erase_suspended = 0;
  1388. chip->in_progress_block_addr = adr;
  1389. INVALIDATE_CACHE_UDELAY(map, chip,
  1390. adr, len,
  1391. chip->erase_time*500);
  1392. timeo = jiffies + (HZ*20);
  1393. for (;;) {
  1394. if (chip->state != FL_ERASING) {
  1395. /* Someone's suspended the erase. Sleep */
  1396. set_current_state(TASK_UNINTERRUPTIBLE);
  1397. add_wait_queue(&chip->wq, &wait);
  1398. mutex_unlock(&chip->mutex);
  1399. schedule();
  1400. remove_wait_queue(&chip->wq, &wait);
  1401. mutex_lock(&chip->mutex);
  1402. continue;
  1403. }
  1404. if (chip->erase_suspended) {
  1405. /* This erase was suspended and resumed.
  1406. Adjust the timeout */
  1407. timeo = jiffies + (HZ*20); /* FIXME */
  1408. chip->erase_suspended = 0;
  1409. }
  1410. if (chip_ready(map, adr)) {
  1411. xip_enable(map, chip, adr);
  1412. break;
  1413. }
  1414. if (time_after(jiffies, timeo)) {
  1415. xip_enable(map, chip, adr);
  1416. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1417. __func__ );
  1418. break;
  1419. }
  1420. /* Latency issues. Drop the lock, wait a while and retry */
  1421. UDELAY(map, chip, adr, 1000000/HZ);
  1422. }
  1423. /* Did we succeed? */
  1424. if (!chip_good(map, adr, map_word_ff(map))) {
  1425. /* reset on all failures. */
  1426. map_write( map, CMD(0xF0), chip->start );
  1427. /* FIXME - should have reset delay before continuing */
  1428. ret = -EIO;
  1429. }
  1430. chip->state = FL_READY;
  1431. put_chip(map, chip, adr);
  1432. mutex_unlock(&chip->mutex);
  1433. return ret;
  1434. }
  1435. static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
  1436. {
  1437. unsigned long ofs, len;
  1438. int ret;
  1439. ofs = instr->addr;
  1440. len = instr->len;
  1441. ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
  1442. if (ret)
  1443. return ret;
  1444. instr->state = MTD_ERASE_DONE;
  1445. mtd_erase_callback(instr);
  1446. return 0;
  1447. }
  1448. static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
  1449. {
  1450. struct map_info *map = mtd->priv;
  1451. struct cfi_private *cfi = map->fldrv_priv;
  1452. int ret = 0;
  1453. if (instr->addr != 0)
  1454. return -EINVAL;
  1455. if (instr->len != mtd->size)
  1456. return -EINVAL;
  1457. ret = do_erase_chip(map, &cfi->chips[0]);
  1458. if (ret)
  1459. return ret;
  1460. instr->state = MTD_ERASE_DONE;
  1461. mtd_erase_callback(instr);
  1462. return 0;
  1463. }
  1464. static int do_atmel_lock(struct map_info *map, struct flchip *chip,
  1465. unsigned long adr, int len, void *thunk)
  1466. {
  1467. struct cfi_private *cfi = map->fldrv_priv;
  1468. int ret;
  1469. mutex_lock(&chip->mutex);
  1470. ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
  1471. if (ret)
  1472. goto out_unlock;
  1473. chip->state = FL_LOCKING;
  1474. DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
  1475. __func__, adr, len);
  1476. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1477. cfi->device_type, NULL);
  1478. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1479. cfi->device_type, NULL);
  1480. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
  1481. cfi->device_type, NULL);
  1482. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1483. cfi->device_type, NULL);
  1484. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1485. cfi->device_type, NULL);
  1486. map_write(map, CMD(0x40), chip->start + adr);
  1487. chip->state = FL_READY;
  1488. put_chip(map, chip, adr + chip->start);
  1489. ret = 0;
  1490. out_unlock:
  1491. mutex_unlock(&chip->mutex);
  1492. return ret;
  1493. }
  1494. static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
  1495. unsigned long adr, int len, void *thunk)
  1496. {
  1497. struct cfi_private *cfi = map->fldrv_priv;
  1498. int ret;
  1499. mutex_lock(&chip->mutex);
  1500. ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
  1501. if (ret)
  1502. goto out_unlock;
  1503. chip->state = FL_UNLOCKING;
  1504. DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
  1505. __func__, adr, len);
  1506. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1507. cfi->device_type, NULL);
  1508. map_write(map, CMD(0x70), adr);
  1509. chip->state = FL_READY;
  1510. put_chip(map, chip, adr + chip->start);
  1511. ret = 0;
  1512. out_unlock:
  1513. mutex_unlock(&chip->mutex);
  1514. return ret;
  1515. }
  1516. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1517. {
  1518. return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
  1519. }
  1520. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1521. {
  1522. return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
  1523. }
  1524. static void cfi_amdstd_sync (struct mtd_info *mtd)
  1525. {
  1526. struct map_info *map = mtd->priv;
  1527. struct cfi_private *cfi = map->fldrv_priv;
  1528. int i;
  1529. struct flchip *chip;
  1530. int ret = 0;
  1531. DECLARE_WAITQUEUE(wait, current);
  1532. for (i=0; !ret && i<cfi->numchips; i++) {
  1533. chip = &cfi->chips[i];
  1534. retry:
  1535. mutex_lock(&chip->mutex);
  1536. switch(chip->state) {
  1537. case FL_READY:
  1538. case FL_STATUS:
  1539. case FL_CFI_QUERY:
  1540. case FL_JEDEC_QUERY:
  1541. chip->oldstate = chip->state;
  1542. chip->state = FL_SYNCING;
  1543. /* No need to wake_up() on this state change -
  1544. * as the whole point is that nobody can do anything
  1545. * with the chip now anyway.
  1546. */
  1547. case FL_SYNCING:
  1548. mutex_unlock(&chip->mutex);
  1549. break;
  1550. default:
  1551. /* Not an idle state */
  1552. set_current_state(TASK_UNINTERRUPTIBLE);
  1553. add_wait_queue(&chip->wq, &wait);
  1554. mutex_unlock(&chip->mutex);
  1555. schedule();
  1556. remove_wait_queue(&chip->wq, &wait);
  1557. goto retry;
  1558. }
  1559. }
  1560. /* Unlock the chips again */
  1561. for (i--; i >=0; i--) {
  1562. chip = &cfi->chips[i];
  1563. mutex_lock(&chip->mutex);
  1564. if (chip->state == FL_SYNCING) {
  1565. chip->state = chip->oldstate;
  1566. wake_up(&chip->wq);
  1567. }
  1568. mutex_unlock(&chip->mutex);
  1569. }
  1570. }
  1571. static int cfi_amdstd_suspend(struct mtd_info *mtd)
  1572. {
  1573. struct map_info *map = mtd->priv;
  1574. struct cfi_private *cfi = map->fldrv_priv;
  1575. int i;
  1576. struct flchip *chip;
  1577. int ret = 0;
  1578. for (i=0; !ret && i<cfi->numchips; i++) {
  1579. chip = &cfi->chips[i];
  1580. mutex_lock(&chip->mutex);
  1581. switch(chip->state) {
  1582. case FL_READY:
  1583. case FL_STATUS:
  1584. case FL_CFI_QUERY:
  1585. case FL_JEDEC_QUERY:
  1586. chip->oldstate = chip->state;
  1587. chip->state = FL_PM_SUSPENDED;
  1588. /* No need to wake_up() on this state change -
  1589. * as the whole point is that nobody can do anything
  1590. * with the chip now anyway.
  1591. */
  1592. case FL_PM_SUSPENDED:
  1593. break;
  1594. default:
  1595. ret = -EAGAIN;
  1596. break;
  1597. }
  1598. mutex_unlock(&chip->mutex);
  1599. }
  1600. /* Unlock the chips again */
  1601. if (ret) {
  1602. for (i--; i >=0; i--) {
  1603. chip = &cfi->chips[i];
  1604. mutex_lock(&chip->mutex);
  1605. if (chip->state == FL_PM_SUSPENDED) {
  1606. chip->state = chip->oldstate;
  1607. wake_up(&chip->wq);
  1608. }
  1609. mutex_unlock(&chip->mutex);
  1610. }
  1611. }
  1612. return ret;
  1613. }
  1614. static void cfi_amdstd_resume(struct mtd_info *mtd)
  1615. {
  1616. struct map_info *map = mtd->priv;
  1617. struct cfi_private *cfi = map->fldrv_priv;
  1618. int i;
  1619. struct flchip *chip;
  1620. for (i=0; i<cfi->numchips; i++) {
  1621. chip = &cfi->chips[i];
  1622. mutex_lock(&chip->mutex);
  1623. if (chip->state == FL_PM_SUSPENDED) {
  1624. chip->state = FL_READY;
  1625. map_write(map, CMD(0xF0), chip->start);
  1626. wake_up(&chip->wq);
  1627. }
  1628. else
  1629. printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
  1630. mutex_unlock(&chip->mutex);
  1631. }
  1632. }
  1633. /*
  1634. * Ensure that the flash device is put back into read array mode before
  1635. * unloading the driver or rebooting. On some systems, rebooting while
  1636. * the flash is in query/program/erase mode will prevent the CPU from
  1637. * fetching the bootloader code, requiring a hard reset or power cycle.
  1638. */
  1639. static int cfi_amdstd_reset(struct mtd_info *mtd)
  1640. {
  1641. struct map_info *map = mtd->priv;
  1642. struct cfi_private *cfi = map->fldrv_priv;
  1643. int i, ret;
  1644. struct flchip *chip;
  1645. for (i = 0; i < cfi->numchips; i++) {
  1646. chip = &cfi->chips[i];
  1647. mutex_lock(&chip->mutex);
  1648. ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
  1649. if (!ret) {
  1650. map_write(map, CMD(0xF0), chip->start);
  1651. chip->state = FL_SHUTDOWN;
  1652. put_chip(map, chip, chip->start);
  1653. }
  1654. mutex_unlock(&chip->mutex);
  1655. }
  1656. return 0;
  1657. }
  1658. static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
  1659. void *v)
  1660. {
  1661. struct mtd_info *mtd;
  1662. mtd = container_of(nb, struct mtd_info, reboot_notifier);
  1663. cfi_amdstd_reset(mtd);
  1664. return NOTIFY_DONE;
  1665. }
  1666. static void cfi_amdstd_destroy(struct mtd_info *mtd)
  1667. {
  1668. struct map_info *map = mtd->priv;
  1669. struct cfi_private *cfi = map->fldrv_priv;
  1670. cfi_amdstd_reset(mtd);
  1671. unregister_reboot_notifier(&mtd->reboot_notifier);
  1672. kfree(cfi->cmdset_priv);
  1673. kfree(cfi->cfiq);
  1674. kfree(cfi);
  1675. kfree(mtd->eraseregions);
  1676. }
  1677. MODULE_LICENSE("GPL");
  1678. MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
  1679. MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
  1680. MODULE_ALIAS("cfi_cmdset_0006");
  1681. MODULE_ALIAS("cfi_cmdset_0701");