1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/kernel.h>
16#include <linux/pci.h>
17#include <linux/msi.h>
18#include <linux/clk.h>
19#include <linux/module.h>
20#include <linux/mbus.h>
21#include <linux/slab.h>
22#include <linux/delay.h>
23#include <linux/interrupt.h>
24#include <linux/irqchip/arm-gic-v3.h>
25#include <linux/platform_device.h>
26#include <linux/of_address.h>
27#include <linux/of_pci.h>
28#include <linux/of_irq.h>
29#include <linux/of_platform.h>
30#include <linux/phy/phy.h>
31
32#include "pcie-iproc.h"
33
34#define EP_PERST_SOURCE_SELECT_SHIFT 2
35#define EP_PERST_SOURCE_SELECT BIT(EP_PERST_SOURCE_SELECT_SHIFT)
36#define EP_MODE_SURVIVE_PERST_SHIFT 1
37#define EP_MODE_SURVIVE_PERST BIT(EP_MODE_SURVIVE_PERST_SHIFT)
38#define RC_PCIE_RST_OUTPUT_SHIFT 0
39#define RC_PCIE_RST_OUTPUT BIT(RC_PCIE_RST_OUTPUT_SHIFT)
40#define PAXC_RESET_MASK 0x7f
41
42#define GIC_V3_CFG_SHIFT 0
43#define GIC_V3_CFG BIT(GIC_V3_CFG_SHIFT)
44
45#define MSI_ENABLE_CFG_SHIFT 0
46#define MSI_ENABLE_CFG BIT(MSI_ENABLE_CFG_SHIFT)
47
48#define CFG_IND_ADDR_MASK 0x00001ffc
49
50#define CFG_ADDR_BUS_NUM_SHIFT 20
51#define CFG_ADDR_BUS_NUM_MASK 0x0ff00000
52#define CFG_ADDR_DEV_NUM_SHIFT 15
53#define CFG_ADDR_DEV_NUM_MASK 0x000f8000
54#define CFG_ADDR_FUNC_NUM_SHIFT 12
55#define CFG_ADDR_FUNC_NUM_MASK 0x00007000
56#define CFG_ADDR_REG_NUM_SHIFT 2
57#define CFG_ADDR_REG_NUM_MASK 0x00000ffc
58#define CFG_ADDR_CFG_TYPE_SHIFT 0
59#define CFG_ADDR_CFG_TYPE_MASK 0x00000003
60
61#define SYS_RC_INTX_MASK 0xf
62
63#define PCIE_PHYLINKUP_SHIFT 3
64#define PCIE_PHYLINKUP BIT(PCIE_PHYLINKUP_SHIFT)
65#define PCIE_DL_ACTIVE_SHIFT 2
66#define PCIE_DL_ACTIVE BIT(PCIE_DL_ACTIVE_SHIFT)
67
68#define APB_ERR_EN_SHIFT 0
69#define APB_ERR_EN BIT(APB_ERR_EN_SHIFT)
70
71
72#define MAP_REG(base_reg, index) ((base_reg) + (index) * 2)
73
74
75
76
77
78#define MAX_NUM_OB_WINDOW_SIZES 4
79
80#define OARR_VALID_SHIFT 0
81#define OARR_VALID BIT(OARR_VALID_SHIFT)
82#define OARR_SIZE_CFG_SHIFT 1
83
84
85
86
87
88#define MAX_NUM_IB_REGION_SIZES 9
89
90#define IMAP_VALID_SHIFT 0
91#define IMAP_VALID BIT(IMAP_VALID_SHIFT)
92
93#define PCI_EXP_CAP 0xac
94
95#define IPROC_PCIE_REG_INVALID 0xffff
96
97
98
99
100
101
102
103struct iproc_pcie_ob_map {
104 resource_size_t window_sizes[MAX_NUM_OB_WINDOW_SIZES];
105 unsigned int nr_sizes;
106};
107
108static const struct iproc_pcie_ob_map paxb_ob_map[] = {
109 {
110
111 .window_sizes = { 128, 256 },
112 .nr_sizes = 2,
113 },
114 {
115
116 .window_sizes = { 128, 256 },
117 .nr_sizes = 2,
118 },
119};
120
121static const struct iproc_pcie_ob_map paxb_v2_ob_map[] = {
122 {
123
124 .window_sizes = { 128, 256 },
125 .nr_sizes = 2,
126 },
127 {
128
129 .window_sizes = { 128, 256 },
130 .nr_sizes = 2,
131 },
132 {
133
134 .window_sizes = { 128, 256, 512, 1024 },
135 .nr_sizes = 4,
136 },
137 {
138
139 .window_sizes = { 128, 256, 512, 1024 },
140 .nr_sizes = 4,
141 },
142};
143
144
145
146
147enum iproc_pcie_ib_map_type {
148
149 IPROC_PCIE_IB_MAP_MEM = 0,
150
151
152 IPROC_PCIE_IB_MAP_IO,
153
154
155 IPROC_PCIE_IB_MAP_INVALID
156};
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172struct iproc_pcie_ib_map {
173 enum iproc_pcie_ib_map_type type;
174 unsigned int size_unit;
175 resource_size_t region_sizes[MAX_NUM_IB_REGION_SIZES];
176 unsigned int nr_sizes;
177 unsigned int nr_windows;
178 u16 imap_addr_offset;
179 u16 imap_window_offset;
180};
181
182static const struct iproc_pcie_ib_map paxb_v2_ib_map[] = {
183 {
184
185 .type = IPROC_PCIE_IB_MAP_IO,
186 .size_unit = SZ_1K,
187 .region_sizes = { 32 },
188 .nr_sizes = 1,
189 .nr_windows = 8,
190 .imap_addr_offset = 0x40,
191 .imap_window_offset = 0x4,
192 },
193 {
194
195 .type = IPROC_PCIE_IB_MAP_INVALID,
196 },
197 {
198
199 .type = IPROC_PCIE_IB_MAP_MEM,
200 .size_unit = SZ_1M,
201 .region_sizes = { 64, 128, 256, 512, 1024, 2048, 4096, 8192,
202 16384 },
203 .nr_sizes = 9,
204 .nr_windows = 1,
205 .imap_addr_offset = 0x4,
206 .imap_window_offset = 0x8,
207 },
208 {
209
210 .type = IPROC_PCIE_IB_MAP_MEM,
211 .size_unit = SZ_1G,
212 .region_sizes = { 1, 2, 4, 8, 16, 32 },
213 .nr_sizes = 6,
214 .nr_windows = 8,
215 .imap_addr_offset = 0x4,
216 .imap_window_offset = 0x8,
217 },
218 {
219
220 .type = IPROC_PCIE_IB_MAP_MEM,
221 .size_unit = SZ_1G,
222 .region_sizes = { 32, 64, 128, 256, 512 },
223 .nr_sizes = 5,
224 .nr_windows = 8,
225 .imap_addr_offset = 0x4,
226 .imap_window_offset = 0x8,
227 },
228};
229
230
231
232
233enum iproc_pcie_reg {
234
235 IPROC_PCIE_CLK_CTRL = 0,
236
237
238
239
240
241 IPROC_PCIE_MSI_GIC_MODE,
242
243
244
245
246
247
248 IPROC_PCIE_MSI_BASE_ADDR,
249 IPROC_PCIE_MSI_WINDOW_SIZE,
250
251
252
253
254
255
256 IPROC_PCIE_MSI_ADDR_LO,
257 IPROC_PCIE_MSI_ADDR_HI,
258
259
260 IPROC_PCIE_MSI_EN_CFG,
261
262
263 IPROC_PCIE_CFG_IND_ADDR,
264 IPROC_PCIE_CFG_IND_DATA,
265
266
267 IPROC_PCIE_CFG_ADDR,
268 IPROC_PCIE_CFG_DATA,
269
270
271 IPROC_PCIE_INTX_EN,
272
273
274 IPROC_PCIE_OARR0,
275 IPROC_PCIE_OMAP0,
276 IPROC_PCIE_OARR1,
277 IPROC_PCIE_OMAP1,
278 IPROC_PCIE_OARR2,
279 IPROC_PCIE_OMAP2,
280 IPROC_PCIE_OARR3,
281 IPROC_PCIE_OMAP3,
282
283
284 IPROC_PCIE_IARR0,
285 IPROC_PCIE_IMAP0,
286 IPROC_PCIE_IARR1,
287 IPROC_PCIE_IMAP1,
288 IPROC_PCIE_IARR2,
289 IPROC_PCIE_IMAP2,
290 IPROC_PCIE_IARR3,
291 IPROC_PCIE_IMAP3,
292 IPROC_PCIE_IARR4,
293 IPROC_PCIE_IMAP4,
294
295
296 IPROC_PCIE_LINK_STATUS,
297
298
299 IPROC_PCIE_APB_ERR_EN,
300
301
302 IPROC_PCIE_MAX_NUM_REG,
303};
304
305
306static const u16 iproc_pcie_reg_paxb_bcma[] = {
307 [IPROC_PCIE_CLK_CTRL] = 0x000,
308 [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
309 [IPROC_PCIE_CFG_IND_DATA] = 0x124,
310 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
311 [IPROC_PCIE_CFG_DATA] = 0x1fc,
312 [IPROC_PCIE_INTX_EN] = 0x330,
313 [IPROC_PCIE_LINK_STATUS] = 0xf0c,
314};
315
316
317static const u16 iproc_pcie_reg_paxb[] = {
318 [IPROC_PCIE_CLK_CTRL] = 0x000,
319 [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
320 [IPROC_PCIE_CFG_IND_DATA] = 0x124,
321 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
322 [IPROC_PCIE_CFG_DATA] = 0x1fc,
323 [IPROC_PCIE_INTX_EN] = 0x330,
324 [IPROC_PCIE_OARR0] = 0xd20,
325 [IPROC_PCIE_OMAP0] = 0xd40,
326 [IPROC_PCIE_OARR1] = 0xd28,
327 [IPROC_PCIE_OMAP1] = 0xd48,
328 [IPROC_PCIE_LINK_STATUS] = 0xf0c,
329 [IPROC_PCIE_APB_ERR_EN] = 0xf40,
330};
331
332
333static const u16 iproc_pcie_reg_paxb_v2[] = {
334 [IPROC_PCIE_CLK_CTRL] = 0x000,
335 [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
336 [IPROC_PCIE_CFG_IND_DATA] = 0x124,
337 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
338 [IPROC_PCIE_CFG_DATA] = 0x1fc,
339 [IPROC_PCIE_INTX_EN] = 0x330,
340 [IPROC_PCIE_OARR0] = 0xd20,
341 [IPROC_PCIE_OMAP0] = 0xd40,
342 [IPROC_PCIE_OARR1] = 0xd28,
343 [IPROC_PCIE_OMAP1] = 0xd48,
344 [IPROC_PCIE_OARR2] = 0xd60,
345 [IPROC_PCIE_OMAP2] = 0xd68,
346 [IPROC_PCIE_OARR3] = 0xdf0,
347 [IPROC_PCIE_OMAP3] = 0xdf8,
348 [IPROC_PCIE_IARR0] = 0xd00,
349 [IPROC_PCIE_IMAP0] = 0xc00,
350 [IPROC_PCIE_IARR2] = 0xd10,
351 [IPROC_PCIE_IMAP2] = 0xcc0,
352 [IPROC_PCIE_IARR3] = 0xe00,
353 [IPROC_PCIE_IMAP3] = 0xe08,
354 [IPROC_PCIE_IARR4] = 0xe68,
355 [IPROC_PCIE_IMAP4] = 0xe70,
356 [IPROC_PCIE_LINK_STATUS] = 0xf0c,
357 [IPROC_PCIE_APB_ERR_EN] = 0xf40,
358};
359
360
361static const u16 iproc_pcie_reg_paxc[] = {
362 [IPROC_PCIE_CLK_CTRL] = 0x000,
363 [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0,
364 [IPROC_PCIE_CFG_IND_DATA] = 0x1f4,
365 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
366 [IPROC_PCIE_CFG_DATA] = 0x1fc,
367};
368
369
370static const u16 iproc_pcie_reg_paxc_v2[] = {
371 [IPROC_PCIE_MSI_GIC_MODE] = 0x050,
372 [IPROC_PCIE_MSI_BASE_ADDR] = 0x074,
373 [IPROC_PCIE_MSI_WINDOW_SIZE] = 0x078,
374 [IPROC_PCIE_MSI_ADDR_LO] = 0x07c,
375 [IPROC_PCIE_MSI_ADDR_HI] = 0x080,
376 [IPROC_PCIE_MSI_EN_CFG] = 0x09c,
377 [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0,
378 [IPROC_PCIE_CFG_IND_DATA] = 0x1f4,
379 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
380 [IPROC_PCIE_CFG_DATA] = 0x1fc,
381};
382
383static inline struct iproc_pcie *iproc_data(struct pci_bus *bus)
384{
385 struct iproc_pcie *pcie;
386#ifdef CONFIG_ARM
387 struct pci_sys_data *sys = bus->sysdata;
388
389 pcie = sys->private_data;
390#else
391 pcie = bus->sysdata;
392#endif
393 return pcie;
394}
395
396static inline bool iproc_pcie_reg_is_invalid(u16 reg_offset)
397{
398 return !!(reg_offset == IPROC_PCIE_REG_INVALID);
399}
400
401static inline u16 iproc_pcie_reg_offset(struct iproc_pcie *pcie,
402 enum iproc_pcie_reg reg)
403{
404 return pcie->reg_offsets[reg];
405}
406
407static inline u32 iproc_pcie_read_reg(struct iproc_pcie *pcie,
408 enum iproc_pcie_reg reg)
409{
410 u16 offset = iproc_pcie_reg_offset(pcie, reg);
411
412 if (iproc_pcie_reg_is_invalid(offset))
413 return 0;
414
415 return readl(pcie->base + offset);
416}
417
418static inline void iproc_pcie_write_reg(struct iproc_pcie *pcie,
419 enum iproc_pcie_reg reg, u32 val)
420{
421 u16 offset = iproc_pcie_reg_offset(pcie, reg);
422
423 if (iproc_pcie_reg_is_invalid(offset))
424 return;
425
426 writel(val, pcie->base + offset);
427}
428
429
430
431
432
433
434
435static inline void iproc_pcie_apb_err_disable(struct pci_bus *bus,
436 bool disable)
437{
438 struct iproc_pcie *pcie = iproc_data(bus);
439 u32 val;
440
441 if (bus->number && pcie->has_apb_err_disable) {
442 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_APB_ERR_EN);
443 if (disable)
444 val &= ~APB_ERR_EN;
445 else
446 val |= APB_ERR_EN;
447 iproc_pcie_write_reg(pcie, IPROC_PCIE_APB_ERR_EN, val);
448 }
449}
450
451
452
453
454
455static void __iomem *iproc_pcie_map_cfg_bus(struct iproc_pcie *pcie,
456 int busno,
457 unsigned int devfn,
458 int where)
459{
460 unsigned slot = PCI_SLOT(devfn);
461 unsigned fn = PCI_FUNC(devfn);
462 u32 val;
463 u16 offset;
464
465
466 if (busno == 0) {
467 if (slot > 0 || fn > 0)
468 return NULL;
469
470 iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_IND_ADDR,
471 where & CFG_IND_ADDR_MASK);
472 offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_IND_DATA);
473 if (iproc_pcie_reg_is_invalid(offset))
474 return NULL;
475 else
476 return (pcie->base + offset);
477 }
478
479
480
481
482
483 if (pcie->ep_is_internal)
484 if (slot > 0)
485 return NULL;
486
487
488 val = (busno << CFG_ADDR_BUS_NUM_SHIFT) |
489 (slot << CFG_ADDR_DEV_NUM_SHIFT) |
490 (fn << CFG_ADDR_FUNC_NUM_SHIFT) |
491 (where & CFG_ADDR_REG_NUM_MASK) |
492 (1 & CFG_ADDR_CFG_TYPE_MASK);
493 iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_ADDR, val);
494 offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_DATA);
495 if (iproc_pcie_reg_is_invalid(offset))
496 return NULL;
497 else
498 return (pcie->base + offset);
499}
500
501static void __iomem *iproc_pcie_bus_map_cfg_bus(struct pci_bus *bus,
502 unsigned int devfn,
503 int where)
504{
505 return iproc_pcie_map_cfg_bus(iproc_data(bus), bus->number, devfn,
506 where);
507}
508
509static int iproc_pci_raw_config_read32(struct iproc_pcie *pcie,
510 unsigned int devfn, int where,
511 int size, u32 *val)
512{
513 void __iomem *addr;
514
515 addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3);
516 if (!addr) {
517 *val = ~0;
518 return PCIBIOS_DEVICE_NOT_FOUND;
519 }
520
521 *val = readl(addr);
522
523 if (size <= 2)
524 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
525
526 return PCIBIOS_SUCCESSFUL;
527}
528
529static int iproc_pci_raw_config_write32(struct iproc_pcie *pcie,
530 unsigned int devfn, int where,
531 int size, u32 val)
532{
533 void __iomem *addr;
534 u32 mask, tmp;
535
536 addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3);
537 if (!addr)
538 return PCIBIOS_DEVICE_NOT_FOUND;
539
540 if (size == 4) {
541 writel(val, addr);
542 return PCIBIOS_SUCCESSFUL;
543 }
544
545 mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
546 tmp = readl(addr) & mask;
547 tmp |= val << ((where & 0x3) * 8);
548 writel(tmp, addr);
549
550 return PCIBIOS_SUCCESSFUL;
551}
552
553static int iproc_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
554 int where, int size, u32 *val)
555{
556 int ret;
557
558 iproc_pcie_apb_err_disable(bus, true);
559 ret = pci_generic_config_read32(bus, devfn, where, size, val);
560 iproc_pcie_apb_err_disable(bus, false);
561
562 return ret;
563}
564
565static int iproc_pcie_config_write32(struct pci_bus *bus, unsigned int devfn,
566 int where, int size, u32 val)
567{
568 int ret;
569
570 iproc_pcie_apb_err_disable(bus, true);
571 ret = pci_generic_config_write32(bus, devfn, where, size, val);
572 iproc_pcie_apb_err_disable(bus, false);
573
574 return ret;
575}
576
577static struct pci_ops iproc_pcie_ops = {
578 .map_bus = iproc_pcie_bus_map_cfg_bus,
579 .read = iproc_pcie_config_read32,
580 .write = iproc_pcie_config_write32,
581};
582
583static void iproc_pcie_reset(struct iproc_pcie *pcie)
584{
585 u32 val;
586
587
588
589
590
591
592 if (pcie->ep_is_internal)
593 return;
594
595
596
597
598
599 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
600 val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST &
601 ~RC_PCIE_RST_OUTPUT;
602 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
603 udelay(250);
604
605 val |= RC_PCIE_RST_OUTPUT;
606 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
607 msleep(100);
608}
609
610static int iproc_pcie_check_link(struct iproc_pcie *pcie)
611{
612 struct device *dev = pcie->dev;
613 u32 hdr_type, link_ctrl, link_status, class, val;
614 u16 pos = PCI_EXP_CAP;
615 bool link_is_active = false;
616
617
618
619
620
621 if (pcie->ep_is_internal)
622 return 0;
623
624 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_LINK_STATUS);
625 if (!(val & PCIE_PHYLINKUP) || !(val & PCIE_DL_ACTIVE)) {
626 dev_err(dev, "PHY or data link is INACTIVE!\n");
627 return -ENODEV;
628 }
629
630
631 iproc_pci_raw_config_read32(pcie, 0, PCI_HEADER_TYPE, 1, &hdr_type);
632 if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) {
633 dev_err(dev, "in EP mode, hdr=%#02x\n", hdr_type);
634 return -EFAULT;
635 }
636
637
638#define PCI_BRIDGE_CTRL_REG_OFFSET 0x43c
639#define PCI_CLASS_BRIDGE_MASK 0xffff00
640#define PCI_CLASS_BRIDGE_SHIFT 8
641 iproc_pci_raw_config_read32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET,
642 4, &class);
643 class &= ~PCI_CLASS_BRIDGE_MASK;
644 class |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT);
645 iproc_pci_raw_config_write32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET,
646 4, class);
647
648
649 iproc_pci_raw_config_read32(pcie, 0, pos + PCI_EXP_LNKSTA,
650 2, &link_status);
651 if (link_status & PCI_EXP_LNKSTA_NLW)
652 link_is_active = true;
653
654 if (!link_is_active) {
655
656#define PCI_TARGET_LINK_SPEED_MASK 0xf
657#define PCI_TARGET_LINK_SPEED_GEN2 0x2
658#define PCI_TARGET_LINK_SPEED_GEN1 0x1
659 iproc_pci_raw_config_read32(pcie, 0,
660 pos + PCI_EXP_LNKCTL2, 4,
661 &link_ctrl);
662 if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) ==
663 PCI_TARGET_LINK_SPEED_GEN2) {
664 link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK;
665 link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1;
666 iproc_pci_raw_config_write32(pcie, 0,
667 pos + PCI_EXP_LNKCTL2,
668 4, link_ctrl);
669 msleep(100);
670
671 iproc_pci_raw_config_read32(pcie, 0,
672 pos + PCI_EXP_LNKSTA,
673 2, &link_status);
674 if (link_status & PCI_EXP_LNKSTA_NLW)
675 link_is_active = true;
676 }
677 }
678
679 dev_info(dev, "link: %s\n", link_is_active ? "UP" : "DOWN");
680
681 return link_is_active ? 0 : -ENODEV;
682}
683
684static void iproc_pcie_enable(struct iproc_pcie *pcie)
685{
686 iproc_pcie_write_reg(pcie, IPROC_PCIE_INTX_EN, SYS_RC_INTX_MASK);
687}
688
689static inline bool iproc_pcie_ob_is_valid(struct iproc_pcie *pcie,
690 int window_idx)
691{
692 u32 val;
693
694 val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_OARR0, window_idx));
695
696 return !!(val & OARR_VALID);
697}
698
699static inline int iproc_pcie_ob_write(struct iproc_pcie *pcie, int window_idx,
700 int size_idx, u64 axi_addr, u64 pci_addr)
701{
702 struct device *dev = pcie->dev;
703 u16 oarr_offset, omap_offset;
704
705
706
707
708
709 oarr_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OARR0,
710 window_idx));
711 omap_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OMAP0,
712 window_idx));
713 if (iproc_pcie_reg_is_invalid(oarr_offset) ||
714 iproc_pcie_reg_is_invalid(omap_offset))
715 return -EINVAL;
716
717
718
719
720
721 writel(lower_32_bits(axi_addr) | (size_idx << OARR_SIZE_CFG_SHIFT) |
722 OARR_VALID, pcie->base + oarr_offset);
723 writel(upper_32_bits(axi_addr), pcie->base + oarr_offset + 4);
724
725
726 writel(lower_32_bits(pci_addr), pcie->base + omap_offset);
727 writel(upper_32_bits(pci_addr), pcie->base + omap_offset + 4);
728
729 dev_info(dev, "ob window [%d]: offset 0x%x axi %pap pci %pap\n",
730 window_idx, oarr_offset, &axi_addr, &pci_addr);
731 dev_info(dev, "oarr lo 0x%x oarr hi 0x%x\n",
732 readl(pcie->base + oarr_offset),
733 readl(pcie->base + oarr_offset + 4));
734 dev_info(dev, "omap lo 0x%x omap hi 0x%x\n",
735 readl(pcie->base + omap_offset),
736 readl(pcie->base + omap_offset + 4));
737
738 return 0;
739}
740
741
742
743
744
745
746
747
748
749
750
751
752static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr,
753 u64 pci_addr, resource_size_t size)
754{
755 struct iproc_pcie_ob *ob = &pcie->ob;
756 struct device *dev = pcie->dev;
757 int ret = -EINVAL, window_idx, size_idx;
758
759 if (axi_addr < ob->axi_offset) {
760 dev_err(dev, "axi address %pap less than offset %pap\n",
761 &axi_addr, &ob->axi_offset);
762 return -EINVAL;
763 }
764
765
766
767
768
769 axi_addr -= ob->axi_offset;
770
771
772 for (window_idx = ob->nr_windows - 1; window_idx >= 0; window_idx--) {
773 const struct iproc_pcie_ob_map *ob_map =
774 &pcie->ob_map[window_idx];
775
776
777
778
779
780 if (iproc_pcie_ob_is_valid(pcie, window_idx))
781 continue;
782
783
784
785
786
787
788 for (size_idx = ob_map->nr_sizes - 1; size_idx >= 0;
789 size_idx--) {
790 resource_size_t window_size =
791 ob_map->window_sizes[size_idx] * SZ_1M;
792
793 if (size < window_size)
794 continue;
795
796 if (!IS_ALIGNED(axi_addr, window_size) ||
797 !IS_ALIGNED(pci_addr, window_size)) {
798 dev_err(dev,
799 "axi %pap or pci %pap not aligned\n",
800 &axi_addr, &pci_addr);
801 return -EINVAL;
802 }
803
804
805
806
807
808 ret = iproc_pcie_ob_write(pcie, window_idx, size_idx,
809 axi_addr, pci_addr);
810 if (ret)
811 goto err_ob;
812
813 size -= window_size;
814 if (size == 0)
815 return 0;
816
817
818
819
820
821
822 axi_addr += window_size;
823 pci_addr += window_size;
824 break;
825 }
826 }
827
828err_ob:
829 dev_err(dev, "unable to configure outbound mapping\n");
830 dev_err(dev,
831 "axi %pap, axi offset %pap, pci %pap, res size %pap\n",
832 &axi_addr, &ob->axi_offset, &pci_addr, &size);
833
834 return ret;
835}
836
837static int iproc_pcie_map_ranges(struct iproc_pcie *pcie,
838 struct list_head *resources)
839{
840 struct device *dev = pcie->dev;
841 struct resource_entry *window;
842 int ret;
843
844 resource_list_for_each_entry(window, resources) {
845 struct resource *res = window->res;
846 u64 res_type = resource_type(res);
847
848 switch (res_type) {
849 case IORESOURCE_IO:
850 case IORESOURCE_BUS:
851 break;
852 case IORESOURCE_MEM:
853 ret = iproc_pcie_setup_ob(pcie, res->start,
854 res->start - window->offset,
855 resource_size(res));
856 if (ret)
857 return ret;
858 break;
859 default:
860 dev_err(dev, "invalid resource %pR\n", res);
861 return -EINVAL;
862 }
863 }
864
865 return 0;
866}
867
868static inline bool iproc_pcie_ib_is_in_use(struct iproc_pcie *pcie,
869 int region_idx)
870{
871 const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
872 u32 val;
873
874 val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_IARR0, region_idx));
875
876 return !!(val & (BIT(ib_map->nr_sizes) - 1));
877}
878
879static inline bool iproc_pcie_ib_check_type(const struct iproc_pcie_ib_map *ib_map,
880 enum iproc_pcie_ib_map_type type)
881{
882 return !!(ib_map->type == type);
883}
884
885static int iproc_pcie_ib_write(struct iproc_pcie *pcie, int region_idx,
886 int size_idx, int nr_windows, u64 axi_addr,
887 u64 pci_addr, resource_size_t size)
888{
889 struct device *dev = pcie->dev;
890 const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
891 u16 iarr_offset, imap_offset;
892 u32 val;
893 int window_idx;
894
895 iarr_offset = iproc_pcie_reg_offset(pcie,
896 MAP_REG(IPROC_PCIE_IARR0, region_idx));
897 imap_offset = iproc_pcie_reg_offset(pcie,
898 MAP_REG(IPROC_PCIE_IMAP0, region_idx));
899 if (iproc_pcie_reg_is_invalid(iarr_offset) ||
900 iproc_pcie_reg_is_invalid(imap_offset))
901 return -EINVAL;
902
903 dev_info(dev, "ib region [%d]: offset 0x%x axi %pap pci %pap\n",
904 region_idx, iarr_offset, &axi_addr, &pci_addr);
905
906
907
908
909
910 writel(lower_32_bits(pci_addr) | BIT(size_idx),
911 pcie->base + iarr_offset);
912 writel(upper_32_bits(pci_addr), pcie->base + iarr_offset + 4);
913
914 dev_info(dev, "iarr lo 0x%x iarr hi 0x%x\n",
915 readl(pcie->base + iarr_offset),
916 readl(pcie->base + iarr_offset + 4));
917
918
919
920
921
922 size >>= ilog2(nr_windows);
923 for (window_idx = 0; window_idx < nr_windows; window_idx++) {
924 val = readl(pcie->base + imap_offset);
925 val |= lower_32_bits(axi_addr) | IMAP_VALID;
926 writel(val, pcie->base + imap_offset);
927 writel(upper_32_bits(axi_addr),
928 pcie->base + imap_offset + ib_map->imap_addr_offset);
929
930 dev_info(dev, "imap window [%d] lo 0x%x hi 0x%x\n",
931 window_idx, readl(pcie->base + imap_offset),
932 readl(pcie->base + imap_offset +
933 ib_map->imap_addr_offset));
934
935 imap_offset += ib_map->imap_window_offset;
936 axi_addr += size;
937 }
938
939 return 0;
940}
941
942static int iproc_pcie_setup_ib(struct iproc_pcie *pcie,
943 struct of_pci_range *range,
944 enum iproc_pcie_ib_map_type type)
945{
946 struct device *dev = pcie->dev;
947 struct iproc_pcie_ib *ib = &pcie->ib;
948 int ret;
949 unsigned int region_idx, size_idx;
950 u64 axi_addr = range->cpu_addr, pci_addr = range->pci_addr;
951 resource_size_t size = range->size;
952
953
954 for (region_idx = 0; region_idx < ib->nr_regions; region_idx++) {
955 const struct iproc_pcie_ib_map *ib_map =
956 &pcie->ib_map[region_idx];
957
958
959
960
961
962 if (iproc_pcie_ib_is_in_use(pcie, region_idx) ||
963 !iproc_pcie_ib_check_type(ib_map, type))
964 continue;
965
966
967 for (size_idx = 0; size_idx < ib_map->nr_sizes; size_idx++) {
968 resource_size_t region_size =
969 ib_map->region_sizes[size_idx] * ib_map->size_unit;
970
971 if (size != region_size)
972 continue;
973
974 if (!IS_ALIGNED(axi_addr, region_size) ||
975 !IS_ALIGNED(pci_addr, region_size)) {
976 dev_err(dev,
977 "axi %pap or pci %pap not aligned\n",
978 &axi_addr, &pci_addr);
979 return -EINVAL;
980 }
981
982
983 ret = iproc_pcie_ib_write(pcie, region_idx, size_idx,
984 ib_map->nr_windows, axi_addr,
985 pci_addr, size);
986 if (ret)
987 goto err_ib;
988 else
989 return 0;
990
991 }
992 }
993 ret = -EINVAL;
994
995err_ib:
996 dev_err(dev, "unable to configure inbound mapping\n");
997 dev_err(dev, "axi %pap, pci %pap, res size %pap\n",
998 &axi_addr, &pci_addr, &size);
999
1000 return ret;
1001}
1002
1003static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
1004 struct device_node *node)
1005{
1006 const int na = 3, ns = 2;
1007 int rlen;
1008
1009 parser->node = node;
1010 parser->pna = of_n_addr_cells(node);
1011 parser->np = parser->pna + na + ns;
1012
1013 parser->range = of_get_property(node, "dma-ranges", &rlen);
1014 if (!parser->range)
1015 return -ENOENT;
1016
1017 parser->end = parser->range + rlen / sizeof(__be32);
1018 return 0;
1019}
1020
1021static int iproc_pcie_map_dma_ranges(struct iproc_pcie *pcie)
1022{
1023 struct of_pci_range range;
1024 struct of_pci_range_parser parser;
1025 int ret;
1026
1027
1028 ret = pci_dma_range_parser_init(&parser, pcie->dev->of_node);
1029 if (ret)
1030 return ret;
1031
1032 for_each_of_pci_range(&parser, &range) {
1033
1034 ret = iproc_pcie_setup_ib(pcie, &range, IPROC_PCIE_IB_MAP_MEM);
1035 if (ret)
1036 return ret;
1037 }
1038
1039 return 0;
1040}
1041
1042static int iproce_pcie_get_msi(struct iproc_pcie *pcie,
1043 struct device_node *msi_node,
1044 u64 *msi_addr)
1045{
1046 struct device *dev = pcie->dev;
1047 int ret;
1048 struct resource res;
1049
1050
1051
1052
1053
1054 if (!of_device_is_compatible(msi_node, "arm,gic-v3-its")) {
1055 dev_err(dev, "unable to find compatible MSI controller\n");
1056 return -ENODEV;
1057 }
1058
1059
1060 ret = of_address_to_resource(msi_node, 0, &res);
1061 if (ret < 0) {
1062 dev_err(dev, "unable to obtain MSI controller resources\n");
1063 return ret;
1064 }
1065
1066 *msi_addr = res.start + GITS_TRANSLATER;
1067 return 0;
1068}
1069
1070static int iproc_pcie_paxb_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr)
1071{
1072 int ret;
1073 struct of_pci_range range;
1074
1075 memset(&range, 0, sizeof(range));
1076 range.size = SZ_32K;
1077 range.pci_addr = range.cpu_addr = msi_addr & ~(range.size - 1);
1078
1079 ret = iproc_pcie_setup_ib(pcie, &range, IPROC_PCIE_IB_MAP_IO);
1080 return ret;
1081}
1082
1083static void iproc_pcie_paxc_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr)
1084{
1085 u32 val;
1086
1087
1088
1089
1090
1091
1092
1093 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_BASE_ADDR,
1094 (u32)(msi_addr >> 13));
1095
1096
1097 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_WINDOW_SIZE, 0);
1098
1099
1100 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_GIC_MODE);
1101 val |= GIC_V3_CFG;
1102 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_GIC_MODE, val);
1103
1104
1105
1106
1107
1108 msi_addr >>= 2;
1109 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_HI,
1110 upper_32_bits(msi_addr));
1111 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_LO,
1112 lower_32_bits(msi_addr));
1113
1114
1115 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG);
1116 val |= MSI_ENABLE_CFG;
1117 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val);
1118}
1119
1120static int iproc_pcie_msi_steer(struct iproc_pcie *pcie,
1121 struct device_node *msi_node)
1122{
1123 struct device *dev = pcie->dev;
1124 int ret;
1125 u64 msi_addr;
1126
1127 ret = iproce_pcie_get_msi(pcie, msi_node, &msi_addr);
1128 if (ret < 0) {
1129 dev_err(dev, "msi steering failed\n");
1130 return ret;
1131 }
1132
1133 switch (pcie->type) {
1134 case IPROC_PCIE_PAXB_V2:
1135 ret = iproc_pcie_paxb_v2_msi_steer(pcie, msi_addr);
1136 if (ret)
1137 return ret;
1138 break;
1139 case IPROC_PCIE_PAXC_V2:
1140 iproc_pcie_paxc_v2_msi_steer(pcie, msi_addr);
1141 break;
1142 default:
1143 return -EINVAL;
1144 }
1145
1146 return 0;
1147}
1148
1149static int iproc_pcie_msi_enable(struct iproc_pcie *pcie)
1150{
1151 struct device_node *msi_node;
1152 int ret;
1153
1154
1155
1156
1157
1158
1159 msi_node = of_parse_phandle(pcie->dev->of_node, "msi-parent", 0);
1160 if (!msi_node) {
1161 const __be32 *msi_map = NULL;
1162 int len;
1163 u32 phandle;
1164
1165 msi_map = of_get_property(pcie->dev->of_node, "msi-map", &len);
1166 if (!msi_map)
1167 return -ENODEV;
1168
1169 phandle = be32_to_cpup(msi_map + 1);
1170 msi_node = of_find_node_by_phandle(phandle);
1171 if (!msi_node)
1172 return -ENODEV;
1173 }
1174
1175
1176
1177
1178
1179
1180 if (pcie->need_msi_steer) {
1181 ret = iproc_pcie_msi_steer(pcie, msi_node);
1182 if (ret)
1183 return ret;
1184 }
1185
1186
1187
1188
1189
1190 return iproc_msi_init(pcie, msi_node);
1191}
1192
1193static void iproc_pcie_msi_disable(struct iproc_pcie *pcie)
1194{
1195 iproc_msi_exit(pcie);
1196}
1197
1198static int iproc_pcie_rev_init(struct iproc_pcie *pcie)
1199{
1200 struct device *dev = pcie->dev;
1201 unsigned int reg_idx;
1202 const u16 *regs;
1203
1204 switch (pcie->type) {
1205 case IPROC_PCIE_PAXB_BCMA:
1206 regs = iproc_pcie_reg_paxb_bcma;
1207 break;
1208 case IPROC_PCIE_PAXB:
1209 regs = iproc_pcie_reg_paxb;
1210 pcie->has_apb_err_disable = true;
1211 if (pcie->need_ob_cfg) {
1212 pcie->ob_map = paxb_ob_map;
1213 pcie->ob.nr_windows = ARRAY_SIZE(paxb_ob_map);
1214 }
1215 break;
1216 case IPROC_PCIE_PAXB_V2:
1217 regs = iproc_pcie_reg_paxb_v2;
1218 pcie->has_apb_err_disable = true;
1219 if (pcie->need_ob_cfg) {
1220 pcie->ob_map = paxb_v2_ob_map;
1221 pcie->ob.nr_windows = ARRAY_SIZE(paxb_v2_ob_map);
1222 }
1223 pcie->ib.nr_regions = ARRAY_SIZE(paxb_v2_ib_map);
1224 pcie->ib_map = paxb_v2_ib_map;
1225 pcie->need_msi_steer = true;
1226 break;
1227 case IPROC_PCIE_PAXC:
1228 regs = iproc_pcie_reg_paxc;
1229 pcie->ep_is_internal = true;
1230 break;
1231 case IPROC_PCIE_PAXC_V2:
1232 regs = iproc_pcie_reg_paxc_v2;
1233 pcie->ep_is_internal = true;
1234 pcie->need_msi_steer = true;
1235 break;
1236 default:
1237 dev_err(dev, "incompatible iProc PCIe interface\n");
1238 return -EINVAL;
1239 }
1240
1241 pcie->reg_offsets = devm_kcalloc(dev, IPROC_PCIE_MAX_NUM_REG,
1242 sizeof(*pcie->reg_offsets),
1243 GFP_KERNEL);
1244 if (!pcie->reg_offsets)
1245 return -ENOMEM;
1246
1247
1248 pcie->reg_offsets[0] = (pcie->type == IPROC_PCIE_PAXC_V2) ?
1249 IPROC_PCIE_REG_INVALID : regs[0];
1250 for (reg_idx = 1; reg_idx < IPROC_PCIE_MAX_NUM_REG; reg_idx++)
1251 pcie->reg_offsets[reg_idx] = regs[reg_idx] ?
1252 regs[reg_idx] : IPROC_PCIE_REG_INVALID;
1253
1254 return 0;
1255}
1256
1257int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res)
1258{
1259 struct device *dev;
1260 int ret;
1261 void *sysdata;
1262 struct pci_bus *child;
1263 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1264
1265 dev = pcie->dev;
1266
1267 ret = iproc_pcie_rev_init(pcie);
1268 if (ret) {
1269 dev_err(dev, "unable to initialize controller parameters\n");
1270 return ret;
1271 }
1272
1273 ret = devm_request_pci_bus_resources(dev, res);
1274 if (ret)
1275 return ret;
1276
1277 ret = phy_init(pcie->phy);
1278 if (ret) {
1279 dev_err(dev, "unable to initialize PCIe PHY\n");
1280 return ret;
1281 }
1282
1283 ret = phy_power_on(pcie->phy);
1284 if (ret) {
1285 dev_err(dev, "unable to power on PCIe PHY\n");
1286 goto err_exit_phy;
1287 }
1288
1289 iproc_pcie_reset(pcie);
1290
1291 if (pcie->need_ob_cfg) {
1292 ret = iproc_pcie_map_ranges(pcie, res);
1293 if (ret) {
1294 dev_err(dev, "map failed\n");
1295 goto err_power_off_phy;
1296 }
1297 }
1298
1299 ret = iproc_pcie_map_dma_ranges(pcie);
1300 if (ret && ret != -ENOENT)
1301 goto err_power_off_phy;
1302
1303#ifdef CONFIG_ARM
1304 pcie->sysdata.private_data = pcie;
1305 sysdata = &pcie->sysdata;
1306#else
1307 sysdata = pcie;
1308#endif
1309
1310 ret = iproc_pcie_check_link(pcie);
1311 if (ret) {
1312 dev_err(dev, "no PCIe EP device detected\n");
1313 goto err_power_off_phy;
1314 }
1315
1316 iproc_pcie_enable(pcie);
1317
1318 if (IS_ENABLED(CONFIG_PCI_MSI))
1319 if (iproc_pcie_msi_enable(pcie))
1320 dev_info(dev, "not using iProc MSI\n");
1321
1322 list_splice_init(res, &host->windows);
1323 host->busnr = 0;
1324 host->dev.parent = dev;
1325 host->ops = &iproc_pcie_ops;
1326 host->sysdata = sysdata;
1327 host->map_irq = pcie->map_irq;
1328 host->swizzle_irq = pci_common_swizzle;
1329
1330 ret = pci_scan_root_bus_bridge(host);
1331 if (ret < 0) {
1332 dev_err(dev, "failed to scan host: %d\n", ret);
1333 goto err_power_off_phy;
1334 }
1335
1336 pci_assign_unassigned_bus_resources(host->bus);
1337
1338 pcie->root_bus = host->bus;
1339
1340 list_for_each_entry(child, &host->bus->children, node)
1341 pcie_bus_configure_settings(child);
1342
1343 pci_bus_add_devices(host->bus);
1344
1345 return 0;
1346
1347err_power_off_phy:
1348 phy_power_off(pcie->phy);
1349err_exit_phy:
1350 phy_exit(pcie->phy);
1351 return ret;
1352}
1353EXPORT_SYMBOL(iproc_pcie_setup);
1354
1355int iproc_pcie_remove(struct iproc_pcie *pcie)
1356{
1357 pci_stop_root_bus(pcie->root_bus);
1358 pci_remove_root_bus(pcie->root_bus);
1359
1360 iproc_pcie_msi_disable(pcie);
1361
1362 phy_power_off(pcie->phy);
1363 phy_exit(pcie->phy);
1364
1365 return 0;
1366}
1367EXPORT_SYMBOL(iproc_pcie_remove);
1368
1369MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
1370MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver");
1371MODULE_LICENSE("GPL v2");
1372