1 // SPDX-License-Identifier: GPL-2.0+
3 * Rockchip AXI PCIe host controller driver
5 * Copyright (c) 2016 Rockchip, Inc.
10 * Bits taken from Synopsys DesignWare Host controller driver and
11 * ARM PCI Host generic driver.
14 #include <linux/bitrev.h>
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/iopoll.h>
21 #include <linux/irq.h>
22 #include <linux/irqchip/chained_irq.h>
23 #include <linux/irqdomain.h>
24 #include <linux/kernel.h>
25 #include <linux/mfd/syscon.h>
26 #include <linux/module.h>
28 #include <linux/of_pci.h>
29 #include <linux/pci.h>
30 #include <linux/pci_ids.h>
31 #include <linux/phy/phy.h>
32 #include <linux/platform_device.h>
33 #include <linux/reset.h>
34 #include <linux/regmap.h>
37 #include "pcie-rockchip.h"
39 static void rockchip_pcie_enable_bw_int(struct rockchip_pcie *rockchip)
43 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
44 status |= (PCI_EXP_LNKCTL_LBMIE | PCI_EXP_LNKCTL_LABIE);
45 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
48 static void rockchip_pcie_clr_bw_int(struct rockchip_pcie *rockchip)
52 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
53 status |= (PCI_EXP_LNKSTA_LBMS | PCI_EXP_LNKSTA_LABS) << 16;
54 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
57 static void rockchip_pcie_update_txcredit_mui(struct rockchip_pcie *rockchip)
61 /* Update Tx credit maximum update interval */
62 val = rockchip_pcie_read(rockchip, PCIE_CORE_TXCREDIT_CFG1);
63 val &= ~PCIE_CORE_TXCREDIT_CFG1_MUI_MASK;
64 val |= PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(24000); /* ns */
65 rockchip_pcie_write(rockchip, val, PCIE_CORE_TXCREDIT_CFG1);
68 static int rockchip_pcie_valid_device(struct rockchip_pcie *rockchip,
69 struct pci_bus *bus, int dev)
72 * Access only one slot on each root port.
73 * Do not read more than one device on the bus directly attached
74 * to RC's downstream side.
76 if (pci_is_root_bus(bus) || pci_is_root_bus(bus->parent))
82 static u8 rockchip_pcie_lane_map(struct rockchip_pcie *rockchip)
87 if (rockchip->legacy_phy)
88 return GENMASK(MAX_LANE_NUM - 1, 0);
90 val = rockchip_pcie_read(rockchip, PCIE_CORE_LANE_MAP);
91 map = val & PCIE_CORE_LANE_MAP_MASK;
93 /* The link may be using a reverse-indexed mapping. */
94 if (val & PCIE_CORE_LANE_MAP_REVERSE)
95 map = bitrev8(map) >> 4;
100 static int rockchip_pcie_rd_own_conf(struct rockchip_pcie *rockchip,
101 int where, int size, u32 *val)
105 addr = rockchip->apb_base + PCIE_RC_CONFIG_NORMAL_BASE + where;
107 if (!IS_ALIGNED((uintptr_t)addr, size)) {
109 return PCIBIOS_BAD_REGISTER_NUMBER;
114 } else if (size == 2) {
116 } else if (size == 1) {
120 return PCIBIOS_BAD_REGISTER_NUMBER;
122 return PCIBIOS_SUCCESSFUL;
125 static int rockchip_pcie_wr_own_conf(struct rockchip_pcie *rockchip,
126 int where, int size, u32 val)
128 u32 mask, tmp, offset;
131 offset = where & ~0x3;
132 addr = rockchip->apb_base + PCIE_RC_CONFIG_NORMAL_BASE + offset;
136 return PCIBIOS_SUCCESSFUL;
139 mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
142 * N.B. This read/modify/write isn't safe in general because it can
143 * corrupt RW1C bits in adjacent registers. But the hardware
144 * doesn't support smaller writes.
146 tmp = readl(addr) & mask;
147 tmp |= val << ((where & 0x3) * 8);
150 return PCIBIOS_SUCCESSFUL;
153 static int rockchip_pcie_rd_other_conf(struct rockchip_pcie *rockchip,
154 struct pci_bus *bus, u32 devfn,
155 int where, int size, u32 *val)
159 addr = rockchip->reg_base + PCIE_ECAM_OFFSET(bus->number, devfn, where);
161 if (!IS_ALIGNED((uintptr_t)addr, size)) {
163 return PCIBIOS_BAD_REGISTER_NUMBER;
166 if (pci_is_root_bus(bus->parent))
167 rockchip_pcie_cfg_configuration_accesses(rockchip,
168 AXI_WRAPPER_TYPE0_CFG);
170 rockchip_pcie_cfg_configuration_accesses(rockchip,
171 AXI_WRAPPER_TYPE1_CFG);
175 } else if (size == 2) {
177 } else if (size == 1) {
181 return PCIBIOS_BAD_REGISTER_NUMBER;
183 return PCIBIOS_SUCCESSFUL;
186 static int rockchip_pcie_wr_other_conf(struct rockchip_pcie *rockchip,
187 struct pci_bus *bus, u32 devfn,
188 int where, int size, u32 val)
192 addr = rockchip->reg_base + PCIE_ECAM_OFFSET(bus->number, devfn, where);
194 if (!IS_ALIGNED((uintptr_t)addr, size))
195 return PCIBIOS_BAD_REGISTER_NUMBER;
197 if (pci_is_root_bus(bus->parent))
198 rockchip_pcie_cfg_configuration_accesses(rockchip,
199 AXI_WRAPPER_TYPE0_CFG);
201 rockchip_pcie_cfg_configuration_accesses(rockchip,
202 AXI_WRAPPER_TYPE1_CFG);
211 return PCIBIOS_BAD_REGISTER_NUMBER;
213 return PCIBIOS_SUCCESSFUL;
216 static int rockchip_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
219 struct rockchip_pcie *rockchip = bus->sysdata;
221 if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn)))
222 return PCIBIOS_DEVICE_NOT_FOUND;
224 if (pci_is_root_bus(bus))
225 return rockchip_pcie_rd_own_conf(rockchip, where, size, val);
227 return rockchip_pcie_rd_other_conf(rockchip, bus, devfn, where, size,
231 static int rockchip_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
232 int where, int size, u32 val)
234 struct rockchip_pcie *rockchip = bus->sysdata;
236 if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn)))
237 return PCIBIOS_DEVICE_NOT_FOUND;
239 if (pci_is_root_bus(bus))
240 return rockchip_pcie_wr_own_conf(rockchip, where, size, val);
242 return rockchip_pcie_wr_other_conf(rockchip, bus, devfn, where, size,
246 static struct pci_ops rockchip_pcie_ops = {
247 .read = rockchip_pcie_rd_conf,
248 .write = rockchip_pcie_wr_conf,
251 static void rockchip_pcie_set_power_limit(struct rockchip_pcie *rockchip)
254 u32 status, scale, power;
256 if (IS_ERR(rockchip->vpcie3v3))
260 * Set RC's captured slot power limit and scale if
261 * vpcie3v3 available. The default values are both zero
262 * which means the software should set these two according
263 * to the actual power supply.
265 curr = regulator_get_current_limit(rockchip->vpcie3v3);
269 scale = 3; /* 0.001x */
270 curr = curr / 1000; /* convert to mA */
271 power = (curr * 3300) / 1000; /* milliwatt */
272 while (power > PCIE_RC_CONFIG_DCR_CSPL_LIMIT) {
274 dev_warn(rockchip->dev, "invalid power supply\n");
281 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_DCR);
282 status |= (power << PCIE_RC_CONFIG_DCR_CSPL_SHIFT) |
283 (scale << PCIE_RC_CONFIG_DCR_CPLS_SHIFT);
284 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_DCR);
288 * rockchip_pcie_host_init_port - Initialize hardware
289 * @rockchip: PCIe port information
291 static int rockchip_pcie_host_init_port(struct rockchip_pcie *rockchip)
293 struct device *dev = rockchip->dev;
294 int err, i = MAX_LANE_NUM;
297 gpiod_set_value_cansleep(rockchip->ep_gpio, 0);
299 err = rockchip_pcie_init_port(rockchip);
303 /* Fix the transmitted FTS count desired to exit from L0s. */
304 status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL_PLC1);
305 status = (status & ~PCIE_CORE_CTRL_PLC1_FTS_MASK) |
306 (PCIE_CORE_CTRL_PLC1_FTS_CNT << PCIE_CORE_CTRL_PLC1_FTS_SHIFT);
307 rockchip_pcie_write(rockchip, status, PCIE_CORE_CTRL_PLC1);
309 rockchip_pcie_set_power_limit(rockchip);
311 /* Set RC's clock architecture as common clock */
312 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
313 status |= PCI_EXP_LNKSTA_SLC << 16;
314 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
316 /* Set RC's RCB to 128 */
317 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
318 status |= PCI_EXP_LNKCTL_RCB;
319 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
321 /* Enable Gen1 training */
322 rockchip_pcie_write(rockchip, PCIE_CLIENT_LINK_TRAIN_ENABLE,
325 gpiod_set_value_cansleep(rockchip->ep_gpio, 1);
327 /* 500ms timeout value should be enough for Gen1/2 training */
328 err = readl_poll_timeout(rockchip->apb_base + PCIE_CLIENT_BASIC_STATUS1,
329 status, PCIE_LINK_UP(status), 20,
330 500 * USEC_PER_MSEC);
332 dev_err(dev, "PCIe link training gen1 timeout!\n");
333 goto err_power_off_phy;
336 if (rockchip->link_gen == 2) {
338 * Enable retrain for gen2. This should be configured only after
341 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
342 status |= PCI_EXP_LNKCTL_RL;
343 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
345 err = readl_poll_timeout(rockchip->apb_base + PCIE_CORE_CTRL,
346 status, PCIE_LINK_IS_GEN2(status), 20,
347 500 * USEC_PER_MSEC);
349 dev_dbg(dev, "PCIe link training gen2 timeout, fall back to gen1!\n");
352 /* Check the final link width from negotiated lane counter from MGMT */
353 status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL);
354 status = 0x1 << ((status & PCIE_CORE_PL_CONF_LANE_MASK) >>
355 PCIE_CORE_PL_CONF_LANE_SHIFT);
356 dev_dbg(dev, "current link width is x%d\n", status);
358 /* Power off unused lane(s) */
359 rockchip->lanes_map = rockchip_pcie_lane_map(rockchip);
360 for (i = 0; i < MAX_LANE_NUM; i++) {
361 if (!(rockchip->lanes_map & BIT(i))) {
362 dev_dbg(dev, "idling lane %d\n", i);
363 phy_power_off(rockchip->phys[i]);
367 rockchip_pcie_write(rockchip, ROCKCHIP_VENDOR_ID,
368 PCIE_CORE_CONFIG_VENDOR);
369 rockchip_pcie_write(rockchip,
370 PCI_CLASS_BRIDGE_PCI_NORMAL << 8,
371 PCIE_RC_CONFIG_RID_CCR);
373 /* Clear THP cap's next cap pointer to remove L1 substate cap */
374 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_THP_CAP);
375 status &= ~PCIE_RC_CONFIG_THP_CAP_NEXT_MASK;
376 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_THP_CAP);
378 /* Clear L0s from RC's link cap */
379 if (of_property_read_bool(dev->of_node, "aspm-no-l0s")) {
380 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LINK_CAP);
381 status &= ~PCIE_RC_CONFIG_LINK_CAP_L0S;
382 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LINK_CAP);
385 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_DCSR);
386 status &= ~PCIE_RC_CONFIG_DCSR_MPS_MASK;
387 status |= PCIE_RC_CONFIG_DCSR_MPS_256;
388 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_DCSR);
393 phy_power_off(rockchip->phys[i]);
396 phy_exit(rockchip->phys[i]);
400 static irqreturn_t rockchip_pcie_subsys_irq_handler(int irq, void *arg)
402 struct rockchip_pcie *rockchip = arg;
403 struct device *dev = rockchip->dev;
407 reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
408 if (reg & PCIE_CLIENT_INT_LOCAL) {
409 dev_dbg(dev, "local interrupt received\n");
410 sub_reg = rockchip_pcie_read(rockchip, PCIE_CORE_INT_STATUS);
411 if (sub_reg & PCIE_CORE_INT_PRFPE)
412 dev_dbg(dev, "parity error detected while reading from the PNP receive FIFO RAM\n");
414 if (sub_reg & PCIE_CORE_INT_CRFPE)
415 dev_dbg(dev, "parity error detected while reading from the Completion Receive FIFO RAM\n");
417 if (sub_reg & PCIE_CORE_INT_RRPE)
418 dev_dbg(dev, "parity error detected while reading from replay buffer RAM\n");
420 if (sub_reg & PCIE_CORE_INT_PRFO)
421 dev_dbg(dev, "overflow occurred in the PNP receive FIFO\n");
423 if (sub_reg & PCIE_CORE_INT_CRFO)
424 dev_dbg(dev, "overflow occurred in the completion receive FIFO\n");
426 if (sub_reg & PCIE_CORE_INT_RT)
427 dev_dbg(dev, "replay timer timed out\n");
429 if (sub_reg & PCIE_CORE_INT_RTR)
430 dev_dbg(dev, "replay timer rolled over after 4 transmissions of the same TLP\n");
432 if (sub_reg & PCIE_CORE_INT_PE)
433 dev_dbg(dev, "phy error detected on receive side\n");
435 if (sub_reg & PCIE_CORE_INT_MTR)
436 dev_dbg(dev, "malformed TLP received from the link\n");
438 if (sub_reg & PCIE_CORE_INT_UCR)
439 dev_dbg(dev, "malformed TLP received from the link\n");
441 if (sub_reg & PCIE_CORE_INT_FCE)
442 dev_dbg(dev, "an error was observed in the flow control advertisements from the other side\n");
444 if (sub_reg & PCIE_CORE_INT_CT)
445 dev_dbg(dev, "a request timed out waiting for completion\n");
447 if (sub_reg & PCIE_CORE_INT_UTC)
448 dev_dbg(dev, "unmapped TC error\n");
450 if (sub_reg & PCIE_CORE_INT_MMVC)
451 dev_dbg(dev, "MSI mask register changes\n");
453 rockchip_pcie_write(rockchip, sub_reg, PCIE_CORE_INT_STATUS);
454 } else if (reg & PCIE_CLIENT_INT_PHY) {
455 dev_dbg(dev, "phy link changes\n");
456 rockchip_pcie_update_txcredit_mui(rockchip);
457 rockchip_pcie_clr_bw_int(rockchip);
460 rockchip_pcie_write(rockchip, reg & PCIE_CLIENT_INT_LOCAL,
461 PCIE_CLIENT_INT_STATUS);
466 static irqreturn_t rockchip_pcie_client_irq_handler(int irq, void *arg)
468 struct rockchip_pcie *rockchip = arg;
469 struct device *dev = rockchip->dev;
472 reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
473 if (reg & PCIE_CLIENT_INT_LEGACY_DONE)
474 dev_dbg(dev, "legacy done interrupt received\n");
476 if (reg & PCIE_CLIENT_INT_MSG)
477 dev_dbg(dev, "message done interrupt received\n");
479 if (reg & PCIE_CLIENT_INT_HOT_RST)
480 dev_dbg(dev, "hot reset interrupt received\n");
482 if (reg & PCIE_CLIENT_INT_DPA)
483 dev_dbg(dev, "dpa interrupt received\n");
485 if (reg & PCIE_CLIENT_INT_FATAL_ERR)
486 dev_dbg(dev, "fatal error interrupt received\n");
488 if (reg & PCIE_CLIENT_INT_NFATAL_ERR)
489 dev_dbg(dev, "no fatal error interrupt received\n");
491 if (reg & PCIE_CLIENT_INT_CORR_ERR)
492 dev_dbg(dev, "correctable error interrupt received\n");
494 if (reg & PCIE_CLIENT_INT_PHY)
495 dev_dbg(dev, "phy interrupt received\n");
497 rockchip_pcie_write(rockchip, reg & (PCIE_CLIENT_INT_LEGACY_DONE |
498 PCIE_CLIENT_INT_MSG | PCIE_CLIENT_INT_HOT_RST |
499 PCIE_CLIENT_INT_DPA | PCIE_CLIENT_INT_FATAL_ERR |
500 PCIE_CLIENT_INT_NFATAL_ERR |
501 PCIE_CLIENT_INT_CORR_ERR |
502 PCIE_CLIENT_INT_PHY),
503 PCIE_CLIENT_INT_STATUS);
508 static void rockchip_pcie_legacy_int_handler(struct irq_desc *desc)
510 struct irq_chip *chip = irq_desc_get_chip(desc);
511 struct rockchip_pcie *rockchip = irq_desc_get_handler_data(desc);
512 struct device *dev = rockchip->dev;
517 chained_irq_enter(chip, desc);
519 reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
520 reg = (reg & PCIE_CLIENT_INTR_MASK) >> PCIE_CLIENT_INTR_SHIFT;
523 hwirq = ffs(reg) - 1;
526 ret = generic_handle_domain_irq(rockchip->irq_domain, hwirq);
528 dev_err(dev, "unexpected IRQ, INT%d\n", hwirq);
531 chained_irq_exit(chip, desc);
534 static int rockchip_pcie_setup_irq(struct rockchip_pcie *rockchip)
537 struct device *dev = rockchip->dev;
538 struct platform_device *pdev = to_platform_device(dev);
540 irq = platform_get_irq_byname(pdev, "sys");
544 err = devm_request_irq(dev, irq, rockchip_pcie_subsys_irq_handler,
545 IRQF_SHARED, "pcie-sys", rockchip);
547 dev_err(dev, "failed to request PCIe subsystem IRQ\n");
551 irq = platform_get_irq_byname(pdev, "legacy");
555 irq_set_chained_handler_and_data(irq,
556 rockchip_pcie_legacy_int_handler,
559 irq = platform_get_irq_byname(pdev, "client");
563 err = devm_request_irq(dev, irq, rockchip_pcie_client_irq_handler,
564 IRQF_SHARED, "pcie-client", rockchip);
566 dev_err(dev, "failed to request PCIe client IRQ\n");
574 * rockchip_pcie_parse_host_dt - Parse Device Tree
575 * @rockchip: PCIe port information
577 * Return: '0' on success and error value on failure
579 static int rockchip_pcie_parse_host_dt(struct rockchip_pcie *rockchip)
581 struct device *dev = rockchip->dev;
584 err = rockchip_pcie_parse_dt(rockchip);
588 rockchip->vpcie12v = devm_regulator_get_optional(dev, "vpcie12v");
589 if (IS_ERR(rockchip->vpcie12v)) {
590 if (PTR_ERR(rockchip->vpcie12v) != -ENODEV)
591 return PTR_ERR(rockchip->vpcie12v);
592 dev_info(dev, "no vpcie12v regulator found\n");
595 rockchip->vpcie3v3 = devm_regulator_get_optional(dev, "vpcie3v3");
596 if (IS_ERR(rockchip->vpcie3v3)) {
597 if (PTR_ERR(rockchip->vpcie3v3) != -ENODEV)
598 return PTR_ERR(rockchip->vpcie3v3);
599 dev_info(dev, "no vpcie3v3 regulator found\n");
602 rockchip->vpcie1v8 = devm_regulator_get(dev, "vpcie1v8");
603 if (IS_ERR(rockchip->vpcie1v8))
604 return PTR_ERR(rockchip->vpcie1v8);
606 rockchip->vpcie0v9 = devm_regulator_get(dev, "vpcie0v9");
607 if (IS_ERR(rockchip->vpcie0v9))
608 return PTR_ERR(rockchip->vpcie0v9);
613 static int rockchip_pcie_set_vpcie(struct rockchip_pcie *rockchip)
615 struct device *dev = rockchip->dev;
618 if (!IS_ERR(rockchip->vpcie12v)) {
619 err = regulator_enable(rockchip->vpcie12v);
621 dev_err(dev, "fail to enable vpcie12v regulator\n");
626 if (!IS_ERR(rockchip->vpcie3v3)) {
627 err = regulator_enable(rockchip->vpcie3v3);
629 dev_err(dev, "fail to enable vpcie3v3 regulator\n");
630 goto err_disable_12v;
634 err = regulator_enable(rockchip->vpcie1v8);
636 dev_err(dev, "fail to enable vpcie1v8 regulator\n");
637 goto err_disable_3v3;
640 err = regulator_enable(rockchip->vpcie0v9);
642 dev_err(dev, "fail to enable vpcie0v9 regulator\n");
643 goto err_disable_1v8;
649 regulator_disable(rockchip->vpcie1v8);
651 if (!IS_ERR(rockchip->vpcie3v3))
652 regulator_disable(rockchip->vpcie3v3);
654 if (!IS_ERR(rockchip->vpcie12v))
655 regulator_disable(rockchip->vpcie12v);
660 static void rockchip_pcie_enable_interrupts(struct rockchip_pcie *rockchip)
662 rockchip_pcie_write(rockchip, (PCIE_CLIENT_INT_CLI << 16) &
663 (~PCIE_CLIENT_INT_CLI), PCIE_CLIENT_INT_MASK);
664 rockchip_pcie_write(rockchip, (u32)(~PCIE_CORE_INT),
667 rockchip_pcie_enable_bw_int(rockchip);
670 static int rockchip_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
671 irq_hw_number_t hwirq)
673 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
674 irq_set_chip_data(irq, domain->host_data);
679 static const struct irq_domain_ops intx_domain_ops = {
680 .map = rockchip_pcie_intx_map,
683 static int rockchip_pcie_init_irq_domain(struct rockchip_pcie *rockchip)
685 struct device *dev = rockchip->dev;
686 struct device_node *intc = of_get_next_child(dev->of_node, NULL);
689 dev_err(dev, "missing child interrupt-controller node\n");
693 rockchip->irq_domain = irq_domain_add_linear(intc, PCI_NUM_INTX,
694 &intx_domain_ops, rockchip);
696 if (!rockchip->irq_domain) {
697 dev_err(dev, "failed to get a INTx IRQ domain\n");
704 static int rockchip_pcie_prog_ob_atu(struct rockchip_pcie *rockchip,
705 int region_no, int type, u8 num_pass_bits,
706 u32 lower_addr, u32 upper_addr)
713 if (region_no >= MAX_AXI_WRAPPER_REGION_NUM)
715 if (num_pass_bits + 1 < 8)
717 if (num_pass_bits > 63)
719 if (region_no == 0) {
720 if (AXI_REGION_0_SIZE < (2ULL << num_pass_bits))
723 if (region_no != 0) {
724 if (AXI_REGION_SIZE < (2ULL << num_pass_bits))
728 aw_offset = (region_no << OB_REG_SIZE_SHIFT);
730 ob_addr_0 = num_pass_bits & PCIE_CORE_OB_REGION_ADDR0_NUM_BITS;
731 ob_addr_0 |= lower_addr & PCIE_CORE_OB_REGION_ADDR0_LO_ADDR;
732 ob_addr_1 = upper_addr;
733 ob_desc_0 = (1 << 23 | type);
735 rockchip_pcie_write(rockchip, ob_addr_0,
736 PCIE_CORE_OB_REGION_ADDR0 + aw_offset);
737 rockchip_pcie_write(rockchip, ob_addr_1,
738 PCIE_CORE_OB_REGION_ADDR1 + aw_offset);
739 rockchip_pcie_write(rockchip, ob_desc_0,
740 PCIE_CORE_OB_REGION_DESC0 + aw_offset);
741 rockchip_pcie_write(rockchip, 0,
742 PCIE_CORE_OB_REGION_DESC1 + aw_offset);
747 static int rockchip_pcie_prog_ib_atu(struct rockchip_pcie *rockchip,
748 int region_no, u8 num_pass_bits,
749 u32 lower_addr, u32 upper_addr)
755 if (region_no > MAX_AXI_IB_ROOTPORT_REGION_NUM)
757 if (num_pass_bits + 1 < MIN_AXI_ADDR_BITS_PASSED)
759 if (num_pass_bits > 63)
762 aw_offset = (region_no << IB_ROOT_PORT_REG_SIZE_SHIFT);
764 ib_addr_0 = num_pass_bits & PCIE_CORE_IB_REGION_ADDR0_NUM_BITS;
765 ib_addr_0 |= (lower_addr << 8) & PCIE_CORE_IB_REGION_ADDR0_LO_ADDR;
766 ib_addr_1 = upper_addr;
768 rockchip_pcie_write(rockchip, ib_addr_0, PCIE_RP_IB_ADDR0 + aw_offset);
769 rockchip_pcie_write(rockchip, ib_addr_1, PCIE_RP_IB_ADDR1 + aw_offset);
774 static int rockchip_pcie_cfg_atu(struct rockchip_pcie *rockchip)
776 struct device *dev = rockchip->dev;
777 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(rockchip);
778 struct resource_entry *entry;
784 rockchip_pcie_cfg_configuration_accesses(rockchip,
785 AXI_WRAPPER_TYPE0_CFG);
786 entry = resource_list_first_type(&bridge->windows, IORESOURCE_MEM);
790 size = resource_size(entry->res);
791 pci_addr = entry->res->start - entry->offset;
792 rockchip->msg_bus_addr = pci_addr;
794 for (reg_no = 0; reg_no < (size >> 20); reg_no++) {
795 err = rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1,
796 AXI_WRAPPER_MEM_WRITE,
798 pci_addr + (reg_no << 20),
801 dev_err(dev, "program RC mem outbound ATU failed\n");
806 err = rockchip_pcie_prog_ib_atu(rockchip, 2, 32 - 1, 0x0, 0);
808 dev_err(dev, "program RC mem inbound ATU failed\n");
812 entry = resource_list_first_type(&bridge->windows, IORESOURCE_IO);
816 /* store the register number offset to program RC io outbound ATU */
819 size = resource_size(entry->res);
820 pci_addr = entry->res->start - entry->offset;
822 for (reg_no = 0; reg_no < (size >> 20); reg_no++) {
823 err = rockchip_pcie_prog_ob_atu(rockchip,
825 AXI_WRAPPER_IO_WRITE,
827 pci_addr + (reg_no << 20),
830 dev_err(dev, "program RC io outbound ATU failed\n");
835 /* assign message regions */
836 rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1 + offset,
840 rockchip->msg_bus_addr += ((reg_no + offset) << 20);
844 static int rockchip_pcie_wait_l2(struct rockchip_pcie *rockchip)
849 /* send PME_TURN_OFF message */
850 writel(0x0, rockchip->msg_region + PCIE_RC_SEND_PME_OFF);
852 /* read LTSSM and wait for falling into L2 link state */
853 err = readl_poll_timeout(rockchip->apb_base + PCIE_CLIENT_DEBUG_OUT_0,
854 value, PCIE_LINK_IS_L2(value), 20,
855 jiffies_to_usecs(5 * HZ));
857 dev_err(rockchip->dev, "PCIe link enter L2 timeout!\n");
864 static int rockchip_pcie_suspend_noirq(struct device *dev)
866 struct rockchip_pcie *rockchip = dev_get_drvdata(dev);
869 /* disable core and cli int since we don't need to ack PME_ACK */
870 rockchip_pcie_write(rockchip, (PCIE_CLIENT_INT_CLI << 16) |
871 PCIE_CLIENT_INT_CLI, PCIE_CLIENT_INT_MASK);
872 rockchip_pcie_write(rockchip, (u32)PCIE_CORE_INT, PCIE_CORE_INT_MASK);
874 ret = rockchip_pcie_wait_l2(rockchip);
876 rockchip_pcie_enable_interrupts(rockchip);
880 rockchip_pcie_deinit_phys(rockchip);
882 rockchip_pcie_disable_clocks(rockchip);
884 regulator_disable(rockchip->vpcie0v9);
889 static int rockchip_pcie_resume_noirq(struct device *dev)
891 struct rockchip_pcie *rockchip = dev_get_drvdata(dev);
894 err = regulator_enable(rockchip->vpcie0v9);
896 dev_err(dev, "fail to enable vpcie0v9 regulator\n");
900 err = rockchip_pcie_enable_clocks(rockchip);
902 goto err_disable_0v9;
904 err = rockchip_pcie_host_init_port(rockchip);
906 goto err_pcie_resume;
908 err = rockchip_pcie_cfg_atu(rockchip);
910 goto err_err_deinit_port;
912 /* Need this to enter L1 again */
913 rockchip_pcie_update_txcredit_mui(rockchip);
914 rockchip_pcie_enable_interrupts(rockchip);
919 rockchip_pcie_deinit_phys(rockchip);
921 rockchip_pcie_disable_clocks(rockchip);
923 regulator_disable(rockchip->vpcie0v9);
927 static int rockchip_pcie_probe(struct platform_device *pdev)
929 struct rockchip_pcie *rockchip;
930 struct device *dev = &pdev->dev;
931 struct pci_host_bridge *bridge;
937 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rockchip));
941 rockchip = pci_host_bridge_priv(bridge);
943 platform_set_drvdata(pdev, rockchip);
945 rockchip->is_rc = true;
947 err = rockchip_pcie_parse_host_dt(rockchip);
951 err = rockchip_pcie_enable_clocks(rockchip);
955 err = rockchip_pcie_set_vpcie(rockchip);
957 dev_err(dev, "failed to set vpcie regulator\n");
961 err = rockchip_pcie_host_init_port(rockchip);
965 err = rockchip_pcie_init_irq_domain(rockchip);
967 goto err_deinit_port;
969 err = rockchip_pcie_cfg_atu(rockchip);
971 goto err_remove_irq_domain;
973 rockchip->msg_region = devm_ioremap(dev, rockchip->msg_bus_addr, SZ_1M);
974 if (!rockchip->msg_region) {
976 goto err_remove_irq_domain;
979 bridge->sysdata = rockchip;
980 bridge->ops = &rockchip_pcie_ops;
982 err = rockchip_pcie_setup_irq(rockchip);
984 goto err_remove_irq_domain;
986 rockchip_pcie_enable_interrupts(rockchip);
988 err = pci_host_probe(bridge);
990 goto err_remove_irq_domain;
994 err_remove_irq_domain:
995 irq_domain_remove(rockchip->irq_domain);
997 rockchip_pcie_deinit_phys(rockchip);
999 if (!IS_ERR(rockchip->vpcie12v))
1000 regulator_disable(rockchip->vpcie12v);
1001 if (!IS_ERR(rockchip->vpcie3v3))
1002 regulator_disable(rockchip->vpcie3v3);
1003 regulator_disable(rockchip->vpcie1v8);
1004 regulator_disable(rockchip->vpcie0v9);
1006 rockchip_pcie_disable_clocks(rockchip);
1010 static void rockchip_pcie_remove(struct platform_device *pdev)
1012 struct device *dev = &pdev->dev;
1013 struct rockchip_pcie *rockchip = dev_get_drvdata(dev);
1014 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(rockchip);
1016 pci_stop_root_bus(bridge->bus);
1017 pci_remove_root_bus(bridge->bus);
1018 irq_domain_remove(rockchip->irq_domain);
1020 rockchip_pcie_deinit_phys(rockchip);
1022 rockchip_pcie_disable_clocks(rockchip);
1024 if (!IS_ERR(rockchip->vpcie12v))
1025 regulator_disable(rockchip->vpcie12v);
1026 if (!IS_ERR(rockchip->vpcie3v3))
1027 regulator_disable(rockchip->vpcie3v3);
1028 regulator_disable(rockchip->vpcie1v8);
1029 regulator_disable(rockchip->vpcie0v9);
1032 static const struct dev_pm_ops rockchip_pcie_pm_ops = {
1033 NOIRQ_SYSTEM_SLEEP_PM_OPS(rockchip_pcie_suspend_noirq,
1034 rockchip_pcie_resume_noirq)
1037 static const struct of_device_id rockchip_pcie_of_match[] = {
1038 { .compatible = "rockchip,rk3399-pcie", },
1041 MODULE_DEVICE_TABLE(of, rockchip_pcie_of_match);
1043 static struct platform_driver rockchip_pcie_driver = {
1045 .name = "rockchip-pcie",
1046 .of_match_table = rockchip_pcie_of_match,
1047 .pm = &rockchip_pcie_pm_ops,
1049 .probe = rockchip_pcie_probe,
1050 .remove_new = rockchip_pcie_remove,
1052 module_platform_driver(rockchip_pcie_driver);
1054 MODULE_AUTHOR("Rockchip Inc");
1055 MODULE_DESCRIPTION("Rockchip AXI PCIe driver");
1056 MODULE_LICENSE("GPL v2");