cfi_cmdset_0002.c 51 KB

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