Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007-2009 Freescale Semiconductor, Inc. |
| 4 | * Copyright (C) 2008-2009 MontaVista Software, Inc. |
| 5 | * |
| 6 | * Authors: Tony Li <tony.li@freescale.com> |
| 7 | * Anton Vorontsov <avorontsov@ru.mvista.com> |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <pci.h> |
| 12 | #include <mpc83xx.h> |
| 13 | #include <asm/io.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | #define PCIE_MAX_BUSES 2 |
| 19 | |
Ilya Yanok | f0b1fc5 | 2010-09-17 23:41:46 +0200 | [diff] [blame] | 20 | static struct { |
| 21 | u32 base; |
| 22 | u32 size; |
| 23 | } mpc83xx_pcie_cfg_space[] = { |
| 24 | { |
| 25 | .base = CONFIG_SYS_PCIE1_CFG_BASE, |
| 26 | .size = CONFIG_SYS_PCIE1_CFG_SIZE, |
| 27 | }, |
| 28 | #if defined(CONFIG_SYS_PCIE2_CFG_BASE) && defined(CONFIG_SYS_PCIE2_CFG_SIZE) |
| 29 | { |
| 30 | .base = CONFIG_SYS_PCIE2_CFG_BASE, |
| 31 | .size = CONFIG_SYS_PCIE2_CFG_SIZE, |
| 32 | }, |
| 33 | #endif |
| 34 | }; |
| 35 | |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 36 | #ifdef CONFIG_83XX_GENERIC_PCIE_REGISTER_HOSES |
| 37 | |
Leo Liu | e87bc03 | 2011-01-19 19:50:47 +0800 | [diff] [blame] | 38 | /* private structure for mpc83xx pcie hose */ |
| 39 | static struct mpc83xx_pcie_priv { |
| 40 | u8 index; |
| 41 | } pcie_priv[PCIE_MAX_BUSES] = { |
| 42 | { |
| 43 | /* pcie controller 1 */ |
| 44 | .index = 0, |
| 45 | }, |
| 46 | { |
| 47 | /* pcie controller 2 */ |
| 48 | .index = 1, |
| 49 | }, |
| 50 | }; |
| 51 | |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 52 | static int mpc83xx_pcie_remap_cfg(struct pci_controller *hose, pci_dev_t dev) |
| 53 | { |
| 54 | int bus = PCI_BUS(dev) - hose->first_busno; |
| 55 | immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
Leo Liu | e87bc03 | 2011-01-19 19:50:47 +0800 | [diff] [blame] | 56 | struct mpc83xx_pcie_priv *pcie_priv = hose->priv_data; |
| 57 | pex83xx_t *pex = &immr->pciexp[pcie_priv->index]; |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 58 | struct pex_outbound_window *out_win = &pex->bridge.pex_outbound_win[0]; |
| 59 | u8 devfn = PCI_DEV(dev) << 3 | PCI_FUNC(dev); |
| 60 | u32 dev_base = bus << 24 | devfn << 16; |
| 61 | |
| 62 | if (hose->indirect_type == INDIRECT_TYPE_NO_PCIE_LINK) |
| 63 | return -1; |
| 64 | /* |
| 65 | * Workaround for the HW bug: for Type 0 configure transactions the |
| 66 | * PCI-E controller does not check the device number bits and just |
| 67 | * assumes that the device number bits are 0. |
| 68 | */ |
| 69 | if (devfn & 0xf8) |
| 70 | return -1; |
| 71 | |
| 72 | out_le32(&out_win->tarl, dev_base); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | #define cfg_read(val, addr, type, op) \ |
| 77 | do { *val = op((type)(addr)); } while (0) |
| 78 | #define cfg_write(val, addr, type, op) \ |
| 79 | do { op((type *)(addr), (val)); } while (0) |
| 80 | |
Anton Vorontsov | 63626fa | 2009-02-19 18:20:44 +0300 | [diff] [blame] | 81 | #define cfg_read_err(val) do { *val = -1; } while (0) |
| 82 | #define cfg_write_err(val) do { } while (0) |
| 83 | |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 84 | #define PCIE_OP(rw, size, type, op) \ |
| 85 | static int pcie_##rw##_config_##size(struct pci_controller *hose, \ |
| 86 | pci_dev_t dev, int offset, \ |
| 87 | type val) \ |
| 88 | { \ |
| 89 | int ret; \ |
| 90 | \ |
| 91 | ret = mpc83xx_pcie_remap_cfg(hose, dev); \ |
Anton Vorontsov | 63626fa | 2009-02-19 18:20:44 +0300 | [diff] [blame] | 92 | if (ret) { \ |
| 93 | cfg_##rw##_err(val); \ |
| 94 | return ret; \ |
| 95 | } \ |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 96 | cfg_##rw(val, (void *)hose->cfg_addr + offset, type, op); \ |
| 97 | return 0; \ |
| 98 | } |
| 99 | |
| 100 | PCIE_OP(read, byte, u8 *, in_8) |
| 101 | PCIE_OP(read, word, u16 *, in_le16) |
| 102 | PCIE_OP(read, dword, u32 *, in_le32) |
| 103 | PCIE_OP(write, byte, u8, out_8) |
| 104 | PCIE_OP(write, word, u16, out_le16) |
| 105 | PCIE_OP(write, dword, u32, out_le32) |
| 106 | |
| 107 | static void mpc83xx_pcie_register_hose(int bus, struct pci_region *reg, |
| 108 | u8 link) |
| 109 | { |
| 110 | extern void disable_addr_trans(void); /* start.S */ |
| 111 | static struct pci_controller pcie_hose[PCIE_MAX_BUSES]; |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 112 | struct pci_controller *hose = &pcie_hose[bus]; |
| 113 | int i; |
| 114 | |
| 115 | /* |
| 116 | * There are no spare BATs to remap all PCI-E windows for U-Boot, so |
| 117 | * disable translations. In general, this is not great solution, and |
| 118 | * that's why we don't register PCI-E hoses by default. |
| 119 | */ |
| 120 | disable_addr_trans(); |
| 121 | |
| 122 | for (i = 0; i < 2; i++, reg++) { |
| 123 | if (reg->size == 0) |
| 124 | break; |
| 125 | |
| 126 | hose->regions[i] = *reg; |
| 127 | hose->region_count++; |
| 128 | } |
| 129 | |
| 130 | i = hose->region_count++; |
| 131 | hose->regions[i].bus_start = 0; |
| 132 | hose->regions[i].phys_start = 0; |
| 133 | hose->regions[i].size = gd->ram_size; |
Kumar Gala | efa1f1d | 2009-02-06 09:49:31 -0600 | [diff] [blame] | 134 | hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY; |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 135 | |
| 136 | i = hose->region_count++; |
| 137 | hose->regions[i].bus_start = CONFIG_SYS_IMMR; |
| 138 | hose->regions[i].phys_start = CONFIG_SYS_IMMR; |
| 139 | hose->regions[i].size = 0x100000; |
Kumar Gala | efa1f1d | 2009-02-06 09:49:31 -0600 | [diff] [blame] | 140 | hose->regions[i].flags = PCI_REGION_MEM | PCI_REGION_SYS_MEMORY; |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 141 | |
Anton Vorontsov | f23e481 | 2009-02-19 18:20:42 +0300 | [diff] [blame] | 142 | hose->first_busno = pci_last_busno() + 1; |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 143 | hose->last_busno = 0xff; |
| 144 | |
Kim Phillips | 3b386ae | 2010-09-22 15:31:01 -0500 | [diff] [blame] | 145 | hose->cfg_addr = (unsigned int *)mpc83xx_pcie_cfg_space[bus].base; |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 146 | |
Leo Liu | e87bc03 | 2011-01-19 19:50:47 +0800 | [diff] [blame] | 147 | hose->priv_data = &pcie_priv[bus]; |
| 148 | |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 149 | pci_set_ops(hose, |
| 150 | pcie_read_config_byte, |
| 151 | pcie_read_config_word, |
| 152 | pcie_read_config_dword, |
| 153 | pcie_write_config_byte, |
| 154 | pcie_write_config_word, |
| 155 | pcie_write_config_dword); |
| 156 | |
| 157 | if (!link) |
| 158 | hose->indirect_type = INDIRECT_TYPE_NO_PCIE_LINK; |
| 159 | |
| 160 | pci_register_hose(hose); |
| 161 | |
| 162 | #ifdef CONFIG_PCI_SCAN_SHOW |
| 163 | printf("PCI: Bus Dev VenId DevId Class Int\n"); |
| 164 | #endif |
| 165 | /* |
| 166 | * Hose scan. |
| 167 | */ |
| 168 | hose->last_busno = pci_hose_scan(hose); |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | #else |
| 172 | |
| 173 | static void mpc83xx_pcie_register_hose(int bus, struct pci_region *reg, |
| 174 | u8 link) {} |
| 175 | |
| 176 | #endif /* CONFIG_83XX_GENERIC_PCIE_REGISTER_HOSES */ |
| 177 | |
Mario Six | de09afa | 2019-01-21 09:17:55 +0100 | [diff] [blame] | 178 | int get_pcie_clk(int index) |
| 179 | { |
| 180 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
| 181 | u32 pci_sync_in; |
| 182 | u8 spmf; |
| 183 | u8 clkin_div; |
| 184 | u32 sccr; |
| 185 | u32 csb_clk; |
| 186 | u32 testval; |
| 187 | |
| 188 | clkin_div = ((im->clk.spmr & SPMR_CKID) >> SPMR_CKID_SHIFT); |
| 189 | sccr = im->clk.sccr; |
| 190 | pci_sync_in = CONFIG_SYS_CLK_FREQ / (1 + clkin_div); |
| 191 | spmf = (im->clk.spmr & SPMR_SPMF) >> SPMR_SPMF_SHIFT; |
| 192 | csb_clk = pci_sync_in * (1 + clkin_div) * spmf; |
| 193 | |
| 194 | if (index) |
| 195 | testval = (sccr & SCCR_PCIEXP2CM) >> SCCR_PCIEXP2CM_SHIFT; |
| 196 | else |
| 197 | testval = (sccr & SCCR_PCIEXP1CM) >> SCCR_PCIEXP1CM_SHIFT; |
| 198 | |
| 199 | switch (testval) { |
| 200 | case 0: |
| 201 | return 0; |
| 202 | case 1: |
| 203 | return csb_clk; |
| 204 | case 2: |
| 205 | return csb_clk / 2; |
| 206 | case 3: |
| 207 | return csb_clk / 3; |
| 208 | } |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 213 | static void mpc83xx_pcie_init_bus(int bus, struct pci_region *reg) |
| 214 | { |
| 215 | immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
| 216 | pex83xx_t *pex = &immr->pciexp[bus]; |
| 217 | struct pex_outbound_window *out_win; |
| 218 | struct pex_inbound_window *in_win; |
| 219 | void *hose_cfg_base; |
| 220 | unsigned int ram_sz; |
| 221 | unsigned int barl; |
| 222 | unsigned int tar; |
| 223 | u16 reg16; |
| 224 | int i; |
| 225 | |
| 226 | /* Enable pex csb bridge inbound & outbound transactions */ |
| 227 | out_le32(&pex->bridge.pex_csb_ctrl, |
| 228 | in_le32(&pex->bridge.pex_csb_ctrl) | PEX_CSB_CTRL_OBPIOE | |
| 229 | PEX_CSB_CTRL_IBPIOE); |
| 230 | |
| 231 | /* Enable bridge outbound */ |
| 232 | out_le32(&pex->bridge.pex_csb_obctrl, PEX_CSB_OBCTRL_PIOE | |
| 233 | PEX_CSB_OBCTRL_MEMWE | PEX_CSB_OBCTRL_IOWE | |
| 234 | PEX_CSB_OBCTRL_CFGWE); |
| 235 | |
| 236 | out_win = &pex->bridge.pex_outbound_win[0]; |
Ilya Yanok | f0b1fc5 | 2010-09-17 23:41:46 +0200 | [diff] [blame] | 237 | out_le32(&out_win->ar, PEX_OWAR_EN | PEX_OWAR_TYPE_CFG | |
| 238 | mpc83xx_pcie_cfg_space[bus].size); |
| 239 | out_le32(&out_win->bar, mpc83xx_pcie_cfg_space[bus].base); |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 240 | out_le32(&out_win->tarl, 0); |
| 241 | out_le32(&out_win->tarh, 0); |
| 242 | |
Baidu Boy | 1388f22 | 2010-11-29 21:10:45 +0800 | [diff] [blame] | 243 | for (i = 0; i < 2; i++) { |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 244 | u32 ar; |
| 245 | |
Baidu Boy | 1388f22 | 2010-11-29 21:10:45 +0800 | [diff] [blame] | 246 | if (reg[i].size == 0) |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 247 | break; |
| 248 | |
| 249 | out_win = &pex->bridge.pex_outbound_win[i + 1]; |
Baidu Boy | 1388f22 | 2010-11-29 21:10:45 +0800 | [diff] [blame] | 250 | out_le32(&out_win->bar, reg[i].phys_start); |
| 251 | out_le32(&out_win->tarl, reg[i].bus_start); |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 252 | out_le32(&out_win->tarh, 0); |
Baidu Boy | 1388f22 | 2010-11-29 21:10:45 +0800 | [diff] [blame] | 253 | ar = PEX_OWAR_EN | (reg[i].size & PEX_OWAR_SIZE); |
| 254 | if (reg[i].flags & PCI_REGION_IO) |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 255 | ar |= PEX_OWAR_TYPE_IO; |
| 256 | else |
| 257 | ar |= PEX_OWAR_TYPE_MEM; |
| 258 | out_le32(&out_win->ar, ar); |
| 259 | } |
| 260 | |
| 261 | out_le32(&pex->bridge.pex_csb_ibctrl, PEX_CSB_IBCTRL_PIOE); |
| 262 | |
| 263 | ram_sz = gd->ram_size; |
| 264 | barl = 0; |
| 265 | tar = 0; |
| 266 | i = 0; |
| 267 | while (ram_sz > 0) { |
| 268 | in_win = &pex->bridge.pex_inbound_win[i]; |
| 269 | out_le32(&in_win->barl, barl); |
| 270 | out_le32(&in_win->barh, 0x0); |
| 271 | out_le32(&in_win->tar, tar); |
| 272 | if (ram_sz >= 0x10000000) { |
| 273 | /* The maxium windows size is 256M */ |
| 274 | out_le32(&in_win->ar, PEX_IWAR_EN | PEX_IWAR_NSOV | |
| 275 | PEX_IWAR_TYPE_PF | 0x0FFFF000); |
| 276 | barl += 0x10000000; |
| 277 | tar += 0x10000000; |
| 278 | ram_sz -= 0x10000000; |
| 279 | } else { |
| 280 | /* The UM is not clear here. |
| 281 | * So, round up to even Mb boundary */ |
| 282 | |
| 283 | ram_sz = ram_sz >> (20 + |
| 284 | ((ram_sz & 0xFFFFF) ? 1 : 0)); |
| 285 | if (!(ram_sz % 2)) |
| 286 | ram_sz -= 1; |
| 287 | out_le32(&in_win->ar, PEX_IWAR_EN | PEX_IWAR_NSOV | |
| 288 | PEX_IWAR_TYPE_PF | (ram_sz << 20) | 0xFF000); |
| 289 | ram_sz = 0; |
| 290 | } |
| 291 | i++; |
| 292 | } |
| 293 | |
| 294 | in_win = &pex->bridge.pex_inbound_win[i]; |
| 295 | out_le32(&in_win->barl, CONFIG_SYS_IMMR); |
| 296 | out_le32(&in_win->barh, 0); |
| 297 | out_le32(&in_win->tar, CONFIG_SYS_IMMR); |
| 298 | out_le32(&in_win->ar, PEX_IWAR_EN | |
| 299 | PEX_IWAR_TYPE_NO_PF | PEX_IWAR_SIZE_1M); |
| 300 | |
| 301 | /* Enable the host virtual INTX interrupts */ |
| 302 | out_le32(&pex->bridge.pex_int_axi_misc_enb, |
| 303 | in_le32(&pex->bridge.pex_int_axi_misc_enb) | 0x1E0); |
| 304 | |
| 305 | /* Hose configure header is memory-mapped */ |
| 306 | hose_cfg_base = (void *)pex; |
| 307 | |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 308 | /* Configure the PCIE controller core clock ratio */ |
| 309 | out_le32(hose_cfg_base + PEX_GCLK_RATIO, |
Mario Six | de09afa | 2019-01-21 09:17:55 +0100 | [diff] [blame] | 310 | ((get_pcie_clk(bus) / 1000000) * 16) / 333); |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 311 | udelay(1000000); |
| 312 | |
| 313 | /* Do Type 1 bridge configuration */ |
| 314 | out_8(hose_cfg_base + PCI_PRIMARY_BUS, 0); |
| 315 | out_8(hose_cfg_base + PCI_SECONDARY_BUS, 1); |
| 316 | out_8(hose_cfg_base + PCI_SUBORDINATE_BUS, 255); |
| 317 | |
| 318 | /* |
| 319 | * Write to Command register |
| 320 | */ |
| 321 | reg16 = in_le16(hose_cfg_base + PCI_COMMAND); |
| 322 | reg16 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO | |
| 323 | PCI_COMMAND_SERR | PCI_COMMAND_PARITY; |
| 324 | out_le16(hose_cfg_base + PCI_COMMAND, reg16); |
| 325 | |
| 326 | /* |
| 327 | * Clear non-reserved bits in status register. |
| 328 | */ |
| 329 | out_le16(hose_cfg_base + PCI_STATUS, 0xffff); |
| 330 | out_8(hose_cfg_base + PCI_LATENCY_TIMER, 0x80); |
| 331 | out_8(hose_cfg_base + PCI_CACHE_LINE_SIZE, 0x08); |
| 332 | |
| 333 | printf("PCIE%d: ", bus); |
| 334 | |
Roy Zang | 06fd4dd | 2012-12-10 19:02:59 +0800 | [diff] [blame] | 335 | #define PCI_LTSSM 0x404 /* PCIe Link Training, Status State Machine */ |
| 336 | #define PCI_LTSSM_L0 0x16 /* L0 state */ |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 337 | reg16 = in_le16(hose_cfg_base + PCI_LTSSM); |
| 338 | if (reg16 >= PCI_LTSSM_L0) |
| 339 | printf("link\n"); |
| 340 | else |
| 341 | printf("No link\n"); |
| 342 | |
| 343 | mpc83xx_pcie_register_hose(bus, reg, reg16 >= PCI_LTSSM_L0); |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * The caller must have already set SCCR, SERDES and the PCIE_LAW BARs |
| 348 | * must have been set to cover all of the requested regions. |
| 349 | */ |
Peter Tyser | e228332 | 2010-09-14 19:13:50 -0500 | [diff] [blame] | 350 | void mpc83xx_pcie_init(int num_buses, struct pci_region **reg) |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 351 | { |
| 352 | int i; |
| 353 | |
| 354 | /* |
| 355 | * Release PCI RST Output signal. |
| 356 | * Power on to RST high must be at least 100 ms as per PCI spec. |
Peter Tyser | e228332 | 2010-09-14 19:13:50 -0500 | [diff] [blame] | 357 | * On warm boots only 1 ms is required, but we play it safe. |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 358 | */ |
Peter Tyser | e228332 | 2010-09-14 19:13:50 -0500 | [diff] [blame] | 359 | udelay(100000); |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 360 | |
Ilya Yanok | f0b1fc5 | 2010-09-17 23:41:46 +0200 | [diff] [blame] | 361 | if (num_buses > ARRAY_SIZE(mpc83xx_pcie_cfg_space)) { |
| 362 | printf("Second PCIE host contoller not configured!\n"); |
| 363 | num_buses = ARRAY_SIZE(mpc83xx_pcie_cfg_space); |
| 364 | } |
| 365 | |
Anton Vorontsov | 1a8206c | 2009-01-08 04:26:12 +0300 | [diff] [blame] | 366 | for (i = 0; i < num_buses; i++) |
| 367 | mpc83xx_pcie_init_bus(i, reg[i]); |
| 368 | } |