2 * ***************************************************************************
3 * Copyright (C) 2015 Marvell International Ltd.
4 * ***************************************************************************
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation, either version 2 of the License, or any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * ***************************************************************************
20 * Ported from Linux driver - driver/pci/host/pci-aardvark.c
22 * Author: Victor Gu <xigu@marvell.com>
23 * Hezi Shahmoon <hezi.shahmoon@marvell.com>
31 #include <asm-generic/gpio.h>
32 #include <linux/ioport.h>
34 /* PCIe core registers */
35 #define PCIE_CORE_CMD_STATUS_REG 0x4
36 #define PCIE_CORE_CMD_IO_ACCESS_EN BIT(0)
37 #define PCIE_CORE_CMD_MEM_ACCESS_EN BIT(1)
38 #define PCIE_CORE_CMD_MEM_IO_REQ_EN BIT(2)
39 #define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8
40 #define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4)
41 #define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11)
42 #define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0
43 #define PCIE_CORE_LINK_TRAINING BIT(5)
44 #define PCIE_CORE_ERR_CAPCTL_REG 0x118
45 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
46 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
47 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK BIT(7)
48 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV BIT(8)
50 /* PIO registers base address and register offsets */
51 #define PIO_BASE_ADDR 0x4000
52 #define PIO_CTRL (PIO_BASE_ADDR + 0x0)
53 #define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
54 #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
55 #define PIO_STAT (PIO_BASE_ADDR + 0x4)
56 #define PIO_COMPLETION_STATUS_SHIFT 7
57 #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
58 #define PIO_COMPLETION_STATUS_OK 0
59 #define PIO_COMPLETION_STATUS_UR 1
60 #define PIO_COMPLETION_STATUS_CRS 2
61 #define PIO_COMPLETION_STATUS_CA 4
62 #define PIO_NON_POSTED_REQ BIT(10)
63 #define PIO_ERR_STATUS BIT(11)
64 #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
65 #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
66 #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
67 #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
68 #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
69 #define PIO_START (PIO_BASE_ADDR + 0x1c)
70 #define PIO_ISR (PIO_BASE_ADDR + 0x20)
72 /* Aardvark Control registers */
73 #define CONTROL_BASE_ADDR 0x4800
74 #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
75 #define PCIE_GEN_SEL_MSK 0x3
76 #define PCIE_GEN_SEL_SHIFT 0x0
82 #define LANE_CNT_MSK 0x18
83 #define LANE_CNT_SHIFT 0x3
84 #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
85 #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
86 #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
87 #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
88 #define LINK_TRAINING_EN BIT(6)
89 #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
90 #define PCIE_CORE_CTRL2_RESERVED 0x7
91 #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
92 #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
93 #define PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE BIT(6)
95 /* LMI registers base address and register offsets */
96 #define LMI_BASE_ADDR 0x6000
97 #define CFG_REG (LMI_BASE_ADDR + 0x0)
98 #define LTSSM_SHIFT 24
99 #define LTSSM_MASK 0x3f
100 #define LTSSM_L0 0x10
102 /* PCIe core controller registers */
103 #define CTRL_CORE_BASE_ADDR 0x18000
104 #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
105 #define CTRL_MODE_SHIFT 0x0
106 #define CTRL_MODE_MASK 0x1
107 #define PCIE_CORE_MODE_DIRECT 0x0
108 #define PCIE_CORE_MODE_COMMAND 0x1
110 /* Transaction types */
111 #define PCIE_CONFIG_RD_TYPE0 0x8
112 #define PCIE_CONFIG_RD_TYPE1 0x9
113 #define PCIE_CONFIG_WR_TYPE0 0xa
114 #define PCIE_CONFIG_WR_TYPE1 0xb
116 /* PCI_BDF shifts 8bit, so we need extra 4bit shift */
117 #define PCIE_BDF(dev) (dev << 4)
118 #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
119 #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
120 #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
121 #define PCIE_CONF_REG(reg) ((reg) & 0xffc)
122 #define PCIE_CONF_ADDR(bus, devfn, where) \
123 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
124 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
126 /* PCIe Retries & Timeout definitions */
127 #define MAX_RETRIES 10
128 #define PIO_WAIT_TIMEOUT 100
129 #define LINK_WAIT_TIMEOUT 100000
131 #define CFG_RD_UR_VAL 0xFFFFFFFF
132 #define CFG_RD_CRS_VAL 0xFFFF0001
135 * struct pcie_advk - Advk PCIe controller state
137 * @reg_base: The base address of the register space.
138 * @first_busno: This driver supports multiple PCIe controllers.
139 * first_busno stores the bus number of the PCIe root-port
140 * number which may vary depending on the PCIe setup
141 * (PEX switches etc).
142 * @device: The pointer to PCI uclass device.
150 static inline void advk_writel(struct pcie_advk *pcie, uint val, uint reg)
152 writel(val, pcie->base + reg);
155 static inline uint advk_readl(struct pcie_advk *pcie, uint reg)
157 return readl(pcie->base + reg);
161 * pcie_advk_addr_valid() - Check for valid bus address
163 * @bdf: The PCI device to access
164 * @first_busno: Bus number of the PCIe controller root complex
166 * Return: 1 on valid, 0 on invalid
168 static int pcie_advk_addr_valid(pci_dev_t bdf, int first_busno)
171 * In PCIE-E only a single device (0) can exist
172 * on the local bus. Beyound the local bus, there might be
173 * a Switch and everything is possible.
175 if ((PCI_BUS(bdf) == first_busno) && (PCI_DEV(bdf) > 0))
182 * pcie_advk_wait_pio() - Wait for PIO access to be accomplished
184 * @pcie: The PCI device to access
186 * Wait up to 1 micro second for PIO access to be accomplished.
188 * Return 1 (true) if PIO access is accomplished.
189 * Return 0 (false) if PIO access is timed out.
191 static int pcie_advk_wait_pio(struct pcie_advk *pcie)
196 for (count = 0; count < MAX_RETRIES; count++) {
197 start = advk_readl(pcie, PIO_START);
198 isr = advk_readl(pcie, PIO_ISR);
202 * Do not check the PIO state too frequently,
203 * 100us delay is appropriate.
205 udelay(PIO_WAIT_TIMEOUT);
208 dev_err(pcie->dev, "config read/write timed out\n");
213 * pcie_advk_check_pio_status() - Validate PIO status and get the read result
215 * @pcie: Pointer to the PCI bus
216 * @read: Read from or write to configuration space - true(read) false(write)
217 * @read_val: Pointer to the read result, only valid when read is true
220 static int pcie_advk_check_pio_status(struct pcie_advk *pcie,
226 char *strcomp_status, *str_posted;
228 reg = advk_readl(pcie, PIO_STAT);
229 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
230 PIO_COMPLETION_STATUS_SHIFT;
233 case PIO_COMPLETION_STATUS_OK:
234 if (reg & PIO_ERR_STATUS) {
235 strcomp_status = "COMP_ERR";
238 /* Get the read result */
240 *read_val = advk_readl(pcie, PIO_RD_DATA);
242 strcomp_status = NULL;
244 case PIO_COMPLETION_STATUS_UR:
246 /* For reading, UR is not an error status. */
247 *read_val = CFG_RD_UR_VAL;
248 strcomp_status = NULL;
250 strcomp_status = "UR";
253 case PIO_COMPLETION_STATUS_CRS:
255 /* For reading, CRS is not an error status. */
256 *read_val = CFG_RD_CRS_VAL;
257 strcomp_status = NULL;
259 strcomp_status = "CRS";
262 case PIO_COMPLETION_STATUS_CA:
263 strcomp_status = "CA";
266 strcomp_status = "Unknown";
273 if (reg & PIO_NON_POSTED_REQ)
274 str_posted = "Non-posted";
276 str_posted = "Posted";
278 dev_err(pcie->dev, "%s PIO Response Status: %s, %#x @ %#x\n",
279 str_posted, strcomp_status, reg,
280 advk_readl(pcie, PIO_ADDR_LS));
286 * pcie_advk_read_config() - Read from configuration space
288 * @bus: Pointer to the PCI bus
289 * @bdf: Identifies the PCIe device to access
290 * @offset: The offset into the device's configuration space
291 * @valuep: A pointer at which to store the read value
292 * @size: Indicates the size of access to perform
294 * Read a value of size @size from offset @offset within the configuration
295 * space of the device identified by the bus, device & function numbers in @bdf
296 * on the PCI bus @bus.
298 * Return: 0 on success
300 static int pcie_advk_read_config(struct udevice *bus, pci_dev_t bdf,
301 uint offset, ulong *valuep,
302 enum pci_size_t size)
304 struct pcie_advk *pcie = dev_get_priv(bus);
308 dev_dbg(pcie->dev, "PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
309 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
311 if (!pcie_advk_addr_valid(bdf, pcie->first_busno)) {
312 dev_dbg(pcie->dev, "- out of range\n");
313 *valuep = pci_get_ff(size);
318 advk_writel(pcie, 0, PIO_START);
319 advk_writel(pcie, 1, PIO_ISR);
321 /* Program the control register */
322 reg = advk_readl(pcie, PIO_CTRL);
323 reg &= ~PIO_CTRL_TYPE_MASK;
324 if (PCI_BUS(bdf) == pcie->first_busno)
325 reg |= PCIE_CONFIG_RD_TYPE0;
327 reg |= PCIE_CONFIG_RD_TYPE1;
328 advk_writel(pcie, reg, PIO_CTRL);
330 /* Program the address registers */
331 reg = PCIE_BDF(bdf) | PCIE_CONF_REG(offset);
332 advk_writel(pcie, reg, PIO_ADDR_LS);
333 advk_writel(pcie, 0, PIO_ADDR_MS);
335 /* Start the transfer */
336 advk_writel(pcie, 1, PIO_START);
338 if (!pcie_advk_wait_pio(pcie))
341 /* Check PIO status and get the read result */
342 ret = pcie_advk_check_pio_status(pcie, true, ®);
346 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n",
348 *valuep = pci_conv_32_to_size(reg, offset, size);
354 * pcie_calc_datastrobe() - Calculate data strobe
356 * @offset: The offset into the device's configuration space
357 * @size: Indicates the size of access to perform
359 * Calculate data strobe according to offset and size
362 static uint pcie_calc_datastrobe(uint offset, enum pci_size_t size)
364 uint bytes, data_strobe;
377 data_strobe = GENMASK(bytes - 1, 0) << (offset & 0x3);
383 * pcie_advk_write_config() - Write to configuration space
385 * @bus: Pointer to the PCI bus
386 * @bdf: Identifies the PCIe device to access
387 * @offset: The offset into the device's configuration space
388 * @value: The value to write
389 * @size: Indicates the size of access to perform
391 * Write the value @value of size @size from offset @offset within the
392 * configuration space of the device identified by the bus, device & function
393 * numbers in @bdf on the PCI bus @bus.
395 * Return: 0 on success
397 static int pcie_advk_write_config(struct udevice *bus, pci_dev_t bdf,
398 uint offset, ulong value,
399 enum pci_size_t size)
401 struct pcie_advk *pcie = dev_get_priv(bus);
404 dev_dbg(pcie->dev, "PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
405 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
406 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n",
407 offset, size, value);
409 if (!pcie_advk_addr_valid(bdf, pcie->first_busno)) {
410 dev_dbg(pcie->dev, "- out of range\n");
415 advk_writel(pcie, 0, PIO_START);
416 advk_writel(pcie, 1, PIO_ISR);
418 /* Program the control register */
419 reg = advk_readl(pcie, PIO_CTRL);
420 reg &= ~PIO_CTRL_TYPE_MASK;
421 if (PCI_BUS(bdf) == pcie->first_busno)
422 reg |= PCIE_CONFIG_WR_TYPE0;
424 reg |= PCIE_CONFIG_WR_TYPE1;
425 advk_writel(pcie, reg, PIO_CTRL);
427 /* Program the address registers */
428 reg = PCIE_BDF(bdf) | PCIE_CONF_REG(offset);
429 advk_writel(pcie, reg, PIO_ADDR_LS);
430 advk_writel(pcie, 0, PIO_ADDR_MS);
431 dev_dbg(pcie->dev, "\tPIO req. - addr = 0x%08x\n", reg);
433 /* Program the data register */
434 reg = pci_conv_size_to_32(0, value, offset, size);
435 advk_writel(pcie, reg, PIO_WR_DATA);
436 dev_dbg(pcie->dev, "\tPIO req. - val = 0x%08x\n", reg);
438 /* Program the data strobe */
439 reg = pcie_calc_datastrobe(offset, size);
440 advk_writel(pcie, reg, PIO_WR_DATA_STRB);
441 dev_dbg(pcie->dev, "\tPIO req. - strb = 0x%02x\n", reg);
443 /* Start the transfer */
444 advk_writel(pcie, 1, PIO_START);
446 if (!pcie_advk_wait_pio(pcie)) {
447 dev_dbg(pcie->dev, "- wait pio timeout\n");
451 /* Check PIO status */
452 pcie_advk_check_pio_status(pcie, false, ®);
458 * pcie_advk_link_up() - Check if PCIe link is up or not
460 * @pcie: The PCI device to access
462 * Return 1 (true) on link up.
463 * Return 0 (false) on link down.
465 static int pcie_advk_link_up(struct pcie_advk *pcie)
467 u32 val, ltssm_state;
469 val = advk_readl(pcie, CFG_REG);
470 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
471 return ltssm_state >= LTSSM_L0;
475 * pcie_advk_wait_for_link() - Wait for link training to be accomplished
477 * @pcie: The PCI device to access
479 * Wait up to 1 second for link training to be accomplished.
481 * Return 1 (true) if link training ends up with link up success.
482 * Return 0 (false) if link training ends up with link up failure.
484 static int pcie_advk_wait_for_link(struct pcie_advk *pcie)
488 /* check if the link is up or not */
489 for (retries = 0; retries < MAX_RETRIES; retries++) {
490 if (pcie_advk_link_up(pcie)) {
491 printf("PCIE-%d: Link up\n", pcie->first_busno);
495 udelay(LINK_WAIT_TIMEOUT);
498 printf("PCIE-%d: Link down\n", pcie->first_busno);
504 * pcie_advk_setup_hw() - PCIe initailzation
506 * @pcie: The PCI device to access
508 * Return: 0 on success
510 static int pcie_advk_setup_hw(struct pcie_advk *pcie)
514 /* Set to Direct mode */
515 reg = advk_readl(pcie, CTRL_CONFIG_REG);
516 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
517 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
518 advk_writel(pcie, reg, CTRL_CONFIG_REG);
520 /* Set PCI global control register to RC mode */
521 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
522 reg |= (IS_RC_MSK << IS_RC_SHIFT);
523 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
525 /* Set Advanced Error Capabilities and Control PF0 register */
526 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
527 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
528 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK |
529 PCIE_CORE_ERR_CAPCTL_ECRC_CHECK_RCV;
530 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
532 /* Set PCIe Device Control and Status 1 PF0 register */
533 reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
534 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE;
535 advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
537 /* Program PCIe Control 2 to disable strict ordering */
538 reg = PCIE_CORE_CTRL2_RESERVED |
539 PCIE_CORE_CTRL2_TD_ENABLE;
540 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
543 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
544 reg &= ~PCIE_GEN_SEL_MSK;
546 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
549 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
550 reg &= ~LANE_CNT_MSK;
552 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
554 /* Enable link training */
555 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
556 reg |= LINK_TRAINING_EN;
557 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
560 * Enable AXI address window location generation:
561 * When it is enabled, the default outbound window
562 * configurations (Default User Field: 0xD0074CFC)
563 * are used to transparent address translation for
564 * the outbound transactions. Thus, PCIe address
565 * windows are not required.
567 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
568 reg |= PCIE_CORE_CTRL2_ADDRWIN_MAP_ENABLE;
569 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
572 * Bypass the address window mapping for PIO:
573 * Since PIO access already contains all required
574 * info over AXI interface by PIO registers, the
575 * address window is not required.
577 reg = advk_readl(pcie, PIO_CTRL);
578 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
579 advk_writel(pcie, reg, PIO_CTRL);
581 /* Start link training */
582 reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
583 reg |= PCIE_CORE_LINK_TRAINING;
584 advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
586 /* Wait for PCIe link up */
587 if (pcie_advk_wait_for_link(pcie))
590 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
591 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
592 PCIE_CORE_CMD_IO_ACCESS_EN |
593 PCIE_CORE_CMD_MEM_IO_REQ_EN;
594 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
600 * pcie_advk_probe() - Probe the PCIe bus for active link
602 * @dev: A pointer to the device being operated on
604 * Probe for an active link on the PCIe bus and configure the controller
605 * to enable this port.
607 * Return: 0 on success, else -ENODEV
609 static int pcie_advk_probe(struct udevice *dev)
611 struct pcie_advk *pcie = dev_get_priv(dev);
613 #ifdef CONFIG_DM_GPIO
614 struct gpio_desc reset_gpio;
616 gpio_request_by_name(dev, "reset-gpio", 0, &reset_gpio,
619 * Issue reset to add-in card through the dedicated GPIO.
620 * Some boards are connecting the card reset pin to common system
621 * reset wire and others are using separate GPIO port.
622 * In the last case we have to release a reset of the addon card
626 * The PCIe RESET signal is not supposed to be released along
627 * with the SOC RESET signal. It should be lowered as early as
628 * possible before PCIe PHY initialization. Moreover, the PCIe
629 * clock should be gated as well.
631 if (dm_gpio_is_valid(&reset_gpio)) {
632 dev_dbg(pcie->dev, "Toggle PCIE Reset GPIO ...\n");
633 dm_gpio_set_value(&reset_gpio, 0);
635 dm_gpio_set_value(&reset_gpio, 1);
638 dev_dbg(pcie->dev, "PCIE Reset on GPIO support is missing\n");
639 #endif /* CONFIG_DM_GPIO */
641 pcie->first_busno = dev->seq;
642 pcie->dev = pci_get_controller(dev);
644 return pcie_advk_setup_hw(pcie);
648 * pcie_advk_ofdata_to_platdata() - Translate from DT to device state
650 * @dev: A pointer to the device being operated on
652 * Translate relevant data from the device tree pertaining to device @dev into
653 * state that the driver will later make use of. This state is stored in the
654 * device's private data structure.
656 * Return: 0 on success, else -EINVAL
658 static int pcie_advk_ofdata_to_platdata(struct udevice *dev)
660 struct pcie_advk *pcie = dev_get_priv(dev);
662 /* Get the register base address */
663 pcie->base = (void *)dev_read_addr_index(dev, 0);
664 if ((fdt_addr_t)pcie->base == FDT_ADDR_T_NONE)
670 static const struct dm_pci_ops pcie_advk_ops = {
671 .read_config = pcie_advk_read_config,
672 .write_config = pcie_advk_write_config,
675 static const struct udevice_id pcie_advk_ids[] = {
676 { .compatible = "marvell,armada-37xx-pcie" },
680 U_BOOT_DRIVER(pcie_advk) = {
683 .of_match = pcie_advk_ids,
684 .ops = &pcie_advk_ops,
685 .ofdata_to_platdata = pcie_advk_ofdata_to_platdata,
686 .probe = pcie_advk_probe,
687 .priv_auto_alloc_size = sizeof(struct pcie_advk),