cfi_cmdset_0002.c 52 KB

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