7255d230243a26a425930fe7161707bfaa8e0319
[linux-flexiantxendom0-3.2.10.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 <linux/version.h>
30 #include <asm/pci.h>
31 #include <asm/io.h>
32 #include "gt64240.h"
33
34 #include <linux/init.h>
35
36 #define SELF 0
37 #define MASTER_ABORT_BIT 0x100
38
39 /*
40  * These functions and structures provide the BIOS scan and mapping of the PCI
41  * devices.
42  */
43
44 #define MAX_PCI_DEVS 10
45
46 void gt64240_board_pcibios_fixup_bus(struct pci_bus *c);
47
48 /*  Functions to implement "pci ops"  */
49 static int galileo_pcibios_read_config_word(int bus, int devfn,
50                                             int offset, u16 * val);
51 static int galileo_pcibios_read_config_byte(int bus, int devfn,
52                                             int offset, u8 * val);
53 static int galileo_pcibios_read_config_dword(int bus, int devfn,
54                                              int offset, u32 * val);
55 static int galileo_pcibios_write_config_byte(int bus, int devfn,
56                                              int offset, u8 val);
57 static int galileo_pcibios_write_config_word(int bus, int devfn,
58                                              int offset, u16 val);
59 static int galileo_pcibios_write_config_dword(int bus, int devfn,
60                                               int offset, u32 val);
61 #if 0
62 static void galileo_pcibios_set_master(struct pci_dev *dev);
63 #endif
64
65 static int pci_read(struct pci_bus *bus, unsigned int devfs, int where,
66                     int size, u32 * val);
67 static int pci_write(struct pci_bus *bus, unsigned int devfs, int where,
68                      int size, u32 val);
69
70 /*
71  *  General-purpose PCI functions.
72  */
73
74
75 /*
76  * pci_range_ck -
77  *
78  * Check if the pci device that are trying to access does really exists
79  * on the evaluation board.
80  *
81  * Inputs :
82  * bus - bus number (0 for PCI 0 ; 1 for PCI 1)
83  * dev - number of device on the specific pci bus
84  *
85  * Outpus :
86  * 0 - if OK , 1 - if failure
87  */
88 static __inline__ int pci_range_ck(unsigned char bus, unsigned char dev)
89 {
90         /* Accessing device 31 crashes the GT-64240. */
91         if (dev < 5)
92                 return 0;
93         return -1;
94 }
95
96 /*
97  * galileo_pcibios_(read/write)_config_(dword/word/byte) -
98  *
99  * reads/write a dword/word/byte register from the configuration space
100  * of a device.
101  *
102  * Note that bus 0 and bus 1 are local, and we assume all other busses are
103  * bridged from bus 1.  This is a safe assumption, since any other
104  * configuration will require major modifications to the CP7000G
105  *
106  * Inputs :
107  * bus - bus number
108  * dev - device number
109  * offset - register offset in the configuration space
110  * val - value to be written / read
111  *
112  * Outputs :
113  * PCIBIOS_SUCCESSFUL when operation was succesfull
114  * PCIBIOS_DEVICE_NOT_FOUND when the bus or dev is errorneous
115  * PCIBIOS_BAD_REGISTER_NUMBER when accessing non aligned
116  */
117
118 static int galileo_pcibios_read_config_dword(int bus, int devfn,
119                                              int offset, u32 * val)
120 {
121         int dev, func;
122         uint32_t address_reg, data_reg;
123         uint32_t address;
124
125         dev = PCI_SLOT(devfn);
126         func = PCI_FUNC(devfn);
127
128         /* verify the range */
129         if (pci_range_ck(bus, dev))
130                 return PCIBIOS_DEVICE_NOT_FOUND;
131
132         /* select the GT-64240 registers to communicate with the PCI bus */
133         if (bus == 0) {
134                 address_reg = PCI_0CONFIGURATION_ADDRESS;
135                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
136                 GT_WRITE(PCI_0ERROR_CAUSE, ~MASTER_ABORT_BIT);
137         } else {
138                 address_reg = PCI_1CONFIGURATION_ADDRESS;
139                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
140                 GT_WRITE(PCI_1ERROR_CAUSE, ~MASTER_ABORT_BIT);
141                 if (bus == 1)
142                         bus = 0;
143         }
144
145         address = (bus << 16) | (dev << 11) | (func << 8) |
146             (offset & 0xfc) | 0x80000000;
147
148         /* start the configuration cycle */
149         GT_WRITE(address_reg, address);
150
151         /* read the data */
152         GT_READ(data_reg, val);
153
154         return PCIBIOS_SUCCESSFUL;
155 }
156
157
158 static int galileo_pcibios_read_config_word(int bus, int devfn,
159                                             int offset, u16 * val)
160 {
161         int dev, func;
162         uint32_t address_reg, data_reg;
163         uint32_t address;
164
165         dev = PCI_SLOT(devfn);
166         func = PCI_FUNC(devfn);
167
168         /* verify the range */
169         if (pci_range_ck(bus, dev))
170                 return PCIBIOS_DEVICE_NOT_FOUND;
171
172         /* select the GT-64240 registers to communicate with the PCI bus */
173         if (bus == 0) {
174                 address_reg = PCI_0CONFIGURATION_ADDRESS;
175                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
176                 GT_WRITE(PCI_0ERROR_CAUSE, ~MASTER_ABORT_BIT);
177         } else {
178                 address_reg = PCI_1CONFIGURATION_ADDRESS;
179                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
180                 GT_WRITE(PCI_1ERROR_CAUSE, ~MASTER_ABORT_BIT);
181                 if (bus == 1)
182                         bus = 0;
183         }
184
185         address = (bus << 16) | (dev << 11) | (func << 8) |
186             (offset & 0xfc) | 0x80000000;
187
188         /* start the configuration cycle */
189         GT_WRITE(address_reg, address);
190
191         /* read the data */
192         GT_READ_16(data_reg + (offset & 0x3), val);
193
194         return PCIBIOS_SUCCESSFUL;
195 }
196
197 static int galileo_pcibios_read_config_byte(int bus, int devfn,
198                                             int offset, u8 * val)
199 {
200         int dev, func;
201         uint32_t address_reg, data_reg;
202         uint32_t address;
203
204         dev = PCI_SLOT(devfn);
205         func = PCI_FUNC(devfn);
206
207         /* verify the range */
208         if (pci_range_ck(bus, dev))
209                 return PCIBIOS_DEVICE_NOT_FOUND;
210
211         /* select the GT-64240 registers to communicate with the PCI bus */
212         if (bus == 0) {
213                 address_reg = PCI_0CONFIGURATION_ADDRESS;
214                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
215         } else {
216                 address_reg = PCI_1CONFIGURATION_ADDRESS;
217                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
218                 if (bus == 1)
219                         bus = 0;
220         }
221
222         address = (bus << 16) | (dev << 11) | (func << 8) |
223             (offset & 0xfc) | 0x80000000;
224
225         /* start the configuration cycle */
226         GT_WRITE(address_reg, address);
227
228         /* write the data */
229         GT_READ_8(data_reg + (offset & 0x3), val);
230
231         return PCIBIOS_SUCCESSFUL;
232 }
233
234 static int galileo_pcibios_write_config_dword(int bus, int devfn,
235                                               int offset, u32 val)
236 {
237         int dev, func;
238         uint32_t address_reg, data_reg;
239         uint32_t address;
240
241         dev = PCI_SLOT(devfn);
242         func = PCI_FUNC(devfn);
243
244         /* verify the range */
245         if (pci_range_ck(bus, dev))
246                 return PCIBIOS_DEVICE_NOT_FOUND;
247
248         /* select the GT-64240 registers to communicate with the PCI bus */
249         if (bus == 0) {
250                 address_reg = PCI_0CONFIGURATION_ADDRESS;
251                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
252         } else {
253                 address_reg = PCI_1CONFIGURATION_ADDRESS;
254                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
255                 if (bus == 1)
256                         bus = 0;
257         }
258
259         address = (bus << 16) | (dev << 11) | (func << 8) |
260             (offset & 0xfc) | 0x80000000;
261
262         /* start the configuration cycle */
263         GT_WRITE(address_reg, address);
264
265         /* write the data */
266         GT_WRITE(data_reg, val);
267
268         return PCIBIOS_SUCCESSFUL;
269 }
270
271
272 static int galileo_pcibios_write_config_word(int bus, int devfn,
273                                              int offset, u16 val)
274 {
275         int dev, func;
276         uint32_t address_reg, data_reg;
277         uint32_t address;
278
279         dev = PCI_SLOT(devfn);
280         func = PCI_FUNC(devfn);
281
282         /* verify the range */
283         if (pci_range_ck(bus, dev))
284                 return PCIBIOS_DEVICE_NOT_FOUND;
285
286         /* select the GT-64240 registers to communicate with the PCI bus */
287         if (bus == 0) {
288                 address_reg = PCI_0CONFIGURATION_ADDRESS;
289                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
290         } else {
291                 address_reg = PCI_1CONFIGURATION_ADDRESS;
292                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
293                 if (bus == 1)
294                         bus = 0;
295         }
296
297         address = (bus << 16) | (dev << 11) | (func << 8) |
298             (offset & 0xfc) | 0x80000000;
299
300         /* start the configuration cycle */
301         GT_WRITE(address_reg, address);
302
303         /* write the data */
304         GT_WRITE_16(data_reg + (offset & 0x3), val);
305
306         return PCIBIOS_SUCCESSFUL;
307 }
308
309 static int galileo_pcibios_write_config_byte(int bus, int devfn,
310                                              int offset, u8 val)
311 {
312         int dev, func;
313         uint32_t address_reg, data_reg;
314         uint32_t address;
315
316         dev = PCI_SLOT(devfn);
317         func = PCI_FUNC(devfn);
318
319         /* verify the range */
320         if (pci_range_ck(bus, dev))
321                 return PCIBIOS_DEVICE_NOT_FOUND;
322
323         /* select the GT-64240 registers to communicate with the PCI bus */
324         if (bus == 0) {
325                 address_reg = PCI_0CONFIGURATION_ADDRESS;
326                 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
327         } else {
328                 address_reg = PCI_1CONFIGURATION_ADDRESS;
329                 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
330                 if (bus == 1)
331                         bus = 0;
332         }
333
334         address = (bus << 16) | (dev << 11) | (func << 8) |
335             (offset & 0xfc) | 0x80000000;
336
337         /* start the configuration cycle */
338         GT_WRITE(address_reg, address);
339
340         /* write the data */
341         GT_WRITE_8(data_reg + (offset & 0x3), val);
342
343         return PCIBIOS_SUCCESSFUL;
344 }
345
346 #if 0
347 static void galileo_pcibios_set_master(struct pci_dev *dev)
348 {
349         u16 cmd;
350
351         galileo_pcibios_read_config_word(dev, PCI_COMMAND, &cmd);
352         cmd |= PCI_COMMAND_MASTER;
353         galileo_pcibios_write_config_word(dev, PCI_COMMAND, cmd);
354 }
355 #endif
356
357 /*  Externally-expected functions.  Do not change function names  */
358
359 int pcibios_enable_resources(struct pci_dev *dev)
360 {
361         u16 cmd, old_cmd;
362         u8 tmp1;
363         int idx;
364         struct resource *r;
365
366         pci_read(dev->bus, dev->devfn, PCI_COMMAND, 2, (u32 *) & cmd);
367         old_cmd = cmd;
368         for (idx = 0; idx < 6; idx++) {
369                 r = &dev->resource[idx];
370                 if (!r->start && r->end) {
371                         printk(KERN_ERR
372                                "PCI: Device %s not available because of "
373                                "resource collisions\n", dev->slot_name);
374                         return -EINVAL;
375                 }
376                 if (r->flags & IORESOURCE_IO)
377                         cmd |= PCI_COMMAND_IO;
378                 if (r->flags & IORESOURCE_MEM)
379                         cmd |= PCI_COMMAND_MEMORY;
380         }
381         if (cmd != old_cmd) {
382                 pci_write(dev->bus, dev->devfn, PCI_COMMAND, 2, cmd);
383         }
384
385         /*
386          * Let's fix up the latency timer and cache line size here.  Cache
387          * line size = 32 bytes / sizeof dword (4) = 8.
388          * Latency timer must be > 8.  32 is random but appears to work.
389          */
390         pci_read(dev->bus, dev->devfn, PCI_CACHE_LINE_SIZE, 1,
391                  (u32 *) & tmp1);
392         if (tmp1 != 8) {
393                 printk(KERN_WARNING
394                        "PCI setting cache line size to 8 from " "%d\n",
395                        tmp1);
396                 pci_write(dev->bus, dev->devfn, PCI_CACHE_LINE_SIZE, 1, 8);
397         }
398         pci_read(dev->bus, dev->devfn, PCI_LATENCY_TIMER, 1,
399                  (u32 *) & tmp1);
400         if (tmp1 < 32) {
401                 printk(KERN_WARNING
402                        "PCI setting latency timer to 32 from %d\n", tmp1);
403                 pci_write(dev->bus, dev->devfn, PCI_LATENCY_TIMER, 1, 32);
404         }
405
406         return 0;
407 }
408
409 int pcibios_enable_device(struct pci_dev *dev, int mask)
410 {
411         return pcibios_enable_resources(dev);
412 }
413
414 void pcibios_align_resource(void *data, struct resource *res,
415                             unsigned long size, unsigned long align)
416 {
417         struct pci_dev *dev = data;
418
419         if (res->flags & IORESOURCE_IO) {
420                 unsigned long start = res->start;
421
422                 /* We need to avoid collisions with `mirrored' VGA ports
423                    and other strange ISA hardware, so we always want the
424                    addresses kilobyte aligned.  */
425                 if (size > 0x100) {
426                         printk(KERN_ERR "PCI: I/O Region %s/%d too large"
427                                " (%ld bytes)\n", dev->slot_name,
428                                dev->resource - res, size);
429                 }
430
431                 start = (start + 1024 - 1) & ~(1024 - 1);
432                 res->start = start;
433         }
434 }
435
436 struct pci_ops galileo_pci_ops = {
437         .read = pci_read,
438         .write = pci_write
439 };
440
441 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
442                     int size, u32 * val)
443 {
444         switch (size) {
445         case 1:
446                 return galileo_pcibios_read_config_byte(bus->number,
447                                                         devfn, where,
448                                                         (u8 *) val);
449         case 2:
450                 return galileo_pcibios_read_config_word(bus->number,
451                                                         devfn, where,
452                                                         (u16 *) val);
453         case 4:
454                 return galileo_pcibios_read_config_dword(bus->number,
455                                                          devfn, where,
456                                                          (u32 *) val);
457         }
458         return PCIBIOS_FUNC_NOT_SUPPORTED;
459 }
460
461 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
462                      int size, u32 val)
463 {
464         switch (size) {
465         case 1:
466                 return galileo_pcibios_write_config_byte(bus->number,
467                                                          devfn, where,
468                                                          val);
469         case 2:
470                 return galileo_pcibios_write_config_word(bus->number,
471                                                          devfn, where,
472                                                          val);
473         case 4:
474                 return galileo_pcibios_write_config_dword(bus->number,
475                                                           devfn, where,
476                                                           val);
477         }
478         return PCIBIOS_FUNC_NOT_SUPPORTED;
479 }
480
481 struct pci_fixup pcibios_fixups[] = {
482         {0}
483 };
484
485 void __devinit pcibios_fixup_bus(struct pci_bus *c)
486 {
487         gt64240_board_pcibios_fixup_bus(c);
488 }
489
490
491 /********************************************************************
492 * pci0P2PConfig - This function set the PCI_0 P2P configurate.
493 *                 For more information on the P2P read PCI spec.
494 *
495 * Inputs:  unsigned int SecondBusLow - Secondery PCI interface Bus Range Lower
496 *                                      Boundry.
497 *          unsigned int SecondBusHigh - Secondry PCI interface Bus Range upper
498 *                                      Boundry.
499 *          unsigned int busNum - The CPI bus number to which the PCI interface
500 *                                      is connected.
501 *          unsigned int devNum - The PCI interface's device number.
502 *
503 * Returns:  true.
504 */
505 void pci0P2PConfig(unsigned int SecondBusLow, unsigned int SecondBusHigh,
506                    unsigned int busNum, unsigned int devNum)
507 {
508         uint32_t regData;
509
510         regData = (SecondBusLow & 0xff) | ((SecondBusHigh & 0xff) << 8) |
511             ((busNum & 0xff) << 16) | ((devNum & 0x1f) << 24);
512         GT_WRITE(PCI_0P2P_CONFIGURATION, regData);
513 }
514
515 /********************************************************************
516 * pci1P2PConfig - This function set the PCI_1 P2P configurate.
517 *                 For more information on the P2P read PCI spec.
518 *
519 * Inputs:  unsigned int SecondBusLow - Secondery PCI interface Bus Range Lower
520 *               Boundry.
521 *          unsigned int SecondBusHigh - Secondry PCI interface Bus Range upper
522 *               Boundry.
523 *          unsigned int busNum - The CPI bus number to which the PCI interface
524 *               is connected.
525 *          unsigned int devNum - The PCI interface's device number.
526 *
527 * Returns:  true.
528 */
529 void pci1P2PConfig(unsigned int SecondBusLow, unsigned int SecondBusHigh,
530                    unsigned int busNum, unsigned int devNum)
531 {
532         uint32_t regData;
533
534         regData = (SecondBusLow & 0xff) | ((SecondBusHigh & 0xff) << 8) |
535             ((busNum & 0xff) << 16) | ((devNum & 0x1f) << 24);
536         GT_WRITE(PCI_1P2P_CONFIGURATION, regData);
537 }
538
539 #define PCI0_STATUS_COMMAND_REG                 0x4
540 #define PCI1_STATUS_COMMAND_REG                 0x84
541
542 static int __init pcibios_init(void)
543 {
544         /* Reset PCI I/O and PCI MEM values */
545         ioport_resource.start = 0xe0000000;
546         ioport_resource.end = 0xe0000000 + 0x20000000 - 1;
547         iomem_resource.start = 0xc0000000;
548         iomem_resource.end = 0xc0000000 + 0x20000000 - 1;
549
550         pci_scan_bus(0, &galileo_pci_ops, NULL);
551         pci_scan_bus(1, &galileo_pci_ops, NULL);
552
553         return 0;
554 }
555
556 subsys_initcall(pcibios_init);
557
558 /*
559  * for parsing "pci=" kernel boot arguments.
560  */
561 char *pcibios_setup(char *str)
562 {
563         printk(KERN_INFO "rr: pcibios_setup\n");
564         /* Nothing to do for now.  */
565
566         return str;
567 }
568
569 unsigned __init int pcibios_assign_all_busses(void)
570 {
571         return 1;
572 }