cfi_cmdset_0001.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623
  1. /*
  2. * Common Flash Interface support:
  3. * Intel Extended Vendor Command Set (ID 0x0001)
  4. *
  5. * (C) 2000 Red Hat. GPL'd
  6. *
  7. *
  8. * 10/10/2000 Nicolas Pitre <nico@fluxnic.net>
  9. * - completely revamped method functions so they are aware and
  10. * independent of the flash geometry (buswidth, interleave, etc.)
  11. * - scalability vs code size is completely set at compile-time
  12. * (see include/linux/mtd/cfi.h for selection)
  13. * - optimized write buffer method
  14. * 02/05/2002 Christopher Hoover <ch@hpl.hp.com>/<ch@murgatroid.com>
  15. * - reworked lock/unlock/erase support for var size flash
  16. * 21/03/2007 Rodolfo Giometti <giometti@linux.it>
  17. * - auto unlock sectors on resume for auto locking flash on power up
  18. */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/init.h>
  24. #include <asm/io.h>
  25. #include <asm/byteorder.h>
  26. #include <linux/errno.h>
  27. #include <linux/slab.h>
  28. #include <linux/delay.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/reboot.h>
  31. #include <linux/bitmap.h>
  32. #include <linux/mtd/xip.h>
  33. #include <linux/mtd/map.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/compatmac.h>
  36. #include <linux/mtd/cfi.h>
  37. /* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */
  38. /* #define CMDSET0001_DISABLE_WRITE_SUSPEND */
  39. // debugging, turns off buffer write mode if set to 1
  40. #define FORCE_WORD_WRITE 0
  41. /* Intel chips */
  42. #define I82802AB 0x00ad
  43. #define I82802AC 0x00ac
  44. #define PF38F4476 0x881c
  45. /* STMicroelectronics chips */
  46. #define M50LPW080 0x002F
  47. #define M50FLW080A 0x0080
  48. #define M50FLW080B 0x0081
  49. /* Atmel chips */
  50. #define AT49BV640D 0x02de
  51. #define AT49BV640DT 0x02db
  52. static int cfi_intelext_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  53. static int cfi_intelext_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  54. static int cfi_intelext_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  55. static int cfi_intelext_writev(struct mtd_info *, const struct kvec *, unsigned long, loff_t, size_t *);
  56. static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *);
  57. static void cfi_intelext_sync (struct mtd_info *);
  58. static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  59. static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  60. #ifdef CONFIG_MTD_OTP
  61. static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  62. static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  63. static int cfi_intelext_write_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  64. static int cfi_intelext_lock_user_prot_reg (struct mtd_info *, loff_t, size_t);
  65. static int cfi_intelext_get_fact_prot_info (struct mtd_info *,
  66. struct otp_info *, size_t);
  67. static int cfi_intelext_get_user_prot_info (struct mtd_info *,
  68. struct otp_info *, size_t);
  69. #endif
  70. static int cfi_intelext_suspend (struct mtd_info *);
  71. static void cfi_intelext_resume (struct mtd_info *);
  72. static int cfi_intelext_reboot (struct notifier_block *, unsigned long, void *);
  73. static void cfi_intelext_destroy(struct mtd_info *);
  74. struct mtd_info *cfi_cmdset_0001(struct map_info *, int);
  75. static struct mtd_info *cfi_intelext_setup (struct mtd_info *);
  76. static int cfi_intelext_partition_fixup(struct mtd_info *, struct cfi_private **);
  77. static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len,
  78. size_t *retlen, void **virt, resource_size_t *phys);
  79. static void cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
  80. static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
  81. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
  82. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
  83. #include "fwh_lock.h"
  84. /*
  85. * *********** SETUP AND PROBE BITS ***********
  86. */
  87. static struct mtd_chip_driver cfi_intelext_chipdrv = {
  88. .probe = NULL, /* Not usable directly */
  89. .destroy = cfi_intelext_destroy,
  90. .name = "cfi_cmdset_0001",
  91. .module = THIS_MODULE
  92. };
  93. /* #define DEBUG_LOCK_BITS */
  94. /* #define DEBUG_CFI_FEATURES */
  95. #ifdef DEBUG_CFI_FEATURES
  96. static void cfi_tell_features(struct cfi_pri_intelext *extp)
  97. {
  98. int i;
  99. printk(" Extended Query version %c.%c\n", extp->MajorVersion, extp->MinorVersion);
  100. printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport);
  101. printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported");
  102. printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported");
  103. printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported");
  104. printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported");
  105. printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported");
  106. printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported");
  107. printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported");
  108. printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported");
  109. printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported");
  110. printk(" - Simultaneous operations: %s\n", extp->FeatureSupport&512?"supported":"unsupported");
  111. printk(" - Extended Flash Array: %s\n", extp->FeatureSupport&1024?"supported":"unsupported");
  112. for (i=11; i<32; i++) {
  113. if (extp->FeatureSupport & (1<<i))
  114. printk(" - Unknown Bit %X: supported\n", i);
  115. }
  116. printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport);
  117. printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported");
  118. for (i=1; i<8; i++) {
  119. if (extp->SuspendCmdSupport & (1<<i))
  120. printk(" - Unknown Bit %X: supported\n", i);
  121. }
  122. printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask);
  123. printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no");
  124. printk(" - Lock-Down Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no");
  125. for (i=2; i<3; i++) {
  126. if (extp->BlkStatusRegMask & (1<<i))
  127. printk(" - Unknown Bit %X Active: yes\n",i);
  128. }
  129. printk(" - EFA Lock Bit: %s\n", extp->BlkStatusRegMask&16?"yes":"no");
  130. printk(" - EFA Lock-Down Bit: %s\n", extp->BlkStatusRegMask&32?"yes":"no");
  131. for (i=6; i<16; i++) {
  132. if (extp->BlkStatusRegMask & (1<<i))
  133. printk(" - Unknown Bit %X Active: yes\n",i);
  134. }
  135. printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
  136. extp->VccOptimal >> 4, extp->VccOptimal & 0xf);
  137. if (extp->VppOptimal)
  138. printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
  139. extp->VppOptimal >> 4, extp->VppOptimal & 0xf);
  140. }
  141. #endif
  142. /* Atmel chips don't use the same PRI format as Intel chips */
  143. static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
  144. {
  145. struct map_info *map = mtd->priv;
  146. struct cfi_private *cfi = map->fldrv_priv;
  147. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  148. struct cfi_pri_atmel atmel_pri;
  149. uint32_t features = 0;
  150. /* Reverse byteswapping */
  151. extp->FeatureSupport = cpu_to_le32(extp->FeatureSupport);
  152. extp->BlkStatusRegMask = cpu_to_le16(extp->BlkStatusRegMask);
  153. extp->ProtRegAddr = cpu_to_le16(extp->ProtRegAddr);
  154. memcpy(&atmel_pri, extp, sizeof(atmel_pri));
  155. memset((char *)extp + 5, 0, sizeof(*extp) - 5);
  156. printk(KERN_ERR "atmel Features: %02x\n", atmel_pri.Features);
  157. if (atmel_pri.Features & 0x01) /* chip erase supported */
  158. features |= (1<<0);
  159. if (atmel_pri.Features & 0x02) /* erase suspend supported */
  160. features |= (1<<1);
  161. if (atmel_pri.Features & 0x04) /* program suspend supported */
  162. features |= (1<<2);
  163. if (atmel_pri.Features & 0x08) /* simultaneous operations supported */
  164. features |= (1<<9);
  165. if (atmel_pri.Features & 0x20) /* page mode read supported */
  166. features |= (1<<7);
  167. if (atmel_pri.Features & 0x40) /* queued erase supported */
  168. features |= (1<<4);
  169. if (atmel_pri.Features & 0x80) /* Protection bits supported */
  170. features |= (1<<6);
  171. extp->FeatureSupport = features;
  172. /* burst write mode not supported */
  173. cfi->cfiq->BufWriteTimeoutTyp = 0;
  174. cfi->cfiq->BufWriteTimeoutMax = 0;
  175. }
  176. static void fixup_at49bv640dx_lock(struct mtd_info *mtd, void *param)
  177. {
  178. struct map_info *map = mtd->priv;
  179. struct cfi_private *cfi = map->fldrv_priv;
  180. struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
  181. cfip->FeatureSupport |= (1 << 5);
  182. mtd->flags |= MTD_POWERUP_LOCK;
  183. }
  184. #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
  185. /* Some Intel Strata Flash prior to FPO revision C has bugs in this area */
  186. static void fixup_intel_strataflash(struct mtd_info *mtd, void* param)
  187. {
  188. struct map_info *map = mtd->priv;
  189. struct cfi_private *cfi = map->fldrv_priv;
  190. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  191. printk(KERN_WARNING "cfi_cmdset_0001: Suspend "
  192. "erase on write disabled.\n");
  193. extp->SuspendCmdSupport &= ~1;
  194. }
  195. #endif
  196. #ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
  197. static void fixup_no_write_suspend(struct mtd_info *mtd, void* param)
  198. {
  199. struct map_info *map = mtd->priv;
  200. struct cfi_private *cfi = map->fldrv_priv;
  201. struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
  202. if (cfip && (cfip->FeatureSupport&4)) {
  203. cfip->FeatureSupport &= ~4;
  204. printk(KERN_WARNING "cfi_cmdset_0001: write suspend disabled\n");
  205. }
  206. }
  207. #endif
  208. static void fixup_st_m28w320ct(struct mtd_info *mtd, void* param)
  209. {
  210. struct map_info *map = mtd->priv;
  211. struct cfi_private *cfi = map->fldrv_priv;
  212. cfi->cfiq->BufWriteTimeoutTyp = 0; /* Not supported */
  213. cfi->cfiq->BufWriteTimeoutMax = 0; /* Not supported */
  214. }
  215. static void fixup_st_m28w320cb(struct mtd_info *mtd, void* param)
  216. {
  217. struct map_info *map = mtd->priv;
  218. struct cfi_private *cfi = map->fldrv_priv;
  219. /* Note this is done after the region info is endian swapped */
  220. cfi->cfiq->EraseRegionInfo[1] =
  221. (cfi->cfiq->EraseRegionInfo[1] & 0xffff0000) | 0x3e;
  222. };
  223. static void fixup_use_point(struct mtd_info *mtd, void *param)
  224. {
  225. struct map_info *map = mtd->priv;
  226. if (!mtd->point && map_is_linear(map)) {
  227. mtd->point = cfi_intelext_point;
  228. mtd->unpoint = cfi_intelext_unpoint;
  229. }
  230. }
  231. static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
  232. {
  233. struct map_info *map = mtd->priv;
  234. struct cfi_private *cfi = map->fldrv_priv;
  235. if (cfi->cfiq->BufWriteTimeoutTyp) {
  236. printk(KERN_INFO "Using buffer write method\n" );
  237. mtd->write = cfi_intelext_write_buffers;
  238. mtd->writev = cfi_intelext_writev;
  239. }
  240. }
  241. /*
  242. * Some chips power-up with all sectors locked by default.
  243. */
  244. static void fixup_unlock_powerup_lock(struct mtd_info *mtd, void *param)
  245. {
  246. struct map_info *map = mtd->priv;
  247. struct cfi_private *cfi = map->fldrv_priv;
  248. struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
  249. if (cfip->FeatureSupport&32) {
  250. printk(KERN_INFO "Using auto-unlock on power-up/resume\n" );
  251. mtd->flags |= MTD_POWERUP_LOCK;
  252. }
  253. }
  254. static struct cfi_fixup cfi_fixup_table[] = {
  255. { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
  256. { CFI_MFR_ATMEL, AT49BV640D, fixup_at49bv640dx_lock, NULL },
  257. { CFI_MFR_ATMEL, AT49BV640DT, fixup_at49bv640dx_lock, NULL },
  258. #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
  259. { CFI_MFR_ANY, CFI_ID_ANY, fixup_intel_strataflash, NULL },
  260. #endif
  261. #ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
  262. { CFI_MFR_ANY, CFI_ID_ANY, fixup_no_write_suspend, NULL },
  263. #endif
  264. #if !FORCE_WORD_WRITE
  265. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL },
  266. #endif
  267. { CFI_MFR_ST, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct, NULL },
  268. { CFI_MFR_ST, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb, NULL },
  269. { CFI_MFR_INTEL, CFI_ID_ANY, fixup_unlock_powerup_lock, NULL, },
  270. { 0, 0, NULL, NULL }
  271. };
  272. static struct cfi_fixup jedec_fixup_table[] = {
  273. { CFI_MFR_INTEL, I82802AB, fixup_use_fwh_lock, NULL, },
  274. { CFI_MFR_INTEL, I82802AC, fixup_use_fwh_lock, NULL, },
  275. { CFI_MFR_ST, M50LPW080, fixup_use_fwh_lock, NULL, },
  276. { CFI_MFR_ST, M50FLW080A, fixup_use_fwh_lock, NULL, },
  277. { CFI_MFR_ST, M50FLW080B, fixup_use_fwh_lock, NULL, },
  278. { 0, 0, NULL, NULL }
  279. };
  280. static struct cfi_fixup fixup_table[] = {
  281. /* The CFI vendor ids and the JEDEC vendor IDs appear
  282. * to be common. It is like the devices id's are as
  283. * well. This table is to pick all cases where
  284. * we know that is the case.
  285. */
  286. { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_point, NULL },
  287. { 0, 0, NULL, NULL }
  288. };
  289. static void cfi_fixup_major_minor(struct cfi_private *cfi,
  290. struct cfi_pri_intelext *extp)
  291. {
  292. if (cfi->mfr == CFI_MFR_INTEL &&
  293. cfi->id == PF38F4476 && extp->MinorVersion == '3')
  294. extp->MinorVersion = '1';
  295. }
  296. static inline struct cfi_pri_intelext *
  297. read_pri_intelext(struct map_info *map, __u16 adr)
  298. {
  299. struct cfi_private *cfi = map->fldrv_priv;
  300. struct cfi_pri_intelext *extp;
  301. unsigned int extra_size = 0;
  302. unsigned int extp_size = sizeof(*extp);
  303. again:
  304. extp = (struct cfi_pri_intelext *)cfi_read_pri(map, adr, extp_size, "Intel/Sharp");
  305. if (!extp)
  306. return NULL;
  307. cfi_fixup_major_minor(cfi, extp);
  308. if (extp->MajorVersion != '1' ||
  309. (extp->MinorVersion < '0' || extp->MinorVersion > '5')) {
  310. printk(KERN_ERR " Unknown Intel/Sharp Extended Query "
  311. "version %c.%c.\n", extp->MajorVersion,
  312. extp->MinorVersion);
  313. kfree(extp);
  314. return NULL;
  315. }
  316. /* Do some byteswapping if necessary */
  317. extp->FeatureSupport = le32_to_cpu(extp->FeatureSupport);
  318. extp->BlkStatusRegMask = le16_to_cpu(extp->BlkStatusRegMask);
  319. extp->ProtRegAddr = le16_to_cpu(extp->ProtRegAddr);
  320. if (extp->MinorVersion >= '0') {
  321. extra_size = 0;
  322. /* Protection Register info */
  323. extra_size += (extp->NumProtectionFields - 1) *
  324. sizeof(struct cfi_intelext_otpinfo);
  325. }
  326. if (extp->MinorVersion >= '1') {
  327. /* Burst Read info */
  328. extra_size += 2;
  329. if (extp_size < sizeof(*extp) + extra_size)
  330. goto need_more;
  331. extra_size += extp->extra[extra_size - 1];
  332. }
  333. if (extp->MinorVersion >= '3') {
  334. int nb_parts, i;
  335. /* Number of hardware-partitions */
  336. extra_size += 1;
  337. if (extp_size < sizeof(*extp) + extra_size)
  338. goto need_more;
  339. nb_parts = extp->extra[extra_size - 1];
  340. /* skip the sizeof(partregion) field in CFI 1.4 */
  341. if (extp->MinorVersion >= '4')
  342. extra_size += 2;
  343. for (i = 0; i < nb_parts; i++) {
  344. struct cfi_intelext_regioninfo *rinfo;
  345. rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[extra_size];
  346. extra_size += sizeof(*rinfo);
  347. if (extp_size < sizeof(*extp) + extra_size)
  348. goto need_more;
  349. rinfo->NumIdentPartitions=le16_to_cpu(rinfo->NumIdentPartitions);
  350. extra_size += (rinfo->NumBlockTypes - 1)
  351. * sizeof(struct cfi_intelext_blockinfo);
  352. }
  353. if (extp->MinorVersion >= '4')
  354. extra_size += sizeof(struct cfi_intelext_programming_regioninfo);
  355. if (extp_size < sizeof(*extp) + extra_size) {
  356. need_more:
  357. extp_size = sizeof(*extp) + extra_size;
  358. kfree(extp);
  359. if (extp_size > 4096) {
  360. printk(KERN_ERR
  361. "%s: cfi_pri_intelext is too fat\n",
  362. __func__);
  363. return NULL;
  364. }
  365. goto again;
  366. }
  367. }
  368. return extp;
  369. }
  370. struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
  371. {
  372. struct cfi_private *cfi = map->fldrv_priv;
  373. struct mtd_info *mtd;
  374. int i;
  375. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  376. if (!mtd) {
  377. printk(KERN_ERR "Failed to allocate memory for MTD device\n");
  378. return NULL;
  379. }
  380. mtd->priv = map;
  381. mtd->type = MTD_NORFLASH;
  382. /* Fill in the default mtd operations */
  383. mtd->erase = cfi_intelext_erase_varsize;
  384. mtd->read = cfi_intelext_read;
  385. mtd->write = cfi_intelext_write_words;
  386. mtd->sync = cfi_intelext_sync;
  387. mtd->lock = cfi_intelext_lock;
  388. mtd->unlock = cfi_intelext_unlock;
  389. mtd->suspend = cfi_intelext_suspend;
  390. mtd->resume = cfi_intelext_resume;
  391. mtd->flags = MTD_CAP_NORFLASH;
  392. mtd->name = map->name;
  393. mtd->writesize = 1;
  394. mtd->reboot_notifier.notifier_call = cfi_intelext_reboot;
  395. if (cfi->cfi_mode == CFI_MODE_CFI) {
  396. /*
  397. * It's a real CFI chip, not one for which the probe
  398. * routine faked a CFI structure. So we read the feature
  399. * table from it.
  400. */
  401. __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
  402. struct cfi_pri_intelext *extp;
  403. extp = read_pri_intelext(map, adr);
  404. if (!extp) {
  405. kfree(mtd);
  406. return NULL;
  407. }
  408. /* Install our own private info structure */
  409. cfi->cmdset_priv = extp;
  410. cfi_fixup(mtd, cfi_fixup_table);
  411. #ifdef DEBUG_CFI_FEATURES
  412. /* Tell the user about it in lots of lovely detail */
  413. cfi_tell_features(extp);
  414. #endif
  415. if(extp->SuspendCmdSupport & 1) {
  416. printk(KERN_NOTICE "cfi_cmdset_0001: Erase suspend on write enabled\n");
  417. }
  418. }
  419. else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
  420. /* Apply jedec specific fixups */
  421. cfi_fixup(mtd, jedec_fixup_table);
  422. }
  423. /* Apply generic fixups */
  424. cfi_fixup(mtd, fixup_table);
  425. for (i=0; i< cfi->numchips; i++) {
  426. if (cfi->cfiq->WordWriteTimeoutTyp)
  427. cfi->chips[i].word_write_time =
  428. 1<<cfi->cfiq->WordWriteTimeoutTyp;
  429. else
  430. cfi->chips[i].word_write_time = 50000;
  431. if (cfi->cfiq->BufWriteTimeoutTyp)
  432. cfi->chips[i].buffer_write_time =
  433. 1<<cfi->cfiq->BufWriteTimeoutTyp;
  434. /* No default; if it isn't specified, we won't use it */
  435. if (cfi->cfiq->BlockEraseTimeoutTyp)
  436. cfi->chips[i].erase_time =
  437. 1000<<cfi->cfiq->BlockEraseTimeoutTyp;
  438. else
  439. cfi->chips[i].erase_time = 2000000;
  440. if (cfi->cfiq->WordWriteTimeoutTyp &&
  441. cfi->cfiq->WordWriteTimeoutMax)
  442. cfi->chips[i].word_write_time_max =
  443. 1<<(cfi->cfiq->WordWriteTimeoutTyp +
  444. cfi->cfiq->WordWriteTimeoutMax);
  445. else
  446. cfi->chips[i].word_write_time_max = 50000 * 8;
  447. if (cfi->cfiq->BufWriteTimeoutTyp &&
  448. cfi->cfiq->BufWriteTimeoutMax)
  449. cfi->chips[i].buffer_write_time_max =
  450. 1<<(cfi->cfiq->BufWriteTimeoutTyp +
  451. cfi->cfiq->BufWriteTimeoutMax);
  452. if (cfi->cfiq->BlockEraseTimeoutTyp &&
  453. cfi->cfiq->BlockEraseTimeoutMax)
  454. cfi->chips[i].erase_time_max =
  455. 1000<<(cfi->cfiq->BlockEraseTimeoutTyp +
  456. cfi->cfiq->BlockEraseTimeoutMax);
  457. else
  458. cfi->chips[i].erase_time_max = 2000000 * 8;
  459. cfi->chips[i].ref_point_counter = 0;
  460. init_waitqueue_head(&(cfi->chips[i].wq));
  461. }
  462. map->fldrv = &cfi_intelext_chipdrv;
  463. return cfi_intelext_setup(mtd);
  464. }
  465. struct mtd_info *cfi_cmdset_0003(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
  466. struct mtd_info *cfi_cmdset_0200(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
  467. EXPORT_SYMBOL_GPL(cfi_cmdset_0001);
  468. EXPORT_SYMBOL_GPL(cfi_cmdset_0003);
  469. EXPORT_SYMBOL_GPL(cfi_cmdset_0200);
  470. static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
  471. {
  472. struct map_info *map = mtd->priv;
  473. struct cfi_private *cfi = map->fldrv_priv;
  474. unsigned long offset = 0;
  475. int i,j;
  476. unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
  477. //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
  478. mtd->size = devsize * cfi->numchips;
  479. mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
  480. mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
  481. * mtd->numeraseregions, GFP_KERNEL);
  482. if (!mtd->eraseregions) {
  483. printk(KERN_ERR "Failed to allocate memory for MTD erase region info\n");
  484. goto setup_err;
  485. }
  486. for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
  487. unsigned long ernum, ersize;
  488. ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
  489. ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
  490. if (mtd->erasesize < ersize) {
  491. mtd->erasesize = ersize;
  492. }
  493. for (j=0; j<cfi->numchips; j++) {
  494. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
  495. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
  496. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
  497. mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap = kmalloc(ernum / 8 + 1, GFP_KERNEL);
  498. }
  499. offset += (ersize * ernum);
  500. }
  501. if (offset != devsize) {
  502. /* Argh */
  503. printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
  504. goto setup_err;
  505. }
  506. for (i=0; i<mtd->numeraseregions;i++){
  507. printk(KERN_DEBUG "erase region %d: offset=0x%llx,size=0x%x,blocks=%d\n",
  508. i,(unsigned long long)mtd->eraseregions[i].offset,
  509. mtd->eraseregions[i].erasesize,
  510. mtd->eraseregions[i].numblocks);
  511. }
  512. #ifdef CONFIG_MTD_OTP
  513. mtd->read_fact_prot_reg = cfi_intelext_read_fact_prot_reg;
  514. mtd->read_user_prot_reg = cfi_intelext_read_user_prot_reg;
  515. mtd->write_user_prot_reg = cfi_intelext_write_user_prot_reg;
  516. mtd->lock_user_prot_reg = cfi_intelext_lock_user_prot_reg;
  517. mtd->get_fact_prot_info = cfi_intelext_get_fact_prot_info;
  518. mtd->get_user_prot_info = cfi_intelext_get_user_prot_info;
  519. #endif
  520. /* This function has the potential to distort the reality
  521. a bit and therefore should be called last. */
  522. if (cfi_intelext_partition_fixup(mtd, &cfi) != 0)
  523. goto setup_err;
  524. __module_get(THIS_MODULE);
  525. register_reboot_notifier(&mtd->reboot_notifier);
  526. return mtd;
  527. setup_err:
  528. if(mtd) {
  529. kfree(mtd->eraseregions);
  530. kfree(mtd);
  531. }
  532. kfree(cfi->cmdset_priv);
  533. return NULL;
  534. }
  535. static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
  536. struct cfi_private **pcfi)
  537. {
  538. struct map_info *map = mtd->priv;
  539. struct cfi_private *cfi = *pcfi;
  540. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  541. /*
  542. * Probing of multi-partition flash chips.
  543. *
  544. * To support multiple partitions when available, we simply arrange
  545. * for each of them to have their own flchip structure even if they
  546. * are on the same physical chip. This means completely recreating
  547. * a new cfi_private structure right here which is a blatent code
  548. * layering violation, but this is still the least intrusive
  549. * arrangement at this point. This can be rearranged in the future
  550. * if someone feels motivated enough. --nico
  551. */
  552. if (extp && extp->MajorVersion == '1' && extp->MinorVersion >= '3'
  553. && extp->FeatureSupport & (1 << 9)) {
  554. struct cfi_private *newcfi;
  555. struct flchip *chip;
  556. struct flchip_shared *shared;
  557. int offs, numregions, numparts, partshift, numvirtchips, i, j;
  558. /* Protection Register info */
  559. offs = (extp->NumProtectionFields - 1) *
  560. sizeof(struct cfi_intelext_otpinfo);
  561. /* Burst Read info */
  562. offs += extp->extra[offs+1]+2;
  563. /* Number of partition regions */
  564. numregions = extp->extra[offs];
  565. offs += 1;
  566. /* skip the sizeof(partregion) field in CFI 1.4 */
  567. if (extp->MinorVersion >= '4')
  568. offs += 2;
  569. /* Number of hardware partitions */
  570. numparts = 0;
  571. for (i = 0; i < numregions; i++) {
  572. struct cfi_intelext_regioninfo *rinfo;
  573. rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[offs];
  574. numparts += rinfo->NumIdentPartitions;
  575. offs += sizeof(*rinfo)
  576. + (rinfo->NumBlockTypes - 1) *
  577. sizeof(struct cfi_intelext_blockinfo);
  578. }
  579. if (!numparts)
  580. numparts = 1;
  581. /* Programming Region info */
  582. if (extp->MinorVersion >= '4') {
  583. struct cfi_intelext_programming_regioninfo *prinfo;
  584. prinfo = (struct cfi_intelext_programming_regioninfo *)&extp->extra[offs];
  585. mtd->writesize = cfi->interleave << prinfo->ProgRegShift;
  586. mtd->flags &= ~MTD_BIT_WRITEABLE;
  587. printk(KERN_DEBUG "%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
  588. map->name, mtd->writesize,
  589. cfi->interleave * prinfo->ControlValid,
  590. cfi->interleave * prinfo->ControlInvalid);
  591. }
  592. /*
  593. * All functions below currently rely on all chips having
  594. * the same geometry so we'll just assume that all hardware
  595. * partitions are of the same size too.
  596. */
  597. partshift = cfi->chipshift - __ffs(numparts);
  598. if ((1 << partshift) < mtd->erasesize) {
  599. printk( KERN_ERR
  600. "%s: bad number of hw partitions (%d)\n",
  601. __func__, numparts);
  602. return -EINVAL;
  603. }
  604. numvirtchips = cfi->numchips * numparts;
  605. newcfi = kmalloc(sizeof(struct cfi_private) + numvirtchips * sizeof(struct flchip), GFP_KERNEL);
  606. if (!newcfi)
  607. return -ENOMEM;
  608. shared = kmalloc(sizeof(struct flchip_shared) * cfi->numchips, GFP_KERNEL);
  609. if (!shared) {
  610. kfree(newcfi);
  611. return -ENOMEM;
  612. }
  613. memcpy(newcfi, cfi, sizeof(struct cfi_private));
  614. newcfi->numchips = numvirtchips;
  615. newcfi->chipshift = partshift;
  616. chip = &newcfi->chips[0];
  617. for (i = 0; i < cfi->numchips; i++) {
  618. shared[i].writing = shared[i].erasing = NULL;
  619. spin_lock_init(&shared[i].lock);
  620. for (j = 0; j < numparts; j++) {
  621. *chip = cfi->chips[i];
  622. chip->start += j << partshift;
  623. chip->priv = &shared[i];
  624. /* those should be reset too since
  625. they create memory references. */
  626. init_waitqueue_head(&chip->wq);
  627. spin_lock_init(&chip->_spinlock);
  628. chip->mutex = &chip->_spinlock;
  629. chip++;
  630. }
  631. }
  632. printk(KERN_DEBUG "%s: %d set(s) of %d interleaved chips "
  633. "--> %d partitions of %d KiB\n",
  634. map->name, cfi->numchips, cfi->interleave,
  635. newcfi->numchips, 1<<(newcfi->chipshift-10));
  636. map->fldrv_priv = newcfi;
  637. *pcfi = newcfi;
  638. kfree(cfi);
  639. }
  640. return 0;
  641. }
  642. /*
  643. * *********** CHIP ACCESS FUNCTIONS ***********
  644. */
  645. static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  646. {
  647. DECLARE_WAITQUEUE(wait, current);
  648. struct cfi_private *cfi = map->fldrv_priv;
  649. map_word status, status_OK = CMD(0x80), status_PWS = CMD(0x01);
  650. struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
  651. unsigned long timeo = jiffies + HZ;
  652. /* Prevent setting state FL_SYNCING for chip in suspended state. */
  653. if (mode == FL_SYNCING && chip->oldstate != FL_READY)
  654. goto sleep;
  655. switch (chip->state) {
  656. case FL_STATUS:
  657. for (;;) {
  658. status = map_read(map, adr);
  659. if (map_word_andequal(map, status, status_OK, status_OK))
  660. break;
  661. /* At this point we're fine with write operations
  662. in other partitions as they don't conflict. */
  663. if (chip->priv && map_word_andequal(map, status, status_PWS, status_PWS))
  664. break;
  665. spin_unlock(chip->mutex);
  666. cfi_udelay(1);
  667. spin_lock(chip->mutex);
  668. /* Someone else might have been playing with it. */
  669. return -EAGAIN;
  670. }
  671. /* Fall through */
  672. case FL_READY:
  673. case FL_CFI_QUERY:
  674. case FL_JEDEC_QUERY:
  675. return 0;
  676. case FL_ERASING:
  677. if (!cfip ||
  678. !(cfip->FeatureSupport & 2) ||
  679. !(mode == FL_READY || mode == FL_POINT ||
  680. (mode == FL_WRITING && (cfip->SuspendCmdSupport & 1))))
  681. goto sleep;
  682. /* Erase suspend */
  683. map_write(map, CMD(0xB0), adr);
  684. /* If the flash has finished erasing, then 'erase suspend'
  685. * appears to make some (28F320) flash devices switch to
  686. * 'read' mode. Make sure that we switch to 'read status'
  687. * mode so we get the right data. --rmk
  688. */
  689. map_write(map, CMD(0x70), adr);
  690. chip->oldstate = FL_ERASING;
  691. chip->state = FL_ERASE_SUSPENDING;
  692. chip->erase_suspended = 1;
  693. for (;;) {
  694. status = map_read(map, adr);
  695. if (map_word_andequal(map, status, status_OK, status_OK))
  696. break;
  697. if (time_after(jiffies, timeo)) {
  698. /* Urgh. Resume and pretend we weren't here. */
  699. map_write(map, CMD(0xd0), adr);
  700. /* Make sure we're in 'read status' mode if it had finished */
  701. map_write(map, CMD(0x70), adr);
  702. chip->state = FL_ERASING;
  703. chip->oldstate = FL_READY;
  704. printk(KERN_ERR "%s: Chip not ready after erase "
  705. "suspended: status = 0x%lx\n", map->name, status.x[0]);
  706. return -EIO;
  707. }
  708. spin_unlock(chip->mutex);
  709. cfi_udelay(1);
  710. spin_lock(chip->mutex);
  711. /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
  712. So we can just loop here. */
  713. }
  714. chip->state = FL_STATUS;
  715. return 0;
  716. case FL_XIP_WHILE_ERASING:
  717. if (mode != FL_READY && mode != FL_POINT &&
  718. (mode != FL_WRITING || !cfip || !(cfip->SuspendCmdSupport&1)))
  719. goto sleep;
  720. chip->oldstate = chip->state;
  721. chip->state = FL_READY;
  722. return 0;
  723. case FL_SHUTDOWN:
  724. /* The machine is rebooting now,so no one can get chip anymore */
  725. return -EIO;
  726. case FL_POINT:
  727. /* Only if there's no operation suspended... */
  728. if (mode == FL_READY && chip->oldstate == FL_READY)
  729. return 0;
  730. /* Fall through */
  731. default:
  732. sleep:
  733. set_current_state(TASK_UNINTERRUPTIBLE);
  734. add_wait_queue(&chip->wq, &wait);
  735. spin_unlock(chip->mutex);
  736. schedule();
  737. remove_wait_queue(&chip->wq, &wait);
  738. spin_lock(chip->mutex);
  739. return -EAGAIN;
  740. }
  741. }
  742. static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
  743. {
  744. int ret;
  745. DECLARE_WAITQUEUE(wait, current);
  746. retry:
  747. if (chip->priv &&
  748. (mode == FL_WRITING || mode == FL_ERASING || mode == FL_OTP_WRITE
  749. || mode == FL_SHUTDOWN) && chip->state != FL_SYNCING) {
  750. /*
  751. * OK. We have possibility for contention on the write/erase
  752. * operations which are global to the real chip and not per
  753. * partition. So let's fight it over in the partition which
  754. * currently has authority on the operation.
  755. *
  756. * The rules are as follows:
  757. *
  758. * - any write operation must own shared->writing.
  759. *
  760. * - any erase operation must own _both_ shared->writing and
  761. * shared->erasing.
  762. *
  763. * - contention arbitration is handled in the owner's context.
  764. *
  765. * The 'shared' struct can be read and/or written only when
  766. * its lock is taken.
  767. */
  768. struct flchip_shared *shared = chip->priv;
  769. struct flchip *contender;
  770. spin_lock(&shared->lock);
  771. contender = shared->writing;
  772. if (contender && contender != chip) {
  773. /*
  774. * The engine to perform desired operation on this
  775. * partition is already in use by someone else.
  776. * Let's fight over it in the context of the chip
  777. * currently using it. If it is possible to suspend,
  778. * that other partition will do just that, otherwise
  779. * it'll happily send us to sleep. In any case, when
  780. * get_chip returns success we're clear to go ahead.
  781. */
  782. ret = spin_trylock(contender->mutex);
  783. spin_unlock(&shared->lock);
  784. if (!ret)
  785. goto retry;
  786. spin_unlock(chip->mutex);
  787. ret = chip_ready(map, contender, contender->start, mode);
  788. spin_lock(chip->mutex);
  789. if (ret == -EAGAIN) {
  790. spin_unlock(contender->mutex);
  791. goto retry;
  792. }
  793. if (ret) {
  794. spin_unlock(contender->mutex);
  795. return ret;
  796. }
  797. spin_lock(&shared->lock);
  798. /* We should not own chip if it is already
  799. * in FL_SYNCING state. Put contender and retry. */
  800. if (chip->state == FL_SYNCING) {
  801. put_chip(map, contender, contender->start);
  802. spin_unlock(contender->mutex);
  803. goto retry;
  804. }
  805. spin_unlock(contender->mutex);
  806. }
  807. /* Check if we already have suspended erase
  808. * on this chip. Sleep. */
  809. if (mode == FL_ERASING && shared->erasing
  810. && shared->erasing->oldstate == FL_ERASING) {
  811. spin_unlock(&shared->lock);
  812. set_current_state(TASK_UNINTERRUPTIBLE);
  813. add_wait_queue(&chip->wq, &wait);
  814. spin_unlock(chip->mutex);
  815. schedule();
  816. remove_wait_queue(&chip->wq, &wait);
  817. spin_lock(chip->mutex);
  818. goto retry;
  819. }
  820. /* We now own it */
  821. shared->writing = chip;
  822. if (mode == FL_ERASING)
  823. shared->erasing = chip;
  824. spin_unlock(&shared->lock);
  825. }
  826. ret = chip_ready(map, chip, adr, mode);
  827. if (ret == -EAGAIN)
  828. goto retry;
  829. return ret;
  830. }
  831. static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
  832. {
  833. struct cfi_private *cfi = map->fldrv_priv;
  834. if (chip->priv) {
  835. struct flchip_shared *shared = chip->priv;
  836. spin_lock(&shared->lock);
  837. if (shared->writing == chip && chip->oldstate == FL_READY) {
  838. /* We own the ability to write, but we're done */
  839. shared->writing = shared->erasing;
  840. if (shared->writing && shared->writing != chip) {
  841. /* give back ownership to who we loaned it from */
  842. struct flchip *loaner = shared->writing;
  843. spin_lock(loaner->mutex);
  844. spin_unlock(&shared->lock);
  845. spin_unlock(chip->mutex);
  846. put_chip(map, loaner, loaner->start);
  847. spin_lock(chip->mutex);
  848. spin_unlock(loaner->mutex);
  849. wake_up(&chip->wq);
  850. return;
  851. }
  852. shared->erasing = NULL;
  853. shared->writing = NULL;
  854. } else if (shared->erasing == chip && shared->writing != chip) {
  855. /*
  856. * We own the ability to erase without the ability
  857. * to write, which means the erase was suspended
  858. * and some other partition is currently writing.
  859. * Don't let the switch below mess things up since
  860. * we don't have ownership to resume anything.
  861. */
  862. spin_unlock(&shared->lock);
  863. wake_up(&chip->wq);
  864. return;
  865. }
  866. spin_unlock(&shared->lock);
  867. }
  868. switch(chip->oldstate) {
  869. case FL_ERASING:
  870. chip->state = chip->oldstate;
  871. /* What if one interleaved chip has finished and the
  872. other hasn't? The old code would leave the finished
  873. one in READY mode. That's bad, and caused -EROFS
  874. errors to be returned from do_erase_oneblock because
  875. that's the only bit it checked for at the time.
  876. As the state machine appears to explicitly allow
  877. sending the 0x70 (Read Status) command to an erasing
  878. chip and expecting it to be ignored, that's what we
  879. do. */
  880. map_write(map, CMD(0xd0), adr);
  881. map_write(map, CMD(0x70), adr);
  882. chip->oldstate = FL_READY;
  883. chip->state = FL_ERASING;
  884. break;
  885. case FL_XIP_WHILE_ERASING:
  886. chip->state = chip->oldstate;
  887. chip->oldstate = FL_READY;
  888. break;
  889. case FL_READY:
  890. case FL_STATUS:
  891. case FL_JEDEC_QUERY:
  892. /* We should really make set_vpp() count, rather than doing this */
  893. DISABLE_VPP(map);
  894. break;
  895. default:
  896. printk(KERN_ERR "%s: put_chip() called with oldstate %d!!\n", map->name, chip->oldstate);
  897. }
  898. wake_up(&chip->wq);
  899. }
  900. #ifdef CONFIG_MTD_XIP
  901. /*
  902. * No interrupt what so ever can be serviced while the flash isn't in array
  903. * mode. This is ensured by the xip_disable() and xip_enable() functions
  904. * enclosing any code path where the flash is known not to be in array mode.
  905. * And within a XIP disabled code path, only functions marked with __xipram
  906. * may be called and nothing else (it's a good thing to inspect generated
  907. * assembly to make sure inline functions were actually inlined and that gcc
  908. * didn't emit calls to its own support functions). Also configuring MTD CFI
  909. * support to a single buswidth and a single interleave is also recommended.
  910. */
  911. static void xip_disable(struct map_info *map, struct flchip *chip,
  912. unsigned long adr)
  913. {
  914. /* TODO: chips with no XIP use should ignore and return */
  915. (void) map_read(map, adr); /* ensure mmu mapping is up to date */
  916. local_irq_disable();
  917. }
  918. static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
  919. unsigned long adr)
  920. {
  921. struct cfi_private *cfi = map->fldrv_priv;
  922. if (chip->state != FL_POINT && chip->state != FL_READY) {
  923. map_write(map, CMD(0xff), adr);
  924. chip->state = FL_READY;
  925. }
  926. (void) map_read(map, adr);
  927. xip_iprefetch();
  928. local_irq_enable();
  929. }
  930. /*
  931. * When a delay is required for the flash operation to complete, the
  932. * xip_wait_for_operation() function is polling for both the given timeout
  933. * and pending (but still masked) hardware interrupts. Whenever there is an
  934. * interrupt pending then the flash erase or write operation is suspended,
  935. * array mode restored and interrupts unmasked. Task scheduling might also
  936. * happen at that point. The CPU eventually returns from the interrupt or
  937. * the call to schedule() and the suspended flash operation is resumed for
  938. * the remaining of the delay period.
  939. *
  940. * Warning: this function _will_ fool interrupt latency tracing tools.
  941. */
  942. static int __xipram xip_wait_for_operation(
  943. struct map_info *map, struct flchip *chip,
  944. unsigned long adr, unsigned int chip_op_time_max)
  945. {
  946. struct cfi_private *cfi = map->fldrv_priv;
  947. struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
  948. map_word status, OK = CMD(0x80);
  949. unsigned long usec, suspended, start, done;
  950. flstate_t oldstate, newstate;
  951. start = xip_currtime();
  952. usec = chip_op_time_max;
  953. if (usec == 0)
  954. usec = 500000;
  955. done = 0;
  956. do {
  957. cpu_relax();
  958. if (xip_irqpending() && cfip &&
  959. ((chip->state == FL_ERASING && (cfip->FeatureSupport&2)) ||
  960. (chip->state == FL_WRITING && (cfip->FeatureSupport&4))) &&
  961. (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
  962. /*
  963. * Let's suspend the erase or write operation when
  964. * supported. Note that we currently don't try to
  965. * suspend interleaved chips if there is already
  966. * another operation suspended (imagine what happens
  967. * when one chip was already done with the current
  968. * operation while another chip suspended it, then
  969. * we resume the whole thing at once). Yes, it
  970. * can happen!
  971. */
  972. usec -= done;
  973. map_write(map, CMD(0xb0), adr);
  974. map_write(map, CMD(0x70), adr);
  975. suspended = xip_currtime();
  976. do {
  977. if (xip_elapsed_since(suspended) > 100000) {
  978. /*
  979. * The chip doesn't want to suspend
  980. * after waiting for 100 msecs.
  981. * This is a critical error but there
  982. * is not much we can do here.
  983. */
  984. return -EIO;
  985. }
  986. status = map_read(map, adr);
  987. } while (!map_word_andequal(map, status, OK, OK));
  988. /* Suspend succeeded */
  989. oldstate = chip->state;
  990. if (oldstate == FL_ERASING) {
  991. if (!map_word_bitsset(map, status, CMD(0x40)))
  992. break;
  993. newstate = FL_XIP_WHILE_ERASING;
  994. chip->erase_suspended = 1;
  995. } else {
  996. if (!map_word_bitsset(map, status, CMD(0x04)))
  997. break;
  998. newstate = FL_XIP_WHILE_WRITING;
  999. chip->write_suspended = 1;
  1000. }
  1001. chip->state = newstate;
  1002. map_write(map, CMD(0xff), adr);
  1003. (void) map_read(map, adr);
  1004. xip_iprefetch();
  1005. local_irq_enable();
  1006. spin_unlock(chip->mutex);
  1007. xip_iprefetch();
  1008. cond_resched();
  1009. /*
  1010. * We're back. However someone else might have
  1011. * decided to go write to the chip if we are in
  1012. * a suspended erase state. If so let's wait
  1013. * until it's done.
  1014. */
  1015. spin_lock(chip->mutex);
  1016. while (chip->state != newstate) {
  1017. DECLARE_WAITQUEUE(wait, current);
  1018. set_current_state(TASK_UNINTERRUPTIBLE);
  1019. add_wait_queue(&chip->wq, &wait);
  1020. spin_unlock(chip->mutex);
  1021. schedule();
  1022. remove_wait_queue(&chip->wq, &wait);
  1023. spin_lock(chip->mutex);
  1024. }
  1025. /* Disallow XIP again */
  1026. local_irq_disable();
  1027. /* Resume the write or erase operation */
  1028. map_write(map, CMD(0xd0), adr);
  1029. map_write(map, CMD(0x70), adr);
  1030. chip->state = oldstate;
  1031. start = xip_currtime();
  1032. } else if (usec >= 1000000/HZ) {
  1033. /*
  1034. * Try to save on CPU power when waiting delay
  1035. * is at least a system timer tick period.
  1036. * No need to be extremely accurate here.
  1037. */
  1038. xip_cpu_idle();
  1039. }
  1040. status = map_read(map, adr);
  1041. done = xip_elapsed_since(start);
  1042. } while (!map_word_andequal(map, status, OK, OK)
  1043. && done < usec);
  1044. return (done >= usec) ? -ETIME : 0;
  1045. }
  1046. /*
  1047. * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
  1048. * the flash is actively programming or erasing since we have to poll for
  1049. * the operation to complete anyway. We can't do that in a generic way with
  1050. * a XIP setup so do it before the actual flash operation in this case
  1051. * and stub it out from INVAL_CACHE_AND_WAIT.
  1052. */
  1053. #define XIP_INVAL_CACHED_RANGE(map, from, size) \
  1054. INVALIDATE_CACHED_RANGE(map, from, size)
  1055. #define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, usec, usec_max) \
  1056. xip_wait_for_operation(map, chip, cmd_adr, usec_max)
  1057. #else
  1058. #define xip_disable(map, chip, adr)
  1059. #define xip_enable(map, chip, adr)
  1060. #define XIP_INVAL_CACHED_RANGE(x...)
  1061. #define INVAL_CACHE_AND_WAIT inval_cache_and_wait_for_operation
  1062. static int inval_cache_and_wait_for_operation(
  1063. struct map_info *map, struct flchip *chip,
  1064. unsigned long cmd_adr, unsigned long inval_adr, int inval_len,
  1065. unsigned int chip_op_time, unsigned int chip_op_time_max)
  1066. {
  1067. struct cfi_private *cfi = map->fldrv_priv;
  1068. map_word status, status_OK = CMD(0x80);
  1069. int chip_state = chip->state;
  1070. unsigned int timeo, sleep_time, reset_timeo;
  1071. spin_unlock(chip->mutex);
  1072. if (inval_len)
  1073. INVALIDATE_CACHED_RANGE(map, inval_adr, inval_len);
  1074. spin_lock(chip->mutex);
  1075. timeo = chip_op_time_max;
  1076. if (!timeo)
  1077. timeo = 500000;
  1078. reset_timeo = timeo;
  1079. sleep_time = chip_op_time / 2;
  1080. for (;;) {
  1081. status = map_read(map, cmd_adr);
  1082. if (map_word_andequal(map, status, status_OK, status_OK))
  1083. break;
  1084. if (!timeo) {
  1085. map_write(map, CMD(0x70), cmd_adr);
  1086. chip->state = FL_STATUS;
  1087. return -ETIME;
  1088. }
  1089. /* OK Still waiting. Drop the lock, wait a while and retry. */
  1090. spin_unlock(chip->mutex);
  1091. if (sleep_time >= 1000000/HZ) {
  1092. /*
  1093. * Half of the normal delay still remaining
  1094. * can be performed with a sleeping delay instead
  1095. * of busy waiting.
  1096. */
  1097. msleep(sleep_time/1000);
  1098. timeo -= sleep_time;
  1099. sleep_time = 1000000/HZ;
  1100. } else {
  1101. udelay(1);
  1102. cond_resched();
  1103. timeo--;
  1104. }
  1105. spin_lock(chip->mutex);
  1106. while (chip->state != chip_state) {
  1107. /* Someone's suspended the operation: sleep */
  1108. DECLARE_WAITQUEUE(wait, current);
  1109. set_current_state(TASK_UNINTERRUPTIBLE);
  1110. add_wait_queue(&chip->wq, &wait);
  1111. spin_unlock(chip->mutex);
  1112. schedule();
  1113. remove_wait_queue(&chip->wq, &wait);
  1114. spin_lock(chip->mutex);
  1115. }
  1116. if (chip->erase_suspended && chip_state == FL_ERASING) {
  1117. /* Erase suspend occured while sleep: reset timeout */
  1118. timeo = reset_timeo;
  1119. chip->erase_suspended = 0;
  1120. }
  1121. if (chip->write_suspended && chip_state == FL_WRITING) {
  1122. /* Write suspend occured while sleep: reset timeout */
  1123. timeo = reset_timeo;
  1124. chip->write_suspended = 0;
  1125. }
  1126. }
  1127. /* Done and happy. */
  1128. chip->state = FL_STATUS;
  1129. return 0;
  1130. }
  1131. #endif
  1132. #define WAIT_TIMEOUT(map, chip, adr, udelay, udelay_max) \
  1133. INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, udelay, udelay_max);
  1134. static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
  1135. {
  1136. unsigned long cmd_addr;
  1137. struct cfi_private *cfi = map->fldrv_priv;
  1138. int ret = 0;
  1139. adr += chip->start;
  1140. /* Ensure cmd read/writes are aligned. */
  1141. cmd_addr = adr & ~(map_bankwidth(map)-1);
  1142. spin_lock(chip->mutex);
  1143. ret = get_chip(map, chip, cmd_addr, FL_POINT);
  1144. if (!ret) {
  1145. if (chip->state != FL_POINT && chip->state != FL_READY)
  1146. map_write(map, CMD(0xff), cmd_addr);
  1147. chip->state = FL_POINT;
  1148. chip->ref_point_counter++;
  1149. }
  1150. spin_unlock(chip->mutex);
  1151. return ret;
  1152. }
  1153. static int cfi_intelext_point(struct mtd_info *mtd, loff_t from, size_t len,
  1154. size_t *retlen, void **virt, resource_size_t *phys)
  1155. {
  1156. struct map_info *map = mtd->priv;
  1157. struct cfi_private *cfi = map->fldrv_priv;
  1158. unsigned long ofs, last_end = 0;
  1159. int chipnum;
  1160. int ret = 0;
  1161. if (!map->virt || (from + len > mtd->size))
  1162. return -EINVAL;
  1163. /* Now lock the chip(s) to POINT state */
  1164. /* ofs: offset within the first chip that the first read should start */
  1165. chipnum = (from >> cfi->chipshift);
  1166. ofs = from - (chipnum << cfi->chipshift);
  1167. *virt = map->virt + cfi->chips[chipnum].start + ofs;
  1168. *retlen = 0;
  1169. if (phys)
  1170. *phys = map->phys + cfi->chips[chipnum].start + ofs;
  1171. while (len) {
  1172. unsigned long thislen;
  1173. if (chipnum >= cfi->numchips)
  1174. break;
  1175. /* We cannot point across chips that are virtually disjoint */
  1176. if (!last_end)
  1177. last_end = cfi->chips[chipnum].start;
  1178. else if (cfi->chips[chipnum].start != last_end)
  1179. break;
  1180. if ((len + ofs -1) >> cfi->chipshift)
  1181. thislen = (1<<cfi->chipshift) - ofs;
  1182. else
  1183. thislen = len;
  1184. ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen);
  1185. if (ret)
  1186. break;
  1187. *retlen += thislen;
  1188. len -= thislen;
  1189. ofs = 0;
  1190. last_end += 1 << cfi->chipshift;
  1191. chipnum++;
  1192. }
  1193. return 0;
  1194. }
  1195. static void cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  1196. {
  1197. struct map_info *map = mtd->priv;
  1198. struct cfi_private *cfi = map->fldrv_priv;
  1199. unsigned long ofs;
  1200. int chipnum;
  1201. /* Now unlock the chip(s) POINT state */
  1202. /* ofs: offset within the first chip that the first read should start */
  1203. chipnum = (from >> cfi->chipshift);
  1204. ofs = from - (chipnum << cfi->chipshift);
  1205. while (len) {
  1206. unsigned long thislen;
  1207. struct flchip *chip;
  1208. chip = &cfi->chips[chipnum];
  1209. if (chipnum >= cfi->numchips)
  1210. break;
  1211. if ((len + ofs -1) >> cfi->chipshift)
  1212. thislen = (1<<cfi->chipshift) - ofs;
  1213. else
  1214. thislen = len;
  1215. spin_lock(chip->mutex);
  1216. if (chip->state == FL_POINT) {
  1217. chip->ref_point_counter--;
  1218. if(chip->ref_point_counter == 0)
  1219. chip->state = FL_READY;
  1220. } else
  1221. printk(KERN_ERR "%s: Warning: unpoint called on non pointed region\n", map->name); /* Should this give an error? */
  1222. put_chip(map, chip, chip->start);
  1223. spin_unlock(chip->mutex);
  1224. len -= thislen;
  1225. ofs = 0;
  1226. chipnum++;
  1227. }
  1228. }
  1229. static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
  1230. {
  1231. unsigned long cmd_addr;
  1232. struct cfi_private *cfi = map->fldrv_priv;
  1233. int ret;
  1234. adr += chip->start;
  1235. /* Ensure cmd read/writes are aligned. */
  1236. cmd_addr = adr & ~(map_bankwidth(map)-1);
  1237. spin_lock(chip->mutex);
  1238. ret = get_chip(map, chip, cmd_addr, FL_READY);
  1239. if (ret) {
  1240. spin_unlock(chip->mutex);
  1241. return ret;
  1242. }
  1243. if (chip->state != FL_POINT && chip->state != FL_READY) {
  1244. map_write(map, CMD(0xff), cmd_addr);
  1245. chip->state = FL_READY;
  1246. }
  1247. map_copy_from(map, buf, adr, len);
  1248. put_chip(map, chip, cmd_addr);
  1249. spin_unlock(chip->mutex);
  1250. return 0;
  1251. }
  1252. static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  1253. {
  1254. struct map_info *map = mtd->priv;
  1255. struct cfi_private *cfi = map->fldrv_priv;
  1256. unsigned long ofs;
  1257. int chipnum;
  1258. int ret = 0;
  1259. /* ofs: offset within the first chip that the first read should start */
  1260. chipnum = (from >> cfi->chipshift);
  1261. ofs = from - (chipnum << cfi->chipshift);
  1262. *retlen = 0;
  1263. while (len) {
  1264. unsigned long thislen;
  1265. if (chipnum >= cfi->numchips)
  1266. break;
  1267. if ((len + ofs -1) >> cfi->chipshift)
  1268. thislen = (1<<cfi->chipshift) - ofs;
  1269. else
  1270. thislen = len;
  1271. ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
  1272. if (ret)
  1273. break;
  1274. *retlen += thislen;
  1275. len -= thislen;
  1276. buf += thislen;
  1277. ofs = 0;
  1278. chipnum++;
  1279. }
  1280. return ret;
  1281. }
  1282. static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
  1283. unsigned long adr, map_word datum, int mode)
  1284. {
  1285. struct cfi_private *cfi = map->fldrv_priv;
  1286. map_word status, write_cmd;
  1287. int ret=0;
  1288. adr += chip->start;
  1289. switch (mode) {
  1290. case FL_WRITING:
  1291. write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0x40) : CMD(0x41);
  1292. break;
  1293. case FL_OTP_WRITE:
  1294. write_cmd = CMD(0xc0);
  1295. break;
  1296. default:
  1297. return -EINVAL;
  1298. }
  1299. spin_lock(chip->mutex);
  1300. ret = get_chip(map, chip, adr, mode);
  1301. if (ret) {
  1302. spin_unlock(chip->mutex);
  1303. return ret;
  1304. }
  1305. XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
  1306. ENABLE_VPP(map);
  1307. xip_disable(map, chip, adr);
  1308. map_write(map, write_cmd, adr);
  1309. map_write(map, datum, adr);
  1310. chip->state = mode;
  1311. ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
  1312. adr, map_bankwidth(map),
  1313. chip->word_write_time,
  1314. chip->word_write_time_max);
  1315. if (ret) {
  1316. xip_enable(map, chip, adr);
  1317. printk(KERN_ERR "%s: word write error (status timeout)\n", map->name);
  1318. goto out;
  1319. }
  1320. /* check for errors */
  1321. status = map_read(map, adr);
  1322. if (map_word_bitsset(map, status, CMD(0x1a))) {
  1323. unsigned long chipstatus = MERGESTATUS(status);
  1324. /* reset status */
  1325. map_write(map, CMD(0x50), adr);
  1326. map_write(map, CMD(0x70), adr);
  1327. xip_enable(map, chip, adr);
  1328. if (chipstatus & 0x02) {
  1329. ret = -EROFS;
  1330. } else if (chipstatus & 0x08) {
  1331. printk(KERN_ERR "%s: word write error (bad VPP)\n", map->name);
  1332. ret = -EIO;
  1333. } else {
  1334. printk(KERN_ERR "%s: word write error (status 0x%lx)\n", map->name, chipstatus);
  1335. ret = -EINVAL;
  1336. }
  1337. goto out;
  1338. }
  1339. xip_enable(map, chip, adr);
  1340. out: put_chip(map, chip, adr);
  1341. spin_unlock(chip->mutex);
  1342. return ret;
  1343. }
  1344. static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
  1345. {
  1346. struct map_info *map = mtd->priv;
  1347. struct cfi_private *cfi = map->fldrv_priv;
  1348. int ret = 0;
  1349. int chipnum;
  1350. unsigned long ofs;
  1351. *retlen = 0;
  1352. if (!len)
  1353. return 0;
  1354. chipnum = to >> cfi->chipshift;
  1355. ofs = to - (chipnum << cfi->chipshift);
  1356. /* If it's not bus-aligned, do the first byte write */
  1357. if (ofs & (map_bankwidth(map)-1)) {
  1358. unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
  1359. int gap = ofs - bus_ofs;
  1360. int n;
  1361. map_word datum;
  1362. n = min_t(int, len, map_bankwidth(map)-gap);
  1363. datum = map_word_ff(map);
  1364. datum = map_word_load_partial(map, datum, buf, gap, n);
  1365. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1366. bus_ofs, datum, FL_WRITING);
  1367. if (ret)
  1368. return ret;
  1369. len -= n;
  1370. ofs += n;
  1371. buf += n;
  1372. (*retlen) += n;
  1373. if (ofs >> cfi->chipshift) {
  1374. chipnum ++;
  1375. ofs = 0;
  1376. if (chipnum == cfi->numchips)
  1377. return 0;
  1378. }
  1379. }
  1380. while(len >= map_bankwidth(map)) {
  1381. map_word datum = map_word_load(map, buf);
  1382. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1383. ofs, datum, FL_WRITING);
  1384. if (ret)
  1385. return ret;
  1386. ofs += map_bankwidth(map);
  1387. buf += map_bankwidth(map);
  1388. (*retlen) += map_bankwidth(map);
  1389. len -= map_bankwidth(map);
  1390. if (ofs >> cfi->chipshift) {
  1391. chipnum ++;
  1392. ofs = 0;
  1393. if (chipnum == cfi->numchips)
  1394. return 0;
  1395. }
  1396. }
  1397. if (len & (map_bankwidth(map)-1)) {
  1398. map_word datum;
  1399. datum = map_word_ff(map);
  1400. datum = map_word_load_partial(map, datum, buf, 0, len);
  1401. ret = do_write_oneword(map, &cfi->chips[chipnum],
  1402. ofs, datum, FL_WRITING);
  1403. if (ret)
  1404. return ret;
  1405. (*retlen) += len;
  1406. }
  1407. return 0;
  1408. }
  1409. static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
  1410. unsigned long adr, const struct kvec **pvec,
  1411. unsigned long *pvec_seek, int len)
  1412. {
  1413. struct cfi_private *cfi = map->fldrv_priv;
  1414. map_word status, write_cmd, datum;
  1415. unsigned long cmd_adr;
  1416. int ret, wbufsize, word_gap, words;
  1417. const struct kvec *vec;
  1418. unsigned long vec_seek;
  1419. unsigned long initial_adr;
  1420. int initial_len = len;
  1421. wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1422. adr += chip->start;
  1423. initial_adr = adr;
  1424. cmd_adr = adr & ~(wbufsize-1);
  1425. /* Let's determine this according to the interleave only once */
  1426. write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0xe8) : CMD(0xe9);
  1427. spin_lock(chip->mutex);
  1428. ret = get_chip(map, chip, cmd_adr, FL_WRITING);
  1429. if (ret) {
  1430. spin_unlock(chip->mutex);
  1431. return ret;
  1432. }
  1433. XIP_INVAL_CACHED_RANGE(map, initial_adr, initial_len);
  1434. ENABLE_VPP(map);
  1435. xip_disable(map, chip, cmd_adr);
  1436. /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
  1437. [...], the device will not accept any more Write to Buffer commands".
  1438. So we must check here and reset those bits if they're set. Otherwise
  1439. we're just pissing in the wind */
  1440. if (chip->state != FL_STATUS) {
  1441. map_write(map, CMD(0x70), cmd_adr);
  1442. chip->state = FL_STATUS;
  1443. }
  1444. status = map_read(map, cmd_adr);
  1445. if (map_word_bitsset(map, status, CMD(0x30))) {
  1446. xip_enable(map, chip, cmd_adr);
  1447. printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]);
  1448. xip_disable(map, chip, cmd_adr);
  1449. map_write(map, CMD(0x50), cmd_adr);
  1450. map_write(map, CMD(0x70), cmd_adr);
  1451. }
  1452. chip->state = FL_WRITING_TO_BUFFER;
  1453. map_write(map, write_cmd, cmd_adr);
  1454. ret = WAIT_TIMEOUT(map, chip, cmd_adr, 0, 0);
  1455. if (ret) {
  1456. /* Argh. Not ready for write to buffer */
  1457. map_word Xstatus = map_read(map, cmd_adr);
  1458. map_write(map, CMD(0x70), cmd_adr);
  1459. chip->state = FL_STATUS;
  1460. status = map_read(map, cmd_adr);
  1461. map_write(map, CMD(0x50), cmd_adr);
  1462. map_write(map, CMD(0x70), cmd_adr);
  1463. xip_enable(map, chip, cmd_adr);
  1464. printk(KERN_ERR "%s: Chip not ready for buffer write. Xstatus = %lx, status = %lx\n",
  1465. map->name, Xstatus.x[0], status.x[0]);
  1466. goto out;
  1467. }
  1468. /* Figure out the number of words to write */
  1469. word_gap = (-adr & (map_bankwidth(map)-1));
  1470. words = DIV_ROUND_UP(len - word_gap, map_bankwidth(map));
  1471. if (!word_gap) {
  1472. words--;
  1473. } else {
  1474. word_gap = map_bankwidth(map) - word_gap;
  1475. adr -= word_gap;
  1476. datum = map_word_ff(map);
  1477. }
  1478. /* Write length of data to come */
  1479. map_write(map, CMD(words), cmd_adr );
  1480. /* Write data */
  1481. vec = *pvec;
  1482. vec_seek = *pvec_seek;
  1483. do {
  1484. int n = map_bankwidth(map) - word_gap;
  1485. if (n > vec->iov_len - vec_seek)
  1486. n = vec->iov_len - vec_seek;
  1487. if (n > len)
  1488. n = len;
  1489. if (!word_gap && len < map_bankwidth(map))
  1490. datum = map_word_ff(map);
  1491. datum = map_word_load_partial(map, datum,
  1492. vec->iov_base + vec_seek,
  1493. word_gap, n);
  1494. len -= n;
  1495. word_gap += n;
  1496. if (!len || word_gap == map_bankwidth(map)) {
  1497. map_write(map, datum, adr);
  1498. adr += map_bankwidth(map);
  1499. word_gap = 0;
  1500. }
  1501. vec_seek += n;
  1502. if (vec_seek == vec->iov_len) {
  1503. vec++;
  1504. vec_seek = 0;
  1505. }
  1506. } while (len);
  1507. *pvec = vec;
  1508. *pvec_seek = vec_seek;
  1509. /* GO GO GO */
  1510. map_write(map, CMD(0xd0), cmd_adr);
  1511. chip->state = FL_WRITING;
  1512. ret = INVAL_CACHE_AND_WAIT(map, chip, cmd_adr,
  1513. initial_adr, initial_len,
  1514. chip->buffer_write_time,
  1515. chip->buffer_write_time_max);
  1516. if (ret) {
  1517. map_write(map, CMD(0x70), cmd_adr);
  1518. chip->state = FL_STATUS;
  1519. xip_enable(map, chip, cmd_adr);
  1520. printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name);
  1521. goto out;
  1522. }
  1523. /* check for errors */
  1524. status = map_read(map, cmd_adr);
  1525. if (map_word_bitsset(map, status, CMD(0x1a))) {
  1526. unsigned long chipstatus = MERGESTATUS(status);
  1527. /* reset status */
  1528. map_write(map, CMD(0x50), cmd_adr);
  1529. map_write(map, CMD(0x70), cmd_adr);
  1530. xip_enable(map, chip, cmd_adr);
  1531. if (chipstatus & 0x02) {
  1532. ret = -EROFS;
  1533. } else if (chipstatus & 0x08) {
  1534. printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name);
  1535. ret = -EIO;
  1536. } else {
  1537. printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
  1538. ret = -EINVAL;
  1539. }
  1540. goto out;
  1541. }
  1542. xip_enable(map, chip, cmd_adr);
  1543. out: put_chip(map, chip, cmd_adr);
  1544. spin_unlock(chip->mutex);
  1545. return ret;
  1546. }
  1547. static int cfi_intelext_writev (struct mtd_info *mtd, const struct kvec *vecs,
  1548. unsigned long count, loff_t to, size_t *retlen)
  1549. {
  1550. struct map_info *map = mtd->priv;
  1551. struct cfi_private *cfi = map->fldrv_priv;
  1552. int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
  1553. int ret = 0;
  1554. int chipnum;
  1555. unsigned long ofs, vec_seek, i;
  1556. size_t len = 0;
  1557. for (i = 0; i < count; i++)
  1558. len += vecs[i].iov_len;
  1559. *retlen = 0;
  1560. if (!len)
  1561. return 0;
  1562. chipnum = to >> cfi->chipshift;
  1563. ofs = to - (chipnum << cfi->chipshift);
  1564. vec_seek = 0;
  1565. do {
  1566. /* We must not cross write block boundaries */
  1567. int size = wbufsize - (ofs & (wbufsize-1));
  1568. if (size > len)
  1569. size = len;
  1570. ret = do_write_buffer(map, &cfi->chips[chipnum],
  1571. ofs, &vecs, &vec_seek, size);
  1572. if (ret)
  1573. return ret;
  1574. ofs += size;
  1575. (*retlen) += size;
  1576. len -= size;
  1577. if (ofs >> cfi->chipshift) {
  1578. chipnum ++;
  1579. ofs = 0;
  1580. if (chipnum == cfi->numchips)
  1581. return 0;
  1582. }
  1583. /* Be nice and reschedule with the chip in a usable state for other
  1584. processes. */
  1585. cond_resched();
  1586. } while (len);
  1587. return 0;
  1588. }
  1589. static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to,
  1590. size_t len, size_t *retlen, const u_char *buf)
  1591. {
  1592. struct kvec vec;
  1593. vec.iov_base = (void *) buf;
  1594. vec.iov_len = len;
  1595. return cfi_intelext_writev(mtd, &vec, 1, to, retlen);
  1596. }
  1597. static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip,
  1598. unsigned long adr, int len, void *thunk)
  1599. {
  1600. struct cfi_private *cfi = map->fldrv_priv;
  1601. map_word status;
  1602. int retries = 3;
  1603. int ret;
  1604. adr += chip->start;
  1605. retry:
  1606. spin_lock(chip->mutex);
  1607. ret = get_chip(map, chip, adr, FL_ERASING);
  1608. if (ret) {
  1609. spin_unlock(chip->mutex);
  1610. return ret;
  1611. }
  1612. XIP_INVAL_CACHED_RANGE(map, adr, len);
  1613. ENABLE_VPP(map);
  1614. xip_disable(map, chip, adr);
  1615. /* Clear the status register first */
  1616. map_write(map, CMD(0x50), adr);
  1617. /* Now erase */
  1618. map_write(map, CMD(0x20), adr);
  1619. map_write(map, CMD(0xD0), adr);
  1620. chip->state = FL_ERASING;
  1621. chip->erase_suspended = 0;
  1622. ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
  1623. adr, len,
  1624. chip->erase_time,
  1625. chip->erase_time_max);
  1626. if (ret) {
  1627. map_write(map, CMD(0x70), adr);
  1628. chip->state = FL_STATUS;
  1629. xip_enable(map, chip, adr);
  1630. printk(KERN_ERR "%s: block erase error: (status timeout)\n", map->name);
  1631. goto out;
  1632. }
  1633. /* We've broken this before. It doesn't hurt to be safe */
  1634. map_write(map, CMD(0x70), adr);
  1635. chip->state = FL_STATUS;
  1636. status = map_read(map, adr);
  1637. /* check for errors */
  1638. if (map_word_bitsset(map, status, CMD(0x3a))) {
  1639. unsigned long chipstatus = MERGESTATUS(status);
  1640. /* Reset the error bits */
  1641. map_write(map, CMD(0x50), adr);
  1642. map_write(map, CMD(0x70), adr);
  1643. xip_enable(map, chip, adr);
  1644. if ((chipstatus & 0x30) == 0x30) {
  1645. printk(KERN_ERR "%s: block erase error: (bad command sequence, status 0x%lx)\n", map->name, chipstatus);
  1646. ret = -EINVAL;
  1647. } else if (chipstatus & 0x02) {
  1648. /* Protection bit set */
  1649. ret = -EROFS;
  1650. } else if (chipstatus & 0x8) {
  1651. /* Voltage */
  1652. printk(KERN_ERR "%s: block erase error: (bad VPP)\n", map->name);
  1653. ret = -EIO;
  1654. } else if (chipstatus & 0x20 && retries--) {
  1655. printk(KERN_DEBUG "block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr, chipstatus);
  1656. put_chip(map, chip, adr);
  1657. spin_unlock(chip->mutex);
  1658. goto retry;
  1659. } else {
  1660. printk(KERN_ERR "%s: block erase failed at 0x%08lx (status 0x%lx)\n", map->name, adr, chipstatus);
  1661. ret = -EIO;
  1662. }
  1663. goto out;
  1664. }
  1665. xip_enable(map, chip, adr);
  1666. out: put_chip(map, chip, adr);
  1667. spin_unlock(chip->mutex);
  1668. return ret;
  1669. }
  1670. static int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
  1671. {
  1672. unsigned long ofs, len;
  1673. int ret;
  1674. ofs = instr->addr;
  1675. len = instr->len;
  1676. ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
  1677. if (ret)
  1678. return ret;
  1679. instr->state = MTD_ERASE_DONE;
  1680. mtd_erase_callback(instr);
  1681. return 0;
  1682. }
  1683. static void cfi_intelext_sync (struct mtd_info *mtd)
  1684. {
  1685. struct map_info *map = mtd->priv;
  1686. struct cfi_private *cfi = map->fldrv_priv;
  1687. int i;
  1688. struct flchip *chip;
  1689. int ret = 0;
  1690. for (i=0; !ret && i<cfi->numchips; i++) {
  1691. chip = &cfi->chips[i];
  1692. spin_lock(chip->mutex);
  1693. ret = get_chip(map, chip, chip->start, FL_SYNCING);
  1694. if (!ret) {
  1695. chip->oldstate = chip->state;
  1696. chip->state = FL_SYNCING;
  1697. /* No need to wake_up() on this state change -
  1698. * as the whole point is that nobody can do anything
  1699. * with the chip now anyway.
  1700. */
  1701. }
  1702. spin_unlock(chip->mutex);
  1703. }
  1704. /* Unlock the chips again */
  1705. for (i--; i >=0; i--) {
  1706. chip = &cfi->chips[i];
  1707. spin_lock(chip->mutex);
  1708. if (chip->state == FL_SYNCING) {
  1709. chip->state = chip->oldstate;
  1710. chip->oldstate = FL_READY;
  1711. wake_up(&chip->wq);
  1712. }
  1713. spin_unlock(chip->mutex);
  1714. }
  1715. }
  1716. static int __xipram do_getlockstatus_oneblock(struct map_info *map,
  1717. struct flchip *chip,
  1718. unsigned long adr,
  1719. int len, void *thunk)
  1720. {
  1721. struct cfi_private *cfi = map->fldrv_priv;
  1722. int status, ofs_factor = cfi->interleave * cfi->device_type;
  1723. adr += chip->start;
  1724. xip_disable(map, chip, adr+(2*ofs_factor));
  1725. map_write(map, CMD(0x90), adr+(2*ofs_factor));
  1726. chip->state = FL_JEDEC_QUERY;
  1727. status = cfi_read_query(map, adr+(2*ofs_factor));
  1728. xip_enable(map, chip, 0);
  1729. return status;
  1730. }
  1731. #ifdef DEBUG_LOCK_BITS
  1732. static int __xipram do_printlockstatus_oneblock(struct map_info *map,
  1733. struct flchip *chip,
  1734. unsigned long adr,
  1735. int len, void *thunk)
  1736. {
  1737. printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
  1738. adr, do_getlockstatus_oneblock(map, chip, adr, len, thunk));
  1739. return 0;
  1740. }
  1741. #endif
  1742. #define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
  1743. #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
  1744. static int __xipram do_xxlock_oneblock(struct map_info *map, struct flchip *chip,
  1745. unsigned long adr, int len, void *thunk)
  1746. {
  1747. struct cfi_private *cfi = map->fldrv_priv;
  1748. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  1749. int udelay;
  1750. int ret;
  1751. adr += chip->start;
  1752. spin_lock(chip->mutex);
  1753. ret = get_chip(map, chip, adr, FL_LOCKING);
  1754. if (ret) {
  1755. spin_unlock(chip->mutex);
  1756. return ret;
  1757. }
  1758. ENABLE_VPP(map);
  1759. xip_disable(map, chip, adr);
  1760. map_write(map, CMD(0x60), adr);
  1761. if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
  1762. map_write(map, CMD(0x01), adr);
  1763. chip->state = FL_LOCKING;
  1764. } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
  1765. map_write(map, CMD(0xD0), adr);
  1766. chip->state = FL_UNLOCKING;
  1767. } else
  1768. BUG();
  1769. /*
  1770. * If Instant Individual Block Locking supported then no need
  1771. * to delay.
  1772. */
  1773. udelay = (!extp || !(extp->FeatureSupport & (1 << 5))) ? 1000000/HZ : 0;
  1774. ret = WAIT_TIMEOUT(map, chip, adr, udelay, udelay * 100);
  1775. if (ret) {
  1776. map_write(map, CMD(0x70), adr);
  1777. chip->state = FL_STATUS;
  1778. xip_enable(map, chip, adr);
  1779. printk(KERN_ERR "%s: block unlock error: (status timeout)\n", map->name);
  1780. goto out;
  1781. }
  1782. xip_enable(map, chip, adr);
  1783. out: put_chip(map, chip, adr);
  1784. spin_unlock(chip->mutex);
  1785. return ret;
  1786. }
  1787. static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1788. {
  1789. int ret;
  1790. #ifdef DEBUG_LOCK_BITS
  1791. printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
  1792. __func__, ofs, len);
  1793. cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
  1794. ofs, len, NULL);
  1795. #endif
  1796. ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
  1797. ofs, len, DO_XXLOCK_ONEBLOCK_LOCK);
  1798. #ifdef DEBUG_LOCK_BITS
  1799. printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
  1800. __func__, ret);
  1801. cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
  1802. ofs, len, NULL);
  1803. #endif
  1804. return ret;
  1805. }
  1806. static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1807. {
  1808. int ret;
  1809. #ifdef DEBUG_LOCK_BITS
  1810. printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
  1811. __func__, ofs, len);
  1812. cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
  1813. ofs, len, NULL);
  1814. #endif
  1815. ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
  1816. ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK);
  1817. #ifdef DEBUG_LOCK_BITS
  1818. printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
  1819. __func__, ret);
  1820. cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
  1821. ofs, len, NULL);
  1822. #endif
  1823. return ret;
  1824. }
  1825. #ifdef CONFIG_MTD_OTP
  1826. typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
  1827. u_long data_offset, u_char *buf, u_int size,
  1828. u_long prot_offset, u_int groupno, u_int groupsize);
  1829. static int __xipram
  1830. do_otp_read(struct map_info *map, struct flchip *chip, u_long offset,
  1831. u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
  1832. {
  1833. struct cfi_private *cfi = map->fldrv_priv;
  1834. int ret;
  1835. spin_lock(chip->mutex);
  1836. ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY);
  1837. if (ret) {
  1838. spin_unlock(chip->mutex);
  1839. return ret;
  1840. }
  1841. /* let's ensure we're not reading back cached data from array mode */
  1842. INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
  1843. xip_disable(map, chip, chip->start);
  1844. if (chip->state != FL_JEDEC_QUERY) {
  1845. map_write(map, CMD(0x90), chip->start);
  1846. chip->state = FL_JEDEC_QUERY;
  1847. }
  1848. map_copy_from(map, buf, chip->start + offset, size);
  1849. xip_enable(map, chip, chip->start);
  1850. /* then ensure we don't keep OTP data in the cache */
  1851. INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
  1852. put_chip(map, chip, chip->start);
  1853. spin_unlock(chip->mutex);
  1854. return 0;
  1855. }
  1856. static int
  1857. do_otp_write(struct map_info *map, struct flchip *chip, u_long offset,
  1858. u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
  1859. {
  1860. int ret;
  1861. while (size) {
  1862. unsigned long bus_ofs = offset & ~(map_bankwidth(map)-1);
  1863. int gap = offset - bus_ofs;
  1864. int n = min_t(int, size, map_bankwidth(map)-gap);
  1865. map_word datum = map_word_ff(map);
  1866. datum = map_word_load_partial(map, datum, buf, gap, n);
  1867. ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
  1868. if (ret)
  1869. return ret;
  1870. offset += n;
  1871. buf += n;
  1872. size -= n;
  1873. }
  1874. return 0;
  1875. }
  1876. static int
  1877. do_otp_lock(struct map_info *map, struct flchip *chip, u_long offset,
  1878. u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
  1879. {
  1880. struct cfi_private *cfi = map->fldrv_priv;
  1881. map_word datum;
  1882. /* make sure area matches group boundaries */
  1883. if (size != grpsz)
  1884. return -EXDEV;
  1885. datum = map_word_ff(map);
  1886. datum = map_word_clr(map, datum, CMD(1 << grpno));
  1887. return do_write_oneword(map, chip, prot, datum, FL_OTP_WRITE);
  1888. }
  1889. static int cfi_intelext_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1890. size_t *retlen, u_char *buf,
  1891. otp_op_t action, int user_regs)
  1892. {
  1893. struct map_info *map = mtd->priv;
  1894. struct cfi_private *cfi = map->fldrv_priv;
  1895. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  1896. struct flchip *chip;
  1897. struct cfi_intelext_otpinfo *otp;
  1898. u_long devsize, reg_prot_offset, data_offset;
  1899. u_int chip_num, chip_step, field, reg_fact_size, reg_user_size;
  1900. u_int groups, groupno, groupsize, reg_fact_groups, reg_user_groups;
  1901. int ret;
  1902. *retlen = 0;
  1903. /* Check that we actually have some OTP registers */
  1904. if (!extp || !(extp->FeatureSupport & 64) || !extp->NumProtectionFields)
  1905. return -ENODATA;
  1906. /* we need real chips here not virtual ones */
  1907. devsize = (1 << cfi->cfiq->DevSize) * cfi->interleave;
  1908. chip_step = devsize >> cfi->chipshift;
  1909. chip_num = 0;
  1910. /* Some chips have OTP located in the _top_ partition only.
  1911. For example: Intel 28F256L18T (T means top-parameter device) */
  1912. if (cfi->mfr == CFI_MFR_INTEL) {
  1913. switch (cfi->id) {
  1914. case 0x880b:
  1915. case 0x880c:
  1916. case 0x880d:
  1917. chip_num = chip_step - 1;
  1918. }
  1919. }
  1920. for ( ; chip_num < cfi->numchips; chip_num += chip_step) {
  1921. chip = &cfi->chips[chip_num];
  1922. otp = (struct cfi_intelext_otpinfo *)&extp->extra[0];
  1923. /* first OTP region */
  1924. field = 0;
  1925. reg_prot_offset = extp->ProtRegAddr;
  1926. reg_fact_groups = 1;
  1927. reg_fact_size = 1 << extp->FactProtRegSize;
  1928. reg_user_groups = 1;
  1929. reg_user_size = 1 << extp->UserProtRegSize;
  1930. while (len > 0) {
  1931. /* flash geometry fixup */
  1932. data_offset = reg_prot_offset + 1;
  1933. data_offset *= cfi->interleave * cfi->device_type;
  1934. reg_prot_offset *= cfi->interleave * cfi->device_type;
  1935. reg_fact_size *= cfi->interleave;
  1936. reg_user_size *= cfi->interleave;
  1937. if (user_regs) {
  1938. groups = reg_user_groups;
  1939. groupsize = reg_user_size;
  1940. /* skip over factory reg area */
  1941. groupno = reg_fact_groups;
  1942. data_offset += reg_fact_groups * reg_fact_size;
  1943. } else {
  1944. groups = reg_fact_groups;
  1945. groupsize = reg_fact_size;
  1946. groupno = 0;
  1947. }
  1948. while (len > 0 && groups > 0) {
  1949. if (!action) {
  1950. /*
  1951. * Special case: if action is NULL
  1952. * we fill buf with otp_info records.
  1953. */
  1954. struct otp_info *otpinfo;
  1955. map_word lockword;
  1956. len -= sizeof(struct otp_info);
  1957. if (len <= 0)
  1958. return -ENOSPC;
  1959. ret = do_otp_read(map, chip,
  1960. reg_prot_offset,
  1961. (u_char *)&lockword,
  1962. map_bankwidth(map),
  1963. 0, 0, 0);
  1964. if (ret)
  1965. return ret;
  1966. otpinfo = (struct otp_info *)buf;
  1967. otpinfo->start = from;
  1968. otpinfo->length = groupsize;
  1969. otpinfo->locked =
  1970. !map_word_bitsset(map, lockword,
  1971. CMD(1 << groupno));
  1972. from += groupsize;
  1973. buf += sizeof(*otpinfo);
  1974. *retlen += sizeof(*otpinfo);
  1975. } else if (from >= groupsize) {
  1976. from -= groupsize;
  1977. data_offset += groupsize;
  1978. } else {
  1979. int size = groupsize;
  1980. data_offset += from;
  1981. size -= from;
  1982. from = 0;
  1983. if (size > len)
  1984. size = len;
  1985. ret = action(map, chip, data_offset,
  1986. buf, size, reg_prot_offset,
  1987. groupno, groupsize);
  1988. if (ret < 0)
  1989. return ret;
  1990. buf += size;
  1991. len -= size;
  1992. *retlen += size;
  1993. data_offset += size;
  1994. }
  1995. groupno++;
  1996. groups--;
  1997. }
  1998. /* next OTP region */
  1999. if (++field == extp->NumProtectionFields)
  2000. break;
  2001. reg_prot_offset = otp->ProtRegAddr;
  2002. reg_fact_groups = otp->FactGroups;
  2003. reg_fact_size = 1 << otp->FactProtRegSize;
  2004. reg_user_groups = otp->UserGroups;
  2005. reg_user_size = 1 << otp->UserProtRegSize;
  2006. otp++;
  2007. }
  2008. }
  2009. return 0;
  2010. }
  2011. static int cfi_intelext_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  2012. size_t len, size_t *retlen,
  2013. u_char *buf)
  2014. {
  2015. return cfi_intelext_otp_walk(mtd, from, len, retlen,
  2016. buf, do_otp_read, 0);
  2017. }
  2018. static int cfi_intelext_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2019. size_t len, size_t *retlen,
  2020. u_char *buf)
  2021. {
  2022. return cfi_intelext_otp_walk(mtd, from, len, retlen,
  2023. buf, do_otp_read, 1);
  2024. }
  2025. static int cfi_intelext_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2026. size_t len, size_t *retlen,
  2027. u_char *buf)
  2028. {
  2029. return cfi_intelext_otp_walk(mtd, from, len, retlen,
  2030. buf, do_otp_write, 1);
  2031. }
  2032. static int cfi_intelext_lock_user_prot_reg(struct mtd_info *mtd,
  2033. loff_t from, size_t len)
  2034. {
  2035. size_t retlen;
  2036. return cfi_intelext_otp_walk(mtd, from, len, &retlen,
  2037. NULL, do_otp_lock, 1);
  2038. }
  2039. static int cfi_intelext_get_fact_prot_info(struct mtd_info *mtd,
  2040. struct otp_info *buf, size_t len)
  2041. {
  2042. size_t retlen;
  2043. int ret;
  2044. ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 0);
  2045. return ret ? : retlen;
  2046. }
  2047. static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd,
  2048. struct otp_info *buf, size_t len)
  2049. {
  2050. size_t retlen;
  2051. int ret;
  2052. ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 1);
  2053. return ret ? : retlen;
  2054. }
  2055. #endif
  2056. static void cfi_intelext_save_locks(struct mtd_info *mtd)
  2057. {
  2058. struct mtd_erase_region_info *region;
  2059. int block, status, i;
  2060. unsigned long adr;
  2061. size_t len;
  2062. for (i = 0; i < mtd->numeraseregions; i++) {
  2063. region = &mtd->eraseregions[i];
  2064. if (!region->lockmap)
  2065. continue;
  2066. for (block = 0; block < region->numblocks; block++){
  2067. len = region->erasesize;
  2068. adr = region->offset + block * len;
  2069. status = cfi_varsize_frob(mtd,
  2070. do_getlockstatus_oneblock, adr, len, NULL);
  2071. if (status)
  2072. set_bit(block, region->lockmap);
  2073. else
  2074. clear_bit(block, region->lockmap);
  2075. }
  2076. }
  2077. }
  2078. static int cfi_intelext_suspend(struct mtd_info *mtd)
  2079. {
  2080. struct map_info *map = mtd->priv;
  2081. struct cfi_private *cfi = map->fldrv_priv;
  2082. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  2083. int i;
  2084. struct flchip *chip;
  2085. int ret = 0;
  2086. if ((mtd->flags & MTD_POWERUP_LOCK)
  2087. && extp && (extp->FeatureSupport & (1 << 5)))
  2088. cfi_intelext_save_locks(mtd);
  2089. for (i=0; !ret && i<cfi->numchips; i++) {
  2090. chip = &cfi->chips[i];
  2091. spin_lock(chip->mutex);
  2092. switch (chip->state) {
  2093. case FL_READY:
  2094. case FL_STATUS:
  2095. case FL_CFI_QUERY:
  2096. case FL_JEDEC_QUERY:
  2097. if (chip->oldstate == FL_READY) {
  2098. /* place the chip in a known state before suspend */
  2099. map_write(map, CMD(0xFF), cfi->chips[i].start);
  2100. chip->oldstate = chip->state;
  2101. chip->state = FL_PM_SUSPENDED;
  2102. /* No need to wake_up() on this state change -
  2103. * as the whole point is that nobody can do anything
  2104. * with the chip now anyway.
  2105. */
  2106. } else {
  2107. /* There seems to be an operation pending. We must wait for it. */
  2108. printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate);
  2109. ret = -EAGAIN;
  2110. }
  2111. break;
  2112. default:
  2113. /* Should we actually wait? Once upon a time these routines weren't
  2114. allowed to. Or should we return -EAGAIN, because the upper layers
  2115. ought to have already shut down anything which was using the device
  2116. anyway? The latter for now. */
  2117. printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->oldstate);
  2118. ret = -EAGAIN;
  2119. case FL_PM_SUSPENDED:
  2120. break;
  2121. }
  2122. spin_unlock(chip->mutex);
  2123. }
  2124. /* Unlock the chips again */
  2125. if (ret) {
  2126. for (i--; i >=0; i--) {
  2127. chip = &cfi->chips[i];
  2128. spin_lock(chip->mutex);
  2129. if (chip->state == FL_PM_SUSPENDED) {
  2130. /* No need to force it into a known state here,
  2131. because we're returning failure, and it didn't
  2132. get power cycled */
  2133. chip->state = chip->oldstate;
  2134. chip->oldstate = FL_READY;
  2135. wake_up(&chip->wq);
  2136. }
  2137. spin_unlock(chip->mutex);
  2138. }
  2139. }
  2140. return ret;
  2141. }
  2142. static void cfi_intelext_restore_locks(struct mtd_info *mtd)
  2143. {
  2144. struct mtd_erase_region_info *region;
  2145. int block, i;
  2146. unsigned long adr;
  2147. size_t len;
  2148. for (i = 0; i < mtd->numeraseregions; i++) {
  2149. region = &mtd->eraseregions[i];
  2150. if (!region->lockmap)
  2151. continue;
  2152. for (block = 0; block < region->numblocks; block++) {
  2153. len = region->erasesize;
  2154. adr = region->offset + block * len;
  2155. if (!test_bit(block, region->lockmap))
  2156. cfi_intelext_unlock(mtd, adr, len);
  2157. }
  2158. }
  2159. }
  2160. static void cfi_intelext_resume(struct mtd_info *mtd)
  2161. {
  2162. struct map_info *map = mtd->priv;
  2163. struct cfi_private *cfi = map->fldrv_priv;
  2164. struct cfi_pri_intelext *extp = cfi->cmdset_priv;
  2165. int i;
  2166. struct flchip *chip;
  2167. for (i=0; i<cfi->numchips; i++) {
  2168. chip = &cfi->chips[i];
  2169. spin_lock(chip->mutex);
  2170. /* Go to known state. Chip may have been power cycled */
  2171. if (chip->state == FL_PM_SUSPENDED) {
  2172. map_write(map, CMD(0xFF), cfi->chips[i].start);
  2173. chip->oldstate = chip->state = FL_READY;
  2174. wake_up(&chip->wq);
  2175. }
  2176. spin_unlock(chip->mutex);
  2177. }
  2178. if ((mtd->flags & MTD_POWERUP_LOCK)
  2179. && extp && (extp->FeatureSupport & (1 << 5)))
  2180. cfi_intelext_restore_locks(mtd);
  2181. }
  2182. static int cfi_intelext_reset(struct mtd_info *mtd)
  2183. {
  2184. struct map_info *map = mtd->priv;
  2185. struct cfi_private *cfi = map->fldrv_priv;
  2186. int i, ret;
  2187. for (i=0; i < cfi->numchips; i++) {
  2188. struct flchip *chip = &cfi->chips[i];
  2189. /* force the completion of any ongoing operation
  2190. and switch to array mode so any bootloader in
  2191. flash is accessible for soft reboot. */
  2192. spin_lock(chip->mutex);
  2193. ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
  2194. if (!ret) {
  2195. map_write(map, CMD(0xff), chip->start);
  2196. chip->state = FL_SHUTDOWN;
  2197. put_chip(map, chip, chip->start);
  2198. }
  2199. spin_unlock(chip->mutex);
  2200. }
  2201. return 0;
  2202. }
  2203. static int cfi_intelext_reboot(struct notifier_block *nb, unsigned long val,
  2204. void *v)
  2205. {
  2206. struct mtd_info *mtd;
  2207. mtd = container_of(nb, struct mtd_info, reboot_notifier);
  2208. cfi_intelext_reset(mtd);
  2209. return NOTIFY_DONE;
  2210. }
  2211. static void cfi_intelext_destroy(struct mtd_info *mtd)
  2212. {
  2213. struct map_info *map = mtd->priv;
  2214. struct cfi_private *cfi = map->fldrv_priv;
  2215. struct mtd_erase_region_info *region;
  2216. int i;
  2217. cfi_intelext_reset(mtd);
  2218. unregister_reboot_notifier(&mtd->reboot_notifier);
  2219. kfree(cfi->cmdset_priv);
  2220. kfree(cfi->cfiq);
  2221. kfree(cfi->chips[0].priv);
  2222. kfree(cfi);
  2223. for (i = 0; i < mtd->numeraseregions; i++) {
  2224. region = &mtd->eraseregions[i];
  2225. if (region->lockmap)
  2226. kfree(region->lockmap);
  2227. }
  2228. kfree(mtd->eraseregions);
  2229. }
  2230. MODULE_LICENSE("GPL");
  2231. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
  2232. MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");
  2233. MODULE_ALIAS("cfi_cmdset_0003");
  2234. MODULE_ALIAS("cfi_cmdset_0200");