2 * Sonics Silicon Backplane
3 * Broadcom PCI-core driver
5 * Copyright 2005, Broadcom Corporation
6 * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
8 * Licensed under the GNU/GPL. See COPYING for details.
11 #include <linux/ssb/ssb.h>
12 #include <linux/pci.h>
13 #include <linux/export.h>
14 #include <linux/delay.h>
15 #include <linux/ssb/ssb_embedded.h>
17 #include "ssb_private.h"
19 static u32
ssb_pcie_read(struct ssb_pcicore
*pc
, u32 address
);
20 static void ssb_pcie_write(struct ssb_pcicore
*pc
, u32 address
, u32 data
);
21 static u16
ssb_pcie_mdio_read(struct ssb_pcicore
*pc
, u8 device
, u8 address
);
22 static void ssb_pcie_mdio_write(struct ssb_pcicore
*pc
, u8 device
,
23 u8 address
, u16 data
);
26 u32
pcicore_read32(struct ssb_pcicore
*pc
, u16 offset
)
28 return ssb_read32(pc
->dev
, offset
);
32 void pcicore_write32(struct ssb_pcicore
*pc
, u16 offset
, u32 value
)
34 ssb_write32(pc
->dev
, offset
, value
);
38 u16
pcicore_read16(struct ssb_pcicore
*pc
, u16 offset
)
40 return ssb_read16(pc
->dev
, offset
);
44 void pcicore_write16(struct ssb_pcicore
*pc
, u16 offset
, u16 value
)
46 ssb_write16(pc
->dev
, offset
, value
);
49 /**************************************************
50 * Code for hostmode operation.
51 **************************************************/
53 #ifdef CONFIG_SSB_PCICORE_HOSTMODE
55 #include <asm/paccess.h>
56 /* Probe a 32bit value on the bus and catch bus exceptions.
57 * Returns nonzero on a bus exception.
58 * This is MIPS specific */
59 #define mips_busprobe32(val, addr) get_dbe((val), ((u32 *)(addr)))
61 /* Assume one-hot slot wiring */
62 #define SSB_PCI_SLOT_MAX 16
64 /* Global lock is OK, as we won't have more than one extpci anyway. */
65 static DEFINE_SPINLOCK(cfgspace_lock
);
66 /* Core to access the external PCI config space. Can only have one. */
67 static struct ssb_pcicore
*extpci_core
;
70 static u32
get_cfgspace_addr(struct ssb_pcicore
*pc
,
71 unsigned int bus
, unsigned int dev
,
72 unsigned int func
, unsigned int off
)
77 /* We do only have one cardbus device behind the bridge. */
78 if (pc
->cardbusmode
&& (dev
>= 1))
82 /* Type 0 transaction */
83 if (unlikely(dev
>= SSB_PCI_SLOT_MAX
))
85 /* Slide the window */
86 tmp
= SSB_PCICORE_SBTOPCI_CFG0
;
87 tmp
|= ((1 << (dev
+ 16)) & SSB_PCICORE_SBTOPCI1_MASK
);
88 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI1
, tmp
);
89 /* Calculate the address */
91 addr
|= ((1 << (dev
+ 16)) & ~SSB_PCICORE_SBTOPCI1_MASK
);
95 /* Type 1 transaction */
96 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI1
,
97 SSB_PCICORE_SBTOPCI_CFG1
);
98 /* Calculate the address */
109 static int ssb_extpci_read_config(struct ssb_pcicore
*pc
,
110 unsigned int bus
, unsigned int dev
,
111 unsigned int func
, unsigned int off
,
118 SSB_WARN_ON(!pc
->hostmode
);
119 if (unlikely(len
!= 1 && len
!= 2 && len
!= 4))
121 addr
= get_cfgspace_addr(pc
, bus
, dev
, func
, off
);
125 mmio
= ioremap_nocache(addr
, len
);
129 if (mips_busprobe32(val
, mmio
)) {
135 val
>>= (8 * (off
& 3));
139 *((u8
*)buf
) = (u8
)val
;
142 *((u16
*)buf
) = (u16
)val
;
145 *((u32
*)buf
) = (u32
)val
;
155 static int ssb_extpci_write_config(struct ssb_pcicore
*pc
,
156 unsigned int bus
, unsigned int dev
,
157 unsigned int func
, unsigned int off
,
158 const void *buf
, int len
)
164 SSB_WARN_ON(!pc
->hostmode
);
165 if (unlikely(len
!= 1 && len
!= 2 && len
!= 4))
167 addr
= get_cfgspace_addr(pc
, bus
, dev
, func
, off
);
171 mmio
= ioremap_nocache(addr
, len
);
175 if (mips_busprobe32(val
, mmio
)) {
183 val
&= ~(0xFF << (8 * (off
& 3)));
184 val
|= *((const u8
*)buf
) << (8 * (off
& 3));
188 val
&= ~(0xFFFF << (8 * (off
& 3)));
189 val
|= *((const u16
*)buf
) << (8 * (off
& 3));
192 val
= *((const u32
*)buf
);
204 static int ssb_pcicore_read_config(struct pci_bus
*bus
, unsigned int devfn
,
205 int reg
, int size
, u32
*val
)
210 spin_lock_irqsave(&cfgspace_lock
, flags
);
211 err
= ssb_extpci_read_config(extpci_core
, bus
->number
, PCI_SLOT(devfn
),
212 PCI_FUNC(devfn
), reg
, val
, size
);
213 spin_unlock_irqrestore(&cfgspace_lock
, flags
);
215 return err
? PCIBIOS_DEVICE_NOT_FOUND
: PCIBIOS_SUCCESSFUL
;
218 static int ssb_pcicore_write_config(struct pci_bus
*bus
, unsigned int devfn
,
219 int reg
, int size
, u32 val
)
224 spin_lock_irqsave(&cfgspace_lock
, flags
);
225 err
= ssb_extpci_write_config(extpci_core
, bus
->number
, PCI_SLOT(devfn
),
226 PCI_FUNC(devfn
), reg
, &val
, size
);
227 spin_unlock_irqrestore(&cfgspace_lock
, flags
);
229 return err
? PCIBIOS_DEVICE_NOT_FOUND
: PCIBIOS_SUCCESSFUL
;
232 static struct pci_ops ssb_pcicore_pciops
= {
233 .read
= ssb_pcicore_read_config
,
234 .write
= ssb_pcicore_write_config
,
237 static struct resource ssb_pcicore_mem_resource
= {
238 .name
= "SSB PCIcore external memory",
239 .start
= SSB_PCI_DMA
,
240 .end
= SSB_PCI_DMA
+ SSB_PCI_DMA_SZ
- 1,
241 .flags
= IORESOURCE_MEM
| IORESOURCE_PCI_FIXED
,
244 static struct resource ssb_pcicore_io_resource
= {
245 .name
= "SSB PCIcore external I/O",
248 .flags
= IORESOURCE_IO
| IORESOURCE_PCI_FIXED
,
251 static struct pci_controller ssb_pcicore_controller
= {
252 .pci_ops
= &ssb_pcicore_pciops
,
253 .io_resource
= &ssb_pcicore_io_resource
,
254 .mem_resource
= &ssb_pcicore_mem_resource
,
257 /* This function is called when doing a pci_enable_device().
258 * We must first check if the device is a device on the PCI-core bridge. */
259 int ssb_pcicore_plat_dev_init(struct pci_dev
*d
)
261 if (d
->bus
->ops
!= &ssb_pcicore_pciops
) {
262 /* This is not a device on the PCI-core bridge. */
266 ssb_printk(KERN_INFO
"PCI: Fixing up device %s\n",
269 /* Fix up interrupt lines */
270 d
->irq
= ssb_mips_irq(extpci_core
->dev
) + 2;
271 pci_write_config_byte(d
, PCI_INTERRUPT_LINE
, d
->irq
);
276 /* Early PCI fixup for a device on the PCI-core bridge. */
277 static void ssb_pcicore_fixup_pcibridge(struct pci_dev
*dev
)
281 if (dev
->bus
->ops
!= &ssb_pcicore_pciops
) {
282 /* This is not a device on the PCI-core bridge. */
285 if (dev
->bus
->number
!= 0 || PCI_SLOT(dev
->devfn
) != 0)
288 ssb_printk(KERN_INFO
"PCI: Fixing up bridge %s\n", pci_name(dev
));
290 /* Enable PCI bridge bus mastering and memory space */
292 if (pcibios_enable_device(dev
, ~0) < 0) {
293 ssb_printk(KERN_ERR
"PCI: SSB bridge enable failed\n");
297 /* Enable PCI bridge BAR1 prefetch and burst */
298 pci_write_config_dword(dev
, SSB_BAR1_CONTROL
, 3);
300 /* Make sure our latency is high enough to handle the devices behind us */
302 ssb_printk(KERN_INFO
"PCI: Fixing latency timer of device %s to %u\n",
304 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, lat
);
306 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID
, PCI_ANY_ID
, ssb_pcicore_fixup_pcibridge
);
308 /* PCI device IRQ mapping. */
309 int ssb_pcicore_pcibios_map_irq(const struct pci_dev
*dev
, u8 slot
, u8 pin
)
311 if (dev
->bus
->ops
!= &ssb_pcicore_pciops
) {
312 /* This is not a device on the PCI-core bridge. */
315 return ssb_mips_irq(extpci_core
->dev
) + 2;
318 static void __devinit
ssb_pcicore_init_hostmode(struct ssb_pcicore
*pc
)
322 if (WARN_ON(extpci_core
))
326 ssb_dprintk(KERN_INFO PFX
"PCIcore in host mode found\n");
327 /* Reset devices on the external PCI bus */
328 val
= SSB_PCICORE_CTL_RST_OE
;
329 val
|= SSB_PCICORE_CTL_CLK_OE
;
330 pcicore_write32(pc
, SSB_PCICORE_CTL
, val
);
331 val
|= SSB_PCICORE_CTL_CLK
; /* Clock on */
332 pcicore_write32(pc
, SSB_PCICORE_CTL
, val
);
333 udelay(150); /* Assertion time demanded by the PCI standard */
334 val
|= SSB_PCICORE_CTL_RST
; /* Deassert RST# */
335 pcicore_write32(pc
, SSB_PCICORE_CTL
, val
);
336 val
= SSB_PCICORE_ARBCTL_INTERN
;
337 pcicore_write32(pc
, SSB_PCICORE_ARBCTL
, val
);
338 udelay(1); /* Assertion time demanded by the PCI standard */
340 if (pc
->dev
->bus
->has_cardbus_slot
) {
341 ssb_dprintk(KERN_INFO PFX
"CardBus slot detected\n");
343 /* GPIO 1 resets the bridge */
344 ssb_gpio_out(pc
->dev
->bus
, 1, 1);
345 ssb_gpio_outen(pc
->dev
->bus
, 1, 1);
346 pcicore_write16(pc
, SSB_PCICORE_SPROM(0),
347 pcicore_read16(pc
, SSB_PCICORE_SPROM(0))
351 /* 64MB I/O window */
352 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI0
,
353 SSB_PCICORE_SBTOPCI_IO
);
354 /* 64MB config space */
355 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI1
,
356 SSB_PCICORE_SBTOPCI_CFG0
);
357 /* 1GB memory window */
358 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI2
,
359 SSB_PCICORE_SBTOPCI_MEM
| SSB_PCI_DMA
);
361 /* Enable PCI bridge BAR0 prefetch and burst */
362 val
= PCI_COMMAND_MASTER
| PCI_COMMAND_MEMORY
;
363 ssb_extpci_write_config(pc
, 0, 0, 0, PCI_COMMAND
, &val
, 2);
364 /* Clear error conditions */
366 ssb_extpci_write_config(pc
, 0, 0, 0, PCI_STATUS
, &val
, 2);
368 /* Enable PCI interrupts */
369 pcicore_write32(pc
, SSB_PCICORE_IMASK
,
370 SSB_PCICORE_IMASK_INTA
);
372 /* Ok, ready to run, register it to the system.
373 * The following needs change, if we want to port hostmode
374 * to non-MIPS platform. */
375 ssb_pcicore_controller
.io_map_base
= (unsigned long)ioremap_nocache(SSB_PCI_MEM
, 0x04000000);
376 set_io_port_base(ssb_pcicore_controller
.io_map_base
);
377 /* Give some time to the PCI controller to configure itself with the new
378 * values. Not waiting at this point causes crashes of the machine. */
380 register_pci_controller(&ssb_pcicore_controller
);
383 static int __devinit
pcicore_is_in_hostmode(struct ssb_pcicore
*pc
)
385 struct ssb_bus
*bus
= pc
->dev
->bus
;
389 chipid_top
= (bus
->chip_id
& 0xFF00);
390 if (chipid_top
!= 0x4700 &&
391 chipid_top
!= 0x5300)
394 if (bus
->sprom
.boardflags_lo
& SSB_PCICORE_BFL_NOPCI
)
397 /* The 200-pin BCM4712 package does not bond out PCI. Even when
398 * PCI is bonded out, some boards may leave the pins floating. */
399 if (bus
->chip_id
== 0x4712) {
400 if (bus
->chip_package
== SSB_CHIPPACK_BCM4712S
)
402 if (bus
->chip_package
== SSB_CHIPPACK_BCM4712M
)
405 if (bus
->chip_id
== 0x5350)
408 return !mips_busprobe32(tmp
, (bus
->mmio
+ (pc
->dev
->core_index
* SSB_CORE_SIZE
)));
410 #endif /* CONFIG_SSB_PCICORE_HOSTMODE */
412 /**************************************************
414 **************************************************/
416 static void __devinit
ssb_pcicore_fix_sprom_core_index(struct ssb_pcicore
*pc
)
418 u16 tmp
= pcicore_read16(pc
, SSB_PCICORE_SPROM(0));
419 if (((tmp
& 0xF000) >> 12) != pc
->dev
->core_index
) {
421 tmp
|= (pc
->dev
->core_index
<< 12);
422 pcicore_write16(pc
, SSB_PCICORE_SPROM(0), tmp
);
426 static u8
ssb_pcicore_polarity_workaround(struct ssb_pcicore
*pc
)
428 return (ssb_pcie_read(pc
, 0x204) & 0x10) ? 0xC0 : 0x80;
431 static void ssb_pcicore_serdes_workaround(struct ssb_pcicore
*pc
)
433 const u8 serdes_pll_device
= 0x1D;
434 const u8 serdes_rx_device
= 0x1F;
437 ssb_pcie_mdio_write(pc
, serdes_rx_device
, 1 /* Control */,
438 ssb_pcicore_polarity_workaround(pc
));
439 tmp
= ssb_pcie_mdio_read(pc
, serdes_pll_device
, 1 /* Control */);
441 ssb_pcie_mdio_write(pc
, serdes_pll_device
, 1, tmp
& ~0x4000);
444 static void ssb_pcicore_pci_setup_workarounds(struct ssb_pcicore
*pc
)
446 struct ssb_device
*pdev
= pc
->dev
;
447 struct ssb_bus
*bus
= pdev
->bus
;
450 tmp
= pcicore_read32(pc
, SSB_PCICORE_SBTOPCI2
);
451 tmp
|= SSB_PCICORE_SBTOPCI_PREF
;
452 tmp
|= SSB_PCICORE_SBTOPCI_BURST
;
453 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI2
, tmp
);
455 if (pdev
->id
.revision
< 5) {
456 tmp
= ssb_read32(pdev
, SSB_IMCFGLO
);
457 tmp
&= ~SSB_IMCFGLO_SERTO
;
459 tmp
&= ~SSB_IMCFGLO_REQTO
;
460 tmp
|= 3 << SSB_IMCFGLO_REQTO_SHIFT
;
461 ssb_write32(pdev
, SSB_IMCFGLO
, tmp
);
462 ssb_commit_settings(bus
);
463 } else if (pdev
->id
.revision
>= 11) {
464 tmp
= pcicore_read32(pc
, SSB_PCICORE_SBTOPCI2
);
465 tmp
|= SSB_PCICORE_SBTOPCI_MRM
;
466 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI2
, tmp
);
470 static void ssb_pcicore_pcie_setup_workarounds(struct ssb_pcicore
*pc
)
473 u8 rev
= pc
->dev
->id
.revision
;
475 if (rev
== 0 || rev
== 1) {
476 /* TLP Workaround register. */
477 tmp
= ssb_pcie_read(pc
, 0x4);
479 ssb_pcie_write(pc
, 0x4, tmp
);
482 /* DLLP Link Control register. */
483 tmp
= ssb_pcie_read(pc
, 0x100);
485 ssb_pcie_write(pc
, 0x100, tmp
);
489 const u8 serdes_rx_device
= 0x1F;
491 ssb_pcie_mdio_write(pc
, serdes_rx_device
,
492 2 /* Timer */, 0x8128);
493 ssb_pcie_mdio_write(pc
, serdes_rx_device
,
494 6 /* CDR */, 0x0100);
495 ssb_pcie_mdio_write(pc
, serdes_rx_device
,
496 7 /* CDR BW */, 0x1466);
497 } else if (rev
== 3 || rev
== 4 || rev
== 5) {
498 /* TODO: DLLP Power Management Threshold */
499 ssb_pcicore_serdes_workaround(pc
);
501 } else if (rev
== 7) {
502 /* TODO: No PLL down */
506 /* Miscellaneous Configuration Fixup */
507 tmp
= pcicore_read16(pc
, SSB_PCICORE_SPROM(5));
509 pcicore_write16(pc
, SSB_PCICORE_SPROM(5),
514 /**************************************************
515 * Generic and Clientmode operation code.
516 **************************************************/
518 static void __devinit
ssb_pcicore_init_clientmode(struct ssb_pcicore
*pc
)
520 ssb_pcicore_fix_sprom_core_index(pc
);
522 /* Disable PCI interrupts. */
523 ssb_write32(pc
->dev
, SSB_INTVEC
, 0);
525 /* Additional PCIe always once-executed workarounds */
526 if (pc
->dev
->id
.coreid
== SSB_DEV_PCIE
) {
527 ssb_pcicore_serdes_workaround(pc
);
529 /* TODO: Clock Request Update */
533 void __devinit
ssb_pcicore_init(struct ssb_pcicore
*pc
)
535 struct ssb_device
*dev
= pc
->dev
;
539 if (!ssb_device_is_enabled(dev
))
540 ssb_device_enable(dev
, 0);
542 #ifdef CONFIG_SSB_PCICORE_HOSTMODE
543 pc
->hostmode
= pcicore_is_in_hostmode(pc
);
545 ssb_pcicore_init_hostmode(pc
);
546 #endif /* CONFIG_SSB_PCICORE_HOSTMODE */
548 ssb_pcicore_init_clientmode(pc
);
551 static u32
ssb_pcie_read(struct ssb_pcicore
*pc
, u32 address
)
553 pcicore_write32(pc
, 0x130, address
);
554 return pcicore_read32(pc
, 0x134);
557 static void ssb_pcie_write(struct ssb_pcicore
*pc
, u32 address
, u32 data
)
559 pcicore_write32(pc
, 0x130, address
);
560 pcicore_write32(pc
, 0x134, data
);
563 static void ssb_pcie_mdio_set_phy(struct ssb_pcicore
*pc
, u8 phy
)
565 const u16 mdio_control
= 0x128;
566 const u16 mdio_data
= 0x12C;
570 v
= (1 << 30); /* Start of Transaction */
571 v
|= (1 << 28); /* Write Transaction */
572 v
|= (1 << 17); /* Turnaround */
575 pcicore_write32(pc
, mdio_data
, v
);
578 for (i
= 0; i
< 200; i
++) {
579 v
= pcicore_read32(pc
, mdio_control
);
580 if (v
& 0x100 /* Trans complete */)
586 static u16
ssb_pcie_mdio_read(struct ssb_pcicore
*pc
, u8 device
, u8 address
)
588 const u16 mdio_control
= 0x128;
589 const u16 mdio_data
= 0x12C;
590 int max_retries
= 10;
595 v
= 0x80; /* Enable Preamble Sequence */
596 v
|= 0x2; /* MDIO Clock Divisor */
597 pcicore_write32(pc
, mdio_control
, v
);
599 if (pc
->dev
->id
.revision
>= 10) {
601 ssb_pcie_mdio_set_phy(pc
, device
);
604 v
= (1 << 30); /* Start of Transaction */
605 v
|= (1 << 29); /* Read Transaction */
606 v
|= (1 << 17); /* Turnaround */
607 if (pc
->dev
->id
.revision
< 10)
608 v
|= (u32
)device
<< 22;
609 v
|= (u32
)address
<< 18;
610 pcicore_write32(pc
, mdio_data
, v
);
611 /* Wait for the device to complete the transaction */
613 for (i
= 0; i
< max_retries
; i
++) {
614 v
= pcicore_read32(pc
, mdio_control
);
615 if (v
& 0x100 /* Trans complete */) {
617 ret
= pcicore_read32(pc
, mdio_data
);
622 pcicore_write32(pc
, mdio_control
, 0);
626 static void ssb_pcie_mdio_write(struct ssb_pcicore
*pc
, u8 device
,
627 u8 address
, u16 data
)
629 const u16 mdio_control
= 0x128;
630 const u16 mdio_data
= 0x12C;
631 int max_retries
= 10;
635 v
= 0x80; /* Enable Preamble Sequence */
636 v
|= 0x2; /* MDIO Clock Divisor */
637 pcicore_write32(pc
, mdio_control
, v
);
639 if (pc
->dev
->id
.revision
>= 10) {
641 ssb_pcie_mdio_set_phy(pc
, device
);
644 v
= (1 << 30); /* Start of Transaction */
645 v
|= (1 << 28); /* Write Transaction */
646 v
|= (1 << 17); /* Turnaround */
647 if (pc
->dev
->id
.revision
< 10)
648 v
|= (u32
)device
<< 22;
649 v
|= (u32
)address
<< 18;
651 pcicore_write32(pc
, mdio_data
, v
);
652 /* Wait for the device to complete the transaction */
654 for (i
= 0; i
< max_retries
; i
++) {
655 v
= pcicore_read32(pc
, mdio_control
);
656 if (v
& 0x100 /* Trans complete */)
660 pcicore_write32(pc
, mdio_control
, 0);
663 int ssb_pcicore_dev_irqvecs_enable(struct ssb_pcicore
*pc
,
664 struct ssb_device
*dev
)
666 struct ssb_device
*pdev
= pc
->dev
;
671 if (dev
->bus
->bustype
!= SSB_BUSTYPE_PCI
) {
672 /* This SSB device is not on a PCI host-bus. So the IRQs are
673 * not routed through the PCI core.
674 * So we must not enable routing through the PCI core. */
682 might_sleep_if(pdev
->id
.coreid
!= SSB_DEV_PCI
);
684 /* Enable interrupts for this device. */
685 if ((pdev
->id
.revision
>= 6) || (pdev
->id
.coreid
== SSB_DEV_PCIE
)) {
688 /* Calculate the "coremask" for the device. */
689 coremask
= (1 << dev
->core_index
);
691 SSB_WARN_ON(bus
->bustype
!= SSB_BUSTYPE_PCI
);
692 err
= pci_read_config_dword(bus
->host_pci
, SSB_PCI_IRQMASK
, &tmp
);
695 tmp
|= coremask
<< 8;
696 err
= pci_write_config_dword(bus
->host_pci
, SSB_PCI_IRQMASK
, tmp
);
702 intvec
= ssb_read32(pdev
, SSB_INTVEC
);
703 tmp
= ssb_read32(dev
, SSB_TPSFLAG
);
704 tmp
&= SSB_TPSFLAG_BPFLAG
;
705 intvec
|= (1 << tmp
);
706 ssb_write32(pdev
, SSB_INTVEC
, intvec
);
709 /* Setup PCIcore operation. */
712 if (pdev
->id
.coreid
== SSB_DEV_PCI
) {
713 ssb_pcicore_pci_setup_workarounds(pc
);
715 WARN_ON(pdev
->id
.coreid
!= SSB_DEV_PCIE
);
716 ssb_pcicore_pcie_setup_workarounds(pc
);
722 EXPORT_SYMBOL(ssb_pcicore_dev_irqvecs_enable
);