1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for HiSilicon STB SoCs
5 * Copyright (C) 2016-2017 HiSilicon Co., Ltd. http://www.hisilicon.com
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/pci.h>
19 #include <linux/phy/phy.h>
20 #include <linux/platform_device.h>
21 #include <linux/resource.h>
22 #include <linux/reset.h>
24 #include "pcie-designware.h"
26 #define to_histb_pcie(x) dev_get_drvdata((x)->dev)
28 #define PCIE_SYS_CTRL0 0x0000
29 #define PCIE_SYS_CTRL1 0x0004
30 #define PCIE_SYS_CTRL7 0x001C
31 #define PCIE_SYS_CTRL13 0x0034
32 #define PCIE_SYS_CTRL15 0x003C
33 #define PCIE_SYS_CTRL16 0x0040
34 #define PCIE_SYS_CTRL17 0x0044
36 #define PCIE_SYS_STAT0 0x0100
37 #define PCIE_SYS_STAT4 0x0110
39 #define PCIE_RDLH_LINK_UP BIT(5)
40 #define PCIE_XMLH_LINK_UP BIT(15)
41 #define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
42 #define PCIE_APP_LTSSM_ENABLE BIT(11)
44 #define PCIE_DEVICE_TYPE_MASK GENMASK(31, 28)
46 #define PCIE_WM_LEGACY BIT(1)
47 #define PCIE_WM_RC BIT(30)
49 #define PCIE_LTSSM_STATE_MASK GENMASK(5, 0)
50 #define PCIE_LTSSM_STATE_ACTIVE 0x11
59 struct reset_control *soft_reset;
60 struct reset_control *sys_reset;
61 struct reset_control *bus_reset;
63 struct gpio_desc *reset_gpio;
64 struct regulator *vpcie;
67 static u32 histb_pcie_readl(struct histb_pcie *histb_pcie, u32 reg)
69 return readl(histb_pcie->ctrl + reg);
72 static void histb_pcie_writel(struct histb_pcie *histb_pcie, u32 reg, u32 val)
74 writel(val, histb_pcie->ctrl + reg);
77 static void histb_pcie_dbi_w_mode(struct dw_pcie_rp *pp, bool enable)
79 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
80 struct histb_pcie *hipcie = to_histb_pcie(pci);
83 val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
85 val |= PCIE_ELBI_SLV_DBI_ENABLE;
87 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
88 histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, val);
91 static void histb_pcie_dbi_r_mode(struct dw_pcie_rp *pp, bool enable)
93 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
94 struct histb_pcie *hipcie = to_histb_pcie(pci);
97 val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL1);
99 val |= PCIE_ELBI_SLV_DBI_ENABLE;
101 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
102 histb_pcie_writel(hipcie, PCIE_SYS_CTRL1, val);
105 static u32 histb_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
106 u32 reg, size_t size)
110 histb_pcie_dbi_r_mode(&pci->pp, true);
111 dw_pcie_read(base + reg, size, &val);
112 histb_pcie_dbi_r_mode(&pci->pp, false);
117 static void histb_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
118 u32 reg, size_t size, u32 val)
120 histb_pcie_dbi_w_mode(&pci->pp, true);
121 dw_pcie_write(base + reg, size, val);
122 histb_pcie_dbi_w_mode(&pci->pp, false);
125 static int histb_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
126 int where, int size, u32 *val)
128 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
131 return PCIBIOS_DEVICE_NOT_FOUND;
133 *val = dw_pcie_read_dbi(pci, where, size);
134 return PCIBIOS_SUCCESSFUL;
137 static int histb_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
138 int where, int size, u32 val)
140 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
143 return PCIBIOS_DEVICE_NOT_FOUND;
145 dw_pcie_write_dbi(pci, where, size, val);
146 return PCIBIOS_SUCCESSFUL;
149 static struct pci_ops histb_pci_ops = {
150 .read = histb_pcie_rd_own_conf,
151 .write = histb_pcie_wr_own_conf,
154 static int histb_pcie_link_up(struct dw_pcie *pci)
156 struct histb_pcie *hipcie = to_histb_pcie(pci);
160 regval = histb_pcie_readl(hipcie, PCIE_SYS_STAT0);
161 status = histb_pcie_readl(hipcie, PCIE_SYS_STAT4);
162 status &= PCIE_LTSSM_STATE_MASK;
163 if ((regval & PCIE_XMLH_LINK_UP) && (regval & PCIE_RDLH_LINK_UP) &&
164 (status == PCIE_LTSSM_STATE_ACTIVE))
170 static int histb_pcie_start_link(struct dw_pcie *pci)
172 struct histb_pcie *hipcie = to_histb_pcie(pci);
175 /* assert LTSSM enable */
176 regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL7);
177 regval |= PCIE_APP_LTSSM_ENABLE;
178 histb_pcie_writel(hipcie, PCIE_SYS_CTRL7, regval);
183 static int histb_pcie_host_init(struct dw_pcie_rp *pp)
185 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
186 struct histb_pcie *hipcie = to_histb_pcie(pci);
189 pp->bridge->ops = &histb_pci_ops;
191 /* PCIe RC work mode */
192 regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
193 regval &= ~PCIE_DEVICE_TYPE_MASK;
194 regval |= PCIE_WM_RC;
195 histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, regval);
200 static const struct dw_pcie_host_ops histb_pcie_host_ops = {
201 .host_init = histb_pcie_host_init,
204 static void histb_pcie_host_disable(struct histb_pcie *hipcie)
206 reset_control_assert(hipcie->soft_reset);
207 reset_control_assert(hipcie->sys_reset);
208 reset_control_assert(hipcie->bus_reset);
210 clk_disable_unprepare(hipcie->aux_clk);
211 clk_disable_unprepare(hipcie->pipe_clk);
212 clk_disable_unprepare(hipcie->sys_clk);
213 clk_disable_unprepare(hipcie->bus_clk);
215 if (hipcie->reset_gpio)
216 gpiod_set_value_cansleep(hipcie->reset_gpio, 1);
219 regulator_disable(hipcie->vpcie);
222 static int histb_pcie_host_enable(struct dw_pcie_rp *pp)
224 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
225 struct histb_pcie *hipcie = to_histb_pcie(pci);
226 struct device *dev = pci->dev;
229 /* power on PCIe device if have */
231 ret = regulator_enable(hipcie->vpcie);
233 dev_err(dev, "failed to enable regulator: %d\n", ret);
238 if (hipcie->reset_gpio)
239 gpiod_set_value_cansleep(hipcie->reset_gpio, 0);
241 ret = clk_prepare_enable(hipcie->bus_clk);
243 dev_err(dev, "cannot prepare/enable bus clk\n");
247 ret = clk_prepare_enable(hipcie->sys_clk);
249 dev_err(dev, "cannot prepare/enable sys clk\n");
253 ret = clk_prepare_enable(hipcie->pipe_clk);
255 dev_err(dev, "cannot prepare/enable pipe clk\n");
259 ret = clk_prepare_enable(hipcie->aux_clk);
261 dev_err(dev, "cannot prepare/enable aux clk\n");
265 reset_control_assert(hipcie->soft_reset);
266 reset_control_deassert(hipcie->soft_reset);
268 reset_control_assert(hipcie->sys_reset);
269 reset_control_deassert(hipcie->sys_reset);
271 reset_control_assert(hipcie->bus_reset);
272 reset_control_deassert(hipcie->bus_reset);
277 clk_disable_unprepare(hipcie->pipe_clk);
279 clk_disable_unprepare(hipcie->sys_clk);
281 clk_disable_unprepare(hipcie->bus_clk);
284 regulator_disable(hipcie->vpcie);
289 static const struct dw_pcie_ops dw_pcie_ops = {
290 .read_dbi = histb_pcie_read_dbi,
291 .write_dbi = histb_pcie_write_dbi,
292 .link_up = histb_pcie_link_up,
293 .start_link = histb_pcie_start_link,
296 static int histb_pcie_probe(struct platform_device *pdev)
298 struct histb_pcie *hipcie;
300 struct dw_pcie_rp *pp;
301 struct device *dev = &pdev->dev;
304 hipcie = devm_kzalloc(dev, sizeof(*hipcie), GFP_KERNEL);
308 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
315 pci->ops = &dw_pcie_ops;
317 hipcie->ctrl = devm_platform_ioremap_resource_byname(pdev, "control");
318 if (IS_ERR(hipcie->ctrl)) {
319 dev_err(dev, "cannot get control reg base\n");
320 return PTR_ERR(hipcie->ctrl);
323 pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "rc-dbi");
324 if (IS_ERR(pci->dbi_base)) {
325 dev_err(dev, "cannot get rc-dbi base\n");
326 return PTR_ERR(pci->dbi_base);
329 hipcie->vpcie = devm_regulator_get_optional(dev, "vpcie");
330 if (IS_ERR(hipcie->vpcie)) {
331 if (PTR_ERR(hipcie->vpcie) != -ENODEV)
332 return PTR_ERR(hipcie->vpcie);
333 hipcie->vpcie = NULL;
336 hipcie->reset_gpio = devm_gpiod_get_optional(dev, "reset",
338 ret = PTR_ERR_OR_ZERO(hipcie->reset_gpio);
340 dev_err(dev, "unable to request reset gpio: %d\n", ret);
344 ret = gpiod_set_consumer_name(hipcie->reset_gpio,
345 "PCIe device power control");
347 dev_err(dev, "unable to set reset gpio name: %d\n", ret);
351 hipcie->aux_clk = devm_clk_get(dev, "aux");
352 if (IS_ERR(hipcie->aux_clk)) {
353 dev_err(dev, "Failed to get PCIe aux clk\n");
354 return PTR_ERR(hipcie->aux_clk);
357 hipcie->pipe_clk = devm_clk_get(dev, "pipe");
358 if (IS_ERR(hipcie->pipe_clk)) {
359 dev_err(dev, "Failed to get PCIe pipe clk\n");
360 return PTR_ERR(hipcie->pipe_clk);
363 hipcie->sys_clk = devm_clk_get(dev, "sys");
364 if (IS_ERR(hipcie->sys_clk)) {
365 dev_err(dev, "Failed to get PCIEe sys clk\n");
366 return PTR_ERR(hipcie->sys_clk);
369 hipcie->bus_clk = devm_clk_get(dev, "bus");
370 if (IS_ERR(hipcie->bus_clk)) {
371 dev_err(dev, "Failed to get PCIe bus clk\n");
372 return PTR_ERR(hipcie->bus_clk);
375 hipcie->soft_reset = devm_reset_control_get(dev, "soft");
376 if (IS_ERR(hipcie->soft_reset)) {
377 dev_err(dev, "couldn't get soft reset\n");
378 return PTR_ERR(hipcie->soft_reset);
381 hipcie->sys_reset = devm_reset_control_get(dev, "sys");
382 if (IS_ERR(hipcie->sys_reset)) {
383 dev_err(dev, "couldn't get sys reset\n");
384 return PTR_ERR(hipcie->sys_reset);
387 hipcie->bus_reset = devm_reset_control_get(dev, "bus");
388 if (IS_ERR(hipcie->bus_reset)) {
389 dev_err(dev, "couldn't get bus reset\n");
390 return PTR_ERR(hipcie->bus_reset);
393 hipcie->phy = devm_phy_get(dev, "phy");
394 if (IS_ERR(hipcie->phy)) {
395 dev_info(dev, "no pcie-phy found\n");
397 /* fall through here!
398 * if no pcie-phy found, phy init
399 * should be done under boot!
402 phy_init(hipcie->phy);
405 pp->ops = &histb_pcie_host_ops;
407 platform_set_drvdata(pdev, hipcie);
409 ret = histb_pcie_host_enable(pp);
411 dev_err(dev, "failed to enable host\n");
415 ret = dw_pcie_host_init(pp);
417 dev_err(dev, "failed to initialize host\n");
424 static int histb_pcie_remove(struct platform_device *pdev)
426 struct histb_pcie *hipcie = platform_get_drvdata(pdev);
428 histb_pcie_host_disable(hipcie);
431 phy_exit(hipcie->phy);
436 static const struct of_device_id histb_pcie_of_match[] = {
437 { .compatible = "hisilicon,hi3798cv200-pcie", },
440 MODULE_DEVICE_TABLE(of, histb_pcie_of_match);
442 static struct platform_driver histb_pcie_platform_driver = {
443 .probe = histb_pcie_probe,
444 .remove = histb_pcie_remove,
446 .name = "histb-pcie",
447 .of_match_table = histb_pcie_of_match,
450 module_platform_driver(histb_pcie_platform_driver);
452 MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");
453 MODULE_LICENSE("GPL v2");