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
7 * Authors: Ruqiang Ju <juruqiang@hisilicon.com>
8 * Jianguo Sun <sunjianguo1@huawei.com>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/of_gpio.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
;
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 pcie_port
*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 pcie_port
*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 pcie_port
*pp
, int where
,
128 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
131 histb_pcie_dbi_r_mode(pp
, true);
132 ret
= dw_pcie_read(pci
->dbi_base
+ where
, size
, val
);
133 histb_pcie_dbi_r_mode(pp
, false);
138 static int histb_pcie_wr_own_conf(struct pcie_port
*pp
, int where
,
141 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
144 histb_pcie_dbi_w_mode(pp
, true);
145 ret
= dw_pcie_write(pci
->dbi_base
+ where
, size
, val
);
146 histb_pcie_dbi_w_mode(pp
, false);
151 static int histb_pcie_link_up(struct dw_pcie
*pci
)
153 struct histb_pcie
*hipcie
= to_histb_pcie(pci
);
157 regval
= histb_pcie_readl(hipcie
, PCIE_SYS_STAT0
);
158 status
= histb_pcie_readl(hipcie
, PCIE_SYS_STAT4
);
159 status
&= PCIE_LTSSM_STATE_MASK
;
160 if ((regval
& PCIE_XMLH_LINK_UP
) && (regval
& PCIE_RDLH_LINK_UP
) &&
161 (status
== PCIE_LTSSM_STATE_ACTIVE
))
167 static int histb_pcie_establish_link(struct pcie_port
*pp
)
169 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
170 struct histb_pcie
*hipcie
= to_histb_pcie(pci
);
173 if (dw_pcie_link_up(pci
)) {
174 dev_info(pci
->dev
, "Link already up\n");
178 /* PCIe RC work mode */
179 regval
= histb_pcie_readl(hipcie
, PCIE_SYS_CTRL0
);
180 regval
&= ~PCIE_DEVICE_TYPE_MASK
;
181 regval
|= PCIE_WM_RC
;
182 histb_pcie_writel(hipcie
, PCIE_SYS_CTRL0
, regval
);
184 /* setup root complex */
185 dw_pcie_setup_rc(pp
);
187 /* assert LTSSM enable */
188 regval
= histb_pcie_readl(hipcie
, PCIE_SYS_CTRL7
);
189 regval
|= PCIE_APP_LTSSM_ENABLE
;
190 histb_pcie_writel(hipcie
, PCIE_SYS_CTRL7
, regval
);
192 return dw_pcie_wait_for_link(pci
);
195 static int histb_pcie_host_init(struct pcie_port
*pp
)
197 histb_pcie_establish_link(pp
);
199 if (IS_ENABLED(CONFIG_PCI_MSI
))
200 dw_pcie_msi_init(pp
);
205 static struct dw_pcie_host_ops histb_pcie_host_ops
= {
206 .rd_own_conf
= histb_pcie_rd_own_conf
,
207 .wr_own_conf
= histb_pcie_wr_own_conf
,
208 .host_init
= histb_pcie_host_init
,
211 static void histb_pcie_host_disable(struct histb_pcie
*hipcie
)
213 reset_control_assert(hipcie
->soft_reset
);
214 reset_control_assert(hipcie
->sys_reset
);
215 reset_control_assert(hipcie
->bus_reset
);
217 clk_disable_unprepare(hipcie
->aux_clk
);
218 clk_disable_unprepare(hipcie
->pipe_clk
);
219 clk_disable_unprepare(hipcie
->sys_clk
);
220 clk_disable_unprepare(hipcie
->bus_clk
);
222 if (gpio_is_valid(hipcie
->reset_gpio
))
223 gpio_set_value_cansleep(hipcie
->reset_gpio
, 0);
226 regulator_disable(hipcie
->vpcie
);
229 static int histb_pcie_host_enable(struct pcie_port
*pp
)
231 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
232 struct histb_pcie
*hipcie
= to_histb_pcie(pci
);
233 struct device
*dev
= pci
->dev
;
236 /* power on PCIe device if have */
238 ret
= regulator_enable(hipcie
->vpcie
);
240 dev_err(dev
, "failed to enable regulator: %d\n", ret
);
245 if (gpio_is_valid(hipcie
->reset_gpio
))
246 gpio_set_value_cansleep(hipcie
->reset_gpio
, 1);
248 ret
= clk_prepare_enable(hipcie
->bus_clk
);
250 dev_err(dev
, "cannot prepare/enable bus clk\n");
254 ret
= clk_prepare_enable(hipcie
->sys_clk
);
256 dev_err(dev
, "cannot prepare/enable sys clk\n");
260 ret
= clk_prepare_enable(hipcie
->pipe_clk
);
262 dev_err(dev
, "cannot prepare/enable pipe clk\n");
266 ret
= clk_prepare_enable(hipcie
->aux_clk
);
268 dev_err(dev
, "cannot prepare/enable aux clk\n");
272 reset_control_assert(hipcie
->soft_reset
);
273 reset_control_deassert(hipcie
->soft_reset
);
275 reset_control_assert(hipcie
->sys_reset
);
276 reset_control_deassert(hipcie
->sys_reset
);
278 reset_control_assert(hipcie
->bus_reset
);
279 reset_control_deassert(hipcie
->bus_reset
);
284 clk_disable_unprepare(hipcie
->pipe_clk
);
286 clk_disable_unprepare(hipcie
->sys_clk
);
288 clk_disable_unprepare(hipcie
->bus_clk
);
291 regulator_disable(hipcie
->vpcie
);
296 static const struct dw_pcie_ops dw_pcie_ops
= {
297 .read_dbi
= histb_pcie_read_dbi
,
298 .write_dbi
= histb_pcie_write_dbi
,
299 .link_up
= histb_pcie_link_up
,
302 static int histb_pcie_probe(struct platform_device
*pdev
)
304 struct histb_pcie
*hipcie
;
306 struct pcie_port
*pp
;
307 struct resource
*res
;
308 struct device_node
*np
= pdev
->dev
.of_node
;
309 struct device
*dev
= &pdev
->dev
;
310 enum of_gpio_flags of_flags
;
311 unsigned long flag
= GPIOF_DIR_OUT
;
314 hipcie
= devm_kzalloc(dev
, sizeof(*hipcie
), GFP_KERNEL
);
318 pci
= devm_kzalloc(dev
, sizeof(*pci
), GFP_KERNEL
);
325 pci
->ops
= &dw_pcie_ops
;
327 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "control");
328 hipcie
->ctrl
= devm_ioremap_resource(dev
, res
);
329 if (IS_ERR(hipcie
->ctrl
)) {
330 dev_err(dev
, "cannot get control reg base\n");
331 return PTR_ERR(hipcie
->ctrl
);
334 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "rc-dbi");
335 pci
->dbi_base
= devm_ioremap_resource(dev
, res
);
336 if (IS_ERR(pci
->dbi_base
)) {
337 dev_err(dev
, "cannot get rc-dbi base\n");
338 return PTR_ERR(pci
->dbi_base
);
341 hipcie
->vpcie
= devm_regulator_get_optional(dev
, "vpcie");
342 if (IS_ERR(hipcie
->vpcie
)) {
343 if (PTR_ERR(hipcie
->vpcie
) == -EPROBE_DEFER
)
344 return -EPROBE_DEFER
;
345 hipcie
->vpcie
= NULL
;
348 hipcie
->reset_gpio
= of_get_named_gpio_flags(np
,
349 "reset-gpios", 0, &of_flags
);
350 if (of_flags
& OF_GPIO_ACTIVE_LOW
)
351 flag
|= GPIOF_ACTIVE_LOW
;
352 if (gpio_is_valid(hipcie
->reset_gpio
)) {
353 ret
= devm_gpio_request_one(dev
, hipcie
->reset_gpio
,
354 flag
, "PCIe device power control");
356 dev_err(dev
, "unable to request gpio\n");
361 hipcie
->aux_clk
= devm_clk_get(dev
, "aux");
362 if (IS_ERR(hipcie
->aux_clk
)) {
363 dev_err(dev
, "Failed to get PCIe aux clk\n");
364 return PTR_ERR(hipcie
->aux_clk
);
367 hipcie
->pipe_clk
= devm_clk_get(dev
, "pipe");
368 if (IS_ERR(hipcie
->pipe_clk
)) {
369 dev_err(dev
, "Failed to get PCIe pipe clk\n");
370 return PTR_ERR(hipcie
->pipe_clk
);
373 hipcie
->sys_clk
= devm_clk_get(dev
, "sys");
374 if (IS_ERR(hipcie
->sys_clk
)) {
375 dev_err(dev
, "Failed to get PCIEe sys clk\n");
376 return PTR_ERR(hipcie
->sys_clk
);
379 hipcie
->bus_clk
= devm_clk_get(dev
, "bus");
380 if (IS_ERR(hipcie
->bus_clk
)) {
381 dev_err(dev
, "Failed to get PCIe bus clk\n");
382 return PTR_ERR(hipcie
->bus_clk
);
385 hipcie
->soft_reset
= devm_reset_control_get(dev
, "soft");
386 if (IS_ERR(hipcie
->soft_reset
)) {
387 dev_err(dev
, "couldn't get soft reset\n");
388 return PTR_ERR(hipcie
->soft_reset
);
391 hipcie
->sys_reset
= devm_reset_control_get(dev
, "sys");
392 if (IS_ERR(hipcie
->sys_reset
)) {
393 dev_err(dev
, "couldn't get sys reset\n");
394 return PTR_ERR(hipcie
->sys_reset
);
397 hipcie
->bus_reset
= devm_reset_control_get(dev
, "bus");
398 if (IS_ERR(hipcie
->bus_reset
)) {
399 dev_err(dev
, "couldn't get bus reset\n");
400 return PTR_ERR(hipcie
->bus_reset
);
403 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
404 pp
->msi_irq
= platform_get_irq_byname(pdev
, "msi");
405 if (pp
->msi_irq
< 0) {
406 dev_err(dev
, "Failed to get MSI IRQ\n");
411 hipcie
->phy
= devm_phy_get(dev
, "phy");
412 if (IS_ERR(hipcie
->phy
)) {
413 dev_info(dev
, "no pcie-phy found\n");
415 /* fall through here!
416 * if no pcie-phy found, phy init
417 * should be done under boot!
420 phy_init(hipcie
->phy
);
423 pp
->ops
= &histb_pcie_host_ops
;
425 platform_set_drvdata(pdev
, hipcie
);
427 ret
= histb_pcie_host_enable(pp
);
429 dev_err(dev
, "failed to enable host\n");
433 ret
= dw_pcie_host_init(pp
);
435 dev_err(dev
, "failed to initialize host\n");
442 static int histb_pcie_remove(struct platform_device
*pdev
)
444 struct histb_pcie
*hipcie
= platform_get_drvdata(pdev
);
446 histb_pcie_host_disable(hipcie
);
449 phy_exit(hipcie
->phy
);
454 static const struct of_device_id histb_pcie_of_match
[] = {
455 { .compatible
= "hisilicon,hi3798cv200-pcie", },
458 MODULE_DEVICE_TABLE(of
, histb_pcie_of_match
);
460 static struct platform_driver histb_pcie_platform_driver
= {
461 .probe
= histb_pcie_probe
,
462 .remove
= histb_pcie_remove
,
464 .name
= "histb-pcie",
465 .of_match_table
= histb_pcie_of_match
,
468 module_platform_driver(histb_pcie_platform_driver
);
470 MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");
471 MODULE_LICENSE("GPL v2");