2 * Atheros AR71xx PCI host controller driver
4 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 * Parts of this file are based on Atheros' 2.6.15 BSP
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
14 #include <linux/resource.h>
15 #include <linux/types.h>
16 #include <linux/delay.h>
17 #include <linux/bitops.h>
18 #include <linux/pci.h>
19 #include <linux/pci_regs.h>
20 #include <linux/interrupt.h>
22 #include <asm/mach-ar71xx/ar71xx.h>
23 #include <asm/mach-ar71xx/pci.h>
27 #define DBG(fmt, args...) printk(KERN_DEBUG fmt, ## args)
29 #define DBG(fmt, args...)
32 #define AR71XX_PCI_DELAY 100 /* msecs */
35 #define PCI_IDSEL_BASE PCI_IDSEL_ADL_START
37 #define PCI_IDSEL_BASE 0
40 static void __iomem
*ar71xx_pcicfg_base
;
41 static DEFINE_SPINLOCK(ar71xx_pci_lock
);
42 static int ar71xx_pci_fixup_enable
;
44 static inline void ar71xx_pci_delay(void)
46 mdelay(AR71XX_PCI_DELAY
);
49 /* Byte lane enable bits */
50 static u8 ble_table
[4][4] = {
57 static inline u32
ar71xx_pci_get_ble(int where
, int size
, int local
)
61 t
= ble_table
[size
& 3][where
& 3];
63 t
<<= (local
) ? 20 : 4;
67 static inline u32
ar71xx_pci_bus_addr(struct pci_bus
*bus
, unsigned int devfn
,
74 ret
= (1 << (PCI_IDSEL_BASE
+ PCI_SLOT(devfn
)))
75 | (PCI_FUNC(devfn
) << 8) | (where
& ~3);
78 ret
= (bus
->number
<< 16) | (PCI_SLOT(devfn
) << 11)
79 | (PCI_FUNC(devfn
) << 8) | (where
& ~3) | 1;
85 int ar71xx_pci_be_handler(int is_fixup
)
87 void __iomem
*base
= ar71xx_pcicfg_base
;
91 pci_err
= __raw_readl(base
+ PCI_REG_PCI_ERR
) & 3;
94 printk(KERN_ALERT
"PCI error %d at PCI addr 0x%x\n",
96 __raw_readl(base
+ PCI_REG_PCI_ERR_ADDR
));
98 __raw_writel(pci_err
, base
+ PCI_REG_PCI_ERR
);
101 ahb_err
= __raw_readl(base
+ PCI_REG_AHB_ERR
) & 1;
104 printk(KERN_ALERT
"AHB error at AHB address 0x%x\n",
105 __raw_readl(base
+ PCI_REG_AHB_ERR_ADDR
));
107 __raw_writel(ahb_err
, base
+ PCI_REG_AHB_ERR
);
110 return (ahb_err
| pci_err
) ? 1 : 0;
113 static inline int ar71xx_pci_set_cfgaddr(struct pci_bus
*bus
,
114 unsigned int devfn
, int where
, int size
, u32 cmd
)
116 void __iomem
*base
= ar71xx_pcicfg_base
;
119 addr
= ar71xx_pci_bus_addr(bus
, devfn
, where
);
121 DBG("PCI: set cfgaddr: %02x:%02x.%01x/%02x:%01d, addr=%08x\n",
122 bus
->number
, PCI_SLOT(devfn
), PCI_FUNC(devfn
),
125 __raw_writel(addr
, base
+ PCI_REG_CFG_AD
);
126 __raw_writel(cmd
| ar71xx_pci_get_ble(where
, size
, 0),
127 base
+ PCI_REG_CFG_CBE
);
129 return ar71xx_pci_be_handler(1);
132 static int ar71xx_pci_read_config(struct pci_bus
*bus
, unsigned int devfn
,
133 int where
, int size
, u32
*value
)
135 void __iomem
*base
= ar71xx_pcicfg_base
;
136 static u32 mask
[8] = {0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0};
142 ret
= PCIBIOS_SUCCESSFUL
;
144 DBG("PCI: read config: %02x:%02x.%01x/%02x:%01d\n", bus
->number
,
145 PCI_SLOT(devfn
), PCI_FUNC(devfn
), where
, size
);
148 spin_lock_irqsave(&ar71xx_pci_lock
, flags
);
150 if (bus
->number
== 0 && devfn
== 0) {
153 t
= PCI_CRP_CMD_READ
| (where
& ~3);
155 __raw_writel(t
, base
+ PCI_REG_CRP_AD_CBE
);
156 data
= __raw_readl(base
+ PCI_REG_CRP_RDDATA
);
158 DBG("PCI: rd local cfg, ad_cbe:%08x, data:%08x\n", t
, data
);
163 err
= ar71xx_pci_set_cfgaddr(bus
, devfn
, where
, size
,
167 data
= __raw_readl(base
+ PCI_REG_CFG_RDDATA
);
169 ret
= PCIBIOS_DEVICE_NOT_FOUND
;
174 spin_unlock_irqrestore(&ar71xx_pci_lock
, flags
);
176 DBG("PCI: read config: data=%08x raw=%08x\n",
177 (data
>> (8 * (where
& 3))) & mask
[size
& 7], data
);
179 *value
= (data
>> (8 * (where
& 3))) & mask
[size
& 7];
182 * PCI controller bug: sometimes reads to the PCI_COMMAND register
183 * return 0xffff, even though the PCI trace shows the correct value.
184 * Work around this by retrying reads to this register
186 if (where
== PCI_COMMAND
&& (*value
& 0xffff) == 0xffff && retry
++ < 2)
192 static int ar71xx_pci_write_config(struct pci_bus
*bus
, unsigned int devfn
,
193 int where
, int size
, u32 value
)
195 void __iomem
*base
= ar71xx_pcicfg_base
;
199 DBG("PCI: write config: %02x:%02x.%01x/%02x:%01d value=%08x\n",
200 bus
->number
, PCI_SLOT(devfn
), PCI_FUNC(devfn
),
203 value
= value
<< (8 * (where
& 3));
204 ret
= PCIBIOS_SUCCESSFUL
;
206 spin_lock_irqsave(&ar71xx_pci_lock
, flags
);
207 if (bus
->number
== 0 && devfn
== 0) {
210 t
= PCI_CRP_CMD_WRITE
| (where
& ~3);
211 t
|= ar71xx_pci_get_ble(where
, size
, 1);
213 DBG("PCI: wr local cfg, ad_cbe:%08x, value:%08x\n", t
, value
);
215 __raw_writel(t
, base
+ PCI_REG_CRP_AD_CBE
);
216 __raw_writel(value
, base
+ PCI_REG_CRP_WRDATA
);
220 err
= ar71xx_pci_set_cfgaddr(bus
, devfn
, where
, size
,
224 __raw_writel(value
, base
+ PCI_REG_CFG_WRDATA
);
226 ret
= PCIBIOS_DEVICE_NOT_FOUND
;
228 spin_unlock_irqrestore(&ar71xx_pci_lock
, flags
);
233 static void ar71xx_pci_fixup(struct pci_dev
*dev
)
237 if (!ar71xx_pci_fixup_enable
)
240 if (dev
->bus
->number
!= 0 || dev
->devfn
!= 0)
243 DBG("PCI: fixup host controller %s (%04x:%04x)\n", pci_name(dev
),
244 dev
->vendor
, dev
->device
);
246 /* setup COMMAND register */
247 t
= PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
| PCI_COMMAND_INVALIDATE
248 | PCI_COMMAND_PARITY
| PCI_COMMAND_SERR
| PCI_COMMAND_FAST_BACK
;
250 pci_write_config_word(dev
, PCI_COMMAND
, t
);
252 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID
, PCI_ANY_ID
, ar71xx_pci_fixup
);
254 int __init
ar71xx_pcibios_map_irq(const struct pci_dev
*dev
, uint8_t slot
,
260 slot
-= PCI_IDSEL_ADL_START
- PCI_IDSEL_BASE
;
262 for (i
= 0; i
< ar71xx_pci_nr_irqs
; i
++) {
263 struct ar71xx_pci_irq
*entry
;
265 entry
= &ar71xx_pci_irq_map
[i
];
266 if (entry
->slot
== slot
&& entry
->pin
== pin
) {
273 printk(KERN_ALERT
"PCI: no irq found for pin%u@%s\n",
274 pin
, pci_name((struct pci_dev
*)dev
));
276 printk(KERN_INFO
"PCI: mapping irq %d to pin%u@%s\n",
277 irq
, pin
, pci_name((struct pci_dev
*)dev
));
283 static struct pci_ops ar71xx_pci_ops
= {
284 .read
= ar71xx_pci_read_config
,
285 .write
= ar71xx_pci_write_config
,
288 static struct resource ar71xx_pci_io_resource
= {
289 .name
= "PCI IO space",
292 .flags
= IORESOURCE_IO
,
295 static struct resource ar71xx_pci_mem_resource
= {
296 .name
= "PCI memory space",
297 .start
= AR71XX_PCI_MEM_BASE
,
298 .end
= AR71XX_PCI_MEM_BASE
+ AR71XX_PCI_MEM_SIZE
- 1,
299 .flags
= IORESOURCE_MEM
302 static struct pci_controller ar71xx_pci_controller
= {
303 .pci_ops
= &ar71xx_pci_ops
,
304 .mem_resource
= &ar71xx_pci_mem_resource
,
305 .io_resource
= &ar71xx_pci_io_resource
,
308 static void ar71xx_pci_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
310 void __iomem
*base
= ar71xx_reset_base
;
313 pending
= __raw_readl(base
+ AR71XX_RESET_REG_PCI_INT_STATUS
) &
314 __raw_readl(base
+ AR71XX_RESET_REG_PCI_INT_ENABLE
);
316 if (pending
& PCI_INT_DEV0
)
317 generic_handle_irq(AR71XX_PCI_IRQ_DEV0
);
319 else if (pending
& PCI_INT_DEV1
)
320 generic_handle_irq(AR71XX_PCI_IRQ_DEV1
);
322 else if (pending
& PCI_INT_DEV2
)
323 generic_handle_irq(AR71XX_PCI_IRQ_DEV2
);
325 else if (pending
& PCI_INT_CORE
)
326 generic_handle_irq(AR71XX_PCI_IRQ_CORE
);
329 spurious_interrupt();
332 static void ar71xx_pci_irq_unmask(struct irq_data
*d
)
334 unsigned int irq
= d
->irq
- AR71XX_PCI_IRQ_BASE
;
335 void __iomem
*base
= ar71xx_reset_base
;
338 t
= __raw_readl(base
+ AR71XX_RESET_REG_PCI_INT_ENABLE
);
339 __raw_writel(t
| (1 << irq
), base
+ AR71XX_RESET_REG_PCI_INT_ENABLE
);
342 (void) __raw_readl(base
+ AR71XX_RESET_REG_PCI_INT_ENABLE
);
345 static void ar71xx_pci_irq_mask(struct irq_data
*d
)
347 unsigned int irq
= d
->irq
- AR71XX_PCI_IRQ_BASE
;
348 void __iomem
*base
= ar71xx_reset_base
;
351 t
= __raw_readl(base
+ AR71XX_RESET_REG_PCI_INT_ENABLE
);
352 __raw_writel(t
& ~(1 << irq
), base
+ AR71XX_RESET_REG_PCI_INT_ENABLE
);
355 (void) __raw_readl(base
+ AR71XX_RESET_REG_PCI_INT_ENABLE
);
358 static struct irq_chip ar71xx_pci_irq_chip
= {
359 .name
= "AR71XX PCI ",
360 .irq_mask
= ar71xx_pci_irq_mask
,
361 .irq_unmask
= ar71xx_pci_irq_unmask
,
362 .irq_mask_ack
= ar71xx_pci_irq_mask
,
365 static void __init
ar71xx_pci_irq_init(void)
367 void __iomem
*base
= ar71xx_reset_base
;
370 __raw_writel(0, base
+ AR71XX_RESET_REG_PCI_INT_ENABLE
);
371 __raw_writel(0, base
+ AR71XX_RESET_REG_PCI_INT_STATUS
);
373 for (i
= AR71XX_PCI_IRQ_BASE
;
374 i
< AR71XX_PCI_IRQ_BASE
+ AR71XX_PCI_IRQ_COUNT
; i
++)
375 irq_set_chip_and_handler(i
, &ar71xx_pci_irq_chip
,
378 irq_set_chained_handler(AR71XX_CPU_IRQ_IP2
, ar71xx_pci_irq_handler
);
381 int __init
ar71xx_pcibios_init(void)
383 void __iomem
*ddr_base
= ar71xx_ddr_base
;
385 ar71xx_device_stop(RESET_MODULE_PCI_BUS
| RESET_MODULE_PCI_CORE
);
388 ar71xx_device_start(RESET_MODULE_PCI_BUS
| RESET_MODULE_PCI_CORE
);
391 ar71xx_pcicfg_base
= ioremap_nocache(AR71XX_PCI_CFG_BASE
,
392 AR71XX_PCI_CFG_SIZE
);
393 if (ar71xx_pcicfg_base
== NULL
)
396 __raw_writel(PCI_WIN0_OFFS
, ddr_base
+ AR71XX_DDR_REG_PCI_WIN0
);
397 __raw_writel(PCI_WIN1_OFFS
, ddr_base
+ AR71XX_DDR_REG_PCI_WIN1
);
398 __raw_writel(PCI_WIN2_OFFS
, ddr_base
+ AR71XX_DDR_REG_PCI_WIN2
);
399 __raw_writel(PCI_WIN3_OFFS
, ddr_base
+ AR71XX_DDR_REG_PCI_WIN3
);
400 __raw_writel(PCI_WIN4_OFFS
, ddr_base
+ AR71XX_DDR_REG_PCI_WIN4
);
401 __raw_writel(PCI_WIN5_OFFS
, ddr_base
+ AR71XX_DDR_REG_PCI_WIN5
);
402 __raw_writel(PCI_WIN6_OFFS
, ddr_base
+ AR71XX_DDR_REG_PCI_WIN6
);
403 __raw_writel(PCI_WIN7_OFFS
, ddr_base
+ AR71XX_DDR_REG_PCI_WIN7
);
407 /* clear bus errors */
408 (void)ar71xx_pci_be_handler(1);
410 ar71xx_pci_fixup_enable
= 1;
411 ar71xx_pci_irq_init();
412 register_pci_controller(&ar71xx_pci_controller
);