blob: 6d23683c0892f59646b4142242fa80bf0e85be98 [file] [log] [blame]
Niklas Cassela3cbfae2016-05-09 13:49:03 +02001/*
2 * PCIe host controller driver for Axis ARTPEC-6 SoC
3 *
Paul Gortmaker58bdaa12016-07-02 19:13:22 -04004 * Author: Niklas Cassel <niklas.cassel@axis.com>
5 *
Niklas Cassela3cbfae2016-05-09 13:49:03 +02006 * Based on work done by Phil Edworthy <phil@edworthys.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/delay.h>
14#include <linux/kernel.h>
Paul Gortmaker58bdaa12016-07-02 19:13:22 -040015#include <linux/init.h>
Niklas Cassela3cbfae2016-05-09 13:49:03 +020016#include <linux/pci.h>
17#include <linux/platform_device.h>
18#include <linux/resource.h>
19#include <linux/signal.h>
20#include <linux/types.h>
21#include <linux/interrupt.h>
22#include <linux/mfd/syscon.h>
23#include <linux/regmap.h>
24
25#include "pcie-designware.h"
26
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053027#define to_artpec6_pcie(x) dev_get_drvdata((x)->dev)
Niklas Cassela3cbfae2016-05-09 13:49:03 +020028
29struct artpec6_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053030 struct dw_pcie *pci;
Bjorn Helgaas7c62efc2016-10-06 13:30:56 -050031 struct regmap *regmap; /* DT axis,syscon-pcie */
32 void __iomem *phy_base; /* DT phy */
Niklas Cassela3cbfae2016-05-09 13:49:03 +020033};
34
35/* PCIe Port Logic registers (memory-mapped) */
36#define PL_OFFSET 0x700
37#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
38#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
39
40#define MISC_CONTROL_1_OFF (PL_OFFSET + 0x1bc)
41#define DBI_RO_WR_EN 1
42
43/* ARTPEC-6 specific registers */
44#define PCIECFG 0x18
45#define PCIECFG_DBG_OEN (1 << 24)
46#define PCIECFG_CORE_RESET_REQ (1 << 21)
47#define PCIECFG_LTSSM_ENABLE (1 << 20)
48#define PCIECFG_CLKREQ_B (1 << 11)
49#define PCIECFG_REFCLK_ENABLE (1 << 10)
50#define PCIECFG_PLL_ENABLE (1 << 9)
51#define PCIECFG_PCLK_ENABLE (1 << 8)
52#define PCIECFG_RISRCREN (1 << 4)
53#define PCIECFG_MODE_TX_DRV_EN (1 << 3)
54#define PCIECFG_CISRREN (1 << 2)
55#define PCIECFG_MACRO_ENABLE (1 << 0)
56
57#define NOCCFG 0x40
58#define NOCCFG_ENABLE_CLK_PCIE (1 << 4)
59#define NOCCFG_POWER_PCIE_IDLEACK (1 << 3)
60#define NOCCFG_POWER_PCIE_IDLE (1 << 2)
61#define NOCCFG_POWER_PCIE_IDLEREQ (1 << 1)
62
63#define PHY_STATUS 0x118
64#define PHY_COSPLLLOCK (1 << 0)
65
66#define ARTPEC6_CPU_TO_BUS_ADDR 0x0fffffff
67
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -050068static u32 artpec6_pcie_readl(struct artpec6_pcie *artpec6_pcie, u32 offset)
69{
70 u32 val;
71
72 regmap_read(artpec6_pcie->regmap, offset, &val);
73 return val;
74}
75
76static void artpec6_pcie_writel(struct artpec6_pcie *artpec6_pcie, u32 offset, u32 val)
77{
78 regmap_write(artpec6_pcie->regmap, offset, val);
79}
80
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -050081static int artpec6_pcie_establish_link(struct artpec6_pcie *artpec6_pcie)
Niklas Cassela3cbfae2016-05-09 13:49:03 +020082{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053083 struct dw_pcie *pci = artpec6_pcie->pci;
84 struct pcie_port *pp = &pci->pp;
Niklas Cassela3cbfae2016-05-09 13:49:03 +020085 u32 val;
86 unsigned int retries;
87
88 /* Hold DW core in reset */
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -050089 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +020090 val |= PCIECFG_CORE_RESET_REQ;
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -050091 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
Niklas Cassela3cbfae2016-05-09 13:49:03 +020092
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -050093 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +020094 val |= PCIECFG_RISRCREN | /* Receiver term. 50 Ohm */
95 PCIECFG_MODE_TX_DRV_EN |
96 PCIECFG_CISRREN | /* Reference clock term. 100 Ohm */
97 PCIECFG_MACRO_ENABLE;
98 val |= PCIECFG_REFCLK_ENABLE;
99 val &= ~PCIECFG_DBG_OEN;
100 val &= ~PCIECFG_CLKREQ_B;
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500101 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200102 usleep_range(5000, 6000);
103
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500104 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200105 val |= NOCCFG_ENABLE_CLK_PCIE;
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500106 artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200107 usleep_range(20, 30);
108
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500109 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200110 val |= PCIECFG_PCLK_ENABLE | PCIECFG_PLL_ENABLE;
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500111 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200112 usleep_range(6000, 7000);
113
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500114 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200115 val &= ~NOCCFG_POWER_PCIE_IDLEREQ;
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500116 artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200117
118 retries = 50;
119 do {
120 usleep_range(1000, 2000);
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500121 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200122 retries--;
123 } while (retries &&
124 (val & (NOCCFG_POWER_PCIE_IDLEACK | NOCCFG_POWER_PCIE_IDLE)));
125
126 retries = 50;
127 do {
128 usleep_range(1000, 2000);
129 val = readl(artpec6_pcie->phy_base + PHY_STATUS);
130 retries--;
131 } while (retries && !(val & PHY_COSPLLLOCK));
132
133 /* Take DW core out of reset */
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500134 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200135 val &= ~PCIECFG_CORE_RESET_REQ;
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500136 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200137 usleep_range(100, 200);
138
139 /*
140 * Enable writing to config regs. This is required as the Synopsys
141 * driver changes the class code. That register needs DBI write enable.
142 */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530143 dw_pcie_writel_dbi(pci, MISC_CONTROL_1_OFF, DBI_RO_WR_EN);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200144
145 pp->io_base &= ARTPEC6_CPU_TO_BUS_ADDR;
146 pp->mem_base &= ARTPEC6_CPU_TO_BUS_ADDR;
147 pp->cfg0_base &= ARTPEC6_CPU_TO_BUS_ADDR;
148 pp->cfg1_base &= ARTPEC6_CPU_TO_BUS_ADDR;
149
150 /* setup root complex */
151 dw_pcie_setup_rc(pp);
152
153 /* assert LTSSM enable */
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500154 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200155 val |= PCIECFG_LTSSM_ENABLE;
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500156 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200157
158 /* check if the link is up or not */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530159 if (!dw_pcie_wait_for_link(pci))
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200160 return 0;
161
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530162 dev_dbg(pci->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
163 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
164 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200165
166 return -ETIMEDOUT;
167}
168
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500169static void artpec6_pcie_enable_interrupts(struct artpec6_pcie *artpec6_pcie)
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200170{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530171 struct dw_pcie *pci = artpec6_pcie->pci;
172 struct pcie_port *pp = &pci->pp;
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500173
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200174 if (IS_ENABLED(CONFIG_PCI_MSI))
175 dw_pcie_msi_init(pp);
176}
177
178static void artpec6_pcie_host_init(struct pcie_port *pp)
179{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530180 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
181 struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500182
183 artpec6_pcie_establish_link(artpec6_pcie);
184 artpec6_pcie_enable_interrupts(artpec6_pcie);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200185}
186
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530187static struct dw_pcie_host_ops artpec6_pcie_host_ops = {
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200188 .host_init = artpec6_pcie_host_init,
189};
190
191static irqreturn_t artpec6_pcie_msi_handler(int irq, void *arg)
192{
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500193 struct artpec6_pcie *artpec6_pcie = arg;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530194 struct dw_pcie *pci = artpec6_pcie->pci;
195 struct pcie_port *pp = &pci->pp;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200196
197 return dw_handle_msi_irq(pp);
198}
199
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500200static int artpec6_add_pcie_port(struct artpec6_pcie *artpec6_pcie,
Niklas Casselb58ddf12016-09-09 09:45:30 +0200201 struct platform_device *pdev)
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200202{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530203 struct dw_pcie *pci = artpec6_pcie->pci;
204 struct pcie_port *pp = &pci->pp;
205 struct device *dev = pci->dev;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200206 int ret;
207
208 if (IS_ENABLED(CONFIG_PCI_MSI)) {
209 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
210 if (pp->msi_irq <= 0) {
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500211 dev_err(dev, "failed to get MSI irq\n");
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200212 return -ENODEV;
213 }
214
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500215 ret = devm_request_irq(dev, pp->msi_irq,
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200216 artpec6_pcie_msi_handler,
217 IRQF_SHARED | IRQF_NO_THREAD,
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500218 "artpec6-pcie-msi", artpec6_pcie);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200219 if (ret) {
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500220 dev_err(dev, "failed to request MSI irq\n");
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200221 return ret;
222 }
223 }
224
225 pp->root_bus_nr = -1;
226 pp->ops = &artpec6_pcie_host_ops;
227
228 ret = dw_pcie_host_init(pp);
229 if (ret) {
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500230 dev_err(dev, "failed to initialize host\n");
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200231 return ret;
232 }
233
234 return 0;
235}
236
Niklas Cassel794a8602017-04-03 17:35:12 -0500237static const struct dw_pcie_ops dw_pcie_ops = {
238};
239
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200240static int artpec6_pcie_probe(struct platform_device *pdev)
241{
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500242 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530243 struct dw_pcie *pci;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200244 struct artpec6_pcie *artpec6_pcie;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200245 struct resource *dbi_base;
246 struct resource *phy_base;
247 int ret;
248
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500249 artpec6_pcie = devm_kzalloc(dev, sizeof(*artpec6_pcie), GFP_KERNEL);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200250 if (!artpec6_pcie)
251 return -ENOMEM;
252
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530253 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
254 if (!pci)
255 return -ENOMEM;
256
257 pci->dev = dev;
Niklas Cassel794a8602017-04-03 17:35:12 -0500258 pci->ops = &dw_pcie_ops;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200259
Guenter Roeckc0464062017-02-25 02:08:12 -0800260 artpec6_pcie->pci = pci;
261
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200262 dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530263 pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
264 if (IS_ERR(pci->dbi_base))
265 return PTR_ERR(pci->dbi_base);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200266
267 phy_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500268 artpec6_pcie->phy_base = devm_ioremap_resource(dev, phy_base);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200269 if (IS_ERR(artpec6_pcie->phy_base))
270 return PTR_ERR(artpec6_pcie->phy_base);
271
272 artpec6_pcie->regmap =
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500273 syscon_regmap_lookup_by_phandle(dev->of_node,
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200274 "axis,syscon-pcie");
275 if (IS_ERR(artpec6_pcie->regmap))
276 return PTR_ERR(artpec6_pcie->regmap);
277
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530278 platform_set_drvdata(pdev, artpec6_pcie);
279
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500280 ret = artpec6_add_pcie_port(artpec6_pcie, pdev);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200281 if (ret < 0)
282 return ret;
283
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200284 return 0;
285}
286
287static const struct of_device_id artpec6_pcie_of_match[] = {
288 { .compatible = "axis,artpec6-pcie", },
289 {},
290};
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200291
292static struct platform_driver artpec6_pcie_driver = {
293 .probe = artpec6_pcie_probe,
294 .driver = {
295 .name = "artpec6-pcie",
296 .of_match_table = artpec6_pcie_of_match,
297 },
298};
Paul Gortmaker58bdaa12016-07-02 19:13:22 -0400299builtin_platform_driver(artpec6_pcie_driver);