2 * p6032/pci_machdep.c: Machine-specific functions for PCI autoconfiguration.
4 * Copyright (c) 2000-2001, Algorithmics Ltd. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the "Free MIPS" License Agreement, a copy of
8 * which is available at:
10 * http://www.algor.co.uk/ftp/pub/doc/freemips-license.txt
12 * You may not, however, modify or remove any part of this copyright
13 * message if this program is redistributed or reused in whole or in
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * "Free MIPS" License for more details.
28 #define PCI_MISC_LTIM_SHIFT 8
29 #define PCI_MISC_LTIM_MASK 0xff
30 #define PCI_MISC_LTIM(mr) \
31 (((mr) >> PCI_MISC_LTIM_SHIFT) & PCI_MISC_LTIM_MASK)
32 #define PCI_MISC_LTIM_SET(mr,v) \
33 (mr) = ((mr) & ~(PCI_MISC_LTIM_MASK << PCI_MISC_LTIM_SHIFT)) | \
34 ((v) << PCI_MISC_LTIM_SHIFT)
37 /* default PCI mem regions in PCI space */
38 #define PCI_MEM_SPACE_PCI_BASE 0x00000000
39 #define PCI_LOCAL_MEM_PCI_BASE 0x80000000
40 #define PCI_LOCAL_MEM_ISA_BASE 0x00800000
42 /* soft versions of above */
43 static pcireg_t
const pci_mem_space
= PCI_MEM_SPACE
; /* CPU base for access to mapped PCI memory */
44 static pcireg_t pci_mem_space_pci_base
; /* PCI mapped memory base */
45 static pcireg_t pci_local_mem_pci_base
; /* PCI address of local memory */
46 static pcireg_t pci_local_mem_isa_base
; /* PCI address of ISA accessible local memory */
48 static pcireg_t
const pci_io_space
= PCI_IO_SPACE
; /* CPU base for access to mapped PCI IO */
49 static pcireg_t
const pci_io_space_pci_base
= 0; /* PCI mapped IO base */
51 /* PCI mem space allocation */
52 pcireg_t minpcimemaddr
;
53 pcireg_t nextpcimemaddr
;
54 pcireg_t minpcimemaddr
;
55 pcireg_t maxpcimemaddr
;
58 /* PCI i/o space allocation */
59 pcireg_t minpciioaddr
;
60 pcireg_t maxpciioaddr
;
61 pcireg_t nextpciioaddr
;
63 typedef long hsaddr_t
;
65 #define hs_write8(a,b) *((volatile uint8_t *) (a)) = (b)
66 #define hs_write16(a,b) *((volatile uint16_t *) (a)) = (b)
67 #define hs_write32(a,b) *((volatile uint32_t *) (a)) = (b)
68 #define hs_write64(a,b) *((volatile uint32_t *) (a)) = (b)
69 #define hs_read8(a) *((volatile uint8_t *) (a))
70 #define hs_read16(a) *((volatile uint16_t *) (a))
71 #define hs_read32(a) *((volatile uint32_t *) (a))
72 #define hs_read64(a) *((volatile uint64_t *) (a))
75 static const struct pci_bus bonito_pci_bus
= {
76 0, /* minimum grant */
77 255, /* maximum latency */
78 0, /* devsel time = fast */
79 0, /* we don't support fast back-to-back */
80 0, /* we don't support prefetch */
81 0, /* we don't support 66 MHz */
82 0, /* we don't support 64 bits */
83 4000000, /* bandwidth: in 0.25us cycles / sec */
84 1 /* initially one device on bus (i.e. us) */
88 const cons_t pci_optnames
[] = {
89 {"verbose",PCI_FLG_VERBOSE
},
93 extern int _pciverbose
;
96 const int _pci_maxbus
= MAXBUS
; /* maximum # buses we support */
97 static int _pci_nbus
= 0;
98 static struct pci_bus _pci_bus
[MAXBUS
];
100 /* Access functions */
102 /* The following must either fail or return the next sequential bus
103 number to make secondary/subordinate numbering work correctly. */
105 pci_nextbus (int port
)
116 pci_maxbus (int port
)
118 return _pci_nbus
- 1;
122 pci_businfo (int port
, int bus
)
124 return (bus
< _pci_nbus
? &_pci_bus
[bus
] : NULL
);
128 * PCI address resources.
129 * NB: initial limits for address allocation are assumed to be aligned
130 * appropriately for PCI bridges (4K boundaries for I/O, 1M for memory).
134 pci_minmemaddr (int port
)
136 return minpcimemaddr
;
140 pci_maxmemaddr (int port
)
142 return maxpcimemaddr
;
146 pci_minioaddr (int port
)
152 pci_maxioaddr (int port
)
157 static char * const _bonito
= (char * const) PHYS_TO_K1(BONITO_BASE
);
159 static int pcimaster
;
160 static int pcireserved
;
163 * Called to initialise the bridge at the beginning of time
166 pci_hwinit (int port
, pci_flags_t flags
)
174 _pci_bus
[0] = bonito_pci_bus
;
177 if (initialise
&& pcimaster
) {
179 * We are initialising and we are the bus master
180 * so we get to define the mappings
182 pci_mem_space_pci_base
= PCI_MEM_SPACE_PCI_BASE
;
184 pci_local_mem_pci_base
= PCI_LOCAL_MEM_PCI_BASE
;
185 pci_local_mem_isa_base
= PCI_LOCAL_MEM_ISA_BASE
;
187 /* point to start and end of region (leaving bottom 16M for ISA) */
188 minpcimemaddr
= pci_mem_space_pci_base
;
189 if (minpcimemaddr
< 0x1000000)
190 minpcimemaddr
= 0x1000000;
191 nextpcimemaddr
= pci_mem_space_pci_base
+ PCI_MEM_SPACE_SIZE
;
192 maxpcimemaddr
= nextpcimemaddr
;
194 printf("minpcimem=%08X next=%08X\n",minpcimemaddr
,nextpcimemaddr
);
196 /* leave 64KB at beginning of PCI i/o space for ISA bridge */
197 minpciioaddr
= pci_io_space_pci_base
;
198 if (minpciioaddr
< 0x10000)
199 minpciioaddr
= 0x10000;
200 nextpciioaddr
= pci_io_space_pci_base
+ PCI_IO_SPACE_SIZE
;
201 maxpciioaddr
= nextpciioaddr
;
204 /* If we are not the master then wait for somebody to configure us */
206 /* wait for up to 3s until we get configured */
208 for (i
= 0; i
< 300; i
++) {
210 if (BONITO_PCICMD
& PCI_COMMAND_MEM_ENABLE
)
213 /* wait a little bit longer for the master scan to complete */
217 /* This assumes the address space is contiguous starting at PCIMAP_LO0 */
218 pci_mem_space_pci_base
= (BONITO_PCIMAP
& BONITO_PCIMAP_PCIMAP_LO0
) << (26 - BONITO_PCIMAP_PCIMAP_LO0_SHIFT
);
220 pci_local_mem_pci_base
= BONITO_PCIBASE0
& PCI_MAPREG_MEM_ADDR_MASK
;
222 /* assume BAR1 is suitable for 24 bit accesses */
223 pci_local_mem_isa_base
= BONITO_PCIBASE1
& PCI_MAPREG_MEM_ADDR_MASK
;
228 /* set up Local->PCI mappings */
229 /* LOCAL:PCI_MEM_SPACE+00000000 -> PCI:pci_mem_space_pci_base#0x0c000000 */
230 /* LOCAL:80000000 -> PCI:80000000#0x80000000 */
232 BONITO_PCIMAP_WIN(0, pci_mem_space_pci_base
+0x00000000) |
233 BONITO_PCIMAP_WIN(1, pci_mem_space_pci_base
+0x04000000) |
234 BONITO_PCIMAP_WIN(2, pci_mem_space_pci_base
+0x08000000) |
235 BONITO_PCIMAP_PCIMAP_2
;
237 /* LOCAL:PCI_IO_SPACE -> PCI:00000000-00100000 */
242 /* set up PCI->Local mappings */
244 /* pcimembasecfg has been set up by low-level code */
246 /* Initialise BARs for access to our memory */
247 /* PCI:pci_local_mem_pci_base -> LOCAL:0 */
248 BONITO_PCIBASE0
= pci_local_mem_pci_base
;
250 /* PCI:pci_local_mem_isa_base -> LOCAL:0 */
251 BONITO_PCIBASE1
= pci_local_mem_isa_base
;
253 /* PCI:PCI_LOCAL_MEM_PCI_BASE+10000000 -> LOCAL bonito registers (512b) */
254 BONITO_PCIBASE2
= pci_local_mem_pci_base
+ 0x10000000;
259 #ifdef BONITO_PCICFG_PCIRESET
260 /* reset the PCI bus */
261 BONITO_PCICFG
&= ~BONITO_PCICFG_PCIRESET
;
264 /* unreset the PCI bus */
265 BONITO_PCICFG
|= BONITO_PCICFG_PCIRESET
;
269 return initialise
&& pcimaster
;
274 * Called to reinitialise the bridge after we've scanned each PCI device
275 * and know what is possible.
278 pci_hwreinit (int port
, pci_flags_t flags
)
281 /* set variable latency timer */
282 PCI_MISC_LTIM_SET(BONITO_PCILTIMER
, _pci_bus
[0].def_ltim
);
284 if (_pci_bus
[0].prefetch
) {
285 /* we can safely prefetch from all pci mem devices */
294 /* flush read-ahead fifos (!) */
301 pci_make_tag(int port
, int bus
, int device
, int function
)
304 tag
= (bus
<< 16) | (device
<< 11) | (function
<< 8);
309 pci_break_tag(pcitag_t tag
,
310 int *portp
, int *busp
, int *devicep
, int *functionp
)
312 if (portp
) *portp
= 0;
313 if (busp
) *busp
= (tag
>> 16) & 255;
314 if (devicep
) *devicep
= (tag
>> 11) & 31;
315 if (functionp
) *functionp
= (tag
>> 8) & 7;
319 pci_canscan (pcitag_t tag
)
322 pci_break_tag (tag
, NULL
, &bus
, &dev
, NULL
);
323 return !(bus
== 0 && (pcireserved
& (1 << dev
)));
327 * flush Bonito register writes
329 static void __inline__
332 (void)BONITO_PCIMAP_CFG
; /* register address is arbitrary */
336 pci_conf_read(pcitag_t tag
, int reg
)
340 int bus
, device
, function
;
342 if ((reg
& 3) || reg
< 0 || reg
>= 0x100) {
343 if (_pciverbose
>= 1)
344 pci_tagprintf (tag
, "pci_conf_read: bad reg 0x%x\n", reg
);
348 pci_break_tag (tag
, NULL
, &bus
, &device
, &function
);
350 /* Type 0 configuration on onboard PCI bus */
351 if (device
> 20 || function
> 7)
352 return ~0; /* device out of range */
353 addr
= (1 << (device
+11)) | (function
<< 8) | reg
;
357 /* Type 1 configuration on offboard PCI bus */
358 if (bus
> 255 || device
> 31 || function
> 7)
359 return ~0; /* device out of range */
360 addr
= (bus
<< 16) | (device
<< 11) | (function
<< 8) | reg
;
365 BONITO_PCICMD
|= PCI_STATUS_MASTER_ABORT
| PCI_STATUS_MASTER_TARGET_ABORT
;
367 BONITO_PCIMAP_CFG
= (addr
>> 16) | type
;
370 data
= *(volatile pcireg_t
*)PHYS_TO_K1(BONITO_PCICFG_BASE
| (addr
& 0xfffc));
372 stat
= BONITO_PCICMD
;
374 if (stat
& PCI_STATUS_MASTER_ABORT
) {
378 if (stat
& PCI_STATUS_MASTER_TARGET_ABORT
) {
379 if (_pciverbose
>= 1)
380 pci_tagprintf (tag
, "pci_conf_read: target abort\n");
389 pci_conf_write(pcitag_t tag
, int reg
, pcireg_t data
)
393 int bus
, device
, function
;
395 if ((reg
& 3) || reg
< 0 || reg
>= 0x100) {
396 if (_pciverbose
>= 1)
397 pci_tagprintf (tag
, "pci_conf_write: bad reg %x\n", reg
);
401 pci_break_tag (tag
, NULL
, &bus
, &device
, &function
);
404 /* Type 0 configuration on onboard PCI bus */
405 if (device
> 20 || function
> 7)
406 return; /* device out of range */
407 addr
= (1 << (device
+11)) | (function
<< 8) | reg
;
411 /* Type 1 configuration on offboard PCI bus */
412 if (bus
> 255 || device
> 31 || function
> 7)
413 return; /* device out of range */
414 addr
= (bus
<< 16) | (device
<< 11) | (function
<< 8) | reg
;
419 BONITO_PCICMD
|= PCI_STATUS_MASTER_ABORT
| PCI_STATUS_MASTER_TARGET_ABORT
;
421 BONITO_PCIMAP_CFG
= (addr
>> 16) | type
;
424 *(volatile pcireg_t
*)PHYS_TO_K1(BONITO_PCICFG_BASE
| (addr
& 0xfffc)) = data
;
425 stat
= BONITO_PCICMD
;
427 if (stat
& PCI_STATUS_MASTER_ABORT
) {
428 if (_pciverbose
>= 1)
429 pci_tagprintf (tag
, "pci_conf_write: master abort\n");
432 if (stat
& PCI_STATUS_MASTER_TARGET_ABORT
) {
433 if (_pciverbose
>= 1)
434 pci_tagprintf (tag
, "pci_conf_write: target abort\n");
439 pci_conf_write_acked(pcitag_t tag
, int reg
, pcireg_t data
)
441 pci_conf_write(tag
,reg
,data
);
446 pci_conf_read16(pcitag_t tag
, int reg
)
448 return pci_conf_read (tag
, reg
);
452 pci_conf_write16(pcitag_t tag
, int reg
, pcireg_t data
)
454 pci_conf_write (tag
, reg
, data
);
461 pci_map_io(pcitag_t tag
, int reg
, pci_endian_t endian
, phys_addr_t
*pap
)
466 if (reg
< PCI_MAPREG_START
|| reg
>= PCI_MAPREG_END
|| (reg
& 3)) {
467 if (_pciverbose
>= 1)
468 pci_tagprintf(tag
, "_pci_map_io: bad request\n");
472 address
= pci_conf_read(tag
, reg
);
474 if ((address
& PCI_MAPREG_TYPE_IO
) == 0) {
475 if (_pciverbose
>= 1)
476 pci_tagprintf (tag
, "_pci_map_io: attempt to i/o map a memory region\n");
480 pa
= (address
& PCI_MAPREG_IO_ADDR_MASK
) - pci_io_space_pci_base
;
484 if (_pciverbose
>= 3)
485 pci_tagprintf(tag
, "_pci_map_io: mapping i/o at physical %016llx\n",
492 pci_map_mem(pcitag_t tag
, int reg
, pci_endian_t endian
, phys_addr_t
*pap
)
497 if (reg
== PCI_MAPREG_ROM
) {
499 address
= pci_conf_read(tag
, reg
);
500 if (!(address
& PCI_MAPREG_ROM
)) {
501 pci_tagprintf (tag
, "_pci_map_mem: attempt to map missing rom\n");
504 pa
= address
& PCI_MAPREG_ROM_ADDR_MASK
;
507 if (reg
< PCI_MAPREG_START
|| reg
>= PCI_MAPREG_END
|| (reg
& 3)) {
508 if (_pciverbose
>= 1)
509 pci_tagprintf(tag
, "_pci_map_mem: bad request\n");
513 address
= pci_conf_read(tag
, reg
);
515 if ((address
& PCI_MAPREG_TYPE_IO
) != 0) {
516 if (_pciverbose
>= 1)
517 pci_tagprintf(tag
, "pci_map_mem: attempt to memory map an I/O region\n");
521 switch (address
& PCI_MAPREG_MEM_TYPE_MASK
) {
522 case PCI_MAPREG_MEM_TYPE_32BIT
:
523 case PCI_MAPREG_MEM_TYPE_32BIT_1M
:
525 case PCI_MAPREG_MEM_TYPE_64BIT
:
526 if (_pciverbose
>= 1)
527 pci_tagprintf (tag
, "_pci_map_mem: attempt to map 64-bit region\n");
530 if (_pciverbose
>= 1)
531 pci_tagprintf (tag
, "_pci_map_mem: reserved mapping type\n");
534 pa
= address
& PCI_MAPREG_MEM_ADDR_MASK
;
538 pa
-= pci_mem_space_pci_base
;
542 if (_pciverbose
>= 3)
543 pci_tagprintf (tag
, "_pci_map_mem: mapping memory at physical 0x%016llX\n",
549 * allocate PCI address spaces
550 * only applicable if we are the PCI master
557 * Handle mapping of fixed ISA addresses
561 #define ISAPORT_ADDR(port) (PHYS_TO_K1(pci_io_space_pci_base + (port)))
565 inb (unsigned int port
)
567 return hs_read8(ISAPORT_ADDR(port
));
571 inw (unsigned int port
)
573 return hs_read16(ISAPORT_ADDR(port
));
577 inl (unsigned int port
)
579 return hs_read32(ISAPORT_ADDR(port
));
583 outb (unsigned int port
, uint8_t val
)
585 hs_write8(ISAPORT_ADDR(port
),val
);
590 outw (unsigned int port
, uint16_t val
)
592 hs_write16(ISAPORT_ADDR(port
),val
);
597 outl (unsigned int port
, uint32_t val
)
599 hs_write32(ISAPORT_ADDR(port
),val
);
605 pci_probe_tag(pcitag_t tag
)
609 if (!pci_canscan(tag
))
612 data
= pci_conf_read(tag
,PCI_ID_REG
);
616 if ((data
== 0) || (data
== 0xffffffff)) return 0;
622 pci_map_window(phys_addr_t va
,
623 unsigned int offset
, unsigned int len
,
624 int l2ca
, int endian
)
630 pci_unmap_window(unsigned int offset
, unsigned int len
)
636 pci_device_preset (pcitag_t tag
)
638 /* Nothing to do for now. */
642 pci_device_setup (pcitag_t tag
)
644 /* Nothing to do for now. */
648 /* Called for each bridge after configuring the secondary bus, to allow
649 device-specific initialization. */
651 pci_bridge_setup (pcitag_t tag
, pci_flags_t flags
)
656 /* The base shift of a slot or device on the motherboard. Only device
657 ids 3, 4 and 5 are implemented as PCI connectors. */
659 pci_int_shift_0(pcitag_t tag
)
663 pci_break_tag (tag
, NULL
, &bus
, &device
, NULL
);
669 case 0: case 1: case 2: /* dedicated on-board devices */
671 case 3: case 4: case 5: /* PCI slots */
672 return ((device
- 3) % 4);
678 #define PCI_INTERRUPT_ENET 5
679 #define PCI_INTERRUPT_SCSI 6
680 #define PCI_INTERRUPT_USB 8
682 /* Return the mapping of a P5064 device/function interrupt to an
683 interrupt line. Values 1-4 indicate the PCIIRQ0-3 inputs to the
684 interrupt mapper, respectively, dedicated values as above, or 0 if
685 there is no mapping. This is board specific. */
687 pci_int_map_0(pcitag_t tag
)
690 int pin
, bus
, device
;
692 data
= pci_conf_read(tag
, PCI_BPARAM_INTERRUPT_REG
);
693 pin
= PCI_INTERRUPT_PIN(data
);
699 if (_pciverbose
>= 1)
700 pci_tagprintf (tag
, "pci_map_int: bad interrupt pin %d\n", pin
);
704 pci_break_tag (tag
, NULL
, &bus
, &device
, NULL
);
711 return PCI_INTERRUPT_ENET
;
713 return PCI_INTERRUPT_SCSI
;
715 return PCI_INTERRUPT_USB
;
716 case 3: case 4: case 5:
717 return (((pin
- 1) + pci_int_shift_0(tag
)) % 4) + 1;
723 /* Map PCI interrupts A, B, C, D into a value for the IntLine
724 register. For SB1250, return the source number used by the
725 interrupt mapper, or 0xff if none. */
727 pci_int_line(uint8_t pci_int
)
730 case PCI_INTERRUPT_ENET
:
732 case PCI_INTERRUPT_SCSI
:
734 case PCI_INTERRUPT_USB
:
736 case 1: case 2: case 3: case 4: /* PCI_INT_A .. PCI_INT_D */
737 return 0x10 << (pci_int
- 1);