1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020-2021 Broadcom
10 #include <generic-phy.h>
14 #include <dm/device_compat.h>
15 #include <linux/delay.h>
16 #include <linux/log2.h>
18 #define EP_PERST_SOURCE_SELECT_SHIFT 2
19 #define EP_PERST_SOURCE_SELECT BIT(EP_PERST_SOURCE_SELECT_SHIFT)
20 #define EP_MODE_SURVIVE_PERST_SHIFT 1
21 #define EP_MODE_SURVIVE_PERST BIT(EP_MODE_SURVIVE_PERST_SHIFT)
22 #define RC_PCIE_RST_OUTPUT_SHIFT 0
23 #define RC_PCIE_RST_OUTPUT BIT(RC_PCIE_RST_OUTPUT_SHIFT)
25 #define CFG_IND_ADDR_MASK 0x00001ffc
27 #define CFG_ADDR_BUS_NUM_SHIFT 20
28 #define CFG_ADDR_BUS_NUM_MASK 0x0ff00000
29 #define CFG_ADDR_DEV_NUM_SHIFT 15
30 #define CFG_ADDR_DEV_NUM_MASK 0x000f8000
31 #define CFG_ADDR_FUNC_NUM_SHIFT 12
32 #define CFG_ADDR_FUNC_NUM_MASK 0x00007000
33 #define CFG_ADDR_REG_NUM_SHIFT 2
34 #define CFG_ADDR_REG_NUM_MASK 0x00000ffc
35 #define CFG_ADDR_CFG_TYPE_SHIFT 0
36 #define CFG_ADDR_CFG_TYPE_MASK 0x00000003
38 #define IPROC_PCI_PM_CAP 0x48
39 #define IPROC_PCI_PM_CAP_MASK 0xffff
40 #define IPROC_PCI_EXP_CAP 0xac
42 #define IPROC_PCIE_REG_INVALID 0xffff
44 #define PCI_EXP_TYPE_ROOT_PORT 0x4 /* Root Port */
45 #define PCI_EXP_RTCTL 28 /* Root Control */
46 /* CRS Software Visibility capability */
47 #define PCI_EXP_RTCAP_CRSVIS 0x0001
49 #define PCI_EXP_LNKSTA 18 /* Link Status */
50 #define PCI_EXP_LNKSTA_NLW 0x03f0 /* Negotiated Link Width */
52 #define PCIE_PHYLINKUP_SHIFT 3
53 #define PCIE_PHYLINKUP BIT(PCIE_PHYLINKUP_SHIFT)
54 #define PCIE_DL_ACTIVE_SHIFT 2
55 #define PCIE_DL_ACTIVE BIT(PCIE_DL_ACTIVE_SHIFT)
57 /* derive the enum index of the outbound/inbound mapping registers */
58 #define MAP_REG(base_reg, index) ((base_reg) + (index) * 2)
61 * Maximum number of outbound mapping window sizes that can be supported by any
62 * OARR/OMAP mapping pair
64 #define MAX_NUM_OB_WINDOW_SIZES 4
66 #define OARR_VALID_SHIFT 0
67 #define OARR_VALID BIT(OARR_VALID_SHIFT)
68 #define OARR_SIZE_CFG_SHIFT 1
71 * Maximum number of inbound mapping region sizes that can be supported by an
74 #define MAX_NUM_IB_REGION_SIZES 9
76 #define IMAP_VALID_SHIFT 0
77 #define IMAP_VALID BIT(IMAP_VALID_SHIFT)
79 #define APB_ERR_EN_SHIFT 0
80 #define APB_ERR_EN BIT(APB_ERR_EN_SHIFT)
83 * iProc PCIe host registers
86 /* clock/reset signal control */
87 IPROC_PCIE_CLK_CTRL = 0,
90 * To allow MSI to be steered to an external MSI controller (e.g., ARM
93 IPROC_PCIE_MSI_GIC_MODE,
96 * IPROC_PCIE_MSI_BASE_ADDR and IPROC_PCIE_MSI_WINDOW_SIZE define the
97 * window where the MSI posted writes are written, for the writes to be
98 * interpreted as MSI writes.
100 IPROC_PCIE_MSI_BASE_ADDR,
101 IPROC_PCIE_MSI_WINDOW_SIZE,
104 * To hold the address of the register where the MSI writes are
105 * programed. When ARM GICv3 ITS is used, this should be programmed
106 * with the address of the GITS_TRANSLATER register.
108 IPROC_PCIE_MSI_ADDR_LO,
109 IPROC_PCIE_MSI_ADDR_HI,
112 IPROC_PCIE_MSI_EN_CFG,
114 /* allow access to root complex configuration space */
115 IPROC_PCIE_CFG_IND_ADDR,
116 IPROC_PCIE_CFG_IND_DATA,
118 /* allow access to device configuration space */
126 /* outbound address mapping */
136 /* inbound address mapping */
148 /* config read status */
149 IPROC_PCIE_CFG_RD_STATUS,
152 IPROC_PCIE_LINK_STATUS,
154 /* enable APB error for unsupported requests */
155 IPROC_PCIE_APB_ERR_EN,
157 /* Ordering Mode configuration registers */
158 IPROC_PCIE_ORDERING_CFG,
159 IPROC_PCIE_IMAP0_RO_CONTROL,
160 IPROC_PCIE_IMAP1_RO_CONTROL,
161 IPROC_PCIE_IMAP2_RO_CONTROL,
162 IPROC_PCIE_IMAP3_RO_CONTROL,
163 IPROC_PCIE_IMAP4_RO_CONTROL,
165 /* total number of core registers */
166 IPROC_PCIE_MAX_NUM_REG,
169 /* iProc PCIe PAXB v2 registers */
170 static const u16 iproc_pcie_reg_paxb_v2[] = {
171 [IPROC_PCIE_CLK_CTRL] = 0x000,
172 [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
173 [IPROC_PCIE_CFG_IND_DATA] = 0x124,
174 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
175 [IPROC_PCIE_CFG_DATA] = 0x1fc,
176 [IPROC_PCIE_INTX_EN] = 0x330,
177 [IPROC_PCIE_INTX_CSR] = 0x334,
178 [IPROC_PCIE_OARR0] = 0xd20,
179 [IPROC_PCIE_OMAP0] = 0xd40,
180 [IPROC_PCIE_OARR1] = 0xd28,
181 [IPROC_PCIE_OMAP1] = 0xd48,
182 [IPROC_PCIE_OARR2] = 0xd60,
183 [IPROC_PCIE_OMAP2] = 0xd68,
184 [IPROC_PCIE_OARR3] = 0xdf0,
185 [IPROC_PCIE_OMAP3] = 0xdf8,
186 [IPROC_PCIE_IARR0] = 0xd00,
187 [IPROC_PCIE_IMAP0] = 0xc00,
188 [IPROC_PCIE_IARR2] = 0xd10,
189 [IPROC_PCIE_IMAP2] = 0xcc0,
190 [IPROC_PCIE_IARR3] = 0xe00,
191 [IPROC_PCIE_IMAP3] = 0xe08,
192 [IPROC_PCIE_IARR4] = 0xe68,
193 [IPROC_PCIE_IMAP4] = 0xe70,
194 [IPROC_PCIE_CFG_RD_STATUS] = 0xee0,
195 [IPROC_PCIE_LINK_STATUS] = 0xf0c,
196 [IPROC_PCIE_APB_ERR_EN] = 0xf40,
197 [IPROC_PCIE_ORDERING_CFG] = 0x2000,
198 [IPROC_PCIE_IMAP0_RO_CONTROL] = 0x201c,
199 [IPROC_PCIE_IMAP1_RO_CONTROL] = 0x2020,
200 [IPROC_PCIE_IMAP2_RO_CONTROL] = 0x2024,
201 [IPROC_PCIE_IMAP3_RO_CONTROL] = 0x2028,
202 [IPROC_PCIE_IMAP4_RO_CONTROL] = 0x202c,
205 /* iProc PCIe PAXC v2 registers */
206 static const u16 iproc_pcie_reg_paxc_v2[] = {
207 [IPROC_PCIE_MSI_GIC_MODE] = 0x050,
208 [IPROC_PCIE_MSI_BASE_ADDR] = 0x074,
209 [IPROC_PCIE_MSI_WINDOW_SIZE] = 0x078,
210 [IPROC_PCIE_MSI_ADDR_LO] = 0x07c,
211 [IPROC_PCIE_MSI_ADDR_HI] = 0x080,
212 [IPROC_PCIE_MSI_EN_CFG] = 0x09c,
213 [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0,
214 [IPROC_PCIE_CFG_IND_DATA] = 0x1f4,
215 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
216 [IPROC_PCIE_CFG_DATA] = 0x1fc,
220 * List of device IDs of controllers that have corrupted
221 * capability list that require SW fixup
223 static const u16 iproc_pcie_corrupt_cap_did[] = {
230 enum iproc_pcie_type {
237 * struct iproc_pcie_ob - iProc PCIe outbound mapping
239 * @axi_offset: offset from the AXI address to the internal address used by
240 * the iProc PCIe core
241 * @nr_windows: total number of supported outbound mapping windows
243 struct iproc_pcie_ob {
244 resource_size_t axi_offset;
245 unsigned int nr_windows;
249 * struct iproc_pcie_ib - iProc PCIe inbound mapping
251 * @nr_regions: total number of supported inbound mapping regions
253 struct iproc_pcie_ib {
254 unsigned int nr_regions;
258 * struct iproc_pcie_ob_map - outbound mapping controller specific parameters
260 * @window_sizes: list of supported outbound mapping window sizes in MB
261 * @nr_sizes: number of supported outbound mapping window sizes
263 struct iproc_pcie_ob_map {
264 resource_size_t window_sizes[MAX_NUM_OB_WINDOW_SIZES];
265 unsigned int nr_sizes;
268 static const struct iproc_pcie_ob_map paxb_v2_ob_map[] = {
271 .window_sizes = { 128, 256 },
276 .window_sizes = { 128, 256 },
281 .window_sizes = { 128, 256, 512, 1024 },
286 .window_sizes = { 128, 256, 512, 1024 },
292 * iProc PCIe inbound mapping type
294 enum iproc_pcie_ib_map_type {
296 IPROC_PCIE_IB_MAP_MEM = 0,
298 /* for device I/O memory */
299 IPROC_PCIE_IB_MAP_IO,
301 /* invalid or unused */
302 IPROC_PCIE_IB_MAP_INVALID
306 * struct iproc_pcie_ib_map - inbound mapping controller specific parameters
308 * @type: inbound mapping region type
309 * @size_unit: inbound mapping region size unit, could be SZ_1K, SZ_1M, or SZ_1G
310 * @region_sizes: list of supported inbound mapping region sizes in KB, MB, or
311 * GB, depedning on the size unit
312 * @nr_sizes: number of supported inbound mapping region sizes
313 * @nr_windows: number of supported inbound mapping windows for the region
314 * @imap_addr_offset: register offset between the upper and lower 32-bit
315 * IMAP address registers
316 * @imap_window_offset: register offset between each IMAP window
318 struct iproc_pcie_ib_map {
319 enum iproc_pcie_ib_map_type type;
320 unsigned int size_unit;
321 resource_size_t region_sizes[MAX_NUM_IB_REGION_SIZES];
322 unsigned int nr_sizes;
323 unsigned int nr_windows;
324 u16 imap_addr_offset;
325 u16 imap_window_offset;
328 static const struct iproc_pcie_ib_map paxb_v2_ib_map[] = {
331 .type = IPROC_PCIE_IB_MAP_IO,
333 .region_sizes = { 32 },
336 .imap_addr_offset = 0x40,
337 .imap_window_offset = 0x4,
340 /* IARR1/IMAP1 (currently unused) */
341 .type = IPROC_PCIE_IB_MAP_INVALID,
345 .type = IPROC_PCIE_IB_MAP_MEM,
347 .region_sizes = { 64, 128, 256, 512, 1024, 2048, 4096, 8192,
351 .imap_addr_offset = 0x4,
352 .imap_window_offset = 0x8,
356 .type = IPROC_PCIE_IB_MAP_MEM,
358 .region_sizes = { 1, 2, 4, 8, 16, 32 },
361 .imap_addr_offset = 0x4,
362 .imap_window_offset = 0x8,
366 .type = IPROC_PCIE_IB_MAP_MEM,
368 .region_sizes = { 32, 64, 128, 256, 512 },
371 .imap_addr_offset = 0x4,
372 .imap_window_offset = 0x8,
377 * struct iproc_pcie - iproc pcie device instance
379 * @dev: pointer to pcie udevice
380 * @base: device I/O base address
381 * @type: pci device type, PAXC or PAXB
382 * @reg_offsets: pointer to pcie host register
383 * @fix_paxc_cap: paxc capability
384 * @need_ob_cfg: outbound mapping status
385 * @ob: pcie outbound mapping
386 * @ob_map: pointer to outbound mapping parameters
387 * @need_ib_cfg: inbound mapping status
388 * @ib: pcie inbound mapping
389 * @ib_map: pointer to inbound mapping parameters
390 * @ep_is_internal: ep status
392 * @link_is_active: link up status
393 * @has_apb_err_disable: apb error status
398 enum iproc_pcie_type type;
402 struct iproc_pcie_ob ob;
403 const struct iproc_pcie_ob_map *ob_map;
405 struct iproc_pcie_ib ib;
406 const struct iproc_pcie_ib_map *ib_map;
410 bool has_apb_err_disable;
413 static inline bool iproc_pcie_reg_is_invalid(u16 reg_offset)
415 return !!(reg_offset == IPROC_PCIE_REG_INVALID);
418 static inline u16 iproc_pcie_reg_offset(struct iproc_pcie *pcie,
419 enum iproc_pcie_reg reg)
421 return pcie->reg_offsets[reg];
424 static inline u32 iproc_pcie_read_reg(struct iproc_pcie *pcie,
425 enum iproc_pcie_reg reg)
427 u16 offset = iproc_pcie_reg_offset(pcie, reg);
429 if (iproc_pcie_reg_is_invalid(offset))
432 return readl(pcie->base + offset);
435 static inline void iproc_pcie_write_reg(struct iproc_pcie *pcie,
436 enum iproc_pcie_reg reg, u32 val)
438 u16 offset = iproc_pcie_reg_offset(pcie, reg);
440 if (iproc_pcie_reg_is_invalid(offset))
443 writel(val, pcie->base + offset);
446 static int iproc_pcie_map_ep_cfg_reg(const struct udevice *udev, pci_dev_t bdf,
447 uint where, void **paddress)
449 struct iproc_pcie *pcie = dev_get_priv(udev);
450 unsigned int busno = PCI_BUS(bdf);
451 unsigned int slot = PCI_DEV(bdf);
452 unsigned int fn = PCI_FUNC(bdf);
457 /* root complex access */
459 if (slot > 0 || fn > 0)
462 iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_IND_ADDR,
463 where & CFG_IND_ADDR_MASK);
464 offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_IND_DATA);
465 if (iproc_pcie_reg_is_invalid(offset))
468 *paddress = (pcie->base + offset);
472 if (!pcie->link_is_active)
475 /* EP device access */
476 val = (busno << CFG_ADDR_BUS_NUM_SHIFT) |
477 (slot << CFG_ADDR_DEV_NUM_SHIFT) |
478 (fn << CFG_ADDR_FUNC_NUM_SHIFT) |
479 (where & CFG_ADDR_REG_NUM_MASK) |
480 (1 & CFG_ADDR_CFG_TYPE_MASK);
482 iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_ADDR, val);
483 offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_DATA);
485 if (iproc_pcie_reg_is_invalid(offset))
488 *paddress = (pcie->base + offset);
493 static void iproc_pcie_fix_cap(struct iproc_pcie *pcie, int where, ulong *val)
497 switch (where & ~0x3) {
502 * Activate fixup for those controllers that have corrupted
503 * capability list registers
505 for (i = 0; i < ARRAY_SIZE(iproc_pcie_corrupt_cap_did); i++)
506 if (dev_id == iproc_pcie_corrupt_cap_did[i])
507 pcie->fix_paxc_cap = true;
510 case IPROC_PCI_PM_CAP:
511 if (pcie->fix_paxc_cap) {
512 /* advertise PM, force next capability to PCIe */
513 *val &= ~IPROC_PCI_PM_CAP_MASK;
514 *val |= IPROC_PCI_EXP_CAP << 8 | PCI_CAP_ID_PM;
518 case IPROC_PCI_EXP_CAP:
519 if (pcie->fix_paxc_cap) {
520 /* advertise root port, version 2, terminate here */
521 *val = (PCI_EXP_TYPE_ROOT_PORT << 4 | 2) << 16 |
526 case IPROC_PCI_EXP_CAP + PCI_EXP_RTCTL:
527 /* Don't advertise CRS SV support */
528 *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16);
536 static int iproc_pci_raw_config_read32(struct iproc_pcie *pcie,
537 unsigned int devfn, int where,
543 ret = iproc_pcie_map_ep_cfg_reg(pcie->dev, devfn, where & ~0x3, &addr);
552 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
557 static int iproc_pci_raw_config_write32(struct iproc_pcie *pcie,
558 unsigned int devfn, int where,
565 ret = iproc_pcie_map_ep_cfg_reg(pcie->dev, devfn, where & ~0x3, &addr);
574 mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
575 tmp = readl(addr) & mask;
576 tmp |= val << ((where & 0x3) * 8);
582 * iproc_pcie_apb_err_disable() - configure apb error
584 * APB error forwarding can be disabled during access of configuration
585 * registers of the endpoint device, to prevent unsupported requests
586 * (typically seen during enumeration with multi-function devices) from
587 * triggering a system exception.
591 * @disabled: flag to enable/disabled apb error
593 static inline void iproc_pcie_apb_err_disable(const struct udevice *bus,
594 pci_dev_t bdf, bool disable)
596 struct iproc_pcie *pcie = dev_get_priv(bus);
599 if (PCI_BUS(bdf) && pcie->has_apb_err_disable) {
600 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_APB_ERR_EN);
605 iproc_pcie_write_reg(pcie, IPROC_PCIE_APB_ERR_EN, val);
609 static int iproc_pcie_config_read32(const struct udevice *bus, pci_dev_t bdf,
610 uint offset, ulong *valuep,
611 enum pci_size_t size)
613 struct iproc_pcie *pcie = dev_get_priv(bus);
617 iproc_pcie_apb_err_disable(bus, bdf, true);
618 ret = pci_generic_mmap_read_config(bus, iproc_pcie_map_ep_cfg_reg,
619 bdf, offset, &data, PCI_SIZE_32);
620 iproc_pcie_apb_err_disable(bus, bdf, false);
621 if (size <= PCI_SIZE_16)
622 *valuep = (data >> (8 * (offset & 3))) &
623 ((1 << (BIT(size) * 8)) - 1);
627 if (!ret && PCI_BUS(bdf) == 0)
628 iproc_pcie_fix_cap(pcie, offset, valuep);
633 static int iproc_pcie_config_write32(struct udevice *bus, pci_dev_t bdf,
634 uint offset, ulong value,
635 enum pci_size_t size)
641 ret = iproc_pcie_map_ep_cfg_reg(bus, bdf, offset, &addr);
645 if (size == PCI_SIZE_32) {
650 iproc_pcie_apb_err_disable(bus, bdf, true);
651 mask = ~(((1 << (BIT(size) * 8)) - 1) << ((offset & 0x3) * 8));
652 tmp = readl(addr) & mask;
653 tmp |= (value << ((offset & 0x3) * 8));
655 iproc_pcie_apb_err_disable(bus, bdf, false);
660 const static struct dm_pci_ops iproc_pcie_ops = {
661 .read_config = iproc_pcie_config_read32,
662 .write_config = iproc_pcie_config_write32,
665 static int iproc_pcie_rev_init(struct iproc_pcie *pcie)
667 unsigned int reg_idx;
671 switch (pcie->type) {
672 case IPROC_PCIE_PAXC_V2:
673 pcie->ep_is_internal = true;
674 regs = iproc_pcie_reg_paxc_v2;
675 num_elements = ARRAY_SIZE(iproc_pcie_reg_paxc_v2);
677 case IPROC_PCIE_PAXB_V2:
678 regs = iproc_pcie_reg_paxb_v2;
679 num_elements = ARRAY_SIZE(iproc_pcie_reg_paxb_v2);
680 pcie->has_apb_err_disable = true;
681 if (pcie->need_ob_cfg) {
682 pcie->ob.axi_offset = 0;
683 pcie->ob_map = paxb_v2_ob_map;
684 pcie->ob.nr_windows = ARRAY_SIZE(paxb_v2_ob_map);
686 pcie->need_ib_cfg = true;
687 pcie->ib.nr_regions = ARRAY_SIZE(paxb_v2_ib_map);
688 pcie->ib_map = paxb_v2_ib_map;
691 dev_dbg(pcie->dev, "incompatible iProc PCIe interface\n");
695 pcie->reg_offsets = calloc(IPROC_PCIE_MAX_NUM_REG,
696 sizeof(*pcie->reg_offsets));
697 if (!pcie->reg_offsets)
700 /* go through the register table and populate all valid registers */
701 pcie->reg_offsets[0] = (pcie->type == IPROC_PCIE_PAXC_V2) ?
702 IPROC_PCIE_REG_INVALID : regs[0];
703 for (reg_idx = 1; reg_idx < num_elements; reg_idx++)
704 pcie->reg_offsets[reg_idx] = regs[reg_idx] ?
705 regs[reg_idx] : IPROC_PCIE_REG_INVALID;
710 static inline bool iproc_pcie_ob_is_valid(struct iproc_pcie *pcie,
715 val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_OARR0, window_idx));
717 return !!(val & OARR_VALID);
720 static inline int iproc_pcie_ob_write(struct iproc_pcie *pcie, int window_idx,
721 int size_idx, u64 axi_addr, u64 pci_addr)
723 u16 oarr_offset, omap_offset;
726 * Derive the OARR/OMAP offset from the first pair (OARR0/OMAP0) based
729 oarr_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OARR0,
731 omap_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OMAP0,
733 if (iproc_pcie_reg_is_invalid(oarr_offset) ||
734 iproc_pcie_reg_is_invalid(omap_offset))
738 * Program the OARR registers. The upper 32-bit OARR register is
739 * always right after the lower 32-bit OARR register.
741 writel(lower_32_bits(axi_addr) | (size_idx << OARR_SIZE_CFG_SHIFT) |
742 OARR_VALID, pcie->base + oarr_offset);
743 writel(upper_32_bits(axi_addr), pcie->base + oarr_offset + 4);
745 /* now program the OMAP registers */
746 writel(lower_32_bits(pci_addr), pcie->base + omap_offset);
747 writel(upper_32_bits(pci_addr), pcie->base + omap_offset + 4);
749 debug("ob window [%d]: offset 0x%x axi %pap pci %pap\n",
750 window_idx, oarr_offset, &axi_addr, &pci_addr);
751 debug("oarr lo 0x%x oarr hi 0x%x\n",
752 readl(pcie->base + oarr_offset),
753 readl(pcie->base + oarr_offset + 4));
754 debug("omap lo 0x%x omap hi 0x%x\n",
755 readl(pcie->base + omap_offset),
756 readl(pcie->base + omap_offset + 4));
762 * iproc_pcie_setup_ob() - setup outbound address mapping
764 * Some iProc SoCs require the SW to configure the outbound address mapping
765 * Outbound address translation:
767 * iproc_pcie_address = axi_address - axi_offset
768 * OARR = iproc_pcie_address
770 * axi_addr -> iproc_pcie_address -> OARR -> OMAP -> pci_address
773 * @axi_addr: axi address to be translated
774 * @pci_addr: pci address
777 * @return: 0 on success and -ve on failure
779 static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr,
780 u64 pci_addr, resource_size_t size)
782 struct iproc_pcie_ob *ob = &pcie->ob;
783 int ret = -EINVAL, window_idx, size_idx;
785 if (axi_addr < ob->axi_offset) {
786 pr_err("axi address %pap less than offset %pap\n",
787 &axi_addr, &ob->axi_offset);
792 * Translate the AXI address to the internal address used by the iProc
793 * PCIe core before programming the OARR
795 axi_addr -= ob->axi_offset;
797 /* iterate through all OARR/OMAP mapping windows */
798 for (window_idx = ob->nr_windows - 1; window_idx >= 0; window_idx--) {
799 const struct iproc_pcie_ob_map *ob_map =
800 &pcie->ob_map[window_idx];
803 * If current outbound window is already in use, move on to the
806 if (iproc_pcie_ob_is_valid(pcie, window_idx))
810 * Iterate through all supported window sizes within the
811 * OARR/OMAP pair to find a match. Go through the window sizes
812 * in a descending order.
814 for (size_idx = ob_map->nr_sizes - 1; size_idx >= 0;
816 resource_size_t window_size =
817 ob_map->window_sizes[size_idx] * SZ_1M;
820 * Keep iterating until we reach the last window and
821 * with the minimal window size at index zero. In this
822 * case, we take a compromise by mapping it using the
823 * minimum window size that can be supported
825 if (size < window_size) {
826 if (size_idx > 0 || window_idx > 0)
830 * For the corner case of reaching the minimal
831 * window size that can be supported on the
834 axi_addr = ALIGN_DOWN(axi_addr, window_size);
835 pci_addr = ALIGN_DOWN(pci_addr, window_size);
839 if (!IS_ALIGNED(axi_addr, window_size) ||
840 !IS_ALIGNED(pci_addr, window_size)) {
841 pr_err("axi %pap or pci %pap not aligned\n",
842 &axi_addr, &pci_addr);
847 * Match found! Program both OARR and OMAP and mark
848 * them as a valid entry.
850 ret = iproc_pcie_ob_write(pcie, window_idx, size_idx,
860 * If we are here, we are done with the current window,
861 * but not yet finished all mappings. Need to move on
862 * to the next window.
864 axi_addr += window_size;
865 pci_addr += window_size;
871 pr_err("unable to configure outbound mapping\n");
872 pr_err("axi %pap, axi offset %pap, pci %pap, res size %pap\n",
873 &axi_addr, &ob->axi_offset, &pci_addr, &size);
878 static int iproc_pcie_map_ranges(struct udevice *dev)
880 struct iproc_pcie *pcie = dev_get_priv(dev);
881 struct udevice *bus = pci_get_controller(dev);
882 struct pci_controller *hose = dev_get_uclass_priv(bus);
885 for (i = 0; i < hose->region_count; i++) {
886 if (hose->regions[i].flags == PCI_REGION_MEM ||
887 hose->regions[i].flags == PCI_REGION_PREFETCH) {
888 debug("%d: bus_addr %p, axi_addr %p, size 0x%llx\n",
889 i, &hose->regions[i].bus_start,
890 &hose->regions[i].phys_start,
891 hose->regions[i].size);
892 ret = iproc_pcie_setup_ob(pcie,
893 hose->regions[i].phys_start,
894 hose->regions[i].bus_start,
895 hose->regions[i].size);
904 static inline bool iproc_pcie_ib_is_in_use(struct iproc_pcie *pcie,
907 const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
910 val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_IARR0, region_idx));
912 return !!(val & (BIT(ib_map->nr_sizes) - 1));
916 iproc_pcie_ib_check_type(const struct iproc_pcie_ib_map *ib_map,
917 enum iproc_pcie_ib_map_type type)
919 return !!(ib_map->type == type);
922 static int iproc_pcie_ib_write(struct iproc_pcie *pcie, int region_idx,
923 int size_idx, int nr_windows, u64 axi_addr,
924 u64 pci_addr, resource_size_t size)
926 const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
927 u16 iarr_offset, imap_offset;
931 iarr_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_IARR0,
933 imap_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_IMAP0,
935 if (iproc_pcie_reg_is_invalid(iarr_offset) ||
936 iproc_pcie_reg_is_invalid(imap_offset))
939 debug("ib region [%d]: offset 0x%x axi %pap pci %pap\n",
940 region_idx, iarr_offset, &axi_addr, &pci_addr);
943 * Program the IARR registers. The upper 32-bit IARR register is
944 * always right after the lower 32-bit IARR register.
946 writel(lower_32_bits(pci_addr) | BIT(size_idx),
947 pcie->base + iarr_offset);
948 writel(upper_32_bits(pci_addr), pcie->base + iarr_offset + 4);
950 debug("iarr lo 0x%x iarr hi 0x%x\n",
951 readl(pcie->base + iarr_offset),
952 readl(pcie->base + iarr_offset + 4));
955 * Now program the IMAP registers. Each IARR region may have one or
958 size >>= ilog2(nr_windows);
959 for (window_idx = 0; window_idx < nr_windows; window_idx++) {
960 val = readl(pcie->base + imap_offset);
961 val |= lower_32_bits(axi_addr) | IMAP_VALID;
962 writel(val, pcie->base + imap_offset);
963 writel(upper_32_bits(axi_addr),
964 pcie->base + imap_offset + ib_map->imap_addr_offset);
966 debug("imap window [%d] lo 0x%x hi 0x%x\n",
967 window_idx, readl(pcie->base + imap_offset),
968 readl(pcie->base + imap_offset +
969 ib_map->imap_addr_offset));
971 imap_offset += ib_map->imap_window_offset;
979 * iproc_pcie_setup_ib() - setup inbound address mapping
982 * @axi_addr: axi address to be translated
983 * @pci_addr: pci address
985 * @type: inbound mapping type
987 * @return: 0 on success and -ve on failure
989 static int iproc_pcie_setup_ib(struct iproc_pcie *pcie, u64 axi_addr,
990 u64 pci_addr, resource_size_t size,
991 enum iproc_pcie_ib_map_type type)
993 struct iproc_pcie_ib *ib = &pcie->ib;
995 unsigned int region_idx, size_idx;
997 /* iterate through all IARR mapping regions */
998 for (region_idx = 0; region_idx < ib->nr_regions; region_idx++) {
999 const struct iproc_pcie_ib_map *ib_map =
1000 &pcie->ib_map[region_idx];
1003 * If current inbound region is already in use or not a
1004 * compatible type, move on to the next.
1006 if (iproc_pcie_ib_is_in_use(pcie, region_idx) ||
1007 !iproc_pcie_ib_check_type(ib_map, type))
1010 /* iterate through all supported region sizes to find a match */
1011 for (size_idx = 0; size_idx < ib_map->nr_sizes; size_idx++) {
1012 resource_size_t region_size =
1013 ib_map->region_sizes[size_idx] * ib_map->size_unit;
1015 if (size != region_size)
1018 if (!IS_ALIGNED(axi_addr, region_size) ||
1019 !IS_ALIGNED(pci_addr, region_size)) {
1020 pr_err("axi %pap or pci %pap not aligned\n",
1021 &axi_addr, &pci_addr);
1025 /* Match found! Program IARR and all IMAP windows. */
1026 ret = iproc_pcie_ib_write(pcie, region_idx, size_idx,
1027 ib_map->nr_windows, axi_addr,
1038 pr_err("unable to configure inbound mapping\n");
1039 pr_err("axi %pap, pci %pap, res size %pap\n",
1040 &axi_addr, &pci_addr, &size);
1045 static int iproc_pcie_map_dma_ranges(struct iproc_pcie *pcie)
1048 struct pci_region regions;
1051 while (!pci_get_dma_regions(pcie->dev, ®ions, i)) {
1053 "dma %d: bus_addr %#llx, axi_addr %#llx, size %#llx\n",
1054 i, regions.bus_start, regions.phys_start, regions.size);
1056 /* Each range entry corresponds to an inbound mapping region */
1057 ret = iproc_pcie_setup_ib(pcie, regions.phys_start,
1060 IPROC_PCIE_IB_MAP_MEM);
1068 static void iproc_pcie_reset_map_regs(struct iproc_pcie *pcie)
1070 struct iproc_pcie_ib *ib = &pcie->ib;
1071 struct iproc_pcie_ob *ob = &pcie->ob;
1072 int window_idx, region_idx;
1074 if (pcie->ep_is_internal)
1077 /* iterate through all OARR mapping regions */
1078 for (window_idx = ob->nr_windows - 1; window_idx >= 0; window_idx--) {
1079 iproc_pcie_write_reg(pcie, MAP_REG(IPROC_PCIE_OARR0,
1083 /* iterate through all IARR mapping regions */
1084 for (region_idx = 0; region_idx < ib->nr_regions; region_idx++) {
1085 iproc_pcie_write_reg(pcie, MAP_REG(IPROC_PCIE_IARR0,
1090 static void iproc_pcie_reset(struct iproc_pcie *pcie)
1095 * PAXC and the internal emulated endpoint device downstream should not
1096 * be reset. If firmware has been loaded on the endpoint device at an
1097 * earlier boot stage, reset here causes issues.
1099 if (pcie->ep_is_internal)
1103 * Select perst_b signal as reset source. Put the device into reset,
1104 * and then bring it out of reset
1106 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
1107 val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST &
1108 ~RC_PCIE_RST_OUTPUT;
1109 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
1112 val |= RC_PCIE_RST_OUTPUT;
1113 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
1117 static inline bool iproc_pcie_link_is_active(struct iproc_pcie *pcie)
1121 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_LINK_STATUS);
1122 return !!((val & PCIE_PHYLINKUP) && (val & PCIE_DL_ACTIVE));
1125 static int iproc_pcie_check_link(struct iproc_pcie *pcie)
1127 u32 link_status, class;
1129 pcie->link_is_active = false;
1130 /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */
1131 #define PCI_BRIDGE_CTRL_REG_OFFSET 0x43c
1132 #define PCI_CLASS_BRIDGE_MASK 0xffff00
1133 #define PCI_CLASS_BRIDGE_SHIFT 8
1134 iproc_pci_raw_config_read32(pcie, 0,
1135 PCI_BRIDGE_CTRL_REG_OFFSET,
1137 class &= ~PCI_CLASS_BRIDGE_MASK;
1138 class |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT);
1139 iproc_pci_raw_config_write32(pcie, 0,
1140 PCI_BRIDGE_CTRL_REG_OFFSET,
1144 * PAXC connects to emulated endpoint devices directly and does not
1145 * have a Serdes. Therefore skip the link detection logic here.
1147 if (pcie->ep_is_internal) {
1148 pcie->link_is_active = true;
1152 if (!iproc_pcie_link_is_active(pcie)) {
1153 pr_err("PHY or data link is INACTIVE!\n");
1157 #define PCI_TARGET_LINK_SPEED_MASK 0xf
1158 #define PCI_TARGET_LINK_WIDTH_MASK 0x3f
1159 #define PCI_TARGET_LINK_WIDTH_OFFSET 0x4
1161 /* check link status to see if link is active */
1162 iproc_pci_raw_config_read32(pcie, 0,
1163 IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA,
1165 if (link_status & PCI_EXP_LNKSTA_NLW)
1166 pcie->link_is_active = true;
1168 if (pcie->link_is_active)
1169 pr_info("link UP @ Speed Gen-%d and width-x%d\n",
1170 link_status & PCI_TARGET_LINK_SPEED_MASK,
1171 (link_status >> PCI_TARGET_LINK_WIDTH_OFFSET) &
1172 PCI_TARGET_LINK_WIDTH_MASK);
1174 pr_info("link DOWN\n");
1179 static int iproc_pcie_probe(struct udevice *dev)
1181 struct iproc_pcie *pcie = dev_get_priv(dev);
1184 pcie->type = (enum iproc_pcie_type)dev_get_driver_data(dev);
1185 debug("PAX type %d\n", pcie->type);
1186 pcie->base = dev_read_addr_ptr(dev);
1187 debug("PAX reg base %p\n", pcie->base);
1192 if (dev_read_bool(dev, "brcm,pcie-ob"))
1193 pcie->need_ob_cfg = true;
1196 ret = iproc_pcie_rev_init(pcie);
1200 if (!pcie->ep_is_internal) {
1201 ret = generic_phy_get_by_name(dev, "pcie-phy", &pcie->phy);
1203 ret = generic_phy_init(&pcie->phy);
1205 pr_err("failed to init %s PHY\n", dev->name);
1209 ret = generic_phy_power_on(&pcie->phy);
1211 pr_err("power on %s PHY failed\n", dev->name);
1217 iproc_pcie_reset(pcie);
1219 if (pcie->need_ob_cfg) {
1220 ret = iproc_pcie_map_ranges(dev);
1222 pr_err("outbound map failed\n");
1223 goto err_power_off_phy;
1227 if (pcie->need_ib_cfg) {
1228 ret = iproc_pcie_map_dma_ranges(pcie);
1230 pr_err("inbound map failed\n");
1231 goto err_power_off_phy;
1235 if (iproc_pcie_check_link(pcie))
1236 pr_info("no PCIe EP device detected\n");
1241 generic_phy_power_off(&pcie->phy);
1243 generic_phy_exit(&pcie->phy);
1247 static int iproc_pcie_remove(struct udevice *dev)
1249 struct iproc_pcie *pcie = dev_get_priv(dev);
1252 iproc_pcie_reset_map_regs(pcie);
1254 if (generic_phy_valid(&pcie->phy)) {
1255 ret = generic_phy_power_off(&pcie->phy);
1257 pr_err("failed to power off PCIe phy\n");
1261 ret = generic_phy_exit(&pcie->phy);
1263 pr_err("failed to power off PCIe phy\n");
1271 static const struct udevice_id pci_iproc_ids[] = {
1272 { .compatible = "brcm,iproc-pcie-paxb-v2",
1273 .data = IPROC_PCIE_PAXB_V2 },
1274 { .compatible = "brcm,iproc-pcie-paxc-v2",
1275 .data = IPROC_PCIE_PAXC_V2 },
1279 U_BOOT_DRIVER(pci_iproc) = {
1280 .name = "pci_iproc",
1282 .of_match = pci_iproc_ids,
1283 .ops = &iproc_pcie_ops,
1284 .probe = iproc_pcie_probe,
1285 .remove = iproc_pcie_remove,
1286 .priv_auto = sizeof(struct iproc_pcie),
1287 .flags = DM_FLAG_OS_PREPARE,