cfi_cmdset_0002.c 55 KB

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