2 * Ralink RT3883 SoC PCI support
4 * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
6 * Parts of this file are based on Ralink's 2.6.21 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/types.h>
14 #include <linux/pci.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
20 #include <asm/mach-ralink/rt3883.h>
21 #include <asm/mach-ralink/rt3883_regs.h>
23 #define RT3883_MEMORY_BASE 0x00000000
24 #define RT3883_MEMORY_SIZE 0x02000000
26 #define RT3883_PCI_MEM_BASE 0x20000000
27 #define RT3883_PCI_MEM_SIZE 0x10000000
28 #define RT3883_PCI_IO_BASE 0x10160000
29 #define RT3883_PCI_IO_SIZE 0x00010000
31 #define RT3883_PCI_REG_PCICFG_ADDR 0x00
32 #define RT3883_PCI_REG_PCIRAW_ADDR 0x04
33 #define RT3883_PCI_REG_PCIINT_ADDR 0x08
34 #define RT3883_PCI_REG_PCIMSK_ADDR 0x0c
35 #define RT3833_PCI_PCIINT_PCIE BIT(20)
36 #define RT3833_PCI_PCIINT_PCI1 BIT(19)
37 #define RT3833_PCI_PCIINT_PCI0 BIT(18)
39 #define RT3883_PCI_REG_CONFIG_ADDR 0x20
40 #define RT3883_PCI_REG_CONFIG_DATA 0x24
41 #define RT3883_PCI_REG_MEMBASE 0x28
42 #define RT3883_PCI_REG_IOBASE 0x2c
43 #define RT3883_PCI_REG_ARBCTL 0x80
45 #define RT3883_PCI_REG_BASE(_x) (0x1000 + (_x) * 0x1000)
46 #define RT3883_PCI_REG_BAR0SETUP_ADDR(_x) (RT3883_PCI_REG_BASE((_x)) + 0x10)
47 #define RT3883_PCI_REG_IMBASEBAR0_ADDR(_x) (RT3883_PCI_REG_BASE((_x)) + 0x18)
48 #define RT3883_PCI_REG_ID(_x) (RT3883_PCI_REG_BASE((_x)) + 0x30)
49 #define RT3883_PCI_REG_CLASS(_x) (RT3883_PCI_REG_BASE((_x)) + 0x34)
50 #define RT3883_PCI_REG_SUBID(_x) (RT3883_PCI_REG_BASE((_x)) + 0x38)
51 #define RT3883_PCI_REG_STATUS(_x) (RT3883_PCI_REG_BASE((_x)) + 0x50)
53 static int (*rt3883_pci_plat_dev_init
)(struct pci_dev
*dev
);
54 static void __iomem
*rt3883_pci_base
;
55 static DEFINE_SPINLOCK(rt3883_pci_lock
);
57 static inline u32
rt3883_pci_rr(unsigned reg
)
59 return readl(rt3883_pci_base
+ reg
);
62 static inline void rt3883_pci_wr(u32 val
, unsigned reg
)
64 writel(val
, rt3883_pci_base
+ reg
);
67 static inline u32
rt3883_pci_get_cfgaddr(unsigned int bus
, unsigned int slot
,
68 unsigned int func
, unsigned int where
)
70 return ((bus
<< 16) | (slot
<< 11) | (func
<< 8) | (where
& 0xfc) |
74 static u32
rt3883_pci_read_u32(unsigned bus
, unsigned slot
,
75 unsigned func
, unsigned reg
)
81 address
= rt3883_pci_get_cfgaddr(bus
, slot
, func
, reg
);
83 spin_lock_irqsave(&rt3883_pci_lock
, flags
);
84 rt3883_pci_wr(address
, RT3883_PCI_REG_CONFIG_ADDR
);
85 ret
= rt3883_pci_rr(RT3883_PCI_REG_CONFIG_DATA
);
86 spin_unlock_irqrestore(&rt3883_pci_lock
, flags
);
91 static void rt3883_pci_write_u32(unsigned bus
, unsigned slot
,
92 unsigned func
, unsigned reg
, u32 val
)
97 address
= rt3883_pci_get_cfgaddr(bus
, slot
, func
, reg
);
99 spin_lock_irqsave(&rt3883_pci_lock
, flags
);
100 rt3883_pci_wr(address
, RT3883_PCI_REG_CONFIG_ADDR
);
101 rt3883_pci_wr(val
, RT3883_PCI_REG_CONFIG_DATA
);
102 spin_unlock_irqrestore(&rt3883_pci_lock
, flags
);
105 static void rt3883_pci_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
109 pending
= rt3883_pci_rr(RT3883_PCI_REG_PCIINT_ADDR
) &
110 rt3883_pci_rr(RT3883_PCI_REG_PCIMSK_ADDR
);
113 spurious_interrupt();
117 if (pending
& RT3833_PCI_PCIINT_PCI0
)
118 generic_handle_irq(RT3883_PCI_IRQ_PCI0
);
120 if (pending
& RT3833_PCI_PCIINT_PCI1
)
121 generic_handle_irq(RT3883_PCI_IRQ_PCI1
);
123 if (pending
& RT3833_PCI_PCIINT_PCIE
)
124 generic_handle_irq(RT3883_PCI_IRQ_PCIE
);
127 static void rt3883_pci_irq_unmask(struct irq_data
*d
)
134 case RT3883_PCI_IRQ_PCI0
:
135 mask
= RT3833_PCI_PCIINT_PCI0
;
137 case RT3883_PCI_IRQ_PCI1
:
138 mask
= RT3833_PCI_PCIINT_PCI1
;
140 case RT3883_PCI_IRQ_PCIE
:
141 mask
= RT3833_PCI_PCIINT_PCIE
;
147 t
= rt3883_pci_rr(RT3883_PCI_REG_PCIMSK_ADDR
);
148 rt3883_pci_wr(t
| mask
, RT3883_PCI_REG_PCIMSK_ADDR
);
150 rt3883_pci_rr(RT3883_PCI_REG_PCIMSK_ADDR
);
153 static void rt3883_pci_irq_mask(struct irq_data
*d
)
160 case RT3883_PCI_IRQ_PCI0
:
161 mask
= RT3833_PCI_PCIINT_PCI0
;
163 case RT3883_PCI_IRQ_PCI1
:
164 mask
= RT3833_PCI_PCIINT_PCI1
;
166 case RT3883_PCI_IRQ_PCIE
:
167 mask
= RT3833_PCI_PCIINT_PCIE
;
173 t
= rt3883_pci_rr(RT3883_PCI_REG_PCIMSK_ADDR
);
174 rt3883_pci_wr(t
& ~mask
, RT3883_PCI_REG_PCIMSK_ADDR
);
176 rt3883_pci_rr(RT3883_PCI_REG_PCIMSK_ADDR
);
179 static struct irq_chip rt3883_pci_irq_chip
= {
180 .name
= "RT3883 PCI",
181 .irq_mask
= rt3883_pci_irq_mask
,
182 .irq_unmask
= rt3883_pci_irq_unmask
,
183 .irq_mask_ack
= rt3883_pci_irq_mask
,
186 static void __init
rt3883_pci_irq_init(void)
190 /* disable all interrupts */
191 rt3883_pci_wr(0, RT3883_PCI_REG_PCIMSK_ADDR
);
193 for (i
= RT3883_PCI_IRQ_BASE
;
194 i
< RT3883_PCI_IRQ_BASE
+ RT3883_PCI_IRQ_COUNT
; i
++) {
195 irq_set_chip_and_handler(i
, &rt3883_pci_irq_chip
,
199 irq_set_chained_handler(RT3883_CPU_IRQ_PCI
, rt3883_pci_irq_handler
);
202 static int rt3883_pci_config_read(struct pci_bus
*bus
, unsigned int devfn
,
203 int where
, int size
, u32
*val
)
209 address
= rt3883_pci_get_cfgaddr(bus
->number
, PCI_SLOT(devfn
),
210 PCI_FUNC(devfn
), where
);
212 spin_lock_irqsave(&rt3883_pci_lock
, flags
);
213 rt3883_pci_wr(address
, RT3883_PCI_REG_CONFIG_ADDR
);
214 data
= rt3883_pci_rr(RT3883_PCI_REG_CONFIG_DATA
);
215 spin_unlock_irqrestore(&rt3883_pci_lock
, flags
);
219 *val
= (data
>> ((where
& 3) << 3)) & 0xff;
222 *val
= (data
>> ((where
& 3) << 3)) & 0xffff;
229 return PCIBIOS_SUCCESSFUL
;
232 static int rt3883_pci_config_write(struct pci_bus
*bus
, unsigned int devfn
,
233 int where
, int size
, u32 val
)
239 address
= rt3883_pci_get_cfgaddr(bus
->number
, PCI_SLOT(devfn
),
240 PCI_FUNC(devfn
), where
);
242 spin_lock_irqsave(&rt3883_pci_lock
, flags
);
243 rt3883_pci_wr(address
, RT3883_PCI_REG_CONFIG_ADDR
);
244 data
= rt3883_pci_rr(RT3883_PCI_REG_CONFIG_DATA
);
248 data
= (data
& ~(0xff << ((where
& 3) << 3))) |
249 (val
<< ((where
& 3) << 3));
252 data
= (data
& ~(0xffff << ((where
& 3) << 3))) |
253 (val
<< ((where
& 3) << 3));
260 rt3883_pci_wr(data
, RT3883_PCI_REG_CONFIG_DATA
);
261 spin_unlock_irqrestore(&rt3883_pci_lock
, flags
);
263 return PCIBIOS_SUCCESSFUL
;
266 static struct pci_ops rt3883_pci_ops
= {
267 .read
= rt3883_pci_config_read
,
268 .write
= rt3883_pci_config_write
,
271 static struct resource rt3883_pci_mem_resource
= {
272 .name
= "PCI MEM space",
273 .start
= RT3883_PCI_MEM_BASE
,
274 .end
= RT3883_PCI_MEM_BASE
+ RT3883_PCI_MEM_SIZE
- 1,
275 .flags
= IORESOURCE_MEM
,
278 static struct resource rt3883_pci_io_resource
= {
279 .name
= "PCI IO space",
280 .start
= RT3883_PCI_IO_BASE
,
281 .end
= RT3883_PCI_IO_BASE
+ RT3883_PCI_IO_SIZE
- 1,
282 .flags
= IORESOURCE_IO
,
285 static struct pci_controller rt3883_pci_controller
= {
286 .pci_ops
= &rt3883_pci_ops
,
287 .mem_resource
= &rt3883_pci_mem_resource
,
288 .io_resource
= &rt3883_pci_io_resource
,
291 static void rt3883_pci_preinit(unsigned mode
)
297 if (mode
& RT3883_PCI_MODE_PCIE
) {
300 val
= rt3883_sysc_rr(RT3883_SYSC_REG_SYSCFG1
);
303 rt3883_sysc_wr(val
, RT3883_SYSC_REG_SYSCFG1
);
305 val
= rt3883_sysc_rr(RT3883_SYSC_REG_PCIE_CLK_GEN0
);
307 rt3883_sysc_wr(val
, RT3883_SYSC_REG_PCIE_CLK_GEN0
);
309 val
= rt3883_sysc_rr(RT3883_SYSC_REG_PCIE_CLK_GEN1
);
311 rt3883_sysc_wr(val
, RT3883_SYSC_REG_PCIE_CLK_GEN1
);
313 val
= rt3883_sysc_rr(RT3883_SYSC_REG_PCIE_CLK_GEN1
);
315 rt3883_sysc_wr(val
, RT3883_SYSC_REG_PCIE_CLK_GEN1
);
317 val
= rt3883_sysc_rr(RT3883_SYSC_REG_PCIE_CLK_GEN0
);
319 rt3883_sysc_wr(val
, RT3883_SYSC_REG_PCIE_CLK_GEN0
);
324 syscfg1
= rt3883_sysc_rr(RT3883_SYSC_REG_SYSCFG1
);
325 syscfg1
&= ~(RT3883_SYSCFG1_PCIE_RC_MODE
|
326 RT3883_SYSCFG1_PCI_HOST_MODE
);
328 rstctrl
= rt3883_sysc_rr(RT3883_SYSC_REG_RSTCTRL
);
329 rstctrl
|= (RT3883_RSTCTRL_PCI
| RT3883_RSTCTRL_PCIE
);
331 clkcfg1
= rt3883_sysc_rr(RT3883_SYSC_REG_CLKCFG1
);
332 clkcfg1
&= ~(RT3883_CLKCFG1_PCI_CLK_EN
|
333 RT3883_CLKCFG1_PCIE_CLK_EN
);
335 if (mode
& RT3883_PCI_MODE_PCI
) {
336 syscfg1
|= RT3883_SYSCFG1_PCI_HOST_MODE
;
337 clkcfg1
|= RT3883_CLKCFG1_PCI_CLK_EN
;
338 rstctrl
&= ~RT3883_RSTCTRL_PCI
;
340 if (mode
& RT3883_PCI_MODE_PCIE
) {
341 syscfg1
|= RT3883_SYSCFG1_PCI_HOST_MODE
|
342 RT3883_SYSCFG1_PCIE_RC_MODE
;
343 clkcfg1
|= RT3883_CLKCFG1_PCIE_CLK_EN
;
344 rstctrl
&= ~RT3883_RSTCTRL_PCIE
;
347 rt3883_sysc_wr(syscfg1
, RT3883_SYSC_REG_SYSCFG1
);
348 rt3883_sysc_wr(rstctrl
, RT3883_SYSC_REG_RSTCTRL
);
349 rt3883_sysc_wr(clkcfg1
, RT3883_SYSC_REG_CLKCFG1
);
354 static int rt3883_pcie_ready(void)
360 status
= rt3883_pci_rr(RT3883_PCI_REG_STATUS(1));
364 /* TODO: reset PCIe and turn off PCIe clock */
369 void __init
rt3883_pci_init(unsigned mode
)
374 rt3883_pci_preinit(mode
);
376 rt3883_pci_base
= ioremap(RT3883_PCI_BASE
, PAGE_SIZE
);
377 if (rt3883_pci_base
== NULL
) {
378 pr_err("failed to ioremap PCI registers\n");
382 rt3883_pci_wr(0, RT3883_PCI_REG_PCICFG_ADDR
);
383 if (mode
& RT3883_PCI_MODE_PCI
)
384 rt3883_pci_wr(BIT(16), RT3883_PCI_REG_PCICFG_ADDR
);
388 if (mode
& RT3883_PCI_MODE_PCIE
) {
389 err
= rt3883_pcie_ready();
394 if (mode
& RT3883_PCI_MODE_PCI
)
395 rt3883_pci_wr(0x79, RT3883_PCI_REG_ARBCTL
);
397 rt3883_pci_wr(RT3883_PCI_MEM_BASE
, RT3883_PCI_REG_MEMBASE
);
398 rt3883_pci_wr(RT3883_PCI_IO_BASE
, RT3883_PCI_REG_IOBASE
);
401 rt3883_pci_wr(0x03ff0000, RT3883_PCI_REG_BAR0SETUP_ADDR(0));
402 rt3883_pci_wr(RT3883_MEMORY_BASE
, RT3883_PCI_REG_IMBASEBAR0_ADDR(0));
403 rt3883_pci_wr(0x08021814, RT3883_PCI_REG_ID(0));
404 rt3883_pci_wr(0x00800001, RT3883_PCI_REG_CLASS(0));
405 rt3883_pci_wr(0x28801814, RT3883_PCI_REG_SUBID(0));
408 rt3883_pci_wr(0x01ff0000, RT3883_PCI_REG_BAR0SETUP_ADDR(1));
409 rt3883_pci_wr(RT3883_MEMORY_BASE
, RT3883_PCI_REG_IMBASEBAR0_ADDR(1));
410 rt3883_pci_wr(0x08021814, RT3883_PCI_REG_ID(1));
411 rt3883_pci_wr(0x06040001, RT3883_PCI_REG_CLASS(1));
412 rt3883_pci_wr(0x28801814, RT3883_PCI_REG_SUBID(1));
414 rt3883_pci_irq_init();
417 val
= rt3883_pci_read_u32(0, 0x01, 0, PCI_COMMAND
);
419 rt3883_pci_write_u32(0, 0x01, 0, PCI_COMMAND
, val
);
422 val
= rt3883_pci_read_u32(0, 0x00, 0, PCI_COMMAND
);
424 rt3883_pci_write_u32(0, 0x00, 0, PCI_COMMAND
, val
);
426 ioport_resource
.start
= rt3883_pci_io_resource
.start
;
427 ioport_resource
.end
= rt3883_pci_io_resource
.end
;
429 register_pci_controller(&rt3883_pci_controller
);
432 int __init
pcibios_map_irq(const struct pci_dev
*dev
, u8 slot
, u8 pin
)
436 switch (dev
->bus
->number
) {
438 switch (PCI_SLOT(dev
->devfn
)) {
440 rt3883_pci_wr(0x03ff0001,
441 RT3883_PCI_REG_BAR0SETUP_ADDR(0));
442 rt3883_pci_wr(0x03ff0001,
443 RT3883_PCI_REG_BAR0SETUP_ADDR(1));
445 rt3883_pci_write_u32(0, 0x00, 0, PCI_BASE_ADDRESS_0
,
447 rt3883_pci_read_u32(0, 0x00, 0, PCI_BASE_ADDRESS_0
);
449 irq
= RT3883_CPU_IRQ_PCI
;
452 rt3883_pci_write_u32(0, 0x01, 0, PCI_IO_BASE
,
456 irq
= RT3883_PCI_IRQ_PCI0
;
459 irq
= RT3883_PCI_IRQ_PCI1
;
465 irq
= RT3883_PCI_IRQ_PCIE
;
469 dev_err(&dev
->dev
, "no IRQ specified\n");
476 void __init
rt3883_pci_set_plat_dev_init(int (*f
)(struct pci_dev
*dev
))
478 rt3883_pci_plat_dev_init
= f
;
481 int pcibios_plat_dev_init(struct pci_dev
*dev
)
483 if (rt3883_pci_plat_dev_init
)
484 return rt3883_pci_plat_dev_init(dev
);