2 * PCIe host controller driver for HiSilicon SoCs
4 * Copyright (C) 2015 HiSilicon Co., Ltd. http://www.hisilicon.com
6 * Authors: Zhou Wang <wangzhou1@hisilicon.com>
7 * Dacai Zhu <zhudacai@hisilicon.com>
8 * Gabriele Paoloni <gabriele.paoloni@huawei.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/of_address.h>
18 #include <linux/of_pci.h>
19 #include <linux/platform_device.h>
20 #include <linux/of_device.h>
21 #include <linux/regmap.h>
23 #include "pcie-designware.h"
25 #define PCIE_LTSSM_LINKUP_STATE 0x11
26 #define PCIE_LTSSM_STATE_MASK 0x3F
27 #define PCIE_SUBCTRL_SYS_STATE4_REG 0x6818
28 #define PCIE_SYS_STATE4 0x31c
29 #define PCIE_HIP06_CTRL_OFF 0x1000
31 #define to_hisi_pcie(x) container_of(x, struct hisi_pcie, pp)
36 int (*hisi_pcie_link_up
)(struct hisi_pcie
*pcie
);
40 struct regmap
*subctrl
;
41 void __iomem
*reg_base
;
44 struct pcie_soc_ops
*soc_ops
;
47 static inline void hisi_pcie_apb_writel(struct hisi_pcie
*pcie
,
50 writel(val
, pcie
->reg_base
+ reg
);
53 static inline u32
hisi_pcie_apb_readl(struct hisi_pcie
*pcie
, u32 reg
)
55 return readl(pcie
->reg_base
+ reg
);
58 /* HipXX PCIe host only supports 32-bit config access */
59 static int hisi_pcie_cfg_read(struct pcie_port
*pp
, int where
, int size
,
64 struct hisi_pcie
*pcie
= to_hisi_pcie(pp
);
65 void *walker
= ®_val
;
67 walker
+= (where
& 0x3);
69 reg_val
= hisi_pcie_apb_readl(pcie
, reg
);
72 *val
= *(u8 __force
*) walker
;
74 *val
= *(u16 __force
*) walker
;
78 return PCIBIOS_BAD_REGISTER_NUMBER
;
80 return PCIBIOS_SUCCESSFUL
;
83 /* HipXX PCIe host only supports 32-bit config access */
84 static int hisi_pcie_cfg_write(struct pcie_port
*pp
, int where
, int size
,
89 struct hisi_pcie
*pcie
= to_hisi_pcie(pp
);
90 void *walker
= ®_val
;
92 walker
+= (where
& 0x3);
95 hisi_pcie_apb_writel(pcie
, val
, reg
);
97 reg_val
= hisi_pcie_apb_readl(pcie
, reg
);
98 *(u16 __force
*) walker
= val
;
99 hisi_pcie_apb_writel(pcie
, reg_val
, reg
);
100 } else if (size
== 1) {
101 reg_val
= hisi_pcie_apb_readl(pcie
, reg
);
102 *(u8 __force
*) walker
= val
;
103 hisi_pcie_apb_writel(pcie
, reg_val
, reg
);
105 return PCIBIOS_BAD_REGISTER_NUMBER
;
107 return PCIBIOS_SUCCESSFUL
;
110 static int hisi_pcie_link_up_hip05(struct hisi_pcie
*hisi_pcie
)
114 regmap_read(hisi_pcie
->subctrl
, PCIE_SUBCTRL_SYS_STATE4_REG
+
115 0x100 * hisi_pcie
->port_id
, &val
);
117 return ((val
& PCIE_LTSSM_STATE_MASK
) == PCIE_LTSSM_LINKUP_STATE
);
120 static int hisi_pcie_link_up_hip06(struct hisi_pcie
*hisi_pcie
)
124 val
= hisi_pcie_apb_readl(hisi_pcie
, PCIE_HIP06_CTRL_OFF
+
127 return ((val
& PCIE_LTSSM_STATE_MASK
) == PCIE_LTSSM_LINKUP_STATE
);
130 static int hisi_pcie_link_up(struct pcie_port
*pp
)
132 struct hisi_pcie
*hisi_pcie
= to_hisi_pcie(pp
);
134 return hisi_pcie
->soc_ops
->hisi_pcie_link_up(hisi_pcie
);
137 static struct pcie_host_ops hisi_pcie_host_ops
= {
138 .rd_own_conf
= hisi_pcie_cfg_read
,
139 .wr_own_conf
= hisi_pcie_cfg_write
,
140 .link_up
= hisi_pcie_link_up
,
143 static int hisi_add_pcie_port(struct pcie_port
*pp
,
144 struct platform_device
*pdev
)
148 struct hisi_pcie
*hisi_pcie
= to_hisi_pcie(pp
);
150 if (of_property_read_u32(pdev
->dev
.of_node
, "port-id", &port_id
)) {
151 dev_err(&pdev
->dev
, "failed to read port-id\n");
155 dev_err(&pdev
->dev
, "Invalid port-id: %d\n", port_id
);
158 hisi_pcie
->port_id
= port_id
;
160 pp
->ops
= &hisi_pcie_host_ops
;
162 ret
= dw_pcie_host_init(pp
);
164 dev_err(&pdev
->dev
, "failed to initialize host\n");
171 static int hisi_pcie_probe(struct platform_device
*pdev
)
173 struct hisi_pcie
*hisi_pcie
;
174 struct pcie_port
*pp
;
175 const struct of_device_id
*match
;
176 struct resource
*reg
;
177 struct device_driver
*driver
;
180 hisi_pcie
= devm_kzalloc(&pdev
->dev
, sizeof(*hisi_pcie
), GFP_KERNEL
);
185 pp
->dev
= &pdev
->dev
;
186 driver
= (pdev
->dev
).driver
;
188 match
= of_match_device(driver
->of_match_table
, &pdev
->dev
);
189 hisi_pcie
->soc_ops
= (struct pcie_soc_ops
*) match
->data
;
192 syscon_regmap_lookup_by_compatible("hisilicon,pcie-sas-subctrl");
193 if (IS_ERR(hisi_pcie
->subctrl
)) {
194 dev_err(pp
->dev
, "cannot get subctrl base\n");
195 return PTR_ERR(hisi_pcie
->subctrl
);
198 reg
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "rc_dbi");
199 hisi_pcie
->reg_base
= devm_ioremap_resource(&pdev
->dev
, reg
);
200 if (IS_ERR(hisi_pcie
->reg_base
)) {
201 dev_err(pp
->dev
, "cannot get rc_dbi base\n");
202 return PTR_ERR(hisi_pcie
->reg_base
);
205 hisi_pcie
->pp
.dbi_base
= hisi_pcie
->reg_base
;
207 ret
= hisi_add_pcie_port(pp
, pdev
);
211 platform_set_drvdata(pdev
, hisi_pcie
);
213 dev_warn(pp
->dev
, "only 32-bit config accesses supported; smaller writes may corrupt adjacent RW1C fields\n");
218 static struct pcie_soc_ops hip05_ops
= {
219 &hisi_pcie_link_up_hip05
222 static struct pcie_soc_ops hip06_ops
= {
223 &hisi_pcie_link_up_hip06
226 static const struct of_device_id hisi_pcie_of_match
[] = {
228 .compatible
= "hisilicon,hip05-pcie",
229 .data
= (void *) &hip05_ops
,
232 .compatible
= "hisilicon,hip06-pcie",
233 .data
= (void *) &hip06_ops
,
239 MODULE_DEVICE_TABLE(of
, hisi_pcie_of_match
);
241 static struct platform_driver hisi_pcie_driver
= {
242 .probe
= hisi_pcie_probe
,
245 .of_match_table
= hisi_pcie_of_match
,
249 module_platform_driver(hisi_pcie_driver
);
251 MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
252 MODULE_AUTHOR("Dacai Zhu <zhudacai@hisilicon.com>");
253 MODULE_AUTHOR("Gabriele Paoloni <gabriele.paoloni@huawei.com>");
254 MODULE_LICENSE("GPL v2");