blob: 99d3bf3af3bc88cc518a55864ea1e2337cd8ed55 [file] [log] [blame]
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2017 NXP
4 * Copyright 2020 Linaro
5 *
6 */
7
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +08008#include <spl.h>
9#include <asm/io.h>
10#include <errno.h>
11#include <command.h>
12#include <asm/io.h>
13#include <asm/arch/lpddr4_define.h>
14#include <asm/mach-imx/iomux-v3.h>
15#include <asm/mach-imx/gpio.h>
16#include <asm-generic/gpio.h>
17#include <asm/arch/ddr.h>
18#include <asm/arch/imx8mq_pins.h>
19#include <asm/arch/sys_proto.h>
20#include <asm/arch/clock.h>
21#include <asm/mach-imx/gpio.h>
22#include "ddr.h"
23
Fabio Estevam6c2024d2022-04-12 13:05:36 -030024#include <linux/delay.h>
25
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +080026struct lpddr4_desc {
27 char name[16];
28 unsigned int id;
29 unsigned int size;
30 unsigned int count;
31 /* an optional field
32 * use it if default is not the
33 * 1-st array entry
34 */
35 unsigned int _default;
36 /* An optional field to distiguish DRAM chips that
37 * have different geometry, though return the same MRR.
38 * Default value 0xff
39 */
40 u8 subind;
41 struct dram_timing_info *timing;
42 char *desc[4];
43};
44
45#define DEFAULT (('D' << 24) + ('E' << 16) + ('F' << 8) + 'A')
46static const struct lpddr4_desc lpddr4_array[] = {
47 { .name = "Nanya", .id = 0x05000010, .subind = 0xff,
48 .size = 2048, .count = 1, .timing = &ucm_dram_timing_01061010},
49 { .name = "Samsung", .id = 0x01061010, .subind = 0xff,
50 .size = 2048, .count = 1, .timing = &ucm_dram_timing_01061010},
51 { .name = "Kingston", .id = 0xff000010, .subind = 0x04,
52 .size = 4096, .count = 1, .timing = &ucm_dram_timing_ff000110},
53 { .name = "Kingston", .id = 0xff000010, .subind = 0x02,
54 .size = 2048, .count = 1, .timing = &ucm_dram_timing_01061010},
55 { .name = "Micron", .id = 0xff020008, .subind = 0xff,
56 .size = 2048, .count = 1, .timing = &ucm_dram_timing_ff020008},
57 { .name = "Micron", .id = 0xff000110, .subind = 0xff,
58 .size = 4096, .count = 1, .timing = &ucm_dram_timing_ff000110},
59};
60
61static unsigned int lpddr4_get_mr(void)
62{
63 int i = 0, attempts = 5;
64 unsigned int ddr_info = 0;
65 unsigned int regs[] = { 5, 6, 7, 8 };
66
67 do {
68 for (i = 0 ; i < ARRAY_SIZE(regs) ; i++) {
69 unsigned int data = 0;
70
71 data = lpddr4_mr_read(0xF, regs[i]);
72 ddr_info <<= 8;
73 ddr_info += (data & 0xFF);
74 }
75 if (ddr_info != 0xFFFFFFFF && ddr_info != 0)
76 break; // The attempt was successful
77 } while (--attempts);
78 return ddr_info;
79}
80
81static void spl_tcm_init(struct lpddr4_tcm_desc *lpddr4_tcm_desc)
82{
83 if (lpddr4_tcm_desc->sign == DEFAULT)
84 return;
85
86 lpddr4_tcm_desc->sign = DEFAULT;
87 lpddr4_tcm_desc->index = 0;
88}
89
90static void spl_tcm_fini(struct lpddr4_tcm_desc *lpddr4_tcm_desc)
91{
92 if (lpddr4_tcm_desc->sign != DEFAULT)
93 return;
94
95 lpddr4_tcm_desc->sign = ~DEFAULT;
96 lpddr4_tcm_desc->index = 0;
97}
98
99#define SPL_TCM_DATA 0x7e0000
100#define SPL_TCM_INIT spl_tcm_init(lpddr4_tcm_desc)
101#define SPL_TCM_FINI spl_tcm_fini(lpddr4_tcm_desc)
102
103void spl_dram_init_compulab(void)
104{
105 unsigned int ddr_info = 0xdeadbeef;
106 unsigned int ddr_info_mrr = 0xdeadbeef;
107 unsigned int ddr_found = 0;
108 int i = 0;
109
110 struct lpddr4_tcm_desc *lpddr4_tcm_desc =
111 (struct lpddr4_tcm_desc *)SPL_TCM_DATA;
112
113 if (lpddr4_tcm_desc->sign != DEFAULT) {
Fabio Estevam6c2024d2022-04-12 13:05:36 -0300114 /* get ddr type from the eeprom if not in tcm scan mode */
115 ddr_info = cl_eeprom_get_ddrinfo();
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +0800116 for (i = 0; i < ARRAY_SIZE(lpddr4_array); i++) {
117 if (lpddr4_array[i].id == ddr_info &&
Fabio Estevam6c2024d2022-04-12 13:05:36 -0300118 lpddr4_array[i].subind == cl_eeprom_get_subind()) {
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +0800119 ddr_found = 1;
120 break;
121 }
122 }
123 }
124
125 /* Walk trought all available ddr ids and apply
126 * one by one. Save the index at the tcm memory that
127 * persists after the reset.
128 */
129 if (ddr_found == 0) {
130 SPL_TCM_INIT;
131
132 if (lpddr4_tcm_desc->index < ARRAY_SIZE(lpddr4_array)) {
133 printf("DDRINFO: Cfg attempt: [ %d/%lu ]\n",
134 lpddr4_tcm_desc->index + 1,
135 ARRAY_SIZE(lpddr4_array));
136 i = lpddr4_tcm_desc->index;
137 lpddr4_tcm_desc->index += 1;
138 } else {
139 /* Ran out all available ddr setings */
140 printf("DDRINFO: Ran out all [ %lu ] cfg attempts. A non supported configuration.\n",
141 ARRAY_SIZE(lpddr4_array));
142 while (1)
143 ;
144 }
145 ddr_info = lpddr4_array[i].id;
146 } else {
147 printf("DDRINFO(%s): %s %dG\n", (ddr_found ? "D" : "?"),
148 lpddr4_array[i].name,
149 lpddr4_array[i].size);
150 }
151
152 if (ddr_init(lpddr4_array[i].timing)) {
153 SPL_TCM_INIT;
154 do_reset(NULL, 0, 0, NULL);
155 }
156
157 ddr_info_mrr = lpddr4_get_mr();
158 if (ddr_info_mrr == 0xFFFFFFFF) {
159 printf("DDRINFO(M): mr5-8 [ 0x%x ] is invalid; reset\n",
160 ddr_info_mrr);
161 SPL_TCM_INIT;
162 do_reset(NULL, 0, 0, NULL);
163 }
164
165 printf("DDRINFO(M): mr5-8 [ 0x%x ]\n", ddr_info_mrr);
166 printf("DDRINFO(%s): mr5-8 [ 0x%x ]\n", (ddr_found ? "E" : "T"),
167 ddr_info);
168
169 if (ddr_info_mrr != ddr_info) {
170 SPL_TCM_INIT;
171 do_reset(NULL, 0, 0, NULL);
172 }
173
174 SPL_TCM_FINI;
175
Fabio Estevam6c2024d2022-04-12 13:05:36 -0300176 if (ddr_found == 0) {
177 /* Update eeprom */
178 cl_eeprom_set_ddrinfo(ddr_info_mrr);
179 mdelay(10);
180 ddr_info = cl_eeprom_get_ddrinfo();
181 mdelay(10);
182 cl_eeprom_set_subind(lpddr4_array[i].subind);
183 /* make sure that the ddr_info has reached the eeprom */
184 printf("DDRINFO(E): mr5-8 [ 0x%x ], read back\n", ddr_info);
185 if (ddr_info_mrr != ddr_info || cl_eeprom_get_subind() != lpddr4_array[i].subind) {
186 printf("DDRINFO(EEPROM): make sure that the eeprom is accessible\n");
187 printf("DDRINFO(EEPROM): i2c dev 1; i2c md 0x51 0x40 0x50\n");
188 }
189 }
190
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +0800191 /* Pass the dram size to th U-Boot through the tcm memory */
192 { /* To figure out what to store into the TCM buffer */
193 /* For debug purpouse only. To override the real memsize */
Fabio Estevam6c2024d2022-04-12 13:05:36 -0300194 unsigned int ddr_tcm_size = cl_eeprom_get_osize();
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +0800195
196 if (ddr_tcm_size == 0 || ddr_tcm_size == -1)
197 ddr_tcm_size = lpddr4_array[i].size;
198
199 lpddr4_tcm_desc->size = ddr_tcm_size;
200 }
201}