blob: 77f8a7c5859771aaad870d36d362411155731bcb [file] [log] [blame]
Thomas Petazzoni45361a42013-05-16 17:55:22 +02001/*
2 * PCIe driver for Marvell Armada 370 and Armada XP SoCs
3 *
4 * This file is licensed under the terms of the GNU General Public
5 * License version 2. This program is licensed "as is" without any
6 * warranty of any kind, whether express or implied.
7 */
8
9#include <linux/kernel.h>
10#include <linux/pci.h>
11#include <linux/clk.h>
Sebastian Hesselbarth52ba9922013-08-13 14:25:23 +020012#include <linux/delay.h>
13#include <linux/gpio.h>
Thomas Petazzoni45361a42013-05-16 17:55:22 +020014#include <linux/module.h>
15#include <linux/mbus.h>
Thomas Petazzoni5b4deb62013-08-09 22:27:14 +020016#include <linux/msi.h>
Thomas Petazzoni45361a42013-05-16 17:55:22 +020017#include <linux/slab.h>
18#include <linux/platform_device.h>
19#include <linux/of_address.h>
Thomas Petazzoni45361a42013-05-16 17:55:22 +020020#include <linux/of_irq.h>
Sebastian Hesselbarth52ba9922013-08-13 14:25:23 +020021#include <linux/of_gpio.h>
22#include <linux/of_pci.h>
Thomas Petazzoni45361a42013-05-16 17:55:22 +020023#include <linux/of_platform.h>
24
25/*
26 * PCIe unit register offsets.
27 */
28#define PCIE_DEV_ID_OFF 0x0000
29#define PCIE_CMD_OFF 0x0004
30#define PCIE_DEV_REV_OFF 0x0008
31#define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
32#define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
33#define PCIE_HEADER_LOG_4_OFF 0x0128
34#define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
35#define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
36#define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
37#define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
38#define PCIE_WIN5_CTRL_OFF 0x1880
39#define PCIE_WIN5_BASE_OFF 0x1884
40#define PCIE_WIN5_REMAP_OFF 0x188c
41#define PCIE_CONF_ADDR_OFF 0x18f8
42#define PCIE_CONF_ADDR_EN 0x80000000
43#define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
44#define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
45#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
46#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
47#define PCIE_CONF_ADDR(bus, devfn, where) \
48 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
49 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \
50 PCIE_CONF_ADDR_EN)
51#define PCIE_CONF_DATA_OFF 0x18fc
52#define PCIE_MASK_OFF 0x1910
53#define PCIE_MASK_ENABLE_INTS 0x0f000000
54#define PCIE_CTRL_OFF 0x1a00
55#define PCIE_CTRL_X1_MODE 0x0001
56#define PCIE_STAT_OFF 0x1a04
57#define PCIE_STAT_BUS 0xff00
Thomas Petazzonif4ac9902013-05-23 16:32:51 +020058#define PCIE_STAT_DEV 0x1f0000
Thomas Petazzoni45361a42013-05-16 17:55:22 +020059#define PCIE_STAT_LINK_DOWN BIT(0)
60#define PCIE_DEBUG_CTRL 0x1a60
61#define PCIE_DEBUG_SOFT_RESET BIT(20)
62
63/*
64 * This product ID is registered by Marvell, and used when the Marvell
65 * SoC is not the root complex, but an endpoint on the PCIe bus. It is
66 * therefore safe to re-use this PCI ID for our emulated PCI-to-PCI
67 * bridge.
68 */
69#define MARVELL_EMULATED_PCI_PCI_BRIDGE_ID 0x7846
70
71/* PCI configuration space of a PCI-to-PCI bridge */
72struct mvebu_sw_pci_bridge {
73 u16 vendor;
74 u16 device;
75 u16 command;
Thomas Petazzoni45361a42013-05-16 17:55:22 +020076 u16 class;
77 u8 interface;
78 u8 revision;
79 u8 bist;
80 u8 header_type;
81 u8 latency_timer;
82 u8 cache_line_size;
83 u32 bar[2];
84 u8 primary_bus;
85 u8 secondary_bus;
86 u8 subordinate_bus;
87 u8 secondary_latency_timer;
88 u8 iobase;
89 u8 iolimit;
90 u16 secondary_status;
91 u16 membase;
92 u16 memlimit;
Thomas Petazzoni45361a42013-05-16 17:55:22 +020093 u16 iobaseupper;
94 u16 iolimitupper;
95 u8 cappointer;
96 u8 reserved1;
97 u16 reserved2;
98 u32 romaddr;
99 u8 intline;
100 u8 intpin;
101 u16 bridgectrl;
102};
103
104struct mvebu_pcie_port;
105
106/* Structure representing all PCIe interfaces */
107struct mvebu_pcie {
108 struct platform_device *pdev;
109 struct mvebu_pcie_port *ports;
Thomas Petazzoni5b4deb62013-08-09 22:27:14 +0200110 struct msi_chip *msi;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200111 struct resource io;
112 struct resource realio;
113 struct resource mem;
114 struct resource busn;
115 int nports;
116};
117
118/* Structure representing one PCIe interface */
119struct mvebu_pcie_port {
120 char *name;
121 void __iomem *base;
122 spinlock_t conf_lock;
123 int haslink;
124 u32 port;
125 u32 lane;
126 int devfn;
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300127 unsigned int mem_target;
128 unsigned int mem_attr;
129 unsigned int io_target;
130 unsigned int io_attr;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200131 struct clk *clk;
Sebastian Hesselbarth52ba9922013-08-13 14:25:23 +0200132 int reset_gpio;
133 int reset_active_low;
134 char *reset_name;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200135 struct mvebu_sw_pci_bridge bridge;
136 struct device_node *dn;
137 struct mvebu_pcie *pcie;
138 phys_addr_t memwin_base;
139 size_t memwin_size;
140 phys_addr_t iowin_base;
141 size_t iowin_size;
142};
143
144static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port)
145{
146 return !(readl(port->base + PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
147}
148
149static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
150{
151 u32 stat;
152
153 stat = readl(port->base + PCIE_STAT_OFF);
154 stat &= ~PCIE_STAT_BUS;
155 stat |= nr << 8;
156 writel(stat, port->base + PCIE_STAT_OFF);
157}
158
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200159static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr)
160{
161 u32 stat;
162
163 stat = readl(port->base + PCIE_STAT_OFF);
164 stat &= ~PCIE_STAT_DEV;
165 stat |= nr << 16;
166 writel(stat, port->base + PCIE_STAT_OFF);
167}
168
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200169/*
170 * Setup PCIE BARs and Address Decode Wins:
171 * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
172 * WIN[0-3] -> DRAM bank[0-3]
173 */
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200174static void mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200175{
176 const struct mbus_dram_target_info *dram;
177 u32 size;
178 int i;
179
180 dram = mv_mbus_dram_info();
181
182 /* First, disable and clear BARs and windows. */
183 for (i = 1; i < 3; i++) {
184 writel(0, port->base + PCIE_BAR_CTRL_OFF(i));
185 writel(0, port->base + PCIE_BAR_LO_OFF(i));
186 writel(0, port->base + PCIE_BAR_HI_OFF(i));
187 }
188
189 for (i = 0; i < 5; i++) {
190 writel(0, port->base + PCIE_WIN04_CTRL_OFF(i));
191 writel(0, port->base + PCIE_WIN04_BASE_OFF(i));
192 writel(0, port->base + PCIE_WIN04_REMAP_OFF(i));
193 }
194
195 writel(0, port->base + PCIE_WIN5_CTRL_OFF);
196 writel(0, port->base + PCIE_WIN5_BASE_OFF);
197 writel(0, port->base + PCIE_WIN5_REMAP_OFF);
198
199 /* Setup windows for DDR banks. Count total DDR size on the fly. */
200 size = 0;
201 for (i = 0; i < dram->num_cs; i++) {
202 const struct mbus_dram_window *cs = dram->cs + i;
203
204 writel(cs->base & 0xffff0000,
205 port->base + PCIE_WIN04_BASE_OFF(i));
206 writel(0, port->base + PCIE_WIN04_REMAP_OFF(i));
207 writel(((cs->size - 1) & 0xffff0000) |
208 (cs->mbus_attr << 8) |
209 (dram->mbus_dram_target_id << 4) | 1,
210 port->base + PCIE_WIN04_CTRL_OFF(i));
211
212 size += cs->size;
213 }
214
215 /* Round up 'size' to the nearest power of two. */
216 if ((size & (size - 1)) != 0)
217 size = 1 << fls(size);
218
219 /* Setup BAR[1] to all DRAM banks. */
220 writel(dram->cs[0].base, port->base + PCIE_BAR_LO_OFF(1));
221 writel(0, port->base + PCIE_BAR_HI_OFF(1));
222 writel(((size - 1) & 0xffff0000) | 1,
223 port->base + PCIE_BAR_CTRL_OFF(1));
224}
225
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200226static void mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200227{
228 u16 cmd;
229 u32 mask;
230
231 /* Point PCIe unit MBUS decode windows to DRAM space. */
232 mvebu_pcie_setup_wins(port);
233
234 /* Master + slave enable. */
235 cmd = readw(port->base + PCIE_CMD_OFF);
236 cmd |= PCI_COMMAND_IO;
237 cmd |= PCI_COMMAND_MEMORY;
238 cmd |= PCI_COMMAND_MASTER;
239 writew(cmd, port->base + PCIE_CMD_OFF);
240
241 /* Enable interrupt lines A-D. */
242 mask = readl(port->base + PCIE_MASK_OFF);
243 mask |= PCIE_MASK_ENABLE_INTS;
244 writel(mask, port->base + PCIE_MASK_OFF);
245}
246
247static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
248 struct pci_bus *bus,
249 u32 devfn, int where, int size, u32 *val)
250{
251 writel(PCIE_CONF_ADDR(bus->number, devfn, where),
252 port->base + PCIE_CONF_ADDR_OFF);
253
254 *val = readl(port->base + PCIE_CONF_DATA_OFF);
255
256 if (size == 1)
257 *val = (*val >> (8 * (where & 3))) & 0xff;
258 else if (size == 2)
259 *val = (*val >> (8 * (where & 3))) & 0xffff;
260
261 return PCIBIOS_SUCCESSFUL;
262}
263
264static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
265 struct pci_bus *bus,
266 u32 devfn, int where, int size, u32 val)
267{
268 int ret = PCIBIOS_SUCCESSFUL;
269
270 writel(PCIE_CONF_ADDR(bus->number, devfn, where),
271 port->base + PCIE_CONF_ADDR_OFF);
272
273 if (size == 4)
274 writel(val, port->base + PCIE_CONF_DATA_OFF);
275 else if (size == 2)
276 writew(val, port->base + PCIE_CONF_DATA_OFF + (where & 3));
277 else if (size == 1)
278 writeb(val, port->base + PCIE_CONF_DATA_OFF + (where & 3));
279 else
280 ret = PCIBIOS_BAD_REGISTER_NUMBER;
281
282 return ret;
283}
284
285static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
286{
287 phys_addr_t iobase;
288
289 /* Are the new iobase/iolimit values invalid? */
290 if (port->bridge.iolimit < port->bridge.iobase ||
291 port->bridge.iolimitupper < port->bridge.iobaseupper) {
292
293 /* If a window was configured, remove it */
294 if (port->iowin_base) {
295 mvebu_mbus_del_window(port->iowin_base,
296 port->iowin_size);
297 port->iowin_base = 0;
298 port->iowin_size = 0;
299 }
300
301 return;
302 }
303
304 /*
305 * We read the PCI-to-PCI bridge emulated registers, and
306 * calculate the base address and size of the address decoding
307 * window to setup, according to the PCI-to-PCI bridge
308 * specifications. iobase is the bus address, port->iowin_base
309 * is the CPU address.
310 */
311 iobase = ((port->bridge.iobase & 0xF0) << 8) |
312 (port->bridge.iobaseupper << 16);
313 port->iowin_base = port->pcie->io.start + iobase;
314 port->iowin_size = ((0xFFF | ((port->bridge.iolimit & 0xF0) << 8) |
315 (port->bridge.iolimitupper << 16)) -
316 iobase);
317
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300318 mvebu_mbus_add_window_remap_by_id(port->io_target, port->io_attr,
319 port->iowin_base, port->iowin_size,
320 iobase);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200321
322 pci_ioremap_io(iobase, port->iowin_base);
323}
324
325static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
326{
327 /* Are the new membase/memlimit values invalid? */
328 if (port->bridge.memlimit < port->bridge.membase) {
329
330 /* If a window was configured, remove it */
331 if (port->memwin_base) {
332 mvebu_mbus_del_window(port->memwin_base,
333 port->memwin_size);
334 port->memwin_base = 0;
335 port->memwin_size = 0;
336 }
337
338 return;
339 }
340
341 /*
342 * We read the PCI-to-PCI bridge emulated registers, and
343 * calculate the base address and size of the address decoding
344 * window to setup, according to the PCI-to-PCI bridge
345 * specifications.
346 */
347 port->memwin_base = ((port->bridge.membase & 0xFFF0) << 16);
348 port->memwin_size =
349 (((port->bridge.memlimit & 0xFFF0) << 16) | 0xFFFFF) -
350 port->memwin_base;
351
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300352 mvebu_mbus_add_window_by_id(port->mem_target, port->mem_attr,
353 port->memwin_base, port->memwin_size);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200354}
355
356/*
357 * Initialize the configuration space of the PCI-to-PCI bridge
358 * associated with the given PCIe interface.
359 */
360static void mvebu_sw_pci_bridge_init(struct mvebu_pcie_port *port)
361{
362 struct mvebu_sw_pci_bridge *bridge = &port->bridge;
363
364 memset(bridge, 0, sizeof(struct mvebu_sw_pci_bridge));
365
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200366 bridge->class = PCI_CLASS_BRIDGE_PCI;
367 bridge->vendor = PCI_VENDOR_ID_MARVELL;
368 bridge->device = MARVELL_EMULATED_PCI_PCI_BRIDGE_ID;
369 bridge->header_type = PCI_HEADER_TYPE_BRIDGE;
370 bridge->cache_line_size = 0x10;
371
372 /* We support 32 bits I/O addressing */
373 bridge->iobase = PCI_IO_RANGE_TYPE_32;
374 bridge->iolimit = PCI_IO_RANGE_TYPE_32;
375}
376
377/*
378 * Read the configuration space of the PCI-to-PCI bridge associated to
379 * the given PCIe interface.
380 */
381static int mvebu_sw_pci_bridge_read(struct mvebu_pcie_port *port,
382 unsigned int where, int size, u32 *value)
383{
384 struct mvebu_sw_pci_bridge *bridge = &port->bridge;
385
386 switch (where & ~3) {
387 case PCI_VENDOR_ID:
388 *value = bridge->device << 16 | bridge->vendor;
389 break;
390
391 case PCI_COMMAND:
Thomas Petazzoni6eb237c2013-05-23 16:32:53 +0200392 *value = bridge->command;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200393 break;
394
395 case PCI_CLASS_REVISION:
396 *value = bridge->class << 16 | bridge->interface << 8 |
397 bridge->revision;
398 break;
399
400 case PCI_CACHE_LINE_SIZE:
401 *value = bridge->bist << 24 | bridge->header_type << 16 |
402 bridge->latency_timer << 8 | bridge->cache_line_size;
403 break;
404
405 case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
406 *value = bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4];
407 break;
408
409 case PCI_PRIMARY_BUS:
410 *value = (bridge->secondary_latency_timer << 24 |
411 bridge->subordinate_bus << 16 |
412 bridge->secondary_bus << 8 |
413 bridge->primary_bus);
414 break;
415
416 case PCI_IO_BASE:
417 *value = (bridge->secondary_status << 16 |
418 bridge->iolimit << 8 |
419 bridge->iobase);
420 break;
421
422 case PCI_MEMORY_BASE:
423 *value = (bridge->memlimit << 16 | bridge->membase);
424 break;
425
426 case PCI_PREF_MEMORY_BASE:
Thomas Petazzoni36dd1f32013-08-01 15:44:19 +0200427 *value = 0;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200428 break;
429
430 case PCI_IO_BASE_UPPER16:
431 *value = (bridge->iolimitupper << 16 | bridge->iobaseupper);
432 break;
433
434 case PCI_ROM_ADDRESS1:
435 *value = 0;
436 break;
437
438 default:
439 *value = 0xffffffff;
440 return PCIBIOS_BAD_REGISTER_NUMBER;
441 }
442
443 if (size == 2)
444 *value = (*value >> (8 * (where & 3))) & 0xffff;
445 else if (size == 1)
446 *value = (*value >> (8 * (where & 3))) & 0xff;
447
448 return PCIBIOS_SUCCESSFUL;
449}
450
451/* Write to the PCI-to-PCI bridge configuration space */
452static int mvebu_sw_pci_bridge_write(struct mvebu_pcie_port *port,
453 unsigned int where, int size, u32 value)
454{
455 struct mvebu_sw_pci_bridge *bridge = &port->bridge;
456 u32 mask, reg;
457 int err;
458
459 if (size == 4)
460 mask = 0x0;
461 else if (size == 2)
462 mask = ~(0xffff << ((where & 3) * 8));
463 else if (size == 1)
464 mask = ~(0xff << ((where & 3) * 8));
465 else
466 return PCIBIOS_BAD_REGISTER_NUMBER;
467
468 err = mvebu_sw_pci_bridge_read(port, where & ~3, 4, &reg);
469 if (err)
470 return err;
471
472 value = (reg & mask) | value << ((where & 3) * 8);
473
474 switch (where & ~3) {
475 case PCI_COMMAND:
476 bridge->command = value & 0xffff;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200477 break;
478
479 case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
480 bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4] = value;
481 break;
482
483 case PCI_IO_BASE:
484 /*
485 * We also keep bit 1 set, it is a read-only bit that
486 * indicates we support 32 bits addressing for the
487 * I/O
488 */
489 bridge->iobase = (value & 0xff) | PCI_IO_RANGE_TYPE_32;
490 bridge->iolimit = ((value >> 8) & 0xff) | PCI_IO_RANGE_TYPE_32;
491 bridge->secondary_status = value >> 16;
492 mvebu_pcie_handle_iobase_change(port);
493 break;
494
495 case PCI_MEMORY_BASE:
496 bridge->membase = value & 0xffff;
497 bridge->memlimit = value >> 16;
498 mvebu_pcie_handle_membase_change(port);
499 break;
500
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200501 case PCI_IO_BASE_UPPER16:
502 bridge->iobaseupper = value & 0xffff;
503 bridge->iolimitupper = value >> 16;
504 mvebu_pcie_handle_iobase_change(port);
505 break;
506
507 case PCI_PRIMARY_BUS:
508 bridge->primary_bus = value & 0xff;
509 bridge->secondary_bus = (value >> 8) & 0xff;
510 bridge->subordinate_bus = (value >> 16) & 0xff;
511 bridge->secondary_latency_timer = (value >> 24) & 0xff;
512 mvebu_pcie_set_local_bus_nr(port, bridge->secondary_bus);
513 break;
514
515 default:
516 break;
517 }
518
519 return PCIBIOS_SUCCESSFUL;
520}
521
522static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
523{
524 return sys->private_data;
525}
526
527static struct mvebu_pcie_port *
528mvebu_pcie_find_port(struct mvebu_pcie *pcie, struct pci_bus *bus,
529 int devfn)
530{
531 int i;
532
533 for (i = 0; i < pcie->nports; i++) {
534 struct mvebu_pcie_port *port = &pcie->ports[i];
535 if (bus->number == 0 && port->devfn == devfn)
536 return port;
537 if (bus->number != 0 &&
Thomas Petazzoni197fc222013-05-23 16:32:52 +0200538 bus->number >= port->bridge.secondary_bus &&
539 bus->number <= port->bridge.subordinate_bus)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200540 return port;
541 }
542
543 return NULL;
544}
545
546/* PCI configuration space write function */
547static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
548 int where, int size, u32 val)
549{
550 struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
551 struct mvebu_pcie_port *port;
552 unsigned long flags;
553 int ret;
554
555 port = mvebu_pcie_find_port(pcie, bus, devfn);
556 if (!port)
557 return PCIBIOS_DEVICE_NOT_FOUND;
558
559 /* Access the emulated PCI-to-PCI bridge */
560 if (bus->number == 0)
561 return mvebu_sw_pci_bridge_write(port, where, size, val);
562
Thomas Petazzoni197fc222013-05-23 16:32:52 +0200563 if (!port->haslink)
564 return PCIBIOS_DEVICE_NOT_FOUND;
565
566 /*
567 * On the secondary bus, we don't want to expose any other
568 * device than the device physically connected in the PCIe
569 * slot, visible in slot 0. In slot 1, there's a special
570 * Marvell device that only makes sense when the Armada is
571 * used as a PCIe endpoint.
572 */
573 if (bus->number == port->bridge.secondary_bus &&
574 PCI_SLOT(devfn) != 0)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200575 return PCIBIOS_DEVICE_NOT_FOUND;
576
577 /* Access the real PCIe interface */
578 spin_lock_irqsave(&port->conf_lock, flags);
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200579 ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200580 where, size, val);
581 spin_unlock_irqrestore(&port->conf_lock, flags);
582
583 return ret;
584}
585
586/* PCI configuration space read function */
587static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
588 int size, u32 *val)
589{
590 struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
591 struct mvebu_pcie_port *port;
592 unsigned long flags;
593 int ret;
594
595 port = mvebu_pcie_find_port(pcie, bus, devfn);
596 if (!port) {
597 *val = 0xffffffff;
598 return PCIBIOS_DEVICE_NOT_FOUND;
599 }
600
601 /* Access the emulated PCI-to-PCI bridge */
602 if (bus->number == 0)
603 return mvebu_sw_pci_bridge_read(port, where, size, val);
604
Thomas Petazzoni197fc222013-05-23 16:32:52 +0200605 if (!port->haslink) {
606 *val = 0xffffffff;
607 return PCIBIOS_DEVICE_NOT_FOUND;
608 }
609
610 /*
611 * On the secondary bus, we don't want to expose any other
612 * device than the device physically connected in the PCIe
613 * slot, visible in slot 0. In slot 1, there's a special
614 * Marvell device that only makes sense when the Armada is
615 * used as a PCIe endpoint.
616 */
617 if (bus->number == port->bridge.secondary_bus &&
618 PCI_SLOT(devfn) != 0) {
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200619 *val = 0xffffffff;
620 return PCIBIOS_DEVICE_NOT_FOUND;
621 }
622
623 /* Access the real PCIe interface */
624 spin_lock_irqsave(&port->conf_lock, flags);
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200625 ret = mvebu_pcie_hw_rd_conf(port, bus, devfn,
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200626 where, size, val);
627 spin_unlock_irqrestore(&port->conf_lock, flags);
628
629 return ret;
630}
631
632static struct pci_ops mvebu_pcie_ops = {
633 .read = mvebu_pcie_rd_conf,
634 .write = mvebu_pcie_wr_conf,
635};
636
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200637static int mvebu_pcie_setup(int nr, struct pci_sys_data *sys)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200638{
639 struct mvebu_pcie *pcie = sys_to_pcie(sys);
640 int i;
641
642 pci_add_resource_offset(&sys->resources, &pcie->realio, sys->io_offset);
643 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
644 pci_add_resource(&sys->resources, &pcie->busn);
645
646 for (i = 0; i < pcie->nports; i++) {
647 struct mvebu_pcie_port *port = &pcie->ports[i];
Ezequiel Garciab22503a2013-07-26 10:17:49 -0300648 if (!port->base)
649 continue;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200650 mvebu_pcie_setup_hw(port);
651 }
652
653 return 1;
654}
655
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200656static int mvebu_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200657{
658 struct of_irq oirq;
659 int ret;
660
661 ret = of_irq_map_pci(dev, &oirq);
662 if (ret)
663 return ret;
664
665 return irq_create_of_mapping(oirq.controller, oirq.specifier,
666 oirq.size);
667}
668
669static struct pci_bus *mvebu_pcie_scan_bus(int nr, struct pci_sys_data *sys)
670{
671 struct mvebu_pcie *pcie = sys_to_pcie(sys);
672 struct pci_bus *bus;
673
674 bus = pci_create_root_bus(&pcie->pdev->dev, sys->busnr,
675 &mvebu_pcie_ops, sys, &sys->resources);
676 if (!bus)
677 return NULL;
678
679 pci_scan_child_bus(bus);
680
681 return bus;
682}
683
Thomas Petazzoni5b4deb62013-08-09 22:27:14 +0200684void mvebu_pcie_add_bus(struct pci_bus *bus)
685{
686 struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
687 bus->msi = pcie->msi;
688}
689
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200690resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
691 const struct resource *res,
692 resource_size_t start,
693 resource_size_t size,
694 resource_size_t align)
695{
696 if (dev->bus->number != 0)
697 return start;
698
699 /*
700 * On the PCI-to-PCI bridge side, the I/O windows must have at
701 * least a 64 KB size and be aligned on their size, and the
702 * memory windows must have at least a 1 MB size and be
703 * aligned on their size
704 */
705 if (res->flags & IORESOURCE_IO)
706 return round_up(start, max((resource_size_t)SZ_64K, size));
707 else if (res->flags & IORESOURCE_MEM)
708 return round_up(start, max((resource_size_t)SZ_1M, size));
709 else
710 return start;
711}
712
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200713static void mvebu_pcie_enable(struct mvebu_pcie *pcie)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200714{
715 struct hw_pci hw;
716
717 memset(&hw, 0, sizeof(hw));
718
719 hw.nr_controllers = 1;
720 hw.private_data = (void **)&pcie;
721 hw.setup = mvebu_pcie_setup;
722 hw.scan = mvebu_pcie_scan_bus;
723 hw.map_irq = mvebu_pcie_map_irq;
724 hw.ops = &mvebu_pcie_ops;
725 hw.align_resource = mvebu_pcie_align_resource;
Thomas Petazzoni5b4deb62013-08-09 22:27:14 +0200726 hw.add_bus = mvebu_pcie_add_bus;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200727
728 pci_common_init(&hw);
729}
730
731/*
732 * Looks up the list of register addresses encoded into the reg =
733 * <...> property for one that matches the given port/lane. Once
734 * found, maps it.
735 */
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200736static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
737 struct device_node *np, struct mvebu_pcie_port *port)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200738{
739 struct resource regs;
740 int ret = 0;
741
742 ret = of_address_to_resource(np, 0, &regs);
743 if (ret)
Tushar Beheraf48fbf92013-06-17 14:46:13 +0530744 return ERR_PTR(ret);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200745
Tushar Beheraf48fbf92013-06-17 14:46:13 +0530746 return devm_ioremap_resource(&pdev->dev, &regs);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200747}
748
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300749#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
750#define DT_TYPE_IO 0x1
751#define DT_TYPE_MEM32 0x2
752#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
753#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
754
755static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
756 unsigned long type, int *tgt, int *attr)
757{
758 const int na = 3, ns = 2;
759 const __be32 *range;
760 int rlen, nranges, rangesz, pna, i;
761
762 range = of_get_property(np, "ranges", &rlen);
763 if (!range)
764 return -EINVAL;
765
766 pna = of_n_addr_cells(np);
767 rangesz = pna + na + ns;
768 nranges = rlen / sizeof(__be32) / rangesz;
769
770 for (i = 0; i < nranges; i++) {
771 u32 flags = of_read_number(range, 1);
772 u32 slot = of_read_number(range, 2);
773 u64 cpuaddr = of_read_number(range + na, pna);
774 unsigned long rtype;
775
776 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
777 rtype = IORESOURCE_IO;
778 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
779 rtype = IORESOURCE_MEM;
780
781 if (slot == PCI_SLOT(devfn) && type == rtype) {
782 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
783 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
784 return 0;
785 }
786
787 range += rangesz;
788 }
789
790 return -ENOENT;
791}
792
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200793static void mvebu_pcie_msi_enable(struct mvebu_pcie *pcie)
Thomas Petazzoni5b4deb62013-08-09 22:27:14 +0200794{
795 struct device_node *msi_node;
796
797 msi_node = of_parse_phandle(pcie->pdev->dev.of_node,
798 "msi-parent", 0);
799 if (!msi_node)
800 return;
801
802 pcie->msi = of_pci_find_msi_chip_by_node(msi_node);
803
804 if (pcie->msi)
805 pcie->msi->dev = &pcie->pdev->dev;
806}
807
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200808static int mvebu_pcie_probe(struct platform_device *pdev)
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200809{
810 struct mvebu_pcie *pcie;
811 struct device_node *np = pdev->dev.of_node;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200812 struct device_node *child;
813 int i, ret;
814
815 pcie = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_pcie),
816 GFP_KERNEL);
817 if (!pcie)
818 return -ENOMEM;
819
820 pcie->pdev = pdev;
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200821 platform_set_drvdata(pdev, pcie);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200822
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300823 /* Get the PCIe memory and I/O aperture */
824 mvebu_mbus_get_pcie_mem_aperture(&pcie->mem);
825 if (resource_size(&pcie->mem) == 0) {
826 dev_err(&pdev->dev, "invalid memory aperture size\n");
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200827 return -EINVAL;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200828 }
829
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300830 mvebu_mbus_get_pcie_io_aperture(&pcie->io);
831 if (resource_size(&pcie->io) == 0) {
832 dev_err(&pdev->dev, "invalid I/O aperture size\n");
833 return -EINVAL;
834 }
835
836 pcie->realio.flags = pcie->io.flags;
837 pcie->realio.start = PCIBIOS_MIN_IO;
838 pcie->realio.end = min_t(resource_size_t,
839 IO_SPACE_LIMIT,
840 resource_size(&pcie->io));
841
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200842 /* Get the bus range */
843 ret = of_pci_parse_bus_range(np, &pcie->busn);
844 if (ret) {
845 dev_err(&pdev->dev, "failed to parse bus-range property: %d\n",
846 ret);
847 return ret;
848 }
849
Sebastian Hesselbarthbf09b6a2013-08-13 14:25:21 +0200850 i = 0;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200851 for_each_child_of_node(pdev->dev.of_node, child) {
852 if (!of_device_is_available(child))
853 continue;
Sebastian Hesselbarthbf09b6a2013-08-13 14:25:21 +0200854 i++;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200855 }
856
Sebastian Hesselbarthbf09b6a2013-08-13 14:25:21 +0200857 pcie->ports = devm_kzalloc(&pdev->dev, i *
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200858 sizeof(struct mvebu_pcie_port),
859 GFP_KERNEL);
860 if (!pcie->ports)
861 return -ENOMEM;
862
863 i = 0;
864 for_each_child_of_node(pdev->dev.of_node, child) {
865 struct mvebu_pcie_port *port = &pcie->ports[i];
Sebastian Hesselbarth52ba9922013-08-13 14:25:23 +0200866 enum of_gpio_flags flags;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200867
868 if (!of_device_is_available(child))
869 continue;
870
871 port->pcie = pcie;
872
873 if (of_property_read_u32(child, "marvell,pcie-port",
874 &port->port)) {
875 dev_warn(&pdev->dev,
876 "ignoring PCIe DT node, missing pcie-port property\n");
877 continue;
878 }
879
880 if (of_property_read_u32(child, "marvell,pcie-lane",
881 &port->lane))
882 port->lane = 0;
883
884 port->name = kasprintf(GFP_KERNEL, "pcie%d.%d",
885 port->port, port->lane);
886
887 port->devfn = of_pci_get_devfn(child);
888 if (port->devfn < 0)
889 continue;
890
Thomas Petazzoni11be6542013-07-26 10:17:48 -0300891 ret = mvebu_get_tgt_attr(np, port->devfn, IORESOURCE_MEM,
892 &port->mem_target, &port->mem_attr);
893 if (ret < 0) {
894 dev_err(&pdev->dev, "PCIe%d.%d: cannot get tgt/attr for mem window\n",
895 port->port, port->lane);
896 continue;
897 }
898
899 ret = mvebu_get_tgt_attr(np, port->devfn, IORESOURCE_IO,
900 &port->io_target, &port->io_attr);
901 if (ret < 0) {
902 dev_err(&pdev->dev, "PCIe%d.%d: cannot get tgt/attr for io window\n",
903 port->port, port->lane);
904 continue;
905 }
906
Sebastian Hesselbarth52ba9922013-08-13 14:25:23 +0200907 port->reset_gpio = of_get_named_gpio_flags(child,
908 "reset-gpios", 0, &flags);
909 if (gpio_is_valid(port->reset_gpio)) {
910 u32 reset_udelay = 20000;
911
912 port->reset_active_low = flags & OF_GPIO_ACTIVE_LOW;
913 port->reset_name = kasprintf(GFP_KERNEL,
914 "pcie%d.%d-reset", port->port, port->lane);
915 of_property_read_u32(child, "reset-delay-us",
916 &reset_udelay);
917
918 ret = devm_gpio_request_one(&pdev->dev,
919 port->reset_gpio, GPIOF_DIR_OUT, port->reset_name);
920 if (ret) {
921 if (ret == -EPROBE_DEFER)
922 return ret;
923 continue;
924 }
925
926 gpio_set_value(port->reset_gpio,
927 (port->reset_active_low) ? 1 : 0);
928 msleep(reset_udelay/1000);
929 }
930
Sebastian Hesselbarthb42285f2013-08-13 14:25:20 +0200931 port->clk = of_clk_get_by_name(child, NULL);
932 if (IS_ERR(port->clk)) {
933 dev_err(&pdev->dev, "PCIe%d.%d: cannot get clock\n",
934 port->port, port->lane);
935 continue;
936 }
937
938 ret = clk_prepare_enable(port->clk);
939 if (ret)
940 continue;
941
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200942 port->base = mvebu_pcie_map_registers(pdev, child, port);
Tushar Beheraf48fbf92013-06-17 14:46:13 +0530943 if (IS_ERR(port->base)) {
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200944 dev_err(&pdev->dev, "PCIe%d.%d: cannot map registers\n",
945 port->port, port->lane);
Tushar Beheraf48fbf92013-06-17 14:46:13 +0530946 port->base = NULL;
Sebastian Hesselbarthb42285f2013-08-13 14:25:20 +0200947 clk_disable_unprepare(port->clk);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200948 continue;
949 }
950
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200951 mvebu_pcie_set_local_dev_nr(port, 1);
952
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200953 if (mvebu_pcie_link_up(port)) {
954 port->haslink = 1;
955 dev_info(&pdev->dev, "PCIe%d.%d: link up\n",
956 port->port, port->lane);
957 } else {
958 port->haslink = 0;
959 dev_info(&pdev->dev, "PCIe%d.%d: link down\n",
960 port->port, port->lane);
961 }
962
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200963 port->dn = child;
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200964 spin_lock_init(&port->conf_lock);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200965 mvebu_sw_pci_bridge_init(port);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200966 i++;
967 }
968
Sebastian Hesselbarthbf09b6a2013-08-13 14:25:21 +0200969 pcie->nports = i;
Thomas Petazzoni5b4deb62013-08-09 22:27:14 +0200970 mvebu_pcie_msi_enable(pcie);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200971 mvebu_pcie_enable(pcie);
972
973 return 0;
974}
975
976static const struct of_device_id mvebu_pcie_of_match_table[] = {
977 { .compatible = "marvell,armada-xp-pcie", },
978 { .compatible = "marvell,armada-370-pcie", },
Sebastian Hesselbarthcc54ccd2013-08-13 14:25:24 +0200979 { .compatible = "marvell,dove-pcie", },
Thomas Petazzoni005625f2013-05-15 15:36:54 +0200980 { .compatible = "marvell,kirkwood-pcie", },
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200981 {},
982};
983MODULE_DEVICE_TABLE(of, mvebu_pcie_of_match_table);
984
985static struct platform_driver mvebu_pcie_driver = {
986 .driver = {
987 .owner = THIS_MODULE,
988 .name = "mvebu-pcie",
989 .of_match_table =
990 of_match_ptr(mvebu_pcie_of_match_table),
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200991 /* driver unloading/unbinding currently not supported */
992 .suppress_bind_attrs = true,
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200993 },
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200994 .probe = mvebu_pcie_probe,
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200995};
Sebastian Hesselbarthe5615c32013-08-13 14:25:22 +0200996module_platform_driver(mvebu_pcie_driver);
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200997
998MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
999MODULE_DESCRIPTION("Marvell EBU PCIe driver");
1000MODULE_LICENSE("GPLv2");