blob: b8c95a293f641edc317e1dc71195b23c486fe2e7 [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>
12#include <linux/module.h>
13#include <linux/mbus.h>
14#include <linux/slab.h>
15#include <linux/platform_device.h>
16#include <linux/of_address.h>
17#include <linux/of_pci.h>
18#include <linux/of_irq.h>
19#include <linux/of_platform.h>
20
21/*
22 * PCIe unit register offsets.
23 */
24#define PCIE_DEV_ID_OFF 0x0000
25#define PCIE_CMD_OFF 0x0004
26#define PCIE_DEV_REV_OFF 0x0008
27#define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
28#define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
29#define PCIE_HEADER_LOG_4_OFF 0x0128
30#define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
31#define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
32#define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
33#define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
34#define PCIE_WIN5_CTRL_OFF 0x1880
35#define PCIE_WIN5_BASE_OFF 0x1884
36#define PCIE_WIN5_REMAP_OFF 0x188c
37#define PCIE_CONF_ADDR_OFF 0x18f8
38#define PCIE_CONF_ADDR_EN 0x80000000
39#define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
40#define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
41#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
42#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
43#define PCIE_CONF_ADDR(bus, devfn, where) \
44 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
45 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \
46 PCIE_CONF_ADDR_EN)
47#define PCIE_CONF_DATA_OFF 0x18fc
48#define PCIE_MASK_OFF 0x1910
49#define PCIE_MASK_ENABLE_INTS 0x0f000000
50#define PCIE_CTRL_OFF 0x1a00
51#define PCIE_CTRL_X1_MODE 0x0001
52#define PCIE_STAT_OFF 0x1a04
53#define PCIE_STAT_BUS 0xff00
Thomas Petazzonif4ac9902013-05-23 16:32:51 +020054#define PCIE_STAT_DEV 0x1f0000
Thomas Petazzoni45361a42013-05-16 17:55:22 +020055#define PCIE_STAT_LINK_DOWN BIT(0)
56#define PCIE_DEBUG_CTRL 0x1a60
57#define PCIE_DEBUG_SOFT_RESET BIT(20)
58
59/*
60 * This product ID is registered by Marvell, and used when the Marvell
61 * SoC is not the root complex, but an endpoint on the PCIe bus. It is
62 * therefore safe to re-use this PCI ID for our emulated PCI-to-PCI
63 * bridge.
64 */
65#define MARVELL_EMULATED_PCI_PCI_BRIDGE_ID 0x7846
66
67/* PCI configuration space of a PCI-to-PCI bridge */
68struct mvebu_sw_pci_bridge {
69 u16 vendor;
70 u16 device;
71 u16 command;
72 u16 status;
73 u16 class;
74 u8 interface;
75 u8 revision;
76 u8 bist;
77 u8 header_type;
78 u8 latency_timer;
79 u8 cache_line_size;
80 u32 bar[2];
81 u8 primary_bus;
82 u8 secondary_bus;
83 u8 subordinate_bus;
84 u8 secondary_latency_timer;
85 u8 iobase;
86 u8 iolimit;
87 u16 secondary_status;
88 u16 membase;
89 u16 memlimit;
90 u16 prefmembase;
91 u16 prefmemlimit;
92 u32 prefbaseupper;
93 u32 preflimitupper;
94 u16 iobaseupper;
95 u16 iolimitupper;
96 u8 cappointer;
97 u8 reserved1;
98 u16 reserved2;
99 u32 romaddr;
100 u8 intline;
101 u8 intpin;
102 u16 bridgectrl;
103};
104
105struct mvebu_pcie_port;
106
107/* Structure representing all PCIe interfaces */
108struct mvebu_pcie {
109 struct platform_device *pdev;
110 struct mvebu_pcie_port *ports;
111 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;
127 struct clk *clk;
128 struct mvebu_sw_pci_bridge bridge;
129 struct device_node *dn;
130 struct mvebu_pcie *pcie;
131 phys_addr_t memwin_base;
132 size_t memwin_size;
133 phys_addr_t iowin_base;
134 size_t iowin_size;
135};
136
137static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port)
138{
139 return !(readl(port->base + PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
140}
141
142static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
143{
144 u32 stat;
145
146 stat = readl(port->base + PCIE_STAT_OFF);
147 stat &= ~PCIE_STAT_BUS;
148 stat |= nr << 8;
149 writel(stat, port->base + PCIE_STAT_OFF);
150}
151
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200152static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr)
153{
154 u32 stat;
155
156 stat = readl(port->base + PCIE_STAT_OFF);
157 stat &= ~PCIE_STAT_DEV;
158 stat |= nr << 16;
159 writel(stat, port->base + PCIE_STAT_OFF);
160}
161
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200162/*
163 * Setup PCIE BARs and Address Decode Wins:
164 * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
165 * WIN[0-3] -> DRAM bank[0-3]
166 */
167static void __init mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
168{
169 const struct mbus_dram_target_info *dram;
170 u32 size;
171 int i;
172
173 dram = mv_mbus_dram_info();
174
175 /* First, disable and clear BARs and windows. */
176 for (i = 1; i < 3; i++) {
177 writel(0, port->base + PCIE_BAR_CTRL_OFF(i));
178 writel(0, port->base + PCIE_BAR_LO_OFF(i));
179 writel(0, port->base + PCIE_BAR_HI_OFF(i));
180 }
181
182 for (i = 0; i < 5; i++) {
183 writel(0, port->base + PCIE_WIN04_CTRL_OFF(i));
184 writel(0, port->base + PCIE_WIN04_BASE_OFF(i));
185 writel(0, port->base + PCIE_WIN04_REMAP_OFF(i));
186 }
187
188 writel(0, port->base + PCIE_WIN5_CTRL_OFF);
189 writel(0, port->base + PCIE_WIN5_BASE_OFF);
190 writel(0, port->base + PCIE_WIN5_REMAP_OFF);
191
192 /* Setup windows for DDR banks. Count total DDR size on the fly. */
193 size = 0;
194 for (i = 0; i < dram->num_cs; i++) {
195 const struct mbus_dram_window *cs = dram->cs + i;
196
197 writel(cs->base & 0xffff0000,
198 port->base + PCIE_WIN04_BASE_OFF(i));
199 writel(0, port->base + PCIE_WIN04_REMAP_OFF(i));
200 writel(((cs->size - 1) & 0xffff0000) |
201 (cs->mbus_attr << 8) |
202 (dram->mbus_dram_target_id << 4) | 1,
203 port->base + PCIE_WIN04_CTRL_OFF(i));
204
205 size += cs->size;
206 }
207
208 /* Round up 'size' to the nearest power of two. */
209 if ((size & (size - 1)) != 0)
210 size = 1 << fls(size);
211
212 /* Setup BAR[1] to all DRAM banks. */
213 writel(dram->cs[0].base, port->base + PCIE_BAR_LO_OFF(1));
214 writel(0, port->base + PCIE_BAR_HI_OFF(1));
215 writel(((size - 1) & 0xffff0000) | 1,
216 port->base + PCIE_BAR_CTRL_OFF(1));
217}
218
219static void __init mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
220{
221 u16 cmd;
222 u32 mask;
223
224 /* Point PCIe unit MBUS decode windows to DRAM space. */
225 mvebu_pcie_setup_wins(port);
226
227 /* Master + slave enable. */
228 cmd = readw(port->base + PCIE_CMD_OFF);
229 cmd |= PCI_COMMAND_IO;
230 cmd |= PCI_COMMAND_MEMORY;
231 cmd |= PCI_COMMAND_MASTER;
232 writew(cmd, port->base + PCIE_CMD_OFF);
233
234 /* Enable interrupt lines A-D. */
235 mask = readl(port->base + PCIE_MASK_OFF);
236 mask |= PCIE_MASK_ENABLE_INTS;
237 writel(mask, port->base + PCIE_MASK_OFF);
238}
239
240static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
241 struct pci_bus *bus,
242 u32 devfn, int where, int size, u32 *val)
243{
244 writel(PCIE_CONF_ADDR(bus->number, devfn, where),
245 port->base + PCIE_CONF_ADDR_OFF);
246
247 *val = readl(port->base + PCIE_CONF_DATA_OFF);
248
249 if (size == 1)
250 *val = (*val >> (8 * (where & 3))) & 0xff;
251 else if (size == 2)
252 *val = (*val >> (8 * (where & 3))) & 0xffff;
253
254 return PCIBIOS_SUCCESSFUL;
255}
256
257static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
258 struct pci_bus *bus,
259 u32 devfn, int where, int size, u32 val)
260{
261 int ret = PCIBIOS_SUCCESSFUL;
262
263 writel(PCIE_CONF_ADDR(bus->number, devfn, where),
264 port->base + PCIE_CONF_ADDR_OFF);
265
266 if (size == 4)
267 writel(val, port->base + PCIE_CONF_DATA_OFF);
268 else if (size == 2)
269 writew(val, port->base + PCIE_CONF_DATA_OFF + (where & 3));
270 else if (size == 1)
271 writeb(val, port->base + PCIE_CONF_DATA_OFF + (where & 3));
272 else
273 ret = PCIBIOS_BAD_REGISTER_NUMBER;
274
275 return ret;
276}
277
278static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
279{
280 phys_addr_t iobase;
281
282 /* Are the new iobase/iolimit values invalid? */
283 if (port->bridge.iolimit < port->bridge.iobase ||
284 port->bridge.iolimitupper < port->bridge.iobaseupper) {
285
286 /* If a window was configured, remove it */
287 if (port->iowin_base) {
288 mvebu_mbus_del_window(port->iowin_base,
289 port->iowin_size);
290 port->iowin_base = 0;
291 port->iowin_size = 0;
292 }
293
294 return;
295 }
296
297 /*
298 * We read the PCI-to-PCI bridge emulated registers, and
299 * calculate the base address and size of the address decoding
300 * window to setup, according to the PCI-to-PCI bridge
301 * specifications. iobase is the bus address, port->iowin_base
302 * is the CPU address.
303 */
304 iobase = ((port->bridge.iobase & 0xF0) << 8) |
305 (port->bridge.iobaseupper << 16);
306 port->iowin_base = port->pcie->io.start + iobase;
307 port->iowin_size = ((0xFFF | ((port->bridge.iolimit & 0xF0) << 8) |
308 (port->bridge.iolimitupper << 16)) -
309 iobase);
310
311 mvebu_mbus_add_window_remap_flags(port->name, port->iowin_base,
312 port->iowin_size,
313 iobase,
314 MVEBU_MBUS_PCI_IO);
315
316 pci_ioremap_io(iobase, port->iowin_base);
317}
318
319static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
320{
321 /* Are the new membase/memlimit values invalid? */
322 if (port->bridge.memlimit < port->bridge.membase) {
323
324 /* If a window was configured, remove it */
325 if (port->memwin_base) {
326 mvebu_mbus_del_window(port->memwin_base,
327 port->memwin_size);
328 port->memwin_base = 0;
329 port->memwin_size = 0;
330 }
331
332 return;
333 }
334
335 /*
336 * We read the PCI-to-PCI bridge emulated registers, and
337 * calculate the base address and size of the address decoding
338 * window to setup, according to the PCI-to-PCI bridge
339 * specifications.
340 */
341 port->memwin_base = ((port->bridge.membase & 0xFFF0) << 16);
342 port->memwin_size =
343 (((port->bridge.memlimit & 0xFFF0) << 16) | 0xFFFFF) -
344 port->memwin_base;
345
346 mvebu_mbus_add_window_remap_flags(port->name, port->memwin_base,
347 port->memwin_size,
348 MVEBU_MBUS_NO_REMAP,
349 MVEBU_MBUS_PCI_MEM);
350}
351
352/*
353 * Initialize the configuration space of the PCI-to-PCI bridge
354 * associated with the given PCIe interface.
355 */
356static void mvebu_sw_pci_bridge_init(struct mvebu_pcie_port *port)
357{
358 struct mvebu_sw_pci_bridge *bridge = &port->bridge;
359
360 memset(bridge, 0, sizeof(struct mvebu_sw_pci_bridge));
361
362 bridge->status = PCI_STATUS_CAP_LIST;
363 bridge->class = PCI_CLASS_BRIDGE_PCI;
364 bridge->vendor = PCI_VENDOR_ID_MARVELL;
365 bridge->device = MARVELL_EMULATED_PCI_PCI_BRIDGE_ID;
366 bridge->header_type = PCI_HEADER_TYPE_BRIDGE;
367 bridge->cache_line_size = 0x10;
368
369 /* We support 32 bits I/O addressing */
370 bridge->iobase = PCI_IO_RANGE_TYPE_32;
371 bridge->iolimit = PCI_IO_RANGE_TYPE_32;
372}
373
374/*
375 * Read the configuration space of the PCI-to-PCI bridge associated to
376 * the given PCIe interface.
377 */
378static int mvebu_sw_pci_bridge_read(struct mvebu_pcie_port *port,
379 unsigned int where, int size, u32 *value)
380{
381 struct mvebu_sw_pci_bridge *bridge = &port->bridge;
382
383 switch (where & ~3) {
384 case PCI_VENDOR_ID:
385 *value = bridge->device << 16 | bridge->vendor;
386 break;
387
388 case PCI_COMMAND:
389 *value = bridge->status << 16 | bridge->command;
390 break;
391
392 case PCI_CLASS_REVISION:
393 *value = bridge->class << 16 | bridge->interface << 8 |
394 bridge->revision;
395 break;
396
397 case PCI_CACHE_LINE_SIZE:
398 *value = bridge->bist << 24 | bridge->header_type << 16 |
399 bridge->latency_timer << 8 | bridge->cache_line_size;
400 break;
401
402 case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
403 *value = bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4];
404 break;
405
406 case PCI_PRIMARY_BUS:
407 *value = (bridge->secondary_latency_timer << 24 |
408 bridge->subordinate_bus << 16 |
409 bridge->secondary_bus << 8 |
410 bridge->primary_bus);
411 break;
412
413 case PCI_IO_BASE:
414 *value = (bridge->secondary_status << 16 |
415 bridge->iolimit << 8 |
416 bridge->iobase);
417 break;
418
419 case PCI_MEMORY_BASE:
420 *value = (bridge->memlimit << 16 | bridge->membase);
421 break;
422
423 case PCI_PREF_MEMORY_BASE:
424 *value = (bridge->prefmemlimit << 16 | bridge->prefmembase);
425 break;
426
427 case PCI_PREF_BASE_UPPER32:
428 *value = bridge->prefbaseupper;
429 break;
430
431 case PCI_PREF_LIMIT_UPPER32:
432 *value = bridge->preflimitupper;
433 break;
434
435 case PCI_IO_BASE_UPPER16:
436 *value = (bridge->iolimitupper << 16 | bridge->iobaseupper);
437 break;
438
439 case PCI_ROM_ADDRESS1:
440 *value = 0;
441 break;
442
443 default:
444 *value = 0xffffffff;
445 return PCIBIOS_BAD_REGISTER_NUMBER;
446 }
447
448 if (size == 2)
449 *value = (*value >> (8 * (where & 3))) & 0xffff;
450 else if (size == 1)
451 *value = (*value >> (8 * (where & 3))) & 0xff;
452
453 return PCIBIOS_SUCCESSFUL;
454}
455
456/* Write to the PCI-to-PCI bridge configuration space */
457static int mvebu_sw_pci_bridge_write(struct mvebu_pcie_port *port,
458 unsigned int where, int size, u32 value)
459{
460 struct mvebu_sw_pci_bridge *bridge = &port->bridge;
461 u32 mask, reg;
462 int err;
463
464 if (size == 4)
465 mask = 0x0;
466 else if (size == 2)
467 mask = ~(0xffff << ((where & 3) * 8));
468 else if (size == 1)
469 mask = ~(0xff << ((where & 3) * 8));
470 else
471 return PCIBIOS_BAD_REGISTER_NUMBER;
472
473 err = mvebu_sw_pci_bridge_read(port, where & ~3, 4, &reg);
474 if (err)
475 return err;
476
477 value = (reg & mask) | value << ((where & 3) * 8);
478
479 switch (where & ~3) {
480 case PCI_COMMAND:
481 bridge->command = value & 0xffff;
482 bridge->status = value >> 16;
483 break;
484
485 case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
486 bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4] = value;
487 break;
488
489 case PCI_IO_BASE:
490 /*
491 * We also keep bit 1 set, it is a read-only bit that
492 * indicates we support 32 bits addressing for the
493 * I/O
494 */
495 bridge->iobase = (value & 0xff) | PCI_IO_RANGE_TYPE_32;
496 bridge->iolimit = ((value >> 8) & 0xff) | PCI_IO_RANGE_TYPE_32;
497 bridge->secondary_status = value >> 16;
498 mvebu_pcie_handle_iobase_change(port);
499 break;
500
501 case PCI_MEMORY_BASE:
502 bridge->membase = value & 0xffff;
503 bridge->memlimit = value >> 16;
504 mvebu_pcie_handle_membase_change(port);
505 break;
506
507 case PCI_PREF_MEMORY_BASE:
508 bridge->prefmembase = value & 0xffff;
509 bridge->prefmemlimit = value >> 16;
510 break;
511
512 case PCI_PREF_BASE_UPPER32:
513 bridge->prefbaseupper = value;
514 break;
515
516 case PCI_PREF_LIMIT_UPPER32:
517 bridge->preflimitupper = value;
518 break;
519
520 case PCI_IO_BASE_UPPER16:
521 bridge->iobaseupper = value & 0xffff;
522 bridge->iolimitupper = value >> 16;
523 mvebu_pcie_handle_iobase_change(port);
524 break;
525
526 case PCI_PRIMARY_BUS:
527 bridge->primary_bus = value & 0xff;
528 bridge->secondary_bus = (value >> 8) & 0xff;
529 bridge->subordinate_bus = (value >> 16) & 0xff;
530 bridge->secondary_latency_timer = (value >> 24) & 0xff;
531 mvebu_pcie_set_local_bus_nr(port, bridge->secondary_bus);
532 break;
533
534 default:
535 break;
536 }
537
538 return PCIBIOS_SUCCESSFUL;
539}
540
541static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
542{
543 return sys->private_data;
544}
545
546static struct mvebu_pcie_port *
547mvebu_pcie_find_port(struct mvebu_pcie *pcie, struct pci_bus *bus,
548 int devfn)
549{
550 int i;
551
552 for (i = 0; i < pcie->nports; i++) {
553 struct mvebu_pcie_port *port = &pcie->ports[i];
554 if (bus->number == 0 && port->devfn == devfn)
555 return port;
556 if (bus->number != 0 &&
557 port->bridge.secondary_bus == bus->number)
558 return port;
559 }
560
561 return NULL;
562}
563
564/* PCI configuration space write function */
565static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
566 int where, int size, u32 val)
567{
568 struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
569 struct mvebu_pcie_port *port;
570 unsigned long flags;
571 int ret;
572
573 port = mvebu_pcie_find_port(pcie, bus, devfn);
574 if (!port)
575 return PCIBIOS_DEVICE_NOT_FOUND;
576
577 /* Access the emulated PCI-to-PCI bridge */
578 if (bus->number == 0)
579 return mvebu_sw_pci_bridge_write(port, where, size, val);
580
581 if (!port->haslink || PCI_SLOT(devfn) != 0)
582 return PCIBIOS_DEVICE_NOT_FOUND;
583
584 /* Access the real PCIe interface */
585 spin_lock_irqsave(&port->conf_lock, flags);
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200586 ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200587 where, size, val);
588 spin_unlock_irqrestore(&port->conf_lock, flags);
589
590 return ret;
591}
592
593/* PCI configuration space read function */
594static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
595 int size, u32 *val)
596{
597 struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
598 struct mvebu_pcie_port *port;
599 unsigned long flags;
600 int ret;
601
602 port = mvebu_pcie_find_port(pcie, bus, devfn);
603 if (!port) {
604 *val = 0xffffffff;
605 return PCIBIOS_DEVICE_NOT_FOUND;
606 }
607
608 /* Access the emulated PCI-to-PCI bridge */
609 if (bus->number == 0)
610 return mvebu_sw_pci_bridge_read(port, where, size, val);
611
612 if (!port->haslink || PCI_SLOT(devfn) != 0) {
613 *val = 0xffffffff;
614 return PCIBIOS_DEVICE_NOT_FOUND;
615 }
616
617 /* Access the real PCIe interface */
618 spin_lock_irqsave(&port->conf_lock, flags);
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200619 ret = mvebu_pcie_hw_rd_conf(port, bus, devfn,
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200620 where, size, val);
621 spin_unlock_irqrestore(&port->conf_lock, flags);
622
623 return ret;
624}
625
626static struct pci_ops mvebu_pcie_ops = {
627 .read = mvebu_pcie_rd_conf,
628 .write = mvebu_pcie_wr_conf,
629};
630
631static int __init mvebu_pcie_setup(int nr, struct pci_sys_data *sys)
632{
633 struct mvebu_pcie *pcie = sys_to_pcie(sys);
634 int i;
635
636 pci_add_resource_offset(&sys->resources, &pcie->realio, sys->io_offset);
637 pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
638 pci_add_resource(&sys->resources, &pcie->busn);
639
640 for (i = 0; i < pcie->nports; i++) {
641 struct mvebu_pcie_port *port = &pcie->ports[i];
642 mvebu_pcie_setup_hw(port);
643 }
644
645 return 1;
646}
647
648static int __init mvebu_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
649{
650 struct of_irq oirq;
651 int ret;
652
653 ret = of_irq_map_pci(dev, &oirq);
654 if (ret)
655 return ret;
656
657 return irq_create_of_mapping(oirq.controller, oirq.specifier,
658 oirq.size);
659}
660
661static struct pci_bus *mvebu_pcie_scan_bus(int nr, struct pci_sys_data *sys)
662{
663 struct mvebu_pcie *pcie = sys_to_pcie(sys);
664 struct pci_bus *bus;
665
666 bus = pci_create_root_bus(&pcie->pdev->dev, sys->busnr,
667 &mvebu_pcie_ops, sys, &sys->resources);
668 if (!bus)
669 return NULL;
670
671 pci_scan_child_bus(bus);
672
673 return bus;
674}
675
676resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
677 const struct resource *res,
678 resource_size_t start,
679 resource_size_t size,
680 resource_size_t align)
681{
682 if (dev->bus->number != 0)
683 return start;
684
685 /*
686 * On the PCI-to-PCI bridge side, the I/O windows must have at
687 * least a 64 KB size and be aligned on their size, and the
688 * memory windows must have at least a 1 MB size and be
689 * aligned on their size
690 */
691 if (res->flags & IORESOURCE_IO)
692 return round_up(start, max((resource_size_t)SZ_64K, size));
693 else if (res->flags & IORESOURCE_MEM)
694 return round_up(start, max((resource_size_t)SZ_1M, size));
695 else
696 return start;
697}
698
699static void __init mvebu_pcie_enable(struct mvebu_pcie *pcie)
700{
701 struct hw_pci hw;
702
703 memset(&hw, 0, sizeof(hw));
704
705 hw.nr_controllers = 1;
706 hw.private_data = (void **)&pcie;
707 hw.setup = mvebu_pcie_setup;
708 hw.scan = mvebu_pcie_scan_bus;
709 hw.map_irq = mvebu_pcie_map_irq;
710 hw.ops = &mvebu_pcie_ops;
711 hw.align_resource = mvebu_pcie_align_resource;
712
713 pci_common_init(&hw);
714}
715
716/*
717 * Looks up the list of register addresses encoded into the reg =
718 * <...> property for one that matches the given port/lane. Once
719 * found, maps it.
720 */
721static void __iomem * __init
722mvebu_pcie_map_registers(struct platform_device *pdev,
723 struct device_node *np,
724 struct mvebu_pcie_port *port)
725{
726 struct resource regs;
727 int ret = 0;
728
729 ret = of_address_to_resource(np, 0, &regs);
730 if (ret)
731 return NULL;
732
733 return devm_request_and_ioremap(&pdev->dev, &regs);
734}
735
736static int __init mvebu_pcie_probe(struct platform_device *pdev)
737{
738 struct mvebu_pcie *pcie;
739 struct device_node *np = pdev->dev.of_node;
740 struct of_pci_range range;
741 struct of_pci_range_parser parser;
742 struct device_node *child;
743 int i, ret;
744
745 pcie = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_pcie),
746 GFP_KERNEL);
747 if (!pcie)
748 return -ENOMEM;
749
750 pcie->pdev = pdev;
751
752 if (of_pci_range_parser_init(&parser, np))
753 return -EINVAL;
754
755 /* Get the I/O and memory ranges from DT */
756 for_each_of_pci_range(&parser, &range) {
757 unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
758 if (restype == IORESOURCE_IO) {
759 of_pci_range_to_resource(&range, np, &pcie->io);
760 of_pci_range_to_resource(&range, np, &pcie->realio);
761 pcie->io.name = "I/O";
762 pcie->realio.start = max_t(resource_size_t,
763 PCIBIOS_MIN_IO,
764 range.pci_addr);
765 pcie->realio.end = min_t(resource_size_t,
766 IO_SPACE_LIMIT,
767 range.pci_addr + range.size);
768 }
769 if (restype == IORESOURCE_MEM) {
770 of_pci_range_to_resource(&range, np, &pcie->mem);
771 pcie->mem.name = "MEM";
772 }
773 }
774
775 /* Get the bus range */
776 ret = of_pci_parse_bus_range(np, &pcie->busn);
777 if (ret) {
778 dev_err(&pdev->dev, "failed to parse bus-range property: %d\n",
779 ret);
780 return ret;
781 }
782
783 for_each_child_of_node(pdev->dev.of_node, child) {
784 if (!of_device_is_available(child))
785 continue;
786 pcie->nports++;
787 }
788
789 pcie->ports = devm_kzalloc(&pdev->dev, pcie->nports *
790 sizeof(struct mvebu_pcie_port),
791 GFP_KERNEL);
792 if (!pcie->ports)
793 return -ENOMEM;
794
795 i = 0;
796 for_each_child_of_node(pdev->dev.of_node, child) {
797 struct mvebu_pcie_port *port = &pcie->ports[i];
798
799 if (!of_device_is_available(child))
800 continue;
801
802 port->pcie = pcie;
803
804 if (of_property_read_u32(child, "marvell,pcie-port",
805 &port->port)) {
806 dev_warn(&pdev->dev,
807 "ignoring PCIe DT node, missing pcie-port property\n");
808 continue;
809 }
810
811 if (of_property_read_u32(child, "marvell,pcie-lane",
812 &port->lane))
813 port->lane = 0;
814
815 port->name = kasprintf(GFP_KERNEL, "pcie%d.%d",
816 port->port, port->lane);
817
818 port->devfn = of_pci_get_devfn(child);
819 if (port->devfn < 0)
820 continue;
821
822 port->base = mvebu_pcie_map_registers(pdev, child, port);
823 if (!port->base) {
824 dev_err(&pdev->dev, "PCIe%d.%d: cannot map registers\n",
825 port->port, port->lane);
826 continue;
827 }
828
Thomas Petazzonif4ac9902013-05-23 16:32:51 +0200829 mvebu_pcie_set_local_dev_nr(port, 1);
830
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200831 if (mvebu_pcie_link_up(port)) {
832 port->haslink = 1;
833 dev_info(&pdev->dev, "PCIe%d.%d: link up\n",
834 port->port, port->lane);
835 } else {
836 port->haslink = 0;
837 dev_info(&pdev->dev, "PCIe%d.%d: link down\n",
838 port->port, port->lane);
839 }
840
841 port->clk = of_clk_get_by_name(child, NULL);
Wei Yongjun3d9939c2013-05-27 11:38:41 +0800842 if (IS_ERR(port->clk)) {
Thomas Petazzoni45361a42013-05-16 17:55:22 +0200843 dev_err(&pdev->dev, "PCIe%d.%d: cannot get clock\n",
844 port->port, port->lane);
845 iounmap(port->base);
846 port->haslink = 0;
847 continue;
848 }
849
850 port->dn = child;
851
852 clk_prepare_enable(port->clk);
853 spin_lock_init(&port->conf_lock);
854
855 mvebu_sw_pci_bridge_init(port);
856
857 i++;
858 }
859
860 mvebu_pcie_enable(pcie);
861
862 return 0;
863}
864
865static const struct of_device_id mvebu_pcie_of_match_table[] = {
866 { .compatible = "marvell,armada-xp-pcie", },
867 { .compatible = "marvell,armada-370-pcie", },
868 {},
869};
870MODULE_DEVICE_TABLE(of, mvebu_pcie_of_match_table);
871
872static struct platform_driver mvebu_pcie_driver = {
873 .driver = {
874 .owner = THIS_MODULE,
875 .name = "mvebu-pcie",
876 .of_match_table =
877 of_match_ptr(mvebu_pcie_of_match_table),
878 },
879};
880
881static int __init mvebu_pcie_init(void)
882{
883 return platform_driver_probe(&mvebu_pcie_driver,
884 mvebu_pcie_probe);
885}
886
887subsys_initcall(mvebu_pcie_init);
888
889MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
890MODULE_DESCRIPTION("Marvell EBU PCIe driver");
891MODULE_LICENSE("GPLv2");