blob: 7d05e51205b3876e227c9fb6395a35a30252a66a [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Ley Foon Taneaa61112015-10-23 18:27:12 +08002/*
3 * Copyright Altera Corporation (C) 2013-2015. All rights reserved
4 *
Paul Gortmakerbb9b54c2016-08-22 17:59:42 -04005 * Author: Ley Foon Tan <lftan@altera.com>
6 * Description: Altera PCIe host controller driver
Ley Foon Taneaa61112015-10-23 18:27:12 +08007 */
8
9#include <linux/delay.h>
10#include <linux/interrupt.h>
11#include <linux/irqchip/chained_irq.h>
Paul Gortmakerbb9b54c2016-08-22 17:59:42 -040012#include <linux/init.h>
Ley Foon Taneaa61112015-10-23 18:27:12 +080013#include <linux/of_address.h>
14#include <linux/of_irq.h>
15#include <linux/of_pci.h>
16#include <linux/pci.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19
Rob Herring9e2aee82018-05-11 12:15:30 -050020#include "../pci.h"
21
Ley Foon Taneaa61112015-10-23 18:27:12 +080022#define RP_TX_REG0 0x2000
23#define RP_TX_REG1 0x2004
24#define RP_TX_CNTRL 0x2008
25#define RP_TX_EOP 0x2
26#define RP_TX_SOP 0x1
27#define RP_RXCPL_STATUS 0x2010
28#define RP_RXCPL_EOP 0x2
29#define RP_RXCPL_SOP 0x1
30#define RP_RXCPL_REG0 0x2014
31#define RP_RXCPL_REG1 0x2018
32#define P2A_INT_STATUS 0x3060
33#define P2A_INT_STS_ALL 0xf
34#define P2A_INT_ENABLE 0x3070
35#define P2A_INT_ENA_ALL 0xf
36#define RP_LTSSM 0x3c64
Ley Foon Taneff31f42016-03-02 17:43:07 +080037#define RP_LTSSM_MASK 0x1f
Ley Foon Taneaa61112015-10-23 18:27:12 +080038#define LTSSM_L0 0xf
39
Ley Foon Tance4f1c72016-08-26 09:47:25 +080040#define PCIE_CAP_OFFSET 0x80
Ley Foon Taneaa61112015-10-23 18:27:12 +080041/* TLP configuration type 0 and 1 */
42#define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
43#define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
44#define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
45#define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
46#define TLP_PAYLOAD_SIZE 0x01
47#define TLP_READ_TAG 0x1d
48#define TLP_WRITE_TAG 0x10
Bjorn Helgaas4f276282016-10-06 13:29:02 -050049#define RP_DEVFN 0
50#define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
Ley Foon Tan2a7275a2017-02-28 18:37:16 +080051#define TLP_CFGRD_DW0(pcie, bus) \
Bjorn Helgaaseb576712016-10-06 13:29:01 -050052 ((((bus == pcie->root_bus_nr) ? TLP_FMTTYPE_CFGRD0 \
53 : TLP_FMTTYPE_CFGRD1) << 24) | \
54 TLP_PAYLOAD_SIZE)
Ley Foon Tan2a7275a2017-02-28 18:37:16 +080055#define TLP_CFGWR_DW0(pcie, bus) \
56 ((((bus == pcie->root_bus_nr) ? TLP_FMTTYPE_CFGWR0 \
57 : TLP_FMTTYPE_CFGWR1) << 24) | \
58 TLP_PAYLOAD_SIZE)
Bjorn Helgaas4f276282016-10-06 13:29:02 -050059#define TLP_CFG_DW1(pcie, tag, be) \
60 (((TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN)) << 16) | (tag << 8) | (be))
Ley Foon Taneaa61112015-10-23 18:27:12 +080061#define TLP_CFG_DW2(bus, devfn, offset) \
62 (((bus) << 24) | ((devfn) << 16) | (offset))
Yadi Hu8ca6e0a2017-02-17 14:20:26 -060063#define TLP_COMP_STATUS(s) (((s) >> 13) & 7)
Ley Foon Taneaa61112015-10-23 18:27:12 +080064#define TLP_HDR_SIZE 3
65#define TLP_LOOP 500
66
Ley Foon Tan411dc322016-08-15 14:06:02 +080067#define LINK_UP_TIMEOUT HZ
68#define LINK_RETRAIN_TIMEOUT HZ
Ley Foon Tan3a928e92016-06-21 16:53:13 +080069
Ley Foon Taneaa61112015-10-23 18:27:12 +080070#define DWORD_MASK 3
71
72struct altera_pcie {
73 struct platform_device *pdev;
Bjorn Helgaasdbeb4bd2016-10-06 13:29:02 -050074 void __iomem *cra_base; /* DT Cra */
Ley Foon Taneaa61112015-10-23 18:27:12 +080075 int irq;
76 u8 root_bus_nr;
77 struct irq_domain *irq_domain;
78 struct resource bus_range;
79 struct list_head resources;
80};
81
82struct tlp_rp_regpair_t {
83 u32 ctrl;
84 u32 reg0;
85 u32 reg1;
86};
87
Bjorn Helgaasf8be11a2016-07-22 15:54:41 -050088static inline void cra_writel(struct altera_pcie *pcie, const u32 value,
89 const u32 reg)
90{
91 writel_relaxed(value, pcie->cra_base + reg);
92}
93
94static inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg)
95{
96 return readl_relaxed(pcie->cra_base + reg);
97}
98
Bjorn Helgaas499c0102017-11-09 16:17:39 -060099static bool altera_pcie_link_up(struct altera_pcie *pcie)
Bjorn Helgaasf8be11a2016-07-22 15:54:41 -0500100{
101 return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0);
102}
103
Ley Foon Taneaa61112015-10-23 18:27:12 +0800104/*
105 * Altera PCIe port uses BAR0 of RC's configuration space as the translation
106 * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
107 * using these registers, so it can be reached by DMA from EP devices.
108 * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
109 * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
110 * should be hidden during enumeration to avoid the sizing and resource
111 * allocation by PCIe core.
112 */
113static bool altera_pcie_hide_rc_bar(struct pci_bus *bus, unsigned int devfn,
114 int offset)
115{
116 if (pci_is_root_bus(bus) && (devfn == 0) &&
117 (offset == PCI_BASE_ADDRESS_0))
118 return true;
119
120 return false;
121}
122
Ley Foon Taneaa61112015-10-23 18:27:12 +0800123static void tlp_write_tx(struct altera_pcie *pcie,
124 struct tlp_rp_regpair_t *tlp_rp_regdata)
125{
126 cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0);
127 cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1);
128 cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL);
129}
130
Bjorn Helgaas14c7b952016-10-06 13:29:03 -0500131static bool altera_pcie_valid_device(struct altera_pcie *pcie,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800132 struct pci_bus *bus, int dev)
133{
134 /* If there is no link, then there is no device */
135 if (bus->number != pcie->root_bus_nr) {
Bjorn Helgaas499c0102017-11-09 16:17:39 -0600136 if (!altera_pcie_link_up(pcie))
Ley Foon Taneaa61112015-10-23 18:27:12 +0800137 return false;
138 }
139
140 /* access only one slot on each root port */
141 if (bus->number == pcie->root_bus_nr && dev > 0)
142 return false;
143
Ley Foon Taneaa61112015-10-23 18:27:12 +0800144 return true;
145}
146
147static int tlp_read_packet(struct altera_pcie *pcie, u32 *value)
148{
Dan Carpenter7f52f312015-12-04 16:21:08 -0600149 int i;
Gustavo A. R. Silvadb89ed12018-01-19 21:26:51 -0600150 bool sop = false;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800151 u32 ctrl;
152 u32 reg0, reg1;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600153 u32 comp_status = 1;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800154
155 /*
156 * Minimum 2 loops to read TLP headers and 1 loop to read data
157 * payload.
158 */
Dan Carpenter7f52f312015-12-04 16:21:08 -0600159 for (i = 0; i < TLP_LOOP; i++) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800160 ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
161 if ((ctrl & RP_RXCPL_SOP) || (ctrl & RP_RXCPL_EOP) || sop) {
162 reg0 = cra_readl(pcie, RP_RXCPL_REG0);
163 reg1 = cra_readl(pcie, RP_RXCPL_REG1);
164
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600165 if (ctrl & RP_RXCPL_SOP) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800166 sop = true;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600167 comp_status = TLP_COMP_STATUS(reg1);
168 }
Ley Foon Taneaa61112015-10-23 18:27:12 +0800169
170 if (ctrl & RP_RXCPL_EOP) {
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600171 if (comp_status)
172 return PCIBIOS_DEVICE_NOT_FOUND;
173
Ley Foon Taneaa61112015-10-23 18:27:12 +0800174 if (value)
175 *value = reg0;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600176
Ley Foon Taneaa61112015-10-23 18:27:12 +0800177 return PCIBIOS_SUCCESSFUL;
178 }
179 }
180 udelay(5);
181 }
182
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600183 return PCIBIOS_DEVICE_NOT_FOUND;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800184}
185
186static void tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
187 u32 data, bool align)
188{
189 struct tlp_rp_regpair_t tlp_rp_regdata;
190
191 tlp_rp_regdata.reg0 = headers[0];
192 tlp_rp_regdata.reg1 = headers[1];
193 tlp_rp_regdata.ctrl = RP_TX_SOP;
194 tlp_write_tx(pcie, &tlp_rp_regdata);
195
196 if (align) {
197 tlp_rp_regdata.reg0 = headers[2];
198 tlp_rp_regdata.reg1 = 0;
199 tlp_rp_regdata.ctrl = 0;
200 tlp_write_tx(pcie, &tlp_rp_regdata);
201
202 tlp_rp_regdata.reg0 = data;
203 tlp_rp_regdata.reg1 = 0;
204 } else {
205 tlp_rp_regdata.reg0 = headers[2];
206 tlp_rp_regdata.reg1 = data;
207 }
208
209 tlp_rp_regdata.ctrl = RP_TX_EOP;
210 tlp_write_tx(pcie, &tlp_rp_regdata);
211}
212
213static int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn,
214 int where, u8 byte_en, u32 *value)
215{
216 u32 headers[TLP_HDR_SIZE];
217
Ley Foon Tan2a7275a2017-02-28 18:37:16 +0800218 headers[0] = TLP_CFGRD_DW0(pcie, bus);
Bjorn Helgaas4f276282016-10-06 13:29:02 -0500219 headers[1] = TLP_CFG_DW1(pcie, TLP_READ_TAG, byte_en);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800220 headers[2] = TLP_CFG_DW2(bus, devfn, where);
221
222 tlp_write_packet(pcie, headers, 0, false);
223
224 return tlp_read_packet(pcie, value);
225}
226
227static int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn,
228 int where, u8 byte_en, u32 value)
229{
230 u32 headers[TLP_HDR_SIZE];
231 int ret;
232
Ley Foon Tan2a7275a2017-02-28 18:37:16 +0800233 headers[0] = TLP_CFGWR_DW0(pcie, bus);
Bjorn Helgaas4f276282016-10-06 13:29:02 -0500234 headers[1] = TLP_CFG_DW1(pcie, TLP_WRITE_TAG, byte_en);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800235 headers[2] = TLP_CFG_DW2(bus, devfn, where);
236
237 /* check alignment to Qword */
238 if ((where & 0x7) == 0)
239 tlp_write_packet(pcie, headers, value, true);
240 else
241 tlp_write_packet(pcie, headers, value, false);
242
243 ret = tlp_read_packet(pcie, NULL);
244 if (ret != PCIBIOS_SUCCESSFUL)
245 return ret;
246
247 /*
248 * Monitor changes to PCI_PRIMARY_BUS register on root port
249 * and update local copy of root bus number accordingly.
250 */
251 if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS))
252 pcie->root_bus_nr = (u8)(value);
253
254 return PCIBIOS_SUCCESSFUL;
255}
256
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800257static int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8 busno,
258 unsigned int devfn, int where, int size,
259 u32 *value)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800260{
Ley Foon Taneaa61112015-10-23 18:27:12 +0800261 int ret;
262 u32 data;
263 u8 byte_en;
264
Ley Foon Taneaa61112015-10-23 18:27:12 +0800265 switch (size) {
266 case 1:
267 byte_en = 1 << (where & 3);
268 break;
269 case 2:
270 byte_en = 3 << (where & 3);
271 break;
272 default:
273 byte_en = 0xf;
274 break;
275 }
276
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800277 ret = tlp_cfg_dword_read(pcie, busno, devfn,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800278 (where & ~DWORD_MASK), byte_en, &data);
279 if (ret != PCIBIOS_SUCCESSFUL)
280 return ret;
281
282 switch (size) {
283 case 1:
284 *value = (data >> (8 * (where & 0x3))) & 0xff;
285 break;
286 case 2:
287 *value = (data >> (8 * (where & 0x2))) & 0xffff;
288 break;
289 default:
290 *value = data;
291 break;
292 }
293
294 return PCIBIOS_SUCCESSFUL;
295}
296
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800297static int _altera_pcie_cfg_write(struct altera_pcie *pcie, u8 busno,
298 unsigned int devfn, int where, int size,
299 u32 value)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800300{
Ley Foon Taneaa61112015-10-23 18:27:12 +0800301 u32 data32;
302 u32 shift = 8 * (where & 3);
303 u8 byte_en;
304
Ley Foon Taneaa61112015-10-23 18:27:12 +0800305 switch (size) {
306 case 1:
307 data32 = (value & 0xff) << shift;
308 byte_en = 1 << (where & 3);
309 break;
310 case 2:
311 data32 = (value & 0xffff) << shift;
312 byte_en = 3 << (where & 3);
313 break;
314 default:
315 data32 = value;
316 byte_en = 0xf;
317 break;
318 }
319
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800320 return tlp_cfg_dword_write(pcie, busno, devfn, (where & ~DWORD_MASK),
321 byte_en, data32);
322}
323
324static int altera_pcie_cfg_read(struct pci_bus *bus, unsigned int devfn,
325 int where, int size, u32 *value)
326{
327 struct altera_pcie *pcie = bus->sysdata;
328
329 if (altera_pcie_hide_rc_bar(bus, devfn, where))
330 return PCIBIOS_BAD_REGISTER_NUMBER;
331
Bjorn Helgaas14c7b952016-10-06 13:29:03 -0500332 if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn))) {
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800333 *value = 0xffffffff;
334 return PCIBIOS_DEVICE_NOT_FOUND;
335 }
336
337 return _altera_pcie_cfg_read(pcie, bus->number, devfn, where, size,
338 value);
339}
340
341static int altera_pcie_cfg_write(struct pci_bus *bus, unsigned int devfn,
342 int where, int size, u32 value)
343{
344 struct altera_pcie *pcie = bus->sysdata;
345
346 if (altera_pcie_hide_rc_bar(bus, devfn, where))
347 return PCIBIOS_BAD_REGISTER_NUMBER;
348
Bjorn Helgaas14c7b952016-10-06 13:29:03 -0500349 if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn)))
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800350 return PCIBIOS_DEVICE_NOT_FOUND;
351
352 return _altera_pcie_cfg_write(pcie, bus->number, devfn, where, size,
353 value);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800354}
355
356static struct pci_ops altera_pcie_ops = {
357 .read = altera_pcie_cfg_read,
358 .write = altera_pcie_cfg_write,
359};
360
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800361static int altera_read_cap_word(struct altera_pcie *pcie, u8 busno,
362 unsigned int devfn, int offset, u16 *value)
363{
364 u32 data;
365 int ret;
366
367 ret = _altera_pcie_cfg_read(pcie, busno, devfn,
368 PCIE_CAP_OFFSET + offset, sizeof(*value),
369 &data);
370 *value = data;
371 return ret;
372}
373
374static int altera_write_cap_word(struct altera_pcie *pcie, u8 busno,
375 unsigned int devfn, int offset, u16 value)
376{
377 return _altera_pcie_cfg_write(pcie, busno, devfn,
378 PCIE_CAP_OFFSET + offset, sizeof(value),
379 value);
380}
381
382static void altera_wait_link_retrain(struct altera_pcie *pcie)
383{
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500384 struct device *dev = &pcie->pdev->dev;
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800385 u16 reg16;
386 unsigned long start_jiffies;
387
388 /* Wait for link training end. */
389 start_jiffies = jiffies;
390 for (;;) {
391 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
392 PCI_EXP_LNKSTA, &reg16);
393 if (!(reg16 & PCI_EXP_LNKSTA_LT))
394 break;
395
396 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500397 dev_err(dev, "link retrain timeout\n");
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800398 break;
399 }
400 udelay(100);
401 }
402
403 /* Wait for link is up */
404 start_jiffies = jiffies;
405 for (;;) {
Bjorn Helgaas499c0102017-11-09 16:17:39 -0600406 if (altera_pcie_link_up(pcie))
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800407 break;
408
409 if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500410 dev_err(dev, "link up timeout\n");
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800411 break;
412 }
413 udelay(100);
414 }
415}
416
417static void altera_pcie_retrain(struct altera_pcie *pcie)
418{
419 u16 linkcap, linkstat, linkctl;
420
Bjorn Helgaas499c0102017-11-09 16:17:39 -0600421 if (!altera_pcie_link_up(pcie))
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800422 return;
423
424 /*
425 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
426 * current speed is 2.5 GB/s.
427 */
428 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKCAP,
429 &linkcap);
430 if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB)
431 return;
432
433 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKSTA,
434 &linkstat);
435 if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) {
436 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
437 PCI_EXP_LNKCTL, &linkctl);
438 linkctl |= PCI_EXP_LNKCTL_RL;
439 altera_write_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
440 PCI_EXP_LNKCTL, linkctl);
441
442 altera_wait_link_retrain(pcie);
443 }
444}
445
Ley Foon Taneaa61112015-10-23 18:27:12 +0800446static int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
447 irq_hw_number_t hwirq)
448{
449 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
450 irq_set_chip_data(irq, domain->host_data);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800451 return 0;
452}
453
454static const struct irq_domain_ops intx_domain_ops = {
455 .map = altera_pcie_intx_map,
Paul Burtonbfdbbf02017-08-15 16:24:38 -0500456 .xlate = pci_irqd_intx_xlate,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800457};
458
459static void altera_pcie_isr(struct irq_desc *desc)
460{
461 struct irq_chip *chip = irq_desc_get_chip(desc);
462 struct altera_pcie *pcie;
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500463 struct device *dev;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800464 unsigned long status;
465 u32 bit;
466 u32 virq;
467
468 chained_irq_enter(chip, desc);
469 pcie = irq_desc_get_handler_data(desc);
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500470 dev = &pcie->pdev->dev;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800471
472 while ((status = cra_readl(pcie, P2A_INT_STATUS)
473 & P2A_INT_STS_ALL) != 0) {
Paul Burtonbfdbbf02017-08-15 16:24:38 -0500474 for_each_set_bit(bit, &status, PCI_NUM_INTX) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800475 /* clear interrupts */
476 cra_writel(pcie, 1 << bit, P2A_INT_STATUS);
477
Paul Burtonbfdbbf02017-08-15 16:24:38 -0500478 virq = irq_find_mapping(pcie->irq_domain, bit);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800479 if (virq)
480 generic_handle_irq(virq);
481 else
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500482 dev_err(dev, "unexpected IRQ, INT%d\n", bit);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800483 }
484 }
485
486 chained_irq_exit(chip, desc);
487}
488
Ley Foon Taneaa61112015-10-23 18:27:12 +0800489static int altera_pcie_parse_request_of_pci_ranges(struct altera_pcie *pcie)
490{
491 int err, res_valid = 0;
492 struct device *dev = &pcie->pdev->dev;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800493 struct resource_entry *win;
494
Jan Kiszka5bd51b32018-05-15 11:07:05 +0200495 err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
Jan Kiszka055f87a2018-05-15 11:07:03 +0200496 &pcie->resources, NULL);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800497 if (err)
498 return err;
499
Bjorn Helgaas74462282016-05-31 12:14:17 -0500500 err = devm_request_pci_bus_resources(dev, &pcie->resources);
501 if (err)
502 goto out_release_res;
503
Ley Foon Taneaa61112015-10-23 18:27:12 +0800504 resource_list_for_each_entry(win, &pcie->resources) {
Bjorn Helgaas74462282016-05-31 12:14:17 -0500505 struct resource *res = win->res;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800506
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500507 if (resource_type(res) == IORESOURCE_MEM)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800508 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800509 }
510
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500511 if (res_valid)
512 return 0;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800513
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500514 dev_err(dev, "non-prefetchable memory resource required\n");
515 err = -EINVAL;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800516
517out_release_res:
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500518 pci_free_resource_list(&pcie->resources);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800519 return err;
520}
521
522static int altera_pcie_init_irq_domain(struct altera_pcie *pcie)
523{
524 struct device *dev = &pcie->pdev->dev;
525 struct device_node *node = dev->of_node;
526
527 /* Setup INTx */
Paul Burtonbfdbbf02017-08-15 16:24:38 -0500528 pcie->irq_domain = irq_domain_add_linear(node, PCI_NUM_INTX,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800529 &intx_domain_ops, pcie);
530 if (!pcie->irq_domain) {
531 dev_err(dev, "Failed to get a INTx IRQ domain\n");
532 return -ENOMEM;
533 }
534
535 return 0;
536}
537
538static int altera_pcie_parse_dt(struct altera_pcie *pcie)
539{
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500540 struct device *dev = &pcie->pdev->dev;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800541 struct platform_device *pdev = pcie->pdev;
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500542 struct resource *cra;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800543
544 cra = platform_get_resource_byname(pdev, IORESOURCE_MEM, "Cra");
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500545 pcie->cra_base = devm_ioremap_resource(dev, cra);
Wei Yongjunc19699a2016-10-17 14:56:13 +0000546 if (IS_ERR(pcie->cra_base))
Ley Foon Taneaa61112015-10-23 18:27:12 +0800547 return PTR_ERR(pcie->cra_base);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800548
549 /* setup IRQ */
550 pcie->irq = platform_get_irq(pdev, 0);
Fabio Estevamef753692017-08-31 14:52:07 -0300551 if (pcie->irq < 0) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500552 dev_err(dev, "failed to get IRQ: %d\n", pcie->irq);
Fabio Estevamef753692017-08-31 14:52:07 -0300553 return pcie->irq;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800554 }
555
556 irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800557 return 0;
558}
559
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800560static void altera_pcie_host_init(struct altera_pcie *pcie)
561{
562 altera_pcie_retrain(pcie);
563}
564
Ley Foon Taneaa61112015-10-23 18:27:12 +0800565static int altera_pcie_probe(struct platform_device *pdev)
566{
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500567 struct device *dev = &pdev->dev;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800568 struct altera_pcie *pcie;
569 struct pci_bus *bus;
570 struct pci_bus *child;
Lorenzo Pieralisi98157912017-06-28 15:13:58 -0500571 struct pci_host_bridge *bridge;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800572 int ret;
573
Lorenzo Pieralisi98157912017-06-28 15:13:58 -0500574 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
575 if (!bridge)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800576 return -ENOMEM;
577
Lorenzo Pieralisi98157912017-06-28 15:13:58 -0500578 pcie = pci_host_bridge_priv(bridge);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800579 pcie->pdev = pdev;
580
581 ret = altera_pcie_parse_dt(pcie);
582 if (ret) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500583 dev_err(dev, "Parsing DT failed\n");
Ley Foon Taneaa61112015-10-23 18:27:12 +0800584 return ret;
585 }
586
587 INIT_LIST_HEAD(&pcie->resources);
588
589 ret = altera_pcie_parse_request_of_pci_ranges(pcie);
590 if (ret) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500591 dev_err(dev, "Failed add resources\n");
Ley Foon Taneaa61112015-10-23 18:27:12 +0800592 return ret;
593 }
594
595 ret = altera_pcie_init_irq_domain(pcie);
596 if (ret) {
Bjorn Helgaasfe490672016-10-11 19:50:52 -0500597 dev_err(dev, "Failed creating IRQ Domain\n");
Ley Foon Taneaa61112015-10-23 18:27:12 +0800598 return ret;
599 }
600
601 /* clear all interrupts */
602 cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
603 /* enable all interrupts */
604 cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE);
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800605 altera_pcie_host_init(pcie);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800606
Lorenzo Pieralisi98157912017-06-28 15:13:58 -0500607 list_splice_init(&pcie->resources, &bridge->windows);
608 bridge->dev.parent = dev;
609 bridge->sysdata = pcie;
610 bridge->busnr = pcie->root_bus_nr;
611 bridge->ops = &altera_pcie_ops;
Lorenzo Pieralisi6ab38092017-06-28 15:14:10 -0500612 bridge->map_irq = of_irq_parse_and_map_pci;
613 bridge->swizzle_irq = pci_common_swizzle;
Lorenzo Pieralisi98157912017-06-28 15:13:58 -0500614
615 ret = pci_scan_root_bus_bridge(bridge);
616 if (ret < 0)
617 return ret;
618
619 bus = bridge->bus;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800620
Ley Foon Taneaa61112015-10-23 18:27:12 +0800621 pci_assign_unassigned_bus_resources(bus);
622
623 /* Configure PCI Express setting. */
624 list_for_each_entry(child, &bus->children, node)
625 pcie_bus_configure_settings(child);
626
627 pci_bus_add_devices(bus);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800628 return ret;
629}
630
631static const struct of_device_id altera_pcie_of_match[] = {
632 { .compatible = "altr,pcie-root-port-1.0", },
633 {},
634};
Ley Foon Taneaa61112015-10-23 18:27:12 +0800635
636static struct platform_driver altera_pcie_driver = {
637 .probe = altera_pcie_probe,
638 .driver = {
639 .name = "altera-pcie",
640 .of_match_table = altera_pcie_of_match,
641 .suppress_bind_attrs = true,
642 },
643};
644
Wei Yongjunc5d933b2016-09-10 12:31:01 +0000645builtin_platform_driver(altera_pcie_driver);