cfi_cmdset_0002.c 49 KB

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