ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sh / boards / mpc1211 / pci.c
1 /*
2  *      Low-Level PCI Support for the MPC-1211(CTP/PCI/MPC-SH02)
3  *
4  *  (c) 2002-2003 Saito.K & Jeanne
5  *
6  *  Dustin McIntire (dustin@sensoria.com)
7  *      Derived from arch/i386/kernel/pci-*.c which bore the message:
8  *      (c) 1999--2000 Martin Mares <mj@ucw.cz>
9  *      
10  *  May be copied or modified under the terms of the GNU General Public
11  *  License.  See linux/COPYING for more information.
12  *
13  */
14 #include <linux/config.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <linux/pci.h>
20 #include <linux/sched.h>
21 #include <linux/ioport.h>
22 #include <linux/errno.h>
23 #include <linux/irq.h>
24 #include <linux/interrupt.h>
25
26 #include <asm/machvec.h>
27 #include <asm/io.h>
28 #include <asm/mpc1211/pci.h>
29
30 static struct resource mpcpci_io_resource = {
31         "MPCPCI IO",
32         0x00000000,
33         0xffffffff,
34         IORESOURCE_IO
35 };
36
37 static struct resource mpcpci_mem_resource = {
38         "MPCPCI mem",
39         0x00000000,
40         0xffffffff,
41         IORESOURCE_MEM
42 };
43
44 static struct pci_ops pci_direct_conf1;
45 struct pci_channel board_pci_channels[] = {
46         {&pci_direct_conf1, &mpcpci_io_resource, &mpcpci_mem_resource, 0, 256},
47         {NULL, NULL, NULL, 0, 0},
48 };
49
50 /*
51  * Direct access to PCI hardware...
52  */
53
54
55 #define CONFIG_CMD(bus, devfn, where) (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
56
57 /*
58  * Functions for accessing PCI configuration space with type 1 accesses
59  */
60 static int pci_conf1_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
61 {
62         u32 word;
63         unsigned long flags;
64
65         /* 
66          * PCIPDR may only be accessed as 32 bit words, 
67          * so we must do byte alignment by hand 
68          */
69         local_irq_save(flags);
70         writel(CONFIG_CMD(bus,devfn,where), PCIPAR);
71         word = readl(PCIPDR);
72         local_irq_restore(flags);
73
74         switch (size) {
75         case 1:
76                 switch (where & 0x3) {
77                 case 3:
78                         *value = (u8)(word >> 24);
79                         break;
80                 case 2:
81                         *value = (u8)(word >> 16);
82                         break;
83                 case 1:
84                         *value = (u8)(word >> 8);
85                         break;
86                 default:
87                         *value = (u8)word;
88                         break;
89                 }
90                 break;
91         case 2:
92                 switch (where & 0x3) {
93                 case 3:
94                         *value = (u16)(word >> 24);
95                         local_irq_save(flags);
96                         writel(CONFIG_CMD(bus,devfn,(where+1)), PCIPAR);
97                         word = readl(PCIPDR);
98                         local_irq_restore(flags);
99                         *value |= ((word & 0xff) << 8);
100                         break;
101                 case 2:
102                         *value = (u16)(word >> 16);
103                         break;
104                 case 1:
105                         *value = (u16)(word >> 8);
106                         break;
107                 default:
108                         *value = (u16)word;
109                         break;
110                 }
111                 break;
112         case 4:
113                 *value = word;
114                 break;
115         }
116         PCIDBG(4,"pci_conf1_read@0x%08x=0x%x\n", CONFIG_CMD(bus,devfn,where),*value); 
117         return PCIBIOS_SUCCESSFUL;    
118 }
119
120 /* 
121  * Since MPC-1211 only does 32bit access we'll have to do a read,mask,write operation.  
122  * We'll allow an odd byte offset, though it should be illegal.
123  */ 
124 static int pci_conf1_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
125 {
126         u32 word,mask = 0;
127         unsigned long flags;
128         u32 shift = (where & 3) * 8;
129
130         if(size == 1) {
131                 mask = ((1 << 8) - 1) << shift;  // create the byte mask
132         } else if(size == 2){
133                 if(shift == 24)
134                         return PCIBIOS_BAD_REGISTER_NUMBER;           
135                 mask = ((1 << 16) - 1) << shift;  // create the word mask
136         }
137         local_irq_save(flags);
138         writel(CONFIG_CMD(bus,devfn,where), PCIPAR);
139         if(size == 4){
140                 writel(value, PCIPDR);
141                 local_irq_restore(flags);
142                 PCIDBG(4,"pci_conf1_write@0x%08x=0x%x\n", CONFIG_CMD(bus,devfn,where),value);
143                 return PCIBIOS_SUCCESSFUL;
144         }
145         word = readl(PCIPDR);
146         word &= ~mask;
147         word |= ((value << shift) & mask);
148         writel(word, PCIPDR);
149         local_irq_restore(flags);
150         PCIDBG(4,"pci_conf1_write@0x%08x=0x%x\n", CONFIG_CMD(bus,devfn,where),word);
151         return PCIBIOS_SUCCESSFUL;
152 }
153
154 #undef CONFIG_CMD
155
156 static struct pci_ops pci_direct_conf1 = {
157         .read =         pci_conf1_read,
158         .write =        pci_conf1_write,
159 };
160
161 static void __devinit quirk_ali_ide_ports(struct pci_dev *dev)
162 {
163         dev->resource[0].start = 0x1f0;
164         dev->resource[0].end   = 0x1f7;
165         dev->resource[0].flags = IORESOURCE_IO;
166         dev->resource[1].start = 0x3f6;
167         dev->resource[1].end   = 0x3f6;
168         dev->resource[1].flags = IORESOURCE_IO;
169         dev->resource[2].start = 0x170;
170         dev->resource[2].end   = 0x177;
171         dev->resource[2].flags = IORESOURCE_IO;
172         dev->resource[3].start = 0x376;
173         dev->resource[3].end   = 0x376;
174         dev->resource[3].flags = IORESOURCE_IO;
175         dev->resource[4].start = 0xf000;
176         dev->resource[4].end   = 0xf00f;
177         dev->resource[4].flags = IORESOURCE_IO;
178 }
179
180
181 /* Add future fixups here... */
182 struct pci_fixup pcibios_fixups[] = {
183         { PCI_FIXUP_HEADER, PCI_VENDOR_ID_AL,
184           PCI_DEVICE_ID_AL_M5229, quirk_ali_ide_ports },
185         { 0 }
186 };
187
188 char * __devinit pcibios_setup(char *str)
189 {
190         return str;
191 }
192
193 /*
194  *  Called after each bus is probed, but before its children
195  *  are examined.
196  */
197
198 void __init pcibios_fixup_bus(struct pci_bus *b)
199 {
200         pci_read_bridge_bases(b);
201 }
202
203 /* 
204  *      IRQ functions 
205  */
206 static inline u8 bridge_swizzle(u8 pin, u8 slot)
207 {
208         return (((pin-1) + slot) % 4) + 1;
209 }
210
211 static inline u8 bridge_swizzle_pci_1(u8 pin, u8 slot)
212 {
213         return (((pin-1) - slot) & 3) + 1;
214 }
215
216 static u8 __init mpc1211_swizzle(struct pci_dev *dev, u8 *pinp)
217 {
218         unsigned long flags;
219         u8 pin = *pinp;
220         u32 word;
221
222         for ( ; dev->bus->self; dev = dev->bus->self) {
223                 if (!pin)
224                         continue;
225
226                 if (dev->bus->number == 1) {
227                         local_irq_save(flags);
228                         writel(0x80000000 | 0x2c, PCIPAR);
229                         word = readl(PCIPDR);
230                         local_irq_restore(flags);
231                         word >>= 16;
232
233                         if (word == 0x0001)
234                                 pin = bridge_swizzle_pci_1(pin, PCI_SLOT(dev->devfn));
235                         else
236                                 pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
237                 } else
238                         pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
239         }
240
241         *pinp = pin;
242
243         return PCI_SLOT(dev->devfn);
244 }
245
246 static int __init map_mpc1211_irq(struct pci_dev *dev, u8 slot, u8 pin)
247 {
248         int irq = -1;
249
250         /* now lookup the actual IRQ on a platform specific basis (pci-'platform'.c) */
251         if (dev->bus->number == 0) {
252                 switch (slot) {
253                 case 13:   irq =  9; break;   /* USB */
254                 case 22:   irq = 10; break;   /* LAN */
255                 default:   irq =  0; break;
256                 }
257         } else {
258                 switch (pin) {
259                 case 0:   irq =  0; break;
260                 case 1:   irq =  7; break;
261                 case 2:   irq =  9; break;
262                 case 3:   irq = 10; break;
263                 case 4:   irq = 11; break;
264                 }
265         }
266
267         if( irq < 0 ) {
268                 PCIDBG(3, "PCI: Error mapping IRQ on device %s\n", pci_name(dev));
269                 return irq;
270         }
271         
272         PCIDBG(2, "Setting IRQ for slot %s to %d\n", pci_name(dev), irq);
273
274         return irq;
275 }
276
277 void __init pcibios_fixup_irqs(void)
278 {
279         pci_fixup_irqs(mpc1211_swizzle, map_mpc1211_irq);
280 }
281
282 void pcibios_align_resource(void *data, struct resource *res,
283                             unsigned long size, unsigned long align)
284 {
285         unsigned long start = res->start;
286
287         if (res->flags & IORESOURCE_IO) {
288                 if (start >= 0x10000UL) {
289                         if ((start & 0xffffUL) < 0x4000UL) {
290                                 start = (start & 0xffff0000UL) + 0x4000UL;
291                         } else if ((start & 0xffffUL) >= 0xf000UL) {
292                                 start = (start & 0xffff0000UL) + 0x10000UL;
293                         }
294                         res->start = start;
295                 } else {
296                         if (start & 0x300) {
297                                 start = (start + 0x3ff) & ~0x3ff;
298                                 res->start = start;
299                         }
300                 }
301         }
302 }
303