2 * Atheros AR724x PCI host controller driver
4 * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
6 * Parts of this file are based on Atheros' 2.6.15 BSP
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
13 #include <linux/resource.h>
14 #include <linux/types.h>
15 #include <linux/delay.h>
16 #include <linux/bitops.h>
17 #include <linux/pci.h>
18 #include <linux/pci_regs.h>
19 #include <linux/interrupt.h>
21 #include <asm/mach-ar71xx/ar71xx.h>
22 #include <asm/mach-ar71xx/pci.h>
26 #define DBG(fmt, args...) printk(KERN_INFO fmt, ## args)
28 #define DBG(fmt, args...)
31 static void __iomem
*ar724x_pci_localcfg_base
;
32 static void __iomem
*ar724x_pci_devcfg_base
;
33 static void __iomem
*ar724x_pci_ctrl_base
;
34 static int ar724x_pci_fixup_enable
;
36 static DEFINE_SPINLOCK(ar724x_pci_lock
);
38 static void ar724x_pci_read(void __iomem
*base
, int where
, int size
, u32
*value
)
43 spin_lock_irqsave(&ar724x_pci_lock
, flags
);
44 data
= __raw_readl(base
+ (where
& ~3));
62 spin_unlock_irqrestore(&ar724x_pci_lock
, flags
);
65 static void ar724x_pci_write(void __iomem
*base
, int where
, int size
, u32 value
)
71 spin_lock_irqsave(&ar724x_pci_lock
, flags
);
72 data
= __raw_readl(base
+ (where
& ~3));
76 s
= ((where
& 3) << 3);
78 data
|= ((value
& 0xFF) << s
);
81 s
= ((where
& 2) << 3);
82 data
&= ~(0xFFFF << s
);
83 data
|= ((value
& 0xFFFF) << s
);
90 __raw_writel(data
, base
+ (where
& ~3));
92 (void)__raw_readl(base
+ (where
& ~3));
93 spin_unlock_irqrestore(&ar724x_pci_lock
, flags
);
96 static int ar724x_pci_read_config(struct pci_bus
*bus
, unsigned int devfn
,
97 int where
, int size
, u32
*value
)
100 if (bus
->number
!= 0 || devfn
!= 0)
101 return PCIBIOS_DEVICE_NOT_FOUND
;
103 ar724x_pci_read(ar724x_pci_devcfg_base
, where
, size
, value
);
105 DBG("PCI: read config: %02x:%02x.%01x/%02x:%01d, value=%08x\n",
106 bus
->number
, PCI_SLOT(devfn
), PCI_FUNC(devfn
),
107 where
, size
, *value
);
110 * WAR for BAR issue - We are unable to access the PCI device space
111 * if we set the BAR with proper base address
113 if ((where
== 0x10) && (size
== 4)) {
114 if (ar71xx_soc
== AR71XX_SOC_AR7240
)
115 ar724x_pci_write(ar724x_pci_devcfg_base
, where
, size
, 0xffff);
117 ar724x_pci_write(ar724x_pci_devcfg_base
, where
, size
, 0x1000ffff);
120 return PCIBIOS_SUCCESSFUL
;
123 static int ar724x_pci_write_config(struct pci_bus
*bus
, unsigned int devfn
,
124 int where
, int size
, u32 value
)
126 if (bus
->number
!= 0 || devfn
!= 0)
127 return PCIBIOS_DEVICE_NOT_FOUND
;
129 DBG("PCI: write config: %02x:%02x.%01x/%02x:%01d, value=%08x\n",
130 bus
->number
, PCI_SLOT(devfn
), PCI_FUNC(devfn
),
133 ar724x_pci_write(ar724x_pci_devcfg_base
, where
, size
, value
);
135 return PCIBIOS_SUCCESSFUL
;
138 static void ar724x_pci_fixup(struct pci_dev
*dev
)
142 if (!ar724x_pci_fixup_enable
)
145 if (dev
->bus
->number
!= 0 || dev
->devfn
!= 0)
148 /* setup COMMAND register */
149 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
150 cmd
|= PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
|
151 PCI_COMMAND_INVALIDATE
| PCI_COMMAND_PARITY
| PCI_COMMAND_SERR
|
152 PCI_COMMAND_FAST_BACK
;
154 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
156 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID
, PCI_ANY_ID
, ar724x_pci_fixup
);
158 int __init
ar724x_pcibios_map_irq(const struct pci_dev
*dev
, uint8_t slot
,
164 for (i
= 0; i
< ar71xx_pci_nr_irqs
; i
++) {
165 struct ar71xx_pci_irq
*entry
;
166 entry
= &ar71xx_pci_irq_map
[i
];
168 if (entry
->slot
== slot
&& entry
->pin
== pin
) {
175 printk(KERN_ALERT
"PCI: no irq found for pin%u@%s\n",
176 pin
, pci_name((struct pci_dev
*)dev
));
178 printk(KERN_INFO
"PCI: mapping irq %d to pin%u@%s\n",
179 irq
, pin
, pci_name((struct pci_dev
*)dev
));
184 static struct pci_ops ar724x_pci_ops
= {
185 .read
= ar724x_pci_read_config
,
186 .write
= ar724x_pci_write_config
,
189 static struct resource ar724x_pci_io_resource
= {
190 .name
= "PCI IO space",
193 .flags
= IORESOURCE_IO
,
196 static struct resource ar724x_pci_mem_resource
= {
197 .name
= "PCI memory space",
198 .start
= AR71XX_PCI_MEM_BASE
,
199 .end
= AR71XX_PCI_MEM_BASE
+ AR71XX_PCI_MEM_SIZE
- 1,
200 .flags
= IORESOURCE_MEM
203 static struct pci_controller ar724x_pci_controller
= {
204 .pci_ops
= &ar724x_pci_ops
,
205 .mem_resource
= &ar724x_pci_mem_resource
,
206 .io_resource
= &ar724x_pci_io_resource
,
209 static void __init
ar724x_pci_reset(void)
211 ar71xx_device_stop(AR724X_RESET_PCIE
);
212 ar71xx_device_stop(AR724X_RESET_PCIE_PHY
);
213 ar71xx_device_stop(AR724X_RESET_PCIE_PHY_SERIAL
);
216 ar71xx_device_start(AR724X_RESET_PCIE_PHY_SERIAL
);
218 ar71xx_device_start(AR724X_RESET_PCIE_PHY
);
219 ar71xx_device_start(AR724X_RESET_PCIE
);
222 static int __init
ar724x_pci_setup(void)
224 void __iomem
*base
= ar724x_pci_ctrl_base
;
227 /* setup COMMAND register */
228 t
= PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
| PCI_COMMAND_INVALIDATE
|
229 PCI_COMMAND_PARITY
|PCI_COMMAND_SERR
|PCI_COMMAND_FAST_BACK
;
231 ar724x_pci_write(ar724x_pci_localcfg_base
, PCI_COMMAND
, 4, t
);
232 ar724x_pci_write(ar724x_pci_localcfg_base
, 0x20, 4, 0x1ff01000);
233 ar724x_pci_write(ar724x_pci_localcfg_base
, 0x24, 4, 0x1ff01000);
235 t
= __raw_readl(base
+ AR724X_PCI_REG_RESET
);
238 __raw_writel(0, base
+ AR724X_PCI_REG_RESET
);
240 __raw_writel(4, base
+ AR724X_PCI_REG_RESET
);
244 if (ar71xx_soc
== AR71XX_SOC_AR7240
)
245 t
= AR724X_PCI_APP_LTSSM_ENABLE
;
248 __raw_writel(t
, base
+ AR724X_PCI_REG_APP
);
250 (void) __raw_readl(base
+ AR724X_PCI_REG_APP
);
253 t
= __raw_readl(base
+ AR724X_PCI_REG_RESET
);
254 if ((t
& AR724X_PCI_RESET_LINK_UP
) == 0x0) {
255 printk(KERN_WARNING
"PCI: no PCIe module found\n");
259 if (ar71xx_soc
== AR71XX_SOC_AR7241
|| ar71xx_soc
== AR71XX_SOC_AR7242
) {
260 t
= __raw_readl(base
+ AR724X_PCI_REG_APP
);
262 __raw_writel(t
, base
+ AR724X_PCI_REG_APP
);
268 static void ar724x_pci_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
270 void __iomem
*base
= ar724x_pci_ctrl_base
;
273 pending
= __raw_readl(base
+ AR724X_PCI_REG_INT_STATUS
) &
274 __raw_readl(base
+ AR724X_PCI_REG_INT_MASK
);
276 if (pending
& AR724X_PCI_INT_DEV0
)
277 generic_handle_irq(AR71XX_PCI_IRQ_DEV0
);
280 spurious_interrupt();
283 static void ar724x_pci_irq_unmask(unsigned int irq
)
285 void __iomem
*base
= ar724x_pci_ctrl_base
;
289 case AR71XX_PCI_IRQ_DEV0
:
290 irq
-= AR71XX_PCI_IRQ_BASE
;
292 t
= __raw_readl(base
+ AR724X_PCI_REG_INT_MASK
);
293 __raw_writel(t
| AR724X_PCI_INT_DEV0
,
294 base
+ AR724X_PCI_REG_INT_MASK
);
296 (void) __raw_readl(base
+ AR724X_PCI_REG_INT_MASK
);
300 static void ar724x_pci_irq_mask(unsigned int irq
)
302 void __iomem
*base
= ar724x_pci_ctrl_base
;
306 case AR71XX_PCI_IRQ_DEV0
:
307 irq
-= AR71XX_PCI_IRQ_BASE
;
309 t
= __raw_readl(base
+ AR724X_PCI_REG_INT_MASK
);
310 __raw_writel(t
& ~AR724X_PCI_INT_DEV0
,
311 base
+ AR724X_PCI_REG_INT_MASK
);
314 (void) __raw_readl(base
+ AR724X_PCI_REG_INT_MASK
);
316 t
= __raw_readl(base
+ AR724X_PCI_REG_INT_STATUS
);
317 __raw_writel(t
| AR724X_PCI_INT_DEV0
,
318 base
+ AR724X_PCI_REG_INT_STATUS
);
321 (void) __raw_readl(base
+ AR724X_PCI_REG_INT_STATUS
);
325 static struct irq_chip ar724x_pci_irq_chip
= {
326 .name
= "AR724X PCI ",
327 .mask
= ar724x_pci_irq_mask
,
328 .unmask
= ar724x_pci_irq_unmask
,
329 .mask_ack
= ar724x_pci_irq_mask
,
332 static void __init
ar724x_pci_irq_init(void)
334 void __iomem
*base
= ar724x_pci_ctrl_base
;
338 t
= ar71xx_reset_rr(AR724X_RESET_REG_RESET_MODULE
);
339 if (t
& (AR724X_RESET_PCIE
| AR724X_RESET_PCIE_PHY
|
340 AR724X_RESET_PCIE_PHY_SERIAL
)) {
344 __raw_writel(0, base
+ AR724X_PCI_REG_INT_MASK
);
345 __raw_writel(0, base
+ AR724X_PCI_REG_INT_STATUS
);
347 for (i
= AR71XX_PCI_IRQ_BASE
;
348 i
< AR71XX_PCI_IRQ_BASE
+ AR71XX_PCI_IRQ_COUNT
; i
++) {
349 irq_desc
[i
].status
= IRQ_DISABLED
;
350 set_irq_chip_and_handler(i
, &ar724x_pci_irq_chip
,
354 set_irq_chained_handler(AR71XX_CPU_IRQ_IP2
, ar724x_pci_irq_handler
);
357 int __init
ar724x_pcibios_init(void)
361 ar724x_pci_localcfg_base
= ioremap_nocache(AR724X_PCI_CRP_BASE
,
362 AR724X_PCI_CRP_SIZE
);
363 if (ar724x_pci_localcfg_base
== NULL
)
366 ar724x_pci_devcfg_base
= ioremap_nocache(AR724X_PCI_CFG_BASE
,
367 AR724X_PCI_CFG_SIZE
);
368 if (ar724x_pci_devcfg_base
== NULL
)
369 goto err_unmap_localcfg
;
371 ar724x_pci_ctrl_base
= ioremap_nocache(AR724X_PCI_CTRL_BASE
,
372 AR724X_PCI_CTRL_SIZE
);
373 if (ar724x_pci_ctrl_base
== NULL
)
374 goto err_unmap_devcfg
;
377 ret
= ar724x_pci_setup();
381 ar724x_pci_fixup_enable
= 1;
382 ar724x_pci_irq_init();
383 register_pci_controller(&ar724x_pci_controller
);
388 iounmap(ar724x_pci_ctrl_base
);
390 iounmap(ar724x_pci_devcfg_base
);
392 iounmap(ar724x_pci_localcfg_base
);