2 * BRIEF MODULE DESCRIPTION
3 * Galileo Evaluation Boards PCI support.
5 * The general-purpose functions to read/write and configure the GT64120A's
6 * PCI registers (function names start with pci0 or pci1) are either direct
7 * copies of functions written by Galileo Technology, or are modifications
8 * of their functions to work with Linux 2.4 vs Linux 2.2. These functions
9 * are Copyright - Galileo Technology.
11 * Other functions are derived from other MIPS PCI implementations, or were
12 * written by RidgeRun, Inc, Copyright (C) 2000 RidgeRun, Inc.
13 * glonnon@ridgerun.com, skranz@ridgerun.com, stevej@ridgerun.com
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
23 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/kernel.h>
38 #include <linux/slab.h>
39 #include <linux/version.h>
42 #include <asm/galileo-boards/ev64120.h>
43 #include <asm/gt64120.h>
45 #include <linux/init.h>
50 #define DBG(x...) printk(x)
58 * These functions and structures provide the BIOS scan and mapping of the PCI
62 #define MAX_PCI_DEVS 10
70 static void __init
scan_and_initialize_pci(void);
71 static u32 __init
scan_pci_bus(struct pci_device
*pci_devices
);
72 static void __init
allocate_pci_space(struct pci_device
*pci_devices
);
74 static void __devinit
galileo_pcibios_fixup_bus(struct pci_bus
*bus
);
77 * The functions that actually read and write to the controller.
78 * Copied from or modified from Galileo Technology code.
80 static unsigned int pci0ReadConfigReg(int offset
, struct pci_dev
*device
);
81 static void pci0WriteConfigReg(unsigned int offset
,
82 struct pci_dev
*device
, unsigned int data
);
83 static unsigned int pci1ReadConfigReg(int offset
, struct pci_dev
*device
);
84 static void pci1WriteConfigReg(unsigned int offset
,
85 struct pci_dev
*device
, unsigned int data
);
87 static void pci0MapIOspace(unsigned int pci0IoBase
,
88 unsigned int pci0IoLength
);
89 static void pci1MapIOspace(unsigned int pci1IoBase
,
90 unsigned int pci1IoLength
);
91 static void pci0MapMemory0space(unsigned int pci0Mem0Base
,
92 unsigned int pci0Mem0Length
);
93 static void pci1MapMemory0space(unsigned int pci1Mem0Base
,
94 unsigned int pci1Mem0Length
);
95 static void pci0MapMemory1space(unsigned int pci0Mem1Base
,
96 unsigned int pci0Mem1Length
);
97 static void pci1MapMemory1space(unsigned int pci1Mem1Base
,
98 unsigned int pci1Mem1Length
);
99 static unsigned int pci0GetIOspaceBase(void);
100 static unsigned int pci0GetIOspaceSize(void);
101 static unsigned int pci0GetMemory0Base(void);
102 static unsigned int pci0GetMemory0Size(void);
103 static unsigned int pci0GetMemory1Base(void);
104 static unsigned int pci0GetMemory1Size(void);
105 static unsigned int pci1GetIOspaceBase(void);
106 static unsigned int pci1GetIOspaceSize(void);
107 static unsigned int pci1GetMemory0Base(void);
108 static unsigned int pci1GetMemory0Size(void);
109 static unsigned int pci1GetMemory1Base(void);
110 static unsigned int pci1GetMemory1Size(void);
113 /* Functions to implement "pci ops" */
114 static int galileo_pcibios_read_config_word(struct pci_dev
*dev
,
115 int offset
, u16
* val
);
116 static int galileo_pcibios_read_config_byte(struct pci_dev
*dev
,
117 int offset
, u8
* val
);
118 static int galileo_pcibios_read_config_dword(struct pci_dev
*dev
,
119 int offset
, u32
* val
);
120 static int galileo_pcibios_write_config_byte(struct pci_dev
*dev
,
122 static int galileo_pcibios_write_config_word(struct pci_dev
*dev
,
123 int offset
, u16 val
);
124 static int galileo_pcibios_write_config_dword(struct pci_dev
*dev
,
125 int offset
, u32 val
);
126 static void galileo_pcibios_set_master(struct pci_dev
*dev
);
129 * General-purpose PCI functions.
133 * pci0MapIOspace - Maps PCI0 IO space for the master.
134 * Inputs: base and length of pci0Io
136 static void pci0MapIOspace(unsigned int pci0IoBase
,
137 unsigned int pci0IoLength
)
139 unsigned int pci0IoTop
=
140 (unsigned int) (pci0IoBase
+ pci0IoLength
);
142 if (pci0IoLength
== 0)
145 pci0IoBase
= (unsigned int) (pci0IoBase
>> 21);
146 pci0IoTop
= (unsigned int) (((pci0IoTop
- 1) & 0x0fffffff) >> 21);
147 GT_WRITE(GT_PCI0IOLD_OFS
, pci0IoBase
);
148 GT_WRITE(GT_PCI0IOHD_OFS
, pci0IoTop
);
152 * pci1MapIOspace - Maps PCI1 IO space for the master.
153 * Inputs: base and length of pci1Io
156 static void pci1MapIOspace(unsigned int pci1IoBase
,
157 unsigned int pci1IoLength
)
159 unsigned int pci1IoTop
=
160 (unsigned int) (pci1IoBase
+ pci1IoLength
);
162 if (pci1IoLength
== 0)
165 pci1IoBase
= (unsigned int) (pci1IoBase
>> 21);
166 pci1IoTop
= (unsigned int) (((pci1IoTop
- 1) & 0x0fffffff) >> 21);
167 GT_WRITE(GT_PCI1IOLD_OFS
, pci1IoBase
);
168 GT_WRITE(GT_PCI1IOHD_OFS
, pci1IoTop
);
172 * pci0MapMemory0space - Maps PCI0 memory0 space for the master.
173 * Inputs: base and length of pci0Mem0
176 static void pci0MapMemory0space(unsigned int pci0Mem0Base
,
177 unsigned int pci0Mem0Length
)
179 unsigned int pci0Mem0Top
= pci0Mem0Base
+ pci0Mem0Length
;
181 if (pci0Mem0Length
== 0)
184 pci0Mem0Base
= pci0Mem0Base
>> 21;
185 pci0Mem0Top
= ((pci0Mem0Top
- 1) & 0x0fffffff) >> 21;
186 GT_WRITE(GT_PCI0M0LD_OFS
, pci0Mem0Base
);
187 GT_WRITE(GT_PCI0M0HD_OFS
, pci0Mem0Top
);
191 * pci1MapMemory0space - Maps PCI1 memory0 space for the master.
192 * Inputs: base and length of pci1Mem0
195 static void pci1MapMemory0space(unsigned int pci1Mem0Base
,
196 unsigned int pci1Mem0Length
)
198 unsigned int pci1Mem0Top
= pci1Mem0Base
+ pci1Mem0Length
;
200 if (pci1Mem0Length
== 0)
203 pci1Mem0Base
= pci1Mem0Base
>> 21;
204 pci1Mem0Top
= ((pci1Mem0Top
- 1) & 0x0fffffff) >> 21;
205 GT_WRITE(GT_PCI1M0LD_OFS
, pci1Mem0Base
);
206 GT_WRITE(GT_PCI1M0HD_OFS
, pci1Mem0Top
);
210 * pci0MapMemory1space - Maps PCI0 memory1 space for the master.
211 * Inputs: base and length of pci0Mem1
214 static void pci0MapMemory1space(unsigned int pci0Mem1Base
,
215 unsigned int pci0Mem1Length
)
217 unsigned int pci0Mem1Top
= pci0Mem1Base
+ pci0Mem1Length
;
219 if (pci0Mem1Length
== 0)
222 pci0Mem1Base
= pci0Mem1Base
>> 21;
223 pci0Mem1Top
= ((pci0Mem1Top
- 1) & 0x0fffffff) >> 21;
224 GT_WRITE(GT_PCI0M1LD_OFS
, pci0Mem1Base
);
225 GT_WRITE(GT_PCI0M1HD_OFS
, pci0Mem1Top
);
230 * pci1MapMemory1space - Maps PCI1 memory1 space for the master.
231 * Inputs: base and length of pci1Mem1
234 static void pci1MapMemory1space(unsigned int pci1Mem1Base
,
235 unsigned int pci1Mem1Length
)
237 unsigned int pci1Mem1Top
= pci1Mem1Base
+ pci1Mem1Length
;
239 if (pci1Mem1Length
== 0)
242 pci1Mem1Base
= pci1Mem1Base
>> 21;
243 pci1Mem1Top
= ((pci1Mem1Top
- 1) & 0x0fffffff) >> 21;
244 GT_WRITE(GT_PCI1M1LD_OFS
, pci1Mem1Base
);
245 GT_WRITE(GT_PCI1M1HD_OFS
, pci1Mem1Top
);
249 * pci0GetIOspaceBase - Return PCI0 IO Base Address.
251 * Returns: PCI0 IO Base Address.
254 static unsigned int pci0GetIOspaceBase(void)
257 GT_READ(GT_PCI0IOLD_OFS
, &base
);
263 * pci0GetIOspaceSize - Return PCI0 IO Bar Size.
265 * Returns: PCI0 IO Bar Size.
267 static unsigned int pci0GetIOspaceSize(void)
269 unsigned int top
, base
, size
;
270 GT_READ(GT_PCI0IOLD_OFS
, &base
);
272 GT_READ(GT_PCI0IOHD_OFS
, &top
);
274 size
= ((top
- base
) & 0xfffffff);
275 size
= size
| 0x1fffff;
280 * pci0GetMemory0Base - Return PCI0 Memory 0 Base Address.
282 * Returns: PCI0 Memory 0 Base Address.
284 static unsigned int pci0GetMemory0Base(void)
287 GT_READ(GT_PCI0M0LD_OFS
, &base
);
293 * pci0GetMemory0Size - Return PCI0 Memory 0 Bar Size.
295 * Returns: PCI0 Memory 0 Bar Size.
297 static unsigned int pci0GetMemory0Size(void)
299 unsigned int top
, base
, size
;
300 GT_READ(GT_PCI0M0LD_OFS
, &base
);
302 GT_READ(GT_PCI0M0HD_OFS
, &top
);
304 size
= ((top
- base
) & 0xfffffff);
305 size
= size
| 0x1fffff;
310 * pci0GetMemory1Base - Return PCI0 Memory 1 Base Address.
312 * Returns: PCI0 Memory 1 Base Address.
314 static unsigned int pci0GetMemory1Base(void)
317 GT_READ(GT_PCI0M1LD_OFS
, &base
);
323 * pci0GetMemory1Size - Return PCI0 Memory 1 Bar Size.
325 * Returns: PCI0 Memory 1 Bar Size.
328 static unsigned int pci0GetMemory1Size(void)
330 unsigned int top
, base
, size
;
331 GT_READ(GT_PCI0M1LD_OFS
, &base
);
333 GT_READ(GT_PCI0M1HD_OFS
, &top
);
335 size
= ((top
- base
) & 0xfffffff);
336 size
= size
| 0x1fffff;
341 * pci1GetIOspaceBase - Return PCI1 IO Base Address.
343 * Returns: PCI1 IO Base Address.
346 static unsigned int pci1GetIOspaceBase(void)
349 GT_READ(GT_PCI1IOLD_OFS
, &base
);
355 * pci1GetIOspaceSize - Return PCI1 IO Bar Size.
357 * Returns: PCI1 IO Bar Size.
360 static unsigned int pci1GetIOspaceSize(void)
362 unsigned int top
, base
, size
;
363 GT_READ(GT_PCI1IOLD_OFS
, &base
);
365 GT_READ(GT_PCI1IOHD_OFS
, &top
);
367 size
= ((top
- base
) & 0xfffffff);
368 size
= size
| 0x1fffff;
373 * pci1GetMemory0Base - Return PCI1 Memory 0 Base Address.
375 * Returns: PCI1 Memory 0 Base Address.
378 static unsigned int pci1GetMemory0Base(void)
381 GT_READ(GT_PCI1M0LD_OFS
, &base
);
387 * pci1GetMemory0Size - Return PCI1 Memory 0 Bar Size.
389 * Returns: PCI1 Memory 0 Bar Size.
392 static unsigned int pci1GetMemory0Size(void)
394 unsigned int top
, base
, size
;
395 GT_READ(GT_PCI1M1LD_OFS
, &base
);
397 GT_READ(GT_PCI1M1HD_OFS
, &top
);
399 size
= ((top
- base
) & 0xfffffff);
400 size
= size
| 0x1fffff;
405 * pci1GetMemory1Base - Return PCI1 Memory 1 Base Address.
407 * Returns: PCI1 Memory 1 Base Address.
410 static unsigned int pci1GetMemory1Base(void)
413 GT_READ(GT_PCI1M1LD_OFS
, &base
);
419 * pci1GetMemory1Size - Return PCI1 Memory 1 Bar Size.
421 * Returns: PCI1 Memory 1 Bar Size.
424 static unsigned int pci1GetMemory1Size(void)
426 unsigned int top
, base
, size
;
427 GT_READ(GT_PCI1M1LD_OFS
, &base
);
429 GT_READ(GT_PCI1M1HD_OFS
, &top
);
431 size
= ((top
- base
) & 0xfffffff);
432 size
= size
| 0x1fffff;
441 * Check if the pci device that are trying to access does really exists
442 * on the evaluation board.
445 * bus - bus number (0 for PCI 0 ; 1 for PCI 1)
446 * dev - number of device on the specific pci bus
449 * 0 - if OK , 1 - if failure
451 static __inline__
int pci_range_ck(unsigned char bus
, unsigned char dev
)
453 //DBG(KERN_INFO "p_r_c %d %d\n",bus,dev);
454 if (((bus
== 0) || (bus
== 1)) && (dev
>= 6) && (dev
<= 8))
455 return 0; // Bus/Device Number OK
456 return -1; // Bus/Device Number not OK
460 * pciXReadConfigReg - Read from a PCI configuration register
461 * - Make sure the GT is configured as a master before
462 * reading from another device on the PCI.
463 * - The function takes care of Big/Little endian conversion.
464 * INPUTS: regOffset: The register offset as it apears in the GT spec (or PCI
466 * pciDevNum: The device number needs to be addressed.
467 * RETURNS: data , if the data == 0xffffffff check the master abort bit in the
468 * cause register to make sure the data is valid
470 * Configuration Address 0xCF8:
472 * 31 30 24 23 16 15 11 10 8 7 2 0 <=bit Number
473 * |congif|Reserved| Bus |Device|Function|Register|00|
474 * |Enable| |Number|Number| Number | Number | | <=field Name
477 static unsigned int pci0ReadConfigReg(int offset
, struct pci_dev
*device
)
479 unsigned int DataForRegCf8
;
482 DataForRegCf8
= ((PCI_SLOT(device
->devfn
) << 11) |
483 (PCI_FUNC(device
->devfn
) << 8) |
484 (offset
& ~0x3)) | 0x80000000;
485 GT_WRITE(GT_PCI0_CFGADDR_OFS
, DataForRegCf8
);
487 /* The casual observer might wonder why the READ is duplicated here,
488 rather than immediately following the WRITE, and just have the
489 swap in the "if". That's because there is a latency problem
490 with trying to read immediately after setting up the address
491 register. The "if" check gives enough time for the address
492 to stabilize, so the READ can work.
494 if (PCI_SLOT(device
->devfn
) == SELF
) { /* This board */
495 GT_READ(GT_PCI0_CFGDATA_OFS
, &data
);
497 } else { /* The PCI is working in LE Mode so swap the Data. */
498 GT_READ(GT_PCI0_CFGDATA_OFS
, &data
);
499 return cpu_to_le32(data
);
503 static unsigned int pci1ReadConfigReg(int offset
, struct pci_dev
*device
)
505 unsigned int DataForRegCf8
;
508 DataForRegCf8
= ((PCI_SLOT(device
->devfn
) << 11) |
509 (PCI_FUNC(device
->devfn
) << 8) |
510 (offset
& ~0x3)) | 0x80000000;
511 /* The casual observer might wonder why the READ is duplicated here,
512 rather than immediately following the WRITE, and just have the
513 swap in the "if". That's because there is a latency problem
514 with trying to read immediately after setting up the address
515 register. The "if" check gives enough time for the address
516 to stabilize, so the READ can work.
518 if (PCI_SLOT(device
->devfn
) == SELF
) { /* This board */
519 /* when configurating our own PCI 1 L-unit the access is through
520 the PCI 0 interface with reg number = reg number + 0x80 */
521 DataForRegCf8
|= 0x80;
522 GT_WRITE(GT_PCI0_CFGADDR_OFS
, DataForRegCf8
);
523 } else { /* The PCI is working in LE Mode so swap the Data. */
524 GT_WRITE(GT_PCI1_CFGADDR_OFS
, DataForRegCf8
);
526 if (PCI_SLOT(device
->devfn
) == SELF
) { /* This board */
527 GT_READ(GT_PCI0_CFGDATA_OFS
, &data
);
530 GT_READ(GT_PCI1_CFGDATA_OFS
, &data
);
531 return cpu_to_le32(data
);
538 * pciXWriteConfigReg - Write to a PCI configuration register
539 * - Make sure the GT is configured as a master before
540 * writingto another device on the PCI.
541 * - The function takes care of Big/Little endian conversion.
542 * Inputs: unsigned int regOffset: The register offset as it apears in the
544 * (or any other PCI device spec)
545 * pciDevNum: The device number needs to be addressed.
547 * Configuration Address 0xCF8:
549 * 31 30 24 23 16 15 11 10 8 7 2 0 <=bit Number
550 * |congif|Reserved| Bus |Device|Function|Register|00|
551 * |Enable| |Number|Number| Number | Number | | <=field Name
554 static void pci0WriteConfigReg(unsigned int offset
,
555 struct pci_dev
*device
, unsigned int data
)
557 unsigned int DataForRegCf8
;
559 DataForRegCf8
= ((PCI_SLOT(device
->devfn
) << 11) |
560 (PCI_FUNC(device
->devfn
) << 8) |
561 (offset
& ~0x3)) | 0x80000000;
562 GT_WRITE(GT_PCI0_CFGADDR_OFS
, DataForRegCf8
);
563 if (PCI_SLOT(device
->devfn
) == SELF
) { /* This board */
564 GT_WRITE(GT_PCI0_CFGDATA_OFS
, data
);
565 } else { /* configuration Transaction over the pci. */
566 /* The PCI is working in LE Mode so swap the Data. */
567 GT_WRITE(GT_PCI0_CFGDATA_OFS
, le32_to_cpu(data
));
571 static void pci1WriteConfigReg(unsigned int offset
,
572 struct pci_dev
*device
, unsigned int data
)
574 unsigned int DataForRegCf8
;
576 DataForRegCf8
= ((PCI_SLOT(device
->devfn
) << 11) |
577 (PCI_FUNC(device
->devfn
) << 8) |
578 (offset
& ~0x3)) | 0x80000000;
579 /* There is a latency problem
580 with trying to read immediately after setting up the address
581 register. The "if" check gives enough time for the address
582 to stabilize, so the WRITE can work.
584 if (PCI_SLOT(device
->devfn
) == SELF
) { /* This board */
585 /* when configurating our own PCI 1 L-unit the access is through
586 the PCI 0 interface with reg number = reg number + 0x80 */
587 DataForRegCf8
|= 0x80;
588 GT_WRITE(GT_PCI0_CFGADDR_OFS
, DataForRegCf8
);
589 } else { /* configuration Transaction over the pci. */
590 /* The PCI is working in LE Mode so swap the Data. */
591 GT_WRITE(GT_PCI1_CFGADDR_OFS
, DataForRegCf8
);
593 if (PCI_SLOT(device
->devfn
) == SELF
) { /* This board */
594 GT_WRITE(GT_PCI0_CFGDATA_OFS
, data
);
595 } else { /* configuration Transaction over the pci. */
596 GT_WRITE(GT_PCI1_CFGADDR_OFS
, le32_to_cpu(data
));
602 * galileo_pcibios_(read/write)_config_(dword/word/byte) -
604 * reads/write a dword/word/byte register from the configuration space
609 * dev - device number
610 * offset - register offset in the configuration space
611 * val - value to be written / read
614 * PCIBIOS_SUCCESSFUL when operation was succesfull
615 * PCIBIOS_DEVICE_NOT_FOUND when the bus or dev is errorneous
616 * PCIBIOS_BAD_REGISTER_NUMBER when accessing non aligned
619 static int galileo_pcibios_read_config_dword(struct pci_dev
*device
,
620 int offset
, u32
* val
)
623 //DBG(KERN_INFO "rcd entry \n",offset,val);
624 bus
= device
->bus
->number
;
625 dev
= PCI_SLOT(device
->devfn
);
627 if (pci_range_ck(bus
, dev
)) {
629 return PCIBIOS_DEVICE_NOT_FOUND
;
632 return PCIBIOS_BAD_REGISTER_NUMBER
;
634 *val
= pci0ReadConfigReg(offset
, device
);
635 // if (bus == 1) *val = pci1ReadConfigReg (offset,device);
636 DBG(KERN_INFO
"rr: rcd dev %d offset %x %x\n", dev
, offset
, *val
);
639 * This is so that the upper PCI layer will get the correct return
640 * value if we're not attached to anything.
642 if ((offset
== 0) && (*val
== 0xffffffff)) {
643 return PCIBIOS_DEVICE_NOT_FOUND
;
646 return PCIBIOS_SUCCESSFUL
;
649 static int galileo_pcibios_read_config_word(struct pci_dev
*device
,
650 int offset
, u16
* val
)
654 bus
= device
->bus
->number
;
655 dev
= PCI_SLOT(device
->devfn
);
657 if (pci_range_ck(bus
, dev
)) {
659 return PCIBIOS_DEVICE_NOT_FOUND
;
662 return PCIBIOS_BAD_REGISTER_NUMBER
;
666 (unsigned short) (pci0ReadConfigReg(offset
, device
) >>
667 ((offset
& ~0x3) * 8));
668 // if (bus == 1) *val = (unsigned short) (pci1ReadConfigReg(offset,device) >> ((offset & ~0x3) * 8));
670 DBG(KERN_INFO
"rr: rcw dev %d offset %x %x\n", dev
, offset
, *val
);
672 return PCIBIOS_SUCCESSFUL
;
675 static int galileo_pcibios_read_config_byte(struct pci_dev
*device
,
676 int offset
, u8
* val
)
680 bus
= device
->bus
->number
;
681 dev
= PCI_SLOT(device
->devfn
);
683 if (pci_range_ck(bus
, dev
)) {
685 return PCIBIOS_DEVICE_NOT_FOUND
;
690 (unsigned char) (pci0ReadConfigReg(offset
, device
) >>
691 ((offset
& ~0x3) * 8));
692 // if (bus == 1) *val = (unsigned char) (pci1ReadConfigReg(offset,device) >> ((offset & ~0x3) * 8));
694 DBG(KERN_INFO
"rr: rcb dev %d offset %x %x\n", dev
, offset
, *val
);
696 /* This is so that the upper PCI layer will get the correct return value if
697 we're not attached to anything. */
698 if ((offset
== 0xe) && (*val
== 0xff)) {
700 GT_READ(GT_INTRCAUSE_OFS
, &MasterAbort
);
701 if (MasterAbort
& 0x40000) {
702 DBG(KERN_INFO
"PCI Master Abort, ICR %x\n",
704 GT_WRITE(GT_INTRCAUSE_OFS
,
705 (MasterAbort
& 0xfffbffff));
706 return PCIBIOS_DEVICE_NOT_FOUND
;
710 return PCIBIOS_SUCCESSFUL
;
713 static int galileo_pcibios_write_config_dword(struct pci_dev
*device
,
718 bus
= device
->bus
->number
;
719 dev
= PCI_SLOT(device
->devfn
);
721 if (pci_range_ck(bus
, dev
))
722 return PCIBIOS_DEVICE_NOT_FOUND
;
724 return PCIBIOS_BAD_REGISTER_NUMBER
;
726 pci0WriteConfigReg(offset
, device
, val
);
727 // if (bus == 1) pci1WriteConfigReg (offset,device,val);
729 DBG(KERN_INFO
"rr: wcd dev %d, offset %x, val %x\n", dev
, offset
,
731 return PCIBIOS_SUCCESSFUL
;
735 static int galileo_pcibios_write_config_word(struct pci_dev
*device
,
741 bus
= device
->bus
->number
;
742 dev
= PCI_SLOT(device
->devfn
);
744 if (pci_range_ck(bus
, dev
))
745 return PCIBIOS_DEVICE_NOT_FOUND
;
747 return PCIBIOS_BAD_REGISTER_NUMBER
;
749 tmp
= pci0ReadConfigReg(offset
, device
);
750 // if (bus == 1) tmp = pci1ReadConfigReg (offset,device);
752 if ((offset
% 4) == 0)
753 tmp
= (tmp
& 0xffff0000) | (val
& 0xffff);
754 if ((offset
% 4) == 2)
755 tmp
= (tmp
& 0x0000ffff) | ((val
& 0xffff) << 16);
758 pci0WriteConfigReg(offset
, device
, tmp
);
759 // if (bus == 1) pci1WriteConfigReg (offset,device,tmp);
760 DBG(KERN_INFO
"rr: wcw dev %d, offset %x, val %x\n", dev
, offset
,
762 return PCIBIOS_SUCCESSFUL
;
765 static int galileo_pcibios_write_config_byte(struct pci_dev
*device
,
771 bus
= device
->bus
->number
;
772 dev
= PCI_SLOT(device
->devfn
);
774 if (pci_range_ck(bus
, dev
))
775 return PCIBIOS_DEVICE_NOT_FOUND
;
777 tmp
= pci0ReadConfigReg(offset
, device
);
778 // if (bus == 1) tmp = pci1ReadConfigReg (offset,device);
780 if ((offset
% 4) == 0)
781 tmp
= (tmp
& 0xffffff00) | (val
& 0xff);
782 if ((offset
% 4) == 1)
783 tmp
= (tmp
& 0xffff00ff) | ((val
& 0xff) << 8);
784 if ((offset
% 4) == 2)
785 tmp
= (tmp
& 0xff00ffff) | ((val
& 0xff) << 16);
786 if ((offset
% 4) == 3)
787 tmp
= (tmp
& 0x00ffffff) | ((val
& 0xff) << 24);
790 pci0WriteConfigReg(offset
, device
, tmp
);
791 // if (bus == 1) pci1WriteConfigReg (offset,device,tmp);
792 DBG(KERN_INFO
"rr: wcb dev %d, offset %x, val %x\n", dev
, offset
,
795 return PCIBIOS_SUCCESSFUL
;
798 static void galileo_pcibios_set_master(struct pci_dev
*dev
)
802 DBG(KERN_INFO
"rr: galileo_pcibios_set_master\n");
804 galileo_pcibios_read_config_word(dev
, PCI_COMMAND
, &cmd
);
805 cmd
|= PCI_COMMAND_MASTER
;
806 galileo_pcibios_write_config_word(dev
, PCI_COMMAND
, cmd
);
807 DBG("PCI: Enabling device %s (%04x)\n", dev
->slot_name
, cmd
);
810 /* Externally-expected functions. Do not change function names */
812 int pcibios_enable_resources(struct pci_dev
*dev
)
820 DBG(KERN_INFO
"rr: pcibios_enable_resources\n");
822 galileo_pcibios_read_config_word(dev
, PCI_COMMAND
, &cmd
);
824 for (idx
= 0; idx
< 6; idx
++) {
825 r
= &dev
->resource
[idx
];
827 "rr: BAR %d, start %lx, end %lx, flags %lx\n", idx
,
828 r
->start
, r
->end
, r
->flags
);
829 if (!r
->start
&& r
->end
) {
831 "PCI: Device %s not available because of resource collisions\n",
835 if (r
->flags
& IORESOURCE_IO
)
836 cmd
|= PCI_COMMAND_IO
;
837 if (r
->flags
& IORESOURCE_MEM
)
838 cmd
|= PCI_COMMAND_MEMORY
;
840 if (cmd
!= old_cmd
) {
841 DBG(KERN_INFO
"PCI: Enabling device %s (%04x -> %04x)\n",
842 dev
->slot_name
, old_cmd
, cmd
);
843 galileo_pcibios_write_config_word(dev
, PCI_COMMAND
, cmd
);
847 Let's fix up the latency timer and cache line size here. Cache line size =
848 32 bytes / sizeof dword (4) = 8.
849 Latency timer must be > 8. 32 is random but appears to work.
851 galileo_pcibios_read_config_byte(dev
, PCI_CACHE_LINE_SIZE
, &tmp1
);
854 "rr: PCI setting cache line size to 8 from %d\n",
856 galileo_pcibios_write_config_byte(dev
, PCI_CACHE_LINE_SIZE
,
859 galileo_pcibios_read_config_byte(dev
, PCI_LATENCY_TIMER
, &tmp1
);
862 "rr: PCI setting latency timer to 32 from %d\n", tmp1
);
863 galileo_pcibios_write_config_byte(dev
, PCI_LATENCY_TIMER
,
870 int pcibios_enable_device(struct pci_dev
*dev
, int mask
)
872 DBG(KERN_INFO
"rr: pcibios_enable_device\n");
873 return pcibios_enable_resources(dev
);
876 void pcibios_align_resource(void *data
, struct resource
*res
,
877 unsigned long size
, unsigned long align
)
879 if (res
->flags
& IORESOURCE_IO
) {
880 unsigned long start
= res
->start
;
882 /* We need to avoid collisions with `mirrored' VGA ports
883 and other strange ISA hardware, so we always want the
884 addresses kilobyte aligned. */
886 DBG(KERN_ERR
"PCI: I/O Region %s/%d too large"
887 " (%ld bytes)\n", dev
->slot_name
,
888 dev
->resource
- res
, size
);
891 start
= (start
+ 1024 - 1) & ~(1024 - 1);
897 * structure galileo_pci_ops
899 * This structure holds the pointers for the PCI configuration space
900 * access, and the fixup for the interrupts.
901 * This structure is registered to the operating system in boot time
903 struct pci_ops galileo_pci_ops
= {
904 galileo_pcibios_read_config_byte
,
905 galileo_pcibios_read_config_word
,
906 galileo_pcibios_read_config_dword
,
907 galileo_pcibios_write_config_byte
,
908 galileo_pcibios_write_config_word
,
909 galileo_pcibios_write_config_dword
913 * galileo_pcibios_fixup_bus -
915 * After detecting all agents over the PCI , this function is called
916 * in order to give an interrupt number for each PCI device starting
917 * from IRQ 20. It does also enables master for each device.
920 * mem_start , mem_end are not relevant in MIPS architecture.
923 * return always mem_start
925 static void __devinit
galileo_pcibios_fixup_bus(struct pci_bus
*bus
)
927 unsigned int Current_IRQ
= 20;
928 struct pci_bus
*current_bus
= bus
;
929 struct pci_dev
*devices
;
930 struct list_head
*devices_link
;
932 list_for_each(devices_link
, &(current_bus
->devices
)) {
933 devices
= pci_dev_b(devices_link
);
934 if (devices
!= NULL
) {
935 devices
->irq
= Current_IRQ
++;
937 /* Assign an interrupt number for the device */
938 galileo_pcibios_write_config_byte(devices
,
941 galileo_pcibios_set_master(devices
);
948 struct pci_fixup pcibios_fixups
[] = {
949 // { PCI_FIXUP_HEADER, 0x4620, 0x11ab, galileo_pcibios_fixup },
953 void __devinit
pcibios_fixup_bus(struct pci_bus
*c
)
955 DBG(KERN_INFO
"rr: pcibios_fixup_bus\n");
956 galileo_pcibios_fixup_bus(c
);
960 * This code was derived from Galileo Technology's example
961 * and significantly reworked.
963 * This is very simple. It does not scan multiple function devices. It does
964 * not scan behind bridges. Those would be simple to implement, but we don't
965 * currently need this.
967 static void __init
scan_and_initialize_pci(void)
969 struct pci_device pci_devices
[MAX_PCI_DEVS
];
971 if (scan_pci_bus(pci_devices
)) {
972 allocate_pci_space(pci_devices
);
977 * This is your basic PCI scan. It goes through each slot and checks to
978 * see if there's something that responds. If so, then get the size and
979 * type of each of the responding BARs. Save them for later.
982 static u32 __init
scan_pci_bus(struct pci_device
*pci_devices
)
984 u32 arrayCounter
= 0;
990 struct pci_dev device
;
992 DBG(KERN_INFO
"rr: scan_pci_bus\n");
995 According to PCI REV 2.1 MAX agents on the bus are 21.
996 We don't bother scanning ourselves (slot 0).
998 for (pci_slot
= 1; pci_slot
< 22; pci_slot
++) {
1000 device
.devfn
= PCI_DEVFN(pci_slot
, 0);
1001 id
= pci0ReadConfigReg(PCI_VENDOR_ID
, &device
);
1003 /* Check for a PCI Master Abort (nothing responds in the slot) */
1004 GT_READ(GT_INTRCAUSE_OFS
, &c18RegValue
);
1005 /* Clearing bit 18 of in the Cause Register 0xc18 by writting 0. */
1006 GT_WRITE(GT_INTRCAUSE_OFS
, (c18RegValue
& 0xfffbffff));
1007 if ((id
!= 0xffffffff) && !(c18RegValue
& 0x40000)) {
1008 DBG(KERN_INFO
"rr: found device %x, slot %d\n", id
,
1010 pci_devices
[arrayCounter
].slot
= pci_slot
;
1011 for (bar
= 0; bar
< 6; bar
++) {
1013 pci0ReadConfigReg(PCI_BASE_ADDRESS_0
+
1014 (bar
* 4), &device
);
1015 pci_devices
[arrayCounter
].BARtype
[bar
] =
1017 pci0WriteConfigReg(PCI_BASE_ADDRESS_0
+
1021 pci0ReadConfigReg(PCI_BASE_ADDRESS_0
+
1022 (bar
* 4), &device
);
1023 if (memType
& 1) { /* IO space */
1024 pci_devices
[arrayCounter
].
1026 ~(memSize
& 0xfffffffc) + 1;
1027 } else { /* memory space */
1028 pci_devices
[arrayCounter
].
1030 ~(memSize
& 0xfffffff0) + 1;
1033 "rr: BAR %d, type %d, size %x\n", bar
,
1035 pci_devices
[arrayCounter
].
1041 /* found a device */
1042 } /* slot counter */
1044 DBG(KERN_INFO
"rr: found %d devices\n", arrayCounter
);
1045 if (arrayCounter
< MAX_PCI_DEVS
) {
1046 pci_devices
[arrayCounter
].slot
= -1;
1048 return (arrayCounter
);
1051 #define ALIGN(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
1052 #define MAX(val1, val2) ((val1) > (val2) ? (val1) : (val2))
1055 * This function goes through the list of devices and allocates the BARs in
1056 * either IO or MEM space. It does it in order of size, which will limit the
1057 * amount of fragmentation we have in the IO and MEM spaces.
1060 static void __init
allocate_pci_space(struct pci_device
*pci_devices
)
1062 u32 count
, maxcount
, bar
;
1063 u32 maxSize
, maxDevice
, maxBAR
;
1066 u32 pci0_mem_base
= pci0GetMemory0Base();
1067 u32 pci0_io_base
= pci0GetIOspaceBase();
1068 struct pci_dev device
;
1070 DBG(KERN_INFO
"rr: allocate_pci_space\n");
1072 DBG(KERN_INFO
"pci0_io_base %x\n", pci0_io_base
);
1073 DBG(KERN_INFO
"pci0_mem_base %x\n", pci0_mem_base
);
1075 /* How many PCI devices do we have? */
1076 maxcount
= MAX_PCI_DEVS
;
1077 for (count
= 0; count
< MAX_PCI_DEVS
; count
++) {
1078 if (pci_devices
[count
].slot
== -1) {
1084 // DBG(KERN_INFO "Found %d devices\n", maxcount);
1087 /* Find the largest size BAR we need to allocate */
1089 for (count
= 0; count
< maxcount
; count
++) {
1090 for (bar
= 0; bar
< 6; bar
++) {
1091 if (pci_devices
[count
].BARsize
[bar
] >
1103 We've found the largest BAR. Allocate it into IO or
1104 mem space. We don't idiot check the bases to make
1105 sure they haven't overflowed the current size for that aperture.
1107 Don't bother to enable the device's IO or MEM space here. That will
1108 be done in pci_enable_resources if the device is activated by a driver.
1112 PCI_DEVFN(pci_devices
[maxDevice
].slot
, 0);
1113 if (pci_devices
[maxDevice
].BARtype
[maxBAR
] == 1) {
1114 alignto
= MAX(0x1000, maxSize
);
1115 base
= ALIGN(pci0_io_base
, alignto
);
1116 pci0WriteConfigReg(PCI_BASE_ADDRESS_0
+
1117 (maxBAR
* 4), &device
,
1119 pci0_io_base
= base
+ alignto
;
1121 "Device %d BAR %d address %x\n",
1122 pci_devices
[maxDevice
].slot
, maxBAR
,
1124 DBG(KERN_INFO
"New IO base %x\n",
1127 alignto
= MAX(0x1000, maxSize
);
1128 base
= ALIGN(pci0_mem_base
, alignto
);
1129 pci0WriteConfigReg(PCI_BASE_ADDRESS_0
+
1130 (maxBAR
* 4), &device
,
1132 pci0_mem_base
= base
+ alignto
;
1134 "Device %d BAR %d address %x\n",
1135 pci_devices
[maxDevice
].slot
, maxBAR
,
1137 DBG(KERN_INFO
"New mem base %x\n",
1141 This entry is finished. Remove it from the list we'll scan.
1143 pci_devices
[maxDevice
].BARsize
[maxBAR
] = 0;
1148 unsigned __init
int pcibios_assign_all_busses(void)
1153 static int __init
pcibios_init(void)
1157 struct pci_dev controller
;
1159 controller
.devfn
= SELF
;
1161 DBG(KERN_INFO
"rr: pcibios_init\n");
1162 GT_READ(GT_PCI0_CMD_OFS
, &tmp
);
1163 DBG(KERN_INFO
"rr: PCI0 command - %x\n", tmp
);
1164 GT_READ(GT_PCI0_BARE_OFS
, &tmp
);
1165 DBG(KERN_INFO
"rr: BAR0 - %x\n", tmp
);
1168 * You have to enable bus mastering to configure any other
1171 tmp
= pci0ReadConfigReg(PCI_COMMAND
, &controller
);
1172 DBG(KERN_INFO
"rr: command/status - %x\n", tmp
);
1173 tmp
|= PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
| PCI_COMMAND_SERR
;
1174 DBG(KERN_INFO
"rr: new command/status - %x\n", tmp
);
1175 pci0WriteConfigReg(PCI_COMMAND
, &controller
, tmp
);
1177 /* This scans the PCI bus and sets up initial values. */
1178 scan_and_initialize_pci();
1181 * Reset PCI I/O and PCI MEM values to ones supported by EVM.
1183 ioport_resource
.start
= 0x10000000;
1184 ioport_resource
.end
= 0x11ffffff; /* 32 MB */
1185 iomem_resource
.start
= 0x12000000;
1186 iomem_resource
.end
= 0x13ffffff; /* 32 MB */
1188 pci_scan_bus(0, &galileo_pci_ops
, NULL
);
1193 subsys_initcall(pcibios_init
);
1195 char *pcibios_setup(char *str
)
1197 printk(KERN_INFO
"rr: pcibios_setup\n");
1198 /* Nothing to do for now. */