ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / pci / pci-ocelot-g.c
1 /*
2  * Copyright 2002 Momentum Computer
3  * Author: Matthew Dharm <mdharm@momenco.com>
4  *
5  *  This program is free software; you can redistribute  it and/or modify it
6  *  under  the terms of  the GNU General  Public License as published by the
7  *  Free Software Foundation;  either version 2 of the  License, or (at your
8  *  option) any later version.
9  *
10  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
11  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
12  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
13  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
14  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
15  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
16  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
17  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
18  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
19  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20  *
21  *  You should have received a copy of the  GNU General Public License along
22  *  with this program; if not, write  to the Free Software Foundation, Inc.,
23  *  675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25 #include <linux/types.h>
26 #include <linux/pci.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <asm/pci.h>
30 #include <asm/io.h>
31 #include "gt64240.h"
32
33 #include <linux/init.h>
34
35 #define SELF 0
36 #define MASTER_ABORT_BIT 0x100
37
38 /*
39  * These functions and structures provide the BIOS scan and mapping of the PCI
40  * devices.
41  */
42
43 void gt64240_board_pcibios_fixup_bus(struct pci_bus *c);
44
45 /*  Functions to implement "pci ops"  */
46 static int galileo_pcibios_read_config_word(int bus, int devfn,
47                                             int offset, u16 * val);
48 static int galileo_pcibios_read_config_byte(int bus, int devfn,
49                                             int offset, u8 * val);
50 static int galileo_pcibios_read_config_dword(int bus, int devfn,
51                                              int offset, u32 * val);
52 static int galileo_pcibios_write_config_byte(int bus, int devfn,
53                                              int offset, u8 val);
54 static int galileo_pcibios_write_config_word(int bus, int devfn,
55                                              int offset, u16 val);
56 static int galileo_pcibios_write_config_dword(int bus, int devfn,
57                                               int offset, u32 val);
58
59 static int pci_read(struct pci_bus *bus, unsigned int devfs, int where,
60                     int size, u32 * val);
61 static int pci_write(struct pci_bus *bus, unsigned int devfs, int where,
62                      int size, u32 val);
63
64 /*
65  *  General-purpose PCI functions.
66  */
67
68
69 /*
70  * pci_range_ck -
71  *
72  * Check if the pci device that are trying to access does really exists
73  * on the evaluation board.
74  *
75  * Inputs :
76  * bus - bus number (0 for PCI 0 ; 1 for PCI 1)
77  * dev - number of device on the specific pci bus
78  *
79  * Outpus :
80  * 0 - if OK , 1 - if failure
81  */
82 static __inline__ int pci_range_ck(unsigned char bus, unsigned char dev)
83 {
84         /* Accessing device 31 crashes the GT-64240. */
85         if (dev < 5)
86                 return 0;
87         return -1;
88 }
89
90 /*
91  * galileo_pcibios_(read/write)_config_(dword/word/byte) -
92  *
93  * reads/write a dword/word/byte register from the configuration space
94  * of a device.
95  *
96  * Note that bus 0 and bus 1 are local, and we assume all other busses are
97  * bridged from bus 1.  This is a safe assumption, since any other
98  * configuration will require major modifications to the CP7000G
99  *
100  * Inputs :
101  * bus - bus number
102  * dev - device number
103  * offset - register offset in the configuration space
104  * val - value to be written / read
105  *
106  * Outputs :
107  * PCIBIOS_SUCCESSFUL when operation was succesfull
108  * PCIBIOS_DEVICE_NOT_FOUND when the bus or dev is errorneous
109  * PCIBIOS_BAD_REGISTER_NUMBER when accessing non aligned
110  */
111
112 static int galileo_pcibios_read_config_dword(int bus, int devfn,
113                                              int offset, u32 * val)
114 {
115         int dev, func;
116         uint32_t address_reg, data_reg;
117         uint32_t address;
118
119         dev = PCI_SLOT(devfn);
120         func = PCI_FUNC(devfn);
121
122         /* verify the range */
123         if (pci_range_ck(bus, dev))
124                 return PCIBIOS_DEVICE_NOT_FOUND;
125
126         /* select the GT-64240 registers to communicate with the PCI bus */
127         if (bus == 0) {
128                 address_reg = PCI_0CONFIGURATION_ADDRESS;
129                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
130                 GT_WRITE(PCI_0ERROR_CAUSE, ~MASTER_ABORT_BIT);
131         } else {
132                 address_reg = PCI_1CONFIGURATION_ADDRESS;
133                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
134                 GT_WRITE(PCI_1ERROR_CAUSE, ~MASTER_ABORT_BIT);
135                 if (bus == 1)
136                         bus = 0;
137         }
138
139         address = (bus << 16) | (dev << 11) | (func << 8) |
140             (offset & 0xfc) | 0x80000000;
141
142         /* start the configuration cycle */
143         GT_WRITE(address_reg, address);
144
145         /* read the data */
146         GT_READ(data_reg, val);
147
148         return PCIBIOS_SUCCESSFUL;
149 }
150
151
152 static int galileo_pcibios_read_config_word(int bus, int devfn,
153                                             int offset, u16 * val)
154 {
155         int dev, func;
156         uint32_t address_reg, data_reg;
157         uint32_t address;
158
159         dev = PCI_SLOT(devfn);
160         func = PCI_FUNC(devfn);
161
162         /* verify the range */
163         if (pci_range_ck(bus, dev))
164                 return PCIBIOS_DEVICE_NOT_FOUND;
165
166         /* select the GT-64240 registers to communicate with the PCI bus */
167         if (bus == 0) {
168                 address_reg = PCI_0CONFIGURATION_ADDRESS;
169                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
170                 GT_WRITE(PCI_0ERROR_CAUSE, ~MASTER_ABORT_BIT);
171         } else {
172                 address_reg = PCI_1CONFIGURATION_ADDRESS;
173                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
174                 GT_WRITE(PCI_1ERROR_CAUSE, ~MASTER_ABORT_BIT);
175                 if (bus == 1)
176                         bus = 0;
177         }
178
179         address = (bus << 16) | (dev << 11) | (func << 8) |
180             (offset & 0xfc) | 0x80000000;
181
182         /* start the configuration cycle */
183         GT_WRITE(address_reg, address);
184
185         /* read the data */
186         GT_READ_16(data_reg + (offset & 0x3), val);
187
188         return PCIBIOS_SUCCESSFUL;
189 }
190
191 static int galileo_pcibios_read_config_byte(int bus, int devfn,
192                                             int offset, u8 * val)
193 {
194         int dev, func;
195         uint32_t address_reg, data_reg;
196         uint32_t address;
197
198         dev = PCI_SLOT(devfn);
199         func = PCI_FUNC(devfn);
200
201         /* verify the range */
202         if (pci_range_ck(bus, dev))
203                 return PCIBIOS_DEVICE_NOT_FOUND;
204
205         /* select the GT-64240 registers to communicate with the PCI bus */
206         if (bus == 0) {
207                 address_reg = PCI_0CONFIGURATION_ADDRESS;
208                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
209         } else {
210                 address_reg = PCI_1CONFIGURATION_ADDRESS;
211                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
212                 if (bus == 1)
213                         bus = 0;
214         }
215
216         address = (bus << 16) | (dev << 11) | (func << 8) |
217             (offset & 0xfc) | 0x80000000;
218
219         /* start the configuration cycle */
220         GT_WRITE(address_reg, address);
221
222         /* write the data */
223         GT_READ_8(data_reg + (offset & 0x3), val);
224
225         return PCIBIOS_SUCCESSFUL;
226 }
227
228 static int galileo_pcibios_write_config_dword(int bus, int devfn,
229                                               int offset, u32 val)
230 {
231         int dev, func;
232         uint32_t address_reg, data_reg;
233         uint32_t address;
234
235         dev = PCI_SLOT(devfn);
236         func = PCI_FUNC(devfn);
237
238         /* verify the range */
239         if (pci_range_ck(bus, dev))
240                 return PCIBIOS_DEVICE_NOT_FOUND;
241
242         /* select the GT-64240 registers to communicate with the PCI bus */
243         if (bus == 0) {
244                 address_reg = PCI_0CONFIGURATION_ADDRESS;
245                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
246         } else {
247                 address_reg = PCI_1CONFIGURATION_ADDRESS;
248                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
249                 if (bus == 1)
250                         bus = 0;
251         }
252
253         address = (bus << 16) | (dev << 11) | (func << 8) |
254             (offset & 0xfc) | 0x80000000;
255
256         /* start the configuration cycle */
257         GT_WRITE(address_reg, address);
258
259         /* write the data */
260         GT_WRITE(data_reg, val);
261
262         return PCIBIOS_SUCCESSFUL;
263 }
264
265
266 static int galileo_pcibios_write_config_word(int bus, int devfn,
267                                              int offset, u16 val)
268 {
269         int dev, func;
270         uint32_t address_reg, data_reg;
271         uint32_t address;
272
273         dev = PCI_SLOT(devfn);
274         func = PCI_FUNC(devfn);
275
276         /* verify the range */
277         if (pci_range_ck(bus, dev))
278                 return PCIBIOS_DEVICE_NOT_FOUND;
279
280         /* select the GT-64240 registers to communicate with the PCI bus */
281         if (bus == 0) {
282                 address_reg = PCI_0CONFIGURATION_ADDRESS;
283                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
284         } else {
285                 address_reg = PCI_1CONFIGURATION_ADDRESS;
286                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
287                 if (bus == 1)
288                         bus = 0;
289         }
290
291         address = (bus << 16) | (dev << 11) | (func << 8) |
292             (offset & 0xfc) | 0x80000000;
293
294         /* start the configuration cycle */
295         GT_WRITE(address_reg, address);
296
297         /* write the data */
298         GT_WRITE_16(data_reg + (offset & 0x3), val);
299
300         return PCIBIOS_SUCCESSFUL;
301 }
302
303 static int galileo_pcibios_write_config_byte(int bus, int devfn,
304                                              int offset, u8 val)
305 {
306         int dev, func;
307         uint32_t address_reg, data_reg;
308         uint32_t address;
309
310         dev = PCI_SLOT(devfn);
311         func = PCI_FUNC(devfn);
312
313         /* verify the range */
314         if (pci_range_ck(bus, dev))
315                 return PCIBIOS_DEVICE_NOT_FOUND;
316
317         /* select the GT-64240 registers to communicate with the PCI bus */
318         if (bus == 0) {
319                 address_reg = PCI_0CONFIGURATION_ADDRESS;
320                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
321         } else {
322                 address_reg = PCI_1CONFIGURATION_ADDRESS;
323                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
324                 if (bus == 1)
325                         bus = 0;
326         }
327
328         address = (bus << 16) | (dev << 11) | (func << 8) |
329             (offset & 0xfc) | 0x80000000;
330
331         /* start the configuration cycle */
332         GT_WRITE(address_reg, address);
333
334         /* write the data */
335         GT_WRITE_8(data_reg + (offset & 0x3), val);
336
337         return PCIBIOS_SUCCESSFUL;
338 }
339
340 struct pci_ops galileo_pci_ops = {
341         .read = pci_read,
342         .write = pci_write
343 };
344
345 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
346                     int size, u32 * val)
347 {
348         switch (size) {
349         case 1:
350                 return galileo_pcibios_read_config_byte(bus->number,
351                                                         devfn, where,
352                                                         (u8 *) val);
353         case 2:
354                 return galileo_pcibios_read_config_word(bus->number,
355                                                         devfn, where,
356                                                         (u16 *) val);
357         case 4:
358                 return galileo_pcibios_read_config_dword(bus->number,
359                                                          devfn, where,
360                                                          (u32 *) val);
361         }
362         return PCIBIOS_FUNC_NOT_SUPPORTED;
363 }
364
365 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
366                      int size, u32 val)
367 {
368         switch (size) {
369         case 1:
370                 return galileo_pcibios_write_config_byte(bus->number,
371                                                          devfn, where,
372                                                          val);
373         case 2:
374                 return galileo_pcibios_write_config_word(bus->number,
375                                                          devfn, where,
376                                                          val);
377         case 4:
378                 return galileo_pcibios_write_config_dword(bus->number,
379                                                           devfn, where,
380                                                           val);
381         }
382         return PCIBIOS_FUNC_NOT_SUPPORTED;
383 }
384
385 struct pci_fixup pcibios_fixups[] = {
386         {0}
387 };
388
389 void __devinit pcibios_fixup_bus(struct pci_bus *c)
390 {
391         gt64240_board_pcibios_fixup_bus(c);
392 }
393
394
395 /********************************************************************
396 * pci0P2PConfig - This function set the PCI_0 P2P configurate.
397 *                 For more information on the P2P read PCI spec.
398 *
399 * Inputs:  unsigned int SecondBusLow - Secondery PCI interface Bus Range Lower
400 *                                      Boundry.
401 *          unsigned int SecondBusHigh - Secondry PCI interface Bus Range upper
402 *                                      Boundry.
403 *          unsigned int busNum - The CPI bus number to which the PCI interface
404 *                                      is connected.
405 *          unsigned int devNum - The PCI interface's device number.
406 *
407 * Returns:  true.
408 */
409 void pci0P2PConfig(unsigned int SecondBusLow, unsigned int SecondBusHigh,
410                    unsigned int busNum, unsigned int devNum)
411 {
412         uint32_t regData;
413
414         regData = (SecondBusLow & 0xff) | ((SecondBusHigh & 0xff) << 8) |
415             ((busNum & 0xff) << 16) | ((devNum & 0x1f) << 24);
416         GT_WRITE(PCI_0P2P_CONFIGURATION, regData);
417 }
418
419 /********************************************************************
420 * pci1P2PConfig - This function set the PCI_1 P2P configurate.
421 *                 For more information on the P2P read PCI spec.
422 *
423 * Inputs:  unsigned int SecondBusLow - Secondery PCI interface Bus Range Lower
424 *               Boundry.
425 *          unsigned int SecondBusHigh - Secondry PCI interface Bus Range upper
426 *               Boundry.
427 *          unsigned int busNum - The CPI bus number to which the PCI interface
428 *               is connected.
429 *          unsigned int devNum - The PCI interface's device number.
430 *
431 * Returns:  true.
432 */
433 void pci1P2PConfig(unsigned int SecondBusLow, unsigned int SecondBusHigh,
434                    unsigned int busNum, unsigned int devNum)
435 {
436         uint32_t regData;
437
438         regData = (SecondBusLow & 0xff) | ((SecondBusHigh & 0xff) << 8) |
439             ((busNum & 0xff) << 16) | ((devNum & 0x1f) << 24);
440         GT_WRITE(PCI_1P2P_CONFIGURATION, regData);
441 }
442
443 #define PCI0_STATUS_COMMAND_REG                 0x4
444 #define PCI1_STATUS_COMMAND_REG                 0x84
445
446 static int __init pcibios_init(void)
447 {
448         /* Reset PCI I/O and PCI MEM values */
449         ioport_resource.start = 0xe0000000;
450         ioport_resource.end = 0xe0000000 + 0x20000000 - 1;
451         iomem_resource.start = 0xc0000000;
452         iomem_resource.end = 0xc0000000 + 0x20000000 - 1;
453
454         pci_scan_bus(0, &galileo_pci_ops, NULL);
455         pci_scan_bus(1, &galileo_pci_ops, NULL);
456
457         return 0;
458 }
459
460 subsys_initcall(pcibios_init);