tegra124-emc.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /*
  2. * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Author:
  5. * Mikko Perttunen <mperttunen@nvidia.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/clk-provider.h>
  18. #include <linux/clk.h>
  19. #include <linux/clkdev.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/delay.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/sort.h>
  26. #include <linux/string.h>
  27. #include <soc/tegra/emc.h>
  28. #include <soc/tegra/fuse.h>
  29. #include <soc/tegra/mc.h>
  30. #define EMC_FBIO_CFG5 0x104
  31. #define EMC_FBIO_CFG5_DRAM_TYPE_MASK 0x3
  32. #define EMC_FBIO_CFG5_DRAM_TYPE_SHIFT 0
  33. #define EMC_INTSTATUS 0x0
  34. #define EMC_INTSTATUS_CLKCHANGE_COMPLETE BIT(4)
  35. #define EMC_CFG 0xc
  36. #define EMC_CFG_DRAM_CLKSTOP_PD BIT(31)
  37. #define EMC_CFG_DRAM_CLKSTOP_SR BIT(30)
  38. #define EMC_CFG_DRAM_ACPD BIT(29)
  39. #define EMC_CFG_DYN_SREF BIT(28)
  40. #define EMC_CFG_PWR_MASK ((0xF << 28) | BIT(18))
  41. #define EMC_CFG_DSR_VTTGEN_DRV_EN BIT(18)
  42. #define EMC_REFCTRL 0x20
  43. #define EMC_REFCTRL_DEV_SEL_SHIFT 0
  44. #define EMC_REFCTRL_ENABLE BIT(31)
  45. #define EMC_TIMING_CONTROL 0x28
  46. #define EMC_RC 0x2c
  47. #define EMC_RFC 0x30
  48. #define EMC_RAS 0x34
  49. #define EMC_RP 0x38
  50. #define EMC_R2W 0x3c
  51. #define EMC_W2R 0x40
  52. #define EMC_R2P 0x44
  53. #define EMC_W2P 0x48
  54. #define EMC_RD_RCD 0x4c
  55. #define EMC_WR_RCD 0x50
  56. #define EMC_RRD 0x54
  57. #define EMC_REXT 0x58
  58. #define EMC_WDV 0x5c
  59. #define EMC_QUSE 0x60
  60. #define EMC_QRST 0x64
  61. #define EMC_QSAFE 0x68
  62. #define EMC_RDV 0x6c
  63. #define EMC_REFRESH 0x70
  64. #define EMC_BURST_REFRESH_NUM 0x74
  65. #define EMC_PDEX2WR 0x78
  66. #define EMC_PDEX2RD 0x7c
  67. #define EMC_PCHG2PDEN 0x80
  68. #define EMC_ACT2PDEN 0x84
  69. #define EMC_AR2PDEN 0x88
  70. #define EMC_RW2PDEN 0x8c
  71. #define EMC_TXSR 0x90
  72. #define EMC_TCKE 0x94
  73. #define EMC_TFAW 0x98
  74. #define EMC_TRPAB 0x9c
  75. #define EMC_TCLKSTABLE 0xa0
  76. #define EMC_TCLKSTOP 0xa4
  77. #define EMC_TREFBW 0xa8
  78. #define EMC_ODT_WRITE 0xb0
  79. #define EMC_ODT_READ 0xb4
  80. #define EMC_WEXT 0xb8
  81. #define EMC_CTT 0xbc
  82. #define EMC_RFC_SLR 0xc0
  83. #define EMC_MRS_WAIT_CNT2 0xc4
  84. #define EMC_MRS_WAIT_CNT 0xc8
  85. #define EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT 0
  86. #define EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK \
  87. (0x3FF << EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT)
  88. #define EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT 16
  89. #define EMC_MRS_WAIT_CNT_LONG_WAIT_MASK \
  90. (0x3FF << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT)
  91. #define EMC_MRS 0xcc
  92. #define EMC_MODE_SET_DLL_RESET BIT(8)
  93. #define EMC_MODE_SET_LONG_CNT BIT(26)
  94. #define EMC_EMRS 0xd0
  95. #define EMC_REF 0xd4
  96. #define EMC_PRE 0xd8
  97. #define EMC_SELF_REF 0xe0
  98. #define EMC_SELF_REF_CMD_ENABLED BIT(0)
  99. #define EMC_SELF_REF_DEV_SEL_SHIFT 30
  100. #define EMC_MRW 0xe8
  101. #define EMC_MRR 0xec
  102. #define EMC_MRR_MA_SHIFT 16
  103. #define LPDDR2_MR4_TEMP_SHIFT 0
  104. #define EMC_XM2DQSPADCTRL3 0xf8
  105. #define EMC_FBIO_SPARE 0x100
  106. #define EMC_FBIO_CFG6 0x114
  107. #define EMC_EMRS2 0x12c
  108. #define EMC_MRW2 0x134
  109. #define EMC_MRW4 0x13c
  110. #define EMC_EINPUT 0x14c
  111. #define EMC_EINPUT_DURATION 0x150
  112. #define EMC_PUTERM_EXTRA 0x154
  113. #define EMC_TCKESR 0x158
  114. #define EMC_TPD 0x15c
  115. #define EMC_AUTO_CAL_CONFIG 0x2a4
  116. #define EMC_AUTO_CAL_CONFIG_AUTO_CAL_START BIT(31)
  117. #define EMC_AUTO_CAL_INTERVAL 0x2a8
  118. #define EMC_AUTO_CAL_STATUS 0x2ac
  119. #define EMC_AUTO_CAL_STATUS_ACTIVE BIT(31)
  120. #define EMC_STATUS 0x2b4
  121. #define EMC_STATUS_TIMING_UPDATE_STALLED BIT(23)
  122. #define EMC_CFG_2 0x2b8
  123. #define EMC_CFG_2_MODE_SHIFT 0
  124. #define EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR BIT(6)
  125. #define EMC_CFG_DIG_DLL 0x2bc
  126. #define EMC_CFG_DIG_DLL_PERIOD 0x2c0
  127. #define EMC_RDV_MASK 0x2cc
  128. #define EMC_WDV_MASK 0x2d0
  129. #define EMC_CTT_DURATION 0x2d8
  130. #define EMC_CTT_TERM_CTRL 0x2dc
  131. #define EMC_ZCAL_INTERVAL 0x2e0
  132. #define EMC_ZCAL_WAIT_CNT 0x2e4
  133. #define EMC_ZQ_CAL 0x2ec
  134. #define EMC_ZQ_CAL_CMD BIT(0)
  135. #define EMC_ZQ_CAL_LONG BIT(4)
  136. #define EMC_ZQ_CAL_LONG_CMD_DEV0 \
  137. (DRAM_DEV_SEL_0 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
  138. #define EMC_ZQ_CAL_LONG_CMD_DEV1 \
  139. (DRAM_DEV_SEL_1 | EMC_ZQ_CAL_LONG | EMC_ZQ_CAL_CMD)
  140. #define EMC_XM2CMDPADCTRL 0x2f0
  141. #define EMC_XM2DQSPADCTRL 0x2f8
  142. #define EMC_XM2DQSPADCTRL2 0x2fc
  143. #define EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE BIT(0)
  144. #define EMC_XM2DQSPADCTRL2_VREF_ENABLE BIT(5)
  145. #define EMC_XM2DQPADCTRL 0x300
  146. #define EMC_XM2DQPADCTRL2 0x304
  147. #define EMC_XM2CLKPADCTRL 0x308
  148. #define EMC_XM2COMPPADCTRL 0x30c
  149. #define EMC_XM2VTTGENPADCTRL 0x310
  150. #define EMC_XM2VTTGENPADCTRL2 0x314
  151. #define EMC_XM2VTTGENPADCTRL3 0x318
  152. #define EMC_XM2DQSPADCTRL4 0x320
  153. #define EMC_DLL_XFORM_DQS0 0x328
  154. #define EMC_DLL_XFORM_DQS1 0x32c
  155. #define EMC_DLL_XFORM_DQS2 0x330
  156. #define EMC_DLL_XFORM_DQS3 0x334
  157. #define EMC_DLL_XFORM_DQS4 0x338
  158. #define EMC_DLL_XFORM_DQS5 0x33c
  159. #define EMC_DLL_XFORM_DQS6 0x340
  160. #define EMC_DLL_XFORM_DQS7 0x344
  161. #define EMC_DLL_XFORM_QUSE0 0x348
  162. #define EMC_DLL_XFORM_QUSE1 0x34c
  163. #define EMC_DLL_XFORM_QUSE2 0x350
  164. #define EMC_DLL_XFORM_QUSE3 0x354
  165. #define EMC_DLL_XFORM_QUSE4 0x358
  166. #define EMC_DLL_XFORM_QUSE5 0x35c
  167. #define EMC_DLL_XFORM_QUSE6 0x360
  168. #define EMC_DLL_XFORM_QUSE7 0x364
  169. #define EMC_DLL_XFORM_DQ0 0x368
  170. #define EMC_DLL_XFORM_DQ1 0x36c
  171. #define EMC_DLL_XFORM_DQ2 0x370
  172. #define EMC_DLL_XFORM_DQ3 0x374
  173. #define EMC_DLI_TRIM_TXDQS0 0x3a8
  174. #define EMC_DLI_TRIM_TXDQS1 0x3ac
  175. #define EMC_DLI_TRIM_TXDQS2 0x3b0
  176. #define EMC_DLI_TRIM_TXDQS3 0x3b4
  177. #define EMC_DLI_TRIM_TXDQS4 0x3b8
  178. #define EMC_DLI_TRIM_TXDQS5 0x3bc
  179. #define EMC_DLI_TRIM_TXDQS6 0x3c0
  180. #define EMC_DLI_TRIM_TXDQS7 0x3c4
  181. #define EMC_STALL_THEN_EXE_AFTER_CLKCHANGE 0x3cc
  182. #define EMC_SEL_DPD_CTRL 0x3d8
  183. #define EMC_SEL_DPD_CTRL_DATA_SEL_DPD BIT(8)
  184. #define EMC_SEL_DPD_CTRL_ODT_SEL_DPD BIT(5)
  185. #define EMC_SEL_DPD_CTRL_RESET_SEL_DPD BIT(4)
  186. #define EMC_SEL_DPD_CTRL_CA_SEL_DPD BIT(3)
  187. #define EMC_SEL_DPD_CTRL_CLK_SEL_DPD BIT(2)
  188. #define EMC_SEL_DPD_CTRL_DDR3_MASK \
  189. ((0xf << 2) | BIT(8))
  190. #define EMC_SEL_DPD_CTRL_MASK \
  191. ((0x3 << 2) | BIT(5) | BIT(8))
  192. #define EMC_PRE_REFRESH_REQ_CNT 0x3dc
  193. #define EMC_DYN_SELF_REF_CONTROL 0x3e0
  194. #define EMC_TXSRDLL 0x3e4
  195. #define EMC_CCFIFO_ADDR 0x3e8
  196. #define EMC_CCFIFO_DATA 0x3ec
  197. #define EMC_CCFIFO_STATUS 0x3f0
  198. #define EMC_CDB_CNTL_1 0x3f4
  199. #define EMC_CDB_CNTL_2 0x3f8
  200. #define EMC_XM2CLKPADCTRL2 0x3fc
  201. #define EMC_AUTO_CAL_CONFIG2 0x458
  202. #define EMC_AUTO_CAL_CONFIG3 0x45c
  203. #define EMC_IBDLY 0x468
  204. #define EMC_DLL_XFORM_ADDR0 0x46c
  205. #define EMC_DLL_XFORM_ADDR1 0x470
  206. #define EMC_DLL_XFORM_ADDR2 0x474
  207. #define EMC_DSR_VTTGEN_DRV 0x47c
  208. #define EMC_TXDSRVTTGEN 0x480
  209. #define EMC_XM2CMDPADCTRL4 0x484
  210. #define EMC_XM2CMDPADCTRL5 0x488
  211. #define EMC_DLL_XFORM_DQS8 0x4a0
  212. #define EMC_DLL_XFORM_DQS9 0x4a4
  213. #define EMC_DLL_XFORM_DQS10 0x4a8
  214. #define EMC_DLL_XFORM_DQS11 0x4ac
  215. #define EMC_DLL_XFORM_DQS12 0x4b0
  216. #define EMC_DLL_XFORM_DQS13 0x4b4
  217. #define EMC_DLL_XFORM_DQS14 0x4b8
  218. #define EMC_DLL_XFORM_DQS15 0x4bc
  219. #define EMC_DLL_XFORM_QUSE8 0x4c0
  220. #define EMC_DLL_XFORM_QUSE9 0x4c4
  221. #define EMC_DLL_XFORM_QUSE10 0x4c8
  222. #define EMC_DLL_XFORM_QUSE11 0x4cc
  223. #define EMC_DLL_XFORM_QUSE12 0x4d0
  224. #define EMC_DLL_XFORM_QUSE13 0x4d4
  225. #define EMC_DLL_XFORM_QUSE14 0x4d8
  226. #define EMC_DLL_XFORM_QUSE15 0x4dc
  227. #define EMC_DLL_XFORM_DQ4 0x4e0
  228. #define EMC_DLL_XFORM_DQ5 0x4e4
  229. #define EMC_DLL_XFORM_DQ6 0x4e8
  230. #define EMC_DLL_XFORM_DQ7 0x4ec
  231. #define EMC_DLI_TRIM_TXDQS8 0x520
  232. #define EMC_DLI_TRIM_TXDQS9 0x524
  233. #define EMC_DLI_TRIM_TXDQS10 0x528
  234. #define EMC_DLI_TRIM_TXDQS11 0x52c
  235. #define EMC_DLI_TRIM_TXDQS12 0x530
  236. #define EMC_DLI_TRIM_TXDQS13 0x534
  237. #define EMC_DLI_TRIM_TXDQS14 0x538
  238. #define EMC_DLI_TRIM_TXDQS15 0x53c
  239. #define EMC_CDB_CNTL_3 0x540
  240. #define EMC_XM2DQSPADCTRL5 0x544
  241. #define EMC_XM2DQSPADCTRL6 0x548
  242. #define EMC_XM2DQPADCTRL3 0x54c
  243. #define EMC_DLL_XFORM_ADDR3 0x550
  244. #define EMC_DLL_XFORM_ADDR4 0x554
  245. #define EMC_DLL_XFORM_ADDR5 0x558
  246. #define EMC_CFG_PIPE 0x560
  247. #define EMC_QPOP 0x564
  248. #define EMC_QUSE_WIDTH 0x568
  249. #define EMC_PUTERM_WIDTH 0x56c
  250. #define EMC_BGBIAS_CTL0 0x570
  251. #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX BIT(3)
  252. #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN BIT(2)
  253. #define EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD BIT(1)
  254. #define EMC_PUTERM_ADJ 0x574
  255. #define DRAM_DEV_SEL_ALL 0
  256. #define DRAM_DEV_SEL_0 (2 << 30)
  257. #define DRAM_DEV_SEL_1 (1 << 30)
  258. #define EMC_CFG_POWER_FEATURES_MASK \
  259. (EMC_CFG_DYN_SREF | EMC_CFG_DRAM_ACPD | EMC_CFG_DRAM_CLKSTOP_SR | \
  260. EMC_CFG_DRAM_CLKSTOP_PD | EMC_CFG_DSR_VTTGEN_DRV_EN)
  261. #define EMC_REFCTRL_DEV_SEL(n) (((n > 1) ? 0 : 2) << EMC_REFCTRL_DEV_SEL_SHIFT)
  262. #define EMC_DRAM_DEV_SEL(n) ((n > 1) ? DRAM_DEV_SEL_ALL : DRAM_DEV_SEL_0)
  263. /* Maximum amount of time in us. to wait for changes to become effective */
  264. #define EMC_STATUS_UPDATE_TIMEOUT 1000
  265. enum emc_dram_type {
  266. DRAM_TYPE_DDR3 = 0,
  267. DRAM_TYPE_DDR1 = 1,
  268. DRAM_TYPE_LPDDR3 = 2,
  269. DRAM_TYPE_DDR2 = 3
  270. };
  271. enum emc_dll_change {
  272. DLL_CHANGE_NONE,
  273. DLL_CHANGE_ON,
  274. DLL_CHANGE_OFF
  275. };
  276. static const unsigned long emc_burst_regs[] = {
  277. EMC_RC,
  278. EMC_RFC,
  279. EMC_RFC_SLR,
  280. EMC_RAS,
  281. EMC_RP,
  282. EMC_R2W,
  283. EMC_W2R,
  284. EMC_R2P,
  285. EMC_W2P,
  286. EMC_RD_RCD,
  287. EMC_WR_RCD,
  288. EMC_RRD,
  289. EMC_REXT,
  290. EMC_WEXT,
  291. EMC_WDV,
  292. EMC_WDV_MASK,
  293. EMC_QUSE,
  294. EMC_QUSE_WIDTH,
  295. EMC_IBDLY,
  296. EMC_EINPUT,
  297. EMC_EINPUT_DURATION,
  298. EMC_PUTERM_EXTRA,
  299. EMC_PUTERM_WIDTH,
  300. EMC_PUTERM_ADJ,
  301. EMC_CDB_CNTL_1,
  302. EMC_CDB_CNTL_2,
  303. EMC_CDB_CNTL_3,
  304. EMC_QRST,
  305. EMC_QSAFE,
  306. EMC_RDV,
  307. EMC_RDV_MASK,
  308. EMC_REFRESH,
  309. EMC_BURST_REFRESH_NUM,
  310. EMC_PRE_REFRESH_REQ_CNT,
  311. EMC_PDEX2WR,
  312. EMC_PDEX2RD,
  313. EMC_PCHG2PDEN,
  314. EMC_ACT2PDEN,
  315. EMC_AR2PDEN,
  316. EMC_RW2PDEN,
  317. EMC_TXSR,
  318. EMC_TXSRDLL,
  319. EMC_TCKE,
  320. EMC_TCKESR,
  321. EMC_TPD,
  322. EMC_TFAW,
  323. EMC_TRPAB,
  324. EMC_TCLKSTABLE,
  325. EMC_TCLKSTOP,
  326. EMC_TREFBW,
  327. EMC_FBIO_CFG6,
  328. EMC_ODT_WRITE,
  329. EMC_ODT_READ,
  330. EMC_FBIO_CFG5,
  331. EMC_CFG_DIG_DLL,
  332. EMC_CFG_DIG_DLL_PERIOD,
  333. EMC_DLL_XFORM_DQS0,
  334. EMC_DLL_XFORM_DQS1,
  335. EMC_DLL_XFORM_DQS2,
  336. EMC_DLL_XFORM_DQS3,
  337. EMC_DLL_XFORM_DQS4,
  338. EMC_DLL_XFORM_DQS5,
  339. EMC_DLL_XFORM_DQS6,
  340. EMC_DLL_XFORM_DQS7,
  341. EMC_DLL_XFORM_DQS8,
  342. EMC_DLL_XFORM_DQS9,
  343. EMC_DLL_XFORM_DQS10,
  344. EMC_DLL_XFORM_DQS11,
  345. EMC_DLL_XFORM_DQS12,
  346. EMC_DLL_XFORM_DQS13,
  347. EMC_DLL_XFORM_DQS14,
  348. EMC_DLL_XFORM_DQS15,
  349. EMC_DLL_XFORM_QUSE0,
  350. EMC_DLL_XFORM_QUSE1,
  351. EMC_DLL_XFORM_QUSE2,
  352. EMC_DLL_XFORM_QUSE3,
  353. EMC_DLL_XFORM_QUSE4,
  354. EMC_DLL_XFORM_QUSE5,
  355. EMC_DLL_XFORM_QUSE6,
  356. EMC_DLL_XFORM_QUSE7,
  357. EMC_DLL_XFORM_ADDR0,
  358. EMC_DLL_XFORM_ADDR1,
  359. EMC_DLL_XFORM_ADDR2,
  360. EMC_DLL_XFORM_ADDR3,
  361. EMC_DLL_XFORM_ADDR4,
  362. EMC_DLL_XFORM_ADDR5,
  363. EMC_DLL_XFORM_QUSE8,
  364. EMC_DLL_XFORM_QUSE9,
  365. EMC_DLL_XFORM_QUSE10,
  366. EMC_DLL_XFORM_QUSE11,
  367. EMC_DLL_XFORM_QUSE12,
  368. EMC_DLL_XFORM_QUSE13,
  369. EMC_DLL_XFORM_QUSE14,
  370. EMC_DLL_XFORM_QUSE15,
  371. EMC_DLI_TRIM_TXDQS0,
  372. EMC_DLI_TRIM_TXDQS1,
  373. EMC_DLI_TRIM_TXDQS2,
  374. EMC_DLI_TRIM_TXDQS3,
  375. EMC_DLI_TRIM_TXDQS4,
  376. EMC_DLI_TRIM_TXDQS5,
  377. EMC_DLI_TRIM_TXDQS6,
  378. EMC_DLI_TRIM_TXDQS7,
  379. EMC_DLI_TRIM_TXDQS8,
  380. EMC_DLI_TRIM_TXDQS9,
  381. EMC_DLI_TRIM_TXDQS10,
  382. EMC_DLI_TRIM_TXDQS11,
  383. EMC_DLI_TRIM_TXDQS12,
  384. EMC_DLI_TRIM_TXDQS13,
  385. EMC_DLI_TRIM_TXDQS14,
  386. EMC_DLI_TRIM_TXDQS15,
  387. EMC_DLL_XFORM_DQ0,
  388. EMC_DLL_XFORM_DQ1,
  389. EMC_DLL_XFORM_DQ2,
  390. EMC_DLL_XFORM_DQ3,
  391. EMC_DLL_XFORM_DQ4,
  392. EMC_DLL_XFORM_DQ5,
  393. EMC_DLL_XFORM_DQ6,
  394. EMC_DLL_XFORM_DQ7,
  395. EMC_XM2CMDPADCTRL,
  396. EMC_XM2CMDPADCTRL4,
  397. EMC_XM2CMDPADCTRL5,
  398. EMC_XM2DQPADCTRL2,
  399. EMC_XM2DQPADCTRL3,
  400. EMC_XM2CLKPADCTRL,
  401. EMC_XM2CLKPADCTRL2,
  402. EMC_XM2COMPPADCTRL,
  403. EMC_XM2VTTGENPADCTRL,
  404. EMC_XM2VTTGENPADCTRL2,
  405. EMC_XM2VTTGENPADCTRL3,
  406. EMC_XM2DQSPADCTRL3,
  407. EMC_XM2DQSPADCTRL4,
  408. EMC_XM2DQSPADCTRL5,
  409. EMC_XM2DQSPADCTRL6,
  410. EMC_DSR_VTTGEN_DRV,
  411. EMC_TXDSRVTTGEN,
  412. EMC_FBIO_SPARE,
  413. EMC_ZCAL_WAIT_CNT,
  414. EMC_MRS_WAIT_CNT2,
  415. EMC_CTT,
  416. EMC_CTT_DURATION,
  417. EMC_CFG_PIPE,
  418. EMC_DYN_SELF_REF_CONTROL,
  419. EMC_QPOP
  420. };
  421. struct emc_timing {
  422. unsigned long rate;
  423. u32 emc_burst_data[ARRAY_SIZE(emc_burst_regs)];
  424. u32 emc_auto_cal_config;
  425. u32 emc_auto_cal_config2;
  426. u32 emc_auto_cal_config3;
  427. u32 emc_auto_cal_interval;
  428. u32 emc_bgbias_ctl0;
  429. u32 emc_cfg;
  430. u32 emc_cfg_2;
  431. u32 emc_ctt_term_ctrl;
  432. u32 emc_mode_1;
  433. u32 emc_mode_2;
  434. u32 emc_mode_4;
  435. u32 emc_mode_reset;
  436. u32 emc_mrs_wait_cnt;
  437. u32 emc_sel_dpd_ctrl;
  438. u32 emc_xm2dqspadctrl2;
  439. u32 emc_zcal_cnt_long;
  440. u32 emc_zcal_interval;
  441. };
  442. struct tegra_emc {
  443. struct device *dev;
  444. struct tegra_mc *mc;
  445. void __iomem *regs;
  446. enum emc_dram_type dram_type;
  447. unsigned int dram_num;
  448. struct emc_timing last_timing;
  449. struct emc_timing *timings;
  450. unsigned int num_timings;
  451. };
  452. /* Timing change sequence functions */
  453. static void emc_ccfifo_writel(struct tegra_emc *emc, u32 value,
  454. unsigned long offset)
  455. {
  456. writel(value, emc->regs + EMC_CCFIFO_DATA);
  457. writel(offset, emc->regs + EMC_CCFIFO_ADDR);
  458. }
  459. static void emc_seq_update_timing(struct tegra_emc *emc)
  460. {
  461. unsigned int i;
  462. u32 value;
  463. writel(1, emc->regs + EMC_TIMING_CONTROL);
  464. for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
  465. value = readl(emc->regs + EMC_STATUS);
  466. if ((value & EMC_STATUS_TIMING_UPDATE_STALLED) == 0)
  467. return;
  468. udelay(1);
  469. }
  470. dev_err(emc->dev, "timing update timed out\n");
  471. }
  472. static void emc_seq_disable_auto_cal(struct tegra_emc *emc)
  473. {
  474. unsigned int i;
  475. u32 value;
  476. writel(0, emc->regs + EMC_AUTO_CAL_INTERVAL);
  477. for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
  478. value = readl(emc->regs + EMC_AUTO_CAL_STATUS);
  479. if ((value & EMC_AUTO_CAL_STATUS_ACTIVE) == 0)
  480. return;
  481. udelay(1);
  482. }
  483. dev_err(emc->dev, "auto cal disable timed out\n");
  484. }
  485. static void emc_seq_wait_clkchange(struct tegra_emc *emc)
  486. {
  487. unsigned int i;
  488. u32 value;
  489. for (i = 0; i < EMC_STATUS_UPDATE_TIMEOUT; ++i) {
  490. value = readl(emc->regs + EMC_INTSTATUS);
  491. if (value & EMC_INTSTATUS_CLKCHANGE_COMPLETE)
  492. return;
  493. udelay(1);
  494. }
  495. dev_err(emc->dev, "clock change timed out\n");
  496. }
  497. static struct emc_timing *tegra_emc_find_timing(struct tegra_emc *emc,
  498. unsigned long rate)
  499. {
  500. struct emc_timing *timing = NULL;
  501. unsigned int i;
  502. for (i = 0; i < emc->num_timings; i++) {
  503. if (emc->timings[i].rate == rate) {
  504. timing = &emc->timings[i];
  505. break;
  506. }
  507. }
  508. if (!timing) {
  509. dev_err(emc->dev, "no timing for rate %lu\n", rate);
  510. return NULL;
  511. }
  512. return timing;
  513. }
  514. int tegra_emc_prepare_timing_change(struct tegra_emc *emc,
  515. unsigned long rate)
  516. {
  517. struct emc_timing *timing = tegra_emc_find_timing(emc, rate);
  518. struct emc_timing *last = &emc->last_timing;
  519. enum emc_dll_change dll_change;
  520. unsigned int pre_wait = 0;
  521. u32 val, val2, mask;
  522. bool update = false;
  523. unsigned int i;
  524. if (!timing)
  525. return -ENOENT;
  526. if ((last->emc_mode_1 & 0x1) == (timing->emc_mode_1 & 0x1))
  527. dll_change = DLL_CHANGE_NONE;
  528. else if (timing->emc_mode_1 & 0x1)
  529. dll_change = DLL_CHANGE_ON;
  530. else
  531. dll_change = DLL_CHANGE_OFF;
  532. /* Clear CLKCHANGE_COMPLETE interrupts */
  533. writel(EMC_INTSTATUS_CLKCHANGE_COMPLETE, emc->regs + EMC_INTSTATUS);
  534. /* Disable dynamic self-refresh */
  535. val = readl(emc->regs + EMC_CFG);
  536. if (val & EMC_CFG_PWR_MASK) {
  537. val &= ~EMC_CFG_POWER_FEATURES_MASK;
  538. writel(val, emc->regs + EMC_CFG);
  539. pre_wait = 5;
  540. }
  541. /* Disable SEL_DPD_CTRL for clock change */
  542. if (emc->dram_type == DRAM_TYPE_DDR3)
  543. mask = EMC_SEL_DPD_CTRL_DDR3_MASK;
  544. else
  545. mask = EMC_SEL_DPD_CTRL_MASK;
  546. val = readl(emc->regs + EMC_SEL_DPD_CTRL);
  547. if (val & mask) {
  548. val &= ~mask;
  549. writel(val, emc->regs + EMC_SEL_DPD_CTRL);
  550. }
  551. /* Prepare DQ/DQS for clock change */
  552. val = readl(emc->regs + EMC_BGBIAS_CTL0);
  553. val2 = last->emc_bgbias_ctl0;
  554. if (!(timing->emc_bgbias_ctl0 &
  555. EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) &&
  556. (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX)) {
  557. val2 &= ~EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX;
  558. update = true;
  559. }
  560. if ((val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD) ||
  561. (val & EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN)) {
  562. update = true;
  563. }
  564. if (update) {
  565. writel(val2, emc->regs + EMC_BGBIAS_CTL0);
  566. if (pre_wait < 5)
  567. pre_wait = 5;
  568. }
  569. update = false;
  570. val = readl(emc->regs + EMC_XM2DQSPADCTRL2);
  571. if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_VREF_ENABLE &&
  572. !(val & EMC_XM2DQSPADCTRL2_VREF_ENABLE)) {
  573. val |= EMC_XM2DQSPADCTRL2_VREF_ENABLE;
  574. update = true;
  575. }
  576. if (timing->emc_xm2dqspadctrl2 & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE &&
  577. !(val & EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE)) {
  578. val |= EMC_XM2DQSPADCTRL2_RX_FT_REC_ENABLE;
  579. update = true;
  580. }
  581. if (update) {
  582. writel(val, emc->regs + EMC_XM2DQSPADCTRL2);
  583. if (pre_wait < 30)
  584. pre_wait = 30;
  585. }
  586. /* Wait to settle */
  587. if (pre_wait) {
  588. emc_seq_update_timing(emc);
  589. udelay(pre_wait);
  590. }
  591. /* Program CTT_TERM control */
  592. if (last->emc_ctt_term_ctrl != timing->emc_ctt_term_ctrl) {
  593. emc_seq_disable_auto_cal(emc);
  594. writel(timing->emc_ctt_term_ctrl,
  595. emc->regs + EMC_CTT_TERM_CTRL);
  596. emc_seq_update_timing(emc);
  597. }
  598. /* Program burst shadow registers */
  599. for (i = 0; i < ARRAY_SIZE(timing->emc_burst_data); ++i)
  600. writel(timing->emc_burst_data[i],
  601. emc->regs + emc_burst_regs[i]);
  602. writel(timing->emc_xm2dqspadctrl2, emc->regs + EMC_XM2DQSPADCTRL2);
  603. writel(timing->emc_zcal_interval, emc->regs + EMC_ZCAL_INTERVAL);
  604. tegra_mc_write_emem_configuration(emc->mc, timing->rate);
  605. val = timing->emc_cfg & ~EMC_CFG_POWER_FEATURES_MASK;
  606. emc_ccfifo_writel(emc, val, EMC_CFG);
  607. /* Program AUTO_CAL_CONFIG */
  608. if (timing->emc_auto_cal_config2 != last->emc_auto_cal_config2)
  609. emc_ccfifo_writel(emc, timing->emc_auto_cal_config2,
  610. EMC_AUTO_CAL_CONFIG2);
  611. if (timing->emc_auto_cal_config3 != last->emc_auto_cal_config3)
  612. emc_ccfifo_writel(emc, timing->emc_auto_cal_config3,
  613. EMC_AUTO_CAL_CONFIG3);
  614. if (timing->emc_auto_cal_config != last->emc_auto_cal_config) {
  615. val = timing->emc_auto_cal_config;
  616. val &= EMC_AUTO_CAL_CONFIG_AUTO_CAL_START;
  617. emc_ccfifo_writel(emc, val, EMC_AUTO_CAL_CONFIG);
  618. }
  619. /* DDR3: predict MRS long wait count */
  620. if (emc->dram_type == DRAM_TYPE_DDR3 &&
  621. dll_change == DLL_CHANGE_ON) {
  622. u32 cnt = 512;
  623. if (timing->emc_zcal_interval != 0 &&
  624. last->emc_zcal_interval == 0)
  625. cnt -= emc->dram_num * 256;
  626. val = (timing->emc_mrs_wait_cnt
  627. & EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK)
  628. >> EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT;
  629. if (cnt < val)
  630. cnt = val;
  631. val = timing->emc_mrs_wait_cnt
  632. & ~EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
  633. val |= (cnt << EMC_MRS_WAIT_CNT_LONG_WAIT_SHIFT)
  634. & EMC_MRS_WAIT_CNT_LONG_WAIT_MASK;
  635. writel(val, emc->regs + EMC_MRS_WAIT_CNT);
  636. }
  637. val = timing->emc_cfg_2;
  638. val &= ~EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR;
  639. emc_ccfifo_writel(emc, val, EMC_CFG_2);
  640. /* DDR3: Turn off DLL and enter self-refresh */
  641. if (emc->dram_type == DRAM_TYPE_DDR3 && dll_change == DLL_CHANGE_OFF)
  642. emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS);
  643. /* Disable refresh controller */
  644. emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num),
  645. EMC_REFCTRL);
  646. if (emc->dram_type == DRAM_TYPE_DDR3)
  647. emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num) |
  648. EMC_SELF_REF_CMD_ENABLED,
  649. EMC_SELF_REF);
  650. /* Flow control marker */
  651. emc_ccfifo_writel(emc, 1, EMC_STALL_THEN_EXE_AFTER_CLKCHANGE);
  652. /* DDR3: Exit self-refresh */
  653. if (emc->dram_type == DRAM_TYPE_DDR3)
  654. emc_ccfifo_writel(emc, EMC_DRAM_DEV_SEL(emc->dram_num),
  655. EMC_SELF_REF);
  656. emc_ccfifo_writel(emc, EMC_REFCTRL_DEV_SEL(emc->dram_num) |
  657. EMC_REFCTRL_ENABLE,
  658. EMC_REFCTRL);
  659. /* Set DRAM mode registers */
  660. if (emc->dram_type == DRAM_TYPE_DDR3) {
  661. if (timing->emc_mode_1 != last->emc_mode_1)
  662. emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_EMRS);
  663. if (timing->emc_mode_2 != last->emc_mode_2)
  664. emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_EMRS2);
  665. if ((timing->emc_mode_reset != last->emc_mode_reset) ||
  666. dll_change == DLL_CHANGE_ON) {
  667. val = timing->emc_mode_reset;
  668. if (dll_change == DLL_CHANGE_ON) {
  669. val |= EMC_MODE_SET_DLL_RESET;
  670. val |= EMC_MODE_SET_LONG_CNT;
  671. } else {
  672. val &= ~EMC_MODE_SET_DLL_RESET;
  673. }
  674. emc_ccfifo_writel(emc, val, EMC_MRS);
  675. }
  676. } else {
  677. if (timing->emc_mode_2 != last->emc_mode_2)
  678. emc_ccfifo_writel(emc, timing->emc_mode_2, EMC_MRW2);
  679. if (timing->emc_mode_1 != last->emc_mode_1)
  680. emc_ccfifo_writel(emc, timing->emc_mode_1, EMC_MRW);
  681. if (timing->emc_mode_4 != last->emc_mode_4)
  682. emc_ccfifo_writel(emc, timing->emc_mode_4, EMC_MRW4);
  683. }
  684. /* Issue ZCAL command if turning ZCAL on */
  685. if (timing->emc_zcal_interval != 0 && last->emc_zcal_interval == 0) {
  686. emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV0, EMC_ZQ_CAL);
  687. if (emc->dram_num > 1)
  688. emc_ccfifo_writel(emc, EMC_ZQ_CAL_LONG_CMD_DEV1,
  689. EMC_ZQ_CAL);
  690. }
  691. /* Write to RO register to remove stall after change */
  692. emc_ccfifo_writel(emc, 0, EMC_CCFIFO_STATUS);
  693. if (timing->emc_cfg_2 & EMC_CFG_2_DIS_STP_OB_CLK_DURING_NON_WR)
  694. emc_ccfifo_writel(emc, timing->emc_cfg_2, EMC_CFG_2);
  695. /* Disable AUTO_CAL for clock change */
  696. emc_seq_disable_auto_cal(emc);
  697. /* Read register to wait until programming has settled */
  698. readl(emc->regs + EMC_INTSTATUS);
  699. return 0;
  700. }
  701. void tegra_emc_complete_timing_change(struct tegra_emc *emc,
  702. unsigned long rate)
  703. {
  704. struct emc_timing *timing = tegra_emc_find_timing(emc, rate);
  705. struct emc_timing *last = &emc->last_timing;
  706. u32 val;
  707. if (!timing)
  708. return;
  709. /* Wait until the state machine has settled */
  710. emc_seq_wait_clkchange(emc);
  711. /* Restore AUTO_CAL */
  712. if (timing->emc_ctt_term_ctrl != last->emc_ctt_term_ctrl)
  713. writel(timing->emc_auto_cal_interval,
  714. emc->regs + EMC_AUTO_CAL_INTERVAL);
  715. /* Restore dynamic self-refresh */
  716. if (timing->emc_cfg & EMC_CFG_PWR_MASK)
  717. writel(timing->emc_cfg, emc->regs + EMC_CFG);
  718. /* Set ZCAL wait count */
  719. writel(timing->emc_zcal_cnt_long, emc->regs + EMC_ZCAL_WAIT_CNT);
  720. /* LPDDR3: Turn off BGBIAS if low frequency */
  721. if (emc->dram_type == DRAM_TYPE_LPDDR3 &&
  722. timing->emc_bgbias_ctl0 &
  723. EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_RX) {
  724. val = timing->emc_bgbias_ctl0;
  725. val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD_IBIAS_VTTGEN;
  726. val |= EMC_BGBIAS_CTL0_BIAS0_DSC_E_PWRD;
  727. writel(val, emc->regs + EMC_BGBIAS_CTL0);
  728. } else {
  729. if (emc->dram_type == DRAM_TYPE_DDR3 &&
  730. readl(emc->regs + EMC_BGBIAS_CTL0) !=
  731. timing->emc_bgbias_ctl0) {
  732. writel(timing->emc_bgbias_ctl0,
  733. emc->regs + EMC_BGBIAS_CTL0);
  734. }
  735. writel(timing->emc_auto_cal_interval,
  736. emc->regs + EMC_AUTO_CAL_INTERVAL);
  737. }
  738. /* Wait for timing to settle */
  739. udelay(2);
  740. /* Reprogram SEL_DPD_CTRL */
  741. writel(timing->emc_sel_dpd_ctrl, emc->regs + EMC_SEL_DPD_CTRL);
  742. emc_seq_update_timing(emc);
  743. emc->last_timing = *timing;
  744. }
  745. /* Initialization and deinitialization */
  746. static void emc_read_current_timing(struct tegra_emc *emc,
  747. struct emc_timing *timing)
  748. {
  749. unsigned int i;
  750. for (i = 0; i < ARRAY_SIZE(emc_burst_regs); ++i)
  751. timing->emc_burst_data[i] =
  752. readl(emc->regs + emc_burst_regs[i]);
  753. timing->emc_cfg = readl(emc->regs + EMC_CFG);
  754. timing->emc_auto_cal_interval = 0;
  755. timing->emc_zcal_cnt_long = 0;
  756. timing->emc_mode_1 = 0;
  757. timing->emc_mode_2 = 0;
  758. timing->emc_mode_4 = 0;
  759. timing->emc_mode_reset = 0;
  760. }
  761. static int emc_init(struct tegra_emc *emc)
  762. {
  763. emc->dram_type = readl(emc->regs + EMC_FBIO_CFG5);
  764. emc->dram_type &= EMC_FBIO_CFG5_DRAM_TYPE_MASK;
  765. emc->dram_type >>= EMC_FBIO_CFG5_DRAM_TYPE_SHIFT;
  766. emc->dram_num = tegra_mc_get_emem_device_count(emc->mc);
  767. emc_read_current_timing(emc, &emc->last_timing);
  768. return 0;
  769. }
  770. static int load_one_timing_from_dt(struct tegra_emc *emc,
  771. struct emc_timing *timing,
  772. struct device_node *node)
  773. {
  774. u32 value;
  775. int err;
  776. err = of_property_read_u32(node, "clock-frequency", &value);
  777. if (err) {
  778. dev_err(emc->dev, "timing %s: failed to read rate: %d\n",
  779. node->name, err);
  780. return err;
  781. }
  782. timing->rate = value;
  783. err = of_property_read_u32_array(node, "nvidia,emc-configuration",
  784. timing->emc_burst_data,
  785. ARRAY_SIZE(timing->emc_burst_data));
  786. if (err) {
  787. dev_err(emc->dev,
  788. "timing %s: failed to read emc burst data: %d\n",
  789. node->name, err);
  790. return err;
  791. }
  792. #define EMC_READ_PROP(prop, dtprop) { \
  793. err = of_property_read_u32(node, dtprop, &timing->prop); \
  794. if (err) { \
  795. dev_err(emc->dev, "timing %s: failed to read " #prop ": %d\n", \
  796. node->name, err); \
  797. return err; \
  798. } \
  799. }
  800. EMC_READ_PROP(emc_auto_cal_config, "nvidia,emc-auto-cal-config")
  801. EMC_READ_PROP(emc_auto_cal_config2, "nvidia,emc-auto-cal-config2")
  802. EMC_READ_PROP(emc_auto_cal_config3, "nvidia,emc-auto-cal-config3")
  803. EMC_READ_PROP(emc_auto_cal_interval, "nvidia,emc-auto-cal-interval")
  804. EMC_READ_PROP(emc_bgbias_ctl0, "nvidia,emc-bgbias-ctl0")
  805. EMC_READ_PROP(emc_cfg, "nvidia,emc-cfg")
  806. EMC_READ_PROP(emc_cfg_2, "nvidia,emc-cfg-2")
  807. EMC_READ_PROP(emc_ctt_term_ctrl, "nvidia,emc-ctt-term-ctrl")
  808. EMC_READ_PROP(emc_mode_1, "nvidia,emc-mode-1")
  809. EMC_READ_PROP(emc_mode_2, "nvidia,emc-mode-2")
  810. EMC_READ_PROP(emc_mode_4, "nvidia,emc-mode-4")
  811. EMC_READ_PROP(emc_mode_reset, "nvidia,emc-mode-reset")
  812. EMC_READ_PROP(emc_mrs_wait_cnt, "nvidia,emc-mrs-wait-cnt")
  813. EMC_READ_PROP(emc_sel_dpd_ctrl, "nvidia,emc-sel-dpd-ctrl")
  814. EMC_READ_PROP(emc_xm2dqspadctrl2, "nvidia,emc-xm2dqspadctrl2")
  815. EMC_READ_PROP(emc_zcal_cnt_long, "nvidia,emc-zcal-cnt-long")
  816. EMC_READ_PROP(emc_zcal_interval, "nvidia,emc-zcal-interval")
  817. #undef EMC_READ_PROP
  818. return 0;
  819. }
  820. static int cmp_timings(const void *_a, const void *_b)
  821. {
  822. const struct emc_timing *a = _a;
  823. const struct emc_timing *b = _b;
  824. if (a->rate < b->rate)
  825. return -1;
  826. else if (a->rate == b->rate)
  827. return 0;
  828. else
  829. return 1;
  830. }
  831. static int tegra_emc_load_timings_from_dt(struct tegra_emc *emc,
  832. struct device_node *node)
  833. {
  834. int child_count = of_get_child_count(node);
  835. struct device_node *child;
  836. struct emc_timing *timing;
  837. unsigned int i = 0;
  838. int err;
  839. emc->timings = devm_kcalloc(emc->dev, child_count, sizeof(*timing),
  840. GFP_KERNEL);
  841. if (!emc->timings)
  842. return -ENOMEM;
  843. emc->num_timings = child_count;
  844. for_each_child_of_node(node, child) {
  845. timing = &emc->timings[i++];
  846. err = load_one_timing_from_dt(emc, timing, child);
  847. if (err)
  848. return err;
  849. }
  850. sort(emc->timings, emc->num_timings, sizeof(*timing), cmp_timings,
  851. NULL);
  852. return 0;
  853. }
  854. static const struct of_device_id tegra_emc_of_match[] = {
  855. { .compatible = "nvidia,tegra124-emc" },
  856. {}
  857. };
  858. static struct device_node *
  859. tegra_emc_find_node_by_ram_code(struct device_node *node, u32 ram_code)
  860. {
  861. struct device_node *np;
  862. int err;
  863. for_each_child_of_node(node, np) {
  864. u32 value;
  865. err = of_property_read_u32(np, "nvidia,ram-code", &value);
  866. if (err || (value != ram_code)) {
  867. of_node_put(np);
  868. continue;
  869. }
  870. return np;
  871. }
  872. return NULL;
  873. }
  874. /* Debugfs entry */
  875. static int emc_debug_rate_get(void *data, u64 *rate)
  876. {
  877. struct clk *c = data;
  878. *rate = clk_get_rate(c);
  879. return 0;
  880. }
  881. static int emc_debug_rate_set(void *data, u64 rate)
  882. {
  883. struct clk *c = data;
  884. return clk_set_rate(c, rate);
  885. }
  886. DEFINE_SIMPLE_ATTRIBUTE(emc_debug_rate_fops, emc_debug_rate_get,
  887. emc_debug_rate_set, "%lld\n");
  888. static int emc_debug_supported_rates_show(struct seq_file *s, void *data)
  889. {
  890. struct tegra_emc *emc = s->private;
  891. const char *prefix = "";
  892. unsigned int i;
  893. for (i = 0; i < emc->num_timings; i++) {
  894. struct emc_timing *timing = &emc->timings[i];
  895. seq_printf(s, "%s%lu", prefix, timing->rate);
  896. prefix = " ";
  897. }
  898. seq_puts(s, "\n");
  899. return 0;
  900. }
  901. static int emc_debug_supported_rates_open(struct inode *inode,
  902. struct file *file)
  903. {
  904. return single_open(file, emc_debug_supported_rates_show,
  905. inode->i_private);
  906. }
  907. static const struct file_operations emc_debug_supported_rates_fops = {
  908. .open = emc_debug_supported_rates_open,
  909. .read = seq_read,
  910. .llseek = seq_lseek,
  911. .release = single_release,
  912. };
  913. static void emc_debugfs_init(struct device *dev, struct tegra_emc *emc)
  914. {
  915. struct dentry *root, *file;
  916. struct clk *clk;
  917. root = debugfs_create_dir("emc", NULL);
  918. if (!root) {
  919. dev_err(dev, "failed to create debugfs directory\n");
  920. return;
  921. }
  922. clk = clk_get_sys("tegra-clk-debug", "emc");
  923. if (IS_ERR(clk)) {
  924. dev_err(dev, "failed to get debug clock: %ld\n", PTR_ERR(clk));
  925. return;
  926. }
  927. file = debugfs_create_file("rate", S_IRUGO | S_IWUSR, root, clk,
  928. &emc_debug_rate_fops);
  929. if (!file)
  930. dev_err(dev, "failed to create debugfs entry\n");
  931. file = debugfs_create_file("supported_rates", S_IRUGO, root, emc,
  932. &emc_debug_supported_rates_fops);
  933. if (!file)
  934. dev_err(dev, "failed to create debugfs entry\n");
  935. }
  936. static int tegra_emc_probe(struct platform_device *pdev)
  937. {
  938. struct platform_device *mc;
  939. struct device_node *np;
  940. struct tegra_emc *emc;
  941. struct resource *res;
  942. u32 ram_code;
  943. int err;
  944. emc = devm_kzalloc(&pdev->dev, sizeof(*emc), GFP_KERNEL);
  945. if (!emc)
  946. return -ENOMEM;
  947. emc->dev = &pdev->dev;
  948. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  949. emc->regs = devm_ioremap_resource(&pdev->dev, res);
  950. if (IS_ERR(emc->regs))
  951. return PTR_ERR(emc->regs);
  952. np = of_parse_phandle(pdev->dev.of_node, "nvidia,memory-controller", 0);
  953. if (!np) {
  954. dev_err(&pdev->dev, "could not get memory controller\n");
  955. return -ENOENT;
  956. }
  957. mc = of_find_device_by_node(np);
  958. if (!mc)
  959. return -ENOENT;
  960. of_node_put(np);
  961. emc->mc = platform_get_drvdata(mc);
  962. if (!emc->mc)
  963. return -EPROBE_DEFER;
  964. ram_code = tegra_read_ram_code();
  965. np = tegra_emc_find_node_by_ram_code(pdev->dev.of_node, ram_code);
  966. if (!np) {
  967. dev_err(&pdev->dev,
  968. "no memory timings for RAM code %u found in DT\n",
  969. ram_code);
  970. return -ENOENT;
  971. }
  972. err = tegra_emc_load_timings_from_dt(emc, np);
  973. of_node_put(np);
  974. if (err)
  975. return err;
  976. if (emc->num_timings == 0) {
  977. dev_err(&pdev->dev,
  978. "no memory timings for RAM code %u registered\n",
  979. ram_code);
  980. return -ENOENT;
  981. }
  982. err = emc_init(emc);
  983. if (err) {
  984. dev_err(&pdev->dev, "EMC initialization failed: %d\n", err);
  985. return err;
  986. }
  987. platform_set_drvdata(pdev, emc);
  988. if (IS_ENABLED(CONFIG_DEBUG_FS))
  989. emc_debugfs_init(&pdev->dev, emc);
  990. return 0;
  991. };
  992. static struct platform_driver tegra_emc_driver = {
  993. .probe = tegra_emc_probe,
  994. .driver = {
  995. .name = "tegra-emc",
  996. .of_match_table = tegra_emc_of_match,
  997. .suppress_bind_attrs = true,
  998. },
  999. };
  1000. static int tegra_emc_init(void)
  1001. {
  1002. return platform_driver_register(&tegra_emc_driver);
  1003. }
  1004. subsys_initcall(tegra_emc_init);