Feed the mess through indent.
[linux-2.6/linux-mips.git] / arch / mips / pci / pci-ocelot-g.c
blob7255d230243a26a425930fe7161707bfaa8e0319
1 /*
2 * Copyright 2002 Momentum Computer
3 * Author: Matthew Dharm <mdharm@momenco.com>
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.
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.
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.
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"
34 #include <linux/init.h>
36 #define SELF 0
37 #define MASTER_ABORT_BIT 0x100
40 * These functions and structures provide the BIOS scan and mapping of the PCI
41 * devices.
44 #define MAX_PCI_DEVS 10
46 void gt64240_board_pcibios_fixup_bus(struct pci_bus *c);
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
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);
71 * General-purpose PCI functions.
76 * pci_range_ck -
78 * Check if the pci device that are trying to access does really exists
79 * on the evaluation board.
81 * Inputs :
82 * bus - bus number (0 for PCI 0 ; 1 for PCI 1)
83 * dev - number of device on the specific pci bus
85 * Outpus :
86 * 0 - if OK , 1 - if failure
88 static __inline__ int pci_range_ck(unsigned char bus, unsigned char dev)
90 /* Accessing device 31 crashes the GT-64240. */
91 if (dev < 5)
92 return 0;
93 return -1;
97 * galileo_pcibios_(read/write)_config_(dword/word/byte) -
99 * reads/write a dword/word/byte register from the configuration space
100 * of a device.
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
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
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
118 static int galileo_pcibios_read_config_dword(int bus, int devfn,
119 int offset, u32 * val)
121 int dev, func;
122 uint32_t address_reg, data_reg;
123 uint32_t address;
125 dev = PCI_SLOT(devfn);
126 func = PCI_FUNC(devfn);
128 /* verify the range */
129 if (pci_range_ck(bus, dev))
130 return PCIBIOS_DEVICE_NOT_FOUND;
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;
145 address = (bus << 16) | (dev << 11) | (func << 8) |
146 (offset & 0xfc) | 0x80000000;
148 /* start the configuration cycle */
149 GT_WRITE(address_reg, address);
151 /* read the data */
152 GT_READ(data_reg, val);
154 return PCIBIOS_SUCCESSFUL;
158 static int galileo_pcibios_read_config_word(int bus, int devfn,
159 int offset, u16 * val)
161 int dev, func;
162 uint32_t address_reg, data_reg;
163 uint32_t address;
165 dev = PCI_SLOT(devfn);
166 func = PCI_FUNC(devfn);
168 /* verify the range */
169 if (pci_range_ck(bus, dev))
170 return PCIBIOS_DEVICE_NOT_FOUND;
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;
185 address = (bus << 16) | (dev << 11) | (func << 8) |
186 (offset & 0xfc) | 0x80000000;
188 /* start the configuration cycle */
189 GT_WRITE(address_reg, address);
191 /* read the data */
192 GT_READ_16(data_reg + (offset & 0x3), val);
194 return PCIBIOS_SUCCESSFUL;
197 static int galileo_pcibios_read_config_byte(int bus, int devfn,
198 int offset, u8 * val)
200 int dev, func;
201 uint32_t address_reg, data_reg;
202 uint32_t address;
204 dev = PCI_SLOT(devfn);
205 func = PCI_FUNC(devfn);
207 /* verify the range */
208 if (pci_range_ck(bus, dev))
209 return PCIBIOS_DEVICE_NOT_FOUND;
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;
222 address = (bus << 16) | (dev << 11) | (func << 8) |
223 (offset & 0xfc) | 0x80000000;
225 /* start the configuration cycle */
226 GT_WRITE(address_reg, address);
228 /* write the data */
229 GT_READ_8(data_reg + (offset & 0x3), val);
231 return PCIBIOS_SUCCESSFUL;
234 static int galileo_pcibios_write_config_dword(int bus, int devfn,
235 int offset, u32 val)
237 int dev, func;
238 uint32_t address_reg, data_reg;
239 uint32_t address;
241 dev = PCI_SLOT(devfn);
242 func = PCI_FUNC(devfn);
244 /* verify the range */
245 if (pci_range_ck(bus, dev))
246 return PCIBIOS_DEVICE_NOT_FOUND;
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;
259 address = (bus << 16) | (dev << 11) | (func << 8) |
260 (offset & 0xfc) | 0x80000000;
262 /* start the configuration cycle */
263 GT_WRITE(address_reg, address);
265 /* write the data */
266 GT_WRITE(data_reg, val);
268 return PCIBIOS_SUCCESSFUL;
272 static int galileo_pcibios_write_config_word(int bus, int devfn,
273 int offset, u16 val)
275 int dev, func;
276 uint32_t address_reg, data_reg;
277 uint32_t address;
279 dev = PCI_SLOT(devfn);
280 func = PCI_FUNC(devfn);
282 /* verify the range */
283 if (pci_range_ck(bus, dev))
284 return PCIBIOS_DEVICE_NOT_FOUND;
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;
297 address = (bus << 16) | (dev << 11) | (func << 8) |
298 (offset & 0xfc) | 0x80000000;
300 /* start the configuration cycle */
301 GT_WRITE(address_reg, address);
303 /* write the data */
304 GT_WRITE_16(data_reg + (offset & 0x3), val);
306 return PCIBIOS_SUCCESSFUL;
309 static int galileo_pcibios_write_config_byte(int bus, int devfn,
310 int offset, u8 val)
312 int dev, func;
313 uint32_t address_reg, data_reg;
314 uint32_t address;
316 dev = PCI_SLOT(devfn);
317 func = PCI_FUNC(devfn);
319 /* verify the range */
320 if (pci_range_ck(bus, dev))
321 return PCIBIOS_DEVICE_NOT_FOUND;
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;
334 address = (bus << 16) | (dev << 11) | (func << 8) |
335 (offset & 0xfc) | 0x80000000;
337 /* start the configuration cycle */
338 GT_WRITE(address_reg, address);
340 /* write the data */
341 GT_WRITE_8(data_reg + (offset & 0x3), val);
343 return PCIBIOS_SUCCESSFUL;
346 #if 0
347 static void galileo_pcibios_set_master(struct pci_dev *dev)
349 u16 cmd;
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);
355 #endif
357 /* Externally-expected functions. Do not change function names */
359 int pcibios_enable_resources(struct pci_dev *dev)
361 u16 cmd, old_cmd;
362 u8 tmp1;
363 int idx;
364 struct resource *r;
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;
376 if (r->flags & IORESOURCE_IO)
377 cmd |= PCI_COMMAND_IO;
378 if (r->flags & IORESOURCE_MEM)
379 cmd |= PCI_COMMAND_MEMORY;
381 if (cmd != old_cmd) {
382 pci_write(dev->bus, dev->devfn, PCI_COMMAND, 2, cmd);
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.
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);
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);
406 return 0;
409 int pcibios_enable_device(struct pci_dev *dev, int mask)
411 return pcibios_enable_resources(dev);
414 void pcibios_align_resource(void *data, struct resource *res,
415 unsigned long size, unsigned long align)
417 struct pci_dev *dev = data;
419 if (res->flags & IORESOURCE_IO) {
420 unsigned long start = res->start;
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);
431 start = (start + 1024 - 1) & ~(1024 - 1);
432 res->start = start;
436 struct pci_ops galileo_pci_ops = {
437 .read = pci_read,
438 .write = pci_write
441 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
442 int size, u32 * val)
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);
458 return PCIBIOS_FUNC_NOT_SUPPORTED;
461 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
462 int size, u32 val)
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);
478 return PCIBIOS_FUNC_NOT_SUPPORTED;
481 struct pci_fixup pcibios_fixups[] = {
485 void __devinit pcibios_fixup_bus(struct pci_bus *c)
487 gt64240_board_pcibios_fixup_bus(c);
491 /********************************************************************
492 * pci0P2PConfig - This function set the PCI_0 P2P configurate.
493 * For more information on the P2P read PCI spec.
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.
503 * Returns: true.
505 void pci0P2PConfig(unsigned int SecondBusLow, unsigned int SecondBusHigh,
506 unsigned int busNum, unsigned int devNum)
508 uint32_t regData;
510 regData = (SecondBusLow & 0xff) | ((SecondBusHigh & 0xff) << 8) |
511 ((busNum & 0xff) << 16) | ((devNum & 0x1f) << 24);
512 GT_WRITE(PCI_0P2P_CONFIGURATION, regData);
515 /********************************************************************
516 * pci1P2PConfig - This function set the PCI_1 P2P configurate.
517 * For more information on the P2P read PCI spec.
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.
527 * Returns: true.
529 void pci1P2PConfig(unsigned int SecondBusLow, unsigned int SecondBusHigh,
530 unsigned int busNum, unsigned int devNum)
532 uint32_t regData;
534 regData = (SecondBusLow & 0xff) | ((SecondBusHigh & 0xff) << 8) |
535 ((busNum & 0xff) << 16) | ((devNum & 0x1f) << 24);
536 GT_WRITE(PCI_1P2P_CONFIGURATION, regData);
539 #define PCI0_STATUS_COMMAND_REG 0x4
540 #define PCI1_STATUS_COMMAND_REG 0x84
542 static int __init pcibios_init(void)
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;
550 pci_scan_bus(0, &galileo_pci_ops, NULL);
551 pci_scan_bus(1, &galileo_pci_ops, NULL);
553 return 0;
556 subsys_initcall(pcibios_init);
559 * for parsing "pci=" kernel boot arguments.
561 char *pcibios_setup(char *str)
563 printk(KERN_INFO "rr: pcibios_setup\n");
564 /* Nothing to do for now. */
566 return str;
569 unsigned __init int pcibios_assign_all_busses(void)
571 return 1;