cfi_cmdset_0002.c 55 KB

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