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/init.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_SUBCTRL_SYS_STATE4_REG 0x6818
26 #define PCIE_HIP06_CTRL_OFF 0x1000
27 #define PCIE_SYS_STATE4 (PCIE_HIP06_CTRL_OFF + 0x31c)
28 #define PCIE_LTSSM_LINKUP_STATE 0x11
29 #define PCIE_LTSSM_STATE_MASK 0x3F
31 #define to_hisi_pcie(x) container_of(x, struct hisi_pcie, pp)
36 int (*hisi_pcie_link_up)(struct hisi_pcie *hisi_pcie);
40 struct pcie_port pp; /* pp.dbi_base is DT rc_dbi */
41 struct regmap *subctrl;
43 struct pcie_soc_ops *soc_ops;
46 /* HipXX PCIe host only supports 32-bit config access */
47 static int hisi_pcie_cfg_read(struct pcie_port *pp, int where, int size,
52 void *walker = ®_val;
54 walker += (where & 0x3);
56 reg_val = dw_pcie_readl_rc(pp, reg);
59 *val = *(u8 __force *) walker;
61 *val = *(u16 __force *) walker;
65 return PCIBIOS_BAD_REGISTER_NUMBER;
67 return PCIBIOS_SUCCESSFUL;
70 /* HipXX PCIe host only supports 32-bit config access */
71 static int hisi_pcie_cfg_write(struct pcie_port *pp, int where, int size,
76 void *walker = ®_val;
78 walker += (where & 0x3);
81 dw_pcie_writel_rc(pp, reg, val);
83 reg_val = dw_pcie_readl_rc(pp, reg);
84 *(u16 __force *) walker = val;
85 dw_pcie_writel_rc(pp, reg, reg_val);
86 } else if (size == 1) {
87 reg_val = dw_pcie_readl_rc(pp, reg);
88 *(u8 __force *) walker = val;
89 dw_pcie_writel_rc(pp, reg, reg_val);
91 return PCIBIOS_BAD_REGISTER_NUMBER;
93 return PCIBIOS_SUCCESSFUL;
96 static int hisi_pcie_link_up_hip05(struct hisi_pcie *hisi_pcie)
100 regmap_read(hisi_pcie->subctrl, PCIE_SUBCTRL_SYS_STATE4_REG +
101 0x100 * hisi_pcie->port_id, &val);
103 return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
106 static int hisi_pcie_link_up_hip06(struct hisi_pcie *hisi_pcie)
108 struct pcie_port *pp = &hisi_pcie->pp;
111 val = dw_pcie_readl_rc(pp, PCIE_SYS_STATE4);
113 return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
116 static int hisi_pcie_link_up(struct pcie_port *pp)
118 struct hisi_pcie *hisi_pcie = to_hisi_pcie(pp);
120 return hisi_pcie->soc_ops->hisi_pcie_link_up(hisi_pcie);
123 static struct pcie_host_ops hisi_pcie_host_ops = {
124 .rd_own_conf = hisi_pcie_cfg_read,
125 .wr_own_conf = hisi_pcie_cfg_write,
126 .link_up = hisi_pcie_link_up,
129 static int hisi_add_pcie_port(struct hisi_pcie *hisi_pcie,
130 struct platform_device *pdev)
132 struct pcie_port *pp = &hisi_pcie->pp;
133 struct device *dev = pp->dev;
137 if (of_property_read_u32(dev->of_node, "port-id", &port_id)) {
138 dev_err(dev, "failed to read port-id\n");
142 dev_err(dev, "Invalid port-id: %d\n", port_id);
145 hisi_pcie->port_id = port_id;
147 pp->ops = &hisi_pcie_host_ops;
149 ret = dw_pcie_host_init(pp);
151 dev_err(dev, "failed to initialize host\n");
158 static int hisi_pcie_probe(struct platform_device *pdev)
160 struct device *dev = &pdev->dev;
161 struct hisi_pcie *hisi_pcie;
162 struct pcie_port *pp;
163 const struct of_device_id *match;
164 struct resource *reg;
165 struct device_driver *driver;
168 hisi_pcie = devm_kzalloc(dev, sizeof(*hisi_pcie), GFP_KERNEL);
174 driver = dev->driver;
176 match = of_match_device(driver->of_match_table, dev);
177 hisi_pcie->soc_ops = (struct pcie_soc_ops *) match->data;
180 syscon_regmap_lookup_by_compatible("hisilicon,pcie-sas-subctrl");
181 if (IS_ERR(hisi_pcie->subctrl)) {
182 dev_err(dev, "cannot get subctrl base\n");
183 return PTR_ERR(hisi_pcie->subctrl);
186 reg = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbi");
187 pp->dbi_base = devm_ioremap_resource(dev, reg);
188 if (IS_ERR(pp->dbi_base)) {
189 dev_err(dev, "cannot get rc_dbi base\n");
190 return PTR_ERR(pp->dbi_base);
193 ret = hisi_add_pcie_port(hisi_pcie, pdev);
197 dev_warn(dev, "only 32-bit config accesses supported; smaller writes may corrupt adjacent RW1C fields\n");
202 static struct pcie_soc_ops hip05_ops = {
203 &hisi_pcie_link_up_hip05
206 static struct pcie_soc_ops hip06_ops = {
207 &hisi_pcie_link_up_hip06
210 static const struct of_device_id hisi_pcie_of_match[] = {
212 .compatible = "hisilicon,hip05-pcie",
213 .data = (void *) &hip05_ops,
216 .compatible = "hisilicon,hip06-pcie",
217 .data = (void *) &hip06_ops,
222 static struct platform_driver hisi_pcie_driver = {
223 .probe = hisi_pcie_probe,
226 .of_match_table = hisi_pcie_of_match,
229 builtin_platform_driver(hisi_pcie_driver);