cfi_cmdset_0002.c 61 KB

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