cfi_cmdset_0002.c 79 KB

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