tegra124-emc.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * Author:
  6. * Mikko Perttunen <[email protected]>
  7. */
  8. #include <linux/clk-provider.h>
  9. #include <linux/clk.h>
  10. #include <linux/clkdev.h>
  11. #include <linux/clk/tegra.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/delay.h>
  14. #include <linux/interconnect-provider.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_opp.h>
  22. #include <linux/sort.h>
  23. #include <linux/string.h>
  24. #include <soc/tegra/fuse.h>
  25. #include <soc/tegra/mc.h>
  26. #include "mc.h"
  27. #define EMC_FBIO_CFG5 0x104
  28. #define EMC_FBIO_CFG5_DRAM_TYPE_MASK 0x3
  29. #define EMC_FBIO_CFG5_DRAM_TYPE_SHIFT 0
  30. #define EMC_FBIO_CFG5_DRAM_WIDTH_X64 BIT(4)
  31. #define EMC_INTSTATUS 0x0
  32. #define EMC_INTSTATUS_CLKCHANGE_COMPLETE BIT(4)
  33. #define EMC_CFG 0xc
  34. #define EMC_CFG_DRAM_CLKSTOP_PD BIT(31)
  35. #define EMC_CFG_DRAM_CLKSTOP_SR BIT(30)
  36. #define EMC_CFG_DRAM_ACPD BIT(29)
  37. #define EMC_CFG_DYN_SREF BIT(28)
  38. #define EMC_CFG_PWR_MASK ((0xF << 28) | BIT(18))
  39. #define EMC_CFG_DSR_VTTGEN_DRV_EN BIT(18)
  40. #define EMC_REFCTRL 0x20
  41. #define EMC_REFCTRL_DEV_SEL_SHIFT 0
  42. #define EMC_REFCTRL_ENABLE BIT(31)
  43. #define EMC_TIMING_CONTROL 0x28
  44. #define EMC_RC 0x2c
  45. #define EMC_RFC 0x30
  46. #define EMC_RAS 0x34
  47. #define EMC_RP 0x38
  48. #define EMC_R2W 0x3c
  49. #define EMC_W2R 0x40
  50. #define EMC_R2P 0x44
  51. #define EMC_W2P 0x48
  52. #define EMC_RD_RCD 0x4c
  53. #define EMC_WR_RCD 0x50
  54. #define EMC_RRD 0x54
  55. #define EMC_REXT 0x58
  56. #define EMC_WDV 0x5c
  57. #define EMC_QUSE 0x60
  58. #define EMC_QRST 0x64
  59. #define EMC_QSAFE 0x68
  60. #define EMC_RDV 0x6c
  61. #define EMC_REFRESH 0x70
  62. #define EMC_BURST_REFRESH_NUM 0x74
  63. #define EMC_PDEX2WR 0x78
  64. #define EMC_PDEX2RD 0x7c
  65. #define EMC_PCHG2PDEN 0x80
  66. #define EMC_ACT2PDEN 0x84
  67. #define EMC_AR2PDEN 0x88
  68. #define EMC_RW2PDEN 0x8c
  69. #define EMC_TXSR 0x90
  70. #define EMC_TCKE 0x94
  71. #define EMC_TFAW 0x98
  72. #define EMC_TRPAB 0x9c
  73. #define EMC_TCLKSTABLE 0xa0
  74. #define EMC_TCLKSTOP 0xa4
  75. #define EMC_TREFBW 0xa8
  76. #define EMC_ODT_WRITE 0xb0
  77. #define EMC_ODT_READ 0xb4
  78. #define EMC_WEXT 0xb8
  79. #define EMC_CTT 0xbc
  80. #define EMC_RFC_SLR 0xc0
  81. #define EMC_MRS_WAIT_CNT2 0xc4
  82. #define EMC_MRS_WAIT_CNT 0xc8
  83. #define EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT 0
  84. #define EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK \
  85. (0x3FF << EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT)
  86. #define EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT 16
  87. #define EMC_MRS_WAIT_CNT_LONG_WAIT_MASK \
  88. (0x3FF << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT)
  89. #define EMC_MRS 0xcc
  90. #define EMC_MODE_SET_DLL_RESET BIT(8)
  91. #define EMC_MODE_SET_LONG_CNT BIT(26)
  92. #define EMC_EMRS 0xd0
  93. #define EMC_REF 0xd4
  94. #define EMC_PRE 0xd8
  95. #define EMC_SELF_REF 0xe0
  96. #define EMC_SELF_REF_CMD_ENABLED BIT(0)
  97. #define EMC_SELF_REF_DEV_SEL_SHIFT 30
  98. #define EMC_MRW 0xe8
  99. #define EMC_MRR 0xec
  100. #define EMC_MRR_MA_SHIFT 16
  101. #define LPDDR2_MR4_TEMP_SHIFT 0
  102. #define EMC_XM2DQSPADCTRL3 0xf8
  103. #define EMC_FBIO_SPARE 0x100
  104. #define EMC_FBIO_CFG6 0x114
  105. #define EMC_EMRS2 0x12c
  106. #define EMC_MRW2 0x134
  107. #define EMC_MRW4 0x13c
  108. #define EMC_EINPUT 0x14c
  109. #define EMC_EINPUT_DURATION 0x150
  110. #define EMC_PUTERM_EXTRA 0x154
  111. #define EMC_TCKESR 0x158
  112. #define EMC_TPD 0x15c
  113. #define EMC_AUTO_CAL_CONFIG 0x2a4
  114. #define EMC_AUTO_CAL_CONFIG_AUTO_CAL_START BIT(31)
  115. #define EMC_AUTO_CAL_INTERVAL 0x2a8
  116. #define EMC_AUTO_CAL_STATUS 0x2ac
  117. #define EMC_AUTO_CAL_STATUS_ACTIVE BIT(31)
  118. #define EMC_STATUS 0x2b4
  119. #define EMC_STATUS_TIMING_UPDATE_STALLED BIT(23)
  120. #define EMC_CFG_2 0x2b8
  121. #define EMC_CFG_2_MODE_SHIFT 0
  122. #define EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR BIT(6)
  123. #define EMC_CFG_DIG_DLL 0x2bc
  124. #define EMC_CFG_DIG_DLL_PERIOD 0x2c0
  125. #define EMC_RDV_MASK 0x2cc
  126. #define EMC_WDV_MASK 0x2d0
  127. #define EMC_CTT_DURATION 0x2d8
  128. #define EMC_CTT_TERM_CTRL 0x2dc
  129. #define EMC_ZCAL_INTERVAL 0x2e0
  130. #define EMC_ZCAL_WAIT_CNT 0x2e4
  131. #define EMC_ZQ_CAL 0x2ec
  132. #define EMC_ZQ_CAL_CMD BIT(0)
  133. #define EMC_ZQ_CAL_LONG BIT(4)
  134. #define EMC_ZQ_CAL_LONG_CMD_DEV0 \
  135. (DRAM_DEV_SEL_0 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
  136. #define EMC_ZQ_CAL_LONG_CMD_DEV1 \
  137. (DRAM_DEV_SEL_1 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
  138. #define EMC_XM2CMDPADCTRL 0x2f0
  139. #define EMC_XM2DQSPADCTRL 0x2f8
  140. #define EMC_XM2DQSPADCTRL2 0x2fc
  141. #define EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE BIT(0)
  142. #define EMC_XM2DQSPADCTRL2_VREF_ENABLE BIT(5)
  143. #define EMC_XM2DQPADCTRL 0x300
  144. #define EMC_XM2DQPADCTRL2 0x304
  145. #define EMC_XM2CLKPADCTRL 0x308
  146. #define EMC_XM2COMPPADCTRL 0x30c
  147. #define EMC_XM2VTTGENPADCTRL 0x310
  148. #define EMC_XM2VTTGENPADCTRL2 0x314
  149. #define EMC_XM2VTTGENPADCTRL3 0x318
  150. #define EMC_XM2DQSPADCTRL4 0x320
  151. #define EMC_DLL_XFORM_DQS0 0x328
  152. #define EMC_DLL_XFORM_DQS1 0x32c
  153. #define EMC_DLL_XFORM_DQS2 0x330
  154. #define EMC_DLL_XFORM_DQS3 0x334
  155. #define EMC_DLL_XFORM_DQS4 0x338
  156. #define EMC_DLL_XFORM_DQS5 0x33c
  157. #define EMC_DLL_XFORM_DQS6 0x340
  158. #define EMC_DLL_XFORM_DQS7 0x344
  159. #define EMC_DLL_XFORM_QUSE0 0x348
  160. #define EMC_DLL_XFORM_QUSE1 0x34c
  161. #define EMC_DLL_XFORM_QUSE2 0x350
  162. #define EMC_DLL_XFORM_QUSE3 0x354
  163. #define EMC_DLL_XFORM_QUSE4 0x358
  164. #define EMC_DLL_XFORM_QUSE5 0x35c
  165. #define EMC_DLL_XFORM_QUSE6 0x360
  166. #define EMC_DLL_XFORM_QUSE7 0x364
  167. #define EMC_DLL_XFORM_DQ0 0x368
  168. #define EMC_DLL_XFORM_DQ1 0x36c
  169. #define EMC_DLL_XFORM_DQ2 0x370
  170. #define EMC_DLL_XFORM_DQ3 0x374
  171. #define EMC_DLI_TRIM_TXDQS0 0x3a8
  172. #define EMC_DLI_TRIM_TXDQS1 0x3ac
  173. #define EMC_DLI_TRIM_TXDQS2 0x3b0
  174. #define EMC_DLI_TRIM_TXDQS3 0x3b4
  175. #define EMC_DLI_TRIM_TXDQS4 0x3b8
  176. #define EMC_DLI_TRIM_TXDQS5 0x3bc
  177. #define EMC_DLI_TRIM_TXDQS6 0x3c0
  178. #define EMC_DLI_TRIM_TXDQS7 0x3c4
  179. #define EMC_STALL_THEN_EXE_AFTER_CLKCHANGE 0x3cc
  180. #define EMC_SEL_DPD_CTRL 0x3d8
  181. #define EMC_SEL_DPD_CTRL_DATA_SEL_DPD BIT(8)
  182. #define EMC_SEL_DPD_CTRL_ODT_SEL_DPD BIT(5)
  183. #define EMC_SEL_DPD_CTRL_RESET_SEL_DPD BIT(4)
  184. #define EMC_SEL_DPD_CTRL_CA_SEL_DPD BIT(3)
  185. #define EMC_SEL_DPD_CTRL_CLK_SEL_DPD BIT(2)
  186. #define EMC_SEL_DPD_CTRL_DDR3_MASK \
  187. ((0xf << 2) | BIT(8))
  188. #define EMC_SEL_DPD_CTRL_MASK \
  189. ((0x3 << 2) | BIT(5) | BIT(8))
  190. #define EMC_PRE_REFRESH_REQ_CNT 0x3dc
  191. #define EMC_DYN_SELF_REF_CONTROL 0x3e0
  192. #define EMC_TXSRDLL 0x3e4
  193. #define EMC_CCFIFO_ADDR 0x3e8
  194. #define EMC_CCFIFO_DATA 0x3ec
  195. #define EMC_CCFIFO_STATUS 0x3f0
  196. #define EMC_CDB_CNTL_1 0x3f4
  197. #define EMC_CDB_CNTL_2 0x3f8
  198. #define EMC_XM2CLKPADCTRL2 0x3fc
  199. #define EMC_AUTO_CAL_CONFIG2 0x458
  200. #define EMC_AUTO_CAL_CONFIG3 0x45c
  201. #define EMC_IBDLY 0x468
  202. #define EMC_DLL_XFORM_ADDR0 0x46c
  203. #define EMC_DLL_XFORM_ADDR1 0x470
  204. #define EMC_DLL_XFORM_ADDR2 0x474
  205. #define EMC_DSR_VTTGEN_DRV 0x47c
  206. #define EMC_TXDSRVTTGEN 0x480
  207. #define EMC_XM2CMDPADCTRL4 0x484
  208. #define EMC_XM2CMDPADCTRL5 0x488
  209. #define EMC_DLL_XFORM_DQS8 0x4a0
  210. #define EMC_DLL_XFORM_DQS9 0x4a4
  211. #define EMC_DLL_XFORM_DQS10 0x4a8
  212. #define EMC_DLL_XFORM_DQS11 0x4ac
  213. #define EMC_DLL_XFORM_DQS12 0x4b0
  214. #define EMC_DLL_XFORM_DQS13 0x4b4
  215. #define EMC_DLL_XFORM_DQS14 0x4b8
  216. #define EMC_DLL_XFORM_DQS15 0x4bc
  217. #define EMC_DLL_XFORM_QUSE8 0x4c0
  218. #define EMC_DLL_XFORM_QUSE9 0x4c4
  219. #define EMC_DLL_XFORM_QUSE10 0x4c8
  220. #define EMC_DLL_XFORM_QUSE11 0x4cc
  221. #define EMC_DLL_XFORM_QUSE12 0x4d0
  222. #define EMC_DLL_XFORM_QUSE13 0x4d4
  223. #define EMC_DLL_XFORM_QUSE14 0x4d8
  224. #define EMC_DLL_XFORM_QUSE15 0x4dc
  225. #define EMC_DLL_XFORM_DQ4 0x4e0
  226. #define EMC_DLL_XFORM_DQ5 0x4e4
  227. #define EMC_DLL_XFORM_DQ6 0x4e8
  228. #define EMC_DLL_XFORM_DQ7 0x4ec
  229. #define EMC_DLI_TRIM_TXDQS8 0x520
  230. #define EMC_DLI_TRIM_TXDQS9 0x524
  231. #define EMC_DLI_TRIM_TXDQS10 0x528
  232. #define EMC_DLI_TRIM_TXDQS11 0x52c
  233. #define EMC_DLI_TRIM_TXDQS12 0x530
  234. #define EMC_DLI_TRIM_TXDQS13 0x534
  235. #define EMC_DLI_TRIM_TXDQS14 0x538
  236. #define EMC_DLI_TRIM_TXDQS15 0x53c
  237. #define EMC_CDB_CNTL_3 0x540
  238. #define EMC_XM2DQSPADCTRL5 0x544
  239. #define EMC_XM2DQSPADCTRL6 0x548
  240. #define EMC_XM2DQPADCTRL3 0x54c
  241. #define EMC_DLL_XFORM_ADDR3 0x550
  242. #define EMC_DLL_XFORM_ADDR4 0x554
  243. #define EMC_DLL_XFORM_ADDR5 0x558
  244. #define EMC_CFG_PIPE 0x560
  245. #define EMC_QPOP 0x564
  246. #define EMC_QUSE_WIDTH 0x568
  247. #define EMC_PUTERM_WIDTH 0x56c
  248. #define EMC_BGBIAS_CTL0 0x570
  249. #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX BIT(3)
  250. #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN BIT(2)
  251. #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD BIT(1)
  252. #define EMC_PUTERM_ADJ 0x574
  253. #define DRAM_DEV_SEL_ALL 0
  254. #define DRAM_DEV_SEL_0 BIT(31)
  255. #define DRAM_DEV_SEL_1 BIT(30)
  256. #define EMC_CFG_POWER_FEATURES_MASK \
  257. (EMC_CFG_DYN_SREF | EMC_CFG_DRAM_ACPD | EMC_CFG_DRAM_CLKSTOP_SR | \
  258. EMC_CFG_DRAM_CLKSTOP_PD | EMC_CFG_DSR_VTTGEN_DRV_EN)
  259. #define EMC_REFCTRL_DEV_SEL(n) (((n > 1) ? 0 : 2) << EMC_REFCTRL_DEV_SEL_SHIFT)
  260. #define EMC_DRAM_DEV_SEL(n) ((n > 1) ? DRAM_DEV_SEL_ALL : DRAM_DEV_SEL_0)
  261. /* Maximum amount of time in us. to wait for changes to become effective */
  262. #define EMC_STATUS_UPDATE_TIMEOUT 1000
  263. enum emc_dram_type {
  264. DRAM_TYPE_DDR3 = 0,
  265. DRAM_TYPE_DDR1 = 1,
  266. DRAM_TYPE_LPDDR3 = 2,
  267. DRAM_TYPE_DDR2 = 3
  268. };
  269. enum emc_dll_change {
  270. DLL_CHANGE_NONE,
  271. DLL_CHANGE_ON,
  272. DLL_CHANGE_OFF
  273. };
  274. static const unsigned long emc_burst_regs[] = {
  275. EMC_RC,
  276. EMC_RFC,
  277. EMC_RFC_SLR,
  278. EMC_RAS,
  279. EMC_RP,
  280. EMC_R2W,
  281. EMC_W2R,
  282. EMC_R2P,
  283. EMC_W2P,
  284. EMC_RD_RCD,
  285. EMC_WR_RCD,
  286. EMC_RRD,
  287. EMC_REXT,
  288. EMC_WEXT,
  289. EMC_WDV,
  290. EMC_WDV_MASK,
  291. EMC_QUSE,
  292. EMC_QUSE_WIDTH,
  293. EMC_IBDLY,
  294. EMC_EINPUT,
  295. EMC_EINPUT_DURATION,
  296. EMC_PUTERM_EXTRA,
  297. EMC_PUTERM_WIDTH,
  298. EMC_PUTERM_ADJ,
  299. EMC_CDB_CNTL_1,
  300. EMC_CDB_CNTL_2,
  301. EMC_CDB_CNTL_3,
  302. EMC_QRST,
  303. EMC_QSAFE,
  304. EMC_RDV,
  305. EMC_RDV_MASK,
  306. EMC_REFRESH,
  307. EMC_BURST_REFRESH_NUM,
  308. EMC_PRE_REFRESH_REQ_CNT,
  309. EMC_PDEX2WR,
  310. EMC_PDEX2RD,
  311. EMC_PCHG2PDEN,
  312. EMC_ACT2PDEN,
  313. EMC_AR2PDEN,
  314. EMC_RW2PDEN,
  315. EMC_TXSR,
  316. EMC_TXSRDLL,
  317. EMC_TCKE,
  318. EMC_TCKESR,
  319. EMC_TPD,
  320. EMC_TFAW,
  321. EMC_TRPAB,
  322. EMC_TCLKSTABLE,
  323. EMC_TCLKSTOP,
  324. EMC_TREFBW,
  325. EMC_FBIO_CFG6,
  326. EMC_ODT_WRITE,
  327. EMC_ODT_READ,
  328. EMC_FBIO_CFG5,
  329. EMC_CFG_DIG_DLL,
  330. EMC_CFG_DIG_DLL_PERIOD,
  331. EMC_DLL_XFORM_DQS0,
  332. EMC_DLL_XFORM_DQS1,
  333. EMC_DLL_XFORM_DQS2,
  334. EMC_DLL_XFORM_DQS3,
  335. EMC_DLL_XFORM_DQS4,
  336. EMC_DLL_XFORM_DQS5,
  337. EMC_DLL_XFORM_DQS6,
  338. EMC_DLL_XFORM_DQS7,
  339. EMC_DLL_XFORM_DQS8,
  340. EMC_DLL_XFORM_DQS9,
  341. EMC_DLL_XFORM_DQS10,
  342. EMC_DLL_XFORM_DQS11,
  343. EMC_DLL_XFORM_DQS12,
  344. EMC_DLL_XFORM_DQS13,
  345. EMC_DLL_XFORM_DQS14,
  346. EMC_DLL_XFORM_DQS15,
  347. EMC_DLL_XFORM_QUSE0,
  348. EMC_DLL_XFORM_QUSE1,
  349. EMC_DLL_XFORM_QUSE2,
  350. EMC_DLL_XFORM_QUSE3,
  351. EMC_DLL_XFORM_QUSE4,
  352. EMC_DLL_XFORM_QUSE5,
  353. EMC_DLL_XFORM_QUSE6,
  354. EMC_DLL_XFORM_QUSE7,
  355. EMC_DLL_XFORM_ADDR0,
  356. EMC_DLL_XFORM_ADDR1,
  357. EMC_DLL_XFORM_ADDR2,
  358. EMC_DLL_XFORM_ADDR3,
  359. EMC_DLL_XFORM_ADDR4,
  360. EMC_DLL_XFORM_ADDR5,
  361. EMC_DLL_XFORM_QUSE8,
  362. EMC_DLL_XFORM_QUSE9,
  363. EMC_DLL_XFORM_QUSE10,
  364. EMC_DLL_XFORM_QUSE11,
  365. EMC_DLL_XFORM_QUSE12,
  366. EMC_DLL_XFORM_QUSE13,
  367. EMC_DLL_XFORM_QUSE14,
  368. EMC_DLL_XFORM_QUSE15,
  369. EMC_DLI_TRIM_TXDQS0,
  370. EMC_DLI_TRIM_TXDQS1,
  371. EMC_DLI_TRIM_TXDQS2,
  372. EMC_DLI_TRIM_TXDQS3,
  373. EMC_DLI_TRIM_TXDQS4,
  374. EMC_DLI_TRIM_TXDQS5,
  375. EMC_DLI_TRIM_TXDQS6,
  376. EMC_DLI_TRIM_TXDQS7,
  377. EMC_DLI_TRIM_TXDQS8,
  378. EMC_DLI_TRIM_TXDQS9,
  379. EMC_DLI_TRIM_TXDQS10,
  380. EMC_DLI_TRIM_TXDQS11,
  381. EMC_DLI_TRIM_TXDQS12,
  382. EMC_DLI_TRIM_TXDQS13,
  383. EMC_DLI_TRIM_TXDQS14,
  384. EMC_DLI_TRIM_TXDQS15,
  385. EMC_DLL_XFORM_DQ0,
  386. EMC_DLL_XFORM_DQ1,
  387. EMC_DLL_XFORM_DQ2,
  388. EMC_DLL_XFORM_DQ3,
  389. EMC_DLL_XFORM_DQ4,
  390. EMC_DLL_XFORM_DQ5,
  391. EMC_DLL_XFORM_DQ6,
  392. EMC_DLL_XFORM_DQ7,
  393. EMC_XM2CMDPADCTRL,
  394. EMC_XM2CMDPADCTRL4,
  395. EMC_XM2CMDPADCTRL5,
  396. EMC_XM2DQPADCTRL2,
  397. EMC_XM2DQPADCTRL3,
  398. EMC_XM2CLKPADCTRL,
  399. EMC_XM2CLKPADCTRL2,
  400. EMC_XM2COMPPADCTRL,
  401. EMC_XM2VTTGENPADCTRL,
  402. EMC_XM2VTTGENPADCTRL2,
  403. EMC_XM2VTTGENPADCTRL3,
  404. EMC_XM2DQSPADCTRL3,
  405. EMC_XM2DQSPADCTRL4,
  406. EMC_XM2DQSPADCTRL5,
  407. EMC_XM2DQSPADCTRL6,
  408. EMC_DSR_VTTGEN_DRV,
  409. EMC_TXDSRVTTGEN,
  410. EMC_FBIO_SPARE,
  411. EMC_ZCAL_WAIT_CNT,
  412. EMC_MRS_WAIT_CNT2,
  413. EMC_CTT,
  414. EMC_CTT_DURATION,
  415. EMC_CFG_PIPE,
  416. EMC_DYN_SELF_REF_CONTROL,
  417. EMC_QPOP
  418. };
  419. struct emc_timing {
  420. unsigned long rate;
  421. u32 emc_burst_data[ARRAY_SIZE(emc_burst_regs)];
  422. u32 emc_auto_cal_config;
  423. u32 emc_auto_cal_config2;
  424. u32 emc_auto_cal_config3;
  425. u32 emc_auto_cal_interval;
  426. u32 emc_bgbias_ctl0;
  427. u32 emc_cfg;
  428. u32 emc_cfg_2;
  429. u32 emc_ctt_term_ctrl;
  430. u32 emc_mode_1;
  431. u32 emc_mode_2;
  432. u32 emc_mode_4;
  433. u32 emc_mode_reset;
  434. u32 emc_mrs_wait_cnt;
  435. u32 emc_sel_dpd_ctrl;
  436. u32 emc_xm2dqspadctrl2;
  437. u32 emc_zcal_cnt_long;
  438. u32 emc_zcal_interval;
  439. };
  440. enum emc_rate_request_type {
  441. EMC_RATE_DEBUG,
  442. EMC_RATE_ICC,
  443. EMC_RATE_TYPE_MAX,
  444. };
  445. struct emc_rate_request {
  446. unsigned long min_rate;
  447. unsigned long max_rate;
  448. };
  449. struct tegra_emc {
  450. struct device *dev;
  451. struct tegra_mc *mc;
  452. void __iomem *regs;
  453. struct clk *clk;
  454. enum emc_dram_type dram_type;
  455. unsigned int dram_bus_width;
  456. unsigned int dram_num;
  457. struct emc_timing last_timing;
  458. struct emc_timing *timings;
  459. unsigned int num_timings;
  460. struct {
  461. struct dentry *root;
  462. unsigned long min_rate;
  463. unsigned long max_rate;
  464. } debugfs;
  465. struct icc_provider provider;
  466. /*
  467. * There are multiple sources in the EMC driver which could request
  468. * a min/max clock rate, these rates are contained in this array.
  469. */
  470. struct emc_rate_request requested_rate[EMC_RATE_TYPE_MAX];
  471. /* protect shared rate-change code path */
  472. struct mutex rate_lock;
  473. };
  474. /* Timing change sequence functions */
  475. static void emc_ccfifo_writel(struct tegra_emc *emc, u32 value,
  476. unsigned long offset)
  477. {
  478. writel(value, emc->regs + EMC_CCFIFO_DATA);
  479. writel(offset, emc->regs + EMC_CCFIFO_ADDR);
  480. }
  481. static void emc_seq_update_timing(struct tegra_emc *emc)
  482. {
  483. unsigned int i;
  484. u32 value;
  485. writel(1, emc->regs + EMC_TIMING_CONTROL);
  486. for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
  487. value = readl(emc->regs + EMC_STATUS);
  488. if ((value & EMC_STATUS_TIMING_UPDATE_STALLED) == 0)
  489. return;
  490. udelay(1);
  491. }
  492. dev_err(emc->dev, "timing update timed out\n");
  493. }
  494. static void emc_seq_disable_auto_cal(struct tegra_emc *emc)
  495. {
  496. unsigned int i;
  497. u32 value;
  498. writel(0, emc->regs + EMC_AUTO_CAL_INTERVAL);
  499. for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
  500. value = readl(emc->regs + EMC_AUTO_CAL_STATUS);
  501. if ((value & EMC_AUTO_CAL_STATUS_ACTIVE) == 0)
  502. return;
  503. udelay(1);
  504. }
  505. dev_err(emc->dev, "auto cal disable timed out\n");
  506. }
  507. static void emc_seq_wait_clkchange(struct tegra_emc *emc)
  508. {
  509. unsigned int i;
  510. u32 value;
  511. for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
  512. value = readl(emc->regs + EMC_INTSTATUS);
  513. if (value & EMC_INTSTATUS_CLKCHANGE_COMPLETE)
  514. return;
  515. udelay(1);
  516. }
  517. dev_err(emc->dev, "clock change timed out\n");
  518. }
  519. static struct emc_timing *tegra_emc_find_timing(struct tegra_emc *emc,
  520. unsigned long rate)
  521. {
  522. struct emc_timing *timing = NULL;
  523. unsigned int i;
  524. for (i = 0; i < emc->num_timings; i++) {
  525. if (emc->timings[i].rate == rate) {
  526. timing = &emc->timings[i];
  527. break;
  528. }
  529. }
  530. if (!timing) {
  531. dev_err(emc->dev, "no timing for rate %lu\n", rate);
  532. return NULL;
  533. }
  534. return timing;
  535. }
  536. static int tegra_emc_prepare_timing_change(struct tegra_emc *emc,
  537. unsigned long rate)
  538. {
  539. struct emc_timing *timing = tegra_emc_find_timing(emc, rate);
  540. struct emc_timing *last = &emc->last_timing;
  541. enum emc_dll_change dll_change;
  542. unsigned int pre_wait = 0;
  543. u32 val, val2, mask;
  544. bool update = false;
  545. unsigned int i;
  546. if (!timing)
  547. return -ENOENT;
  548. if ((last->emc_mode_1 & 0x1) == (timing->emc_mode_1 & 0x1))
  549. dll_change = DLL_CHANGE_NONE;
  550. else if (timing->emc_mode_1 & 0x1)
  551. dll_change = DLL_CHANGE_ON;
  552. else
  553. dll_change = DLL_CHANGE_OFF;
  554. /* Clear CLKCHANGE_COMPLETE interrupts */
  555. writel(EMC_INTSTATUS_CLKCHANGE_COMPLETE, emc->regs + EMC_INTSTATUS);
  556. /* Disable dynamic self-refresh */
  557. val = readl(emc->regs + EMC_CFG);
  558. if (val & EMC_CFG_PWR_MASK) {
  559. val &= ~EMC_CFG_POWER_FEATURES_MASK;
  560. writel(val, emc->regs + EMC_CFG);
  561. pre_wait = 5;
  562. }
  563. /* Disable SEL_DPD_CTRL for clock change */
  564. if (emc->dram_type == DRAM_TYPE_DDR3)
  565. mask = EMC_SEL_DPD_CTRL_DDR3_MASK;
  566. else
  567. mask = EMC_SEL_DPD_CTRL_MASK;
  568. val = readl(emc->regs + EMC_SEL_DPD_CTRL);
  569. if (val & mask) {
  570. val &= ~mask;
  571. writel(val, emc->regs + EMC_SEL_DPD_CTRL);
  572. }
  573. /* Prepare DQ/DQS for clock change */
  574. val = readl(emc->regs + EMC_BGBIAS_CTL0);
  575. val2 = last->emc_bgbias_ctl0;
  576. if (!(timing->emc_bgbias_ctl0 &
  577. EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) &&
  578. (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX)) {
  579. val2 &= ~EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX;
  580. update = true;
  581. }
  582. if ((val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD) ||
  583. (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN)) {
  584. update = true;
  585. }
  586. if (update) {
  587. writel(val2, emc->regs + EMC_BGBIAS_CTL0);
  588. if (pre_wait < 5)
  589. pre_wait = 5;
  590. }
  591. update = false;
  592. val = readl(emc->regs + EMC_XM2DQSPADCTRL2);
  593. if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_VREF_ENABLE &&
  594. !(val & EMC_XM2DQSPADCTRL2_VREF_ENABLE)) {
  595. val |= EMC_XM2DQSPADCTRL2_VREF_ENABLE;
  596. update = true;
  597. }
  598. if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE &&
  599. !(val & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE)) {
  600. val |= EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE;
  601. update = true;
  602. }
  603. if (update) {
  604. writel(val, emc->regs + EMC_XM2DQSPADCTRL2);
  605. if (pre_wait < 30)
  606. pre_wait = 30;
  607. }
  608. /* Wait to settle */
  609. if (pre_wait) {
  610. emc_seq_update_timing(emc);
  611. udelay(pre_wait);
  612. }
  613. /* Program CTT_TERM control */
  614. if (last->emc_ctt_term_ctrl != timing->emc_ctt_term_ctrl) {
  615. emc_seq_disable_auto_cal(emc);
  616. writel(timing->emc_ctt_term_ctrl,
  617. emc->regs + EMC_CTT_TERM_CTRL);
  618. emc_seq_update_timing(emc);
  619. }
  620. /* Program burst shadow registers */
  621. for (i = 0; i < ARRAY_SIZE(timing->emc_burst_data); ++i)
  622. writel(timing->emc_burst_data[i],
  623. emc->regs + emc_burst_regs[i]);
  624. writel(timing->emc_xm2dqspadctrl2, emc->regs + EMC_XM2DQSPADCTRL2);
  625. writel(timing->emc_zcal_interval, emc->regs + EMC_ZCAL_INTERVAL);
  626. tegra_mc_write_emem_configuration(emc->mc, timing->rate);
  627. val = timing->emc_cfg & ~EMC_CFG_POWER_FEATURES_MASK;
  628. emc_ccfifo_writel(emc, val, EMC_CFG);
  629. /* Program AUTO_CAL_CONFIG */
  630. if (timing->emc_auto_cal_config2 != last->emc_auto_cal_config2)
  631. emc_ccfifo_writel(emc, timing->emc_auto_cal_config2,
  632. EMC_AUTO_CAL_CONFIG2);
  633. if (timing->emc_auto_cal_config3 != last->emc_auto_cal_config3)
  634. emc_ccfifo_writel(emc, timing->emc_auto_cal_config3,
  635. EMC_AUTO_CAL_CONFIG3);
  636. if (timing->emc_auto_cal_config != last->emc_auto_cal_config) {
  637. val = timing->emc_auto_cal_config;
  638. val &= EMC_AUTO_CAL_CONFIG_AUTO_CAL_START;
  639. emc_ccfifo_writel(emc, val, EMC_AUTO_CAL_CONFIG);
  640. }
  641. /* DDR3: predict MRS long wait count */
  642. if (emc->dram_type == DRAM_TYPE_DDR3 &&
  643. dll_change == DLL_CHANGE_ON) {
  644. u32 cnt = 512;
  645. if (timing->emc_zcal_interval != 0 &&
  646. last->emc_zcal_interval == 0)
  647. cnt -= emc->dram_num * 256;
  648. val = (timing->emc_mrs_wait_cnt
  649. & EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK)
  650. >> EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT;
  651. if (cnt < val)
  652. cnt = val;
  653. val = timing->emc_mrs_wait_cnt
  654. & ~EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
  655. val |= (cnt << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT)
  656. & EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
  657. writel(val, emc->regs + EMC_MRS_WAIT_CNT);
  658. }
  659. val = timing->emc_cfg_2;
  660. val &= ~EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR;
  661. emc_ccfifo_writel(emc, val, EMC_CFG_2);
  662. /* DDR3: Turn off DLL and enter self-refresh */
  663. if (emc->dram_type == DRAM_TYPE_DDR3 && dll_change == DLL_CHANGE_OFF)
  664. emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS);
  665. /* Disable refresh controller */
  666. emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num),
  667. EMC_REFCTRL);
  668. if (emc->dram_type == DRAM_TYPE_DDR3)
  669. emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num) |
  670. EMC_SELF_REF_CMD_ENABLED,
  671. EMC_SELF_REF);
  672. /* Flow control marker */
  673. emc_ccfifo_writel(emc, 1, EMC_STALL_THEN_EXE_AFTER_CLKCHANGE);
  674. /* DDR3: Exit self-refresh */
  675. if (emc->dram_type == DRAM_TYPE_DDR3)
  676. emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num),
  677. EMC_SELF_REF);
  678. emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num) |
  679. EMC_REFCTRL_ENABLE,
  680. EMC_REFCTRL);
  681. /* Set DRAM mode registers */
  682. if (emc->dram_type == DRAM_TYPE_DDR3) {
  683. if (timing->emc_mode_1 != last->emc_mode_1)
  684. emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS);
  685. if (timing->emc_mode_2 != last->emc_mode_2)
  686. emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_EMRS2);
  687. if ((timing->emc_mode_reset != last->emc_mode_reset) ||
  688. dll_change == DLL_CHANGE_ON) {
  689. val = timing->emc_mode_reset;
  690. if (dll_change == DLL_CHANGE_ON) {
  691. val |= EMC_MODE_SET_DLL_RESET;
  692. val |= EMC_MODE_SET_LONG_CNT;
  693. } else {
  694. val &= ~EMC_MODE_SET_DLL_RESET;
  695. }
  696. emc_ccfifo_writel(emc, val, EMC_MRS);
  697. }
  698. } else {
  699. if (timing->emc_mode_2 != last->emc_mode_2)
  700. emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_MRW2);
  701. if (timing->emc_mode_1 != last->emc_mode_1)
  702. emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_MRW);
  703. if (timing->emc_mode_4 != last->emc_mode_4)
  704. emc_ccfifo_writel(emc, timing->emc_mode_4, EMC_MRW4);
  705. }
  706. /* Issue ZCAL command if turning ZCAL on */
  707. if (timing->emc_zcal_interval != 0 && last->emc_zcal_interval == 0) {
  708. emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV0, EMC_ZQ_CAL);
  709. if (emc->dram_num > 1)
  710. emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV1,
  711. EMC_ZQ_CAL);
  712. }
  713. /* Write to RO register to remove stall after change */
  714. emc_ccfifo_writel(emc, 0, EMC_CCFIFO_STATUS);
  715. if (timing->emc_cfg_2 & EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR)
  716. emc_ccfifo_writel(emc, timing->emc_cfg_2, EMC_CFG_2);
  717. /* Disable AUTO_CAL for clock change */
  718. emc_seq_disable_auto_cal(emc);
  719. /* Read register to wait until programming has settled */
  720. readl(emc->regs + EMC_INTSTATUS);
  721. return 0;
  722. }
  723. static void tegra_emc_complete_timing_change(struct tegra_emc *emc,
  724. unsigned long rate)
  725. {
  726. struct emc_timing *timing = tegra_emc_find_timing(emc, rate);
  727. struct emc_timing *last = &emc->last_timing;
  728. u32 val;
  729. if (!timing)
  730. return;
  731. /* Wait until the state machine has settled */
  732. emc_seq_wait_clkchange(emc);
  733. /* Restore AUTO_CAL */
  734. if (timing->emc_ctt_term_ctrl != last->emc_ctt_term_ctrl)
  735. writel(timing->emc_auto_cal_interval,
  736. emc->regs + EMC_AUTO_CAL_INTERVAL);
  737. /* Restore dynamic self-refresh */
  738. if (timing->emc_cfg & EMC_CFG_PWR_MASK)
  739. writel(timing->emc_cfg, emc->regs + EMC_CFG);
  740. /* Set ZCAL wait count */
  741. writel(timing->emc_zcal_cnt_long, emc->regs + EMC_ZCAL_WAIT_CNT);
  742. /* LPDDR3: Turn off BGBIAS if low frequency */
  743. if (emc->dram_type == DRAM_TYPE_LPDDR3 &&
  744. timing->emc_bgbias_ctl0 &
  745. EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) {
  746. val = timing->emc_bgbias_ctl0;
  747. val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN;
  748. val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD;
  749. writel(val, emc->regs + EMC_BGBIAS_CTL0);
  750. } else {
  751. if (emc->dram_type == DRAM_TYPE_DDR3 &&
  752. readl(emc->regs + EMC_BGBIAS_CTL0) !=
  753. timing->emc_bgbias_ctl0) {
  754. writel(timing->emc_bgbias_ctl0,
  755. emc->regs + EMC_BGBIAS_CTL0);
  756. }
  757. writel(timing->emc_auto_cal_interval,
  758. emc->regs + EMC_AUTO_CAL_INTERVAL);
  759. }
  760. /* Wait for timing to settle */
  761. udelay(2);
  762. /* Reprogram SEL_DPD_CTRL */
  763. writel(timing->emc_sel_dpd_ctrl, emc->regs + EMC_SEL_DPD_CTRL);
  764. emc_seq_update_timing(emc);
  765. emc->last_timing = *timing;
  766. }
  767. /* Initialization and deinitialization */
  768. static void emc_read_current_timing(struct tegra_emc *emc,
  769. struct emc_timing *timing)
  770. {
  771. unsigned int i;
  772. for (i = 0; i < ARRAY_SIZE(emc_burst_regs); ++i)
  773. timing->emc_burst_data[i] =
  774. readl(emc->regs + emc_burst_regs[i]);
  775. timing->emc_cfg = readl(emc->regs + EMC_CFG);
  776. timing->emc_auto_cal_interval = 0;
  777. timing->emc_zcal_cnt_long = 0;
  778. timing->emc_mode_1 = 0;
  779. timing->emc_mode_2 = 0;
  780. timing->emc_mode_4 = 0;
  781. timing->emc_mode_reset = 0;
  782. }
  783. static int emc_init(struct tegra_emc *emc)
  784. {
  785. emc->dram_type = readl(emc->regs + EMC_FBIO_CFG5);
  786. if (emc->dram_type & EMC_FBIO_CFG5_DRAM_WIDTH_X64)
  787. emc->dram_bus_width = 64;
  788. else
  789. emc->dram_bus_width = 32;
  790. dev_info_once(emc->dev, "%ubit DRAM bus\n", emc->dram_bus_width);
  791. emc->dram_type &= EMC_FBIO_CFG5_DRAM_TYPE_MASK;
  792. emc->dram_type >>= EMC_FBIO_CFG5_DRAM_TYPE_SHIFT;
  793. emc->dram_num = tegra_mc_get_emem_device_count(emc->mc);
  794. emc_read_current_timing(emc, &emc->last_timing);
  795. return 0;
  796. }
  797. static int load_one_timing_from_dt(struct tegra_emc *emc,
  798. struct emc_timing *timing,
  799. struct device_node *node)
  800. {
  801. u32 value;
  802. int err;
  803. err = of_property_read_u32(node, "clock-frequency", &value);
  804. if (err) {
  805. dev_err(emc->dev, "timing %pOFn: failed to read rate: %d\n",
  806. node, err);
  807. return err;
  808. }
  809. timing->rate = value;
  810. err = of_property_read_u32_array(node, "nvidia,emc-configuration",
  811. timing->emc_burst_data,
  812. ARRAY_SIZE(timing->emc_burst_data));
  813. if (err) {
  814. dev_err(emc->dev,
  815. "timing %pOFn: failed to read emc burst data: %d\n",
  816. node, err);
  817. return err;
  818. }
  819. #define EMC_READ_PROP(prop, dtprop) { \
  820. err = of_property_read_u32(node, dtprop, &timing->prop); \
  821. if (err) { \
  822. dev_err(emc->dev, "timing %pOFn: failed to read " #prop ": %d\n", \
  823. node, err); \
  824. return err; \
  825. } \
  826. }
  827. EMC_READ_PROP(emc_auto_cal_config, "nvidia,emc-auto-cal-config")
  828. EMC_READ_PROP(emc_auto_cal_config2, "nvidia,emc-auto-cal-config2")
  829. EMC_READ_PROP(emc_auto_cal_config3, "nvidia,emc-auto-cal-config3")
  830. EMC_READ_PROP(emc_auto_cal_interval, "nvidia,emc-auto-cal-interval")
  831. EMC_READ_PROP(emc_bgbias_ctl0, "nvidia,emc-bgbias-ctl0")
  832. EMC_READ_PROP(emc_cfg, "nvidia,emc-cfg")
  833. EMC_READ_PROP(emc_cfg_2, "nvidia,emc-cfg-2")
  834. EMC_READ_PROP(emc_ctt_term_ctrl, "nvidia,emc-ctt-term-ctrl")
  835. EMC_READ_PROP(emc_mode_1, "nvidia,emc-mode-1")
  836. EMC_READ_PROP(emc_mode_2, "nvidia,emc-mode-2")
  837. EMC_READ_PROP(emc_mode_4, "nvidia,emc-mode-4")
  838. EMC_READ_PROP(emc_mode_reset, "nvidia,emc-mode-reset")
  839. EMC_READ_PROP(emc_mrs_wait_cnt, "nvidia,emc-mrs-wait-cnt")
  840. EMC_READ_PROP(emc_sel_dpd_ctrl, "nvidia,emc-sel-dpd-ctrl")
  841. EMC_READ_PROP(emc_xm2dqspadctrl2, "nvidia,emc-xm2dqspadctrl2")
  842. EMC_READ_PROP(emc_zcal_cnt_long, "nvidia,emc-zcal-cnt-long")
  843. EMC_READ_PROP(emc_zcal_interval, "nvidia,emc-zcal-interval")
  844. #undef EMC_READ_PROP
  845. return 0;
  846. }
  847. static int cmp_timings(const void *_a, const void *_b)
  848. {
  849. const struct emc_timing *a = _a;
  850. const struct emc_timing *b = _b;
  851. if (a->rate < b->rate)
  852. return -1;
  853. else if (a->rate == b->rate)
  854. return 0;
  855. else
  856. return 1;
  857. }
  858. static int tegra_emc_load_timings_from_dt(struct tegra_emc *emc,
  859. struct device_node *node)
  860. {
  861. int child_count = of_get_child_count(node);
  862. struct device_node *child;
  863. struct emc_timing *timing;
  864. unsigned int i = 0;
  865. int err;
  866. emc->timings = devm_kcalloc(emc->dev, child_count, sizeof(*timing),
  867. GFP_KERNEL);
  868. if (!emc->timings)
  869. return -ENOMEM;
  870. emc->num_timings = child_count;
  871. for_each_child_of_node(node, child) {
  872. timing = &emc->timings[i++];
  873. err = load_one_timing_from_dt(emc, timing, child);
  874. if (err) {
  875. of_node_put(child);
  876. return err;
  877. }
  878. }
  879. sort(emc->timings, emc->num_timings, sizeof(*timing), cmp_timings,
  880. NULL);
  881. return 0;
  882. }
  883. static const struct of_device_id tegra_emc_of_match[] = {
  884. { .compatible = "nvidia,tegra124-emc" },
  885. { .compatible = "nvidia,tegra132-emc" },
  886. {}
  887. };
  888. MODULE_DEVICE_TABLE(of, tegra_emc_of_match);
  889. static struct device_node *
  890. tegra_emc_find_node_by_ram_code(struct device_node *node, u32 ram_code)
  891. {
  892. struct device_node *np;
  893. int err;
  894. for_each_child_of_node(node, np) {
  895. u32 value;
  896. err = of_property_read_u32(np, "nvidia,ram-code", &value);
  897. if (err || (value != ram_code))
  898. continue;
  899. return np;
  900. }
  901. return NULL;
  902. }
  903. static void tegra_emc_rate_requests_init(struct tegra_emc *emc)
  904. {
  905. unsigned int i;
  906. for (i = 0; i < EMC_RATE_TYPE_MAX; i++) {
  907. emc->requested_rate[i].min_rate = 0;
  908. emc->requested_rate[i].max_rate = ULONG_MAX;
  909. }
  910. }
  911. static int emc_request_rate(struct tegra_emc *emc,
  912. unsigned long new_min_rate,
  913. unsigned long new_max_rate,
  914. enum emc_rate_request_type type)
  915. {
  916. struct emc_rate_request *req = emc->requested_rate;
  917. unsigned long min_rate = 0, max_rate = ULONG_MAX;
  918. unsigned int i;
  919. int err;
  920. /* select minimum and maximum rates among the requested rates */
  921. for (i = 0; i < EMC_RATE_TYPE_MAX; i++, req++) {
  922. if (i == type) {
  923. min_rate = max(new_min_rate, min_rate);
  924. max_rate = min(new_max_rate, max_rate);
  925. } else {
  926. min_rate = max(req->min_rate, min_rate);
  927. max_rate = min(req->max_rate, max_rate);
  928. }
  929. }
  930. if (min_rate > max_rate) {
  931. dev_err_ratelimited(emc->dev, "%s: type %u: out of range: %lu %lu\n",
  932. __func__, type, min_rate, max_rate);
  933. return -ERANGE;
  934. }
  935. /*
  936. * EMC rate-changes should go via OPP API because it manages voltage
  937. * changes.
  938. */
  939. err = dev_pm_opp_set_rate(emc->dev, min_rate);
  940. if (err)
  941. return err;
  942. emc->requested_rate[type].min_rate = new_min_rate;
  943. emc->requested_rate[type].max_rate = new_max_rate;
  944. return 0;
  945. }
  946. static int emc_set_min_rate(struct tegra_emc *emc, unsigned long rate,
  947. enum emc_rate_request_type type)
  948. {
  949. struct emc_rate_request *req = &emc->requested_rate[type];
  950. int ret;
  951. mutex_lock(&emc->rate_lock);
  952. ret = emc_request_rate(emc, rate, req->max_rate, type);
  953. mutex_unlock(&emc->rate_lock);
  954. return ret;
  955. }
  956. static int emc_set_max_rate(struct tegra_emc *emc, unsigned long rate,
  957. enum emc_rate_request_type type)
  958. {
  959. struct emc_rate_request *req = &emc->requested_rate[type];
  960. int ret;
  961. mutex_lock(&emc->rate_lock);
  962. ret = emc_request_rate(emc, req->min_rate, rate, type);
  963. mutex_unlock(&emc->rate_lock);
  964. return ret;
  965. }
  966. /*
  967. * debugfs interface
  968. *
  969. * The memory controller driver exposes some files in debugfs that can be used
  970. * to control the EMC frequency. The top-level directory can be found here:
  971. *
  972. * /sys/kernel/debug/emc
  973. *
  974. * It contains the following files:
  975. *
  976. * - available_rates: This file contains a list of valid, space-separated
  977. * EMC frequencies.
  978. *
  979. * - min_rate: Writing a value to this file sets the given frequency as the
  980. * floor of the permitted range. If this is higher than the currently
  981. * configured EMC frequency, this will cause the frequency to be
  982. * increased so that it stays within the valid range.
  983. *
  984. * - max_rate: Similarily to the min_rate file, writing a value to this file
  985. * sets the given frequency as the ceiling of the permitted range. If
  986. * the value is lower than the currently configured EMC frequency, this
  987. * will cause the frequency to be decreased so that it stays within the
  988. * valid range.
  989. */
  990. static bool tegra_emc_validate_rate(struct tegra_emc *emc, unsigned long rate)
  991. {
  992. unsigned int i;
  993. for (i = 0; i < emc->num_timings; i++)
  994. if (rate == emc->timings[i].rate)
  995. return true;
  996. return false;
  997. }
  998. static int tegra_emc_debug_available_rates_show(struct seq_file *s,
  999. void *data)
  1000. {
  1001. struct tegra_emc *emc = s->private;
  1002. const char *prefix = "";
  1003. unsigned int i;
  1004. for (i = 0; i < emc->num_timings; i++) {
  1005. seq_printf(s, "%s%lu", prefix, emc->timings[i].rate);
  1006. prefix = " ";
  1007. }
  1008. seq_puts(s, "\n");
  1009. return 0;
  1010. }
  1011. DEFINE_SHOW_ATTRIBUTE(tegra_emc_debug_available_rates);
  1012. static int tegra_emc_debug_min_rate_get(void *data, u64 *rate)
  1013. {
  1014. struct tegra_emc *emc = data;
  1015. *rate = emc->debugfs.min_rate;
  1016. return 0;
  1017. }
  1018. static int tegra_emc_debug_min_rate_set(void *data, u64 rate)
  1019. {
  1020. struct tegra_emc *emc = data;
  1021. int err;
  1022. if (!tegra_emc_validate_rate(emc, rate))
  1023. return -EINVAL;
  1024. err = emc_set_min_rate(emc, rate, EMC_RATE_DEBUG);
  1025. if (err < 0)
  1026. return err;
  1027. emc->debugfs.min_rate = rate;
  1028. return 0;
  1029. }
  1030. DEFINE_DEBUGFS_ATTRIBUTE(tegra_emc_debug_min_rate_fops,
  1031. tegra_emc_debug_min_rate_get,
  1032. tegra_emc_debug_min_rate_set, "%llu\n");
  1033. static int tegra_emc_debug_max_rate_get(void *data, u64 *rate)
  1034. {
  1035. struct tegra_emc *emc = data;
  1036. *rate = emc->debugfs.max_rate;
  1037. return 0;
  1038. }
  1039. static int tegra_emc_debug_max_rate_set(void *data, u64 rate)
  1040. {
  1041. struct tegra_emc *emc = data;
  1042. int err;
  1043. if (!tegra_emc_validate_rate(emc, rate))
  1044. return -EINVAL;
  1045. err = emc_set_max_rate(emc, rate, EMC_RATE_DEBUG);
  1046. if (err < 0)
  1047. return err;
  1048. emc->debugfs.max_rate = rate;
  1049. return 0;
  1050. }
  1051. DEFINE_DEBUGFS_ATTRIBUTE(tegra_emc_debug_max_rate_fops,
  1052. tegra_emc_debug_max_rate_get,
  1053. tegra_emc_debug_max_rate_set, "%llu\n");
  1054. static void emc_debugfs_init(struct device *dev, struct tegra_emc *emc)
  1055. {
  1056. unsigned int i;
  1057. int err;
  1058. emc->debugfs.min_rate = ULONG_MAX;
  1059. emc->debugfs.max_rate = 0;
  1060. for (i = 0; i < emc->num_timings; i++) {
  1061. if (emc->timings[i].rate < emc->debugfs.min_rate)
  1062. emc->debugfs.min_rate = emc->timings[i].rate;
  1063. if (emc->timings[i].rate > emc->debugfs.max_rate)
  1064. emc->debugfs.max_rate = emc->timings[i].rate;
  1065. }
  1066. if (!emc->num_timings) {
  1067. emc->debugfs.min_rate = clk_get_rate(emc->clk);
  1068. emc->debugfs.max_rate = emc->debugfs.min_rate;
  1069. }
  1070. err = clk_set_rate_range(emc->clk, emc->debugfs.min_rate,
  1071. emc->debugfs.max_rate);
  1072. if (err < 0) {
  1073. dev_err(dev, "failed to set rate range [%lu-%lu] for %pC\n",
  1074. emc->debugfs.min_rate, emc->debugfs.max_rate,
  1075. emc->clk);
  1076. return;
  1077. }
  1078. emc->debugfs.root = debugfs_create_dir("emc", NULL);
  1079. debugfs_create_file("available_rates", 0444, emc->debugfs.root, emc,
  1080. &tegra_emc_debug_available_rates_fops);
  1081. debugfs_create_file("min_rate", 0644, emc->debugfs.root,
  1082. emc, &tegra_emc_debug_min_rate_fops);
  1083. debugfs_create_file("max_rate", 0644, emc->debugfs.root,
  1084. emc, &tegra_emc_debug_max_rate_fops);
  1085. }
  1086. static inline struct tegra_emc *
  1087. to_tegra_emc_provider(struct icc_provider *provider)
  1088. {
  1089. return container_of(provider, struct tegra_emc, provider);
  1090. }
  1091. static struct icc_node_data *
  1092. emc_of_icc_xlate_extended(struct of_phandle_args *spec, void *data)
  1093. {
  1094. struct icc_provider *provider = data;
  1095. struct icc_node_data *ndata;
  1096. struct icc_node *node;
  1097. /* External Memory is the only possible ICC route */
  1098. list_for_each_entry(node, &provider->nodes, node_list) {
  1099. if (node->id != TEGRA_ICC_EMEM)
  1100. continue;
  1101. ndata = kzalloc(sizeof(*ndata), GFP_KERNEL);
  1102. if (!ndata)
  1103. return ERR_PTR(-ENOMEM);
  1104. /*
  1105. * SRC and DST nodes should have matching TAG in order to have
  1106. * it set by default for a requested path.
  1107. */
  1108. ndata->tag = TEGRA_MC_ICC_TAG_ISO;
  1109. ndata->node = node;
  1110. return ndata;
  1111. }
  1112. return ERR_PTR(-EPROBE_DEFER);
  1113. }
  1114. static int emc_icc_set(struct icc_node *src, struct icc_node *dst)
  1115. {
  1116. struct tegra_emc *emc = to_tegra_emc_provider(dst->provider);
  1117. unsigned long long peak_bw = icc_units_to_bps(dst->peak_bw);
  1118. unsigned long long avg_bw = icc_units_to_bps(dst->avg_bw);
  1119. unsigned long long rate = max(avg_bw, peak_bw);
  1120. unsigned int dram_data_bus_width_bytes;
  1121. const unsigned int ddr = 2;
  1122. int err;
  1123. /*
  1124. * Tegra124 EMC runs on a clock rate of SDRAM bus. This means that
  1125. * EMC clock rate is twice smaller than the peak data rate because
  1126. * data is sampled on both EMC clock edges.
  1127. */
  1128. dram_data_bus_width_bytes = emc->dram_bus_width / 8;
  1129. do_div(rate, ddr * dram_data_bus_width_bytes);
  1130. rate = min_t(u64, rate, U32_MAX);
  1131. err = emc_set_min_rate(emc, rate, EMC_RATE_ICC);
  1132. if (err)
  1133. return err;
  1134. return 0;
  1135. }
  1136. static int tegra_emc_interconnect_init(struct tegra_emc *emc)
  1137. {
  1138. const struct tegra_mc_soc *soc = emc->mc->soc;
  1139. struct icc_node *node;
  1140. int err;
  1141. emc->provider.dev = emc->dev;
  1142. emc->provider.set = emc_icc_set;
  1143. emc->provider.data = &emc->provider;
  1144. emc->provider.aggregate = soc->icc_ops->aggregate;
  1145. emc->provider.xlate_extended = emc_of_icc_xlate_extended;
  1146. icc_provider_init(&emc->provider);
  1147. /* create External Memory Controller node */
  1148. node = icc_node_create(TEGRA_ICC_EMC);
  1149. if (IS_ERR(node)) {
  1150. err = PTR_ERR(node);
  1151. goto err_msg;
  1152. }
  1153. node->name = "External Memory Controller";
  1154. icc_node_add(node, &emc->provider);
  1155. /* link External Memory Controller to External Memory (DRAM) */
  1156. err = icc_link_create(node, TEGRA_ICC_EMEM);
  1157. if (err)
  1158. goto remove_nodes;
  1159. /* create External Memory node */
  1160. node = icc_node_create(TEGRA_ICC_EMEM);
  1161. if (IS_ERR(node)) {
  1162. err = PTR_ERR(node);
  1163. goto remove_nodes;
  1164. }
  1165. node->name = "External Memory (DRAM)";
  1166. icc_node_add(node, &emc->provider);
  1167. err = icc_provider_register(&emc->provider);
  1168. if (err)
  1169. goto remove_nodes;
  1170. return 0;
  1171. remove_nodes:
  1172. icc_nodes_remove(&emc->provider);
  1173. err_msg:
  1174. dev_err(emc->dev, "failed to initialize ICC: %d\n", err);
  1175. return err;
  1176. }
  1177. static int tegra_emc_opp_table_init(struct tegra_emc *emc)
  1178. {
  1179. u32 hw_version = BIT(tegra_sku_info.soc_speedo_id);
  1180. int opp_token, err;
  1181. err = dev_pm_opp_set_supported_hw(emc->dev, &hw_version, 1);
  1182. if (err < 0) {
  1183. dev_err(emc->dev, "failed to set OPP supported HW: %d\n", err);
  1184. return err;
  1185. }
  1186. opp_token = err;
  1187. err = dev_pm_opp_of_add_table(emc->dev);
  1188. if (err) {
  1189. if (err == -ENODEV)
  1190. dev_err(emc->dev, "OPP table not found, please update your device tree\n");
  1191. else
  1192. dev_err(emc->dev, "failed to add OPP table: %d\n", err);
  1193. goto put_hw_table;
  1194. }
  1195. dev_info_once(emc->dev, "OPP HW ver. 0x%x, current clock rate %lu MHz\n",
  1196. hw_version, clk_get_rate(emc->clk) / 1000000);
  1197. /* first dummy rate-set initializes voltage state */
  1198. err = dev_pm_opp_set_rate(emc->dev, clk_get_rate(emc->clk));
  1199. if (err) {
  1200. dev_err(emc->dev, "failed to initialize OPP clock: %d\n", err);
  1201. goto remove_table;
  1202. }
  1203. return 0;
  1204. remove_table:
  1205. dev_pm_opp_of_remove_table(emc->dev);
  1206. put_hw_table:
  1207. dev_pm_opp_put_supported_hw(opp_token);
  1208. return err;
  1209. }
  1210. static void devm_tegra_emc_unset_callback(void *data)
  1211. {
  1212. tegra124_clk_set_emc_callbacks(NULL, NULL);
  1213. }
  1214. static int tegra_emc_probe(struct platform_device *pdev)
  1215. {
  1216. struct device_node *np;
  1217. struct tegra_emc *emc;
  1218. u32 ram_code;
  1219. int err;
  1220. emc = devm_kzalloc(&pdev->dev, sizeof(*emc), GFP_KERNEL);
  1221. if (!emc)
  1222. return -ENOMEM;
  1223. mutex_init(&emc->rate_lock);
  1224. emc->dev = &pdev->dev;
  1225. emc->regs = devm_platform_ioremap_resource(pdev, 0);
  1226. if (IS_ERR(emc->regs))
  1227. return PTR_ERR(emc->regs);
  1228. emc->mc = devm_tegra_memory_controller_get(&pdev->dev);
  1229. if (IS_ERR(emc->mc))
  1230. return PTR_ERR(emc->mc);
  1231. ram_code = tegra_read_ram_code();
  1232. np = tegra_emc_find_node_by_ram_code(pdev->dev.of_node, ram_code);
  1233. if (np) {
  1234. err = tegra_emc_load_timings_from_dt(emc, np);
  1235. of_node_put(np);
  1236. if (err)
  1237. return err;
  1238. } else {
  1239. dev_info_once(&pdev->dev,
  1240. "no memory timings for RAM code %u found in DT\n",
  1241. ram_code);
  1242. }
  1243. err = emc_init(emc);
  1244. if (err) {
  1245. dev_err(&pdev->dev, "EMC initialization failed: %d\n", err);
  1246. return err;
  1247. }
  1248. platform_set_drvdata(pdev, emc);
  1249. tegra124_clk_set_emc_callbacks(tegra_emc_prepare_timing_change,
  1250. tegra_emc_complete_timing_change);
  1251. err = devm_add_action_or_reset(&pdev->dev, devm_tegra_emc_unset_callback,
  1252. NULL);
  1253. if (err)
  1254. return err;
  1255. emc->clk = devm_clk_get(&pdev->dev, "emc");
  1256. if (IS_ERR(emc->clk)) {
  1257. err = PTR_ERR(emc->clk);
  1258. dev_err(&pdev->dev, "failed to get EMC clock: %d\n", err);
  1259. return err;
  1260. }
  1261. err = tegra_emc_opp_table_init(emc);
  1262. if (err)
  1263. return err;
  1264. tegra_emc_rate_requests_init(emc);
  1265. if (IS_ENABLED(CONFIG_DEBUG_FS))
  1266. emc_debugfs_init(&pdev->dev, emc);
  1267. tegra_emc_interconnect_init(emc);
  1268. /*
  1269. * Don't allow the kernel module to be unloaded. Unloading adds some
  1270. * extra complexity which doesn't really worth the effort in a case of
  1271. * this driver.
  1272. */
  1273. try_module_get(THIS_MODULE);
  1274. return 0;
  1275. };
  1276. static struct platform_driver tegra_emc_driver = {
  1277. .probe = tegra_emc_probe,
  1278. .driver = {
  1279. .name = "tegra-emc",
  1280. .of_match_table = tegra_emc_of_match,
  1281. .suppress_bind_attrs = true,
  1282. .sync_state = icc_sync_state,
  1283. },
  1284. };
  1285. module_platform_driver(tegra_emc_driver);
  1286. MODULE_AUTHOR("Mikko Perttunen <[email protected]>");
  1287. MODULE_DESCRIPTION("NVIDIA Tegra124 EMC driver");
  1288. MODULE_LICENSE("GPL v2");