Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Hauke Mehrtens <hauke@hauke-m.de> |
Florian Fainelli | be908d2 | 2015-10-16 12:04:04 -0700 | [diff] [blame] | 3 | * Copyright (C) 2015 Broadcom Corporation |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation version 2. |
| 8 | * |
| 9 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 10 | * kind, whether express or implied; without even the implied warranty |
| 11 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/pci.h> |
| 17 | #include <linux/msi.h> |
| 18 | #include <linux/clk.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/mbus.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/of_address.h> |
| 26 | #include <linux/of_pci.h> |
| 27 | #include <linux/of_irq.h> |
| 28 | #include <linux/of_platform.h> |
| 29 | #include <linux/phy/phy.h> |
| 30 | |
| 31 | #include "pcie-iproc.h" |
| 32 | |
Ray Jui | 199ff14 | 2015-09-15 17:39:18 -0700 | [diff] [blame] | 33 | #define EP_PERST_SOURCE_SELECT_SHIFT 2 |
| 34 | #define EP_PERST_SOURCE_SELECT BIT(EP_PERST_SOURCE_SELECT_SHIFT) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 35 | #define EP_MODE_SURVIVE_PERST_SHIFT 1 |
| 36 | #define EP_MODE_SURVIVE_PERST BIT(EP_MODE_SURVIVE_PERST_SHIFT) |
| 37 | #define RC_PCIE_RST_OUTPUT_SHIFT 0 |
| 38 | #define RC_PCIE_RST_OUTPUT BIT(RC_PCIE_RST_OUTPUT_SHIFT) |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 39 | #define PAXC_RESET_MASK 0x7f |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 40 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 41 | #define CFG_IND_ADDR_MASK 0x00001ffc |
| 42 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 43 | #define CFG_ADDR_BUS_NUM_SHIFT 20 |
| 44 | #define CFG_ADDR_BUS_NUM_MASK 0x0ff00000 |
| 45 | #define CFG_ADDR_DEV_NUM_SHIFT 15 |
| 46 | #define CFG_ADDR_DEV_NUM_MASK 0x000f8000 |
| 47 | #define CFG_ADDR_FUNC_NUM_SHIFT 12 |
| 48 | #define CFG_ADDR_FUNC_NUM_MASK 0x00007000 |
| 49 | #define CFG_ADDR_REG_NUM_SHIFT 2 |
| 50 | #define CFG_ADDR_REG_NUM_MASK 0x00000ffc |
| 51 | #define CFG_ADDR_CFG_TYPE_SHIFT 0 |
| 52 | #define CFG_ADDR_CFG_TYPE_MASK 0x00000003 |
| 53 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 54 | #define SYS_RC_INTX_MASK 0xf |
| 55 | |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 56 | #define PCIE_PHYLINKUP_SHIFT 3 |
| 57 | #define PCIE_PHYLINKUP BIT(PCIE_PHYLINKUP_SHIFT) |
| 58 | #define PCIE_DL_ACTIVE_SHIFT 2 |
| 59 | #define PCIE_DL_ACTIVE BIT(PCIE_DL_ACTIVE_SHIFT) |
| 60 | |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 61 | #define OARR_VALID_SHIFT 0 |
| 62 | #define OARR_VALID BIT(OARR_VALID_SHIFT) |
| 63 | #define OARR_SIZE_CFG_SHIFT 1 |
| 64 | #define OARR_SIZE_CFG BIT(OARR_SIZE_CFG_SHIFT) |
| 65 | |
Bjorn Helgaas | e3a1698 | 2016-10-06 13:36:07 -0500 | [diff] [blame] | 66 | #define PCI_EXP_CAP 0xac |
| 67 | |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 68 | #define MAX_NUM_OB_WINDOWS 2 |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 69 | |
| 70 | #define IPROC_PCIE_REG_INVALID 0xffff |
| 71 | |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 72 | /* |
| 73 | * iProc PCIe host registers |
| 74 | */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 75 | enum iproc_pcie_reg { |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 76 | /* clock/reset signal control */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 77 | IPROC_PCIE_CLK_CTRL = 0, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 78 | |
| 79 | /* allow access to root complex configuration space */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 80 | IPROC_PCIE_CFG_IND_ADDR, |
| 81 | IPROC_PCIE_CFG_IND_DATA, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 82 | |
| 83 | /* allow access to device configuration space */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 84 | IPROC_PCIE_CFG_ADDR, |
| 85 | IPROC_PCIE_CFG_DATA, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 86 | |
| 87 | /* enable INTx */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 88 | IPROC_PCIE_INTX_EN, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 89 | |
| 90 | /* outbound address mapping */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 91 | IPROC_PCIE_OARR_LO, |
| 92 | IPROC_PCIE_OARR_HI, |
| 93 | IPROC_PCIE_OMAP_LO, |
| 94 | IPROC_PCIE_OMAP_HI, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 95 | |
| 96 | /* link status */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 97 | IPROC_PCIE_LINK_STATUS, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 98 | |
| 99 | /* total number of core registers */ |
| 100 | IPROC_PCIE_MAX_NUM_REG, |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | /* iProc PCIe PAXB registers */ |
| 104 | static const u16 iproc_pcie_reg_paxb[] = { |
| 105 | [IPROC_PCIE_CLK_CTRL] = 0x000, |
| 106 | [IPROC_PCIE_CFG_IND_ADDR] = 0x120, |
| 107 | [IPROC_PCIE_CFG_IND_DATA] = 0x124, |
| 108 | [IPROC_PCIE_CFG_ADDR] = 0x1f8, |
| 109 | [IPROC_PCIE_CFG_DATA] = 0x1fc, |
| 110 | [IPROC_PCIE_INTX_EN] = 0x330, |
| 111 | [IPROC_PCIE_OARR_LO] = 0xd20, |
| 112 | [IPROC_PCIE_OARR_HI] = 0xd24, |
| 113 | [IPROC_PCIE_OMAP_LO] = 0xd40, |
| 114 | [IPROC_PCIE_OMAP_HI] = 0xd44, |
| 115 | [IPROC_PCIE_LINK_STATUS] = 0xf0c, |
| 116 | }; |
| 117 | |
| 118 | /* iProc PCIe PAXC v1 registers */ |
| 119 | static const u16 iproc_pcie_reg_paxc[] = { |
| 120 | [IPROC_PCIE_CLK_CTRL] = 0x000, |
| 121 | [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0, |
| 122 | [IPROC_PCIE_CFG_IND_DATA] = 0x1f4, |
| 123 | [IPROC_PCIE_CFG_ADDR] = 0x1f8, |
| 124 | [IPROC_PCIE_CFG_DATA] = 0x1fc, |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 125 | }; |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 126 | |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 127 | static inline struct iproc_pcie *iproc_data(struct pci_bus *bus) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 128 | { |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 129 | struct iproc_pcie *pcie; |
| 130 | #ifdef CONFIG_ARM |
| 131 | struct pci_sys_data *sys = bus->sysdata; |
| 132 | |
| 133 | pcie = sys->private_data; |
| 134 | #else |
| 135 | pcie = bus->sysdata; |
| 136 | #endif |
| 137 | return pcie; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 140 | static inline bool iproc_pcie_reg_is_invalid(u16 reg_offset) |
| 141 | { |
| 142 | return !!(reg_offset == IPROC_PCIE_REG_INVALID); |
| 143 | } |
| 144 | |
| 145 | static inline u16 iproc_pcie_reg_offset(struct iproc_pcie *pcie, |
| 146 | enum iproc_pcie_reg reg) |
| 147 | { |
| 148 | return pcie->reg_offsets[reg]; |
| 149 | } |
| 150 | |
| 151 | static inline u32 iproc_pcie_read_reg(struct iproc_pcie *pcie, |
| 152 | enum iproc_pcie_reg reg) |
| 153 | { |
| 154 | u16 offset = iproc_pcie_reg_offset(pcie, reg); |
| 155 | |
| 156 | if (iproc_pcie_reg_is_invalid(offset)) |
| 157 | return 0; |
| 158 | |
| 159 | return readl(pcie->base + offset); |
| 160 | } |
| 161 | |
| 162 | static inline void iproc_pcie_write_reg(struct iproc_pcie *pcie, |
| 163 | enum iproc_pcie_reg reg, u32 val) |
| 164 | { |
| 165 | u16 offset = iproc_pcie_reg_offset(pcie, reg); |
| 166 | |
| 167 | if (iproc_pcie_reg_is_invalid(offset)) |
| 168 | return; |
| 169 | |
| 170 | writel(val, pcie->base + offset); |
| 171 | } |
| 172 | |
| 173 | static inline void iproc_pcie_ob_write(struct iproc_pcie *pcie, |
| 174 | enum iproc_pcie_reg reg, |
| 175 | unsigned window, u32 val) |
| 176 | { |
| 177 | u16 offset = iproc_pcie_reg_offset(pcie, reg); |
| 178 | |
| 179 | if (iproc_pcie_reg_is_invalid(offset)) |
| 180 | return; |
| 181 | |
| 182 | writel(val, pcie->base + offset + (window * 8)); |
| 183 | } |
| 184 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 185 | /** |
| 186 | * Note access to the configuration registers are protected at the higher layer |
| 187 | * by 'pci_lock' in drivers/pci/access.c |
| 188 | */ |
| 189 | static void __iomem *iproc_pcie_map_cfg_bus(struct pci_bus *bus, |
| 190 | unsigned int devfn, |
| 191 | int where) |
| 192 | { |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 193 | struct iproc_pcie *pcie = iproc_data(bus); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 194 | unsigned slot = PCI_SLOT(devfn); |
| 195 | unsigned fn = PCI_FUNC(devfn); |
| 196 | unsigned busno = bus->number; |
| 197 | u32 val; |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 198 | u16 offset; |
| 199 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 200 | /* root complex access */ |
| 201 | if (busno == 0) { |
Ray Jui | 4656038 | 2016-01-27 16:52:24 -0600 | [diff] [blame] | 202 | if (slot > 0 || fn > 0) |
| 203 | return NULL; |
| 204 | |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 205 | iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_IND_ADDR, |
| 206 | where & CFG_IND_ADDR_MASK); |
| 207 | offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_IND_DATA); |
| 208 | if (iproc_pcie_reg_is_invalid(offset)) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 209 | return NULL; |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 210 | else |
| 211 | return (pcie->base + offset); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Ray Jui | 4656038 | 2016-01-27 16:52:24 -0600 | [diff] [blame] | 214 | /* |
| 215 | * PAXC is connected to an internally emulated EP within the SoC. It |
| 216 | * allows only one device. |
| 217 | */ |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 218 | if (pcie->ep_is_internal) |
Ray Jui | 4656038 | 2016-01-27 16:52:24 -0600 | [diff] [blame] | 219 | if (slot > 0) |
| 220 | return NULL; |
| 221 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 222 | /* EP device access */ |
| 223 | val = (busno << CFG_ADDR_BUS_NUM_SHIFT) | |
| 224 | (slot << CFG_ADDR_DEV_NUM_SHIFT) | |
| 225 | (fn << CFG_ADDR_FUNC_NUM_SHIFT) | |
| 226 | (where & CFG_ADDR_REG_NUM_MASK) | |
| 227 | (1 & CFG_ADDR_CFG_TYPE_MASK); |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 228 | iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_ADDR, val); |
| 229 | offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_DATA); |
| 230 | if (iproc_pcie_reg_is_invalid(offset)) |
| 231 | return NULL; |
| 232 | else |
| 233 | return (pcie->base + offset); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static struct pci_ops iproc_pcie_ops = { |
| 237 | .map_bus = iproc_pcie_map_cfg_bus, |
| 238 | .read = pci_generic_config_read32, |
| 239 | .write = pci_generic_config_write32, |
| 240 | }; |
| 241 | |
| 242 | static void iproc_pcie_reset(struct iproc_pcie *pcie) |
| 243 | { |
| 244 | u32 val; |
| 245 | |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 246 | if (pcie->ep_is_internal) { |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 247 | val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL); |
| 248 | val &= ~PAXC_RESET_MASK; |
| 249 | iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val); |
| 250 | udelay(100); |
| 251 | val |= PAXC_RESET_MASK; |
| 252 | iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val); |
| 253 | udelay(100); |
| 254 | return; |
| 255 | } |
| 256 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 257 | /* |
Ray Jui | 199ff14 | 2015-09-15 17:39:18 -0700 | [diff] [blame] | 258 | * Select perst_b signal as reset source. Put the device into reset, |
| 259 | * and then bring it out of reset |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 260 | */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 261 | val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL); |
Ray Jui | 199ff14 | 2015-09-15 17:39:18 -0700 | [diff] [blame] | 262 | val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST & |
| 263 | ~RC_PCIE_RST_OUTPUT; |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 264 | iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 265 | udelay(250); |
Ray Jui | 199ff14 | 2015-09-15 17:39:18 -0700 | [diff] [blame] | 266 | |
| 267 | val |= RC_PCIE_RST_OUTPUT; |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 268 | iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val); |
Ray Jui | 199ff14 | 2015-09-15 17:39:18 -0700 | [diff] [blame] | 269 | msleep(100); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static int iproc_pcie_check_link(struct iproc_pcie *pcie, struct pci_bus *bus) |
| 273 | { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 274 | struct device *dev = pcie->dev; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 275 | u8 hdr_type; |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 276 | u32 link_ctrl, class, val; |
Bjorn Helgaas | e3a1698 | 2016-10-06 13:36:07 -0500 | [diff] [blame] | 277 | u16 pos = PCI_EXP_CAP, link_status; |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 278 | bool link_is_active = false; |
| 279 | |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 280 | /* |
| 281 | * PAXC connects to emulated endpoint devices directly and does not |
| 282 | * have a Serdes. Therefore skip the link detection logic here. |
| 283 | */ |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 284 | if (pcie->ep_is_internal) |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 285 | return 0; |
| 286 | |
| 287 | val = iproc_pcie_read_reg(pcie, IPROC_PCIE_LINK_STATUS); |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 288 | if (!(val & PCIE_PHYLINKUP) || !(val & PCIE_DL_ACTIVE)) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 289 | dev_err(dev, "PHY or data link is INACTIVE!\n"); |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 290 | return -ENODEV; |
| 291 | } |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 292 | |
| 293 | /* make sure we are not in EP mode */ |
| 294 | pci_bus_read_config_byte(bus, 0, PCI_HEADER_TYPE, &hdr_type); |
| 295 | if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 296 | dev_err(dev, "in EP mode, hdr=%#02x\n", hdr_type); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 297 | return -EFAULT; |
| 298 | } |
| 299 | |
| 300 | /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */ |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 301 | #define PCI_BRIDGE_CTRL_REG_OFFSET 0x43c |
| 302 | #define PCI_CLASS_BRIDGE_MASK 0xffff00 |
| 303 | #define PCI_CLASS_BRIDGE_SHIFT 8 |
| 304 | pci_bus_read_config_dword(bus, 0, PCI_BRIDGE_CTRL_REG_OFFSET, &class); |
| 305 | class &= ~PCI_CLASS_BRIDGE_MASK; |
| 306 | class |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT); |
| 307 | pci_bus_write_config_dword(bus, 0, PCI_BRIDGE_CTRL_REG_OFFSET, class); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 308 | |
| 309 | /* check link status to see if link is active */ |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 310 | pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA, &link_status); |
| 311 | if (link_status & PCI_EXP_LNKSTA_NLW) |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 312 | link_is_active = true; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 313 | |
| 314 | if (!link_is_active) { |
| 315 | /* try GEN 1 link speed */ |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 316 | #define PCI_TARGET_LINK_SPEED_MASK 0xf |
| 317 | #define PCI_TARGET_LINK_SPEED_GEN2 0x2 |
| 318 | #define PCI_TARGET_LINK_SPEED_GEN1 0x1 |
| 319 | pci_bus_read_config_dword(bus, 0, |
Bjorn Helgaas | e3a1698 | 2016-10-06 13:36:07 -0500 | [diff] [blame] | 320 | pos + PCI_EXP_LNKCTL2, |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 321 | &link_ctrl); |
| 322 | if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) == |
| 323 | PCI_TARGET_LINK_SPEED_GEN2) { |
| 324 | link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK; |
| 325 | link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1; |
| 326 | pci_bus_write_config_dword(bus, 0, |
Bjorn Helgaas | e3a1698 | 2016-10-06 13:36:07 -0500 | [diff] [blame] | 327 | pos + PCI_EXP_LNKCTL2, |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 328 | link_ctrl); |
| 329 | msleep(100); |
| 330 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 331 | pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA, |
| 332 | &link_status); |
| 333 | if (link_status & PCI_EXP_LNKSTA_NLW) |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 334 | link_is_active = true; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 338 | dev_info(dev, "link: %s\n", link_is_active ? "UP" : "DOWN"); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 339 | |
| 340 | return link_is_active ? 0 : -ENODEV; |
| 341 | } |
| 342 | |
| 343 | static void iproc_pcie_enable(struct iproc_pcie *pcie) |
| 344 | { |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 345 | iproc_pcie_write_reg(pcie, IPROC_PCIE_INTX_EN, SYS_RC_INTX_MASK); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 348 | /** |
| 349 | * Some iProc SoCs require the SW to configure the outbound address mapping |
| 350 | * |
| 351 | * Outbound address translation: |
| 352 | * |
| 353 | * iproc_pcie_address = axi_address - axi_offset |
| 354 | * OARR = iproc_pcie_address |
| 355 | * OMAP = pci_addr |
| 356 | * |
| 357 | * axi_addr -> iproc_pcie_address -> OARR -> OMAP -> pci_address |
| 358 | */ |
| 359 | static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr, |
| 360 | u64 pci_addr, resource_size_t size) |
| 361 | { |
| 362 | struct iproc_pcie_ob *ob = &pcie->ob; |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 363 | struct device *dev = pcie->dev; |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 364 | unsigned i; |
| 365 | u64 max_size = (u64)ob->window_size * MAX_NUM_OB_WINDOWS; |
| 366 | u64 remainder; |
| 367 | |
| 368 | if (size > max_size) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 369 | dev_err(dev, |
Dmitry V. Krivenok | 57303e9 | 2015-11-30 23:45:49 +0300 | [diff] [blame] | 370 | "res size %pap exceeds max supported size 0x%llx\n", |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 371 | &size, max_size); |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | |
| 375 | div64_u64_rem(size, ob->window_size, &remainder); |
| 376 | if (remainder) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 377 | dev_err(dev, |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 378 | "res size %pap needs to be multiple of window size %pap\n", |
| 379 | &size, &ob->window_size); |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
| 383 | if (axi_addr < ob->axi_offset) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 384 | dev_err(dev, "axi address %pap less than offset %pap\n", |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 385 | &axi_addr, &ob->axi_offset); |
| 386 | return -EINVAL; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Translate the AXI address to the internal address used by the iProc |
| 391 | * PCIe core before programming the OARR |
| 392 | */ |
| 393 | axi_addr -= ob->axi_offset; |
| 394 | |
| 395 | for (i = 0; i < MAX_NUM_OB_WINDOWS; i++) { |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 396 | iproc_pcie_ob_write(pcie, IPROC_PCIE_OARR_LO, i, |
| 397 | lower_32_bits(axi_addr) | OARR_VALID | |
| 398 | (ob->set_oarr_size ? 1 : 0)); |
| 399 | iproc_pcie_ob_write(pcie, IPROC_PCIE_OARR_HI, i, |
| 400 | upper_32_bits(axi_addr)); |
| 401 | iproc_pcie_ob_write(pcie, IPROC_PCIE_OMAP_LO, i, |
| 402 | lower_32_bits(pci_addr)); |
| 403 | iproc_pcie_ob_write(pcie, IPROC_PCIE_OMAP_HI, i, |
| 404 | upper_32_bits(pci_addr)); |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 405 | |
| 406 | size -= ob->window_size; |
| 407 | if (size == 0) |
| 408 | break; |
| 409 | |
| 410 | axi_addr += ob->window_size; |
| 411 | pci_addr += ob->window_size; |
| 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static int iproc_pcie_map_ranges(struct iproc_pcie *pcie, |
| 418 | struct list_head *resources) |
| 419 | { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 420 | struct device *dev = pcie->dev; |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 421 | struct resource_entry *window; |
| 422 | int ret; |
| 423 | |
| 424 | resource_list_for_each_entry(window, resources) { |
| 425 | struct resource *res = window->res; |
| 426 | u64 res_type = resource_type(res); |
| 427 | |
| 428 | switch (res_type) { |
| 429 | case IORESOURCE_IO: |
| 430 | case IORESOURCE_BUS: |
| 431 | break; |
| 432 | case IORESOURCE_MEM: |
| 433 | ret = iproc_pcie_setup_ob(pcie, res->start, |
| 434 | res->start - window->offset, |
| 435 | resource_size(res)); |
| 436 | if (ret) |
| 437 | return ret; |
| 438 | break; |
| 439 | default: |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 440 | dev_err(dev, "invalid resource %pR\n", res); |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 441 | return -EINVAL; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Ray Jui | 3bc2b23 | 2016-01-06 18:04:35 -0600 | [diff] [blame] | 448 | static int iproc_pcie_msi_enable(struct iproc_pcie *pcie) |
| 449 | { |
| 450 | struct device_node *msi_node; |
| 451 | |
| 452 | msi_node = of_parse_phandle(pcie->dev->of_node, "msi-parent", 0); |
| 453 | if (!msi_node) |
| 454 | return -ENODEV; |
| 455 | |
| 456 | /* |
| 457 | * If another MSI controller is being used, the call below should fail |
| 458 | * but that is okay |
| 459 | */ |
| 460 | return iproc_msi_init(pcie, msi_node); |
| 461 | } |
| 462 | |
| 463 | static void iproc_pcie_msi_disable(struct iproc_pcie *pcie) |
| 464 | { |
| 465 | iproc_msi_exit(pcie); |
| 466 | } |
| 467 | |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 468 | static int iproc_pcie_rev_init(struct iproc_pcie *pcie) |
| 469 | { |
| 470 | struct device *dev = pcie->dev; |
| 471 | unsigned int reg_idx; |
| 472 | const u16 *regs; |
| 473 | |
| 474 | switch (pcie->type) { |
| 475 | case IPROC_PCIE_PAXB: |
| 476 | regs = iproc_pcie_reg_paxb; |
| 477 | break; |
| 478 | case IPROC_PCIE_PAXC: |
| 479 | regs = iproc_pcie_reg_paxc; |
| 480 | pcie->ep_is_internal = true; |
| 481 | break; |
| 482 | default: |
| 483 | dev_err(dev, "incompatible iProc PCIe interface\n"); |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | |
| 487 | pcie->reg_offsets = devm_kcalloc(dev, IPROC_PCIE_MAX_NUM_REG, |
| 488 | sizeof(*pcie->reg_offsets), |
| 489 | GFP_KERNEL); |
| 490 | if (!pcie->reg_offsets) |
| 491 | return -ENOMEM; |
| 492 | |
| 493 | /* go through the register table and populate all valid registers */ |
| 494 | pcie->reg_offsets[0] = regs[0]; |
| 495 | for (reg_idx = 1; reg_idx < IPROC_PCIE_MAX_NUM_REG; reg_idx++) |
| 496 | pcie->reg_offsets[reg_idx] = regs[reg_idx] ? |
| 497 | regs[reg_idx] : IPROC_PCIE_REG_INVALID; |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
Hauke Mehrtens | 18c4342 | 2015-05-24 22:37:02 +0200 | [diff] [blame] | 502 | int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 503 | { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 504 | struct device *dev; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 505 | int ret; |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 506 | void *sysdata; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 507 | struct pci_bus *bus; |
| 508 | |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 509 | dev = pcie->dev; |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame^] | 510 | |
| 511 | ret = iproc_pcie_rev_init(pcie); |
| 512 | if (ret) { |
| 513 | dev_err(dev, "unable to initialize controller parameters\n"); |
| 514 | return ret; |
| 515 | } |
| 516 | |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 517 | ret = devm_request_pci_bus_resources(dev, res); |
Bjorn Helgaas | c3245a5 | 2016-05-28 18:22:24 -0500 | [diff] [blame] | 518 | if (ret) |
| 519 | return ret; |
| 520 | |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 521 | ret = phy_init(pcie->phy); |
| 522 | if (ret) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 523 | dev_err(dev, "unable to initialize PCIe PHY\n"); |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 524 | return ret; |
| 525 | } |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 526 | |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 527 | ret = phy_power_on(pcie->phy); |
| 528 | if (ret) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 529 | dev_err(dev, "unable to power on PCIe PHY\n"); |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 530 | goto err_exit_phy; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | iproc_pcie_reset(pcie); |
| 534 | |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 535 | if (pcie->need_ob_cfg) { |
| 536 | ret = iproc_pcie_map_ranges(pcie, res); |
| 537 | if (ret) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 538 | dev_err(dev, "map failed\n"); |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 539 | goto err_power_off_phy; |
| 540 | } |
| 541 | } |
| 542 | |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 543 | #ifdef CONFIG_ARM |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 544 | pcie->sysdata.private_data = pcie; |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 545 | sysdata = &pcie->sysdata; |
| 546 | #else |
| 547 | sysdata = pcie; |
| 548 | #endif |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 549 | |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 550 | bus = pci_create_root_bus(dev, 0, &iproc_pcie_ops, sysdata, res); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 551 | if (!bus) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 552 | dev_err(dev, "unable to create PCI root bus\n"); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 553 | ret = -ENOMEM; |
| 554 | goto err_power_off_phy; |
| 555 | } |
| 556 | pcie->root_bus = bus; |
| 557 | |
| 558 | ret = iproc_pcie_check_link(pcie, bus); |
| 559 | if (ret) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 560 | dev_err(dev, "no PCIe EP device detected\n"); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 561 | goto err_rm_root_bus; |
| 562 | } |
| 563 | |
| 564 | iproc_pcie_enable(pcie); |
| 565 | |
Ray Jui | 3bc2b23 | 2016-01-06 18:04:35 -0600 | [diff] [blame] | 566 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
| 567 | if (iproc_pcie_msi_enable(pcie)) |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 568 | dev_info(dev, "not using iProc MSI\n"); |
Ray Jui | 3bc2b23 | 2016-01-06 18:04:35 -0600 | [diff] [blame] | 569 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 570 | pci_scan_child_bus(bus); |
| 571 | pci_assign_unassigned_bus_resources(bus); |
Hauke Mehrtens | c1e02ce | 2015-05-12 23:23:00 +0200 | [diff] [blame] | 572 | pci_fixup_irqs(pci_common_swizzle, pcie->map_irq); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 573 | pci_bus_add_devices(bus); |
| 574 | |
| 575 | return 0; |
| 576 | |
| 577 | err_rm_root_bus: |
| 578 | pci_stop_root_bus(bus); |
| 579 | pci_remove_root_bus(bus); |
| 580 | |
| 581 | err_power_off_phy: |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 582 | phy_power_off(pcie->phy); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 583 | err_exit_phy: |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 584 | phy_exit(pcie->phy); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 585 | return ret; |
| 586 | } |
| 587 | EXPORT_SYMBOL(iproc_pcie_setup); |
| 588 | |
| 589 | int iproc_pcie_remove(struct iproc_pcie *pcie) |
| 590 | { |
| 591 | pci_stop_root_bus(pcie->root_bus); |
| 592 | pci_remove_root_bus(pcie->root_bus); |
| 593 | |
Ray Jui | 3bc2b23 | 2016-01-06 18:04:35 -0600 | [diff] [blame] | 594 | iproc_pcie_msi_disable(pcie); |
| 595 | |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 596 | phy_power_off(pcie->phy); |
| 597 | phy_exit(pcie->phy); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | EXPORT_SYMBOL(iproc_pcie_remove); |
| 602 | |
| 603 | MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>"); |
| 604 | MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver"); |
| 605 | MODULE_LICENSE("GPL v2"); |