blob: 80e0541548a15a47afb98aa5732504f878416845 [file] [log] [blame]
Ray Jui1fb37a82015-04-08 11:21:35 -07001/*
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 Jui199ff142015-09-15 17:39:18 -070034#define EP_PERST_SOURCE_SELECT_SHIFT 2
35#define EP_PERST_SOURCE_SELECT BIT(EP_PERST_SOURCE_SELECT_SHIFT)
Ray Jui1fb37a82015-04-08 11:21:35 -070036#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 Jui8d9bfe32015-07-21 18:29:40 -070063static inline struct iproc_pcie *iproc_data(struct pci_bus *bus)
Ray Jui1fb37a82015-04-08 11:21:35 -070064{
Ray Jui8d9bfe32015-07-21 18:29:40 -070065 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 Jui1fb37a82015-04-08 11:21:35 -070074}
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 */
80static void __iomem *iproc_pcie_map_cfg_bus(struct pci_bus *bus,
81 unsigned int devfn,
82 int where)
83{
Ray Jui8d9bfe32015-07-21 18:29:40 -070084 struct iproc_pcie *pcie = iproc_data(bus);
Ray Jui1fb37a82015-04-08 11:21:35 -070085 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
113static 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
119static void iproc_pcie_reset(struct iproc_pcie *pcie)
120{
121 u32 val;
122
123 /*
Ray Jui199ff142015-09-15 17:39:18 -0700124 * Select perst_b signal as reset source. Put the device into reset,
125 * and then bring it out of reset
Ray Jui1fb37a82015-04-08 11:21:35 -0700126 */
Ray Jui199ff142015-09-15 17:39:18 -0700127 val = readl(pcie->base + CLK_CONTROL_OFFSET);
128 val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST &
129 ~RC_PCIE_RST_OUTPUT;
Ray Jui1fb37a82015-04-08 11:21:35 -0700130 writel(val, pcie->base + CLK_CONTROL_OFFSET);
131 udelay(250);
Ray Jui199ff142015-09-15 17:39:18 -0700132
133 val |= RC_PCIE_RST_OUTPUT;
Ray Jui1fb37a82015-04-08 11:21:35 -0700134 writel(val, pcie->base + CLK_CONTROL_OFFSET);
Ray Jui199ff142015-09-15 17:39:18 -0700135 msleep(100);
Ray Jui1fb37a82015-04-08 11:21:35 -0700136}
137
138static 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
193static void iproc_pcie_enable(struct iproc_pcie *pcie)
194{
195 writel(SYS_RC_INTX_MASK, pcie->base + SYS_RC_INTX_EN);
196}
197
Hauke Mehrtens18c43422015-05-24 22:37:02 +0200198int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res)
Ray Jui1fb37a82015-04-08 11:21:35 -0700199{
200 int ret;
Ray Jui8d9bfe32015-07-21 18:29:40 -0700201 void *sysdata;
Ray Jui1fb37a82015-04-08 11:21:35 -0700202 struct pci_bus *bus;
203
204 if (!pcie || !pcie->dev || !pcie->base)
205 return -EINVAL;
206
Markus Elfring93972d12015-06-28 16:42:04 +0200207 ret = phy_init(pcie->phy);
208 if (ret) {
209 dev_err(pcie->dev, "unable to initialize PCIe PHY\n");
210 return ret;
211 }
Ray Jui1fb37a82015-04-08 11:21:35 -0700212
Markus Elfring93972d12015-06-28 16:42:04 +0200213 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 Jui1fb37a82015-04-08 11:21:35 -0700217 }
218
219 iproc_pcie_reset(pcie);
220
Ray Jui8d9bfe32015-07-21 18:29:40 -0700221#ifdef CONFIG_ARM
Ray Jui1fb37a82015-04-08 11:21:35 -0700222 pcie->sysdata.private_data = pcie;
Ray Jui8d9bfe32015-07-21 18:29:40 -0700223 sysdata = &pcie->sysdata;
224#else
225 sysdata = pcie;
226#endif
Ray Jui1fb37a82015-04-08 11:21:35 -0700227
Ray Jui8d9bfe32015-07-21 18:29:40 -0700228 bus = pci_create_root_bus(pcie->dev, 0, &iproc_pcie_ops, sysdata, res);
Ray Jui1fb37a82015-04-08 11:21:35 -0700229 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 Mehrtensc1e02ce2015-05-12 23:23:00 +0200246 pci_fixup_irqs(pci_common_swizzle, pcie->map_irq);
Ray Jui1fb37a82015-04-08 11:21:35 -0700247 pci_bus_add_devices(bus);
248
249 return 0;
250
251err_rm_root_bus:
252 pci_stop_root_bus(bus);
253 pci_remove_root_bus(bus);
254
255err_power_off_phy:
Markus Elfring93972d12015-06-28 16:42:04 +0200256 phy_power_off(pcie->phy);
Ray Jui1fb37a82015-04-08 11:21:35 -0700257err_exit_phy:
Markus Elfring93972d12015-06-28 16:42:04 +0200258 phy_exit(pcie->phy);
Ray Jui1fb37a82015-04-08 11:21:35 -0700259 return ret;
260}
261EXPORT_SYMBOL(iproc_pcie_setup);
262
263int 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 Elfring93972d12015-06-28 16:42:04 +0200268 phy_power_off(pcie->phy);
269 phy_exit(pcie->phy);
Ray Jui1fb37a82015-04-08 11:21:35 -0700270
271 return 0;
272}
273EXPORT_SYMBOL(iproc_pcie_remove);
274
275MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
276MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver");
277MODULE_LICENSE("GPL v2");