blob: 25e3976e6569a0d10d900511cb8f5047755b1698 [file] [log] [blame]
Dave Gerlachd712b362021-05-11 10:22:11 -05001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments' K3 DDRSS driver
4 *
5 * Copyright (C) 2020-2021 Texas Instruments Incorporated - http://www.ti.com/
6 */
7
8#include <common.h>
9#include <clk.h>
10#include <dm.h>
11#include <dm/device_compat.h>
12#include <ram.h>
13#include <hang.h>
14#include <log.h>
15#include <asm/io.h>
16#include <power-domain.h>
17#include <wait_bit.h>
Lokesh Vutladd01c632021-05-11 10:22:13 -050018#include <power/regulator.h>
Dave Gerlachd712b362021-05-11 10:22:11 -050019
20#include "lpddr4_obj_if.h"
21#include "lpddr4_if.h"
22#include "lpddr4_structs_if.h"
23#include "lpddr4_ctl_regs.h"
24
25#define SRAM_MAX 512
26
27#define CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS 0x80
28#define CTRLMMR_DDR4_FSP_CLKCHNG_ACK_OFFS 0xc0
29
Wolfgang Denk62fb2b42021-09-27 17:42:39 +020030#define DDRSS_V2A_R1_MAT_REG 0x0020
31#define DDRSS_ECC_CTRL_REG 0x0120
Dave Gerlach2c861a92021-05-11 10:22:12 -050032
Aswath Govindrajub232cb42022-01-25 20:56:29 +053033#define SINGLE_DDR_SUBSYSTEM 0x1
34#define MULTI_DDR_SUBSYSTEM 0x2
35
Aswath Govindraju6324bc72022-01-25 20:56:30 +053036#define MULTI_DDR_CFG0 0x00114100
37#define MULTI_DDR_CFG1 0x00114104
38#define DDR_CFG_LOAD 0x00114110
39
40enum intrlv_gran {
41 GRAN_128B,
42 GRAN_512B,
43 GRAN_2KB,
44 GRAN_4KB,
45 GRAN_16KB,
46 GRAN_32KB,
47 GRAN_512KB,
48 GRAN_1GB,
49 GRAN_1_5GB,
50 GRAN_2GB,
51 GRAN_3GB,
52 GRAN_4GB,
53 GRAN_6GB,
54 GRAN_8GB,
55 GRAN_16GB
56};
57
58enum intrlv_size {
59 SIZE_0,
60 SIZE_128MB,
61 SIZE_256MB,
62 SIZE_512MB,
63 SIZE_1GB,
64 SIZE_2GB,
65 SIZE_3GB,
66 SIZE_4GB,
67 SIZE_6GB,
68 SIZE_8GB,
69 SIZE_12GB,
70 SIZE_16GB,
71 SIZE_32GB
72};
73
74struct k3_ddrss_data {
75 u32 flags;
76};
77
78enum ecc_enable {
79 DISABLE_ALL = 0,
80 ENABLE_0,
81 ENABLE_1,
82 ENABLE_ALL
83};
84
85enum emif_config {
86 INTERLEAVE_ALL = 0,
87 SEPR0,
88 SEPR1
89};
90
91enum emif_active {
92 EMIF_0 = 1,
93 EMIF_1,
94 EMIF_ALL
95};
96
97struct k3_msmc {
98 enum intrlv_gran gran;
99 enum intrlv_size size;
100 enum ecc_enable enable;
101 enum emif_config config;
102 enum emif_active active;
103};
104
Dave Gerlachd712b362021-05-11 10:22:11 -0500105struct k3_ddrss_desc {
106 struct udevice *dev;
107 void __iomem *ddrss_ss_cfg;
108 void __iomem *ddrss_ctrl_mmr;
109 struct power_domain ddrcfg_pwrdmn;
110 struct power_domain ddrdata_pwrdmn;
111 struct clk ddr_clk;
112 struct clk osc_clk;
113 u32 ddr_freq1;
114 u32 ddr_freq2;
115 u32 ddr_fhs_cnt;
Lokesh Vutladd01c632021-05-11 10:22:13 -0500116 struct udevice *vtt_supply;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530117 u32 instance;
118 lpddr4_obj *driverdt;
119 lpddr4_config config;
120 lpddr4_privatedata pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500121};
122
Dave Gerlachd712b362021-05-11 10:22:11 -0500123struct reginitdata {
124 u32 ctl_regs[LPDDR4_INTR_CTL_REG_COUNT];
125 u16 ctl_regs_offs[LPDDR4_INTR_CTL_REG_COUNT];
126 u32 pi_regs[LPDDR4_INTR_PHY_INDEP_REG_COUNT];
127 u16 pi_regs_offs[LPDDR4_INTR_PHY_INDEP_REG_COUNT];
128 u32 phy_regs[LPDDR4_INTR_PHY_REG_COUNT];
129 u16 phy_regs_offs[LPDDR4_INTR_PHY_REG_COUNT];
130};
131
132#define TH_MACRO_EXP(fld, str) (fld##str)
133
134#define TH_FLD_MASK(fld) TH_MACRO_EXP(fld, _MASK)
135#define TH_FLD_SHIFT(fld) TH_MACRO_EXP(fld, _SHIFT)
136#define TH_FLD_WIDTH(fld) TH_MACRO_EXP(fld, _WIDTH)
137#define TH_FLD_WOCLR(fld) TH_MACRO_EXP(fld, _WOCLR)
138#define TH_FLD_WOSET(fld) TH_MACRO_EXP(fld, _WOSET)
139
140#define str(s) #s
141#define xstr(s) str(s)
142
143#define CTL_SHIFT 11
144#define PHY_SHIFT 11
145#define PI_SHIFT 10
146
147#define DENALI_CTL_0_DRAM_CLASS_DDR4 0xA
148#define DENALI_CTL_0_DRAM_CLASS_LPDDR4 0xB
149
150#define TH_OFFSET_FROM_REG(REG, SHIFT, offset) do {\
151 char *i, *pstr = xstr(REG); offset = 0;\
152 for (i = &pstr[SHIFT]; *i != '\0'; ++i) {\
153 offset = offset * 10 + (*i - '0'); } \
154 } while (0)
155
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530156static u32 k3_lpddr4_read_ddr_type(const lpddr4_privatedata *pd)
Dave Gerlachd712b362021-05-11 10:22:11 -0500157{
158 u32 status = 0U;
159 u32 offset = 0U;
160 u32 regval = 0U;
161 u32 dram_class = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530162 struct k3_ddrss_desc *ddrss = (struct k3_ddrss_desc *)pd->ddr_instance;
Dave Gerlachd712b362021-05-11 10:22:11 -0500163
164 TH_OFFSET_FROM_REG(LPDDR4__DRAM_CLASS__REG, CTL_SHIFT, offset);
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530165 status = ddrss->driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, &regval);
Dave Gerlachd712b362021-05-11 10:22:11 -0500166 if (status > 0U) {
167 printf("%s: Failed to read DRAM_CLASS\n", __func__);
168 hang();
169 }
170
171 dram_class = ((regval & TH_FLD_MASK(LPDDR4__DRAM_CLASS__FLD)) >>
172 TH_FLD_SHIFT(LPDDR4__DRAM_CLASS__FLD));
173 return dram_class;
174}
175
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530176static void k3_lpddr4_freq_update(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500177{
178 unsigned int req_type, counter;
179
180 for (counter = 0; counter < ddrss->ddr_fhs_cnt; counter++) {
181 if (wait_for_bit_le32(ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530182 CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS + ddrss->instance * 0x10, 0x80,
Dave Gerlachd712b362021-05-11 10:22:11 -0500183 true, 10000, false)) {
184 printf("Timeout during frequency handshake\n");
185 hang();
186 }
187
188 req_type = readl(ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530189 CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS + ddrss->instance * 0x10) & 0x03;
Dave Gerlachd712b362021-05-11 10:22:11 -0500190
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530191 debug("%s: received freq change req: req type = %d, req no. = %d, instance = %d\n",
192 __func__, req_type, counter, ddrss->instance);
Dave Gerlachd712b362021-05-11 10:22:11 -0500193
194 if (req_type == 1)
195 clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq1);
196 else if (req_type == 2)
197 clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq2);
198 else if (req_type == 0)
199 /* Put DDR pll in bypass mode */
200 clk_set_rate(&ddrss->ddr_clk,
201 clk_get_rate(&ddrss->osc_clk));
202 else
203 printf("%s: Invalid freq request type\n", __func__);
204
205 writel(0x1, ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530206 CTRLMMR_DDR4_FSP_CLKCHNG_ACK_OFFS + ddrss->instance * 0x10);
Dave Gerlachd712b362021-05-11 10:22:11 -0500207 if (wait_for_bit_le32(ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530208 CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS + ddrss->instance * 0x10, 0x80,
Dave Gerlachd712b362021-05-11 10:22:11 -0500209 false, 10, false)) {
210 printf("Timeout during frequency handshake\n");
211 hang();
212 }
213 writel(0x0, ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530214 CTRLMMR_DDR4_FSP_CLKCHNG_ACK_OFFS + ddrss->instance * 0x10);
Dave Gerlachd712b362021-05-11 10:22:11 -0500215 }
216}
217
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530218static void k3_lpddr4_ack_freq_upd_req(const lpddr4_privatedata *pd)
Dave Gerlachd712b362021-05-11 10:22:11 -0500219{
220 u32 dram_class;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530221 struct k3_ddrss_desc *ddrss = (struct k3_ddrss_desc *)pd->ddr_instance;
Dave Gerlachd712b362021-05-11 10:22:11 -0500222
223 debug("--->>> LPDDR4 Initialization is in progress ... <<<---\n");
224
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530225 dram_class = k3_lpddr4_read_ddr_type(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500226
227 switch (dram_class) {
228 case DENALI_CTL_0_DRAM_CLASS_DDR4:
229 break;
230 case DENALI_CTL_0_DRAM_CLASS_LPDDR4:
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530231 k3_lpddr4_freq_update(ddrss);
Dave Gerlachd712b362021-05-11 10:22:11 -0500232 break;
233 default:
234 printf("Unrecognized dram_class cannot update frequency!\n");
235 }
236}
237
238static int k3_ddrss_init_freq(struct k3_ddrss_desc *ddrss)
239{
240 u32 dram_class;
241 int ret;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530242 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500243
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530244 dram_class = k3_lpddr4_read_ddr_type(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500245
246 switch (dram_class) {
247 case DENALI_CTL_0_DRAM_CLASS_DDR4:
248 /* Set to ddr_freq1 from DT for DDR4 */
249 ret = clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq1);
250 break;
251 case DENALI_CTL_0_DRAM_CLASS_LPDDR4:
252 /* Set to bypass frequency for LPDDR4*/
253 ret = clk_set_rate(&ddrss->ddr_clk, clk_get_rate(&ddrss->osc_clk));
254 break;
255 default:
256 ret = -EINVAL;
257 printf("Unrecognized dram_class cannot init frequency!\n");
258 }
259
260 if (ret < 0)
261 dev_err(ddrss->dev, "ddr clk init failed: %d\n", ret);
262 else
263 ret = 0;
264
265 return ret;
266}
267
268static void k3_lpddr4_info_handler(const lpddr4_privatedata *pd,
269 lpddr4_infotype infotype)
270{
271 if (infotype == LPDDR4_DRV_SOC_PLL_UPDATE)
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530272 k3_lpddr4_ack_freq_upd_req(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500273}
274
275static int k3_ddrss_power_on(struct k3_ddrss_desc *ddrss)
276{
277 int ret;
278
279 debug("%s(ddrss=%p)\n", __func__, ddrss);
280
281 ret = power_domain_on(&ddrss->ddrcfg_pwrdmn);
282 if (ret) {
283 dev_err(ddrss->dev, "power_domain_on() failed: %d\n", ret);
284 return ret;
285 }
286
287 ret = power_domain_on(&ddrss->ddrdata_pwrdmn);
288 if (ret) {
289 dev_err(ddrss->dev, "power_domain_on() failed: %d\n", ret);
290 return ret;
291 }
292
Lokesh Vutladd01c632021-05-11 10:22:13 -0500293 ret = device_get_supply_regulator(ddrss->dev, "vtt-supply",
294 &ddrss->vtt_supply);
295 if (ret) {
296 dev_dbg(ddrss->dev, "vtt-supply not found.\n");
297 } else {
298 ret = regulator_set_value(ddrss->vtt_supply, 3300000);
299 if (ret)
300 return ret;
301 dev_dbg(ddrss->dev, "VTT regulator enabled, volt = %d\n",
302 regulator_get_value(ddrss->vtt_supply));
303 }
304
Dave Gerlachd712b362021-05-11 10:22:11 -0500305 return 0;
306}
307
308static int k3_ddrss_ofdata_to_priv(struct udevice *dev)
309{
310 struct k3_ddrss_desc *ddrss = dev_get_priv(dev);
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530311 struct k3_ddrss_data *ddrss_data = (struct k3_ddrss_data *)dev_get_driver_data(dev);
Dave Gerlachd712b362021-05-11 10:22:11 -0500312 phys_addr_t reg;
313 int ret;
314
315 debug("%s(dev=%p)\n", __func__, dev);
316
317 reg = dev_read_addr_name(dev, "cfg");
318 if (reg == FDT_ADDR_T_NONE) {
319 dev_err(dev, "No reg property for DDRSS wrapper logic\n");
320 return -EINVAL;
321 }
322 ddrss->ddrss_ss_cfg = (void *)reg;
323
324 reg = dev_read_addr_name(dev, "ctrl_mmr_lp4");
325 if (reg == FDT_ADDR_T_NONE) {
326 dev_err(dev, "No reg property for CTRL MMR\n");
327 return -EINVAL;
328 }
329 ddrss->ddrss_ctrl_mmr = (void *)reg;
330
331 ret = power_domain_get_by_index(dev, &ddrss->ddrcfg_pwrdmn, 0);
332 if (ret) {
333 dev_err(dev, "power_domain_get() failed: %d\n", ret);
334 return ret;
335 }
336
337 ret = power_domain_get_by_index(dev, &ddrss->ddrdata_pwrdmn, 1);
338 if (ret) {
339 dev_err(dev, "power_domain_get() failed: %d\n", ret);
340 return ret;
341 }
342
343 ret = clk_get_by_index(dev, 0, &ddrss->ddr_clk);
344 if (ret)
345 dev_err(dev, "clk get failed%d\n", ret);
346
347 ret = clk_get_by_index(dev, 1, &ddrss->osc_clk);
348 if (ret)
349 dev_err(dev, "clk get failed for osc clk %d\n", ret);
350
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530351 /* Reading instance number for multi ddr subystems */
352 if (ddrss_data->flags & MULTI_DDR_SUBSYSTEM) {
353 ret = dev_read_u32(dev, "instance", &ddrss->instance);
354 if (ret) {
355 dev_err(dev, "missing instance property");
356 return -EINVAL;
357 }
358 } else {
359 ddrss->instance = 0;
360 }
361
Dave Gerlachd712b362021-05-11 10:22:11 -0500362 ret = dev_read_u32(dev, "ti,ddr-freq1", &ddrss->ddr_freq1);
363 if (ret)
364 dev_err(dev, "ddr freq1 not populated %d\n", ret);
365
366 ret = dev_read_u32(dev, "ti,ddr-freq2", &ddrss->ddr_freq2);
367 if (ret)
368 dev_err(dev, "ddr freq2 not populated %d\n", ret);
369
370 ret = dev_read_u32(dev, "ti,ddr-fhs-cnt", &ddrss->ddr_fhs_cnt);
371 if (ret)
372 dev_err(dev, "ddr fhs cnt not populated %d\n", ret);
373
374 return ret;
375}
376
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530377void k3_lpddr4_probe(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500378{
379 u32 status = 0U;
380 u16 configsize = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530381 lpddr4_config *config = &ddrss->config;
Dave Gerlachd712b362021-05-11 10:22:11 -0500382
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530383 status = ddrss->driverdt->probe(config, &configsize);
Dave Gerlachd712b362021-05-11 10:22:11 -0500384
385 if ((status != 0) || (configsize != sizeof(lpddr4_privatedata))
386 || (configsize > SRAM_MAX)) {
387 printf("%s: FAIL\n", __func__);
388 hang();
389 } else {
390 debug("%s: PASS\n", __func__);
391 }
392}
393
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530394void k3_lpddr4_init(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500395{
396 u32 status = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530397 lpddr4_config *config = &ddrss->config;
398 lpddr4_obj *driverdt = ddrss->driverdt;
399 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500400
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530401 if ((sizeof(*pd) != sizeof(lpddr4_privatedata)) || (sizeof(*pd) > SRAM_MAX)) {
Dave Gerlachd712b362021-05-11 10:22:11 -0500402 printf("%s: FAIL\n", __func__);
403 hang();
404 }
405
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530406 config->ctlbase = (struct lpddr4_ctlregs_s *)ddrss->ddrss_ss_cfg;
407 config->infohandler = (lpddr4_infocallback) k3_lpddr4_info_handler;
Dave Gerlachd712b362021-05-11 10:22:11 -0500408
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530409 status = driverdt->init(pd, config);
410
411 /* linking ddr instance to lpddr4 */
412 pd->ddr_instance = (void *)ddrss;
Dave Gerlachd712b362021-05-11 10:22:11 -0500413
414 if ((status > 0U) ||
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530415 (pd->ctlbase != (struct lpddr4_ctlregs_s *)config->ctlbase) ||
416 (pd->ctlinterrupthandler != config->ctlinterrupthandler) ||
417 (pd->phyindepinterrupthandler != config->phyindepinterrupthandler)) {
Dave Gerlachd712b362021-05-11 10:22:11 -0500418 printf("%s: FAIL\n", __func__);
419 hang();
420 } else {
421 debug("%s: PASS\n", __func__);
422 }
423}
424
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530425void populate_data_array_from_dt(struct k3_ddrss_desc *ddrss,
426 struct reginitdata *reginit_data)
Dave Gerlachd712b362021-05-11 10:22:11 -0500427{
428 int ret, i;
429
430 ret = dev_read_u32_array(ddrss->dev, "ti,ctl-data",
431 (u32 *)reginit_data->ctl_regs,
432 LPDDR4_INTR_CTL_REG_COUNT);
433 if (ret)
434 printf("Error reading ctrl data %d\n", ret);
435
436 for (i = 0; i < LPDDR4_INTR_CTL_REG_COUNT; i++)
437 reginit_data->ctl_regs_offs[i] = i;
438
439 ret = dev_read_u32_array(ddrss->dev, "ti,pi-data",
440 (u32 *)reginit_data->pi_regs,
441 LPDDR4_INTR_PHY_INDEP_REG_COUNT);
442 if (ret)
443 printf("Error reading PI data\n");
444
445 for (i = 0; i < LPDDR4_INTR_PHY_INDEP_REG_COUNT; i++)
446 reginit_data->pi_regs_offs[i] = i;
447
448 ret = dev_read_u32_array(ddrss->dev, "ti,phy-data",
449 (u32 *)reginit_data->phy_regs,
450 LPDDR4_INTR_PHY_REG_COUNT);
451 if (ret)
452 printf("Error reading PHY data %d\n", ret);
453
454 for (i = 0; i < LPDDR4_INTR_PHY_REG_COUNT; i++)
455 reginit_data->phy_regs_offs[i] = i;
456}
457
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530458void k3_lpddr4_hardware_reg_init(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500459{
460 u32 status = 0U;
461 struct reginitdata reginitdata;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530462 lpddr4_obj *driverdt = ddrss->driverdt;
463 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500464
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530465 populate_data_array_from_dt(ddrss, &reginitdata);
Dave Gerlachd712b362021-05-11 10:22:11 -0500466
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530467 status = driverdt->writectlconfig(pd, reginitdata.ctl_regs,
Dave Gerlachd712b362021-05-11 10:22:11 -0500468 reginitdata.ctl_regs_offs,
469 LPDDR4_INTR_CTL_REG_COUNT);
470 if (!status)
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530471 status = driverdt->writephyindepconfig(pd, reginitdata.pi_regs,
Dave Gerlachd712b362021-05-11 10:22:11 -0500472 reginitdata.pi_regs_offs,
473 LPDDR4_INTR_PHY_INDEP_REG_COUNT);
474 if (!status)
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530475 status = driverdt->writephyconfig(pd, reginitdata.phy_regs,
Dave Gerlachd712b362021-05-11 10:22:11 -0500476 reginitdata.phy_regs_offs,
477 LPDDR4_INTR_PHY_REG_COUNT);
478 if (status) {
479 printf("%s: FAIL\n", __func__);
480 hang();
481 }
482}
483
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530484void k3_lpddr4_start(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500485{
486 u32 status = 0U;
487 u32 regval = 0U;
488 u32 offset = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530489 lpddr4_obj *driverdt = ddrss->driverdt;
490 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500491
492 TH_OFFSET_FROM_REG(LPDDR4__START__REG, CTL_SHIFT, offset);
493
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530494 status = driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, &regval);
Dave Gerlachd712b362021-05-11 10:22:11 -0500495 if ((status > 0U) || ((regval & TH_FLD_MASK(LPDDR4__START__FLD)) != 0U)) {
496 printf("%s: Pre start FAIL\n", __func__);
497 hang();
498 }
499
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530500 status = driverdt->start(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500501 if (status > 0U) {
502 printf("%s: FAIL\n", __func__);
503 hang();
504 }
505
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530506 status = driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, &regval);
Dave Gerlachd712b362021-05-11 10:22:11 -0500507 if ((status > 0U) || ((regval & TH_FLD_MASK(LPDDR4__START__FLD)) != 1U)) {
508 printf("%s: Post start FAIL\n", __func__);
509 hang();
510 } else {
511 debug("%s: Post start PASS\n", __func__);
512 }
513}
514
515static int k3_ddrss_probe(struct udevice *dev)
516{
517 int ret;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530518 struct k3_ddrss_desc *ddrss = dev_get_priv(dev);
Dave Gerlachd712b362021-05-11 10:22:11 -0500519
520 debug("%s(dev=%p)\n", __func__, dev);
521
522 ret = k3_ddrss_ofdata_to_priv(dev);
523 if (ret)
524 return ret;
525
526 ddrss->dev = dev;
527 ret = k3_ddrss_power_on(ddrss);
528 if (ret)
529 return ret;
530
Dave Gerlach2c861a92021-05-11 10:22:12 -0500531#ifdef CONFIG_K3_AM64_DDRSS
532
533 writel(0x000001EF, ddrss->ddrss_ss_cfg + DDRSS_V2A_R1_MAT_REG);
534 writel(0x0, ddrss->ddrss_ss_cfg + DDRSS_ECC_CTRL_REG);
535#endif
536
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530537 ddrss->driverdt = lpddr4_getinstance();
538
539 k3_lpddr4_probe(ddrss);
540 k3_lpddr4_init(ddrss);
541 k3_lpddr4_hardware_reg_init(ddrss);
Dave Gerlachd712b362021-05-11 10:22:11 -0500542
543 ret = k3_ddrss_init_freq(ddrss);
544 if (ret)
545 return ret;
546
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530547 k3_lpddr4_start(ddrss);
Dave Gerlachd712b362021-05-11 10:22:11 -0500548
549 return ret;
550}
551
552static int k3_ddrss_get_info(struct udevice *dev, struct ram_info *info)
553{
554 return 0;
555}
556
557static struct ram_ops k3_ddrss_ops = {
558 .get_info = k3_ddrss_get_info,
559};
560
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530561static const struct k3_ddrss_data k3_data = {
562 .flags = SINGLE_DDR_SUBSYSTEM,
563};
564
565static const struct k3_ddrss_data j721s2_data = {
566 .flags = MULTI_DDR_SUBSYSTEM,
567};
568
Dave Gerlachd712b362021-05-11 10:22:11 -0500569static const struct udevice_id k3_ddrss_ids[] = {
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530570 {.compatible = "ti,am64-ddrss", .data = (ulong)&k3_data, },
571 {.compatible = "ti,j721e-ddrss", .data = (ulong)&k3_data, },
572 {.compatible = "ti,j721s2-ddrss", .data = (ulong)&j721s2_data, },
Dave Gerlachd712b362021-05-11 10:22:11 -0500573 {}
574};
575
576U_BOOT_DRIVER(k3_ddrss) = {
577 .name = "k3_ddrss",
578 .id = UCLASS_RAM,
579 .of_match = k3_ddrss_ids,
580 .ops = &k3_ddrss_ops,
581 .probe = k3_ddrss_probe,
582 .priv_auto = sizeof(struct k3_ddrss_desc),
583};
Aswath Govindraju6324bc72022-01-25 20:56:30 +0530584
585static int k3_msmc_set_config(struct k3_msmc *msmc)
586{
587 u32 ddr_cfg0 = 0;
588 u32 ddr_cfg1 = 0;
589
590 ddr_cfg0 |= msmc->gran << 24;
591 ddr_cfg0 |= msmc->size << 16;
592 /* heartbeat_per, bit[4:0] setting to 3 is advisable */
593 ddr_cfg0 |= 3;
594
595 /* Program MULTI_DDR_CFG0 */
596 writel(ddr_cfg0, MULTI_DDR_CFG0);
597
598 ddr_cfg1 |= msmc->enable << 16;
599 ddr_cfg1 |= msmc->config << 8;
600 ddr_cfg1 |= msmc->active;
601
602 /* Program MULTI_DDR_CFG1 */
603 writel(ddr_cfg1, MULTI_DDR_CFG1);
604
605 /* Program DDR_CFG_LOAD */
606 writel(0x60000000, DDR_CFG_LOAD);
607
608 return 0;
609}
610
611static int k3_msmc_probe(struct udevice *dev)
612{
613 struct k3_msmc *msmc = dev_get_priv(dev);
614 int ret = 0;
615
616 /* Read the granular size from DT */
617 ret = dev_read_u32(dev, "intrlv-gran", &msmc->gran);
618 if (ret) {
619 dev_err(dev, "missing intrlv-gran property");
620 return -EINVAL;
621 }
622
623 /* Read the interleave region from DT */
624 ret = dev_read_u32(dev, "intrlv-size", &msmc->size);
625 if (ret) {
626 dev_err(dev, "missing intrlv-size property");
627 return -EINVAL;
628 }
629
630 /* Read ECC enable config */
631 ret = dev_read_u32(dev, "ecc-enable", &msmc->enable);
632 if (ret) {
633 dev_err(dev, "missing ecc-enable property");
634 return -EINVAL;
635 }
636
637 /* Read EMIF configuration */
638 ret = dev_read_u32(dev, "emif-config", &msmc->config);
639 if (ret) {
640 dev_err(dev, "missing emif-config property");
641 return -EINVAL;
642 }
643
644 /* Read EMIF active */
645 ret = dev_read_u32(dev, "emif-active", &msmc->active);
646 if (ret) {
647 dev_err(dev, "missing emif-active property");
648 return -EINVAL;
649 }
650
651 ret = k3_msmc_set_config(msmc);
652 if (ret) {
653 dev_err(dev, "error setting msmc config");
654 return -EINVAL;
655 }
656
657 return 0;
658}
659
660static const struct udevice_id k3_msmc_ids[] = {
661 { .compatible = "ti,j721s2-msmc"},
662 {}
663};
664
665U_BOOT_DRIVER(k3_msmc) = {
666 .name = "k3_msmc",
667 .of_match = k3_msmc_ids,
668 .id = UCLASS_MISC,
669 .probe = k3_msmc_probe,
670 .priv_auto = sizeof(struct k3_msmc),
671 .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF,
672};