blob: 797d7a802bafc5ea5df80a9f038b328ff766abd0 [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
8#include <common.h>
9#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>
22#include <dm/uclass.h>
23#include <env.h>
24#include <env_internal.h>
25#include <errno.h>
26#include <fdt_support.h>
27#include <linux/bitops.h>
28#include <asm/setup.h>
29#include <asm/bootm.h>
30#include <asm/arch-imx/cpu.h>
Peng Fan3700c472022-07-26 16:40:56 +080031#include <asm/mach-imx/s400_api.h>
Peng Fan65563792022-07-26 16:41:02 +080032#include <linux/delay.h>
Peng Fanbbcd2c42022-07-26 16:40:39 +080033
34DECLARE_GLOBAL_DATA_PTR;
35
Peng Fan5de0fc02022-07-26 16:40:48 +080036struct rom_api *g_rom_api = (struct rom_api *)0x1980;
37
38#ifdef CONFIG_ENV_IS_IN_MMC
39__weak int board_mmc_get_env_dev(int devno)
40{
41 return devno; }
42
43int mmc_get_env_dev(void)
44{
45 volatile gd_t *pgd = gd;
46 int ret;
47 u32 boot;
48 u16 boot_type;
49 u8 boot_instance;
50
51 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
52 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
53 set_gd(pgd);
54
55 if (ret != ROM_API_OKAY) {
56 puts("ROMAPI: failure at query_boot_info\n");
57 return CONFIG_SYS_MMC_ENV_DEV;
58 }
59
60 boot_type = boot >> 16;
61 boot_instance = (boot >> 8) & 0xff;
62
63 debug("boot_type %d, instance %d\n", boot_type, boot_instance);
64
65 /* If not boot from sd/mmc, use default value */
66 if (boot_type != BOOT_TYPE_SD && boot_type != BOOT_TYPE_MMC)
67 return env_get_ulong("mmcdev", 10, CONFIG_SYS_MMC_ENV_DEV);
68
69 return board_mmc_get_env_dev(boot_instance);
70}
71#endif
72
Peng Fan3700c472022-07-26 16:40:56 +080073static void set_cpu_info(struct sentinel_get_info_data *info)
74{
75 gd->arch.soc_rev = info->soc;
76 gd->arch.lifecycle = info->lc;
77 memcpy((void *)&gd->arch.uid, &info->uid, 4 * sizeof(u32));
78}
79
Peng Fanbbcd2c42022-07-26 16:40:39 +080080u32 get_cpu_rev(void)
81{
Peng Fan3700c472022-07-26 16:40:56 +080082 u32 rev = (gd->arch.soc_rev >> 24) - 0xa0;
83
84 return (MXC_CPU_IMX93 << 12) | (CHIP_REV_1_0 + rev);
Peng Fanbbcd2c42022-07-26 16:40:39 +080085}
86
Ye Li9e19ff92022-07-26 16:40:47 +080087#define UNLOCK_WORD 0xD928C520 /* unlock word */
88#define REFRESH_WORD 0xB480A602 /* refresh word */
89
90static void disable_wdog(void __iomem *wdog_base)
91{
92 u32 val_cs = readl(wdog_base + 0x00);
93
94 if (!(val_cs & 0x80))
95 return;
96
97 /* default is 32bits cmd */
98 writel(REFRESH_WORD, (wdog_base + 0x04)); /* Refresh the CNT */
99
100 if (!(val_cs & 0x800)) {
101 writel(UNLOCK_WORD, (wdog_base + 0x04));
102 while (!(readl(wdog_base + 0x00) & 0x800))
103 ;
104 }
105 writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */
106 writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */
107 writel(0x2120, (wdog_base + 0x00)); /* Disable it and set update */
108
109 while (!(readl(wdog_base + 0x00) & 0x400))
110 ;
111}
112
113void init_wdog(void)
114{
115 u32 src_val;
116
117 disable_wdog((void __iomem *)WDG3_BASE_ADDR);
118 disable_wdog((void __iomem *)WDG4_BASE_ADDR);
119 disable_wdog((void __iomem *)WDG5_BASE_ADDR);
120
121 src_val = readl(0x54460018); /* reset mask */
122 src_val &= ~0x1c;
123 writel(src_val, 0x54460018);
124}
125
Peng Fanbbcd2c42022-07-26 16:40:39 +0800126static struct mm_region imx93_mem_map[] = {
127 {
128 /* ROM */
129 .virt = 0x0UL,
130 .phys = 0x0UL,
131 .size = 0x100000UL,
132 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
133 PTE_BLOCK_OUTER_SHARE
134 }, {
Peng Fan313af252022-07-26 16:41:04 +0800135 /* TCM */
136 .virt = 0x201c0000UL,
137 .phys = 0x201c0000UL,
138 .size = 0x80000UL,
139 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
140 PTE_BLOCK_NON_SHARE |
141 PTE_BLOCK_PXN | PTE_BLOCK_UXN
142 }, {
Peng Fanbbcd2c42022-07-26 16:40:39 +0800143 /* OCRAM */
144 .virt = 0x20480000UL,
145 .phys = 0x20480000UL,
146 .size = 0xA0000UL,
147 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
148 PTE_BLOCK_OUTER_SHARE
149 }, {
150 /* AIPS */
151 .virt = 0x40000000UL,
152 .phys = 0x40000000UL,
153 .size = 0x40000000UL,
154 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
155 PTE_BLOCK_NON_SHARE |
156 PTE_BLOCK_PXN | PTE_BLOCK_UXN
157 }, {
158 /* Flexible Serial Peripheral Interface */
159 .virt = 0x28000000UL,
160 .phys = 0x28000000UL,
161 .size = 0x30000000UL,
162 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
163 PTE_BLOCK_NON_SHARE |
164 PTE_BLOCK_PXN | PTE_BLOCK_UXN
165 }, {
166 /* DRAM1 */
167 .virt = 0x80000000UL,
168 .phys = 0x80000000UL,
169 .size = PHYS_SDRAM_SIZE,
170 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
171 PTE_BLOCK_OUTER_SHARE
172 }, {
173 /* empty entrie to split table entry 5 if needed when TEEs are used */
174 0,
175 }, {
176 /* List terminator */
177 0,
178 }
179};
180
181struct mm_region *mem_map = imx93_mem_map;
182
183int dram_init(void)
184{
185 gd->ram_size = PHYS_SDRAM_SIZE;
186
187 return 0;
188}
189
190void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
191{
192 mac[0] = 0x1;
193 mac[1] = 0x2;
194 mac[2] = 0x3;
195 mac[3] = 0x4;
196 mac[4] = 0x5;
197 mac[5] = 0x6;
198}
199
200int print_cpuinfo(void)
201{
202 u32 cpurev;
203
204 cpurev = get_cpu_rev();
205
206 printf("CPU: i.MX93 rev%d.%d\n", (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0);
207
208 return 0;
209}
210
211int arch_misc_init(void)
212{
213 return 0;
214}
215
216int ft_system_setup(void *blob, struct bd_info *bd)
217{
218 return 0;
219}
Peng Fan3700c472022-07-26 16:40:56 +0800220
221#if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)
222void get_board_serial(struct tag_serialnr *serialnr)
223{
224 printf("UID: 0x%x 0x%x 0x%x 0x%x\n",
225 gd->arch.uid[0], gd->arch.uid[1], gd->arch.uid[2], gd->arch.uid[3]);
226
227 serialnr->low = gd->arch.uid[0];
228 serialnr->high = gd->arch.uid[3];
229}
230#endif
Peng Fanbbcd2c42022-07-26 16:40:39 +0800231
232int arch_cpu_init(void)
233{
Ye Li9e19ff92022-07-26 16:40:47 +0800234 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
235 /* Disable wdog */
236 init_wdog();
237
Peng Fan28b5cb52022-07-26 16:40:43 +0800238 clock_init();
Ye Li62185922022-07-26 16:40:54 +0800239
240 trdc_early_init();
Ye Li9e19ff92022-07-26 16:40:47 +0800241 }
Peng Fan28b5cb52022-07-26 16:40:43 +0800242
Peng Fanbbcd2c42022-07-26 16:40:39 +0800243 return 0;
244}
Peng Fan3700c472022-07-26 16:40:56 +0800245
246int imx9_probe_mu(void *ctx, struct event *event)
247{
248 struct udevice *devp;
249 int node, ret;
250 u32 res;
251 struct sentinel_get_info_data info;
252
253 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx93-mu-s4");
254
255 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
256 if (ret)
257 return ret;
258
259 if (gd->flags & GD_FLG_RELOC)
260 return 0;
261
262 ret = ahab_get_info(&info, &res);
263 if (ret)
264 return ret;
265
266 set_cpu_info(&info);
267
268 return 0;
269}
270EVENT_SPY(EVT_DM_POST_INIT, imx9_probe_mu);
Jian Liacf41a32022-07-26 16:40:46 +0800271
272int timer_init(void)
273{
274#ifdef CONFIG_SPL_BUILD
275 struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
276 unsigned long freq = readl(&sctr->cntfid0);
277
278 /* Update with accurate clock frequency */
279 asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
280
281 clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
282 SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
283#endif
284
285 gd->arch.tbl = 0;
286 gd->arch.tbu = 0;
287
288 return 0;
289}
Peng Fan65563792022-07-26 16:41:02 +0800290
Ye Li8e8687c2022-07-26 16:41:05 +0800291enum env_location env_get_location(enum env_operation op, int prio)
292{
293 enum boot_device dev = get_boot_device();
294 enum env_location env_loc = ENVL_UNKNOWN;
295
296 if (prio)
297 return env_loc;
298
299 switch (dev) {
300#if defined(CONFIG_ENV_IS_IN_SPI_FLASH)
301 case QSPI_BOOT:
302 env_loc = ENVL_SPI_FLASH;
303 break;
304#endif
305#if defined(CONFIG_ENV_IS_IN_MMC)
306 case SD1_BOOT:
307 case SD2_BOOT:
308 case SD3_BOOT:
309 case MMC1_BOOT:
310 case MMC2_BOOT:
311 case MMC3_BOOT:
312 env_loc = ENVL_MMC;
313 break;
314#endif
315 default:
316#if defined(CONFIG_ENV_IS_NOWHERE)
317 env_loc = ENVL_NOWHERE;
318#endif
319 break;
320 }
321
322 return env_loc;
323}
324
Peng Fan65563792022-07-26 16:41:02 +0800325static int mix_power_init(enum mix_power_domain pd)
326{
327 enum src_mix_slice_id mix_id;
328 enum src_mem_slice_id mem_id;
329 struct src_mix_slice_regs *mix_regs;
330 struct src_mem_slice_regs *mem_regs;
331 struct src_general_regs *global_regs;
332 u32 scr, val;
333
334 switch (pd) {
335 case MIX_PD_MEDIAMIX:
336 mix_id = SRC_MIX_MEDIA;
337 mem_id = SRC_MEM_MEDIA;
338 scr = BIT(5);
339
340 /* Enable S400 handshake */
341 struct blk_ctrl_s_aonmix_regs *s_regs =
342 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
343
344 setbits_le32(&s_regs->lp_handshake[0], BIT(13));
345 break;
346 case MIX_PD_MLMIX:
347 mix_id = SRC_MIX_ML;
348 mem_id = SRC_MEM_ML;
349 scr = BIT(4);
350 break;
351 case MIX_PD_DDRMIX:
352 mix_id = SRC_MIX_DDRMIX;
353 mem_id = SRC_MEM_DDRMIX;
354 scr = BIT(6);
355 break;
356 default:
357 return -EINVAL;
358 }
359
360 mix_regs = (struct src_mix_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x400 * (mix_id + 1));
361 mem_regs =
362 (struct src_mem_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x3800 + 0x400 * mem_id);
363 global_regs = (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
364
365 /* Allow NS to set it */
366 setbits_le32(&mix_regs->authen_ctrl, BIT(9));
367
368 clrsetbits_le32(&mix_regs->psw_ack_ctrl[0], BIT(28), BIT(29));
369
370 /* mix reset will be held until boot core write this bit to 1 */
371 setbits_le32(&global_regs->scr, scr);
372
373 /* Enable mem in Low power auto sequence */
374 setbits_le32(&mem_regs->mem_ctrl, BIT(2));
375
376 /* Set the power down state */
377 val = readl(&mix_regs->func_stat);
378 if (val & SRC_MIX_SLICE_FUNC_STAT_PSW_STAT) {
379 /* The mix is default power off, power down it to make PDN_SFT bit
380 * aligned with FUNC STAT
381 */
382 setbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
383 val = readl(&mix_regs->func_stat);
384
385 /* Since PSW_STAT is 1, can't be used for power off status (SW_CTRL BIT31 set)) */
386 /* Check the MEM STAT change to ensure SSAR is completed */
387 while (!(val & SRC_MIX_SLICE_FUNC_STAT_MEM_STAT))
388 val = readl(&mix_regs->func_stat);
389
390 /* wait few ipg clock cycles to ensure FSM done and power off status is correct */
391 /* About 5 cycles at 24Mhz, 1us is enough */
392 udelay(1);
393 } else {
394 /* The mix is default power on, Do mix power cycle */
395 setbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
396 val = readl(&mix_regs->func_stat);
397 while (!(val & SRC_MIX_SLICE_FUNC_STAT_PSW_STAT))
398 val = readl(&mix_regs->func_stat);
399 }
400
401 /* power on */
402 clrbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
403 val = readl(&mix_regs->func_stat);
404 while (val & SRC_MIX_SLICE_FUNC_STAT_ISO_STAT)
405 val = readl(&mix_regs->func_stat);
406
407 return 0;
408}
409
410void disable_isolation(void)
411{
412 struct src_general_regs *global_regs = (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
413 /* clear isolation for usbphy, dsi, csi*/
414 writel(0x0, &global_regs->sp_iso_ctrl);
415}
416
417void soc_power_init(void)
418{
419 mix_power_init(MIX_PD_MEDIAMIX);
420 mix_power_init(MIX_PD_MLMIX);
421
422 disable_isolation();
423}
Peng Fan6d929962022-07-26 16:41:03 +0800424
Peng Fan313af252022-07-26 16:41:04 +0800425bool m33_is_rom_kicked(void)
Peng Fan6d929962022-07-26 16:41:03 +0800426{
427 struct blk_ctrl_s_aonmix_regs *s_regs =
428 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
429
430 if (!(readl(&s_regs->m33_cfg) & BIT(2)))
431 return true;
432
433 return false;
434}
435
436int m33_prepare(void)
437{
438 struct src_mix_slice_regs *mix_regs =
439 (struct src_mix_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x400 * (SRC_MIX_CM33 + 1));
440 struct src_general_regs *global_regs =
441 (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
442 struct blk_ctrl_s_aonmix_regs *s_regs =
443 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
444 u32 val;
445
446 if (m33_is_rom_kicked())
447 return -EPERM;
448
449 /* Release reset of M33 */
450 setbits_le32(&global_regs->scr, BIT(0));
451
452 /* Check the reset released in M33 MIX func stat */
453 val = readl(&mix_regs->func_stat);
454 while (!(val & SRC_MIX_SLICE_FUNC_STAT_RST_STAT))
455 val = readl(&mix_regs->func_stat);
456
457 /* Release Sentinel TROUT */
458 ahab_release_m33_trout();
459
460 /* Mask WDOG1 IRQ from A55, we use it for M33 reset */
461 setbits_le32(&s_regs->ca55_irq_mask[1], BIT(6));
462
463 /* Turn on WDOG1 clock */
464 ccm_lpcg_on(CCGR_WDG1, 1);
465
466 /* Set sentinel LP handshake for M33 reset */
467 setbits_le32(&s_regs->lp_handshake[0], BIT(6));
468
469 /* Clear M33 TCM for ECC */
470 memset((void *)(ulong)0x201e0000, 0, 0x40000);
471
472 return 0;
473}