cfi_cmdset_0002.c 80 KB

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