cfi_cmdset_0002.c 54 KB

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