cfi_cmdset_0002.c 55 KB

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