cfi_cmdset_0002.c 52 KB

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