Lines Matching +full:pcie +full:- +full:phy
1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Kirin Phone SoCs
22 #include <linux/phy/phy.h>
29 #include "pcie-designware.h"
31 #define to_kirin_pcie(x) dev_get_drvdata((x)->dev)
33 /* PCIe ELBI registers */
60 * in-board Ethernet adapter and the other two connected to M.2 and mini
77 struct phy *phy; member
83 /* Per-slot PERST# */
88 /* Per-slot clkreq */
95 * Kirin 960 PHY. Can't be split into a PHY driver without changing the
101 /* PHY info located in APB */
142 writel(val, hi3660_pcie_phy->base + reg); in kirin_apb_phy_writel()
148 return readl(hi3660_pcie_phy->base + reg); in kirin_apb_phy_readl()
151 static int hi3660_pcie_phy_get_clk(struct hi3660_pcie_phy *phy) in hi3660_pcie_phy_get_clk() argument
153 struct device *dev = phy->dev; in hi3660_pcie_phy_get_clk()
155 phy->phy_ref_clk = devm_clk_get(dev, "pcie_phy_ref"); in hi3660_pcie_phy_get_clk()
156 if (IS_ERR(phy->phy_ref_clk)) in hi3660_pcie_phy_get_clk()
157 return PTR_ERR(phy->phy_ref_clk); in hi3660_pcie_phy_get_clk()
159 phy->aux_clk = devm_clk_get(dev, "pcie_aux"); in hi3660_pcie_phy_get_clk()
160 if (IS_ERR(phy->aux_clk)) in hi3660_pcie_phy_get_clk()
161 return PTR_ERR(phy->aux_clk); in hi3660_pcie_phy_get_clk()
163 phy->apb_phy_clk = devm_clk_get(dev, "pcie_apb_phy"); in hi3660_pcie_phy_get_clk()
164 if (IS_ERR(phy->apb_phy_clk)) in hi3660_pcie_phy_get_clk()
165 return PTR_ERR(phy->apb_phy_clk); in hi3660_pcie_phy_get_clk()
167 phy->apb_sys_clk = devm_clk_get(dev, "pcie_apb_sys"); in hi3660_pcie_phy_get_clk()
168 if (IS_ERR(phy->apb_sys_clk)) in hi3660_pcie_phy_get_clk()
169 return PTR_ERR(phy->apb_sys_clk); in hi3660_pcie_phy_get_clk()
171 phy->aclk = devm_clk_get(dev, "pcie_aclk"); in hi3660_pcie_phy_get_clk()
172 if (IS_ERR(phy->aclk)) in hi3660_pcie_phy_get_clk()
173 return PTR_ERR(phy->aclk); in hi3660_pcie_phy_get_clk()
178 static int hi3660_pcie_phy_get_resource(struct hi3660_pcie_phy *phy) in hi3660_pcie_phy_get_resource() argument
180 struct device *dev = phy->dev; in hi3660_pcie_phy_get_resource()
186 phy->base = devm_platform_ioremap_resource_byname(pdev, "phy"); in hi3660_pcie_phy_get_resource()
187 if (IS_ERR(phy->base)) in hi3660_pcie_phy_get_resource()
188 return PTR_ERR(phy->base); in hi3660_pcie_phy_get_resource()
190 phy->crgctrl = syscon_regmap_lookup_by_compatible("hisilicon,hi3660-crgctrl"); in hi3660_pcie_phy_get_resource()
191 if (IS_ERR(phy->crgctrl)) in hi3660_pcie_phy_get_resource()
192 return PTR_ERR(phy->crgctrl); in hi3660_pcie_phy_get_resource()
194 phy->sysctrl = syscon_regmap_lookup_by_compatible("hisilicon,hi3660-sctrl"); in hi3660_pcie_phy_get_resource()
195 if (IS_ERR(phy->sysctrl)) in hi3660_pcie_phy_get_resource()
196 return PTR_ERR(phy->sysctrl); in hi3660_pcie_phy_get_resource()
201 static int hi3660_pcie_phy_start(struct hi3660_pcie_phy *phy) in hi3660_pcie_phy_start() argument
203 struct device *dev = phy->dev; in hi3660_pcie_phy_start()
206 reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_CTRL1); in hi3660_pcie_phy_start()
208 kirin_apb_phy_writel(phy, reg_val, PCIE_APB_PHY_CTRL1); in hi3660_pcie_phy_start()
210 reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_CTRL0); in hi3660_pcie_phy_start()
212 kirin_apb_phy_writel(phy, reg_val, PCIE_APB_PHY_CTRL0); in hi3660_pcie_phy_start()
215 reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_CTRL1); in hi3660_pcie_phy_start()
217 kirin_apb_phy_writel(phy, reg_val, PCIE_APB_PHY_CTRL1); in hi3660_pcie_phy_start()
220 reg_val = kirin_apb_phy_readl(phy, PCIE_APB_PHY_STATUS0); in hi3660_pcie_phy_start()
223 return -EINVAL; in hi3660_pcie_phy_start()
229 static void hi3660_pcie_phy_oe_enable(struct hi3660_pcie_phy *phy) in hi3660_pcie_phy_oe_enable() argument
233 regmap_read(phy->sysctrl, SCTRL_PCIE_OE_OFFSET, &val); in hi3660_pcie_phy_oe_enable()
236 regmap_write(phy->sysctrl, SCTRL_PCIE_OE_OFFSET, val); in hi3660_pcie_phy_oe_enable()
239 static int hi3660_pcie_phy_clk_ctrl(struct hi3660_pcie_phy *phy, bool enable) in hi3660_pcie_phy_clk_ctrl() argument
246 ret = clk_set_rate(phy->phy_ref_clk, REF_CLK_FREQ); in hi3660_pcie_phy_clk_ctrl()
250 ret = clk_prepare_enable(phy->phy_ref_clk); in hi3660_pcie_phy_clk_ctrl()
254 ret = clk_prepare_enable(phy->apb_sys_clk); in hi3660_pcie_phy_clk_ctrl()
258 ret = clk_prepare_enable(phy->apb_phy_clk); in hi3660_pcie_phy_clk_ctrl()
262 ret = clk_prepare_enable(phy->aclk); in hi3660_pcie_phy_clk_ctrl()
266 ret = clk_prepare_enable(phy->aux_clk); in hi3660_pcie_phy_clk_ctrl()
273 clk_disable_unprepare(phy->aux_clk); in hi3660_pcie_phy_clk_ctrl()
275 clk_disable_unprepare(phy->aclk); in hi3660_pcie_phy_clk_ctrl()
277 clk_disable_unprepare(phy->apb_phy_clk); in hi3660_pcie_phy_clk_ctrl()
279 clk_disable_unprepare(phy->apb_sys_clk); in hi3660_pcie_phy_clk_ctrl()
281 clk_disable_unprepare(phy->phy_ref_clk); in hi3660_pcie_phy_clk_ctrl()
286 static int hi3660_pcie_phy_power_on(struct kirin_pcie *pcie) in hi3660_pcie_phy_power_on() argument
288 struct hi3660_pcie_phy *phy = pcie->phy_priv; in hi3660_pcie_phy_power_on() local
292 regmap_write(phy->sysctrl, in hi3660_pcie_phy_power_on()
296 hi3660_pcie_phy_oe_enable(phy); in hi3660_pcie_phy_power_on()
298 ret = hi3660_pcie_phy_clk_ctrl(phy, true); in hi3660_pcie_phy_power_on()
302 /* ISO disable, PCIeCtrl, PHY assert and clk gate clear */ in hi3660_pcie_phy_power_on()
303 regmap_write(phy->sysctrl, in hi3660_pcie_phy_power_on()
305 regmap_write(phy->crgctrl, in hi3660_pcie_phy_power_on()
307 regmap_write(phy->sysctrl, in hi3660_pcie_phy_power_on()
310 ret = hi3660_pcie_phy_start(phy); in hi3660_pcie_phy_power_on()
317 hi3660_pcie_phy_clk_ctrl(phy, false); in hi3660_pcie_phy_power_on()
322 struct kirin_pcie *pcie) in hi3660_pcie_phy_init() argument
324 struct device *dev = &pdev->dev; in hi3660_pcie_phy_init()
325 struct hi3660_pcie_phy *phy; in hi3660_pcie_phy_init() local
328 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL); in hi3660_pcie_phy_init()
329 if (!phy) in hi3660_pcie_phy_init()
330 return -ENOMEM; in hi3660_pcie_phy_init()
332 pcie->phy_priv = phy; in hi3660_pcie_phy_init()
333 phy->dev = dev; in hi3660_pcie_phy_init()
335 ret = hi3660_pcie_phy_get_clk(phy); in hi3660_pcie_phy_init()
339 return hi3660_pcie_phy_get_resource(phy); in hi3660_pcie_phy_init()
342 static int hi3660_pcie_phy_power_off(struct kirin_pcie *pcie) in hi3660_pcie_phy_power_off() argument
344 struct hi3660_pcie_phy *phy = pcie->phy_priv; in hi3660_pcie_phy_power_off() local
347 regmap_write(phy->sysctrl, SCTRL_PCIE_CMOS_OFFSET, 0x00); in hi3660_pcie_phy_power_off()
349 hi3660_pcie_phy_clk_ctrl(phy, false); in hi3660_pcie_phy_power_off()
355 * The non-PHY part starts here
365 static int kirin_pcie_get_gpio_enable(struct kirin_pcie *pcie, in kirin_pcie_get_gpio_enable() argument
368 struct device *dev = &pdev->dev; in kirin_pcie_get_gpio_enable()
378 return -EINVAL; in kirin_pcie_get_gpio_enable()
381 pcie->n_gpio_clkreq = ret; in kirin_pcie_get_gpio_enable()
383 for (i = 0; i < pcie->n_gpio_clkreq; i++) { in kirin_pcie_get_gpio_enable()
384 pcie->gpio_id_clkreq[i] = of_get_named_gpio(dev->of_node, in kirin_pcie_get_gpio_enable()
385 "hisilicon,clken-gpios", i); in kirin_pcie_get_gpio_enable()
386 if (pcie->gpio_id_clkreq[i] < 0) in kirin_pcie_get_gpio_enable()
387 return pcie->gpio_id_clkreq[i]; in kirin_pcie_get_gpio_enable()
389 pcie->clkreq_names[i] = devm_kasprintf(dev, GFP_KERNEL, in kirin_pcie_get_gpio_enable()
391 if (!pcie->clkreq_names[i]) in kirin_pcie_get_gpio_enable()
392 return -ENOMEM; in kirin_pcie_get_gpio_enable()
398 static int kirin_pcie_parse_port(struct kirin_pcie *pcie, in kirin_pcie_parse_port() argument
402 struct device *dev = &pdev->dev; in kirin_pcie_parse_port()
408 i = pcie->num_slots; in kirin_pcie_parse_port()
410 pcie->gpio_id_reset[i] = of_get_named_gpio(child, in kirin_pcie_parse_port()
411 "reset-gpios", 0); in kirin_pcie_parse_port()
412 if (pcie->gpio_id_reset[i] < 0) in kirin_pcie_parse_port()
415 pcie->num_slots++; in kirin_pcie_parse_port()
416 if (pcie->num_slots > MAX_PCI_SLOTS) { in kirin_pcie_parse_port()
418 ret = -EINVAL; in kirin_pcie_parse_port()
430 pcie->reset_names[i] = devm_kasprintf(dev, GFP_KERNEL, in kirin_pcie_parse_port()
433 if (!pcie->reset_names[i]) { in kirin_pcie_parse_port()
434 ret = -ENOMEM; in kirin_pcie_parse_port()
451 struct device *dev = &pdev->dev; in kirin_pcie_get_resource()
452 struct device_node *child, *node = dev->of_node; in kirin_pcie_get_resource()
460 kirin_pcie->apb = devm_regmap_init_mmio(dev, apb_base, in kirin_pcie_get_resource()
462 if (IS_ERR(kirin_pcie->apb)) in kirin_pcie_get_resource()
463 return PTR_ERR(kirin_pcie->apb); in kirin_pcie_get_resource()
465 /* pcie internal PERST# gpio */ in kirin_pcie_get_resource()
466 kirin_pcie->gpio_id_dwc_perst = of_get_named_gpio(dev->of_node, in kirin_pcie_get_resource()
467 "reset-gpios", 0); in kirin_pcie_get_resource()
468 if (kirin_pcie->gpio_id_dwc_perst == -EPROBE_DEFER) { in kirin_pcie_get_resource()
469 return -EPROBE_DEFER; in kirin_pcie_get_resource()
470 } else if (!gpio_is_valid(kirin_pcie->gpio_id_dwc_perst)) { in kirin_pcie_get_resource()
472 return -ENODEV; in kirin_pcie_get_resource()
498 regmap_read(kirin_pcie->apb, SOC_PCIECTRL_CTRL0_ADDR, &val); in kirin_pcie_sideband_dbi_w_mode()
504 regmap_write(kirin_pcie->apb, SOC_PCIECTRL_CTRL0_ADDR, val); in kirin_pcie_sideband_dbi_w_mode()
512 regmap_read(kirin_pcie->apb, SOC_PCIECTRL_CTRL1_ADDR, &val); in kirin_pcie_sideband_dbi_r_mode()
518 regmap_write(kirin_pcie->apb, SOC_PCIECTRL_CTRL1_ADDR, val); in kirin_pcie_sideband_dbi_r_mode()
524 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata); in kirin_pcie_rd_own_conf()
536 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata); in kirin_pcie_wr_own_conf()
547 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata); in kirin_pcie_add_bus()
551 if (!kirin_pcie->num_slots) in kirin_pcie_add_bus()
555 for (i = 0; i < kirin_pcie->num_slots; i++) { in kirin_pcie_add_bus()
556 ret = gpio_direction_output(kirin_pcie->gpio_id_reset[i], 1); in kirin_pcie_add_bus()
558 dev_err(pci->dev, "PERST# %s error: %d\n", in kirin_pcie_add_bus()
559 kirin_pcie->reset_names[i], ret); in kirin_pcie_add_bus()
601 regmap_read(kirin_pcie->apb, PCIE_APB_PHY_STATUS0, &val); in kirin_pcie_link_up()
613 regmap_write(kirin_pcie->apb, PCIE_APP_LTSSM_ENABLE, in kirin_pcie_start_link()
621 pp->bridge->ops = &kirin_pci_ops; in kirin_pcie_host_init()
631 for (i = 0; i < kirin_pcie->num_slots; i++) { in kirin_pcie_gpio_request()
632 if (!gpio_is_valid(kirin_pcie->gpio_id_reset[i])) { in kirin_pcie_gpio_request()
634 kirin_pcie->reset_names[i]); in kirin_pcie_gpio_request()
635 return -ENODEV; in kirin_pcie_gpio_request()
638 ret = devm_gpio_request(dev, kirin_pcie->gpio_id_reset[i], in kirin_pcie_gpio_request()
639 kirin_pcie->reset_names[i]); in kirin_pcie_gpio_request()
644 for (i = 0; i < kirin_pcie->n_gpio_clkreq; i++) { in kirin_pcie_gpio_request()
645 if (!gpio_is_valid(kirin_pcie->gpio_id_clkreq[i])) { in kirin_pcie_gpio_request()
647 kirin_pcie->clkreq_names[i]); in kirin_pcie_gpio_request()
648 return -ENODEV; in kirin_pcie_gpio_request()
651 ret = devm_gpio_request(dev, kirin_pcie->gpio_id_clkreq[i], in kirin_pcie_gpio_request()
652 kirin_pcie->clkreq_names[i]); in kirin_pcie_gpio_request()
656 ret = gpio_direction_output(kirin_pcie->gpio_id_clkreq[i], 0); in kirin_pcie_gpio_request()
679 if (kirin_pcie->type == PCIE_KIRIN_INTERNAL_PHY) in kirin_pcie_power_off()
682 for (i = 0; i < kirin_pcie->n_gpio_clkreq; i++) in kirin_pcie_power_off()
683 gpio_direction_output(kirin_pcie->gpio_id_clkreq[i], 1); in kirin_pcie_power_off()
685 phy_power_off(kirin_pcie->phy); in kirin_pcie_power_off()
686 phy_exit(kirin_pcie->phy); in kirin_pcie_power_off()
694 struct device *dev = &pdev->dev; in kirin_pcie_power_on()
697 if (kirin_pcie->type == PCIE_KIRIN_INTERNAL_PHY) { in kirin_pcie_power_on()
706 kirin_pcie->phy = devm_of_phy_get(dev, dev->of_node, NULL); in kirin_pcie_power_on()
707 if (IS_ERR(kirin_pcie->phy)) in kirin_pcie_power_on()
708 return PTR_ERR(kirin_pcie->phy); in kirin_pcie_power_on()
714 ret = phy_init(kirin_pcie->phy); in kirin_pcie_power_on()
718 ret = phy_power_on(kirin_pcie->phy); in kirin_pcie_power_on()
726 if (!gpio_request(kirin_pcie->gpio_id_dwc_perst, "pcie_perst_bridge")) { in kirin_pcie_power_on()
727 ret = gpio_direction_output(kirin_pcie->gpio_id_dwc_perst, 1); in kirin_pcie_power_on()
745 dw_pcie_host_deinit(&kirin_pcie->pci->pp); in kirin_pcie_remove()
763 { .compatible = "hisilicon,kirin960-pcie", .data = &kirin_960_data },
764 { .compatible = "hisilicon,kirin970-pcie", .data = &kirin_970_data },
770 struct device *dev = &pdev->dev; in kirin_pcie_probe()
776 if (!dev->of_node) { in kirin_pcie_probe()
778 return -EINVAL; in kirin_pcie_probe()
784 return -EINVAL; in kirin_pcie_probe()
789 return -ENOMEM; in kirin_pcie_probe()
793 return -ENOMEM; in kirin_pcie_probe()
795 pci->dev = dev; in kirin_pcie_probe()
796 pci->ops = &kirin_dw_pcie_ops; in kirin_pcie_probe()
797 pci->pp.ops = &kirin_pcie_host_ops; in kirin_pcie_probe()
798 kirin_pcie->pci = pci; in kirin_pcie_probe()
799 kirin_pcie->type = data->phy_type; in kirin_pcie_probe()
811 return dw_pcie_host_init(&pci->pp); in kirin_pcie_probe()
818 .name = "kirin-pcie",
826 MODULE_DESCRIPTION("PCIe host controller driver for Kirin Phone SoCs");