Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Rockchip Electronics Co., Ltd. |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | 67c4e9f | 2019-11-14 12:57:45 -0700 | [diff] [blame] | 8 | #include <init.h> |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 9 | #include <ram.h> |
| 10 | #include <asm/io.h> |
Kever Yang | 5d19ddf | 2019-11-15 11:04:33 +0800 | [diff] [blame] | 11 | #include <asm/arch-rockchip/sdram.h> |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 12 | #include <dm/uclass-internal.h> |
| 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
Kever Yang | 5eb9a78 | 2019-07-22 20:02:02 +0800 | [diff] [blame] | 15 | |
| 16 | #define TRUST_PARAMETER_OFFSET (34 * 1024 * 1024) |
| 17 | |
| 18 | struct tos_parameter_t { |
| 19 | u32 version; |
| 20 | u32 checksum; |
| 21 | struct { |
| 22 | char name[8]; |
| 23 | s64 phy_addr; |
| 24 | u32 size; |
| 25 | u32 flags; |
| 26 | } tee_mem; |
| 27 | struct { |
| 28 | char name[8]; |
| 29 | s64 phy_addr; |
| 30 | u32 size; |
| 31 | u32 flags; |
| 32 | } drm_mem; |
| 33 | s64 reserve[8]; |
| 34 | }; |
| 35 | |
| 36 | int dram_init_banksize(void) |
| 37 | { |
| 38 | size_t top = min((unsigned long)(gd->ram_size + CONFIG_SYS_SDRAM_BASE), |
| 39 | gd->ram_top); |
| 40 | |
| 41 | #ifdef CONFIG_ARM64 |
| 42 | /* Reserve 0x200000 for ATF bl31 */ |
| 43 | gd->bd->bi_dram[0].start = 0x200000; |
| 44 | gd->bd->bi_dram[0].size = top - gd->bd->bi_dram[0].start; |
| 45 | #else |
| 46 | #ifdef CONFIG_SPL_OPTEE |
| 47 | struct tos_parameter_t *tos_parameter; |
| 48 | |
| 49 | tos_parameter = (struct tos_parameter_t *)(CONFIG_SYS_SDRAM_BASE + |
| 50 | TRUST_PARAMETER_OFFSET); |
| 51 | |
| 52 | if (tos_parameter->tee_mem.flags == 1) { |
| 53 | gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE; |
| 54 | gd->bd->bi_dram[0].size = tos_parameter->tee_mem.phy_addr |
| 55 | - CONFIG_SYS_SDRAM_BASE; |
| 56 | gd->bd->bi_dram[1].start = tos_parameter->tee_mem.phy_addr + |
| 57 | tos_parameter->tee_mem.size; |
| 58 | gd->bd->bi_dram[1].size = gd->bd->bi_dram[0].start |
| 59 | + top - gd->bd->bi_dram[1].start; |
| 60 | } else { |
| 61 | gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE; |
| 62 | gd->bd->bi_dram[0].size = 0x8400000; |
| 63 | /* Reserve 32M for OPTEE with TA */ |
| 64 | gd->bd->bi_dram[1].start = CONFIG_SYS_SDRAM_BASE |
| 65 | + gd->bd->bi_dram[0].size + 0x2000000; |
| 66 | gd->bd->bi_dram[1].size = gd->bd->bi_dram[0].start |
| 67 | + top - gd->bd->bi_dram[1].start; |
| 68 | } |
| 69 | #else |
| 70 | gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE; |
| 71 | gd->bd->bi_dram[0].size = top - gd->bd->bi_dram[0].start; |
| 72 | #endif |
| 73 | #endif |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 78 | size_t rockchip_sdram_size(phys_addr_t reg) |
| 79 | { |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 80 | u32 rank, cs0_col, bk, cs0_row, cs1_row, bw, row_3_4; |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 81 | size_t chipsize_mb = 0; |
| 82 | size_t size_mb = 0; |
| 83 | u32 ch; |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 84 | u32 cs1_col = 0; |
| 85 | u32 bg = 0; |
| 86 | u32 dbw, dram_type; |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 87 | u32 sys_reg2 = readl(reg); |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 88 | u32 sys_reg3 = readl(reg + 4); |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 89 | u32 ch_num = 1 + ((sys_reg2 >> SYS_REG_NUM_CH_SHIFT) |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 90 | & SYS_REG_NUM_CH_MASK); |
| 91 | |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 92 | dram_type = (sys_reg2 >> SYS_REG_DDRTYPE_SHIFT) & SYS_REG_DDRTYPE_MASK; |
| 93 | debug("%s %x %x\n", __func__, (u32)reg, sys_reg2); |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 94 | for (ch = 0; ch < ch_num; ch++) { |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 95 | rank = 1 + (sys_reg2 >> SYS_REG_RANK_SHIFT(ch) & |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 96 | SYS_REG_RANK_MASK); |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 97 | cs0_col = 9 + (sys_reg2 >> SYS_REG_COL_SHIFT(ch) & |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 98 | SYS_REG_COL_MASK); |
| 99 | cs1_col = cs0_col; |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 100 | bk = 3 - ((sys_reg2 >> SYS_REG_BK_SHIFT(ch)) & SYS_REG_BK_MASK); |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 101 | if ((sys_reg3 >> SYS_REG_VERSION_SHIFT & |
| 102 | SYS_REG_VERSION_MASK) == 0x2) { |
| 103 | cs1_col = 9 + (sys_reg3 >> SYS_REG_CS1_COL_SHIFT(ch) & |
| 104 | SYS_REG_CS1_COL_MASK); |
| 105 | if (((sys_reg3 >> SYS_REG_EXTEND_CS0_ROW_SHIFT(ch) & |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 106 | SYS_REG_EXTEND_CS0_ROW_MASK) << 2) + (sys_reg2 >> |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 107 | SYS_REG_CS0_ROW_SHIFT(ch) & |
| 108 | SYS_REG_CS0_ROW_MASK) == 7) |
| 109 | cs0_row = 12; |
| 110 | else |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 111 | cs0_row = 13 + (sys_reg2 >> |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 112 | SYS_REG_CS0_ROW_SHIFT(ch) & |
| 113 | SYS_REG_CS0_ROW_MASK) + |
| 114 | ((sys_reg3 >> |
| 115 | SYS_REG_EXTEND_CS0_ROW_SHIFT(ch) & |
| 116 | SYS_REG_EXTEND_CS0_ROW_MASK) << 2); |
| 117 | if (((sys_reg3 >> SYS_REG_EXTEND_CS1_ROW_SHIFT(ch) & |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 118 | SYS_REG_EXTEND_CS1_ROW_MASK) << 2) + (sys_reg2 >> |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 119 | SYS_REG_CS1_ROW_SHIFT(ch) & |
| 120 | SYS_REG_CS1_ROW_MASK) == 7) |
| 121 | cs1_row = 12; |
| 122 | else |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 123 | cs1_row = 13 + (sys_reg2 >> |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 124 | SYS_REG_CS1_ROW_SHIFT(ch) & |
| 125 | SYS_REG_CS1_ROW_MASK) + |
| 126 | ((sys_reg3 >> |
| 127 | SYS_REG_EXTEND_CS1_ROW_SHIFT(ch) & |
| 128 | SYS_REG_EXTEND_CS1_ROW_MASK) << 2); |
| 129 | } else { |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 130 | cs0_row = 13 + (sys_reg2 >> SYS_REG_CS0_ROW_SHIFT(ch) & |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 131 | SYS_REG_CS0_ROW_MASK); |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 132 | cs1_row = 13 + (sys_reg2 >> SYS_REG_CS1_ROW_SHIFT(ch) & |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 133 | SYS_REG_CS1_ROW_MASK); |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 134 | } |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 135 | bw = (2 >> ((sys_reg2 >> SYS_REG_BW_SHIFT(ch)) & |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 136 | SYS_REG_BW_MASK)); |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 137 | row_3_4 = sys_reg2 >> SYS_REG_ROW_3_4_SHIFT(ch) & |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 138 | SYS_REG_ROW_3_4_MASK; |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 139 | if (dram_type == DDR4) { |
Kever Yang | c7becc3 | 2019-11-15 11:04:36 +0800 | [diff] [blame] | 140 | dbw = (sys_reg2 >> SYS_REG_DBW_SHIFT(ch)) & |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 141 | SYS_REG_DBW_MASK; |
| 142 | bg = (dbw == 2) ? 2 : 1; |
| 143 | } |
| 144 | chipsize_mb = (1 << (cs0_row + cs0_col + bk + bg + bw - 20)); |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 145 | |
| 146 | if (rank > 1) |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 147 | chipsize_mb += chipsize_mb >> ((cs0_row - cs1_row) + |
| 148 | (cs0_col - cs1_col)); |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 149 | if (row_3_4) |
| 150 | chipsize_mb = chipsize_mb * 3 / 4; |
| 151 | size_mb += chipsize_mb; |
Kever Yang | 9a46f2a | 2019-11-15 11:04:35 +0800 | [diff] [blame] | 152 | if (rank > 1) |
| 153 | debug("rank %d cs0_col %d cs1_col %d bk %d cs0_row %d\ |
| 154 | cs1_row %d bw %d row_3_4 %d\n", |
| 155 | rank, cs0_col, cs1_col, bk, cs0_row, |
| 156 | cs1_row, bw, row_3_4); |
| 157 | else |
| 158 | debug("rank %d cs0_col %d bk %d cs0_row %d\ |
| 159 | bw %d row_3_4 %d\n", |
| 160 | rank, cs0_col, bk, cs0_row, |
| 161 | bw, row_3_4); |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 162 | } |
| 163 | |
Kever Yang | 3119ecc | 2018-12-28 09:56:48 +0800 | [diff] [blame] | 164 | /* |
| 165 | * This is workaround for issue we can't get correct size for 4GB ram |
| 166 | * in 32bit system and available before we really need ram space |
| 167 | * out of 4GB, eg.enable ARM LAPE(rk3288 supports 8GB ram). |
| 168 | * The size of 4GB is '0x1 00000000', and this value will be truncated |
| 169 | * to 0 in 32bit system, and system can not get correct ram size. |
| 170 | * Rockchip SoCs reserve a blob of space for peripheral near 4GB, |
| 171 | * and we are now setting SDRAM_MAX_SIZE as max available space for |
| 172 | * ram in 4GB, so we can use this directly to workaround the issue. |
| 173 | * TODO: |
| 174 | * 1. update correct value for SDRAM_MAX_SIZE as what dram |
| 175 | * controller sees. |
| 176 | * 2. update board_get_usable_ram_top() and dram_init_banksize() |
| 177 | * to reserve memory for peripheral space after previous update. |
| 178 | */ |
| 179 | if (size_mb > (SDRAM_MAX_SIZE >> 20)) |
| 180 | size_mb = (SDRAM_MAX_SIZE >> 20); |
| 181 | |
Kever Yang | 6d1970f | 2017-06-23 16:11:05 +0800 | [diff] [blame] | 182 | return (size_t)size_mb << 20; |
| 183 | } |
| 184 | |
| 185 | int dram_init(void) |
| 186 | { |
| 187 | struct ram_info ram; |
| 188 | struct udevice *dev; |
| 189 | int ret; |
| 190 | |
| 191 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 192 | if (ret) { |
| 193 | debug("DRAM init failed: %d\n", ret); |
| 194 | return ret; |
| 195 | } |
| 196 | ret = ram_get_info(dev, &ram); |
| 197 | if (ret) { |
| 198 | debug("Cannot get DRAM size: %d\n", ret); |
| 199 | return ret; |
| 200 | } |
| 201 | gd->ram_size = ram.size; |
| 202 | debug("SDRAM base=%lx, size=%lx\n", |
| 203 | (unsigned long)ram.base, (unsigned long)ram.size); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | ulong board_get_usable_ram_top(ulong total_size) |
| 209 | { |
| 210 | unsigned long top = CONFIG_SYS_SDRAM_BASE + SDRAM_MAX_SIZE; |
| 211 | |
| 212 | return (gd->ram_top > top) ? top : gd->ram_top; |
| 213 | } |