cfi_cmdset_0002.c 55 KB

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