blob: 252663a9eec2e4af264fed151fb2c84254ad1483 [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
Peng Fanbbcd2c42022-07-26 16:40:39 +0800211int ft_system_setup(void *blob, struct bd_info *bd)
212{
213 return 0;
214}
Peng Fan3700c472022-07-26 16:40:56 +0800215
216#if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)
217void get_board_serial(struct tag_serialnr *serialnr)
218{
219 printf("UID: 0x%x 0x%x 0x%x 0x%x\n",
220 gd->arch.uid[0], gd->arch.uid[1], gd->arch.uid[2], gd->arch.uid[3]);
221
222 serialnr->low = gd->arch.uid[0];
223 serialnr->high = gd->arch.uid[3];
224}
225#endif
Peng Fanbbcd2c42022-07-26 16:40:39 +0800226
227int arch_cpu_init(void)
228{
Ye Li9e19ff92022-07-26 16:40:47 +0800229 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
230 /* Disable wdog */
231 init_wdog();
232
Peng Fan28b5cb52022-07-26 16:40:43 +0800233 clock_init();
Ye Li62185922022-07-26 16:40:54 +0800234
235 trdc_early_init();
Ye Li9e19ff92022-07-26 16:40:47 +0800236 }
Peng Fan28b5cb52022-07-26 16:40:43 +0800237
Peng Fanbbcd2c42022-07-26 16:40:39 +0800238 return 0;
239}
Peng Fan3700c472022-07-26 16:40:56 +0800240
241int imx9_probe_mu(void *ctx, struct event *event)
242{
243 struct udevice *devp;
244 int node, ret;
245 u32 res;
246 struct sentinel_get_info_data info;
247
248 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx93-mu-s4");
249
250 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
251 if (ret)
252 return ret;
253
254 if (gd->flags & GD_FLG_RELOC)
255 return 0;
256
257 ret = ahab_get_info(&info, &res);
258 if (ret)
259 return ret;
260
261 set_cpu_info(&info);
262
263 return 0;
264}
Simon Glass93074012023-05-04 16:50:45 -0600265EVENT_SPY(EVT_DM_POST_INIT_F, imx9_probe_mu);
Jian Liacf41a32022-07-26 16:40:46 +0800266
267int timer_init(void)
268{
269#ifdef CONFIG_SPL_BUILD
270 struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR;
271 unsigned long freq = readl(&sctr->cntfid0);
272
273 /* Update with accurate clock frequency */
274 asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory");
275
276 clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1,
277 SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG);
278#endif
279
280 gd->arch.tbl = 0;
281 gd->arch.tbu = 0;
282
283 return 0;
284}
Peng Fan65563792022-07-26 16:41:02 +0800285
Ye Li8e8687c2022-07-26 16:41:05 +0800286enum env_location env_get_location(enum env_operation op, int prio)
287{
288 enum boot_device dev = get_boot_device();
289 enum env_location env_loc = ENVL_UNKNOWN;
290
291 if (prio)
292 return env_loc;
293
294 switch (dev) {
295#if defined(CONFIG_ENV_IS_IN_SPI_FLASH)
296 case QSPI_BOOT:
297 env_loc = ENVL_SPI_FLASH;
298 break;
299#endif
300#if defined(CONFIG_ENV_IS_IN_MMC)
301 case SD1_BOOT:
302 case SD2_BOOT:
303 case SD3_BOOT:
304 case MMC1_BOOT:
305 case MMC2_BOOT:
306 case MMC3_BOOT:
307 env_loc = ENVL_MMC;
308 break;
309#endif
310 default:
311#if defined(CONFIG_ENV_IS_NOWHERE)
312 env_loc = ENVL_NOWHERE;
313#endif
314 break;
315 }
316
317 return env_loc;
318}
319
Peng Fan65563792022-07-26 16:41:02 +0800320static int mix_power_init(enum mix_power_domain pd)
321{
322 enum src_mix_slice_id mix_id;
323 enum src_mem_slice_id mem_id;
324 struct src_mix_slice_regs *mix_regs;
325 struct src_mem_slice_regs *mem_regs;
326 struct src_general_regs *global_regs;
327 u32 scr, val;
328
329 switch (pd) {
330 case MIX_PD_MEDIAMIX:
331 mix_id = SRC_MIX_MEDIA;
332 mem_id = SRC_MEM_MEDIA;
333 scr = BIT(5);
334
335 /* Enable S400 handshake */
336 struct blk_ctrl_s_aonmix_regs *s_regs =
337 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
338
339 setbits_le32(&s_regs->lp_handshake[0], BIT(13));
340 break;
341 case MIX_PD_MLMIX:
342 mix_id = SRC_MIX_ML;
343 mem_id = SRC_MEM_ML;
344 scr = BIT(4);
345 break;
346 case MIX_PD_DDRMIX:
347 mix_id = SRC_MIX_DDRMIX;
348 mem_id = SRC_MEM_DDRMIX;
349 scr = BIT(6);
350 break;
351 default:
352 return -EINVAL;
353 }
354
355 mix_regs = (struct src_mix_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x400 * (mix_id + 1));
356 mem_regs =
357 (struct src_mem_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x3800 + 0x400 * mem_id);
358 global_regs = (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
359
360 /* Allow NS to set it */
361 setbits_le32(&mix_regs->authen_ctrl, BIT(9));
362
363 clrsetbits_le32(&mix_regs->psw_ack_ctrl[0], BIT(28), BIT(29));
364
365 /* mix reset will be held until boot core write this bit to 1 */
366 setbits_le32(&global_regs->scr, scr);
367
368 /* Enable mem in Low power auto sequence */
369 setbits_le32(&mem_regs->mem_ctrl, BIT(2));
370
371 /* Set the power down state */
372 val = readl(&mix_regs->func_stat);
373 if (val & SRC_MIX_SLICE_FUNC_STAT_PSW_STAT) {
374 /* The mix is default power off, power down it to make PDN_SFT bit
375 * aligned with FUNC STAT
376 */
377 setbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
378 val = readl(&mix_regs->func_stat);
379
380 /* Since PSW_STAT is 1, can't be used for power off status (SW_CTRL BIT31 set)) */
381 /* Check the MEM STAT change to ensure SSAR is completed */
382 while (!(val & SRC_MIX_SLICE_FUNC_STAT_MEM_STAT))
383 val = readl(&mix_regs->func_stat);
384
385 /* wait few ipg clock cycles to ensure FSM done and power off status is correct */
386 /* About 5 cycles at 24Mhz, 1us is enough */
387 udelay(1);
388 } else {
389 /* The mix is default power on, Do mix power cycle */
390 setbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
391 val = readl(&mix_regs->func_stat);
392 while (!(val & SRC_MIX_SLICE_FUNC_STAT_PSW_STAT))
393 val = readl(&mix_regs->func_stat);
394 }
395
396 /* power on */
397 clrbits_le32(&mix_regs->slice_sw_ctrl, BIT(31));
398 val = readl(&mix_regs->func_stat);
399 while (val & SRC_MIX_SLICE_FUNC_STAT_ISO_STAT)
400 val = readl(&mix_regs->func_stat);
401
402 return 0;
403}
404
405void disable_isolation(void)
406{
407 struct src_general_regs *global_regs = (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
408 /* clear isolation for usbphy, dsi, csi*/
409 writel(0x0, &global_regs->sp_iso_ctrl);
410}
411
412void soc_power_init(void)
413{
414 mix_power_init(MIX_PD_MEDIAMIX);
415 mix_power_init(MIX_PD_MLMIX);
416
417 disable_isolation();
418}
Peng Fan6d929962022-07-26 16:41:03 +0800419
Peng Fan313af252022-07-26 16:41:04 +0800420bool m33_is_rom_kicked(void)
Peng Fan6d929962022-07-26 16:41:03 +0800421{
422 struct blk_ctrl_s_aonmix_regs *s_regs =
423 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
424
425 if (!(readl(&s_regs->m33_cfg) & BIT(2)))
426 return true;
427
428 return false;
429}
430
431int m33_prepare(void)
432{
433 struct src_mix_slice_regs *mix_regs =
434 (struct src_mix_slice_regs *)(ulong)(SRC_IPS_BASE_ADDR + 0x400 * (SRC_MIX_CM33 + 1));
435 struct src_general_regs *global_regs =
436 (struct src_general_regs *)(ulong)SRC_GLOBAL_RBASE;
437 struct blk_ctrl_s_aonmix_regs *s_regs =
438 (struct blk_ctrl_s_aonmix_regs *)BLK_CTRL_S_ANOMIX_BASE_ADDR;
439 u32 val;
440
441 if (m33_is_rom_kicked())
442 return -EPERM;
443
444 /* Release reset of M33 */
445 setbits_le32(&global_regs->scr, BIT(0));
446
447 /* Check the reset released in M33 MIX func stat */
448 val = readl(&mix_regs->func_stat);
449 while (!(val & SRC_MIX_SLICE_FUNC_STAT_RST_STAT))
450 val = readl(&mix_regs->func_stat);
451
452 /* Release Sentinel TROUT */
453 ahab_release_m33_trout();
454
455 /* Mask WDOG1 IRQ from A55, we use it for M33 reset */
456 setbits_le32(&s_regs->ca55_irq_mask[1], BIT(6));
457
458 /* Turn on WDOG1 clock */
459 ccm_lpcg_on(CCGR_WDG1, 1);
460
461 /* Set sentinel LP handshake for M33 reset */
462 setbits_le32(&s_regs->lp_handshake[0], BIT(6));
463
464 /* Clear M33 TCM for ECC */
465 memset((void *)(ulong)0x201e0000, 0, 0x40000);
466
467 return 0;
468}