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