1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015-2017 Socionext Inc.
11 #include <linux/printk.h>
12 #include <linux/sizes.h>
14 #include "../soc-info.h"
15 #include "ddrmphy-regs.h"
17 /* Select either decimal or hexadecimal */
19 #define PRINTF_FORMAT "%2d"
21 #define PRINTF_FORMAT "%02x"
26 #define ptr_to_uint(p) ((unsigned int)(unsigned long)(p))
28 #define UNIPHIER_MAX_NR_DDRMPHY 3
30 struct uniphier_ddrmphy_param {
37 } phy[UNIPHIER_MAX_NR_DDRMPHY];
40 static const struct uniphier_ddrmphy_param uniphier_ddrmphy_param[] = {
42 .soc_id = UNIPHIER_PXS2_ID,
45 { .base = 0x5b830000, .nr_zq = 3, .nr_dx = 4, },
46 { .base = 0x5ba30000, .nr_zq = 3, .nr_dx = 4, },
47 { .base = 0x5bc30000, .nr_zq = 2, .nr_dx = 2, },
51 .soc_id = UNIPHIER_LD6B_ID,
54 { .base = 0x5b830000, .nr_zq = 3, .nr_dx = 4, },
55 { .base = 0x5ba30000, .nr_zq = 3, .nr_dx = 4, },
56 { .base = 0x5bc30000, .nr_zq = 2, .nr_dx = 2, },
60 UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_ddrmphy_param, uniphier_ddrmphy_param)
62 static void print_bdl(void __iomem *reg, int n)
67 for (i = 0; i < n; i++)
68 printf(FS PRINTF_FORMAT, (val >> i * 8) & 0x1f);
71 static void dump_loop(const struct uniphier_ddrmphy_param *param,
72 void (*callback)(void __iomem *))
74 void __iomem *phy_base, *dx_base;
77 for (phy = 0; phy < param->nr_phy; phy++) {
78 phy_base = ioremap(param->phy[phy].base, SZ_4K);
79 dx_base = phy_base + MPHY_DX_BASE;
81 for (dx = 0; dx < param->phy[phy].nr_dx; dx++) {
82 printf("PHY%dDX%d:", phy, dx);
84 dx_base += MPHY_DX_STRIDE;
92 static void zq_dump(const struct uniphier_ddrmphy_param *param)
94 void __iomem *phy_base, *zq_base;
98 printf("\n--- Impedance Data ---\n");
99 printf(" ZPD ZPU OPD OPU ZDV ODV\n");
101 for (phy = 0; phy < param->nr_phy; phy++) {
102 phy_base = ioremap(param->phy[phy].base, SZ_4K);
103 zq_base = phy_base + MPHY_ZQ_BASE;
105 for (zq = 0; zq < param->phy[phy].nr_zq; zq++) {
106 printf("PHY%dZQ%d:", phy, zq);
108 val = readl(zq_base + MPHY_ZQ_DR);
109 for (i = 0; i < 4; i++) {
110 printf(FS PRINTF_FORMAT, val & 0x7f);
114 val = readl(zq_base + MPHY_ZQ_PR);
115 for (i = 0; i < 2; i++) {
116 printf(FS PRINTF_FORMAT, val & 0xf);
120 zq_base += MPHY_ZQ_STRIDE;
128 static void __wbdl_dump(void __iomem *dx_base)
130 print_bdl(dx_base + MPHY_DX_BDLR0, 4);
131 print_bdl(dx_base + MPHY_DX_BDLR1, 4);
132 print_bdl(dx_base + MPHY_DX_BDLR2, 2);
134 printf(FS "(+" PRINTF_FORMAT ")",
135 readl(dx_base + MPHY_DX_LCDLR1) & 0xff);
138 static void wbdl_dump(const struct uniphier_ddrmphy_param *param)
140 printf("\n--- Write Bit Delay Line ---\n");
141 printf(" DQ0 DQ1 DQ2 DQ3 DQ4 DQ5 DQ6 DQ7 DM DQS (WDQD)\n");
143 dump_loop(param, &__wbdl_dump);
146 static void __rbdl_dump(void __iomem *dx_base)
148 print_bdl(dx_base + MPHY_DX_BDLR3, 4);
149 print_bdl(dx_base + MPHY_DX_BDLR4, 4);
150 print_bdl(dx_base + MPHY_DX_BDLR5, 1);
152 printf(FS "(+" PRINTF_FORMAT ")",
153 (readl(dx_base + MPHY_DX_LCDLR1) >> 8) & 0xff);
155 printf(FS "(+" PRINTF_FORMAT ")",
156 (readl(dx_base + MPHY_DX_LCDLR1) >> 16) & 0xff);
159 static void rbdl_dump(const struct uniphier_ddrmphy_param *param)
161 printf("\n--- Read Bit Delay Line ---\n");
162 printf(" DQ0 DQ1 DQ2 DQ3 DQ4 DQ5 DQ6 DQ7 DM (RDQSD) (RDQSND)\n");
164 dump_loop(param, &__rbdl_dump);
167 static void __wld_dump(void __iomem *dx_base)
170 u32 lcdlr0 = readl(dx_base + MPHY_DX_LCDLR0);
171 u32 gtr = readl(dx_base + MPHY_DX_GTR);
173 for (rank = 0; rank < 4; rank++) {
174 u32 wld = (lcdlr0 >> (8 * rank)) & 0xff; /* Delay */
175 u32 wlsl = (gtr >> (12 + 2 * rank)) & 0x3; /* System Latency */
177 printf(FS PRINTF_FORMAT "%sT", wld,
178 wlsl == 0 ? "-1" : wlsl == 1 ? "+0" : "+1");
182 static void wld_dump(const struct uniphier_ddrmphy_param *param)
184 printf("\n--- Write Leveling Delay ---\n");
185 printf(" Rank0 Rank1 Rank2 Rank3\n");
187 dump_loop(param, &__wld_dump);
190 static void __dqsgd_dump(void __iomem *dx_base)
193 u32 lcdlr2 = readl(dx_base + MPHY_DX_LCDLR2);
194 u32 gtr = readl(dx_base + MPHY_DX_GTR);
196 for (rank = 0; rank < 4; rank++) {
197 u32 dqsgd = (lcdlr2 >> (8 * rank)) & 0xff; /* Delay */
198 u32 dgsl = (gtr >> (3 * rank)) & 0x7; /* System Latency */
200 printf(FS PRINTF_FORMAT "+%dT", dqsgd, dgsl);
204 static void dqsgd_dump(const struct uniphier_ddrmphy_param *param)
206 printf("\n--- DQS Gating Delay ---\n");
207 printf(" Rank0 Rank1 Rank2 Rank3\n");
209 dump_loop(param, &__dqsgd_dump);
212 static void __mdl_dump(void __iomem *dx_base)
215 u32 mdl = readl(dx_base + MPHY_DX_MDLR);
217 for (i = 0; i < 3; i++)
218 printf(FS PRINTF_FORMAT, (mdl >> (8 * i)) & 0xff);
221 static void mdl_dump(const struct uniphier_ddrmphy_param *param)
223 printf("\n--- Master Delay Line ---\n");
224 printf(" IPRD TPRD MDLD\n");
226 dump_loop(param, &__mdl_dump);
229 #define REG_DUMP(x) \
230 { int ofst = MPHY_ ## x; void __iomem *reg = phy_base + ofst; \
231 printf("%3d: %-10s: %p : %08x\n", \
232 ofst >> MPHY_SHIFT, #x, reg, readl(reg)); }
234 #define DX_REG_DUMP(dx, x) \
235 { int ofst = MPHY_DX_BASE + MPHY_DX_STRIDE * (dx) + \
237 void __iomem *reg = phy_base + ofst; \
238 printf("%3d: DX%d%-7s: %p : %08x\n", \
239 ofst >> MPHY_SHIFT, (dx), #x, reg, readl(reg)); }
241 static void reg_dump(const struct uniphier_ddrmphy_param *param)
243 void __iomem *phy_base;
246 printf("\n--- DDR Multi PHY registers ---\n");
248 for (phy = 0; phy < param->nr_phy; phy++) {
249 phy_base = ioremap(param->phy[phy].base, SZ_4K);
251 printf("== PHY%d (base: %08x) ==\n", phy,
252 ptr_to_uint(phy_base));
253 printf(" No: Name : Address : Data\n");
283 for (dx = 0; dx < param->phy[phy].nr_dx; dx++) {
284 DX_REG_DUMP(dx, GCR0);
285 DX_REG_DUMP(dx, GCR1);
286 DX_REG_DUMP(dx, GCR2);
287 DX_REG_DUMP(dx, GCR3);
288 DX_REG_DUMP(dx, GTR);
295 static int do_ddrm(struct cmd_tbl *cmdtp, int flag, int argc,
298 const struct uniphier_ddrmphy_param *param;
301 param = uniphier_get_ddrmphy_param();
303 pr_err("unsupported SoC\n");
304 return CMD_RET_FAILURE;
312 if (!strcmp(cmd, "zq") || !strcmp(cmd, "all"))
315 if (!strcmp(cmd, "wbdl") || !strcmp(cmd, "all"))
318 if (!strcmp(cmd, "rbdl") || !strcmp(cmd, "all"))
321 if (!strcmp(cmd, "wld") || !strcmp(cmd, "all"))
324 if (!strcmp(cmd, "dqsgd") || !strcmp(cmd, "all"))
327 if (!strcmp(cmd, "mdl") || !strcmp(cmd, "all"))
330 if (!strcmp(cmd, "reg") || !strcmp(cmd, "all"))
333 return CMD_RET_SUCCESS;
338 "UniPhier DDR Multi PHY parameters dumper",
339 "- dump all of the following\n"
340 "ddrm zq - dump Impedance Data\n"
341 "ddrm wbdl - dump Write Bit Delay\n"
342 "ddrm rbdl - dump Read Bit Delay\n"
343 "ddrm wld - dump Write Leveling\n"
344 "ddrm dqsgd - dump DQS Gating Delay\n"
345 "ddrm mdl - dump Master Delay Line\n"
346 "ddrm reg - dump registers\n"