blob: 23d7f73cc347d12297a91c157061a1544da0455d [file] [log] [blame]
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001/*
2 * PCIe driver for Marvell Armada 370 and Armada XP SoCs
3 *
Paul Gortmaker82641d92016-07-02 19:13:28 -04004 * Author: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
5 *
Thomas Petazzoni45361a42013-05-16 17:55:22 +02006 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 */
10
11#include <linux/kernel.h>
12#include <linux/pci.h>
13#include <linux/clk.h>
Sebastian Hesselbarth52ba9922013-08-13 14:25:23 +020014#include <linux/delay.h>
15#include <linux/gpio.h>
Paul Gortmaker82641d92016-07-02 19:13:28 -040016#include <linux/init.h>
Thomas Petazzoni45361a42013-05-16 17:55:22 +020017#include <linux/mbus.h>
Thomas Petazzoni5b4deb62013-08-09 22:27:14 +020018#include <linux/msi.h>
Thomas Petazzoni45361a42013-05-16 17:55:22 +020019#include <linux/slab.h>
20#include <linux/platform_device.h>
21#include <linux/of_address.h>
Thomas Petazzoni45361a42013-05-16 17:55:22 +020022#include <linux/of_irq.h>
Sebastian Hesselbarth52ba9922013-08-13 14:25:23 +020023#include <linux/of_gpio.h>
24#include <linux/of_pci.h>
Thomas Petazzoni45361a42013-05-16 17:55:22 +020025#include <linux/of_platform.h>
26
27/*
28 * PCIe unit register offsets.
29 */
30#define PCIE_DEV_ID_OFF 0x0000
31#define PCIE_CMD_OFF 0x0004
32#define PCIE_DEV_REV_OFF 0x0008
33#define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
34#define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
Russell Kingdc0352a2015-10-03 19:13:33 +010035#define PCIE_CAP_PCIEXP 0x0060
Thomas Petazzoni45361a42013-05-16 17:55:22 +020036#define PCIE_HEADER_LOG_4_OFF 0x0128
37#define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
38#define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
39#define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
40#define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
41#define PCIE_WIN5_CTRL_OFF 0x1880
42#define PCIE_WIN5_BASE_OFF 0x1884
43#define PCIE_WIN5_REMAP_OFF 0x188c
44#define PCIE_CONF_ADDR_OFF 0x18f8
45#define PCIE_CONF_ADDR_EN 0x80000000
46#define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
47#define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
48#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
49#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
50#define PCIE_CONF_ADDR(bus, devfn, where) \
51 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
52 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \
53 PCIE_CONF_ADDR_EN)
54#define PCIE_CONF_DATA_OFF 0x18fc
55#define PCIE_MASK_OFF 0x1910
56#define PCIE_MASK_ENABLE_INTS 0x0f000000
57#define PCIE_CTRL_OFF 0x1a00
58#define PCIE_CTRL_X1_MODE 0x0001
59#define PCIE_STAT_OFF 0x1a04
60#define PCIE_STAT_BUS 0xff00
Thomas Petazzonif4ac9902013-05-23 16:32:51 +020061#define PCIE_STAT_DEV 0x1f0000
Thomas Petazzoni45361a42013-05-16 17:55:22 +020062#define PCIE_STAT_LINK_DOWN BIT(0)
Russell Kingdc0352a2015-10-03 19:13:33 +010063#define PCIE_RC_RTSTA 0x1a14
Thomas Petazzoni45361a42013-05-16 17:55:22 +020064#define PCIE_DEBUG_CTRL 0x1a60
65#define PCIE_DEBUG_SOFT_RESET BIT(20)
66
Russell Kingdc0352a2015-10-03 19:13:33 +010067enum {
68 PCISWCAP = PCI_BRIDGE_CONTROL + 2,
69 PCISWCAP_EXP_LIST_ID = PCISWCAP + PCI_CAP_LIST_ID,
70 PCISWCAP_EXP_DEVCAP = PCISWCAP + PCI_EXP_DEVCAP,
71 PCISWCAP_EXP_DEVCTL = PCISWCAP + PCI_EXP_DEVCTL,
72 PCISWCAP_EXP_LNKCAP = PCISWCAP + PCI_EXP_LNKCAP,
73 PCISWCAP_EXP_LNKCTL = PCISWCAP + PCI_EXP_LNKCTL,
74 PCISWCAP_EXP_SLTCAP = PCISWCAP + PCI_EXP_SLTCAP,
75 PCISWCAP_EXP_SLTCTL = PCISWCAP + PCI_EXP_SLTCTL,
76 PCISWCAP_EXP_RTCTL = PCISWCAP + PCI_EXP_RTCTL,
77 PCISWCAP_EXP_RTSTA = PCISWCAP + PCI_EXP_RTSTA,
78 PCISWCAP_EXP_DEVCAP2 = PCISWCAP + PCI_EXP_DEVCAP2,
79 PCISWCAP_EXP_DEVCTL2 = PCISWCAP + PCI_EXP_DEVCTL2,
80 PCISWCAP_EXP_LNKCAP2 = PCISWCAP + PCI_EXP_LNKCAP2,
81 PCISWCAP_EXP_LNKCTL2 = PCISWCAP + PCI_EXP_LNKCTL2,
82 PCISWCAP_EXP_SLTCAP2 = PCISWCAP + PCI_EXP_SLTCAP2,
83 PCISWCAP_EXP_SLTCTL2 = PCISWCAP + PCI_EXP_SLTCTL2,
84};
85
Thomas Petazzoni45361a42013-05-16 17:55:22 +020086/* PCI configuration space of a PCI-to-PCI bridge */
87struct mvebu_sw_pci_bridge {
88 u16 vendor;
89 u16 device;
90 u16 command;
Russell Kingdc0352a2015-10-03 19:13:33 +010091 u16 status;
Thomas Petazzoni45361a42013-05-16 17:55:22 +020092 u16 class;
93 u8 interface;
94 u8 revision;
95 u8 bist;
96 u8 header_type;
97 u8 latency_timer;
98 u8 cache_line_size;
99 u32 bar[2];
100 u8 primary_bus;
101 u8 secondary_bus;
102 u8 subordinate_bus;
103 u8 secondary_latency_timer;
104 u8 iobase;
105 u8 iolimit;
106 u16 secondary_status;
107 u16 membase;
108 u16 memlimit;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200109 u16 iobaseupper;
110 u16 iolimitupper;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200111 u32 romaddr;
112 u8 intline;
113 u8 intpin;
114 u16 bridgectrl;
Russell Kingdc0352a2015-10-03 19:13:33 +0100115
116 /* PCI express capability */
117 u32 pcie_sltcap;
118 u16 pcie_devctl;
119 u16 pcie_rtctl;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200120};
121
122struct mvebu_pcie_port;
123
124/* Structure representing all PCIe interfaces */
125struct mvebu_pcie {
126 struct platform_device *pdev;
127 struct mvebu_pcie_port *ports;
Yijing Wangc2791b82014-11-11 17:45:45 -0700128 struct msi_controller *msi;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200129 struct resource io;
130 struct resource realio;
131 struct resource mem;
132 struct resource busn;
133 int nports;
134};
135
Jason Gunthorpe7ac8a102016-12-12 11:30:20 -0700136struct mvebu_pcie_window {
137 phys_addr_t base;
138 phys_addr_t remap;
139 size_t size;
140};
141
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200142/* Structure representing one PCIe interface */
143struct mvebu_pcie_port {
144 char *name;
145 void __iomem *base;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200146 u32 port;
147 u32 lane;
148 int devfn;
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300149 unsigned int mem_target;
150 unsigned int mem_attr;
151 unsigned int io_target;
152 unsigned int io_attr;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200153 struct clk *clk;
Russell King8a182c22015-10-03 19:13:22 +0100154 struct gpio_desc *reset_gpio;
Sebastian Hesselbarth52ba9922013-08-13 14:25:23 +0200155 char *reset_name;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200156 struct mvebu_sw_pci_bridge bridge;
157 struct device_node *dn;
158 struct mvebu_pcie *pcie;
Jason Gunthorpe7ac8a102016-12-12 11:30:20 -0700159 struct mvebu_pcie_window memwin;
160 struct mvebu_pcie_window iowin;
Thomas Petazzoniab14d452015-03-17 15:55:45 +0100161 u32 saved_pcie_stat;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200162};
163
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900164static inline void mvebu_writel(struct mvebu_pcie_port *port, u32 val, u32 reg)
165{
166 writel(val, port->base + reg);
167}
168
169static inline u32 mvebu_readl(struct mvebu_pcie_port *port, u32 reg)
170{
171 return readl(port->base + reg);
172}
173
Jason Gunthorpe641e6742013-11-26 11:02:55 -0700174static inline bool mvebu_has_ioport(struct mvebu_pcie_port *port)
175{
176 return port->io_target != -1 && port->io_attr != -1;
177}
178
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200179static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port)
180{
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900181 return !(mvebu_readl(port, PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200182}
183
184static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
185{
186 u32 stat;
187
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900188 stat = mvebu_readl(port, PCIE_STAT_OFF);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200189 stat &= ~PCIE_STAT_BUS;
190 stat |= nr << 8;
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900191 mvebu_writel(port, stat, PCIE_STAT_OFF);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200192}
193
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200194static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr)
195{
196 u32 stat;
197
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900198 stat = mvebu_readl(port, PCIE_STAT_OFF);
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200199 stat &= ~PCIE_STAT_DEV;
200 stat |= nr << 16;
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900201 mvebu_writel(port, stat, PCIE_STAT_OFF);
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200202}
203
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200204/*
205 * Setup PCIE BARs and Address Decode Wins:
206 * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
207 * WIN[0-3] -> DRAM bank[0-3]
208 */
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200209static void mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200210{
211 const struct mbus_dram_target_info *dram;
212 u32 size;
213 int i;
214
215 dram = mv_mbus_dram_info();
216
217 /* First, disable and clear BARs and windows. */
218 for (i = 1; i < 3; i++) {
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900219 mvebu_writel(port, 0, PCIE_BAR_CTRL_OFF(i));
220 mvebu_writel(port, 0, PCIE_BAR_LO_OFF(i));
221 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(i));
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200222 }
223
224 for (i = 0; i < 5; i++) {
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900225 mvebu_writel(port, 0, PCIE_WIN04_CTRL_OFF(i));
226 mvebu_writel(port, 0, PCIE_WIN04_BASE_OFF(i));
227 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200228 }
229
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900230 mvebu_writel(port, 0, PCIE_WIN5_CTRL_OFF);
231 mvebu_writel(port, 0, PCIE_WIN5_BASE_OFF);
232 mvebu_writel(port, 0, PCIE_WIN5_REMAP_OFF);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200233
234 /* Setup windows for DDR banks. Count total DDR size on the fly. */
235 size = 0;
236 for (i = 0; i < dram->num_cs; i++) {
237 const struct mbus_dram_window *cs = dram->cs + i;
238
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900239 mvebu_writel(port, cs->base & 0xffff0000,
240 PCIE_WIN04_BASE_OFF(i));
241 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
242 mvebu_writel(port,
243 ((cs->size - 1) & 0xffff0000) |
244 (cs->mbus_attr << 8) |
245 (dram->mbus_dram_target_id << 4) | 1,
246 PCIE_WIN04_CTRL_OFF(i));
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200247
248 size += cs->size;
249 }
250
251 /* Round up 'size' to the nearest power of two. */
252 if ((size & (size - 1)) != 0)
253 size = 1 << fls(size);
254
255 /* Setup BAR[1] to all DRAM banks. */
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900256 mvebu_writel(port, dram->cs[0].base, PCIE_BAR_LO_OFF(1));
257 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(1));
258 mvebu_writel(port, ((size - 1) & 0xffff0000) | 1,
259 PCIE_BAR_CTRL_OFF(1));
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200260}
261
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200262static void mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200263{
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900264 u32 cmd, mask;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200265
266 /* Point PCIe unit MBUS decode windows to DRAM space. */
267 mvebu_pcie_setup_wins(port);
268
269 /* Master + slave enable. */
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900270 cmd = mvebu_readl(port, PCIE_CMD_OFF);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200271 cmd |= PCI_COMMAND_IO;
272 cmd |= PCI_COMMAND_MEMORY;
273 cmd |= PCI_COMMAND_MASTER;
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900274 mvebu_writel(port, cmd, PCIE_CMD_OFF);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200275
276 /* Enable interrupt lines A-D. */
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900277 mask = mvebu_readl(port, PCIE_MASK_OFF);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200278 mask |= PCIE_MASK_ENABLE_INTS;
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900279 mvebu_writel(port, mask, PCIE_MASK_OFF);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200280}
281
282static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
283 struct pci_bus *bus,
284 u32 devfn, int where, int size, u32 *val)
285{
Russell King79e3f6c2015-09-23 18:17:32 +0100286 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
287
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900288 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
289 PCIE_CONF_ADDR_OFF);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200290
Russell King79e3f6c2015-09-23 18:17:32 +0100291 switch (size) {
292 case 1:
293 *val = readb_relaxed(conf_data + (where & 3));
294 break;
295 case 2:
296 *val = readw_relaxed(conf_data + (where & 2));
297 break;
298 case 4:
299 *val = readl_relaxed(conf_data);
300 break;
301 }
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200302
303 return PCIBIOS_SUCCESSFUL;
304}
305
306static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
307 struct pci_bus *bus,
308 u32 devfn, int where, int size, u32 val)
309{
Russell King79e3f6c2015-09-23 18:17:32 +0100310 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200311
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900312 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
313 PCIE_CONF_ADDR_OFF);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200314
Russell King79e3f6c2015-09-23 18:17:32 +0100315 switch (size) {
316 case 1:
317 writeb(val, conf_data + (where & 3));
318 break;
319 case 2:
320 writew(val, conf_data + (where & 2));
321 break;
322 case 4:
323 writel(val, conf_data);
324 break;
325 default:
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900326 return PCIBIOS_BAD_REGISTER_NUMBER;
Russell King79e3f6c2015-09-23 18:17:32 +0100327 }
Seungwon Jeon032b4c02013-10-04 18:58:15 +0900328
329 return PCIBIOS_SUCCESSFUL;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200330}
331
Thomas Petazzoni398f5d52014-04-18 14:19:53 +0200332/*
333 * Remove windows, starting from the largest ones to the smallest
334 * ones.
335 */
336static void mvebu_pcie_del_windows(struct mvebu_pcie_port *port,
337 phys_addr_t base, size_t size)
338{
339 while (size) {
340 size_t sz = 1 << (fls(size) - 1);
341
342 mvebu_mbus_del_window(base, sz);
343 base += sz;
344 size -= sz;
345 }
346}
347
348/*
349 * MBus windows can only have a power of two size, but PCI BARs do not
350 * have this constraint. Therefore, we have to split the PCI BAR into
351 * areas each having a power of two size. We start from the largest
352 * one (i.e highest order bit set in the size).
353 */
354static void mvebu_pcie_add_windows(struct mvebu_pcie_port *port,
355 unsigned int target, unsigned int attribute,
356 phys_addr_t base, size_t size,
357 phys_addr_t remap)
358{
359 size_t size_mapped = 0;
360
361 while (size) {
362 size_t sz = 1 << (fls(size) - 1);
363 int ret;
364
365 ret = mvebu_mbus_add_window_remap_by_id(target, attribute, base,
366 sz, remap);
367 if (ret) {
Fabio Estevam9aa52852014-04-29 09:58:07 -0300368 phys_addr_t end = base + sz - 1;
369
Thomas Petazzoni398f5d52014-04-18 14:19:53 +0200370 dev_err(&port->pcie->pdev->dev,
Fabio Estevam9aa52852014-04-29 09:58:07 -0300371 "Could not create MBus window at [mem %pa-%pa]: %d\n",
372 &base, &end, ret);
Thomas Petazzoni398f5d52014-04-18 14:19:53 +0200373 mvebu_pcie_del_windows(port, base - size_mapped,
374 size_mapped);
375 return;
376 }
377
378 size -= sz;
379 size_mapped += sz;
380 base += sz;
381 if (remap != MVEBU_MBUS_NO_REMAP)
382 remap += sz;
383 }
384}
385
Jason Gunthorpe7ac8a102016-12-12 11:30:20 -0700386static void mvebu_pcie_set_window(struct mvebu_pcie_port *port,
387 unsigned int target, unsigned int attribute,
388 const struct mvebu_pcie_window *desired,
389 struct mvebu_pcie_window *cur)
390{
391 if (desired->base == cur->base && desired->remap == cur->remap &&
392 desired->size == cur->size)
393 return;
394
395 if (cur->size != 0) {
396 mvebu_pcie_del_windows(port, cur->base, cur->size);
397 cur->size = 0;
398 cur->base = 0;
399
400 /*
401 * If something tries to change the window while it is enabled
402 * the change will not be done atomically. That would be
403 * difficult to do in the general case.
404 */
405 }
406
407 if (desired->size == 0)
408 return;
409
410 mvebu_pcie_add_windows(port, target, attribute, desired->base,
411 desired->size, desired->remap);
412 *cur = *desired;
413}
414
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200415static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
416{
Jason Gunthorpe7ac8a102016-12-12 11:30:20 -0700417 struct mvebu_pcie_window desired = {};
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200418
419 /* Are the new iobase/iolimit values invalid? */
420 if (port->bridge.iolimit < port->bridge.iobase ||
Jason Gunthorpe43a16f92013-11-26 11:02:54 -0700421 port->bridge.iolimitupper < port->bridge.iobaseupper ||
422 !(port->bridge.command & PCI_COMMAND_IO)) {
Jason Gunthorpe7ac8a102016-12-12 11:30:20 -0700423 mvebu_pcie_set_window(port, port->io_target, port->io_attr,
424 &desired, &port->iowin);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200425 return;
426 }
427
Jason Gunthorpe641e6742013-11-26 11:02:55 -0700428 if (!mvebu_has_ioport(port)) {
429 dev_WARN(&port->pcie->pdev->dev,
430 "Attempt to set IO when IO is disabled\n");
431 return;
432 }
433
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200434 /*
435 * We read the PCI-to-PCI bridge emulated registers, and
436 * calculate the base address and size of the address decoding
437 * window to setup, according to the PCI-to-PCI bridge
438 * specifications. iobase is the bus address, port->iowin_base
439 * is the CPU address.
440 */
Jason Gunthorpe7ac8a102016-12-12 11:30:20 -0700441 desired.remap = ((port->bridge.iobase & 0xF0) << 8) |
442 (port->bridge.iobaseupper << 16);
443 desired.base = port->pcie->io.start + desired.remap;
444 desired.size = ((0xFFF | ((port->bridge.iolimit & 0xF0) << 8) |
445 (port->bridge.iolimitupper << 16)) -
446 desired.remap) +
447 1;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200448
Jason Gunthorpe7ac8a102016-12-12 11:30:20 -0700449 mvebu_pcie_set_window(port, port->io_target, port->io_attr, &desired,
450 &port->iowin);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200451}
452
453static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
454{
Jason Gunthorpe7ac8a102016-12-12 11:30:20 -0700455 struct mvebu_pcie_window desired = {.remap = MVEBU_MBUS_NO_REMAP};
456
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200457 /* Are the new membase/memlimit values invalid? */
Jason Gunthorpe43a16f92013-11-26 11:02:54 -0700458 if (port->bridge.memlimit < port->bridge.membase ||
459 !(port->bridge.command & PCI_COMMAND_MEMORY)) {
Jason Gunthorpe7ac8a102016-12-12 11:30:20 -0700460 mvebu_pcie_set_window(port, port->mem_target, port->mem_attr,
461 &desired, &port->memwin);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200462 return;
463 }
464
465 /*
466 * We read the PCI-to-PCI bridge emulated registers, and
467 * calculate the base address and size of the address decoding
468 * window to setup, according to the PCI-to-PCI bridge
469 * specifications.
470 */
Jason Gunthorpe7ac8a102016-12-12 11:30:20 -0700471 desired.base = ((port->bridge.membase & 0xFFF0) << 16);
472 desired.size = (((port->bridge.memlimit & 0xFFF0) << 16) | 0xFFFFF) -
473 desired.base + 1;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200474
Jason Gunthorpe7ac8a102016-12-12 11:30:20 -0700475 mvebu_pcie_set_window(port, port->mem_target, port->mem_attr, &desired,
476 &port->memwin);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200477}
478
479/*
480 * Initialize the configuration space of the PCI-to-PCI bridge
481 * associated with the given PCIe interface.
482 */
483static void mvebu_sw_pci_bridge_init(struct mvebu_pcie_port *port)
484{
485 struct mvebu_sw_pci_bridge *bridge = &port->bridge;
486
487 memset(bridge, 0, sizeof(struct mvebu_sw_pci_bridge));
488
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200489 bridge->class = PCI_CLASS_BRIDGE_PCI;
490 bridge->vendor = PCI_VENDOR_ID_MARVELL;
Andrew Lunna760d2f2014-02-05 11:55:49 +0100491 bridge->device = mvebu_readl(port, PCIE_DEV_ID_OFF) >> 16;
492 bridge->revision = mvebu_readl(port, PCIE_DEV_REV_OFF) & 0xff;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200493 bridge->header_type = PCI_HEADER_TYPE_BRIDGE;
494 bridge->cache_line_size = 0x10;
495
496 /* We support 32 bits I/O addressing */
497 bridge->iobase = PCI_IO_RANGE_TYPE_32;
498 bridge->iolimit = PCI_IO_RANGE_TYPE_32;
Russell Kingdc0352a2015-10-03 19:13:33 +0100499
500 /* Add capabilities */
501 bridge->status = PCI_STATUS_CAP_LIST;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200502}
503
504/*
505 * Read the configuration space of the PCI-to-PCI bridge associated to
506 * the given PCIe interface.
507 */
508static int mvebu_sw_pci_bridge_read(struct mvebu_pcie_port *port,
509 unsigned int where, int size, u32 *value)
510{
511 struct mvebu_sw_pci_bridge *bridge = &port->bridge;
512
513 switch (where & ~3) {
514 case PCI_VENDOR_ID:
515 *value = bridge->device << 16 | bridge->vendor;
516 break;
517
518 case PCI_COMMAND:
Russell Kingdc0352a2015-10-03 19:13:33 +0100519 *value = bridge->command | bridge->status << 16;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200520 break;
521
522 case PCI_CLASS_REVISION:
523 *value = bridge->class << 16 | bridge->interface << 8 |
524 bridge->revision;
525 break;
526
527 case PCI_CACHE_LINE_SIZE:
528 *value = bridge->bist << 24 | bridge->header_type << 16 |
529 bridge->latency_timer << 8 | bridge->cache_line_size;
530 break;
531
532 case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
533 *value = bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4];
534 break;
535
536 case PCI_PRIMARY_BUS:
537 *value = (bridge->secondary_latency_timer << 24 |
538 bridge->subordinate_bus << 16 |
539 bridge->secondary_bus << 8 |
540 bridge->primary_bus);
541 break;
542
543 case PCI_IO_BASE:
Jason Gunthorpe641e6742013-11-26 11:02:55 -0700544 if (!mvebu_has_ioport(port))
545 *value = bridge->secondary_status << 16;
546 else
547 *value = (bridge->secondary_status << 16 |
548 bridge->iolimit << 8 |
549 bridge->iobase);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200550 break;
551
552 case PCI_MEMORY_BASE:
553 *value = (bridge->memlimit << 16 | bridge->membase);
554 break;
555
556 case PCI_PREF_MEMORY_BASE:
Thomas Petazzoni36dd1f32013-08-01 15:44:19 +0200557 *value = 0;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200558 break;
559
560 case PCI_IO_BASE_UPPER16:
561 *value = (bridge->iolimitupper << 16 | bridge->iobaseupper);
562 break;
563
Russell Kingdc0352a2015-10-03 19:13:33 +0100564 case PCI_CAPABILITY_LIST:
565 *value = PCISWCAP;
566 break;
567
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200568 case PCI_ROM_ADDRESS1:
569 *value = 0;
570 break;
571
Jason Gunthorpef407dae2013-11-26 11:27:28 -0700572 case PCI_INTERRUPT_LINE:
573 /* LINE PIN MIN_GNT MAX_LAT */
574 *value = 0;
575 break;
576
Russell Kingdc0352a2015-10-03 19:13:33 +0100577 case PCISWCAP_EXP_LIST_ID:
578 /* Set PCIe v2, root port, slot support */
579 *value = (PCI_EXP_TYPE_ROOT_PORT << 4 | 2 |
580 PCI_EXP_FLAGS_SLOT) << 16 | PCI_CAP_ID_EXP;
581 break;
582
583 case PCISWCAP_EXP_DEVCAP:
584 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCAP);
585 break;
586
587 case PCISWCAP_EXP_DEVCTL:
588 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL) &
589 ~(PCI_EXP_DEVCTL_URRE | PCI_EXP_DEVCTL_FERE |
590 PCI_EXP_DEVCTL_NFERE | PCI_EXP_DEVCTL_CERE);
591 *value |= bridge->pcie_devctl;
592 break;
593
594 case PCISWCAP_EXP_LNKCAP:
595 /*
596 * PCIe requires the clock power management capability to be
597 * hard-wired to zero for downstream ports
598 */
599 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCAP) &
600 ~PCI_EXP_LNKCAP_CLKPM;
601 break;
602
603 case PCISWCAP_EXP_LNKCTL:
604 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
605 break;
606
607 case PCISWCAP_EXP_SLTCAP:
608 *value = bridge->pcie_sltcap;
609 break;
610
611 case PCISWCAP_EXP_SLTCTL:
612 *value = PCI_EXP_SLTSTA_PDS << 16;
613 break;
614
615 case PCISWCAP_EXP_RTCTL:
616 *value = bridge->pcie_rtctl;
617 break;
618
619 case PCISWCAP_EXP_RTSTA:
620 *value = mvebu_readl(port, PCIE_RC_RTSTA);
621 break;
622
623 /* PCIe requires the v2 fields to be hard-wired to zero */
624 case PCISWCAP_EXP_DEVCAP2:
625 case PCISWCAP_EXP_DEVCTL2:
626 case PCISWCAP_EXP_LNKCAP2:
627 case PCISWCAP_EXP_LNKCTL2:
628 case PCISWCAP_EXP_SLTCAP2:
629 case PCISWCAP_EXP_SLTCTL2:
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200630 default:
Russell King58c19a12015-09-23 18:17:26 +0100631 /*
632 * PCI defines configuration read accesses to reserved or
633 * unimplemented registers to read as zero and complete
634 * normally.
635 */
636 *value = 0;
637 return PCIBIOS_SUCCESSFUL;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200638 }
639
640 if (size == 2)
641 *value = (*value >> (8 * (where & 3))) & 0xffff;
642 else if (size == 1)
643 *value = (*value >> (8 * (where & 3))) & 0xff;
644
645 return PCIBIOS_SUCCESSFUL;
646}
647
648/* Write to the PCI-to-PCI bridge configuration space */
649static int mvebu_sw_pci_bridge_write(struct mvebu_pcie_port *port,
650 unsigned int where, int size, u32 value)
651{
652 struct mvebu_sw_pci_bridge *bridge = &port->bridge;
653 u32 mask, reg;
654 int err;
655
656 if (size == 4)
657 mask = 0x0;
658 else if (size == 2)
659 mask = ~(0xffff << ((where & 3) * 8));
660 else if (size == 1)
661 mask = ~(0xff << ((where & 3) * 8));
662 else
663 return PCIBIOS_BAD_REGISTER_NUMBER;
664
665 err = mvebu_sw_pci_bridge_read(port, where & ~3, 4, &reg);
666 if (err)
667 return err;
668
669 value = (reg & mask) | value << ((where & 3) * 8);
670
671 switch (where & ~3) {
672 case PCI_COMMAND:
Jason Gunthorpe43a16f92013-11-26 11:02:54 -0700673 {
674 u32 old = bridge->command;
675
Jason Gunthorpe641e6742013-11-26 11:02:55 -0700676 if (!mvebu_has_ioport(port))
677 value &= ~PCI_COMMAND_IO;
678
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200679 bridge->command = value & 0xffff;
Jason Gunthorpe43a16f92013-11-26 11:02:54 -0700680 if ((old ^ bridge->command) & PCI_COMMAND_IO)
681 mvebu_pcie_handle_iobase_change(port);
682 if ((old ^ bridge->command) & PCI_COMMAND_MEMORY)
683 mvebu_pcie_handle_membase_change(port);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200684 break;
Jason Gunthorpe43a16f92013-11-26 11:02:54 -0700685 }
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200686
687 case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
688 bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4] = value;
689 break;
690
691 case PCI_IO_BASE:
692 /*
693 * We also keep bit 1 set, it is a read-only bit that
694 * indicates we support 32 bits addressing for the
695 * I/O
696 */
697 bridge->iobase = (value & 0xff) | PCI_IO_RANGE_TYPE_32;
698 bridge->iolimit = ((value >> 8) & 0xff) | PCI_IO_RANGE_TYPE_32;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200699 mvebu_pcie_handle_iobase_change(port);
700 break;
701
702 case PCI_MEMORY_BASE:
703 bridge->membase = value & 0xffff;
704 bridge->memlimit = value >> 16;
705 mvebu_pcie_handle_membase_change(port);
706 break;
707
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200708 case PCI_IO_BASE_UPPER16:
709 bridge->iobaseupper = value & 0xffff;
710 bridge->iolimitupper = value >> 16;
711 mvebu_pcie_handle_iobase_change(port);
712 break;
713
714 case PCI_PRIMARY_BUS:
715 bridge->primary_bus = value & 0xff;
716 bridge->secondary_bus = (value >> 8) & 0xff;
717 bridge->subordinate_bus = (value >> 16) & 0xff;
718 bridge->secondary_latency_timer = (value >> 24) & 0xff;
719 mvebu_pcie_set_local_bus_nr(port, bridge->secondary_bus);
720 break;
721
Russell Kingdc0352a2015-10-03 19:13:33 +0100722 case PCISWCAP_EXP_DEVCTL:
723 /*
724 * Armada370 data says these bits must always
725 * be zero when in root complex mode.
726 */
727 value &= ~(PCI_EXP_DEVCTL_URRE | PCI_EXP_DEVCTL_FERE |
728 PCI_EXP_DEVCTL_NFERE | PCI_EXP_DEVCTL_CERE);
729
730 /*
731 * If the mask is 0xffff0000, then we only want to write
732 * the device control register, rather than clearing the
733 * RW1C bits in the device status register. Mask out the
734 * status register bits.
735 */
736 if (mask == 0xffff0000)
737 value &= 0xffff;
738
739 mvebu_writel(port, value, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL);
740 break;
741
742 case PCISWCAP_EXP_LNKCTL:
743 /*
744 * If we don't support CLKREQ, we must ensure that the
745 * CLKREQ enable bit always reads zero. Since we haven't
746 * had this capability, and it's dependent on board wiring,
747 * disable it for the time being.
748 */
749 value &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
750
751 /*
752 * If the mask is 0xffff0000, then we only want to write
753 * the link control register, rather than clearing the
754 * RW1C bits in the link status register. Mask out the
755 * status register bits.
756 */
757 if (mask == 0xffff0000)
758 value &= 0xffff;
759
760 mvebu_writel(port, value, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
761 break;
762
763 case PCISWCAP_EXP_RTSTA:
764 mvebu_writel(port, value, PCIE_RC_RTSTA);
765 break;
766
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200767 default:
768 break;
769 }
770
771 return PCIBIOS_SUCCESSFUL;
772}
773
774static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
775{
776 return sys->private_data;
777}
778
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400779static struct mvebu_pcie_port *mvebu_pcie_find_port(struct mvebu_pcie *pcie,
780 struct pci_bus *bus,
781 int devfn)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200782{
783 int i;
784
785 for (i = 0; i < pcie->nports; i++) {
786 struct mvebu_pcie_port *port = &pcie->ports[i];
Jingoo Hancf3a9d62014-11-12 12:27:54 +0900787
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200788 if (bus->number == 0 && port->devfn == devfn)
789 return port;
790 if (bus->number != 0 &&
Thomas Petazzoni197fc222013-05-23 16:32:52 +0200791 bus->number >= port->bridge.secondary_bus &&
792 bus->number <= port->bridge.subordinate_bus)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200793 return port;
794 }
795
796 return NULL;
797}
798
799/* PCI configuration space write function */
800static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
801 int where, int size, u32 val)
802{
803 struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
804 struct mvebu_pcie_port *port;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200805 int ret;
806
807 port = mvebu_pcie_find_port(pcie, bus, devfn);
808 if (!port)
809 return PCIBIOS_DEVICE_NOT_FOUND;
810
811 /* Access the emulated PCI-to-PCI bridge */
812 if (bus->number == 0)
813 return mvebu_sw_pci_bridge_write(port, where, size, val);
814
Jason Gunthorpe9f352f02013-10-01 11:58:00 -0600815 if (!mvebu_pcie_link_up(port))
Thomas Petazzoni197fc222013-05-23 16:32:52 +0200816 return PCIBIOS_DEVICE_NOT_FOUND;
817
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200818 /* Access the real PCIe interface */
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200819 ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200820 where, size, val);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200821
822 return ret;
823}
824
825/* PCI configuration space read function */
826static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
827 int size, u32 *val)
828{
829 struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
830 struct mvebu_pcie_port *port;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200831 int ret;
832
833 port = mvebu_pcie_find_port(pcie, bus, devfn);
834 if (!port) {
835 *val = 0xffffffff;
836 return PCIBIOS_DEVICE_NOT_FOUND;
837 }
838
839 /* Access the emulated PCI-to-PCI bridge */
840 if (bus->number == 0)
841 return mvebu_sw_pci_bridge_read(port, where, size, val);
842
Jason Gunthorpe9f352f02013-10-01 11:58:00 -0600843 if (!mvebu_pcie_link_up(port)) {
Thomas Petazzoni197fc222013-05-23 16:32:52 +0200844 *val = 0xffffffff;
845 return PCIBIOS_DEVICE_NOT_FOUND;
846 }
847
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200848 /* Access the real PCIe interface */
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200849 ret = mvebu_pcie_hw_rd_conf(port, bus, devfn,
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200850 where, size, val);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200851
852 return ret;
853}
854
855static struct pci_ops mvebu_pcie_ops = {
856 .read = mvebu_pcie_rd_conf,
857 .write = mvebu_pcie_wr_conf,
858};
859
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200860static int mvebu_pcie_setup(int nr, struct pci_sys_data *sys)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200861{
862 struct mvebu_pcie *pcie = sys_to_pcie(sys);
Bjorn Helgaas6df68f22016-06-06 15:35:39 -0500863 int err, i;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200864
Lorenzo Pieralisi8c7d14742014-11-21 11:29:26 +0000865 pcie->mem.name = "PCI MEM";
866 pcie->realio.name = "PCI I/O";
Jason Gunthorpe2613ba42014-02-12 15:57:08 -0700867
Bjorn Helgaas6df68f22016-06-06 15:35:39 -0500868 if (resource_size(&pcie->realio) != 0)
Jason Gunthorpe641e6742013-11-26 11:02:55 -0700869 pci_add_resource_offset(&sys->resources, &pcie->realio,
870 sys->io_offset);
Bjorn Helgaas6df68f22016-06-06 15:35:39 -0500871
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200872 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
873 pci_add_resource(&sys->resources, &pcie->busn);
874
Bjorn Helgaas6df68f22016-06-06 15:35:39 -0500875 err = devm_request_pci_bus_resources(&pcie->pdev->dev, &sys->resources);
876 if (err)
877 return 0;
878
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200879 for (i = 0; i < pcie->nports; i++) {
880 struct mvebu_pcie_port *port = &pcie->ports[i];
Jingoo Hancf3a9d62014-11-12 12:27:54 +0900881
Ezequiel Garciab22503a2013-07-26 10:17:49 -0300882 if (!port->base)
883 continue;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200884 mvebu_pcie_setup_hw(port);
885 }
886
887 return 1;
888}
889
Jingoo Hanf5072df2013-09-17 14:26:46 +0900890static resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400891 const struct resource *res,
892 resource_size_t start,
893 resource_size_t size,
894 resource_size_t align)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200895{
896 if (dev->bus->number != 0)
897 return start;
898
899 /*
900 * On the PCI-to-PCI bridge side, the I/O windows must have at
Thomas Petazzoni398f5d52014-04-18 14:19:53 +0200901 * least a 64 KB size and the memory windows must have at
902 * least a 1 MB size. Moreover, MBus windows need to have a
903 * base address aligned on their size, and their size must be
904 * a power of two. This means that if the BAR doesn't have a
905 * power of two size, several MBus windows will actually be
906 * created. We need to ensure that the biggest MBus window
907 * (which will be the first one) is aligned on its size, which
908 * explains the rounddown_pow_of_two() being done here.
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200909 */
910 if (res->flags & IORESOURCE_IO)
Thomas Petazzoni398f5d52014-04-18 14:19:53 +0200911 return round_up(start, max_t(resource_size_t, SZ_64K,
912 rounddown_pow_of_two(size)));
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200913 else if (res->flags & IORESOURCE_MEM)
Thomas Petazzoni398f5d52014-04-18 14:19:53 +0200914 return round_up(start, max_t(resource_size_t, SZ_1M,
915 rounddown_pow_of_two(size)));
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200916 else
917 return start;
918}
919
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200920static void mvebu_pcie_enable(struct mvebu_pcie *pcie)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200921{
922 struct hw_pci hw;
923
924 memset(&hw, 0, sizeof(hw));
925
Yijing Wang26914232014-11-11 15:44:17 -0700926#ifdef CONFIG_PCI_MSI
927 hw.msi_ctrl = pcie->msi;
928#endif
929
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200930 hw.nr_controllers = 1;
931 hw.private_data = (void **)&pcie;
932 hw.setup = mvebu_pcie_setup;
Grant Likely16b84e52013-09-19 16:44:55 -0500933 hw.map_irq = of_irq_parse_and_map_pci;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200934 hw.ops = &mvebu_pcie_ops;
935 hw.align_resource = mvebu_pcie_align_resource;
936
Yijing Wang2dead002015-04-28 15:01:35 +0800937 pci_common_init_dev(&pcie->pdev->dev, &hw);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200938}
939
940/*
941 * Looks up the list of register addresses encoded into the reg =
942 * <...> property for one that matches the given port/lane. Once
943 * found, maps it.
944 */
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200945static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400946 struct device_node *np,
947 struct mvebu_pcie_port *port)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200948{
949 struct resource regs;
950 int ret = 0;
951
952 ret = of_address_to_resource(np, 0, &regs);
953 if (ret)
Tushar Beheraf48fbf92013-06-17 14:46:13 +0530954 return ERR_PTR(ret);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200955
Tushar Beheraf48fbf92013-06-17 14:46:13 +0530956 return devm_ioremap_resource(&pdev->dev, &regs);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200957}
958
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300959#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
960#define DT_TYPE_IO 0x1
961#define DT_TYPE_MEM32 0x2
962#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
963#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
964
965static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
Jason Gunthorpe641e6742013-11-26 11:02:55 -0700966 unsigned long type,
967 unsigned int *tgt,
968 unsigned int *attr)
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300969{
970 const int na = 3, ns = 2;
971 const __be32 *range;
972 int rlen, nranges, rangesz, pna, i;
973
Jason Gunthorpe641e6742013-11-26 11:02:55 -0700974 *tgt = -1;
975 *attr = -1;
976
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300977 range = of_get_property(np, "ranges", &rlen);
978 if (!range)
979 return -EINVAL;
980
981 pna = of_n_addr_cells(np);
982 rangesz = pna + na + ns;
983 nranges = rlen / sizeof(__be32) / rangesz;
984
Thomas Petazzoni56fab6e2014-09-17 17:58:27 +0200985 for (i = 0; i < nranges; i++, range += rangesz) {
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300986 u32 flags = of_read_number(range, 1);
Jean-Jacques Hiblot4f4bde12014-02-14 11:46:15 -0700987 u32 slot = of_read_number(range + 1, 1);
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300988 u64 cpuaddr = of_read_number(range + na, pna);
989 unsigned long rtype;
990
991 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
992 rtype = IORESOURCE_IO;
993 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
994 rtype = IORESOURCE_MEM;
Thomas Petazzoni56fab6e2014-09-17 17:58:27 +0200995 else
996 continue;
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300997
998 if (slot == PCI_SLOT(devfn) && type == rtype) {
999 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
1000 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
1001 return 0;
1002 }
Thomas Petazzoni11be6542013-07-26 10:17:48 -03001003 }
1004
1005 return -ENOENT;
1006}
1007
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +02001008static void mvebu_pcie_msi_enable(struct mvebu_pcie *pcie)
Thomas Petazzoni5b4deb62013-08-09 22:27:14 +02001009{
1010 struct device_node *msi_node;
1011
1012 msi_node = of_parse_phandle(pcie->pdev->dev.of_node,
1013 "msi-parent", 0);
1014 if (!msi_node)
1015 return;
1016
1017 pcie->msi = of_pci_find_msi_chip_by_node(msi_node);
Bjorn Helgaas3a107662015-08-04 14:54:04 -05001018 of_node_put(msi_node);
Thomas Petazzoni5b4deb62013-08-09 22:27:14 +02001019
1020 if (pcie->msi)
1021 pcie->msi->dev = &pcie->pdev->dev;
1022}
1023
Jisheng Zhangdfc65352016-03-16 17:59:41 +08001024#ifdef CONFIG_PM_SLEEP
Thomas Petazzoniab14d452015-03-17 15:55:45 +01001025static int mvebu_pcie_suspend(struct device *dev)
1026{
1027 struct mvebu_pcie *pcie;
1028 int i;
1029
1030 pcie = dev_get_drvdata(dev);
1031 for (i = 0; i < pcie->nports; i++) {
1032 struct mvebu_pcie_port *port = pcie->ports + i;
1033 port->saved_pcie_stat = mvebu_readl(port, PCIE_STAT_OFF);
1034 }
1035
1036 return 0;
1037}
1038
1039static int mvebu_pcie_resume(struct device *dev)
1040{
1041 struct mvebu_pcie *pcie;
1042 int i;
1043
1044 pcie = dev_get_drvdata(dev);
1045 for (i = 0; i < pcie->nports; i++) {
1046 struct mvebu_pcie_port *port = pcie->ports + i;
1047 mvebu_writel(port, port->saved_pcie_stat, PCIE_STAT_OFF);
1048 mvebu_pcie_setup_hw(port);
1049 }
1050
1051 return 0;
1052}
Jisheng Zhangdfc65352016-03-16 17:59:41 +08001053#endif
Thomas Petazzoniab14d452015-03-17 15:55:45 +01001054
Russell King37bfa772015-10-03 19:13:02 +01001055static void mvebu_pcie_port_clk_put(void *data)
1056{
1057 struct mvebu_pcie_port *port = data;
1058
1059 clk_put(port->clk);
1060}
1061
Russell King49cb1f72015-10-03 19:12:57 +01001062static int mvebu_pcie_parse_port(struct mvebu_pcie *pcie,
1063 struct mvebu_pcie_port *port, struct device_node *child)
1064{
1065 struct device *dev = &pcie->pdev->dev;
1066 enum of_gpio_flags flags;
Russell King8a182c22015-10-03 19:13:22 +01001067 int reset_gpio, ret;
Russell King49cb1f72015-10-03 19:12:57 +01001068
1069 port->pcie = pcie;
1070
1071 if (of_property_read_u32(child, "marvell,pcie-port", &port->port)) {
1072 dev_warn(dev, "ignoring %s, missing pcie-port property\n",
1073 of_node_full_name(child));
1074 goto skip;
1075 }
1076
1077 if (of_property_read_u32(child, "marvell,pcie-lane", &port->lane))
1078 port->lane = 0;
1079
Russell King37bfa772015-10-03 19:13:02 +01001080 port->name = devm_kasprintf(dev, GFP_KERNEL, "pcie%d.%d", port->port,
1081 port->lane);
1082 if (!port->name) {
1083 ret = -ENOMEM;
1084 goto err;
1085 }
Russell King49cb1f72015-10-03 19:12:57 +01001086
1087 port->devfn = of_pci_get_devfn(child);
1088 if (port->devfn < 0)
1089 goto skip;
1090
1091 ret = mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_MEM,
1092 &port->mem_target, &port->mem_attr);
1093 if (ret < 0) {
1094 dev_err(dev, "%s: cannot get tgt/attr for mem window\n",
1095 port->name);
1096 goto skip;
1097 }
1098
Russell King37bfa772015-10-03 19:13:02 +01001099 if (resource_size(&pcie->io) != 0) {
Russell King49cb1f72015-10-03 19:12:57 +01001100 mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_IO,
1101 &port->io_target, &port->io_attr);
Russell King37bfa772015-10-03 19:13:02 +01001102 } else {
Russell King49cb1f72015-10-03 19:12:57 +01001103 port->io_target = -1;
1104 port->io_attr = -1;
1105 }
1106
Russell King8a182c22015-10-03 19:13:22 +01001107 reset_gpio = of_get_named_gpio_flags(child, "reset-gpios", 0, &flags);
1108 if (reset_gpio == -EPROBE_DEFER) {
1109 ret = reset_gpio;
Russell King37bfa772015-10-03 19:13:02 +01001110 goto err;
1111 }
1112
Russell King8a182c22015-10-03 19:13:22 +01001113 if (gpio_is_valid(reset_gpio)) {
1114 unsigned long gpio_flags;
1115
Russell King37bfa772015-10-03 19:13:02 +01001116 port->reset_name = devm_kasprintf(dev, GFP_KERNEL, "%s-reset",
1117 port->name);
1118 if (!port->reset_name) {
1119 ret = -ENOMEM;
1120 goto err;
1121 }
Russell King49cb1f72015-10-03 19:12:57 +01001122
Russell King8a182c22015-10-03 19:13:22 +01001123 if (flags & OF_GPIO_ACTIVE_LOW) {
1124 dev_info(dev, "%s: reset gpio is active low\n",
1125 of_node_full_name(child));
1126 gpio_flags = GPIOF_ACTIVE_LOW |
1127 GPIOF_OUT_INIT_LOW;
1128 } else {
1129 gpio_flags = GPIOF_OUT_INIT_HIGH;
1130 }
1131
1132 ret = devm_gpio_request_one(dev, reset_gpio, gpio_flags,
1133 port->reset_name);
Russell King49cb1f72015-10-03 19:12:57 +01001134 if (ret) {
1135 if (ret == -EPROBE_DEFER)
1136 goto err;
1137 goto skip;
1138 }
Russell King8a182c22015-10-03 19:13:22 +01001139
1140 port->reset_gpio = gpio_to_desc(reset_gpio);
Russell King49cb1f72015-10-03 19:12:57 +01001141 }
1142
1143 port->clk = of_clk_get_by_name(child, NULL);
1144 if (IS_ERR(port->clk)) {
1145 dev_err(dev, "%s: cannot get clock\n", port->name);
1146 goto skip;
1147 }
1148
Russell King37bfa772015-10-03 19:13:02 +01001149 ret = devm_add_action(dev, mvebu_pcie_port_clk_put, port);
1150 if (ret < 0) {
1151 clk_put(port->clk);
1152 goto err;
1153 }
1154
Russell King49cb1f72015-10-03 19:12:57 +01001155 return 1;
1156
1157skip:
1158 ret = 0;
Russell King37bfa772015-10-03 19:13:02 +01001159
1160 /* In the case of skipping, we need to free these */
1161 devm_kfree(dev, port->reset_name);
1162 port->reset_name = NULL;
1163 devm_kfree(dev, port->name);
1164 port->name = NULL;
1165
Russell King49cb1f72015-10-03 19:12:57 +01001166err:
1167 return ret;
1168}
1169
Russell Kingd609a8d2015-10-03 19:13:27 +01001170/*
1171 * Power up a PCIe port. PCIe requires the refclk to be stable for 100µs
1172 * prior to releasing PERST. See table 2-4 in section 2.6.2 AC Specifications
1173 * of the PCI Express Card Electromechanical Specification, 1.1.
1174 */
1175static int mvebu_pcie_powerup(struct mvebu_pcie_port *port)
1176{
1177 int ret;
1178
1179 ret = clk_prepare_enable(port->clk);
1180 if (ret < 0)
1181 return ret;
1182
1183 if (port->reset_gpio) {
1184 u32 reset_udelay = 20000;
1185
1186 of_property_read_u32(port->dn, "reset-delay-us",
1187 &reset_udelay);
1188
1189 udelay(100);
1190
1191 gpiod_set_value_cansleep(port->reset_gpio, 0);
1192 msleep(reset_udelay / 1000);
1193 }
1194
1195 return 0;
1196}
1197
1198/*
1199 * Power down a PCIe port. Strictly, PCIe requires us to place the card
1200 * in D3hot state before asserting PERST#.
1201 */
1202static void mvebu_pcie_powerdown(struct mvebu_pcie_port *port)
1203{
1204 if (port->reset_gpio)
1205 gpiod_set_value_cansleep(port->reset_gpio, 1);
1206
1207 clk_disable_unprepare(port->clk);
1208}
1209
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +02001210static int mvebu_pcie_probe(struct platform_device *pdev)
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001211{
Bjorn Helgaas160b4e42016-10-06 13:38:58 -05001212 struct device *dev = &pdev->dev;
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001213 struct mvebu_pcie *pcie;
Bjorn Helgaas160b4e42016-10-06 13:38:58 -05001214 struct device_node *np = dev->of_node;
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001215 struct device_node *child;
Russell King7de36cd2015-09-23 18:17:37 +01001216 int num, i, ret;
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001217
Bjorn Helgaas160b4e42016-10-06 13:38:58 -05001218 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001219 if (!pcie)
1220 return -ENOMEM;
1221
1222 pcie->pdev = pdev;
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +02001223 platform_set_drvdata(pdev, pcie);
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001224
Thomas Petazzoni11be6542013-07-26 10:17:48 -03001225 /* Get the PCIe memory and I/O aperture */
1226 mvebu_mbus_get_pcie_mem_aperture(&pcie->mem);
1227 if (resource_size(&pcie->mem) == 0) {
Bjorn Helgaas160b4e42016-10-06 13:38:58 -05001228 dev_err(dev, "invalid memory aperture size\n");
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001229 return -EINVAL;
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001230 }
1231
Thomas Petazzoni11be6542013-07-26 10:17:48 -03001232 mvebu_mbus_get_pcie_io_aperture(&pcie->io);
Thomas Petazzoni11be6542013-07-26 10:17:48 -03001233
Jason Gunthorpe641e6742013-11-26 11:02:55 -07001234 if (resource_size(&pcie->io) != 0) {
1235 pcie->realio.flags = pcie->io.flags;
1236 pcie->realio.start = PCIBIOS_MIN_IO;
1237 pcie->realio.end = min_t(resource_size_t,
1238 IO_SPACE_LIMIT,
Thomas Petazzonie93d8212018-08-03 16:38:44 +02001239 resource_size(&pcie->io) - 1);
Jason Gunthorpe641e6742013-11-26 11:02:55 -07001240 } else
1241 pcie->realio = pcie->io;
Thomas Petazzoni11be6542013-07-26 10:17:48 -03001242
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001243 /* Get the bus range */
1244 ret = of_pci_parse_bus_range(np, &pcie->busn);
1245 if (ret) {
Bjorn Helgaas160b4e42016-10-06 13:38:58 -05001246 dev_err(dev, "failed to parse bus-range property: %d\n", ret);
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001247 return ret;
1248 }
1249
Bjorn Helgaas96f61702016-10-11 23:19:05 -05001250 num = of_get_available_child_count(np);
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001251
Bjorn Helgaas160b4e42016-10-06 13:38:58 -05001252 pcie->ports = devm_kcalloc(dev, num, sizeof(*pcie->ports), GFP_KERNEL);
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001253 if (!pcie->ports)
1254 return -ENOMEM;
1255
1256 i = 0;
Bjorn Helgaas96f61702016-10-11 23:19:05 -05001257 for_each_available_child_of_node(np, child) {
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001258 struct mvebu_pcie_port *port = &pcie->ports[i];
1259
Russell King49cb1f72015-10-03 19:12:57 +01001260 ret = mvebu_pcie_parse_port(pcie, port, child);
Russell King37bfa772015-10-03 19:13:02 +01001261 if (ret < 0) {
1262 of_node_put(child);
Russell King49cb1f72015-10-03 19:12:57 +01001263 return ret;
Russell King37bfa772015-10-03 19:13:02 +01001264 } else if (ret == 0) {
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001265 continue;
Russell King37bfa772015-10-03 19:13:02 +01001266 }
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001267
Russell King3884d842015-10-03 19:13:07 +01001268 port->dn = child;
1269 i++;
1270 }
1271 pcie->nports = i;
1272
1273 for (i = 0; i < pcie->nports; i++) {
1274 struct mvebu_pcie_port *port = &pcie->ports[i];
1275
1276 child = port->dn;
1277 if (!child)
1278 continue;
1279
Russell Kingd609a8d2015-10-03 19:13:27 +01001280 ret = mvebu_pcie_powerup(port);
1281 if (ret < 0)
Sebastian Hesselbarthb42285f2013-08-13 14:25:20 +02001282 continue;
1283
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001284 port->base = mvebu_pcie_map_registers(pdev, child, port);
Tushar Beheraf48fbf92013-06-17 14:46:13 +05301285 if (IS_ERR(port->base)) {
Bjorn Helgaas160b4e42016-10-06 13:38:58 -05001286 dev_err(dev, "%s: cannot map registers\n", port->name);
Tushar Beheraf48fbf92013-06-17 14:46:13 +05301287 port->base = NULL;
Russell Kingd609a8d2015-10-03 19:13:27 +01001288 mvebu_pcie_powerdown(port);
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001289 continue;
1290 }
1291
Thomas Petazzonif4ac9902013-05-23 16:32:51 +02001292 mvebu_pcie_set_local_dev_nr(port, 1);
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001293 mvebu_sw_pci_bridge_init(port);
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001294 }
1295
Sebastian Hesselbarthbf09b6a2013-08-13 14:25:21 +02001296 pcie->nports = i;
Thomas Petazzoni31e45ec2013-12-26 16:52:41 +01001297
1298 for (i = 0; i < (IO_SPACE_LIMIT - SZ_64K); i += SZ_64K)
1299 pci_ioremap_io(i, pcie->io.start + i);
1300
Thomas Petazzoni5b4deb62013-08-09 22:27:14 +02001301 mvebu_pcie_msi_enable(pcie);
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001302 mvebu_pcie_enable(pcie);
1303
Thomas Petazzoniab14d452015-03-17 15:55:45 +01001304 platform_set_drvdata(pdev, pcie);
1305
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001306 return 0;
1307}
1308
1309static const struct of_device_id mvebu_pcie_of_match_table[] = {
1310 { .compatible = "marvell,armada-xp-pcie", },
1311 { .compatible = "marvell,armada-370-pcie", },
Sebastian Hesselbarthcc54ccd2013-08-13 14:25:24 +02001312 { .compatible = "marvell,dove-pcie", },
Thomas Petazzoni005625f2013-05-15 15:36:54 +02001313 { .compatible = "marvell,kirkwood-pcie", },
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001314 {},
1315};
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001316
Jisheng Zhang6e9a4262016-03-16 17:59:40 +08001317static const struct dev_pm_ops mvebu_pcie_pm_ops = {
Jisheng Zhangdfc65352016-03-16 17:59:41 +08001318 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mvebu_pcie_suspend, mvebu_pcie_resume)
Thomas Petazzoniab14d452015-03-17 15:55:45 +01001319};
1320
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001321static struct platform_driver mvebu_pcie_driver = {
1322 .driver = {
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001323 .name = "mvebu-pcie",
Sachin Kamat339135f2013-12-19 14:34:59 +05301324 .of_match_table = mvebu_pcie_of_match_table,
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +02001325 /* driver unloading/unbinding currently not supported */
1326 .suppress_bind_attrs = true,
Thomas Petazzoniab14d452015-03-17 15:55:45 +01001327 .pm = &mvebu_pcie_pm_ops,
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001328 },
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +02001329 .probe = mvebu_pcie_probe,
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001330};
Paul Gortmaker82641d92016-07-02 19:13:28 -04001331builtin_platform_driver(mvebu_pcie_driver);