omap-gpmc.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  1. /*
  2. * GPMC support functions
  3. *
  4. * Copyright (C) 2005-2006 Nokia Corporation
  5. *
  6. * Author: Juha Yrjola
  7. *
  8. * Copyright (C) 2009 Texas Instruments
  9. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/irq.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/ioport.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/of.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_mtd.h>
  29. #include <linux/of_device.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/omap-gpmc.h>
  32. #include <linux/mtd/nand.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/platform_data/mtd-nand-omap2.h>
  35. #include <linux/platform_data/mtd-onenand-omap2.h>
  36. #include <asm/mach-types.h>
  37. #define DEVICE_NAME "omap-gpmc"
  38. /* GPMC register offsets */
  39. #define GPMC_REVISION 0x00
  40. #define GPMC_SYSCONFIG 0x10
  41. #define GPMC_SYSSTATUS 0x14
  42. #define GPMC_IRQSTATUS 0x18
  43. #define GPMC_IRQENABLE 0x1c
  44. #define GPMC_TIMEOUT_CONTROL 0x40
  45. #define GPMC_ERR_ADDRESS 0x44
  46. #define GPMC_ERR_TYPE 0x48
  47. #define GPMC_CONFIG 0x50
  48. #define GPMC_STATUS 0x54
  49. #define GPMC_PREFETCH_CONFIG1 0x1e0
  50. #define GPMC_PREFETCH_CONFIG2 0x1e4
  51. #define GPMC_PREFETCH_CONTROL 0x1ec
  52. #define GPMC_PREFETCH_STATUS 0x1f0
  53. #define GPMC_ECC_CONFIG 0x1f4
  54. #define GPMC_ECC_CONTROL 0x1f8
  55. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  56. #define GPMC_ECC1_RESULT 0x200
  57. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  58. #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
  59. #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
  60. #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
  61. #define GPMC_ECC_BCH_RESULT_4 0x300 /* not available on OMAP2 */
  62. #define GPMC_ECC_BCH_RESULT_5 0x304 /* not available on OMAP2 */
  63. #define GPMC_ECC_BCH_RESULT_6 0x308 /* not available on OMAP2 */
  64. /* GPMC ECC control settings */
  65. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  66. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  67. #define GPMC_ECC_CTRL_ECCREG1 0x001
  68. #define GPMC_ECC_CTRL_ECCREG2 0x002
  69. #define GPMC_ECC_CTRL_ECCREG3 0x003
  70. #define GPMC_ECC_CTRL_ECCREG4 0x004
  71. #define GPMC_ECC_CTRL_ECCREG5 0x005
  72. #define GPMC_ECC_CTRL_ECCREG6 0x006
  73. #define GPMC_ECC_CTRL_ECCREG7 0x007
  74. #define GPMC_ECC_CTRL_ECCREG8 0x008
  75. #define GPMC_ECC_CTRL_ECCREG9 0x009
  76. #define GPMC_CONFIG_LIMITEDADDRESS BIT(1)
  77. #define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
  78. #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
  79. #define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
  80. #define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
  81. #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
  82. #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
  83. #define GPMC_CS0_OFFSET 0x60
  84. #define GPMC_CS_SIZE 0x30
  85. #define GPMC_BCH_SIZE 0x10
  86. #define GPMC_MEM_END 0x3FFFFFFF
  87. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  88. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  89. #define CS_NUM_SHIFT 24
  90. #define ENABLE_PREFETCH (0x1 << 7)
  91. #define DMA_MPU_MODE 2
  92. #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
  93. #define GPMC_REVISION_MINOR(l) (l & 0xf)
  94. #define GPMC_HAS_WR_ACCESS 0x1
  95. #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
  96. #define GPMC_HAS_MUX_AAD 0x4
  97. #define GPMC_NR_WAITPINS 4
  98. #define GPMC_CS_CONFIG1 0x00
  99. #define GPMC_CS_CONFIG2 0x04
  100. #define GPMC_CS_CONFIG3 0x08
  101. #define GPMC_CS_CONFIG4 0x0c
  102. #define GPMC_CS_CONFIG5 0x10
  103. #define GPMC_CS_CONFIG6 0x14
  104. #define GPMC_CS_CONFIG7 0x18
  105. #define GPMC_CS_NAND_COMMAND 0x1c
  106. #define GPMC_CS_NAND_ADDRESS 0x20
  107. #define GPMC_CS_NAND_DATA 0x24
  108. /* Control Commands */
  109. #define GPMC_CONFIG_RDY_BSY 0x00000001
  110. #define GPMC_CONFIG_DEV_SIZE 0x00000002
  111. #define GPMC_CONFIG_DEV_TYPE 0x00000003
  112. #define GPMC_SET_IRQ_STATUS 0x00000004
  113. #define GPMC_CONFIG1_WRAPBURST_SUPP (1 << 31)
  114. #define GPMC_CONFIG1_READMULTIPLE_SUPP (1 << 30)
  115. #define GPMC_CONFIG1_READTYPE_ASYNC (0 << 29)
  116. #define GPMC_CONFIG1_READTYPE_SYNC (1 << 29)
  117. #define GPMC_CONFIG1_WRITEMULTIPLE_SUPP (1 << 28)
  118. #define GPMC_CONFIG1_WRITETYPE_ASYNC (0 << 27)
  119. #define GPMC_CONFIG1_WRITETYPE_SYNC (1 << 27)
  120. #define GPMC_CONFIG1_CLKACTIVATIONTIME(val) ((val & 3) << 25)
  121. /** CLKACTIVATIONTIME Max Ticks */
  122. #define GPMC_CONFIG1_CLKACTIVATIONTIME_MAX 2
  123. #define GPMC_CONFIG1_PAGE_LEN(val) ((val & 3) << 23)
  124. /** ATTACHEDDEVICEPAGELENGTH Max Value */
  125. #define GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX 2
  126. #define GPMC_CONFIG1_WAIT_READ_MON (1 << 22)
  127. #define GPMC_CONFIG1_WAIT_WRITE_MON (1 << 21)
  128. #define GPMC_CONFIG1_WAIT_MON_TIME(val) ((val & 3) << 18)
  129. /** WAITMONITORINGTIME Max Ticks */
  130. #define GPMC_CONFIG1_WAITMONITORINGTIME_MAX 2
  131. #define GPMC_CONFIG1_WAIT_PIN_SEL(val) ((val & 3) << 16)
  132. #define GPMC_CONFIG1_DEVICESIZE(val) ((val & 3) << 12)
  133. #define GPMC_CONFIG1_DEVICESIZE_16 GPMC_CONFIG1_DEVICESIZE(1)
  134. /** DEVICESIZE Max Value */
  135. #define GPMC_CONFIG1_DEVICESIZE_MAX 1
  136. #define GPMC_CONFIG1_DEVICETYPE(val) ((val & 3) << 10)
  137. #define GPMC_CONFIG1_DEVICETYPE_NOR GPMC_CONFIG1_DEVICETYPE(0)
  138. #define GPMC_CONFIG1_MUXTYPE(val) ((val & 3) << 8)
  139. #define GPMC_CONFIG1_TIME_PARA_GRAN (1 << 4)
  140. #define GPMC_CONFIG1_FCLK_DIV(val) (val & 3)
  141. #define GPMC_CONFIG1_FCLK_DIV2 (GPMC_CONFIG1_FCLK_DIV(1))
  142. #define GPMC_CONFIG1_FCLK_DIV3 (GPMC_CONFIG1_FCLK_DIV(2))
  143. #define GPMC_CONFIG1_FCLK_DIV4 (GPMC_CONFIG1_FCLK_DIV(3))
  144. #define GPMC_CONFIG7_CSVALID (1 << 6)
  145. #define GPMC_CONFIG7_BASEADDRESS_MASK 0x3f
  146. #define GPMC_CONFIG7_CSVALID_MASK BIT(6)
  147. #define GPMC_CONFIG7_MASKADDRESS_OFFSET 8
  148. #define GPMC_CONFIG7_MASKADDRESS_MASK (0xf << GPMC_CONFIG7_MASKADDRESS_OFFSET)
  149. /* All CONFIG7 bits except reserved bits */
  150. #define GPMC_CONFIG7_MASK (GPMC_CONFIG7_BASEADDRESS_MASK | \
  151. GPMC_CONFIG7_CSVALID_MASK | \
  152. GPMC_CONFIG7_MASKADDRESS_MASK)
  153. #define GPMC_DEVICETYPE_NOR 0
  154. #define GPMC_DEVICETYPE_NAND 2
  155. #define GPMC_CONFIG_WRITEPROTECT 0x00000010
  156. #define WR_RD_PIN_MONITORING 0x00600000
  157. #define GPMC_ENABLE_IRQ 0x0000000d
  158. /* ECC commands */
  159. #define GPMC_ECC_READ 0 /* Reset Hardware ECC for read */
  160. #define GPMC_ECC_WRITE 1 /* Reset Hardware ECC for write */
  161. #define GPMC_ECC_READSYN 2 /* Reset before syndrom is read back */
  162. /* XXX: Only NAND irq has been considered,currently these are the only ones used
  163. */
  164. #define GPMC_NR_IRQ 2
  165. enum gpmc_clk_domain {
  166. GPMC_CD_FCLK,
  167. GPMC_CD_CLK
  168. };
  169. struct gpmc_cs_data {
  170. const char *name;
  171. #define GPMC_CS_RESERVED (1 << 0)
  172. u32 flags;
  173. struct resource mem;
  174. };
  175. struct gpmc_client_irq {
  176. unsigned irq;
  177. u32 bitmask;
  178. };
  179. /* Structure to save gpmc cs context */
  180. struct gpmc_cs_config {
  181. u32 config1;
  182. u32 config2;
  183. u32 config3;
  184. u32 config4;
  185. u32 config5;
  186. u32 config6;
  187. u32 config7;
  188. int is_valid;
  189. };
  190. /*
  191. * Structure to save/restore gpmc context
  192. * to support core off on OMAP3
  193. */
  194. struct omap3_gpmc_regs {
  195. u32 sysconfig;
  196. u32 irqenable;
  197. u32 timeout_ctrl;
  198. u32 config;
  199. u32 prefetch_config1;
  200. u32 prefetch_config2;
  201. u32 prefetch_control;
  202. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  203. };
  204. static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
  205. static struct irq_chip gpmc_irq_chip;
  206. static int gpmc_irq_start;
  207. static struct resource gpmc_mem_root;
  208. static struct gpmc_cs_data gpmc_cs[GPMC_CS_NUM];
  209. static DEFINE_SPINLOCK(gpmc_mem_lock);
  210. /* Define chip-selects as reserved by default until probe completes */
  211. static unsigned int gpmc_cs_num = GPMC_CS_NUM;
  212. static unsigned int gpmc_nr_waitpins;
  213. static struct device *gpmc_dev;
  214. static int gpmc_irq;
  215. static resource_size_t phys_base, mem_size;
  216. static unsigned gpmc_capability;
  217. static void __iomem *gpmc_base;
  218. static struct clk *gpmc_l3_clk;
  219. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  220. static void gpmc_write_reg(int idx, u32 val)
  221. {
  222. writel_relaxed(val, gpmc_base + idx);
  223. }
  224. static u32 gpmc_read_reg(int idx)
  225. {
  226. return readl_relaxed(gpmc_base + idx);
  227. }
  228. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  229. {
  230. void __iomem *reg_addr;
  231. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  232. writel_relaxed(val, reg_addr);
  233. }
  234. static u32 gpmc_cs_read_reg(int cs, int idx)
  235. {
  236. void __iomem *reg_addr;
  237. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  238. return readl_relaxed(reg_addr);
  239. }
  240. /* TODO: Add support for gpmc_fck to clock framework and use it */
  241. static unsigned long gpmc_get_fclk_period(void)
  242. {
  243. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  244. rate /= 1000;
  245. rate = 1000000000 / rate; /* In picoseconds */
  246. return rate;
  247. }
  248. /**
  249. * gpmc_get_clk_period - get period of selected clock domain in ps
  250. * @cs Chip Select Region.
  251. * @cd Clock Domain.
  252. *
  253. * GPMC_CS_CONFIG1 GPMCFCLKDIVIDER for cs has to be setup
  254. * prior to calling this function with GPMC_CD_CLK.
  255. */
  256. static unsigned long gpmc_get_clk_period(int cs, enum gpmc_clk_domain cd)
  257. {
  258. unsigned long tick_ps = gpmc_get_fclk_period();
  259. u32 l;
  260. int div;
  261. switch (cd) {
  262. case GPMC_CD_CLK:
  263. /* get current clk divider */
  264. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  265. div = (l & 0x03) + 1;
  266. /* get GPMC_CLK period */
  267. tick_ps *= div;
  268. break;
  269. case GPMC_CD_FCLK:
  270. /* FALL-THROUGH */
  271. default:
  272. break;
  273. }
  274. return tick_ps;
  275. }
  276. static unsigned int gpmc_ns_to_clk_ticks(unsigned int time_ns, int cs,
  277. enum gpmc_clk_domain cd)
  278. {
  279. unsigned long tick_ps;
  280. /* Calculate in picosecs to yield more exact results */
  281. tick_ps = gpmc_get_clk_period(cs, cd);
  282. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  283. }
  284. static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  285. {
  286. return gpmc_ns_to_clk_ticks(time_ns, /* any CS */ 0, GPMC_CD_FCLK);
  287. }
  288. static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  289. {
  290. unsigned long tick_ps;
  291. /* Calculate in picosecs to yield more exact results */
  292. tick_ps = gpmc_get_fclk_period();
  293. return (time_ps + tick_ps - 1) / tick_ps;
  294. }
  295. unsigned int gpmc_clk_ticks_to_ns(unsigned ticks, int cs,
  296. enum gpmc_clk_domain cd)
  297. {
  298. return ticks * gpmc_get_clk_period(cs, cd) / 1000;
  299. }
  300. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  301. {
  302. return gpmc_clk_ticks_to_ns(ticks, /* any CS */ 0, GPMC_CD_FCLK);
  303. }
  304. static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
  305. {
  306. return ticks * gpmc_get_fclk_period();
  307. }
  308. static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
  309. {
  310. unsigned long ticks = gpmc_ps_to_ticks(time_ps);
  311. return ticks * gpmc_get_fclk_period();
  312. }
  313. static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
  314. {
  315. u32 l;
  316. l = gpmc_cs_read_reg(cs, reg);
  317. if (value)
  318. l |= mask;
  319. else
  320. l &= ~mask;
  321. gpmc_cs_write_reg(cs, reg, l);
  322. }
  323. static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
  324. {
  325. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
  326. GPMC_CONFIG1_TIME_PARA_GRAN,
  327. p->time_para_granularity);
  328. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
  329. GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
  330. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
  331. GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
  332. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  333. GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
  334. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  335. GPMC_CONFIG4_WEEXTRADELAY, p->we_extra_delay);
  336. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  337. GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
  338. p->cycle2cyclesamecsen);
  339. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  340. GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
  341. p->cycle2cyclediffcsen);
  342. }
  343. #ifdef CONFIG_OMAP_GPMC_DEBUG
  344. /**
  345. * get_gpmc_timing_reg - read a timing parameter and print DTS settings for it.
  346. * @cs: Chip Select Region
  347. * @reg: GPMC_CS_CONFIGn register offset.
  348. * @st_bit: Start Bit
  349. * @end_bit: End Bit. Must be >= @st_bit.
  350. * @ma:x Maximum parameter value (before optional @shift).
  351. * If 0, maximum is as high as @st_bit and @end_bit allow.
  352. * @name: DTS node name, w/o "gpmc,"
  353. * @cd: Clock Domain of timing parameter.
  354. * @shift: Parameter value left shifts @shift, which is then printed instead of value.
  355. * @raw: Raw Format Option.
  356. * raw format: gpmc,name = <value>
  357. * tick format: gpmc,name = <value> /&zwj;* x ns -- y ns; x ticks *&zwj;/
  358. * Where x ns -- y ns result in the same tick value.
  359. * When @max is exceeded, "invalid" is printed inside comment.
  360. * @noval: Parameter values equal to 0 are not printed.
  361. * @return: Specified timing parameter (after optional @shift).
  362. *
  363. */
  364. static int get_gpmc_timing_reg(
  365. /* timing specifiers */
  366. int cs, int reg, int st_bit, int end_bit, int max,
  367. const char *name, const enum gpmc_clk_domain cd,
  368. /* value transform */
  369. int shift,
  370. /* format specifiers */
  371. bool raw, bool noval)
  372. {
  373. u32 l;
  374. int nr_bits;
  375. int mask;
  376. bool invalid;
  377. l = gpmc_cs_read_reg(cs, reg);
  378. nr_bits = end_bit - st_bit + 1;
  379. mask = (1 << nr_bits) - 1;
  380. l = (l >> st_bit) & mask;
  381. if (!max)
  382. max = mask;
  383. invalid = l > max;
  384. if (shift)
  385. l = (shift << l);
  386. if (noval && (l == 0))
  387. return 0;
  388. if (!raw) {
  389. /* DTS tick format for timings in ns */
  390. unsigned int time_ns;
  391. unsigned int time_ns_min = 0;
  392. if (l)
  393. time_ns_min = gpmc_clk_ticks_to_ns(l - 1, cs, cd) + 1;
  394. time_ns = gpmc_clk_ticks_to_ns(l, cs, cd);
  395. pr_info("gpmc,%s = <%u> /* %u ns - %u ns; %i ticks%s*/\n",
  396. name, time_ns, time_ns_min, time_ns, l,
  397. invalid ? "; invalid " : " ");
  398. } else {
  399. /* raw format */
  400. pr_info("gpmc,%s = <%u>%s\n", name, l,
  401. invalid ? " /* invalid */" : "");
  402. }
  403. return l;
  404. }
  405. #define GPMC_PRINT_CONFIG(cs, config) \
  406. pr_info("cs%i %s: 0x%08x\n", cs, #config, \
  407. gpmc_cs_read_reg(cs, config))
  408. #define GPMC_GET_RAW(reg, st, end, field) \
  409. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 0)
  410. #define GPMC_GET_RAW_MAX(reg, st, end, max, field) \
  411. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, 0, 1, 0)
  412. #define GPMC_GET_RAW_BOOL(reg, st, end, field) \
  413. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 1)
  414. #define GPMC_GET_RAW_SHIFT_MAX(reg, st, end, shift, max, field) \
  415. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, (shift), 1, 1)
  416. #define GPMC_GET_TICKS(reg, st, end, field) \
  417. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 0, 0)
  418. #define GPMC_GET_TICKS_CD(reg, st, end, field, cd) \
  419. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, (cd), 0, 0, 0)
  420. #define GPMC_GET_TICKS_CD_MAX(reg, st, end, max, field, cd) \
  421. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, (cd), 0, 0, 0)
  422. static void gpmc_show_regs(int cs, const char *desc)
  423. {
  424. pr_info("gpmc cs%i %s:\n", cs, desc);
  425. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG1);
  426. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG2);
  427. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG3);
  428. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG4);
  429. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG5);
  430. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG6);
  431. }
  432. /*
  433. * Note that gpmc,wait-pin handing wrongly assumes bit 8 is available,
  434. * see commit c9fb809.
  435. */
  436. static void gpmc_cs_show_timings(int cs, const char *desc)
  437. {
  438. gpmc_show_regs(cs, desc);
  439. pr_info("gpmc cs%i access configuration:\n", cs);
  440. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 4, 4, "time-para-granularity");
  441. GPMC_GET_RAW(GPMC_CS_CONFIG1, 8, 9, "mux-add-data");
  442. GPMC_GET_RAW_MAX(GPMC_CS_CONFIG1, 12, 13,
  443. GPMC_CONFIG1_DEVICESIZE_MAX, "device-width");
  444. GPMC_GET_RAW(GPMC_CS_CONFIG1, 16, 17, "wait-pin");
  445. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 21, 21, "wait-on-write");
  446. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 22, 22, "wait-on-read");
  447. GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 23, 24, 4,
  448. GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX,
  449. "burst-length");
  450. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 27, 27, "sync-write");
  451. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 28, 28, "burst-write");
  452. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 29, 29, "gpmc,sync-read");
  453. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 30, 30, "burst-read");
  454. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 31, 31, "burst-wrap");
  455. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG2, 7, 7, "cs-extra-delay");
  456. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG3, 7, 7, "adv-extra-delay");
  457. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 23, 23, "we-extra-delay");
  458. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 7, 7, "oe-extra-delay");
  459. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 7, 7, "cycle2cycle-samecsen");
  460. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 6, 6, "cycle2cycle-diffcsen");
  461. pr_info("gpmc cs%i timings configuration:\n", cs);
  462. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 0, 3, "cs-on-ns");
  463. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 8, 12, "cs-rd-off-ns");
  464. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 16, 20, "cs-wr-off-ns");
  465. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 0, 3, "adv-on-ns");
  466. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 8, 12, "adv-rd-off-ns");
  467. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 16, 20, "adv-wr-off-ns");
  468. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 0, 3, "oe-on-ns");
  469. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 8, 12, "oe-off-ns");
  470. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 16, 19, "we-on-ns");
  471. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 24, 28, "we-off-ns");
  472. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 0, 4, "rd-cycle-ns");
  473. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 8, 12, "wr-cycle-ns");
  474. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 16, 20, "access-ns");
  475. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 24, 27, "page-burst-access-ns");
  476. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 0, 3, "bus-turnaround-ns");
  477. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 8, 11, "cycle2cycle-delay-ns");
  478. GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
  479. GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
  480. "wait-monitoring-ns", GPMC_CD_CLK);
  481. GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
  482. GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
  483. "clk-activation-ns", GPMC_CD_FCLK);
  484. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 16, 19, "wr-data-mux-bus-ns");
  485. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 24, 28, "wr-access-ns");
  486. }
  487. #else
  488. static inline void gpmc_cs_show_timings(int cs, const char *desc)
  489. {
  490. }
  491. #endif
  492. /**
  493. * set_gpmc_timing_reg - set a single timing parameter for Chip Select Region.
  494. * Caller is expected to have initialized CONFIG1 GPMCFCLKDIVIDER
  495. * prior to calling this function with @cd equal to GPMC_CD_CLK.
  496. *
  497. * @cs: Chip Select Region.
  498. * @reg: GPMC_CS_CONFIGn register offset.
  499. * @st_bit: Start Bit
  500. * @end_bit: End Bit. Must be >= @st_bit.
  501. * @max: Maximum parameter value.
  502. * If 0, maximum is as high as @st_bit and @end_bit allow.
  503. * @time: Timing parameter in ns.
  504. * @cd: Timing parameter clock domain.
  505. * @name: Timing parameter name.
  506. * @return: 0 on success, -1 on error.
  507. */
  508. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, int max,
  509. int time, enum gpmc_clk_domain cd, const char *name)
  510. {
  511. u32 l;
  512. int ticks, mask, nr_bits;
  513. if (time == 0)
  514. ticks = 0;
  515. else
  516. ticks = gpmc_ns_to_clk_ticks(time, cs, cd);
  517. nr_bits = end_bit - st_bit + 1;
  518. mask = (1 << nr_bits) - 1;
  519. if (!max)
  520. max = mask;
  521. if (ticks > max) {
  522. pr_err("%s: GPMC CS%d: %s %d ns, %d ticks > %d ticks\n",
  523. __func__, cs, name, time, ticks, max);
  524. return -1;
  525. }
  526. l = gpmc_cs_read_reg(cs, reg);
  527. #ifdef CONFIG_OMAP_GPMC_DEBUG
  528. pr_info(
  529. "GPMC CS%d: %-17s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  530. cs, name, ticks, gpmc_get_clk_period(cs, cd) * ticks / 1000,
  531. (l >> st_bit) & mask, time);
  532. #endif
  533. l &= ~(mask << st_bit);
  534. l |= ticks << st_bit;
  535. gpmc_cs_write_reg(cs, reg, l);
  536. return 0;
  537. }
  538. #define GPMC_SET_ONE_CD_MAX(reg, st, end, max, field, cd) \
  539. if (set_gpmc_timing_reg(cs, (reg), (st), (end), (max), \
  540. t->field, (cd), #field) < 0) \
  541. return -1
  542. #define GPMC_SET_ONE(reg, st, end, field) \
  543. GPMC_SET_ONE_CD_MAX(reg, st, end, 0, field, GPMC_CD_FCLK)
  544. /**
  545. * gpmc_calc_waitmonitoring_divider - calculate proper GPMCFCLKDIVIDER based on WAITMONITORINGTIME
  546. * WAITMONITORINGTIME will be _at least_ as long as desired, i.e.
  547. * read --> don't sample bus too early
  548. * write --> data is longer on bus
  549. *
  550. * Formula:
  551. * gpmc_clk_div + 1 = ceil(ceil(waitmonitoringtime_ns / gpmc_fclk_ns)
  552. * / waitmonitoring_ticks)
  553. * WAITMONITORINGTIME resulting in 0 or 1 tick with div = 1 are caught by
  554. * div <= 0 check.
  555. *
  556. * @wait_monitoring: WAITMONITORINGTIME in ns.
  557. * @return: -1 on failure to scale, else proper divider > 0.
  558. */
  559. static int gpmc_calc_waitmonitoring_divider(unsigned int wait_monitoring)
  560. {
  561. int div = gpmc_ns_to_ticks(wait_monitoring);
  562. div += GPMC_CONFIG1_WAITMONITORINGTIME_MAX - 1;
  563. div /= GPMC_CONFIG1_WAITMONITORINGTIME_MAX;
  564. if (div > 4)
  565. return -1;
  566. if (div <= 0)
  567. div = 1;
  568. return div;
  569. }
  570. /**
  571. * gpmc_calc_divider - calculate GPMC_FCLK divider for sync_clk GPMC_CLK period.
  572. * @sync_clk: GPMC_CLK period in ps.
  573. * @return: Returns at least 1 if GPMC_FCLK can be divided to GPMC_CLK.
  574. * Else, returns -1.
  575. */
  576. int gpmc_calc_divider(unsigned int sync_clk)
  577. {
  578. int div = gpmc_ps_to_ticks(sync_clk);
  579. if (div > 4)
  580. return -1;
  581. if (div <= 0)
  582. div = 1;
  583. return div;
  584. }
  585. /**
  586. * gpmc_cs_set_timings - program timing parameters for Chip Select Region.
  587. * @cs: Chip Select Region.
  588. * @t: GPMC timing parameters.
  589. * @s: GPMC timing settings.
  590. * @return: 0 on success, -1 on error.
  591. */
  592. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t,
  593. const struct gpmc_settings *s)
  594. {
  595. int div;
  596. u32 l;
  597. div = gpmc_calc_divider(t->sync_clk);
  598. if (div < 0)
  599. return div;
  600. /*
  601. * See if we need to change the divider for waitmonitoringtime.
  602. *
  603. * Calculate GPMCFCLKDIVIDER independent of gpmc,sync-clk-ps in DT for
  604. * pure asynchronous accesses, i.e. both read and write asynchronous.
  605. * However, only do so if WAITMONITORINGTIME is actually used, i.e.
  606. * either WAITREADMONITORING or WAITWRITEMONITORING is set.
  607. *
  608. * This statement must not change div to scale async WAITMONITORINGTIME
  609. * to protect mixed synchronous and asynchronous accesses.
  610. *
  611. * We raise an error later if WAITMONITORINGTIME does not fit.
  612. */
  613. if (!s->sync_read && !s->sync_write &&
  614. (s->wait_on_read || s->wait_on_write)
  615. ) {
  616. div = gpmc_calc_waitmonitoring_divider(t->wait_monitoring);
  617. if (div < 0) {
  618. pr_err("%s: waitmonitoringtime %3d ns too large for greatest gpmcfclkdivider.\n",
  619. __func__,
  620. t->wait_monitoring
  621. );
  622. return -1;
  623. }
  624. }
  625. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  626. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  627. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  628. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  629. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  630. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  631. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  632. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  633. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  634. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  635. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  636. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  637. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  638. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  639. GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
  640. GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
  641. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  642. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  643. if (gpmc_capability & GPMC_HAS_WR_ACCESS)
  644. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  645. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  646. l &= ~0x03;
  647. l |= (div - 1);
  648. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  649. GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
  650. GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
  651. wait_monitoring, GPMC_CD_CLK);
  652. GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
  653. GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
  654. clk_activation, GPMC_CD_FCLK);
  655. #ifdef CONFIG_OMAP_GPMC_DEBUG
  656. pr_info("GPMC CS%d CLK period is %lu ns (div %d)\n",
  657. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  658. #endif
  659. gpmc_cs_bool_timings(cs, &t->bool_timings);
  660. gpmc_cs_show_timings(cs, "after gpmc_cs_set_timings");
  661. return 0;
  662. }
  663. static int gpmc_cs_set_memconf(int cs, u32 base, u32 size)
  664. {
  665. u32 l;
  666. u32 mask;
  667. /*
  668. * Ensure that base address is aligned on a
  669. * boundary equal to or greater than size.
  670. */
  671. if (base & (size - 1))
  672. return -EINVAL;
  673. base >>= GPMC_CHUNK_SHIFT;
  674. mask = (1 << GPMC_SECTION_SHIFT) - size;
  675. mask >>= GPMC_CHUNK_SHIFT;
  676. mask <<= GPMC_CONFIG7_MASKADDRESS_OFFSET;
  677. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  678. l &= ~GPMC_CONFIG7_MASK;
  679. l |= base & GPMC_CONFIG7_BASEADDRESS_MASK;
  680. l |= mask & GPMC_CONFIG7_MASKADDRESS_MASK;
  681. l |= GPMC_CONFIG7_CSVALID;
  682. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  683. return 0;
  684. }
  685. static void gpmc_cs_enable_mem(int cs)
  686. {
  687. u32 l;
  688. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  689. l |= GPMC_CONFIG7_CSVALID;
  690. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  691. }
  692. static void gpmc_cs_disable_mem(int cs)
  693. {
  694. u32 l;
  695. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  696. l &= ~GPMC_CONFIG7_CSVALID;
  697. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  698. }
  699. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  700. {
  701. u32 l;
  702. u32 mask;
  703. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  704. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  705. mask = (l >> 8) & 0x0f;
  706. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  707. }
  708. static int gpmc_cs_mem_enabled(int cs)
  709. {
  710. u32 l;
  711. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  712. return l & GPMC_CONFIG7_CSVALID;
  713. }
  714. static void gpmc_cs_set_reserved(int cs, int reserved)
  715. {
  716. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  717. gpmc->flags |= GPMC_CS_RESERVED;
  718. }
  719. static bool gpmc_cs_reserved(int cs)
  720. {
  721. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  722. return gpmc->flags & GPMC_CS_RESERVED;
  723. }
  724. static void gpmc_cs_set_name(int cs, const char *name)
  725. {
  726. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  727. gpmc->name = name;
  728. }
  729. static const char *gpmc_cs_get_name(int cs)
  730. {
  731. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  732. return gpmc->name;
  733. }
  734. static unsigned long gpmc_mem_align(unsigned long size)
  735. {
  736. int order;
  737. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  738. order = GPMC_CHUNK_SHIFT - 1;
  739. do {
  740. size >>= 1;
  741. order++;
  742. } while (size);
  743. size = 1 << order;
  744. return size;
  745. }
  746. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  747. {
  748. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  749. struct resource *res = &gpmc->mem;
  750. int r;
  751. size = gpmc_mem_align(size);
  752. spin_lock(&gpmc_mem_lock);
  753. res->start = base;
  754. res->end = base + size - 1;
  755. r = request_resource(&gpmc_mem_root, res);
  756. spin_unlock(&gpmc_mem_lock);
  757. return r;
  758. }
  759. static int gpmc_cs_delete_mem(int cs)
  760. {
  761. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  762. struct resource *res = &gpmc->mem;
  763. int r;
  764. spin_lock(&gpmc_mem_lock);
  765. r = release_resource(res);
  766. res->start = 0;
  767. res->end = 0;
  768. spin_unlock(&gpmc_mem_lock);
  769. return r;
  770. }
  771. /**
  772. * gpmc_cs_remap - remaps a chip-select physical base address
  773. * @cs: chip-select to remap
  774. * @base: physical base address to re-map chip-select to
  775. *
  776. * Re-maps a chip-select to a new physical base address specified by
  777. * "base". Returns 0 on success and appropriate negative error code
  778. * on failure.
  779. */
  780. static int gpmc_cs_remap(int cs, u32 base)
  781. {
  782. int ret;
  783. u32 old_base, size;
  784. if (cs > gpmc_cs_num) {
  785. pr_err("%s: requested chip-select is disabled\n", __func__);
  786. return -ENODEV;
  787. }
  788. /*
  789. * Make sure we ignore any device offsets from the GPMC partition
  790. * allocated for the chip select and that the new base confirms
  791. * to the GPMC 16MB minimum granularity.
  792. */
  793. base &= ~(SZ_16M - 1);
  794. gpmc_cs_get_memconf(cs, &old_base, &size);
  795. if (base == old_base)
  796. return 0;
  797. ret = gpmc_cs_delete_mem(cs);
  798. if (ret < 0)
  799. return ret;
  800. ret = gpmc_cs_insert_mem(cs, base, size);
  801. if (ret < 0)
  802. return ret;
  803. ret = gpmc_cs_set_memconf(cs, base, size);
  804. return ret;
  805. }
  806. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  807. {
  808. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  809. struct resource *res = &gpmc->mem;
  810. int r = -1;
  811. if (cs > gpmc_cs_num) {
  812. pr_err("%s: requested chip-select is disabled\n", __func__);
  813. return -ENODEV;
  814. }
  815. size = gpmc_mem_align(size);
  816. if (size > (1 << GPMC_SECTION_SHIFT))
  817. return -ENOMEM;
  818. spin_lock(&gpmc_mem_lock);
  819. if (gpmc_cs_reserved(cs)) {
  820. r = -EBUSY;
  821. goto out;
  822. }
  823. if (gpmc_cs_mem_enabled(cs))
  824. r = adjust_resource(res, res->start & ~(size - 1), size);
  825. if (r < 0)
  826. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  827. size, NULL, NULL);
  828. if (r < 0)
  829. goto out;
  830. /* Disable CS while changing base address and size mask */
  831. gpmc_cs_disable_mem(cs);
  832. r = gpmc_cs_set_memconf(cs, res->start, resource_size(res));
  833. if (r < 0) {
  834. release_resource(res);
  835. goto out;
  836. }
  837. /* Enable CS */
  838. gpmc_cs_enable_mem(cs);
  839. *base = res->start;
  840. gpmc_cs_set_reserved(cs, 1);
  841. out:
  842. spin_unlock(&gpmc_mem_lock);
  843. return r;
  844. }
  845. EXPORT_SYMBOL(gpmc_cs_request);
  846. void gpmc_cs_free(int cs)
  847. {
  848. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  849. struct resource *res = &gpmc->mem;
  850. spin_lock(&gpmc_mem_lock);
  851. if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
  852. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  853. BUG();
  854. spin_unlock(&gpmc_mem_lock);
  855. return;
  856. }
  857. gpmc_cs_disable_mem(cs);
  858. if (res->flags)
  859. release_resource(res);
  860. gpmc_cs_set_reserved(cs, 0);
  861. spin_unlock(&gpmc_mem_lock);
  862. }
  863. EXPORT_SYMBOL(gpmc_cs_free);
  864. /**
  865. * gpmc_configure - write request to configure gpmc
  866. * @cmd: command type
  867. * @wval: value to write
  868. * @return status of the operation
  869. */
  870. int gpmc_configure(int cmd, int wval)
  871. {
  872. u32 regval;
  873. switch (cmd) {
  874. case GPMC_ENABLE_IRQ:
  875. gpmc_write_reg(GPMC_IRQENABLE, wval);
  876. break;
  877. case GPMC_SET_IRQ_STATUS:
  878. gpmc_write_reg(GPMC_IRQSTATUS, wval);
  879. break;
  880. case GPMC_CONFIG_WP:
  881. regval = gpmc_read_reg(GPMC_CONFIG);
  882. if (wval)
  883. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  884. else
  885. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  886. gpmc_write_reg(GPMC_CONFIG, regval);
  887. break;
  888. default:
  889. pr_err("%s: command not supported\n", __func__);
  890. return -EINVAL;
  891. }
  892. return 0;
  893. }
  894. EXPORT_SYMBOL(gpmc_configure);
  895. void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
  896. {
  897. int i;
  898. reg->gpmc_status = gpmc_base + GPMC_STATUS;
  899. reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
  900. GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
  901. reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
  902. GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
  903. reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
  904. GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
  905. reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
  906. reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
  907. reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
  908. reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
  909. reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
  910. reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
  911. reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
  912. reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
  913. for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
  914. reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
  915. GPMC_BCH_SIZE * i;
  916. reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
  917. GPMC_BCH_SIZE * i;
  918. reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
  919. GPMC_BCH_SIZE * i;
  920. reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
  921. GPMC_BCH_SIZE * i;
  922. reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
  923. i * GPMC_BCH_SIZE;
  924. reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
  925. i * GPMC_BCH_SIZE;
  926. reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
  927. i * GPMC_BCH_SIZE;
  928. }
  929. }
  930. int gpmc_get_client_irq(unsigned irq_config)
  931. {
  932. int i;
  933. if (hweight32(irq_config) > 1)
  934. return 0;
  935. for (i = 0; i < GPMC_NR_IRQ; i++)
  936. if (gpmc_client_irq[i].bitmask & irq_config)
  937. return gpmc_client_irq[i].irq;
  938. return 0;
  939. }
  940. static int gpmc_irq_endis(unsigned irq, bool endis)
  941. {
  942. int i;
  943. u32 regval;
  944. for (i = 0; i < GPMC_NR_IRQ; i++)
  945. if (irq == gpmc_client_irq[i].irq) {
  946. regval = gpmc_read_reg(GPMC_IRQENABLE);
  947. if (endis)
  948. regval |= gpmc_client_irq[i].bitmask;
  949. else
  950. regval &= ~gpmc_client_irq[i].bitmask;
  951. gpmc_write_reg(GPMC_IRQENABLE, regval);
  952. break;
  953. }
  954. return 0;
  955. }
  956. static void gpmc_irq_disable(struct irq_data *p)
  957. {
  958. gpmc_irq_endis(p->irq, false);
  959. }
  960. static void gpmc_irq_enable(struct irq_data *p)
  961. {
  962. gpmc_irq_endis(p->irq, true);
  963. }
  964. static void gpmc_irq_noop(struct irq_data *data) { }
  965. static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
  966. static int gpmc_setup_irq(void)
  967. {
  968. int i;
  969. u32 regval;
  970. if (!gpmc_irq)
  971. return -EINVAL;
  972. gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
  973. if (gpmc_irq_start < 0) {
  974. pr_err("irq_alloc_descs failed\n");
  975. return gpmc_irq_start;
  976. }
  977. gpmc_irq_chip.name = "gpmc";
  978. gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
  979. gpmc_irq_chip.irq_enable = gpmc_irq_enable;
  980. gpmc_irq_chip.irq_disable = gpmc_irq_disable;
  981. gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
  982. gpmc_irq_chip.irq_ack = gpmc_irq_noop;
  983. gpmc_irq_chip.irq_mask = gpmc_irq_noop;
  984. gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
  985. gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
  986. gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
  987. for (i = 0; i < GPMC_NR_IRQ; i++) {
  988. gpmc_client_irq[i].irq = gpmc_irq_start + i;
  989. irq_set_chip_and_handler(gpmc_client_irq[i].irq,
  990. &gpmc_irq_chip, handle_simple_irq);
  991. irq_modify_status(gpmc_client_irq[i].irq, IRQ_NOREQUEST,
  992. IRQ_NOAUTOEN);
  993. }
  994. /* Disable interrupts */
  995. gpmc_write_reg(GPMC_IRQENABLE, 0);
  996. /* clear interrupts */
  997. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  998. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  999. return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
  1000. }
  1001. static int gpmc_free_irq(void)
  1002. {
  1003. int i;
  1004. if (gpmc_irq)
  1005. free_irq(gpmc_irq, NULL);
  1006. for (i = 0; i < GPMC_NR_IRQ; i++) {
  1007. irq_set_handler(gpmc_client_irq[i].irq, NULL);
  1008. irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
  1009. }
  1010. irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
  1011. return 0;
  1012. }
  1013. static void gpmc_mem_exit(void)
  1014. {
  1015. int cs;
  1016. for (cs = 0; cs < gpmc_cs_num; cs++) {
  1017. if (!gpmc_cs_mem_enabled(cs))
  1018. continue;
  1019. gpmc_cs_delete_mem(cs);
  1020. }
  1021. }
  1022. static void gpmc_mem_init(void)
  1023. {
  1024. int cs;
  1025. /*
  1026. * The first 1MB of GPMC address space is typically mapped to
  1027. * the internal ROM. Never allocate the first page, to
  1028. * facilitate bug detection; even if we didn't boot from ROM.
  1029. */
  1030. gpmc_mem_root.start = SZ_1M;
  1031. gpmc_mem_root.end = GPMC_MEM_END;
  1032. /* Reserve all regions that has been set up by bootloader */
  1033. for (cs = 0; cs < gpmc_cs_num; cs++) {
  1034. u32 base, size;
  1035. if (!gpmc_cs_mem_enabled(cs))
  1036. continue;
  1037. gpmc_cs_get_memconf(cs, &base, &size);
  1038. if (gpmc_cs_insert_mem(cs, base, size)) {
  1039. pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
  1040. __func__, cs, base, base + size);
  1041. gpmc_cs_disable_mem(cs);
  1042. }
  1043. }
  1044. }
  1045. static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
  1046. {
  1047. u32 temp;
  1048. int div;
  1049. div = gpmc_calc_divider(sync_clk);
  1050. temp = gpmc_ps_to_ticks(time_ps);
  1051. temp = (temp + div - 1) / div;
  1052. return gpmc_ticks_to_ps(temp * div);
  1053. }
  1054. /* XXX: can the cycles be avoided ? */
  1055. static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
  1056. struct gpmc_device_timings *dev_t,
  1057. bool mux)
  1058. {
  1059. u32 temp;
  1060. /* adv_rd_off */
  1061. temp = dev_t->t_avdp_r;
  1062. /* XXX: mux check required ? */
  1063. if (mux) {
  1064. /* XXX: t_avdp not to be required for sync, only added for tusb
  1065. * this indirectly necessitates requirement of t_avdp_r and
  1066. * t_avdp_w instead of having a single t_avdp
  1067. */
  1068. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
  1069. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1070. }
  1071. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  1072. /* oe_on */
  1073. temp = dev_t->t_oeasu; /* XXX: remove this ? */
  1074. if (mux) {
  1075. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
  1076. temp = max_t(u32, temp, gpmc_t->adv_rd_off +
  1077. gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
  1078. }
  1079. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  1080. /* access */
  1081. /* XXX: any scope for improvement ?, by combining oe_on
  1082. * and clk_activation, need to check whether
  1083. * access = clk_activation + round to sync clk ?
  1084. */
  1085. temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
  1086. temp += gpmc_t->clk_activation;
  1087. if (dev_t->cyc_oe)
  1088. temp = max_t(u32, temp, gpmc_t->oe_on +
  1089. gpmc_ticks_to_ps(dev_t->cyc_oe));
  1090. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  1091. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  1092. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  1093. /* rd_cycle */
  1094. temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
  1095. temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
  1096. gpmc_t->access;
  1097. /* XXX: barter t_ce_rdyz with t_cez_r ? */
  1098. if (dev_t->t_ce_rdyz)
  1099. temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
  1100. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  1101. return 0;
  1102. }
  1103. static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
  1104. struct gpmc_device_timings *dev_t,
  1105. bool mux)
  1106. {
  1107. u32 temp;
  1108. /* adv_wr_off */
  1109. temp = dev_t->t_avdp_w;
  1110. if (mux) {
  1111. temp = max_t(u32, temp,
  1112. gpmc_t->clk_activation + dev_t->t_avdh);
  1113. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1114. }
  1115. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  1116. /* wr_data_mux_bus */
  1117. temp = max_t(u32, dev_t->t_weasu,
  1118. gpmc_t->clk_activation + dev_t->t_rdyo);
  1119. /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
  1120. * and in that case remember to handle we_on properly
  1121. */
  1122. if (mux) {
  1123. temp = max_t(u32, temp,
  1124. gpmc_t->adv_wr_off + dev_t->t_aavdh);
  1125. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  1126. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  1127. }
  1128. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  1129. /* we_on */
  1130. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  1131. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  1132. else
  1133. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  1134. /* wr_access */
  1135. /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
  1136. gpmc_t->wr_access = gpmc_t->access;
  1137. /* we_off */
  1138. temp = gpmc_t->we_on + dev_t->t_wpl;
  1139. temp = max_t(u32, temp,
  1140. gpmc_t->wr_access + gpmc_ticks_to_ps(1));
  1141. temp = max_t(u32, temp,
  1142. gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
  1143. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  1144. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  1145. dev_t->t_wph);
  1146. /* wr_cycle */
  1147. temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
  1148. temp += gpmc_t->wr_access;
  1149. /* XXX: barter t_ce_rdyz with t_cez_w ? */
  1150. if (dev_t->t_ce_rdyz)
  1151. temp = max_t(u32, temp,
  1152. gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
  1153. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  1154. return 0;
  1155. }
  1156. static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
  1157. struct gpmc_device_timings *dev_t,
  1158. bool mux)
  1159. {
  1160. u32 temp;
  1161. /* adv_rd_off */
  1162. temp = dev_t->t_avdp_r;
  1163. if (mux)
  1164. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1165. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  1166. /* oe_on */
  1167. temp = dev_t->t_oeasu;
  1168. if (mux)
  1169. temp = max_t(u32, temp,
  1170. gpmc_t->adv_rd_off + dev_t->t_aavdh);
  1171. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  1172. /* access */
  1173. temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
  1174. gpmc_t->oe_on + dev_t->t_oe);
  1175. temp = max_t(u32, temp,
  1176. gpmc_t->cs_on + dev_t->t_ce);
  1177. temp = max_t(u32, temp,
  1178. gpmc_t->adv_on + dev_t->t_aa);
  1179. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  1180. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  1181. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  1182. /* rd_cycle */
  1183. temp = max_t(u32, dev_t->t_rd_cycle,
  1184. gpmc_t->cs_rd_off + dev_t->t_cez_r);
  1185. temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
  1186. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  1187. return 0;
  1188. }
  1189. static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
  1190. struct gpmc_device_timings *dev_t,
  1191. bool mux)
  1192. {
  1193. u32 temp;
  1194. /* adv_wr_off */
  1195. temp = dev_t->t_avdp_w;
  1196. if (mux)
  1197. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1198. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  1199. /* wr_data_mux_bus */
  1200. temp = dev_t->t_weasu;
  1201. if (mux) {
  1202. temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
  1203. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  1204. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  1205. }
  1206. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  1207. /* we_on */
  1208. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  1209. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  1210. else
  1211. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  1212. /* we_off */
  1213. temp = gpmc_t->we_on + dev_t->t_wpl;
  1214. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  1215. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  1216. dev_t->t_wph);
  1217. /* wr_cycle */
  1218. temp = max_t(u32, dev_t->t_wr_cycle,
  1219. gpmc_t->cs_wr_off + dev_t->t_cez_w);
  1220. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  1221. return 0;
  1222. }
  1223. static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
  1224. struct gpmc_device_timings *dev_t)
  1225. {
  1226. u32 temp;
  1227. gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
  1228. gpmc_get_fclk_period();
  1229. gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
  1230. dev_t->t_bacc,
  1231. gpmc_t->sync_clk);
  1232. temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
  1233. gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
  1234. if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
  1235. return 0;
  1236. if (dev_t->ce_xdelay)
  1237. gpmc_t->bool_timings.cs_extra_delay = true;
  1238. if (dev_t->avd_xdelay)
  1239. gpmc_t->bool_timings.adv_extra_delay = true;
  1240. if (dev_t->oe_xdelay)
  1241. gpmc_t->bool_timings.oe_extra_delay = true;
  1242. if (dev_t->we_xdelay)
  1243. gpmc_t->bool_timings.we_extra_delay = true;
  1244. return 0;
  1245. }
  1246. static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
  1247. struct gpmc_device_timings *dev_t,
  1248. bool sync)
  1249. {
  1250. u32 temp;
  1251. /* cs_on */
  1252. gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
  1253. /* adv_on */
  1254. temp = dev_t->t_avdasu;
  1255. if (dev_t->t_ce_avd)
  1256. temp = max_t(u32, temp,
  1257. gpmc_t->cs_on + dev_t->t_ce_avd);
  1258. gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
  1259. if (sync)
  1260. gpmc_calc_sync_common_timings(gpmc_t, dev_t);
  1261. return 0;
  1262. }
  1263. /* TODO: remove this function once all peripherals are confirmed to
  1264. * work with generic timing. Simultaneously gpmc_cs_set_timings()
  1265. * has to be modified to handle timings in ps instead of ns
  1266. */
  1267. static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
  1268. {
  1269. t->cs_on /= 1000;
  1270. t->cs_rd_off /= 1000;
  1271. t->cs_wr_off /= 1000;
  1272. t->adv_on /= 1000;
  1273. t->adv_rd_off /= 1000;
  1274. t->adv_wr_off /= 1000;
  1275. t->we_on /= 1000;
  1276. t->we_off /= 1000;
  1277. t->oe_on /= 1000;
  1278. t->oe_off /= 1000;
  1279. t->page_burst_access /= 1000;
  1280. t->access /= 1000;
  1281. t->rd_cycle /= 1000;
  1282. t->wr_cycle /= 1000;
  1283. t->bus_turnaround /= 1000;
  1284. t->cycle2cycle_delay /= 1000;
  1285. t->wait_monitoring /= 1000;
  1286. t->clk_activation /= 1000;
  1287. t->wr_access /= 1000;
  1288. t->wr_data_mux_bus /= 1000;
  1289. }
  1290. int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
  1291. struct gpmc_settings *gpmc_s,
  1292. struct gpmc_device_timings *dev_t)
  1293. {
  1294. bool mux = false, sync = false;
  1295. if (gpmc_s) {
  1296. mux = gpmc_s->mux_add_data ? true : false;
  1297. sync = (gpmc_s->sync_read || gpmc_s->sync_write);
  1298. }
  1299. memset(gpmc_t, 0, sizeof(*gpmc_t));
  1300. gpmc_calc_common_timings(gpmc_t, dev_t, sync);
  1301. if (gpmc_s && gpmc_s->sync_read)
  1302. gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
  1303. else
  1304. gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
  1305. if (gpmc_s && gpmc_s->sync_write)
  1306. gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
  1307. else
  1308. gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
  1309. /* TODO: remove, see function definition */
  1310. gpmc_convert_ps_to_ns(gpmc_t);
  1311. return 0;
  1312. }
  1313. /**
  1314. * gpmc_cs_program_settings - programs non-timing related settings
  1315. * @cs: GPMC chip-select to program
  1316. * @p: pointer to GPMC settings structure
  1317. *
  1318. * Programs non-timing related settings for a GPMC chip-select, such as
  1319. * bus-width, burst configuration, etc. Function should be called once
  1320. * for each chip-select that is being used and must be called before
  1321. * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
  1322. * register will be initialised to zero by this function. Returns 0 on
  1323. * success and appropriate negative error code on failure.
  1324. */
  1325. int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
  1326. {
  1327. u32 config1;
  1328. if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
  1329. pr_err("%s: invalid width %d!", __func__, p->device_width);
  1330. return -EINVAL;
  1331. }
  1332. /* Address-data multiplexing not supported for NAND devices */
  1333. if (p->device_nand && p->mux_add_data) {
  1334. pr_err("%s: invalid configuration!\n", __func__);
  1335. return -EINVAL;
  1336. }
  1337. if ((p->mux_add_data > GPMC_MUX_AD) ||
  1338. ((p->mux_add_data == GPMC_MUX_AAD) &&
  1339. !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
  1340. pr_err("%s: invalid multiplex configuration!\n", __func__);
  1341. return -EINVAL;
  1342. }
  1343. /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
  1344. if (p->burst_read || p->burst_write) {
  1345. switch (p->burst_len) {
  1346. case GPMC_BURST_4:
  1347. case GPMC_BURST_8:
  1348. case GPMC_BURST_16:
  1349. break;
  1350. default:
  1351. pr_err("%s: invalid page/burst-length (%d)\n",
  1352. __func__, p->burst_len);
  1353. return -EINVAL;
  1354. }
  1355. }
  1356. if (p->wait_pin > gpmc_nr_waitpins) {
  1357. pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
  1358. return -EINVAL;
  1359. }
  1360. config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
  1361. if (p->sync_read)
  1362. config1 |= GPMC_CONFIG1_READTYPE_SYNC;
  1363. if (p->sync_write)
  1364. config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
  1365. if (p->wait_on_read)
  1366. config1 |= GPMC_CONFIG1_WAIT_READ_MON;
  1367. if (p->wait_on_write)
  1368. config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
  1369. if (p->wait_on_read || p->wait_on_write)
  1370. config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
  1371. if (p->device_nand)
  1372. config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
  1373. if (p->mux_add_data)
  1374. config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
  1375. if (p->burst_read)
  1376. config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
  1377. if (p->burst_write)
  1378. config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
  1379. if (p->burst_read || p->burst_write) {
  1380. config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
  1381. config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
  1382. }
  1383. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
  1384. return 0;
  1385. }
  1386. #ifdef CONFIG_OF
  1387. static const struct of_device_id gpmc_dt_ids[] = {
  1388. { .compatible = "ti,omap2420-gpmc" },
  1389. { .compatible = "ti,omap2430-gpmc" },
  1390. { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
  1391. { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
  1392. { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
  1393. { }
  1394. };
  1395. MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
  1396. /**
  1397. * gpmc_read_settings_dt - read gpmc settings from device-tree
  1398. * @np: pointer to device-tree node for a gpmc child device
  1399. * @p: pointer to gpmc settings structure
  1400. *
  1401. * Reads the GPMC settings for a GPMC child device from device-tree and
  1402. * stores them in the GPMC settings structure passed. The GPMC settings
  1403. * structure is initialised to zero by this function and so any
  1404. * previously stored settings will be cleared.
  1405. */
  1406. void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
  1407. {
  1408. memset(p, 0, sizeof(struct gpmc_settings));
  1409. p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
  1410. p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
  1411. of_property_read_u32(np, "gpmc,device-width", &p->device_width);
  1412. of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
  1413. if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
  1414. p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
  1415. p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
  1416. p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
  1417. if (!p->burst_read && !p->burst_write)
  1418. pr_warn("%s: page/burst-length set but not used!\n",
  1419. __func__);
  1420. }
  1421. if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
  1422. p->wait_on_read = of_property_read_bool(np,
  1423. "gpmc,wait-on-read");
  1424. p->wait_on_write = of_property_read_bool(np,
  1425. "gpmc,wait-on-write");
  1426. if (!p->wait_on_read && !p->wait_on_write)
  1427. pr_debug("%s: rd/wr wait monitoring not enabled!\n",
  1428. __func__);
  1429. }
  1430. }
  1431. static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
  1432. struct gpmc_timings *gpmc_t)
  1433. {
  1434. struct gpmc_bool_timings *p;
  1435. if (!np || !gpmc_t)
  1436. return;
  1437. memset(gpmc_t, 0, sizeof(*gpmc_t));
  1438. /* minimum clock period for syncronous mode */
  1439. of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
  1440. /* chip select timtings */
  1441. of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
  1442. of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
  1443. of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
  1444. /* ADV signal timings */
  1445. of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
  1446. of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
  1447. of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
  1448. /* WE signal timings */
  1449. of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
  1450. of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
  1451. /* OE signal timings */
  1452. of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
  1453. of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
  1454. /* access and cycle timings */
  1455. of_property_read_u32(np, "gpmc,page-burst-access-ns",
  1456. &gpmc_t->page_burst_access);
  1457. of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
  1458. of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
  1459. of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
  1460. of_property_read_u32(np, "gpmc,bus-turnaround-ns",
  1461. &gpmc_t->bus_turnaround);
  1462. of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
  1463. &gpmc_t->cycle2cycle_delay);
  1464. of_property_read_u32(np, "gpmc,wait-monitoring-ns",
  1465. &gpmc_t->wait_monitoring);
  1466. of_property_read_u32(np, "gpmc,clk-activation-ns",
  1467. &gpmc_t->clk_activation);
  1468. /* only applicable to OMAP3+ */
  1469. of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
  1470. of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
  1471. &gpmc_t->wr_data_mux_bus);
  1472. /* bool timing parameters */
  1473. p = &gpmc_t->bool_timings;
  1474. p->cycle2cyclediffcsen =
  1475. of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
  1476. p->cycle2cyclesamecsen =
  1477. of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
  1478. p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
  1479. p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
  1480. p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
  1481. p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
  1482. p->time_para_granularity =
  1483. of_property_read_bool(np, "gpmc,time-para-granularity");
  1484. }
  1485. #if IS_ENABLED(CONFIG_MTD_NAND)
  1486. static const char * const nand_xfer_types[] = {
  1487. [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
  1488. [NAND_OMAP_POLLED] = "polled",
  1489. [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
  1490. [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
  1491. };
  1492. static int gpmc_probe_nand_child(struct platform_device *pdev,
  1493. struct device_node *child)
  1494. {
  1495. u32 val;
  1496. const char *s;
  1497. struct gpmc_timings gpmc_t;
  1498. struct omap_nand_platform_data *gpmc_nand_data;
  1499. if (of_property_read_u32(child, "reg", &val) < 0) {
  1500. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1501. child->full_name);
  1502. return -ENODEV;
  1503. }
  1504. gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
  1505. GFP_KERNEL);
  1506. if (!gpmc_nand_data)
  1507. return -ENOMEM;
  1508. gpmc_nand_data->cs = val;
  1509. gpmc_nand_data->of_node = child;
  1510. /* Detect availability of ELM module */
  1511. gpmc_nand_data->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
  1512. if (gpmc_nand_data->elm_of_node == NULL)
  1513. gpmc_nand_data->elm_of_node =
  1514. of_parse_phandle(child, "elm_id", 0);
  1515. /* select ecc-scheme for NAND */
  1516. if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
  1517. pr_err("%s: ti,nand-ecc-opt not found\n", __func__);
  1518. return -ENODEV;
  1519. }
  1520. if (!strcmp(s, "sw"))
  1521. gpmc_nand_data->ecc_opt = OMAP_ECC_HAM1_CODE_SW;
  1522. else if (!strcmp(s, "ham1") ||
  1523. !strcmp(s, "hw") || !strcmp(s, "hw-romcode"))
  1524. gpmc_nand_data->ecc_opt =
  1525. OMAP_ECC_HAM1_CODE_HW;
  1526. else if (!strcmp(s, "bch4"))
  1527. if (gpmc_nand_data->elm_of_node)
  1528. gpmc_nand_data->ecc_opt =
  1529. OMAP_ECC_BCH4_CODE_HW;
  1530. else
  1531. gpmc_nand_data->ecc_opt =
  1532. OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
  1533. else if (!strcmp(s, "bch8"))
  1534. if (gpmc_nand_data->elm_of_node)
  1535. gpmc_nand_data->ecc_opt =
  1536. OMAP_ECC_BCH8_CODE_HW;
  1537. else
  1538. gpmc_nand_data->ecc_opt =
  1539. OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
  1540. else if (!strcmp(s, "bch16"))
  1541. if (gpmc_nand_data->elm_of_node)
  1542. gpmc_nand_data->ecc_opt =
  1543. OMAP_ECC_BCH16_CODE_HW;
  1544. else
  1545. pr_err("%s: BCH16 requires ELM support\n", __func__);
  1546. else
  1547. pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__);
  1548. /* select data transfer mode for NAND controller */
  1549. if (!of_property_read_string(child, "ti,nand-xfer-type", &s))
  1550. for (val = 0; val < ARRAY_SIZE(nand_xfer_types); val++)
  1551. if (!strcasecmp(s, nand_xfer_types[val])) {
  1552. gpmc_nand_data->xfer_type = val;
  1553. break;
  1554. }
  1555. gpmc_nand_data->flash_bbt = of_get_nand_on_flash_bbt(child);
  1556. val = of_get_nand_bus_width(child);
  1557. if (val == 16)
  1558. gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
  1559. gpmc_read_timings_dt(child, &gpmc_t);
  1560. gpmc_nand_init(gpmc_nand_data, &gpmc_t);
  1561. return 0;
  1562. }
  1563. #else
  1564. static int gpmc_probe_nand_child(struct platform_device *pdev,
  1565. struct device_node *child)
  1566. {
  1567. return 0;
  1568. }
  1569. #endif
  1570. #if IS_ENABLED(CONFIG_MTD_ONENAND)
  1571. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1572. struct device_node *child)
  1573. {
  1574. u32 val;
  1575. struct omap_onenand_platform_data *gpmc_onenand_data;
  1576. if (of_property_read_u32(child, "reg", &val) < 0) {
  1577. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1578. child->full_name);
  1579. return -ENODEV;
  1580. }
  1581. gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
  1582. GFP_KERNEL);
  1583. if (!gpmc_onenand_data)
  1584. return -ENOMEM;
  1585. gpmc_onenand_data->cs = val;
  1586. gpmc_onenand_data->of_node = child;
  1587. gpmc_onenand_data->dma_channel = -1;
  1588. if (!of_property_read_u32(child, "dma-channel", &val))
  1589. gpmc_onenand_data->dma_channel = val;
  1590. return gpmc_onenand_init(gpmc_onenand_data);
  1591. }
  1592. #else
  1593. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1594. struct device_node *child)
  1595. {
  1596. return 0;
  1597. }
  1598. #endif
  1599. /**
  1600. * gpmc_probe_generic_child - configures the gpmc for a child device
  1601. * @pdev: pointer to gpmc platform device
  1602. * @child: pointer to device-tree node for child device
  1603. *
  1604. * Allocates and configures a GPMC chip-select for a child device.
  1605. * Returns 0 on success and appropriate negative error code on failure.
  1606. */
  1607. static int gpmc_probe_generic_child(struct platform_device *pdev,
  1608. struct device_node *child)
  1609. {
  1610. struct gpmc_settings gpmc_s;
  1611. struct gpmc_timings gpmc_t;
  1612. struct resource res;
  1613. unsigned long base;
  1614. const char *name;
  1615. int ret, cs;
  1616. u32 val;
  1617. if (of_property_read_u32(child, "reg", &cs) < 0) {
  1618. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1619. child->full_name);
  1620. return -ENODEV;
  1621. }
  1622. if (of_address_to_resource(child, 0, &res) < 0) {
  1623. dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
  1624. child->full_name);
  1625. return -ENODEV;
  1626. }
  1627. /*
  1628. * Check if we have multiple instances of the same device
  1629. * on a single chip select. If so, use the already initialized
  1630. * timings.
  1631. */
  1632. name = gpmc_cs_get_name(cs);
  1633. if (name && child->name && of_node_cmp(child->name, name) == 0)
  1634. goto no_timings;
  1635. ret = gpmc_cs_request(cs, resource_size(&res), &base);
  1636. if (ret < 0) {
  1637. dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
  1638. return ret;
  1639. }
  1640. gpmc_cs_set_name(cs, child->name);
  1641. gpmc_read_settings_dt(child, &gpmc_s);
  1642. gpmc_read_timings_dt(child, &gpmc_t);
  1643. /*
  1644. * For some GPMC devices we still need to rely on the bootloader
  1645. * timings because the devices can be connected via FPGA.
  1646. * REVISIT: Add timing support from slls644g.pdf.
  1647. */
  1648. if (!gpmc_t.cs_rd_off) {
  1649. WARN(1, "enable GPMC debug to configure .dts timings for CS%i\n",
  1650. cs);
  1651. gpmc_cs_show_timings(cs,
  1652. "please add GPMC bootloader timings to .dts");
  1653. goto no_timings;
  1654. }
  1655. /* CS must be disabled while making changes to gpmc configuration */
  1656. gpmc_cs_disable_mem(cs);
  1657. /*
  1658. * FIXME: gpmc_cs_request() will map the CS to an arbitary
  1659. * location in the gpmc address space. When booting with
  1660. * device-tree we want the NOR flash to be mapped to the
  1661. * location specified in the device-tree blob. So remap the
  1662. * CS to this location. Once DT migration is complete should
  1663. * just make gpmc_cs_request() map a specific address.
  1664. */
  1665. ret = gpmc_cs_remap(cs, res.start);
  1666. if (ret < 0) {
  1667. dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
  1668. cs, &res.start);
  1669. goto err;
  1670. }
  1671. ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
  1672. if (ret < 0)
  1673. goto err;
  1674. gpmc_cs_show_timings(cs, "before gpmc_cs_program_settings");
  1675. ret = gpmc_cs_program_settings(cs, &gpmc_s);
  1676. if (ret < 0)
  1677. goto err;
  1678. ret = gpmc_cs_set_timings(cs, &gpmc_t, &gpmc_s);
  1679. if (ret) {
  1680. dev_err(&pdev->dev, "failed to set gpmc timings for: %s\n",
  1681. child->name);
  1682. goto err;
  1683. }
  1684. /* Clear limited address i.e. enable A26-A11 */
  1685. val = gpmc_read_reg(GPMC_CONFIG);
  1686. val &= ~GPMC_CONFIG_LIMITEDADDRESS;
  1687. gpmc_write_reg(GPMC_CONFIG, val);
  1688. /* Enable CS region */
  1689. gpmc_cs_enable_mem(cs);
  1690. no_timings:
  1691. /* create platform device, NULL on error or when disabled */
  1692. if (!of_platform_device_create(child, NULL, &pdev->dev))
  1693. goto err_child_fail;
  1694. /* is child a common bus? */
  1695. if (of_match_node(of_default_bus_match_table, child))
  1696. /* create children and other common bus children */
  1697. if (of_platform_populate(child, of_default_bus_match_table,
  1698. NULL, &pdev->dev))
  1699. goto err_child_fail;
  1700. return 0;
  1701. err_child_fail:
  1702. dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
  1703. ret = -ENODEV;
  1704. err:
  1705. gpmc_cs_free(cs);
  1706. return ret;
  1707. }
  1708. static int gpmc_probe_dt(struct platform_device *pdev)
  1709. {
  1710. int ret;
  1711. struct device_node *child;
  1712. const struct of_device_id *of_id =
  1713. of_match_device(gpmc_dt_ids, &pdev->dev);
  1714. if (!of_id)
  1715. return 0;
  1716. ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
  1717. &gpmc_cs_num);
  1718. if (ret < 0) {
  1719. pr_err("%s: number of chip-selects not defined\n", __func__);
  1720. return ret;
  1721. } else if (gpmc_cs_num < 1) {
  1722. pr_err("%s: all chip-selects are disabled\n", __func__);
  1723. return -EINVAL;
  1724. } else if (gpmc_cs_num > GPMC_CS_NUM) {
  1725. pr_err("%s: number of supported chip-selects cannot be > %d\n",
  1726. __func__, GPMC_CS_NUM);
  1727. return -EINVAL;
  1728. }
  1729. ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
  1730. &gpmc_nr_waitpins);
  1731. if (ret < 0) {
  1732. pr_err("%s: number of wait pins not found!\n", __func__);
  1733. return ret;
  1734. }
  1735. for_each_available_child_of_node(pdev->dev.of_node, child) {
  1736. if (!child->name)
  1737. continue;
  1738. if (of_node_cmp(child->name, "nand") == 0)
  1739. ret = gpmc_probe_nand_child(pdev, child);
  1740. else if (of_node_cmp(child->name, "onenand") == 0)
  1741. ret = gpmc_probe_onenand_child(pdev, child);
  1742. else
  1743. ret = gpmc_probe_generic_child(pdev, child);
  1744. }
  1745. return 0;
  1746. }
  1747. #else
  1748. static int gpmc_probe_dt(struct platform_device *pdev)
  1749. {
  1750. return 0;
  1751. }
  1752. #endif
  1753. static int gpmc_probe(struct platform_device *pdev)
  1754. {
  1755. int rc;
  1756. u32 l;
  1757. struct resource *res;
  1758. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1759. if (res == NULL)
  1760. return -ENOENT;
  1761. phys_base = res->start;
  1762. mem_size = resource_size(res);
  1763. gpmc_base = devm_ioremap_resource(&pdev->dev, res);
  1764. if (IS_ERR(gpmc_base))
  1765. return PTR_ERR(gpmc_base);
  1766. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1767. if (res == NULL)
  1768. dev_warn(&pdev->dev, "Failed to get resource: irq\n");
  1769. else
  1770. gpmc_irq = res->start;
  1771. gpmc_l3_clk = devm_clk_get(&pdev->dev, "fck");
  1772. if (IS_ERR(gpmc_l3_clk)) {
  1773. dev_err(&pdev->dev, "Failed to get GPMC fck\n");
  1774. gpmc_irq = 0;
  1775. return PTR_ERR(gpmc_l3_clk);
  1776. }
  1777. if (!clk_get_rate(gpmc_l3_clk)) {
  1778. dev_err(&pdev->dev, "Invalid GPMC fck clock rate\n");
  1779. return -EINVAL;
  1780. }
  1781. pm_runtime_enable(&pdev->dev);
  1782. pm_runtime_get_sync(&pdev->dev);
  1783. gpmc_dev = &pdev->dev;
  1784. l = gpmc_read_reg(GPMC_REVISION);
  1785. /*
  1786. * FIXME: Once device-tree migration is complete the below flags
  1787. * should be populated based upon the device-tree compatible
  1788. * string. For now just use the IP revision. OMAP3+ devices have
  1789. * the wr_access and wr_data_mux_bus register fields. OMAP4+
  1790. * devices support the addr-addr-data multiplex protocol.
  1791. *
  1792. * GPMC IP revisions:
  1793. * - OMAP24xx = 2.0
  1794. * - OMAP3xxx = 5.0
  1795. * - OMAP44xx/54xx/AM335x = 6.0
  1796. */
  1797. if (GPMC_REVISION_MAJOR(l) > 0x4)
  1798. gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
  1799. if (GPMC_REVISION_MAJOR(l) > 0x5)
  1800. gpmc_capability |= GPMC_HAS_MUX_AAD;
  1801. dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
  1802. GPMC_REVISION_MINOR(l));
  1803. gpmc_mem_init();
  1804. if (gpmc_setup_irq() < 0)
  1805. dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
  1806. if (!pdev->dev.of_node) {
  1807. gpmc_cs_num = GPMC_CS_NUM;
  1808. gpmc_nr_waitpins = GPMC_NR_WAITPINS;
  1809. }
  1810. rc = gpmc_probe_dt(pdev);
  1811. if (rc < 0) {
  1812. pm_runtime_put_sync(&pdev->dev);
  1813. dev_err(gpmc_dev, "failed to probe DT parameters\n");
  1814. return rc;
  1815. }
  1816. return 0;
  1817. }
  1818. static int gpmc_remove(struct platform_device *pdev)
  1819. {
  1820. gpmc_free_irq();
  1821. gpmc_mem_exit();
  1822. pm_runtime_put_sync(&pdev->dev);
  1823. pm_runtime_disable(&pdev->dev);
  1824. gpmc_dev = NULL;
  1825. return 0;
  1826. }
  1827. #ifdef CONFIG_PM_SLEEP
  1828. static int gpmc_suspend(struct device *dev)
  1829. {
  1830. omap3_gpmc_save_context();
  1831. pm_runtime_put_sync(dev);
  1832. return 0;
  1833. }
  1834. static int gpmc_resume(struct device *dev)
  1835. {
  1836. pm_runtime_get_sync(dev);
  1837. omap3_gpmc_restore_context();
  1838. return 0;
  1839. }
  1840. #endif
  1841. static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
  1842. static struct platform_driver gpmc_driver = {
  1843. .probe = gpmc_probe,
  1844. .remove = gpmc_remove,
  1845. .driver = {
  1846. .name = DEVICE_NAME,
  1847. .of_match_table = of_match_ptr(gpmc_dt_ids),
  1848. .pm = &gpmc_pm_ops,
  1849. },
  1850. };
  1851. static __init int gpmc_init(void)
  1852. {
  1853. return platform_driver_register(&gpmc_driver);
  1854. }
  1855. static __exit void gpmc_exit(void)
  1856. {
  1857. platform_driver_unregister(&gpmc_driver);
  1858. }
  1859. postcore_initcall(gpmc_init);
  1860. module_exit(gpmc_exit);
  1861. static irqreturn_t gpmc_handle_irq(int irq, void *dev)
  1862. {
  1863. int i;
  1864. u32 regval;
  1865. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  1866. if (!regval)
  1867. return IRQ_NONE;
  1868. for (i = 0; i < GPMC_NR_IRQ; i++)
  1869. if (regval & gpmc_client_irq[i].bitmask)
  1870. generic_handle_irq(gpmc_client_irq[i].irq);
  1871. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  1872. return IRQ_HANDLED;
  1873. }
  1874. static struct omap3_gpmc_regs gpmc_context;
  1875. void omap3_gpmc_save_context(void)
  1876. {
  1877. int i;
  1878. if (!gpmc_base)
  1879. return;
  1880. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  1881. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  1882. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  1883. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  1884. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  1885. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  1886. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  1887. for (i = 0; i < gpmc_cs_num; i++) {
  1888. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  1889. if (gpmc_context.cs_context[i].is_valid) {
  1890. gpmc_context.cs_context[i].config1 =
  1891. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  1892. gpmc_context.cs_context[i].config2 =
  1893. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  1894. gpmc_context.cs_context[i].config3 =
  1895. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  1896. gpmc_context.cs_context[i].config4 =
  1897. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  1898. gpmc_context.cs_context[i].config5 =
  1899. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  1900. gpmc_context.cs_context[i].config6 =
  1901. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  1902. gpmc_context.cs_context[i].config7 =
  1903. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  1904. }
  1905. }
  1906. }
  1907. void omap3_gpmc_restore_context(void)
  1908. {
  1909. int i;
  1910. if (!gpmc_base)
  1911. return;
  1912. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  1913. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  1914. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  1915. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  1916. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  1917. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  1918. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  1919. for (i = 0; i < gpmc_cs_num; i++) {
  1920. if (gpmc_context.cs_context[i].is_valid) {
  1921. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  1922. gpmc_context.cs_context[i].config1);
  1923. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  1924. gpmc_context.cs_context[i].config2);
  1925. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  1926. gpmc_context.cs_context[i].config3);
  1927. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  1928. gpmc_context.cs_context[i].config4);
  1929. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  1930. gpmc_context.cs_context[i].config5);
  1931. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  1932. gpmc_context.cs_context[i].config6);
  1933. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  1934. gpmc_context.cs_context[i].config7);
  1935. }
  1936. }
  1937. }