Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | 04b7e62 | 2015-01-15 10:01:51 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 - 2015 Xilinx, Inc. |
| 4 | * Michal Simek <michal.simek@xilinx.com> |
Michal Simek | 04b7e62 | 2015-01-15 10:01:51 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <asm/arch/hardware.h> |
| 9 | #include <asm/arch/sys_proto.h> |
Alexander Graf | 0e2088c | 2016-03-04 01:09:49 +0100 | [diff] [blame] | 10 | #include <asm/armv8/mmu.h> |
Michal Simek | 04b7e62 | 2015-01-15 10:01:51 +0100 | [diff] [blame] | 11 | #include <asm/io.h> |
Ibai Erkiaga | c8a3efa | 2019-09-27 11:37:01 +0100 | [diff] [blame] | 12 | #include <zynqmp_firmware.h> |
Michal Simek | 04b7e62 | 2015-01-15 10:01:51 +0100 | [diff] [blame] | 13 | |
| 14 | #define ZYNQ_SILICON_VER_MASK 0xF000 |
| 15 | #define ZYNQ_SILICON_VER_SHIFT 12 |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Nitin Jain | 9bcc76f | 2018-04-20 12:30:40 +0530 | [diff] [blame] | 19 | /* |
| 20 | * Number of filled static entries and also the first empty |
| 21 | * slot in zynqmp_mem_map. |
| 22 | */ |
| 23 | #define ZYNQMP_MEM_MAP_USED 4 |
| 24 | |
Siva Durga Prasad Paladugu | cafb631 | 2018-01-12 15:35:46 +0530 | [diff] [blame] | 25 | #if !defined(CONFIG_ZYNQMP_NO_DDR) |
Nitin Jain | 9bcc76f | 2018-04-20 12:30:40 +0530 | [diff] [blame] | 26 | #define DRAM_BANKS CONFIG_NR_DRAM_BANKS |
| 27 | #else |
| 28 | #define DRAM_BANKS 0 |
| 29 | #endif |
| 30 | |
| 31 | #if defined(CONFIG_DEFINE_TCM_OCM_MMAP) |
| 32 | #define TCM_MAP 1 |
| 33 | #else |
| 34 | #define TCM_MAP 0 |
Siva Durga Prasad Paladugu | cafb631 | 2018-01-12 15:35:46 +0530 | [diff] [blame] | 35 | #endif |
Nitin Jain | 9bcc76f | 2018-04-20 12:30:40 +0530 | [diff] [blame] | 36 | |
| 37 | /* +1 is end of list which needs to be empty */ |
| 38 | #define ZYNQMP_MEM_MAP_MAX (ZYNQMP_MEM_MAP_USED + DRAM_BANKS + TCM_MAP + 1) |
| 39 | |
| 40 | static struct mm_region zynqmp_mem_map[ZYNQMP_MEM_MAP_MAX] = { |
Siva Durga Prasad Paladugu | cafb631 | 2018-01-12 15:35:46 +0530 | [diff] [blame] | 41 | { |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 42 | .virt = 0x80000000UL, |
| 43 | .phys = 0x80000000UL, |
Alexander Graf | 0e2088c | 2016-03-04 01:09:49 +0100 | [diff] [blame] | 44 | .size = 0x70000000UL, |
| 45 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 46 | PTE_BLOCK_NON_SHARE | |
| 47 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
Nitin Jain | 9bcc76f | 2018-04-20 12:30:40 +0530 | [diff] [blame] | 48 | }, { |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 49 | .virt = 0xf8000000UL, |
| 50 | .phys = 0xf8000000UL, |
Alexander Graf | 0e2088c | 2016-03-04 01:09:49 +0100 | [diff] [blame] | 51 | .size = 0x07e00000UL, |
| 52 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 53 | PTE_BLOCK_NON_SHARE | |
| 54 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 55 | }, { |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 56 | .virt = 0x400000000UL, |
| 57 | .phys = 0x400000000UL, |
Anders Hedlund | fcc0992 | 2017-12-19 17:24:41 +0100 | [diff] [blame] | 58 | .size = 0x400000000UL, |
Alexander Graf | 0e2088c | 2016-03-04 01:09:49 +0100 | [diff] [blame] | 59 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 60 | PTE_BLOCK_NON_SHARE | |
| 61 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
Nitin Jain | 9bcc76f | 2018-04-20 12:30:40 +0530 | [diff] [blame] | 62 | }, { |
Anders Hedlund | fcc0992 | 2017-12-19 17:24:41 +0100 | [diff] [blame] | 63 | .virt = 0x1000000000UL, |
| 64 | .phys = 0x1000000000UL, |
| 65 | .size = 0xf000000000UL, |
Alexander Graf | 0e2088c | 2016-03-04 01:09:49 +0100 | [diff] [blame] | 66 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 67 | PTE_BLOCK_NON_SHARE | |
| 68 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
Alexander Graf | 0e2088c | 2016-03-04 01:09:49 +0100 | [diff] [blame] | 69 | } |
| 70 | }; |
Nitin Jain | 9bcc76f | 2018-04-20 12:30:40 +0530 | [diff] [blame] | 71 | |
| 72 | void mem_map_fill(void) |
| 73 | { |
| 74 | int banks = ZYNQMP_MEM_MAP_USED; |
| 75 | |
| 76 | #if defined(CONFIG_DEFINE_TCM_OCM_MMAP) |
| 77 | zynqmp_mem_map[banks].virt = 0xffe00000UL; |
| 78 | zynqmp_mem_map[banks].phys = 0xffe00000UL; |
| 79 | zynqmp_mem_map[banks].size = 0x00200000UL; |
| 80 | zynqmp_mem_map[banks].attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 81 | PTE_BLOCK_INNER_SHARE; |
| 82 | banks = banks + 1; |
| 83 | #endif |
| 84 | |
| 85 | #if !defined(CONFIG_ZYNQMP_NO_DDR) |
| 86 | for (int i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 87 | /* Zero size means no more DDR that's this is end */ |
| 88 | if (!gd->bd->bi_dram[i].size) |
| 89 | break; |
| 90 | |
| 91 | zynqmp_mem_map[banks].virt = gd->bd->bi_dram[i].start; |
| 92 | zynqmp_mem_map[banks].phys = gd->bd->bi_dram[i].start; |
| 93 | zynqmp_mem_map[banks].size = gd->bd->bi_dram[i].size; |
| 94 | zynqmp_mem_map[banks].attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 95 | PTE_BLOCK_INNER_SHARE; |
| 96 | banks = banks + 1; |
| 97 | } |
| 98 | #endif |
| 99 | } |
| 100 | |
Alexander Graf | 0e2088c | 2016-03-04 01:09:49 +0100 | [diff] [blame] | 101 | struct mm_region *mem_map = zynqmp_mem_map; |
| 102 | |
Michal Simek | 1a2d5e2 | 2016-05-30 10:41:26 +0200 | [diff] [blame] | 103 | u64 get_page_table_size(void) |
| 104 | { |
| 105 | return 0x14000; |
| 106 | } |
| 107 | |
Siva Durga Prasad Paladugu | 48eaa0c | 2018-10-05 15:09:05 +0530 | [diff] [blame] | 108 | #if defined(CONFIG_SYS_MEM_RSVD_FOR_MMU) || defined(CONFIG_DEFINE_TCM_OCM_MMAP) |
| 109 | void tcm_init(u8 mode) |
Siva Durga Prasad Paladugu | 4628c50 | 2017-07-13 19:01:11 +0530 | [diff] [blame] | 110 | { |
Siva Durga Prasad Paladugu | a1ad878 | 2018-10-05 15:09:04 +0530 | [diff] [blame] | 111 | puts("WARNING: Initializing TCM overwrites TCM content\n"); |
| 112 | initialize_tcm(mode); |
Siva Durga Prasad Paladugu | 4628c50 | 2017-07-13 19:01:11 +0530 | [diff] [blame] | 113 | memset((void *)ZYNQMP_TCM_BASE_ADDR, 0, ZYNQMP_TCM_SIZE); |
Siva Durga Prasad Paladugu | a1ad878 | 2018-10-05 15:09:04 +0530 | [diff] [blame] | 114 | } |
Siva Durga Prasad Paladugu | 48eaa0c | 2018-10-05 15:09:05 +0530 | [diff] [blame] | 115 | #endif |
Siva Durga Prasad Paladugu | a1ad878 | 2018-10-05 15:09:04 +0530 | [diff] [blame] | 116 | |
Siva Durga Prasad Paladugu | 48eaa0c | 2018-10-05 15:09:05 +0530 | [diff] [blame] | 117 | #ifdef CONFIG_SYS_MEM_RSVD_FOR_MMU |
Siva Durga Prasad Paladugu | a1ad878 | 2018-10-05 15:09:04 +0530 | [diff] [blame] | 118 | int reserve_mmu(void) |
| 119 | { |
| 120 | tcm_init(TCM_LOCK); |
Siva Durga Prasad Paladugu | 4628c50 | 2017-07-13 19:01:11 +0530 | [diff] [blame] | 121 | gd->arch.tlb_size = PGTABLE_SIZE; |
| 122 | gd->arch.tlb_addr = ZYNQMP_TCM_BASE_ADDR; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | #endif |
| 127 | |
Michal Simek | c23d3f8 | 2015-11-05 08:34:35 +0100 | [diff] [blame] | 128 | static unsigned int zynqmp_get_silicon_version_secure(void) |
| 129 | { |
| 130 | u32 ver; |
| 131 | |
| 132 | ver = readl(&csu_base->version); |
| 133 | ver &= ZYNQMP_SILICON_VER_MASK; |
| 134 | ver >>= ZYNQMP_SILICON_VER_SHIFT; |
| 135 | |
| 136 | return ver; |
| 137 | } |
| 138 | |
Michal Simek | 04b7e62 | 2015-01-15 10:01:51 +0100 | [diff] [blame] | 139 | unsigned int zynqmp_get_silicon_version(void) |
| 140 | { |
Michal Simek | c23d3f8 | 2015-11-05 08:34:35 +0100 | [diff] [blame] | 141 | if (current_el() == 3) |
| 142 | return zynqmp_get_silicon_version_secure(); |
| 143 | |
Michal Simek | 04b7e62 | 2015-01-15 10:01:51 +0100 | [diff] [blame] | 144 | gd->cpu_clk = get_tbclk(); |
| 145 | |
| 146 | switch (gd->cpu_clk) { |
| 147 | case 50000000: |
| 148 | return ZYNQMP_CSU_VERSION_QEMU; |
| 149 | } |
| 150 | |
Michal Simek | 8d2c02d | 2015-08-20 14:01:39 +0200 | [diff] [blame] | 151 | return ZYNQMP_CSU_VERSION_SILICON; |
Michal Simek | 04b7e62 | 2015-01-15 10:01:51 +0100 | [diff] [blame] | 152 | } |
Siva Durga Prasad Paladugu | 0e39bd7 | 2017-02-02 01:10:46 +0530 | [diff] [blame] | 153 | |
| 154 | #define ZYNQMP_MMIO_READ 0xC2000014 |
| 155 | #define ZYNQMP_MMIO_WRITE 0xC2000013 |
| 156 | |
Siva Durga Prasad Paladugu | 668fdd4 | 2017-07-13 19:01:12 +0530 | [diff] [blame] | 157 | int __maybe_unused invoke_smc(u32 pm_api_id, u32 arg0, u32 arg1, u32 arg2, |
| 158 | u32 arg3, u32 *ret_payload) |
Siva Durga Prasad Paladugu | 0e39bd7 | 2017-02-02 01:10:46 +0530 | [diff] [blame] | 159 | { |
| 160 | /* |
| 161 | * Added SIP service call Function Identifier |
| 162 | * Make sure to stay in x0 register |
| 163 | */ |
| 164 | struct pt_regs regs; |
| 165 | |
| 166 | regs.regs[0] = pm_api_id; |
| 167 | regs.regs[1] = ((u64)arg1 << 32) | arg0; |
| 168 | regs.regs[2] = ((u64)arg3 << 32) | arg2; |
| 169 | |
| 170 | smc_call(®s); |
| 171 | |
| 172 | if (ret_payload != NULL) { |
| 173 | ret_payload[0] = (u32)regs.regs[0]; |
| 174 | ret_payload[1] = upper_32_bits(regs.regs[0]); |
| 175 | ret_payload[2] = (u32)regs.regs[1]; |
| 176 | ret_payload[3] = upper_32_bits(regs.regs[1]); |
| 177 | ret_payload[4] = (u32)regs.regs[2]; |
| 178 | } |
| 179 | |
| 180 | return regs.regs[0]; |
| 181 | } |
| 182 | |
Siva Durga Prasad Paladugu | 668fdd4 | 2017-07-13 19:01:12 +0530 | [diff] [blame] | 183 | static int zynqmp_mmio_rawwrite(const u32 address, |
Siva Durga Prasad Paladugu | 0e39bd7 | 2017-02-02 01:10:46 +0530 | [diff] [blame] | 184 | const u32 mask, |
| 185 | const u32 value) |
| 186 | { |
| 187 | u32 data; |
| 188 | u32 value_local = value; |
Michal Simek | faac0ce | 2018-06-13 10:38:33 +0200 | [diff] [blame] | 189 | int ret; |
| 190 | |
| 191 | ret = zynqmp_mmio_read(address, &data); |
| 192 | if (ret) |
| 193 | return ret; |
Siva Durga Prasad Paladugu | 0e39bd7 | 2017-02-02 01:10:46 +0530 | [diff] [blame] | 194 | |
Siva Durga Prasad Paladugu | 0e39bd7 | 2017-02-02 01:10:46 +0530 | [diff] [blame] | 195 | data &= ~mask; |
| 196 | value_local &= mask; |
| 197 | value_local |= data; |
| 198 | writel(value_local, (ulong)address); |
| 199 | return 0; |
| 200 | } |
| 201 | |
Siva Durga Prasad Paladugu | 668fdd4 | 2017-07-13 19:01:12 +0530 | [diff] [blame] | 202 | static int zynqmp_mmio_rawread(const u32 address, u32 *value) |
Siva Durga Prasad Paladugu | 0e39bd7 | 2017-02-02 01:10:46 +0530 | [diff] [blame] | 203 | { |
| 204 | *value = readl((ulong)address); |
| 205 | return 0; |
| 206 | } |
Siva Durga Prasad Paladugu | 668fdd4 | 2017-07-13 19:01:12 +0530 | [diff] [blame] | 207 | |
| 208 | int zynqmp_mmio_write(const u32 address, |
| 209 | const u32 mask, |
| 210 | const u32 value) |
| 211 | { |
| 212 | if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) |
| 213 | return zynqmp_mmio_rawwrite(address, mask, value); |
Heinrich Schuchardt | 9f92f79 | 2017-10-13 01:14:27 +0200 | [diff] [blame] | 214 | else |
Siva Durga Prasad Paladugu | 668fdd4 | 2017-07-13 19:01:12 +0530 | [diff] [blame] | 215 | return invoke_smc(ZYNQMP_MMIO_WRITE, address, mask, |
| 216 | value, 0, NULL); |
| 217 | |
| 218 | return -EINVAL; |
| 219 | } |
| 220 | |
| 221 | int zynqmp_mmio_read(const u32 address, u32 *value) |
| 222 | { |
| 223 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 224 | u32 ret; |
| 225 | |
| 226 | if (!value) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) { |
| 230 | ret = zynqmp_mmio_rawread(address, value); |
Heinrich Schuchardt | 9f92f79 | 2017-10-13 01:14:27 +0200 | [diff] [blame] | 231 | } else { |
Siva Durga Prasad Paladugu | 668fdd4 | 2017-07-13 19:01:12 +0530 | [diff] [blame] | 232 | ret = invoke_smc(ZYNQMP_MMIO_READ, address, 0, 0, |
| 233 | 0, ret_payload); |
| 234 | *value = ret_payload[1]; |
| 235 | } |
| 236 | |
| 237 | return ret; |
| 238 | } |