blob: 5699623027eec0d60b40d8ac1ec135efa9c0ea55 [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
507 mac[0] = val[1] >> 24;
508 mac[1] = val[1] >> 16;
509 mac[2] = val[0] >> 24;
510 mac[3] = val[0] >> 16;
511 mac[4] = val[0] >> 8;
512 mac[5] = val[0];
513 }
514
515 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
516 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
517 return;
518err:
519 memset(mac, 0, 6);
520 printf("%s: fuse read err: %d\n", __func__, ret);
Peng Fanbbcd2c42022-07-26 16:40:39 +0800521}
522
523int print_cpuinfo(void)
524{
525 u32 cpurev;
526
527 cpurev = get_cpu_rev();
528
529 printf("CPU: i.MX93 rev%d.%d\n", (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0);
530
531 return 0;
532}
533
Primoz Fiserebbd4fc2024-01-11 13:56:25 +0100534static int fixup_thermal_trips(void *blob, const char *name)
535{
536 int minc, maxc;
537 int node, trip;
538
539 node = fdt_path_offset(blob, "/thermal-zones");
540 if (node < 0)
541 return node;
542
543 node = fdt_subnode_offset(blob, node, name);
544 if (node < 0)
545 return node;
546
547 node = fdt_subnode_offset(blob, node, "trips");
548 if (node < 0)
549 return node;
550
551 get_cpu_temp_grade(&minc, &maxc);
552
553 fdt_for_each_subnode(trip, blob, node) {
554 const char *type;
555 int temp, ret;
556
557 type = fdt_getprop(blob, trip, "type", NULL);
558 if (!type)
559 continue;
560
561 temp = 0;
562 if (!strcmp(type, "critical"))
Primoz Fiserab813e12024-08-13 14:12:17 +0200563 temp = 1000 * maxc;
Primoz Fiserebbd4fc2024-01-11 13:56:25 +0100564 else if (!strcmp(type, "passive"))
565 temp = 1000 * (maxc - 10);
566 if (temp) {
567 ret = fdt_setprop_u32(blob, trip, "temperature", temp);
568 if (ret)
569 return ret;
570 }
571 }
572
573 return 0;
574}
575
Peng Fanbbcd2c42022-07-26 16:40:39 +0800576int ft_system_setup(void *blob, struct bd_info *bd)
577{
Primoz Fiserebbd4fc2024-01-11 13:56:25 +0100578 if (fixup_thermal_trips(blob, "cpu-thermal"))
579 printf("Failed to update cpu-thermal trip(s)");
580
Peng Fanbbcd2c42022-07-26 16:40:39 +0800581 return 0;
582}
Peng Fan3700c472022-07-26 16:40:56 +0800583
584#if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)
585void get_board_serial(struct tag_serialnr *serialnr)
586{
Ye Li8a7f6ef2024-09-19 12:01:22 +0800587 printf("UID: %08x%08x%08x%08x\n", __be32_to_cpu(gd->arch.uid[0]),
588 __be32_to_cpu(gd->arch.uid[1]), __be32_to_cpu(gd->arch.uid[2]),
589 __be32_to_cpu(gd->arch.uid[3]));
Peng Fan3700c472022-07-26 16:40:56 +0800590
Frank Li52b93ea2024-09-19 12:01:21 +0800591 serialnr->low = __be32_to_cpu(gd->arch.uid[1]);
592 serialnr->high = __be32_to_cpu(gd->arch.uid[0]);
Peng Fan3700c472022-07-26 16:40:56 +0800593}
594#endif
Peng Fanbbcd2c42022-07-26 16:40:39 +0800595
Peng Fanb1815c42023-04-28 12:08:27 +0800596static void save_reset_cause(void)
597{
598 struct src_general_regs *src = (struct src_general_regs *)SRC_GLOBAL_RBASE;
599 u32 srsr = readl(&src->srsr);
600
601 /* clear srsr in sec mode */
602 writel(srsr, &src->srsr);
603
604 /* Save value to GPR1 to pass to nonsecure */
605 writel(srsr, &src->gpr[0]);
606}
607
Peng Fanbbcd2c42022-07-26 16:40:39 +0800608int arch_cpu_init(void)
609{
Ye Li9e19ff92022-07-26 16:40:47 +0800610 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
611 /* Disable wdog */
612 init_wdog();
613
Peng Fan28b5cb52022-07-26 16:40:43 +0800614 clock_init();
Ye Li62185922022-07-26 16:40:54 +0800615
616 trdc_early_init();
Peng Fanb1815c42023-04-28 12:08:27 +0800617
618 /* Save SRC SRSR to GPR1 and clear it */
619 save_reset_cause();
Ye Li9e19ff92022-07-26 16:40:47 +0800620 }
Peng Fan28b5cb52022-07-26 16:40:43 +0800621
Peng Fanbbcd2c42022-07-26 16:40:39 +0800622 return 0;
623}
Peng Fan3700c472022-07-26 16:40:56 +0800624
Simon Glassb8357c12023-08-21 21:16:56 -0600625int imx9_probe_mu(void)
Peng Fan3700c472022-07-26 16:40:56 +0800626{
627 struct udevice *devp;
628 int node, ret;
629 u32 res;
Peng Fand5c31832023-06-15 18:09:05 +0800630 struct ele_get_info_data info;
Peng Fan3700c472022-07-26 16:40:56 +0800631
632 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx93-mu-s4");
633
634 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
635 if (ret)
636 return ret;
637
638 if (gd->flags & GD_FLG_RELOC)
639 return 0;
640
Peng Fand5c31832023-06-15 18:09:05 +0800641 ret = ele_get_info(&info, &res);
Peng Fan3700c472022-07-26 16:40:56 +0800642 if (ret)
643 return ret;
644
645 set_cpu_info(&info);
646
647 return 0;
648}
Simon Glassb8357c12023-08-21 21:16:56 -0600649EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_F, imx9_probe_mu);
Ye Lif41ffc12024-04-01 09:41:09 +0800650EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_R, imx9_probe_mu);
Jian Liacf41a32022-07-26 16:40:46 +0800651
652int timer_init(void)
653{
654#ifdef CONFIG_SPL_BUILD
655 struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
656 unsigned long freq = readl(&sctr->cntfid0);
657
658 /* Update with accurate clock frequency */
659 asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
660
661 clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
662 SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
663#endif
664
665 gd->arch.tbl = 0;
666 gd->arch.tbu = 0;
667
668 return 0;
669}
Peng Fan65563792022-07-26 16:41:02 +0800670
Ye Li8e8687c2022-07-26 16:41:05 +0800671enum env_location env_get_location(enum env_operation op, int prio)
672{
673 enum boot_device dev = get_boot_device();
Ye Li8e8687c2022-07-26 16:41:05 +0800674
675 if (prio)
Oleksandr Suvoroveff7c8d2023-04-11 20:27:41 +0300676 return ENVL_UNKNOWN;
Ye Li8e8687c2022-07-26 16:41:05 +0800677
678 switch (dev) {
Ye Li8e8687c2022-07-26 16:41:05 +0800679 case QSPI_BOOT:
Oleksandr Suvoroveff7c8d2023-04-11 20:27:41 +0300680 if (CONFIG_IS_ENABLED(ENV_IS_IN_SPI_FLASH))
681 return ENVL_SPI_FLASH;
682 return ENVL_NOWHERE;
Ye Li8e8687c2022-07-26 16:41:05 +0800683 case SD1_BOOT:
684 case SD2_BOOT:
685 case SD3_BOOT:
686 case MMC1_BOOT:
687 case MMC2_BOOT:
688 case MMC3_BOOT:
Oleksandr Suvoroveff7c8d2023-04-11 20:27:41 +0300689 if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC))
690 return ENVL_MMC;
691 else if (CONFIG_IS_ENABLED(ENV_IS_IN_EXT4))
692 return ENVL_EXT4;
693 else if (CONFIG_IS_ENABLED(ENV_IS_IN_FAT))
694 return ENVL_FAT;
695 return ENVL_NOWHERE;
Ye Li8e8687c2022-07-26 16:41:05 +0800696 default:
Oleksandr Suvoroveff7c8d2023-04-11 20:27:41 +0300697 return ENVL_NOWHERE;
Ye Li8e8687c2022-07-26 16:41:05 +0800698 }
Ye Li8e8687c2022-07-26 16:41:05 +0800699}
700
Peng Fan65563792022-07-26 16:41:02 +0800701static int mix_power_init(enum mix_power_domain pd)
702{
703 enum src_mix_slice_id mix_id;
704 enum src_mem_slice_id mem_id;
705 struct src_mix_slice_regs *mix_regs;
706 struct src_mem_slice_regs *mem_regs;
707 struct src_general_regs *global_regs;
708 u32 scr, val;
709
710 switch (pd) {
711 case MIX_PD_MEDIAMIX:
712 mix_id = SRC_MIX_MEDIA;
713 mem_id = SRC_MEM_MEDIA;
714 scr = BIT(5);
715
Peng Fand5c31832023-06-15 18:09:05 +0800716 /* Enable ELE handshake */
Peng Fan65563792022-07-26 16:41:02 +0800717 struct blk_ctrl_s_aonmix_regs *s_regs =
718 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
719
720 setbits_le32(&s_regs->lp_handshake[0], BIT(13));
721 break;
722 case MIX_PD_MLMIX:
723 mix_id = SRC_MIX_ML;
724 mem_id = SRC_MEM_ML;
725 scr = BIT(4);
726 break;
727 case MIX_PD_DDRMIX:
728 mix_id = SRC_MIX_DDRMIX;
729 mem_id = SRC_MEM_DDRMIX;
730 scr = BIT(6);
731 break;
732 default:
733 return -EINVAL;
734 }
735
736 mix_regs = (struct src_mix_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x400 * (mix_id + 1));
737 mem_regs =
738 (struct src_mem_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x3800 + 0x400 * mem_id);
739 global_regs = (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
740
741 /* Allow NS to set it */
742 setbits_le32(&mix_regs->authen_ctrl, BIT(9));
743
744 clrsetbits_le32(&mix_regs->psw_ack_ctrl[0], BIT(28), BIT(29));
745
746 /* mix reset will be held until boot core write this bit to 1 */
747 setbits_le32(&global_regs->scr, scr);
748
749 /* Enable mem in Low power auto sequence */
750 setbits_le32(&mem_regs->mem_ctrl, BIT(2));
751
752 /* Set the power down state */
753 val = readl(&mix_regs->func_stat);
754 if (val & SRC_MIX_SLICE_FUNC_STAT_PSW_STAT) {
755 /* The mix is default power off, power down it to make PDN_SFT bit
756 * aligned with FUNC STAT
757 */
758 setbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
759 val = readl(&mix_regs->func_stat);
760
761 /* Since PSW_STAT is 1, can't be used for power off status (SW_CTRL BIT31 set)) */
762 /* Check the MEM STAT change to ensure SSAR is completed */
763 while (!(val & SRC_MIX_SLICE_FUNC_STAT_MEM_STAT))
764 val = readl(&mix_regs->func_stat);
765
766 /* wait few ipg clock cycles to ensure FSM done and power off status is correct */
767 /* About 5 cycles at 24Mhz, 1us is enough */
768 udelay(1);
769 } else {
770 /* The mix is default power on, Do mix power cycle */
771 setbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
772 val = readl(&mix_regs->func_stat);
773 while (!(val & SRC_MIX_SLICE_FUNC_STAT_PSW_STAT))
774 val = readl(&mix_regs->func_stat);
775 }
776
777 /* power on */
778 clrbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
779 val = readl(&mix_regs->func_stat);
Peng Fan06e78ff2024-09-19 12:01:19 +0800780 while (val & SRC_MIX_SLICE_FUNC_STAT_SSAR_STAT)
Peng Fan65563792022-07-26 16:41:02 +0800781 val = readl(&mix_regs->func_stat);
782
783 return 0;
784}
785
786void disable_isolation(void)
787{
788 struct src_general_regs *global_regs = (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
789 /* clear isolation for usbphy, dsi, csi*/
790 writel(0x0, &global_regs->sp_iso_ctrl);
791}
792
793void soc_power_init(void)
794{
795 mix_power_init(MIX_PD_MEDIAMIX);
796 mix_power_init(MIX_PD_MLMIX);
797
798 disable_isolation();
799}
Peng Fan6d929962022-07-26 16:41:03 +0800800
Peng Fan313af252022-07-26 16:41:04 +0800801bool m33_is_rom_kicked(void)
Peng Fan6d929962022-07-26 16:41:03 +0800802{
803 struct blk_ctrl_s_aonmix_regs *s_regs =
804 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
805
806 if (!(readl(&s_regs->m33_cfg) & BIT(2)))
807 return true;
808
809 return false;
810}
811
812int m33_prepare(void)
813{
814 struct src_mix_slice_regs *mix_regs =
815 (struct src_mix_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x400 * (SRC_MIX_CM33 + 1));
816 struct src_general_regs *global_regs =
817 (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
818 struct blk_ctrl_s_aonmix_regs *s_regs =
819 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
Ye Li0667ec92024-09-19 12:01:20 +0800820 u32 val, i;
Peng Fan6d929962022-07-26 16:41:03 +0800821
822 if (m33_is_rom_kicked())
823 return -EPERM;
824
825 /* Release reset of M33 */
826 setbits_le32(&global_regs->scr, BIT(0));
827
828 /* Check the reset released in M33 MIX func stat */
829 val = readl(&mix_regs->func_stat);
830 while (!(val & SRC_MIX_SLICE_FUNC_STAT_RST_STAT))
831 val = readl(&mix_regs->func_stat);
832
Peng Fand5c31832023-06-15 18:09:05 +0800833 /* Release ELE TROUT */
834 ele_release_m33_trout();
Peng Fan6d929962022-07-26 16:41:03 +0800835
836 /* Mask WDOG1 IRQ from A55, we use it for M33 reset */
837 setbits_le32(&s_regs->ca55_irq_mask[1], BIT(6));
838
839 /* Turn on WDOG1 clock */
840 ccm_lpcg_on(CCGR_WDG1, 1);
841
Peng Fand5c31832023-06-15 18:09:05 +0800842 /* Set ELE LP handshake for M33 reset */
Peng Fan6d929962022-07-26 16:41:03 +0800843 setbits_le32(&s_regs->lp_handshake[0], BIT(6));
844
Ye Li0667ec92024-09-19 12:01:20 +0800845 /* OSCCA enabled, reconfigure TRDC for TCM access, otherwise ECC init will raise error */
846 val = readl(BLK_CTRL_NS_ANOMIX_BASE_ADDR + 0x28);
847 if (val & BIT(0)) {
848 trdc_mbc_set_control(0x44270000, 1, 0, 0x6600);
849
850 for (i = 0; i < 32; i++)
851 trdc_mbc_blk_config(0x44270000, 1, 3, 0, i, true, 0);
852
853 for (i = 0; i < 32; i++)
854 trdc_mbc_blk_config(0x44270000, 1, 3, 1, i, true, 0);
855 }
856
Peng Fan6d929962022-07-26 16:41:03 +0800857 /* Clear M33 TCM for ECC */
858 memset((void *)(ulong)0x201e0000, 0, 0x40000);
859
860 return 0;
861}
Peng Fanb1815c42023-04-28 12:08:27 +0800862
863int psci_sysreset_get_status(struct udevice *dev, char *buf, int size)
864{
865 static const char *reset_cause[] = {
866 "POR ",
867 "JTAG ",
868 "IPP USER ",
869 "WDOG1 ",
870 "WDOG2 ",
871 "WDOG3 ",
872 "WDOG4 ",
873 "WDOG5 ",
874 "TEMPSENSE ",
875 "CSU ",
876 "JTAG_SW ",
877 "M33_REQ ",
878 "M33_LOCKUP ",
879 "UNK ",
880 "UNK ",
881 "UNK "
882 };
883
884 struct src_general_regs *src = (struct src_general_regs *)SRC_GLOBAL_RBASE;
885 u32 srsr;
886 u32 i;
887 int res;
888
889 srsr = readl(&src->gpr[0]);
890
891 for (i = ARRAY_SIZE(reset_cause); i > 0; i--) {
892 if (srsr & (BIT(i - 1)))
893 break;
894 }
895
896 res = snprintf(buf, size, "Reset Status: %s\n", i ? reset_cause[i - 1] : "unknown reset");
897 if (res < 0) {
898 dev_err(dev, "Could not write reset status message (err = %d)\n", res);
899 return -EIO;
900 }
901
902 return 0;
903}