1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020 Broadcom
10 #include <generic-phy.h>
14 #include <dm/device_compat.h>
15 #include <linux/log2.h>
17 #define EP_PERST_SOURCE_SELECT_SHIFT 2
18 #define EP_PERST_SOURCE_SELECT BIT(EP_PERST_SOURCE_SELECT_SHIFT)
19 #define EP_MODE_SURVIVE_PERST_SHIFT 1
20 #define EP_MODE_SURVIVE_PERST BIT(EP_MODE_SURVIVE_PERST_SHIFT)
21 #define RC_PCIE_RST_OUTPUT_SHIFT 0
22 #define RC_PCIE_RST_OUTPUT BIT(RC_PCIE_RST_OUTPUT_SHIFT)
24 #define CFG_IND_ADDR_MASK 0x00001ffc
26 #define CFG_ADDR_BUS_NUM_SHIFT 20
27 #define CFG_ADDR_BUS_NUM_MASK 0x0ff00000
28 #define CFG_ADDR_DEV_NUM_SHIFT 15
29 #define CFG_ADDR_DEV_NUM_MASK 0x000f8000
30 #define CFG_ADDR_FUNC_NUM_SHIFT 12
31 #define CFG_ADDR_FUNC_NUM_MASK 0x00007000
32 #define CFG_ADDR_REG_NUM_SHIFT 2
33 #define CFG_ADDR_REG_NUM_MASK 0x00000ffc
34 #define CFG_ADDR_CFG_TYPE_SHIFT 0
35 #define CFG_ADDR_CFG_TYPE_MASK 0x00000003
37 #define IPROC_PCI_PM_CAP 0x48
38 #define IPROC_PCI_PM_CAP_MASK 0xffff
39 #define IPROC_PCI_EXP_CAP 0xac
41 #define IPROC_PCIE_REG_INVALID 0xffff
43 #define PCI_EXP_TYPE_ROOT_PORT 0x4 /* Root Port */
44 #define PCI_EXP_RTCTL 28 /* Root Control */
45 /* CRS Software Visibility capability */
46 #define PCI_EXP_RTCAP_CRSVIS 0x0001
48 #define PCI_EXP_LNKSTA 18 /* Link Status */
49 #define PCI_EXP_LNKSTA_NLW 0x03f0 /* Negotiated Link Width */
51 #define PCIE_PHYLINKUP_SHIFT 3
52 #define PCIE_PHYLINKUP BIT(PCIE_PHYLINKUP_SHIFT)
53 #define PCIE_DL_ACTIVE_SHIFT 2
54 #define PCIE_DL_ACTIVE BIT(PCIE_DL_ACTIVE_SHIFT)
56 /* derive the enum index of the outbound/inbound mapping registers */
57 #define MAP_REG(base_reg, index) ((base_reg) + (index) * 2)
60 * Maximum number of outbound mapping window sizes that can be supported by any
61 * OARR/OMAP mapping pair
63 #define MAX_NUM_OB_WINDOW_SIZES 4
65 #define OARR_VALID_SHIFT 0
66 #define OARR_VALID BIT(OARR_VALID_SHIFT)
67 #define OARR_SIZE_CFG_SHIFT 1
70 * Maximum number of inbound mapping region sizes that can be supported by an
73 #define MAX_NUM_IB_REGION_SIZES 9
75 #define IMAP_VALID_SHIFT 0
76 #define IMAP_VALID BIT(IMAP_VALID_SHIFT)
78 #define APB_ERR_EN_SHIFT 0
79 #define APB_ERR_EN BIT(APB_ERR_EN_SHIFT)
82 * iProc PCIe host registers
85 /* clock/reset signal control */
86 IPROC_PCIE_CLK_CTRL = 0,
89 * To allow MSI to be steered to an external MSI controller (e.g., ARM
92 IPROC_PCIE_MSI_GIC_MODE,
95 * IPROC_PCIE_MSI_BASE_ADDR and IPROC_PCIE_MSI_WINDOW_SIZE define the
96 * window where the MSI posted writes are written, for the writes to be
97 * interpreted as MSI writes.
99 IPROC_PCIE_MSI_BASE_ADDR,
100 IPROC_PCIE_MSI_WINDOW_SIZE,
103 * To hold the address of the register where the MSI writes are
104 * programed. When ARM GICv3 ITS is used, this should be programmed
105 * with the address of the GITS_TRANSLATER register.
107 IPROC_PCIE_MSI_ADDR_LO,
108 IPROC_PCIE_MSI_ADDR_HI,
111 IPROC_PCIE_MSI_EN_CFG,
113 /* allow access to root complex configuration space */
114 IPROC_PCIE_CFG_IND_ADDR,
115 IPROC_PCIE_CFG_IND_DATA,
117 /* allow access to device configuration space */
125 /* outbound address mapping */
135 /* inbound address mapping */
147 /* config read status */
148 IPROC_PCIE_CFG_RD_STATUS,
151 IPROC_PCIE_LINK_STATUS,
153 /* enable APB error for unsupported requests */
154 IPROC_PCIE_APB_ERR_EN,
156 /* Ordering Mode configuration registers */
157 IPROC_PCIE_ORDERING_CFG,
158 IPROC_PCIE_IMAP0_RO_CONTROL,
159 IPROC_PCIE_IMAP1_RO_CONTROL,
160 IPROC_PCIE_IMAP2_RO_CONTROL,
161 IPROC_PCIE_IMAP3_RO_CONTROL,
162 IPROC_PCIE_IMAP4_RO_CONTROL,
164 /* total number of core registers */
165 IPROC_PCIE_MAX_NUM_REG,
168 /* iProc PCIe PAXB v2 registers */
169 static const u16 iproc_pcie_reg_paxb_v2[] = {
170 [IPROC_PCIE_CLK_CTRL] = 0x000,
171 [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
172 [IPROC_PCIE_CFG_IND_DATA] = 0x124,
173 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
174 [IPROC_PCIE_CFG_DATA] = 0x1fc,
175 [IPROC_PCIE_INTX_EN] = 0x330,
176 [IPROC_PCIE_INTX_CSR] = 0x334,
177 [IPROC_PCIE_OARR0] = 0xd20,
178 [IPROC_PCIE_OMAP0] = 0xd40,
179 [IPROC_PCIE_OARR1] = 0xd28,
180 [IPROC_PCIE_OMAP1] = 0xd48,
181 [IPROC_PCIE_OARR2] = 0xd60,
182 [IPROC_PCIE_OMAP2] = 0xd68,
183 [IPROC_PCIE_OARR3] = 0xdf0,
184 [IPROC_PCIE_OMAP3] = 0xdf8,
185 [IPROC_PCIE_IARR0] = 0xd00,
186 [IPROC_PCIE_IMAP0] = 0xc00,
187 [IPROC_PCIE_IARR2] = 0xd10,
188 [IPROC_PCIE_IMAP2] = 0xcc0,
189 [IPROC_PCIE_IARR3] = 0xe00,
190 [IPROC_PCIE_IMAP3] = 0xe08,
191 [IPROC_PCIE_IARR4] = 0xe68,
192 [IPROC_PCIE_IMAP4] = 0xe70,
193 [IPROC_PCIE_CFG_RD_STATUS] = 0xee0,
194 [IPROC_PCIE_LINK_STATUS] = 0xf0c,
195 [IPROC_PCIE_APB_ERR_EN] = 0xf40,
196 [IPROC_PCIE_ORDERING_CFG] = 0x2000,
197 [IPROC_PCIE_IMAP0_RO_CONTROL] = 0x201c,
198 [IPROC_PCIE_IMAP1_RO_CONTROL] = 0x2020,
199 [IPROC_PCIE_IMAP2_RO_CONTROL] = 0x2024,
200 [IPROC_PCIE_IMAP3_RO_CONTROL] = 0x2028,
201 [IPROC_PCIE_IMAP4_RO_CONTROL] = 0x202c,
204 /* iProc PCIe PAXC v2 registers */
205 static const u16 iproc_pcie_reg_paxc_v2[] = {
206 [IPROC_PCIE_MSI_GIC_MODE] = 0x050,
207 [IPROC_PCIE_MSI_BASE_ADDR] = 0x074,
208 [IPROC_PCIE_MSI_WINDOW_SIZE] = 0x078,
209 [IPROC_PCIE_MSI_ADDR_LO] = 0x07c,
210 [IPROC_PCIE_MSI_ADDR_HI] = 0x080,
211 [IPROC_PCIE_MSI_EN_CFG] = 0x09c,
212 [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0,
213 [IPROC_PCIE_CFG_IND_DATA] = 0x1f4,
214 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
215 [IPROC_PCIE_CFG_DATA] = 0x1fc,
219 * List of device IDs of controllers that have corrupted
220 * capability list that require SW fixup
222 static const u16 iproc_pcie_corrupt_cap_did[] = {
229 enum iproc_pcie_type {
236 * struct iproc_pcie_ob - iProc PCIe outbound mapping
238 * @axi_offset: offset from the AXI address to the internal address used by
239 * the iProc PCIe core
240 * @nr_windows: total number of supported outbound mapping windows
242 struct iproc_pcie_ob {
243 resource_size_t axi_offset;
244 unsigned int nr_windows;
248 * struct iproc_pcie_ib - iProc PCIe inbound mapping
250 * @nr_regions: total number of supported inbound mapping regions
252 struct iproc_pcie_ib {
253 unsigned int nr_regions;
257 * struct iproc_pcie_ob_map - outbound mapping controller specific parameters
259 * @window_sizes: list of supported outbound mapping window sizes in MB
260 * @nr_sizes: number of supported outbound mapping window sizes
262 struct iproc_pcie_ob_map {
263 resource_size_t window_sizes[MAX_NUM_OB_WINDOW_SIZES];
264 unsigned int nr_sizes;
267 static const struct iproc_pcie_ob_map paxb_v2_ob_map[] = {
270 .window_sizes = { 128, 256 },
275 .window_sizes = { 128, 256 },
280 .window_sizes = { 128, 256, 512, 1024 },
285 .window_sizes = { 128, 256, 512, 1024 },
291 * iProc PCIe inbound mapping type
293 enum iproc_pcie_ib_map_type {
295 IPROC_PCIE_IB_MAP_MEM = 0,
297 /* for device I/O memory */
298 IPROC_PCIE_IB_MAP_IO,
300 /* invalid or unused */
301 IPROC_PCIE_IB_MAP_INVALID
305 * struct iproc_pcie_ib_map - inbound mapping controller specific parameters
307 * @type: inbound mapping region type
308 * @size_unit: inbound mapping region size unit, could be SZ_1K, SZ_1M, or SZ_1G
309 * @region_sizes: list of supported inbound mapping region sizes in KB, MB, or
310 * GB, depedning on the size unit
311 * @nr_sizes: number of supported inbound mapping region sizes
312 * @nr_windows: number of supported inbound mapping windows for the region
313 * @imap_addr_offset: register offset between the upper and lower 32-bit
314 * IMAP address registers
315 * @imap_window_offset: register offset between each IMAP window
317 struct iproc_pcie_ib_map {
318 enum iproc_pcie_ib_map_type type;
319 unsigned int size_unit;
320 resource_size_t region_sizes[MAX_NUM_IB_REGION_SIZES];
321 unsigned int nr_sizes;
322 unsigned int nr_windows;
323 u16 imap_addr_offset;
324 u16 imap_window_offset;
327 static const struct iproc_pcie_ib_map paxb_v2_ib_map[] = {
330 .type = IPROC_PCIE_IB_MAP_IO,
332 .region_sizes = { 32 },
335 .imap_addr_offset = 0x40,
336 .imap_window_offset = 0x4,
339 /* IARR1/IMAP1 (currently unused) */
340 .type = IPROC_PCIE_IB_MAP_INVALID,
344 .type = IPROC_PCIE_IB_MAP_MEM,
346 .region_sizes = { 64, 128, 256, 512, 1024, 2048, 4096, 8192,
350 .imap_addr_offset = 0x4,
351 .imap_window_offset = 0x8,
355 .type = IPROC_PCIE_IB_MAP_MEM,
357 .region_sizes = { 1, 2, 4, 8, 16, 32 },
360 .imap_addr_offset = 0x4,
361 .imap_window_offset = 0x8,
365 .type = IPROC_PCIE_IB_MAP_MEM,
367 .region_sizes = { 32, 64, 128, 256, 512 },
370 .imap_addr_offset = 0x4,
371 .imap_window_offset = 0x8,
376 * struct iproc_pcie - iproc pcie device instance
378 * @dev: pointer to pcie udevice
379 * @base: device I/O base address
380 * @type: pci device type, PAXC or PAXB
381 * @reg_offsets: pointer to pcie host register
382 * @fix_paxc_cap: paxc capability
383 * @need_ob_cfg: outbound mapping status
384 * @ob: pcie outbound mapping
385 * @ob_map: pointer to outbound mapping parameters
386 * @need_ib_cfg: inbound mapping status
387 * @ib: pcie inbound mapping
388 * @ib_map: pointer to inbound mapping parameters
389 * @ep_is_internal: ep status
391 * @link_is_active: link up status
392 * @has_apb_err_disable: apb error status
397 enum iproc_pcie_type type;
401 struct iproc_pcie_ob ob;
402 const struct iproc_pcie_ob_map *ob_map;
404 struct iproc_pcie_ib ib;
405 const struct iproc_pcie_ib_map *ib_map;
409 bool has_apb_err_disable;
412 static inline bool iproc_pcie_reg_is_invalid(u16 reg_offset)
414 return !!(reg_offset == IPROC_PCIE_REG_INVALID);
417 static inline u16 iproc_pcie_reg_offset(struct iproc_pcie *pcie,
418 enum iproc_pcie_reg reg)
420 return pcie->reg_offsets[reg];
423 static inline u32 iproc_pcie_read_reg(struct iproc_pcie *pcie,
424 enum iproc_pcie_reg reg)
426 u16 offset = iproc_pcie_reg_offset(pcie, reg);
428 if (iproc_pcie_reg_is_invalid(offset))
431 return readl(pcie->base + offset);
434 static inline void iproc_pcie_write_reg(struct iproc_pcie *pcie,
435 enum iproc_pcie_reg reg, u32 val)
437 u16 offset = iproc_pcie_reg_offset(pcie, reg);
439 if (iproc_pcie_reg_is_invalid(offset))
442 writel(val, pcie->base + offset);
445 static int iproc_pcie_map_ep_cfg_reg(const struct udevice *udev, pci_dev_t bdf,
446 uint where, void **paddress)
448 struct iproc_pcie *pcie = dev_get_priv(udev);
449 unsigned int busno = PCI_BUS(bdf);
450 unsigned int slot = PCI_DEV(bdf);
451 unsigned int fn = PCI_FUNC(bdf);
456 /* root complex access */
458 if (slot > 0 || fn > 0)
461 iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_IND_ADDR,
462 where & CFG_IND_ADDR_MASK);
463 offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_IND_DATA);
464 if (iproc_pcie_reg_is_invalid(offset))
467 *paddress = (pcie->base + offset);
471 if (!pcie->link_is_active)
474 /* EP device access */
475 val = (busno << CFG_ADDR_BUS_NUM_SHIFT) |
476 (slot << CFG_ADDR_DEV_NUM_SHIFT) |
477 (fn << CFG_ADDR_FUNC_NUM_SHIFT) |
478 (where & CFG_ADDR_REG_NUM_MASK) |
479 (1 & CFG_ADDR_CFG_TYPE_MASK);
481 iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_ADDR, val);
482 offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_DATA);
484 if (iproc_pcie_reg_is_invalid(offset))
487 *paddress = (pcie->base + offset);
492 static void iproc_pcie_fix_cap(struct iproc_pcie *pcie, int where, ulong *val)
496 switch (where & ~0x3) {
501 * Activate fixup for those controllers that have corrupted
502 * capability list registers
504 for (i = 0; i < ARRAY_SIZE(iproc_pcie_corrupt_cap_did); i++)
505 if (dev_id == iproc_pcie_corrupt_cap_did[i])
506 pcie->fix_paxc_cap = true;
509 case IPROC_PCI_PM_CAP:
510 if (pcie->fix_paxc_cap) {
511 /* advertise PM, force next capability to PCIe */
512 *val &= ~IPROC_PCI_PM_CAP_MASK;
513 *val |= IPROC_PCI_EXP_CAP << 8 | PCI_CAP_ID_PM;
517 case IPROC_PCI_EXP_CAP:
518 if (pcie->fix_paxc_cap) {
519 /* advertise root port, version 2, terminate here */
520 *val = (PCI_EXP_TYPE_ROOT_PORT << 4 | 2) << 16 |
525 case IPROC_PCI_EXP_CAP + PCI_EXP_RTCTL:
526 /* Don't advertise CRS SV support */
527 *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16);
535 static int iproc_pci_raw_config_read32(struct iproc_pcie *pcie,
536 unsigned int devfn, int where,
542 ret = iproc_pcie_map_ep_cfg_reg(pcie->dev, devfn, where & ~0x3, &addr);
551 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
556 static int iproc_pci_raw_config_write32(struct iproc_pcie *pcie,
557 unsigned int devfn, int where,
564 ret = iproc_pcie_map_ep_cfg_reg(pcie->dev, devfn, where & ~0x3, &addr);
573 mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
574 tmp = readl(addr) & mask;
575 tmp |= val << ((where & 0x3) * 8);
581 * iproc_pcie_apb_err_disable() - configure apb error
583 * APB error forwarding can be disabled during access of configuration
584 * registers of the endpoint device, to prevent unsupported requests
585 * (typically seen during enumeration with multi-function devices) from
586 * triggering a system exception.
590 * @disabled: flag to enable/disabled apb error
592 static inline void iproc_pcie_apb_err_disable(const struct udevice *bus,
593 pci_dev_t bdf, bool disable)
595 struct iproc_pcie *pcie = dev_get_priv(bus);
598 if (PCI_BUS(bdf) && pcie->has_apb_err_disable) {
599 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_APB_ERR_EN);
604 iproc_pcie_write_reg(pcie, IPROC_PCIE_APB_ERR_EN, val);
608 static int iproc_pcie_config_read32(const struct udevice *bus, pci_dev_t bdf,
609 uint offset, ulong *valuep,
610 enum pci_size_t size)
612 struct iproc_pcie *pcie = dev_get_priv(bus);
616 iproc_pcie_apb_err_disable(bus, bdf, true);
617 ret = pci_generic_mmap_read_config(bus, iproc_pcie_map_ep_cfg_reg,
618 bdf, offset, &data, PCI_SIZE_32);
619 iproc_pcie_apb_err_disable(bus, bdf, false);
620 if (size <= PCI_SIZE_16)
621 *valuep = (data >> (8 * (offset & 3))) &
622 ((1 << (BIT(size) * 8)) - 1);
626 if (!ret && PCI_BUS(bdf) == 0)
627 iproc_pcie_fix_cap(pcie, offset, valuep);
632 static int iproc_pcie_config_write32(struct udevice *bus, pci_dev_t bdf,
633 uint offset, ulong value,
634 enum pci_size_t size)
640 ret = iproc_pcie_map_ep_cfg_reg(bus, bdf, offset, &addr);
644 if (size == PCI_SIZE_32) {
649 iproc_pcie_apb_err_disable(bus, bdf, true);
650 mask = ~(((1 << (BIT(size) * 8)) - 1) << ((offset & 0x3) * 8));
651 tmp = readl(addr) & mask;
652 tmp |= (value << ((offset & 0x3) * 8));
654 iproc_pcie_apb_err_disable(bus, bdf, false);
659 const static struct dm_pci_ops iproc_pcie_ops = {
660 .read_config = iproc_pcie_config_read32,
661 .write_config = iproc_pcie_config_write32,
664 static int iproc_pcie_rev_init(struct iproc_pcie *pcie)
666 unsigned int reg_idx;
670 switch (pcie->type) {
671 case IPROC_PCIE_PAXC_V2:
672 pcie->ep_is_internal = true;
673 regs = iproc_pcie_reg_paxc_v2;
674 num_elements = ARRAY_SIZE(iproc_pcie_reg_paxc_v2);
676 case IPROC_PCIE_PAXB_V2:
677 regs = iproc_pcie_reg_paxb_v2;
678 num_elements = ARRAY_SIZE(iproc_pcie_reg_paxb_v2);
679 pcie->has_apb_err_disable = true;
680 if (pcie->need_ob_cfg) {
681 pcie->ob.axi_offset = 0;
682 pcie->ob_map = paxb_v2_ob_map;
683 pcie->ob.nr_windows = ARRAY_SIZE(paxb_v2_ob_map);
685 pcie->need_ib_cfg = true;
686 pcie->ib.nr_regions = ARRAY_SIZE(paxb_v2_ib_map);
687 pcie->ib_map = paxb_v2_ib_map;
690 dev_dbg(pcie->dev, "incompatible iProc PCIe interface\n");
694 pcie->reg_offsets = calloc(IPROC_PCIE_MAX_NUM_REG,
695 sizeof(*pcie->reg_offsets));
696 if (!pcie->reg_offsets)
699 /* go through the register table and populate all valid registers */
700 pcie->reg_offsets[0] = (pcie->type == IPROC_PCIE_PAXC_V2) ?
701 IPROC_PCIE_REG_INVALID : regs[0];
702 for (reg_idx = 1; reg_idx < num_elements; reg_idx++)
703 pcie->reg_offsets[reg_idx] = regs[reg_idx] ?
704 regs[reg_idx] : IPROC_PCIE_REG_INVALID;
709 static inline bool iproc_pcie_ob_is_valid(struct iproc_pcie *pcie,
714 val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_OARR0, window_idx));
716 return !!(val & OARR_VALID);
719 static inline int iproc_pcie_ob_write(struct iproc_pcie *pcie, int window_idx,
720 int size_idx, u64 axi_addr, u64 pci_addr)
722 u16 oarr_offset, omap_offset;
725 * Derive the OARR/OMAP offset from the first pair (OARR0/OMAP0) based
728 oarr_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OARR0,
730 omap_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OMAP0,
732 if (iproc_pcie_reg_is_invalid(oarr_offset) ||
733 iproc_pcie_reg_is_invalid(omap_offset))
737 * Program the OARR registers. The upper 32-bit OARR register is
738 * always right after the lower 32-bit OARR register.
740 writel(lower_32_bits(axi_addr) | (size_idx << OARR_SIZE_CFG_SHIFT) |
741 OARR_VALID, pcie->base + oarr_offset);
742 writel(upper_32_bits(axi_addr), pcie->base + oarr_offset + 4);
744 /* now program the OMAP registers */
745 writel(lower_32_bits(pci_addr), pcie->base + omap_offset);
746 writel(upper_32_bits(pci_addr), pcie->base + omap_offset + 4);
748 debug("ob window [%d]: offset 0x%x axi %pap pci %pap\n",
749 window_idx, oarr_offset, &axi_addr, &pci_addr);
750 debug("oarr lo 0x%x oarr hi 0x%x\n",
751 readl(pcie->base + oarr_offset),
752 readl(pcie->base + oarr_offset + 4));
753 debug("omap lo 0x%x omap hi 0x%x\n",
754 readl(pcie->base + omap_offset),
755 readl(pcie->base + omap_offset + 4));
761 * iproc_pcie_setup_ob() - setup outbound address mapping
763 * Some iProc SoCs require the SW to configure the outbound address mapping
764 * Outbound address translation:
766 * iproc_pcie_address = axi_address - axi_offset
767 * OARR = iproc_pcie_address
769 * axi_addr -> iproc_pcie_address -> OARR -> OMAP -> pci_address
772 * @axi_addr: axi address to be translated
773 * @pci_addr: pci address
776 * @return: 0 on success and -ve on failure
778 static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr,
779 u64 pci_addr, resource_size_t size)
781 struct iproc_pcie_ob *ob = &pcie->ob;
782 int ret = -EINVAL, window_idx, size_idx;
784 if (axi_addr < ob->axi_offset) {
785 pr_err("axi address %pap less than offset %pap\n",
786 &axi_addr, &ob->axi_offset);
791 * Translate the AXI address to the internal address used by the iProc
792 * PCIe core before programming the OARR
794 axi_addr -= ob->axi_offset;
796 /* iterate through all OARR/OMAP mapping windows */
797 for (window_idx = ob->nr_windows - 1; window_idx >= 0; window_idx--) {
798 const struct iproc_pcie_ob_map *ob_map =
799 &pcie->ob_map[window_idx];
802 * If current outbound window is already in use, move on to the
805 if (iproc_pcie_ob_is_valid(pcie, window_idx))
809 * Iterate through all supported window sizes within the
810 * OARR/OMAP pair to find a match. Go through the window sizes
811 * in a descending order.
813 for (size_idx = ob_map->nr_sizes - 1; size_idx >= 0;
815 resource_size_t window_size =
816 ob_map->window_sizes[size_idx] * SZ_1M;
819 * Keep iterating until we reach the last window and
820 * with the minimal window size at index zero. In this
821 * case, we take a compromise by mapping it using the
822 * minimum window size that can be supported
824 if (size < window_size) {
825 if (size_idx > 0 || window_idx > 0)
829 * For the corner case of reaching the minimal
830 * window size that can be supported on the
833 axi_addr = ALIGN_DOWN(axi_addr, window_size);
834 pci_addr = ALIGN_DOWN(pci_addr, window_size);
838 if (!IS_ALIGNED(axi_addr, window_size) ||
839 !IS_ALIGNED(pci_addr, window_size)) {
840 pr_err("axi %pap or pci %pap not aligned\n",
841 &axi_addr, &pci_addr);
846 * Match found! Program both OARR and OMAP and mark
847 * them as a valid entry.
849 ret = iproc_pcie_ob_write(pcie, window_idx, size_idx,
859 * If we are here, we are done with the current window,
860 * but not yet finished all mappings. Need to move on
861 * to the next window.
863 axi_addr += window_size;
864 pci_addr += window_size;
870 pr_err("unable to configure outbound mapping\n");
871 pr_err("axi %pap, axi offset %pap, pci %pap, res size %pap\n",
872 &axi_addr, &ob->axi_offset, &pci_addr, &size);
877 static int iproc_pcie_map_ranges(struct udevice *dev)
879 struct iproc_pcie *pcie = dev_get_priv(dev);
880 struct udevice *bus = pci_get_controller(dev);
881 struct pci_controller *hose = dev_get_uclass_priv(bus);
884 for (i = 0; i < hose->region_count; i++) {
885 if (hose->regions[i].flags == PCI_REGION_MEM ||
886 hose->regions[i].flags == PCI_REGION_PREFETCH) {
887 debug("%d: bus_addr %p, axi_addr %p, size 0x%lx\n",
888 i, &hose->regions[i].bus_start,
889 &hose->regions[i].phys_start,
890 hose->regions[i].size);
891 ret = iproc_pcie_setup_ob(pcie,
892 hose->regions[i].phys_start,
893 hose->regions[i].bus_start,
894 hose->regions[i].size);
903 static inline bool iproc_pcie_ib_is_in_use(struct iproc_pcie *pcie,
906 const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
909 val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_IARR0, region_idx));
911 return !!(val & (BIT(ib_map->nr_sizes) - 1));
915 iproc_pcie_ib_check_type(const struct iproc_pcie_ib_map *ib_map,
916 enum iproc_pcie_ib_map_type type)
918 return !!(ib_map->type == type);
921 static int iproc_pcie_ib_write(struct iproc_pcie *pcie, int region_idx,
922 int size_idx, int nr_windows, u64 axi_addr,
923 u64 pci_addr, resource_size_t size)
925 const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
926 u16 iarr_offset, imap_offset;
930 iarr_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_IARR0,
932 imap_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_IMAP0,
934 if (iproc_pcie_reg_is_invalid(iarr_offset) ||
935 iproc_pcie_reg_is_invalid(imap_offset))
938 debug("ib region [%d]: offset 0x%x axi %pap pci %pap\n",
939 region_idx, iarr_offset, &axi_addr, &pci_addr);
942 * Program the IARR registers. The upper 32-bit IARR register is
943 * always right after the lower 32-bit IARR register.
945 writel(lower_32_bits(pci_addr) | BIT(size_idx),
946 pcie->base + iarr_offset);
947 writel(upper_32_bits(pci_addr), pcie->base + iarr_offset + 4);
949 debug("iarr lo 0x%x iarr hi 0x%x\n",
950 readl(pcie->base + iarr_offset),
951 readl(pcie->base + iarr_offset + 4));
954 * Now program the IMAP registers. Each IARR region may have one or
957 size >>= ilog2(nr_windows);
958 for (window_idx = 0; window_idx < nr_windows; window_idx++) {
959 val = readl(pcie->base + imap_offset);
960 val |= lower_32_bits(axi_addr) | IMAP_VALID;
961 writel(val, pcie->base + imap_offset);
962 writel(upper_32_bits(axi_addr),
963 pcie->base + imap_offset + ib_map->imap_addr_offset);
965 debug("imap window [%d] lo 0x%x hi 0x%x\n",
966 window_idx, readl(pcie->base + imap_offset),
967 readl(pcie->base + imap_offset +
968 ib_map->imap_addr_offset));
970 imap_offset += ib_map->imap_window_offset;
978 * iproc_pcie_setup_ib() - setup inbound address mapping
981 * @axi_addr: axi address to be translated
982 * @pci_addr: pci address
984 * @type: inbound mapping type
986 * @return: 0 on success and -ve on failure
988 static int iproc_pcie_setup_ib(struct iproc_pcie *pcie, u64 axi_addr,
989 u64 pci_addr, resource_size_t size,
990 enum iproc_pcie_ib_map_type type)
992 struct iproc_pcie_ib *ib = &pcie->ib;
994 unsigned int region_idx, size_idx;
996 /* iterate through all IARR mapping regions */
997 for (region_idx = 0; region_idx < ib->nr_regions; region_idx++) {
998 const struct iproc_pcie_ib_map *ib_map =
999 &pcie->ib_map[region_idx];
1002 * If current inbound region is already in use or not a
1003 * compatible type, move on to the next.
1005 if (iproc_pcie_ib_is_in_use(pcie, region_idx) ||
1006 !iproc_pcie_ib_check_type(ib_map, type))
1009 /* iterate through all supported region sizes to find a match */
1010 for (size_idx = 0; size_idx < ib_map->nr_sizes; size_idx++) {
1011 resource_size_t region_size =
1012 ib_map->region_sizes[size_idx] * ib_map->size_unit;
1014 if (size != region_size)
1017 if (!IS_ALIGNED(axi_addr, region_size) ||
1018 !IS_ALIGNED(pci_addr, region_size)) {
1019 pr_err("axi %pap or pci %pap not aligned\n",
1020 &axi_addr, &pci_addr);
1024 /* Match found! Program IARR and all IMAP windows. */
1025 ret = iproc_pcie_ib_write(pcie, region_idx, size_idx,
1026 ib_map->nr_windows, axi_addr,
1037 pr_err("unable to configure inbound mapping\n");
1038 pr_err("axi %pap, pci %pap, res size %pap\n",
1039 &axi_addr, &pci_addr, &size);
1044 static int iproc_pcie_map_dma_ranges(struct iproc_pcie *pcie)
1047 struct pci_region regions;
1050 while (!pci_get_dma_regions(pcie->dev, ®ions, i)) {
1052 "dma %d: bus_addr %#lx, axi_addr %#llx, size %#lx\n",
1053 i, regions.bus_start, regions.phys_start, regions.size);
1055 /* Each range entry corresponds to an inbound mapping region */
1056 ret = iproc_pcie_setup_ib(pcie, regions.phys_start,
1059 IPROC_PCIE_IB_MAP_MEM);
1067 static void iproc_pcie_reset_map_regs(struct iproc_pcie *pcie)
1069 struct iproc_pcie_ib *ib = &pcie->ib;
1070 struct iproc_pcie_ob *ob = &pcie->ob;
1071 int window_idx, region_idx;
1073 if (pcie->ep_is_internal)
1076 /* iterate through all OARR mapping regions */
1077 for (window_idx = ob->nr_windows - 1; window_idx >= 0; window_idx--) {
1078 iproc_pcie_write_reg(pcie, MAP_REG(IPROC_PCIE_OARR0,
1082 /* iterate through all IARR mapping regions */
1083 for (region_idx = 0; region_idx < ib->nr_regions; region_idx++) {
1084 iproc_pcie_write_reg(pcie, MAP_REG(IPROC_PCIE_IARR0,
1089 static void iproc_pcie_reset(struct iproc_pcie *pcie)
1094 * PAXC and the internal emulated endpoint device downstream should not
1095 * be reset. If firmware has been loaded on the endpoint device at an
1096 * earlier boot stage, reset here causes issues.
1098 if (pcie->ep_is_internal)
1102 * Select perst_b signal as reset source. Put the device into reset,
1103 * and then bring it out of reset
1105 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
1106 val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST &
1107 ~RC_PCIE_RST_OUTPUT;
1108 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
1111 val |= RC_PCIE_RST_OUTPUT;
1112 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
1116 static inline bool iproc_pcie_link_is_active(struct iproc_pcie *pcie)
1120 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_LINK_STATUS);
1121 return !!((val & PCIE_PHYLINKUP) && (val & PCIE_DL_ACTIVE));
1124 static int iproc_pcie_check_link(struct iproc_pcie *pcie)
1126 u32 link_status, class;
1128 pcie->link_is_active = false;
1129 /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */
1130 #define PCI_BRIDGE_CTRL_REG_OFFSET 0x43c
1131 #define PCI_CLASS_BRIDGE_MASK 0xffff00
1132 #define PCI_CLASS_BRIDGE_SHIFT 8
1133 iproc_pci_raw_config_read32(pcie, 0,
1134 PCI_BRIDGE_CTRL_REG_OFFSET,
1136 class &= ~PCI_CLASS_BRIDGE_MASK;
1137 class |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT);
1138 iproc_pci_raw_config_write32(pcie, 0,
1139 PCI_BRIDGE_CTRL_REG_OFFSET,
1143 * PAXC connects to emulated endpoint devices directly and does not
1144 * have a Serdes. Therefore skip the link detection logic here.
1146 if (pcie->ep_is_internal) {
1147 pcie->link_is_active = true;
1151 if (!iproc_pcie_link_is_active(pcie)) {
1152 pr_err("PHY or data link is INACTIVE!\n");
1156 #define PCI_TARGET_LINK_SPEED_MASK 0xf
1157 #define PCI_TARGET_LINK_WIDTH_MASK 0x3f
1158 #define PCI_TARGET_LINK_WIDTH_OFFSET 0x4
1160 /* check link status to see if link is active */
1161 iproc_pci_raw_config_read32(pcie, 0,
1162 IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA,
1164 if (link_status & PCI_EXP_LNKSTA_NLW)
1165 pcie->link_is_active = true;
1167 if (pcie->link_is_active)
1168 pr_info("link UP @ Speed Gen-%d and width-x%d\n",
1169 link_status & PCI_TARGET_LINK_SPEED_MASK,
1170 (link_status >> PCI_TARGET_LINK_WIDTH_OFFSET) &
1171 PCI_TARGET_LINK_WIDTH_MASK);
1173 pr_info("link DOWN\n");
1178 static int iproc_pcie_probe(struct udevice *dev)
1180 struct iproc_pcie *pcie = dev_get_priv(dev);
1183 pcie->type = (enum iproc_pcie_type)dev_get_driver_data(dev);
1184 debug("PAX type %d\n", pcie->type);
1185 pcie->base = dev_read_addr_ptr(dev);
1186 debug("PAX reg base %p\n", pcie->base);
1191 if (dev_read_bool(dev, "brcm,pcie-ob"))
1192 pcie->need_ob_cfg = true;
1195 ret = iproc_pcie_rev_init(pcie);
1199 if (!pcie->ep_is_internal) {
1200 ret = generic_phy_get_by_name(dev, "pcie-phy", &pcie->phy);
1202 ret = generic_phy_init(&pcie->phy);
1204 pr_err("failed to init %s PHY\n", dev->name);
1208 ret = generic_phy_power_on(&pcie->phy);
1210 pr_err("power on %s PHY failed\n", dev->name);
1216 iproc_pcie_reset(pcie);
1218 if (pcie->need_ob_cfg) {
1219 ret = iproc_pcie_map_ranges(dev);
1221 pr_err("outbound map failed\n");
1222 goto err_power_off_phy;
1226 if (pcie->need_ib_cfg) {
1227 ret = iproc_pcie_map_dma_ranges(pcie);
1229 pr_err("inbound map failed\n");
1230 goto err_power_off_phy;
1234 if (iproc_pcie_check_link(pcie))
1235 pr_info("no PCIe EP device detected\n");
1240 generic_phy_power_off(&pcie->phy);
1242 generic_phy_exit(&pcie->phy);
1246 static int iproc_pcie_remove(struct udevice *dev)
1248 struct iproc_pcie *pcie = dev_get_priv(dev);
1251 iproc_pcie_reset_map_regs(pcie);
1253 if (generic_phy_valid(&pcie->phy)) {
1254 ret = generic_phy_power_off(&pcie->phy);
1256 pr_err("failed to power off PCIe phy\n");
1260 ret = generic_phy_exit(&pcie->phy);
1262 pr_err("failed to power off PCIe phy\n");
1270 static const struct udevice_id pci_iproc_ids[] = {
1271 { .compatible = "brcm,iproc-pcie-paxb-v2",
1272 .data = IPROC_PCIE_PAXB_V2 },
1273 { .compatible = "brcm,iproc-pcie-paxc-v2",
1274 .data = IPROC_PCIE_PAXC_V2 },
1278 U_BOOT_DRIVER(pci_iproc) = {
1279 .name = "pci_iproc",
1281 .of_match = pci_iproc_ids,
1282 .ops = &iproc_pcie_ops,
1283 .probe = iproc_pcie_probe,
1284 .remove = iproc_pcie_remove,
1285 .priv_auto = sizeof(struct iproc_pcie),
1286 .flags = DM_REMOVE_OS_PREPARE,