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