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