blob: c24e96559cbbb8fa0a894e9a07b039cb85d34e50 [file] [log] [blame]
Ley Foon Taneaa61112015-10-23 18:27:12 +08001/*
2 * Copyright Altera Corporation (C) 2013-2015. All rights reserved
3 *
Paul Gortmakerbb9b54c2016-08-22 17:59:42 -04004 * Author: Ley Foon Tan <lftan@altera.com>
5 * Description: Altera PCIe host controller driver
6 *
Ley Foon Taneaa61112015-10-23 18:27:12 +08007 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/delay.h>
21#include <linux/interrupt.h>
22#include <linux/irqchip/chained_irq.h>
Paul Gortmakerbb9b54c2016-08-22 17:59:42 -040023#include <linux/init.h>
Ley Foon Taneaa61112015-10-23 18:27:12 +080024#include <linux/of_address.h>
25#include <linux/of_irq.h>
26#include <linux/of_pci.h>
27#include <linux/pci.h>
28#include <linux/platform_device.h>
29#include <linux/slab.h>
30
31#define RP_TX_REG0 0x2000
32#define RP_TX_REG1 0x2004
33#define RP_TX_CNTRL 0x2008
34#define RP_TX_EOP 0x2
35#define RP_TX_SOP 0x1
36#define RP_RXCPL_STATUS 0x2010
37#define RP_RXCPL_EOP 0x2
38#define RP_RXCPL_SOP 0x1
39#define RP_RXCPL_REG0 0x2014
40#define RP_RXCPL_REG1 0x2018
41#define P2A_INT_STATUS 0x3060
42#define P2A_INT_STS_ALL 0xf
43#define P2A_INT_ENABLE 0x3070
44#define P2A_INT_ENA_ALL 0xf
45#define RP_LTSSM 0x3c64
Ley Foon Taneff31f42016-03-02 17:43:07 +080046#define RP_LTSSM_MASK 0x1f
Ley Foon Taneaa61112015-10-23 18:27:12 +080047#define LTSSM_L0 0xf
48
Ley Foon Tance4f1c72016-08-26 09:47:25 +080049#define PCIE_CAP_OFFSET 0x80
Ley Foon Taneaa61112015-10-23 18:27:12 +080050/* TLP configuration type 0 and 1 */
51#define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
52#define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
53#define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
54#define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
55#define TLP_PAYLOAD_SIZE 0x01
56#define TLP_READ_TAG 0x1d
57#define TLP_WRITE_TAG 0x10
58#define TLP_CFG_DW0(fmttype) (((fmttype) << 24) | TLP_PAYLOAD_SIZE)
59#define TLP_CFG_DW1(reqid, tag, be) (((reqid) << 16) | (tag << 8) | (be))
60#define TLP_CFG_DW2(bus, devfn, offset) \
61 (((bus) << 24) | ((devfn) << 16) | (offset))
62#define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
Ley Foon Tanea1d3792015-12-04 16:21:16 -060063#define TLP_COMP_STATUS(s) (((s) >> 12) & 7)
Ley Foon Taneaa61112015-10-23 18:27:12 +080064#define TLP_HDR_SIZE 3
65#define TLP_LOOP 500
Ley Foon Tan23ec5672015-12-04 16:21:12 -060066#define RP_DEVFN 0
Ley Foon Taneaa61112015-10-23 18:27:12 +080067
Ley Foon Tan411dc322016-08-15 14:06:02 +080068#define LINK_UP_TIMEOUT HZ
69#define LINK_RETRAIN_TIMEOUT HZ
Ley Foon Tan3a928e92016-06-21 16:53:13 +080070
Ley Foon Taneaa61112015-10-23 18:27:12 +080071#define INTX_NUM 4
72
73#define DWORD_MASK 3
74
75struct altera_pcie {
76 struct platform_device *pdev;
77 void __iomem *cra_base;
78 int irq;
79 u8 root_bus_nr;
80 struct irq_domain *irq_domain;
81 struct resource bus_range;
82 struct list_head resources;
83};
84
85struct tlp_rp_regpair_t {
86 u32 ctrl;
87 u32 reg0;
88 u32 reg1;
89};
90
Bjorn Helgaasf8be11a2016-07-22 15:54:41 -050091static inline void cra_writel(struct altera_pcie *pcie, const u32 value,
92 const u32 reg)
93{
94 writel_relaxed(value, pcie->cra_base + reg);
95}
96
97static inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg)
98{
99 return readl_relaxed(pcie->cra_base + reg);
100}
101
102static bool altera_pcie_link_is_up(struct altera_pcie *pcie)
103{
104 return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0);
105}
106
Ley Foon Taneaa61112015-10-23 18:27:12 +0800107/*
108 * Altera PCIe port uses BAR0 of RC's configuration space as the translation
109 * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space
110 * using these registers, so it can be reached by DMA from EP devices.
111 * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
112 * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge
113 * should be hidden during enumeration to avoid the sizing and resource
114 * allocation by PCIe core.
115 */
116static bool altera_pcie_hide_rc_bar(struct pci_bus *bus, unsigned int devfn,
117 int offset)
118{
119 if (pci_is_root_bus(bus) && (devfn == 0) &&
120 (offset == PCI_BASE_ADDRESS_0))
121 return true;
122
123 return false;
124}
125
Ley Foon Taneaa61112015-10-23 18:27:12 +0800126static void tlp_write_tx(struct altera_pcie *pcie,
127 struct tlp_rp_regpair_t *tlp_rp_regdata)
128{
129 cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0);
130 cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1);
131 cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL);
132}
133
Ley Foon Taneaa61112015-10-23 18:27:12 +0800134static bool altera_pcie_valid_config(struct altera_pcie *pcie,
135 struct pci_bus *bus, int dev)
136{
137 /* If there is no link, then there is no device */
138 if (bus->number != pcie->root_bus_nr) {
139 if (!altera_pcie_link_is_up(pcie))
140 return false;
141 }
142
143 /* access only one slot on each root port */
144 if (bus->number == pcie->root_bus_nr && dev > 0)
145 return false;
146
Ley Foon Taneaa61112015-10-23 18:27:12 +0800147 return true;
148}
149
150static int tlp_read_packet(struct altera_pcie *pcie, u32 *value)
151{
Dan Carpenter7f52f312015-12-04 16:21:08 -0600152 int i;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800153 bool sop = 0;
154 u32 ctrl;
155 u32 reg0, reg1;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600156 u32 comp_status = 1;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800157
158 /*
159 * Minimum 2 loops to read TLP headers and 1 loop to read data
160 * payload.
161 */
Dan Carpenter7f52f312015-12-04 16:21:08 -0600162 for (i = 0; i < TLP_LOOP; i++) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800163 ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
164 if ((ctrl & RP_RXCPL_SOP) || (ctrl & RP_RXCPL_EOP) || sop) {
165 reg0 = cra_readl(pcie, RP_RXCPL_REG0);
166 reg1 = cra_readl(pcie, RP_RXCPL_REG1);
167
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600168 if (ctrl & RP_RXCPL_SOP) {
Ley Foon Taneaa61112015-10-23 18:27:12 +0800169 sop = true;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600170 comp_status = TLP_COMP_STATUS(reg1);
171 }
Ley Foon Taneaa61112015-10-23 18:27:12 +0800172
173 if (ctrl & RP_RXCPL_EOP) {
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600174 if (comp_status)
175 return PCIBIOS_DEVICE_NOT_FOUND;
176
Ley Foon Taneaa61112015-10-23 18:27:12 +0800177 if (value)
178 *value = reg0;
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600179
Ley Foon Taneaa61112015-10-23 18:27:12 +0800180 return PCIBIOS_SUCCESSFUL;
181 }
182 }
183 udelay(5);
184 }
185
Ley Foon Tanea1d3792015-12-04 16:21:16 -0600186 return PCIBIOS_DEVICE_NOT_FOUND;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800187}
188
189static void tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
190 u32 data, bool align)
191{
192 struct tlp_rp_regpair_t tlp_rp_regdata;
193
194 tlp_rp_regdata.reg0 = headers[0];
195 tlp_rp_regdata.reg1 = headers[1];
196 tlp_rp_regdata.ctrl = RP_TX_SOP;
197 tlp_write_tx(pcie, &tlp_rp_regdata);
198
199 if (align) {
200 tlp_rp_regdata.reg0 = headers[2];
201 tlp_rp_regdata.reg1 = 0;
202 tlp_rp_regdata.ctrl = 0;
203 tlp_write_tx(pcie, &tlp_rp_regdata);
204
205 tlp_rp_regdata.reg0 = data;
206 tlp_rp_regdata.reg1 = 0;
207 } else {
208 tlp_rp_regdata.reg0 = headers[2];
209 tlp_rp_regdata.reg1 = data;
210 }
211
212 tlp_rp_regdata.ctrl = RP_TX_EOP;
213 tlp_write_tx(pcie, &tlp_rp_regdata);
214}
215
216static int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn,
217 int where, u8 byte_en, u32 *value)
218{
219 u32 headers[TLP_HDR_SIZE];
220
221 if (bus == pcie->root_bus_nr)
222 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD0);
223 else
224 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGRD1);
225
Ley Foon Tan23ec5672015-12-04 16:21:12 -0600226 headers[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN),
Ley Foon Taneaa61112015-10-23 18:27:12 +0800227 TLP_READ_TAG, byte_en);
228 headers[2] = TLP_CFG_DW2(bus, devfn, where);
229
230 tlp_write_packet(pcie, headers, 0, false);
231
232 return tlp_read_packet(pcie, value);
233}
234
235static int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn,
236 int where, u8 byte_en, u32 value)
237{
238 u32 headers[TLP_HDR_SIZE];
239 int ret;
240
241 if (bus == pcie->root_bus_nr)
242 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR0);
243 else
244 headers[0] = TLP_CFG_DW0(TLP_FMTTYPE_CFGWR1);
245
Ley Foon Tan23ec5672015-12-04 16:21:12 -0600246 headers[1] = TLP_CFG_DW1(TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN),
Ley Foon Taneaa61112015-10-23 18:27:12 +0800247 TLP_WRITE_TAG, byte_en);
248 headers[2] = TLP_CFG_DW2(bus, devfn, where);
249
250 /* check alignment to Qword */
251 if ((where & 0x7) == 0)
252 tlp_write_packet(pcie, headers, value, true);
253 else
254 tlp_write_packet(pcie, headers, value, false);
255
256 ret = tlp_read_packet(pcie, NULL);
257 if (ret != PCIBIOS_SUCCESSFUL)
258 return ret;
259
260 /*
261 * Monitor changes to PCI_PRIMARY_BUS register on root port
262 * and update local copy of root bus number accordingly.
263 */
264 if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS))
265 pcie->root_bus_nr = (u8)(value);
266
267 return PCIBIOS_SUCCESSFUL;
268}
269
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800270static int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8 busno,
271 unsigned int devfn, int where, int size,
272 u32 *value)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800273{
Ley Foon Taneaa61112015-10-23 18:27:12 +0800274 int ret;
275 u32 data;
276 u8 byte_en;
277
Ley Foon Taneaa61112015-10-23 18:27:12 +0800278 switch (size) {
279 case 1:
280 byte_en = 1 << (where & 3);
281 break;
282 case 2:
283 byte_en = 3 << (where & 3);
284 break;
285 default:
286 byte_en = 0xf;
287 break;
288 }
289
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800290 ret = tlp_cfg_dword_read(pcie, busno, devfn,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800291 (where & ~DWORD_MASK), byte_en, &data);
292 if (ret != PCIBIOS_SUCCESSFUL)
293 return ret;
294
295 switch (size) {
296 case 1:
297 *value = (data >> (8 * (where & 0x3))) & 0xff;
298 break;
299 case 2:
300 *value = (data >> (8 * (where & 0x2))) & 0xffff;
301 break;
302 default:
303 *value = data;
304 break;
305 }
306
307 return PCIBIOS_SUCCESSFUL;
308}
309
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800310static int _altera_pcie_cfg_write(struct altera_pcie *pcie, u8 busno,
311 unsigned int devfn, int where, int size,
312 u32 value)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800313{
Ley Foon Taneaa61112015-10-23 18:27:12 +0800314 u32 data32;
315 u32 shift = 8 * (where & 3);
316 u8 byte_en;
317
Ley Foon Taneaa61112015-10-23 18:27:12 +0800318 switch (size) {
319 case 1:
320 data32 = (value & 0xff) << shift;
321 byte_en = 1 << (where & 3);
322 break;
323 case 2:
324 data32 = (value & 0xffff) << shift;
325 byte_en = 3 << (where & 3);
326 break;
327 default:
328 data32 = value;
329 byte_en = 0xf;
330 break;
331 }
332
Ley Foon Tan31fc0ad2016-08-26 09:47:24 +0800333 return tlp_cfg_dword_write(pcie, busno, devfn, (where & ~DWORD_MASK),
334 byte_en, data32);
335}
336
337static int altera_pcie_cfg_read(struct pci_bus *bus, unsigned int devfn,
338 int where, int size, u32 *value)
339{
340 struct altera_pcie *pcie = bus->sysdata;
341
342 if (altera_pcie_hide_rc_bar(bus, devfn, where))
343 return PCIBIOS_BAD_REGISTER_NUMBER;
344
345 if (!altera_pcie_valid_config(pcie, bus, PCI_SLOT(devfn))) {
346 *value = 0xffffffff;
347 return PCIBIOS_DEVICE_NOT_FOUND;
348 }
349
350 return _altera_pcie_cfg_read(pcie, bus->number, devfn, where, size,
351 value);
352}
353
354static int altera_pcie_cfg_write(struct pci_bus *bus, unsigned int devfn,
355 int where, int size, u32 value)
356{
357 struct altera_pcie *pcie = bus->sysdata;
358
359 if (altera_pcie_hide_rc_bar(bus, devfn, where))
360 return PCIBIOS_BAD_REGISTER_NUMBER;
361
362 if (!altera_pcie_valid_config(pcie, bus, PCI_SLOT(devfn)))
363 return PCIBIOS_DEVICE_NOT_FOUND;
364
365 return _altera_pcie_cfg_write(pcie, bus->number, devfn, where, size,
366 value);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800367}
368
369static struct pci_ops altera_pcie_ops = {
370 .read = altera_pcie_cfg_read,
371 .write = altera_pcie_cfg_write,
372};
373
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800374static int altera_read_cap_word(struct altera_pcie *pcie, u8 busno,
375 unsigned int devfn, int offset, u16 *value)
376{
377 u32 data;
378 int ret;
379
380 ret = _altera_pcie_cfg_read(pcie, busno, devfn,
381 PCIE_CAP_OFFSET + offset, sizeof(*value),
382 &data);
383 *value = data;
384 return ret;
385}
386
387static int altera_write_cap_word(struct altera_pcie *pcie, u8 busno,
388 unsigned int devfn, int offset, u16 value)
389{
390 return _altera_pcie_cfg_write(pcie, busno, devfn,
391 PCIE_CAP_OFFSET + offset, sizeof(value),
392 value);
393}
394
395static void altera_wait_link_retrain(struct altera_pcie *pcie)
396{
397 u16 reg16;
398 unsigned long start_jiffies;
399
400 /* Wait for link training end. */
401 start_jiffies = jiffies;
402 for (;;) {
403 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
404 PCI_EXP_LNKSTA, &reg16);
405 if (!(reg16 & PCI_EXP_LNKSTA_LT))
406 break;
407
408 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) {
409 dev_err(&pcie->pdev->dev, "link retrain timeout\n");
410 break;
411 }
412 udelay(100);
413 }
414
415 /* Wait for link is up */
416 start_jiffies = jiffies;
417 for (;;) {
418 if (altera_pcie_link_is_up(pcie))
419 break;
420
421 if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) {
422 dev_err(&pcie->pdev->dev, "link up timeout\n");
423 break;
424 }
425 udelay(100);
426 }
427}
428
429static void altera_pcie_retrain(struct altera_pcie *pcie)
430{
431 u16 linkcap, linkstat, linkctl;
432
433 if (!altera_pcie_link_is_up(pcie))
434 return;
435
436 /*
437 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
438 * current speed is 2.5 GB/s.
439 */
440 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKCAP,
441 &linkcap);
442 if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB)
443 return;
444
445 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKSTA,
446 &linkstat);
447 if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) {
448 altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
449 PCI_EXP_LNKCTL, &linkctl);
450 linkctl |= PCI_EXP_LNKCTL_RL;
451 altera_write_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
452 PCI_EXP_LNKCTL, linkctl);
453
454 altera_wait_link_retrain(pcie);
455 }
456}
457
Ley Foon Taneaa61112015-10-23 18:27:12 +0800458static int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
459 irq_hw_number_t hwirq)
460{
461 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
462 irq_set_chip_data(irq, domain->host_data);
463
464 return 0;
465}
466
467static const struct irq_domain_ops intx_domain_ops = {
468 .map = altera_pcie_intx_map,
469};
470
471static void altera_pcie_isr(struct irq_desc *desc)
472{
473 struct irq_chip *chip = irq_desc_get_chip(desc);
474 struct altera_pcie *pcie;
475 unsigned long status;
476 u32 bit;
477 u32 virq;
478
479 chained_irq_enter(chip, desc);
480 pcie = irq_desc_get_handler_data(desc);
481
482 while ((status = cra_readl(pcie, P2A_INT_STATUS)
483 & P2A_INT_STS_ALL) != 0) {
484 for_each_set_bit(bit, &status, INTX_NUM) {
485 /* clear interrupts */
486 cra_writel(pcie, 1 << bit, P2A_INT_STATUS);
487
488 virq = irq_find_mapping(pcie->irq_domain, bit + 1);
489 if (virq)
490 generic_handle_irq(virq);
491 else
492 dev_err(&pcie->pdev->dev,
493 "unexpected IRQ, INT%d\n", bit);
494 }
495 }
496
497 chained_irq_exit(chip, desc);
498}
499
Ley Foon Taneaa61112015-10-23 18:27:12 +0800500static int altera_pcie_parse_request_of_pci_ranges(struct altera_pcie *pcie)
501{
502 int err, res_valid = 0;
503 struct device *dev = &pcie->pdev->dev;
504 struct device_node *np = dev->of_node;
505 struct resource_entry *win;
506
507 err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
508 NULL);
509 if (err)
510 return err;
511
Bjorn Helgaas74462282016-05-31 12:14:17 -0500512 err = devm_request_pci_bus_resources(dev, &pcie->resources);
513 if (err)
514 goto out_release_res;
515
Ley Foon Taneaa61112015-10-23 18:27:12 +0800516 resource_list_for_each_entry(win, &pcie->resources) {
Bjorn Helgaas74462282016-05-31 12:14:17 -0500517 struct resource *res = win->res;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800518
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500519 if (resource_type(res) == IORESOURCE_MEM)
Ley Foon Taneaa61112015-10-23 18:27:12 +0800520 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800521 }
522
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500523 if (res_valid)
524 return 0;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800525
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500526 dev_err(dev, "non-prefetchable memory resource required\n");
527 err = -EINVAL;
Ley Foon Taneaa61112015-10-23 18:27:12 +0800528
529out_release_res:
Bjorn Helgaasba4f6d92016-05-28 18:33:46 -0500530 pci_free_resource_list(&pcie->resources);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800531 return err;
532}
533
534static int altera_pcie_init_irq_domain(struct altera_pcie *pcie)
535{
536 struct device *dev = &pcie->pdev->dev;
537 struct device_node *node = dev->of_node;
538
539 /* Setup INTx */
Ley Foon Tan99496bd2015-12-04 16:21:21 -0600540 pcie->irq_domain = irq_domain_add_linear(node, INTX_NUM + 1,
Ley Foon Taneaa61112015-10-23 18:27:12 +0800541 &intx_domain_ops, pcie);
542 if (!pcie->irq_domain) {
543 dev_err(dev, "Failed to get a INTx IRQ domain\n");
544 return -ENOMEM;
545 }
546
547 return 0;
548}
549
550static int altera_pcie_parse_dt(struct altera_pcie *pcie)
551{
552 struct resource *cra;
553 struct platform_device *pdev = pcie->pdev;
554
555 cra = platform_get_resource_byname(pdev, IORESOURCE_MEM, "Cra");
556 if (!cra) {
557 dev_err(&pdev->dev, "no Cra memory resource defined\n");
558 return -ENODEV;
559 }
560
561 pcie->cra_base = devm_ioremap_resource(&pdev->dev, cra);
562 if (IS_ERR(pcie->cra_base)) {
563 dev_err(&pdev->dev, "failed to map cra memory\n");
564 return PTR_ERR(pcie->cra_base);
565 }
566
567 /* setup IRQ */
568 pcie->irq = platform_get_irq(pdev, 0);
569 if (pcie->irq <= 0) {
570 dev_err(&pdev->dev, "failed to get IRQ: %d\n", pcie->irq);
571 return -EINVAL;
572 }
573
574 irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie);
575
576 return 0;
577}
578
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800579static void altera_pcie_host_init(struct altera_pcie *pcie)
580{
581 altera_pcie_retrain(pcie);
582}
583
Ley Foon Taneaa61112015-10-23 18:27:12 +0800584static int altera_pcie_probe(struct platform_device *pdev)
585{
586 struct altera_pcie *pcie;
587 struct pci_bus *bus;
588 struct pci_bus *child;
589 int ret;
590
591 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
592 if (!pcie)
593 return -ENOMEM;
594
595 pcie->pdev = pdev;
596
597 ret = altera_pcie_parse_dt(pcie);
598 if (ret) {
599 dev_err(&pdev->dev, "Parsing DT failed\n");
600 return ret;
601 }
602
603 INIT_LIST_HEAD(&pcie->resources);
604
605 ret = altera_pcie_parse_request_of_pci_ranges(pcie);
606 if (ret) {
607 dev_err(&pdev->dev, "Failed add resources\n");
608 return ret;
609 }
610
611 ret = altera_pcie_init_irq_domain(pcie);
612 if (ret) {
613 dev_err(&pdev->dev, "Failed creating IRQ Domain\n");
614 return ret;
615 }
616
617 /* clear all interrupts */
618 cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
619 /* enable all interrupts */
620 cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE);
Ley Foon Tance4f1c72016-08-26 09:47:25 +0800621 altera_pcie_host_init(pcie);
Ley Foon Taneaa61112015-10-23 18:27:12 +0800622
623 bus = pci_scan_root_bus(&pdev->dev, pcie->root_bus_nr, &altera_pcie_ops,
624 pcie, &pcie->resources);
625 if (!bus)
626 return -ENOMEM;
627
628 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
629 pci_assign_unassigned_bus_resources(bus);
630
631 /* Configure PCI Express setting. */
632 list_for_each_entry(child, &bus->children, node)
633 pcie_bus_configure_settings(child);
634
635 pci_bus_add_devices(bus);
636
637 platform_set_drvdata(pdev, pcie);
638 return ret;
639}
640
641static const struct of_device_id altera_pcie_of_match[] = {
642 { .compatible = "altr,pcie-root-port-1.0", },
643 {},
644};
Ley Foon Taneaa61112015-10-23 18:27:12 +0800645
646static struct platform_driver altera_pcie_driver = {
647 .probe = altera_pcie_probe,
648 .driver = {
649 .name = "altera-pcie",
650 .of_match_table = altera_pcie_of_match,
651 .suppress_bind_attrs = true,
652 },
653};
654
655static int altera_pcie_init(void)
656{
657 return platform_driver_register(&altera_pcie_driver);
658}
Paul Gortmakerbb9b54c2016-08-22 17:59:42 -0400659device_initcall(altera_pcie_init);