1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Texas Instruments, Inc
10 #include <generic-phy.h>
11 #include <power-domain.h>
15 #include <asm-generic/gpio.h>
16 #include <dm/device_compat.h>
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
19 #include <linux/err.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #define PCIE_VENDORID_MASK GENMASK(15, 0)
24 #define PCIE_DEVICEID_SHIFT 16
26 /* PCI DBICS registers */
27 #define PCIE_CONFIG_BAR0 0x10
28 #define PCIE_LINK_STATUS_REG 0x80
29 #define PCIE_LINK_STATUS_SPEED_OFF 16
30 #define PCIE_LINK_STATUS_SPEED_MASK (0xf << PCIE_LINK_STATUS_SPEED_OFF)
31 #define PCIE_LINK_STATUS_WIDTH_OFF 20
32 #define PCIE_LINK_STATUS_WIDTH_MASK (0xf << PCIE_LINK_STATUS_WIDTH_OFF)
34 #define PCIE_LINK_CAPABILITY 0x7c
35 #define PCIE_LINK_CTL_2 0xa0
36 #define TARGET_LINK_SPEED_MASK 0xf
37 #define LINK_SPEED_GEN_1 0x1
38 #define LINK_SPEED_GEN_2 0x2
39 #define LINK_SPEED_GEN_3 0x3
41 #define PCIE_MISC_CONTROL_1_OFF 0x8bc
42 #define PCIE_DBI_RO_WR_EN BIT(0)
44 #define PLR_OFFSET 0x700
45 #define PCIE_PORT_DEBUG0 (PLR_OFFSET + 0x28)
46 #define PORT_LOGIC_LTSSM_STATE_MASK 0x1f
47 #define PORT_LOGIC_LTSSM_STATE_L0 0x11
49 #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80c
50 #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
52 #define PCIE_LINK_UP_TIMEOUT_MS 100
55 * iATU Unroll-specific register definitions
56 * From 4.80 core version the address translation will be made by unroll.
57 * The registers are offset from atu_base
59 #define PCIE_ATU_UNR_REGION_CTRL1 0x00
60 #define PCIE_ATU_UNR_REGION_CTRL2 0x04
61 #define PCIE_ATU_UNR_LOWER_BASE 0x08
62 #define PCIE_ATU_UNR_UPPER_BASE 0x0c
63 #define PCIE_ATU_UNR_LIMIT 0x10
64 #define PCIE_ATU_UNR_LOWER_TARGET 0x14
65 #define PCIE_ATU_UNR_UPPER_TARGET 0x18
67 #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
68 #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
69 #define PCIE_ATU_TYPE_MEM (0x0 << 0)
70 #define PCIE_ATU_TYPE_IO (0x2 << 0)
71 #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
72 #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
73 #define PCIE_ATU_ENABLE (0x1 << 31)
74 #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
75 #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
76 #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
77 #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
79 /* Register address builder */
80 #define PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(region) ((region) << 9)
82 /* Offsets from App base */
83 #define PCIE_CMD_STATUS 0x04
84 #define LTSSM_EN_VAL BIT(0)
86 /* Parameters for the waiting for iATU enabled routine */
87 #define LINK_WAIT_MAX_IATU_RETRIES 5
88 #define LINK_WAIT_IATU 10000
90 #define AM654_PCIE_DEV_TYPE_MASK 0x3
96 * struct pcie_dw_ti - TI DW PCIe controller state
98 * @app_base: The base address of application register space
99 * @dbics_base: The base address of dbics register space
100 * @cfg_base: The base address of configuration space
101 * @atu_base: The base address of ATU space
102 * @cfg_size: The size of the configuration space which is needed
103 * as it gets written into the PCIE_ATU_LIMIT register
104 * @first_busno: This driver supports multiple PCIe controllers.
105 * first_busno stores the bus number of the PCIe root-port
106 * number which may vary depending on the PCIe setup
107 * (PEX switches etc).
118 /* IO and MEM PCI regions */
119 struct pci_region io;
120 struct pci_region mem;
123 enum dw_pcie_device_mode {
124 DW_PCIE_UNKNOWN_TYPE,
130 static int pcie_dw_get_link_speed(struct pcie_dw_ti *pci)
132 return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) &
133 PCIE_LINK_STATUS_SPEED_MASK) >> PCIE_LINK_STATUS_SPEED_OFF;
136 static int pcie_dw_get_link_width(struct pcie_dw_ti *pci)
138 return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) &
139 PCIE_LINK_STATUS_WIDTH_MASK) >> PCIE_LINK_STATUS_WIDTH_OFF;
142 static void dw_pcie_writel_ob_unroll(struct pcie_dw_ti *pci, u32 index, u32 reg,
145 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
146 void __iomem *base = pci->atu_base;
148 writel(val, base + offset + reg);
151 static u32 dw_pcie_readl_ob_unroll(struct pcie_dw_ti *pci, u32 index, u32 reg)
153 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
154 void __iomem *base = pci->atu_base;
156 return readl(base + offset + reg);
160 * pcie_dw_prog_outbound_atu_unroll() - Configure ATU for outbound accesses
162 * @pcie: Pointer to the PCI controller state
163 * @index: ATU region index
164 * @type: ATU accsess type
165 * @cpu_addr: the physical address for the translation entry
166 * @pci_addr: the pcie bus address for the translation entry
167 * @size: the size of the translation entry
169 static void pcie_dw_prog_outbound_atu_unroll(struct pcie_dw_ti *pci, int index,
170 int type, u64 cpu_addr,
171 u64 pci_addr, u32 size)
175 debug("ATU programmed with: index: %d, type: %d, cpu addr: %8llx, pci addr: %8llx, size: %8x\n",
176 index, type, cpu_addr, pci_addr, size);
178 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE,
179 lower_32_bits(cpu_addr));
180 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE,
181 upper_32_bits(cpu_addr));
182 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LIMIT,
183 lower_32_bits(cpu_addr + size - 1));
184 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
185 lower_32_bits(pci_addr));
186 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
187 upper_32_bits(pci_addr));
188 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1,
190 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
194 * Make sure ATU enable takes effect before any subsequent config
197 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
198 val = dw_pcie_readl_ob_unroll(pci, index,
199 PCIE_ATU_UNR_REGION_CTRL2);
200 if (val & PCIE_ATU_ENABLE)
203 udelay(LINK_WAIT_IATU);
205 dev_err(pci->dev, "outbound iATU is not being enabled\n");
209 * set_cfg_address() - Configure the PCIe controller config space access
211 * @pcie: Pointer to the PCI controller state
212 * @d: PCI device to access
213 * @where: Offset in the configuration space
215 * Configures the PCIe controller to access the configuration space of
216 * a specific PCIe device and returns the address to use for this
219 * Return: Address that can be used to access the configation space
220 * of the requested device / offset
222 static uintptr_t set_cfg_address(struct pcie_dw_ti *pcie,
223 pci_dev_t d, uint where)
225 int bus = PCI_BUS(d) - pcie->first_busno;
226 uintptr_t va_address;
229 /* Use dbi_base for own configuration read and write */
231 va_address = (uintptr_t)pcie->dbi_base;
236 /* For local bus, change TLP Type field to 4. */
237 atu_type = PCIE_ATU_TYPE_CFG0;
239 /* Otherwise, change TLP Type field to 5. */
240 atu_type = PCIE_ATU_TYPE_CFG1;
243 * Not accessing root port configuration space?
244 * Region #0 is used for Outbound CFG space access.
245 * Direction = Outbound
249 d = PCI_ADD_BUS(bus, d);
250 pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
251 atu_type, (u64)pcie->cfg_base,
252 d << 8, pcie->cfg_size);
254 va_address = (uintptr_t)pcie->cfg_base;
257 va_address += where & ~0x3;
263 * pcie_dw_addr_valid() - Check for valid bus address
265 * @d: The PCI device to access
266 * @first_busno: Bus number of the PCIe controller root complex
268 * Return 1 (true) if the PCI device can be accessed by this controller.
270 * Return: 1 on valid, 0 on invalid
272 static int pcie_dw_addr_valid(pci_dev_t d, int first_busno)
274 if ((PCI_BUS(d) == first_busno) && (PCI_DEV(d) > 0))
276 if ((PCI_BUS(d) == first_busno + 1) && (PCI_DEV(d) > 0))
283 * pcie_dw_ti_read_config() - Read from configuration space
285 * @bus: Pointer to the PCI bus
286 * @bdf: Identifies the PCIe device to access
287 * @offset: The offset into the device's configuration space
288 * @valuep: A pointer at which to store the read value
289 * @size: Indicates the size of access to perform
291 * Read a value of size @size from offset @offset within the configuration
292 * space of the device identified by the bus, device & function numbers in @bdf
293 * on the PCI bus @bus.
295 * Return: 0 on success
297 static int pcie_dw_ti_read_config(const struct udevice *bus, pci_dev_t bdf,
298 uint offset, ulong *valuep,
299 enum pci_size_t size)
301 struct pcie_dw_ti *pcie = dev_get_priv(bus);
302 uintptr_t va_address;
305 debug("PCIE CFG read: bdf=%2x:%2x:%2x ",
306 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
308 if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
309 debug("- out of range\n");
310 *valuep = pci_get_ff(size);
314 va_address = set_cfg_address(pcie, bdf, offset);
316 value = readl(va_address);
318 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
319 *valuep = pci_conv_32_to_size(value, offset, size);
321 pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
322 PCIE_ATU_TYPE_IO, pcie->io.phys_start,
323 pcie->io.bus_start, pcie->io.size);
329 * pcie_dw_ti_write_config() - Write to configuration space
331 * @bus: Pointer to the PCI bus
332 * @bdf: Identifies the PCIe device to access
333 * @offset: The offset into the device's configuration space
334 * @value: The value to write
335 * @size: Indicates the size of access to perform
337 * Write the value @value of size @size from offset @offset within the
338 * configuration space of the device identified by the bus, device & function
339 * numbers in @bdf on the PCI bus @bus.
341 * Return: 0 on success
343 static int pcie_dw_ti_write_config(struct udevice *bus, pci_dev_t bdf,
344 uint offset, ulong value,
345 enum pci_size_t size)
347 struct pcie_dw_ti *pcie = dev_get_priv(bus);
348 uintptr_t va_address;
351 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
352 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
353 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
355 if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
356 debug("- out of range\n");
360 va_address = set_cfg_address(pcie, bdf, offset);
362 old = readl(va_address);
363 value = pci_conv_size_to_32(old, value, offset, size);
364 writel(value, va_address);
366 pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
367 PCIE_ATU_TYPE_IO, pcie->io.phys_start,
368 pcie->io.bus_start, pcie->io.size);
373 static inline void dw_pcie_dbi_write_enable(struct pcie_dw_ti *pci, bool en)
377 val = readl(pci->dbi_base + PCIE_MISC_CONTROL_1_OFF);
379 val |= PCIE_DBI_RO_WR_EN;
381 val &= ~PCIE_DBI_RO_WR_EN;
382 writel(val, pci->dbi_base + PCIE_MISC_CONTROL_1_OFF);
386 * pcie_dw_configure() - Configure link capabilities and speed
388 * @regs_base: A pointer to the PCIe controller registers
389 * @cap_speed: The capabilities and speed to configure
391 * Configure the link capabilities and speed in the PCIe root complex.
393 static void pcie_dw_configure(struct pcie_dw_ti *pci, u32 cap_speed)
397 dw_pcie_dbi_write_enable(pci, true);
399 val = readl(pci->dbi_base + PCIE_LINK_CAPABILITY);
400 val &= ~TARGET_LINK_SPEED_MASK;
402 writel(val, pci->dbi_base + PCIE_LINK_CAPABILITY);
404 val = readl(pci->dbi_base + PCIE_LINK_CTL_2);
405 val &= ~TARGET_LINK_SPEED_MASK;
407 writel(val, pci->dbi_base + PCIE_LINK_CTL_2);
409 dw_pcie_dbi_write_enable(pci, false);
413 * is_link_up() - Return the link state
415 * @regs_base: A pointer to the PCIe DBICS registers
417 * Return: 1 (true) for active line and 0 (false) for no link
419 static int is_link_up(struct pcie_dw_ti *pci)
423 val = readl(pci->dbi_base + PCIE_PORT_DEBUG0);
424 val &= PORT_LOGIC_LTSSM_STATE_MASK;
426 return (val == PORT_LOGIC_LTSSM_STATE_L0);
430 * wait_link_up() - Wait for the link to come up
432 * @regs_base: A pointer to the PCIe controller registers
434 * Return: 1 (true) for active line and 0 (false) for no link (timeout)
436 static int wait_link_up(struct pcie_dw_ti *pci)
438 unsigned long timeout;
440 timeout = get_timer(0) + PCIE_LINK_UP_TIMEOUT_MS;
441 while (!is_link_up(pci)) {
442 if (get_timer(0) > timeout)
449 static int pcie_dw_ti_pcie_link_up(struct pcie_dw_ti *pci, u32 cap_speed)
453 if (is_link_up(pci)) {
454 printf("PCI Link already up before configuration!\n");
458 /* DW pre link configurations */
459 pcie_dw_configure(pci, cap_speed);
461 /* Initiate link training */
462 val = readl(pci->app_base + PCIE_CMD_STATUS);
464 writel(val, pci->app_base + PCIE_CMD_STATUS);
466 /* Check that link was established */
467 if (!wait_link_up(pci))
471 * Link can be established in Gen 1. still need to wait
472 * till MAC nagaotiation is completed
480 * pcie_dw_setup_host() - Setup the PCIe controller for RC opertaion
482 * @pcie: Pointer to the PCI controller state
484 * Configure the host BARs of the PCIe controller root port so that
485 * PCI(e) devices may access the system memory.
487 static void pcie_dw_setup_host(struct pcie_dw_ti *pci)
492 writel(PCI_BASE_ADDRESS_MEM_TYPE_64,
493 pci->dbi_base + PCI_BASE_ADDRESS_0);
494 writel(0x0, pci->dbi_base + PCI_BASE_ADDRESS_1);
496 /* setup interrupt pins */
497 dw_pcie_dbi_write_enable(pci, true);
498 val = readl(pci->dbi_base + PCI_INTERRUPT_LINE);
501 writel(val, pci->dbi_base + PCI_INTERRUPT_LINE);
502 dw_pcie_dbi_write_enable(pci, false);
504 /* setup bus numbers */
505 val = readl(pci->dbi_base + PCI_PRIMARY_BUS);
508 writel(val, pci->dbi_base + PCI_PRIMARY_BUS);
510 /* setup command register */
511 val = readl(pci->dbi_base + PCI_COMMAND);
513 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
514 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
515 writel(val, pci->dbi_base + PCI_COMMAND);
517 /* Enable write permission for the DBI read-only register */
518 dw_pcie_dbi_write_enable(pci, true);
519 /* program correct class for RC */
520 writew(PCI_CLASS_BRIDGE_PCI, pci->dbi_base + PCI_CLASS_DEVICE);
521 /* Better disable write permission right after the update */
522 dw_pcie_dbi_write_enable(pci, false);
524 val = readl(pci->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
525 val |= PORT_LOGIC_SPEED_CHANGE;
526 writel(val, pci->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
529 static int pcie_am654_set_mode(struct pcie_dw_ti *pci,
530 enum dw_pcie_device_mode mode)
532 struct regmap *syscon;
537 syscon = syscon_regmap_lookup_by_phandle(pci->dev,
538 "ti,syscon-pcie-mode");
542 mask = AM654_PCIE_DEV_TYPE_MASK;
545 case DW_PCIE_RC_TYPE:
548 case DW_PCIE_EP_TYPE:
552 dev_err(pci->dev, "INVALID device type %d\n", mode);
556 ret = regmap_update_bits(syscon, 0, mask, val);
558 dev_err(pci->dev, "failed to set pcie mode\n");
565 static int pcie_dw_init_id(struct pcie_dw_ti *pci)
567 struct regmap *devctrl_regs;
571 devctrl_regs = syscon_regmap_lookup_by_phandle(pci->dev,
572 "ti,syscon-pcie-id");
573 if (IS_ERR(devctrl_regs))
574 return PTR_ERR(devctrl_regs);
576 ret = regmap_read(devctrl_regs, 0, &id);
580 dw_pcie_dbi_write_enable(pci, true);
581 writew(id & PCIE_VENDORID_MASK, pci->dbi_base + PCI_VENDOR_ID);
582 writew(id >> PCIE_DEVICEID_SHIFT, pci->dbi_base + PCI_DEVICE_ID);
583 dw_pcie_dbi_write_enable(pci, false);
589 * pcie_dw_ti_probe() - Probe the PCIe bus for active link
591 * @dev: A pointer to the device being operated on
593 * Probe for an active link on the PCIe bus and configure the controller
594 * to enable this port.
596 * Return: 0 on success, else -ENODEV
598 static int pcie_dw_ti_probe(struct udevice *dev)
600 struct pcie_dw_ti *pci = dev_get_priv(dev);
601 struct udevice *ctlr = pci_get_controller(dev);
602 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
603 struct power_domain pci_pwrdmn;
604 struct phy phy0, phy1;
607 ret = power_domain_get_by_index(dev, &pci_pwrdmn, 0);
609 dev_err(dev, "failed to get power domain\n");
613 ret = power_domain_on(&pci_pwrdmn);
615 dev_err(dev, "Power domain on failed\n");
619 ret = generic_phy_get_by_name(dev, "pcie-phy0", &phy0);
621 dev_err(dev, "Unable to get phy0");
624 generic_phy_reset(&phy0);
625 generic_phy_init(&phy0);
626 generic_phy_power_on(&phy0);
628 ret = generic_phy_get_by_name(dev, "pcie-phy1", &phy1);
630 dev_err(dev, "Unable to get phy1");
633 generic_phy_reset(&phy1);
634 generic_phy_init(&phy1);
635 generic_phy_power_on(&phy1);
637 pci->first_busno = dev_seq(dev);
640 pcie_dw_setup_host(pci);
641 pcie_dw_init_id(pci);
643 if (device_is_compatible(dev, "ti,am654-pcie-rc"))
644 pcie_am654_set_mode(pci, DW_PCIE_RC_TYPE);
646 if (!pcie_dw_ti_pcie_link_up(pci, LINK_SPEED_GEN_2)) {
647 printf("PCIE-%d: Link down\n", dev_seq(dev));
651 printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", dev_seq(dev),
652 pcie_dw_get_link_speed(pci),
653 pcie_dw_get_link_width(pci),
656 /* Store the IO and MEM windows settings for future use by the ATU */
657 pci->io.phys_start = hose->regions[0].phys_start; /* IO base */
658 pci->io.bus_start = hose->regions[0].bus_start; /* IO_bus_addr */
659 pci->io.size = hose->regions[0].size; /* IO size */
661 pci->mem.phys_start = hose->regions[1].phys_start; /* MEM base */
662 pci->mem.bus_start = hose->regions[1].bus_start; /* MEM_bus_addr */
663 pci->mem.size = hose->regions[1].size; /* MEM size */
665 pcie_dw_prog_outbound_atu_unroll(pci, PCIE_ATU_REGION_INDEX0,
668 pci->mem.bus_start, pci->mem.size);
674 * pcie_dw_ti_of_to_plat() - Translate from DT to device state
676 * @dev: A pointer to the device being operated on
678 * Translate relevant data from the device tree pertaining to device @dev into
679 * state that the driver will later make use of. This state is stored in the
680 * device's private data structure.
682 * Return: 0 on success, else -EINVAL
684 static int pcie_dw_ti_of_to_plat(struct udevice *dev)
686 struct pcie_dw_ti *pcie = dev_get_priv(dev);
688 /* Get the controller base address */
689 pcie->dbi_base = (void *)dev_read_addr_name(dev, "dbics");
690 if ((fdt_addr_t)pcie->dbi_base == FDT_ADDR_T_NONE)
693 /* Get the config space base address and size */
694 pcie->cfg_base = (void *)dev_read_addr_size_name(dev, "config",
696 if ((fdt_addr_t)pcie->cfg_base == FDT_ADDR_T_NONE)
699 /* Get the iATU base address and size */
700 pcie->atu_base = (void *)dev_read_addr_name(dev, "atu");
701 if ((fdt_addr_t)pcie->atu_base == FDT_ADDR_T_NONE)
704 /* Get the app base address and size */
705 pcie->app_base = (void *)dev_read_addr_name(dev, "app");
706 if ((fdt_addr_t)pcie->app_base == FDT_ADDR_T_NONE)
712 static const struct dm_pci_ops pcie_dw_ti_ops = {
713 .read_config = pcie_dw_ti_read_config,
714 .write_config = pcie_dw_ti_write_config,
717 static const struct udevice_id pcie_dw_ti_ids[] = {
718 { .compatible = "ti,am654-pcie-rc" },
722 U_BOOT_DRIVER(pcie_dw_ti) = {
723 .name = "pcie_dw_ti",
725 .of_match = pcie_dw_ti_ids,
726 .ops = &pcie_dw_ti_ops,
727 .of_to_plat = pcie_dw_ti_of_to_plat,
728 .probe = pcie_dw_ti_probe,
729 .priv_auto = sizeof(struct pcie_dw_ti),