cfi_cmdset_0002.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926
  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. kfree(cfi->cfiq);
  633. return NULL;
  634. }
  635. /*
  636. * Return true if the chip is ready.
  637. *
  638. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  639. * non-suspended sector) and is indicated by no toggle bits toggling.
  640. *
  641. * Note that anything more complicated than checking if no bits are toggling
  642. * (including checking DQ5 for an error status) is tricky to get working
  643. * correctly and is therefore not done (particularly with interleaved chips
  644. * as each chip must be checked independently of the others).
  645. */
  646. static int __xipram chip_ready(struct map_info *map, unsigned long addr)
  647. {
  648. map_word d, t;
  649. d = map_read(map, addr);
  650. t = map_read(map, addr);
  651. return map_word_equal(map, d, t);
  652. }
  653. /*
  654. * Return true if the chip is ready and has the correct value.
  655. *
  656. * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
  657. * non-suspended sector) and it is indicated by no bits toggling.
  658. *
  659. * Error are indicated by toggling bits or bits held with the wrong value,
  660. * or with bits toggling.
  661. *
  662. * Note that anything more complicated than checking if no bits are toggling
  663. * (including checking DQ5 for an error status) is tricky to get working
  664. * correctly and is therefore not done (particularly with interleaved chips
  665. * as each chip must be checked independently of the others).
  666. *
  667. */
  668. static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
  669. {
  670. map_word oldd, curd;
  671. oldd = map_read(map, addr);
  672. curd = map_read(map, addr);
  673. return map_word_equal(map, oldd, curd) &&
  674. map_word_equal(map, curd, expected);
  675. }
  676. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  677. {
  678. DECLARE_WAITQUEUE(wait, current);
  679. struct cfi_private *cfi = map->fldrv_priv;
  680. unsigned long timeo;
  681. struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
  682. resettime:
  683. timeo = jiffies + HZ;
  684. retry:
  685. switch (chip->state) {
  686. case FL_STATUS:
  687. for (;;) {
  688. if (chip_ready(map, adr))
  689. break;
  690. if (time_after(jiffies, timeo)) {
  691. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  692. return -EIO;
  693. }
  694. mutex_unlock(&chip->mutex);
  695. cfi_udelay(1);
  696. mutex_lock(&chip->mutex);
  697. /* Someone else might have been playing with it. */
  698. goto retry;
  699. }
  700. case FL_READY:
  701. case FL_CFI_QUERY:
  702. case FL_JEDEC_QUERY:
  703. return 0;
  704. case FL_ERASING:
  705. if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
  706. !(mode == FL_READY || mode == FL_POINT ||
  707. (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
  708. goto sleep;
  709. /* Do not allow suspend iff read/write to EB address */
  710. if ((adr & chip->in_progress_block_mask) ==
  711. chip->in_progress_block_addr)
  712. goto sleep;
  713. /* Erase suspend */
  714. /* It's harmless to issue the Erase-Suspend and Erase-Resume
  715. * commands when the erase algorithm isn't in progress. */
  716. map_write(map, CMD(0xB0), chip->in_progress_block_addr);
  717. chip->oldstate = FL_ERASING;
  718. chip->state = FL_ERASE_SUSPENDING;
  719. chip->erase_suspended = 1;
  720. for (;;) {
  721. if (chip_ready(map, adr))
  722. break;
  723. if (time_after(jiffies, timeo)) {
  724. /* Should have suspended the erase by now.
  725. * Send an Erase-Resume command as either
  726. * there was an error (so leave the erase
  727. * routine to recover from it) or we trying to
  728. * use the erase-in-progress sector. */
  729. put_chip(map, chip, adr);
  730. printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
  731. return -EIO;
  732. }
  733. mutex_unlock(&chip->mutex);
  734. cfi_udelay(1);
  735. mutex_lock(&chip->mutex);
  736. /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
  737. So we can just loop here. */
  738. }
  739. chip->state = FL_READY;
  740. return 0;
  741. case FL_XIP_WHILE_ERASING:
  742. if (mode != FL_READY && mode != FL_POINT &&
  743. (!cfip || !(cfip->EraseSuspend&2)))
  744. goto sleep;
  745. chip->oldstate = chip->state;
  746. chip->state = FL_READY;
  747. return 0;
  748. case FL_SHUTDOWN:
  749. /* The machine is rebooting */
  750. return -EIO;
  751. case FL_POINT:
  752. /* Only if there's no operation suspended... */
  753. if (mode == FL_READY && chip->oldstate == FL_READY)
  754. return 0;
  755. default:
  756. sleep:
  757. set_current_state(TASK_UNINTERRUPTIBLE);
  758. add_wait_queue(&chip->wq, &wait);
  759. mutex_unlock(&chip->mutex);
  760. schedule();
  761. remove_wait_queue(&chip->wq, &wait);
  762. mutex_lock(&chip->mutex);
  763. goto resettime;
  764. }
  765. }
  766. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
  767. {
  768. struct cfi_private *cfi = map->fldrv_priv;
  769. switch(chip->oldstate) {
  770. case FL_ERASING:
  771. cfi_fixup_m29ew_erase_suspend(map,
  772. chip->in_progress_block_addr);
  773. map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
  774. cfi_fixup_m29ew_delay_after_resume(cfi);
  775. chip->oldstate = FL_READY;
  776. chip->state = FL_ERASING;
  777. break;
  778. case FL_XIP_WHILE_ERASING:
  779. chip->state = chip->oldstate;
  780. chip->oldstate = FL_READY;
  781. break;
  782. case FL_READY:
  783. case FL_STATUS:
  784. break;
  785. default:
  786. printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
  787. }
  788. wake_up(&chip->wq);
  789. }
  790. #ifdef CONFIG_MTD_XIP
  791. /*
  792. * No interrupt what so ever can be serviced while the flash isn't in array
  793. * mode. This is ensured by the xip_disable() and xip_enable() functions
  794. * enclosing any code path where the flash is known not to be in array mode.
  795. * And within a XIP disabled code path, only functions marked with __xipram
  796. * may be called and nothing else (it's a good thing to inspect generated
  797. * assembly to make sure inline functions were actually inlined and that gcc
  798. * didn't emit calls to its own support functions). Also configuring MTD CFI
  799. * support to a single buswidth and a single interleave is also recommended.
  800. */
  801. static void xip_disable(struct map_info *map, struct flchip *chip,
  802. unsigned long adr)
  803. {
  804. /* TODO: chips with no XIP use should ignore and return */
  805. (void) map_read(map, adr); /* ensure mmu mapping is up to date */
  806. local_irq_disable();
  807. }
  808. static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
  809. unsigned long adr)
  810. {
  811. struct cfi_private *cfi = map->fldrv_priv;
  812. if (chip->state != FL_POINT && chip->state != FL_READY) {
  813. map_write(map, CMD(0xf0), adr);
  814. chip->state = FL_READY;
  815. }
  816. (void) map_read(map, adr);
  817. xip_iprefetch();
  818. local_irq_enable();
  819. }
  820. /*
  821. * When a delay is required for the flash operation to complete, the
  822. * xip_udelay() function is polling for both the given timeout and pending
  823. * (but still masked) hardware interrupts. Whenever there is an interrupt
  824. * pending then the flash erase operation is suspended, array mode restored
  825. * and interrupts unmasked. Task scheduling might also happen at that
  826. * point. The CPU eventually returns from the interrupt or the call to
  827. * schedule() and the suspended flash operation is resumed for the remaining
  828. * of the delay period.
  829. *
  830. * Warning: this function _will_ fool interrupt latency tracing tools.
  831. */
  832. static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
  833. unsigned long adr, int usec)
  834. {
  835. struct cfi_private *cfi = map->fldrv_priv;
  836. struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
  837. map_word status, OK = CMD(0x80);
  838. unsigned long suspended, start = xip_currtime();
  839. flstate_t oldstate;
  840. do {
  841. cpu_relax();
  842. if (xip_irqpending() && extp &&
  843. ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
  844. (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
  845. /*
  846. * Let's suspend the erase operation when supported.
  847. * Note that we currently don't try to suspend
  848. * interleaved chips if there is already another
  849. * operation suspended (imagine what happens
  850. * when one chip was already done with the current
  851. * operation while another chip suspended it, then
  852. * we resume the whole thing at once). Yes, it
  853. * can happen!
  854. */
  855. map_write(map, CMD(0xb0), adr);
  856. usec -= xip_elapsed_since(start);
  857. suspended = xip_currtime();
  858. do {
  859. if (xip_elapsed_since(suspended) > 100000) {
  860. /*
  861. * The chip doesn't want to suspend
  862. * after waiting for 100 msecs.
  863. * This is a critical error but there
  864. * is not much we can do here.
  865. */
  866. return;
  867. }
  868. status = map_read(map, adr);
  869. } while (!map_word_andequal(map, status, OK, OK));
  870. /* Suspend succeeded */
  871. oldstate = chip->state;
  872. if (!map_word_bitsset(map, status, CMD(0x40)))
  873. break;
  874. chip->state = FL_XIP_WHILE_ERASING;
  875. chip->erase_suspended = 1;
  876. map_write(map, CMD(0xf0), adr);
  877. (void) map_read(map, adr);
  878. xip_iprefetch();
  879. local_irq_enable();
  880. mutex_unlock(&chip->mutex);
  881. xip_iprefetch();
  882. cond_resched();
  883. /*
  884. * We're back. However someone else might have
  885. * decided to go write to the chip if we are in
  886. * a suspended erase state. If so let's wait
  887. * until it's done.
  888. */
  889. mutex_lock(&chip->mutex);
  890. while (chip->state != FL_XIP_WHILE_ERASING) {
  891. DECLARE_WAITQUEUE(wait, current);
  892. set_current_state(TASK_UNINTERRUPTIBLE);
  893. add_wait_queue(&chip->wq, &wait);
  894. mutex_unlock(&chip->mutex);
  895. schedule();
  896. remove_wait_queue(&chip->wq, &wait);
  897. mutex_lock(&chip->mutex);
  898. }
  899. /* Disallow XIP again */
  900. local_irq_disable();
  901. /* Correct Erase Suspend Hangups for M29EW */
  902. cfi_fixup_m29ew_erase_suspend(map, adr);
  903. /* Resume the write or erase operation */
  904. map_write(map, cfi->sector_erase_cmd, adr);
  905. chip->state = oldstate;
  906. start = xip_currtime();
  907. } else if (usec >= 1000000/HZ) {
  908. /*
  909. * Try to save on CPU power when waiting delay
  910. * is at least a system timer tick period.
  911. * No need to be extremely accurate here.
  912. */
  913. xip_cpu_idle();
  914. }
  915. status = map_read(map, adr);
  916. } while (!map_word_andequal(map, status, OK, OK)
  917. && xip_elapsed_since(start) < usec);
  918. }
  919. #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
  920. /*
  921. * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
  922. * the flash is actively programming or erasing since we have to poll for
  923. * the operation to complete anyway. We can't do that in a generic way with
  924. * a XIP setup so do it before the actual flash operation in this case
  925. * and stub it out from INVALIDATE_CACHE_UDELAY.
  926. */
  927. #define XIP_INVAL_CACHED_RANGE(map, from, size) \
  928. INVALIDATE_CACHED_RANGE(map, from, size)
  929. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  930. UDELAY(map, chip, adr, usec)
  931. /*
  932. * Extra notes:
  933. *
  934. * Activating this XIP support changes the way the code works a bit. For
  935. * example the code to suspend the current process when concurrent access
  936. * happens is never executed because xip_udelay() will always return with the
  937. * same chip state as it was entered with. This is why there is no care for
  938. * the presence of add_wait_queue() or schedule() calls from within a couple
  939. * xip_disable()'d areas of code, like in do_erase_oneblock for example.
  940. * The queueing and scheduling are always happening within xip_udelay().
  941. *
  942. * Similarly, get_chip() and put_chip() just happen to always be executed
  943. * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
  944. * is in array mode, therefore never executing many cases therein and not
  945. * causing any problem with XIP.
  946. */
  947. #else
  948. #define xip_disable(map, chip, adr)
  949. #define xip_enable(map, chip, adr)
  950. #define XIP_INVAL_CACHED_RANGE(x...)
  951. #define UDELAY(map, chip, adr, usec) \
  952. do { \
  953. mutex_unlock(&chip->mutex); \
  954. cfi_udelay(usec); \
  955. mutex_lock(&chip->mutex); \
  956. } while (0)
  957. #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
  958. do { \
  959. mutex_unlock(&chip->mutex); \
  960. INVALIDATE_CACHED_RANGE(map, adr, len); \
  961. cfi_udelay(usec); \
  962. mutex_lock(&chip->mutex); \
  963. } while (0)
  964. #endif
  965. static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  966. {
  967. unsigned long cmd_addr;
  968. struct cfi_private *cfi = map->fldrv_priv;
  969. int ret;
  970. adr += chip->start;
  971. /* Ensure cmd read/writes are aligned. */
  972. cmd_addr = adr & ~(map_bankwidth(map)-1);
  973. mutex_lock(&chip->mutex);
  974. ret = get_chip(map, chip, cmd_addr, FL_READY);
  975. if (ret) {
  976. mutex_unlock(&chip->mutex);
  977. return ret;
  978. }
  979. if (chip->state != FL_POINT && chip->state != FL_READY) {
  980. map_write(map, CMD(0xf0), cmd_addr);
  981. chip->state = FL_READY;
  982. }
  983. map_copy_from(map, buf, adr, len);
  984. put_chip(map, chip, cmd_addr);
  985. mutex_unlock(&chip->mutex);
  986. return 0;
  987. }
  988. static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  989. {
  990. struct map_info *map = mtd->priv;
  991. struct cfi_private *cfi = map->fldrv_priv;
  992. unsigned long ofs;
  993. int chipnum;
  994. int ret = 0;
  995. /* ofs: offset within the first chip that the first read should start */
  996. chipnum = (from >> cfi->chipshift);
  997. ofs = from - (chipnum << cfi->chipshift);
  998. while (len) {
  999. unsigned long thislen;
  1000. if (chipnum >= cfi->numchips)
  1001. break;
  1002. if ((len + ofs -1) >> cfi->chipshift)
  1003. thislen = (1<<cfi->chipshift) - ofs;
  1004. else
  1005. thislen = len;
  1006. ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  1007. if (ret)
  1008. break;
  1009. *retlen += thislen;
  1010. len -= thislen;
  1011. buf += thislen;
  1012. ofs = 0;
  1013. chipnum++;
  1014. }
  1015. return ret;
  1016. }
  1017. typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
  1018. loff_t adr, size_t len, u_char *buf, size_t grouplen);
  1019. static inline void otp_enter(struct map_info *map, struct flchip *chip,
  1020. loff_t adr, size_t len)
  1021. {
  1022. struct cfi_private *cfi = map->fldrv_priv;
  1023. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1024. cfi->device_type, NULL);
  1025. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1026. cfi->device_type, NULL);
  1027. cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi,
  1028. cfi->device_type, NULL);
  1029. INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
  1030. }
  1031. static inline void otp_exit(struct map_info *map, struct flchip *chip,
  1032. loff_t adr, size_t len)
  1033. {
  1034. struct cfi_private *cfi = map->fldrv_priv;
  1035. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1036. cfi->device_type, NULL);
  1037. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1038. cfi->device_type, NULL);
  1039. cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi,
  1040. cfi->device_type, NULL);
  1041. cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi,
  1042. cfi->device_type, NULL);
  1043. INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
  1044. }
  1045. static inline int do_read_secsi_onechip(struct map_info *map,
  1046. struct flchip *chip, loff_t adr,
  1047. size_t len, u_char *buf,
  1048. size_t grouplen)
  1049. {
  1050. DECLARE_WAITQUEUE(wait, current);
  1051. retry:
  1052. mutex_lock(&chip->mutex);
  1053. if (chip->state != FL_READY){
  1054. set_current_state(TASK_UNINTERRUPTIBLE);
  1055. add_wait_queue(&chip->wq, &wait);
  1056. mutex_unlock(&chip->mutex);
  1057. schedule();
  1058. remove_wait_queue(&chip->wq, &wait);
  1059. goto retry;
  1060. }
  1061. adr += chip->start;
  1062. chip->state = FL_READY;
  1063. otp_enter(map, chip, adr, len);
  1064. map_copy_from(map, buf, adr, len);
  1065. otp_exit(map, chip, adr, len);
  1066. wake_up(&chip->wq);
  1067. mutex_unlock(&chip->mutex);
  1068. return 0;
  1069. }
  1070. static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  1071. {
  1072. struct map_info *map = mtd->priv;
  1073. struct cfi_private *cfi = map->fldrv_priv;
  1074. unsigned long ofs;
  1075. int chipnum;
  1076. int ret = 0;
  1077. /* ofs: offset within the first chip that the first read should start */
  1078. /* 8 secsi bytes per chip */
  1079. chipnum=from>>3;
  1080. ofs=from & 7;
  1081. while (len) {
  1082. unsigned long thislen;
  1083. if (chipnum >= cfi->numchips)
  1084. break;
  1085. if ((len + ofs -1) >> 3)
  1086. thislen = (1<<3) - ofs;
  1087. else
  1088. thislen = len;
  1089. ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs,
  1090. thislen, buf, 0);
  1091. if (ret)
  1092. break;
  1093. *retlen += thislen;
  1094. len -= thislen;
  1095. buf += thislen;
  1096. ofs = 0;
  1097. chipnum++;
  1098. }
  1099. return ret;
  1100. }
  1101. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
  1102. unsigned long adr, map_word datum,
  1103. int mode);
  1104. static int do_otp_write(struct map_info *map, struct flchip *chip, loff_t adr,
  1105. size_t len, u_char *buf, size_t grouplen)
  1106. {
  1107. int ret;
  1108. while (len) {
  1109. unsigned long bus_ofs = adr & ~(map_bankwidth(map)-1);
  1110. int gap = adr - bus_ofs;
  1111. int n = min_t(int, len, map_bankwidth(map) - gap);
  1112. map_word datum = map_word_ff(map);
  1113. if (n != map_bankwidth(map)) {
  1114. /* partial write of a word, load old contents */
  1115. otp_enter(map, chip, bus_ofs, map_bankwidth(map));
  1116. datum = map_read(map, bus_ofs);
  1117. otp_exit(map, chip, bus_ofs, map_bankwidth(map));
  1118. }
  1119. datum = map_word_load_partial(map, datum, buf, gap, n);
  1120. ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
  1121. if (ret)
  1122. return ret;
  1123. adr += n;
  1124. buf += n;
  1125. len -= n;
  1126. }
  1127. return 0;
  1128. }
  1129. static int do_otp_lock(struct map_info *map, struct flchip *chip, loff_t adr,
  1130. size_t len, u_char *buf, size_t grouplen)
  1131. {
  1132. struct cfi_private *cfi = map->fldrv_priv;
  1133. uint8_t lockreg;
  1134. unsigned long timeo;
  1135. int ret;
  1136. /* make sure area matches group boundaries */
  1137. if ((adr != 0) || (len != grouplen))
  1138. return -EINVAL;
  1139. mutex_lock(&chip->mutex);
  1140. ret = get_chip(map, chip, chip->start, FL_LOCKING);
  1141. if (ret) {
  1142. mutex_unlock(&chip->mutex);
  1143. return ret;
  1144. }
  1145. chip->state = FL_LOCKING;
  1146. /* Enter lock register command */
  1147. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1148. cfi->device_type, NULL);
  1149. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1150. cfi->device_type, NULL);
  1151. cfi_send_gen_cmd(0x40, cfi->addr_unlock1, chip->start, map, cfi,
  1152. cfi->device_type, NULL);
  1153. /* read lock register */
  1154. lockreg = cfi_read_query(map, 0);
  1155. /* set bit 0 to protect extended memory block */
  1156. lockreg &= ~0x01;
  1157. /* set bit 0 to protect extended memory block */
  1158. /* write lock register */
  1159. map_write(map, CMD(0xA0), chip->start);
  1160. map_write(map, CMD(lockreg), chip->start);
  1161. /* wait for chip to become ready */
  1162. timeo = jiffies + msecs_to_jiffies(2);
  1163. for (;;) {
  1164. if (chip_ready(map, adr))
  1165. break;
  1166. if (time_after(jiffies, timeo)) {
  1167. pr_err("Waiting for chip to be ready timed out.\n");
  1168. ret = -EIO;
  1169. break;
  1170. }
  1171. UDELAY(map, chip, 0, 1);
  1172. }
  1173. /* exit protection commands */
  1174. map_write(map, CMD(0x90), chip->start);
  1175. map_write(map, CMD(0x00), chip->start);
  1176. chip->state = FL_READY;
  1177. put_chip(map, chip, chip->start);
  1178. mutex_unlock(&chip->mutex);
  1179. return ret;
  1180. }
  1181. static int cfi_amdstd_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1182. size_t *retlen, u_char *buf,
  1183. otp_op_t action, int user_regs)
  1184. {
  1185. struct map_info *map = mtd->priv;
  1186. struct cfi_private *cfi = map->fldrv_priv;
  1187. int ofs_factor = cfi->interleave * cfi->device_type;
  1188. unsigned long base;
  1189. int chipnum;
  1190. struct flchip *chip;
  1191. uint8_t otp, lockreg;
  1192. int ret;
  1193. size_t user_size, factory_size, otpsize;
  1194. loff_t user_offset, factory_offset, otpoffset;
  1195. int user_locked = 0, otplocked;
  1196. *retlen = 0;
  1197. for (chipnum = 0; chipnum < cfi->numchips; chipnum++) {
  1198. chip = &cfi->chips[chipnum];
  1199. factory_size = 0;
  1200. user_size = 0;
  1201. /* Micron M29EW family */
  1202. if (is_m29ew(cfi)) {
  1203. base = chip->start;
  1204. /* check whether secsi area is factory locked
  1205. or user lockable */
  1206. mutex_lock(&chip->mutex);
  1207. ret = get_chip(map, chip, base, FL_CFI_QUERY);
  1208. if (ret) {
  1209. mutex_unlock(&chip->mutex);
  1210. return ret;
  1211. }
  1212. cfi_qry_mode_on(base, map, cfi);
  1213. otp = cfi_read_query(map, base + 0x3 * ofs_factor);
  1214. cfi_qry_mode_off(base, map, cfi);
  1215. put_chip(map, chip, base);
  1216. mutex_unlock(&chip->mutex);
  1217. if (otp & 0x80) {
  1218. /* factory locked */
  1219. factory_offset = 0;
  1220. factory_size = 0x100;
  1221. } else {
  1222. /* customer lockable */
  1223. user_offset = 0;
  1224. user_size = 0x100;
  1225. mutex_lock(&chip->mutex);
  1226. ret = get_chip(map, chip, base, FL_LOCKING);
  1227. if (ret) {
  1228. mutex_unlock(&chip->mutex);
  1229. return ret;
  1230. }
  1231. /* Enter lock register command */
  1232. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1,
  1233. chip->start, map, cfi,
  1234. cfi->device_type, NULL);
  1235. cfi_send_gen_cmd(0x55, cfi->addr_unlock2,
  1236. chip->start, map, cfi,
  1237. cfi->device_type, NULL);
  1238. cfi_send_gen_cmd(0x40, cfi->addr_unlock1,
  1239. chip->start, map, cfi,
  1240. cfi->device_type, NULL);
  1241. /* read lock register */
  1242. lockreg = cfi_read_query(map, 0);
  1243. /* exit protection commands */
  1244. map_write(map, CMD(0x90), chip->start);
  1245. map_write(map, CMD(0x00), chip->start);
  1246. put_chip(map, chip, chip->start);
  1247. mutex_unlock(&chip->mutex);
  1248. user_locked = ((lockreg & 0x01) == 0x00);
  1249. }
  1250. }
  1251. otpsize = user_regs ? user_size : factory_size;
  1252. if (!otpsize)
  1253. continue;
  1254. otpoffset = user_regs ? user_offset : factory_offset;
  1255. otplocked = user_regs ? user_locked : 1;
  1256. if (!action) {
  1257. /* return otpinfo */
  1258. struct otp_info *otpinfo;
  1259. len -= sizeof(*otpinfo);
  1260. if (len <= 0)
  1261. return -ENOSPC;
  1262. otpinfo = (struct otp_info *)buf;
  1263. otpinfo->start = from;
  1264. otpinfo->length = otpsize;
  1265. otpinfo->locked = otplocked;
  1266. buf += sizeof(*otpinfo);
  1267. *retlen += sizeof(*otpinfo);
  1268. from += otpsize;
  1269. } else if ((from < otpsize) && (len > 0)) {
  1270. size_t size;
  1271. size = (len < otpsize - from) ? len : otpsize - from;
  1272. ret = action(map, chip, otpoffset + from, size, buf,
  1273. otpsize);
  1274. if (ret < 0)
  1275. return ret;
  1276. buf += size;
  1277. len -= size;
  1278. *retlen += size;
  1279. from = 0;
  1280. } else {
  1281. from -= otpsize;
  1282. }
  1283. }
  1284. return 0;
  1285. }
  1286. static int cfi_amdstd_get_fact_prot_info(struct mtd_info *mtd, size_t len,
  1287. size_t *retlen, struct otp_info *buf)
  1288. {
  1289. return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
  1290. NULL, 0);
  1291. }
  1292. static int cfi_amdstd_get_user_prot_info(struct mtd_info *mtd, size_t len,
  1293. size_t *retlen, struct otp_info *buf)
  1294. {
  1295. return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
  1296. NULL, 1);
  1297. }
  1298. static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1299. size_t len, size_t *retlen,
  1300. u_char *buf)
  1301. {
  1302. return cfi_amdstd_otp_walk(mtd, from, len, retlen,
  1303. buf, do_read_secsi_onechip, 0);
  1304. }
  1305. static int cfi_amdstd_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1306. size_t len, size_t *retlen,
  1307. u_char *buf)
  1308. {
  1309. return cfi_amdstd_otp_walk(mtd, from, len, retlen,
  1310. buf, do_read_secsi_onechip, 1);
  1311. }
  1312. static int cfi_amdstd_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1313. size_t len, size_t *retlen,
  1314. u_char *buf)
  1315. {
  1316. return cfi_amdstd_otp_walk(mtd, from, len, retlen, buf,
  1317. do_otp_write, 1);
  1318. }
  1319. static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1320. size_t len)
  1321. {
  1322. size_t retlen;
  1323. return cfi_amdstd_otp_walk(mtd, from, len, &retlen, NULL,
  1324. do_otp_lock, 1);
  1325. }
  1326. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
  1327. unsigned long adr, map_word datum,
  1328. int mode)
  1329. {
  1330. struct cfi_private *cfi = map->fldrv_priv;
  1331. unsigned long timeo = jiffies + HZ;
  1332. /*
  1333. * We use a 1ms + 1 jiffies generic timeout for writes (most devices
  1334. * have a max write time of a few hundreds usec). However, we should
  1335. * use the maximum timeout value given by the chip at probe time
  1336. * instead. Unfortunately, struct flchip does have a field for
  1337. * maximum timeout, only for typical which can be far too short
  1338. * depending of the conditions. The ' + 1' is to avoid having a
  1339. * timeout of 0 jiffies if HZ is smaller than 1000.
  1340. */
  1341. unsigned long uWriteTimeout = (HZ / 1000) + 1;
  1342. int ret = 0;
  1343. map_word oldd;
  1344. int retry_cnt = 0;
  1345. adr += chip->start;
  1346. mutex_lock(&chip->mutex);
  1347. ret = get_chip(map, chip, adr, mode);
  1348. if (ret) {
  1349. mutex_unlock(&chip->mutex);
  1350. return ret;
  1351. }
  1352. pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1353. __func__, adr, datum.x[0]);
  1354. if (mode == FL_OTP_WRITE)
  1355. otp_enter(map, chip, adr, map_bankwidth(map));
  1356. /*
  1357. * Check for a NOP for the case when the datum to write is already
  1358. * present - it saves time and works around buggy chips that corrupt
  1359. * data at other locations when 0xff is written to a location that
  1360. * already contains 0xff.
  1361. */
  1362. oldd = map_read(map, adr);
  1363. if (map_word_equal(map, oldd, datum)) {
  1364. pr_debug("MTD %s(): NOP\n",
  1365. __func__);
  1366. goto op_done;
  1367. }
  1368. XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
  1369. ENABLE_VPP(map);
  1370. xip_disable(map, chip, adr);
  1371. retry:
  1372. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1373. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1374. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1375. map_write(map, datum, adr);
  1376. chip->state = mode;
  1377. INVALIDATE_CACHE_UDELAY(map, chip,
  1378. adr, map_bankwidth(map),
  1379. chip->word_write_time);
  1380. /* See comment above for timeout value. */
  1381. timeo = jiffies + uWriteTimeout;
  1382. for (;;) {
  1383. if (chip->state != mode) {
  1384. /* Someone's suspended the write. Sleep */
  1385. DECLARE_WAITQUEUE(wait, current);
  1386. set_current_state(TASK_UNINTERRUPTIBLE);
  1387. add_wait_queue(&chip->wq, &wait);
  1388. mutex_unlock(&chip->mutex);
  1389. schedule();
  1390. remove_wait_queue(&chip->wq, &wait);
  1391. timeo = jiffies + (HZ / 2); /* FIXME */
  1392. mutex_lock(&chip->mutex);
  1393. continue;
  1394. }
  1395. if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
  1396. xip_enable(map, chip, adr);
  1397. printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
  1398. xip_disable(map, chip, adr);
  1399. break;
  1400. }
  1401. if (chip_ready(map, adr))
  1402. break;
  1403. /* Latency issues. Drop the lock, wait a while and retry */
  1404. UDELAY(map, chip, adr, 1);
  1405. }
  1406. /* Did we succeed? */
  1407. if (!chip_good(map, adr, datum)) {
  1408. /* reset on all failures. */
  1409. map_write(map, CMD(0xF0), chip->start);
  1410. /* FIXME - should have reset delay before continuing */
  1411. if (++retry_cnt <= MAX_RETRIES)
  1412. goto retry;
  1413. ret = -EIO;
  1414. }
  1415. xip_enable(map, chip, adr);
  1416. op_done:
  1417. if (mode == FL_OTP_WRITE)
  1418. otp_exit(map, chip, adr, map_bankwidth(map));
  1419. chip->state = FL_READY;
  1420. DISABLE_VPP(map);
  1421. put_chip(map, chip, adr);
  1422. mutex_unlock(&chip->mutex);
  1423. return ret;
  1424. }
  1425. static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
  1426. size_t *retlen, const u_char *buf)
  1427. {
  1428. struct map_info *map = mtd->priv;
  1429. struct cfi_private *cfi = map->fldrv_priv;
  1430. int ret = 0;
  1431. int chipnum;
  1432. unsigned long ofs, chipstart;
  1433. DECLARE_WAITQUEUE(wait, current);
  1434. chipnum = to >> cfi->chipshift;
  1435. ofs = to - (chipnum << cfi->chipshift);
  1436. chipstart = cfi->chips[chipnum].start;
  1437. /* If it's not bus-aligned, do the first byte write */
  1438. if (ofs & (map_bankwidth(map)-1)) {
  1439. unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
  1440. int i = ofs - bus_ofs;
  1441. int n = 0;
  1442. map_word tmp_buf;
  1443. retry:
  1444. mutex_lock(&cfi->chips[chipnum].mutex);
  1445. if (cfi->chips[chipnum].state != FL_READY) {
  1446. set_current_state(TASK_UNINTERRUPTIBLE);
  1447. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1448. mutex_unlock(&cfi->chips[chipnum].mutex);
  1449. schedule();
  1450. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1451. goto retry;
  1452. }
  1453. /* Load 'tmp_buf' with old contents of flash */
  1454. tmp_buf = map_read(map, bus_ofs+chipstart);
  1455. mutex_unlock(&cfi->chips[chipnum].mutex);
  1456. /* Number of bytes to copy from buffer */
  1457. n = min_t(int, len, map_bankwidth(map)-i);
  1458. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1459. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1460. bus_ofs, tmp_buf, FL_WRITING);
  1461. if (ret)
  1462. return ret;
  1463. ofs += n;
  1464. buf += n;
  1465. (*retlen) += n;
  1466. len -= n;
  1467. if (ofs >> cfi->chipshift) {
  1468. chipnum ++;
  1469. ofs = 0;
  1470. if (chipnum == cfi->numchips)
  1471. return 0;
  1472. }
  1473. }
  1474. /* We are now aligned, write as much as possible */
  1475. while(len >= map_bankwidth(map)) {
  1476. map_word datum;
  1477. datum = map_word_load(map, buf);
  1478. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1479. ofs, datum, FL_WRITING);
  1480. if (ret)
  1481. return ret;
  1482. ofs += map_bankwidth(map);
  1483. buf += map_bankwidth(map);
  1484. (*retlen) += map_bankwidth(map);
  1485. len -= map_bankwidth(map);
  1486. if (ofs >> cfi->chipshift) {
  1487. chipnum ++;
  1488. ofs = 0;
  1489. if (chipnum == cfi->numchips)
  1490. return 0;
  1491. chipstart = cfi->chips[chipnum].start;
  1492. }
  1493. }
  1494. /* Write the trailing bytes if any */
  1495. if (len & (map_bankwidth(map)-1)) {
  1496. map_word tmp_buf;
  1497. retry1:
  1498. mutex_lock(&cfi->chips[chipnum].mutex);
  1499. if (cfi->chips[chipnum].state != FL_READY) {
  1500. set_current_state(TASK_UNINTERRUPTIBLE);
  1501. add_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1502. mutex_unlock(&cfi->chips[chipnum].mutex);
  1503. schedule();
  1504. remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
  1505. goto retry1;
  1506. }
  1507. tmp_buf = map_read(map, ofs + chipstart);
  1508. mutex_unlock(&cfi->chips[chipnum].mutex);
  1509. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1510. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1511. ofs, tmp_buf, FL_WRITING);
  1512. if (ret)
  1513. return ret;
  1514. (*retlen) += len;
  1515. }
  1516. return 0;
  1517. }
  1518. /*
  1519. * FIXME: interleaved mode not tested, and probably not supported!
  1520. */
  1521. static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
  1522. unsigned long adr, const u_char *buf,
  1523. int len)
  1524. {
  1525. struct cfi_private *cfi = map->fldrv_priv;
  1526. unsigned long timeo = jiffies + HZ;
  1527. /*
  1528. * Timeout is calculated according to CFI data, if available.
  1529. * See more comments in cfi_cmdset_0002().
  1530. */
  1531. unsigned long uWriteTimeout =
  1532. usecs_to_jiffies(chip->buffer_write_time_max);
  1533. int ret = -EIO;
  1534. unsigned long cmd_adr;
  1535. int z, words;
  1536. map_word datum;
  1537. adr += chip->start;
  1538. cmd_adr = adr;
  1539. mutex_lock(&chip->mutex);
  1540. ret = get_chip(map, chip, adr, FL_WRITING);
  1541. if (ret) {
  1542. mutex_unlock(&chip->mutex);
  1543. return ret;
  1544. }
  1545. datum = map_word_load(map, buf);
  1546. pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
  1547. __func__, adr, datum.x[0]);
  1548. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1549. ENABLE_VPP(map);
  1550. xip_disable(map, chip, cmd_adr);
  1551. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1552. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1553. /* Write Buffer Load */
  1554. map_write(map, CMD(0x25), cmd_adr);
  1555. chip->state = FL_WRITING_TO_BUFFER;
  1556. /* Write length of data to come */
  1557. words = len / map_bankwidth(map);
  1558. map_write(map, CMD(words - 1), cmd_adr);
  1559. /* Write data */
  1560. z = 0;
  1561. while(z < words * map_bankwidth(map)) {
  1562. datum = map_word_load(map, buf);
  1563. map_write(map, datum, adr + z);
  1564. z += map_bankwidth(map);
  1565. buf += map_bankwidth(map);
  1566. }
  1567. z -= map_bankwidth(map);
  1568. adr += z;
  1569. /* Write Buffer Program Confirm: GO GO GO */
  1570. map_write(map, CMD(0x29), cmd_adr);
  1571. chip->state = FL_WRITING;
  1572. INVALIDATE_CACHE_UDELAY(map, chip,
  1573. adr, map_bankwidth(map),
  1574. chip->word_write_time);
  1575. timeo = jiffies + uWriteTimeout;
  1576. for (;;) {
  1577. if (chip->state != FL_WRITING) {
  1578. /* Someone's suspended the write. Sleep */
  1579. DECLARE_WAITQUEUE(wait, current);
  1580. set_current_state(TASK_UNINTERRUPTIBLE);
  1581. add_wait_queue(&chip->wq, &wait);
  1582. mutex_unlock(&chip->mutex);
  1583. schedule();
  1584. remove_wait_queue(&chip->wq, &wait);
  1585. timeo = jiffies + (HZ / 2); /* FIXME */
  1586. mutex_lock(&chip->mutex);
  1587. continue;
  1588. }
  1589. if (time_after(jiffies, timeo) && !chip_ready(map, adr))
  1590. break;
  1591. if (chip_good(map, adr, datum)) {
  1592. xip_enable(map, chip, adr);
  1593. goto op_done;
  1594. }
  1595. /* Latency issues. Drop the lock, wait a while and retry */
  1596. UDELAY(map, chip, adr, 1);
  1597. }
  1598. /*
  1599. * Recovery from write-buffer programming failures requires
  1600. * the write-to-buffer-reset sequence. Since the last part
  1601. * of the sequence also works as a normal reset, we can run
  1602. * the same commands regardless of why we are here.
  1603. * See e.g.
  1604. * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf
  1605. */
  1606. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  1607. cfi->device_type, NULL);
  1608. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  1609. cfi->device_type, NULL);
  1610. cfi_send_gen_cmd(0xF0, cfi->addr_unlock1, chip->start, map, cfi,
  1611. cfi->device_type, NULL);
  1612. xip_enable(map, chip, adr);
  1613. /* FIXME - should have reset delay before continuing */
  1614. printk(KERN_WARNING "MTD %s(): software timeout, address:0x%.8lx.\n",
  1615. __func__, adr);
  1616. ret = -EIO;
  1617. op_done:
  1618. chip->state = FL_READY;
  1619. DISABLE_VPP(map);
  1620. put_chip(map, chip, adr);
  1621. mutex_unlock(&chip->mutex);
  1622. return ret;
  1623. }
  1624. static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
  1625. size_t *retlen, const u_char *buf)
  1626. {
  1627. struct map_info *map = mtd->priv;
  1628. struct cfi_private *cfi = map->fldrv_priv;
  1629. int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1630. int ret = 0;
  1631. int chipnum;
  1632. unsigned long ofs;
  1633. chipnum = to >> cfi->chipshift;
  1634. ofs = to - (chipnum << cfi->chipshift);
  1635. /* If it's not bus-aligned, do the first word write */
  1636. if (ofs & (map_bankwidth(map)-1)) {
  1637. size_t local_len = (-ofs)&(map_bankwidth(map)-1);
  1638. if (local_len > len)
  1639. local_len = len;
  1640. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1641. local_len, retlen, buf);
  1642. if (ret)
  1643. return ret;
  1644. ofs += local_len;
  1645. buf += local_len;
  1646. len -= local_len;
  1647. if (ofs >> cfi->chipshift) {
  1648. chipnum ++;
  1649. ofs = 0;
  1650. if (chipnum == cfi->numchips)
  1651. return 0;
  1652. }
  1653. }
  1654. /* Write buffer is worth it only if more than one word to write... */
  1655. while (len >= map_bankwidth(map) * 2) {
  1656. /* We must not cross write block boundaries */
  1657. int size = wbufsize - (ofs & (wbufsize-1));
  1658. if (size > len)
  1659. size = len;
  1660. if (size % map_bankwidth(map))
  1661. size -= size % map_bankwidth(map);
  1662. ret = do_write_buffer(map, &cfi->chips[chipnum],
  1663. ofs, buf, size);
  1664. if (ret)
  1665. return ret;
  1666. ofs += size;
  1667. buf += size;
  1668. (*retlen) += size;
  1669. len -= size;
  1670. if (ofs >> cfi->chipshift) {
  1671. chipnum ++;
  1672. ofs = 0;
  1673. if (chipnum == cfi->numchips)
  1674. return 0;
  1675. }
  1676. }
  1677. if (len) {
  1678. size_t retlen_dregs = 0;
  1679. ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
  1680. len, &retlen_dregs, buf);
  1681. *retlen += retlen_dregs;
  1682. return ret;
  1683. }
  1684. return 0;
  1685. }
  1686. /*
  1687. * Wait for the flash chip to become ready to write data
  1688. *
  1689. * This is only called during the panic_write() path. When panic_write()
  1690. * is called, the kernel is in the process of a panic, and will soon be
  1691. * dead. Therefore we don't take any locks, and attempt to get access
  1692. * to the chip as soon as possible.
  1693. */
  1694. static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
  1695. unsigned long adr)
  1696. {
  1697. struct cfi_private *cfi = map->fldrv_priv;
  1698. int retries = 10;
  1699. int i;
  1700. /*
  1701. * If the driver thinks the chip is idle, and no toggle bits
  1702. * are changing, then the chip is actually idle for sure.
  1703. */
  1704. if (chip->state == FL_READY && chip_ready(map, adr))
  1705. return 0;
  1706. /*
  1707. * Try several times to reset the chip and then wait for it
  1708. * to become idle. The upper limit of a few milliseconds of
  1709. * delay isn't a big problem: the kernel is dying anyway. It
  1710. * is more important to save the messages.
  1711. */
  1712. while (retries > 0) {
  1713. const unsigned long timeo = (HZ / 1000) + 1;
  1714. /* send the reset command */
  1715. map_write(map, CMD(0xF0), chip->start);
  1716. /* wait for the chip to become ready */
  1717. for (i = 0; i < jiffies_to_usecs(timeo); i++) {
  1718. if (chip_ready(map, adr))
  1719. return 0;
  1720. udelay(1);
  1721. }
  1722. retries--;
  1723. }
  1724. /* the chip never became ready */
  1725. return -EBUSY;
  1726. }
  1727. /*
  1728. * Write out one word of data to a single flash chip during a kernel panic
  1729. *
  1730. * This is only called during the panic_write() path. When panic_write()
  1731. * is called, the kernel is in the process of a panic, and will soon be
  1732. * dead. Therefore we don't take any locks, and attempt to get access
  1733. * to the chip as soon as possible.
  1734. *
  1735. * The implementation of this routine is intentionally similar to
  1736. * do_write_oneword(), in order to ease code maintenance.
  1737. */
  1738. static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
  1739. unsigned long adr, map_word datum)
  1740. {
  1741. const unsigned long uWriteTimeout = (HZ / 1000) + 1;
  1742. struct cfi_private *cfi = map->fldrv_priv;
  1743. int retry_cnt = 0;
  1744. map_word oldd;
  1745. int ret = 0;
  1746. int i;
  1747. adr += chip->start;
  1748. ret = cfi_amdstd_panic_wait(map, chip, adr);
  1749. if (ret)
  1750. return ret;
  1751. pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
  1752. __func__, adr, datum.x[0]);
  1753. /*
  1754. * Check for a NOP for the case when the datum to write is already
  1755. * present - it saves time and works around buggy chips that corrupt
  1756. * data at other locations when 0xff is written to a location that
  1757. * already contains 0xff.
  1758. */
  1759. oldd = map_read(map, adr);
  1760. if (map_word_equal(map, oldd, datum)) {
  1761. pr_debug("MTD %s(): NOP\n", __func__);
  1762. goto op_done;
  1763. }
  1764. ENABLE_VPP(map);
  1765. retry:
  1766. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1767. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1768. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1769. map_write(map, datum, adr);
  1770. for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
  1771. if (chip_ready(map, adr))
  1772. break;
  1773. udelay(1);
  1774. }
  1775. if (!chip_good(map, adr, datum)) {
  1776. /* reset on all failures. */
  1777. map_write(map, CMD(0xF0), chip->start);
  1778. /* FIXME - should have reset delay before continuing */
  1779. if (++retry_cnt <= MAX_RETRIES)
  1780. goto retry;
  1781. ret = -EIO;
  1782. }
  1783. op_done:
  1784. DISABLE_VPP(map);
  1785. return ret;
  1786. }
  1787. /*
  1788. * Write out some data during a kernel panic
  1789. *
  1790. * This is used by the mtdoops driver to save the dying messages from a
  1791. * kernel which has panic'd.
  1792. *
  1793. * This routine ignores all of the locking used throughout the rest of the
  1794. * driver, in order to ensure that the data gets written out no matter what
  1795. * state this driver (and the flash chip itself) was in when the kernel crashed.
  1796. *
  1797. * The implementation of this routine is intentionally similar to
  1798. * cfi_amdstd_write_words(), in order to ease code maintenance.
  1799. */
  1800. static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  1801. size_t *retlen, const u_char *buf)
  1802. {
  1803. struct map_info *map = mtd->priv;
  1804. struct cfi_private *cfi = map->fldrv_priv;
  1805. unsigned long ofs, chipstart;
  1806. int ret = 0;
  1807. int chipnum;
  1808. chipnum = to >> cfi->chipshift;
  1809. ofs = to - (chipnum << cfi->chipshift);
  1810. chipstart = cfi->chips[chipnum].start;
  1811. /* If it's not bus aligned, do the first byte write */
  1812. if (ofs & (map_bankwidth(map) - 1)) {
  1813. unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
  1814. int i = ofs - bus_ofs;
  1815. int n = 0;
  1816. map_word tmp_buf;
  1817. ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
  1818. if (ret)
  1819. return ret;
  1820. /* Load 'tmp_buf' with old contents of flash */
  1821. tmp_buf = map_read(map, bus_ofs + chipstart);
  1822. /* Number of bytes to copy from buffer */
  1823. n = min_t(int, len, map_bankwidth(map) - i);
  1824. tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
  1825. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1826. bus_ofs, tmp_buf);
  1827. if (ret)
  1828. return ret;
  1829. ofs += n;
  1830. buf += n;
  1831. (*retlen) += n;
  1832. len -= n;
  1833. if (ofs >> cfi->chipshift) {
  1834. chipnum++;
  1835. ofs = 0;
  1836. if (chipnum == cfi->numchips)
  1837. return 0;
  1838. }
  1839. }
  1840. /* We are now aligned, write as much as possible */
  1841. while (len >= map_bankwidth(map)) {
  1842. map_word datum;
  1843. datum = map_word_load(map, buf);
  1844. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1845. ofs, datum);
  1846. if (ret)
  1847. return ret;
  1848. ofs += map_bankwidth(map);
  1849. buf += map_bankwidth(map);
  1850. (*retlen) += map_bankwidth(map);
  1851. len -= map_bankwidth(map);
  1852. if (ofs >> cfi->chipshift) {
  1853. chipnum++;
  1854. ofs = 0;
  1855. if (chipnum == cfi->numchips)
  1856. return 0;
  1857. chipstart = cfi->chips[chipnum].start;
  1858. }
  1859. }
  1860. /* Write the trailing bytes if any */
  1861. if (len & (map_bankwidth(map) - 1)) {
  1862. map_word tmp_buf;
  1863. ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
  1864. if (ret)
  1865. return ret;
  1866. tmp_buf = map_read(map, ofs + chipstart);
  1867. tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
  1868. ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
  1869. ofs, tmp_buf);
  1870. if (ret)
  1871. return ret;
  1872. (*retlen) += len;
  1873. }
  1874. return 0;
  1875. }
  1876. /*
  1877. * Handle devices with one erase region, that only implement
  1878. * the chip erase command.
  1879. */
  1880. static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
  1881. {
  1882. struct cfi_private *cfi = map->fldrv_priv;
  1883. unsigned long timeo = jiffies + HZ;
  1884. unsigned long int adr;
  1885. DECLARE_WAITQUEUE(wait, current);
  1886. int ret = 0;
  1887. int retry_cnt = 0;
  1888. adr = cfi->addr_unlock1;
  1889. mutex_lock(&chip->mutex);
  1890. ret = get_chip(map, chip, adr, FL_WRITING);
  1891. if (ret) {
  1892. mutex_unlock(&chip->mutex);
  1893. return ret;
  1894. }
  1895. pr_debug("MTD %s(): ERASE 0x%.8lx\n",
  1896. __func__, chip->start);
  1897. XIP_INVAL_CACHED_RANGE(map, adr, map->size);
  1898. ENABLE_VPP(map);
  1899. xip_disable(map, chip, adr);
  1900. retry:
  1901. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1902. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1903. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1904. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1905. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1906. cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1907. chip->state = FL_ERASING;
  1908. chip->erase_suspended = 0;
  1909. chip->in_progress_block_addr = adr;
  1910. chip->in_progress_block_mask = ~(map->size - 1);
  1911. INVALIDATE_CACHE_UDELAY(map, chip,
  1912. adr, map->size,
  1913. chip->erase_time*500);
  1914. timeo = jiffies + (HZ*20);
  1915. for (;;) {
  1916. if (chip->state != FL_ERASING) {
  1917. /* Someone's suspended the erase. Sleep */
  1918. set_current_state(TASK_UNINTERRUPTIBLE);
  1919. add_wait_queue(&chip->wq, &wait);
  1920. mutex_unlock(&chip->mutex);
  1921. schedule();
  1922. remove_wait_queue(&chip->wq, &wait);
  1923. mutex_lock(&chip->mutex);
  1924. continue;
  1925. }
  1926. if (chip->erase_suspended) {
  1927. /* This erase was suspended and resumed.
  1928. Adjust the timeout */
  1929. timeo = jiffies + (HZ*20); /* FIXME */
  1930. chip->erase_suspended = 0;
  1931. }
  1932. if (chip_good(map, adr, map_word_ff(map)))
  1933. break;
  1934. if (time_after(jiffies, timeo)) {
  1935. printk(KERN_WARNING "MTD %s(): software timeout\n",
  1936. __func__);
  1937. ret = -EIO;
  1938. break;
  1939. }
  1940. /* Latency issues. Drop the lock, wait a while and retry */
  1941. UDELAY(map, chip, adr, 1000000/HZ);
  1942. }
  1943. /* Did we succeed? */
  1944. if (ret) {
  1945. /* reset on all failures. */
  1946. map_write(map, CMD(0xF0), chip->start);
  1947. /* FIXME - should have reset delay before continuing */
  1948. if (++retry_cnt <= MAX_RETRIES) {
  1949. ret = 0;
  1950. goto retry;
  1951. }
  1952. }
  1953. chip->state = FL_READY;
  1954. xip_enable(map, chip, adr);
  1955. DISABLE_VPP(map);
  1956. put_chip(map, chip, adr);
  1957. mutex_unlock(&chip->mutex);
  1958. return ret;
  1959. }
  1960. static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
  1961. {
  1962. struct cfi_private *cfi = map->fldrv_priv;
  1963. unsigned long timeo = jiffies + HZ;
  1964. DECLARE_WAITQUEUE(wait, current);
  1965. int ret = 0;
  1966. int retry_cnt = 0;
  1967. adr += chip->start;
  1968. mutex_lock(&chip->mutex);
  1969. ret = get_chip(map, chip, adr, FL_ERASING);
  1970. if (ret) {
  1971. mutex_unlock(&chip->mutex);
  1972. return ret;
  1973. }
  1974. pr_debug("MTD %s(): ERASE 0x%.8lx\n",
  1975. __func__, adr);
  1976. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1977. ENABLE_VPP(map);
  1978. xip_disable(map, chip, adr);
  1979. retry:
  1980. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1981. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1982. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1983. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  1984. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  1985. map_write(map, cfi->sector_erase_cmd, adr);
  1986. chip->state = FL_ERASING;
  1987. chip->erase_suspended = 0;
  1988. chip->in_progress_block_addr = adr;
  1989. chip->in_progress_block_mask = ~(len - 1);
  1990. INVALIDATE_CACHE_UDELAY(map, chip,
  1991. adr, len,
  1992. chip->erase_time*500);
  1993. timeo = jiffies + (HZ*20);
  1994. for (;;) {
  1995. if (chip->state != FL_ERASING) {
  1996. /* Someone's suspended the erase. Sleep */
  1997. set_current_state(TASK_UNINTERRUPTIBLE);
  1998. add_wait_queue(&chip->wq, &wait);
  1999. mutex_unlock(&chip->mutex);
  2000. schedule();
  2001. remove_wait_queue(&chip->wq, &wait);
  2002. mutex_lock(&chip->mutex);
  2003. continue;
  2004. }
  2005. if (chip->erase_suspended) {
  2006. /* This erase was suspended and resumed.
  2007. Adjust the timeout */
  2008. timeo = jiffies + (HZ*20); /* FIXME */
  2009. chip->erase_suspended = 0;
  2010. }
  2011. if (chip_good(map, adr, map_word_ff(map)))
  2012. break;
  2013. if (time_after(jiffies, timeo)) {
  2014. printk(KERN_WARNING "MTD %s(): software timeout\n",
  2015. __func__);
  2016. ret = -EIO;
  2017. break;
  2018. }
  2019. /* Latency issues. Drop the lock, wait a while and retry */
  2020. UDELAY(map, chip, adr, 1000000/HZ);
  2021. }
  2022. /* Did we succeed? */
  2023. if (ret) {
  2024. /* reset on all failures. */
  2025. map_write(map, CMD(0xF0), chip->start);
  2026. /* FIXME - should have reset delay before continuing */
  2027. if (++retry_cnt <= MAX_RETRIES) {
  2028. ret = 0;
  2029. goto retry;
  2030. }
  2031. }
  2032. chip->state = FL_READY;
  2033. xip_enable(map, chip, adr);
  2034. DISABLE_VPP(map);
  2035. put_chip(map, chip, adr);
  2036. mutex_unlock(&chip->mutex);
  2037. return ret;
  2038. }
  2039. static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
  2040. {
  2041. return cfi_varsize_frob(mtd, do_erase_oneblock, instr->addr,
  2042. instr->len, NULL);
  2043. }
  2044. static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
  2045. {
  2046. struct map_info *map = mtd->priv;
  2047. struct cfi_private *cfi = map->fldrv_priv;
  2048. if (instr->addr != 0)
  2049. return -EINVAL;
  2050. if (instr->len != mtd->size)
  2051. return -EINVAL;
  2052. return do_erase_chip(map, &cfi->chips[0]);
  2053. }
  2054. static int do_atmel_lock(struct map_info *map, struct flchip *chip,
  2055. unsigned long adr, int len, void *thunk)
  2056. {
  2057. struct cfi_private *cfi = map->fldrv_priv;
  2058. int ret;
  2059. mutex_lock(&chip->mutex);
  2060. ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
  2061. if (ret)
  2062. goto out_unlock;
  2063. chip->state = FL_LOCKING;
  2064. pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
  2065. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2066. cfi->device_type, NULL);
  2067. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2068. cfi->device_type, NULL);
  2069. cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
  2070. cfi->device_type, NULL);
  2071. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2072. cfi->device_type, NULL);
  2073. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2074. cfi->device_type, NULL);
  2075. map_write(map, CMD(0x40), chip->start + adr);
  2076. chip->state = FL_READY;
  2077. put_chip(map, chip, adr + chip->start);
  2078. ret = 0;
  2079. out_unlock:
  2080. mutex_unlock(&chip->mutex);
  2081. return ret;
  2082. }
  2083. static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
  2084. unsigned long adr, int len, void *thunk)
  2085. {
  2086. struct cfi_private *cfi = map->fldrv_priv;
  2087. int ret;
  2088. mutex_lock(&chip->mutex);
  2089. ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
  2090. if (ret)
  2091. goto out_unlock;
  2092. chip->state = FL_UNLOCKING;
  2093. pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
  2094. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2095. cfi->device_type, NULL);
  2096. map_write(map, CMD(0x70), adr);
  2097. chip->state = FL_READY;
  2098. put_chip(map, chip, adr + chip->start);
  2099. ret = 0;
  2100. out_unlock:
  2101. mutex_unlock(&chip->mutex);
  2102. return ret;
  2103. }
  2104. static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2105. {
  2106. return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
  2107. }
  2108. static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2109. {
  2110. return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
  2111. }
  2112. /*
  2113. * Advanced Sector Protection - PPB (Persistent Protection Bit) locking
  2114. */
  2115. struct ppb_lock {
  2116. struct flchip *chip;
  2117. unsigned long adr;
  2118. int locked;
  2119. };
  2120. #define MAX_SECTORS 512
  2121. #define DO_XXLOCK_ONEBLOCK_LOCK ((void *)1)
  2122. #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *)2)
  2123. #define DO_XXLOCK_ONEBLOCK_GETLOCK ((void *)3)
  2124. static int __maybe_unused do_ppb_xxlock(struct map_info *map,
  2125. struct flchip *chip,
  2126. unsigned long adr, int len, void *thunk)
  2127. {
  2128. struct cfi_private *cfi = map->fldrv_priv;
  2129. unsigned long timeo;
  2130. int ret;
  2131. adr += chip->start;
  2132. mutex_lock(&chip->mutex);
  2133. ret = get_chip(map, chip, adr, FL_LOCKING);
  2134. if (ret) {
  2135. mutex_unlock(&chip->mutex);
  2136. return ret;
  2137. }
  2138. pr_debug("MTD %s(): XXLOCK 0x%08lx len %d\n", __func__, adr, len);
  2139. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
  2140. cfi->device_type, NULL);
  2141. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
  2142. cfi->device_type, NULL);
  2143. /* PPB entry command */
  2144. cfi_send_gen_cmd(0xC0, cfi->addr_unlock1, chip->start, map, cfi,
  2145. cfi->device_type, NULL);
  2146. if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
  2147. chip->state = FL_LOCKING;
  2148. map_write(map, CMD(0xA0), adr);
  2149. map_write(map, CMD(0x00), adr);
  2150. } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
  2151. /*
  2152. * Unlocking of one specific sector is not supported, so we
  2153. * have to unlock all sectors of this device instead
  2154. */
  2155. chip->state = FL_UNLOCKING;
  2156. map_write(map, CMD(0x80), chip->start);
  2157. map_write(map, CMD(0x30), chip->start);
  2158. } else if (thunk == DO_XXLOCK_ONEBLOCK_GETLOCK) {
  2159. chip->state = FL_JEDEC_QUERY;
  2160. /* Return locked status: 0->locked, 1->unlocked */
  2161. ret = !cfi_read_query(map, adr);
  2162. } else
  2163. BUG();
  2164. /*
  2165. * Wait for some time as unlocking of all sectors takes quite long
  2166. */
  2167. timeo = jiffies + msecs_to_jiffies(2000); /* 2s max (un)locking */
  2168. for (;;) {
  2169. if (chip_ready(map, adr))
  2170. break;
  2171. if (time_after(jiffies, timeo)) {
  2172. printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
  2173. ret = -EIO;
  2174. break;
  2175. }
  2176. UDELAY(map, chip, adr, 1);
  2177. }
  2178. /* Exit BC commands */
  2179. map_write(map, CMD(0x90), chip->start);
  2180. map_write(map, CMD(0x00), chip->start);
  2181. chip->state = FL_READY;
  2182. put_chip(map, chip, adr);
  2183. mutex_unlock(&chip->mutex);
  2184. return ret;
  2185. }
  2186. static int __maybe_unused cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs,
  2187. uint64_t len)
  2188. {
  2189. return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2190. DO_XXLOCK_ONEBLOCK_LOCK);
  2191. }
  2192. static int __maybe_unused cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs,
  2193. uint64_t len)
  2194. {
  2195. struct mtd_erase_region_info *regions = mtd->eraseregions;
  2196. struct map_info *map = mtd->priv;
  2197. struct cfi_private *cfi = map->fldrv_priv;
  2198. struct ppb_lock *sect;
  2199. unsigned long adr;
  2200. loff_t offset;
  2201. uint64_t length;
  2202. int chipnum;
  2203. int i;
  2204. int sectors;
  2205. int ret;
  2206. /*
  2207. * PPB unlocking always unlocks all sectors of the flash chip.
  2208. * We need to re-lock all previously locked sectors. So lets
  2209. * first check the locking status of all sectors and save
  2210. * it for future use.
  2211. */
  2212. sect = kcalloc(MAX_SECTORS, sizeof(struct ppb_lock), GFP_KERNEL);
  2213. if (!sect)
  2214. return -ENOMEM;
  2215. /*
  2216. * This code to walk all sectors is a slightly modified version
  2217. * of the cfi_varsize_frob() code.
  2218. */
  2219. i = 0;
  2220. chipnum = 0;
  2221. adr = 0;
  2222. sectors = 0;
  2223. offset = 0;
  2224. length = mtd->size;
  2225. while (length) {
  2226. int size = regions[i].erasesize;
  2227. /*
  2228. * Only test sectors that shall not be unlocked. The other
  2229. * sectors shall be unlocked, so lets keep their locking
  2230. * status at "unlocked" (locked=0) for the final re-locking.
  2231. */
  2232. if ((offset < ofs) || (offset >= (ofs + len))) {
  2233. sect[sectors].chip = &cfi->chips[chipnum];
  2234. sect[sectors].adr = adr;
  2235. sect[sectors].locked = do_ppb_xxlock(
  2236. map, &cfi->chips[chipnum], adr, 0,
  2237. DO_XXLOCK_ONEBLOCK_GETLOCK);
  2238. }
  2239. adr += size;
  2240. offset += size;
  2241. length -= size;
  2242. if (offset == regions[i].offset + size * regions[i].numblocks)
  2243. i++;
  2244. if (adr >> cfi->chipshift) {
  2245. if (offset >= (ofs + len))
  2246. break;
  2247. adr = 0;
  2248. chipnum++;
  2249. if (chipnum >= cfi->numchips)
  2250. break;
  2251. }
  2252. sectors++;
  2253. if (sectors >= MAX_SECTORS) {
  2254. printk(KERN_ERR "Only %d sectors for PPB locking supported!\n",
  2255. MAX_SECTORS);
  2256. kfree(sect);
  2257. return -EINVAL;
  2258. }
  2259. }
  2260. /* Now unlock the whole chip */
  2261. ret = cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2262. DO_XXLOCK_ONEBLOCK_UNLOCK);
  2263. if (ret) {
  2264. kfree(sect);
  2265. return ret;
  2266. }
  2267. /*
  2268. * PPB unlocking always unlocks all sectors of the flash chip.
  2269. * We need to re-lock all previously locked sectors.
  2270. */
  2271. for (i = 0; i < sectors; i++) {
  2272. if (sect[i].locked)
  2273. do_ppb_xxlock(map, sect[i].chip, sect[i].adr, 0,
  2274. DO_XXLOCK_ONEBLOCK_LOCK);
  2275. }
  2276. kfree(sect);
  2277. return ret;
  2278. }
  2279. static int __maybe_unused cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs,
  2280. uint64_t len)
  2281. {
  2282. return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
  2283. DO_XXLOCK_ONEBLOCK_GETLOCK) ? 1 : 0;
  2284. }
  2285. static void cfi_amdstd_sync (struct mtd_info *mtd)
  2286. {
  2287. struct map_info *map = mtd->priv;
  2288. struct cfi_private *cfi = map->fldrv_priv;
  2289. int i;
  2290. struct flchip *chip;
  2291. int ret = 0;
  2292. DECLARE_WAITQUEUE(wait, current);
  2293. for (i=0; !ret && i<cfi->numchips; i++) {
  2294. chip = &cfi->chips[i];
  2295. retry:
  2296. mutex_lock(&chip->mutex);
  2297. switch(chip->state) {
  2298. case FL_READY:
  2299. case FL_STATUS:
  2300. case FL_CFI_QUERY:
  2301. case FL_JEDEC_QUERY:
  2302. chip->oldstate = chip->state;
  2303. chip->state = FL_SYNCING;
  2304. /* No need to wake_up() on this state change -
  2305. * as the whole point is that nobody can do anything
  2306. * with the chip now anyway.
  2307. */
  2308. case FL_SYNCING:
  2309. mutex_unlock(&chip->mutex);
  2310. break;
  2311. default:
  2312. /* Not an idle state */
  2313. set_current_state(TASK_UNINTERRUPTIBLE);
  2314. add_wait_queue(&chip->wq, &wait);
  2315. mutex_unlock(&chip->mutex);
  2316. schedule();
  2317. remove_wait_queue(&chip->wq, &wait);
  2318. goto retry;
  2319. }
  2320. }
  2321. /* Unlock the chips again */
  2322. for (i--; i >=0; i--) {
  2323. chip = &cfi->chips[i];
  2324. mutex_lock(&chip->mutex);
  2325. if (chip->state == FL_SYNCING) {
  2326. chip->state = chip->oldstate;
  2327. wake_up(&chip->wq);
  2328. }
  2329. mutex_unlock(&chip->mutex);
  2330. }
  2331. }
  2332. static int cfi_amdstd_suspend(struct mtd_info *mtd)
  2333. {
  2334. struct map_info *map = mtd->priv;
  2335. struct cfi_private *cfi = map->fldrv_priv;
  2336. int i;
  2337. struct flchip *chip;
  2338. int ret = 0;
  2339. for (i=0; !ret && i<cfi->numchips; i++) {
  2340. chip = &cfi->chips[i];
  2341. mutex_lock(&chip->mutex);
  2342. switch(chip->state) {
  2343. case FL_READY:
  2344. case FL_STATUS:
  2345. case FL_CFI_QUERY:
  2346. case FL_JEDEC_QUERY:
  2347. chip->oldstate = chip->state;
  2348. chip->state = FL_PM_SUSPENDED;
  2349. /* No need to wake_up() on this state change -
  2350. * as the whole point is that nobody can do anything
  2351. * with the chip now anyway.
  2352. */
  2353. case FL_PM_SUSPENDED:
  2354. break;
  2355. default:
  2356. ret = -EAGAIN;
  2357. break;
  2358. }
  2359. mutex_unlock(&chip->mutex);
  2360. }
  2361. /* Unlock the chips again */
  2362. if (ret) {
  2363. for (i--; i >=0; i--) {
  2364. chip = &cfi->chips[i];
  2365. mutex_lock(&chip->mutex);
  2366. if (chip->state == FL_PM_SUSPENDED) {
  2367. chip->state = chip->oldstate;
  2368. wake_up(&chip->wq);
  2369. }
  2370. mutex_unlock(&chip->mutex);
  2371. }
  2372. }
  2373. return ret;
  2374. }
  2375. static void cfi_amdstd_resume(struct mtd_info *mtd)
  2376. {
  2377. struct map_info *map = mtd->priv;
  2378. struct cfi_private *cfi = map->fldrv_priv;
  2379. int i;
  2380. struct flchip *chip;
  2381. for (i=0; i<cfi->numchips; i++) {
  2382. chip = &cfi->chips[i];
  2383. mutex_lock(&chip->mutex);
  2384. if (chip->state == FL_PM_SUSPENDED) {
  2385. chip->state = FL_READY;
  2386. map_write(map, CMD(0xF0), chip->start);
  2387. wake_up(&chip->wq);
  2388. }
  2389. else
  2390. printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
  2391. mutex_unlock(&chip->mutex);
  2392. }
  2393. }
  2394. /*
  2395. * Ensure that the flash device is put back into read array mode before
  2396. * unloading the driver or rebooting. On some systems, rebooting while
  2397. * the flash is in query/program/erase mode will prevent the CPU from
  2398. * fetching the bootloader code, requiring a hard reset or power cycle.
  2399. */
  2400. static int cfi_amdstd_reset(struct mtd_info *mtd)
  2401. {
  2402. struct map_info *map = mtd->priv;
  2403. struct cfi_private *cfi = map->fldrv_priv;
  2404. int i, ret;
  2405. struct flchip *chip;
  2406. for (i = 0; i < cfi->numchips; i++) {
  2407. chip = &cfi->chips[i];
  2408. mutex_lock(&chip->mutex);
  2409. ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
  2410. if (!ret) {
  2411. map_write(map, CMD(0xF0), chip->start);
  2412. chip->state = FL_SHUTDOWN;
  2413. put_chip(map, chip, chip->start);
  2414. }
  2415. mutex_unlock(&chip->mutex);
  2416. }
  2417. return 0;
  2418. }
  2419. static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
  2420. void *v)
  2421. {
  2422. struct mtd_info *mtd;
  2423. mtd = container_of(nb, struct mtd_info, reboot_notifier);
  2424. cfi_amdstd_reset(mtd);
  2425. return NOTIFY_DONE;
  2426. }
  2427. static void cfi_amdstd_destroy(struct mtd_info *mtd)
  2428. {
  2429. struct map_info *map = mtd->priv;
  2430. struct cfi_private *cfi = map->fldrv_priv;
  2431. cfi_amdstd_reset(mtd);
  2432. unregister_reboot_notifier(&mtd->reboot_notifier);
  2433. kfree(cfi->cmdset_priv);
  2434. kfree(cfi->cfiq);
  2435. kfree(cfi);
  2436. kfree(mtd->eraseregions);
  2437. }
  2438. MODULE_LICENSE("GPL");
  2439. MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
  2440. MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
  2441. MODULE_ALIAS("cfi_cmdset_0006");
  2442. MODULE_ALIAS("cfi_cmdset_0701");