1 #include <linux/types.h>
4 #include <linux/init.h>
6 #include <asm/mach-ralink/rt288x.h>
7 #include <asm/mach-ralink/rt288x_regs.h>
9 #define RT2880_PCI_SLOT1_BASE 0x20000000
11 #define RT2880_PCI_REG_PCICFG_ADDR 0x00
12 #define RT2880_PCI_REG_PCIMSK_ADDR 0x0c
13 #define RT2880_PCI_REG_BAR0SETUP_ADDR 0x10
14 #define RT2880_PCI_REG_IMBASEBAR0_ADDR 0x18
15 #define RT2880_PCI_REG_CONFIG_ADDR 0x20
16 #define RT2880_PCI_REG_CONFIG_DATA 0x24
17 #define RT2880_PCI_REG_MEMBASE 0x28
18 #define RT2880_PCI_REG_IOBASE 0x2c
19 #define RT2880_PCI_REG_ID 0x30
20 #define RT2880_PCI_REG_CLASS 0x34
21 #define RT2880_PCI_REG_SUBID 0x38
22 #define RT2880_PCI_REG_ARBCTL 0x80
24 #define PCI_ACCESS_READ 0
25 #define PCI_ACCESS_WRITE 1
27 void __iomem *rt2880_pci_base;
29 static u32 rt2880_pci_reg_read(u32 reg)
31 return readl(rt2880_pci_base + reg);
34 static void rt2880_pci_reg_write(u32 val, u32 reg)
36 writel(val, rt2880_pci_base + reg);
39 static int config_access(unsigned char access_type, struct pci_bus *bus,
40 unsigned int devfn, unsigned char where, u32 *data)
42 unsigned int slot = PCI_SLOT(devfn);
44 u8 func = PCI_FUNC(devfn);
46 address = (bus->number << 16) | (slot << 11) | (func << 8) |
47 (where & 0xfc) | 0x80000000;
49 rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
50 if (access_type == PCI_ACCESS_WRITE)
51 rt2880_pci_reg_write(*data, RT2880_PCI_REG_CONFIG_DATA);
53 *data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
58 static int rt2880_pci_config_read(struct pci_bus *bus, unsigned int devfn,
59 int where, int size, u32 *val)
63 if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
64 return PCIBIOS_DEVICE_NOT_FOUND;
67 *val = (data >> ((where & 3) << 3)) & 0xff;
69 *val = (data >> ((where & 3) << 3)) & 0xffff;
73 return PCIBIOS_SUCCESSFUL;
76 static int rt2880_pci_config_write(struct pci_bus *bus, unsigned int devfn,
77 int where, int size, u32 val)
84 if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
85 return PCIBIOS_DEVICE_NOT_FOUND;
87 data = (data & ~(0xff << ((where & 3) << 3))) |
88 (val << ((where & 3) << 3));
90 data = (data & ~(0xffff << ((where & 3) << 3))) |
91 (val << ((where & 3) << 3));
94 if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
95 return PCIBIOS_DEVICE_NOT_FOUND;
97 return PCIBIOS_SUCCESSFUL;
100 static struct pci_ops rt2880_pci_ops = {
101 .read = rt2880_pci_config_read,
102 .write = rt2880_pci_config_write,
105 static struct resource rt2880_pci_io_resource = {
106 .name = "PCI MEM space",
109 .flags = IORESOURCE_MEM,
112 static struct resource rt2880_pci_mem_resource = {
113 .name = "PCI IO space",
116 .flags = IORESOURCE_IO,
119 static struct pci_controller rt2880_pci_controller = {
120 .pci_ops = &rt2880_pci_ops,
121 .mem_resource = &rt2880_pci_io_resource,
122 .io_resource = &rt2880_pci_mem_resource,
125 void inline read_config(unsigned long bus, unsigned long dev,
126 unsigned long func, unsigned long reg,
129 unsigned long address;
131 address = (bus << 16) | (dev << 11) | (func << 8) | (reg & 0xfc) |
133 rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
134 *val = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
137 void inline write_config(unsigned long bus, unsigned long dev,
138 unsigned long func, unsigned long reg,
141 unsigned long address;
143 address = (bus << 16) | (dev << 11) | (func << 8) | (reg & 0xfc) |
145 rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
146 rt2880_pci_reg_write(val, RT2880_PCI_REG_CONFIG_DATA);
149 int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
155 if (dev->bus->number != 0)
158 switch (PCI_SLOT(dev->devfn)) {
160 write_config(0, 0, 0, PCI_BASE_ADDRESS_0, 0x08000000);
161 read_config(0, 0, 0, PCI_BASE_ADDRESS_0, &val);
164 irq = RT288X_CPU_IRQ_PCI;
167 printk("%s:%s[%d] trying to alloc unknown pci irq\n",
168 __FILE__, __func__, __LINE__);
173 pci_write_config_byte((struct pci_dev*)dev, PCI_CACHE_LINE_SIZE, 0x14);
174 pci_write_config_byte((struct pci_dev*)dev, PCI_LATENCY_TIMER, 0xFF);
175 pci_read_config_word((struct pci_dev*)dev, PCI_COMMAND, &cmd);
176 cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
177 PCI_COMMAND_INVALIDATE | PCI_COMMAND_FAST_BACK |
178 PCI_COMMAND_SERR | PCI_COMMAND_WAIT | PCI_COMMAND_PARITY;
179 pci_write_config_word((struct pci_dev*)dev, PCI_COMMAND, cmd);
180 pci_write_config_byte((struct pci_dev*)dev, PCI_INTERRUPT_LINE,
185 static int __init rt2880_pci_init(void)
187 unsigned long val = 0;
190 rt2880_pci_base = ioremap_nocache(RT2880_PCI_BASE, PAGE_SIZE);
192 rt2880_pci_reg_write(0, RT2880_PCI_REG_PCICFG_ADDR);
193 for(i = 0; i < 0xfffff; i++) {}
195 rt2880_pci_reg_write(0x79, RT2880_PCI_REG_ARBCTL);
196 rt2880_pci_reg_write(0x07FF0001, RT2880_PCI_REG_BAR0SETUP_ADDR);
197 rt2880_pci_reg_write(RT2880_PCI_SLOT1_BASE, RT2880_PCI_REG_MEMBASE);
198 rt2880_pci_reg_write(0x00460000, RT2880_PCI_REG_IOBASE);
199 rt2880_pci_reg_write(0x08000000, RT2880_PCI_REG_IMBASEBAR0_ADDR);
200 rt2880_pci_reg_write(0x08021814, RT2880_PCI_REG_ID);
201 rt2880_pci_reg_write(0x00800001, RT2880_PCI_REG_CLASS);
202 rt2880_pci_reg_write(0x28801814, RT2880_PCI_REG_SUBID);
203 rt2880_pci_reg_write(0x000c0000, RT2880_PCI_REG_PCIMSK_ADDR);
204 write_config(0, 0, 0, PCI_BASE_ADDRESS_0, 0x08000000);
205 read_config(0, 0, 0, PCI_BASE_ADDRESS_0, &val);
207 register_pci_controller(&rt2880_pci_controller);
211 int pcibios_plat_dev_init(struct pci_dev *dev)
216 struct pci_fixup pcibios_fixups[] = {
220 arch_initcall(rt2880_pci_init);