cfi_cmdset_0002.c 70 KB

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