blob: 56154c25980c6d83b1a155fc07e7520e8cee3ac1 [file] [log] [blame]
Zhou Wang500a1d92015-10-29 20:02:51 -05001/*
Gabriele Paoloni5930fe42015-11-27 01:17:05 +08002 * PCIe host controller driver for HiSilicon SoCs
Zhou Wang500a1d92015-10-29 20:02:51 -05003 *
4 * Copyright (C) 2015 HiSilicon Co., Ltd. http://www.hisilicon.com
5 *
Gabriele Paoloni5930fe42015-11-27 01:17:05 +08006 * Authors: Zhou Wang <wangzhou1@hisilicon.com>
7 * Dacai Zhu <zhudacai@hisilicon.com>
8 * Gabriele Paoloni <gabriele.paoloni@huawei.com>
Zhou Wang500a1d92015-10-29 20:02:51 -05009 *
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.
13 */
14#include <linux/interrupt.h>
Paul Gortmakerfb381182016-07-02 19:13:25 -040015#include <linux/init.h>
Zhou Wang500a1d92015-10-29 20:02:51 -050016#include <linux/mfd/syscon.h>
17#include <linux/of_address.h>
18#include <linux/of_pci.h>
19#include <linux/platform_device.h>
Gabriele Paoloni5930fe42015-11-27 01:17:05 +080020#include <linux/of_device.h>
Zhou Wang500a1d92015-10-29 20:02:51 -050021#include <linux/regmap.h>
22
23#include "pcie-designware.h"
24
Bjorn Helgaasa458ce32016-10-06 13:34:24 -050025#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
Zhou Wang500a1d92015-10-29 20:02:51 -050030
31#define to_hisi_pcie(x) container_of(x, struct hisi_pcie, pp)
32
Gabriele Paoloni5930fe42015-11-27 01:17:05 +080033struct hisi_pcie;
34
35struct pcie_soc_ops {
Bjorn Helgaasbf4ed372016-10-11 21:40:32 -050036 int (*hisi_pcie_link_up)(struct hisi_pcie *hisi_pcie);
Gabriele Paoloni5930fe42015-11-27 01:17:05 +080037};
38
Zhou Wang500a1d92015-10-29 20:02:51 -050039struct hisi_pcie {
Bjorn Helgaasf84cfdf2016-10-06 13:34:24 -050040 struct pcie_port pp; /* pp.dbi_base is DT rc_dbi */
Zhou Wang500a1d92015-10-29 20:02:51 -050041 struct regmap *subctrl;
Zhou Wang500a1d92015-10-29 20:02:51 -050042 u32 port_id;
Gabriele Paoloni5930fe42015-11-27 01:17:05 +080043 struct pcie_soc_ops *soc_ops;
Zhou Wang500a1d92015-10-29 20:02:51 -050044};
45
Gabriele Paoloni5930fe42015-11-27 01:17:05 +080046/* HipXX PCIe host only supports 32-bit config access */
Zhou Wang500a1d92015-10-29 20:02:51 -050047static int hisi_pcie_cfg_read(struct pcie_port *pp, int where, int size,
48 u32 *val)
49{
50 u32 reg;
51 u32 reg_val;
Zhou Wang500a1d92015-10-29 20:02:51 -050052 void *walker = &reg_val;
53
54 walker += (where & 0x3);
55 reg = where & ~0x3;
Bjorn Helgaas4368f092016-10-06 13:34:24 -050056 reg_val = dw_pcie_readl_rc(pp, reg);
Zhou Wang500a1d92015-10-29 20:02:51 -050057
58 if (size == 1)
59 *val = *(u8 __force *) walker;
60 else if (size == 2)
61 *val = *(u16 __force *) walker;
Dongdong Liu1dbe1622015-12-04 16:32:25 -060062 else if (size == 4)
63 *val = reg_val;
64 else
Zhou Wang500a1d92015-10-29 20:02:51 -050065 return PCIBIOS_BAD_REGISTER_NUMBER;
66
67 return PCIBIOS_SUCCESSFUL;
68}
69
Gabriele Paoloni5930fe42015-11-27 01:17:05 +080070/* HipXX PCIe host only supports 32-bit config access */
Zhou Wang500a1d92015-10-29 20:02:51 -050071static int hisi_pcie_cfg_write(struct pcie_port *pp, int where, int size,
72 u32 val)
73{
74 u32 reg_val;
75 u32 reg;
Zhou Wang500a1d92015-10-29 20:02:51 -050076 void *walker = &reg_val;
77
78 walker += (where & 0x3);
79 reg = where & ~0x3;
80 if (size == 4)
Bjorn Helgaas4368f092016-10-06 13:34:24 -050081 dw_pcie_writel_rc(pp, reg, val);
Zhou Wang500a1d92015-10-29 20:02:51 -050082 else if (size == 2) {
Bjorn Helgaas4368f092016-10-06 13:34:24 -050083 reg_val = dw_pcie_readl_rc(pp, reg);
Zhou Wang500a1d92015-10-29 20:02:51 -050084 *(u16 __force *) walker = val;
Bjorn Helgaas4368f092016-10-06 13:34:24 -050085 dw_pcie_writel_rc(pp, reg, reg_val);
Zhou Wang500a1d92015-10-29 20:02:51 -050086 } else if (size == 1) {
Bjorn Helgaas4368f092016-10-06 13:34:24 -050087 reg_val = dw_pcie_readl_rc(pp, reg);
Zhou Wang500a1d92015-10-29 20:02:51 -050088 *(u8 __force *) walker = val;
Bjorn Helgaas4368f092016-10-06 13:34:24 -050089 dw_pcie_writel_rc(pp, reg, reg_val);
Zhou Wang500a1d92015-10-29 20:02:51 -050090 } else
91 return PCIBIOS_BAD_REGISTER_NUMBER;
92
93 return PCIBIOS_SUCCESSFUL;
94}
95
Gabriele Paoloni5930fe42015-11-27 01:17:05 +080096static int hisi_pcie_link_up_hip05(struct hisi_pcie *hisi_pcie)
Zhou Wang500a1d92015-10-29 20:02:51 -050097{
98 u32 val;
Zhou Wang500a1d92015-10-29 20:02:51 -050099
100 regmap_read(hisi_pcie->subctrl, PCIE_SUBCTRL_SYS_STATE4_REG +
101 0x100 * hisi_pcie->port_id, &val);
102
103 return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
104}
105
Gabriele Paoloni5930fe42015-11-27 01:17:05 +0800106static int hisi_pcie_link_up_hip06(struct hisi_pcie *hisi_pcie)
107{
Bjorn Helgaas4368f092016-10-06 13:34:24 -0500108 struct pcie_port *pp = &hisi_pcie->pp;
Gabriele Paoloni5930fe42015-11-27 01:17:05 +0800109 u32 val;
110
Bjorn Helgaasa458ce32016-10-06 13:34:24 -0500111 val = dw_pcie_readl_rc(pp, PCIE_SYS_STATE4);
Gabriele Paoloni5930fe42015-11-27 01:17:05 +0800112
113 return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
114}
115
116static int hisi_pcie_link_up(struct pcie_port *pp)
117{
118 struct hisi_pcie *hisi_pcie = to_hisi_pcie(pp);
119
120 return hisi_pcie->soc_ops->hisi_pcie_link_up(hisi_pcie);
121}
122
Zhou Wang500a1d92015-10-29 20:02:51 -0500123static 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,
127};
128
Bjorn Helgaase9480b52016-10-06 13:34:23 -0500129static int hisi_add_pcie_port(struct hisi_pcie *hisi_pcie,
130 struct platform_device *pdev)
Zhou Wang500a1d92015-10-29 20:02:51 -0500131{
Bjorn Helgaase9480b52016-10-06 13:34:23 -0500132 struct pcie_port *pp = &hisi_pcie->pp;
Bjorn Helgaas88790f92016-10-06 13:34:25 -0500133 struct device *dev = pp->dev;
Zhou Wang500a1d92015-10-29 20:02:51 -0500134 int ret;
135 u32 port_id;
Zhou Wang500a1d92015-10-29 20:02:51 -0500136
Bjorn Helgaas88790f92016-10-06 13:34:25 -0500137 if (of_property_read_u32(dev->of_node, "port-id", &port_id)) {
138 dev_err(dev, "failed to read port-id\n");
Zhou Wang500a1d92015-10-29 20:02:51 -0500139 return -EINVAL;
140 }
141 if (port_id > 3) {
Bjorn Helgaas88790f92016-10-06 13:34:25 -0500142 dev_err(dev, "Invalid port-id: %d\n", port_id);
Zhou Wang500a1d92015-10-29 20:02:51 -0500143 return -EINVAL;
144 }
145 hisi_pcie->port_id = port_id;
146
147 pp->ops = &hisi_pcie_host_ops;
148
149 ret = dw_pcie_host_init(pp);
150 if (ret) {
Bjorn Helgaas88790f92016-10-06 13:34:25 -0500151 dev_err(dev, "failed to initialize host\n");
Zhou Wang500a1d92015-10-29 20:02:51 -0500152 return ret;
153 }
154
155 return 0;
156}
157
Arnd Bergmann9f55cf52015-11-24 15:38:07 -0600158static int hisi_pcie_probe(struct platform_device *pdev)
Zhou Wang500a1d92015-10-29 20:02:51 -0500159{
Bjorn Helgaas88790f92016-10-06 13:34:25 -0500160 struct device *dev = &pdev->dev;
Zhou Wang500a1d92015-10-29 20:02:51 -0500161 struct hisi_pcie *hisi_pcie;
162 struct pcie_port *pp;
Gabriele Paoloni5930fe42015-11-27 01:17:05 +0800163 const struct of_device_id *match;
Zhou Wang500a1d92015-10-29 20:02:51 -0500164 struct resource *reg;
Gabriele Paoloni5930fe42015-11-27 01:17:05 +0800165 struct device_driver *driver;
Zhou Wang500a1d92015-10-29 20:02:51 -0500166 int ret;
167
Bjorn Helgaas88790f92016-10-06 13:34:25 -0500168 hisi_pcie = devm_kzalloc(dev, sizeof(*hisi_pcie), GFP_KERNEL);
Zhou Wang500a1d92015-10-29 20:02:51 -0500169 if (!hisi_pcie)
170 return -ENOMEM;
171
172 pp = &hisi_pcie->pp;
Bjorn Helgaas88790f92016-10-06 13:34:25 -0500173 pp->dev = dev;
174 driver = dev->driver;
Gabriele Paoloni5930fe42015-11-27 01:17:05 +0800175
Bjorn Helgaas88790f92016-10-06 13:34:25 -0500176 match = of_match_device(driver->of_match_table, dev);
Gabriele Paoloni5930fe42015-11-27 01:17:05 +0800177 hisi_pcie->soc_ops = (struct pcie_soc_ops *) match->data;
Zhou Wang500a1d92015-10-29 20:02:51 -0500178
179 hisi_pcie->subctrl =
180 syscon_regmap_lookup_by_compatible("hisilicon,pcie-sas-subctrl");
181 if (IS_ERR(hisi_pcie->subctrl)) {
Bjorn Helgaas88790f92016-10-06 13:34:25 -0500182 dev_err(dev, "cannot get subctrl base\n");
Zhou Wang500a1d92015-10-29 20:02:51 -0500183 return PTR_ERR(hisi_pcie->subctrl);
184 }
185
186 reg = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbi");
Bjorn Helgaas761c43c2016-10-06 13:34:23 -0500187 pp->dbi_base = devm_ioremap_resource(dev, reg);
188 if (IS_ERR(pp->dbi_base)) {
Bjorn Helgaas88790f92016-10-06 13:34:25 -0500189 dev_err(dev, "cannot get rc_dbi base\n");
Bjorn Helgaas761c43c2016-10-06 13:34:23 -0500190 return PTR_ERR(pp->dbi_base);
Zhou Wang500a1d92015-10-29 20:02:51 -0500191 }
192
Bjorn Helgaase9480b52016-10-06 13:34:23 -0500193 ret = hisi_add_pcie_port(hisi_pcie, pdev);
Zhou Wang500a1d92015-10-29 20:02:51 -0500194 if (ret)
195 return ret;
196
Bjorn Helgaas88790f92016-10-06 13:34:25 -0500197 dev_warn(dev, "only 32-bit config accesses supported; smaller writes may corrupt adjacent RW1C fields\n");
Zhou Wang500a1d92015-10-29 20:02:51 -0500198
199 return 0;
200}
201
Gabriele Paoloni5930fe42015-11-27 01:17:05 +0800202static struct pcie_soc_ops hip05_ops = {
203 &hisi_pcie_link_up_hip05
204};
205
206static struct pcie_soc_ops hip06_ops = {
207 &hisi_pcie_link_up_hip06
208};
209
Zhou Wang500a1d92015-10-29 20:02:51 -0500210static const struct of_device_id hisi_pcie_of_match[] = {
Gabriele Paoloni5930fe42015-11-27 01:17:05 +0800211 {
212 .compatible = "hisilicon,hip05-pcie",
213 .data = (void *) &hip05_ops,
214 },
215 {
216 .compatible = "hisilicon,hip06-pcie",
217 .data = (void *) &hip06_ops,
218 },
Zhou Wang500a1d92015-10-29 20:02:51 -0500219 {},
220};
221
Zhou Wang500a1d92015-10-29 20:02:51 -0500222static struct platform_driver hisi_pcie_driver = {
223 .probe = hisi_pcie_probe,
224 .driver = {
225 .name = "hisi-pcie",
226 .of_match_table = hisi_pcie_of_match,
227 },
228};
Paul Gortmakerfb381182016-07-02 19:13:25 -0400229builtin_platform_driver(hisi_pcie_driver);