blob: 81ef5797944a9744cbf03ccbdf4573c3d2e2ae24 [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 Juhos6335aef2012-03-14 10:29:24 +010022#include <asm/mach-ath79/pci.h>
Gabor Juhos3a6208d2012-03-14 10:29:22 +010023#include "pci.h"
Gabor Juhose2dbdc42012-03-14 10:29:21 +010024
Gabor Juhosa68ad4d2012-03-14 10:36:09 +010025static int (*ath79_pci_plat_dev_init)(struct pci_dev *dev);
Gabor Juhosd22ce252012-03-14 10:36:11 +010026static const struct ath79_pci_irq *ath79_pci_irq_map __initdata;
27static unsigned ath79_pci_nr_irqs __initdata;
Gabor Juhose2dbdc42012-03-14 10:29:21 +010028
Gabor Juhosd22ce252012-03-14 10:36:11 +010029static const struct ath79_pci_irq ar71xx_pci_irq_map[] __initconst = {
30 {
31 .slot = 17,
32 .pin = 1,
33 .irq = ATH79_PCI_IRQ(0),
34 }, {
35 .slot = 18,
36 .pin = 1,
37 .irq = ATH79_PCI_IRQ(1),
38 }, {
39 .slot = 19,
40 .pin = 1,
41 .irq = ATH79_PCI_IRQ(2),
42 }
43};
44
45static const struct ath79_pci_irq ar724x_pci_irq_map[] __initconst = {
46 {
47 .slot = 0,
48 .pin = 1,
49 .irq = ATH79_PCI_IRQ(0),
50 }
51};
52
Gabor Juhose2dbdc42012-03-14 10:29:21 +010053int __init pcibios_map_irq(const struct pci_dev *dev, uint8_t slot, uint8_t pin)
54{
Gabor Juhose2dbdc42012-03-14 10:29:21 +010055 int irq = -1;
Gabor Juhosd22ce252012-03-14 10:36:11 +010056 int i;
Gabor Juhose2dbdc42012-03-14 10:29:21 +010057
Gabor Juhosd22ce252012-03-14 10:36:11 +010058 if (ath79_pci_nr_irqs == 0 ||
59 ath79_pci_irq_map == NULL) {
60 if (soc_is_ar71xx()) {
61 ath79_pci_irq_map = ar71xx_pci_irq_map;
62 ath79_pci_nr_irqs = ARRAY_SIZE(ar71xx_pci_irq_map);
Gabor Juhosec950252012-03-14 10:45:30 +010063 } else if (soc_is_ar724x() ||
64 soc_is_ar9342() ||
65 soc_is_ar9344()) {
Gabor Juhosd22ce252012-03-14 10:36:11 +010066 ath79_pci_irq_map = ar724x_pci_irq_map;
67 ath79_pci_nr_irqs = ARRAY_SIZE(ar724x_pci_irq_map);
68 } else {
69 pr_crit("pci %s: invalid irq map\n",
70 pci_name((struct pci_dev *) dev));
71 return irq;
72 }
73 }
Gabor Juhose2dbdc42012-03-14 10:29:21 +010074
Gabor Juhosd22ce252012-03-14 10:36:11 +010075 for (i = 0; i < ath79_pci_nr_irqs; i++) {
76 const struct ath79_pci_irq *entry;
77
78 entry = &ath79_pci_irq_map[i];
79 if (entry->slot == slot && entry->pin == pin) {
80 irq = entry->irq;
81 break;
82 }
83 }
84
85 if (irq < 0)
86 pr_crit("pci %s: no irq found for pin %u\n",
87 pci_name((struct pci_dev *) dev), pin);
88 else
89 pr_info("pci %s: using irq %d for pin %u\n",
90 pci_name((struct pci_dev *) dev), irq, pin);
Gabor Juhose2dbdc42012-03-14 10:29:21 +010091
92 return irq;
93}
94
95int pcibios_plat_dev_init(struct pci_dev *dev)
96{
Gabor Juhosa68ad4d2012-03-14 10:36:09 +010097 if (ath79_pci_plat_dev_init)
98 return ath79_pci_plat_dev_init(dev);
Gabor Juhose2dbdc42012-03-14 10:29:21 +010099
Gabor Juhosa68ad4d2012-03-14 10:36:09 +0100100 return 0;
101}
Gabor Juhose2dbdc42012-03-14 10:29:21 +0100102
Gabor Juhosd22ce252012-03-14 10:36:11 +0100103void __init ath79_pci_set_irq_map(unsigned nr_irqs,
104 const struct ath79_pci_irq *map)
105{
106 ath79_pci_nr_irqs = nr_irqs;
107 ath79_pci_irq_map = map;
108}
109
Gabor Juhosa68ad4d2012-03-14 10:36:09 +0100110void __init ath79_pci_set_plat_dev_init(int (*func)(struct pci_dev *dev))
111{
112 ath79_pci_plat_dev_init = func;
Gabor Juhose2dbdc42012-03-14 10:29:21 +0100113}
Gabor Juhos6335aef2012-03-14 10:29:24 +0100114
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000115static struct platform_device *
116ath79_register_pci_ar71xx(void)
117{
118 struct platform_device *pdev;
119 struct resource res[2];
120
121 memset(res, 0, sizeof(res));
122
123 res[0].name = "cfg_base";
124 res[0].flags = IORESOURCE_MEM;
125 res[0].start = AR71XX_PCI_CFG_BASE;
126 res[0].end = AR71XX_PCI_CFG_BASE + AR71XX_PCI_CFG_SIZE - 1;
127
128 res[1].flags = IORESOURCE_IRQ;
129 res[1].start = ATH79_CPU_IRQ_IP2;
130 res[1].end = ATH79_CPU_IRQ_IP2;
131
132 pdev = platform_device_register_simple("ar71xx-pci", -1,
133 res, ARRAY_SIZE(res));
134 return pdev;
135}
136
137static struct platform_device *
138ath79_register_pci_ar724x(int id,
139 unsigned long cfg_base,
140 unsigned long ctrl_base,
141 int irq)
142{
143 struct platform_device *pdev;
144 struct resource res[3];
145
146 memset(res, 0, sizeof(res));
147
148 res[0].name = "cfg_base";
149 res[0].flags = IORESOURCE_MEM;
150 res[0].start = cfg_base;
151 res[0].end = cfg_base + AR724X_PCI_CFG_SIZE - 1;
152
153 res[1].name = "ctrl_base";
154 res[1].flags = IORESOURCE_MEM;
155 res[1].start = ctrl_base;
156 res[1].end = ctrl_base + AR724X_PCI_CTRL_SIZE - 1;
157
158 res[2].flags = IORESOURCE_IRQ;
159 res[2].start = irq;
160 res[2].end = irq;
161
162 pdev = platform_device_register_simple("ar724x-pci", id,
163 res, ARRAY_SIZE(res));
164 return pdev;
165}
166
Gabor Juhos6335aef2012-03-14 10:29:24 +0100167int __init ath79_register_pci(void)
168{
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000169 struct platform_device *pdev = NULL;
Gabor Juhosd22ce252012-03-14 10:36:11 +0100170
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000171 if (soc_is_ar71xx()) {
172 pdev = ath79_register_pci_ar71xx();
173 } else if (soc_is_ar724x()) {
174 pdev = ath79_register_pci_ar724x(-1,
175 AR724X_PCI_CFG_BASE,
176 AR724X_PCI_CTRL_BASE,
177 ATH79_CPU_IRQ_IP2);
178 } else if (soc_is_ar9342() ||
179 soc_is_ar9344()) {
Gabor Juhosec950252012-03-14 10:45:30 +0100180 u32 bootstrap;
181
182 bootstrap = ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP);
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000183 if ((bootstrap & AR934X_BOOTSTRAP_PCIE_RC) == 0)
184 return -ENODEV;
185
186 pdev = ath79_register_pci_ar724x(-1,
187 AR724X_PCI_CFG_BASE,
188 AR724X_PCI_CTRL_BASE,
189 ATH79_IP2_IRQ(0));
190 } else {
191 /* No PCI support */
192 return -ENODEV;
Gabor Juhosec950252012-03-14 10:45:30 +0100193 }
194
Gabor Juhos9fc1ca52013-02-02 11:44:24 +0000195 if (!pdev)
196 pr_err("unable to register PCI controller device\n");
197
198 return pdev ? 0 : -ENODEV;
Gabor Juhos6335aef2012-03-14 10:29:24 +0100199}