Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2007,2009-2014 Freescale Semiconductor, Inc. |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
Simon Glass | 33d1e70 | 2019-11-14 12:57:32 -0700 | [diff] [blame] | 8 | #include <cpu_func.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 9 | #include <env.h> |
Simon Glass | 18afe10 | 2019-11-14 12:57:47 -0700 | [diff] [blame] | 10 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 12 | #include <net.h> |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 13 | #include <pci.h> |
Simon Glass | a9dc068 | 2019-12-28 10:44:59 -0700 | [diff] [blame] | 14 | #include <time.h> |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 15 | #include <asm/processor.h> |
| 16 | #include <asm/mmu.h> |
| 17 | #include <asm/fsl_pci.h> |
| 18 | #include <asm/io.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 19 | #include <linux/libfdt.h> |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 20 | #include <fdt_support.h> |
| 21 | #include <netdev.h> |
| 22 | #include <fdtdec.h> |
| 23 | #include <errno.h> |
| 24 | #include <malloc.h> |
| 25 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
| 28 | static void *get_fdt_virt(void) |
| 29 | { |
| 30 | return (void *)CONFIG_SYS_TMPVIRT; |
| 31 | } |
| 32 | |
| 33 | static uint64_t get_fdt_phys(void) |
| 34 | { |
| 35 | return (uint64_t)(uintptr_t)gd->fdt_blob; |
| 36 | } |
| 37 | |
| 38 | static void map_fdt_as(int esel) |
| 39 | { |
| 40 | u32 mas0, mas1, mas2, mas3, mas7; |
| 41 | uint64_t fdt_phys = get_fdt_phys(); |
| 42 | unsigned long fdt_phys_tlb = fdt_phys & ~0xffffful; |
| 43 | unsigned long fdt_virt_tlb = (ulong)get_fdt_virt() & ~0xffffful; |
| 44 | |
| 45 | mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(esel); |
| 46 | mas1 = MAS1_VALID | MAS1_TID(0) | MAS1_TS | MAS1_TSIZE(BOOKE_PAGESZ_1M); |
| 47 | mas2 = FSL_BOOKE_MAS2(fdt_virt_tlb, 0); |
| 48 | mas3 = FSL_BOOKE_MAS3(fdt_phys_tlb, 0, MAS3_SW|MAS3_SR); |
| 49 | mas7 = FSL_BOOKE_MAS7(fdt_phys_tlb); |
| 50 | |
| 51 | write_tlb(mas0, mas1, mas2, mas3, mas7); |
| 52 | } |
| 53 | |
| 54 | uint64_t get_phys_ccsrbar_addr_early(void) |
| 55 | { |
| 56 | void *fdt = get_fdt_virt(); |
| 57 | uint64_t r; |
Tom Rini | 661d6d8 | 2017-08-03 08:53:36 -0400 | [diff] [blame] | 58 | int size, node; |
| 59 | u32 naddr; |
| 60 | const fdt32_t *prop; |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * To be able to read the FDT we need to create a temporary TLB |
| 64 | * map for it. |
| 65 | */ |
| 66 | map_fdt_as(10); |
Tom Rini | 661d6d8 | 2017-08-03 08:53:36 -0400 | [diff] [blame] | 67 | node = fdt_path_offset(fdt, "/soc"); |
| 68 | naddr = fdt_address_cells(fdt, node); |
| 69 | prop = fdt_getprop(fdt, node, "ranges", &size); |
| 70 | r = fdt_translate_address(fdt, node, prop + naddr); |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 71 | disable_tlb(10); |
| 72 | |
| 73 | return r; |
| 74 | } |
| 75 | |
| 76 | int board_early_init_f(void) |
| 77 | { |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | int checkboard(void) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int pci_map_region(void *fdt, int pci_node, int range_id, |
| 87 | phys_size_t *ppaddr, pci_addr_t *pvaddr, |
| 88 | pci_size_t *psize, ulong *pmap_addr) |
| 89 | { |
| 90 | uint64_t addr; |
| 91 | uint64_t size; |
| 92 | ulong map_addr; |
| 93 | int r; |
| 94 | |
Miao Yan | c198088 | 2015-12-21 01:19:59 -0800 | [diff] [blame] | 95 | r = fdt_read_range(fdt, pci_node, range_id, NULL, &addr, &size); |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 96 | if (r) |
| 97 | return r; |
| 98 | |
| 99 | if (ppaddr) |
| 100 | *ppaddr = addr; |
| 101 | if (psize) |
| 102 | *psize = size; |
| 103 | |
| 104 | if (!pmap_addr) |
| 105 | return 0; |
| 106 | |
| 107 | map_addr = *pmap_addr; |
| 108 | |
| 109 | /* Align map_addr */ |
| 110 | map_addr += size - 1; |
| 111 | map_addr &= ~(size - 1); |
| 112 | |
| 113 | if (map_addr + size >= CONFIG_SYS_PCI_MAP_END) |
| 114 | return -1; |
| 115 | |
| 116 | /* Map virtual memory for range */ |
| 117 | assert(!tlb_map_range(map_addr, addr, size, TLB_MAP_IO)); |
| 118 | *pmap_addr = map_addr + size; |
| 119 | |
| 120 | if (pvaddr) |
| 121 | *pvaddr = map_addr; |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | void pci_init_board(void) |
| 127 | { |
| 128 | struct pci_controller *pci_hoses; |
| 129 | void *fdt = get_fdt_virt(); |
| 130 | int pci_node = -1; |
| 131 | int pci_num = 0; |
| 132 | int pci_count = 0; |
| 133 | ulong map_addr; |
| 134 | |
| 135 | puts("\n"); |
| 136 | |
| 137 | /* Start MMIO and PIO range maps above RAM */ |
| 138 | map_addr = CONFIG_SYS_PCI_MAP_START; |
| 139 | |
| 140 | /* Count and allocate PCI buses */ |
| 141 | pci_node = fdt_node_offset_by_prop_value(fdt, pci_node, |
| 142 | "device_type", "pci", 4); |
| 143 | while (pci_node != -FDT_ERR_NOTFOUND) { |
| 144 | pci_node = fdt_node_offset_by_prop_value(fdt, pci_node, |
| 145 | "device_type", "pci", 4); |
| 146 | pci_count++; |
| 147 | } |
| 148 | |
| 149 | if (pci_count) { |
| 150 | pci_hoses = malloc(sizeof(struct pci_controller) * pci_count); |
| 151 | } else { |
| 152 | printf("PCI: disabled\n\n"); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | /* Spawn PCI buses based on device tree */ |
| 157 | pci_node = fdt_node_offset_by_prop_value(fdt, pci_node, |
| 158 | "device_type", "pci", 4); |
| 159 | while (pci_node != -FDT_ERR_NOTFOUND) { |
| 160 | struct fsl_pci_info pci_info = { }; |
| 161 | const fdt32_t *reg; |
| 162 | int r; |
| 163 | |
| 164 | reg = fdt_getprop(fdt, pci_node, "reg", NULL); |
| 165 | pci_info.regs = fdt_translate_address(fdt, pci_node, reg); |
| 166 | |
| 167 | /* Map MMIO range */ |
| 168 | r = pci_map_region(fdt, pci_node, 0, &pci_info.mem_phys, NULL, |
| 169 | &pci_info.mem_size, &map_addr); |
| 170 | if (r) |
| 171 | break; |
| 172 | |
| 173 | /* Map PIO range */ |
| 174 | r = pci_map_region(fdt, pci_node, 1, &pci_info.io_phys, NULL, |
| 175 | &pci_info.io_size, &map_addr); |
| 176 | if (r) |
| 177 | break; |
| 178 | |
| 179 | /* |
| 180 | * The PCI framework finds virtual addresses for the buses |
| 181 | * through our address map, so tell it the physical addresses. |
| 182 | */ |
| 183 | pci_info.mem_bus = pci_info.mem_phys; |
| 184 | pci_info.io_bus = pci_info.io_phys; |
| 185 | |
| 186 | /* Instantiate */ |
| 187 | pci_info.pci_num = pci_num + 1; |
| 188 | |
| 189 | fsl_setup_hose(&pci_hoses[pci_num], pci_info.regs); |
| 190 | printf("PCI: base address %lx\n", pci_info.regs); |
| 191 | |
| 192 | fsl_pci_init_port(&pci_info, &pci_hoses[pci_num], pci_num); |
| 193 | |
| 194 | /* Jump to next PCI node */ |
| 195 | pci_node = fdt_node_offset_by_prop_value(fdt, pci_node, |
| 196 | "device_type", "pci", 4); |
| 197 | pci_num++; |
| 198 | } |
| 199 | |
| 200 | puts("\n"); |
| 201 | } |
| 202 | |
| 203 | int last_stage_init(void) |
| 204 | { |
| 205 | void *fdt = get_fdt_virt(); |
| 206 | int len = 0; |
| 207 | const uint64_t *prop; |
| 208 | int chosen; |
| 209 | |
| 210 | chosen = fdt_path_offset(fdt, "/chosen"); |
| 211 | if (chosen < 0) { |
| 212 | printf("Couldn't find /chosen node in fdt\n"); |
| 213 | return -EIO; |
| 214 | } |
| 215 | |
| 216 | /* -kernel boot */ |
| 217 | prop = fdt_getprop(fdt, chosen, "qemu,boot-kernel", &len); |
| 218 | if (prop && (len >= 8)) |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 219 | env_set_hex("qemu_kernel_addr", *prop); |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 220 | |
| 221 | /* Give the user a variable for the host fdt */ |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 222 | env_set_hex("fdt_addr_r", (ulong)fdt); |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static uint64_t get_linear_ram_size(void) |
| 228 | { |
| 229 | void *fdt = get_fdt_virt(); |
| 230 | const void *prop; |
| 231 | int memory; |
| 232 | int len; |
| 233 | |
| 234 | memory = fdt_path_offset(fdt, "/memory"); |
| 235 | prop = fdt_getprop(fdt, memory, "reg", &len); |
| 236 | |
| 237 | if (prop && len >= 16) |
| 238 | return *(uint64_t *)(prop+8); |
| 239 | |
| 240 | panic("Couldn't determine RAM size"); |
| 241 | } |
| 242 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 243 | int board_eth_init(struct bd_info *bis) |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 244 | { |
| 245 | return pci_eth_init(bis); |
| 246 | } |
| 247 | |
| 248 | #if defined(CONFIG_OF_BOARD_SETUP) |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 249 | int ft_board_setup(void *blob, struct bd_info *bd) |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 250 | { |
| 251 | FT_FSL_PCI_SETUP; |
Simon Glass | 2aec3cc | 2014-10-23 18:58:47 -0600 | [diff] [blame] | 252 | |
| 253 | return 0; |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 254 | } |
| 255 | #endif |
| 256 | |
| 257 | void print_laws(void) |
| 258 | { |
| 259 | /* We don't emulate LAWs yet */ |
| 260 | } |
| 261 | |
| 262 | phys_size_t fixed_sdram(void) |
| 263 | { |
| 264 | return get_linear_ram_size(); |
| 265 | } |
| 266 | |
| 267 | phys_size_t fsl_ddr_sdram_size(void) |
| 268 | { |
| 269 | return get_linear_ram_size(); |
| 270 | } |
| 271 | |
| 272 | void init_tlbs(void) |
| 273 | { |
| 274 | phys_size_t ram_size; |
| 275 | |
| 276 | /* |
| 277 | * Create a temporary AS=1 map for the fdt |
| 278 | * |
| 279 | * We use ESEL=0 here to overwrite the previous AS=0 map for ourselves |
| 280 | * which was only 4k big. This way we don't have to clear any other maps. |
| 281 | */ |
| 282 | map_fdt_as(0); |
| 283 | |
| 284 | /* Fetch RAM size from the fdt */ |
| 285 | ram_size = get_linear_ram_size(); |
| 286 | |
| 287 | /* And remove our fdt map again */ |
| 288 | disable_tlb(0); |
| 289 | |
| 290 | /* Create an internal map of manually created TLB maps */ |
| 291 | init_used_tlb_cams(); |
| 292 | |
| 293 | /* Create a dynamic AS=0 CCSRBAR mapping */ |
| 294 | assert(!tlb_map_range(CONFIG_SYS_CCSRBAR, CONFIG_SYS_CCSRBAR_PHYS, |
| 295 | 1024 * 1024, TLB_MAP_IO)); |
| 296 | |
| 297 | /* Create a RAM map that spans all accessible RAM */ |
| 298 | setup_ddr_tlbs(ram_size >> 20); |
| 299 | |
| 300 | /* Create a map for the TLB */ |
| 301 | assert(!tlb_map_range((ulong)get_fdt_virt(), get_fdt_phys(), |
| 302 | 1024 * 1024, TLB_MAP_RAM)); |
| 303 | } |
| 304 | |
| 305 | void init_laws(void) |
| 306 | { |
| 307 | /* We don't emulate LAWs yet */ |
| 308 | } |
| 309 | |
| 310 | static uint32_t get_cpu_freq(void) |
| 311 | { |
| 312 | void *fdt = get_fdt_virt(); |
| 313 | int cpus_node = fdt_path_offset(fdt, "/cpus"); |
| 314 | int cpu_node = fdt_first_subnode(fdt, cpus_node); |
| 315 | const char *prop = "clock-frequency"; |
| 316 | return fdt_getprop_u32_default_node(fdt, cpu_node, 0, prop, 0); |
| 317 | } |
| 318 | |
| 319 | void get_sys_info(sys_info_t *sys_info) |
| 320 | { |
| 321 | int freq = get_cpu_freq(); |
| 322 | |
| 323 | memset(sys_info, 0, sizeof(sys_info_t)); |
| 324 | sys_info->freq_systembus = freq; |
| 325 | sys_info->freq_ddrbus = freq; |
| 326 | sys_info->freq_processor[0] = freq; |
| 327 | } |
| 328 | |
Simon Glass | 85d6531 | 2019-12-28 10:44:58 -0700 | [diff] [blame] | 329 | int get_clocks(void) |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 330 | { |
| 331 | sys_info_t sys_info; |
| 332 | |
| 333 | get_sys_info(&sys_info); |
| 334 | |
| 335 | gd->cpu_clk = sys_info.freq_processor[0]; |
| 336 | gd->bus_clk = sys_info.freq_systembus; |
| 337 | gd->mem_clk = sys_info.freq_ddrbus; |
| 338 | gd->arch.lbc_clk = sys_info.freq_ddrbus; |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Simon Glass | a9dc068 | 2019-12-28 10:44:59 -0700 | [diff] [blame] | 343 | unsigned long get_tbclk(void) |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 344 | { |
| 345 | void *fdt = get_fdt_virt(); |
| 346 | int cpus_node = fdt_path_offset(fdt, "/cpus"); |
| 347 | int cpu_node = fdt_first_subnode(fdt, cpus_node); |
| 348 | const char *prop = "timebase-frequency"; |
| 349 | return fdt_getprop_u32_default_node(fdt, cpu_node, 0, prop, 0); |
| 350 | } |
| 351 | |
| 352 | /******************************************** |
| 353 | * get_bus_freq |
| 354 | * return system bus freq in Hz |
| 355 | *********************************************/ |
Simon Glass | 85d6531 | 2019-12-28 10:44:58 -0700 | [diff] [blame] | 356 | ulong get_bus_freq(ulong dummy) |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 357 | { |
| 358 | sys_info_t sys_info; |
| 359 | get_sys_info(&sys_info); |
| 360 | return sys_info.freq_systembus; |
| 361 | } |
Alexander Graf | 5b9e18c | 2014-04-30 19:21:10 +0200 | [diff] [blame] | 362 | |
| 363 | /* |
| 364 | * Return the number of cores on this SOC. |
| 365 | */ |
| 366 | int cpu_numcores(void) |
| 367 | { |
| 368 | /* |
| 369 | * The QEMU u-boot target only needs to drive the first core, |
| 370 | * spinning and device tree nodes get driven by QEMU itself |
| 371 | */ |
| 372 | return 1; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Return a 32-bit mask indicating which cores are present on this SOC. |
| 377 | */ |
| 378 | u32 cpu_mask(void) |
| 379 | { |
| 380 | return (1 << cpu_numcores()) - 1; |
| 381 | } |