blob: accba502e4924e53ae1b1306234ac4d6bbb620a1 [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
Peng Fan2e6be072018-10-18 14:28:18 +02006#include <clk.h>
Anatolij Gustschin9b39be92018-10-18 14:28:24 +02007#include <cpu.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07008#include <cpu_func.h>
Peng Fan2e6be072018-10-18 14:28:18 +02009#include <dm.h>
Simon Glassfc557362022-03-04 08:43:05 -070010#include <event.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070011#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060013#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Peng Fan2e6be072018-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 Fan617fc292020-05-05 20:28:41 +080019#include <spl.h>
Peng Fan48f9c4e2019-04-26 01:44:27 +000020#include <thermal.h>
Peng Fan2e0644a2023-04-28 12:08:09 +080021#include <firmware/imx/sci/sci.h>
Peng Fan29c9dd32018-10-18 14:28:19 +020022#include <asm/arch/sys_proto.h>
Peng Fan2e6be072018-10-18 14:28:18 +020023#include <asm/arch-imx/cpu.h>
24#include <asm/armv8/cpu.h>
Peng Fan4f211a52018-10-18 14:28:21 +020025#include <asm/armv8/mmu.h>
Peng Fand2aaf0c2020-05-05 20:28:39 +080026#include <asm/setup.h>
Peng Fan29c9dd32018-10-18 14:28:19 +020027#include <asm/mach-imx/boot_mode.h>
Ye Lic07ac742023-06-15 18:09:20 +080028#include <power-domain.h>
29#include <elf.h>
Ye Lic3169bd2020-05-05 20:28:42 +080030#include <spl.h>
Peng Fan2e6be072018-10-18 14:28:18 +020031
32DECLARE_GLOBAL_DATA_PTR;
33
Peng Fan14b4cd22018-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 Opaniukab5ffef2024-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 Estevam70b39d02024-03-26 09:19:49 -030086__weak void reset_cpu(void)
87{
88}
89
Peng Fan14b4cd22018-10-18 14:28:22 +020090int arch_cpu_init(void)
91{
Peng Fan617fc292020-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 Fan0bcec7f2019-01-18 08:58:38 +000096#ifdef CONFIG_SPL_BUILD
97 struct pass_over_info_t *pass_over;
Peng Fan14b4cd22018-10-18 14:28:22 +020098
Peng Fan0bcec7f2019-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 Fan14b4cd22018-10-18 14:28:22 +0200108 }
Peng Fan0bcec7f2019-01-18 08:58:38 +0000109#endif
Peng Fan14b4cd22018-10-18 14:28:22 +0200110
111 return 0;
112}
113
Simon Glassb8357c12023-08-21 21:16:56 -0600114static int imx8_init_mu(void)
Peng Fan14b4cd22018-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 Fan14b4cd22018-10-18 14:28:22 +0200120
Ye Lif2ea6f02019-08-26 08:11:42 +0000121 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
Peng Fan14b4cd22018-10-18 14:28:22 +0200122 if (ret) {
Ye Lif2ea6f02019-08-26 08:11:42 +0000123 printf("could not get scu %d\n", ret);
Peng Fan14b4cd22018-10-18 14:28:22 +0200124 return ret;
125 }
126
Peng Fanee380c52019-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 Fan14b4cd22018-10-18 14:28:22 +0200134 return 0;
135}
Simon Glassb8357c12023-08-21 21:16:56 -0600136EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_F, imx8_init_mu);
Peng Fan14b4cd22018-10-18 14:28:22 +0200137
Gaurav Jaindb4dd6a2022-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 Liec346892022-05-11 13:56:20 +0530147 printf("Failed to initialize caam_jr: %d\n", ret);
Gaurav Jaindb4dd6a2022-03-24 11:50:33 +0530148 }
149
150 return 0;
151}
152#endif
153
Ye Lic07ac742023-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 Fan29c9dd32018-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 Opaniukab5ffef2024-01-31 13:49:26 +0100367 printf("Reset cause: %s\n", get_reset_cause());
368
Peng Fan29c9dd32018-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 Fan93b6cfd2018-10-18 14:28:20 +0200410
Tom Riniae21e7f2021-08-30 09:16:29 -0400411#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
Peng Fand2aaf0c2020-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 Fan25b7eb42023-06-15 18:08:58 +0800416 int err;
Peng Fand2aaf0c2020-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 Fan25b7eb42023-06-15 18:08:58 +0800427 if (err) {
Peng Fand2aaf0c2020-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 Fan25b7eb42023-06-15 18:08:58 +0800433 if (err) {
Peng Fand2aaf0c2020-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 Riniae21e7f2021-08-30 09:16:29 -0400440#endif /*CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG*/
Peng Fand2aaf0c2020-05-05 20:28:39 +0800441
Peng Fan93b6cfd2018-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 Fan4f211a52018-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 Ziswilera45585b2020-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 Fan4f211a52018-10-18 14:28:21 +0200511phys_size_t get_effective_memsize(void)
512{
513 sc_rm_mr_t mr;
Ye Li7545bd12020-05-05 20:28:38 +0800514 sc_faddr_t start, end, end1, start_aligned;
Marcel Ziswilera45585b2020-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 Fan4f211a52018-10-18 14:28:21 +0200517 int err;
518
Marcel Ziswilera45585b2020-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);
521
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300522 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200523 for (mr = 0; mr < 64; mr++) {
524 err = get_owned_memreg(mr, &start, &end);
525 if (!err) {
Ye Li7545bd12020-05-05 20:28:38 +0800526 start_aligned = roundup(start, MEMSTART_ALIGNMENT);
Peng Fan4f211a52018-10-18 14:28:21 +0200527 /* Too small memory region, not use it */
Ye Li7545bd12020-05-05 20:28:38 +0800528 if (start_aligned > end)
Peng Fan4f211a52018-10-18 14:28:21 +0200529 continue;
530
Peng Fan14b4cd22018-10-18 14:28:22 +0200531 /* Find the memory region runs the U-Boot */
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300532 if (start >= phys_sdram_1_start && start <= end1 &&
Simon Glass72cc5382022-10-20 18:22:39 -0600533 (start <= CONFIG_TEXT_BASE &&
534 end >= CONFIG_TEXT_BASE)) {
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300535 if ((end + 1) <=
536 ((sc_faddr_t)phys_sdram_1_start +
537 phys_sdram_1_size))
538 return (end - phys_sdram_1_start + 1);
Peng Fan4f211a52018-10-18 14:28:21 +0200539 else
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300540 return phys_sdram_1_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200541 }
542 }
543 }
544
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300545 return phys_sdram_1_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200546}
547
548int dram_init(void)
549{
550 sc_rm_mr_t mr;
551 sc_faddr_t start, end, end1, end2;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300552 u64 phys_sdram_1_start, phys_sdram_1_size;
553 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200554 int err;
555
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300556 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
557 &phys_sdram_2_start, &phys_sdram_2_size);
558
559 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
560 end2 = (sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200561 for (mr = 0; mr < 64; mr++) {
562 err = get_owned_memreg(mr, &start, &end);
563 if (!err) {
564 start = roundup(start, MEMSTART_ALIGNMENT);
565 /* Too small memory region, not use it */
566 if (start > end)
567 continue;
568
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300569 if (start >= phys_sdram_1_start && start <= end1) {
Peng Fan4f211a52018-10-18 14:28:21 +0200570 if ((end + 1) <= end1)
571 gd->ram_size += end - start + 1;
572 else
573 gd->ram_size += end1 - start;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300574 } else if (start >= phys_sdram_2_start &&
575 start <= end2) {
Peng Fan4f211a52018-10-18 14:28:21 +0200576 if ((end + 1) <= end2)
577 gd->ram_size += end - start + 1;
578 else
579 gd->ram_size += end2 - start;
580 }
581 }
582 }
583
584 /* If error, set to the default value */
585 if (!gd->ram_size) {
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300586 gd->ram_size = phys_sdram_1_size;
587 gd->ram_size += phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200588 }
589 return 0;
590}
591
592static void dram_bank_sort(int current_bank)
593{
594 phys_addr_t start;
595 phys_size_t size;
596
597 while (current_bank > 0) {
598 if (gd->bd->bi_dram[current_bank - 1].start >
599 gd->bd->bi_dram[current_bank].start) {
600 start = gd->bd->bi_dram[current_bank - 1].start;
601 size = gd->bd->bi_dram[current_bank - 1].size;
602
603 gd->bd->bi_dram[current_bank - 1].start =
604 gd->bd->bi_dram[current_bank].start;
605 gd->bd->bi_dram[current_bank - 1].size =
606 gd->bd->bi_dram[current_bank].size;
607
608 gd->bd->bi_dram[current_bank].start = start;
609 gd->bd->bi_dram[current_bank].size = size;
610 }
611 current_bank--;
612 }
613}
614
615int dram_init_banksize(void)
616{
617 sc_rm_mr_t mr;
618 sc_faddr_t start, end, end1, end2;
619 int i = 0;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300620 u64 phys_sdram_1_start, phys_sdram_1_size;
621 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200622 int err;
623
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300624 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
625 &phys_sdram_2_start, &phys_sdram_2_size);
Peng Fan4f211a52018-10-18 14:28:21 +0200626
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300627 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
628 end2 = (sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200629 for (mr = 0; mr < 64 && i < CONFIG_NR_DRAM_BANKS; mr++) {
630 err = get_owned_memreg(mr, &start, &end);
631 if (!err) {
632 start = roundup(start, MEMSTART_ALIGNMENT);
633 if (start > end) /* Small memory region, no use it */
634 continue;
635
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300636 if (start >= phys_sdram_1_start && start <= end1) {
Peng Fan4f211a52018-10-18 14:28:21 +0200637 gd->bd->bi_dram[i].start = start;
638
639 if ((end + 1) <= end1)
640 gd->bd->bi_dram[i].size =
641 end - start + 1;
642 else
643 gd->bd->bi_dram[i].size = end1 - start;
644
645 dram_bank_sort(i);
646 i++;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300647 } else if (start >= phys_sdram_2_start && start <= end2) {
Peng Fan4f211a52018-10-18 14:28:21 +0200648 gd->bd->bi_dram[i].start = start;
649
650 if ((end + 1) <= end2)
651 gd->bd->bi_dram[i].size =
652 end - start + 1;
653 else
654 gd->bd->bi_dram[i].size = end2 - start;
655
656 dram_bank_sort(i);
657 i++;
658 }
659 }
660 }
661
662 /* If error, set to the default value */
663 if (!i) {
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300664 gd->bd->bi_dram[0].start = phys_sdram_1_start;
665 gd->bd->bi_dram[0].size = phys_sdram_1_size;
666 gd->bd->bi_dram[1].start = phys_sdram_2_start;
667 gd->bd->bi_dram[1].size = phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200668 }
669
670 return 0;
671}
672
673static u64 get_block_attrs(sc_faddr_t addr_start)
674{
675 u64 attr = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE |
676 PTE_BLOCK_PXN | PTE_BLOCK_UXN;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300677 u64 phys_sdram_1_start, phys_sdram_1_size;
678 u64 phys_sdram_2_start, phys_sdram_2_size;
679
680 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
681 &phys_sdram_2_start, &phys_sdram_2_size);
Peng Fan4f211a52018-10-18 14:28:21 +0200682
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300683 if ((addr_start >= phys_sdram_1_start &&
684 addr_start <= ((sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size)) ||
685 (addr_start >= phys_sdram_2_start &&
686 addr_start <= ((sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size)))
Peng Fan4f211a52018-10-18 14:28:21 +0200687 return (PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_OUTER_SHARE);
688
689 return attr;
690}
691
692static u64 get_block_size(sc_faddr_t addr_start, sc_faddr_t addr_end)
693{
694 sc_faddr_t end1, end2;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300695 u64 phys_sdram_1_start, phys_sdram_1_size;
696 u64 phys_sdram_2_start, phys_sdram_2_size;
Peng Fan4f211a52018-10-18 14:28:21 +0200697
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300698 board_mem_get_layout(&phys_sdram_1_start, &phys_sdram_1_size,
699 &phys_sdram_2_start, &phys_sdram_2_size);
Peng Fan4f211a52018-10-18 14:28:21 +0200700
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300701 end1 = (sc_faddr_t)phys_sdram_1_start + phys_sdram_1_size;
702 end2 = (sc_faddr_t)phys_sdram_2_start + phys_sdram_2_size;
703
704 if (addr_start >= phys_sdram_1_start && addr_start <= end1) {
Peng Fan4f211a52018-10-18 14:28:21 +0200705 if ((addr_end + 1) > end1)
706 return end1 - addr_start;
Marcel Ziswilera45585b2020-10-22 11:21:40 +0300707 } else if (addr_start >= phys_sdram_2_start && addr_start <= end2) {
Peng Fan4f211a52018-10-18 14:28:21 +0200708 if ((addr_end + 1) > end2)
709 return end2 - addr_start;
710 }
711
712 return (addr_end - addr_start + 1);
713}
714
715#define MAX_PTE_ENTRIES 512
716#define MAX_MEM_MAP_REGIONS 16
717
718static struct mm_region imx8_mem_map[MAX_MEM_MAP_REGIONS];
719struct mm_region *mem_map = imx8_mem_map;
720
721void enable_caches(void)
722{
723 sc_rm_mr_t mr;
724 sc_faddr_t start, end;
725 int err, i;
726
727 /* Create map for registers access from 0x1c000000 to 0x80000000*/
728 imx8_mem_map[0].virt = 0x1c000000UL;
729 imx8_mem_map[0].phys = 0x1c000000UL;
730 imx8_mem_map[0].size = 0x64000000UL;
731 imx8_mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
732 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN;
733
734 i = 1;
735 for (mr = 0; mr < 64 && i < MAX_MEM_MAP_REGIONS; mr++) {
736 err = get_owned_memreg(mr, &start, &end);
737 if (!err) {
738 imx8_mem_map[i].virt = start;
739 imx8_mem_map[i].phys = start;
740 imx8_mem_map[i].size = get_block_size(start, end);
741 imx8_mem_map[i].attrs = get_block_attrs(start);
742 i++;
743 }
744 }
745
746 if (i < MAX_MEM_MAP_REGIONS) {
747 imx8_mem_map[i].size = 0;
748 imx8_mem_map[i].attrs = 0;
749 } else {
750 puts("Error, need more MEM MAP REGIONS reserved\n");
751 icache_enable();
752 return;
753 }
754
755 for (i = 0; i < MAX_MEM_MAP_REGIONS; i++) {
756 debug("[%d] vir = 0x%llx phys = 0x%llx size = 0x%llx attrs = 0x%llx\n",
757 i, imx8_mem_map[i].virt, imx8_mem_map[i].phys,
758 imx8_mem_map[i].size, imx8_mem_map[i].attrs);
759 }
760
761 icache_enable();
762 dcache_enable();
763}
764
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400765#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Peng Fan4f211a52018-10-18 14:28:21 +0200766u64 get_page_table_size(void)
767{
768 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
769 u64 size = 0;
770
771 /*
772 * For each memory region, the max table size:
773 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
774 */
775 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
776
777 /*
778 * We need to duplicate our page table once to have an emergency pt to
779 * resort to when splitting page tables later on
780 */
781 size *= 2;
782
783 /*
784 * We may need to split page tables later on if dcache settings change,
785 * so reserve up to 4 (random pick) page tables for that.
786 */
787 size += one_pt * 4;
788
789 return size;
790}
791#endif
Anatolij Gustschin05b354b2018-10-18 14:28:23 +0200792
Peng Fan303324d2019-08-26 08:12:23 +0000793#if defined(CONFIG_IMX8QM)
794#define FUSE_MAC0_WORD0 452
795#define FUSE_MAC0_WORD1 453
796#define FUSE_MAC1_WORD0 454
797#define FUSE_MAC1_WORD1 455
798#elif defined(CONFIG_IMX8QXP)
Anatolij Gustschin05b354b2018-10-18 14:28:23 +0200799#define FUSE_MAC0_WORD0 708
800#define FUSE_MAC0_WORD1 709
801#define FUSE_MAC1_WORD0 710
802#define FUSE_MAC1_WORD1 711
Peng Fan303324d2019-08-26 08:12:23 +0000803#endif
Anatolij Gustschin05b354b2018-10-18 14:28:23 +0200804
805void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
806{
807 u32 word[2], val[2] = {};
808 int i, ret;
809
810 if (dev_id == 0) {
811 word[0] = FUSE_MAC0_WORD0;
812 word[1] = FUSE_MAC0_WORD1;
813 } else {
814 word[0] = FUSE_MAC1_WORD0;
815 word[1] = FUSE_MAC1_WORD1;
816 }
817
818 for (i = 0; i < 2; i++) {
819 ret = sc_misc_otp_fuse_read(-1, word[i], &val[i]);
820 if (ret < 0)
821 goto err;
822 }
823
824 mac[0] = val[0];
825 mac[1] = val[0] >> 8;
826 mac[2] = val[0] >> 16;
827 mac[3] = val[0] >> 24;
828 mac[4] = val[1];
829 mac[5] = val[1] >> 8;
830
831 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
832 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
833 return;
834err:
835 printf("%s: fuse %d, err: %d\n", __func__, word[i], ret);
836}
Anatolij Gustschin9b39be92018-10-18 14:28:24 +0200837
Anatolij Gustschin9b39be92018-10-18 14:28:24 +0200838u32 get_cpu_rev(void)
839{
840 u32 id = 0, rev = 0;
841 int ret;
842
843 ret = sc_misc_get_control(-1, SC_R_SYSTEM, SC_C_ID, &id);
844 if (ret)
845 return 0;
846
847 rev = (id >> 5) & 0xf;
848 id = (id & 0x1f) + MXC_SOC_IMX8; /* Dummy ID for chip */
849
850 return (id << 12) | rev;
851}
Ye Lic3169bd2020-05-05 20:28:42 +0800852
853void board_boot_order(u32 *spl_boot_list)
854{
855 spl_boot_list[0] = spl_boot_device();
856
857 if (spl_boot_list[0] == BOOT_DEVICE_SPI) {
858 /* Check whether we own the flexspi0, if not, use NOR boot */
859 if (!sc_rm_is_resource_owned(-1, SC_R_FSPI_0))
860 spl_boot_list[0] = BOOT_DEVICE_NOR;
861 }
862}
Peng Fan2d65a162020-05-05 20:28:43 +0800863
864bool m4_parts_booted(void)
865{
866 sc_rm_pt_t m4_parts[2];
867 int err;
868
869 err = sc_rm_get_resource_owner(-1, SC_R_M4_0_PID0, &m4_parts[0]);
870 if (err) {
871 printf("%s get resource [%d] owner error: %d\n", __func__,
872 SC_R_M4_0_PID0, err);
873 return false;
874 }
875
876 if (sc_pm_is_partition_started(-1, m4_parts[0]))
877 return true;
878
879 if (is_imx8qm()) {
880 err = sc_rm_get_resource_owner(-1, SC_R_M4_1_PID0, &m4_parts[1]);
881 if (err) {
882 printf("%s get resource [%d] owner error: %d\n",
883 __func__, SC_R_M4_1_PID0, err);
884 return false;
885 }
886
887 if (sc_pm_is_partition_started(-1, m4_parts[1]))
888 return true;
889 }
890
891 return false;
892}