Wilson Ding | a6bdc86 | 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 | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 32 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 33 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 34 | #include <linux/delay.h> |
Wilson Ding | a6bdc86 | 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) |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 42 | #define PCIE_CORE_DEV_REV_REG 0x8 |
| 43 | #define PCIE_CORE_EXP_ROM_BAR_REG 0x30 |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 44 | #define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8 |
| 45 | #define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4) |
| 46 | #define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11) |
Pali Rohár | 9057a2c | 2021-02-05 15:32:28 +0100 | [diff] [blame] | 47 | #define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE 0x2 |
| 48 | #define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT 5 |
| 49 | #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE 0x2 |
| 50 | #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT 12 |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 51 | #define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0 |
| 52 | #define PCIE_CORE_LINK_TRAINING BIT(5) |
| 53 | #define PCIE_CORE_ERR_CAPCTL_REG 0x118 |
| 54 | #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5) |
| 55 | #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6) |
| 56 | #define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK BIT(7) |
| 57 | #define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV BIT(8) |
| 58 | |
| 59 | /* PIO registers base address and register offsets */ |
| 60 | #define PIO_BASE_ADDR 0x4000 |
| 61 | #define PIO_CTRL (PIO_BASE_ADDR + 0x0) |
| 62 | #define PIO_CTRL_TYPE_MASK GENMASK(3, 0) |
| 63 | #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24) |
| 64 | #define PIO_STAT (PIO_BASE_ADDR + 0x4) |
| 65 | #define PIO_COMPLETION_STATUS_SHIFT 7 |
| 66 | #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7) |
| 67 | #define PIO_COMPLETION_STATUS_OK 0 |
| 68 | #define PIO_COMPLETION_STATUS_UR 1 |
| 69 | #define PIO_COMPLETION_STATUS_CRS 2 |
| 70 | #define PIO_COMPLETION_STATUS_CA 4 |
| 71 | #define PIO_NON_POSTED_REQ BIT(10) |
| 72 | #define PIO_ERR_STATUS BIT(11) |
| 73 | #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8) |
| 74 | #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc) |
| 75 | #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10) |
| 76 | #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14) |
| 77 | #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18) |
| 78 | #define PIO_START (PIO_BASE_ADDR + 0x1c) |
| 79 | #define PIO_ISR (PIO_BASE_ADDR + 0x20) |
| 80 | |
| 81 | /* Aardvark Control registers */ |
| 82 | #define CONTROL_BASE_ADDR 0x4800 |
| 83 | #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0) |
| 84 | #define PCIE_GEN_SEL_MSK 0x3 |
| 85 | #define PCIE_GEN_SEL_SHIFT 0x0 |
| 86 | #define SPEED_GEN_1 0 |
| 87 | #define SPEED_GEN_2 1 |
| 88 | #define SPEED_GEN_3 2 |
| 89 | #define IS_RC_MSK 1 |
| 90 | #define IS_RC_SHIFT 2 |
| 91 | #define LANE_CNT_MSK 0x18 |
| 92 | #define LANE_CNT_SHIFT 0x3 |
| 93 | #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT) |
| 94 | #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT) |
| 95 | #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT) |
| 96 | #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT) |
| 97 | #define LINK_TRAINING_EN BIT(6) |
| 98 | #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8) |
| 99 | #define PCIE_CORE_CTRL2_RESERVED 0x7 |
| 100 | #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4) |
| 101 | #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5) |
| 102 | #define PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE BIT(6) |
| 103 | |
Pali Rohár | e78c8e0 | 2021-05-26 17:59:40 +0200 | [diff] [blame] | 104 | /* PCIe window configuration */ |
| 105 | #define OB_WIN_BASE_ADDR 0x4c00 |
| 106 | #define OB_WIN_BLOCK_SIZE 0x20 |
| 107 | #define OB_WIN_COUNT 8 |
| 108 | #define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \ |
| 109 | OB_WIN_BLOCK_SIZE * (win) + \ |
| 110 | (offset)) |
| 111 | #define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00) |
| 112 | #define OB_WIN_ENABLE BIT(0) |
| 113 | #define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04) |
| 114 | #define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08) |
| 115 | #define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c) |
| 116 | #define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10) |
| 117 | #define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14) |
| 118 | #define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18) |
| 119 | #define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4) |
| 120 | #define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24) |
| 121 | #define OB_WIN_FUNC_NUM_SHIFT 24 |
| 122 | #define OB_WIN_FUNC_NUM_ENABLE BIT(23) |
| 123 | #define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20) |
| 124 | #define OB_WIN_BUS_NUM_BITS_SHIFT 20 |
| 125 | #define OB_WIN_MSG_CODE_ENABLE BIT(22) |
| 126 | #define OB_WIN_MSG_CODE_MASK GENMASK(21, 14) |
| 127 | #define OB_WIN_MSG_CODE_SHIFT 14 |
| 128 | #define OB_WIN_MSG_PAYLOAD_LEN BIT(12) |
| 129 | #define OB_WIN_ATTR_ENABLE BIT(11) |
| 130 | #define OB_WIN_ATTR_TC_MASK GENMASK(10, 8) |
| 131 | #define OB_WIN_ATTR_TC_SHIFT 8 |
| 132 | #define OB_WIN_ATTR_RELAXED BIT(7) |
| 133 | #define OB_WIN_ATTR_NOSNOOP BIT(6) |
| 134 | #define OB_WIN_ATTR_POISON BIT(5) |
| 135 | #define OB_WIN_ATTR_IDO BIT(4) |
| 136 | #define OB_WIN_TYPE_MASK GENMASK(3, 0) |
| 137 | #define OB_WIN_TYPE_SHIFT 0 |
| 138 | #define OB_WIN_TYPE_MEM 0x0 |
| 139 | #define OB_WIN_TYPE_IO 0x4 |
| 140 | #define OB_WIN_TYPE_CONFIG_TYPE0 0x8 |
| 141 | #define OB_WIN_TYPE_CONFIG_TYPE1 0x9 |
| 142 | #define OB_WIN_TYPE_MSG 0xc |
| 143 | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 144 | /* LMI registers base address and register offsets */ |
| 145 | #define LMI_BASE_ADDR 0x6000 |
| 146 | #define CFG_REG (LMI_BASE_ADDR + 0x0) |
| 147 | #define LTSSM_SHIFT 24 |
| 148 | #define LTSSM_MASK 0x3f |
| 149 | #define LTSSM_L0 0x10 |
Pali Rohár | 6d11d9e | 2021-09-26 00:54:41 +0200 | [diff] [blame] | 150 | #define LTSSM_DISABLED 0x20 |
Pali Rohár | ba40b6c | 2021-03-03 14:37:59 +0100 | [diff] [blame] | 151 | #define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44) |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 152 | |
| 153 | /* PCIe core controller registers */ |
| 154 | #define CTRL_CORE_BASE_ADDR 0x18000 |
| 155 | #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0) |
| 156 | #define CTRL_MODE_SHIFT 0x0 |
| 157 | #define CTRL_MODE_MASK 0x1 |
| 158 | #define PCIE_CORE_MODE_DIRECT 0x0 |
| 159 | #define PCIE_CORE_MODE_COMMAND 0x1 |
| 160 | |
| 161 | /* Transaction types */ |
| 162 | #define PCIE_CONFIG_RD_TYPE0 0x8 |
| 163 | #define PCIE_CONFIG_RD_TYPE1 0x9 |
| 164 | #define PCIE_CONFIG_WR_TYPE0 0xa |
| 165 | #define PCIE_CONFIG_WR_TYPE1 0xb |
| 166 | |
| 167 | /* PCI_BDF shifts 8bit, so we need extra 4bit shift */ |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 168 | #define PCIE_BDF(b, d, f) (PCI_BDF(b, d, f) << 4) |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 169 | #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20) |
| 170 | #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15) |
| 171 | #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12) |
| 172 | #define PCIE_CONF_REG(reg) ((reg) & 0xffc) |
| 173 | #define PCIE_CONF_ADDR(bus, devfn, where) \ |
| 174 | (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \ |
| 175 | PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where)) |
| 176 | |
| 177 | /* PCIe Retries & Timeout definitions */ |
Pali Rohár | fc0eddf | 2021-04-22 16:23:04 +0200 | [diff] [blame] | 178 | #define PIO_MAX_RETRIES 1500 |
| 179 | #define PIO_WAIT_TIMEOUT 1000 |
| 180 | #define LINK_MAX_RETRIES 10 |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 181 | #define LINK_WAIT_TIMEOUT 100000 |
| 182 | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 183 | #define CFG_RD_CRS_VAL 0xFFFF0001 |
| 184 | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 185 | /** |
| 186 | * struct pcie_advk - Advk PCIe controller state |
| 187 | * |
| 188 | * @reg_base: The base address of the register space. |
| 189 | * @first_busno: This driver supports multiple PCIe controllers. |
| 190 | * first_busno stores the bus number of the PCIe root-port |
| 191 | * number which may vary depending on the PCIe setup |
| 192 | * (PEX switches etc). |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 193 | * @sec_busno: sec_busno stores the bus number for the device behind |
| 194 | * the PCIe root-port |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 195 | * @device: The pointer to PCI uclass device. |
| 196 | */ |
| 197 | struct pcie_advk { |
| 198 | void *base; |
| 199 | int first_busno; |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 200 | int sec_busno; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 201 | struct udevice *dev; |
Pali Rohár | 6a58b97 | 2020-08-19 15:57:07 +0200 | [diff] [blame] | 202 | struct gpio_desc reset_gpio; |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 203 | u32 cfgcache[0x34 - 0x10]; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | static inline void advk_writel(struct pcie_advk *pcie, uint val, uint reg) |
| 207 | { |
| 208 | writel(val, pcie->base + reg); |
| 209 | } |
| 210 | |
| 211 | static inline uint advk_readl(struct pcie_advk *pcie, uint reg) |
| 212 | { |
| 213 | return readl(pcie->base + reg); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * pcie_advk_addr_valid() - Check for valid bus address |
| 218 | * |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 219 | * @pcie: Pointer to the PCI bus |
| 220 | * @busno: Bus number of PCI device |
| 221 | * @dev: Device number of PCI device |
| 222 | * @func: Function number of PCI device |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 223 | * @bdf: The PCI device to access |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 224 | * |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 225 | * Return: true on valid, false on invalid |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 226 | */ |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 227 | static bool pcie_advk_addr_valid(struct pcie_advk *pcie, |
| 228 | int busno, u8 dev, u8 func) |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 229 | { |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 230 | /* On the primary (local) bus there is only one PCI Bridge */ |
| 231 | if (busno == pcie->first_busno && (dev != 0 || func != 0)) |
| 232 | return false; |
| 233 | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 234 | /* |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 235 | * In PCI-E only a single device (0) can exist on the secondary bus. |
| 236 | * Beyond the secondary bus, there might be a Switch and anything is |
| 237 | * possible. |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 238 | */ |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 239 | if (busno == pcie->sec_busno && dev != 0) |
| 240 | return false; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 241 | |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 242 | return true; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
| 246 | * pcie_advk_wait_pio() - Wait for PIO access to be accomplished |
| 247 | * |
| 248 | * @pcie: The PCI device to access |
| 249 | * |
Pali Rohár | fc0eddf | 2021-04-22 16:23:04 +0200 | [diff] [blame] | 250 | * Wait up to 1.5 seconds for PIO access to be accomplished. |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 251 | * |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 252 | * Return positive - retry count if PIO access is accomplished. |
| 253 | * Return negative - error if PIO access is timed out. |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 254 | */ |
| 255 | static int pcie_advk_wait_pio(struct pcie_advk *pcie) |
| 256 | { |
| 257 | uint start, isr; |
| 258 | uint count; |
| 259 | |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 260 | for (count = 1; count <= PIO_MAX_RETRIES; count++) { |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 261 | start = advk_readl(pcie, PIO_START); |
| 262 | isr = advk_readl(pcie, PIO_ISR); |
| 263 | if (!start && isr) |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 264 | return count; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 265 | /* |
| 266 | * Do not check the PIO state too frequently, |
| 267 | * 100us delay is appropriate. |
| 268 | */ |
| 269 | udelay(PIO_WAIT_TIMEOUT); |
| 270 | } |
| 271 | |
Pali Rohár | fc0eddf | 2021-04-22 16:23:04 +0200 | [diff] [blame] | 272 | dev_err(pcie->dev, "PIO read/write transfer time out\n"); |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 273 | return -ETIMEDOUT; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
| 277 | * pcie_advk_check_pio_status() - Validate PIO status and get the read result |
| 278 | * |
| 279 | * @pcie: Pointer to the PCI bus |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 280 | * @allow_crs: Only for read requests, if CRS response is allowed |
| 281 | * @read_val: Pointer to the read result |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 282 | * |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 283 | * Return: 0 on success |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 284 | */ |
| 285 | static int pcie_advk_check_pio_status(struct pcie_advk *pcie, |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 286 | bool allow_crs, |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 287 | uint *read_val) |
| 288 | { |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 289 | int ret; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 290 | uint reg; |
| 291 | unsigned int status; |
| 292 | char *strcomp_status, *str_posted; |
| 293 | |
| 294 | reg = advk_readl(pcie, PIO_STAT); |
| 295 | status = (reg & PIO_COMPLETION_STATUS_MASK) >> |
| 296 | PIO_COMPLETION_STATUS_SHIFT; |
| 297 | |
| 298 | switch (status) { |
| 299 | case PIO_COMPLETION_STATUS_OK: |
| 300 | if (reg & PIO_ERR_STATUS) { |
| 301 | strcomp_status = "COMP_ERR"; |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 302 | ret = -EFAULT; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | /* Get the read result */ |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 306 | if (read_val) |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 307 | *read_val = advk_readl(pcie, PIO_RD_DATA); |
| 308 | /* No error */ |
| 309 | strcomp_status = NULL; |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 310 | ret = 0; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 311 | break; |
| 312 | case PIO_COMPLETION_STATUS_UR: |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 313 | strcomp_status = "UR"; |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 314 | ret = -EOPNOTSUPP; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 315 | break; |
| 316 | case PIO_COMPLETION_STATUS_CRS: |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 317 | if (allow_crs && read_val) { |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 318 | /* For reading, CRS is not an error status. */ |
| 319 | *read_val = CFG_RD_CRS_VAL; |
| 320 | strcomp_status = NULL; |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 321 | ret = 0; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 322 | } else { |
| 323 | strcomp_status = "CRS"; |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 324 | ret = -EAGAIN; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 325 | } |
| 326 | break; |
| 327 | case PIO_COMPLETION_STATUS_CA: |
| 328 | strcomp_status = "CA"; |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 329 | ret = -ECANCELED; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 330 | break; |
| 331 | default: |
| 332 | strcomp_status = "Unknown"; |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 333 | ret = -EINVAL; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | |
| 337 | if (!strcomp_status) |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 338 | return ret; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 339 | |
| 340 | if (reg & PIO_NON_POSTED_REQ) |
| 341 | str_posted = "Non-posted"; |
| 342 | else |
| 343 | str_posted = "Posted"; |
| 344 | |
Marek Behún | 73d776a | 2021-09-07 17:27:08 +0200 | [diff] [blame] | 345 | dev_dbg(pcie->dev, "%s PIO Response Status: %s, %#x @ %#x\n", |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 346 | str_posted, strcomp_status, reg, |
| 347 | advk_readl(pcie, PIO_ADDR_LS)); |
| 348 | |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 349 | return ret; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /** |
| 353 | * pcie_advk_read_config() - Read from configuration space |
| 354 | * |
| 355 | * @bus: Pointer to the PCI bus |
| 356 | * @bdf: Identifies the PCIe device to access |
| 357 | * @offset: The offset into the device's configuration space |
| 358 | * @valuep: A pointer at which to store the read value |
| 359 | * @size: Indicates the size of access to perform |
| 360 | * |
| 361 | * Read a value of size @size from offset @offset within the configuration |
| 362 | * space of the device identified by the bus, device & function numbers in @bdf |
| 363 | * on the PCI bus @bus. |
| 364 | * |
| 365 | * Return: 0 on success |
| 366 | */ |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 367 | static int pcie_advk_read_config(const struct udevice *bus, pci_dev_t bdf, |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 368 | uint offset, ulong *valuep, |
| 369 | enum pci_size_t size) |
| 370 | { |
| 371 | struct pcie_advk *pcie = dev_get_priv(bus); |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 372 | int busno = PCI_BUS(bdf) - dev_seq(bus); |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 373 | int retry_count; |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 374 | bool allow_crs; |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 375 | ulong data; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 376 | uint reg; |
| 377 | int ret; |
| 378 | |
| 379 | dev_dbg(pcie->dev, "PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ", |
| 380 | PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
| 381 | |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 382 | if (!pcie_advk_addr_valid(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) { |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 383 | dev_dbg(pcie->dev, "- out of range\n"); |
| 384 | *valuep = pci_get_ff(size); |
| 385 | return 0; |
| 386 | } |
| 387 | |
Pali Rohár | 67cd721 | 2021-08-27 14:14:43 +0200 | [diff] [blame] | 388 | /* |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 389 | * The configuration space of the PCI Bridge on primary (local) bus is |
| 390 | * not accessible via PIO transfers like all other PCIe devices. PCI |
| 391 | * Bridge config registers are available directly in Aardvark memory |
| 392 | * space starting at offset zero. Moreover PCI Bridge registers in the |
| 393 | * range 0x10 - 0x34 are not available and register 0x38 (Expansion ROM |
| 394 | * Base Address) is at offset 0x30. |
| 395 | * We therefore read configuration space content of the primary PCI |
| 396 | * Bridge from our virtual cache. |
| 397 | */ |
| 398 | if (busno == pcie->first_busno) { |
| 399 | if (offset >= 0x10 && offset < 0x34) |
| 400 | data = pcie->cfgcache[(offset - 0x10) / 4]; |
| 401 | else if ((offset & ~3) == PCI_ROM_ADDRESS1) |
| 402 | data = advk_readl(pcie, PCIE_CORE_EXP_ROM_BAR_REG); |
| 403 | else |
| 404 | data = advk_readl(pcie, offset & ~3); |
| 405 | |
| 406 | if ((offset & ~3) == (PCI_HEADER_TYPE & ~3)) { |
| 407 | /* |
| 408 | * Change Header Type of PCI Bridge device to Type 1 |
| 409 | * (0x01, used by PCI Bridges) because hardwired value |
| 410 | * is Type 0 (0x00, used by Endpoint devices). |
| 411 | */ |
| 412 | data &= ~0x007f0000; |
| 413 | data |= PCI_HEADER_TYPE_BRIDGE << 16; |
| 414 | } |
| 415 | |
| 416 | *valuep = pci_conv_32_to_size(data, offset, size); |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | /* |
Pali Rohár | 67cd721 | 2021-08-27 14:14:43 +0200 | [diff] [blame] | 422 | * Returning fabricated CRS value (0xFFFF0001) by PCIe Root Complex to |
| 423 | * OS is allowed only for 4-byte PCI_VENDOR_ID config read request and |
| 424 | * only when CRSSVE bit in Root Port PCIe device is enabled. In all |
| 425 | * other error PCIe Root Complex must return all-ones. |
| 426 | * Aardvark HW does not have Root Port PCIe device and U-Boot does not |
| 427 | * implement emulation of this device. |
| 428 | * U-Boot currently does not support handling of CRS return value for |
| 429 | * PCI_VENDOR_ID config read request and also does not set CRSSVE bit. |
| 430 | * Therefore disable returning CRS response for now. |
| 431 | */ |
| 432 | allow_crs = false; |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 433 | |
Pali Rohár | fc0eddf | 2021-04-22 16:23:04 +0200 | [diff] [blame] | 434 | if (advk_readl(pcie, PIO_START)) { |
| 435 | dev_err(pcie->dev, |
| 436 | "Previous PIO read/write transfer is still running\n"); |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 437 | if (allow_crs) { |
| 438 | *valuep = CFG_RD_CRS_VAL; |
| 439 | return 0; |
| 440 | } |
| 441 | *valuep = pci_get_ff(size); |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 442 | return -EAGAIN; |
Pali Rohár | fc0eddf | 2021-04-22 16:23:04 +0200 | [diff] [blame] | 443 | } |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 444 | |
| 445 | /* Program the control register */ |
| 446 | reg = advk_readl(pcie, PIO_CTRL); |
| 447 | reg &= ~PIO_CTRL_TYPE_MASK; |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 448 | if (busno == pcie->sec_busno) |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 449 | reg |= PCIE_CONFIG_RD_TYPE0; |
| 450 | else |
| 451 | reg |= PCIE_CONFIG_RD_TYPE1; |
| 452 | advk_writel(pcie, reg, PIO_CTRL); |
| 453 | |
| 454 | /* Program the address registers */ |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 455 | reg = PCIE_BDF(busno, PCI_DEV(bdf), PCI_FUNC(bdf)) | PCIE_CONF_REG(offset); |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 456 | advk_writel(pcie, reg, PIO_ADDR_LS); |
| 457 | advk_writel(pcie, 0, PIO_ADDR_MS); |
| 458 | |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 459 | retry_count = 0; |
| 460 | |
| 461 | retry: |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 462 | /* Start the transfer */ |
Pali Rohár | fc0eddf | 2021-04-22 16:23:04 +0200 | [diff] [blame] | 463 | advk_writel(pcie, 1, PIO_ISR); |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 464 | advk_writel(pcie, 1, PIO_START); |
| 465 | |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 466 | ret = pcie_advk_wait_pio(pcie); |
| 467 | if (ret < 0) { |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 468 | if (allow_crs) { |
| 469 | *valuep = CFG_RD_CRS_VAL; |
| 470 | return 0; |
| 471 | } |
| 472 | *valuep = pci_get_ff(size); |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 473 | return ret; |
Pali Rohár | fc0eddf | 2021-04-22 16:23:04 +0200 | [diff] [blame] | 474 | } |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 475 | |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 476 | retry_count += ret; |
| 477 | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 478 | /* Check PIO status and get the read result */ |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 479 | ret = pcie_advk_check_pio_status(pcie, allow_crs, ®); |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 480 | if (ret == -EAGAIN && retry_count < PIO_MAX_RETRIES) |
| 481 | goto retry; |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 482 | if (ret) { |
| 483 | *valuep = pci_get_ff(size); |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 484 | return ret; |
Pali Rohár | b8344cf | 2021-08-09 09:53:13 +0200 | [diff] [blame] | 485 | } |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 486 | |
| 487 | dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n", |
| 488 | offset, size, reg); |
| 489 | *valuep = pci_conv_32_to_size(reg, offset, size); |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * pcie_calc_datastrobe() - Calculate data strobe |
| 496 | * |
| 497 | * @offset: The offset into the device's configuration space |
| 498 | * @size: Indicates the size of access to perform |
| 499 | * |
| 500 | * Calculate data strobe according to offset and size |
| 501 | * |
| 502 | */ |
| 503 | static uint pcie_calc_datastrobe(uint offset, enum pci_size_t size) |
| 504 | { |
| 505 | uint bytes, data_strobe; |
| 506 | |
| 507 | switch (size) { |
| 508 | case PCI_SIZE_8: |
| 509 | bytes = 1; |
| 510 | break; |
| 511 | case PCI_SIZE_16: |
| 512 | bytes = 2; |
| 513 | break; |
| 514 | default: |
| 515 | bytes = 4; |
| 516 | } |
| 517 | |
| 518 | data_strobe = GENMASK(bytes - 1, 0) << (offset & 0x3); |
| 519 | |
| 520 | return data_strobe; |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * pcie_advk_write_config() - Write to configuration space |
| 525 | * |
| 526 | * @bus: Pointer to the PCI bus |
| 527 | * @bdf: Identifies the PCIe device to access |
| 528 | * @offset: The offset into the device's configuration space |
| 529 | * @value: The value to write |
| 530 | * @size: Indicates the size of access to perform |
| 531 | * |
| 532 | * Write the value @value of size @size from offset @offset within the |
| 533 | * configuration space of the device identified by the bus, device & function |
| 534 | * numbers in @bdf on the PCI bus @bus. |
| 535 | * |
| 536 | * Return: 0 on success |
| 537 | */ |
| 538 | static int pcie_advk_write_config(struct udevice *bus, pci_dev_t bdf, |
| 539 | uint offset, ulong value, |
| 540 | enum pci_size_t size) |
| 541 | { |
| 542 | struct pcie_advk *pcie = dev_get_priv(bus); |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 543 | int busno = PCI_BUS(bdf) - dev_seq(bus); |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 544 | int retry_count; |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 545 | ulong data; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 546 | uint reg; |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 547 | int ret; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 548 | |
| 549 | dev_dbg(pcie->dev, "PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ", |
| 550 | PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
| 551 | dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", |
| 552 | offset, size, value); |
| 553 | |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 554 | if (!pcie_advk_addr_valid(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) { |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 555 | dev_dbg(pcie->dev, "- out of range\n"); |
| 556 | return 0; |
| 557 | } |
| 558 | |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 559 | /* |
| 560 | * As explained in pcie_advk_read_config(), for the configuration |
| 561 | * space of the primary PCI Bridge, we write the content into virtual |
| 562 | * cache. |
| 563 | */ |
| 564 | if (busno == pcie->first_busno) { |
| 565 | if (offset >= 0x10 && offset < 0x34) { |
| 566 | data = pcie->cfgcache[(offset - 0x10) / 4]; |
| 567 | data = pci_conv_size_to_32(data, value, offset, size); |
| 568 | pcie->cfgcache[(offset - 0x10) / 4] = data; |
| 569 | } else if ((offset & ~3) == PCI_ROM_ADDRESS1) { |
| 570 | data = advk_readl(pcie, PCIE_CORE_EXP_ROM_BAR_REG); |
| 571 | data = pci_conv_size_to_32(data, value, offset, size); |
| 572 | advk_writel(pcie, data, PCIE_CORE_EXP_ROM_BAR_REG); |
| 573 | } else { |
| 574 | data = advk_readl(pcie, offset & ~3); |
| 575 | data = pci_conv_size_to_32(data, value, offset, size); |
| 576 | advk_writel(pcie, data, offset & ~3); |
| 577 | } |
| 578 | |
| 579 | if (offset == PCI_PRIMARY_BUS) |
| 580 | pcie->first_busno = data & 0xff; |
| 581 | |
| 582 | if (offset == PCI_SECONDARY_BUS || |
| 583 | (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8)) |
| 584 | pcie->sec_busno = (data >> 8) & 0xff; |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
Pali Rohár | fc0eddf | 2021-04-22 16:23:04 +0200 | [diff] [blame] | 589 | if (advk_readl(pcie, PIO_START)) { |
| 590 | dev_err(pcie->dev, |
| 591 | "Previous PIO read/write transfer is still running\n"); |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 592 | return -EAGAIN; |
Pali Rohár | fc0eddf | 2021-04-22 16:23:04 +0200 | [diff] [blame] | 593 | } |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 594 | |
| 595 | /* Program the control register */ |
| 596 | reg = advk_readl(pcie, PIO_CTRL); |
| 597 | reg &= ~PIO_CTRL_TYPE_MASK; |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 598 | if (busno == pcie->sec_busno) |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 599 | reg |= PCIE_CONFIG_WR_TYPE0; |
| 600 | else |
| 601 | reg |= PCIE_CONFIG_WR_TYPE1; |
| 602 | advk_writel(pcie, reg, PIO_CTRL); |
| 603 | |
| 604 | /* Program the address registers */ |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 605 | reg = PCIE_BDF(busno, PCI_DEV(bdf), PCI_FUNC(bdf)) | PCIE_CONF_REG(offset); |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 606 | advk_writel(pcie, reg, PIO_ADDR_LS); |
| 607 | advk_writel(pcie, 0, PIO_ADDR_MS); |
| 608 | dev_dbg(pcie->dev, "\tPIO req. - addr = 0x%08x\n", reg); |
| 609 | |
| 610 | /* Program the data register */ |
| 611 | reg = pci_conv_size_to_32(0, value, offset, size); |
| 612 | advk_writel(pcie, reg, PIO_WR_DATA); |
| 613 | dev_dbg(pcie->dev, "\tPIO req. - val = 0x%08x\n", reg); |
| 614 | |
| 615 | /* Program the data strobe */ |
| 616 | reg = pcie_calc_datastrobe(offset, size); |
| 617 | advk_writel(pcie, reg, PIO_WR_DATA_STRB); |
| 618 | dev_dbg(pcie->dev, "\tPIO req. - strb = 0x%02x\n", reg); |
| 619 | |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 620 | retry_count = 0; |
| 621 | |
| 622 | retry: |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 623 | /* Start the transfer */ |
Pali Rohár | fc0eddf | 2021-04-22 16:23:04 +0200 | [diff] [blame] | 624 | advk_writel(pcie, 1, PIO_ISR); |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 625 | advk_writel(pcie, 1, PIO_START); |
| 626 | |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 627 | ret = pcie_advk_wait_pio(pcie); |
| 628 | if (ret < 0) |
| 629 | return ret; |
| 630 | |
| 631 | retry_count += ret; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 632 | |
| 633 | /* Check PIO status */ |
Pali Rohár | 907cd9a | 2021-08-27 14:14:44 +0200 | [diff] [blame] | 634 | ret = pcie_advk_check_pio_status(pcie, false, NULL); |
| 635 | if (ret == -EAGAIN && retry_count < PIO_MAX_RETRIES) |
| 636 | goto retry; |
| 637 | return ret; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
| 641 | * pcie_advk_link_up() - Check if PCIe link is up or not |
| 642 | * |
| 643 | * @pcie: The PCI device to access |
| 644 | * |
| 645 | * Return 1 (true) on link up. |
| 646 | * Return 0 (false) on link down. |
| 647 | */ |
| 648 | static int pcie_advk_link_up(struct pcie_advk *pcie) |
| 649 | { |
| 650 | u32 val, ltssm_state; |
| 651 | |
| 652 | val = advk_readl(pcie, CFG_REG); |
| 653 | ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK; |
Pali Rohár | 6d11d9e | 2021-09-26 00:54:41 +0200 | [diff] [blame] | 654 | return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /** |
| 658 | * pcie_advk_wait_for_link() - Wait for link training to be accomplished |
| 659 | * |
| 660 | * @pcie: The PCI device to access |
| 661 | * |
| 662 | * Wait up to 1 second for link training to be accomplished. |
| 663 | * |
| 664 | * Return 1 (true) if link training ends up with link up success. |
| 665 | * Return 0 (false) if link training ends up with link up failure. |
| 666 | */ |
| 667 | static int pcie_advk_wait_for_link(struct pcie_advk *pcie) |
| 668 | { |
| 669 | int retries; |
| 670 | |
| 671 | /* check if the link is up or not */ |
Pali Rohár | fc0eddf | 2021-04-22 16:23:04 +0200 | [diff] [blame] | 672 | for (retries = 0; retries < LINK_MAX_RETRIES; retries++) { |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 673 | if (pcie_advk_link_up(pcie)) { |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 674 | printf("PCIe: Link up\n"); |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | udelay(LINK_WAIT_TIMEOUT); |
| 679 | } |
| 680 | |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 681 | printf("PCIe: Link down\n"); |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 682 | |
| 683 | return -ETIMEDOUT; |
| 684 | } |
| 685 | |
Pali Rohár | e78c8e0 | 2021-05-26 17:59:40 +0200 | [diff] [blame] | 686 | /* |
| 687 | * Set PCIe address window register which could be used for memory |
| 688 | * mapping. |
| 689 | */ |
| 690 | static void pcie_advk_set_ob_win(struct pcie_advk *pcie, u8 win_num, |
| 691 | phys_addr_t match, phys_addr_t remap, |
| 692 | phys_addr_t mask, u32 actions) |
| 693 | { |
| 694 | advk_writel(pcie, OB_WIN_ENABLE | |
| 695 | lower_32_bits(match), OB_WIN_MATCH_LS(win_num)); |
| 696 | advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num)); |
| 697 | advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num)); |
| 698 | advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num)); |
| 699 | advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num)); |
| 700 | advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num)); |
| 701 | advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num)); |
| 702 | } |
| 703 | |
| 704 | static void pcie_advk_disable_ob_win(struct pcie_advk *pcie, u8 win_num) |
| 705 | { |
| 706 | advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num)); |
| 707 | advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num)); |
| 708 | advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num)); |
| 709 | advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num)); |
| 710 | advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num)); |
| 711 | advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num)); |
| 712 | advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num)); |
| 713 | } |
| 714 | |
| 715 | static void pcie_advk_set_ob_region(struct pcie_advk *pcie, int *wins, |
| 716 | struct pci_region *region, u32 actions) |
| 717 | { |
| 718 | phys_addr_t phys_start = region->phys_start; |
| 719 | pci_addr_t bus_start = region->bus_start; |
| 720 | pci_size_t size = region->size; |
| 721 | phys_addr_t win_mask; |
| 722 | u64 win_size; |
| 723 | |
| 724 | if (*wins == -1) |
| 725 | return; |
| 726 | |
| 727 | /* |
| 728 | * The n-th PCIe window is configured by tuple (match, remap, mask) |
Pali Rohár | 2fef1db | 2021-07-08 20:19:00 +0200 | [diff] [blame] | 729 | * and an access to address A uses this window if A matches the |
Pali Rohár | e78c8e0 | 2021-05-26 17:59:40 +0200 | [diff] [blame] | 730 | * match with given mask. |
| 731 | * So every PCIe window size must be a power of two and every start |
| 732 | * address must be aligned to window size. Minimal size is 64 KiB |
Pali Rohár | 22b1e08 | 2021-07-08 20:18:58 +0200 | [diff] [blame] | 733 | * because lower 16 bits of mask must be zero. Remapped address |
| 734 | * may have set only bits from the mask. |
Pali Rohár | e78c8e0 | 2021-05-26 17:59:40 +0200 | [diff] [blame] | 735 | */ |
| 736 | while (*wins < OB_WIN_COUNT && size > 0) { |
| 737 | /* Calculate the largest aligned window size */ |
| 738 | win_size = (1ULL << (fls64(size) - 1)) | |
| 739 | (phys_start ? (1ULL << __ffs64(phys_start)) : 0); |
| 740 | win_size = 1ULL << __ffs64(win_size); |
Pali Rohár | 22b1e08 | 2021-07-08 20:18:58 +0200 | [diff] [blame] | 741 | win_mask = ~(win_size - 1); |
| 742 | if (win_size < 0x10000 || (bus_start & ~win_mask)) |
Pali Rohár | e78c8e0 | 2021-05-26 17:59:40 +0200 | [diff] [blame] | 743 | break; |
| 744 | |
| 745 | dev_dbg(pcie->dev, |
| 746 | "Configuring PCIe window %d: [0x%llx-0x%llx] as 0x%x\n", |
| 747 | *wins, (u64)phys_start, (u64)phys_start + win_size, |
| 748 | actions); |
Pali Rohár | e78c8e0 | 2021-05-26 17:59:40 +0200 | [diff] [blame] | 749 | pcie_advk_set_ob_win(pcie, *wins, phys_start, bus_start, |
| 750 | win_mask, actions); |
| 751 | |
| 752 | phys_start += win_size; |
| 753 | bus_start += win_size; |
| 754 | size -= win_size; |
| 755 | (*wins)++; |
| 756 | } |
| 757 | |
| 758 | if (size > 0) { |
| 759 | *wins = -1; |
| 760 | dev_err(pcie->dev, |
| 761 | "Invalid PCIe region [0x%llx-0x%llx]\n", |
| 762 | (u64)region->phys_start, |
| 763 | (u64)region->phys_start + region->size); |
| 764 | } |
| 765 | } |
| 766 | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 767 | /** |
| 768 | * pcie_advk_setup_hw() - PCIe initailzation |
| 769 | * |
| 770 | * @pcie: The PCI device to access |
| 771 | * |
| 772 | * Return: 0 on success |
| 773 | */ |
| 774 | static int pcie_advk_setup_hw(struct pcie_advk *pcie) |
| 775 | { |
Pali Rohár | e78c8e0 | 2021-05-26 17:59:40 +0200 | [diff] [blame] | 776 | struct pci_region *io, *mem, *pref; |
| 777 | int i, wins; |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 778 | u32 reg; |
| 779 | |
| 780 | /* Set to Direct mode */ |
| 781 | reg = advk_readl(pcie, CTRL_CONFIG_REG); |
| 782 | reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT); |
| 783 | reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT); |
| 784 | advk_writel(pcie, reg, CTRL_CONFIG_REG); |
| 785 | |
| 786 | /* Set PCI global control register to RC mode */ |
| 787 | reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); |
| 788 | reg |= (IS_RC_MSK << IS_RC_SHIFT); |
| 789 | advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); |
| 790 | |
Pali Rohár | ba40b6c | 2021-03-03 14:37:59 +0100 | [diff] [blame] | 791 | /* |
| 792 | * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab. |
| 793 | * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor |
| 794 | * id in high 16 bits. Updating this register changes readback value of |
| 795 | * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround |
| 796 | * for erratum 4.1: "The value of device and vendor ID is incorrect". |
| 797 | */ |
| 798 | advk_writel(pcie, 0x11ab11ab, VENDOR_ID_REG); |
| 799 | |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 800 | /* |
| 801 | * Change Class Code of PCI Bridge device to PCI Bridge (0x600400), |
| 802 | * because default value is Mass Storage Controller (0x010400), causing |
| 803 | * U-Boot to fail to recognize it as P2P Bridge. |
| 804 | * |
| 805 | * Note that this Aardvark PCI Bridge does not have a compliant Type 1 |
| 806 | * Configuration Space and it even cannot be accessed via Aardvark's |
| 807 | * PCI config space access method. Something like config space is |
| 808 | * available in internal Aardvark registers starting at offset 0x0 |
| 809 | * and is reported as Type 0. In range 0x10 - 0x34 it has totally |
| 810 | * different registers. So our driver reports Header Type as Type 1 and |
| 811 | * for the above mentioned range redirects access to the virtual |
| 812 | * cfgcache[] buffer, which avoids changing internal Aardvark registers. |
| 813 | */ |
| 814 | reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG); |
| 815 | reg &= ~0xffffff00; |
| 816 | reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8; |
| 817 | advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG); |
| 818 | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 819 | /* Set Advanced Error Capabilities and Control PF0 register */ |
| 820 | reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX | |
| 821 | PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN | |
| 822 | PCIE_CORE_ERR_CAPCTL_ECRC_CHECK | |
| 823 | PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV; |
| 824 | advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG); |
| 825 | |
| 826 | /* Set PCIe Device Control and Status 1 PF0 register */ |
| 827 | reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE | |
Pali Rohár | 9057a2c | 2021-02-05 15:32:28 +0100 | [diff] [blame] | 828 | (PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE << |
| 829 | PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SIZE_SHIFT) | |
| 830 | (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE << |
| 831 | PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT) | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 832 | PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE; |
| 833 | advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG); |
| 834 | |
| 835 | /* Program PCIe Control 2 to disable strict ordering */ |
| 836 | reg = PCIE_CORE_CTRL2_RESERVED | |
| 837 | PCIE_CORE_CTRL2_TD_ENABLE; |
| 838 | advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); |
| 839 | |
| 840 | /* Set GEN2 */ |
| 841 | reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); |
| 842 | reg &= ~PCIE_GEN_SEL_MSK; |
| 843 | reg |= SPEED_GEN_2; |
| 844 | advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); |
| 845 | |
| 846 | /* Set lane X1 */ |
| 847 | reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); |
| 848 | reg &= ~LANE_CNT_MSK; |
| 849 | reg |= LANE_COUNT_1; |
| 850 | advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); |
| 851 | |
| 852 | /* Enable link training */ |
| 853 | reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); |
| 854 | reg |= LINK_TRAINING_EN; |
| 855 | advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); |
| 856 | |
| 857 | /* |
| 858 | * Enable AXI address window location generation: |
| 859 | * When it is enabled, the default outbound window |
| 860 | * configurations (Default User Field: 0xD0074CFC) |
| 861 | * are used to transparent address translation for |
| 862 | * the outbound transactions. Thus, PCIe address |
Pali Rohár | e78c8e0 | 2021-05-26 17:59:40 +0200 | [diff] [blame] | 863 | * windows are not required for transparent memory |
| 864 | * access when default outbound window configuration |
| 865 | * is set for memory access. |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 866 | */ |
| 867 | reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG); |
| 868 | reg |= PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE; |
| 869 | advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); |
| 870 | |
| 871 | /* |
| 872 | * Bypass the address window mapping for PIO: |
| 873 | * Since PIO access already contains all required |
| 874 | * info over AXI interface by PIO registers, the |
| 875 | * address window is not required. |
| 876 | */ |
| 877 | reg = advk_readl(pcie, PIO_CTRL); |
| 878 | reg |= PIO_CTRL_ADDR_WIN_DISABLE; |
| 879 | advk_writel(pcie, reg, PIO_CTRL); |
| 880 | |
Pali Rohár | e78c8e0 | 2021-05-26 17:59:40 +0200 | [diff] [blame] | 881 | /* |
| 882 | * Set memory access in Default User Field so it |
| 883 | * is not required to configure PCIe address for |
| 884 | * transparent memory access. |
| 885 | */ |
| 886 | advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS); |
| 887 | |
| 888 | /* |
| 889 | * Configure PCIe address windows for non-memory or |
| 890 | * non-transparent access as by default PCIe uses |
| 891 | * transparent memory access. |
| 892 | */ |
| 893 | wins = 0; |
| 894 | pci_get_regions(pcie->dev, &io, &mem, &pref); |
| 895 | if (io) |
| 896 | pcie_advk_set_ob_region(pcie, &wins, io, OB_WIN_TYPE_IO); |
| 897 | if (mem && mem->phys_start != mem->bus_start) |
| 898 | pcie_advk_set_ob_region(pcie, &wins, mem, OB_WIN_TYPE_MEM); |
| 899 | if (pref && pref->phys_start != pref->bus_start) |
| 900 | pcie_advk_set_ob_region(pcie, &wins, pref, OB_WIN_TYPE_MEM); |
| 901 | |
| 902 | /* Disable remaining PCIe outbound windows */ |
| 903 | for (i = ((wins >= 0) ? wins : 0); i < OB_WIN_COUNT; i++) |
| 904 | pcie_advk_disable_ob_win(pcie, i); |
| 905 | |
| 906 | if (wins == -1) |
| 907 | return -EINVAL; |
| 908 | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 909 | /* Wait for PCIe link up */ |
| 910 | if (pcie_advk_wait_for_link(pcie)) |
| 911 | return -ENXIO; |
| 912 | |
| 913 | reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); |
| 914 | reg |= PCIE_CORE_CMD_MEM_ACCESS_EN | |
| 915 | PCIE_CORE_CMD_IO_ACCESS_EN | |
| 916 | PCIE_CORE_CMD_MEM_IO_REQ_EN; |
| 917 | advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG); |
| 918 | |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * pcie_advk_probe() - Probe the PCIe bus for active link |
| 924 | * |
| 925 | * @dev: A pointer to the device being operated on |
| 926 | * |
| 927 | * Probe for an active link on the PCIe bus and configure the controller |
| 928 | * to enable this port. |
| 929 | * |
| 930 | * Return: 0 on success, else -ENODEV |
| 931 | */ |
| 932 | static int pcie_advk_probe(struct udevice *dev) |
| 933 | { |
| 934 | struct pcie_advk *pcie = dev_get_priv(dev); |
| 935 | |
Pali Rohár | 6a58b97 | 2020-08-19 15:57:07 +0200 | [diff] [blame] | 936 | gpio_request_by_name(dev, "reset-gpios", 0, &pcie->reset_gpio, |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 937 | GPIOD_IS_OUT); |
| 938 | /* |
| 939 | * Issue reset to add-in card through the dedicated GPIO. |
| 940 | * Some boards are connecting the card reset pin to common system |
| 941 | * reset wire and others are using separate GPIO port. |
| 942 | * In the last case we have to release a reset of the addon card |
| 943 | * using this GPIO. |
| 944 | * |
| 945 | * FIX-ME: |
| 946 | * The PCIe RESET signal is not supposed to be released along |
| 947 | * with the SOC RESET signal. It should be lowered as early as |
| 948 | * possible before PCIe PHY initialization. Moreover, the PCIe |
| 949 | * clock should be gated as well. |
| 950 | */ |
Pali Rohár | 6a58b97 | 2020-08-19 15:57:07 +0200 | [diff] [blame] | 951 | if (dm_gpio_is_valid(&pcie->reset_gpio)) { |
Pali Rohár | e3271bb | 2021-01-18 12:09:33 +0100 | [diff] [blame] | 952 | dev_dbg(dev, "Toggle PCIE Reset GPIO ...\n"); |
Pali Rohár | 6a58b97 | 2020-08-19 15:57:07 +0200 | [diff] [blame] | 953 | dm_gpio_set_value(&pcie->reset_gpio, 1); |
Pali Rohár | 0130a61 | 2020-08-19 15:57:06 +0200 | [diff] [blame] | 954 | mdelay(200); |
Pali Rohár | 6a58b97 | 2020-08-19 15:57:07 +0200 | [diff] [blame] | 955 | dm_gpio_set_value(&pcie->reset_gpio, 0); |
Pali Rohár | 5c6edca | 2020-08-25 10:45:04 +0200 | [diff] [blame] | 956 | } else { |
Pali Rohár | e3271bb | 2021-01-18 12:09:33 +0100 | [diff] [blame] | 957 | dev_warn(dev, "PCIE Reset on GPIO support is missing\n"); |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 958 | } |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 959 | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 960 | pcie->dev = pci_get_controller(dev); |
| 961 | |
Pali Rohár | 525886e | 2021-09-26 00:54:42 +0200 | [diff] [blame^] | 962 | /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */ |
| 963 | pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] = |
| 964 | PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8); |
| 965 | pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] = |
| 966 | PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16); |
| 967 | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 968 | return pcie_advk_setup_hw(pcie); |
| 969 | } |
| 970 | |
Pali Rohár | 6a58b97 | 2020-08-19 15:57:07 +0200 | [diff] [blame] | 971 | static int pcie_advk_remove(struct udevice *dev) |
| 972 | { |
Pali Rohár | 6a58b97 | 2020-08-19 15:57:07 +0200 | [diff] [blame] | 973 | struct pcie_advk *pcie = dev_get_priv(dev); |
Pali Rohár | ff87699 | 2020-09-22 13:21:38 +0200 | [diff] [blame] | 974 | u32 reg; |
Pali Rohár | e78c8e0 | 2021-05-26 17:59:40 +0200 | [diff] [blame] | 975 | int i; |
| 976 | |
| 977 | for (i = 0; i < OB_WIN_COUNT; i++) |
| 978 | pcie_advk_disable_ob_win(pcie, i); |
Pali Rohár | 6a58b97 | 2020-08-19 15:57:07 +0200 | [diff] [blame] | 979 | |
Pali Rohár | ead9634 | 2021-05-26 17:59:35 +0200 | [diff] [blame] | 980 | reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); |
| 981 | reg &= ~(PCIE_CORE_CMD_MEM_ACCESS_EN | |
| 982 | PCIE_CORE_CMD_IO_ACCESS_EN | |
| 983 | PCIE_CORE_CMD_MEM_IO_REQ_EN); |
| 984 | advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG); |
| 985 | |
Pali Rohár | ff87699 | 2020-09-22 13:21:38 +0200 | [diff] [blame] | 986 | reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); |
| 987 | reg &= ~LINK_TRAINING_EN; |
| 988 | advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); |
| 989 | |
Pali Rohár | 6a58b97 | 2020-08-19 15:57:07 +0200 | [diff] [blame] | 990 | return 0; |
| 991 | } |
| 992 | |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 993 | /** |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 994 | * pcie_advk_of_to_plat() - Translate from DT to device state |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 995 | * |
| 996 | * @dev: A pointer to the device being operated on |
| 997 | * |
| 998 | * Translate relevant data from the device tree pertaining to device @dev into |
| 999 | * state that the driver will later make use of. This state is stored in the |
| 1000 | * device's private data structure. |
| 1001 | * |
| 1002 | * Return: 0 on success, else -EINVAL |
| 1003 | */ |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1004 | static int pcie_advk_of_to_plat(struct udevice *dev) |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 1005 | { |
| 1006 | struct pcie_advk *pcie = dev_get_priv(dev); |
| 1007 | |
| 1008 | /* Get the register base address */ |
| 1009 | pcie->base = (void *)dev_read_addr_index(dev, 0); |
| 1010 | if ((fdt_addr_t)pcie->base == FDT_ADDR_T_NONE) |
| 1011 | return -EINVAL; |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | static const struct dm_pci_ops pcie_advk_ops = { |
| 1017 | .read_config = pcie_advk_read_config, |
| 1018 | .write_config = pcie_advk_write_config, |
| 1019 | }; |
| 1020 | |
| 1021 | static const struct udevice_id pcie_advk_ids[] = { |
Pali Rohár | 678cf9d | 2021-05-26 17:59:36 +0200 | [diff] [blame] | 1022 | { .compatible = "marvell,armada-3700-pcie" }, |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 1023 | { } |
| 1024 | }; |
| 1025 | |
| 1026 | U_BOOT_DRIVER(pcie_advk) = { |
| 1027 | .name = "pcie_advk", |
| 1028 | .id = UCLASS_PCI, |
| 1029 | .of_match = pcie_advk_ids, |
| 1030 | .ops = &pcie_advk_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1031 | .of_to_plat = pcie_advk_of_to_plat, |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 1032 | .probe = pcie_advk_probe, |
Pali Rohár | 6a58b97 | 2020-08-19 15:57:07 +0200 | [diff] [blame] | 1033 | .remove = pcie_advk_remove, |
| 1034 | .flags = DM_FLAG_OS_PREPARE, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1035 | .priv_auto = sizeof(struct pcie_advk), |
Wilson Ding | a6bdc86 | 2018-03-26 15:57:29 +0800 | [diff] [blame] | 1036 | }; |