2 * Atheros AR71xx PCI host controller driver
4 * Copyright (C) 2008-2009 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>
21 #include <asm/mach-ar71xx/ar71xx.h>
22 #include <asm/mach-ar71xx/pci.h>
26 #define DBG(fmt, args...) printk(KERN_DEBUG fmt, ## args)
28 #define DBG(fmt, args...)
31 #define AR71XX_PCI_DELAY 100 /* msecs */
34 #define PCI_IDSEL_BASE PCI_IDSEL_ADL_START
36 #define PCI_IDSEL_BASE 0
39 static void __iomem
*ar71xx_pcicfg_base
;
40 static DEFINE_SPINLOCK(ar71xx_pci_lock
);
41 static int ar71xx_pci_fixup_enable
;
43 static inline void ar71xx_pci_delay(void)
45 mdelay(AR71XX_PCI_DELAY
);
48 static inline u32
ar71xx_pcicfg_rr(unsigned int reg
)
50 return __raw_readl(ar71xx_pcicfg_base
+ reg
);
53 static inline void ar71xx_pcicfg_wr(unsigned int reg
, u32 val
)
55 __raw_writel(val
, ar71xx_pcicfg_base
+ reg
);
58 /* Byte lane enable bits */
59 static u8 ble_table
[4][4] = {
66 static inline u32
ar71xx_pci_get_ble(int where
, int size
, int local
)
70 t
= ble_table
[size
& 3][where
& 3];
72 t
<<= (local
) ? 20 : 4;
76 static inline u32
ar71xx_pci_bus_addr(struct pci_bus
*bus
, unsigned int devfn
,
83 ret
= (1 << (PCI_IDSEL_BASE
+ PCI_SLOT(devfn
)))
84 | (PCI_FUNC(devfn
) << 8) | (where
& ~3);
87 ret
= (bus
->number
<< 16) | (PCI_SLOT(devfn
) << 11)
88 | (PCI_FUNC(devfn
) << 8) | (where
& ~3) | 1;
94 int ar71xx_pci_be_handler(int is_fixup
)
99 pci_err
= ar71xx_pcicfg_rr(PCI_REG_PCI_ERR
) & 3;
102 printk(KERN_ALERT
"PCI error %d at PCI addr 0x%x\n",
104 ar71xx_pcicfg_rr(PCI_REG_PCI_ERR_ADDR
));
106 ar71xx_pcicfg_wr(PCI_REG_PCI_ERR
, pci_err
);
109 ahb_err
= ar71xx_pcicfg_rr(PCI_REG_AHB_ERR
) & 1;
112 printk(KERN_ALERT
"AHB error at AHB address 0x%x\n",
113 ar71xx_pcicfg_rr(PCI_REG_AHB_ERR_ADDR
));
115 ar71xx_pcicfg_wr(PCI_REG_AHB_ERR
, ahb_err
);
118 return ((ahb_err
| pci_err
) ? 1 : 0);
121 static inline int ar71xx_pci_set_cfgaddr(struct pci_bus
*bus
,
122 unsigned int devfn
, int where
, int size
, u32 cmd
)
126 addr
= ar71xx_pci_bus_addr(bus
, devfn
, where
);
128 DBG("PCI: set cfgaddr: %02x:%02x.%01x/%02x:%01d, addr=%08x\n",
129 bus
->number
, PCI_SLOT(devfn
), PCI_FUNC(devfn
),
132 ar71xx_pcicfg_wr(PCI_REG_CFG_AD
, addr
);
133 ar71xx_pcicfg_wr(PCI_REG_CFG_CBE
,
134 cmd
| ar71xx_pci_get_ble(where
, size
, 0));
136 return ar71xx_pci_be_handler(1);
139 static int ar71xx_pci_read_config(struct pci_bus
*bus
, unsigned int devfn
,
140 int where
, int size
, u32
*value
)
142 static u32 mask
[8] = {0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0};
147 ret
= PCIBIOS_SUCCESSFUL
;
149 DBG("PCI: read config: %02x:%02x.%01x/%02x:%01d\n", bus
->number
,
150 PCI_SLOT(devfn
), PCI_FUNC(devfn
), where
, size
);
152 spin_lock_irqsave(&ar71xx_pci_lock
, flags
);
154 if (bus
->number
== 0 && devfn
== 0) {
157 t
= PCI_CRP_CMD_READ
| (where
& ~3);
159 ar71xx_pcicfg_wr(PCI_REG_CRP_AD_CBE
, t
);
160 data
= ar71xx_pcicfg_rr(PCI_REG_CRP_RDDATA
);
162 DBG("PCI: rd local cfg, ad_cbe:%08x, data:%08x\n", t
, data
);
167 err
= ar71xx_pci_set_cfgaddr(bus
, devfn
, where
, size
,
171 data
= ar71xx_pcicfg_rr(PCI_REG_CFG_RDDATA
);
173 ret
= PCIBIOS_DEVICE_NOT_FOUND
;
178 spin_unlock_irqrestore(&ar71xx_pci_lock
, flags
);
180 DBG("PCI: read config: data=%08x raw=%08x\n",
181 (data
>> (8 * (where
& 3))) & mask
[size
& 7], data
);
183 *value
= (data
>> (8 * (where
& 3))) & mask
[size
& 7];
188 static int ar71xx_pci_write_config(struct pci_bus
*bus
, unsigned int devfn
,
189 int where
, int size
, u32 value
)
194 DBG("PCI: write config: %02x:%02x.%01x/%02x:%01d value=%08x\n",
195 bus
->number
, PCI_SLOT(devfn
), PCI_FUNC(devfn
),
198 value
= value
<< (8 * (where
& 3));
199 ret
= PCIBIOS_SUCCESSFUL
;
201 spin_lock_irqsave(&ar71xx_pci_lock
, flags
);
202 if (bus
->number
== 0 && devfn
== 0) {
205 t
= PCI_CRP_CMD_WRITE
| (where
& ~3);
206 t
|= ar71xx_pci_get_ble(where
, size
, 1);
208 DBG("PCI: wr local cfg, ad_cbe:%08x, value:%08x\n", t
, value
);
210 ar71xx_pcicfg_wr(PCI_REG_CRP_AD_CBE
, t
);
211 ar71xx_pcicfg_wr(PCI_REG_CRP_WRDATA
, value
);
215 err
= ar71xx_pci_set_cfgaddr(bus
, devfn
, where
, size
,
219 ar71xx_pcicfg_wr(PCI_REG_CFG_WRDATA
, value
);
221 ret
= PCIBIOS_DEVICE_NOT_FOUND
;
223 spin_unlock_irqrestore(&ar71xx_pci_lock
, flags
);
228 static void ar71xx_pci_fixup(struct pci_dev
*dev
)
232 if (!ar71xx_pci_fixup_enable
)
235 if (dev
->bus
->number
!= 0 || dev
->devfn
!= 0)
238 DBG("PCI: fixup host controller %s (%04x:%04x)\n", pci_name(dev
),
239 dev
->vendor
, dev
->device
);
241 /* setup COMMAND register */
242 t
= PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
| PCI_COMMAND_INVALIDATE
243 | PCI_COMMAND_PARITY
| PCI_COMMAND_SERR
| PCI_COMMAND_FAST_BACK
;
245 pci_write_config_word(dev
, PCI_COMMAND
, t
);
247 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID
, PCI_ANY_ID
, ar71xx_pci_fixup
);
249 int __init
ar71xx_pcibios_map_irq(const struct pci_dev
*dev
, uint8_t slot
,
255 slot
-= PCI_IDSEL_ADL_START
- PCI_IDSEL_BASE
;
257 for (i
= 0; i
< ar71xx_pci_nr_irqs
; i
++) {
258 struct ar71xx_pci_irq
*entry
;
260 entry
= &ar71xx_pci_irq_map
[i
];
261 if (entry
->slot
== slot
&& entry
->pin
== pin
) {
268 printk(KERN_ALERT
"PCI: no irq found for pin%u@%s\n",
269 pin
, pci_name((struct pci_dev
*)dev
));
271 printk(KERN_INFO
"PCI: mapping irq %d to pin%u@%s\n",
272 irq
, pin
, pci_name((struct pci_dev
*)dev
));
278 static struct pci_ops ar71xx_pci_ops
= {
279 .read
= ar71xx_pci_read_config
,
280 .write
= ar71xx_pci_write_config
,
283 static struct resource ar71xx_pci_io_resource
= {
284 .name
= "PCI IO space",
287 .flags
= IORESOURCE_IO
,
290 static struct resource ar71xx_pci_mem_resource
= {
291 .name
= "PCI memory space",
292 .start
= AR71XX_PCI_MEM_BASE
,
293 .end
= AR71XX_PCI_MEM_BASE
+ AR71XX_PCI_MEM_SIZE
- 1,
294 .flags
= IORESOURCE_MEM
297 static struct pci_controller ar71xx_pci_controller
= {
298 .pci_ops
= &ar71xx_pci_ops
,
299 .mem_resource
= &ar71xx_pci_mem_resource
,
300 .io_resource
= &ar71xx_pci_io_resource
,
303 int __init
ar71xx_pcibios_init(void)
305 ar71xx_device_stop(RESET_MODULE_PCI_BUS
| RESET_MODULE_PCI_CORE
);
308 ar71xx_device_start(RESET_MODULE_PCI_BUS
| RESET_MODULE_PCI_CORE
);
311 ar71xx_pcicfg_base
= ioremap_nocache(AR71XX_PCI_CFG_BASE
,
312 AR71XX_PCI_CFG_SIZE
);
314 ar71xx_ddr_wr(AR71XX_DDR_REG_PCI_WIN0
, PCI_WIN0_OFFS
);
315 ar71xx_ddr_wr(AR71XX_DDR_REG_PCI_WIN1
, PCI_WIN1_OFFS
);
316 ar71xx_ddr_wr(AR71XX_DDR_REG_PCI_WIN2
, PCI_WIN2_OFFS
);
317 ar71xx_ddr_wr(AR71XX_DDR_REG_PCI_WIN3
, PCI_WIN3_OFFS
);
318 ar71xx_ddr_wr(AR71XX_DDR_REG_PCI_WIN4
, PCI_WIN4_OFFS
);
319 ar71xx_ddr_wr(AR71XX_DDR_REG_PCI_WIN5
, PCI_WIN5_OFFS
);
320 ar71xx_ddr_wr(AR71XX_DDR_REG_PCI_WIN6
, PCI_WIN6_OFFS
);
321 ar71xx_ddr_wr(AR71XX_DDR_REG_PCI_WIN7
, PCI_WIN7_OFFS
);
325 /* clear bus errors */
326 (void)ar71xx_pci_be_handler(1);
328 ar71xx_pci_fixup_enable
= 1;
329 register_pci_controller(&ar71xx_pci_controller
);