Sekhar Nori | 03c396b | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Texas Instruments, Inc |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <pci.h> |
| 9 | #include <generic-phy.h> |
| 10 | #include <power-domain.h> |
| 11 | #include <regmap.h> |
| 12 | #include <syscon.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <asm-generic/gpio.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame^] | 15 | #include <linux/err.h> |
Sekhar Nori | 03c396b | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | #define PCIE_VENDORID_MASK GENMASK(15, 0) |
| 20 | #define PCIE_DEVICEID_SHIFT 16 |
| 21 | |
| 22 | /* PCI DBICS registers */ |
| 23 | #define PCIE_CONFIG_BAR0 0x10 |
| 24 | #define PCIE_LINK_STATUS_REG 0x80 |
| 25 | #define PCIE_LINK_STATUS_SPEED_OFF 16 |
| 26 | #define PCIE_LINK_STATUS_SPEED_MASK (0xf << PCIE_LINK_STATUS_SPEED_OFF) |
| 27 | #define PCIE_LINK_STATUS_WIDTH_OFF 20 |
| 28 | #define PCIE_LINK_STATUS_WIDTH_MASK (0xf << PCIE_LINK_STATUS_WIDTH_OFF) |
| 29 | |
| 30 | #define PCIE_LINK_CAPABILITY 0x7c |
| 31 | #define PCIE_LINK_CTL_2 0xa0 |
| 32 | #define TARGET_LINK_SPEED_MASK 0xf |
| 33 | #define LINK_SPEED_GEN_1 0x1 |
| 34 | #define LINK_SPEED_GEN_2 0x2 |
| 35 | #define LINK_SPEED_GEN_3 0x3 |
| 36 | |
| 37 | #define PCIE_MISC_CONTROL_1_OFF 0x8bc |
| 38 | #define PCIE_DBI_RO_WR_EN BIT(0) |
| 39 | |
| 40 | #define PLR_OFFSET 0x700 |
| 41 | #define PCIE_PORT_DEBUG0 (PLR_OFFSET + 0x28) |
| 42 | #define PORT_LOGIC_LTSSM_STATE_MASK 0x1f |
| 43 | #define PORT_LOGIC_LTSSM_STATE_L0 0x11 |
| 44 | |
| 45 | #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80c |
| 46 | #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17) |
| 47 | |
| 48 | #define PCIE_LINK_UP_TIMEOUT_MS 100 |
| 49 | |
| 50 | /* |
| 51 | * iATU Unroll-specific register definitions |
| 52 | * From 4.80 core version the address translation will be made by unroll. |
| 53 | * The registers are offset from atu_base |
| 54 | */ |
| 55 | #define PCIE_ATU_UNR_REGION_CTRL1 0x00 |
| 56 | #define PCIE_ATU_UNR_REGION_CTRL2 0x04 |
| 57 | #define PCIE_ATU_UNR_LOWER_BASE 0x08 |
| 58 | #define PCIE_ATU_UNR_UPPER_BASE 0x0c |
| 59 | #define PCIE_ATU_UNR_LIMIT 0x10 |
| 60 | #define PCIE_ATU_UNR_LOWER_TARGET 0x14 |
| 61 | #define PCIE_ATU_UNR_UPPER_TARGET 0x18 |
| 62 | |
| 63 | #define PCIE_ATU_REGION_INDEX1 (0x1 << 0) |
| 64 | #define PCIE_ATU_REGION_INDEX0 (0x0 << 0) |
| 65 | #define PCIE_ATU_TYPE_MEM (0x0 << 0) |
| 66 | #define PCIE_ATU_TYPE_IO (0x2 << 0) |
| 67 | #define PCIE_ATU_TYPE_CFG0 (0x4 << 0) |
| 68 | #define PCIE_ATU_TYPE_CFG1 (0x5 << 0) |
| 69 | #define PCIE_ATU_ENABLE (0x1 << 31) |
| 70 | #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30) |
| 71 | #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24) |
| 72 | #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19) |
| 73 | #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16) |
| 74 | |
| 75 | /* Register address builder */ |
| 76 | #define PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(region) ((region) << 9) |
| 77 | |
| 78 | /* Offsets from App base */ |
| 79 | #define PCIE_CMD_STATUS 0x04 |
| 80 | #define LTSSM_EN_VAL BIT(0) |
| 81 | |
| 82 | /* Parameters for the waiting for iATU enabled routine */ |
| 83 | #define LINK_WAIT_MAX_IATU_RETRIES 5 |
| 84 | #define LINK_WAIT_IATU 10000 |
| 85 | |
| 86 | #define AM654_PCIE_DEV_TYPE_MASK 0x3 |
| 87 | #define EP 0x0 |
| 88 | #define LEG_EP 0x1 |
| 89 | #define RC 0x2 |
| 90 | |
| 91 | /** |
| 92 | * struct pcie_dw_ti - TI DW PCIe controller state |
| 93 | * |
| 94 | * @app_base: The base address of application register space |
| 95 | * @dbics_base: The base address of dbics register space |
| 96 | * @cfg_base: The base address of configuration space |
| 97 | * @atu_base: The base address of ATU space |
| 98 | * @cfg_size: The size of the configuration space which is needed |
| 99 | * as it gets written into the PCIE_ATU_LIMIT register |
| 100 | * @first_busno: This driver supports multiple PCIe controllers. |
| 101 | * first_busno stores the bus number of the PCIe root-port |
| 102 | * number which may vary depending on the PCIe setup |
| 103 | * (PEX switches etc). |
| 104 | */ |
| 105 | struct pcie_dw_ti { |
| 106 | void *app_base; |
| 107 | void *dbi_base; |
| 108 | void *cfg_base; |
| 109 | void *atu_base; |
| 110 | fdt_size_t cfg_size; |
| 111 | int first_busno; |
| 112 | struct udevice *dev; |
| 113 | |
| 114 | /* IO and MEM PCI regions */ |
| 115 | struct pci_region io; |
| 116 | struct pci_region mem; |
| 117 | }; |
| 118 | |
| 119 | enum dw_pcie_device_mode { |
| 120 | DW_PCIE_UNKNOWN_TYPE, |
| 121 | DW_PCIE_EP_TYPE, |
| 122 | DW_PCIE_LEG_EP_TYPE, |
| 123 | DW_PCIE_RC_TYPE, |
| 124 | }; |
| 125 | |
| 126 | static int pcie_dw_get_link_speed(struct pcie_dw_ti *pci) |
| 127 | { |
| 128 | return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) & |
| 129 | PCIE_LINK_STATUS_SPEED_MASK) >> PCIE_LINK_STATUS_SPEED_OFF; |
| 130 | } |
| 131 | |
| 132 | static int pcie_dw_get_link_width(struct pcie_dw_ti *pci) |
| 133 | { |
| 134 | return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) & |
| 135 | PCIE_LINK_STATUS_WIDTH_MASK) >> PCIE_LINK_STATUS_WIDTH_OFF; |
| 136 | } |
| 137 | |
| 138 | static void dw_pcie_writel_ob_unroll(struct pcie_dw_ti *pci, u32 index, u32 reg, |
| 139 | u32 val) |
| 140 | { |
| 141 | u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index); |
| 142 | void __iomem *base = pci->atu_base; |
| 143 | |
| 144 | writel(val, base + offset + reg); |
| 145 | } |
| 146 | |
| 147 | static u32 dw_pcie_readl_ob_unroll(struct pcie_dw_ti *pci, u32 index, u32 reg) |
| 148 | { |
| 149 | u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index); |
| 150 | void __iomem *base = pci->atu_base; |
| 151 | |
| 152 | return readl(base + offset + reg); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * pcie_dw_prog_outbound_atu_unroll() - Configure ATU for outbound accesses |
| 157 | * |
| 158 | * @pcie: Pointer to the PCI controller state |
| 159 | * @index: ATU region index |
| 160 | * @type: ATU accsess type |
| 161 | * @cpu_addr: the physical address for the translation entry |
| 162 | * @pci_addr: the pcie bus address for the translation entry |
| 163 | * @size: the size of the translation entry |
| 164 | */ |
| 165 | static void pcie_dw_prog_outbound_atu_unroll(struct pcie_dw_ti *pci, int index, |
| 166 | int type, u64 cpu_addr, |
| 167 | u64 pci_addr, u32 size) |
| 168 | { |
| 169 | u32 retries, val; |
| 170 | |
| 171 | debug("ATU programmed with: index: %d, type: %d, cpu addr: %8llx, pci addr: %8llx, size: %8x\n", |
| 172 | index, type, cpu_addr, pci_addr, size); |
| 173 | |
| 174 | dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE, |
| 175 | lower_32_bits(cpu_addr)); |
| 176 | dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE, |
| 177 | upper_32_bits(cpu_addr)); |
| 178 | dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LIMIT, |
| 179 | lower_32_bits(cpu_addr + size - 1)); |
| 180 | dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET, |
| 181 | lower_32_bits(pci_addr)); |
| 182 | dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET, |
| 183 | upper_32_bits(pci_addr)); |
| 184 | dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1, |
| 185 | type); |
| 186 | dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2, |
| 187 | PCIE_ATU_ENABLE); |
| 188 | |
| 189 | /* |
| 190 | * Make sure ATU enable takes effect before any subsequent config |
| 191 | * and I/O accesses. |
| 192 | */ |
| 193 | for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) { |
| 194 | val = dw_pcie_readl_ob_unroll(pci, index, |
| 195 | PCIE_ATU_UNR_REGION_CTRL2); |
| 196 | if (val & PCIE_ATU_ENABLE) |
| 197 | return; |
| 198 | |
| 199 | udelay(LINK_WAIT_IATU); |
| 200 | } |
| 201 | dev_err(pci->dev, "outbound iATU is not being enabled\n"); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * set_cfg_address() - Configure the PCIe controller config space access |
| 206 | * |
| 207 | * @pcie: Pointer to the PCI controller state |
| 208 | * @d: PCI device to access |
| 209 | * @where: Offset in the configuration space |
| 210 | * |
| 211 | * Configures the PCIe controller to access the configuration space of |
| 212 | * a specific PCIe device and returns the address to use for this |
| 213 | * access. |
| 214 | * |
| 215 | * Return: Address that can be used to access the configation space |
| 216 | * of the requested device / offset |
| 217 | */ |
| 218 | static uintptr_t set_cfg_address(struct pcie_dw_ti *pcie, |
| 219 | pci_dev_t d, uint where) |
| 220 | { |
| 221 | int bus = PCI_BUS(d) - pcie->first_busno; |
| 222 | uintptr_t va_address; |
| 223 | u32 atu_type; |
| 224 | |
| 225 | /* Use dbi_base for own configuration read and write */ |
| 226 | if (!bus) { |
| 227 | va_address = (uintptr_t)pcie->dbi_base; |
| 228 | goto out; |
| 229 | } |
| 230 | |
| 231 | if (bus == 1) |
| 232 | /* For local bus, change TLP Type field to 4. */ |
| 233 | atu_type = PCIE_ATU_TYPE_CFG0; |
| 234 | else |
| 235 | /* Otherwise, change TLP Type field to 5. */ |
| 236 | atu_type = PCIE_ATU_TYPE_CFG1; |
| 237 | |
| 238 | /* |
| 239 | * Not accessing root port configuration space? |
| 240 | * Region #0 is used for Outbound CFG space access. |
| 241 | * Direction = Outbound |
| 242 | * Region Index = 0 |
| 243 | */ |
| 244 | d = PCI_MASK_BUS(d); |
| 245 | d = PCI_ADD_BUS(bus, d); |
| 246 | pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1, |
| 247 | atu_type, (u64)pcie->cfg_base, |
| 248 | d << 8, pcie->cfg_size); |
| 249 | |
| 250 | va_address = (uintptr_t)pcie->cfg_base; |
| 251 | |
| 252 | out: |
| 253 | va_address += where & ~0x3; |
| 254 | |
| 255 | return va_address; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * pcie_dw_addr_valid() - Check for valid bus address |
| 260 | * |
| 261 | * @d: The PCI device to access |
| 262 | * @first_busno: Bus number of the PCIe controller root complex |
| 263 | * |
| 264 | * Return 1 (true) if the PCI device can be accessed by this controller. |
| 265 | * |
| 266 | * Return: 1 on valid, 0 on invalid |
| 267 | */ |
| 268 | static int pcie_dw_addr_valid(pci_dev_t d, int first_busno) |
| 269 | { |
| 270 | if ((PCI_BUS(d) == first_busno) && (PCI_DEV(d) > 0)) |
| 271 | return 0; |
| 272 | if ((PCI_BUS(d) == first_busno + 1) && (PCI_DEV(d) > 0)) |
| 273 | return 0; |
| 274 | |
| 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * pcie_dw_ti_read_config() - Read from configuration space |
| 280 | * |
| 281 | * @bus: Pointer to the PCI bus |
| 282 | * @bdf: Identifies the PCIe device to access |
| 283 | * @offset: The offset into the device's configuration space |
| 284 | * @valuep: A pointer at which to store the read value |
| 285 | * @size: Indicates the size of access to perform |
| 286 | * |
| 287 | * Read a value of size @size from offset @offset within the configuration |
| 288 | * space of the device identified by the bus, device & function numbers in @bdf |
| 289 | * on the PCI bus @bus. |
| 290 | * |
| 291 | * Return: 0 on success |
| 292 | */ |
Simon Glass | c4e72c4 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 293 | static int pcie_dw_ti_read_config(const struct udevice *bus, pci_dev_t bdf, |
Sekhar Nori | 03c396b | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 294 | uint offset, ulong *valuep, |
| 295 | enum pci_size_t size) |
| 296 | { |
| 297 | struct pcie_dw_ti *pcie = dev_get_priv(bus); |
| 298 | uintptr_t va_address; |
| 299 | ulong value; |
| 300 | |
| 301 | debug("PCIE CFG read: bdf=%2x:%2x:%2x ", |
| 302 | PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
| 303 | |
| 304 | if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) { |
| 305 | debug("- out of range\n"); |
| 306 | *valuep = pci_get_ff(size); |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | va_address = set_cfg_address(pcie, bdf, offset); |
| 311 | |
| 312 | value = readl(va_address); |
| 313 | |
| 314 | debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value); |
| 315 | *valuep = pci_conv_32_to_size(value, offset, size); |
| 316 | |
| 317 | pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1, |
| 318 | PCIE_ATU_TYPE_IO, pcie->io.phys_start, |
| 319 | pcie->io.bus_start, pcie->io.size); |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * pcie_dw_ti_write_config() - Write to configuration space |
| 326 | * |
| 327 | * @bus: Pointer to the PCI bus |
| 328 | * @bdf: Identifies the PCIe device to access |
| 329 | * @offset: The offset into the device's configuration space |
| 330 | * @value: The value to write |
| 331 | * @size: Indicates the size of access to perform |
| 332 | * |
| 333 | * Write the value @value of size @size from offset @offset within the |
| 334 | * configuration space of the device identified by the bus, device & function |
| 335 | * numbers in @bdf on the PCI bus @bus. |
| 336 | * |
| 337 | * Return: 0 on success |
| 338 | */ |
| 339 | static int pcie_dw_ti_write_config(struct udevice *bus, pci_dev_t bdf, |
| 340 | uint offset, ulong value, |
| 341 | enum pci_size_t size) |
| 342 | { |
| 343 | struct pcie_dw_ti *pcie = dev_get_priv(bus); |
| 344 | uintptr_t va_address; |
| 345 | ulong old; |
| 346 | |
| 347 | debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ", |
| 348 | PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
| 349 | debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value); |
| 350 | |
| 351 | if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) { |
| 352 | debug("- out of range\n"); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | va_address = set_cfg_address(pcie, bdf, offset); |
| 357 | |
| 358 | old = readl(va_address); |
| 359 | value = pci_conv_size_to_32(old, value, offset, size); |
| 360 | writel(value, va_address); |
| 361 | |
| 362 | pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1, |
| 363 | PCIE_ATU_TYPE_IO, pcie->io.phys_start, |
| 364 | pcie->io.bus_start, pcie->io.size); |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static inline void dw_pcie_dbi_write_enable(struct pcie_dw_ti *pci, bool en) |
| 370 | { |
| 371 | u32 val; |
| 372 | |
| 373 | val = readl(pci->dbi_base + PCIE_MISC_CONTROL_1_OFF); |
| 374 | if (en) |
| 375 | val |= PCIE_DBI_RO_WR_EN; |
| 376 | else |
| 377 | val &= ~PCIE_DBI_RO_WR_EN; |
| 378 | writel(val, pci->dbi_base + PCIE_MISC_CONTROL_1_OFF); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * pcie_dw_configure() - Configure link capabilities and speed |
| 383 | * |
| 384 | * @regs_base: A pointer to the PCIe controller registers |
| 385 | * @cap_speed: The capabilities and speed to configure |
| 386 | * |
| 387 | * Configure the link capabilities and speed in the PCIe root complex. |
| 388 | */ |
| 389 | static void pcie_dw_configure(struct pcie_dw_ti *pci, u32 cap_speed) |
| 390 | { |
| 391 | u32 val; |
| 392 | |
| 393 | dw_pcie_dbi_write_enable(pci, true); |
| 394 | |
| 395 | val = readl(pci->dbi_base + PCIE_LINK_CAPABILITY); |
| 396 | val &= ~TARGET_LINK_SPEED_MASK; |
| 397 | val |= cap_speed; |
| 398 | writel(val, pci->dbi_base + PCIE_LINK_CAPABILITY); |
| 399 | |
| 400 | val = readl(pci->dbi_base + PCIE_LINK_CTL_2); |
| 401 | val &= ~TARGET_LINK_SPEED_MASK; |
| 402 | val |= cap_speed; |
| 403 | writel(val, pci->dbi_base + PCIE_LINK_CTL_2); |
| 404 | |
| 405 | dw_pcie_dbi_write_enable(pci, false); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * is_link_up() - Return the link state |
| 410 | * |
| 411 | * @regs_base: A pointer to the PCIe DBICS registers |
| 412 | * |
| 413 | * Return: 1 (true) for active line and 0 (false) for no link |
| 414 | */ |
| 415 | static int is_link_up(struct pcie_dw_ti *pci) |
| 416 | { |
| 417 | u32 val; |
| 418 | |
| 419 | val = readl(pci->dbi_base + PCIE_PORT_DEBUG0); |
| 420 | val &= PORT_LOGIC_LTSSM_STATE_MASK; |
| 421 | |
| 422 | return (val == PORT_LOGIC_LTSSM_STATE_L0); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * wait_link_up() - Wait for the link to come up |
| 427 | * |
| 428 | * @regs_base: A pointer to the PCIe controller registers |
| 429 | * |
| 430 | * Return: 1 (true) for active line and 0 (false) for no link (timeout) |
| 431 | */ |
| 432 | static int wait_link_up(struct pcie_dw_ti *pci) |
| 433 | { |
| 434 | unsigned long timeout; |
| 435 | |
| 436 | timeout = get_timer(0) + PCIE_LINK_UP_TIMEOUT_MS; |
| 437 | while (!is_link_up(pci)) { |
| 438 | if (get_timer(0) > timeout) |
| 439 | return 0; |
| 440 | }; |
| 441 | |
| 442 | return 1; |
| 443 | } |
| 444 | |
| 445 | static int pcie_dw_ti_pcie_link_up(struct pcie_dw_ti *pci, u32 cap_speed) |
| 446 | { |
| 447 | u32 val; |
| 448 | |
| 449 | if (is_link_up(pci)) { |
| 450 | printf("PCI Link already up before configuration!\n"); |
| 451 | return 1; |
| 452 | } |
| 453 | |
| 454 | /* DW pre link configurations */ |
| 455 | pcie_dw_configure(pci, cap_speed); |
| 456 | |
| 457 | /* Initiate link training */ |
| 458 | val = readl(pci->app_base + PCIE_CMD_STATUS); |
| 459 | val |= LTSSM_EN_VAL; |
| 460 | writel(val, pci->app_base + PCIE_CMD_STATUS); |
| 461 | |
| 462 | /* Check that link was established */ |
| 463 | if (!wait_link_up(pci)) |
| 464 | return 0; |
| 465 | |
| 466 | /* |
| 467 | * Link can be established in Gen 1. still need to wait |
| 468 | * till MAC nagaotiation is completed |
| 469 | */ |
| 470 | udelay(100); |
| 471 | |
| 472 | return 1; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * pcie_dw_setup_host() - Setup the PCIe controller for RC opertaion |
| 477 | * |
| 478 | * @pcie: Pointer to the PCI controller state |
| 479 | * |
| 480 | * Configure the host BARs of the PCIe controller root port so that |
| 481 | * PCI(e) devices may access the system memory. |
| 482 | */ |
| 483 | static void pcie_dw_setup_host(struct pcie_dw_ti *pci) |
| 484 | { |
| 485 | u32 val; |
| 486 | |
| 487 | /* setup RC BARs */ |
| 488 | writel(PCI_BASE_ADDRESS_MEM_TYPE_64, |
| 489 | pci->dbi_base + PCI_BASE_ADDRESS_0); |
| 490 | writel(0x0, pci->dbi_base + PCI_BASE_ADDRESS_1); |
| 491 | |
| 492 | /* setup interrupt pins */ |
| 493 | dw_pcie_dbi_write_enable(pci, true); |
| 494 | val = readl(pci->dbi_base + PCI_INTERRUPT_LINE); |
| 495 | val &= 0xffff00ff; |
| 496 | val |= 0x00000100; |
| 497 | writel(val, pci->dbi_base + PCI_INTERRUPT_LINE); |
| 498 | dw_pcie_dbi_write_enable(pci, false); |
| 499 | |
| 500 | /* setup bus numbers */ |
| 501 | val = readl(pci->dbi_base + PCI_PRIMARY_BUS); |
| 502 | val &= 0xff000000; |
| 503 | val |= 0x00ff0100; |
| 504 | writel(val, pci->dbi_base + PCI_PRIMARY_BUS); |
| 505 | |
| 506 | /* setup command register */ |
| 507 | val = readl(pci->dbi_base + PCI_COMMAND); |
| 508 | val &= 0xffff0000; |
| 509 | val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | |
| 510 | PCI_COMMAND_MASTER | PCI_COMMAND_SERR; |
| 511 | writel(val, pci->dbi_base + PCI_COMMAND); |
| 512 | |
| 513 | /* Enable write permission for the DBI read-only register */ |
| 514 | dw_pcie_dbi_write_enable(pci, true); |
| 515 | /* program correct class for RC */ |
| 516 | writew(PCI_CLASS_BRIDGE_PCI, pci->dbi_base + PCI_CLASS_DEVICE); |
| 517 | /* Better disable write permission right after the update */ |
| 518 | dw_pcie_dbi_write_enable(pci, false); |
| 519 | |
| 520 | val = readl(pci->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL); |
| 521 | val |= PORT_LOGIC_SPEED_CHANGE; |
| 522 | writel(val, pci->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL); |
| 523 | } |
| 524 | |
| 525 | static int pcie_am654_set_mode(struct pcie_dw_ti *pci, |
| 526 | enum dw_pcie_device_mode mode) |
| 527 | { |
| 528 | struct regmap *syscon; |
| 529 | u32 val; |
| 530 | u32 mask; |
| 531 | int ret; |
| 532 | |
| 533 | syscon = syscon_regmap_lookup_by_phandle(pci->dev, |
| 534 | "ti,syscon-pcie-mode"); |
| 535 | if (IS_ERR(syscon)) |
| 536 | return 0; |
| 537 | |
| 538 | mask = AM654_PCIE_DEV_TYPE_MASK; |
| 539 | |
| 540 | switch (mode) { |
| 541 | case DW_PCIE_RC_TYPE: |
| 542 | val = RC; |
| 543 | break; |
| 544 | case DW_PCIE_EP_TYPE: |
| 545 | val = EP; |
| 546 | break; |
| 547 | default: |
| 548 | dev_err(pci->dev, "INVALID device type %d\n", mode); |
| 549 | return -EINVAL; |
| 550 | } |
| 551 | |
| 552 | ret = regmap_update_bits(syscon, 0, mask, val); |
| 553 | if (ret) { |
| 554 | dev_err(pci->dev, "failed to set pcie mode\n"); |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static int pcie_dw_init_id(struct pcie_dw_ti *pci) |
| 562 | { |
| 563 | struct regmap *devctrl_regs; |
| 564 | unsigned int id; |
| 565 | int ret; |
| 566 | |
| 567 | devctrl_regs = syscon_regmap_lookup_by_phandle(pci->dev, |
| 568 | "ti,syscon-pcie-id"); |
| 569 | if (IS_ERR(devctrl_regs)) |
| 570 | return PTR_ERR(devctrl_regs); |
| 571 | |
| 572 | ret = regmap_read(devctrl_regs, 0, &id); |
| 573 | if (ret) |
| 574 | return ret; |
| 575 | |
| 576 | dw_pcie_dbi_write_enable(pci, true); |
| 577 | writew(id & PCIE_VENDORID_MASK, pci->dbi_base + PCI_VENDOR_ID); |
| 578 | writew(id >> PCIE_DEVICEID_SHIFT, pci->dbi_base + PCI_DEVICE_ID); |
| 579 | dw_pcie_dbi_write_enable(pci, false); |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * pcie_dw_ti_probe() - Probe the PCIe bus for active link |
| 586 | * |
| 587 | * @dev: A pointer to the device being operated on |
| 588 | * |
| 589 | * Probe for an active link on the PCIe bus and configure the controller |
| 590 | * to enable this port. |
| 591 | * |
| 592 | * Return: 0 on success, else -ENODEV |
| 593 | */ |
| 594 | static int pcie_dw_ti_probe(struct udevice *dev) |
| 595 | { |
| 596 | struct pcie_dw_ti *pci = dev_get_priv(dev); |
| 597 | struct udevice *ctlr = pci_get_controller(dev); |
| 598 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
| 599 | struct power_domain pci_pwrdmn; |
| 600 | struct phy phy0, phy1; |
| 601 | int ret; |
| 602 | |
| 603 | ret = power_domain_get_by_index(dev, &pci_pwrdmn, 0); |
| 604 | if (ret) { |
| 605 | dev_err(dev, "failed to get power domain\n"); |
| 606 | return ret; |
| 607 | } |
| 608 | |
| 609 | ret = power_domain_on(&pci_pwrdmn); |
| 610 | if (ret) { |
| 611 | dev_err(dev, "Power domain on failed\n"); |
| 612 | return ret; |
| 613 | } |
| 614 | |
| 615 | ret = generic_phy_get_by_name(dev, "pcie-phy0", &phy0); |
| 616 | if (ret) { |
| 617 | dev_err(dev, "Unable to get phy0"); |
| 618 | return ret; |
| 619 | } |
| 620 | generic_phy_reset(&phy0); |
| 621 | generic_phy_init(&phy0); |
| 622 | generic_phy_power_on(&phy0); |
| 623 | |
| 624 | ret = generic_phy_get_by_name(dev, "pcie-phy1", &phy1); |
| 625 | if (ret) { |
| 626 | dev_err(dev, "Unable to get phy1"); |
| 627 | return ret; |
| 628 | } |
| 629 | generic_phy_reset(&phy1); |
| 630 | generic_phy_init(&phy1); |
| 631 | generic_phy_power_on(&phy1); |
| 632 | |
| 633 | pci->first_busno = dev->seq; |
| 634 | pci->dev = dev; |
| 635 | |
| 636 | pcie_dw_setup_host(pci); |
| 637 | pcie_dw_init_id(pci); |
| 638 | |
| 639 | if (device_is_compatible(dev, "ti,am654-pcie-rc")) |
| 640 | pcie_am654_set_mode(pci, DW_PCIE_RC_TYPE); |
| 641 | |
| 642 | if (!pcie_dw_ti_pcie_link_up(pci, LINK_SPEED_GEN_2)) { |
| 643 | printf("PCIE-%d: Link down\n", dev->seq); |
| 644 | return -ENODEV; |
| 645 | } |
| 646 | |
| 647 | printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", dev->seq, |
| 648 | pcie_dw_get_link_speed(pci), |
| 649 | pcie_dw_get_link_width(pci), |
| 650 | hose->first_busno); |
| 651 | |
| 652 | /* Store the IO and MEM windows settings for future use by the ATU */ |
| 653 | pci->io.phys_start = hose->regions[0].phys_start; /* IO base */ |
| 654 | pci->io.bus_start = hose->regions[0].bus_start; /* IO_bus_addr */ |
| 655 | pci->io.size = hose->regions[0].size; /* IO size */ |
| 656 | |
| 657 | pci->mem.phys_start = hose->regions[1].phys_start; /* MEM base */ |
| 658 | pci->mem.bus_start = hose->regions[1].bus_start; /* MEM_bus_addr */ |
| 659 | pci->mem.size = hose->regions[1].size; /* MEM size */ |
| 660 | |
| 661 | pcie_dw_prog_outbound_atu_unroll(pci, PCIE_ATU_REGION_INDEX0, |
| 662 | PCIE_ATU_TYPE_MEM, |
| 663 | pci->mem.phys_start, |
| 664 | pci->mem.bus_start, pci->mem.size); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * pcie_dw_ti_ofdata_to_platdata() - Translate from DT to device state |
| 671 | * |
| 672 | * @dev: A pointer to the device being operated on |
| 673 | * |
| 674 | * Translate relevant data from the device tree pertaining to device @dev into |
| 675 | * state that the driver will later make use of. This state is stored in the |
| 676 | * device's private data structure. |
| 677 | * |
| 678 | * Return: 0 on success, else -EINVAL |
| 679 | */ |
| 680 | static int pcie_dw_ti_ofdata_to_platdata(struct udevice *dev) |
| 681 | { |
| 682 | struct pcie_dw_ti *pcie = dev_get_priv(dev); |
| 683 | |
| 684 | /* Get the controller base address */ |
| 685 | pcie->dbi_base = (void *)dev_read_addr_name(dev, "dbics"); |
| 686 | if ((fdt_addr_t)pcie->dbi_base == FDT_ADDR_T_NONE) |
| 687 | return -EINVAL; |
| 688 | |
| 689 | /* Get the config space base address and size */ |
| 690 | pcie->cfg_base = (void *)dev_read_addr_size_name(dev, "config", |
| 691 | &pcie->cfg_size); |
| 692 | if ((fdt_addr_t)pcie->cfg_base == FDT_ADDR_T_NONE) |
| 693 | return -EINVAL; |
| 694 | |
| 695 | /* Get the iATU base address and size */ |
| 696 | pcie->atu_base = (void *)dev_read_addr_name(dev, "atu"); |
| 697 | if ((fdt_addr_t)pcie->atu_base == FDT_ADDR_T_NONE) |
| 698 | return -EINVAL; |
| 699 | |
| 700 | /* Get the app base address and size */ |
| 701 | pcie->app_base = (void *)dev_read_addr_name(dev, "app"); |
| 702 | if ((fdt_addr_t)pcie->app_base == FDT_ADDR_T_NONE) |
| 703 | return -EINVAL; |
| 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | static const struct dm_pci_ops pcie_dw_ti_ops = { |
| 709 | .read_config = pcie_dw_ti_read_config, |
| 710 | .write_config = pcie_dw_ti_write_config, |
| 711 | }; |
| 712 | |
| 713 | static const struct udevice_id pcie_dw_ti_ids[] = { |
| 714 | { .compatible = "ti,am654-pcie-rc" }, |
| 715 | { } |
| 716 | }; |
| 717 | |
| 718 | U_BOOT_DRIVER(pcie_dw_ti) = { |
| 719 | .name = "pcie_dw_ti", |
| 720 | .id = UCLASS_PCI, |
| 721 | .of_match = pcie_dw_ti_ids, |
| 722 | .ops = &pcie_dw_ti_ops, |
| 723 | .ofdata_to_platdata = pcie_dw_ti_ofdata_to_platdata, |
| 724 | .probe = pcie_dw_ti_probe, |
| 725 | .priv_auto_alloc_size = sizeof(struct pcie_dw_ti), |
| 726 | }; |