blob: a924af431c9760880b26895a0ece07b4881bd85a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Faneae4de22018-01-10 13:20:37 +08002/*
3 * Copyright 2017 NXP
4 *
5 * Peng Fan <peng.fan@nxp.com>
Peng Faneae4de22018-01-10 13:20:37 +08006 */
7
8#include <common.h>
9#include <asm/arch/imx-regs.h>
10#include <asm/io.h>
11#include <asm/arch/clock.h>
12#include <asm/arch/sys_proto.h>
13#include <asm/mach-imx/hab.h>
14#include <asm/mach-imx/boot_mode.h>
15#include <asm/mach-imx/syscounter.h>
16#include <asm/armv8/mmu.h>
Peng Fanc98e0322019-08-27 06:25:58 +000017#include <dm/uclass.h>
Peng Faneae4de22018-01-10 13:20:37 +080018#include <errno.h>
19#include <fdt_support.h>
20#include <fsl_wdog.h>
21#include <imx_sip.h>
22
23DECLARE_GLOBAL_DATA_PTR;
24
Stefano Babicf8b509b2019-09-20 08:47:53 +020025#if defined(CONFIG_IMX_HAB)
Peng Faneae4de22018-01-10 13:20:37 +080026struct imx_sec_config_fuse_t const imx_sec_config_fuse = {
27 .bank = 1,
28 .word = 3,
29};
30#endif
31
32int timer_init(void)
33{
34#ifdef CONFIG_SPL_BUILD
35 struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
36 unsigned long freq = readl(&sctr->cntfid0);
37
38 /* Update with accurate clock frequency */
39 asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
40
41 clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
42 SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
43#endif
44
45 gd->arch.tbl = 0;
46 gd->arch.tbu = 0;
47
48 return 0;
49}
50
51void enable_tzc380(void)
52{
53 struct iomuxc_gpr_base_regs *gpr =
54 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
55
56 /* Enable TZASC and lock setting */
57 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN);
58 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN_LOCK);
Peng Fan99047fc2019-08-27 06:25:30 +000059 if (IS_ENABLED(CONFIG_IMX8MM))
60 setbits_le32(&gpr->gpr[10], BIT(1));
Ye Li4c97c462019-08-27 06:25:34 +000061 /*
62 * set Region 0 attribute to allow secure and non-secure
63 * read/write permission. Found some masters like usb dwc3
64 * controllers can't work with secure memory.
65 */
66 writel(0xf0000000, TZASC_BASE_ADDR + 0x108);
Peng Faneae4de22018-01-10 13:20:37 +080067}
68
69void set_wdog_reset(struct wdog_regs *wdog)
70{
71 /*
72 * Output WDOG_B signal to reset external pmic or POR_B decided by
73 * the board design. Without external reset, the peripherals/DDR/
74 * PMIC are not reset, that may cause system working abnormal.
75 * WDZST bit is write-once only bit. Align this bit in kernel,
76 * otherwise kernel code will have no chance to set this bit.
77 */
78 setbits_le16(&wdog->wcr, WDOG_WDT_MASK | WDOG_WDZST_MASK);
79}
80
81static struct mm_region imx8m_mem_map[] = {
82 {
83 /* ROM */
84 .virt = 0x0UL,
85 .phys = 0x0UL,
86 .size = 0x100000UL,
87 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
88 PTE_BLOCK_OUTER_SHARE
89 }, {
Gary Bisson5c72a452018-11-14 17:55:28 +010090 /* CAAM */
91 .virt = 0x100000UL,
92 .phys = 0x100000UL,
93 .size = 0x8000UL,
94 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
95 PTE_BLOCK_NON_SHARE |
96 PTE_BLOCK_PXN | PTE_BLOCK_UXN
97 }, {
98 /* TCM */
99 .virt = 0x7C0000UL,
100 .phys = 0x7C0000UL,
101 .size = 0x80000UL,
102 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
103 PTE_BLOCK_NON_SHARE |
104 PTE_BLOCK_PXN | PTE_BLOCK_UXN
105 }, {
Peng Faneae4de22018-01-10 13:20:37 +0800106 /* OCRAM */
107 .virt = 0x900000UL,
108 .phys = 0x900000UL,
109 .size = 0x200000UL,
110 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
111 PTE_BLOCK_OUTER_SHARE
112 }, {
113 /* AIPS */
114 .virt = 0xB00000UL,
115 .phys = 0xB00000UL,
116 .size = 0x3f500000UL,
117 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
118 PTE_BLOCK_NON_SHARE |
119 PTE_BLOCK_PXN | PTE_BLOCK_UXN
120 }, {
121 /* DRAM1 */
122 .virt = 0x40000000UL,
123 .phys = 0x40000000UL,
Peng Fanb749b5e2019-08-27 06:25:27 +0000124 .size = PHYS_SDRAM_SIZE,
Peng Faneae4de22018-01-10 13:20:37 +0800125 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
126 PTE_BLOCK_OUTER_SHARE
Peng Fanb749b5e2019-08-27 06:25:27 +0000127#ifdef PHYS_SDRAM_2_SIZE
Peng Faneae4de22018-01-10 13:20:37 +0800128 }, {
129 /* DRAM2 */
130 .virt = 0x100000000UL,
131 .phys = 0x100000000UL,
Peng Fanb749b5e2019-08-27 06:25:27 +0000132 .size = PHYS_SDRAM_2_SIZE,
Peng Faneae4de22018-01-10 13:20:37 +0800133 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
134 PTE_BLOCK_OUTER_SHARE
Peng Fanb749b5e2019-08-27 06:25:27 +0000135#endif
Peng Faneae4de22018-01-10 13:20:37 +0800136 }, {
137 /* List terminator */
138 0,
139 }
140};
141
142struct mm_region *mem_map = imx8m_mem_map;
143
Peng Fanb749b5e2019-08-27 06:25:27 +0000144void enable_caches(void)
145{
146 /*
147 * If OPTEE runs, remove OPTEE memory from MMU table to
148 * avoid speculative prefetch. OPTEE runs at the top of
149 * the first memory bank
150 */
151 if (rom_pointer[1])
152 imx8m_mem_map[5].size -= rom_pointer[1];
153
154 icache_enable();
155 dcache_enable();
156}
157
Peng Fan1caffdf2019-08-27 06:25:17 +0000158static u32 get_cpu_variant_type(u32 type)
159{
160 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
161 struct fuse_bank *bank = &ocotp->bank[1];
162 struct fuse_bank1_regs *fuse =
163 (struct fuse_bank1_regs *)bank->fuse_regs;
164
165 u32 value = readl(&fuse->tester4);
166
167 if (type == MXC_CPU_IMX8MM) {
168 switch (value & 0x3) {
169 case 2:
170 if (value & 0x1c0000)
171 return MXC_CPU_IMX8MMDL;
172 else
173 return MXC_CPU_IMX8MMD;
174 case 3:
175 if (value & 0x1c0000)
176 return MXC_CPU_IMX8MMSL;
177 else
178 return MXC_CPU_IMX8MMS;
179 default:
180 if (value & 0x1c0000)
181 return MXC_CPU_IMX8MML;
182 break;
183 }
184 }
185
186 return type;
187}
188
Peng Faneae4de22018-01-10 13:20:37 +0800189u32 get_cpu_rev(void)
190{
191 struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
192 u32 reg = readl(&ana_pll->digprog);
193 u32 type = (reg >> 16) & 0xff;
Peng Fan1caffdf2019-08-27 06:25:17 +0000194 u32 major_low = (reg >> 8) & 0xff;
Peng Faneae4de22018-01-10 13:20:37 +0800195 u32 rom_version;
196
197 reg &= 0xff;
198
Peng Fan1caffdf2019-08-27 06:25:17 +0000199 /* i.MX8MM */
200 if (major_low == 0x41) {
201 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
202 } else {
203 if (reg == CHIP_REV_1_0) {
204 /*
Peng Fanc23fbdd2019-10-16 10:24:17 +0000205 * For B0 chip, the DIGPROG is not updated,
206 * it is still TO1.0. we have to check ROM
207 * version or OCOTP_READ_FUSE_DATA.
208 * 0xff0055aa is magic number for B1.
Peng Fan1caffdf2019-08-27 06:25:17 +0000209 */
Peng Fanc23fbdd2019-10-16 10:24:17 +0000210 if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) {
211 reg = CHIP_REV_2_1;
212 } else {
213 rom_version =
214 readl((void __iomem *)ROM_VERSION_A0);
215 if (rom_version != CHIP_REV_1_0) {
216 rom_version = readl((void __iomem *)ROM_VERSION_B0);
217 if (rom_version == CHIP_REV_2_0)
218 reg = CHIP_REV_2_0;
219 }
Peng Fan1caffdf2019-08-27 06:25:17 +0000220 }
Peng Faneae4de22018-01-10 13:20:37 +0800221 }
222 }
223
224 return (type << 12) | reg;
225}
226
227static void imx_set_wdog_powerdown(bool enable)
228{
229 struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
230 struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
231 struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
232
233 /* Write to the PDE (Power Down Enable) bit */
234 writew(enable, &wdog1->wmcr);
235 writew(enable, &wdog2->wmcr);
236 writew(enable, &wdog3->wmcr);
237}
238
Peng Fanc98e0322019-08-27 06:25:58 +0000239int arch_cpu_init_dm(void)
240{
241 struct udevice *dev;
242 int ret;
243
244 ret = uclass_get_device_by_name(UCLASS_CLK,
245 "clock-controller@30380000",
246 &dev);
247 if (ret < 0) {
248 printf("Failed to find clock node. Check device tree\n");
249 return ret;
250 }
251
252 return 0;
253}
254
Peng Faneae4de22018-01-10 13:20:37 +0800255int arch_cpu_init(void)
256{
Peng Fanc0b30d72019-04-17 09:41:16 +0000257 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
Peng Faneae4de22018-01-10 13:20:37 +0800258 /*
Peng Fand0ca2892019-08-27 06:25:37 +0000259 * ROM might disable clock for SCTR,
260 * enable the clock before timer_init.
261 */
262 if (IS_ENABLED(CONFIG_SPL_BUILD))
263 clock_enable(CCGR_SCTR, 1);
264 /*
Peng Faneae4de22018-01-10 13:20:37 +0800265 * Init timer at very early state, because sscg pll setting
266 * will use it
267 */
268 timer_init();
269
270 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
271 clock_init();
272 imx_set_wdog_powerdown(false);
273 }
274
Peng Fanc0b30d72019-04-17 09:41:16 +0000275 if (is_imx8mq()) {
276 clock_enable(CCGR_OCOTP, 1);
277 if (readl(&ocotp->ctrl) & 0x200)
278 writel(0x200, &ocotp->ctrl_clr);
279 }
280
Peng Faneae4de22018-01-10 13:20:37 +0800281 return 0;
282}
283
284bool is_usb_boot(void)
285{
286 return get_boot_device() == USB_BOOT;
287}
288
289#ifdef CONFIG_OF_SYSTEM_SETUP
290int ft_system_setup(void *blob, bd_t *bd)
291{
292 int i = 0;
293 int rc;
294 int nodeoff;
295
296 /* Disable the CPU idle for A0 chip since the HW does not support it */
297 if (is_soc_rev(CHIP_REV_1_0)) {
298 static const char * const nodes_path[] = {
299 "/cpus/cpu@0",
300 "/cpus/cpu@1",
301 "/cpus/cpu@2",
302 "/cpus/cpu@3",
303 };
304
305 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
306 nodeoff = fdt_path_offset(blob, nodes_path[i]);
307 if (nodeoff < 0)
308 continue; /* Not found, skip it */
309
310 printf("Found %s node\n", nodes_path[i]);
311
312 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
313 if (rc) {
314 printf("Unable to update property %s:%s, err=%s\n",
315 nodes_path[i], "status", fdt_strerror(rc));
316 return rc;
317 }
318
319 printf("Remove %s:%s\n", nodes_path[i],
320 "cpu-idle-states");
321 }
322 }
323
324 return 0;
325}
326#endif
327
Peng Fan24290d92019-08-27 06:25:41 +0000328#if defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SYSRESET)
Peng Faneae4de22018-01-10 13:20:37 +0800329void reset_cpu(ulong addr)
330{
Peng Fan24290d92019-08-27 06:25:41 +0000331 struct watchdog_regs *wdog = (struct watchdog_regs *)addr;
Peng Faneae4de22018-01-10 13:20:37 +0800332
Peng Fan24290d92019-08-27 06:25:41 +0000333 if (!addr)
334 wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
Peng Faneae4de22018-01-10 13:20:37 +0800335
Peng Fan24290d92019-08-27 06:25:41 +0000336 /* Clear WDA to trigger WDOG_B immediately */
337 writew((WCR_WDE | WCR_SRS), &wdog->wcr);
338
339 while (1) {
340 /*
341 * spin for .5 seconds before reset
342 */
343 }
Peng Faneae4de22018-01-10 13:20:37 +0800344}
Peng Fan24290d92019-08-27 06:25:41 +0000345#endif