1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Texas Instruments, Inc
9 #include <generic-phy.h>
10 #include <power-domain.h>
14 #include <asm-generic/gpio.h>
15 #include <linux/err.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 #define PCIE_VENDORID_MASK GENMASK(15, 0)
20 #define PCIE_DEVICEID_SHIFT 16
22 /* PCI DBICS registers */
23 #define PCIE_CONFIG_BAR0 0x10
24 #define PCIE_LINK_STATUS_REG 0x80
25 #define PCIE_LINK_STATUS_SPEED_OFF 16
26 #define PCIE_LINK_STATUS_SPEED_MASK (0xf << PCIE_LINK_STATUS_SPEED_OFF)
27 #define PCIE_LINK_STATUS_WIDTH_OFF 20
28 #define PCIE_LINK_STATUS_WIDTH_MASK (0xf << PCIE_LINK_STATUS_WIDTH_OFF)
30 #define PCIE_LINK_CAPABILITY 0x7c
31 #define PCIE_LINK_CTL_2 0xa0
32 #define TARGET_LINK_SPEED_MASK 0xf
33 #define LINK_SPEED_GEN_1 0x1
34 #define LINK_SPEED_GEN_2 0x2
35 #define LINK_SPEED_GEN_3 0x3
37 #define PCIE_MISC_CONTROL_1_OFF 0x8bc
38 #define PCIE_DBI_RO_WR_EN BIT(0)
40 #define PLR_OFFSET 0x700
41 #define PCIE_PORT_DEBUG0 (PLR_OFFSET + 0x28)
42 #define PORT_LOGIC_LTSSM_STATE_MASK 0x1f
43 #define PORT_LOGIC_LTSSM_STATE_L0 0x11
45 #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80c
46 #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
48 #define PCIE_LINK_UP_TIMEOUT_MS 100
51 * iATU Unroll-specific register definitions
52 * From 4.80 core version the address translation will be made by unroll.
53 * The registers are offset from atu_base
55 #define PCIE_ATU_UNR_REGION_CTRL1 0x00
56 #define PCIE_ATU_UNR_REGION_CTRL2 0x04
57 #define PCIE_ATU_UNR_LOWER_BASE 0x08
58 #define PCIE_ATU_UNR_UPPER_BASE 0x0c
59 #define PCIE_ATU_UNR_LIMIT 0x10
60 #define PCIE_ATU_UNR_LOWER_TARGET 0x14
61 #define PCIE_ATU_UNR_UPPER_TARGET 0x18
63 #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
64 #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
65 #define PCIE_ATU_TYPE_MEM (0x0 << 0)
66 #define PCIE_ATU_TYPE_IO (0x2 << 0)
67 #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
68 #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
69 #define PCIE_ATU_ENABLE (0x1 << 31)
70 #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
71 #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
72 #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
73 #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
75 /* Register address builder */
76 #define PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(region) ((region) << 9)
78 /* Offsets from App base */
79 #define PCIE_CMD_STATUS 0x04
80 #define LTSSM_EN_VAL BIT(0)
82 /* Parameters for the waiting for iATU enabled routine */
83 #define LINK_WAIT_MAX_IATU_RETRIES 5
84 #define LINK_WAIT_IATU 10000
86 #define AM654_PCIE_DEV_TYPE_MASK 0x3
92 * struct pcie_dw_ti - TI DW PCIe controller state
94 * @app_base: The base address of application register space
95 * @dbics_base: The base address of dbics register space
96 * @cfg_base: The base address of configuration space
97 * @atu_base: The base address of ATU space
98 * @cfg_size: The size of the configuration space which is needed
99 * as it gets written into the PCIE_ATU_LIMIT register
100 * @first_busno: This driver supports multiple PCIe controllers.
101 * first_busno stores the bus number of the PCIe root-port
102 * number which may vary depending on the PCIe setup
103 * (PEX switches etc).
114 /* IO and MEM PCI regions */
115 struct pci_region io;
116 struct pci_region mem;
119 enum dw_pcie_device_mode {
120 DW_PCIE_UNKNOWN_TYPE,
126 static int pcie_dw_get_link_speed(struct pcie_dw_ti *pci)
128 return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) &
129 PCIE_LINK_STATUS_SPEED_MASK) >> PCIE_LINK_STATUS_SPEED_OFF;
132 static int pcie_dw_get_link_width(struct pcie_dw_ti *pci)
134 return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) &
135 PCIE_LINK_STATUS_WIDTH_MASK) >> PCIE_LINK_STATUS_WIDTH_OFF;
138 static void dw_pcie_writel_ob_unroll(struct pcie_dw_ti *pci, u32 index, u32 reg,
141 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
142 void __iomem *base = pci->atu_base;
144 writel(val, base + offset + reg);
147 static u32 dw_pcie_readl_ob_unroll(struct pcie_dw_ti *pci, u32 index, u32 reg)
149 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
150 void __iomem *base = pci->atu_base;
152 return readl(base + offset + reg);
156 * pcie_dw_prog_outbound_atu_unroll() - Configure ATU for outbound accesses
158 * @pcie: Pointer to the PCI controller state
159 * @index: ATU region index
160 * @type: ATU accsess type
161 * @cpu_addr: the physical address for the translation entry
162 * @pci_addr: the pcie bus address for the translation entry
163 * @size: the size of the translation entry
165 static void pcie_dw_prog_outbound_atu_unroll(struct pcie_dw_ti *pci, int index,
166 int type, u64 cpu_addr,
167 u64 pci_addr, u32 size)
171 debug("ATU programmed with: index: %d, type: %d, cpu addr: %8llx, pci addr: %8llx, size: %8x\n",
172 index, type, cpu_addr, pci_addr, size);
174 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE,
175 lower_32_bits(cpu_addr));
176 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE,
177 upper_32_bits(cpu_addr));
178 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LIMIT,
179 lower_32_bits(cpu_addr + size - 1));
180 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
181 lower_32_bits(pci_addr));
182 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
183 upper_32_bits(pci_addr));
184 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1,
186 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
190 * Make sure ATU enable takes effect before any subsequent config
193 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
194 val = dw_pcie_readl_ob_unroll(pci, index,
195 PCIE_ATU_UNR_REGION_CTRL2);
196 if (val & PCIE_ATU_ENABLE)
199 udelay(LINK_WAIT_IATU);
201 dev_err(pci->dev, "outbound iATU is not being enabled\n");
205 * set_cfg_address() - Configure the PCIe controller config space access
207 * @pcie: Pointer to the PCI controller state
208 * @d: PCI device to access
209 * @where: Offset in the configuration space
211 * Configures the PCIe controller to access the configuration space of
212 * a specific PCIe device and returns the address to use for this
215 * Return: Address that can be used to access the configation space
216 * of the requested device / offset
218 static uintptr_t set_cfg_address(struct pcie_dw_ti *pcie,
219 pci_dev_t d, uint where)
221 int bus = PCI_BUS(d) - pcie->first_busno;
222 uintptr_t va_address;
225 /* Use dbi_base for own configuration read and write */
227 va_address = (uintptr_t)pcie->dbi_base;
232 /* For local bus, change TLP Type field to 4. */
233 atu_type = PCIE_ATU_TYPE_CFG0;
235 /* Otherwise, change TLP Type field to 5. */
236 atu_type = PCIE_ATU_TYPE_CFG1;
239 * Not accessing root port configuration space?
240 * Region #0 is used for Outbound CFG space access.
241 * Direction = Outbound
245 d = PCI_ADD_BUS(bus, d);
246 pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
247 atu_type, (u64)pcie->cfg_base,
248 d << 8, pcie->cfg_size);
250 va_address = (uintptr_t)pcie->cfg_base;
253 va_address += where & ~0x3;
259 * pcie_dw_addr_valid() - Check for valid bus address
261 * @d: The PCI device to access
262 * @first_busno: Bus number of the PCIe controller root complex
264 * Return 1 (true) if the PCI device can be accessed by this controller.
266 * Return: 1 on valid, 0 on invalid
268 static int pcie_dw_addr_valid(pci_dev_t d, int first_busno)
270 if ((PCI_BUS(d) == first_busno) && (PCI_DEV(d) > 0))
272 if ((PCI_BUS(d) == first_busno + 1) && (PCI_DEV(d) > 0))
279 * pcie_dw_ti_read_config() - Read from configuration space
281 * @bus: Pointer to the PCI bus
282 * @bdf: Identifies the PCIe device to access
283 * @offset: The offset into the device's configuration space
284 * @valuep: A pointer at which to store the read value
285 * @size: Indicates the size of access to perform
287 * Read a value of size @size from offset @offset within the configuration
288 * space of the device identified by the bus, device & function numbers in @bdf
289 * on the PCI bus @bus.
291 * Return: 0 on success
293 static int pcie_dw_ti_read_config(const struct udevice *bus, pci_dev_t bdf,
294 uint offset, ulong *valuep,
295 enum pci_size_t size)
297 struct pcie_dw_ti *pcie = dev_get_priv(bus);
298 uintptr_t va_address;
301 debug("PCIE CFG read: bdf=%2x:%2x:%2x ",
302 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
304 if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
305 debug("- out of range\n");
306 *valuep = pci_get_ff(size);
310 va_address = set_cfg_address(pcie, bdf, offset);
312 value = readl(va_address);
314 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
315 *valuep = pci_conv_32_to_size(value, offset, size);
317 pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
318 PCIE_ATU_TYPE_IO, pcie->io.phys_start,
319 pcie->io.bus_start, pcie->io.size);
325 * pcie_dw_ti_write_config() - Write to configuration space
327 * @bus: Pointer to the PCI bus
328 * @bdf: Identifies the PCIe device to access
329 * @offset: The offset into the device's configuration space
330 * @value: The value to write
331 * @size: Indicates the size of access to perform
333 * Write the value @value of size @size from offset @offset within the
334 * configuration space of the device identified by the bus, device & function
335 * numbers in @bdf on the PCI bus @bus.
337 * Return: 0 on success
339 static int pcie_dw_ti_write_config(struct udevice *bus, pci_dev_t bdf,
340 uint offset, ulong value,
341 enum pci_size_t size)
343 struct pcie_dw_ti *pcie = dev_get_priv(bus);
344 uintptr_t va_address;
347 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
348 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
349 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
351 if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
352 debug("- out of range\n");
356 va_address = set_cfg_address(pcie, bdf, offset);
358 old = readl(va_address);
359 value = pci_conv_size_to_32(old, value, offset, size);
360 writel(value, va_address);
362 pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
363 PCIE_ATU_TYPE_IO, pcie->io.phys_start,
364 pcie->io.bus_start, pcie->io.size);
369 static inline void dw_pcie_dbi_write_enable(struct pcie_dw_ti *pci, bool en)
373 val = readl(pci->dbi_base + PCIE_MISC_CONTROL_1_OFF);
375 val |= PCIE_DBI_RO_WR_EN;
377 val &= ~PCIE_DBI_RO_WR_EN;
378 writel(val, pci->dbi_base + PCIE_MISC_CONTROL_1_OFF);
382 * pcie_dw_configure() - Configure link capabilities and speed
384 * @regs_base: A pointer to the PCIe controller registers
385 * @cap_speed: The capabilities and speed to configure
387 * Configure the link capabilities and speed in the PCIe root complex.
389 static void pcie_dw_configure(struct pcie_dw_ti *pci, u32 cap_speed)
393 dw_pcie_dbi_write_enable(pci, true);
395 val = readl(pci->dbi_base + PCIE_LINK_CAPABILITY);
396 val &= ~TARGET_LINK_SPEED_MASK;
398 writel(val, pci->dbi_base + PCIE_LINK_CAPABILITY);
400 val = readl(pci->dbi_base + PCIE_LINK_CTL_2);
401 val &= ~TARGET_LINK_SPEED_MASK;
403 writel(val, pci->dbi_base + PCIE_LINK_CTL_2);
405 dw_pcie_dbi_write_enable(pci, false);
409 * is_link_up() - Return the link state
411 * @regs_base: A pointer to the PCIe DBICS registers
413 * Return: 1 (true) for active line and 0 (false) for no link
415 static int is_link_up(struct pcie_dw_ti *pci)
419 val = readl(pci->dbi_base + PCIE_PORT_DEBUG0);
420 val &= PORT_LOGIC_LTSSM_STATE_MASK;
422 return (val == PORT_LOGIC_LTSSM_STATE_L0);
426 * wait_link_up() - Wait for the link to come up
428 * @regs_base: A pointer to the PCIe controller registers
430 * Return: 1 (true) for active line and 0 (false) for no link (timeout)
432 static int wait_link_up(struct pcie_dw_ti *pci)
434 unsigned long timeout;
436 timeout = get_timer(0) + PCIE_LINK_UP_TIMEOUT_MS;
437 while (!is_link_up(pci)) {
438 if (get_timer(0) > timeout)
445 static int pcie_dw_ti_pcie_link_up(struct pcie_dw_ti *pci, u32 cap_speed)
449 if (is_link_up(pci)) {
450 printf("PCI Link already up before configuration!\n");
454 /* DW pre link configurations */
455 pcie_dw_configure(pci, cap_speed);
457 /* Initiate link training */
458 val = readl(pci->app_base + PCIE_CMD_STATUS);
460 writel(val, pci->app_base + PCIE_CMD_STATUS);
462 /* Check that link was established */
463 if (!wait_link_up(pci))
467 * Link can be established in Gen 1. still need to wait
468 * till MAC nagaotiation is completed
476 * pcie_dw_setup_host() - Setup the PCIe controller for RC opertaion
478 * @pcie: Pointer to the PCI controller state
480 * Configure the host BARs of the PCIe controller root port so that
481 * PCI(e) devices may access the system memory.
483 static void pcie_dw_setup_host(struct pcie_dw_ti *pci)
488 writel(PCI_BASE_ADDRESS_MEM_TYPE_64,
489 pci->dbi_base + PCI_BASE_ADDRESS_0);
490 writel(0x0, pci->dbi_base + PCI_BASE_ADDRESS_1);
492 /* setup interrupt pins */
493 dw_pcie_dbi_write_enable(pci, true);
494 val = readl(pci->dbi_base + PCI_INTERRUPT_LINE);
497 writel(val, pci->dbi_base + PCI_INTERRUPT_LINE);
498 dw_pcie_dbi_write_enable(pci, false);
500 /* setup bus numbers */
501 val = readl(pci->dbi_base + PCI_PRIMARY_BUS);
504 writel(val, pci->dbi_base + PCI_PRIMARY_BUS);
506 /* setup command register */
507 val = readl(pci->dbi_base + PCI_COMMAND);
509 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
510 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
511 writel(val, pci->dbi_base + PCI_COMMAND);
513 /* Enable write permission for the DBI read-only register */
514 dw_pcie_dbi_write_enable(pci, true);
515 /* program correct class for RC */
516 writew(PCI_CLASS_BRIDGE_PCI, pci->dbi_base + PCI_CLASS_DEVICE);
517 /* Better disable write permission right after the update */
518 dw_pcie_dbi_write_enable(pci, false);
520 val = readl(pci->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
521 val |= PORT_LOGIC_SPEED_CHANGE;
522 writel(val, pci->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
525 static int pcie_am654_set_mode(struct pcie_dw_ti *pci,
526 enum dw_pcie_device_mode mode)
528 struct regmap *syscon;
533 syscon = syscon_regmap_lookup_by_phandle(pci->dev,
534 "ti,syscon-pcie-mode");
538 mask = AM654_PCIE_DEV_TYPE_MASK;
541 case DW_PCIE_RC_TYPE:
544 case DW_PCIE_EP_TYPE:
548 dev_err(pci->dev, "INVALID device type %d\n", mode);
552 ret = regmap_update_bits(syscon, 0, mask, val);
554 dev_err(pci->dev, "failed to set pcie mode\n");
561 static int pcie_dw_init_id(struct pcie_dw_ti *pci)
563 struct regmap *devctrl_regs;
567 devctrl_regs = syscon_regmap_lookup_by_phandle(pci->dev,
568 "ti,syscon-pcie-id");
569 if (IS_ERR(devctrl_regs))
570 return PTR_ERR(devctrl_regs);
572 ret = regmap_read(devctrl_regs, 0, &id);
576 dw_pcie_dbi_write_enable(pci, true);
577 writew(id & PCIE_VENDORID_MASK, pci->dbi_base + PCI_VENDOR_ID);
578 writew(id >> PCIE_DEVICEID_SHIFT, pci->dbi_base + PCI_DEVICE_ID);
579 dw_pcie_dbi_write_enable(pci, false);
585 * pcie_dw_ti_probe() - Probe the PCIe bus for active link
587 * @dev: A pointer to the device being operated on
589 * Probe for an active link on the PCIe bus and configure the controller
590 * to enable this port.
592 * Return: 0 on success, else -ENODEV
594 static int pcie_dw_ti_probe(struct udevice *dev)
596 struct pcie_dw_ti *pci = dev_get_priv(dev);
597 struct udevice *ctlr = pci_get_controller(dev);
598 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
599 struct power_domain pci_pwrdmn;
600 struct phy phy0, phy1;
603 ret = power_domain_get_by_index(dev, &pci_pwrdmn, 0);
605 dev_err(dev, "failed to get power domain\n");
609 ret = power_domain_on(&pci_pwrdmn);
611 dev_err(dev, "Power domain on failed\n");
615 ret = generic_phy_get_by_name(dev, "pcie-phy0", &phy0);
617 dev_err(dev, "Unable to get phy0");
620 generic_phy_reset(&phy0);
621 generic_phy_init(&phy0);
622 generic_phy_power_on(&phy0);
624 ret = generic_phy_get_by_name(dev, "pcie-phy1", &phy1);
626 dev_err(dev, "Unable to get phy1");
629 generic_phy_reset(&phy1);
630 generic_phy_init(&phy1);
631 generic_phy_power_on(&phy1);
633 pci->first_busno = dev->seq;
636 pcie_dw_setup_host(pci);
637 pcie_dw_init_id(pci);
639 if (device_is_compatible(dev, "ti,am654-pcie-rc"))
640 pcie_am654_set_mode(pci, DW_PCIE_RC_TYPE);
642 if (!pcie_dw_ti_pcie_link_up(pci, LINK_SPEED_GEN_2)) {
643 printf("PCIE-%d: Link down\n", dev->seq);
647 printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", dev->seq,
648 pcie_dw_get_link_speed(pci),
649 pcie_dw_get_link_width(pci),
652 /* Store the IO and MEM windows settings for future use by the ATU */
653 pci->io.phys_start = hose->regions[0].phys_start; /* IO base */
654 pci->io.bus_start = hose->regions[0].bus_start; /* IO_bus_addr */
655 pci->io.size = hose->regions[0].size; /* IO size */
657 pci->mem.phys_start = hose->regions[1].phys_start; /* MEM base */
658 pci->mem.bus_start = hose->regions[1].bus_start; /* MEM_bus_addr */
659 pci->mem.size = hose->regions[1].size; /* MEM size */
661 pcie_dw_prog_outbound_atu_unroll(pci, PCIE_ATU_REGION_INDEX0,
664 pci->mem.bus_start, pci->mem.size);
670 * pcie_dw_ti_ofdata_to_platdata() - Translate from DT to device state
672 * @dev: A pointer to the device being operated on
674 * Translate relevant data from the device tree pertaining to device @dev into
675 * state that the driver will later make use of. This state is stored in the
676 * device's private data structure.
678 * Return: 0 on success, else -EINVAL
680 static int pcie_dw_ti_ofdata_to_platdata(struct udevice *dev)
682 struct pcie_dw_ti *pcie = dev_get_priv(dev);
684 /* Get the controller base address */
685 pcie->dbi_base = (void *)dev_read_addr_name(dev, "dbics");
686 if ((fdt_addr_t)pcie->dbi_base == FDT_ADDR_T_NONE)
689 /* Get the config space base address and size */
690 pcie->cfg_base = (void *)dev_read_addr_size_name(dev, "config",
692 if ((fdt_addr_t)pcie->cfg_base == FDT_ADDR_T_NONE)
695 /* Get the iATU base address and size */
696 pcie->atu_base = (void *)dev_read_addr_name(dev, "atu");
697 if ((fdt_addr_t)pcie->atu_base == FDT_ADDR_T_NONE)
700 /* Get the app base address and size */
701 pcie->app_base = (void *)dev_read_addr_name(dev, "app");
702 if ((fdt_addr_t)pcie->app_base == FDT_ADDR_T_NONE)
708 static const struct dm_pci_ops pcie_dw_ti_ops = {
709 .read_config = pcie_dw_ti_read_config,
710 .write_config = pcie_dw_ti_write_config,
713 static const struct udevice_id pcie_dw_ti_ids[] = {
714 { .compatible = "ti,am654-pcie-rc" },
718 U_BOOT_DRIVER(pcie_dw_ti) = {
719 .name = "pcie_dw_ti",
721 .of_match = pcie_dw_ti_ids,
722 .ops = &pcie_dw_ti_ops,
723 .ofdata_to_platdata = pcie_dw_ti_ofdata_to_platdata,
724 .probe = pcie_dw_ti_probe,
725 .priv_auto_alloc_size = sizeof(struct pcie_dw_ti),