1 --- a/arch/mips/ar231x/Makefile
2 +++ b/arch/mips/ar231x/Makefile
3 @@ -14,3 +14,4 @@ obj-$(CONFIG_EARLY_PRINTK) += early_prin
5 obj-$(CONFIG_ATHEROS_AR5312) += ar5312.o
6 obj-$(CONFIG_ATHEROS_AR2315) += ar2315.o
7 +obj-$(CONFIG_ATHEROS_AR2315_PCI) += pci.o
9 +++ b/arch/mips/ar231x/pci.c
12 + * This program is free software; you can redistribute it and/or
13 + * modify it under the terms of the GNU General Public License
14 + * as published by the Free Software Foundation; either version 2
15 + * of the License, or (at your option) any later version.
17 + * This program is distributed in the hope that it will be useful,
18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 + * GNU General Public License for more details.
22 + * You should have received a copy of the GNU General Public License
23 + * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 + * Both AR2315 and AR2316 chips have PCI interface unit, which supports DMA
28 + * and interrupt. PCI interface supports MMIO access method, but does not
29 + * seem to support I/O ports.
31 + * Read/write operation in the region 0x80000000-0xBFFFFFFF causes
32 + * a memory read/write command on the PCI bus. 30 LSBs of address on
33 + * the bus are taken from memory read/write request and 2 MSBs are
34 + * determined by PCI unit configuration.
36 + * To work with the configuration space instead of memory is necessary set
37 + * the CFG_SEL bit in the PCI_MISC_CONFIG register.
39 + * Devices on the bus can perform DMA requests via chip BAR1. PCI host
40 + * controller BARs are programmend as if an external device is programmed.
41 + * Which means that during configuration, IDSEL pin of the chip should be
44 + * We know (and support) only one board that uses the PCI interface -
45 + * Fonera 2.0g (FON2202). It has a USB EHCI controller connected to the
46 + * AR2315 PCI bus. IDSEL pin of USB controller is connected to AD[13] line
47 + * and IDSEL pin of AR125 is connected to AD[16] line.
50 +#include <linux/types.h>
51 +#include <linux/pci.h>
52 +#include <linux/kernel.h>
53 +#include <linux/init.h>
54 +#include <linux/mm.h>
55 +#include <linux/delay.h>
56 +#include <linux/irq.h>
57 +#include <linux/io.h>
58 +#include <asm/paccess.h>
59 +#include <ar231x_platform.h>
61 +#include <ar2315_regs.h>
64 +/* Arbitrary size of memory region to access the configuration space */
65 +#define AR2315_PCI_CFG_SIZE 0x00100000
67 +#define AR2315_PCI_HOST_SLOT 3
68 +#define AR2315_PCI_HOST_DEVID ((0xff18 << 16) | PCI_VENDOR_ID_ATHEROS)
70 +static void __iomem *ar2315_pci_cfg_mem;
72 +static int ar2315_pci_cfg_access(int devfn, int where, int size, u32 *ptr,
75 + int func = PCI_FUNC(devfn);
76 + int dev = PCI_SLOT(devfn);
77 + u32 addr = (1 << (13 + dev)) | (func << 8) | (where & ~3);
78 + u32 mask = 0xffffffff >> 8 * (4 - size);
79 + u32 sh = (where & 3) * 8;
82 + /* Prevent access past the remapped area */
83 + if (addr >= AR2315_PCI_CFG_SIZE || dev > 18)
84 + return PCIBIOS_DEVICE_NOT_FOUND;
86 + /* Clear pending errors */
87 + ar231x_write_reg(AR2315_PCI_ISR, AR2315_PCI_INT_ABORT);
88 + /* Select Configuration access */
89 + ar231x_mask_reg(AR2315_PCI_MISC_CONFIG, 0, AR2315_PCIMISC_CFG_SEL);
91 + mb(); /* PCI must see space change before we begin */
93 + value = __raw_readl(ar2315_pci_cfg_mem + addr);
95 + isr = ar231x_read_reg(AR2315_PCI_ISR);
96 + if (isr & AR2315_PCI_INT_ABORT)
100 + value = (value & ~(mask << sh)) | *ptr << sh;
101 + __raw_writel(value, ar2315_pci_cfg_mem + addr);
102 + isr = ar231x_read_reg(AR2315_PCI_ISR);
103 + if (isr & AR2315_PCI_INT_ABORT)
106 + *ptr = (value >> sh) & mask;
112 + ar231x_write_reg(AR2315_PCI_ISR, AR2315_PCI_INT_ABORT);
117 + /* Select Memory access */
118 + ar231x_mask_reg(AR2315_PCI_MISC_CONFIG, AR2315_PCIMISC_CFG_SEL, 0);
120 + return isr & AR2315_PCI_INT_ABORT ? PCIBIOS_DEVICE_NOT_FOUND :
121 + PCIBIOS_SUCCESSFUL;
124 +static inline int ar2315_pci_local_cfg_rd(unsigned devfn, int where, u32 *val)
126 + return ar2315_pci_cfg_access(devfn, where, sizeof(u32), val, false);
129 +static inline int ar2315_pci_local_cfg_wr(unsigned devfn, int where, u32 val)
131 + return ar2315_pci_cfg_access(devfn, where, sizeof(u32), &val, true);
134 +static int ar2315_pci_cfg_read(struct pci_bus *bus, unsigned int devfn,
135 + int where, int size, u32 *value)
137 + if (PCI_SLOT(devfn) == AR2315_PCI_HOST_SLOT)
138 + return PCIBIOS_DEVICE_NOT_FOUND;
140 + return ar2315_pci_cfg_access(devfn, where, size, value, 0);
143 +static int ar2315_pci_cfg_write(struct pci_bus *bus, unsigned int devfn,
144 + int where, int size, u32 value)
146 + if (PCI_SLOT(devfn) == AR2315_PCI_HOST_SLOT)
147 + return PCIBIOS_DEVICE_NOT_FOUND;
149 + return ar2315_pci_cfg_access(devfn, where, size, &value, 1);
152 +static struct pci_ops ar2315_pci_ops = {
153 + .read = ar2315_pci_cfg_read,
154 + .write = ar2315_pci_cfg_write,
157 +static struct resource ar2315_mem_resource = {
158 + .name = "ar2315-pci-mem",
159 + .start = AR2315_PCIEXT,
160 + .end = AR2315_PCIEXT + AR2315_PCIEXT_SZ - 1,
161 + .flags = IORESOURCE_MEM,
164 +/* PCI controller does not support I/O ports */
165 +static struct resource ar2315_io_resource = {
166 + .name = "ar2315-pci-io",
169 + .flags = IORESOURCE_IO,
172 +static struct pci_controller ar2315_pci_controller = {
173 + .pci_ops = &ar2315_pci_ops,
174 + .mem_resource = &ar2315_mem_resource,
175 + .io_resource = &ar2315_io_resource,
176 + .mem_offset = 0x00000000UL,
177 + .io_offset = 0x00000000UL,
180 +int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
182 + return AR2315_PCI_IRQ_EXT;
185 +int pcibios_plat_dev_init(struct pci_dev *dev)
190 +static int ar2315_pci_host_setup(void)
192 + unsigned devfn = PCI_DEVFN(AR2315_PCI_HOST_SLOT, 0);
196 + res = ar2315_pci_local_cfg_rd(devfn, PCI_VENDOR_ID, &id);
197 + if (res != PCIBIOS_SUCCESSFUL || id != AR2315_PCI_HOST_DEVID)
200 + /* Program MBARs */
201 + ar2315_pci_local_cfg_wr(devfn, PCI_BASE_ADDRESS_0, HOST_PCI_MBAR0);
202 + ar2315_pci_local_cfg_wr(devfn, PCI_BASE_ADDRESS_1, HOST_PCI_MBAR1);
203 + ar2315_pci_local_cfg_wr(devfn, PCI_BASE_ADDRESS_2, HOST_PCI_MBAR2);
206 + ar2315_pci_local_cfg_wr(devfn, PCI_COMMAND, PCI_COMMAND_MEMORY |
207 + PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL |
208 + PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY |
209 + PCI_COMMAND_SERR | PCI_COMMAND_FAST_BACK);
214 +static void ar2315_pci_irq_handler(unsigned irq, struct irq_desc *desc)
216 + u32 pending = ar231x_read_reg(AR2315_PCI_ISR) &
217 + ar231x_read_reg(AR2315_PCI_IMR);
219 + if (pending & AR2315_PCI_INT_EXT)
220 + generic_handle_irq(AR2315_PCI_IRQ_EXT);
221 + else if (pending & AR2315_PCI_INT_ABORT)
222 + generic_handle_irq(AR2315_PCI_IRQ_ABORT);
224 + spurious_interrupt();
227 +static void ar2315_pci_irq_mask(struct irq_data *d)
229 + u32 m = 1 << (d->irq - AR2315_PCI_IRQ_BASE + AR2315_PCI_IRQ_SHIFT);
231 + ar231x_mask_reg(AR2315_PCI_IMR, m, 0);
234 +static void ar2315_pci_irq_mask_ack(struct irq_data *d)
236 + u32 m = 1 << (d->irq - AR2315_PCI_IRQ_BASE + AR2315_PCI_IRQ_SHIFT);
238 + ar231x_mask_reg(AR2315_PCI_IMR, m, 0);
239 + ar231x_write_reg(AR2315_PCI_ISR, m);
242 +static void ar2315_pci_irq_unmask(struct irq_data *d)
244 + u32 m = 1 << (d->irq - AR2315_PCI_IRQ_BASE + AR2315_PCI_IRQ_SHIFT);
246 + ar231x_mask_reg(AR2315_PCI_IMR, 0, m);
249 +static struct irq_chip ar2315_pci_irq_chip = {
250 + .name = "AR2315-PCI",
251 + .irq_mask = ar2315_pci_irq_mask,
252 + .irq_mask_ack = ar2315_pci_irq_mask_ack,
253 + .irq_unmask = ar2315_pci_irq_unmask,
256 +static void ar2315_pci_irq_init(void)
260 + ar231x_mask_reg(AR2315_PCI_IER, AR2315_PCI_IER_ENABLE, 0);
261 + ar231x_mask_reg(AR2315_PCI_IMR, (AR2315_PCI_INT_ABORT |
262 + AR2315_PCI_INT_EXT), 0);
264 + for (i = 0; i < AR2315_PCI_IRQ_COUNT; ++i) {
265 + int irq = AR2315_PCI_IRQ_BASE + i;
267 + irq_set_chip_and_handler(irq, &ar2315_pci_irq_chip,
271 + irq_set_chained_handler(AR2315_IRQ_LCBUS_PCI, ar2315_pci_irq_handler);
273 + /* Clear any pending Abort or external Interrupts
274 + * and enable interrupt processing */
275 + ar231x_write_reg(AR2315_PCI_ISR, (AR2315_PCI_INT_ABORT |
276 + AR2315_PCI_INT_EXT));
277 + ar231x_mask_reg(AR2315_PCI_IER, 0, AR2315_PCI_IER_ENABLE);
281 +ar2315_pci_init(void)
286 + if (ar231x_devtype != DEV_TYPE_AR2315)
289 + /* Remap PCI config space */
290 + ar2315_pci_cfg_mem = ioremap_nocache(AR2315_PCIEXT,
291 + AR2315_PCI_CFG_SIZE);
292 + if (!ar2315_pci_cfg_mem) {
293 + pr_err("ar2315-pci: failed to remap PCI config space\n");
297 + /* Reset PCI DMA logic */
298 + reg = ar231x_mask_reg(AR2315_RESET, 0, AR2315_RESET_PCIDMA);
300 + reg &= ~AR2315_RESET_PCIDMA;
301 + ar231x_write_reg(AR2315_RESET, reg);
304 + ar231x_mask_reg(AR2315_ENDIAN_CTL, 0,
305 + AR2315_CONFIG_PCIAHB | AR2315_CONFIG_PCIAHB_BRIDGE);
307 + ar231x_write_reg(AR2315_PCICLK, AR2315_PCICLK_PLLC_CLKM |
308 + (AR2315_PCICLK_IN_FREQ_DIV_6 << AR2315_PCICLK_DIV_S));
309 + ar231x_mask_reg(AR2315_AHB_ARB_CTL, 0, AR2315_ARB_PCI);
310 + ar231x_mask_reg(AR2315_IF_CTL, AR2315_IF_PCI_CLK_MASK | AR2315_IF_MASK,
311 + AR2315_IF_PCI | AR2315_IF_PCI_HOST |
312 + AR2315_IF_PCI_INTR | (AR2315_IF_PCI_CLK_OUTPUT_CLK <<
313 + AR2315_IF_PCI_CLK_SHIFT));
315 + /* Reset the PCI bus by setting bits 5-4 in PCI_MCFG */
316 + ar231x_mask_reg(AR2315_PCI_MISC_CONFIG, AR2315_PCIMISC_RST_MODE,
317 + AR2315_PCIRST_LOW);
320 + /* Bring the PCI out of reset */
321 + ar231x_mask_reg(AR2315_PCI_MISC_CONFIG, AR2315_PCIMISC_RST_MODE,
322 + AR2315_PCIRST_HIGH | AR2315_PCICACHE_DIS | 0x8);
324 + ar231x_write_reg(AR2315_PCI_UNCACHE_CFG,
325 + 0x1E | /* 1GB uncached */
326 + (1 << 5) | /* Enable uncached */
327 + (0x2 << 30) /* Base: 0x80000000 */);
328 + ar231x_read_reg(AR2315_PCI_UNCACHE_CFG);
332 + res = ar2315_pci_host_setup();
336 + ar2315_pci_irq_init();
338 + register_pci_controller(&ar2315_pci_controller);
343 + iounmap(ar2315_pci_cfg_mem);
347 +arch_initcall(ar2315_pci_init);
348 --- a/arch/mips/ar231x/Kconfig
349 +++ b/arch/mips/ar231x/Kconfig
350 @@ -7,3 +7,10 @@ config ATHEROS_AR2315
351 bool "Atheros 2315+ support"
352 depends on ATHEROS_AR231X
355 +config ATHEROS_AR2315_PCI
357 + depends on ATHEROS_AR2315
361 --- a/arch/mips/ar231x/ar2315.c
362 +++ b/arch/mips/ar231x/ar2315.c
363 @@ -104,6 +104,10 @@ ar2315_irq_dispatch(void)
364 do_IRQ(AR2315_IRQ_WLAN0_INTRS);
365 else if (pending & CAUSEF_IP4)
366 do_IRQ(AR2315_IRQ_ENET0_INTRS);
367 +#ifdef CONFIG_ATHEROS_AR2315_PCI
368 + else if (pending & CAUSEF_IP5)
369 + do_IRQ(AR2315_IRQ_LCBUS_PCI);
371 else if (pending & CAUSEF_IP2)
372 do_IRQ(AR2315_IRQ_MISC_INTRS);
373 else if (pending & CAUSEF_IP7)