blob: 9494c73996973b302be81127a4eeeaa10a813ce4 [file] [log] [blame]
Peng Fanbbcd2c42022-07-26 16:40:39 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2022 NXP
4 *
5 * Peng Fan <peng.fan@nxp.com>
6 */
7
Tom Rinidec7ea02024-05-20 13:35:03 -06008#include <config.h>
Peng Fanbbcd2c42022-07-26 16:40:39 +08009#include <cpu_func.h>
10#include <init.h>
11#include <log.h>
12#include <asm/arch/imx-regs.h>
13#include <asm/global_data.h>
14#include <asm/io.h>
15#include <asm/arch/clock.h>
Peng Fan6d929962022-07-26 16:41:03 +080016#include <asm/arch/ccm_regs.h>
Peng Fanbbcd2c42022-07-26 16:40:39 +080017#include <asm/arch/sys_proto.h>
Ye Li62185922022-07-26 16:40:54 +080018#include <asm/arch/trdc.h>
Peng Fanbbcd2c42022-07-26 16:40:39 +080019#include <asm/mach-imx/boot_mode.h>
20#include <asm/mach-imx/syscounter.h>
21#include <asm/armv8/mmu.h>
Peng Fanb1815c42023-04-28 12:08:27 +080022#include <dm/device.h>
23#include <dm/device_compat.h>
Peng Fanbbcd2c42022-07-26 16:40:39 +080024#include <dm/uclass.h>
25#include <env.h>
26#include <env_internal.h>
27#include <errno.h>
28#include <fdt_support.h>
Peng Fan0c2f3682023-04-28 12:08:28 +080029#include <imx_thermal.h>
Peng Fanbbcd2c42022-07-26 16:40:39 +080030#include <linux/bitops.h>
Peng Fan0c2f3682023-04-28 12:08:28 +080031#include <linux/bitfield.h>
32#include <linux/delay.h>
33#include <thermal.h>
Peng Fanbbcd2c42022-07-26 16:40:39 +080034#include <asm/setup.h>
35#include <asm/bootm.h>
36#include <asm/arch-imx/cpu.h>
Peng Fand5c31832023-06-15 18:09:05 +080037#include <asm/mach-imx/ele_api.h>
Ye Li66baefb2023-04-28 12:08:21 +080038#include <fuse.h>
Ye Libb1187b2023-04-28 12:08:45 +080039#include <asm/arch/ddr.h>
Peng Fanbbcd2c42022-07-26 16:40:39 +080040
41DECLARE_GLOBAL_DATA_PTR;
42
Peng Fan5de0fc02022-07-26 16:40:48 +080043struct rom_api *g_rom_api = (struct rom_api *)0x1980;
44
45#ifdef CONFIG_ENV_IS_IN_MMC
46__weak int board_mmc_get_env_dev(int devno)
47{
Peng Fan77b36c62023-04-28 12:08:33 +080048 return devno;
49}
Peng Fan5de0fc02022-07-26 16:40:48 +080050
51int mmc_get_env_dev(void)
52{
Peng Fan5de0fc02022-07-26 16:40:48 +080053 int ret;
54 u32 boot;
55 u16 boot_type;
56 u8 boot_instance;
57
Peng Fanc459b582023-04-28 12:08:34 +080058 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Peng Fan5de0fc02022-07-26 16:40:48 +080059
60 if (ret != ROM_API_OKAY) {
61 puts("ROMAPI: failure at query_boot_info\n");
62 return CONFIG_SYS_MMC_ENV_DEV;
63 }
64
65 boot_type = boot >> 16;
66 boot_instance = (boot >> 8) & 0xff;
67
68 debug("boot_type %d, instance %d\n", boot_type, boot_instance);
69
70 /* If not boot from sd/mmc, use default value */
71 if (boot_type != BOOT_TYPE_SD && boot_type != BOOT_TYPE_MMC)
72 return env_get_ulong("mmcdev", 10, CONFIG_SYS_MMC_ENV_DEV);
73
74 return board_mmc_get_env_dev(boot_instance);
75}
76#endif
77
Peng Fan0c2f3682023-04-28 12:08:28 +080078/*
79 * SPEED_GRADE[5:4] SPEED_GRADE[3:0] MHz
80 * xx 0000 2300
81 * xx 0001 2200
82 * xx 0010 2100
83 * xx 0011 2000
84 * xx 0100 1900
85 * xx 0101 1800
86 * xx 0110 1700
87 * xx 0111 1600
88 * xx 1000 1500
89 * xx 1001 1400
90 * xx 1010 1300
91 * xx 1011 1200
92 * xx 1100 1100
93 * xx 1101 1000
94 * xx 1110 900
95 * xx 1111 800
96 */
97u32 get_cpu_speed_grade_hz(void)
98{
Peng Fan43126e12024-09-19 12:01:23 +080099 int ret;
100 u32 bank, word, speed, max_speed;
Peng Fan0c2f3682023-04-28 12:08:28 +0800101 u32 val;
102
Peng Fan43126e12024-09-19 12:01:23 +0800103 bank = HW_CFG1 / NUM_WORDS_PER_BANK;
104 word = HW_CFG1 % NUM_WORDS_PER_BANK;
105 ret = fuse_read(bank, word, &val);
106 if (ret)
107 val = 0; /* If read fuse failed, return as blank fuse */
108
Peng Fan0c2f3682023-04-28 12:08:28 +0800109 val = FIELD_GET(SPEED_GRADING_MASK, val) & 0xF;
110
111 speed = MHZ(2300) - val * MHZ(100);
112
113 if (is_imx93())
114 max_speed = MHZ(1700);
115
116 /* In case the fuse of speed grade not programmed */
117 if (speed > max_speed)
118 speed = max_speed;
119
120 return speed;
121}
122
123/*
124 * `00` - Consumer 0C to 95C
125 * `01` - Ext. Consumer -20C to 105C
126 * `10` - Industrial -40C to 105C
127 * `11` - Automotive -40C to 125C
128 */
129u32 get_cpu_temp_grade(int *minc, int *maxc)
130{
Peng Fan43126e12024-09-19 12:01:23 +0800131 int ret;
132 u32 bank, word, val;
133
134 bank = HW_CFG1 / NUM_WORDS_PER_BANK;
135 word = HW_CFG1 % NUM_WORDS_PER_BANK;
136 ret = fuse_read(bank, word, &val);
137 if (ret)
138 val = 0; /* If read fuse failed, return as blank fuse */
Peng Fan0c2f3682023-04-28 12:08:28 +0800139
Peng Fan0c2f3682023-04-28 12:08:28 +0800140 val = FIELD_GET(MARKETING_GRADING_MASK, val);
141
142 if (minc && maxc) {
143 if (val == TEMP_AUTOMOTIVE) {
144 *minc = -40;
145 *maxc = 125;
146 } else if (val == TEMP_INDUSTRIAL) {
147 *minc = -40;
148 *maxc = 105;
149 } else if (val == TEMP_EXTCOMMERCIAL) {
150 if (is_imx93()) {
151 /* imx93 only has extended industrial*/
152 *minc = -40;
153 *maxc = 125;
154 } else {
155 *minc = -20;
156 *maxc = 105;
157 }
158 } else {
159 *minc = 0;
160 *maxc = 95;
161 }
162 }
163 return val;
164}
165
Peng Fand5c31832023-06-15 18:09:05 +0800166static void set_cpu_info(struct ele_get_info_data *info)
Peng Fan3700c472022-07-26 16:40:56 +0800167{
168 gd->arch.soc_rev = info->soc;
169 gd->arch.lifecycle = info->lc;
170 memcpy((void *)&gd->arch.uid, &info->uid, 4 * sizeof(u32));
171}
172
Peng Fanc3db3ad2023-04-28 12:08:32 +0800173static u32 get_cpu_variant_type(u32 type)
174{
Peng Fan43126e12024-09-19 12:01:23 +0800175 u32 bank, word, val, val2;
176 int ret;
177
178 bank = HW_CFG1 / NUM_WORDS_PER_BANK;
179 word = HW_CFG1 % NUM_WORDS_PER_BANK;
180 ret = fuse_read(bank, word, &val);
181 if (ret)
182 val = 0; /* If read fuse failed, return as blank fuse */
183
184 bank = HW_CFG2 / NUM_WORDS_PER_BANK;
185 word = HW_CFG2 % NUM_WORDS_PER_BANK;
186 ret = fuse_read(bank, word, &val2);
187 if (ret)
188 val2 = 0; /* If read fuse failed, return as blank fuse */
189
Peng Fanc3db3ad2023-04-28 12:08:32 +0800190 bool npu_disable = !!(val & BIT(13));
191 bool core1_disable = !!(val & BIT(15));
192 u32 pack_9x9_fused = BIT(4) | BIT(17) | BIT(19) | BIT(24);
193
194 if ((val2 & pack_9x9_fused) == pack_9x9_fused)
195 type = MXC_CPU_IMX9322;
196
197 if (npu_disable && core1_disable)
198 return type + 3;
199 else if (npu_disable)
200 return type + 2;
201 else if (core1_disable)
202 return type + 1;
203
204 return type;
205}
206
Peng Fanbbcd2c42022-07-26 16:40:39 +0800207u32 get_cpu_rev(void)
208{
Peng Fan3700c472022-07-26 16:40:56 +0800209 u32 rev = (gd->arch.soc_rev >> 24) - 0xa0;
210
Peng Fanc3db3ad2023-04-28 12:08:32 +0800211 return (get_cpu_variant_type(MXC_CPU_IMX93) << 12) |
212 (CHIP_REV_1_0 + rev);
Peng Fanbbcd2c42022-07-26 16:40:39 +0800213}
214
Ye Li9e19ff92022-07-26 16:40:47 +0800215#define UNLOCK_WORD 0xD928C520 /* unlock word */
216#define REFRESH_WORD 0xB480A602 /* refresh word */
217
218static void disable_wdog(void __iomem *wdog_base)
219{
220 u32 val_cs = readl(wdog_base + 0x00);
221
222 if (!(val_cs & 0x80))
223 return;
224
225 /* default is 32bits cmd */
226 writel(REFRESH_WORD, (wdog_base + 0x04)); /* Refresh the CNT */
227
228 if (!(val_cs & 0x800)) {
229 writel(UNLOCK_WORD, (wdog_base + 0x04));
230 while (!(readl(wdog_base + 0x00) & 0x800))
231 ;
232 }
233 writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */
234 writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */
235 writel(0x2120, (wdog_base + 0x00)); /* Disable it and set update */
236
237 while (!(readl(wdog_base + 0x00) & 0x400))
238 ;
239}
240
241void init_wdog(void)
242{
243 u32 src_val;
244
245 disable_wdog((void __iomem *)WDG3_BASE_ADDR);
246 disable_wdog((void __iomem *)WDG4_BASE_ADDR);
247 disable_wdog((void __iomem *)WDG5_BASE_ADDR);
248
249 src_val = readl(0x54460018); /* reset mask */
250 src_val &= ~0x1c;
251 writel(src_val, 0x54460018);
252}
253
Peng Fanbbcd2c42022-07-26 16:40:39 +0800254static struct mm_region imx93_mem_map[] = {
255 {
256 /* ROM */
257 .virt = 0x0UL,
258 .phys = 0x0UL,
259 .size = 0x100000UL,
260 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
261 PTE_BLOCK_OUTER_SHARE
262 }, {
Peng Fan313af252022-07-26 16:41:04 +0800263 /* TCM */
264 .virt = 0x201c0000UL,
265 .phys = 0x201c0000UL,
266 .size = 0x80000UL,
267 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
268 PTE_BLOCK_NON_SHARE |
269 PTE_BLOCK_PXN | PTE_BLOCK_UXN
270 }, {
Peng Fanbbcd2c42022-07-26 16:40:39 +0800271 /* OCRAM */
272 .virt = 0x20480000UL,
273 .phys = 0x20480000UL,
274 .size = 0xA0000UL,
275 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
276 PTE_BLOCK_OUTER_SHARE
277 }, {
278 /* AIPS */
279 .virt = 0x40000000UL,
280 .phys = 0x40000000UL,
281 .size = 0x40000000UL,
282 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
283 PTE_BLOCK_NON_SHARE |
284 PTE_BLOCK_PXN | PTE_BLOCK_UXN
285 }, {
286 /* Flexible Serial Peripheral Interface */
287 .virt = 0x28000000UL,
288 .phys = 0x28000000UL,
Ye Li35f15512024-03-28 18:49:18 +0800289 .size = 0x08000000UL,
Peng Fanbbcd2c42022-07-26 16:40:39 +0800290 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
291 PTE_BLOCK_NON_SHARE |
292 PTE_BLOCK_PXN | PTE_BLOCK_UXN
293 }, {
294 /* DRAM1 */
295 .virt = 0x80000000UL,
296 .phys = 0x80000000UL,
297 .size = PHYS_SDRAM_SIZE,
298 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
299 PTE_BLOCK_OUTER_SHARE
300 }, {
301 /* empty entrie to split table entry 5 if needed when TEEs are used */
302 0,
303 }, {
304 /* List terminator */
305 0,
306 }
307};
308
309struct mm_region *mem_map = imx93_mem_map;
310
Peng Fan1ed3c0a2023-04-28 12:08:20 +0800311static unsigned int imx9_find_dram_entry_in_mem_map(void)
312{
313 int i;
314
315 for (i = 0; i < ARRAY_SIZE(imx93_mem_map); i++)
316 if (imx93_mem_map[i].phys == CFG_SYS_SDRAM_BASE)
317 return i;
318
319 hang(); /* Entry not found, this must never happen. */
320}
321
322void enable_caches(void)
323{
324 /* If OPTEE runs, remove OPTEE memory from MMU table to avoid speculative prefetch
325 * If OPTEE does not run, still update the MMU table according to dram banks structure
326 * to set correct dram size from board_phys_sdram_size
327 */
328 int i = 0;
329 /*
330 * please make sure that entry initial value matches
331 * imx93_mem_map for DRAM1
332 */
333 int entry = imx9_find_dram_entry_in_mem_map();
334 u64 attrs = imx93_mem_map[entry].attrs;
335
336 while (i < CONFIG_NR_DRAM_BANKS &&
337 entry < ARRAY_SIZE(imx93_mem_map)) {
338 if (gd->bd->bi_dram[i].start == 0)
339 break;
340 imx93_mem_map[entry].phys = gd->bd->bi_dram[i].start;
341 imx93_mem_map[entry].virt = gd->bd->bi_dram[i].start;
342 imx93_mem_map[entry].size = gd->bd->bi_dram[i].size;
343 imx93_mem_map[entry].attrs = attrs;
344 debug("Added memory mapping (%d): %llx %llx\n", entry,
345 imx93_mem_map[entry].phys, imx93_mem_map[entry].size);
346 i++; entry++;
347 }
348
349 icache_enable();
350 dcache_enable();
351}
352
353__weak int board_phys_sdram_size(phys_size_t *size)
354{
Ye Libb1187b2023-04-28 12:08:45 +0800355 phys_size_t start, end;
356 phys_size_t val;
357
Peng Fan1ed3c0a2023-04-28 12:08:20 +0800358 if (!size)
359 return -EINVAL;
360
Ye Libb1187b2023-04-28 12:08:45 +0800361 val = readl(REG_DDR_CS0_BNDS);
362 start = (val >> 16) << 24;
363 end = (val & 0xFFFF);
364 end = end ? end + 1 : 0;
365 end = end << 24;
366 *size = end - start;
Peng Fan1ed3c0a2023-04-28 12:08:20 +0800367
Ye Libb1187b2023-04-28 12:08:45 +0800368 val = readl(REG_DDR_CS1_BNDS);
369 start = (val >> 16) << 24;
370 end = (val & 0xFFFF);
371 end = end ? end + 1 : 0;
372 end = end << 24;
373 *size += end - start;
374
Peng Fan1ed3c0a2023-04-28 12:08:20 +0800375 return 0;
376}
377
Peng Fanbbcd2c42022-07-26 16:40:39 +0800378int dram_init(void)
379{
Peng Fan1ed3c0a2023-04-28 12:08:20 +0800380 phys_size_t sdram_size;
381 int ret;
382
383 ret = board_phys_sdram_size(&sdram_size);
384 if (ret)
385 return ret;
386
387 /* rom_pointer[1] contains the size of TEE occupies */
Elena Popa65c9edb2023-08-08 14:58:26 +0300388 if (!IS_ENABLED(CONFIG_SPL_BUILD) && rom_pointer[1])
Peng Fan1ed3c0a2023-04-28 12:08:20 +0800389 gd->ram_size = sdram_size - rom_pointer[1];
390 else
391 gd->ram_size = sdram_size;
392
393 return 0;
394}
395
396int dram_init_banksize(void)
397{
398 int bank = 0;
399 int ret;
400 phys_size_t sdram_size;
401 phys_size_t sdram_b1_size, sdram_b2_size;
402
403 ret = board_phys_sdram_size(&sdram_size);
404 if (ret)
405 return ret;
406
407 /* Bank 1 can't cross over 4GB space */
408 if (sdram_size > 0x80000000) {
409 sdram_b1_size = 0x80000000;
410 sdram_b2_size = sdram_size - 0x80000000;
411 } else {
412 sdram_b1_size = sdram_size;
413 sdram_b2_size = 0;
414 }
415
416 gd->bd->bi_dram[bank].start = PHYS_SDRAM;
Elena Popa65c9edb2023-08-08 14:58:26 +0300417 if (!IS_ENABLED(CONFIG_SPL_BUILD) && rom_pointer[1]) {
Peng Fan1ed3c0a2023-04-28 12:08:20 +0800418 phys_addr_t optee_start = (phys_addr_t)rom_pointer[0];
419 phys_size_t optee_size = (size_t)rom_pointer[1];
420
421 gd->bd->bi_dram[bank].size = optee_start - gd->bd->bi_dram[bank].start;
422 if ((optee_start + optee_size) < (PHYS_SDRAM + sdram_b1_size)) {
423 if (++bank >= CONFIG_NR_DRAM_BANKS) {
424 puts("CONFIG_NR_DRAM_BANKS is not enough\n");
425 return -1;
426 }
427
428 gd->bd->bi_dram[bank].start = optee_start + optee_size;
429 gd->bd->bi_dram[bank].size = PHYS_SDRAM +
430 sdram_b1_size - gd->bd->bi_dram[bank].start;
431 }
432 } else {
433 gd->bd->bi_dram[bank].size = sdram_b1_size;
434 }
435
436 if (sdram_b2_size) {
437 if (++bank >= CONFIG_NR_DRAM_BANKS) {
438 puts("CONFIG_NR_DRAM_BANKS is not enough for SDRAM_2\n");
439 return -1;
440 }
441 gd->bd->bi_dram[bank].start = 0x100000000UL;
442 gd->bd->bi_dram[bank].size = sdram_b2_size;
443 }
Peng Fanbbcd2c42022-07-26 16:40:39 +0800444
445 return 0;
446}
447
Peng Fan1ed3c0a2023-04-28 12:08:20 +0800448phys_size_t get_effective_memsize(void)
449{
450 int ret;
451 phys_size_t sdram_size;
452 phys_size_t sdram_b1_size;
453
454 ret = board_phys_sdram_size(&sdram_size);
455 if (!ret) {
456 /* Bank 1 can't cross over 4GB space */
457 if (sdram_size > 0x80000000)
458 sdram_b1_size = 0x80000000;
459 else
460 sdram_b1_size = sdram_size;
461
Elena Popa65c9edb2023-08-08 14:58:26 +0300462 if (!IS_ENABLED(CONFIG_SPL_BUILD) && rom_pointer[1]) {
Peng Fan1ed3c0a2023-04-28 12:08:20 +0800463 /* We will relocate u-boot to top of dram1. TEE position has two cases:
464 * 1. At the top of dram1, Then return the size removed optee size.
465 * 2. In the middle of dram1, return the size of dram1.
466 */
467 if ((rom_pointer[0] + rom_pointer[1]) == (PHYS_SDRAM + sdram_b1_size))
468 return ((phys_addr_t)rom_pointer[0] - PHYS_SDRAM);
469 }
470
471 return sdram_b1_size;
472 } else {
473 return PHYS_SDRAM_SIZE;
474 }
475}
476
Peng Fanbbcd2c42022-07-26 16:40:39 +0800477void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
478{
Ye Li66baefb2023-04-28 12:08:21 +0800479 u32 val[2] = {};
480 int ret;
481
482 if (dev_id == 0) {
483 ret = fuse_read(39, 3, &val[0]);
484 if (ret)
485 goto err;
486
487 ret = fuse_read(39, 4, &val[1]);
488 if (ret)
489 goto err;
490
491 mac[0] = val[1] >> 8;
492 mac[1] = val[1];
493 mac[2] = val[0] >> 24;
494 mac[3] = val[0] >> 16;
495 mac[4] = val[0] >> 8;
496 mac[5] = val[0];
497
498 } else {
499 ret = fuse_read(39, 5, &val[0]);
500 if (ret)
501 goto err;
502
503 ret = fuse_read(39, 4, &val[1]);
504 if (ret)
505 goto err;
506
Ye Liae9b7022024-09-19 12:01:24 +0800507 if (is_imx93() && is_soc_rev(CHIP_REV_1_0)) {
508 mac[0] = val[1] >> 24;
509 mac[1] = val[1] >> 16;
510 mac[2] = val[0] >> 24;
511 mac[3] = val[0] >> 16;
512 mac[4] = val[0] >> 8;
513 mac[5] = val[0];
514 } else {
515 mac[0] = val[0] >> 24;
516 mac[1] = val[0] >> 16;
517 mac[2] = val[0] >> 8;
518 mac[3] = val[0];
519 mac[4] = val[1] >> 24;
520 mac[5] = val[1] >> 16;
521 }
Ye Li66baefb2023-04-28 12:08:21 +0800522 }
523
524 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
525 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
526 return;
527err:
528 memset(mac, 0, 6);
529 printf("%s: fuse read err: %d\n", __func__, ret);
Peng Fanbbcd2c42022-07-26 16:40:39 +0800530}
531
532int print_cpuinfo(void)
533{
534 u32 cpurev;
535
536 cpurev = get_cpu_rev();
537
538 printf("CPU: i.MX93 rev%d.%d\n", (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0);
539
540 return 0;
541}
542
Primoz Fiserebbd4fc2024-01-11 13:56:25 +0100543static int fixup_thermal_trips(void *blob, const char *name)
544{
545 int minc, maxc;
546 int node, trip;
547
548 node = fdt_path_offset(blob, "/thermal-zones");
549 if (node < 0)
550 return node;
551
552 node = fdt_subnode_offset(blob, node, name);
553 if (node < 0)
554 return node;
555
556 node = fdt_subnode_offset(blob, node, "trips");
557 if (node < 0)
558 return node;
559
560 get_cpu_temp_grade(&minc, &maxc);
561
562 fdt_for_each_subnode(trip, blob, node) {
563 const char *type;
564 int temp, ret;
565
566 type = fdt_getprop(blob, trip, "type", NULL);
567 if (!type)
568 continue;
569
570 temp = 0;
571 if (!strcmp(type, "critical"))
Primoz Fiserab813e12024-08-13 14:12:17 +0200572 temp = 1000 * maxc;
Primoz Fiserebbd4fc2024-01-11 13:56:25 +0100573 else if (!strcmp(type, "passive"))
574 temp = 1000 * (maxc - 10);
575 if (temp) {
576 ret = fdt_setprop_u32(blob, trip, "temperature", temp);
577 if (ret)
578 return ret;
579 }
580 }
581
582 return 0;
583}
584
Peng Fanbbcd2c42022-07-26 16:40:39 +0800585int ft_system_setup(void *blob, struct bd_info *bd)
586{
Primoz Fiserebbd4fc2024-01-11 13:56:25 +0100587 if (fixup_thermal_trips(blob, "cpu-thermal"))
588 printf("Failed to update cpu-thermal trip(s)");
589
Peng Fanbbcd2c42022-07-26 16:40:39 +0800590 return 0;
591}
Peng Fan3700c472022-07-26 16:40:56 +0800592
593#if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)
594void get_board_serial(struct tag_serialnr *serialnr)
595{
Ye Li8a7f6ef2024-09-19 12:01:22 +0800596 printf("UID: %08x%08x%08x%08x\n", __be32_to_cpu(gd->arch.uid[0]),
597 __be32_to_cpu(gd->arch.uid[1]), __be32_to_cpu(gd->arch.uid[2]),
598 __be32_to_cpu(gd->arch.uid[3]));
Peng Fan3700c472022-07-26 16:40:56 +0800599
Frank Li52b93ea2024-09-19 12:01:21 +0800600 serialnr->low = __be32_to_cpu(gd->arch.uid[1]);
601 serialnr->high = __be32_to_cpu(gd->arch.uid[0]);
Peng Fan3700c472022-07-26 16:40:56 +0800602}
603#endif
Peng Fanbbcd2c42022-07-26 16:40:39 +0800604
Peng Fanb1815c42023-04-28 12:08:27 +0800605static void save_reset_cause(void)
606{
607 struct src_general_regs *src = (struct src_general_regs *)SRC_GLOBAL_RBASE;
608 u32 srsr = readl(&src->srsr);
609
610 /* clear srsr in sec mode */
611 writel(srsr, &src->srsr);
612
613 /* Save value to GPR1 to pass to nonsecure */
614 writel(srsr, &src->gpr[0]);
615}
616
Peng Fanbbcd2c42022-07-26 16:40:39 +0800617int arch_cpu_init(void)
618{
Ye Li9e19ff92022-07-26 16:40:47 +0800619 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
620 /* Disable wdog */
621 init_wdog();
622
Peng Fan28b5cb52022-07-26 16:40:43 +0800623 clock_init();
Ye Li62185922022-07-26 16:40:54 +0800624
625 trdc_early_init();
Peng Fanb1815c42023-04-28 12:08:27 +0800626
627 /* Save SRC SRSR to GPR1 and clear it */
628 save_reset_cause();
Ye Li9e19ff92022-07-26 16:40:47 +0800629 }
Peng Fan28b5cb52022-07-26 16:40:43 +0800630
Peng Fanbbcd2c42022-07-26 16:40:39 +0800631 return 0;
632}
Peng Fan3700c472022-07-26 16:40:56 +0800633
Simon Glassb8357c12023-08-21 21:16:56 -0600634int imx9_probe_mu(void)
Peng Fan3700c472022-07-26 16:40:56 +0800635{
636 struct udevice *devp;
637 int node, ret;
638 u32 res;
Peng Fand5c31832023-06-15 18:09:05 +0800639 struct ele_get_info_data info;
Peng Fan3700c472022-07-26 16:40:56 +0800640
641 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx93-mu-s4");
642
643 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
644 if (ret)
645 return ret;
646
647 if (gd->flags & GD_FLG_RELOC)
648 return 0;
649
Peng Fand5c31832023-06-15 18:09:05 +0800650 ret = ele_get_info(&info, &res);
Peng Fan3700c472022-07-26 16:40:56 +0800651 if (ret)
652 return ret;
653
654 set_cpu_info(&info);
655
656 return 0;
657}
Simon Glassb8357c12023-08-21 21:16:56 -0600658EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_F, imx9_probe_mu);
Ye Lif41ffc12024-04-01 09:41:09 +0800659EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_R, imx9_probe_mu);
Jian Liacf41a32022-07-26 16:40:46 +0800660
661int timer_init(void)
662{
663#ifdef CONFIG_SPL_BUILD
664 struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
665 unsigned long freq = readl(&sctr->cntfid0);
666
667 /* Update with accurate clock frequency */
668 asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
669
670 clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
671 SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
672#endif
673
674 gd->arch.tbl = 0;
675 gd->arch.tbu = 0;
676
677 return 0;
678}
Peng Fan65563792022-07-26 16:41:02 +0800679
Ye Li8e8687c2022-07-26 16:41:05 +0800680enum env_location env_get_location(enum env_operation op, int prio)
681{
682 enum boot_device dev = get_boot_device();
Ye Li8e8687c2022-07-26 16:41:05 +0800683
684 if (prio)
Oleksandr Suvoroveff7c8d2023-04-11 20:27:41 +0300685 return ENVL_UNKNOWN;
Ye Li8e8687c2022-07-26 16:41:05 +0800686
687 switch (dev) {
Ye Li8e8687c2022-07-26 16:41:05 +0800688 case QSPI_BOOT:
Oleksandr Suvoroveff7c8d2023-04-11 20:27:41 +0300689 if (CONFIG_IS_ENABLED(ENV_IS_IN_SPI_FLASH))
690 return ENVL_SPI_FLASH;
691 return ENVL_NOWHERE;
Ye Li8e8687c2022-07-26 16:41:05 +0800692 case SD1_BOOT:
693 case SD2_BOOT:
694 case SD3_BOOT:
695 case MMC1_BOOT:
696 case MMC2_BOOT:
697 case MMC3_BOOT:
Oleksandr Suvoroveff7c8d2023-04-11 20:27:41 +0300698 if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC))
699 return ENVL_MMC;
700 else if (CONFIG_IS_ENABLED(ENV_IS_IN_EXT4))
701 return ENVL_EXT4;
702 else if (CONFIG_IS_ENABLED(ENV_IS_IN_FAT))
703 return ENVL_FAT;
704 return ENVL_NOWHERE;
Ye Li8e8687c2022-07-26 16:41:05 +0800705 default:
Oleksandr Suvoroveff7c8d2023-04-11 20:27:41 +0300706 return ENVL_NOWHERE;
Ye Li8e8687c2022-07-26 16:41:05 +0800707 }
Ye Li8e8687c2022-07-26 16:41:05 +0800708}
709
Peng Fan65563792022-07-26 16:41:02 +0800710static int mix_power_init(enum mix_power_domain pd)
711{
712 enum src_mix_slice_id mix_id;
713 enum src_mem_slice_id mem_id;
714 struct src_mix_slice_regs *mix_regs;
715 struct src_mem_slice_regs *mem_regs;
716 struct src_general_regs *global_regs;
717 u32 scr, val;
718
719 switch (pd) {
720 case MIX_PD_MEDIAMIX:
721 mix_id = SRC_MIX_MEDIA;
722 mem_id = SRC_MEM_MEDIA;
723 scr = BIT(5);
724
Peng Fand5c31832023-06-15 18:09:05 +0800725 /* Enable ELE handshake */
Peng Fan65563792022-07-26 16:41:02 +0800726 struct blk_ctrl_s_aonmix_regs *s_regs =
727 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
728
729 setbits_le32(&s_regs->lp_handshake[0], BIT(13));
730 break;
731 case MIX_PD_MLMIX:
732 mix_id = SRC_MIX_ML;
733 mem_id = SRC_MEM_ML;
734 scr = BIT(4);
735 break;
736 case MIX_PD_DDRMIX:
737 mix_id = SRC_MIX_DDRMIX;
738 mem_id = SRC_MEM_DDRMIX;
739 scr = BIT(6);
740 break;
741 default:
742 return -EINVAL;
743 }
744
745 mix_regs = (struct src_mix_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x400 * (mix_id + 1));
746 mem_regs =
747 (struct src_mem_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x3800 + 0x400 * mem_id);
748 global_regs = (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
749
750 /* Allow NS to set it */
751 setbits_le32(&mix_regs->authen_ctrl, BIT(9));
752
753 clrsetbits_le32(&mix_regs->psw_ack_ctrl[0], BIT(28), BIT(29));
754
755 /* mix reset will be held until boot core write this bit to 1 */
756 setbits_le32(&global_regs->scr, scr);
757
758 /* Enable mem in Low power auto sequence */
759 setbits_le32(&mem_regs->mem_ctrl, BIT(2));
760
761 /* Set the power down state */
762 val = readl(&mix_regs->func_stat);
763 if (val & SRC_MIX_SLICE_FUNC_STAT_PSW_STAT) {
764 /* The mix is default power off, power down it to make PDN_SFT bit
765 * aligned with FUNC STAT
766 */
767 setbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
768 val = readl(&mix_regs->func_stat);
769
770 /* Since PSW_STAT is 1, can't be used for power off status (SW_CTRL BIT31 set)) */
771 /* Check the MEM STAT change to ensure SSAR is completed */
772 while (!(val & SRC_MIX_SLICE_FUNC_STAT_MEM_STAT))
773 val = readl(&mix_regs->func_stat);
774
775 /* wait few ipg clock cycles to ensure FSM done and power off status is correct */
776 /* About 5 cycles at 24Mhz, 1us is enough */
777 udelay(1);
778 } else {
779 /* The mix is default power on, Do mix power cycle */
780 setbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
781 val = readl(&mix_regs->func_stat);
782 while (!(val & SRC_MIX_SLICE_FUNC_STAT_PSW_STAT))
783 val = readl(&mix_regs->func_stat);
784 }
785
786 /* power on */
787 clrbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
788 val = readl(&mix_regs->func_stat);
Peng Fan06e78ff2024-09-19 12:01:19 +0800789 while (val & SRC_MIX_SLICE_FUNC_STAT_SSAR_STAT)
Peng Fan65563792022-07-26 16:41:02 +0800790 val = readl(&mix_regs->func_stat);
791
792 return 0;
793}
794
795void disable_isolation(void)
796{
797 struct src_general_regs *global_regs = (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
798 /* clear isolation for usbphy, dsi, csi*/
799 writel(0x0, &global_regs->sp_iso_ctrl);
800}
801
802void soc_power_init(void)
803{
804 mix_power_init(MIX_PD_MEDIAMIX);
805 mix_power_init(MIX_PD_MLMIX);
806
807 disable_isolation();
808}
Peng Fan6d929962022-07-26 16:41:03 +0800809
Peng Fan313af252022-07-26 16:41:04 +0800810bool m33_is_rom_kicked(void)
Peng Fan6d929962022-07-26 16:41:03 +0800811{
812 struct blk_ctrl_s_aonmix_regs *s_regs =
813 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
814
815 if (!(readl(&s_regs->m33_cfg) & BIT(2)))
816 return true;
817
818 return false;
819}
820
821int m33_prepare(void)
822{
823 struct src_mix_slice_regs *mix_regs =
824 (struct src_mix_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x400 * (SRC_MIX_CM33 + 1));
825 struct src_general_regs *global_regs =
826 (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
827 struct blk_ctrl_s_aonmix_regs *s_regs =
828 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
Ye Li0667ec92024-09-19 12:01:20 +0800829 u32 val, i;
Peng Fan6d929962022-07-26 16:41:03 +0800830
831 if (m33_is_rom_kicked())
832 return -EPERM;
833
834 /* Release reset of M33 */
835 setbits_le32(&global_regs->scr, BIT(0));
836
837 /* Check the reset released in M33 MIX func stat */
838 val = readl(&mix_regs->func_stat);
839 while (!(val & SRC_MIX_SLICE_FUNC_STAT_RST_STAT))
840 val = readl(&mix_regs->func_stat);
841
Peng Fand5c31832023-06-15 18:09:05 +0800842 /* Release ELE TROUT */
843 ele_release_m33_trout();
Peng Fan6d929962022-07-26 16:41:03 +0800844
845 /* Mask WDOG1 IRQ from A55, we use it for M33 reset */
846 setbits_le32(&s_regs->ca55_irq_mask[1], BIT(6));
847
848 /* Turn on WDOG1 clock */
849 ccm_lpcg_on(CCGR_WDG1, 1);
850
Peng Fand5c31832023-06-15 18:09:05 +0800851 /* Set ELE LP handshake for M33 reset */
Peng Fan6d929962022-07-26 16:41:03 +0800852 setbits_le32(&s_regs->lp_handshake[0], BIT(6));
853
Ye Li0667ec92024-09-19 12:01:20 +0800854 /* OSCCA enabled, reconfigure TRDC for TCM access, otherwise ECC init will raise error */
855 val = readl(BLK_CTRL_NS_ANOMIX_BASE_ADDR + 0x28);
856 if (val & BIT(0)) {
857 trdc_mbc_set_control(0x44270000, 1, 0, 0x6600);
858
859 for (i = 0; i < 32; i++)
860 trdc_mbc_blk_config(0x44270000, 1, 3, 0, i, true, 0);
861
862 for (i = 0; i < 32; i++)
863 trdc_mbc_blk_config(0x44270000, 1, 3, 1, i, true, 0);
864 }
865
Peng Fan6d929962022-07-26 16:41:03 +0800866 /* Clear M33 TCM for ECC */
867 memset((void *)(ulong)0x201e0000, 0, 0x40000);
868
869 return 0;
870}
Peng Fanb1815c42023-04-28 12:08:27 +0800871
872int psci_sysreset_get_status(struct udevice *dev, char *buf, int size)
873{
874 static const char *reset_cause[] = {
875 "POR ",
876 "JTAG ",
877 "IPP USER ",
878 "WDOG1 ",
879 "WDOG2 ",
880 "WDOG3 ",
881 "WDOG4 ",
882 "WDOG5 ",
883 "TEMPSENSE ",
884 "CSU ",
885 "JTAG_SW ",
886 "M33_REQ ",
887 "M33_LOCKUP ",
888 "UNK ",
889 "UNK ",
890 "UNK "
891 };
892
893 struct src_general_regs *src = (struct src_general_regs *)SRC_GLOBAL_RBASE;
894 u32 srsr;
895 u32 i;
896 int res;
897
898 srsr = readl(&src->gpr[0]);
899
900 for (i = ARRAY_SIZE(reset_cause); i > 0; i--) {
901 if (srsr & (BIT(i - 1)))
902 break;
903 }
904
905 res = snprintf(buf, size, "Reset Status: %s\n", i ? reset_cause[i - 1] : "unknown reset");
906 if (res < 0) {
907 dev_err(dev, "Could not write reset status message (err = %d)\n", res);
908 return -EIO;
909 }
910
911 return 0;
912}