cfi_cmdset_0002.c 51 KB

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