Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Hauke Mehrtens <hauke@hauke-m.de> |
| 3 | * Copyright (C) 2015 Broadcom Corporatcommon ion |
| 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 | |
| 33 | #define CLK_CONTROL_OFFSET 0x000 |
Ray Jui | 199ff14 | 2015-09-15 17:39:18 -0700 | [diff] [blame^] | 34 | #define EP_PERST_SOURCE_SELECT_SHIFT 2 |
| 35 | #define EP_PERST_SOURCE_SELECT BIT(EP_PERST_SOURCE_SELECT_SHIFT) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 36 | #define EP_MODE_SURVIVE_PERST_SHIFT 1 |
| 37 | #define EP_MODE_SURVIVE_PERST BIT(EP_MODE_SURVIVE_PERST_SHIFT) |
| 38 | #define RC_PCIE_RST_OUTPUT_SHIFT 0 |
| 39 | #define RC_PCIE_RST_OUTPUT BIT(RC_PCIE_RST_OUTPUT_SHIFT) |
| 40 | |
| 41 | #define CFG_IND_ADDR_OFFSET 0x120 |
| 42 | #define CFG_IND_ADDR_MASK 0x00001ffc |
| 43 | |
| 44 | #define CFG_IND_DATA_OFFSET 0x124 |
| 45 | |
| 46 | #define CFG_ADDR_OFFSET 0x1f8 |
| 47 | #define CFG_ADDR_BUS_NUM_SHIFT 20 |
| 48 | #define CFG_ADDR_BUS_NUM_MASK 0x0ff00000 |
| 49 | #define CFG_ADDR_DEV_NUM_SHIFT 15 |
| 50 | #define CFG_ADDR_DEV_NUM_MASK 0x000f8000 |
| 51 | #define CFG_ADDR_FUNC_NUM_SHIFT 12 |
| 52 | #define CFG_ADDR_FUNC_NUM_MASK 0x00007000 |
| 53 | #define CFG_ADDR_REG_NUM_SHIFT 2 |
| 54 | #define CFG_ADDR_REG_NUM_MASK 0x00000ffc |
| 55 | #define CFG_ADDR_CFG_TYPE_SHIFT 0 |
| 56 | #define CFG_ADDR_CFG_TYPE_MASK 0x00000003 |
| 57 | |
| 58 | #define CFG_DATA_OFFSET 0x1fc |
| 59 | |
| 60 | #define SYS_RC_INTX_EN 0x330 |
| 61 | #define SYS_RC_INTX_MASK 0xf |
| 62 | |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 63 | static inline struct iproc_pcie *iproc_data(struct pci_bus *bus) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 64 | { |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 65 | struct iproc_pcie *pcie; |
| 66 | #ifdef CONFIG_ARM |
| 67 | struct pci_sys_data *sys = bus->sysdata; |
| 68 | |
| 69 | pcie = sys->private_data; |
| 70 | #else |
| 71 | pcie = bus->sysdata; |
| 72 | #endif |
| 73 | return pcie; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Note access to the configuration registers are protected at the higher layer |
| 78 | * by 'pci_lock' in drivers/pci/access.c |
| 79 | */ |
| 80 | static void __iomem *iproc_pcie_map_cfg_bus(struct pci_bus *bus, |
| 81 | unsigned int devfn, |
| 82 | int where) |
| 83 | { |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 84 | struct iproc_pcie *pcie = iproc_data(bus); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 85 | unsigned slot = PCI_SLOT(devfn); |
| 86 | unsigned fn = PCI_FUNC(devfn); |
| 87 | unsigned busno = bus->number; |
| 88 | u32 val; |
| 89 | |
| 90 | /* root complex access */ |
| 91 | if (busno == 0) { |
| 92 | if (slot >= 1) |
| 93 | return NULL; |
| 94 | writel(where & CFG_IND_ADDR_MASK, |
| 95 | pcie->base + CFG_IND_ADDR_OFFSET); |
| 96 | return (pcie->base + CFG_IND_DATA_OFFSET); |
| 97 | } |
| 98 | |
| 99 | if (fn > 1) |
| 100 | return NULL; |
| 101 | |
| 102 | /* EP device access */ |
| 103 | val = (busno << CFG_ADDR_BUS_NUM_SHIFT) | |
| 104 | (slot << CFG_ADDR_DEV_NUM_SHIFT) | |
| 105 | (fn << CFG_ADDR_FUNC_NUM_SHIFT) | |
| 106 | (where & CFG_ADDR_REG_NUM_MASK) | |
| 107 | (1 & CFG_ADDR_CFG_TYPE_MASK); |
| 108 | writel(val, pcie->base + CFG_ADDR_OFFSET); |
| 109 | |
| 110 | return (pcie->base + CFG_DATA_OFFSET); |
| 111 | } |
| 112 | |
| 113 | static struct pci_ops iproc_pcie_ops = { |
| 114 | .map_bus = iproc_pcie_map_cfg_bus, |
| 115 | .read = pci_generic_config_read32, |
| 116 | .write = pci_generic_config_write32, |
| 117 | }; |
| 118 | |
| 119 | static void iproc_pcie_reset(struct iproc_pcie *pcie) |
| 120 | { |
| 121 | u32 val; |
| 122 | |
| 123 | /* |
Ray Jui | 199ff14 | 2015-09-15 17:39:18 -0700 | [diff] [blame^] | 124 | * Select perst_b signal as reset source. Put the device into reset, |
| 125 | * and then bring it out of reset |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 126 | */ |
Ray Jui | 199ff14 | 2015-09-15 17:39:18 -0700 | [diff] [blame^] | 127 | val = readl(pcie->base + CLK_CONTROL_OFFSET); |
| 128 | val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST & |
| 129 | ~RC_PCIE_RST_OUTPUT; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 130 | writel(val, pcie->base + CLK_CONTROL_OFFSET); |
| 131 | udelay(250); |
Ray Jui | 199ff14 | 2015-09-15 17:39:18 -0700 | [diff] [blame^] | 132 | |
| 133 | val |= RC_PCIE_RST_OUTPUT; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 134 | writel(val, pcie->base + CLK_CONTROL_OFFSET); |
Ray Jui | 199ff14 | 2015-09-15 17:39:18 -0700 | [diff] [blame^] | 135 | msleep(100); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static int iproc_pcie_check_link(struct iproc_pcie *pcie, struct pci_bus *bus) |
| 139 | { |
| 140 | u8 hdr_type; |
| 141 | u32 link_ctrl; |
| 142 | u16 pos, link_status; |
| 143 | int link_is_active = 0; |
| 144 | |
| 145 | /* make sure we are not in EP mode */ |
| 146 | pci_bus_read_config_byte(bus, 0, PCI_HEADER_TYPE, &hdr_type); |
| 147 | if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) { |
| 148 | dev_err(pcie->dev, "in EP mode, hdr=%#02x\n", hdr_type); |
| 149 | return -EFAULT; |
| 150 | } |
| 151 | |
| 152 | /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */ |
| 153 | pci_bus_write_config_word(bus, 0, PCI_CLASS_DEVICE, |
| 154 | PCI_CLASS_BRIDGE_PCI); |
| 155 | |
| 156 | /* check link status to see if link is active */ |
| 157 | pos = pci_bus_find_capability(bus, 0, PCI_CAP_ID_EXP); |
| 158 | pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA, &link_status); |
| 159 | if (link_status & PCI_EXP_LNKSTA_NLW) |
| 160 | link_is_active = 1; |
| 161 | |
| 162 | if (!link_is_active) { |
| 163 | /* try GEN 1 link speed */ |
| 164 | #define PCI_LINK_STATUS_CTRL_2_OFFSET 0x0dc |
| 165 | #define PCI_TARGET_LINK_SPEED_MASK 0xf |
| 166 | #define PCI_TARGET_LINK_SPEED_GEN2 0x2 |
| 167 | #define PCI_TARGET_LINK_SPEED_GEN1 0x1 |
| 168 | pci_bus_read_config_dword(bus, 0, |
| 169 | PCI_LINK_STATUS_CTRL_2_OFFSET, |
| 170 | &link_ctrl); |
| 171 | if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) == |
| 172 | PCI_TARGET_LINK_SPEED_GEN2) { |
| 173 | link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK; |
| 174 | link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1; |
| 175 | pci_bus_write_config_dword(bus, 0, |
| 176 | PCI_LINK_STATUS_CTRL_2_OFFSET, |
| 177 | link_ctrl); |
| 178 | msleep(100); |
| 179 | |
| 180 | pos = pci_bus_find_capability(bus, 0, PCI_CAP_ID_EXP); |
| 181 | pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA, |
| 182 | &link_status); |
| 183 | if (link_status & PCI_EXP_LNKSTA_NLW) |
| 184 | link_is_active = 1; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | dev_info(pcie->dev, "link: %s\n", link_is_active ? "UP" : "DOWN"); |
| 189 | |
| 190 | return link_is_active ? 0 : -ENODEV; |
| 191 | } |
| 192 | |
| 193 | static void iproc_pcie_enable(struct iproc_pcie *pcie) |
| 194 | { |
| 195 | writel(SYS_RC_INTX_MASK, pcie->base + SYS_RC_INTX_EN); |
| 196 | } |
| 197 | |
Hauke Mehrtens | 18c4342 | 2015-05-24 22:37:02 +0200 | [diff] [blame] | 198 | int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 199 | { |
| 200 | int ret; |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 201 | void *sysdata; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 202 | struct pci_bus *bus; |
| 203 | |
| 204 | if (!pcie || !pcie->dev || !pcie->base) |
| 205 | return -EINVAL; |
| 206 | |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 207 | ret = phy_init(pcie->phy); |
| 208 | if (ret) { |
| 209 | dev_err(pcie->dev, "unable to initialize PCIe PHY\n"); |
| 210 | return ret; |
| 211 | } |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 212 | |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 213 | ret = phy_power_on(pcie->phy); |
| 214 | if (ret) { |
| 215 | dev_err(pcie->dev, "unable to power on PCIe PHY\n"); |
| 216 | goto err_exit_phy; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | iproc_pcie_reset(pcie); |
| 220 | |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 221 | #ifdef CONFIG_ARM |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 222 | pcie->sysdata.private_data = pcie; |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 223 | sysdata = &pcie->sysdata; |
| 224 | #else |
| 225 | sysdata = pcie; |
| 226 | #endif |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 227 | |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 228 | bus = pci_create_root_bus(pcie->dev, 0, &iproc_pcie_ops, sysdata, res); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 229 | if (!bus) { |
| 230 | dev_err(pcie->dev, "unable to create PCI root bus\n"); |
| 231 | ret = -ENOMEM; |
| 232 | goto err_power_off_phy; |
| 233 | } |
| 234 | pcie->root_bus = bus; |
| 235 | |
| 236 | ret = iproc_pcie_check_link(pcie, bus); |
| 237 | if (ret) { |
| 238 | dev_err(pcie->dev, "no PCIe EP device detected\n"); |
| 239 | goto err_rm_root_bus; |
| 240 | } |
| 241 | |
| 242 | iproc_pcie_enable(pcie); |
| 243 | |
| 244 | pci_scan_child_bus(bus); |
| 245 | pci_assign_unassigned_bus_resources(bus); |
Hauke Mehrtens | c1e02ce | 2015-05-12 23:23:00 +0200 | [diff] [blame] | 246 | pci_fixup_irqs(pci_common_swizzle, pcie->map_irq); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 247 | pci_bus_add_devices(bus); |
| 248 | |
| 249 | return 0; |
| 250 | |
| 251 | err_rm_root_bus: |
| 252 | pci_stop_root_bus(bus); |
| 253 | pci_remove_root_bus(bus); |
| 254 | |
| 255 | err_power_off_phy: |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 256 | phy_power_off(pcie->phy); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 257 | err_exit_phy: |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 258 | phy_exit(pcie->phy); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 259 | return ret; |
| 260 | } |
| 261 | EXPORT_SYMBOL(iproc_pcie_setup); |
| 262 | |
| 263 | int iproc_pcie_remove(struct iproc_pcie *pcie) |
| 264 | { |
| 265 | pci_stop_root_bus(pcie->root_bus); |
| 266 | pci_remove_root_bus(pcie->root_bus); |
| 267 | |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 268 | phy_power_off(pcie->phy); |
| 269 | phy_exit(pcie->phy); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | EXPORT_SYMBOL(iproc_pcie_remove); |
| 274 | |
| 275 | MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>"); |
| 276 | MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver"); |
| 277 | MODULE_LICENSE("GPL v2"); |