omap-gpmc.c 74 KB

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