blob: 0b91e448a5d9241c8e336594bf4487e9f111ec16 [file] [log] [blame]
Peng Fan2e6be072018-10-18 14:28:18 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
Gaurav Jaindb4dd6a2022-03-24 11:50:33 +05303 * Copyright 2018, 2021 NXP
Peng Fan2e6be072018-10-18 14:28:18 +02004 */
5
6#include <common.h>
7#include <clk.h>
Anatolij Gustschin9b39be92018-10-18 14:28:24 +02008#include <cpu.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07009#include <cpu_func.h>
Peng Fan2e6be072018-10-18 14:28:18 +020010#include <dm.h>
Simon Glassfc557362022-03-04 08:43:05 -070011#include <event.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Peng Fan2e6be072018-10-18 14:28:18 +020016#include <dm/device-internal.h>
17#include <dm/lists.h>
18#include <dm/uclass.h>
19#include <errno.h>
Peng Fan617fc292020-05-05 20:28:41 +080020#include <spl.h>
Peng Fan48f9c4e2019-04-26 01:44:27 +000021#include <thermal.h>
Peng Fan2e0644a2023-04-28 12:08:09 +080022#include <firmware/imx/sci/sci.h>
Peng Fan29c9dd32018-10-18 14:28:19 +020023#include <asm/arch/sys_proto.h>
Peng Fan2e6be072018-10-18 14:28:18 +020024#include <asm/arch-imx/cpu.h>
25#include <asm/armv8/cpu.h>
Peng Fan4f211a52018-10-18 14:28:21 +020026#include <asm/armv8/mmu.h>
Peng Fand2aaf0c2020-05-05 20:28:39 +080027#include <asm/setup.h>
Peng Fan29c9dd32018-10-18 14:28:19 +020028#include <asm/mach-imx/boot_mode.h>
Ye Lic07ac742023-06-15 18:09:20 +080029#include <power-domain.h>
30#include <elf.h>
Ye Lic3169bd2020-05-05 20:28:42 +080031#include <spl.h>
Peng Fan2e6be072018-10-18 14:28:18 +020032
33DECLARE_GLOBAL_DATA_PTR;
34
Peng Fan14b4cd22018-10-18 14:28:22 +020035#define BT_PASSOVER_TAG 0x504F
36struct pass_over_info_t *get_pass_over_info(void)
37{
38 struct pass_over_info_t *p =
39 (struct pass_over_info_t *)PASS_OVER_INFO_ADDR;
40
41 if (p->barker != BT_PASSOVER_TAG ||
42 p->len != sizeof(struct pass_over_info_t))
43 return NULL;
44
45 return p;
46}
47
Igor Opaniukab5ffef2024-01-31 13:49:26 +010048static char *get_reset_cause(void)
49{
50 sc_pm_reset_reason_t reason;
51
52 if (sc_pm_reset_reason(-1, &reason) != SC_ERR_NONE)
53 return "Unknown reset";
54
55 switch (reason) {
56 case SC_PM_RESET_REASON_POR:
57 return "POR";
58 case SC_PM_RESET_REASON_JTAG:
59 return "JTAG reset ";
60 case SC_PM_RESET_REASON_SW:
61 return "Software reset";
62 case SC_PM_RESET_REASON_WDOG:
63 return "Watchdog reset";
64 case SC_PM_RESET_REASON_LOCKUP:
65 return "SCU lockup reset";
66 case SC_PM_RESET_REASON_SNVS:
67 return "SNVS reset";
68 case SC_PM_RESET_REASON_TEMP:
69 return "Temp panic reset";
70 case SC_PM_RESET_REASON_MSI:
71 return "MSI reset";
72 case SC_PM_RESET_REASON_UECC:
73 return "ECC reset";
74 case SC_PM_RESET_REASON_SCFW_WDOG:
75 return "SCFW watchdog reset";
76 case SC_PM_RESET_REASON_ROM_WDOG:
77 return "SCU ROM watchdog reset";
78 case SC_PM_RESET_REASON_SECO:
79 return "SECO reset";
80 case SC_PM_RESET_REASON_SCFW_FAULT:
81 return "SCFW fault reset";
82 default:
83 return "Unknown reset";
84 }
85}
86
Peng Fan14b4cd22018-10-18 14:28:22 +020087int arch_cpu_init(void)
88{
Peng Fan617fc292020-05-05 20:28:41 +080089#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_RECOVER_DATA_SECTION)
90 spl_save_restore_data();
91#endif
92
Peng Fan0bcec7f2019-01-18 08:58:38 +000093#ifdef CONFIG_SPL_BUILD
94 struct pass_over_info_t *pass_over;
Peng Fan14b4cd22018-10-18 14:28:22 +020095
Peng Fan0bcec7f2019-01-18 08:58:38 +000096 if (is_soc_rev(CHIP_REV_A)) {
97 pass_over = get_pass_over_info();
98 if (pass_over && pass_over->g_ap_mu == 0) {
99 /*
100 * When ap_mu is 0, means the U-Boot booted
101 * from first container
102 */
103 sc_misc_boot_status(-1, SC_MISC_BOOT_STATUS_SUCCESS);
104 }
Peng Fan14b4cd22018-10-18 14:28:22 +0200105 }
Peng Fan0bcec7f2019-01-18 08:58:38 +0000106#endif
Peng Fan14b4cd22018-10-18 14:28:22 +0200107
108 return 0;
109}
110
Simon Glassb8357c12023-08-21 21:16:56 -0600111static int imx8_init_mu(void)
Peng Fan14b4cd22018-10-18 14:28:22 +0200112{
113 struct udevice *devp;
114 int node, ret;
115
116 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx8-mu");
Peng Fan14b4cd22018-10-18 14:28:22 +0200117
Ye Lif2ea6f02019-08-26 08:11:42 +0000118 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
Peng Fan14b4cd22018-10-18 14:28:22 +0200119 if (ret) {
Ye Lif2ea6f02019-08-26 08:11:42 +0000120 printf("could not get scu %d\n", ret);
Peng Fan14b4cd22018-10-18 14:28:22 +0200121 return ret;
122 }
123
Peng Fanee380c52019-08-26 08:11:49 +0000124 if (is_imx8qm()) {
125 ret = sc_pm_set_resource_power_mode(-1, SC_R_SMMU,
126 SC_PM_PW_MODE_ON);
127 if (ret)
128 return ret;
129 }
130
Peng Fan14b4cd22018-10-18 14:28:22 +0200131 return 0;
132}
Simon Glassb8357c12023-08-21 21:16:56 -0600133EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_F, imx8_init_mu);
Peng Fan14b4cd22018-10-18 14:28:22 +0200134
Gaurav Jaindb4dd6a2022-03-24 11:50:33 +0530135#if defined(CONFIG_ARCH_MISC_INIT)
136int arch_misc_init(void)
137{
138 if (IS_ENABLED(CONFIG_FSL_CAAM)) {
139 struct udevice *dev;
140 int ret;
141
142 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(caam_jr), &dev);
143 if (ret)
Ye Liec346892022-05-11 13:56:20 +0530144 printf("Failed to initialize caam_jr: %d\n", ret);
Gaurav Jaindb4dd6a2022-03-24 11:50:33 +0530145 }
146
147 return 0;
148}
149#endif
150
Ye Lic07ac742023-06-15 18:09:20 +0800151#ifdef CONFIG_IMX_BOOTAUX
152
153#ifdef CONFIG_IMX8QM
154int arch_auxiliary_core_up(u32 core_id, ulong boot_private_data)
155{
156 sc_rsrc_t core_rsrc, mu_rsrc;
157 sc_faddr_t tcml_addr;
158 u32 tcml_size = SZ_128K;
159 ulong addr;
160
161 switch (core_id) {
162 case 0:
163 core_rsrc = SC_R_M4_0_PID0;
164 tcml_addr = 0x34FE0000;
165 mu_rsrc = SC_R_M4_0_MU_1A;
166 break;
167 case 1:
168 core_rsrc = SC_R_M4_1_PID0;
169 tcml_addr = 0x38FE0000;
170 mu_rsrc = SC_R_M4_1_MU_1A;
171 break;
172 default:
173 printf("Not support this core boot up, ID:%u\n", core_id);
174 return -EINVAL;
175 }
176
177 addr = (sc_faddr_t)boot_private_data;
178
179 if (addr >= tcml_addr && addr <= tcml_addr + tcml_size) {
180 printf("Wrong image address 0x%lx, should not in TCML\n",
181 addr);
182 return -EINVAL;
183 }
184
185 printf("Power on M4 and MU\n");
186
187 if (sc_pm_set_resource_power_mode(-1, core_rsrc, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
188 return -EIO;
189
190 if (sc_pm_set_resource_power_mode(-1, mu_rsrc, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
191 return -EIO;
192
193 printf("Copy M4 image from 0x%lx to TCML 0x%lx\n", addr, (ulong)tcml_addr);
194
195 if (addr != tcml_addr)
196 memcpy((void *)tcml_addr, (void *)addr, tcml_size);
197
198 printf("Start M4 %u\n", core_id);
199 if (sc_pm_cpu_start(-1, core_rsrc, true, tcml_addr) != SC_ERR_NONE)
200 return -EIO;
201
202 printf("bootaux complete\n");
203 return 0;
204}
205#endif
206
207#ifdef CONFIG_IMX8QXP
208int arch_auxiliary_core_up(u32 core_id, ulong boot_private_data)
209{
210 sc_rsrc_t core_rsrc, mu_rsrc = SC_R_NONE;
211 sc_faddr_t aux_core_ram;
212 u32 size;
213 ulong addr;
214
215 switch (core_id) {
216 case 0:
217 core_rsrc = SC_R_M4_0_PID0;
218 aux_core_ram = 0x34FE0000;
219 mu_rsrc = SC_R_M4_0_MU_1A;
220 size = SZ_128K;
221 break;
222 case 1:
223 core_rsrc = SC_R_DSP;
224 aux_core_ram = 0x596f8000;
225 size = SZ_2K;
226 break;
227 default:
228 printf("Not support this core boot up, ID:%u\n", core_id);
229 return -EINVAL;
230 }
231
232 addr = (sc_faddr_t)boot_private_data;
233
234 if (addr >= aux_core_ram && addr <= aux_core_ram + size) {
235 printf("Wrong image address 0x%lx, should not in aux core ram\n",
236 addr);
237 return -EINVAL;
238 }
239
240 printf("Power on aux core %d\n", core_id);
241
242 if (sc_pm_set_resource_power_mode(-1, core_rsrc, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
243 return -EIO;
244
245 if (mu_rsrc != SC_R_NONE) {
246 if (sc_pm_set_resource_power_mode(-1, mu_rsrc, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
247 return -EIO;
248 }
249
250 if (core_id == 1) {
251 struct power_domain pd;
252
253 if (sc_pm_clock_enable(-1, core_rsrc, SC_PM_CLK_PER, true, false) != SC_ERR_NONE) {
254 printf("Error enable clock\n");
255 return -EIO;
256 }
257
258 if (!power_domain_lookup_name("audio_sai0", &pd)) {
259 if (power_domain_on(&pd)) {
260 printf("Error power on SAI0\n");
261 return -EIO;
262 }
263 }
264
265 if (!power_domain_lookup_name("audio_ocram", &pd)) {
266 if (power_domain_on(&pd)) {
267 printf("Error power on HIFI RAM\n");
268 return -EIO;
269 }
270 }
271 }
272
273 printf("Copy image from 0x%lx to 0x%lx\n", addr, (ulong)aux_core_ram);
274 if (core_id == 0) {
275 /* M4 use bin file */
276 memcpy((void *)aux_core_ram, (void *)addr, size);
277 } else {
278 /* HIFI use elf file */
279 if (!valid_elf_image(addr))
280 return -1;
281 addr = load_elf_image_shdr(addr);
282 }
283
284 printf("Start %s\n", core_id == 0 ? "M4" : "HIFI");
285
286 if (sc_pm_cpu_start(-1, core_rsrc, true, aux_core_ram) != SC_ERR_NONE)
287 return -EIO;
288
289 printf("bootaux complete\n");
290 return 0;
291}
292#endif
293
294int arch_auxiliary_core_check_up(u32 core_id)
295{
296 sc_rsrc_t core_rsrc;
297 sc_pm_power_mode_t power_mode;
298
299 switch (core_id) {
300 case 0:
301 core_rsrc = SC_R_M4_0_PID0;
302 break;
303#ifdef CONFIG_IMX8QM
304 case 1:
305 core_rsrc = SC_R_M4_1_PID0;
306 break;
307#endif
308 default:
309 printf("Not support this core, ID:%u\n", core_id);
310 return 0;
311 }
312
313 if (sc_pm_get_resource_power_mode(-1, core_rsrc, &power_mode) != SC_ERR_NONE)
314 return 0;
315
316 if (power_mode != SC_PM_PW_MODE_OFF)
317 return 1;
318
319 return 0;
320}
321#endif
322
Peng Fan29c9dd32018-10-18 14:28:19 +0200323int print_bootinfo(void)
324{
325 enum boot_device bt_dev = get_boot_device();
326
327 puts("Boot: ");
328 switch (bt_dev) {
329 case SD1_BOOT:
330 puts("SD0\n");
331 break;
332 case SD2_BOOT:
333 puts("SD1\n");
334 break;
335 case SD3_BOOT:
336 puts("SD2\n");
337 break;
338 case MMC1_BOOT:
339 puts("MMC0\n");
340 break;
341 case MMC2_BOOT:
342 puts("MMC1\n");
343 break;
344 case MMC3_BOOT:
345 puts("MMC2\n");
346 break;
347 case FLEXSPI_BOOT:
348 puts("FLEXSPI\n");
349 break;
350 case SATA_BOOT:
351 puts("SATA\n");
352 break;
353 case NAND_BOOT:
354 puts("NAND\n");
355 break;
356 case USB_BOOT:
357 puts("USB\n");
358 break;
359 default:
360 printf("Unknown device %u\n", bt_dev);
361 break;
362 }
363
Igor Opaniukab5ffef2024-01-31 13:49:26 +0100364 printf("Reset cause: %s\n", get_reset_cause());
365
Peng Fan29c9dd32018-10-18 14:28:19 +0200366 return 0;
367}
368
369enum boot_device get_boot_device(void)
370{
371 enum boot_device boot_dev = SD1_BOOT;
372
373 sc_rsrc_t dev_rsrc;
374
375 sc_misc_get_boot_dev(-1, &dev_rsrc);
376
377 switch (dev_rsrc) {
378 case SC_R_SDHC_0:
379 boot_dev = MMC1_BOOT;
380 break;
381 case SC_R_SDHC_1:
382 boot_dev = SD2_BOOT;
383 break;
384 case SC_R_SDHC_2:
385 boot_dev = SD3_BOOT;
386 break;
387 case SC_R_NAND:
388 boot_dev = NAND_BOOT;
389 break;
390 case SC_R_FSPI_0:
391 boot_dev = FLEXSPI_BOOT;
392 break;
393 case SC_R_SATA_0:
394 boot_dev = SATA_BOOT;
395 break;
396 case SC_R_USB_0:
397 case SC_R_USB_1:
398 case SC_R_USB_2:
399 boot_dev = USB_BOOT;
400 break;
401 default:
402 break;
403 }
404
405 return boot_dev;
406}
Peng Fan93b6cfd2018-10-18 14:28:20 +0200407
Tom Riniae21e7f2021-08-30 09:16:29 -0400408#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
Peng Fand2aaf0c2020-05-05 20:28:39 +0800409#define FUSE_UNIQUE_ID_WORD0 16
410#define FUSE_UNIQUE_ID_WORD1 17
411void get_board_serial(struct tag_serialnr *serialnr)
412{
Peng Fan25b7eb42023-06-15 18:08:58 +0800413 int err;
Peng Fand2aaf0c2020-05-05 20:28:39 +0800414 u32 val1 = 0, val2 = 0;
415 u32 word1, word2;
416
417 if (!serialnr)
418 return;
419
420 word1 = FUSE_UNIQUE_ID_WORD0;
421 word2 = FUSE_UNIQUE_ID_WORD1;
422
423 err = sc_misc_otp_fuse_read(-1, word1, &val1);
Peng Fan25b7eb42023-06-15 18:08:58 +0800424 if (err) {
Peng Fand2aaf0c2020-05-05 20:28:39 +0800425 printf("%s fuse %d read error: %d\n", __func__, word1, err);
426 return;
427 }
428
429 err = sc_misc_otp_fuse_read(-1, word2, &val2);
Peng Fan25b7eb42023-06-15 18:08:58 +0800430 if (err) {
Peng Fand2aaf0c2020-05-05 20:28:39 +0800431 printf("%s fuse %d read error: %d\n", __func__, word2, err);
432 return;
433 }
434 serialnr->low = val1;
435 serialnr->high = val2;
436}
Tom Riniae21e7f2021-08-30 09:16:29 -0400437#endif /*CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG*/
Peng Fand2aaf0c2020-05-05 20:28:39 +0800438
Peng Fan93b6cfd2018-10-18 14:28:20 +0200439#ifdef CONFIG_ENV_IS_IN_MMC
440__weak int board_mmc_get_env_dev(int devno)
441{
442 return CONFIG_SYS_MMC_ENV_DEV;
443}
444
445int mmc_get_env_dev(void)
446{
447 sc_rsrc_t dev_rsrc;
448 int devno;
449
450 sc_misc_get_boot_dev(-1, &dev_rsrc);
451
452 switch (dev_rsrc) {
453 case SC_R_SDHC_0:
454 devno = 0;
455 break;
456 case SC_R_SDHC_1:
457 devno = 1;
458 break;
459 case SC_R_SDHC_2:
460 devno = 2;
461 break;
462 default:
463 /* If not boot from sd/mmc, use default value */
464 return CONFIG_SYS_MMC_ENV_DEV;
465 }
466
467 return board_mmc_get_env_dev(devno);
468}
469#endif
Peng Fan4f211a52018-10-18 14:28:21 +0200470
471#define MEMSTART_ALIGNMENT SZ_2M /* Align the memory start with 2MB */
472
473static int get_owned_memreg(sc_rm_mr_t mr, sc_faddr_t *addr_start,
474 sc_faddr_t *addr_end)
475{
476 sc_faddr_t start, end;
477 int ret;
478 bool owned;
479
480 owned = sc_rm_is_memreg_owned(-1, mr);
481 if (owned) {
482 ret = sc_rm_get_memreg_info(-1, mr, &start, &end);
483 if (ret) {
484 printf("Memreg get info failed, %d\n", ret);
485 return -EINVAL;
486 }
487 debug("0x%llx -- 0x%llx\n", start, end);
488 *addr_start = start;
489 *addr_end = end;
490
491 return 0;
492 }
493
494 return -EINVAL;
495}
496
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300497__weak void board_mem_get_layout(u64 *phys_sdram_1_start,
498 u64 *phys_sdram_1_size,
499 u64 *phys_sdram_2_start,
500 u64 *phys_sdram_2_size)
501{
502 *phys_sdram_1_start = PHYS_SDRAM_1;
503 *phys_sdram_1_size = PHYS_SDRAM_1_SIZE;
504 *phys_sdram_2_start = PHYS_SDRAM_2;
505 *phys_sdram_2_size = PHYS_SDRAM_2_SIZE;
506}
507
Peng Fan4f211a52018-10-18 14:28:21 +0200508phys_size_t get_effective_memsize(void)
509{
510 sc_rm_mr_t mr;
Ye Li7545bd12020-05-05 20:28:38 +0800511 sc_faddr_t start, end, end1, start_aligned;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300512 u64 phys_sdram_1_start, phys_sdram_1_size;
513 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200514 int err;
515
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300516 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
517 &phys_sdram_2_start, &phys_sdram_2_size);
518
Peng Fan4f211a52018-10-18 14:28:21 +0200519
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300520 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200521 for (mr = 0; mr < 64; mr++) {
522 err = get_owned_memreg(mr, &start, &end);
523 if (!err) {
Ye Li7545bd12020-05-05 20:28:38 +0800524 start_aligned = roundup(start, MEMSTART_ALIGNMENT);
Peng Fan4f211a52018-10-18 14:28:21 +0200525 /* Too small memory region, not use it */
Ye Li7545bd12020-05-05 20:28:38 +0800526 if (start_aligned > end)
Peng Fan4f211a52018-10-18 14:28:21 +0200527 continue;
528
Peng Fan14b4cd22018-10-18 14:28:22 +0200529 /* Find the memory region runs the U-Boot */
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300530 if (start >= phys_sdram_1_start && start <= end1 &&
Simon Glass72cc5382022-10-20 18:22:39 -0600531 (start <= CONFIG_TEXT_BASE &&
532 end >= CONFIG_TEXT_BASE)) {
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300533 if ((end + 1) <=
534 ((sc_faddr_t)phys_sdram_1_start +
535 phys_sdram_1_size))
536 return (end - phys_sdram_1_start + 1);
Peng Fan4f211a52018-10-18 14:28:21 +0200537 else
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300538 return phys_sdram_1_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200539 }
540 }
541 }
542
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300543 return phys_sdram_1_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200544}
545
546int dram_init(void)
547{
548 sc_rm_mr_t mr;
549 sc_faddr_t start, end, end1, end2;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300550 u64 phys_sdram_1_start, phys_sdram_1_size;
551 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200552 int err;
553
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300554 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
555 &phys_sdram_2_start, &phys_sdram_2_size);
556
557 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
558 end2 = (sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200559 for (mr = 0; mr < 64; mr++) {
560 err = get_owned_memreg(mr, &start, &end);
561 if (!err) {
562 start = roundup(start, MEMSTART_ALIGNMENT);
563 /* Too small memory region, not use it */
564 if (start > end)
565 continue;
566
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300567 if (start >= phys_sdram_1_start && start <= end1) {
Peng Fan4f211a52018-10-18 14:28:21 +0200568 if ((end + 1) <= end1)
569 gd->ram_size += end - start + 1;
570 else
571 gd->ram_size += end1 - start;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300572 } else if (start >= phys_sdram_2_start &&
573 start <= end2) {
Peng Fan4f211a52018-10-18 14:28:21 +0200574 if ((end + 1) <= end2)
575 gd->ram_size += end - start + 1;
576 else
577 gd->ram_size += end2 - start;
578 }
579 }
580 }
581
582 /* If error, set to the default value */
583 if (!gd->ram_size) {
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300584 gd->ram_size = phys_sdram_1_size;
585 gd->ram_size += phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200586 }
587 return 0;
588}
589
590static void dram_bank_sort(int current_bank)
591{
592 phys_addr_t start;
593 phys_size_t size;
594
595 while (current_bank > 0) {
596 if (gd->bd->bi_dram[current_bank - 1].start >
597 gd->bd->bi_dram[current_bank].start) {
598 start = gd->bd->bi_dram[current_bank - 1].start;
599 size = gd->bd->bi_dram[current_bank - 1].size;
600
601 gd->bd->bi_dram[current_bank - 1].start =
602 gd->bd->bi_dram[current_bank].start;
603 gd->bd->bi_dram[current_bank - 1].size =
604 gd->bd->bi_dram[current_bank].size;
605
606 gd->bd->bi_dram[current_bank].start = start;
607 gd->bd->bi_dram[current_bank].size = size;
608 }
609 current_bank--;
610 }
611}
612
613int dram_init_banksize(void)
614{
615 sc_rm_mr_t mr;
616 sc_faddr_t start, end, end1, end2;
617 int i = 0;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300618 u64 phys_sdram_1_start, phys_sdram_1_size;
619 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200620 int err;
621
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300622 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
623 &phys_sdram_2_start, &phys_sdram_2_size);
Peng Fan4f211a52018-10-18 14:28:21 +0200624
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300625 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
626 end2 = (sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200627 for (mr = 0; mr < 64 && i < CONFIG_NR_DRAM_BANKS; mr++) {
628 err = get_owned_memreg(mr, &start, &end);
629 if (!err) {
630 start = roundup(start, MEMSTART_ALIGNMENT);
631 if (start > end) /* Small memory region, no use it */
632 continue;
633
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300634 if (start >= phys_sdram_1_start && start <= end1) {
Peng Fan4f211a52018-10-18 14:28:21 +0200635 gd->bd->bi_dram[i].start = start;
636
637 if ((end + 1) <= end1)
638 gd->bd->bi_dram[i].size =
639 end - start + 1;
640 else
641 gd->bd->bi_dram[i].size = end1 - start;
642
643 dram_bank_sort(i);
644 i++;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300645 } else if (start >= phys_sdram_2_start && start <= end2) {
Peng Fan4f211a52018-10-18 14:28:21 +0200646 gd->bd->bi_dram[i].start = start;
647
648 if ((end + 1) <= end2)
649 gd->bd->bi_dram[i].size =
650 end - start + 1;
651 else
652 gd->bd->bi_dram[i].size = end2 - start;
653
654 dram_bank_sort(i);
655 i++;
656 }
657 }
658 }
659
660 /* If error, set to the default value */
661 if (!i) {
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300662 gd->bd->bi_dram[0].start = phys_sdram_1_start;
663 gd->bd->bi_dram[0].size = phys_sdram_1_size;
664 gd->bd->bi_dram[1].start = phys_sdram_2_start;
665 gd->bd->bi_dram[1].size = phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200666 }
667
668 return 0;
669}
670
671static u64 get_block_attrs(sc_faddr_t addr_start)
672{
673 u64 attr = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE |
674 PTE_BLOCK_PXN | PTE_BLOCK_UXN;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300675 u64 phys_sdram_1_start, phys_sdram_1_size;
676 u64 phys_sdram_2_start, phys_sdram_2_size;
677
678 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
679 &phys_sdram_2_start, &phys_sdram_2_size);
Peng Fan4f211a52018-10-18 14:28:21 +0200680
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300681 if ((addr_start >= phys_sdram_1_start &&
682 addr_start <= ((sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size)) ||
683 (addr_start >= phys_sdram_2_start &&
684 addr_start <= ((sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size)))
Peng Fan4f211a52018-10-18 14:28:21 +0200685 return (PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_OUTER_SHARE);
686
687 return attr;
688}
689
690static u64 get_block_size(sc_faddr_t addr_start, sc_faddr_t addr_end)
691{
692 sc_faddr_t end1, end2;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300693 u64 phys_sdram_1_start, phys_sdram_1_size;
694 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200695
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300696 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
697 &phys_sdram_2_start, &phys_sdram_2_size);
Peng Fan4f211a52018-10-18 14:28:21 +0200698
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300699
700 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
701 end2 = (sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size;
702
703 if (addr_start >= phys_sdram_1_start && addr_start <= end1) {
Peng Fan4f211a52018-10-18 14:28:21 +0200704 if ((addr_end + 1) > end1)
705 return end1 - addr_start;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300706 } else if (addr_start >= phys_sdram_2_start && addr_start <= end2) {
Peng Fan4f211a52018-10-18 14:28:21 +0200707 if ((addr_end + 1) > end2)
708 return end2 - addr_start;
709 }
710
711 return (addr_end - addr_start + 1);
712}
713
714#define MAX_PTE_ENTRIES 512
715#define MAX_MEM_MAP_REGIONS 16
716
717static struct mm_region imx8_mem_map[MAX_MEM_MAP_REGIONS];
718struct mm_region *mem_map = imx8_mem_map;
719
720void enable_caches(void)
721{
722 sc_rm_mr_t mr;
723 sc_faddr_t start, end;
724 int err, i;
725
726 /* Create map for registers access from 0x1c000000 to 0x80000000*/
727 imx8_mem_map[0].virt = 0x1c000000UL;
728 imx8_mem_map[0].phys = 0x1c000000UL;
729 imx8_mem_map[0].size = 0x64000000UL;
730 imx8_mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
731 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN;
732
733 i = 1;
734 for (mr = 0; mr < 64 && i < MAX_MEM_MAP_REGIONS; mr++) {
735 err = get_owned_memreg(mr, &start, &end);
736 if (!err) {
737 imx8_mem_map[i].virt = start;
738 imx8_mem_map[i].phys = start;
739 imx8_mem_map[i].size = get_block_size(start, end);
740 imx8_mem_map[i].attrs = get_block_attrs(start);
741 i++;
742 }
743 }
744
745 if (i < MAX_MEM_MAP_REGIONS) {
746 imx8_mem_map[i].size = 0;
747 imx8_mem_map[i].attrs = 0;
748 } else {
749 puts("Error, need more MEM MAP REGIONS reserved\n");
750 icache_enable();
751 return;
752 }
753
754 for (i = 0; i < MAX_MEM_MAP_REGIONS; i++) {
755 debug("[%d] vir = 0x%llx phys = 0x%llx size = 0x%llx attrs = 0x%llx\n",
756 i, imx8_mem_map[i].virt, imx8_mem_map[i].phys,
757 imx8_mem_map[i].size, imx8_mem_map[i].attrs);
758 }
759
760 icache_enable();
761 dcache_enable();
762}
763
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400764#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Peng Fan4f211a52018-10-18 14:28:21 +0200765u64 get_page_table_size(void)
766{
767 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
768 u64 size = 0;
769
770 /*
771 * For each memory region, the max table size:
772 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
773 */
774 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
775
776 /*
777 * We need to duplicate our page table once to have an emergency pt to
778 * resort to when splitting page tables later on
779 */
780 size *= 2;
781
782 /*
783 * We may need to split page tables later on if dcache settings change,
784 * so reserve up to 4 (random pick) page tables for that.
785 */
786 size += one_pt * 4;
787
788 return size;
789}
790#endif
Anatolij Gustschin05b354b2018-10-18 14:28:23 +0200791
Peng Fan303324d2019-08-26 08:12:23 +0000792#if defined(CONFIG_IMX8QM)
793#define FUSE_MAC0_WORD0 452
794#define FUSE_MAC0_WORD1 453
795#define FUSE_MAC1_WORD0 454
796#define FUSE_MAC1_WORD1 455
797#elif defined(CONFIG_IMX8QXP)
Anatolij Gustschin05b354b2018-10-18 14:28:23 +0200798#define FUSE_MAC0_WORD0 708
799#define FUSE_MAC0_WORD1 709
800#define FUSE_MAC1_WORD0 710
801#define FUSE_MAC1_WORD1 711
Peng Fan303324d2019-08-26 08:12:23 +0000802#endif
Anatolij Gustschin05b354b2018-10-18 14:28:23 +0200803
804void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
805{
806 u32 word[2], val[2] = {};
807 int i, ret;
808
809 if (dev_id == 0) {
810 word[0] = FUSE_MAC0_WORD0;
811 word[1] = FUSE_MAC0_WORD1;
812 } else {
813 word[0] = FUSE_MAC1_WORD0;
814 word[1] = FUSE_MAC1_WORD1;
815 }
816
817 for (i = 0; i < 2; i++) {
818 ret = sc_misc_otp_fuse_read(-1, word[i], &val[i]);
819 if (ret < 0)
820 goto err;
821 }
822
823 mac[0] = val[0];
824 mac[1] = val[0] >> 8;
825 mac[2] = val[0] >> 16;
826 mac[3] = val[0] >> 24;
827 mac[4] = val[1];
828 mac[5] = val[1] >> 8;
829
830 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
831 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
832 return;
833err:
834 printf("%s: fuse %d, err: %d\n", __func__, word[i], ret);
835}
Anatolij Gustschin9b39be92018-10-18 14:28:24 +0200836
Anatolij Gustschin9b39be92018-10-18 14:28:24 +0200837u32 get_cpu_rev(void)
838{
839 u32 id = 0, rev = 0;
840 int ret;
841
842 ret = sc_misc_get_control(-1, SC_R_SYSTEM, SC_C_ID, &id);
843 if (ret)
844 return 0;
845
846 rev = (id >> 5) & 0xf;
847 id = (id & 0x1f) + MXC_SOC_IMX8; /* Dummy ID for chip */
848
849 return (id << 12) | rev;
850}
Ye Lic3169bd2020-05-05 20:28:42 +0800851
852void board_boot_order(u32 *spl_boot_list)
853{
854 spl_boot_list[0] = spl_boot_device();
855
856 if (spl_boot_list[0] == BOOT_DEVICE_SPI) {
857 /* Check whether we own the flexspi0, if not, use NOR boot */
858 if (!sc_rm_is_resource_owned(-1, SC_R_FSPI_0))
859 spl_boot_list[0] = BOOT_DEVICE_NOR;
860 }
861}
Peng Fan2d65a162020-05-05 20:28:43 +0800862
863bool m4_parts_booted(void)
864{
865 sc_rm_pt_t m4_parts[2];
866 int err;
867
868 err = sc_rm_get_resource_owner(-1, SC_R_M4_0_PID0, &m4_parts[0]);
869 if (err) {
870 printf("%s get resource [%d] owner error: %d\n", __func__,
871 SC_R_M4_0_PID0, err);
872 return false;
873 }
874
875 if (sc_pm_is_partition_started(-1, m4_parts[0]))
876 return true;
877
878 if (is_imx8qm()) {
879 err = sc_rm_get_resource_owner(-1, SC_R_M4_1_PID0, &m4_parts[1]);
880 if (err) {
881 printf("%s get resource [%d] owner error: %d\n",
882 __func__, SC_R_M4_1_PID0, err);
883 return false;
884 }
885
886 if (sc_pm_is_partition_started(-1, m4_parts[1]))
887 return true;
888 }
889
890 return false;
891}