1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Kirin Phone SoCs
5 * Copyright (C) 2017 HiSilicon Electronics Co., Ltd.
6 * https://www.huawei.com
11 #include <linux/clk.h>
12 #include <linux/compiler.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/interrupt.h>
17 #include <linux/mfd/syscon.h>
19 #include <linux/of_pci.h>
20 #include <linux/phy/phy.h>
21 #include <linux/pci.h>
22 #include <linux/pci_regs.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/resource.h>
26 #include <linux/types.h>
27 #include "pcie-designware.h"
29 #define to_kirin_pcie(x) dev_get_drvdata((x)->dev)
31 /* PCIe ELBI registers */
32 #define SOC_PCIECTRL_CTRL0_ADDR 0x000
33 #define SOC_PCIECTRL_CTRL1_ADDR 0x004
34 #define PCIE_ELBI_SLV_DBI_ENABLE (0x1 << 21)
36 /* info located in APB */
37 #define PCIE_APP_LTSSM_ENABLE 0x01c
38 #define PCIE_APB_PHY_STATUS0 0x400
39 #define PCIE_LINKUP_ENABLE (0x8020)
40 #define PCIE_LTSSM_ENABLE_BIT (0x1 << 11)
42 /* info located in sysctrl */
43 #define SCTRL_PCIE_CMOS_OFFSET 0x60
44 #define SCTRL_PCIE_CMOS_BIT 0x10
45 #define SCTRL_PCIE_ISO_OFFSET 0x44
46 #define SCTRL_PCIE_ISO_BIT 0x30
47 #define SCTRL_PCIE_HPCLK_OFFSET 0x190
48 #define SCTRL_PCIE_HPCLK_BIT 0x184000
49 #define SCTRL_PCIE_OE_OFFSET 0x14a
50 #define PCIE_DEBOUNCE_PARAM 0xF0F400
51 #define PCIE_OE_BYPASS (0x3 << 28)
54 * Max number of connected PCI slots at an external PCI bridge
56 * This is used on HiKey 970, which has a PEX 8606 bridge with 4 connected
57 * lanes (lane 0 upstream, and the other three lanes, one connected to an
58 * in-board Ethernet adapter and the other two connected to M.2 and mini
61 * Each slot has a different clock source and uses a separate PERST# pin.
63 #define MAX_PCI_SLOTS 3
65 enum pcie_kirin_phy_type {
66 PCIE_KIRIN_INTERNAL_PHY,
67 PCIE_KIRIN_EXTERNAL_PHY
71 enum pcie_kirin_phy_type type;
76 void *phy_priv; /* only for PCIE_KIRIN_INTERNAL_PHY */
79 struct gpio_desc *id_dwc_perst_gpio;
83 struct gpio_desc *id_reset_gpio[MAX_PCI_SLOTS];
84 const char *reset_names[MAX_PCI_SLOTS];
88 struct gpio_desc *id_clkreq_gpio[MAX_PCI_SLOTS];
89 const char *clkreq_names[MAX_PCI_SLOTS];
93 * Kirin 960 PHY. Can't be split into a PHY driver without changing the
97 #define REF_CLK_FREQ 100000000
99 /* PHY info located in APB */
100 #define PCIE_APB_PHY_CTRL0 0x0
101 #define PCIE_APB_PHY_CTRL1 0x4
102 #define PCIE_APB_PHY_STATUS0 0x400
103 #define PIPE_CLK_STABLE BIT(19)
104 #define PHY_REF_PAD_BIT BIT(8)
105 #define PHY_PWR_DOWN_BIT BIT(22)
106 #define PHY_RST_ACK_BIT BIT(16)
109 #define CRGCTRL_PCIE_ASSERT_OFFSET 0x88
110 #define CRGCTRL_PCIE_ASSERT_BIT 0x8c000000
113 #define REF_2_PERST_MIN 21000
114 #define REF_2_PERST_MAX 25000
115 #define PERST_2_ACCESS_MIN 10000
116 #define PERST_2_ACCESS_MAX 12000
117 #define PIPE_CLK_WAIT_MIN 550
118 #define PIPE_CLK_WAIT_MAX 600
119 #define TIME_CMOS_MIN 100
120 #define TIME_CMOS_MAX 105
121 #define TIME_PHY_PD_MIN 10
122 #define TIME_PHY_PD_MAX 11
124 struct hi3660_pcie_phy {
127 struct regmap *crgctrl;
128 struct regmap *sysctrl;
129 struct clk *apb_sys_clk;
130 struct clk *apb_phy_clk;
131 struct clk *phy_ref_clk;
136 /* Registers in PCIePHY */
137 static inline void kirin_apb_phy_writel(struct hi3660_pcie_phy *hi3660_pcie_phy,
140 writel(val, hi3660_pcie_phy->base + reg);
143 static inline u32 kirin_apb_phy_readl(struct hi3660_pcie_phy *hi3660_pcie_phy,
146 return readl(hi3660_pcie_phy->base + reg);
149 static int hi3660_pcie_phy_get_clk(struct hi3660_pcie_phy *phy)
151 struct device *dev = phy->dev;
153 phy->phy_ref_clk = devm_clk_get(dev, "pcie_phy_ref");
154 if (IS_ERR(phy->phy_ref_clk))
155 return PTR_ERR(phy->phy_ref_clk);
157 phy->aux_clk = devm_clk_get(dev, "pcie_aux");
158 if (IS_ERR(phy->aux_clk))
159 return PTR_ERR(phy->aux_clk);
161 phy->apb_phy_clk = devm_clk_get(dev, "pcie_apb_phy");
162 if (IS_ERR(phy->apb_phy_clk))
163 return PTR_ERR(phy->apb_phy_clk);
165 phy->apb_sys_clk = devm_clk_get(dev, "pcie_apb_sys");
166 if (IS_ERR(phy->apb_sys_clk))
167 return PTR_ERR(phy->apb_sys_clk);
169 phy->aclk = devm_clk_get(dev, "pcie_aclk");
170 if (IS_ERR(phy->aclk))
171 return PTR_ERR(phy->aclk);
176 static int hi3660_pcie_phy_get_resource(struct hi3660_pcie_phy *phy)
178 struct device *dev = phy->dev;
179 struct platform_device *pdev;
182 pdev = container_of(dev, struct platform_device, dev);
184 phy->base = devm_platform_ioremap_resource_byname(pdev, "phy");
185 if (IS_ERR(phy->base))
186 return PTR_ERR(phy->base);
188 phy->crgctrl = syscon_regmap_lookup_by_compatible("hisilicon,hi3660-crgctrl");
189 if (IS_ERR(phy->crgctrl))
190 return PTR_ERR(phy->crgctrl);
192 phy->sysctrl = syscon_regmap_lookup_by_compatible("hisilicon,hi3660-sctrl");
193 if (IS_ERR(phy->sysctrl))
194 return PTR_ERR(phy->sysctrl);
199 static int hi3660_pcie_phy_start(struct hi3660_pcie_phy *phy)
201 struct device *dev = phy->dev;
204 reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_CTRL1);
205 reg_val &= ~PHY_REF_PAD_BIT;
206 kirin_apb_phy_writel(phy, reg_val, PCIE_APB_PHY_CTRL1);
208 reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_CTRL0);
209 reg_val &= ~PHY_PWR_DOWN_BIT;
210 kirin_apb_phy_writel(phy, reg_val, PCIE_APB_PHY_CTRL0);
211 usleep_range(TIME_PHY_PD_MIN, TIME_PHY_PD_MAX);
213 reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_CTRL1);
214 reg_val &= ~PHY_RST_ACK_BIT;
215 kirin_apb_phy_writel(phy, reg_val, PCIE_APB_PHY_CTRL1);
217 usleep_range(PIPE_CLK_WAIT_MIN, PIPE_CLK_WAIT_MAX);
218 reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_STATUS0);
219 if (reg_val & PIPE_CLK_STABLE) {
220 dev_err(dev, "PIPE clk is not stable\n");
227 static void hi3660_pcie_phy_oe_enable(struct hi3660_pcie_phy *phy)
231 regmap_read(phy->sysctrl, SCTRL_PCIE_OE_OFFSET, &val);
232 val |= PCIE_DEBOUNCE_PARAM;
233 val &= ~PCIE_OE_BYPASS;
234 regmap_write(phy->sysctrl, SCTRL_PCIE_OE_OFFSET, val);
237 static int hi3660_pcie_phy_clk_ctrl(struct hi3660_pcie_phy *phy, bool enable)
244 ret = clk_set_rate(phy->phy_ref_clk, REF_CLK_FREQ);
248 ret = clk_prepare_enable(phy->phy_ref_clk);
252 ret = clk_prepare_enable(phy->apb_sys_clk);
256 ret = clk_prepare_enable(phy->apb_phy_clk);
260 ret = clk_prepare_enable(phy->aclk);
264 ret = clk_prepare_enable(phy->aux_clk);
271 clk_disable_unprepare(phy->aux_clk);
273 clk_disable_unprepare(phy->aclk);
275 clk_disable_unprepare(phy->apb_phy_clk);
277 clk_disable_unprepare(phy->apb_sys_clk);
279 clk_disable_unprepare(phy->phy_ref_clk);
284 static int hi3660_pcie_phy_power_on(struct kirin_pcie *pcie)
286 struct hi3660_pcie_phy *phy = pcie->phy_priv;
289 /* Power supply for Host */
290 regmap_write(phy->sysctrl,
291 SCTRL_PCIE_CMOS_OFFSET, SCTRL_PCIE_CMOS_BIT);
292 usleep_range(TIME_CMOS_MIN, TIME_CMOS_MAX);
294 hi3660_pcie_phy_oe_enable(phy);
296 ret = hi3660_pcie_phy_clk_ctrl(phy, true);
300 /* ISO disable, PCIeCtrl, PHY assert and clk gate clear */
301 regmap_write(phy->sysctrl,
302 SCTRL_PCIE_ISO_OFFSET, SCTRL_PCIE_ISO_BIT);
303 regmap_write(phy->crgctrl,
304 CRGCTRL_PCIE_ASSERT_OFFSET, CRGCTRL_PCIE_ASSERT_BIT);
305 regmap_write(phy->sysctrl,
306 SCTRL_PCIE_HPCLK_OFFSET, SCTRL_PCIE_HPCLK_BIT);
308 ret = hi3660_pcie_phy_start(phy);
315 hi3660_pcie_phy_clk_ctrl(phy, false);
319 static int hi3660_pcie_phy_init(struct platform_device *pdev,
320 struct kirin_pcie *pcie)
322 struct device *dev = &pdev->dev;
323 struct hi3660_pcie_phy *phy;
326 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
330 pcie->phy_priv = phy;
333 ret = hi3660_pcie_phy_get_clk(phy);
337 return hi3660_pcie_phy_get_resource(phy);
340 static int hi3660_pcie_phy_power_off(struct kirin_pcie *pcie)
342 struct hi3660_pcie_phy *phy = pcie->phy_priv;
344 /* Drop power supply for Host */
345 regmap_write(phy->sysctrl, SCTRL_PCIE_CMOS_OFFSET, 0x00);
347 hi3660_pcie_phy_clk_ctrl(phy, false);
353 * The non-PHY part starts here
356 static const struct regmap_config pcie_kirin_regmap_conf = {
357 .name = "kirin_pcie_apb",
363 static int kirin_pcie_get_gpio_enable(struct kirin_pcie *pcie,
364 struct platform_device *pdev)
366 struct device *dev = &pdev->dev;
369 /* This is an optional property */
370 ret = gpiod_count(dev, "hisilicon,clken");
374 if (ret > MAX_PCI_SLOTS) {
375 dev_err(dev, "Too many GPIO clock requests!\n");
379 pcie->n_gpio_clkreq = ret;
381 for (i = 0; i < pcie->n_gpio_clkreq; i++) {
382 pcie->id_clkreq_gpio[i] = devm_gpiod_get_index(dev,
383 "hisilicon,clken", i,
385 if (IS_ERR(pcie->id_clkreq_gpio[i]))
386 return dev_err_probe(dev, PTR_ERR(pcie->id_clkreq_gpio[i]),
387 "unable to get a valid clken gpio\n");
389 pcie->clkreq_names[i] = devm_kasprintf(dev, GFP_KERNEL,
390 "pcie_clkreq_%d", i);
391 if (!pcie->clkreq_names[i])
394 gpiod_set_consumer_name(pcie->id_clkreq_gpio[i],
395 pcie->clkreq_names[i]);
401 static int kirin_pcie_parse_port(struct kirin_pcie *pcie,
402 struct platform_device *pdev,
403 struct device_node *node)
405 struct device *dev = &pdev->dev;
408 for_each_available_child_of_node_scoped(node, parent) {
409 for_each_available_child_of_node_scoped(parent, child) {
412 pcie->id_reset_gpio[i] = devm_fwnode_gpiod_get_index(dev,
413 of_fwnode_handle(child),
414 "reset", 0, GPIOD_OUT_LOW,
416 if (IS_ERR(pcie->id_reset_gpio[i])) {
417 if (PTR_ERR(pcie->id_reset_gpio[i]) == -ENOENT)
419 return dev_err_probe(dev, PTR_ERR(pcie->id_reset_gpio[i]),
420 "unable to get a valid reset gpio\n");
423 if (pcie->num_slots + 1 >= MAX_PCI_SLOTS) {
424 dev_err(dev, "Too many PCI slots!\n");
429 ret = of_pci_get_devfn(child);
431 dev_err(dev, "failed to parse devfn: %d\n", ret);
435 slot = PCI_SLOT(ret);
437 pcie->reset_names[i] = devm_kasprintf(dev, GFP_KERNEL,
440 if (!pcie->reset_names[i])
443 gpiod_set_consumer_name(pcie->id_reset_gpio[i],
444 pcie->reset_names[i]);
451 static long kirin_pcie_get_resource(struct kirin_pcie *kirin_pcie,
452 struct platform_device *pdev)
454 struct device *dev = &pdev->dev;
455 struct device_node *child, *node = dev->of_node;
456 void __iomem *apb_base;
459 apb_base = devm_platform_ioremap_resource_byname(pdev, "apb");
460 if (IS_ERR(apb_base))
461 return PTR_ERR(apb_base);
463 kirin_pcie->apb = devm_regmap_init_mmio(dev, apb_base,
464 &pcie_kirin_regmap_conf);
465 if (IS_ERR(kirin_pcie->apb))
466 return PTR_ERR(kirin_pcie->apb);
468 /* pcie internal PERST# gpio */
469 kirin_pcie->id_dwc_perst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
470 if (IS_ERR(kirin_pcie->id_dwc_perst_gpio))
471 return dev_err_probe(dev, PTR_ERR(kirin_pcie->id_dwc_perst_gpio),
472 "unable to get a valid gpio pin\n");
473 gpiod_set_consumer_name(kirin_pcie->id_dwc_perst_gpio, "pcie_perst_bridge");
475 ret = kirin_pcie_get_gpio_enable(kirin_pcie, pdev);
479 /* Parse OF children */
480 for_each_available_child_of_node(node, child) {
481 ret = kirin_pcie_parse_port(kirin_pcie, pdev, child);
493 static void kirin_pcie_sideband_dbi_w_mode(struct kirin_pcie *kirin_pcie,
498 regmap_read(kirin_pcie->apb, SOC_PCIECTRL_CTRL0_ADDR, &val);
500 val = val | PCIE_ELBI_SLV_DBI_ENABLE;
502 val = val & ~PCIE_ELBI_SLV_DBI_ENABLE;
504 regmap_write(kirin_pcie->apb, SOC_PCIECTRL_CTRL0_ADDR, val);
507 static void kirin_pcie_sideband_dbi_r_mode(struct kirin_pcie *kirin_pcie,
512 regmap_read(kirin_pcie->apb, SOC_PCIECTRL_CTRL1_ADDR, &val);
514 val = val | PCIE_ELBI_SLV_DBI_ENABLE;
516 val = val & ~PCIE_ELBI_SLV_DBI_ENABLE;
518 regmap_write(kirin_pcie->apb, SOC_PCIECTRL_CTRL1_ADDR, val);
521 static int kirin_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
522 int where, int size, u32 *val)
524 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
527 return PCIBIOS_DEVICE_NOT_FOUND;
529 *val = dw_pcie_read_dbi(pci, where, size);
530 return PCIBIOS_SUCCESSFUL;
533 static int kirin_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
534 int where, int size, u32 val)
536 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
539 return PCIBIOS_DEVICE_NOT_FOUND;
541 dw_pcie_write_dbi(pci, where, size, val);
542 return PCIBIOS_SUCCESSFUL;
545 static int kirin_pcie_add_bus(struct pci_bus *bus)
547 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
548 struct kirin_pcie *kirin_pcie = to_kirin_pcie(pci);
551 if (!kirin_pcie->num_slots)
554 /* Send PERST# to each slot */
555 for (i = 0; i < kirin_pcie->num_slots; i++) {
556 ret = gpiod_direction_output_raw(kirin_pcie->id_reset_gpio[i], 1);
558 dev_err(pci->dev, "PERST# %s error: %d\n",
559 kirin_pcie->reset_names[i], ret);
562 usleep_range(PERST_2_ACCESS_MIN, PERST_2_ACCESS_MAX);
567 static struct pci_ops kirin_pci_ops = {
568 .read = kirin_pcie_rd_own_conf,
569 .write = kirin_pcie_wr_own_conf,
570 .add_bus = kirin_pcie_add_bus,
573 static u32 kirin_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
574 u32 reg, size_t size)
576 struct kirin_pcie *kirin_pcie = to_kirin_pcie(pci);
579 kirin_pcie_sideband_dbi_r_mode(kirin_pcie, true);
580 dw_pcie_read(base + reg, size, &ret);
581 kirin_pcie_sideband_dbi_r_mode(kirin_pcie, false);
586 static void kirin_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
587 u32 reg, size_t size, u32 val)
589 struct kirin_pcie *kirin_pcie = to_kirin_pcie(pci);
591 kirin_pcie_sideband_dbi_w_mode(kirin_pcie, true);
592 dw_pcie_write(base + reg, size, val);
593 kirin_pcie_sideband_dbi_w_mode(kirin_pcie, false);
596 static int kirin_pcie_link_up(struct dw_pcie *pci)
598 struct kirin_pcie *kirin_pcie = to_kirin_pcie(pci);
601 regmap_read(kirin_pcie->apb, PCIE_APB_PHY_STATUS0, &val);
602 if ((val & PCIE_LINKUP_ENABLE) == PCIE_LINKUP_ENABLE)
608 static int kirin_pcie_start_link(struct dw_pcie *pci)
610 struct kirin_pcie *kirin_pcie = to_kirin_pcie(pci);
612 /* assert LTSSM enable */
613 regmap_write(kirin_pcie->apb, PCIE_APP_LTSSM_ENABLE,
614 PCIE_LTSSM_ENABLE_BIT);
619 static int kirin_pcie_host_init(struct dw_pcie_rp *pp)
621 pp->bridge->ops = &kirin_pci_ops;
626 static const struct dw_pcie_ops kirin_dw_pcie_ops = {
627 .read_dbi = kirin_pcie_read_dbi,
628 .write_dbi = kirin_pcie_write_dbi,
629 .link_up = kirin_pcie_link_up,
630 .start_link = kirin_pcie_start_link,
633 static const struct dw_pcie_host_ops kirin_pcie_host_ops = {
634 .init = kirin_pcie_host_init,
637 static int kirin_pcie_power_off(struct kirin_pcie *kirin_pcie)
641 if (kirin_pcie->type == PCIE_KIRIN_INTERNAL_PHY)
642 return hi3660_pcie_phy_power_off(kirin_pcie);
644 for (i = 0; i < kirin_pcie->n_gpio_clkreq; i++)
645 gpiod_direction_output_raw(kirin_pcie->id_clkreq_gpio[i], 1);
647 phy_power_off(kirin_pcie->phy);
648 phy_exit(kirin_pcie->phy);
653 static int kirin_pcie_power_on(struct platform_device *pdev,
654 struct kirin_pcie *kirin_pcie)
656 struct device *dev = &pdev->dev;
659 if (kirin_pcie->type == PCIE_KIRIN_INTERNAL_PHY) {
660 ret = hi3660_pcie_phy_init(pdev, kirin_pcie);
664 ret = hi3660_pcie_phy_power_on(kirin_pcie);
668 kirin_pcie->phy = devm_of_phy_get(dev, dev->of_node, NULL);
669 if (IS_ERR(kirin_pcie->phy))
670 return PTR_ERR(kirin_pcie->phy);
672 ret = phy_init(kirin_pcie->phy);
676 ret = phy_power_on(kirin_pcie->phy);
681 /* perst assert Endpoint */
682 usleep_range(REF_2_PERST_MIN, REF_2_PERST_MAX);
684 ret = gpiod_direction_output_raw(kirin_pcie->id_dwc_perst_gpio, 1);
688 usleep_range(PERST_2_ACCESS_MIN, PERST_2_ACCESS_MAX);
692 kirin_pcie_power_off(kirin_pcie);
697 static void kirin_pcie_remove(struct platform_device *pdev)
699 struct kirin_pcie *kirin_pcie = platform_get_drvdata(pdev);
701 dw_pcie_host_deinit(&kirin_pcie->pci->pp);
703 kirin_pcie_power_off(kirin_pcie);
706 struct kirin_pcie_data {
707 enum pcie_kirin_phy_type phy_type;
710 static const struct kirin_pcie_data kirin_960_data = {
711 .phy_type = PCIE_KIRIN_INTERNAL_PHY,
714 static const struct kirin_pcie_data kirin_970_data = {
715 .phy_type = PCIE_KIRIN_EXTERNAL_PHY,
718 static const struct of_device_id kirin_pcie_match[] = {
719 { .compatible = "hisilicon,kirin960-pcie", .data = &kirin_960_data },
720 { .compatible = "hisilicon,kirin970-pcie", .data = &kirin_970_data },
724 static int kirin_pcie_probe(struct platform_device *pdev)
726 struct device *dev = &pdev->dev;
727 const struct kirin_pcie_data *data;
728 struct kirin_pcie *kirin_pcie;
733 dev_err(dev, "NULL node\n");
737 data = of_device_get_match_data(dev);
739 dev_err(dev, "OF data missing\n");
743 kirin_pcie = devm_kzalloc(dev, sizeof(struct kirin_pcie), GFP_KERNEL);
747 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
752 pci->ops = &kirin_dw_pcie_ops;
753 pci->pp.ops = &kirin_pcie_host_ops;
754 kirin_pcie->pci = pci;
755 kirin_pcie->type = data->phy_type;
757 ret = kirin_pcie_get_resource(kirin_pcie, pdev);
761 platform_set_drvdata(pdev, kirin_pcie);
763 ret = kirin_pcie_power_on(pdev, kirin_pcie);
767 return dw_pcie_host_init(&pci->pp);
770 static struct platform_driver kirin_pcie_driver = {
771 .probe = kirin_pcie_probe,
772 .remove = kirin_pcie_remove,
774 .name = "kirin-pcie",
775 .of_match_table = kirin_pcie_match,
776 .suppress_bind_attrs = true,
779 module_platform_driver(kirin_pcie_driver);
781 MODULE_DEVICE_TABLE(of, kirin_pcie_match);
782 MODULE_DESCRIPTION("PCIe host controller driver for Kirin Phone SoCs");
784 MODULE_LICENSE("GPL v2");