blob: 2467f122a8292e5f13a20e1b2b807a1c3667de1e [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>
Dave Gerlach296c83a2022-03-17 12:03:43 -05009#include <config.h>
Dave Gerlachd712b362021-05-11 10:22:11 -050010#include <clk.h>
Dave Gerlach296c83a2022-03-17 12:03:43 -050011#include <div64.h>
Dave Gerlachd712b362021-05-11 10:22:11 -050012#include <dm.h>
13#include <dm/device_compat.h>
Dave Gerlach296c83a2022-03-17 12:03:43 -050014#include <fdt_support.h>
Dave Gerlachd712b362021-05-11 10:22:11 -050015#include <ram.h>
16#include <hang.h>
17#include <log.h>
18#include <asm/io.h>
19#include <power-domain.h>
20#include <wait_bit.h>
Lokesh Vutladd01c632021-05-11 10:22:13 -050021#include <power/regulator.h>
Dave Gerlachd712b362021-05-11 10:22:11 -050022
23#include "lpddr4_obj_if.h"
24#include "lpddr4_if.h"
25#include "lpddr4_structs_if.h"
26#include "lpddr4_ctl_regs.h"
27
28#define SRAM_MAX 512
29
30#define CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS 0x80
31#define CTRLMMR_DDR4_FSP_CLKCHNG_ACK_OFFS 0xc0
32
Wolfgang Denk62fb2b42021-09-27 17:42:39 +020033#define DDRSS_V2A_R1_MAT_REG 0x0020
34#define DDRSS_ECC_CTRL_REG 0x0120
Dave Gerlach2c861a92021-05-11 10:22:12 -050035
Dave Gerlach296c83a2022-03-17 12:03:43 -050036#define DDRSS_ECC_CTRL_REG_ECC_EN BIT(0)
37#define DDRSS_ECC_CTRL_REG_RMW_EN BIT(1)
38#define DDRSS_ECC_CTRL_REG_ECC_CK BIT(2)
39#define DDRSS_ECC_CTRL_REG_WR_ALLOC BIT(4)
40
41#define DDRSS_ECC_R0_STR_ADDR_REG 0x0130
42#define DDRSS_ECC_R0_END_ADDR_REG 0x0134
43#define DDRSS_ECC_R1_STR_ADDR_REG 0x0138
44#define DDRSS_ECC_R1_END_ADDR_REG 0x013c
45#define DDRSS_ECC_R2_STR_ADDR_REG 0x0140
46#define DDRSS_ECC_R2_END_ADDR_REG 0x0144
47#define DDRSS_ECC_1B_ERR_CNT_REG 0x0150
48
Aswath Govindrajub232cb42022-01-25 20:56:29 +053049#define SINGLE_DDR_SUBSYSTEM 0x1
50#define MULTI_DDR_SUBSYSTEM 0x2
51
Aswath Govindraju6324bc72022-01-25 20:56:30 +053052#define MULTI_DDR_CFG0 0x00114100
53#define MULTI_DDR_CFG1 0x00114104
54#define DDR_CFG_LOAD 0x00114110
55
56enum intrlv_gran {
57 GRAN_128B,
58 GRAN_512B,
59 GRAN_2KB,
60 GRAN_4KB,
61 GRAN_16KB,
62 GRAN_32KB,
63 GRAN_512KB,
64 GRAN_1GB,
65 GRAN_1_5GB,
66 GRAN_2GB,
67 GRAN_3GB,
68 GRAN_4GB,
69 GRAN_6GB,
70 GRAN_8GB,
71 GRAN_16GB
72};
73
74enum intrlv_size {
75 SIZE_0,
76 SIZE_128MB,
77 SIZE_256MB,
78 SIZE_512MB,
79 SIZE_1GB,
80 SIZE_2GB,
81 SIZE_3GB,
82 SIZE_4GB,
83 SIZE_6GB,
84 SIZE_8GB,
85 SIZE_12GB,
86 SIZE_16GB,
87 SIZE_32GB
88};
89
90struct k3_ddrss_data {
91 u32 flags;
92};
93
94enum ecc_enable {
95 DISABLE_ALL = 0,
96 ENABLE_0,
97 ENABLE_1,
98 ENABLE_ALL
99};
100
101enum emif_config {
102 INTERLEAVE_ALL = 0,
103 SEPR0,
104 SEPR1
105};
106
107enum emif_active {
108 EMIF_0 = 1,
109 EMIF_1,
110 EMIF_ALL
111};
112
113struct k3_msmc {
114 enum intrlv_gran gran;
115 enum intrlv_size size;
116 enum ecc_enable enable;
117 enum emif_config config;
118 enum emif_active active;
119};
120
Dave Gerlach296c83a2022-03-17 12:03:43 -0500121#define K3_DDRSS_MAX_ECC_REGIONS 3
122
123struct k3_ddrss_ecc_region {
124 u32 start;
125 u32 range;
126};
127
Dave Gerlachd712b362021-05-11 10:22:11 -0500128struct k3_ddrss_desc {
129 struct udevice *dev;
130 void __iomem *ddrss_ss_cfg;
131 void __iomem *ddrss_ctrl_mmr;
Dave Gerlachfd199dd2022-03-17 12:03:42 -0500132 void __iomem *ddrss_ctl_cfg;
Dave Gerlachd712b362021-05-11 10:22:11 -0500133 struct power_domain ddrcfg_pwrdmn;
134 struct power_domain ddrdata_pwrdmn;
135 struct clk ddr_clk;
136 struct clk osc_clk;
137 u32 ddr_freq1;
138 u32 ddr_freq2;
139 u32 ddr_fhs_cnt;
Lokesh Vutladd01c632021-05-11 10:22:13 -0500140 struct udevice *vtt_supply;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530141 u32 instance;
142 lpddr4_obj *driverdt;
143 lpddr4_config config;
144 lpddr4_privatedata pd;
Dave Gerlach296c83a2022-03-17 12:03:43 -0500145 struct k3_ddrss_ecc_region ecc_regions[K3_DDRSS_MAX_ECC_REGIONS];
146 u64 ecc_reserved_space;
147 bool ti_ecc_enabled;
Dave Gerlachd712b362021-05-11 10:22:11 -0500148};
149
Dave Gerlachd712b362021-05-11 10:22:11 -0500150struct reginitdata {
151 u32 ctl_regs[LPDDR4_INTR_CTL_REG_COUNT];
152 u16 ctl_regs_offs[LPDDR4_INTR_CTL_REG_COUNT];
153 u32 pi_regs[LPDDR4_INTR_PHY_INDEP_REG_COUNT];
154 u16 pi_regs_offs[LPDDR4_INTR_PHY_INDEP_REG_COUNT];
155 u32 phy_regs[LPDDR4_INTR_PHY_REG_COUNT];
156 u16 phy_regs_offs[LPDDR4_INTR_PHY_REG_COUNT];
157};
158
159#define TH_MACRO_EXP(fld, str) (fld##str)
160
161#define TH_FLD_MASK(fld) TH_MACRO_EXP(fld, _MASK)
162#define TH_FLD_SHIFT(fld) TH_MACRO_EXP(fld, _SHIFT)
163#define TH_FLD_WIDTH(fld) TH_MACRO_EXP(fld, _WIDTH)
164#define TH_FLD_WOCLR(fld) TH_MACRO_EXP(fld, _WOCLR)
165#define TH_FLD_WOSET(fld) TH_MACRO_EXP(fld, _WOSET)
166
167#define str(s) #s
168#define xstr(s) str(s)
169
170#define CTL_SHIFT 11
171#define PHY_SHIFT 11
172#define PI_SHIFT 10
173
174#define DENALI_CTL_0_DRAM_CLASS_DDR4 0xA
175#define DENALI_CTL_0_DRAM_CLASS_LPDDR4 0xB
176
177#define TH_OFFSET_FROM_REG(REG, SHIFT, offset) do {\
178 char *i, *pstr = xstr(REG); offset = 0;\
179 for (i = &pstr[SHIFT]; *i != '\0'; ++i) {\
180 offset = offset * 10 + (*i - '0'); } \
181 } while (0)
182
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530183static u32 k3_lpddr4_read_ddr_type(const lpddr4_privatedata *pd)
Dave Gerlachd712b362021-05-11 10:22:11 -0500184{
185 u32 status = 0U;
186 u32 offset = 0U;
187 u32 regval = 0U;
188 u32 dram_class = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530189 struct k3_ddrss_desc *ddrss = (struct k3_ddrss_desc *)pd->ddr_instance;
Dave Gerlachd712b362021-05-11 10:22:11 -0500190
191 TH_OFFSET_FROM_REG(LPDDR4__DRAM_CLASS__REG, CTL_SHIFT, offset);
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530192 status = ddrss->driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, &regval);
Dave Gerlachd712b362021-05-11 10:22:11 -0500193 if (status > 0U) {
194 printf("%s: Failed to read DRAM_CLASS\n", __func__);
195 hang();
196 }
197
198 dram_class = ((regval & TH_FLD_MASK(LPDDR4__DRAM_CLASS__FLD)) >>
199 TH_FLD_SHIFT(LPDDR4__DRAM_CLASS__FLD));
200 return dram_class;
201}
202
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530203static void k3_lpddr4_freq_update(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500204{
205 unsigned int req_type, counter;
206
207 for (counter = 0; counter < ddrss->ddr_fhs_cnt; counter++) {
208 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 true, 10000, false)) {
211 printf("Timeout during frequency handshake\n");
212 hang();
213 }
214
215 req_type = readl(ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530216 CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS + ddrss->instance * 0x10) & 0x03;
Dave Gerlachd712b362021-05-11 10:22:11 -0500217
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530218 debug("%s: received freq change req: req type = %d, req no. = %d, instance = %d\n",
219 __func__, req_type, counter, ddrss->instance);
Dave Gerlachd712b362021-05-11 10:22:11 -0500220
221 if (req_type == 1)
222 clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq1);
223 else if (req_type == 2)
224 clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq2);
225 else if (req_type == 0)
226 /* Put DDR pll in bypass mode */
227 clk_set_rate(&ddrss->ddr_clk,
228 clk_get_rate(&ddrss->osc_clk));
229 else
230 printf("%s: Invalid freq request type\n", __func__);
231
232 writel(0x1, ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530233 CTRLMMR_DDR4_FSP_CLKCHNG_ACK_OFFS + ddrss->instance * 0x10);
Dave Gerlachd712b362021-05-11 10:22:11 -0500234 if (wait_for_bit_le32(ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530235 CTRLMMR_DDR4_FSP_CLKCHNG_REQ_OFFS + ddrss->instance * 0x10, 0x80,
Dave Gerlachd712b362021-05-11 10:22:11 -0500236 false, 10, false)) {
237 printf("Timeout during frequency handshake\n");
238 hang();
239 }
240 writel(0x0, ddrss->ddrss_ctrl_mmr +
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530241 CTRLMMR_DDR4_FSP_CLKCHNG_ACK_OFFS + ddrss->instance * 0x10);
Dave Gerlachd712b362021-05-11 10:22:11 -0500242 }
243}
244
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530245static void k3_lpddr4_ack_freq_upd_req(const lpddr4_privatedata *pd)
Dave Gerlachd712b362021-05-11 10:22:11 -0500246{
247 u32 dram_class;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530248 struct k3_ddrss_desc *ddrss = (struct k3_ddrss_desc *)pd->ddr_instance;
Dave Gerlachd712b362021-05-11 10:22:11 -0500249
250 debug("--->>> LPDDR4 Initialization is in progress ... <<<---\n");
251
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530252 dram_class = k3_lpddr4_read_ddr_type(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500253
254 switch (dram_class) {
255 case DENALI_CTL_0_DRAM_CLASS_DDR4:
256 break;
257 case DENALI_CTL_0_DRAM_CLASS_LPDDR4:
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530258 k3_lpddr4_freq_update(ddrss);
Dave Gerlachd712b362021-05-11 10:22:11 -0500259 break;
260 default:
261 printf("Unrecognized dram_class cannot update frequency!\n");
262 }
263}
264
265static int k3_ddrss_init_freq(struct k3_ddrss_desc *ddrss)
266{
267 u32 dram_class;
268 int ret;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530269 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500270
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530271 dram_class = k3_lpddr4_read_ddr_type(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500272
273 switch (dram_class) {
274 case DENALI_CTL_0_DRAM_CLASS_DDR4:
275 /* Set to ddr_freq1 from DT for DDR4 */
276 ret = clk_set_rate(&ddrss->ddr_clk, ddrss->ddr_freq1);
277 break;
278 case DENALI_CTL_0_DRAM_CLASS_LPDDR4:
279 /* Set to bypass frequency for LPDDR4*/
280 ret = clk_set_rate(&ddrss->ddr_clk, clk_get_rate(&ddrss->osc_clk));
281 break;
282 default:
283 ret = -EINVAL;
284 printf("Unrecognized dram_class cannot init frequency!\n");
285 }
286
287 if (ret < 0)
288 dev_err(ddrss->dev, "ddr clk init failed: %d\n", ret);
289 else
290 ret = 0;
291
292 return ret;
293}
294
295static void k3_lpddr4_info_handler(const lpddr4_privatedata *pd,
296 lpddr4_infotype infotype)
297{
298 if (infotype == LPDDR4_DRV_SOC_PLL_UPDATE)
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530299 k3_lpddr4_ack_freq_upd_req(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500300}
301
302static int k3_ddrss_power_on(struct k3_ddrss_desc *ddrss)
303{
304 int ret;
305
306 debug("%s(ddrss=%p)\n", __func__, ddrss);
307
308 ret = power_domain_on(&ddrss->ddrcfg_pwrdmn);
309 if (ret) {
310 dev_err(ddrss->dev, "power_domain_on() failed: %d\n", ret);
311 return ret;
312 }
313
314 ret = power_domain_on(&ddrss->ddrdata_pwrdmn);
315 if (ret) {
316 dev_err(ddrss->dev, "power_domain_on() failed: %d\n", ret);
317 return ret;
318 }
319
Lokesh Vutladd01c632021-05-11 10:22:13 -0500320 ret = device_get_supply_regulator(ddrss->dev, "vtt-supply",
321 &ddrss->vtt_supply);
322 if (ret) {
323 dev_dbg(ddrss->dev, "vtt-supply not found.\n");
324 } else {
325 ret = regulator_set_value(ddrss->vtt_supply, 3300000);
326 if (ret)
327 return ret;
328 dev_dbg(ddrss->dev, "VTT regulator enabled, volt = %d\n",
329 regulator_get_value(ddrss->vtt_supply));
330 }
331
Dave Gerlachd712b362021-05-11 10:22:11 -0500332 return 0;
333}
334
335static int k3_ddrss_ofdata_to_priv(struct udevice *dev)
336{
337 struct k3_ddrss_desc *ddrss = dev_get_priv(dev);
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530338 struct k3_ddrss_data *ddrss_data = (struct k3_ddrss_data *)dev_get_driver_data(dev);
Dave Gerlachd712b362021-05-11 10:22:11 -0500339 phys_addr_t reg;
340 int ret;
341
342 debug("%s(dev=%p)\n", __func__, dev);
343
344 reg = dev_read_addr_name(dev, "cfg");
345 if (reg == FDT_ADDR_T_NONE) {
346 dev_err(dev, "No reg property for DDRSS wrapper logic\n");
347 return -EINVAL;
348 }
Dave Gerlachfd199dd2022-03-17 12:03:42 -0500349 ddrss->ddrss_ctl_cfg = (void *)reg;
Dave Gerlachd712b362021-05-11 10:22:11 -0500350
351 reg = dev_read_addr_name(dev, "ctrl_mmr_lp4");
352 if (reg == FDT_ADDR_T_NONE) {
353 dev_err(dev, "No reg property for CTRL MMR\n");
354 return -EINVAL;
355 }
356 ddrss->ddrss_ctrl_mmr = (void *)reg;
357
Dave Gerlach296c83a2022-03-17 12:03:43 -0500358 reg = dev_read_addr_name(dev, "ss_cfg");
359 if (reg == FDT_ADDR_T_NONE) {
360 dev_dbg(dev, "No reg property for SS Config region, but this is optional so continuing.\n");
361 ddrss->ddrss_ss_cfg = NULL;
362 } else {
363 ddrss->ddrss_ss_cfg = (void *)reg;
364 }
365
Dave Gerlachd712b362021-05-11 10:22:11 -0500366 ret = power_domain_get_by_index(dev, &ddrss->ddrcfg_pwrdmn, 0);
367 if (ret) {
368 dev_err(dev, "power_domain_get() failed: %d\n", ret);
369 return ret;
370 }
371
372 ret = power_domain_get_by_index(dev, &ddrss->ddrdata_pwrdmn, 1);
373 if (ret) {
374 dev_err(dev, "power_domain_get() failed: %d\n", ret);
375 return ret;
376 }
377
378 ret = clk_get_by_index(dev, 0, &ddrss->ddr_clk);
379 if (ret)
380 dev_err(dev, "clk get failed%d\n", ret);
381
382 ret = clk_get_by_index(dev, 1, &ddrss->osc_clk);
383 if (ret)
384 dev_err(dev, "clk get failed for osc clk %d\n", ret);
385
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530386 /* Reading instance number for multi ddr subystems */
387 if (ddrss_data->flags & MULTI_DDR_SUBSYSTEM) {
388 ret = dev_read_u32(dev, "instance", &ddrss->instance);
389 if (ret) {
390 dev_err(dev, "missing instance property");
391 return -EINVAL;
392 }
393 } else {
394 ddrss->instance = 0;
395 }
396
Dave Gerlachd712b362021-05-11 10:22:11 -0500397 ret = dev_read_u32(dev, "ti,ddr-freq1", &ddrss->ddr_freq1);
398 if (ret)
399 dev_err(dev, "ddr freq1 not populated %d\n", ret);
400
401 ret = dev_read_u32(dev, "ti,ddr-freq2", &ddrss->ddr_freq2);
402 if (ret)
403 dev_err(dev, "ddr freq2 not populated %d\n", ret);
404
405 ret = dev_read_u32(dev, "ti,ddr-fhs-cnt", &ddrss->ddr_fhs_cnt);
406 if (ret)
407 dev_err(dev, "ddr fhs cnt not populated %d\n", ret);
408
Dave Gerlach296c83a2022-03-17 12:03:43 -0500409 ddrss->ti_ecc_enabled = dev_read_bool(dev, "ti,ecc-enable");
410
Dave Gerlachd712b362021-05-11 10:22:11 -0500411 return ret;
412}
413
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530414void k3_lpddr4_probe(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500415{
416 u32 status = 0U;
417 u16 configsize = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530418 lpddr4_config *config = &ddrss->config;
Dave Gerlachd712b362021-05-11 10:22:11 -0500419
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530420 status = ddrss->driverdt->probe(config, &configsize);
Dave Gerlachd712b362021-05-11 10:22:11 -0500421
422 if ((status != 0) || (configsize != sizeof(lpddr4_privatedata))
423 || (configsize > SRAM_MAX)) {
424 printf("%s: FAIL\n", __func__);
425 hang();
426 } else {
427 debug("%s: PASS\n", __func__);
428 }
429}
430
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530431void k3_lpddr4_init(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500432{
433 u32 status = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530434 lpddr4_config *config = &ddrss->config;
435 lpddr4_obj *driverdt = ddrss->driverdt;
436 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500437
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530438 if ((sizeof(*pd) != sizeof(lpddr4_privatedata)) || (sizeof(*pd) > SRAM_MAX)) {
Dave Gerlachd712b362021-05-11 10:22:11 -0500439 printf("%s: FAIL\n", __func__);
440 hang();
441 }
442
Dave Gerlachfd199dd2022-03-17 12:03:42 -0500443 config->ctlbase = (struct lpddr4_ctlregs_s *)ddrss->ddrss_ctl_cfg;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530444 config->infohandler = (lpddr4_infocallback) k3_lpddr4_info_handler;
Dave Gerlachd712b362021-05-11 10:22:11 -0500445
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530446 status = driverdt->init(pd, config);
447
448 /* linking ddr instance to lpddr4 */
449 pd->ddr_instance = (void *)ddrss;
Dave Gerlachd712b362021-05-11 10:22:11 -0500450
451 if ((status > 0U) ||
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530452 (pd->ctlbase != (struct lpddr4_ctlregs_s *)config->ctlbase) ||
453 (pd->ctlinterrupthandler != config->ctlinterrupthandler) ||
454 (pd->phyindepinterrupthandler != config->phyindepinterrupthandler)) {
Dave Gerlachd712b362021-05-11 10:22:11 -0500455 printf("%s: FAIL\n", __func__);
456 hang();
457 } else {
458 debug("%s: PASS\n", __func__);
459 }
460}
461
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530462void populate_data_array_from_dt(struct k3_ddrss_desc *ddrss,
463 struct reginitdata *reginit_data)
Dave Gerlachd712b362021-05-11 10:22:11 -0500464{
465 int ret, i;
466
467 ret = dev_read_u32_array(ddrss->dev, "ti,ctl-data",
468 (u32 *)reginit_data->ctl_regs,
469 LPDDR4_INTR_CTL_REG_COUNT);
470 if (ret)
471 printf("Error reading ctrl data %d\n", ret);
472
473 for (i = 0; i < LPDDR4_INTR_CTL_REG_COUNT; i++)
474 reginit_data->ctl_regs_offs[i] = i;
475
476 ret = dev_read_u32_array(ddrss->dev, "ti,pi-data",
477 (u32 *)reginit_data->pi_regs,
478 LPDDR4_INTR_PHY_INDEP_REG_COUNT);
479 if (ret)
480 printf("Error reading PI data\n");
481
482 for (i = 0; i < LPDDR4_INTR_PHY_INDEP_REG_COUNT; i++)
483 reginit_data->pi_regs_offs[i] = i;
484
485 ret = dev_read_u32_array(ddrss->dev, "ti,phy-data",
486 (u32 *)reginit_data->phy_regs,
487 LPDDR4_INTR_PHY_REG_COUNT);
488 if (ret)
489 printf("Error reading PHY data %d\n", ret);
490
491 for (i = 0; i < LPDDR4_INTR_PHY_REG_COUNT; i++)
492 reginit_data->phy_regs_offs[i] = i;
493}
494
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530495void k3_lpddr4_hardware_reg_init(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500496{
497 u32 status = 0U;
498 struct reginitdata reginitdata;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530499 lpddr4_obj *driverdt = ddrss->driverdt;
500 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500501
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530502 populate_data_array_from_dt(ddrss, &reginitdata);
Dave Gerlachd712b362021-05-11 10:22:11 -0500503
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530504 status = driverdt->writectlconfig(pd, reginitdata.ctl_regs,
Dave Gerlachd712b362021-05-11 10:22:11 -0500505 reginitdata.ctl_regs_offs,
506 LPDDR4_INTR_CTL_REG_COUNT);
507 if (!status)
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530508 status = driverdt->writephyindepconfig(pd, reginitdata.pi_regs,
Dave Gerlachd712b362021-05-11 10:22:11 -0500509 reginitdata.pi_regs_offs,
510 LPDDR4_INTR_PHY_INDEP_REG_COUNT);
511 if (!status)
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530512 status = driverdt->writephyconfig(pd, reginitdata.phy_regs,
Dave Gerlachd712b362021-05-11 10:22:11 -0500513 reginitdata.phy_regs_offs,
514 LPDDR4_INTR_PHY_REG_COUNT);
515 if (status) {
516 printf("%s: FAIL\n", __func__);
517 hang();
518 }
519}
520
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530521void k3_lpddr4_start(struct k3_ddrss_desc *ddrss)
Dave Gerlachd712b362021-05-11 10:22:11 -0500522{
523 u32 status = 0U;
524 u32 regval = 0U;
525 u32 offset = 0U;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530526 lpddr4_obj *driverdt = ddrss->driverdt;
527 lpddr4_privatedata *pd = &ddrss->pd;
Dave Gerlachd712b362021-05-11 10:22:11 -0500528
529 TH_OFFSET_FROM_REG(LPDDR4__START__REG, CTL_SHIFT, offset);
530
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530531 status = driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, &regval);
Dave Gerlachd712b362021-05-11 10:22:11 -0500532 if ((status > 0U) || ((regval & TH_FLD_MASK(LPDDR4__START__FLD)) != 0U)) {
533 printf("%s: Pre start FAIL\n", __func__);
534 hang();
535 }
536
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530537 status = driverdt->start(pd);
Dave Gerlachd712b362021-05-11 10:22:11 -0500538 if (status > 0U) {
539 printf("%s: FAIL\n", __func__);
540 hang();
541 }
542
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530543 status = driverdt->readreg(pd, LPDDR4_CTL_REGS, offset, &regval);
Dave Gerlachd712b362021-05-11 10:22:11 -0500544 if ((status > 0U) || ((regval & TH_FLD_MASK(LPDDR4__START__FLD)) != 1U)) {
545 printf("%s: Post start FAIL\n", __func__);
546 hang();
547 } else {
548 debug("%s: Post start PASS\n", __func__);
549 }
550}
551
Dave Gerlach296c83a2022-03-17 12:03:43 -0500552static void k3_ddrss_set_ecc_range_r0(u32 base, u32 start_address, u32 size)
553{
554 writel((start_address) >> 16, base + DDRSS_ECC_R0_STR_ADDR_REG);
555 writel((start_address + size - 1) >> 16, base + DDRSS_ECC_R0_END_ADDR_REG);
556}
557
558static void k3_ddrss_preload_ecc_mem_region(u32 *addr, u32 size, u32 word)
559{
560 int i;
561
562 printf("ECC is enabled, priming DDR which will take several seconds.\n");
563
564 for (i = 0; i < (size / 4); i++)
565 addr[i] = word;
566}
567
568static void k3_ddrss_lpddr4_ecc_calc_reserved_mem(struct k3_ddrss_desc *ddrss)
569{
570 fdtdec_setup_mem_size_base_lowest();
571
572 ddrss->ecc_reserved_space = gd->ram_size;
573 do_div(ddrss->ecc_reserved_space, 9);
574
575 /* Round to clean number */
576 ddrss->ecc_reserved_space = 1ull << (fls(ddrss->ecc_reserved_space));
577}
578
579static void k3_ddrss_lpddr4_ecc_init(struct k3_ddrss_desc *ddrss)
580{
581 u32 ecc_region_start = ddrss->ecc_regions[0].start;
582 u32 ecc_range = ddrss->ecc_regions[0].range;
583 u32 base = (u32)ddrss->ddrss_ss_cfg;
584 u32 val;
585
586 /* Only Program region 0 which covers full ddr space */
587 k3_ddrss_set_ecc_range_r0(base, ecc_region_start - gd->ram_base, ecc_range);
588
589 /* Enable ECC, RMW, WR_ALLOC */
590 writel(DDRSS_ECC_CTRL_REG_ECC_EN | DDRSS_ECC_CTRL_REG_RMW_EN |
591 DDRSS_ECC_CTRL_REG_WR_ALLOC, base + DDRSS_ECC_CTRL_REG);
592
593 /* Preload ECC Mem region with 0's */
594 k3_ddrss_preload_ecc_mem_region((u32 *)ecc_region_start, ecc_range,
595 0x00000000);
596
597 /* Clear Error Count Register */
598 writel(0x1, base + DDRSS_ECC_1B_ERR_CNT_REG);
599
600 /* Enable ECC Check */
601 val = readl(base + DDRSS_ECC_CTRL_REG);
602 val |= DDRSS_ECC_CTRL_REG_ECC_CK;
603 writel(val, base + DDRSS_ECC_CTRL_REG);
604}
605
Dave Gerlachd712b362021-05-11 10:22:11 -0500606static int k3_ddrss_probe(struct udevice *dev)
607{
608 int ret;
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530609 struct k3_ddrss_desc *ddrss = dev_get_priv(dev);
Dave Gerlachd712b362021-05-11 10:22:11 -0500610
611 debug("%s(dev=%p)\n", __func__, dev);
612
613 ret = k3_ddrss_ofdata_to_priv(dev);
614 if (ret)
615 return ret;
616
617 ddrss->dev = dev;
618 ret = k3_ddrss_power_on(ddrss);
619 if (ret)
620 return ret;
621
Dave Gerlach2c861a92021-05-11 10:22:12 -0500622#ifdef CONFIG_K3_AM64_DDRSS
623
624 writel(0x000001EF, ddrss->ddrss_ss_cfg + DDRSS_V2A_R1_MAT_REG);
625 writel(0x0, ddrss->ddrss_ss_cfg + DDRSS_ECC_CTRL_REG);
626#endif
627
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530628 ddrss->driverdt = lpddr4_getinstance();
629
630 k3_lpddr4_probe(ddrss);
631 k3_lpddr4_init(ddrss);
632 k3_lpddr4_hardware_reg_init(ddrss);
Dave Gerlachd712b362021-05-11 10:22:11 -0500633
634 ret = k3_ddrss_init_freq(ddrss);
635 if (ret)
636 return ret;
637
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530638 k3_lpddr4_start(ddrss);
Dave Gerlachd712b362021-05-11 10:22:11 -0500639
Dave Gerlach296c83a2022-03-17 12:03:43 -0500640 if (ddrss->ti_ecc_enabled) {
641 if (!ddrss->ddrss_ss_cfg) {
642 printf("%s: ss_cfg is required if ecc is enabled but not provided.",
643 __func__);
644 return -EINVAL;
645 }
646
647 k3_ddrss_lpddr4_ecc_calc_reserved_mem(ddrss);
648
649 /* Always configure one region that covers full DDR space */
650 ddrss->ecc_regions[0].start = gd->ram_base;
651 ddrss->ecc_regions[0].range = gd->ram_size - ddrss->ecc_reserved_space;
652 k3_ddrss_lpddr4_ecc_init(ddrss);
653 }
654
Dave Gerlachd712b362021-05-11 10:22:11 -0500655 return ret;
656}
657
Dave Gerlach296c83a2022-03-17 12:03:43 -0500658int k3_ddrss_ddr_fdt_fixup(struct udevice *dev, void *blob, struct bd_info *bd)
659{
660 struct k3_ddrss_desc *ddrss = dev_get_priv(dev);
661 u64 start[CONFIG_NR_DRAM_BANKS];
662 u64 size[CONFIG_NR_DRAM_BANKS];
663 int bank;
664
665 if (ddrss->ecc_reserved_space == 0)
666 return 0;
667
668 for (bank = CONFIG_NR_DRAM_BANKS - 1; bank >= 0; bank--) {
669 if (ddrss->ecc_reserved_space > bd->bi_dram[bank].size) {
670 ddrss->ecc_reserved_space -= bd->bi_dram[bank].size;
671 bd->bi_dram[bank].size = 0;
672 } else {
673 bd->bi_dram[bank].size -= ddrss->ecc_reserved_space;
674 break;
675 }
676 }
677
678 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
679 start[bank] = bd->bi_dram[bank].start;
680 size[bank] = bd->bi_dram[bank].size;
681 }
682
683 return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
684}
685
Dave Gerlachd712b362021-05-11 10:22:11 -0500686static int k3_ddrss_get_info(struct udevice *dev, struct ram_info *info)
687{
688 return 0;
689}
690
691static struct ram_ops k3_ddrss_ops = {
692 .get_info = k3_ddrss_get_info,
693};
694
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530695static const struct k3_ddrss_data k3_data = {
696 .flags = SINGLE_DDR_SUBSYSTEM,
697};
698
699static const struct k3_ddrss_data j721s2_data = {
700 .flags = MULTI_DDR_SUBSYSTEM,
701};
702
Dave Gerlachd712b362021-05-11 10:22:11 -0500703static const struct udevice_id k3_ddrss_ids[] = {
Aswath Govindrajub232cb42022-01-25 20:56:29 +0530704 {.compatible = "ti,am64-ddrss", .data = (ulong)&k3_data, },
705 {.compatible = "ti,j721e-ddrss", .data = (ulong)&k3_data, },
706 {.compatible = "ti,j721s2-ddrss", .data = (ulong)&j721s2_data, },
Dave Gerlachd712b362021-05-11 10:22:11 -0500707 {}
708};
709
710U_BOOT_DRIVER(k3_ddrss) = {
711 .name = "k3_ddrss",
712 .id = UCLASS_RAM,
713 .of_match = k3_ddrss_ids,
714 .ops = &k3_ddrss_ops,
715 .probe = k3_ddrss_probe,
716 .priv_auto = sizeof(struct k3_ddrss_desc),
717};
Aswath Govindraju6324bc72022-01-25 20:56:30 +0530718
719static int k3_msmc_set_config(struct k3_msmc *msmc)
720{
721 u32 ddr_cfg0 = 0;
722 u32 ddr_cfg1 = 0;
723
724 ddr_cfg0 |= msmc->gran << 24;
725 ddr_cfg0 |= msmc->size << 16;
726 /* heartbeat_per, bit[4:0] setting to 3 is advisable */
727 ddr_cfg0 |= 3;
728
729 /* Program MULTI_DDR_CFG0 */
730 writel(ddr_cfg0, MULTI_DDR_CFG0);
731
732 ddr_cfg1 |= msmc->enable << 16;
733 ddr_cfg1 |= msmc->config << 8;
734 ddr_cfg1 |= msmc->active;
735
736 /* Program MULTI_DDR_CFG1 */
737 writel(ddr_cfg1, MULTI_DDR_CFG1);
738
739 /* Program DDR_CFG_LOAD */
740 writel(0x60000000, DDR_CFG_LOAD);
741
742 return 0;
743}
744
745static int k3_msmc_probe(struct udevice *dev)
746{
747 struct k3_msmc *msmc = dev_get_priv(dev);
748 int ret = 0;
749
750 /* Read the granular size from DT */
751 ret = dev_read_u32(dev, "intrlv-gran", &msmc->gran);
752 if (ret) {
753 dev_err(dev, "missing intrlv-gran property");
754 return -EINVAL;
755 }
756
757 /* Read the interleave region from DT */
758 ret = dev_read_u32(dev, "intrlv-size", &msmc->size);
759 if (ret) {
760 dev_err(dev, "missing intrlv-size property");
761 return -EINVAL;
762 }
763
764 /* Read ECC enable config */
765 ret = dev_read_u32(dev, "ecc-enable", &msmc->enable);
766 if (ret) {
767 dev_err(dev, "missing ecc-enable property");
768 return -EINVAL;
769 }
770
771 /* Read EMIF configuration */
772 ret = dev_read_u32(dev, "emif-config", &msmc->config);
773 if (ret) {
774 dev_err(dev, "missing emif-config property");
775 return -EINVAL;
776 }
777
778 /* Read EMIF active */
779 ret = dev_read_u32(dev, "emif-active", &msmc->active);
780 if (ret) {
781 dev_err(dev, "missing emif-active property");
782 return -EINVAL;
783 }
784
785 ret = k3_msmc_set_config(msmc);
786 if (ret) {
787 dev_err(dev, "error setting msmc config");
788 return -EINVAL;
789 }
790
791 return 0;
792}
793
794static const struct udevice_id k3_msmc_ids[] = {
795 { .compatible = "ti,j721s2-msmc"},
796 {}
797};
798
799U_BOOT_DRIVER(k3_msmc) = {
800 .name = "k3_msmc",
801 .of_match = k3_msmc_ids,
802 .id = UCLASS_MISC,
803 .probe = k3_msmc_probe,
804 .priv_auto = sizeof(struct k3_msmc),
805 .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF,
806};