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