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>
34 #include <linux/init.h>
37 #define MASTER_ABORT_BIT 0x100
40 * These functions and structures provide the BIOS scan and mapping of the PCI
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,
57 static int galileo_pcibios_write_config_word(int bus, int devfn,
59 static int galileo_pcibios_write_config_dword(int bus, int devfn,
62 static void galileo_pcibios_set_master(struct pci_dev *dev);
65 static int pci_read(struct pci_bus *bus, unsigned int devfs, int where,
67 static int pci_write(struct pci_bus *bus, unsigned int devfs, int where,
71 * General-purpose PCI functions.
78 * Check if the pci device that are trying to access does really exists
79 * on the evaluation board.
82 * bus - bus number (0 for PCI 0 ; 1 for PCI 1)
83 * dev - number of device on the specific pci bus
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. */
97 * galileo_pcibios_(read/write)_config_(dword/word/byte) -
99 * reads/write a dword/word/byte register from the configuration space
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
108 * dev - device number
109 * offset - register offset in the configuration space
110 * val - value to be written / read
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)
122 uint32_t address_reg, data_reg;
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 */
134 address_reg = PCI_0CONFIGURATION_ADDRESS;
135 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
136 GT_WRITE(PCI_0ERROR_CAUSE, ~MASTER_ABORT_BIT);
138 address_reg = PCI_1CONFIGURATION_ADDRESS;
139 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
140 GT_WRITE(PCI_1ERROR_CAUSE, ~MASTER_ABORT_BIT);
145 address = (bus << 16) | (dev << 11) | (func << 8) |
146 (offset & 0xfc) | 0x80000000;
148 /* start the configuration cycle */
149 GT_WRITE(address_reg, address);
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)
162 uint32_t address_reg, data_reg;
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 */
174 address_reg = PCI_0CONFIGURATION_ADDRESS;
175 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
176 GT_WRITE(PCI_0ERROR_CAUSE, ~MASTER_ABORT_BIT);
178 address_reg = PCI_1CONFIGURATION_ADDRESS;
179 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
180 GT_WRITE(PCI_1ERROR_CAUSE, ~MASTER_ABORT_BIT);
185 address = (bus << 16) | (dev << 11) | (func << 8) |
186 (offset & 0xfc) | 0x80000000;
188 /* start the configuration cycle */
189 GT_WRITE(address_reg, address);
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)
201 uint32_t address_reg, data_reg;
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 */
213 address_reg = PCI_0CONFIGURATION_ADDRESS;
214 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
216 address_reg = PCI_1CONFIGURATION_ADDRESS;
217 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
222 address = (bus << 16) | (dev << 11) | (func << 8) |
223 (offset & 0xfc) | 0x80000000;
225 /* start the configuration cycle */
226 GT_WRITE(address_reg, address);
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,
238 uint32_t address_reg, data_reg;
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 */
250 address_reg = PCI_0CONFIGURATION_ADDRESS;
251 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
253 address_reg = PCI_1CONFIGURATION_ADDRESS;
254 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
259 address = (bus << 16) | (dev << 11) | (func << 8) |
260 (offset & 0xfc) | 0x80000000;
262 /* start the configuration cycle */
263 GT_WRITE(address_reg, address);
266 GT_WRITE(data_reg, val);
268 return PCIBIOS_SUCCESSFUL;
272 static int galileo_pcibios_write_config_word(int bus, int devfn,
276 uint32_t address_reg, data_reg;
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 */
288 address_reg = PCI_0CONFIGURATION_ADDRESS;
289 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
291 address_reg = PCI_1CONFIGURATION_ADDRESS;
292 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
297 address = (bus << 16) | (dev << 11) | (func << 8) |
298 (offset & 0xfc) | 0x80000000;
300 /* start the configuration cycle */
301 GT_WRITE(address_reg, address);
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,
313 uint32_t address_reg, data_reg;
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 */
325 address_reg = PCI_0CONFIGURATION_ADDRESS;
326 data_reg = PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER;
328 address_reg = PCI_1CONFIGURATION_ADDRESS;
329 data_reg = PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER;
334 address = (bus << 16) | (dev << 11) | (func << 8) |
335 (offset & 0xfc) | 0x80000000;
337 /* start the configuration cycle */
338 GT_WRITE(address_reg, address);
341 GT_WRITE_8(data_reg + (offset & 0x3), val);
343 return PCIBIOS_SUCCESSFUL;
347 static void galileo_pcibios_set_master(struct pci_dev *dev)
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);
357 /* Externally-expected functions. Do not change function names */
359 int pcibios_enable_resources(struct pci_dev *dev)
366 pci_read(dev->bus, dev->devfn, PCI_COMMAND, 2, (u32 *) & cmd);
368 for (idx = 0; idx < 6; idx++) {
369 r = &dev->resource[idx];
370 if (!r->start && r->end) {
372 "PCI: Device %s not available because of "
373 "resource collisions\n", dev->slot_name);
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,
394 "PCI setting cache line size to 8 from " "%d\n",
396 pci_write(dev->bus, dev->devfn, PCI_CACHE_LINE_SIZE, 1, 8);
398 pci_read(dev->bus, dev->devfn, PCI_LATENCY_TIMER, 1,
402 "PCI setting latency timer to 32 from %d\n", tmp1);
403 pci_write(dev->bus, dev->devfn, PCI_LATENCY_TIMER, 1, 32);
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. */
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);
436 struct pci_ops galileo_pci_ops = {
441 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
446 return galileo_pcibios_read_config_byte(bus->number,
450 return galileo_pcibios_read_config_word(bus->number,
454 return galileo_pcibios_read_config_dword(bus->number,
458 return PCIBIOS_FUNC_NOT_SUPPORTED;
461 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
466 return galileo_pcibios_write_config_byte(bus->number,
470 return galileo_pcibios_write_config_word(bus->number,
474 return galileo_pcibios_write_config_dword(bus->number,
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
497 * unsigned int SecondBusHigh - Secondry PCI interface Bus Range upper
499 * unsigned int busNum - The CPI bus number to which the PCI interface
501 * unsigned int devNum - The PCI interface's device number.
505 void pci0P2PConfig(unsigned int SecondBusLow, unsigned int SecondBusHigh,
506 unsigned int busNum, unsigned int devNum)
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
521 * unsigned int SecondBusHigh - Secondry PCI interface Bus Range upper
523 * unsigned int busNum - The CPI bus number to which the PCI interface
525 * unsigned int devNum - The PCI interface's device number.
529 void pci1P2PConfig(unsigned int SecondBusLow, unsigned int SecondBusHigh,
530 unsigned int busNum, unsigned int devNum)
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);
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. */
569 unsigned __init int pcibios_assign_all_busses(void)