cfi_cmdset_0002.c 52 KB

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