omap-gpmc.c 69 KB

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