ramips: add register access functions into the rt288x pci code
[10.03/openwrt.git] / target / linux / ramips / files / arch / mips / pci / pci-rt288x.c
1 #include <linux/types.h>
2 #include <linux/pci.h>
3 #include <linux/io.h>
4 #include <linux/init.h>
5
6 #include <asm/mach-ralink/rt288x.h>
7 #include <asm/mach-ralink/rt288x_regs.h>
8
9 #define RT2880_PCI_SLOT1_BASE           0x20000000
10
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
23
24 #define PCI_ACCESS_READ  0
25 #define PCI_ACCESS_WRITE 1
26
27 void __iomem *rt2880_pci_base;
28
29 static u32 rt2880_pci_reg_read(u32 reg)
30 {
31         return readl(rt2880_pci_base + reg);
32 }
33
34 static void rt2880_pci_reg_write(u32 val, u32 reg)
35 {
36         writel(val, rt2880_pci_base + reg);
37 }
38
39 static int config_access(unsigned char access_type, struct pci_bus *bus,
40                          unsigned int devfn, unsigned char where, u32 *data)
41 {
42         unsigned int slot = PCI_SLOT(devfn);
43         unsigned int address;
44         u8 func = PCI_FUNC(devfn);
45
46         address = (bus->number << 16) | (slot << 11) | (func << 8) |
47                   (where & 0xfc) | 0x80000000;
48
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);
52         else
53                 *data = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
54
55         return 0;
56 }
57
58 static int rt2880_pci_config_read(struct pci_bus *bus, unsigned int devfn,
59                                   int where, int size, u32 *val)
60 {
61         u32 data = 0;
62
63         if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
64                 return PCIBIOS_DEVICE_NOT_FOUND;
65
66         if (size == 1)
67                 *val = (data >> ((where & 3) << 3)) & 0xff;
68         else if (size == 2)
69                 *val = (data >> ((where & 3) << 3)) & 0xffff;
70         else
71                 *val = data;
72
73         return PCIBIOS_SUCCESSFUL;
74 }
75
76 static int rt2880_pci_config_write(struct pci_bus *bus, unsigned int devfn,
77                                    int where, int size, u32 val)
78 {
79         u32 data = 0;
80
81         if (size == 4) {
82                 data = val;
83         } else {
84                 if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
85                         return PCIBIOS_DEVICE_NOT_FOUND;
86                 if (size == 1)
87                         data = (data & ~(0xff << ((where & 3) << 3))) |
88                                (val << ((where & 3) << 3));
89                 else if (size == 2)
90                         data = (data & ~(0xffff << ((where & 3) << 3))) |
91                                (val << ((where & 3) << 3));
92         }
93
94         if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
95                 return PCIBIOS_DEVICE_NOT_FOUND;
96
97         return PCIBIOS_SUCCESSFUL;
98 }
99
100 static struct pci_ops rt2880_pci_ops = {
101         .read   = rt2880_pci_config_read,
102         .write  = rt2880_pci_config_write,
103 };
104
105 static struct resource rt2880_pci_io_resource = {
106         .name   = "PCI MEM space",
107         .start  = 0x20000000,
108         .end    = 0x2FFFFFFF,
109         .flags  = IORESOURCE_MEM,
110 };
111
112 static struct resource rt2880_pci_mem_resource = {
113         .name   = "PCI IO space",
114         .start  = 0x00460000,
115         .end    = 0x0046FFFF,
116         .flags  = IORESOURCE_IO,
117 };
118
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,
123 };
124
125 void inline read_config(unsigned long bus, unsigned long dev,
126                         unsigned long func, unsigned long reg,
127                         unsigned long *val)
128 {
129         unsigned long address;
130
131         address = (bus << 16) | (dev << 11) | (func << 8) | (reg & 0xfc) |
132                   0x80000000;
133         rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
134         *val = rt2880_pci_reg_read(RT2880_PCI_REG_CONFIG_DATA);
135 }
136
137 void inline write_config(unsigned long bus, unsigned long dev,
138                          unsigned long func, unsigned long reg,
139                          unsigned long val)
140 {
141         unsigned long address;
142
143         address = (bus << 16) | (dev << 11) | (func << 8) | (reg & 0xfc) |
144                   0x80000000;
145         rt2880_pci_reg_write(address, RT2880_PCI_REG_CONFIG_ADDR);
146         rt2880_pci_reg_write(val, RT2880_PCI_REG_CONFIG_DATA);
147 }
148
149 int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
150 {
151         u16 cmd;
152         unsigned long val;
153         int irq = -1;
154
155         if (dev->bus->number != 0)
156                 return 0;
157
158         switch (PCI_SLOT(dev->devfn)) {
159         case 0x00:
160                 write_config(0, 0, 0, PCI_BASE_ADDRESS_0, 0x08000000);
161                 read_config(0, 0, 0, PCI_BASE_ADDRESS_0, &val);
162                 break;
163         case 0x11:
164                 irq = RT288X_CPU_IRQ_PCI;
165                 break;
166         default:
167                 printk("%s:%s[%d] trying to alloc unknown pci irq\n",
168                        __FILE__, __func__, __LINE__);
169                 BUG();
170                 break;
171         }
172
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,
181                               dev->irq);
182         return irq;
183 }
184
185 static int __init rt2880_pci_init(void)
186 {
187         unsigned long val = 0;
188         int i;
189
190         rt2880_pci_base = ioremap_nocache(RT2880_PCI_BASE, PAGE_SIZE);
191
192         rt2880_pci_reg_write(0, RT2880_PCI_REG_PCICFG_ADDR);
193         for(i = 0; i < 0xfffff; i++) {}
194
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);
206
207         register_pci_controller(&rt2880_pci_controller);
208         return 0;
209 }
210
211 int pcibios_plat_dev_init(struct pci_dev *dev)
212 {
213         return 0;
214 }
215
216 struct pci_fixup pcibios_fixups[] = {
217         {0}
218 };
219
220 arch_initcall(rt2880_pci_init);