blob: 627baa1d83fdc29b98610ec4cbf44508634115d9 [file] [log] [blame]
Peng Fan60d33fc2018-10-18 14:28:18 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
Gaurav Jaincb5d0412022-03-24 11:50:33 +05303 * Copyright 2018, 2021 NXP
Peng Fan60d33fc2018-10-18 14:28:18 +02004 */
5
Peng Fan60d33fc2018-10-18 14:28:18 +02006#include <clk.h>
Anatolij Gustschin2fdb1a12018-10-18 14:28:24 +02007#include <cpu.h>
Simon Glass9edefc22019-11-14 12:57:37 -07008#include <cpu_func.h>
Peng Fan60d33fc2018-10-18 14:28:18 +02009#include <dm.h>
Simon Glass7fe32b32022-03-04 08:43:05 -070010#include <event.h>
Simon Glass9b4a2052019-12-28 10:45:05 -070011#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass90526e92020-05-10 11:39:56 -060013#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Peng Fan60d33fc2018-10-18 14:28:18 +020015#include <dm/device-internal.h>
16#include <dm/lists.h>
17#include <dm/uclass.h>
18#include <errno.h>
Peng Fan6aead232020-05-05 20:28:41 +080019#include <spl.h>
Peng Fan1796e502019-04-26 01:44:27 +000020#include <thermal.h>
Peng Fan99ac6c72023-04-28 12:08:09 +080021#include <firmware/imx/sci/sci.h>
Peng Fan8aa15052018-10-18 14:28:19 +020022#include <asm/arch/sys_proto.h>
Peng Fan60d33fc2018-10-18 14:28:18 +020023#include <asm/arch-imx/cpu.h>
24#include <asm/armv8/cpu.h>
Peng Fan930b5952018-10-18 14:28:21 +020025#include <asm/armv8/mmu.h>
Peng Fan81037672020-05-05 20:28:39 +080026#include <asm/setup.h>
Peng Fan8aa15052018-10-18 14:28:19 +020027#include <asm/mach-imx/boot_mode.h>
Ye Lie8cd1f62023-06-15 18:09:20 +080028#include <power-domain.h>
29#include <elf.h>
Ye Li42b26dd2020-05-05 20:28:42 +080030#include <spl.h>
Peng Fan60d33fc2018-10-18 14:28:18 +020031
32DECLARE_GLOBAL_DATA_PTR;
33
Peng Fan1ef20a32018-10-18 14:28:22 +020034#define BT_PASSOVER_TAG 0x504F
35struct pass_over_info_t *get_pass_over_info(void)
36{
37 struct pass_over_info_t *p =
38 (struct pass_over_info_t *)PASS_OVER_INFO_ADDR;
39
40 if (p->barker != BT_PASSOVER_TAG ||
41 p->len != sizeof(struct pass_over_info_t))
42 return NULL;
43
44 return p;
45}
46
Igor Opaniukc4bc7db2024-01-31 13:49:26 +010047static char *get_reset_cause(void)
48{
49 sc_pm_reset_reason_t reason;
50
51 if (sc_pm_reset_reason(-1, &reason) != SC_ERR_NONE)
52 return "Unknown reset";
53
54 switch (reason) {
55 case SC_PM_RESET_REASON_POR:
56 return "POR";
57 case SC_PM_RESET_REASON_JTAG:
58 return "JTAG reset ";
59 case SC_PM_RESET_REASON_SW:
60 return "Software reset";
61 case SC_PM_RESET_REASON_WDOG:
62 return "Watchdog reset";
63 case SC_PM_RESET_REASON_LOCKUP:
64 return "SCU lockup reset";
65 case SC_PM_RESET_REASON_SNVS:
66 return "SNVS reset";
67 case SC_PM_RESET_REASON_TEMP:
68 return "Temp panic reset";
69 case SC_PM_RESET_REASON_MSI:
70 return "MSI reset";
71 case SC_PM_RESET_REASON_UECC:
72 return "ECC reset";
73 case SC_PM_RESET_REASON_SCFW_WDOG:
74 return "SCFW watchdog reset";
75 case SC_PM_RESET_REASON_ROM_WDOG:
76 return "SCU ROM watchdog reset";
77 case SC_PM_RESET_REASON_SECO:
78 return "SECO reset";
79 case SC_PM_RESET_REASON_SCFW_FAULT:
80 return "SCFW fault reset";
81 default:
82 return "Unknown reset";
83 }
84}
85
Fabio Estevamf8b874f2024-03-26 09:19:49 -030086__weak void reset_cpu(void)
87{
88}
89
Peng Fan1ef20a32018-10-18 14:28:22 +020090int arch_cpu_init(void)
91{
Peng Fan6aead232020-05-05 20:28:41 +080092#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_RECOVER_DATA_SECTION)
93 spl_save_restore_data();
94#endif
95
Peng Fan9382f732019-01-18 08:58:38 +000096#ifdef CONFIG_SPL_BUILD
97 struct pass_over_info_t *pass_over;
Peng Fan1ef20a32018-10-18 14:28:22 +020098
Peng Fan9382f732019-01-18 08:58:38 +000099 if (is_soc_rev(CHIP_REV_A)) {
100 pass_over = get_pass_over_info();
101 if (pass_over && pass_over->g_ap_mu == 0) {
102 /*
103 * When ap_mu is 0, means the U-Boot booted
104 * from first container
105 */
106 sc_misc_boot_status(-1, SC_MISC_BOOT_STATUS_SUCCESS);
107 }
Peng Fan1ef20a32018-10-18 14:28:22 +0200108 }
Peng Fan9382f732019-01-18 08:58:38 +0000109#endif
Peng Fan1ef20a32018-10-18 14:28:22 +0200110
111 return 0;
112}
113
Simon Glassf72d0d42023-08-21 21:16:56 -0600114static int imx8_init_mu(void)
Peng Fan1ef20a32018-10-18 14:28:22 +0200115{
116 struct udevice *devp;
117 int node, ret;
118
119 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx8-mu");
Peng Fan1ef20a32018-10-18 14:28:22 +0200120
Ye Libcf94ab2019-08-26 08:11:42 +0000121 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
Peng Fan1ef20a32018-10-18 14:28:22 +0200122 if (ret) {
Ye Libcf94ab2019-08-26 08:11:42 +0000123 printf("could not get scu %d\n", ret);
Peng Fan1ef20a32018-10-18 14:28:22 +0200124 return ret;
125 }
126
Peng Fan8f994382019-08-26 08:11:49 +0000127 if (is_imx8qm()) {
128 ret = sc_pm_set_resource_power_mode(-1, SC_R_SMMU,
129 SC_PM_PW_MODE_ON);
130 if (ret)
131 return ret;
132 }
133
Peng Fan1ef20a32018-10-18 14:28:22 +0200134 return 0;
135}
Simon Glassf72d0d42023-08-21 21:16:56 -0600136EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_F, imx8_init_mu);
Peng Fan1ef20a32018-10-18 14:28:22 +0200137
Gaurav Jaincb5d0412022-03-24 11:50:33 +0530138#if defined(CONFIG_ARCH_MISC_INIT)
139int arch_misc_init(void)
140{
141 if (IS_ENABLED(CONFIG_FSL_CAAM)) {
142 struct udevice *dev;
143 int ret;
144
145 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(caam_jr), &dev);
146 if (ret)
Ye Licda8f872022-05-11 13:56:20 +0530147 printf("Failed to initialize caam_jr: %d\n", ret);
Gaurav Jaincb5d0412022-03-24 11:50:33 +0530148 }
149
150 return 0;
151}
152#endif
153
Ye Lie8cd1f62023-06-15 18:09:20 +0800154#ifdef CONFIG_IMX_BOOTAUX
155
156#ifdef CONFIG_IMX8QM
157int arch_auxiliary_core_up(u32 core_id, ulong boot_private_data)
158{
159 sc_rsrc_t core_rsrc, mu_rsrc;
160 sc_faddr_t tcml_addr;
161 u32 tcml_size = SZ_128K;
162 ulong addr;
163
164 switch (core_id) {
165 case 0:
166 core_rsrc = SC_R_M4_0_PID0;
167 tcml_addr = 0x34FE0000;
168 mu_rsrc = SC_R_M4_0_MU_1A;
169 break;
170 case 1:
171 core_rsrc = SC_R_M4_1_PID0;
172 tcml_addr = 0x38FE0000;
173 mu_rsrc = SC_R_M4_1_MU_1A;
174 break;
175 default:
176 printf("Not support this core boot up, ID:%u\n", core_id);
177 return -EINVAL;
178 }
179
180 addr = (sc_faddr_t)boot_private_data;
181
182 if (addr >= tcml_addr && addr <= tcml_addr + tcml_size) {
183 printf("Wrong image address 0x%lx, should not in TCML\n",
184 addr);
185 return -EINVAL;
186 }
187
188 printf("Power on M4 and MU\n");
189
190 if (sc_pm_set_resource_power_mode(-1, core_rsrc, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
191 return -EIO;
192
193 if (sc_pm_set_resource_power_mode(-1, mu_rsrc, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
194 return -EIO;
195
196 printf("Copy M4 image from 0x%lx to TCML 0x%lx\n", addr, (ulong)tcml_addr);
197
198 if (addr != tcml_addr)
199 memcpy((void *)tcml_addr, (void *)addr, tcml_size);
200
201 printf("Start M4 %u\n", core_id);
202 if (sc_pm_cpu_start(-1, core_rsrc, true, tcml_addr) != SC_ERR_NONE)
203 return -EIO;
204
205 printf("bootaux complete\n");
206 return 0;
207}
208#endif
209
210#ifdef CONFIG_IMX8QXP
211int arch_auxiliary_core_up(u32 core_id, ulong boot_private_data)
212{
213 sc_rsrc_t core_rsrc, mu_rsrc = SC_R_NONE;
214 sc_faddr_t aux_core_ram;
215 u32 size;
216 ulong addr;
217
218 switch (core_id) {
219 case 0:
220 core_rsrc = SC_R_M4_0_PID0;
221 aux_core_ram = 0x34FE0000;
222 mu_rsrc = SC_R_M4_0_MU_1A;
223 size = SZ_128K;
224 break;
225 case 1:
226 core_rsrc = SC_R_DSP;
227 aux_core_ram = 0x596f8000;
228 size = SZ_2K;
229 break;
230 default:
231 printf("Not support this core boot up, ID:%u\n", core_id);
232 return -EINVAL;
233 }
234
235 addr = (sc_faddr_t)boot_private_data;
236
237 if (addr >= aux_core_ram && addr <= aux_core_ram + size) {
238 printf("Wrong image address 0x%lx, should not in aux core ram\n",
239 addr);
240 return -EINVAL;
241 }
242
243 printf("Power on aux core %d\n", core_id);
244
245 if (sc_pm_set_resource_power_mode(-1, core_rsrc, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
246 return -EIO;
247
248 if (mu_rsrc != SC_R_NONE) {
249 if (sc_pm_set_resource_power_mode(-1, mu_rsrc, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
250 return -EIO;
251 }
252
253 if (core_id == 1) {
254 struct power_domain pd;
255
256 if (sc_pm_clock_enable(-1, core_rsrc, SC_PM_CLK_PER, true, false) != SC_ERR_NONE) {
257 printf("Error enable clock\n");
258 return -EIO;
259 }
260
261 if (!power_domain_lookup_name("audio_sai0", &pd)) {
262 if (power_domain_on(&pd)) {
263 printf("Error power on SAI0\n");
264 return -EIO;
265 }
266 }
267
268 if (!power_domain_lookup_name("audio_ocram", &pd)) {
269 if (power_domain_on(&pd)) {
270 printf("Error power on HIFI RAM\n");
271 return -EIO;
272 }
273 }
274 }
275
276 printf("Copy image from 0x%lx to 0x%lx\n", addr, (ulong)aux_core_ram);
277 if (core_id == 0) {
278 /* M4 use bin file */
279 memcpy((void *)aux_core_ram, (void *)addr, size);
280 } else {
281 /* HIFI use elf file */
282 if (!valid_elf_image(addr))
283 return -1;
284 addr = load_elf_image_shdr(addr);
285 }
286
287 printf("Start %s\n", core_id == 0 ? "M4" : "HIFI");
288
289 if (sc_pm_cpu_start(-1, core_rsrc, true, aux_core_ram) != SC_ERR_NONE)
290 return -EIO;
291
292 printf("bootaux complete\n");
293 return 0;
294}
295#endif
296
297int arch_auxiliary_core_check_up(u32 core_id)
298{
299 sc_rsrc_t core_rsrc;
300 sc_pm_power_mode_t power_mode;
301
302 switch (core_id) {
303 case 0:
304 core_rsrc = SC_R_M4_0_PID0;
305 break;
306#ifdef CONFIG_IMX8QM
307 case 1:
308 core_rsrc = SC_R_M4_1_PID0;
309 break;
310#endif
311 default:
312 printf("Not support this core, ID:%u\n", core_id);
313 return 0;
314 }
315
316 if (sc_pm_get_resource_power_mode(-1, core_rsrc, &power_mode) != SC_ERR_NONE)
317 return 0;
318
319 if (power_mode != SC_PM_PW_MODE_OFF)
320 return 1;
321
322 return 0;
323}
324#endif
325
Peng Fan8aa15052018-10-18 14:28:19 +0200326int print_bootinfo(void)
327{
328 enum boot_device bt_dev = get_boot_device();
329
330 puts("Boot: ");
331 switch (bt_dev) {
332 case SD1_BOOT:
333 puts("SD0\n");
334 break;
335 case SD2_BOOT:
336 puts("SD1\n");
337 break;
338 case SD3_BOOT:
339 puts("SD2\n");
340 break;
341 case MMC1_BOOT:
342 puts("MMC0\n");
343 break;
344 case MMC2_BOOT:
345 puts("MMC1\n");
346 break;
347 case MMC3_BOOT:
348 puts("MMC2\n");
349 break;
350 case FLEXSPI_BOOT:
351 puts("FLEXSPI\n");
352 break;
353 case SATA_BOOT:
354 puts("SATA\n");
355 break;
356 case NAND_BOOT:
357 puts("NAND\n");
358 break;
359 case USB_BOOT:
360 puts("USB\n");
361 break;
362 default:
363 printf("Unknown device %u\n", bt_dev);
364 break;
365 }
366
Igor Opaniukc4bc7db2024-01-31 13:49:26 +0100367 printf("Reset cause: %s\n", get_reset_cause());
368
Peng Fan8aa15052018-10-18 14:28:19 +0200369 return 0;
370}
371
372enum boot_device get_boot_device(void)
373{
374 enum boot_device boot_dev = SD1_BOOT;
375
376 sc_rsrc_t dev_rsrc;
377
378 sc_misc_get_boot_dev(-1, &dev_rsrc);
379
380 switch (dev_rsrc) {
381 case SC_R_SDHC_0:
382 boot_dev = MMC1_BOOT;
383 break;
384 case SC_R_SDHC_1:
385 boot_dev = SD2_BOOT;
386 break;
387 case SC_R_SDHC_2:
388 boot_dev = SD3_BOOT;
389 break;
390 case SC_R_NAND:
391 boot_dev = NAND_BOOT;
392 break;
393 case SC_R_FSPI_0:
394 boot_dev = FLEXSPI_BOOT;
395 break;
396 case SC_R_SATA_0:
397 boot_dev = SATA_BOOT;
398 break;
399 case SC_R_USB_0:
400 case SC_R_USB_1:
401 case SC_R_USB_2:
402 boot_dev = USB_BOOT;
403 break;
404 default:
405 break;
406 }
407
408 return boot_dev;
409}
Peng Fanc1aae212018-10-18 14:28:20 +0200410
Tom Rinib9d66a02021-08-30 09:16:29 -0400411#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
Peng Fan81037672020-05-05 20:28:39 +0800412#define FUSE_UNIQUE_ID_WORD0 16
413#define FUSE_UNIQUE_ID_WORD1 17
414void get_board_serial(struct tag_serialnr *serialnr)
415{
Peng Fandd654ca2023-06-15 18:08:58 +0800416 int err;
Peng Fan81037672020-05-05 20:28:39 +0800417 u32 val1 = 0, val2 = 0;
418 u32 word1, word2;
419
420 if (!serialnr)
421 return;
422
423 word1 = FUSE_UNIQUE_ID_WORD0;
424 word2 = FUSE_UNIQUE_ID_WORD1;
425
426 err = sc_misc_otp_fuse_read(-1, word1, &val1);
Peng Fandd654ca2023-06-15 18:08:58 +0800427 if (err) {
Peng Fan81037672020-05-05 20:28:39 +0800428 printf("%s fuse %d read error: %d\n", __func__, word1, err);
429 return;
430 }
431
432 err = sc_misc_otp_fuse_read(-1, word2, &val2);
Peng Fandd654ca2023-06-15 18:08:58 +0800433 if (err) {
Peng Fan81037672020-05-05 20:28:39 +0800434 printf("%s fuse %d read error: %d\n", __func__, word2, err);
435 return;
436 }
437 serialnr->low = val1;
438 serialnr->high = val2;
439}
Tom Rinib9d66a02021-08-30 09:16:29 -0400440#endif /*CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG*/
Peng Fan81037672020-05-05 20:28:39 +0800441
Peng Fanc1aae212018-10-18 14:28:20 +0200442#ifdef CONFIG_ENV_IS_IN_MMC
443__weak int board_mmc_get_env_dev(int devno)
444{
445 return CONFIG_SYS_MMC_ENV_DEV;
446}
447
448int mmc_get_env_dev(void)
449{
450 sc_rsrc_t dev_rsrc;
451 int devno;
452
453 sc_misc_get_boot_dev(-1, &dev_rsrc);
454
455 switch (dev_rsrc) {
456 case SC_R_SDHC_0:
457 devno = 0;
458 break;
459 case SC_R_SDHC_1:
460 devno = 1;
461 break;
462 case SC_R_SDHC_2:
463 devno = 2;
464 break;
465 default:
466 /* If not boot from sd/mmc, use default value */
467 return CONFIG_SYS_MMC_ENV_DEV;
468 }
469
470 return board_mmc_get_env_dev(devno);
471}
472#endif
Peng Fan930b5952018-10-18 14:28:21 +0200473
474#define MEMSTART_ALIGNMENT SZ_2M /* Align the memory start with 2MB */
475
476static int get_owned_memreg(sc_rm_mr_t mr, sc_faddr_t *addr_start,
477 sc_faddr_t *addr_end)
478{
479 sc_faddr_t start, end;
480 int ret;
481 bool owned;
482
483 owned = sc_rm_is_memreg_owned(-1, mr);
484 if (owned) {
485 ret = sc_rm_get_memreg_info(-1, mr, &start, &end);
486 if (ret) {
487 printf("Memreg get info failed, %d\n", ret);
488 return -EINVAL;
489 }
490 debug("0x%llx -- 0x%llx\n", start, end);
491 *addr_start = start;
492 *addr_end = end;
493
494 return 0;
495 }
496
497 return -EINVAL;
498}
499
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300500__weak void board_mem_get_layout(u64 *phys_sdram_1_start,
501 u64 *phys_sdram_1_size,
502 u64 *phys_sdram_2_start,
503 u64 *phys_sdram_2_size)
504{
505 *phys_sdram_1_start = PHYS_SDRAM_1;
506 *phys_sdram_1_size = PHYS_SDRAM_1_SIZE;
507 *phys_sdram_2_start = PHYS_SDRAM_2;
508 *phys_sdram_2_size = PHYS_SDRAM_2_SIZE;
509}
510
Peng Fan930b5952018-10-18 14:28:21 +0200511phys_size_t get_effective_memsize(void)
512{
513 sc_rm_mr_t mr;
Ye Li7c351ff2020-05-05 20:28:38 +0800514 sc_faddr_t start, end, end1, start_aligned;
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300515 u64 phys_sdram_1_start, phys_sdram_1_size;
516 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan930b5952018-10-18 14:28:21 +0200517 int err;
518
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300519 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
520 &phys_sdram_2_start, &phys_sdram_2_size);
Peng Fan930b5952018-10-18 14:28:21 +0200521
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300522
523 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
Peng Fan930b5952018-10-18 14:28:21 +0200524 for (mr = 0; mr < 64; mr++) {
525 err = get_owned_memreg(mr, &start, &end);
526 if (!err) {
Ye Li7c351ff2020-05-05 20:28:38 +0800527 start_aligned = roundup(start, MEMSTART_ALIGNMENT);
Peng Fan930b5952018-10-18 14:28:21 +0200528 /* Too small memory region, not use it */
Ye Li7c351ff2020-05-05 20:28:38 +0800529 if (start_aligned > end)
Peng Fan930b5952018-10-18 14:28:21 +0200530 continue;
531
Peng Fan1ef20a32018-10-18 14:28:22 +0200532 /* Find the memory region runs the U-Boot */
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300533 if (start >= phys_sdram_1_start && start <= end1 &&
Simon Glass98463902022-10-20 18:22:39 -0600534 (start <= CONFIG_TEXT_BASE &&
535 end >= CONFIG_TEXT_BASE)) {
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300536 if ((end + 1) <=
537 ((sc_faddr_t)phys_sdram_1_start +
538 phys_sdram_1_size))
539 return (end - phys_sdram_1_start + 1);
Peng Fan930b5952018-10-18 14:28:21 +0200540 else
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300541 return phys_sdram_1_size;
Peng Fan930b5952018-10-18 14:28:21 +0200542 }
543 }
544 }
545
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300546 return phys_sdram_1_size;
Peng Fan930b5952018-10-18 14:28:21 +0200547}
548
549int dram_init(void)
550{
551 sc_rm_mr_t mr;
552 sc_faddr_t start, end, end1, end2;
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300553 u64 phys_sdram_1_start, phys_sdram_1_size;
554 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan930b5952018-10-18 14:28:21 +0200555 int err;
556
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300557 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
558 &phys_sdram_2_start, &phys_sdram_2_size);
559
560 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
561 end2 = (sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size;
Peng Fan930b5952018-10-18 14:28:21 +0200562 for (mr = 0; mr < 64; mr++) {
563 err = get_owned_memreg(mr, &start, &end);
564 if (!err) {
565 start = roundup(start, MEMSTART_ALIGNMENT);
566 /* Too small memory region, not use it */
567 if (start > end)
568 continue;
569
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300570 if (start >= phys_sdram_1_start && start <= end1) {
Peng Fan930b5952018-10-18 14:28:21 +0200571 if ((end + 1) <= end1)
572 gd->ram_size += end - start + 1;
573 else
574 gd->ram_size += end1 - start;
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300575 } else if (start >= phys_sdram_2_start &&
576 start <= end2) {
Peng Fan930b5952018-10-18 14:28:21 +0200577 if ((end + 1) <= end2)
578 gd->ram_size += end - start + 1;
579 else
580 gd->ram_size += end2 - start;
581 }
582 }
583 }
584
585 /* If error, set to the default value */
586 if (!gd->ram_size) {
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300587 gd->ram_size = phys_sdram_1_size;
588 gd->ram_size += phys_sdram_2_size;
Peng Fan930b5952018-10-18 14:28:21 +0200589 }
590 return 0;
591}
592
593static void dram_bank_sort(int current_bank)
594{
595 phys_addr_t start;
596 phys_size_t size;
597
598 while (current_bank > 0) {
599 if (gd->bd->bi_dram[current_bank - 1].start >
600 gd->bd->bi_dram[current_bank].start) {
601 start = gd->bd->bi_dram[current_bank - 1].start;
602 size = gd->bd->bi_dram[current_bank - 1].size;
603
604 gd->bd->bi_dram[current_bank - 1].start =
605 gd->bd->bi_dram[current_bank].start;
606 gd->bd->bi_dram[current_bank - 1].size =
607 gd->bd->bi_dram[current_bank].size;
608
609 gd->bd->bi_dram[current_bank].start = start;
610 gd->bd->bi_dram[current_bank].size = size;
611 }
612 current_bank--;
613 }
614}
615
616int dram_init_banksize(void)
617{
618 sc_rm_mr_t mr;
619 sc_faddr_t start, end, end1, end2;
620 int i = 0;
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300621 u64 phys_sdram_1_start, phys_sdram_1_size;
622 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan930b5952018-10-18 14:28:21 +0200623 int err;
624
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300625 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
626 &phys_sdram_2_start, &phys_sdram_2_size);
Peng Fan930b5952018-10-18 14:28:21 +0200627
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300628 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
629 end2 = (sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size;
Peng Fan930b5952018-10-18 14:28:21 +0200630 for (mr = 0; mr < 64 && i < CONFIG_NR_DRAM_BANKS; mr++) {
631 err = get_owned_memreg(mr, &start, &end);
632 if (!err) {
633 start = roundup(start, MEMSTART_ALIGNMENT);
634 if (start > end) /* Small memory region, no use it */
635 continue;
636
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300637 if (start >= phys_sdram_1_start && start <= end1) {
Peng Fan930b5952018-10-18 14:28:21 +0200638 gd->bd->bi_dram[i].start = start;
639
640 if ((end + 1) <= end1)
641 gd->bd->bi_dram[i].size =
642 end - start + 1;
643 else
644 gd->bd->bi_dram[i].size = end1 - start;
645
646 dram_bank_sort(i);
647 i++;
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300648 } else if (start >= phys_sdram_2_start && start <= end2) {
Peng Fan930b5952018-10-18 14:28:21 +0200649 gd->bd->bi_dram[i].start = start;
650
651 if ((end + 1) <= end2)
652 gd->bd->bi_dram[i].size =
653 end - start + 1;
654 else
655 gd->bd->bi_dram[i].size = end2 - start;
656
657 dram_bank_sort(i);
658 i++;
659 }
660 }
661 }
662
663 /* If error, set to the default value */
664 if (!i) {
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300665 gd->bd->bi_dram[0].start = phys_sdram_1_start;
666 gd->bd->bi_dram[0].size = phys_sdram_1_size;
667 gd->bd->bi_dram[1].start = phys_sdram_2_start;
668 gd->bd->bi_dram[1].size = phys_sdram_2_size;
Peng Fan930b5952018-10-18 14:28:21 +0200669 }
670
671 return 0;
672}
673
674static u64 get_block_attrs(sc_faddr_t addr_start)
675{
676 u64 attr = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE |
677 PTE_BLOCK_PXN | PTE_BLOCK_UXN;
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300678 u64 phys_sdram_1_start, phys_sdram_1_size;
679 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan930b5952018-10-18 14:28:21 +0200680
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300681 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
682 &phys_sdram_2_start, &phys_sdram_2_size);
683
684 if ((addr_start >= phys_sdram_1_start &&
685 addr_start <= ((sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size)) ||
686 (addr_start >= phys_sdram_2_start &&
687 addr_start <= ((sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size)))
Peng Fan930b5952018-10-18 14:28:21 +0200688 return (PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_OUTER_SHARE);
689
690 return attr;
691}
692
693static u64 get_block_size(sc_faddr_t addr_start, sc_faddr_t addr_end)
694{
695 sc_faddr_t end1, end2;
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300696 u64 phys_sdram_1_start, phys_sdram_1_size;
697 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan930b5952018-10-18 14:28:21 +0200698
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300699 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
700 &phys_sdram_2_start, &phys_sdram_2_size);
Peng Fan930b5952018-10-18 14:28:21 +0200701
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300702
703 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
704 end2 = (sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size;
705
706 if (addr_start >= phys_sdram_1_start && addr_start <= end1) {
Peng Fan930b5952018-10-18 14:28:21 +0200707 if ((addr_end + 1) > end1)
708 return end1 - addr_start;
Marcel Ziswiler2f36a692020-10-22 11:21:40 +0300709 } else if (addr_start >= phys_sdram_2_start && addr_start <= end2) {
Peng Fan930b5952018-10-18 14:28:21 +0200710 if ((addr_end + 1) > end2)
711 return end2 - addr_start;
712 }
713
714 return (addr_end - addr_start + 1);
715}
716
717#define MAX_PTE_ENTRIES 512
718#define MAX_MEM_MAP_REGIONS 16
719
720static struct mm_region imx8_mem_map[MAX_MEM_MAP_REGIONS];
721struct mm_region *mem_map = imx8_mem_map;
722
723void enable_caches(void)
724{
725 sc_rm_mr_t mr;
726 sc_faddr_t start, end;
727 int err, i;
728
729 /* Create map for registers access from 0x1c000000 to 0x80000000*/
730 imx8_mem_map[0].virt = 0x1c000000UL;
731 imx8_mem_map[0].phys = 0x1c000000UL;
732 imx8_mem_map[0].size = 0x64000000UL;
733 imx8_mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
734 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN;
735
736 i = 1;
737 for (mr = 0; mr < 64 && i < MAX_MEM_MAP_REGIONS; mr++) {
738 err = get_owned_memreg(mr, &start, &end);
739 if (!err) {
740 imx8_mem_map[i].virt = start;
741 imx8_mem_map[i].phys = start;
742 imx8_mem_map[i].size = get_block_size(start, end);
743 imx8_mem_map[i].attrs = get_block_attrs(start);
744 i++;
745 }
746 }
747
748 if (i < MAX_MEM_MAP_REGIONS) {
749 imx8_mem_map[i].size = 0;
750 imx8_mem_map[i].attrs = 0;
751 } else {
752 puts("Error, need more MEM MAP REGIONS reserved\n");
753 icache_enable();
754 return;
755 }
756
757 for (i = 0; i < MAX_MEM_MAP_REGIONS; i++) {
758 debug("[%d] vir = 0x%llx phys = 0x%llx size = 0x%llx attrs = 0x%llx\n",
759 i, imx8_mem_map[i].virt, imx8_mem_map[i].phys,
760 imx8_mem_map[i].size, imx8_mem_map[i].attrs);
761 }
762
763 icache_enable();
764 dcache_enable();
765}
766
Trevor Woerner10015022019-05-03 09:41:00 -0400767#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Peng Fan930b5952018-10-18 14:28:21 +0200768u64 get_page_table_size(void)
769{
770 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
771 u64 size = 0;
772
773 /*
774 * For each memory region, the max table size:
775 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
776 */
777 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
778
779 /*
780 * We need to duplicate our page table once to have an emergency pt to
781 * resort to when splitting page tables later on
782 */
783 size *= 2;
784
785 /*
786 * We may need to split page tables later on if dcache settings change,
787 * so reserve up to 4 (random pick) page tables for that.
788 */
789 size += one_pt * 4;
790
791 return size;
792}
793#endif
Anatolij Gustschin70b4b492018-10-18 14:28:23 +0200794
Peng Fanbae4e8c2019-08-26 08:12:23 +0000795#if defined(CONFIG_IMX8QM)
796#define FUSE_MAC0_WORD0 452
797#define FUSE_MAC0_WORD1 453
798#define FUSE_MAC1_WORD0 454
799#define FUSE_MAC1_WORD1 455
800#elif defined(CONFIG_IMX8QXP)
Anatolij Gustschin70b4b492018-10-18 14:28:23 +0200801#define FUSE_MAC0_WORD0 708
802#define FUSE_MAC0_WORD1 709
803#define FUSE_MAC1_WORD0 710
804#define FUSE_MAC1_WORD1 711
Peng Fanbae4e8c2019-08-26 08:12:23 +0000805#endif
Anatolij Gustschin70b4b492018-10-18 14:28:23 +0200806
807void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
808{
809 u32 word[2], val[2] = {};
810 int i, ret;
811
812 if (dev_id == 0) {
813 word[0] = FUSE_MAC0_WORD0;
814 word[1] = FUSE_MAC0_WORD1;
815 } else {
816 word[0] = FUSE_MAC1_WORD0;
817 word[1] = FUSE_MAC1_WORD1;
818 }
819
820 for (i = 0; i < 2; i++) {
821 ret = sc_misc_otp_fuse_read(-1, word[i], &val[i]);
822 if (ret < 0)
823 goto err;
824 }
825
826 mac[0] = val[0];
827 mac[1] = val[0] >> 8;
828 mac[2] = val[0] >> 16;
829 mac[3] = val[0] >> 24;
830 mac[4] = val[1];
831 mac[5] = val[1] >> 8;
832
833 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
834 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
835 return;
836err:
837 printf("%s: fuse %d, err: %d\n", __func__, word[i], ret);
838}
Anatolij Gustschin2fdb1a12018-10-18 14:28:24 +0200839
Anatolij Gustschin2fdb1a12018-10-18 14:28:24 +0200840u32 get_cpu_rev(void)
841{
842 u32 id = 0, rev = 0;
843 int ret;
844
845 ret = sc_misc_get_control(-1, SC_R_SYSTEM, SC_C_ID, &id);
846 if (ret)
847 return 0;
848
849 rev = (id >> 5) & 0xf;
850 id = (id & 0x1f) + MXC_SOC_IMX8; /* Dummy ID for chip */
851
852 return (id << 12) | rev;
853}
Ye Li42b26dd2020-05-05 20:28:42 +0800854
855void board_boot_order(u32 *spl_boot_list)
856{
857 spl_boot_list[0] = spl_boot_device();
858
859 if (spl_boot_list[0] == BOOT_DEVICE_SPI) {
860 /* Check whether we own the flexspi0, if not, use NOR boot */
861 if (!sc_rm_is_resource_owned(-1, SC_R_FSPI_0))
862 spl_boot_list[0] = BOOT_DEVICE_NOR;
863 }
864}
Peng Faned5b2532020-05-05 20:28:43 +0800865
866bool m4_parts_booted(void)
867{
868 sc_rm_pt_t m4_parts[2];
869 int err;
870
871 err = sc_rm_get_resource_owner(-1, SC_R_M4_0_PID0, &m4_parts[0]);
872 if (err) {
873 printf("%s get resource [%d] owner error: %d\n", __func__,
874 SC_R_M4_0_PID0, err);
875 return false;
876 }
877
878 if (sc_pm_is_partition_started(-1, m4_parts[0]))
879 return true;
880
881 if (is_imx8qm()) {
882 err = sc_rm_get_resource_owner(-1, SC_R_M4_1_PID0, &m4_parts[1]);
883 if (err) {
884 printf("%s get resource [%d] owner error: %d\n",
885 __func__, SC_R_M4_1_PID0, err);
886 return false;
887 }
888
889 if (sc_pm_is_partition_started(-1, m4_parts[1]))
890 return true;
891 }
892
893 return false;
894}