blob: 942e3f9184f0373f0234418b8491d135349746d5 [file] [log] [blame]
Gabor Juhose2dbdc42012-03-14 10:29:21 +01001/*
2 * Atheros AR71XX/AR724X specific PCI setup code
3 *
4 * Copyright (C) 2011 René Bolldorf <xsecute@googlemail.com>
Gabor Juhose9b62e82012-03-14 10:36:14 +01005 * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
6 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 *
8 * Parts of this file are based on Atheros' 2.6.15 BSP
Gabor Juhose2dbdc42012-03-14 10:29:21 +01009 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
13 */
14
Gabor Juhosd22ce252012-03-14 10:36:11 +010015#include <linux/init.h>
Gabor Juhose2dbdc42012-03-14 10:29:21 +010016#include <linux/pci.h>
Gabor Juhos9fc1ca52013-02-02 11:44:24 +000017#include <linux/resource.h>
18#include <linux/platform_device.h>
Gabor Juhosec950252012-03-14 10:45:30 +010019#include <asm/mach-ath79/ar71xx_regs.h>
Gabor Juhos6335aef2012-03-14 10:29:24 +010020#include <asm/mach-ath79/ath79.h>
Gabor Juhos4c07c7d2012-03-14 10:36:07 +010021#include <asm/mach-ath79/irq.h>
Gabor Juhos3a6208d2012-03-14 10:29:22 +010022#include "pci.h"
Gabor Juhose2dbdc42012-03-14 10:29:21 +010023
Gabor Juhosa68ad4d2012-03-14 10:36:09 +010024static int (*ath79_pci_plat_dev_init)(struct pci_dev *dev);
Gabor Juhosd22ce252012-03-14 10:36:11 +010025static const struct ath79_pci_irq *ath79_pci_irq_map __initdata;
26static unsigned ath79_pci_nr_irqs __initdata;
Gabor Juhose2dbdc42012-03-14 10:29:21 +010027
Gabor Juhosd22ce252012-03-14 10:36:11 +010028static const struct ath79_pci_irq ar71xx_pci_irq_map[] __initconst = {
29 {
30 .slot = 17,
31 .pin = 1,
32 .irq = ATH79_PCI_IRQ(0),
33 }, {
34 .slot = 18,
35 .pin = 1,
36 .irq = ATH79_PCI_IRQ(1),
37 }, {
38 .slot = 19,
39 .pin = 1,
40 .irq = ATH79_PCI_IRQ(2),
41 }
42};
43
44static const struct ath79_pci_irq ar724x_pci_irq_map[] __initconst = {
45 {
46 .slot = 0,
47 .pin = 1,
48 .irq = ATH79_PCI_IRQ(0),
49 }
50};
51
Gabor Juhose2dbdc42012-03-14 10:29:21 +010052int __init pcibios_map_irq(const struct pci_dev *dev, uint8_t slot, uint8_t pin)
53{
Gabor Juhose2dbdc42012-03-14 10:29:21 +010054 int irq = -1;
Gabor Juhosd22ce252012-03-14 10:36:11 +010055 int i;
Gabor Juhose2dbdc42012-03-14 10:29:21 +010056
Gabor Juhosd22ce252012-03-14 10:36:11 +010057 if (ath79_pci_nr_irqs == 0 ||
58 ath79_pci_irq_map == NULL) {
59 if (soc_is_ar71xx()) {
60 ath79_pci_irq_map = ar71xx_pci_irq_map;
61 ath79_pci_nr_irqs = ARRAY_SIZE(ar71xx_pci_irq_map);
Gabor Juhosec950252012-03-14 10:45:30 +010062 } else if (soc_is_ar724x() ||
63 soc_is_ar9342() ||
64 soc_is_ar9344()) {
Gabor Juhosd22ce252012-03-14 10:36:11 +010065 ath79_pci_irq_map = ar724x_pci_irq_map;
66 ath79_pci_nr_irqs = ARRAY_SIZE(ar724x_pci_irq_map);
67 } else {
68 pr_crit("pci %s: invalid irq map\n",
69 pci_name((struct pci_dev *) dev));
70 return irq;
71 }
72 }
Gabor Juhose2dbdc42012-03-14 10:29:21 +010073
Gabor Juhosd22ce252012-03-14 10:36:11 +010074 for (i = 0; i < ath79_pci_nr_irqs; i++) {
75 const struct ath79_pci_irq *entry;
76
77 entry = &ath79_pci_irq_map[i];
Gabor Juhos617fed42013-02-03 09:58:37 +000078 if (entry->bus == dev->bus->number &&
79 entry->slot == slot &&
80 entry->pin == pin) {
Gabor Juhosd22ce252012-03-14 10:36:11 +010081 irq = entry->irq;
82 break;
83 }
84 }
85
86 if (irq < 0)
87 pr_crit("pci %s: no irq found for pin %u\n",
88 pci_name((struct pci_dev *) dev), pin);
89 else
90 pr_info("pci %s: using irq %d for pin %u\n",
91 pci_name((struct pci_dev *) dev), irq, pin);
Gabor Juhose2dbdc42012-03-14 10:29:21 +010092
93 return irq;
94}
95
96int pcibios_plat_dev_init(struct pci_dev *dev)
97{
Gabor Juhosa68ad4d2012-03-14 10:36:09 +010098 if (ath79_pci_plat_dev_init)
99 return ath79_pci_plat_dev_init(dev);
Gabor Juhose2dbdc42012-03-14 10:29:21 +0100100
Gabor Juhosa68ad4d2012-03-14 10:36:09 +0100101 return 0;
102}
Gabor Juhose2dbdc42012-03-14 10:29:21 +0100103
Gabor Juhosd22ce252012-03-14 10:36:11 +0100104void __init ath79_pci_set_irq_map(unsigned nr_irqs,
105 const struct ath79_pci_irq *map)
106{
107 ath79_pci_nr_irqs = nr_irqs;
108 ath79_pci_irq_map = map;
109}
110
Gabor Juhosa68ad4d2012-03-14 10:36:09 +0100111void __init ath79_pci_set_plat_dev_init(int (*func)(struct pci_dev *dev))
112{
113 ath79_pci_plat_dev_init = func;
Gabor Juhose2dbdc42012-03-14 10:29:21 +0100114}
Gabor Juhos6335aef2012-03-14 10:29:24 +0100115
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000116static struct platform_device *
117ath79_register_pci_ar71xx(void)
118{
119 struct platform_device *pdev;
120 struct resource res[2];
121
122 memset(res, 0, sizeof(res));
123
124 res[0].name = "cfg_base";
125 res[0].flags = IORESOURCE_MEM;
126 res[0].start = AR71XX_PCI_CFG_BASE;
127 res[0].end = AR71XX_PCI_CFG_BASE + AR71XX_PCI_CFG_SIZE - 1;
128
129 res[1].flags = IORESOURCE_IRQ;
130 res[1].start = ATH79_CPU_IRQ_IP2;
131 res[1].end = ATH79_CPU_IRQ_IP2;
132
133 pdev = platform_device_register_simple("ar71xx-pci", -1,
134 res, ARRAY_SIZE(res));
135 return pdev;
136}
137
138static struct platform_device *
139ath79_register_pci_ar724x(int id,
140 unsigned long cfg_base,
141 unsigned long ctrl_base,
Gabor Juhos12401fc2013-02-03 14:52:47 +0000142 unsigned long crp_base,
Gabor Juhos34b134a2013-02-03 09:59:45 +0000143 unsigned long mem_base,
144 unsigned long mem_size,
145 unsigned long io_base,
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000146 int irq)
147{
148 struct platform_device *pdev;
Gabor Juhos12401fc2013-02-03 14:52:47 +0000149 struct resource res[6];
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000150
151 memset(res, 0, sizeof(res));
152
153 res[0].name = "cfg_base";
154 res[0].flags = IORESOURCE_MEM;
155 res[0].start = cfg_base;
156 res[0].end = cfg_base + AR724X_PCI_CFG_SIZE - 1;
157
158 res[1].name = "ctrl_base";
159 res[1].flags = IORESOURCE_MEM;
160 res[1].start = ctrl_base;
161 res[1].end = ctrl_base + AR724X_PCI_CTRL_SIZE - 1;
162
163 res[2].flags = IORESOURCE_IRQ;
164 res[2].start = irq;
165 res[2].end = irq;
166
Gabor Juhos34b134a2013-02-03 09:59:45 +0000167 res[3].name = "mem_base";
168 res[3].flags = IORESOURCE_MEM;
169 res[3].start = mem_base;
170 res[3].end = mem_base + mem_size - 1;
171
172 res[4].name = "io_base";
173 res[4].flags = IORESOURCE_IO;
174 res[4].start = io_base;
175 res[4].end = io_base;
176
Gabor Juhos12401fc2013-02-03 14:52:47 +0000177 res[5].name = "crp_base";
178 res[5].flags = IORESOURCE_MEM;
179 res[5].start = crp_base;
180 res[5].end = crp_base + AR724X_PCI_CRP_SIZE - 1;
181
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000182 pdev = platform_device_register_simple("ar724x-pci", id,
183 res, ARRAY_SIZE(res));
184 return pdev;
185}
186
Gabor Juhos6335aef2012-03-14 10:29:24 +0100187int __init ath79_register_pci(void)
188{
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000189 struct platform_device *pdev = NULL;
Gabor Juhosd22ce252012-03-14 10:36:11 +0100190
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000191 if (soc_is_ar71xx()) {
192 pdev = ath79_register_pci_ar71xx();
193 } else if (soc_is_ar724x()) {
194 pdev = ath79_register_pci_ar724x(-1,
195 AR724X_PCI_CFG_BASE,
196 AR724X_PCI_CTRL_BASE,
Gabor Juhos12401fc2013-02-03 14:52:47 +0000197 AR724X_PCI_CRP_BASE,
Gabor Juhos34b134a2013-02-03 09:59:45 +0000198 AR724X_PCI_MEM_BASE,
199 AR724X_PCI_MEM_SIZE,
200 0,
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000201 ATH79_CPU_IRQ_IP2);
202 } else if (soc_is_ar9342() ||
203 soc_is_ar9344()) {
Gabor Juhosec950252012-03-14 10:45:30 +0100204 u32 bootstrap;
205
206 bootstrap = ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP);
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000207 if ((bootstrap & AR934X_BOOTSTRAP_PCIE_RC) == 0)
208 return -ENODEV;
209
210 pdev = ath79_register_pci_ar724x(-1,
211 AR724X_PCI_CFG_BASE,
212 AR724X_PCI_CTRL_BASE,
Gabor Juhos12401fc2013-02-03 14:52:47 +0000213 AR724X_PCI_CRP_BASE,
Gabor Juhos34b134a2013-02-03 09:59:45 +0000214 AR724X_PCI_MEM_BASE,
215 AR724X_PCI_MEM_SIZE,
216 0,
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000217 ATH79_IP2_IRQ(0));
218 } else {
219 /* No PCI support */
220 return -ENODEV;
Gabor Juhosec950252012-03-14 10:45:30 +0100221 }
222
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000223 if (!pdev)
224 pr_err("unable to register PCI controller device\n");
225
226 return pdev ? 0 : -ENODEV;
Gabor Juhos6335aef2012-03-14 10:29:24 +0100227}