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