Sekhar Nori | 18db23d | 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 | |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 6 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 8 | #include <pci.h> |
| 9 | #include <generic-phy.h> |
| 10 | #include <power-domain.h> |
| 11 | #include <regmap.h> |
| 12 | #include <syscon.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 14 | #include <asm/io.h> |
| 15 | #include <asm-generic/gpio.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 20 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 21 | #include "pcie_dw_common.h" |
| 22 | |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
| 25 | #define PCIE_VENDORID_MASK GENMASK(15, 0) |
| 26 | #define PCIE_DEVICEID_SHIFT 16 |
| 27 | |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 28 | #define PCIE_LINK_CAPABILITY 0x7c |
| 29 | #define PCIE_LINK_CTL_2 0xa0 |
| 30 | #define TARGET_LINK_SPEED_MASK 0xf |
| 31 | #define LINK_SPEED_GEN_1 0x1 |
| 32 | #define LINK_SPEED_GEN_2 0x2 |
| 33 | #define LINK_SPEED_GEN_3 0x3 |
| 34 | |
| 35 | #define PCIE_MISC_CONTROL_1_OFF 0x8bc |
| 36 | #define PCIE_DBI_RO_WR_EN BIT(0) |
| 37 | |
| 38 | #define PLR_OFFSET 0x700 |
| 39 | #define PCIE_PORT_DEBUG0 (PLR_OFFSET + 0x28) |
| 40 | #define PORT_LOGIC_LTSSM_STATE_MASK 0x1f |
| 41 | #define PORT_LOGIC_LTSSM_STATE_L0 0x11 |
| 42 | |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 43 | #define PCIE_LINK_UP_TIMEOUT_MS 100 |
| 44 | |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 45 | /* Offsets from App base */ |
| 46 | #define PCIE_CMD_STATUS 0x04 |
| 47 | #define LTSSM_EN_VAL BIT(0) |
| 48 | |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 49 | #define AM654_PCIE_DEV_TYPE_MASK 0x3 |
| 50 | #define EP 0x0 |
| 51 | #define LEG_EP 0x1 |
| 52 | #define RC 0x2 |
| 53 | |
| 54 | /** |
| 55 | * struct pcie_dw_ti - TI DW PCIe controller state |
| 56 | * |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 57 | * @pci: The common PCIe DW structure |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 58 | * @app_base: The base address of application register space |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 59 | */ |
| 60 | struct pcie_dw_ti { |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 61 | /* Must be first member of the struct */ |
| 62 | struct pcie_dw dw; |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 63 | void *app_base; |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | enum dw_pcie_device_mode { |
| 67 | DW_PCIE_UNKNOWN_TYPE, |
| 68 | DW_PCIE_EP_TYPE, |
| 69 | DW_PCIE_LEG_EP_TYPE, |
| 70 | DW_PCIE_RC_TYPE, |
| 71 | }; |
| 72 | |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 73 | /** |
| 74 | * pcie_dw_configure() - Configure link capabilities and speed |
| 75 | * |
| 76 | * @regs_base: A pointer to the PCIe controller registers |
| 77 | * @cap_speed: The capabilities and speed to configure |
| 78 | * |
| 79 | * Configure the link capabilities and speed in the PCIe root complex. |
| 80 | */ |
| 81 | static void pcie_dw_configure(struct pcie_dw_ti *pci, u32 cap_speed) |
| 82 | { |
| 83 | u32 val; |
| 84 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 85 | dw_pcie_dbi_write_enable(&pci->dw, true); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 86 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 87 | val = readl(pci->dw.dbi_base + PCIE_LINK_CAPABILITY); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 88 | val &= ~TARGET_LINK_SPEED_MASK; |
| 89 | val |= cap_speed; |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 90 | writel(val, pci->dw.dbi_base + PCIE_LINK_CAPABILITY); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 91 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 92 | val = readl(pci->dw.dbi_base + PCIE_LINK_CTL_2); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 93 | val &= ~TARGET_LINK_SPEED_MASK; |
| 94 | val |= cap_speed; |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 95 | writel(val, pci->dw.dbi_base + PCIE_LINK_CTL_2); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 96 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 97 | dw_pcie_dbi_write_enable(&pci->dw, false); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * is_link_up() - Return the link state |
| 102 | * |
| 103 | * @regs_base: A pointer to the PCIe DBICS registers |
| 104 | * |
| 105 | * Return: 1 (true) for active line and 0 (false) for no link |
| 106 | */ |
| 107 | static int is_link_up(struct pcie_dw_ti *pci) |
| 108 | { |
| 109 | u32 val; |
| 110 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 111 | val = readl(pci->dw.dbi_base + PCIE_PORT_DEBUG0); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 112 | val &= PORT_LOGIC_LTSSM_STATE_MASK; |
| 113 | |
| 114 | return (val == PORT_LOGIC_LTSSM_STATE_L0); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * wait_link_up() - Wait for the link to come up |
| 119 | * |
| 120 | * @regs_base: A pointer to the PCIe controller registers |
| 121 | * |
| 122 | * Return: 1 (true) for active line and 0 (false) for no link (timeout) |
| 123 | */ |
| 124 | static int wait_link_up(struct pcie_dw_ti *pci) |
| 125 | { |
| 126 | unsigned long timeout; |
| 127 | |
| 128 | timeout = get_timer(0) + PCIE_LINK_UP_TIMEOUT_MS; |
| 129 | while (!is_link_up(pci)) { |
| 130 | if (get_timer(0) > timeout) |
| 131 | return 0; |
| 132 | }; |
| 133 | |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | static int pcie_dw_ti_pcie_link_up(struct pcie_dw_ti *pci, u32 cap_speed) |
| 138 | { |
| 139 | u32 val; |
| 140 | |
| 141 | if (is_link_up(pci)) { |
| 142 | printf("PCI Link already up before configuration!\n"); |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | /* DW pre link configurations */ |
| 147 | pcie_dw_configure(pci, cap_speed); |
| 148 | |
| 149 | /* Initiate link training */ |
| 150 | val = readl(pci->app_base + PCIE_CMD_STATUS); |
| 151 | val |= LTSSM_EN_VAL; |
| 152 | writel(val, pci->app_base + PCIE_CMD_STATUS); |
| 153 | |
| 154 | /* Check that link was established */ |
| 155 | if (!wait_link_up(pci)) |
| 156 | return 0; |
| 157 | |
| 158 | /* |
| 159 | * Link can be established in Gen 1. still need to wait |
| 160 | * till MAC nagaotiation is completed |
| 161 | */ |
| 162 | udelay(100); |
| 163 | |
| 164 | return 1; |
| 165 | } |
| 166 | |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 167 | static int pcie_am654_set_mode(struct pcie_dw_ti *pci, |
| 168 | enum dw_pcie_device_mode mode) |
| 169 | { |
| 170 | struct regmap *syscon; |
| 171 | u32 val; |
| 172 | u32 mask; |
| 173 | int ret; |
| 174 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 175 | syscon = syscon_regmap_lookup_by_phandle(pci->dw.dev, |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 176 | "ti,syscon-pcie-mode"); |
| 177 | if (IS_ERR(syscon)) |
| 178 | return 0; |
| 179 | |
| 180 | mask = AM654_PCIE_DEV_TYPE_MASK; |
| 181 | |
| 182 | switch (mode) { |
| 183 | case DW_PCIE_RC_TYPE: |
| 184 | val = RC; |
| 185 | break; |
| 186 | case DW_PCIE_EP_TYPE: |
| 187 | val = EP; |
| 188 | break; |
| 189 | default: |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 190 | dev_err(pci->dw.dev, "INVALID device type %d\n", mode); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | ret = regmap_update_bits(syscon, 0, mask, val); |
| 195 | if (ret) { |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 196 | dev_err(pci->dw.dev, "failed to set pcie mode\n"); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int pcie_dw_init_id(struct pcie_dw_ti *pci) |
| 204 | { |
| 205 | struct regmap *devctrl_regs; |
| 206 | unsigned int id; |
| 207 | int ret; |
| 208 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 209 | devctrl_regs = syscon_regmap_lookup_by_phandle(pci->dw.dev, |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 210 | "ti,syscon-pcie-id"); |
| 211 | if (IS_ERR(devctrl_regs)) |
| 212 | return PTR_ERR(devctrl_regs); |
| 213 | |
| 214 | ret = regmap_read(devctrl_regs, 0, &id); |
| 215 | if (ret) |
| 216 | return ret; |
| 217 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 218 | dw_pcie_dbi_write_enable(&pci->dw, true); |
| 219 | writew(id & PCIE_VENDORID_MASK, pci->dw.dbi_base + PCI_VENDOR_ID); |
| 220 | writew(id >> PCIE_DEVICEID_SHIFT, pci->dw.dbi_base + PCI_DEVICE_ID); |
| 221 | dw_pcie_dbi_write_enable(&pci->dw, false); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * pcie_dw_ti_probe() - Probe the PCIe bus for active link |
| 228 | * |
| 229 | * @dev: A pointer to the device being operated on |
| 230 | * |
| 231 | * Probe for an active link on the PCIe bus and configure the controller |
| 232 | * to enable this port. |
| 233 | * |
| 234 | * Return: 0 on success, else -ENODEV |
| 235 | */ |
| 236 | static int pcie_dw_ti_probe(struct udevice *dev) |
| 237 | { |
| 238 | struct pcie_dw_ti *pci = dev_get_priv(dev); |
| 239 | struct udevice *ctlr = pci_get_controller(dev); |
| 240 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
| 241 | struct power_domain pci_pwrdmn; |
| 242 | struct phy phy0, phy1; |
| 243 | int ret; |
| 244 | |
| 245 | ret = power_domain_get_by_index(dev, &pci_pwrdmn, 0); |
| 246 | if (ret) { |
| 247 | dev_err(dev, "failed to get power domain\n"); |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | ret = power_domain_on(&pci_pwrdmn); |
| 252 | if (ret) { |
| 253 | dev_err(dev, "Power domain on failed\n"); |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | ret = generic_phy_get_by_name(dev, "pcie-phy0", &phy0); |
| 258 | if (ret) { |
| 259 | dev_err(dev, "Unable to get phy0"); |
| 260 | return ret; |
| 261 | } |
| 262 | generic_phy_reset(&phy0); |
| 263 | generic_phy_init(&phy0); |
| 264 | generic_phy_power_on(&phy0); |
| 265 | |
| 266 | ret = generic_phy_get_by_name(dev, "pcie-phy1", &phy1); |
| 267 | if (ret) { |
| 268 | dev_err(dev, "Unable to get phy1"); |
| 269 | return ret; |
| 270 | } |
| 271 | generic_phy_reset(&phy1); |
| 272 | generic_phy_init(&phy1); |
| 273 | generic_phy_power_on(&phy1); |
| 274 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 275 | pci->dw.first_busno = dev_seq(dev); |
| 276 | pci->dw.dev = dev; |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 277 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 278 | pcie_dw_setup_host(&pci->dw); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 279 | pcie_dw_init_id(pci); |
| 280 | |
| 281 | if (device_is_compatible(dev, "ti,am654-pcie-rc")) |
| 282 | pcie_am654_set_mode(pci, DW_PCIE_RC_TYPE); |
| 283 | |
| 284 | if (!pcie_dw_ti_pcie_link_up(pci, LINK_SPEED_GEN_2)) { |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 285 | printf("PCIE-%d: Link down\n", dev_seq(dev)); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 286 | return -ENODEV; |
| 287 | } |
| 288 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 289 | printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", dev_seq(dev), |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 290 | pcie_dw_get_link_speed(&pci->dw), |
| 291 | pcie_dw_get_link_width(&pci->dw), |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 292 | hose->first_busno); |
| 293 | |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 294 | pcie_dw_prog_outbound_atu_unroll(&pci->dw, PCIE_ATU_REGION_INDEX0, |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 295 | PCIE_ATU_TYPE_MEM, |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 296 | pci->dw.mem.phys_start, |
| 297 | pci->dw.mem.bus_start, pci->dw.mem.size); |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /** |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 303 | * pcie_dw_ti_of_to_plat() - Translate from DT to device state |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 304 | * |
| 305 | * @dev: A pointer to the device being operated on |
| 306 | * |
| 307 | * Translate relevant data from the device tree pertaining to device @dev into |
| 308 | * state that the driver will later make use of. This state is stored in the |
| 309 | * device's private data structure. |
| 310 | * |
| 311 | * Return: 0 on success, else -EINVAL |
| 312 | */ |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 313 | static int pcie_dw_ti_of_to_plat(struct udevice *dev) |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 314 | { |
| 315 | struct pcie_dw_ti *pcie = dev_get_priv(dev); |
| 316 | |
| 317 | /* Get the controller base address */ |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 318 | pcie->dw.dbi_base = (void *)dev_read_addr_name(dev, "dbics"); |
| 319 | if ((fdt_addr_t)pcie->dw.dbi_base == FDT_ADDR_T_NONE) |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 320 | return -EINVAL; |
| 321 | |
| 322 | /* Get the config space base address and size */ |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 323 | pcie->dw.cfg_base = (void *)dev_read_addr_size_name(dev, "config", |
| 324 | &pcie->dw.cfg_size); |
| 325 | if ((fdt_addr_t)pcie->dw.cfg_base == FDT_ADDR_T_NONE) |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 326 | return -EINVAL; |
| 327 | |
| 328 | /* Get the iATU base address and size */ |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 329 | pcie->dw.atu_base = (void *)dev_read_addr_name(dev, "atu"); |
| 330 | if ((fdt_addr_t)pcie->dw.atu_base == FDT_ADDR_T_NONE) |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 331 | return -EINVAL; |
| 332 | |
| 333 | /* Get the app base address and size */ |
| 334 | pcie->app_base = (void *)dev_read_addr_name(dev, "app"); |
| 335 | if ((fdt_addr_t)pcie->app_base == FDT_ADDR_T_NONE) |
| 336 | return -EINVAL; |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static const struct dm_pci_ops pcie_dw_ti_ops = { |
Neil Armstrong | c0c39ce | 2021-03-25 15:49:19 +0100 | [diff] [blame] | 342 | .read_config = pcie_dw_read_config, |
| 343 | .write_config = pcie_dw_write_config, |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | static const struct udevice_id pcie_dw_ti_ids[] = { |
| 347 | { .compatible = "ti,am654-pcie-rc" }, |
| 348 | { } |
| 349 | }; |
| 350 | |
| 351 | U_BOOT_DRIVER(pcie_dw_ti) = { |
| 352 | .name = "pcie_dw_ti", |
| 353 | .id = UCLASS_PCI, |
| 354 | .of_match = pcie_dw_ti_ids, |
| 355 | .ops = &pcie_dw_ti_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 356 | .of_to_plat = pcie_dw_ti_of_to_plat, |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 357 | .probe = pcie_dw_ti_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 358 | .priv_auto = sizeof(struct pcie_dw_ti), |
Sekhar Nori | 18db23d | 2019-08-01 19:12:57 +0530 | [diff] [blame] | 359 | }; |