blob: ffed426b9cca06fdecb53acfdec4fab76ccc5a43 [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;
Dave Gerlachfd199dd2022-03-17 12:03:42 -0500109 void __iomem *ddrss_ctl_cfg;
Dave Gerlachd712b362021-05-11 10:22:11 -0500110 struct power_domain ddrcfg_pwrdmn;
111 struct power_domain ddrdata_pwrdmn;
112 struct clk ddr_clk;
113 struct clk osc_clk;
114 u32 ddr_freq1;
115 u32 ddr_freq2;
116 u32 ddr_fhs_cnt;
Lokesh Vutladd01c632021-05-11 10:22:13 -0500117 struct udevice *vtt_supply;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530118 u32 instance;
119 lpddr4_obj *driverdt;
120 lpddr4_config config;
121 lpddr4_privatedata pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500122};
123
Dave Gerlachd712b362021-05-11 10:22:11 -0500124struct reginitdata {
125 u32 ctl_regs[LPDDR4_INTR_CTL_REG_COUNT];
126 u16 ctl_regs_offs[LPDDR4_INTR_CTL_REG_COUNT];
127 u32 pi_regs[LPDDR4_INTR_PHY_INDEP_REG_COUNT];
128 u16 pi_regs_offs[LPDDR4_INTR_PHY_INDEP_REG_COUNT];
129 u32 phy_regs[LPDDR4_INTR_PHY_REG_COUNT];
130 u16 phy_regs_offs[LPDDR4_INTR_PHY_REG_COUNT];
131};
132
133#define TH_MACRO_EXP(fld, str) (fld##str)
134
135#define TH_FLD_MASK(fld) TH_MACRO_EXP(fld, _MASK)
136#define TH_FLD_SHIFT(fld) TH_MACRO_EXP(fld, _SHIFT)
137#define TH_FLD_WIDTH(fld) TH_MACRO_EXP(fld, _WIDTH)
138#define TH_FLD_WOCLR(fld) TH_MACRO_EXP(fld, _WOCLR)
139#define TH_FLD_WOSET(fld) TH_MACRO_EXP(fld, _WOSET)
140
141#define str(s) #s
142#define xstr(s) str(s)
143
144#define CTL_SHIFT 11
145#define PHY_SHIFT 11
146#define PI_SHIFT 10
147
148#define DENALI_CTL_0_DRAM_CLASS_DDR4 0xA
149#define DENALI_CTL_0_DRAM_CLASS_LPDDR4 0xB
150
151#define TH_OFFSET_FROM_REG(REG, SHIFT, offset) do {\
152 char *i, *pstr = xstr(REG); offset = 0;\
153 for (i = &pstr[SHIFT]; *i != '\0'; ++i) {\
154 offset = offset * 10 + (*i - '0'); } \
155 } while (0)
156
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530157static u32 k3_lpddr4_read_ddr_type(const lpddr4_privatedata *pd)
Dave Gerlachd712b362021-05-11 10:22:11 -0500158{
159 u32 status = 0U;
160 u32 offset = 0U;
161 u32 regval = 0U;
162 u32 dram_class = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530163 struct k3_ddrss_desc *ddrss = (struct k3_ddrss_desc *)pd->ddr_instance;
Dave Gerlachd712b362021-05-11 10:22:11 -0500164
165 TH_OFFSET_FROM_REG(LPDDR4__DRAM_CLASS__REG, CTL_SHIFT, offset);
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530166 status = ddrss->driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, &regval);
Dave Gerlachd712b362021-05-11 10:22:11 -0500167 if (status > 0U) {
168 printf("%s: Failed to read DRAM_CLASS\n", __func__);
169 hang();
170 }
171
172 dram_class = ((regval & TH_FLD_MASK(LPDDR4__DRAM_CLASS__FLD)) >>
173 TH_FLD_SHIFT(LPDDR4__DRAM_CLASS__FLD));
174 return dram_class;
175}
176
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530177static void k3_lpddr4_freq_update(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500178{
179 unsigned int req_type, counter;
180
181 for (counter = 0; counter < ddrss->ddr_fhs_cnt; counter++) {
182 if (wait_for_bit_le32(ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530183 CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS + ddrss->instance * 0x10, 0x80,
Dave Gerlachd712b362021-05-11 10:22:11 -0500184 true, 10000, false)) {
185 printf("Timeout during frequency handshake\n");
186 hang();
187 }
188
189 req_type = readl(ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530190 CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS + ddrss->instance * 0x10) & 0x03;
Dave Gerlachd712b362021-05-11 10:22:11 -0500191
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530192 debug("%s: received freq change req: req type = %d, req no. = %d, instance = %d\n",
193 __func__, req_type, counter, ddrss->instance);
Dave Gerlachd712b362021-05-11 10:22:11 -0500194
195 if (req_type == 1)
196 clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq1);
197 else if (req_type == 2)
198 clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq2);
199 else if (req_type == 0)
200 /* Put DDR pll in bypass mode */
201 clk_set_rate(&ddrss->ddr_clk,
202 clk_get_rate(&ddrss->osc_clk));
203 else
204 printf("%s: Invalid freq request type\n", __func__);
205
206 writel(0x1, ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530207 CTRLMMR_DDR4_FSP_CLKCHNG_ACK_OFFS + ddrss->instance * 0x10);
Dave Gerlachd712b362021-05-11 10:22:11 -0500208 if (wait_for_bit_le32(ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530209 CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS + ddrss->instance * 0x10, 0x80,
Dave Gerlachd712b362021-05-11 10:22:11 -0500210 false, 10, false)) {
211 printf("Timeout during frequency handshake\n");
212 hang();
213 }
214 writel(0x0, ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530215 CTRLMMR_DDR4_FSP_CLKCHNG_ACK_OFFS + ddrss->instance * 0x10);
Dave Gerlachd712b362021-05-11 10:22:11 -0500216 }
217}
218
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530219static void k3_lpddr4_ack_freq_upd_req(const lpddr4_privatedata *pd)
Dave Gerlachd712b362021-05-11 10:22:11 -0500220{
221 u32 dram_class;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530222 struct k3_ddrss_desc *ddrss = (struct k3_ddrss_desc *)pd->ddr_instance;
Dave Gerlachd712b362021-05-11 10:22:11 -0500223
224 debug("--->>> LPDDR4 Initialization is in progress ... <<<---\n");
225
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530226 dram_class = k3_lpddr4_read_ddr_type(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500227
228 switch (dram_class) {
229 case DENALI_CTL_0_DRAM_CLASS_DDR4:
230 break;
231 case DENALI_CTL_0_DRAM_CLASS_LPDDR4:
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530232 k3_lpddr4_freq_update(ddrss);
Dave Gerlachd712b362021-05-11 10:22:11 -0500233 break;
234 default:
235 printf("Unrecognized dram_class cannot update frequency!\n");
236 }
237}
238
239static int k3_ddrss_init_freq(struct k3_ddrss_desc *ddrss)
240{
241 u32 dram_class;
242 int ret;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530243 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500244
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530245 dram_class = k3_lpddr4_read_ddr_type(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500246
247 switch (dram_class) {
248 case DENALI_CTL_0_DRAM_CLASS_DDR4:
249 /* Set to ddr_freq1 from DT for DDR4 */
250 ret = clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq1);
251 break;
252 case DENALI_CTL_0_DRAM_CLASS_LPDDR4:
253 /* Set to bypass frequency for LPDDR4*/
254 ret = clk_set_rate(&ddrss->ddr_clk, clk_get_rate(&ddrss->osc_clk));
255 break;
256 default:
257 ret = -EINVAL;
258 printf("Unrecognized dram_class cannot init frequency!\n");
259 }
260
261 if (ret < 0)
262 dev_err(ddrss->dev, "ddr clk init failed: %d\n", ret);
263 else
264 ret = 0;
265
266 return ret;
267}
268
269static void k3_lpddr4_info_handler(const lpddr4_privatedata *pd,
270 lpddr4_infotype infotype)
271{
272 if (infotype == LPDDR4_DRV_SOC_PLL_UPDATE)
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530273 k3_lpddr4_ack_freq_upd_req(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500274}
275
276static int k3_ddrss_power_on(struct k3_ddrss_desc *ddrss)
277{
278 int ret;
279
280 debug("%s(ddrss=%p)\n", __func__, ddrss);
281
282 ret = power_domain_on(&ddrss->ddrcfg_pwrdmn);
283 if (ret) {
284 dev_err(ddrss->dev, "power_domain_on() failed: %d\n", ret);
285 return ret;
286 }
287
288 ret = power_domain_on(&ddrss->ddrdata_pwrdmn);
289 if (ret) {
290 dev_err(ddrss->dev, "power_domain_on() failed: %d\n", ret);
291 return ret;
292 }
293
Lokesh Vutladd01c632021-05-11 10:22:13 -0500294 ret = device_get_supply_regulator(ddrss->dev, "vtt-supply",
295 &ddrss->vtt_supply);
296 if (ret) {
297 dev_dbg(ddrss->dev, "vtt-supply not found.\n");
298 } else {
299 ret = regulator_set_value(ddrss->vtt_supply, 3300000);
300 if (ret)
301 return ret;
302 dev_dbg(ddrss->dev, "VTT regulator enabled, volt = %d\n",
303 regulator_get_value(ddrss->vtt_supply));
304 }
305
Dave Gerlachd712b362021-05-11 10:22:11 -0500306 return 0;
307}
308
309static int k3_ddrss_ofdata_to_priv(struct udevice *dev)
310{
311 struct k3_ddrss_desc *ddrss = dev_get_priv(dev);
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530312 struct k3_ddrss_data *ddrss_data = (struct k3_ddrss_data *)dev_get_driver_data(dev);
Dave Gerlachd712b362021-05-11 10:22:11 -0500313 phys_addr_t reg;
314 int ret;
315
316 debug("%s(dev=%p)\n", __func__, dev);
317
318 reg = dev_read_addr_name(dev, "cfg");
319 if (reg == FDT_ADDR_T_NONE) {
320 dev_err(dev, "No reg property for DDRSS wrapper logic\n");
321 return -EINVAL;
322 }
Dave Gerlachfd199dd2022-03-17 12:03:42 -0500323 ddrss->ddrss_ctl_cfg = (void *)reg;
Dave Gerlachd712b362021-05-11 10:22:11 -0500324
325 reg = dev_read_addr_name(dev, "ctrl_mmr_lp4");
326 if (reg == FDT_ADDR_T_NONE) {
327 dev_err(dev, "No reg property for CTRL MMR\n");
328 return -EINVAL;
329 }
330 ddrss->ddrss_ctrl_mmr = (void *)reg;
331
332 ret = power_domain_get_by_index(dev, &ddrss->ddrcfg_pwrdmn, 0);
333 if (ret) {
334 dev_err(dev, "power_domain_get() failed: %d\n", ret);
335 return ret;
336 }
337
338 ret = power_domain_get_by_index(dev, &ddrss->ddrdata_pwrdmn, 1);
339 if (ret) {
340 dev_err(dev, "power_domain_get() failed: %d\n", ret);
341 return ret;
342 }
343
344 ret = clk_get_by_index(dev, 0, &ddrss->ddr_clk);
345 if (ret)
346 dev_err(dev, "clk get failed%d\n", ret);
347
348 ret = clk_get_by_index(dev, 1, &ddrss->osc_clk);
349 if (ret)
350 dev_err(dev, "clk get failed for osc clk %d\n", ret);
351
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530352 /* Reading instance number for multi ddr subystems */
353 if (ddrss_data->flags & MULTI_DDR_SUBSYSTEM) {
354 ret = dev_read_u32(dev, "instance", &ddrss->instance);
355 if (ret) {
356 dev_err(dev, "missing instance property");
357 return -EINVAL;
358 }
359 } else {
360 ddrss->instance = 0;
361 }
362
Dave Gerlachd712b362021-05-11 10:22:11 -0500363 ret = dev_read_u32(dev, "ti,ddr-freq1", &ddrss->ddr_freq1);
364 if (ret)
365 dev_err(dev, "ddr freq1 not populated %d\n", ret);
366
367 ret = dev_read_u32(dev, "ti,ddr-freq2", &ddrss->ddr_freq2);
368 if (ret)
369 dev_err(dev, "ddr freq2 not populated %d\n", ret);
370
371 ret = dev_read_u32(dev, "ti,ddr-fhs-cnt", &ddrss->ddr_fhs_cnt);
372 if (ret)
373 dev_err(dev, "ddr fhs cnt not populated %d\n", ret);
374
375 return ret;
376}
377
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530378void k3_lpddr4_probe(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500379{
380 u32 status = 0U;
381 u16 configsize = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530382 lpddr4_config *config = &ddrss->config;
Dave Gerlachd712b362021-05-11 10:22:11 -0500383
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530384 status = ddrss->driverdt->probe(config, &configsize);
Dave Gerlachd712b362021-05-11 10:22:11 -0500385
386 if ((status != 0) || (configsize != sizeof(lpddr4_privatedata))
387 || (configsize > SRAM_MAX)) {
388 printf("%s: FAIL\n", __func__);
389 hang();
390 } else {
391 debug("%s: PASS\n", __func__);
392 }
393}
394
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530395void k3_lpddr4_init(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500396{
397 u32 status = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530398 lpddr4_config *config = &ddrss->config;
399 lpddr4_obj *driverdt = ddrss->driverdt;
400 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500401
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530402 if ((sizeof(*pd) != sizeof(lpddr4_privatedata)) || (sizeof(*pd) > SRAM_MAX)) {
Dave Gerlachd712b362021-05-11 10:22:11 -0500403 printf("%s: FAIL\n", __func__);
404 hang();
405 }
406
Dave Gerlachfd199dd2022-03-17 12:03:42 -0500407 config->ctlbase = (struct lpddr4_ctlregs_s *)ddrss->ddrss_ctl_cfg;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530408 config->infohandler = (lpddr4_infocallback) k3_lpddr4_info_handler;
Dave Gerlachd712b362021-05-11 10:22:11 -0500409
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530410 status = driverdt->init(pd, config);
411
412 /* linking ddr instance to lpddr4 */
413 pd->ddr_instance = (void *)ddrss;
Dave Gerlachd712b362021-05-11 10:22:11 -0500414
415 if ((status > 0U) ||
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530416 (pd->ctlbase != (struct lpddr4_ctlregs_s *)config->ctlbase) ||
417 (pd->ctlinterrupthandler != config->ctlinterrupthandler) ||
418 (pd->phyindepinterrupthandler != config->phyindepinterrupthandler)) {
Dave Gerlachd712b362021-05-11 10:22:11 -0500419 printf("%s: FAIL\n", __func__);
420 hang();
421 } else {
422 debug("%s: PASS\n", __func__);
423 }
424}
425
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530426void populate_data_array_from_dt(struct k3_ddrss_desc *ddrss,
427 struct reginitdata *reginit_data)
Dave Gerlachd712b362021-05-11 10:22:11 -0500428{
429 int ret, i;
430
431 ret = dev_read_u32_array(ddrss->dev, "ti,ctl-data",
432 (u32 *)reginit_data->ctl_regs,
433 LPDDR4_INTR_CTL_REG_COUNT);
434 if (ret)
435 printf("Error reading ctrl data %d\n", ret);
436
437 for (i = 0; i < LPDDR4_INTR_CTL_REG_COUNT; i++)
438 reginit_data->ctl_regs_offs[i] = i;
439
440 ret = dev_read_u32_array(ddrss->dev, "ti,pi-data",
441 (u32 *)reginit_data->pi_regs,
442 LPDDR4_INTR_PHY_INDEP_REG_COUNT);
443 if (ret)
444 printf("Error reading PI data\n");
445
446 for (i = 0; i < LPDDR4_INTR_PHY_INDEP_REG_COUNT; i++)
447 reginit_data->pi_regs_offs[i] = i;
448
449 ret = dev_read_u32_array(ddrss->dev, "ti,phy-data",
450 (u32 *)reginit_data->phy_regs,
451 LPDDR4_INTR_PHY_REG_COUNT);
452 if (ret)
453 printf("Error reading PHY data %d\n", ret);
454
455 for (i = 0; i < LPDDR4_INTR_PHY_REG_COUNT; i++)
456 reginit_data->phy_regs_offs[i] = i;
457}
458
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530459void k3_lpddr4_hardware_reg_init(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500460{
461 u32 status = 0U;
462 struct reginitdata reginitdata;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530463 lpddr4_obj *driverdt = ddrss->driverdt;
464 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500465
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530466 populate_data_array_from_dt(ddrss, &reginitdata);
Dave Gerlachd712b362021-05-11 10:22:11 -0500467
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530468 status = driverdt->writectlconfig(pd, reginitdata.ctl_regs,
Dave Gerlachd712b362021-05-11 10:22:11 -0500469 reginitdata.ctl_regs_offs,
470 LPDDR4_INTR_CTL_REG_COUNT);
471 if (!status)
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530472 status = driverdt->writephyindepconfig(pd, reginitdata.pi_regs,
Dave Gerlachd712b362021-05-11 10:22:11 -0500473 reginitdata.pi_regs_offs,
474 LPDDR4_INTR_PHY_INDEP_REG_COUNT);
475 if (!status)
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530476 status = driverdt->writephyconfig(pd, reginitdata.phy_regs,
Dave Gerlachd712b362021-05-11 10:22:11 -0500477 reginitdata.phy_regs_offs,
478 LPDDR4_INTR_PHY_REG_COUNT);
479 if (status) {
480 printf("%s: FAIL\n", __func__);
481 hang();
482 }
483}
484
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530485void k3_lpddr4_start(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500486{
487 u32 status = 0U;
488 u32 regval = 0U;
489 u32 offset = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530490 lpddr4_obj *driverdt = ddrss->driverdt;
491 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500492
493 TH_OFFSET_FROM_REG(LPDDR4__START__REG, CTL_SHIFT, offset);
494
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530495 status = driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, &regval);
Dave Gerlachd712b362021-05-11 10:22:11 -0500496 if ((status > 0U) || ((regval & TH_FLD_MASK(LPDDR4__START__FLD)) != 0U)) {
497 printf("%s: Pre start FAIL\n", __func__);
498 hang();
499 }
500
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530501 status = driverdt->start(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500502 if (status > 0U) {
503 printf("%s: FAIL\n", __func__);
504 hang();
505 }
506
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530507 status = driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, &regval);
Dave Gerlachd712b362021-05-11 10:22:11 -0500508 if ((status > 0U) || ((regval & TH_FLD_MASK(LPDDR4__START__FLD)) != 1U)) {
509 printf("%s: Post start FAIL\n", __func__);
510 hang();
511 } else {
512 debug("%s: Post start PASS\n", __func__);
513 }
514}
515
516static int k3_ddrss_probe(struct udevice *dev)
517{
518 int ret;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530519 struct k3_ddrss_desc *ddrss = dev_get_priv(dev);
Dave Gerlachd712b362021-05-11 10:22:11 -0500520
521 debug("%s(dev=%p)\n", __func__, dev);
522
523 ret = k3_ddrss_ofdata_to_priv(dev);
524 if (ret)
525 return ret;
526
527 ddrss->dev = dev;
528 ret = k3_ddrss_power_on(ddrss);
529 if (ret)
530 return ret;
531
Dave Gerlach2c861a92021-05-11 10:22:12 -0500532#ifdef CONFIG_K3_AM64_DDRSS
533
534 writel(0x000001EF, ddrss->ddrss_ss_cfg + DDRSS_V2A_R1_MAT_REG);
535 writel(0x0, ddrss->ddrss_ss_cfg + DDRSS_ECC_CTRL_REG);
536#endif
537
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530538 ddrss->driverdt = lpddr4_getinstance();
539
540 k3_lpddr4_probe(ddrss);
541 k3_lpddr4_init(ddrss);
542 k3_lpddr4_hardware_reg_init(ddrss);
Dave Gerlachd712b362021-05-11 10:22:11 -0500543
544 ret = k3_ddrss_init_freq(ddrss);
545 if (ret)
546 return ret;
547
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530548 k3_lpddr4_start(ddrss);
Dave Gerlachd712b362021-05-11 10:22:11 -0500549
550 return ret;
551}
552
553static int k3_ddrss_get_info(struct udevice *dev, struct ram_info *info)
554{
555 return 0;
556}
557
558static struct ram_ops k3_ddrss_ops = {
559 .get_info = k3_ddrss_get_info,
560};
561
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530562static const struct k3_ddrss_data k3_data = {
563 .flags = SINGLE_DDR_SUBSYSTEM,
564};
565
566static const struct k3_ddrss_data j721s2_data = {
567 .flags = MULTI_DDR_SUBSYSTEM,
568};
569
Dave Gerlachd712b362021-05-11 10:22:11 -0500570static const struct udevice_id k3_ddrss_ids[] = {
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530571 {.compatible = "ti,am64-ddrss", .data = (ulong)&k3_data, },
572 {.compatible = "ti,j721e-ddrss", .data = (ulong)&k3_data, },
573 {.compatible = "ti,j721s2-ddrss", .data = (ulong)&j721s2_data, },
Dave Gerlachd712b362021-05-11 10:22:11 -0500574 {}
575};
576
577U_BOOT_DRIVER(k3_ddrss) = {
578 .name = "k3_ddrss",
579 .id = UCLASS_RAM,
580 .of_match = k3_ddrss_ids,
581 .ops = &k3_ddrss_ops,
582 .probe = k3_ddrss_probe,
583 .priv_auto = sizeof(struct k3_ddrss_desc),
584};
Aswath Govindraju6324bc72022-01-25 20:56:30 +0530585
586static int k3_msmc_set_config(struct k3_msmc *msmc)
587{
588 u32 ddr_cfg0 = 0;
589 u32 ddr_cfg1 = 0;
590
591 ddr_cfg0 |= msmc->gran << 24;
592 ddr_cfg0 |= msmc->size << 16;
593 /* heartbeat_per, bit[4:0] setting to 3 is advisable */
594 ddr_cfg0 |= 3;
595
596 /* Program MULTI_DDR_CFG0 */
597 writel(ddr_cfg0, MULTI_DDR_CFG0);
598
599 ddr_cfg1 |= msmc->enable << 16;
600 ddr_cfg1 |= msmc->config << 8;
601 ddr_cfg1 |= msmc->active;
602
603 /* Program MULTI_DDR_CFG1 */
604 writel(ddr_cfg1, MULTI_DDR_CFG1);
605
606 /* Program DDR_CFG_LOAD */
607 writel(0x60000000, DDR_CFG_LOAD);
608
609 return 0;
610}
611
612static int k3_msmc_probe(struct udevice *dev)
613{
614 struct k3_msmc *msmc = dev_get_priv(dev);
615 int ret = 0;
616
617 /* Read the granular size from DT */
618 ret = dev_read_u32(dev, "intrlv-gran", &msmc->gran);
619 if (ret) {
620 dev_err(dev, "missing intrlv-gran property");
621 return -EINVAL;
622 }
623
624 /* Read the interleave region from DT */
625 ret = dev_read_u32(dev, "intrlv-size", &msmc->size);
626 if (ret) {
627 dev_err(dev, "missing intrlv-size property");
628 return -EINVAL;
629 }
630
631 /* Read ECC enable config */
632 ret = dev_read_u32(dev, "ecc-enable", &msmc->enable);
633 if (ret) {
634 dev_err(dev, "missing ecc-enable property");
635 return -EINVAL;
636 }
637
638 /* Read EMIF configuration */
639 ret = dev_read_u32(dev, "emif-config", &msmc->config);
640 if (ret) {
641 dev_err(dev, "missing emif-config property");
642 return -EINVAL;
643 }
644
645 /* Read EMIF active */
646 ret = dev_read_u32(dev, "emif-active", &msmc->active);
647 if (ret) {
648 dev_err(dev, "missing emif-active property");
649 return -EINVAL;
650 }
651
652 ret = k3_msmc_set_config(msmc);
653 if (ret) {
654 dev_err(dev, "error setting msmc config");
655 return -EINVAL;
656 }
657
658 return 0;
659}
660
661static const struct udevice_id k3_msmc_ids[] = {
662 { .compatible = "ti,j721s2-msmc"},
663 {}
664};
665
666U_BOOT_DRIVER(k3_msmc) = {
667 .name = "k3_msmc",
668 .of_match = k3_msmc_ids,
669 .id = UCLASS_MISC,
670 .probe = k3_msmc_probe,
671 .priv_auto = sizeof(struct k3_msmc),
672 .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF,
673};