2 * Low-Level PCI and SB support for BCM47xx
4 * Copyright 2006, Broadcom Corporation
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
12 * $Id: hndpci.c,v 1.1.1.3 2006/04/08 06:13:39 honor Exp $
23 #include <bcmendian.h>
31 #define PCI_MSG(args) printf args
34 #endif /* BCMDBG_PCI */
36 /* Can free sbpci_init() memory after boot */
41 /* Emulated configuration space */
49 static pci_config_regs sb_config_regs
[SB_MAXCORES
];
50 static sb_bar_cfg_t sb_bar_cfg
[SB_MAXCORES
];
52 /* Links to emulated and real PCI configuration spaces */
55 pci_config_regs
*emu
; /* emulated PCI config */
56 pci_config_regs
*pci
; /* real PCI config */
57 sb_bar_cfg_t
*bar
; /* region sizes */
59 static sb_pci_cfg_t sb_pci_cfg
[SB_MAXCORES
][MAXFUNCS
];
61 /* Special emulated config space for non-existing device */
62 static pci_config_regs sb_pci_null
= { 0xffff, 0xffff };
65 static uint16 pci_ban
[SB_MAXCORES
] = { 0 };
66 static uint pci_banned
= 0;
69 static bool cardbus
= FALSE
;
71 /* Disable PCI host core */
72 static bool pci_disabled
= FALSE
;
74 /* Host bridge slot #, default to 0 */
75 static uint8 pci_hbslot
= 0;
78 #define PCI_SLOTAD_MAP 16 /* SLOT<n> mapps to AD<n+16> */
79 #define PCI_HBSBCFG_REV 8 /* MIN. core rev. required to
80 * access host bridge PCI cfg space
85 * Functions for accessing external PCI configuration space
88 /* Assume one-hot slot wiring */
89 #define PCI_SLOT_MAX 16 /* Max. PCI Slots */
92 config_cmd(sb_t
*sbh
, uint bus
, uint dev
, uint func
, uint off
)
99 /* CardBusMode supports only one device */
100 if (cardbus
&& dev
> 1)
105 coreidx
= sb_coreidx(sbh
);
106 regs
= (sbpciregs_t
*) sb_setcore(sbh
, SB_PCI
, 0);
108 /* Type 0 transaction */
110 /* Skip unwired slots */
111 if (dev
< PCI_SLOT_MAX
) {
114 /* Slide the PCI window to the appropriate slot */
115 win
= (SBTOPCI_CFG0
| ((1 << (dev
+ PCI_SLOTAD_MAP
)) & SBTOPCI1_MASK
));
116 W_REG(osh
, ®s
->sbtopci1
, win
);
118 ((1 << (dev
+ PCI_SLOTAD_MAP
)) & ~SBTOPCI1_MASK
) |
119 (func
<< PCICFG_FUN_SHIFT
) |
123 /* Type 1 transaction */
124 W_REG(osh
, ®s
->sbtopci1
, SBTOPCI_CFG1
);
126 (bus
<< PCICFG_BUS_SHIFT
) |
127 (dev
<< PCICFG_SLOT_SHIFT
) |
128 (func
<< PCICFG_FUN_SHIFT
) |
132 sb_setcoreidx(sbh
, coreidx
);
138 * Read host bridge PCI config registers from Silicon Backplane (>=rev8).
140 * It returns TRUE to indicate that access to the host bridge's pci config
141 * from SB is ok, and values in 'addr' and 'val' are valid.
143 * It can only read registers at multiple of 4-bytes. Callers must pick up
144 * needed bytes from 'val' based on 'off' value. Value in 'addr' reflects
145 * the register address where value in 'val' is read.
148 sb_pcihb_read_config(sb_t
*sbh
, uint bus
, uint dev
, uint func
, uint off
,
149 uint32
**addr
, uint32
*val
)
158 ASSERT(dev
== pci_hbslot
);
163 /* read pci config when core rev >= 8 */
164 coreidx
= sb_coreidx(sbh
);
165 regs
= (sbpciregs_t
*)sb_setcore(sbh
, SB_PCI
, 0);
166 if (regs
&& sb_corerev(sbh
) >= PCI_HBSBCFG_REV
) {
167 *addr
= (uint32
*)®s
->pcicfg
[func
][off
>> 2];
168 *val
= R_REG(osh
, *addr
);
171 sb_setcoreidx(sbh
, coreidx
);
177 extpci_read_config(sb_t
*sbh
, uint bus
, uint dev
, uint func
, uint off
, void *buf
, int len
)
179 uint32 addr
= 0, *reg
= NULL
, val
;
183 * Set value to -1 when:
184 * flag 'pci_disabled' is true;
185 * value of 'addr' is zero;
191 else if (bus
== 1 && dev
== pci_hbslot
&& func
== 0 &&
192 sb_pcihb_read_config(sbh
, bus
, dev
, func
, off
, ®
, &val
))
194 else if (((addr
= config_cmd(sbh
, bus
, dev
, func
, off
)) == 0) ||
195 ((reg
= (uint32
*)REG_MAP(addr
, len
)) == 0) ||
196 (BUSPROBE(val
, reg
) != 0))
199 PCI_MSG(("%s: 0x%x <= 0x%p(0x%x), len %d, off 0x%x, buf 0x%p\n",
200 __FUNCTION__
, val
, reg
, addr
, len
, off
, buf
));
202 val
>>= 8 * (off
& 3);
204 *((uint32
*) buf
) = val
;
206 *((uint16
*) buf
) = (uint16
) val
;
208 *((uint8
*) buf
) = (uint8
) val
;
219 extpci_write_config(sb_t
*sbh
, uint bus
, uint dev
, uint func
, uint off
, void *buf
, int len
)
222 uint32 addr
= 0, *reg
= NULL
, val
;
228 * Ignore write attempt when:
229 * flag 'pci_disabled' is true;
230 * value of 'addr' is zero;
236 else if (bus
== 1 && dev
== pci_hbslot
&& func
== 0 &&
237 sb_pcihb_read_config(sbh
, bus
, dev
, func
, off
, ®
, &val
))
239 else if (((addr
= config_cmd(sbh
, bus
, dev
, func
, off
)) == 0) ||
240 ((reg
= (uint32
*) REG_MAP(addr
, len
)) == 0) ||
241 (BUSPROBE(val
, reg
) != 0))
245 val
= *((uint32
*) buf
);
247 val
&= ~(0xffff << (8 * (off
& 3)));
248 val
|= *((uint16
*) buf
) << (8 * (off
& 3));
249 } else if (len
== 1) {
250 val
&= ~(0xff << (8 * (off
& 3)));
251 val
|= *((uint8
*) buf
) << (8 * (off
& 3));
257 PCI_MSG(("%s: 0x%x => 0x%p\n", __FUNCTION__
, val
, reg
));
259 W_REG(osh
, reg
, val
);
269 * Must access emulated PCI configuration at these locations even when
270 * the real PCI config space exists and is accessible.
274 * PCI_CFG_PROGIF (0x09)
275 * PCI_CFG_SUBCL (0x0a)
276 * PCI_CFG_BASECL (0x0b)
281 #define FORCE_EMUCFG(off, len) \
282 ((off == PCI_CFG_VID) || (off == PCI_CFG_DID) || \
283 (off == PCI_CFG_PROGIF) || \
284 (off == PCI_CFG_SUBCL) || (off == PCI_CFG_BASECL) || \
285 (off == PCI_CFG_HDR) || \
286 (off == PCI_CFG_INT) || (off == PCI_CFG_PIN))
288 /* Sync the emulation registers and the real PCI config registers. */
290 sb_pcid_read_config(sb_t
*sbh
, uint coreidx
, sb_pci_cfg_t
*cfg
,
300 /* decide if real PCI config register access is necessary */
301 if (FORCE_EMUCFG(off
, len
))
306 /* access to the real pci config space only when the core is up */
307 oldidx
= sb_coreidx(sbh
);
308 sb_setcoreidx(sbh
, coreidx
);
309 if (sb_iscoreup(sbh
)) {
311 *(uint32
*)((ulong
)cfg
->emu
+ off
) =
312 htol32(R_REG(osh
, (uint32
*)((ulong
)cfg
->pci
+ off
)));
314 *(uint16
*)((ulong
)cfg
->emu
+ off
) =
315 htol16(R_REG(osh
, (uint16
*)((ulong
)cfg
->pci
+ off
)));
317 *(uint8
*)((ulong
)cfg
->emu
+ off
) =
318 R_REG(osh
, (uint8
*)((ulong
)cfg
->pci
+ off
));
320 sb_setcoreidx(sbh
, oldidx
);
324 sb_pcid_write_config(sb_t
*sbh
, uint coreidx
, sb_pci_cfg_t
*cfg
,
336 /* decide if real PCI config register access is necessary */
337 if (FORCE_EMUCFG(off
, len
))
340 /* access to the real pci config space only when the core is up */
341 oldidx
= sb_coreidx(sbh
);
342 sb_setcoreidx(sbh
, coreidx
);
343 if (sb_iscoreup(sbh
)) {
345 W_REG(osh
, (uint32
*)((ulong
)cfg
->pci
+ off
),
346 ltoh32(*(uint32
*)((ulong
)cfg
->emu
+ off
)));
348 W_REG(osh
, (uint16
*)((ulong
)cfg
->pci
+ off
),
349 ltoh16(*(uint16
*)((ulong
)cfg
->emu
+ off
)));
351 W_REG(osh
, (uint8
*)((ulong
)cfg
->pci
+ off
),
352 *(uint8
*)((ulong
)cfg
->emu
+ off
));
354 sb_setcoreidx(sbh
, oldidx
);
358 * Functions for accessing translated SB configuration space
361 sb_read_config(sb_t
*sbh
, uint bus
, uint dev
, uint func
, uint off
, void *buf
, int len
)
363 pci_config_regs
*cfg
;
365 if (dev
>= SB_MAXCORES
|| func
>= MAXFUNCS
|| (off
+ len
) > sizeof(pci_config_regs
))
367 cfg
= sb_pci_cfg
[dev
][func
].emu
;
369 ASSERT(ISALIGNED(off
, len
));
370 ASSERT(ISALIGNED((uintptr
)buf
, len
));
372 /* use special config space if the device does not exist */
375 /* sync emulation with real PCI config if necessary */
376 else if (sb_pci_cfg
[dev
][func
].pci
)
377 sb_pcid_read_config(sbh
, dev
, &sb_pci_cfg
[dev
][func
], off
, len
);
380 *((uint32
*) buf
) = ltoh32(*((uint32
*)((ulong
) cfg
+ off
)));
382 *((uint16
*) buf
) = ltoh16(*((uint16
*)((ulong
) cfg
+ off
)));
384 *((uint8
*) buf
) = *((uint8
*)((ulong
) cfg
+ off
));
392 sb_write_config(sb_t
*sbh
, uint bus
, uint dev
, uint func
, uint off
, void *buf
, int len
)
396 pci_config_regs
*cfg
;
400 if (dev
>= SB_MAXCORES
|| func
>= MAXFUNCS
|| (off
+ len
) > sizeof(pci_config_regs
))
402 cfg
= sb_pci_cfg
[dev
][func
].emu
;
406 ASSERT(ISALIGNED(off
, len
));
407 ASSERT(ISALIGNED((uintptr
)buf
, len
));
411 /* Emulate BAR sizing */
412 if (off
>= OFFSETOF(pci_config_regs
, base
[0]) &&
413 off
<= OFFSETOF(pci_config_regs
, base
[3]) &&
414 len
== 4 && *((uint32
*) buf
) == ~0) {
415 coreidx
= sb_coreidx(sbh
);
416 if ((regs
= sb_setcoreidx(sbh
, dev
))) {
417 bar
= sb_pci_cfg
[dev
][func
].bar
;
418 /* Highest numbered address match register */
419 if (off
== OFFSETOF(pci_config_regs
, base
[0]))
420 cfg
->base
[0] = ~(bar
->size0
- 1);
421 else if (off
== OFFSETOF(pci_config_regs
, base
[1]) && bar
->n
>= 1)
422 cfg
->base
[1] = ~(bar
->size1
- 1);
423 else if (off
== OFFSETOF(pci_config_regs
, base
[2]) && bar
->n
>= 2)
424 cfg
->base
[2] = ~(bar
->size2
- 1);
425 else if (off
== OFFSETOF(pci_config_regs
, base
[3]) && bar
->n
>= 3)
426 cfg
->base
[3] = ~(bar
->size3
- 1);
428 sb_setcoreidx(sbh
, coreidx
);
431 *((uint32
*)((ulong
) cfg
+ off
)) = htol32(*((uint32
*) buf
));
433 *((uint16
*)((ulong
) cfg
+ off
)) = htol16(*((uint16
*) buf
));
435 *((uint8
*)((ulong
) cfg
+ off
)) = *((uint8
*) buf
);
439 /* sync emulation with real PCI config if necessary */
440 if (sb_pci_cfg
[dev
][func
].pci
)
441 sb_pcid_write_config(sbh
, dev
, &sb_pci_cfg
[dev
][func
], off
, len
);
447 sbpci_read_config(sb_t
*sbh
, uint bus
, uint dev
, uint func
, uint off
, void *buf
, int len
)
450 return sb_read_config(sbh
, bus
, dev
, func
, off
, buf
, len
);
452 return extpci_read_config(sbh
, bus
, dev
, func
, off
, buf
, len
);
456 sbpci_write_config(sb_t
*sbh
, uint bus
, uint dev
, uint func
, uint off
, void *buf
, int len
)
459 return sb_write_config(sbh
, bus
, dev
, func
, off
, buf
, len
);
461 return extpci_write_config(sbh
, bus
, dev
, func
, off
, buf
, len
);
465 sbpci_ban(uint16 core
)
467 if (pci_banned
< ARRAYSIZE(pci_ban
))
468 pci_ban
[pci_banned
++] = core
;
472 * Initiliaze PCI core. Return 0 after a successful initialization.
473 * Otherwise return -1 to indicate there is no PCI core and return 1
474 * to indicate PCI core is disabled.
477 sbpci_init_pci(sb_t
*sbh
)
479 uint chip
, chiprev
, chippkg
, host
;
489 chiprev
= sb_chiprev(sbh
);
490 chippkg
= sb_chippkg(sbh
);
494 if (!(pci
= (sbpciregs_t
*) sb_setcore(sbh
, SB_PCI
, 0))) {
495 printk("PCI: no core\n");
500 if ((chip
== 0x4310) && (chiprev
== 0))
503 sb
= (sbconfig_t
*)((ulong
) pci
+ SBCONFIGOFF
);
505 boardflags
= (uint32
) getintvar(NULL
, "boardflags");
508 * The 200-pin BCM4712 package does not bond out PCI. Even when
509 * PCI is bonded out, some boards may leave the pins
512 if (((chip
== BCM4712_CHIP_ID
) &&
513 ((chippkg
== BCM4712SMALL_PKG_ID
) ||
514 (chippkg
== BCM4712MID_PKG_ID
))) ||
515 (boardflags
& BFL_NOPCI
))
518 /* Enable the core */
519 sb_core_reset(sbh
, 0, 0);
522 * If the PCI core should not be touched (disabled, not bonded
523 * out, or pins floating), do not even attempt to access core
524 * registers. Otherwise, try to determine if it is in host
530 host
= !BUSPROBE(val
, &pci
->control
);
535 /* Disable PCI interrupts in client mode */
536 W_REG(osh
, &sb
->sbintvec
, 0);
538 /* Disable the PCI bridge in client mode */
540 sb_core_disable(sbh
, 0);
542 printk("PCI: Disabled\n");
544 printk("PCI: Initializing host\n");
546 /* Disable PCI SBReqeustTimeout for BCM4785 */
547 if (chip
== BCM4785_CHIP_ID
) {
548 AND_REG(osh
, &sb
->sbimconfiglow
, ~0x00000070);
552 /* Reset the external PCI bus and enable the clock */
553 W_REG(osh
, &pci
->control
, 0x5); /* enable the tristate drivers */
554 W_REG(osh
, &pci
->control
, 0xd); /* enable the PCI clock */
555 OSL_DELAY(150); /* delay > 100 us */
556 W_REG(osh
, &pci
->control
, 0xf); /* deassert PCI reset */
557 /* Use internal arbiter and park REQ/GRNT at external master 0 */
558 W_REG(osh
, &pci
->arbcontrol
, PCI_INT_ARB
);
559 OSL_DELAY(1); /* delay 1 us */
560 if (sb_corerev(sbh
) >= 8) {
561 val
= getintvar(NULL
, "parkid");
562 ASSERT(val
<= PCI_PARKID_LAST
);
563 OR_REG(osh
, &pci
->arbcontrol
, val
<< PCI_PARKID_SHIFT
);
567 /* Enable CardBusMode */
568 cardbus
= getintvar(NULL
, "cardbus") == 1;
570 printk("PCI: Enabling CardBus\n");
571 /* GPIO 1 resets the CardBus device on bcm94710ap */
572 sb_gpioout(sbh
, 1, 1, GPIO_DRV_PRIORITY
);
573 sb_gpioouten(sbh
, 1, 1, GPIO_DRV_PRIORITY
);
574 W_REG(osh
, &pci
->sprom
[0], R_REG(osh
, &pci
->sprom
[0]) | 0x400);
577 /* 64 MB I/O access window */
578 W_REG(osh
, &pci
->sbtopci0
, SBTOPCI_IO
);
579 /* 64 MB configuration access window */
580 W_REG(osh
, &pci
->sbtopci1
, SBTOPCI_CFG0
);
581 /* 1 GB memory access window */
582 W_REG(osh
, &pci
->sbtopci2
, SBTOPCI_MEM
| SB_PCI_DMA
);
584 /* Host bridge slot # nvram overwrite */
585 if ((hbslot
= nvram_get("pcihbslot"))) {
586 pci_hbslot
= bcm_strtoul(hbslot
, NULL
, 0);
587 ASSERT(pci_hbslot
< PCI_MAX_DEVICES
);
590 /* Enable PCI bridge BAR0 prefetch and burst */
592 sbpci_write_config(sbh
, 1, pci_hbslot
, 0, PCI_CFG_CMD
, &val
, sizeof(val
));
594 /* Enable PCI interrupts */
595 W_REG(osh
, &pci
->intmask
, PCI_INTA
);
602 * Get the PCI region address and size information.
605 sbpci_init_regions(sb_t
*sbh
, uint func
, pci_config_regs
*cfg
, sb_bar_cfg_t
*bar
)
614 coreid
= sb_coreid(sbh
);
615 regs
= sb_coreregs(sbh
);
616 sb
= (sbconfig_t
*)((ulong
) regs
+ SBCONFIGOFF
);
620 base
= htol32(sb_base(R_REG(osh
, &sb
->sbadmatch0
)));
622 cfg
->base
[0] = func
== 0 ? base
: base
+ 0x800; /* OHCI/EHCI */
629 bar
->size0
= func
== 0 ? 0x200 : 0x100; /* OHCI/EHCI */
635 cfg
->base
[0] = htol32(sb_base(R_REG(osh
, &sb
->sbadmatch0
)));
636 cfg
->base
[1] = htol32(sb_base(R_REG(osh
, &sb
->sbadmatch1
)));
637 cfg
->base
[2] = htol32(sb_base(R_REG(osh
, &sb
->sbadmatch2
)));
638 cfg
->base
[3] = htol32(sb_base(R_REG(osh
, &sb
->sbadmatch3
)));
641 bar
->n
= (R_REG(osh
, &sb
->sbidlow
) & SBIDL_AR_MASK
) >> SBIDL_AR_SHIFT
;
642 bar
->size0
= sb_size(R_REG(osh
, &sb
->sbadmatch0
));
643 bar
->size1
= sb_size(R_REG(osh
, &sb
->sbadmatch1
));
644 bar
->size2
= sb_size(R_REG(osh
, &sb
->sbadmatch2
));
645 bar
->size3
= sb_size(R_REG(osh
, &sb
->sbadmatch3
));
651 * Construct PCI config spaces for SB cores so that they
652 * can be accessed as if they were PCI devices.
655 sbpci_init_cores(sb_t
*sbh
)
657 uint chiprev
, coreidx
, i
;
659 pci_config_regs
*cfg
, *pci
;
663 uint16 vendor
, device
;
665 uint8
class, subclass
, progif
;
670 chiprev
= sb_chiprev(sbh
);
671 coreidx
= sb_coreidx(sbh
);
675 /* Scan the SB bus */
676 bzero(sb_config_regs
, sizeof(sb_config_regs
));
677 bzero(sb_bar_cfg
, sizeof(sb_bar_cfg
));
678 bzero(sb_pci_cfg
, sizeof(sb_pci_cfg
));
679 memset(&sb_pci_null
, -1, sizeof(sb_pci_null
));
680 cfg
= sb_config_regs
;
682 for (dev
= 0; dev
< SB_MAXCORES
; dev
++) {
683 /* Check if the core exists */
684 if (!(regs
= sb_setcoreidx(sbh
, dev
)))
686 sb
= (sbconfig_t
*)((ulong
) regs
+ SBCONFIGOFF
);
688 /* Check if this core is banned */
689 coreid
= sb_coreid(sbh
);
690 for (i
= 0; i
< pci_banned
; i
++)
691 if (coreid
== pci_ban
[i
])
696 for (func
= 0; func
< MAXFUNCS
; ++func
) {
697 /* Make sure we won't go beyond the limit */
698 if (cfg
>= &sb_config_regs
[SB_MAXCORES
]) {
699 printk("PCI: too many emulated devices\n");
703 /* Convert core id to pci id */
704 if (sb_corepciid(sbh
, func
, &vendor
, &device
, &class, &subclass
,
709 * Differentiate real PCI config from emulated.
710 * non zero 'pci' indicate there is a real PCI config space
714 case BCM47XX_GIGETH_ID
:
715 pci
= (pci_config_regs
*)((uint32
)regs
+ 0x800);
717 case BCM47XX_SATAXOR_ID
:
718 pci
= (pci_config_regs
*)((uint32
)regs
+ 0x400);
720 case BCM47XX_ATA100_ID
:
721 pci
= (pci_config_regs
*)((uint32
)regs
+ 0x800);
727 /* Supported translations */
728 cfg
->vendor
= htol16(vendor
);
729 cfg
->device
= htol16(device
);
730 cfg
->rev_id
= chiprev
;
731 cfg
->prog_if
= progif
;
732 cfg
->sub_class
= subclass
;
733 cfg
->base_class
= class;
734 cfg
->header_type
= header
;
735 sbpci_init_regions(sbh
, func
, cfg
, bar
);
736 /* Save core interrupt flag */
737 cfg
->int_pin
= R_REG(osh
, &sb
->sbtpsflag
) & SBTPS_NUM0_MASK
;
738 /* Save core interrupt assignment */
739 cfg
->int_line
= sb_irq(sbh
);
740 /* Indicate there is no SROM */
741 *((uint32
*) &cfg
->sprom_control
) = 0xffffffff;
743 /* Point to the PCI config spaces */
744 sb_pci_cfg
[dev
][func
].emu
= cfg
;
745 sb_pci_cfg
[dev
][func
].pci
= pci
;
746 sb_pci_cfg
[dev
][func
].bar
= bar
;
753 sb_setcoreidx(sbh
, coreidx
);
757 * Initialize PCI core and construct PCI config spaces for SB cores.
758 * Must propagate sbpci_init_pci() return value to the caller to let
759 * them know the PCI core initialization status.
762 sbpci_init(sb_t
*sbh
)
764 int status
= sbpci_init_pci(sbh
);
765 sbpci_init_cores(sbh
);