cfi_cmdset_0002.c 50 KB

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