Yanhong Wang | e28ec34 | 2023-03-29 11:42:08 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2022 StarFive Technology Co., Ltd. |
| 4 | * Author: Yanhong Wang<yanhong.wang@starfivetech.com> |
| 5 | */ |
Yanhong Wang | f69a512 | 2023-06-15 17:36:51 +0800 | [diff] [blame] | 6 | #include <common.h> |
| 7 | #include <asm/arch/eeprom.h> |
Yanhong Wang | e28ec34 | 2023-03-29 11:42:08 +0800 | [diff] [blame] | 8 | #include <asm/csr.h> |
| 9 | #include <asm/sections.h> |
| 10 | #include <dm.h> |
Yanhong Wang | f69a512 | 2023-06-15 17:36:51 +0800 | [diff] [blame] | 11 | #include <linux/sizes.h> |
Yanhong Wang | e28ec34 | 2023-03-29 11:42:08 +0800 | [diff] [blame] | 12 | #include <log.h> |
Yanhong Wang | f69a512 | 2023-06-15 17:36:51 +0800 | [diff] [blame] | 13 | #include <init.h> |
Yanhong Wang | e28ec34 | 2023-03-29 11:42:08 +0800 | [diff] [blame] | 14 | |
| 15 | #define CSR_U74_FEATURE_DISABLE 0x7c1 |
| 16 | #define L2_LIM_MEM_END 0x81FFFFFUL |
| 17 | |
Yanhong Wang | f69a512 | 2023-06-15 17:36:51 +0800 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
| 20 | static bool check_ddr_size(phys_size_t size) |
| 21 | { |
| 22 | switch (size) { |
| 23 | case SZ_2: |
| 24 | case SZ_4: |
| 25 | case SZ_8: |
| 26 | case SZ_16: |
| 27 | return true; |
| 28 | default: |
| 29 | return false; |
| 30 | } |
| 31 | } |
| 32 | |
Yanhong Wang | e28ec34 | 2023-03-29 11:42:08 +0800 | [diff] [blame] | 33 | int spl_soc_init(void) |
| 34 | { |
| 35 | int ret; |
| 36 | struct udevice *dev; |
Yanhong Wang | f69a512 | 2023-06-15 17:36:51 +0800 | [diff] [blame] | 37 | phys_size_t size; |
| 38 | |
| 39 | ret = fdtdec_setup_mem_size_base(); |
| 40 | if (ret) |
| 41 | return ret; |
| 42 | |
| 43 | /* Read the definition of the DDR size from eeprom, and if not, |
| 44 | * use the definition in DT |
| 45 | */ |
| 46 | size = (get_ddr_size_from_eeprom() >> 16) & 0xFF; |
| 47 | if (check_ddr_size(size)) |
| 48 | gd->ram_size = size << 30; |
Yanhong Wang | e28ec34 | 2023-03-29 11:42:08 +0800 | [diff] [blame] | 49 | |
| 50 | /* DDR init */ |
| 51 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 52 | if (ret) { |
| 53 | debug("DRAM init failed: %d\n", ret); |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | void harts_early_init(void) |
| 61 | { |
| 62 | ulong *ptr; |
| 63 | u8 *tmp; |
| 64 | ulong len, remain; |
| 65 | /* |
| 66 | * Feature Disable CSR |
| 67 | * |
| 68 | * Clear feature disable CSR to '0' to turn on all features for |
| 69 | * each core. This operation must be in M-mode. |
| 70 | */ |
| 71 | if (CONFIG_IS_ENABLED(RISCV_MMODE)) |
| 72 | csr_write(CSR_U74_FEATURE_DISABLE, 0); |
| 73 | |
| 74 | /* clear L2 LIM memory |
| 75 | * set __bss_end to 0x81FFFFF region to zero |
| 76 | * The L2 Cache Controller supports ECC. ECC is applied to SRAM. |
| 77 | * If it is not cleared, the ECC part is invalid, and an ECC error |
| 78 | * will be reported when reading data. |
| 79 | */ |
| 80 | ptr = (ulong *)&__bss_end; |
| 81 | len = L2_LIM_MEM_END - (ulong)&__bss_end; |
| 82 | remain = len % sizeof(ulong); |
| 83 | len /= sizeof(ulong); |
| 84 | |
| 85 | while (len--) |
| 86 | *ptr++ = 0; |
| 87 | |
| 88 | /* clear the remain bytes */ |
| 89 | if (remain) { |
| 90 | tmp = (u8 *)ptr; |
| 91 | while (remain--) |
| 92 | *tmp++ = 0; |
| 93 | } |
| 94 | } |