2 * Sonics Silicon Backplane
3 * Broadcom PCI-core driver
5 * Copyright 2005, Broadcom Corporation
6 * Copyright 2006, 2007, Michael Buesch <mb@bu3sch.de>
8 * Licensed under the GNU/GPL. See COPYING for details.
11 #include <linux/ssb/ssb.h>
12 #include <linux/pci.h>
13 #include <linux/delay.h>
15 #include "../ssb_private.h"
18 u32
pcicore_read32(struct ssb_pcicore
*pc
, u16 offset
)
20 return ssb_read32(pc
->dev
, offset
);
24 void pcicore_write32(struct ssb_pcicore
*pc
, u16 offset
, u32 value
)
26 ssb_write32(pc
->dev
, offset
, value
);
29 /**************************************************
30 * Code for hostmode operation.
31 **************************************************/
33 #ifdef CONFIG_SSB_PCICORE_HOSTMODE
35 #include <asm/paccess.h>
36 /* Read the bus and catch bus exceptions. This is MIPS specific. */
37 #define mips_busprobe(val, addr) get_dbe((val), (addr))
39 /* Assume one-hot slot wiring */
40 #define SSB_PCI_SLOT_MAX 16
42 /* Global lock is OK, as we won't have more than one extpci anyway. */
43 static DEFINE_SPINLOCK(cfgspace_lock
);
44 /* Core to access the external PCI config space. Can only have one. */
45 static struct ssb_pcicore
*extpci_core
;
47 u32 pci_iobase
= 0x100;
48 u32 pci_membase
= SSB_PCI_DMA
;
50 int pcibios_plat_dev_init(struct pci_dev
*d
)
56 printk("PCI: Fixing up device %s\n", pci_name(d
));
58 /* Fix up resource bases */
59 for (pos
= 0; pos
< 6; pos
++) {
60 res
= &d
->resource
[pos
];
61 base
= ((res
->flags
& IORESOURCE_IO
) ? &pci_iobase
: &pci_membase
);
63 size
= res
->end
- res
->start
+ 1;
64 if (*base
& (size
- 1))
65 *base
= (*base
+ size
) & ~(size
- 1);
67 res
->end
= res
->start
+ size
- 1;
69 pci_write_config_dword(d
, PCI_BASE_ADDRESS_0
+ (pos
<< 2), res
->start
);
71 /* Fix up PCI bridge BAR0 only */
72 if (d
->bus
->number
== 0 && PCI_SLOT(d
->devfn
) == 0)
75 /* Fix up interrupt lines */
76 d
->irq
= ssb_mips_irq(extpci_core
->dev
) + 2;
77 pci_write_config_byte(d
, PCI_INTERRUPT_LINE
, d
->irq
);
82 static void __init
ssb_fixup_pcibridge(struct pci_dev
*dev
)
84 if (dev
->bus
->number
!= 0 || PCI_SLOT(dev
->devfn
) != 0)
87 printk("PCI: fixing up bridge\n");
89 /* Enable PCI bridge bus mastering and memory space */
91 pcibios_enable_device(dev
, ~0);
93 /* Enable PCI bridge BAR1 prefetch and burst */
94 pci_write_config_dword(dev
, SSB_BAR1_CONTROL
, 3);
96 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID
, PCI_ANY_ID
, ssb_fixup_pcibridge
);
98 int __init
pcibios_map_irq(struct pci_dev
*dev
, u8 slot
, u8 pin
)
100 return ssb_mips_irq(extpci_core
->dev
) + 2;
103 static u32
get_cfgspace_addr(struct ssb_pcicore
*pc
,
104 unsigned int bus
, unsigned int dev
,
105 unsigned int func
, unsigned int off
)
110 if (unlikely(pc
->cardbusmode
&& dev
> 1))
112 if (bus
== 0) {//FIXME busnumber ok?
113 /* Type 0 transaction */
114 if (unlikely(dev
>= SSB_PCI_SLOT_MAX
))
116 /* Slide the window */
117 tmp
= SSB_PCICORE_SBTOPCI_CFG0
;
118 tmp
|= ((1 << (dev
+ 16)) & SSB_PCICORE_SBTOPCI1_MASK
);
119 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI1
, tmp
);
120 /* Calculate the address */
122 addr
|= ((1 << (dev
+ 16)) & ~SSB_PCICORE_SBTOPCI1_MASK
);
126 /* Type 1 transaction */
127 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI1
,
128 SSB_PCICORE_SBTOPCI_CFG1
);
129 /* Calculate the address */
140 static int ssb_extpci_read_config(struct ssb_pcicore
*pc
,
141 unsigned int bus
, unsigned int dev
,
142 unsigned int func
, unsigned int off
,
149 assert(pc
->hostmode
);
150 if (unlikely(len
!= 1 && len
!= 2 && len
!= 4))
152 addr
= get_cfgspace_addr(pc
, bus
, dev
, func
, off
);
156 mmio
= ioremap_nocache(addr
, len
);
160 if (mips_busprobe(val
, (u32
*) mmio
)) {
166 val
>>= (8 * (off
& 3));
170 *((u8
*)buf
) = (u8
)val
;
173 *((u16
*)buf
) = (u16
)val
;
176 *((u32
*)buf
) = (u32
)val
;
186 static int ssb_extpci_write_config(struct ssb_pcicore
*pc
,
187 unsigned int bus
, unsigned int dev
,
188 unsigned int func
, unsigned int off
,
189 const void *buf
, int len
)
195 assert(pc
->hostmode
);
196 if (unlikely(len
!= 1 && len
!= 2 && len
!= 4))
198 addr
= get_cfgspace_addr(pc
, bus
, dev
, func
, off
);
202 mmio
= ioremap_nocache(addr
, len
);
206 if (mips_busprobe(val
, (u32
*) mmio
)) {
214 val
&= ~(0xFF << (8 * (off
& 3)));
215 val
|= *((const u8
*)buf
) << (8 * (off
& 3));
219 val
&= ~(0xFFFF << (8 * (off
& 3)));
220 val
|= *((const u16
*)buf
) << (8 * (off
& 3));
223 val
= *((const u32
*)buf
);
226 writel(*((const u32
*)buf
), mmio
);
235 static int ssb_pcicore_read_config(struct pci_bus
*bus
, unsigned int devfn
,
236 int reg
, int size
, u32
*val
)
241 spin_lock_irqsave(&cfgspace_lock
, flags
);
242 err
= ssb_extpci_read_config(extpci_core
, bus
->number
, PCI_SLOT(devfn
),
243 PCI_FUNC(devfn
), reg
, val
, size
);
244 spin_unlock_irqrestore(&cfgspace_lock
, flags
);
246 return err
? PCIBIOS_DEVICE_NOT_FOUND
: PCIBIOS_SUCCESSFUL
;
249 static int ssb_pcicore_write_config(struct pci_bus
*bus
, unsigned int devfn
,
250 int reg
, int size
, u32 val
)
255 spin_lock_irqsave(&cfgspace_lock
, flags
);
256 err
= ssb_extpci_write_config(extpci_core
, bus
->number
, PCI_SLOT(devfn
),
257 PCI_FUNC(devfn
), reg
, &val
, size
);
258 spin_unlock_irqrestore(&cfgspace_lock
, flags
);
260 return err
? PCIBIOS_DEVICE_NOT_FOUND
: PCIBIOS_SUCCESSFUL
;
263 static struct pci_ops ssb_pcicore_pciops
= {
264 .read
= ssb_pcicore_read_config
,
265 .write
= ssb_pcicore_write_config
,
268 static struct resource ssb_pcicore_mem_resource
= {
269 .name
= "SSB PCIcore external memory",
270 .start
= SSB_PCI_DMA
,
271 .end
= (u32
)SSB_PCI_DMA
+ (u32
)SSB_PCI_DMA_SZ
- 1,
272 .flags
= IORESOURCE_MEM
,
275 static struct resource ssb_pcicore_io_resource
= {
276 .name
= "SSB PCIcore external I/O",
279 .flags
= IORESOURCE_IO
,
282 static struct pci_controller ssb_pcicore_controller
= {
283 .pci_ops
= &ssb_pcicore_pciops
,
284 .io_resource
= &ssb_pcicore_io_resource
,
285 .mem_resource
= &ssb_pcicore_mem_resource
,
286 .mem_offset
= 0x24000000,
289 static void ssb_pcicore_init_hostmode(struct ssb_pcicore
*pc
)
293 assert(!extpci_core
);
296 ssb_dprintk(KERN_INFO PFX
"PCIcore in host mode found\n");
297 /* Reset devices on the external PCI bus */
298 val
= SSB_PCICORE_CTL_RST_OE
;
299 val
|= SSB_PCICORE_CTL_CLK_OE
;
300 pcicore_write32(pc
, SSB_PCICORE_CTL
, val
);
301 val
|= SSB_PCICORE_CTL_CLK
; /* Clock on */
302 pcicore_write32(pc
, SSB_PCICORE_CTL
, val
);
304 val
|= SSB_PCICORE_CTL_RST
; /* Deassert RST# */
305 pcicore_write32(pc
, SSB_PCICORE_CTL
, val
);
310 /* 64MB I/O window */
311 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI0
,
312 SSB_PCICORE_SBTOPCI_IO
);
313 /* 64MB config space */
314 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI1
,
315 SSB_PCICORE_SBTOPCI_CFG0
);
316 /* 1GB memory window */
317 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI2
,
318 SSB_PCICORE_SBTOPCI_MEM
| SSB_PCI_DMA
);
320 /* Enable PCI bridge BAR0 prefetch and burst */
321 val
= PCI_COMMAND_MASTER
| PCI_COMMAND_MEMORY
;
322 ssb_extpci_write_config(pc
, 0, 0, 0, PCI_COMMAND
, &val
, 4);
324 /* Enable PCI interrupts */
325 pcicore_write32(pc
, SSB_PCICORE_IMASK
,
326 SSB_PCICORE_IMASK_INTA
);
328 /* Ok, ready to run, register it to the system.
329 * The following needs change, if we want to port hostmode
330 * to non-MIPS platform. */
331 set_io_port_base((unsigned long)ioremap_nocache(SSB_PCI_MEM
, 0x04000000));
332 register_pci_controller(&ssb_pcicore_controller
);
335 static int pcicore_is_in_hostmode(struct ssb_pcicore
*pc
)
337 struct ssb_bus
*bus
= pc
->dev
->bus
;
341 chipid_top
= (bus
->chip_id
& 0xFF00);
342 if (chipid_top
!= 0x4700 &&
343 chipid_top
!= 0x5300)
346 if (bus
->sprom
.r1
.boardflags_lo
& SSB_PCICORE_BFL_NOPCI
)
349 /* The 200-pin BCM4712 package does not bond out PCI. Even when
350 * PCI is bonded out, some boards may leave the pins floating. */
351 if (bus
->chip_id
== 0x4712) {
352 if (bus
->chip_package
== SSB_CHIPPACK_BCM4712S
)
354 if (bus
->chip_package
== SSB_CHIPPACK_BCM4712M
)
357 if (bus
->chip_id
== 0x5350)
360 return !mips_busprobe(tmp
, (u32
*) (bus
->mmio
+ (pc
->dev
->core_index
* SSB_CORE_SIZE
)));
362 #endif /* CONFIG_SSB_PCICORE_HOSTMODE */
365 /**************************************************
366 * Generic and Clientmode operation code.
367 **************************************************/
369 static void ssb_pcicore_init_clientmode(struct ssb_pcicore
*pc
)
371 /* Disable PCI interrupts. */
372 ssb_write32(pc
->dev
, SSB_INTVEC
, 0);
375 void ssb_pcicore_init(struct ssb_pcicore
*pc
)
377 struct ssb_device
*dev
= pc
->dev
;
383 if (!ssb_device_is_enabled(dev
))
384 ssb_device_enable(dev
, 0);
386 #ifdef CONFIG_SSB_PCICORE_HOSTMODE
387 pc
->hostmode
= pcicore_is_in_hostmode(pc
);
389 ssb_pcicore_init_hostmode(pc
);
390 #endif /* CONFIG_SSB_PCICORE_HOSTMODE */
392 ssb_pcicore_init_clientmode(pc
);
395 static u32
ssb_pcie_read(struct ssb_pcicore
*pc
, u32 address
)
397 pcicore_write32(pc
, 0x130, address
);
398 return pcicore_read32(pc
, 0x134);
401 static void ssb_pcie_write(struct ssb_pcicore
*pc
, u32 address
, u32 data
)
403 pcicore_write32(pc
, 0x130, address
);
404 pcicore_write32(pc
, 0x134, data
);
407 static void ssb_pcie_mdio_write(struct ssb_pcicore
*pc
, u8 device
,
408 u8 address
, u16 data
)
410 const u16 mdio_control
= 0x128;
411 const u16 mdio_data
= 0x12C;
415 v
= 0x80; /* Enable Preamble Sequence */
416 v
|= 0x2; /* MDIO Clock Divisor */
417 pcicore_write32(pc
, mdio_control
, v
);
419 v
= (1 << 30); /* Start of Transaction */
420 v
|= (1 << 28); /* Write Transaction */
421 v
|= (1 << 17); /* Turnaround */
422 v
|= (u32
)device
<< 22;
423 v
|= (u32
)address
<< 18;
425 pcicore_write32(pc
, mdio_data
, v
);
427 for (i
= 0; i
< 10; i
++) {
428 v
= pcicore_read32(pc
, mdio_control
);
429 if (v
& 0x100 /* Trans complete */)
433 pcicore_write32(pc
, mdio_control
, 0);
436 static void ssb_broadcast_value(struct ssb_device
*dev
,
437 u32 address
, u32 data
)
439 /* This is used for both, PCI and ChipCommon core, so be careful. */
440 BUILD_BUG_ON(SSB_PCICORE_BCAST_ADDR
!= SSB_CHIPCO_BCAST_ADDR
);
441 BUILD_BUG_ON(SSB_PCICORE_BCAST_DATA
!= SSB_CHIPCO_BCAST_DATA
);
443 ssb_write32(dev
, SSB_PCICORE_BCAST_ADDR
, address
);
444 ssb_read32(dev
, SSB_PCICORE_BCAST_ADDR
); /* flush */
445 ssb_write32(dev
, SSB_PCICORE_BCAST_DATA
, data
);
446 ssb_read32(dev
, SSB_PCICORE_BCAST_DATA
); /* flush */
449 static void ssb_commit_settings(struct ssb_bus
*bus
)
451 struct ssb_device
*dev
;
453 dev
= bus
->chipco
.dev
? bus
->chipco
.dev
: bus
->pcicore
.dev
;
455 /* This forces an update of the cached registers. */
456 ssb_broadcast_value(dev
, 0xFD8, 0);
459 int ssb_pcicore_dev_irqvecs_enable(struct ssb_pcicore
*pc
,
460 struct ssb_device
*dev
)
462 struct ssb_device
*pdev
= pc
->dev
;
473 /* Enable interrupts for this device. */
475 ((pdev
->id
.revision
>= 6) || (pdev
->id
.coreid
== SSB_DEV_PCIE
))) {
478 /* Calculate the "coremask" for the device. */
479 coremask
= (1 << dev
->core_index
);
481 err
= pci_read_config_dword(bus
->host_pci
, SSB_PCI_IRQMASK
, &tmp
);
484 tmp
|= coremask
<< 8;
485 err
= pci_write_config_dword(bus
->host_pci
, SSB_PCI_IRQMASK
, tmp
);
491 intvec
= ssb_read32(pdev
, SSB_INTVEC
);
492 tmp
= ssb_read32(dev
, SSB_TPSFLAG
);
493 tmp
&= SSB_TPSFLAG_BPFLAG
;
495 ssb_write32(pdev
, SSB_INTVEC
, intvec
);
498 /* Setup PCIcore operation. */
501 if (pdev
->id
.coreid
== SSB_DEV_PCI
) {
502 tmp
= pcicore_read32(pc
, SSB_PCICORE_SBTOPCI2
);
503 tmp
|= SSB_PCICORE_SBTOPCI_PREF
;
504 tmp
|= SSB_PCICORE_SBTOPCI_BURST
;
505 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI2
, tmp
);
507 if (pdev
->id
.revision
< 5) {
508 tmp
= ssb_read32(pdev
, SSB_IMCFGLO
);
509 tmp
&= ~SSB_IMCFGLO_SERTO
;
511 tmp
&= ~SSB_IMCFGLO_REQTO
;
512 tmp
|= 3 << SSB_IMCFGLO_REQTO_SHIFT
;
513 ssb_write32(pdev
, SSB_IMCFGLO
, tmp
);
514 ssb_commit_settings(bus
);
515 } else if (pdev
->id
.revision
>= 11) {
516 tmp
= pcicore_read32(pc
, SSB_PCICORE_SBTOPCI2
);
517 tmp
|= SSB_PCICORE_SBTOPCI_MRM
;
518 pcicore_write32(pc
, SSB_PCICORE_SBTOPCI2
, tmp
);
521 assert(pdev
->id
.coreid
== SSB_DEV_PCIE
);
522 //TODO: Better make defines for all these magic PCIE values.
523 if ((pdev
->id
.revision
== 0) || (pdev
->id
.revision
== 1)) {
524 /* TLP Workaround register. */
525 tmp
= ssb_pcie_read(pc
, 0x4);
527 ssb_pcie_write(pc
, 0x4, tmp
);
529 if (pdev
->id
.revision
== 0) {
530 const u8 serdes_rx_device
= 0x1F;
532 ssb_pcie_mdio_write(pc
, serdes_rx_device
,
533 2 /* Timer */, 0x8128);
534 ssb_pcie_mdio_write(pc
, serdes_rx_device
,
535 6 /* CDR */, 0x0100);
536 ssb_pcie_mdio_write(pc
, serdes_rx_device
,
537 7 /* CDR BW */, 0x1466);
538 } else if (pdev
->id
.revision
== 1) {
539 /* DLLP Link Control register. */
540 tmp
= ssb_pcie_read(pc
, 0x100);
542 ssb_pcie_write(pc
, 0x100, tmp
);
549 EXPORT_SYMBOL(ssb_pcicore_dev_irqvecs_enable
);