cfi_cmdset_0002.c 50 KB

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