Wilson Ding | e51f2b1 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * *************************************************************************** |
| 3 | * Copyright (C) 2015 Marvell International Ltd. |
| 4 | * *************************************************************************** |
| 5 | * This program is free software: you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License as published by the Free |
| 7 | * Software Foundation, either version 2 of the License, or any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * *************************************************************************** |
| 17 | */ |
| 18 | /* pcie_advk.c |
| 19 | * |
| 20 | * Ported from Linux driver - driver/pci/host/pci-aardvark.c |
| 21 | * |
| 22 | * Author: Victor Gu <xigu@marvell.com> |
| 23 | * Hezi Shahmoon <hezi.shahmoon@marvell.com> |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <dm.h> |
| 29 | #include <pci.h> |
| 30 | #include <asm/io.h> |
| 31 | #include <asm-generic/gpio.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 32 | #include <dm/device_compat.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 33 | #include <linux/bitops.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 34 | #include <linux/delay.h> |
Wilson Ding | e51f2b1 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 35 | #include <linux/ioport.h> |
| 36 | |
| 37 | /* PCIe core registers */ |
| 38 | #define PCIE_CORE_CMD_STATUS_REG 0x4 |
| 39 | #define PCIE_CORE_CMD_IO_ACCESS_EN BIT(0) |
| 40 | #define PCIE_CORE_CMD_MEM_ACCESS_EN BIT(1) |
| 41 | #define PCIE_CORE_CMD_MEM_IO_REQ_EN BIT(2) |
| 42 | #define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8 |
| 43 | #define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4) |
| 44 | #define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11) |
| 45 | #define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0 |
| 46 | #define PCIE_CORE_LINK_TRAINING BIT(5) |
| 47 | #define PCIE_CORE_ERR_CAPCTL_REG 0x118 |
| 48 | #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5) |
| 49 | #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6) |
| 50 | #define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK BIT(7) |
| 51 | #define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV BIT(8) |
| 52 | |
| 53 | /* PIO registers base address and register offsets */ |
| 54 | #define PIO_BASE_ADDR 0x4000 |
| 55 | #define PIO_CTRL (PIO_BASE_ADDR + 0x0) |
| 56 | #define PIO_CTRL_TYPE_MASK GENMASK(3, 0) |
| 57 | #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24) |
| 58 | #define PIO_STAT (PIO_BASE_ADDR + 0x4) |
| 59 | #define PIO_COMPLETION_STATUS_SHIFT 7 |
| 60 | #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7) |
| 61 | #define PIO_COMPLETION_STATUS_OK 0 |
| 62 | #define PIO_COMPLETION_STATUS_UR 1 |
| 63 | #define PIO_COMPLETION_STATUS_CRS 2 |
| 64 | #define PIO_COMPLETION_STATUS_CA 4 |
| 65 | #define PIO_NON_POSTED_REQ BIT(10) |
| 66 | #define PIO_ERR_STATUS BIT(11) |
| 67 | #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8) |
| 68 | #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc) |
| 69 | #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10) |
| 70 | #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14) |
| 71 | #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18) |
| 72 | #define PIO_START (PIO_BASE_ADDR + 0x1c) |
| 73 | #define PIO_ISR (PIO_BASE_ADDR + 0x20) |
| 74 | |
| 75 | /* Aardvark Control registers */ |
| 76 | #define CONTROL_BASE_ADDR 0x4800 |
| 77 | #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0) |
| 78 | #define PCIE_GEN_SEL_MSK 0x3 |
| 79 | #define PCIE_GEN_SEL_SHIFT 0x0 |
| 80 | #define SPEED_GEN_1 0 |
| 81 | #define SPEED_GEN_2 1 |
| 82 | #define SPEED_GEN_3 2 |
| 83 | #define IS_RC_MSK 1 |
| 84 | #define IS_RC_SHIFT 2 |
| 85 | #define LANE_CNT_MSK 0x18 |
| 86 | #define LANE_CNT_SHIFT 0x3 |
| 87 | #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT) |
| 88 | #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT) |
| 89 | #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT) |
| 90 | #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT) |
| 91 | #define LINK_TRAINING_EN BIT(6) |
| 92 | #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8) |
| 93 | #define PCIE_CORE_CTRL2_RESERVED 0x7 |
| 94 | #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4) |
| 95 | #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5) |
| 96 | #define PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE BIT(6) |
| 97 | |
| 98 | /* LMI registers base address and register offsets */ |
| 99 | #define LMI_BASE_ADDR 0x6000 |
| 100 | #define CFG_REG (LMI_BASE_ADDR + 0x0) |
| 101 | #define LTSSM_SHIFT 24 |
| 102 | #define LTSSM_MASK 0x3f |
| 103 | #define LTSSM_L0 0x10 |
| 104 | |
| 105 | /* PCIe core controller registers */ |
| 106 | #define CTRL_CORE_BASE_ADDR 0x18000 |
| 107 | #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0) |
| 108 | #define CTRL_MODE_SHIFT 0x0 |
| 109 | #define CTRL_MODE_MASK 0x1 |
| 110 | #define PCIE_CORE_MODE_DIRECT 0x0 |
| 111 | #define PCIE_CORE_MODE_COMMAND 0x1 |
| 112 | |
| 113 | /* Transaction types */ |
| 114 | #define PCIE_CONFIG_RD_TYPE0 0x8 |
| 115 | #define PCIE_CONFIG_RD_TYPE1 0x9 |
| 116 | #define PCIE_CONFIG_WR_TYPE0 0xa |
| 117 | #define PCIE_CONFIG_WR_TYPE1 0xb |
| 118 | |
| 119 | /* PCI_BDF shifts 8bit, so we need extra 4bit shift */ |
| 120 | #define PCIE_BDF(dev) (dev << 4) |
| 121 | #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20) |
| 122 | #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15) |
| 123 | #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12) |
| 124 | #define PCIE_CONF_REG(reg) ((reg) & 0xffc) |
| 125 | #define PCIE_CONF_ADDR(bus, devfn, where) \ |
| 126 | (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \ |
| 127 | PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where)) |
| 128 | |
| 129 | /* PCIe Retries & Timeout definitions */ |
| 130 | #define MAX_RETRIES 10 |
| 131 | #define PIO_WAIT_TIMEOUT 100 |
| 132 | #define LINK_WAIT_TIMEOUT 100000 |
| 133 | |
| 134 | #define CFG_RD_UR_VAL 0xFFFFFFFF |
| 135 | #define CFG_RD_CRS_VAL 0xFFFF0001 |
| 136 | |
Wilson Ding | e51f2b1 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 137 | /** |
| 138 | * struct pcie_advk - Advk PCIe controller state |
| 139 | * |
| 140 | * @reg_base: The base address of the register space. |
| 141 | * @first_busno: This driver supports multiple PCIe controllers. |
| 142 | * first_busno stores the bus number of the PCIe root-port |
| 143 | * number which may vary depending on the PCIe setup |
| 144 | * (PEX switches etc). |
| 145 | * @device: The pointer to PCI uclass device. |
| 146 | */ |
| 147 | struct pcie_advk { |
| 148 | void *base; |
| 149 | int first_busno; |
| 150 | struct udevice *dev; |
| 151 | }; |
| 152 | |
| 153 | static inline void advk_writel(struct pcie_advk *pcie, uint val, uint reg) |
| 154 | { |
| 155 | writel(val, pcie->base + reg); |
| 156 | } |
| 157 | |
| 158 | static inline uint advk_readl(struct pcie_advk *pcie, uint reg) |
| 159 | { |
| 160 | return readl(pcie->base + reg); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * pcie_advk_addr_valid() - Check for valid bus address |
| 165 | * |
| 166 | * @bdf: The PCI device to access |
| 167 | * @first_busno: Bus number of the PCIe controller root complex |
| 168 | * |
| 169 | * Return: 1 on valid, 0 on invalid |
| 170 | */ |
| 171 | static int pcie_advk_addr_valid(pci_dev_t bdf, int first_busno) |
| 172 | { |
| 173 | /* |
| 174 | * In PCIE-E only a single device (0) can exist |
| 175 | * on the local bus. Beyound the local bus, there might be |
| 176 | * a Switch and everything is possible. |
| 177 | */ |
| 178 | if ((PCI_BUS(bdf) == first_busno) && (PCI_DEV(bdf) > 0)) |
| 179 | return 0; |
| 180 | |
| 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * pcie_advk_wait_pio() - Wait for PIO access to be accomplished |
| 186 | * |
| 187 | * @pcie: The PCI device to access |
| 188 | * |
| 189 | * Wait up to 1 micro second for PIO access to be accomplished. |
| 190 | * |
| 191 | * Return 1 (true) if PIO access is accomplished. |
| 192 | * Return 0 (false) if PIO access is timed out. |
| 193 | */ |
| 194 | static int pcie_advk_wait_pio(struct pcie_advk *pcie) |
| 195 | { |
| 196 | uint start, isr; |
| 197 | uint count; |
| 198 | |
| 199 | for (count = 0; count < MAX_RETRIES; count++) { |
| 200 | start = advk_readl(pcie, PIO_START); |
| 201 | isr = advk_readl(pcie, PIO_ISR); |
| 202 | if (!start && isr) |
| 203 | return 1; |
| 204 | /* |
| 205 | * Do not check the PIO state too frequently, |
| 206 | * 100us delay is appropriate. |
| 207 | */ |
| 208 | udelay(PIO_WAIT_TIMEOUT); |
| 209 | } |
| 210 | |
| 211 | dev_err(pcie->dev, "config read/write timed out\n"); |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * pcie_advk_check_pio_status() - Validate PIO status and get the read result |
| 217 | * |
| 218 | * @pcie: Pointer to the PCI bus |
| 219 | * @read: Read from or write to configuration space - true(read) false(write) |
| 220 | * @read_val: Pointer to the read result, only valid when read is true |
| 221 | * |
| 222 | */ |
| 223 | static int pcie_advk_check_pio_status(struct pcie_advk *pcie, |
| 224 | bool read, |
| 225 | uint *read_val) |
| 226 | { |
| 227 | uint reg; |
| 228 | unsigned int status; |
| 229 | char *strcomp_status, *str_posted; |
| 230 | |
| 231 | reg = advk_readl(pcie, PIO_STAT); |
| 232 | status = (reg & PIO_COMPLETION_STATUS_MASK) >> |
| 233 | PIO_COMPLETION_STATUS_SHIFT; |
| 234 | |
| 235 | switch (status) { |
| 236 | case PIO_COMPLETION_STATUS_OK: |
| 237 | if (reg & PIO_ERR_STATUS) { |
| 238 | strcomp_status = "COMP_ERR"; |
| 239 | break; |
| 240 | } |
| 241 | /* Get the read result */ |
| 242 | if (read) |
| 243 | *read_val = advk_readl(pcie, PIO_RD_DATA); |
| 244 | /* No error */ |
| 245 | strcomp_status = NULL; |
| 246 | break; |
| 247 | case PIO_COMPLETION_STATUS_UR: |
| 248 | if (read) { |
| 249 | /* For reading, UR is not an error status. */ |
| 250 | *read_val = CFG_RD_UR_VAL; |
| 251 | strcomp_status = NULL; |
| 252 | } else { |
| 253 | strcomp_status = "UR"; |
| 254 | } |
| 255 | break; |
| 256 | case PIO_COMPLETION_STATUS_CRS: |
| 257 | if (read) { |
| 258 | /* For reading, CRS is not an error status. */ |
| 259 | *read_val = CFG_RD_CRS_VAL; |
| 260 | strcomp_status = NULL; |
| 261 | } else { |
| 262 | strcomp_status = "CRS"; |
| 263 | } |
| 264 | break; |
| 265 | case PIO_COMPLETION_STATUS_CA: |
| 266 | strcomp_status = "CA"; |
| 267 | break; |
| 268 | default: |
| 269 | strcomp_status = "Unknown"; |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | if (!strcomp_status) |
| 274 | return 0; |
| 275 | |
| 276 | if (reg & PIO_NON_POSTED_REQ) |
| 277 | str_posted = "Non-posted"; |
| 278 | else |
| 279 | str_posted = "Posted"; |
| 280 | |
| 281 | dev_err(pcie->dev, "%s PIO Response Status: %s, %#x @ %#x\n", |
| 282 | str_posted, strcomp_status, reg, |
| 283 | advk_readl(pcie, PIO_ADDR_LS)); |
| 284 | |
| 285 | return -EFAULT; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * pcie_advk_read_config() - Read from configuration space |
| 290 | * |
| 291 | * @bus: Pointer to the PCI bus |
| 292 | * @bdf: Identifies the PCIe device to access |
| 293 | * @offset: The offset into the device's configuration space |
| 294 | * @valuep: A pointer at which to store the read value |
| 295 | * @size: Indicates the size of access to perform |
| 296 | * |
| 297 | * Read a value of size @size from offset @offset within the configuration |
| 298 | * space of the device identified by the bus, device & function numbers in @bdf |
| 299 | * on the PCI bus @bus. |
| 300 | * |
| 301 | * Return: 0 on success |
| 302 | */ |
Simon Glass | c4e72c4 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 303 | static int pcie_advk_read_config(const struct udevice *bus, pci_dev_t bdf, |
Wilson Ding | e51f2b1 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 304 | uint offset, ulong *valuep, |
| 305 | enum pci_size_t size) |
| 306 | { |
| 307 | struct pcie_advk *pcie = dev_get_priv(bus); |
| 308 | uint reg; |
| 309 | int ret; |
| 310 | |
| 311 | dev_dbg(pcie->dev, "PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ", |
| 312 | PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
| 313 | |
| 314 | if (!pcie_advk_addr_valid(bdf, pcie->first_busno)) { |
| 315 | dev_dbg(pcie->dev, "- out of range\n"); |
| 316 | *valuep = pci_get_ff(size); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /* Start PIO */ |
| 321 | advk_writel(pcie, 0, PIO_START); |
| 322 | advk_writel(pcie, 1, PIO_ISR); |
| 323 | |
| 324 | /* Program the control register */ |
| 325 | reg = advk_readl(pcie, PIO_CTRL); |
| 326 | reg &= ~PIO_CTRL_TYPE_MASK; |
| 327 | if (PCI_BUS(bdf) == pcie->first_busno) |
| 328 | reg |= PCIE_CONFIG_RD_TYPE0; |
| 329 | else |
| 330 | reg |= PCIE_CONFIG_RD_TYPE1; |
| 331 | advk_writel(pcie, reg, PIO_CTRL); |
| 332 | |
| 333 | /* Program the address registers */ |
| 334 | reg = PCIE_BDF(bdf) | PCIE_CONF_REG(offset); |
| 335 | advk_writel(pcie, reg, PIO_ADDR_LS); |
| 336 | advk_writel(pcie, 0, PIO_ADDR_MS); |
| 337 | |
| 338 | /* Start the transfer */ |
| 339 | advk_writel(pcie, 1, PIO_START); |
| 340 | |
| 341 | if (!pcie_advk_wait_pio(pcie)) |
| 342 | return -EINVAL; |
| 343 | |
| 344 | /* Check PIO status and get the read result */ |
| 345 | ret = pcie_advk_check_pio_status(pcie, true, ®); |
| 346 | if (ret) |
| 347 | return ret; |
| 348 | |
| 349 | dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n", |
| 350 | offset, size, reg); |
| 351 | *valuep = pci_conv_32_to_size(reg, offset, size); |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * pcie_calc_datastrobe() - Calculate data strobe |
| 358 | * |
| 359 | * @offset: The offset into the device's configuration space |
| 360 | * @size: Indicates the size of access to perform |
| 361 | * |
| 362 | * Calculate data strobe according to offset and size |
| 363 | * |
| 364 | */ |
| 365 | static uint pcie_calc_datastrobe(uint offset, enum pci_size_t size) |
| 366 | { |
| 367 | uint bytes, data_strobe; |
| 368 | |
| 369 | switch (size) { |
| 370 | case PCI_SIZE_8: |
| 371 | bytes = 1; |
| 372 | break; |
| 373 | case PCI_SIZE_16: |
| 374 | bytes = 2; |
| 375 | break; |
| 376 | default: |
| 377 | bytes = 4; |
| 378 | } |
| 379 | |
| 380 | data_strobe = GENMASK(bytes - 1, 0) << (offset & 0x3); |
| 381 | |
| 382 | return data_strobe; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * pcie_advk_write_config() - Write to configuration space |
| 387 | * |
| 388 | * @bus: Pointer to the PCI bus |
| 389 | * @bdf: Identifies the PCIe device to access |
| 390 | * @offset: The offset into the device's configuration space |
| 391 | * @value: The value to write |
| 392 | * @size: Indicates the size of access to perform |
| 393 | * |
| 394 | * Write the value @value of size @size from offset @offset within the |
| 395 | * configuration space of the device identified by the bus, device & function |
| 396 | * numbers in @bdf on the PCI bus @bus. |
| 397 | * |
| 398 | * Return: 0 on success |
| 399 | */ |
| 400 | static int pcie_advk_write_config(struct udevice *bus, pci_dev_t bdf, |
| 401 | uint offset, ulong value, |
| 402 | enum pci_size_t size) |
| 403 | { |
| 404 | struct pcie_advk *pcie = dev_get_priv(bus); |
| 405 | uint reg; |
| 406 | |
| 407 | dev_dbg(pcie->dev, "PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ", |
| 408 | PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
| 409 | dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", |
| 410 | offset, size, value); |
| 411 | |
| 412 | if (!pcie_advk_addr_valid(bdf, pcie->first_busno)) { |
| 413 | dev_dbg(pcie->dev, "- out of range\n"); |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | /* Start PIO */ |
| 418 | advk_writel(pcie, 0, PIO_START); |
| 419 | advk_writel(pcie, 1, PIO_ISR); |
| 420 | |
| 421 | /* Program the control register */ |
| 422 | reg = advk_readl(pcie, PIO_CTRL); |
| 423 | reg &= ~PIO_CTRL_TYPE_MASK; |
| 424 | if (PCI_BUS(bdf) == pcie->first_busno) |
| 425 | reg |= PCIE_CONFIG_WR_TYPE0; |
| 426 | else |
| 427 | reg |= PCIE_CONFIG_WR_TYPE1; |
| 428 | advk_writel(pcie, reg, PIO_CTRL); |
| 429 | |
| 430 | /* Program the address registers */ |
| 431 | reg = PCIE_BDF(bdf) | PCIE_CONF_REG(offset); |
| 432 | advk_writel(pcie, reg, PIO_ADDR_LS); |
| 433 | advk_writel(pcie, 0, PIO_ADDR_MS); |
| 434 | dev_dbg(pcie->dev, "\tPIO req. - addr = 0x%08x\n", reg); |
| 435 | |
| 436 | /* Program the data register */ |
| 437 | reg = pci_conv_size_to_32(0, value, offset, size); |
| 438 | advk_writel(pcie, reg, PIO_WR_DATA); |
| 439 | dev_dbg(pcie->dev, "\tPIO req. - val = 0x%08x\n", reg); |
| 440 | |
| 441 | /* Program the data strobe */ |
| 442 | reg = pcie_calc_datastrobe(offset, size); |
| 443 | advk_writel(pcie, reg, PIO_WR_DATA_STRB); |
| 444 | dev_dbg(pcie->dev, "\tPIO req. - strb = 0x%02x\n", reg); |
| 445 | |
| 446 | /* Start the transfer */ |
| 447 | advk_writel(pcie, 1, PIO_START); |
| 448 | |
| 449 | if (!pcie_advk_wait_pio(pcie)) { |
| 450 | dev_dbg(pcie->dev, "- wait pio timeout\n"); |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | |
| 454 | /* Check PIO status */ |
| 455 | pcie_advk_check_pio_status(pcie, false, ®); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * pcie_advk_link_up() - Check if PCIe link is up or not |
| 462 | * |
| 463 | * @pcie: The PCI device to access |
| 464 | * |
| 465 | * Return 1 (true) on link up. |
| 466 | * Return 0 (false) on link down. |
| 467 | */ |
| 468 | static int pcie_advk_link_up(struct pcie_advk *pcie) |
| 469 | { |
| 470 | u32 val, ltssm_state; |
| 471 | |
| 472 | val = advk_readl(pcie, CFG_REG); |
| 473 | ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK; |
| 474 | return ltssm_state >= LTSSM_L0; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * pcie_advk_wait_for_link() - Wait for link training to be accomplished |
| 479 | * |
| 480 | * @pcie: The PCI device to access |
| 481 | * |
| 482 | * Wait up to 1 second for link training to be accomplished. |
| 483 | * |
| 484 | * Return 1 (true) if link training ends up with link up success. |
| 485 | * Return 0 (false) if link training ends up with link up failure. |
| 486 | */ |
| 487 | static int pcie_advk_wait_for_link(struct pcie_advk *pcie) |
| 488 | { |
| 489 | int retries; |
| 490 | |
| 491 | /* check if the link is up or not */ |
| 492 | for (retries = 0; retries < MAX_RETRIES; retries++) { |
| 493 | if (pcie_advk_link_up(pcie)) { |
| 494 | printf("PCIE-%d: Link up\n", pcie->first_busno); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | udelay(LINK_WAIT_TIMEOUT); |
| 499 | } |
| 500 | |
| 501 | printf("PCIE-%d: Link down\n", pcie->first_busno); |
| 502 | |
| 503 | return -ETIMEDOUT; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * pcie_advk_setup_hw() - PCIe initailzation |
| 508 | * |
| 509 | * @pcie: The PCI device to access |
| 510 | * |
| 511 | * Return: 0 on success |
| 512 | */ |
| 513 | static int pcie_advk_setup_hw(struct pcie_advk *pcie) |
| 514 | { |
| 515 | u32 reg; |
| 516 | |
| 517 | /* Set to Direct mode */ |
| 518 | reg = advk_readl(pcie, CTRL_CONFIG_REG); |
| 519 | reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT); |
| 520 | reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT); |
| 521 | advk_writel(pcie, reg, CTRL_CONFIG_REG); |
| 522 | |
| 523 | /* Set PCI global control register to RC mode */ |
| 524 | reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); |
| 525 | reg |= (IS_RC_MSK << IS_RC_SHIFT); |
| 526 | advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); |
| 527 | |
| 528 | /* Set Advanced Error Capabilities and Control PF0 register */ |
| 529 | reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX | |
| 530 | PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN | |
| 531 | PCIE_CORE_ERR_CAPCTL_ECRC_CHECK | |
| 532 | PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV; |
| 533 | advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG); |
| 534 | |
| 535 | /* Set PCIe Device Control and Status 1 PF0 register */ |
| 536 | reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE | |
| 537 | PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE; |
| 538 | advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG); |
| 539 | |
| 540 | /* Program PCIe Control 2 to disable strict ordering */ |
| 541 | reg = PCIE_CORE_CTRL2_RESERVED | |
| 542 | PCIE_CORE_CTRL2_TD_ENABLE; |
| 543 | advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); |
| 544 | |
| 545 | /* Set GEN2 */ |
| 546 | reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); |
| 547 | reg &= ~PCIE_GEN_SEL_MSK; |
| 548 | reg |= SPEED_GEN_2; |
| 549 | advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); |
| 550 | |
| 551 | /* Set lane X1 */ |
| 552 | reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); |
| 553 | reg &= ~LANE_CNT_MSK; |
| 554 | reg |= LANE_COUNT_1; |
| 555 | advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); |
| 556 | |
| 557 | /* Enable link training */ |
| 558 | reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); |
| 559 | reg |= LINK_TRAINING_EN; |
| 560 | advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); |
| 561 | |
| 562 | /* |
| 563 | * Enable AXI address window location generation: |
| 564 | * When it is enabled, the default outbound window |
| 565 | * configurations (Default User Field: 0xD0074CFC) |
| 566 | * are used to transparent address translation for |
| 567 | * the outbound transactions. Thus, PCIe address |
| 568 | * windows are not required. |
| 569 | */ |
| 570 | reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG); |
| 571 | reg |= PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE; |
| 572 | advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); |
| 573 | |
| 574 | /* |
| 575 | * Bypass the address window mapping for PIO: |
| 576 | * Since PIO access already contains all required |
| 577 | * info over AXI interface by PIO registers, the |
| 578 | * address window is not required. |
| 579 | */ |
| 580 | reg = advk_readl(pcie, PIO_CTRL); |
| 581 | reg |= PIO_CTRL_ADDR_WIN_DISABLE; |
| 582 | advk_writel(pcie, reg, PIO_CTRL); |
| 583 | |
| 584 | /* Start link training */ |
| 585 | reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG); |
| 586 | reg |= PCIE_CORE_LINK_TRAINING; |
| 587 | advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG); |
| 588 | |
| 589 | /* Wait for PCIe link up */ |
| 590 | if (pcie_advk_wait_for_link(pcie)) |
| 591 | return -ENXIO; |
| 592 | |
| 593 | reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); |
| 594 | reg |= PCIE_CORE_CMD_MEM_ACCESS_EN | |
| 595 | PCIE_CORE_CMD_IO_ACCESS_EN | |
| 596 | PCIE_CORE_CMD_MEM_IO_REQ_EN; |
| 597 | advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * pcie_advk_probe() - Probe the PCIe bus for active link |
| 604 | * |
| 605 | * @dev: A pointer to the device being operated on |
| 606 | * |
| 607 | * Probe for an active link on the PCIe bus and configure the controller |
| 608 | * to enable this port. |
| 609 | * |
| 610 | * Return: 0 on success, else -ENODEV |
| 611 | */ |
| 612 | static int pcie_advk_probe(struct udevice *dev) |
| 613 | { |
| 614 | struct pcie_advk *pcie = dev_get_priv(dev); |
| 615 | |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 616 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Wilson Ding | e51f2b1 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 617 | struct gpio_desc reset_gpio; |
| 618 | |
| 619 | gpio_request_by_name(dev, "reset-gpio", 0, &reset_gpio, |
| 620 | GPIOD_IS_OUT); |
| 621 | /* |
| 622 | * Issue reset to add-in card through the dedicated GPIO. |
| 623 | * Some boards are connecting the card reset pin to common system |
| 624 | * reset wire and others are using separate GPIO port. |
| 625 | * In the last case we have to release a reset of the addon card |
| 626 | * using this GPIO. |
| 627 | * |
| 628 | * FIX-ME: |
| 629 | * The PCIe RESET signal is not supposed to be released along |
| 630 | * with the SOC RESET signal. It should be lowered as early as |
| 631 | * possible before PCIe PHY initialization. Moreover, the PCIe |
| 632 | * clock should be gated as well. |
| 633 | */ |
| 634 | if (dm_gpio_is_valid(&reset_gpio)) { |
| 635 | dev_dbg(pcie->dev, "Toggle PCIE Reset GPIO ...\n"); |
| 636 | dm_gpio_set_value(&reset_gpio, 0); |
| 637 | mdelay(200); |
| 638 | dm_gpio_set_value(&reset_gpio, 1); |
| 639 | } |
| 640 | #else |
| 641 | dev_dbg(pcie->dev, "PCIE Reset on GPIO support is missing\n"); |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 642 | #endif /* DM_GPIO */ |
Wilson Ding | e51f2b1 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 643 | |
| 644 | pcie->first_busno = dev->seq; |
| 645 | pcie->dev = pci_get_controller(dev); |
| 646 | |
| 647 | return pcie_advk_setup_hw(pcie); |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * pcie_advk_ofdata_to_platdata() - Translate from DT to device state |
| 652 | * |
| 653 | * @dev: A pointer to the device being operated on |
| 654 | * |
| 655 | * Translate relevant data from the device tree pertaining to device @dev into |
| 656 | * state that the driver will later make use of. This state is stored in the |
| 657 | * device's private data structure. |
| 658 | * |
| 659 | * Return: 0 on success, else -EINVAL |
| 660 | */ |
| 661 | static int pcie_advk_ofdata_to_platdata(struct udevice *dev) |
| 662 | { |
| 663 | struct pcie_advk *pcie = dev_get_priv(dev); |
| 664 | |
| 665 | /* Get the register base address */ |
| 666 | pcie->base = (void *)dev_read_addr_index(dev, 0); |
| 667 | if ((fdt_addr_t)pcie->base == FDT_ADDR_T_NONE) |
| 668 | return -EINVAL; |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static const struct dm_pci_ops pcie_advk_ops = { |
| 674 | .read_config = pcie_advk_read_config, |
| 675 | .write_config = pcie_advk_write_config, |
| 676 | }; |
| 677 | |
| 678 | static const struct udevice_id pcie_advk_ids[] = { |
| 679 | { .compatible = "marvell,armada-37xx-pcie" }, |
| 680 | { } |
| 681 | }; |
| 682 | |
| 683 | U_BOOT_DRIVER(pcie_advk) = { |
| 684 | .name = "pcie_advk", |
| 685 | .id = UCLASS_PCI, |
| 686 | .of_match = pcie_advk_ids, |
| 687 | .ops = &pcie_advk_ops, |
| 688 | .ofdata_to_platdata = pcie_advk_ofdata_to_platdata, |
| 689 | .probe = pcie_advk_probe, |
| 690 | .priv_auto_alloc_size = sizeof(struct pcie_advk), |
| 691 | }; |