blob: 321b56cfd5d0a667a5302d88f8c28af9569a4cd1 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Niklas Cassela3cbfae2016-05-09 13:49:03 +02002/*
3 * PCIe host controller driver for Axis ARTPEC-6 SoC
4 *
Paul Gortmaker58bdaa12016-07-02 19:13:22 -04005 * Author: Niklas Cassel <niklas.cassel@axis.com>
6 *
Niklas Cassela3cbfae2016-05-09 13:49:03 +02007 * Based on work done by Phil Edworthy <phil@edworthys.org>
Niklas Cassela3cbfae2016-05-09 13:49:03 +02008 */
9
10#include <linux/delay.h>
11#include <linux/kernel.h>
Paul Gortmaker58bdaa12016-07-02 19:13:22 -040012#include <linux/init.h>
Niklas Casselb5074ef2017-12-20 00:29:35 +010013#include <linux/of_device.h>
Niklas Cassela3cbfae2016-05-09 13:49:03 +020014#include <linux/pci.h>
15#include <linux/platform_device.h>
16#include <linux/resource.h>
17#include <linux/signal.h>
18#include <linux/types.h>
19#include <linux/interrupt.h>
20#include <linux/mfd/syscon.h>
21#include <linux/regmap.h>
22
23#include "pcie-designware.h"
24
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053025#define to_artpec6_pcie(x) dev_get_drvdata((x)->dev)
Niklas Cassela3cbfae2016-05-09 13:49:03 +020026
Niklas Casseldc734ee2017-12-20 00:29:39 +010027enum artpec_pcie_variants {
28 ARTPEC6,
29 ARTPEC7,
30};
31
Niklas Cassela3cbfae2016-05-09 13:49:03 +020032struct artpec6_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053033 struct dw_pcie *pci;
Bjorn Helgaas7c62efc2016-10-06 13:30:56 -050034 struct regmap *regmap; /* DT axis,syscon-pcie */
35 void __iomem *phy_base; /* DT phy */
Niklas Casseldc734ee2017-12-20 00:29:39 +010036 enum artpec_pcie_variants variant;
Niklas Casselb5074ef2017-12-20 00:29:35 +010037 enum dw_pcie_device_mode mode;
Niklas Cassela3cbfae2016-05-09 13:49:03 +020038};
39
Niklas Casselb5074ef2017-12-20 00:29:35 +010040struct artpec_pcie_of_data {
Niklas Casseldc734ee2017-12-20 00:29:39 +010041 enum artpec_pcie_variants variant;
Niklas Casselb5074ef2017-12-20 00:29:35 +010042 enum dw_pcie_device_mode mode;
43};
44
45static const struct of_device_id artpec6_pcie_of_match[];
46
Niklas Cassela3cbfae2016-05-09 13:49:03 +020047/* PCIe Port Logic registers (memory-mapped) */
48#define PL_OFFSET 0x700
Niklas Cassela3cbfae2016-05-09 13:49:03 +020049
Niklas Casseldc734ee2017-12-20 00:29:39 +010050#define ACK_F_ASPM_CTRL_OFF (PL_OFFSET + 0xc)
51#define ACK_N_FTS_MASK GENMASK(15, 8)
52#define ACK_N_FTS(x) (((x) << 8) & ACK_N_FTS_MASK)
53
54#define FAST_TRAINING_SEQ_MASK GENMASK(7, 0)
55#define FAST_TRAINING_SEQ(x) (((x) << 0) & FAST_TRAINING_SEQ_MASK)
Niklas Cassela3cbfae2016-05-09 13:49:03 +020056
57/* ARTPEC-6 specific registers */
58#define PCIECFG 0x18
Niklas Casselbc5d7df2017-12-20 00:29:32 +010059#define PCIECFG_DBG_OEN BIT(24)
60#define PCIECFG_CORE_RESET_REQ BIT(21)
61#define PCIECFG_LTSSM_ENABLE BIT(20)
Niklas Casselb5074ef2017-12-20 00:29:35 +010062#define PCIECFG_DEVICE_TYPE_MASK GENMASK(19, 16)
Niklas Casselbc5d7df2017-12-20 00:29:32 +010063#define PCIECFG_CLKREQ_B BIT(11)
64#define PCIECFG_REFCLK_ENABLE BIT(10)
65#define PCIECFG_PLL_ENABLE BIT(9)
66#define PCIECFG_PCLK_ENABLE BIT(8)
67#define PCIECFG_RISRCREN BIT(4)
68#define PCIECFG_MODE_TX_DRV_EN BIT(3)
69#define PCIECFG_CISRREN BIT(2)
70#define PCIECFG_MACRO_ENABLE BIT(0)
Niklas Casseldc734ee2017-12-20 00:29:39 +010071/* ARTPEC-7 specific fields */
72#define PCIECFG_REFCLKSEL BIT(23)
73#define PCIECFG_NOC_RESET BIT(3)
74
75#define PCIESTAT 0x1c
76/* ARTPEC-7 specific fields */
77#define PCIESTAT_EXTREFCLK BIT(3)
Niklas Cassela3cbfae2016-05-09 13:49:03 +020078
79#define NOCCFG 0x40
Niklas Casselbc5d7df2017-12-20 00:29:32 +010080#define NOCCFG_ENABLE_CLK_PCIE BIT(4)
81#define NOCCFG_POWER_PCIE_IDLEACK BIT(3)
82#define NOCCFG_POWER_PCIE_IDLE BIT(2)
83#define NOCCFG_POWER_PCIE_IDLEREQ BIT(1)
Niklas Cassela3cbfae2016-05-09 13:49:03 +020084
85#define PHY_STATUS 0x118
Niklas Casselbc5d7df2017-12-20 00:29:32 +010086#define PHY_COSPLLLOCK BIT(0)
Niklas Cassela3cbfae2016-05-09 13:49:03 +020087
Niklas Casseldc734ee2017-12-20 00:29:39 +010088#define PHY_TX_ASIC_OUT 0x4040
89#define PHY_TX_ASIC_OUT_TX_ACK BIT(0)
90
91#define PHY_RX_ASIC_OUT 0x405c
92#define PHY_RX_ASIC_OUT_ACK BIT(0)
Niklas Cassela3cbfae2016-05-09 13:49:03 +020093
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -050094static u32 artpec6_pcie_readl(struct artpec6_pcie *artpec6_pcie, u32 offset)
95{
96 u32 val;
97
98 regmap_read(artpec6_pcie->regmap, offset, &val);
99 return val;
100}
101
102static void artpec6_pcie_writel(struct artpec6_pcie *artpec6_pcie, u32 offset, u32 val)
103{
104 regmap_write(artpec6_pcie->regmap, offset, val);
105}
106
Niklas Casselb6900ae2017-12-20 00:29:36 +0100107static u64 artpec6_pcie_cpu_addr_fixup(struct dw_pcie *pci, u64 pci_addr)
Kishon Vijay Abraham I62c5549f2017-03-13 19:13:24 +0530108{
Niklas Casselb6900ae2017-12-20 00:29:36 +0100109 struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
110 struct pcie_port *pp = &pci->pp;
111 struct dw_pcie_ep *ep = &pci->ep;
112
113 switch (artpec6_pcie->mode) {
114 case DW_PCIE_RC_TYPE:
115 return pci_addr - pp->cfg0_base;
116 case DW_PCIE_EP_TYPE:
117 return pci_addr - ep->phys_base;
118 default:
119 dev_err(pci->dev, "UNKNOWN device type\n");
120 }
121 return pci_addr;
Kishon Vijay Abraham I62c5549f2017-03-13 19:13:24 +0530122}
123
Niklas Cassel87c9a732017-12-20 00:29:33 +0100124static int artpec6_pcie_establish_link(struct dw_pcie *pci)
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200125{
Niklas Cassel87c9a732017-12-20 00:29:33 +0100126 struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
127 u32 val;
128
129 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
130 val |= PCIECFG_LTSSM_ENABLE;
131 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
132
133 return 0;
134}
135
Niklas Casselb5074ef2017-12-20 00:29:35 +0100136static void artpec6_pcie_stop_link(struct dw_pcie *pci)
137{
138 struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
139 u32 val;
140
141 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
142 val &= ~PCIECFG_LTSSM_ENABLE;
143 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
144}
145
146static const struct dw_pcie_ops dw_pcie_ops = {
147 .cpu_addr_fixup = artpec6_pcie_cpu_addr_fixup,
148 .start_link = artpec6_pcie_establish_link,
149 .stop_link = artpec6_pcie_stop_link,
150};
151
Niklas Casseldc734ee2017-12-20 00:29:39 +0100152static void artpec6_pcie_wait_for_phy_a6(struct artpec6_pcie *artpec6_pcie)
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200153{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530154 struct dw_pcie *pci = artpec6_pcie->pci;
Niklas Cassel4fdd5b52017-12-20 00:29:37 +0100155 struct device *dev = pci->dev;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200156 u32 val;
157 unsigned int retries;
158
Niklas Cassel4fdd5b52017-12-20 00:29:37 +0100159 retries = 50;
160 do {
161 usleep_range(1000, 2000);
162 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
163 retries--;
164 } while (retries &&
165 (val & (NOCCFG_POWER_PCIE_IDLEACK | NOCCFG_POWER_PCIE_IDLE)));
166 if (!retries)
167 dev_err(dev, "PCIe clock manager did not leave idle state\n");
168
169 retries = 50;
170 do {
171 usleep_range(1000, 2000);
172 val = readl(artpec6_pcie->phy_base + PHY_STATUS);
173 retries--;
174 } while (retries && !(val & PHY_COSPLLLOCK));
175 if (!retries)
176 dev_err(dev, "PHY PLL did not lock\n");
177}
178
Niklas Casseldc734ee2017-12-20 00:29:39 +0100179static void artpec6_pcie_wait_for_phy_a7(struct artpec6_pcie *artpec6_pcie)
180{
181 struct dw_pcie *pci = artpec6_pcie->pci;
182 struct device *dev = pci->dev;
183 u32 val;
184 u16 phy_status_tx, phy_status_rx;
185 unsigned int retries;
186
187 retries = 50;
188 do {
189 usleep_range(1000, 2000);
190 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
191 retries--;
192 } while (retries &&
193 (val & (NOCCFG_POWER_PCIE_IDLEACK | NOCCFG_POWER_PCIE_IDLE)));
194 if (!retries)
195 dev_err(dev, "PCIe clock manager did not leave idle state\n");
196
197 retries = 50;
198 do {
199 usleep_range(1000, 2000);
200 phy_status_tx = readw(artpec6_pcie->phy_base + PHY_TX_ASIC_OUT);
201 phy_status_rx = readw(artpec6_pcie->phy_base + PHY_RX_ASIC_OUT);
202 retries--;
203 } while (retries && ((phy_status_tx & PHY_TX_ASIC_OUT_TX_ACK) ||
204 (phy_status_rx & PHY_RX_ASIC_OUT_ACK)));
205 if (!retries)
206 dev_err(dev, "PHY did not enter Pn state\n");
207}
208
209static void artpec6_pcie_wait_for_phy(struct artpec6_pcie *artpec6_pcie)
210{
211 switch (artpec6_pcie->variant) {
212 case ARTPEC6:
213 artpec6_pcie_wait_for_phy_a6(artpec6_pcie);
214 break;
215 case ARTPEC7:
216 artpec6_pcie_wait_for_phy_a7(artpec6_pcie);
217 break;
218 }
219}
220
221static void artpec6_pcie_init_phy_a6(struct artpec6_pcie *artpec6_pcie)
Niklas Cassel87c9a732017-12-20 00:29:33 +0100222{
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200223 u32 val;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200224
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500225 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200226 val |= PCIECFG_RISRCREN | /* Receiver term. 50 Ohm */
227 PCIECFG_MODE_TX_DRV_EN |
228 PCIECFG_CISRREN | /* Reference clock term. 100 Ohm */
229 PCIECFG_MACRO_ENABLE;
230 val |= PCIECFG_REFCLK_ENABLE;
231 val &= ~PCIECFG_DBG_OEN;
232 val &= ~PCIECFG_CLKREQ_B;
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500233 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200234 usleep_range(5000, 6000);
235
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500236 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200237 val |= NOCCFG_ENABLE_CLK_PCIE;
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500238 artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200239 usleep_range(20, 30);
240
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500241 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200242 val |= PCIECFG_PCLK_ENABLE | PCIECFG_PLL_ENABLE;
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500243 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200244 usleep_range(6000, 7000);
245
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500246 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200247 val &= ~NOCCFG_POWER_PCIE_IDLEREQ;
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500248 artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
Niklas Cassel87c9a732017-12-20 00:29:33 +0100249}
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200250
Niklas Casseldc734ee2017-12-20 00:29:39 +0100251static void artpec6_pcie_init_phy_a7(struct artpec6_pcie *artpec6_pcie)
252{
253 struct dw_pcie *pci = artpec6_pcie->pci;
254 u32 val;
255 bool extrefclk;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200256
Niklas Casseldc734ee2017-12-20 00:29:39 +0100257 /* Check if external reference clock is connected */
258 val = artpec6_pcie_readl(artpec6_pcie, PCIESTAT);
259 extrefclk = !!(val & PCIESTAT_EXTREFCLK);
260 dev_dbg(pci->dev, "Using reference clock: %s\n",
261 extrefclk ? "external" : "internal");
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200262
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500263 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
Niklas Casseldc734ee2017-12-20 00:29:39 +0100264 val |= PCIECFG_RISRCREN | /* Receiver term. 50 Ohm */
265 PCIECFG_PCLK_ENABLE;
266 if (extrefclk)
267 val |= PCIECFG_REFCLKSEL;
268 else
269 val &= ~PCIECFG_REFCLKSEL;
270 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
271 usleep_range(10, 20);
272
273 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
274 val |= NOCCFG_ENABLE_CLK_PCIE;
275 artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
276 usleep_range(20, 30);
277
278 val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
279 val &= ~NOCCFG_POWER_PCIE_IDLEREQ;
280 artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
281}
282
283static void artpec6_pcie_init_phy(struct artpec6_pcie *artpec6_pcie)
284{
285 switch (artpec6_pcie->variant) {
286 case ARTPEC6:
287 artpec6_pcie_init_phy_a6(artpec6_pcie);
288 break;
289 case ARTPEC7:
290 artpec6_pcie_init_phy_a7(artpec6_pcie);
291 break;
292 }
293}
294
295static void artpec6_pcie_set_nfts(struct artpec6_pcie *artpec6_pcie)
296{
297 struct dw_pcie *pci = artpec6_pcie->pci;
298 u32 val;
299
300 if (artpec6_pcie->variant != ARTPEC7)
301 return;
302
303 /*
304 * Increase the N_FTS (Number of Fast Training Sequences)
305 * to be transmitted when transitioning from L0s to L0.
306 */
307 val = dw_pcie_readl_dbi(pci, ACK_F_ASPM_CTRL_OFF);
308 val &= ~ACK_N_FTS_MASK;
309 val |= ACK_N_FTS(180);
310 dw_pcie_writel_dbi(pci, ACK_F_ASPM_CTRL_OFF, val);
311
312 /*
313 * Set the Number of Fast Training Sequences that the core
314 * advertises as its N_FTS during Gen2 or Gen3 link training.
315 */
316 val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
317 val &= ~FAST_TRAINING_SEQ_MASK;
318 val |= FAST_TRAINING_SEQ(180);
319 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
320}
321
Niklas Cassel87c9a732017-12-20 00:29:33 +0100322static void artpec6_pcie_assert_core_reset(struct artpec6_pcie *artpec6_pcie)
323{
324 u32 val;
325
326 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
Niklas Casseldc734ee2017-12-20 00:29:39 +0100327 switch (artpec6_pcie->variant) {
328 case ARTPEC6:
329 val |= PCIECFG_CORE_RESET_REQ;
330 break;
331 case ARTPEC7:
332 val &= ~PCIECFG_NOC_RESET;
333 break;
334 }
Niklas Cassel87c9a732017-12-20 00:29:33 +0100335 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
336}
337
338static void artpec6_pcie_deassert_core_reset(struct artpec6_pcie *artpec6_pcie)
339{
340 u32 val;
341
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200342 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
Niklas Casseldc734ee2017-12-20 00:29:39 +0100343 switch (artpec6_pcie->variant) {
344 case ARTPEC6:
345 val &= ~PCIECFG_CORE_RESET_REQ;
346 break;
347 case ARTPEC7:
348 val |= PCIECFG_NOC_RESET;
349 break;
350 }
Bjorn Helgaas26fbcc52016-10-06 13:30:56 -0500351 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200352 usleep_range(100, 200);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200353}
354
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500355static void artpec6_pcie_enable_interrupts(struct artpec6_pcie *artpec6_pcie)
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200356{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530357 struct dw_pcie *pci = artpec6_pcie->pci;
358 struct pcie_port *pp = &pci->pp;
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500359
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200360 if (IS_ENABLED(CONFIG_PCI_MSI))
361 dw_pcie_msi_init(pp);
362}
363
Bjorn Andersson4a301762017-07-15 23:39:45 -0700364static int artpec6_pcie_host_init(struct pcie_port *pp)
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200365{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530366 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
367 struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500368
Niklas Cassel87c9a732017-12-20 00:29:33 +0100369 artpec6_pcie_assert_core_reset(artpec6_pcie);
370 artpec6_pcie_init_phy(artpec6_pcie);
371 artpec6_pcie_deassert_core_reset(artpec6_pcie);
Niklas Cassel4fdd5b52017-12-20 00:29:37 +0100372 artpec6_pcie_wait_for_phy(artpec6_pcie);
Niklas Casseldc734ee2017-12-20 00:29:39 +0100373 artpec6_pcie_set_nfts(artpec6_pcie);
Niklas Cassel87c9a732017-12-20 00:29:33 +0100374 dw_pcie_setup_rc(pp);
375 artpec6_pcie_establish_link(pci);
376 dw_pcie_wait_for_link(pci);
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500377 artpec6_pcie_enable_interrupts(artpec6_pcie);
Bjorn Andersson4a301762017-07-15 23:39:45 -0700378
379 return 0;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200380}
381
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800382static const struct dw_pcie_host_ops artpec6_pcie_host_ops = {
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200383 .host_init = artpec6_pcie_host_init,
384};
385
Bjorn Helgaasb6f5f432016-10-06 13:30:56 -0500386static int artpec6_add_pcie_port(struct artpec6_pcie *artpec6_pcie,
Niklas Casselb58ddf12016-09-09 09:45:30 +0200387 struct platform_device *pdev)
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200388{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530389 struct dw_pcie *pci = artpec6_pcie->pci;
390 struct pcie_port *pp = &pci->pp;
391 struct device *dev = pci->dev;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200392 int ret;
393
394 if (IS_ENABLED(CONFIG_PCI_MSI)) {
395 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
Fabio Estevam16df7cd2017-08-31 14:52:04 -0300396 if (pp->msi_irq < 0) {
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500397 dev_err(dev, "failed to get MSI irq\n");
Fabio Estevam16df7cd2017-08-31 14:52:04 -0300398 return pp->msi_irq;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200399 }
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200400 }
401
402 pp->root_bus_nr = -1;
403 pp->ops = &artpec6_pcie_host_ops;
404
405 ret = dw_pcie_host_init(pp);
406 if (ret) {
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500407 dev_err(dev, "failed to initialize host\n");
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200408 return ret;
409 }
410
411 return 0;
412}
413
Niklas Casselb5074ef2017-12-20 00:29:35 +0100414static void artpec6_pcie_ep_init(struct dw_pcie_ep *ep)
415{
416 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
417 struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pci);
418 enum pci_barno bar;
419
420 artpec6_pcie_assert_core_reset(artpec6_pcie);
421 artpec6_pcie_init_phy(artpec6_pcie);
422 artpec6_pcie_deassert_core_reset(artpec6_pcie);
Niklas Cassel4fdd5b52017-12-20 00:29:37 +0100423 artpec6_pcie_wait_for_phy(artpec6_pcie);
Niklas Casseldc734ee2017-12-20 00:29:39 +0100424 artpec6_pcie_set_nfts(artpec6_pcie);
Niklas Casselb5074ef2017-12-20 00:29:35 +0100425
426 for (bar = BAR_0; bar <= BAR_5; bar++)
427 dw_pcie_ep_reset_bar(pci, bar);
428}
429
Bjorn Helgaas16093362018-02-01 11:36:07 -0600430static int artpec6_pcie_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
Niklas Casselb5074ef2017-12-20 00:29:35 +0100431 enum pci_epc_irq_type type, u8 interrupt_num)
432{
433 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
434
435 switch (type) {
436 case PCI_EPC_IRQ_LEGACY:
437 dev_err(pci->dev, "EP cannot trigger legacy IRQs\n");
438 return -EINVAL;
439 case PCI_EPC_IRQ_MSI:
Bjorn Helgaas16093362018-02-01 11:36:07 -0600440 return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
Niklas Casselb5074ef2017-12-20 00:29:35 +0100441 default:
442 dev_err(pci->dev, "UNKNOWN IRQ type\n");
443 }
444
445 return 0;
446}
447
448static struct dw_pcie_ep_ops pcie_ep_ops = {
449 .ep_init = artpec6_pcie_ep_init,
450 .raise_irq = artpec6_pcie_raise_irq,
Niklas Cassel794a8602017-04-03 17:35:12 -0500451};
452
Niklas Casselb5074ef2017-12-20 00:29:35 +0100453static int artpec6_add_pcie_ep(struct artpec6_pcie *artpec6_pcie,
454 struct platform_device *pdev)
455{
456 int ret;
457 struct dw_pcie_ep *ep;
458 struct resource *res;
459 struct device *dev = &pdev->dev;
460 struct dw_pcie *pci = artpec6_pcie->pci;
461
462 ep = &pci->ep;
463 ep->ops = &pcie_ep_ops;
464
465 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi2");
Gustavo Pimentel71dcce62018-05-14 18:32:34 +0100466 pci->dbi_base2 = devm_ioremap_resource(dev, res);
467 if (IS_ERR(pci->dbi_base2))
468 return PTR_ERR(pci->dbi_base2);
Niklas Casselb5074ef2017-12-20 00:29:35 +0100469
470 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
471 if (!res)
472 return -EINVAL;
473
474 ep->phys_base = res->start;
475 ep->addr_size = resource_size(res);
476
477 ret = dw_pcie_ep_init(ep);
478 if (ret) {
479 dev_err(dev, "failed to initialize endpoint\n");
480 return ret;
481 }
482
483 return 0;
484}
485
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200486static int artpec6_pcie_probe(struct platform_device *pdev)
487{
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500488 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530489 struct dw_pcie *pci;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200490 struct artpec6_pcie *artpec6_pcie;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200491 struct resource *dbi_base;
492 struct resource *phy_base;
493 int ret;
Niklas Casselb5074ef2017-12-20 00:29:35 +0100494 const struct of_device_id *match;
495 const struct artpec_pcie_of_data *data;
Niklas Casseldc734ee2017-12-20 00:29:39 +0100496 enum artpec_pcie_variants variant;
Niklas Casselb5074ef2017-12-20 00:29:35 +0100497 enum dw_pcie_device_mode mode;
498
499 match = of_match_device(artpec6_pcie_of_match, dev);
500 if (!match)
501 return -EINVAL;
502
503 data = (struct artpec_pcie_of_data *)match->data;
Niklas Casseldc734ee2017-12-20 00:29:39 +0100504 variant = (enum artpec_pcie_variants)data->variant;
Niklas Casselb5074ef2017-12-20 00:29:35 +0100505 mode = (enum dw_pcie_device_mode)data->mode;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200506
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500507 artpec6_pcie = devm_kzalloc(dev, sizeof(*artpec6_pcie), GFP_KERNEL);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200508 if (!artpec6_pcie)
509 return -ENOMEM;
510
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530511 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
512 if (!pci)
513 return -ENOMEM;
514
515 pci->dev = dev;
Niklas Cassel794a8602017-04-03 17:35:12 -0500516 pci->ops = &dw_pcie_ops;
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200517
Guenter Roeckc0464062017-02-25 02:08:12 -0800518 artpec6_pcie->pci = pci;
Niklas Casseldc734ee2017-12-20 00:29:39 +0100519 artpec6_pcie->variant = variant;
Niklas Casselb5074ef2017-12-20 00:29:35 +0100520 artpec6_pcie->mode = mode;
Guenter Roeckc0464062017-02-25 02:08:12 -0800521
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200522 dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530523 pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
524 if (IS_ERR(pci->dbi_base))
525 return PTR_ERR(pci->dbi_base);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200526
527 phy_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500528 artpec6_pcie->phy_base = devm_ioremap_resource(dev, phy_base);
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200529 if (IS_ERR(artpec6_pcie->phy_base))
530 return PTR_ERR(artpec6_pcie->phy_base);
531
532 artpec6_pcie->regmap =
Bjorn Helgaase6f31152016-10-06 13:30:57 -0500533 syscon_regmap_lookup_by_phandle(dev->of_node,
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200534 "axis,syscon-pcie");
535 if (IS_ERR(artpec6_pcie->regmap))
536 return PTR_ERR(artpec6_pcie->regmap);
537
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530538 platform_set_drvdata(pdev, artpec6_pcie);
539
Niklas Casselb5074ef2017-12-20 00:29:35 +0100540 switch (artpec6_pcie->mode) {
541 case DW_PCIE_RC_TYPE:
542 if (!IS_ENABLED(CONFIG_PCIE_ARTPEC6_HOST))
543 return -ENODEV;
544
545 ret = artpec6_add_pcie_port(artpec6_pcie, pdev);
546 if (ret < 0)
547 return ret;
548 break;
549 case DW_PCIE_EP_TYPE: {
550 u32 val;
551
552 if (!IS_ENABLED(CONFIG_PCIE_ARTPEC6_EP))
553 return -ENODEV;
554
555 val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
556 val &= ~PCIECFG_DEVICE_TYPE_MASK;
557 artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
558 ret = artpec6_add_pcie_ep(artpec6_pcie, pdev);
559 if (ret < 0)
560 return ret;
561 break;
562 }
563 default:
564 dev_err(dev, "INVALID device type %d\n", artpec6_pcie->mode);
565 }
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200566
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200567 return 0;
568}
569
Niklas Casselb5074ef2017-12-20 00:29:35 +0100570static const struct artpec_pcie_of_data artpec6_pcie_rc_of_data = {
Niklas Casseldc734ee2017-12-20 00:29:39 +0100571 .variant = ARTPEC6,
Niklas Casselb5074ef2017-12-20 00:29:35 +0100572 .mode = DW_PCIE_RC_TYPE,
573};
574
575static const struct artpec_pcie_of_data artpec6_pcie_ep_of_data = {
Niklas Casseldc734ee2017-12-20 00:29:39 +0100576 .variant = ARTPEC6,
577 .mode = DW_PCIE_EP_TYPE,
578};
579
580static const struct artpec_pcie_of_data artpec7_pcie_rc_of_data = {
581 .variant = ARTPEC7,
582 .mode = DW_PCIE_RC_TYPE,
583};
584
585static const struct artpec_pcie_of_data artpec7_pcie_ep_of_data = {
586 .variant = ARTPEC7,
Niklas Casselb5074ef2017-12-20 00:29:35 +0100587 .mode = DW_PCIE_EP_TYPE,
588};
589
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200590static const struct of_device_id artpec6_pcie_of_match[] = {
Niklas Casselb5074ef2017-12-20 00:29:35 +0100591 {
592 .compatible = "axis,artpec6-pcie",
593 .data = &artpec6_pcie_rc_of_data,
594 },
595 {
596 .compatible = "axis,artpec6-pcie-ep",
597 .data = &artpec6_pcie_ep_of_data,
598 },
Niklas Casseldc734ee2017-12-20 00:29:39 +0100599 {
600 .compatible = "axis,artpec7-pcie",
601 .data = &artpec7_pcie_rc_of_data,
602 },
603 {
604 .compatible = "axis,artpec7-pcie-ep",
605 .data = &artpec7_pcie_ep_of_data,
606 },
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200607 {},
608};
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200609
610static struct platform_driver artpec6_pcie_driver = {
611 .probe = artpec6_pcie_probe,
612 .driver = {
613 .name = "artpec6-pcie",
614 .of_match_table = artpec6_pcie_of_match,
Brian Norrisa5f40e802017-04-20 15:36:25 -0500615 .suppress_bind_attrs = true,
Niklas Cassela3cbfae2016-05-09 13:49:03 +0200616 },
617};
Paul Gortmaker58bdaa12016-07-02 19:13:22 -0400618builtin_platform_driver(artpec6_pcie_driver);