blob: c449b8b1ec487c2d79b4f8e35187c73d79dc6327 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fanfcdbde72018-01-10 13:20:37 +08002/*
3 * Copyright 2017 NXP
4 *
5 * Peng Fan <peng.fan@nxp.com>
Peng Fanfcdbde72018-01-10 13:20:37 +08006 */
7
8#include <common.h>
Simon Glass9edefc22019-11-14 12:57:37 -07009#include <cpu_func.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080010#include <asm/arch/imx-regs.h>
11#include <asm/io.h>
12#include <asm/arch/clock.h>
13#include <asm/arch/sys_proto.h>
14#include <asm/mach-imx/hab.h>
15#include <asm/mach-imx/boot_mode.h>
16#include <asm/mach-imx/syscounter.h>
17#include <asm/armv8/mmu.h>
Peng Fane663c702019-08-27 06:25:58 +000018#include <dm/uclass.h>
Peng Fanfcdbde72018-01-10 13:20:37 +080019#include <errno.h>
20#include <fdt_support.h>
21#include <fsl_wdog.h>
22#include <imx_sip.h>
23
24DECLARE_GLOBAL_DATA_PTR;
25
Stefano Babicd714a752019-09-20 08:47:53 +020026#if defined(CONFIG_IMX_HAB)
Peng Fanfcdbde72018-01-10 13:20:37 +080027struct imx_sec_config_fuse_t const imx_sec_config_fuse = {
28 .bank = 1,
29 .word = 3,
30};
31#endif
32
33int timer_init(void)
34{
35#ifdef CONFIG_SPL_BUILD
36 struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
37 unsigned long freq = readl(&sctr->cntfid0);
38
39 /* Update with accurate clock frequency */
40 asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
41
42 clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
43 SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
44#endif
45
46 gd->arch.tbl = 0;
47 gd->arch.tbu = 0;
48
49 return 0;
50}
51
52void enable_tzc380(void)
53{
54 struct iomuxc_gpr_base_regs *gpr =
55 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
56
57 /* Enable TZASC and lock setting */
58 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN);
59 setbits_le32(&gpr->gpr[10], GPR_TZASC_EN_LOCK);
Peng Fana07c7182019-12-27 10:19:42 +080060 if (is_imx8mm() || is_imx8mn() || is_imx8mp())
Peng Fandbb2b7e2019-08-27 06:25:30 +000061 setbits_le32(&gpr->gpr[10], BIT(1));
Ye Lib3cf0a82019-08-27 06:25:34 +000062 /*
63 * set Region 0 attribute to allow secure and non-secure
64 * read/write permission. Found some masters like usb dwc3
65 * controllers can't work with secure memory.
66 */
67 writel(0xf0000000, TZASC_BASE_ADDR + 0x108);
Peng Fanfcdbde72018-01-10 13:20:37 +080068}
69
70void set_wdog_reset(struct wdog_regs *wdog)
71{
72 /*
73 * Output WDOG_B signal to reset external pmic or POR_B decided by
74 * the board design. Without external reset, the peripherals/DDR/
75 * PMIC are not reset, that may cause system working abnormal.
76 * WDZST bit is write-once only bit. Align this bit in kernel,
77 * otherwise kernel code will have no chance to set this bit.
78 */
79 setbits_le16(&wdog->wcr, WDOG_WDT_MASK | WDOG_WDZST_MASK);
80}
81
82static struct mm_region imx8m_mem_map[] = {
83 {
84 /* ROM */
85 .virt = 0x0UL,
86 .phys = 0x0UL,
87 .size = 0x100000UL,
88 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
89 PTE_BLOCK_OUTER_SHARE
90 }, {
Gary Bissoncb158852018-11-14 17:55:28 +010091 /* CAAM */
92 .virt = 0x100000UL,
93 .phys = 0x100000UL,
94 .size = 0x8000UL,
95 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
96 PTE_BLOCK_NON_SHARE |
97 PTE_BLOCK_PXN | PTE_BLOCK_UXN
98 }, {
99 /* TCM */
100 .virt = 0x7C0000UL,
101 .phys = 0x7C0000UL,
102 .size = 0x80000UL,
103 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
104 PTE_BLOCK_NON_SHARE |
105 PTE_BLOCK_PXN | PTE_BLOCK_UXN
106 }, {
Peng Fanfcdbde72018-01-10 13:20:37 +0800107 /* OCRAM */
108 .virt = 0x900000UL,
109 .phys = 0x900000UL,
110 .size = 0x200000UL,
111 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
112 PTE_BLOCK_OUTER_SHARE
113 }, {
114 /* AIPS */
115 .virt = 0xB00000UL,
116 .phys = 0xB00000UL,
117 .size = 0x3f500000UL,
118 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
119 PTE_BLOCK_NON_SHARE |
120 PTE_BLOCK_PXN | PTE_BLOCK_UXN
121 }, {
122 /* DRAM1 */
123 .virt = 0x40000000UL,
124 .phys = 0x40000000UL,
Peng Fan59efa6b2019-08-27 06:25:27 +0000125 .size = PHYS_SDRAM_SIZE,
Peng Fanfcdbde72018-01-10 13:20:37 +0800126 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
127 PTE_BLOCK_OUTER_SHARE
Peng Fan59efa6b2019-08-27 06:25:27 +0000128#ifdef PHYS_SDRAM_2_SIZE
Peng Fanfcdbde72018-01-10 13:20:37 +0800129 }, {
130 /* DRAM2 */
131 .virt = 0x100000000UL,
132 .phys = 0x100000000UL,
Peng Fan59efa6b2019-08-27 06:25:27 +0000133 .size = PHYS_SDRAM_2_SIZE,
Peng Fanfcdbde72018-01-10 13:20:37 +0800134 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
135 PTE_BLOCK_OUTER_SHARE
Peng Fan59efa6b2019-08-27 06:25:27 +0000136#endif
Peng Fanfcdbde72018-01-10 13:20:37 +0800137 }, {
138 /* List terminator */
139 0,
140 }
141};
142
143struct mm_region *mem_map = imx8m_mem_map;
144
Peng Fan59efa6b2019-08-27 06:25:27 +0000145void enable_caches(void)
146{
147 /*
148 * If OPTEE runs, remove OPTEE memory from MMU table to
149 * avoid speculative prefetch. OPTEE runs at the top of
150 * the first memory bank
151 */
152 if (rom_pointer[1])
153 imx8m_mem_map[5].size -= rom_pointer[1];
154
155 icache_enable();
156 dcache_enable();
157}
158
Peng Fan78db9a52019-08-27 06:25:17 +0000159static u32 get_cpu_variant_type(u32 type)
160{
161 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
162 struct fuse_bank *bank = &ocotp->bank[1];
163 struct fuse_bank1_regs *fuse =
164 (struct fuse_bank1_regs *)bank->fuse_regs;
165
166 u32 value = readl(&fuse->tester4);
167
Peng Fancb1a1de2020-02-05 17:34:54 +0800168 if (type == MXC_CPU_IMX8MQ) {
169 if ((value & 0x3) == 0x2)
170 return MXC_CPU_IMX8MD;
171 else if (value & 0x200000)
172 return MXC_CPU_IMX8MQL;
173
174 } else if (type == MXC_CPU_IMX8MM) {
Peng Fan78db9a52019-08-27 06:25:17 +0000175 switch (value & 0x3) {
176 case 2:
177 if (value & 0x1c0000)
178 return MXC_CPU_IMX8MMDL;
179 else
180 return MXC_CPU_IMX8MMD;
181 case 3:
182 if (value & 0x1c0000)
183 return MXC_CPU_IMX8MMSL;
184 else
185 return MXC_CPU_IMX8MMS;
186 default:
187 if (value & 0x1c0000)
188 return MXC_CPU_IMX8MML;
189 break;
190 }
191 }
192
193 return type;
194}
195
Peng Fanfcdbde72018-01-10 13:20:37 +0800196u32 get_cpu_rev(void)
197{
198 struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
199 u32 reg = readl(&ana_pll->digprog);
200 u32 type = (reg >> 16) & 0xff;
Peng Fan78db9a52019-08-27 06:25:17 +0000201 u32 major_low = (reg >> 8) & 0xff;
Peng Fanfcdbde72018-01-10 13:20:37 +0800202 u32 rom_version;
203
204 reg &= 0xff;
205
Peng Fan625b03d2019-12-27 10:14:02 +0800206 /* iMX8MP */
207 if (major_low == 0x43) {
208 return (MXC_CPU_IMX8MP << 12) | reg;
209 } else if (major_low == 0x42) {
210 /* iMX8MN */
Peng Fan24341312019-06-27 17:23:49 +0800211 return (MXC_CPU_IMX8MN << 12) | reg;
212 } else if (major_low == 0x41) {
Peng Fan78db9a52019-08-27 06:25:17 +0000213 type = get_cpu_variant_type(MXC_CPU_IMX8MM);
214 } else {
215 if (reg == CHIP_REV_1_0) {
216 /*
Peng Fan9e094452019-10-16 10:24:17 +0000217 * For B0 chip, the DIGPROG is not updated,
218 * it is still TO1.0. we have to check ROM
219 * version or OCOTP_READ_FUSE_DATA.
220 * 0xff0055aa is magic number for B1.
Peng Fan78db9a52019-08-27 06:25:17 +0000221 */
Peng Fan9e094452019-10-16 10:24:17 +0000222 if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) {
223 reg = CHIP_REV_2_1;
224 } else {
225 rom_version =
226 readl((void __iomem *)ROM_VERSION_A0);
227 if (rom_version != CHIP_REV_1_0) {
228 rom_version = readl((void __iomem *)ROM_VERSION_B0);
Patrick Wildt6a4b07e2019-11-19 09:42:06 +0100229 rom_version &= 0xff;
Peng Fan9e094452019-10-16 10:24:17 +0000230 if (rom_version == CHIP_REV_2_0)
231 reg = CHIP_REV_2_0;
232 }
Peng Fan78db9a52019-08-27 06:25:17 +0000233 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800234 }
Peng Fancb1a1de2020-02-05 17:34:54 +0800235
236 type = get_cpu_variant_type(type);
Peng Fanfcdbde72018-01-10 13:20:37 +0800237 }
238
239 return (type << 12) | reg;
240}
241
242static void imx_set_wdog_powerdown(bool enable)
243{
244 struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
245 struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
246 struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
247
248 /* Write to the PDE (Power Down Enable) bit */
249 writew(enable, &wdog1->wmcr);
250 writew(enable, &wdog2->wmcr);
251 writew(enable, &wdog3->wmcr);
252}
253
Peng Fane663c702019-08-27 06:25:58 +0000254int arch_cpu_init_dm(void)
255{
256 struct udevice *dev;
257 int ret;
258
Peng Fancd7c8062019-10-16 03:01:51 +0000259 if (CONFIG_IS_ENABLED(CLK)) {
260 ret = uclass_get_device_by_name(UCLASS_CLK,
261 "clock-controller@30380000",
262 &dev);
263 if (ret < 0) {
264 printf("Failed to find clock node. Check device tree\n");
265 return ret;
266 }
Peng Fane663c702019-08-27 06:25:58 +0000267 }
268
269 return 0;
270}
271
Peng Fanfcdbde72018-01-10 13:20:37 +0800272int arch_cpu_init(void)
273{
Peng Fan702339b2019-04-17 09:41:16 +0000274 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +0800275 /*
Peng Fan0528ba02019-08-27 06:25:37 +0000276 * ROM might disable clock for SCTR,
277 * enable the clock before timer_init.
278 */
279 if (IS_ENABLED(CONFIG_SPL_BUILD))
280 clock_enable(CCGR_SCTR, 1);
281 /*
Peng Fanfcdbde72018-01-10 13:20:37 +0800282 * Init timer at very early state, because sscg pll setting
283 * will use it
284 */
285 timer_init();
286
287 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
288 clock_init();
289 imx_set_wdog_powerdown(false);
290 }
291
Peng Fan702339b2019-04-17 09:41:16 +0000292 if (is_imx8mq()) {
293 clock_enable(CCGR_OCOTP, 1);
294 if (readl(&ocotp->ctrl) & 0x200)
295 writel(0x200, &ocotp->ctrl_clr);
296 }
297
Peng Fanfcdbde72018-01-10 13:20:37 +0800298 return 0;
299}
300
Peng Fanb1821372019-09-16 03:09:36 +0000301#if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP)
302struct rom_api *g_rom_api = (struct rom_api *)0x980;
303
304enum boot_device get_boot_device(void)
305{
306 volatile gd_t *pgd = gd;
307 int ret;
308 u32 boot;
309 u16 boot_type;
310 u8 boot_instance;
311 enum boot_device boot_dev = SD1_BOOT;
312
313 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
314 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
315 gd = pgd;
316
317 if (ret != ROM_API_OKAY) {
318 puts("ROMAPI: failure at query_boot_info\n");
319 return -1;
320 }
321
322 boot_type = boot >> 16;
323 boot_instance = (boot >> 8) & 0xff;
324
325 switch (boot_type) {
326 case BT_DEV_TYPE_SD:
327 boot_dev = boot_instance + SD1_BOOT;
328 break;
329 case BT_DEV_TYPE_MMC:
330 boot_dev = boot_instance + MMC1_BOOT;
331 break;
332 case BT_DEV_TYPE_NAND:
333 boot_dev = NAND_BOOT;
334 break;
335 case BT_DEV_TYPE_FLEXSPINOR:
336 boot_dev = QSPI_BOOT;
337 break;
338 case BT_DEV_TYPE_USB:
339 boot_dev = USB_BOOT;
340 break;
341 default:
342 break;
343 }
344
345 return boot_dev;
346}
347#endif
348
Peng Fanfcdbde72018-01-10 13:20:37 +0800349bool is_usb_boot(void)
350{
351 return get_boot_device() == USB_BOOT;
352}
353
354#ifdef CONFIG_OF_SYSTEM_SETUP
355int ft_system_setup(void *blob, bd_t *bd)
356{
357 int i = 0;
358 int rc;
359 int nodeoff;
360
361 /* Disable the CPU idle for A0 chip since the HW does not support it */
362 if (is_soc_rev(CHIP_REV_1_0)) {
363 static const char * const nodes_path[] = {
364 "/cpus/cpu@0",
365 "/cpus/cpu@1",
366 "/cpus/cpu@2",
367 "/cpus/cpu@3",
368 };
369
370 for (i = 0; i < ARRAY_SIZE(nodes_path); i++) {
371 nodeoff = fdt_path_offset(blob, nodes_path[i]);
372 if (nodeoff < 0)
373 continue; /* Not found, skip it */
374
375 printf("Found %s node\n", nodes_path[i]);
376
377 rc = fdt_delprop(blob, nodeoff, "cpu-idle-states");
378 if (rc) {
379 printf("Unable to update property %s:%s, err=%s\n",
380 nodes_path[i], "status", fdt_strerror(rc));
381 return rc;
382 }
383
384 printf("Remove %s:%s\n", nodes_path[i],
385 "cpu-idle-states");
386 }
387 }
388
389 return 0;
390}
391#endif
392
Peng Fand2041722019-08-27 06:25:41 +0000393#if defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SYSRESET)
Peng Fanfcdbde72018-01-10 13:20:37 +0800394void reset_cpu(ulong addr)
395{
Peng Fand2041722019-08-27 06:25:41 +0000396 struct watchdog_regs *wdog = (struct watchdog_regs *)addr;
Peng Fanfcdbde72018-01-10 13:20:37 +0800397
Peng Fand2041722019-08-27 06:25:41 +0000398 if (!addr)
399 wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
Peng Fanfcdbde72018-01-10 13:20:37 +0800400
Peng Fand2041722019-08-27 06:25:41 +0000401 /* Clear WDA to trigger WDOG_B immediately */
402 writew((WCR_WDE | WCR_SRS), &wdog->wcr);
403
404 while (1) {
405 /*
406 * spin for .5 seconds before reset
407 */
408 }
Peng Fanfcdbde72018-01-10 13:20:37 +0800409}
Peng Fand2041722019-08-27 06:25:41 +0000410#endif