2 * Sonics Silicon Backplane
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 "ssb_private.h"
13 #include <linux/delay.h>
15 #include <linux/ssb/ssb.h>
16 #include <linux/ssb/ssb_regs.h>
17 #include <linux/ssb/ssb_driver_gige.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/pci.h>
20 #include <linux/mmc/sdio_func.h>
21 #include <linux/slab.h>
23 #include <pcmcia/cistpl.h>
24 #include <pcmcia/ds.h>
27 MODULE_DESCRIPTION("Sonics Silicon Backplane driver");
28 MODULE_LICENSE("GPL");
31 /* Temporary list of yet-to-be-attached buses */
32 static LIST_HEAD(attach_queue
);
33 /* List if running buses */
34 static LIST_HEAD(buses
);
35 /* Software ID counter */
36 static unsigned int next_busnumber
;
37 /* buses_mutes locks the two buslists and the next_busnumber.
38 * Don't lock this directly, but use ssb_buses_[un]lock() below. */
39 static DEFINE_MUTEX(buses_mutex
);
41 /* There are differences in the codeflow, if the bus is
42 * initialized from early boot, as various needed services
43 * are not available early. This is a mechanism to delay
44 * these initializations to after early boot has finished.
45 * It's also used to avoid mutex locking, as that's not
46 * available and needed early. */
47 static bool ssb_is_early_boot
= 1;
49 static void ssb_buses_lock(void);
50 static void ssb_buses_unlock(void);
53 #ifdef CONFIG_SSB_PCIHOST
54 struct ssb_bus
*ssb_pci_dev_to_bus(struct pci_dev
*pdev
)
59 list_for_each_entry(bus
, &buses
, list
) {
60 if (bus
->bustype
== SSB_BUSTYPE_PCI
&&
61 bus
->host_pci
== pdev
)
70 #endif /* CONFIG_SSB_PCIHOST */
72 #ifdef CONFIG_SSB_PCMCIAHOST
73 struct ssb_bus
*ssb_pcmcia_dev_to_bus(struct pcmcia_device
*pdev
)
78 list_for_each_entry(bus
, &buses
, list
) {
79 if (bus
->bustype
== SSB_BUSTYPE_PCMCIA
&&
80 bus
->host_pcmcia
== pdev
)
89 #endif /* CONFIG_SSB_PCMCIAHOST */
91 #ifdef CONFIG_SSB_SDIOHOST
92 struct ssb_bus
*ssb_sdio_func_to_bus(struct sdio_func
*func
)
97 list_for_each_entry(bus
, &buses
, list
) {
98 if (bus
->bustype
== SSB_BUSTYPE_SDIO
&&
99 bus
->host_sdio
== func
)
108 #endif /* CONFIG_SSB_SDIOHOST */
110 int ssb_for_each_bus_call(unsigned long data
,
111 int (*func
)(struct ssb_bus
*bus
, unsigned long data
))
117 list_for_each_entry(bus
, &buses
, list
) {
118 res
= func(bus
, data
);
129 static struct ssb_device
*ssb_device_get(struct ssb_device
*dev
)
132 get_device(dev
->dev
);
136 static void ssb_device_put(struct ssb_device
*dev
)
139 put_device(dev
->dev
);
142 static inline struct ssb_driver
*ssb_driver_get(struct ssb_driver
*drv
)
145 get_driver(&drv
->drv
);
149 static inline void ssb_driver_put(struct ssb_driver
*drv
)
152 put_driver(&drv
->drv
);
155 static int ssb_device_resume(struct device
*dev
)
157 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
158 struct ssb_driver
*ssb_drv
;
162 ssb_drv
= drv_to_ssb_drv(dev
->driver
);
163 if (ssb_drv
&& ssb_drv
->resume
)
164 err
= ssb_drv
->resume(ssb_dev
);
172 static int ssb_device_suspend(struct device
*dev
, pm_message_t state
)
174 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
175 struct ssb_driver
*ssb_drv
;
179 ssb_drv
= drv_to_ssb_drv(dev
->driver
);
180 if (ssb_drv
&& ssb_drv
->suspend
)
181 err
= ssb_drv
->suspend(ssb_dev
, state
);
189 int ssb_bus_resume(struct ssb_bus
*bus
)
193 /* Reset HW state information in memory, so that HW is
194 * completely reinitialized. */
195 bus
->mapped_device
= NULL
;
196 #ifdef CONFIG_SSB_DRIVER_PCICORE
197 bus
->pcicore
.setup_done
= 0;
200 err
= ssb_bus_powerup(bus
, 0);
203 err
= ssb_pcmcia_hardware_setup(bus
);
205 ssb_bus_may_powerdown(bus
);
208 ssb_chipco_resume(&bus
->chipco
);
209 ssb_bus_may_powerdown(bus
);
213 EXPORT_SYMBOL(ssb_bus_resume
);
215 int ssb_bus_suspend(struct ssb_bus
*bus
)
217 ssb_chipco_suspend(&bus
->chipco
);
218 ssb_pci_xtal(bus
, SSB_GPIO_XTAL
| SSB_GPIO_PLL
, 0);
222 EXPORT_SYMBOL(ssb_bus_suspend
);
224 #ifdef CONFIG_SSB_SPROM
225 /** ssb_devices_freeze - Freeze all devices on the bus.
227 * After freezing no device driver will be handling a device
228 * on this bus anymore. ssb_devices_thaw() must be called after
229 * a successful freeze to reactivate the devices.
232 * @ctx: Context structure. Pass this to ssb_devices_thaw().
234 int ssb_devices_freeze(struct ssb_bus
*bus
, struct ssb_freeze_context
*ctx
)
236 struct ssb_device
*sdev
;
237 struct ssb_driver
*sdrv
;
240 memset(ctx
, 0, sizeof(*ctx
));
242 SSB_WARN_ON(bus
->nr_devices
> ARRAY_SIZE(ctx
->device_frozen
));
244 for (i
= 0; i
< bus
->nr_devices
; i
++) {
245 sdev
= ssb_device_get(&bus
->devices
[i
]);
247 if (!sdev
->dev
|| !sdev
->dev
->driver
||
248 !device_is_registered(sdev
->dev
)) {
249 ssb_device_put(sdev
);
252 sdrv
= ssb_driver_get(drv_to_ssb_drv(sdev
->dev
->driver
));
253 if (!sdrv
|| SSB_WARN_ON(!sdrv
->remove
)) {
254 ssb_device_put(sdev
);
258 ctx
->device_frozen
[i
] = 1;
264 /** ssb_devices_thaw - Unfreeze all devices on the bus.
266 * This will re-attach the device drivers and re-init the devices.
268 * @ctx: The context structure from ssb_devices_freeze()
270 int ssb_devices_thaw(struct ssb_freeze_context
*ctx
)
272 struct ssb_bus
*bus
= ctx
->bus
;
273 struct ssb_device
*sdev
;
274 struct ssb_driver
*sdrv
;
278 for (i
= 0; i
< bus
->nr_devices
; i
++) {
279 if (!ctx
->device_frozen
[i
])
281 sdev
= &bus
->devices
[i
];
283 if (SSB_WARN_ON(!sdev
->dev
|| !sdev
->dev
->driver
))
285 sdrv
= drv_to_ssb_drv(sdev
->dev
->driver
);
286 if (SSB_WARN_ON(!sdrv
|| !sdrv
->probe
))
289 err
= sdrv
->probe(sdev
, &sdev
->id
);
291 ssb_printk(KERN_ERR PFX
"Failed to thaw device %s\n",
292 dev_name(sdev
->dev
));
295 ssb_driver_put(sdrv
);
296 ssb_device_put(sdev
);
301 #endif /* CONFIG_SSB_SPROM */
303 static void ssb_device_shutdown(struct device
*dev
)
305 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
306 struct ssb_driver
*ssb_drv
;
310 ssb_drv
= drv_to_ssb_drv(dev
->driver
);
311 if (ssb_drv
&& ssb_drv
->shutdown
)
312 ssb_drv
->shutdown(ssb_dev
);
315 static int ssb_device_remove(struct device
*dev
)
317 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
318 struct ssb_driver
*ssb_drv
= drv_to_ssb_drv(dev
->driver
);
320 if (ssb_drv
&& ssb_drv
->remove
)
321 ssb_drv
->remove(ssb_dev
);
322 ssb_device_put(ssb_dev
);
327 static int ssb_device_probe(struct device
*dev
)
329 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
330 struct ssb_driver
*ssb_drv
= drv_to_ssb_drv(dev
->driver
);
333 ssb_device_get(ssb_dev
);
334 if (ssb_drv
&& ssb_drv
->probe
)
335 err
= ssb_drv
->probe(ssb_dev
, &ssb_dev
->id
);
337 ssb_device_put(ssb_dev
);
342 static int ssb_match_devid(const struct ssb_device_id
*tabid
,
343 const struct ssb_device_id
*devid
)
345 if ((tabid
->vendor
!= devid
->vendor
) &&
346 tabid
->vendor
!= SSB_ANY_VENDOR
)
348 if ((tabid
->coreid
!= devid
->coreid
) &&
349 tabid
->coreid
!= SSB_ANY_ID
)
351 if ((tabid
->revision
!= devid
->revision
) &&
352 tabid
->revision
!= SSB_ANY_REV
)
357 static int ssb_bus_match(struct device
*dev
, struct device_driver
*drv
)
359 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
360 struct ssb_driver
*ssb_drv
= drv_to_ssb_drv(drv
);
361 const struct ssb_device_id
*id
;
363 for (id
= ssb_drv
->id_table
;
364 id
->vendor
|| id
->coreid
|| id
->revision
;
366 if (ssb_match_devid(id
, &ssb_dev
->id
))
367 return 1; /* found */
373 static int ssb_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
375 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
380 return add_uevent_var(env
,
381 "MODALIAS=ssb:v%04Xid%04Xrev%02X",
382 ssb_dev
->id
.vendor
, ssb_dev
->id
.coreid
,
383 ssb_dev
->id
.revision
);
386 #define ssb_config_attr(attrib, field, format_string) \
388 attrib##_show(struct device *dev, struct device_attribute *attr, char *buf) \
390 return sprintf(buf, format_string, dev_to_ssb_dev(dev)->field); \
393 ssb_config_attr(core_num
, core_index
, "%u\n")
394 ssb_config_attr(coreid
, id
.coreid
, "0x%04x\n")
395 ssb_config_attr(vendor
, id
.vendor
, "0x%04x\n")
396 ssb_config_attr(revision
, id
.revision
, "%u\n")
397 ssb_config_attr(irq
, irq
, "%u\n")
399 name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
401 return sprintf(buf
, "%s\n",
402 ssb_core_name(dev_to_ssb_dev(dev
)->id
.coreid
));
405 static struct device_attribute ssb_device_attrs
[] = {
415 static struct bus_type ssb_bustype
= {
417 .match
= ssb_bus_match
,
418 .probe
= ssb_device_probe
,
419 .remove
= ssb_device_remove
,
420 .shutdown
= ssb_device_shutdown
,
421 .suspend
= ssb_device_suspend
,
422 .resume
= ssb_device_resume
,
423 .uevent
= ssb_device_uevent
,
424 .dev_attrs
= ssb_device_attrs
,
427 static void ssb_buses_lock(void)
429 /* See the comment at the ssb_is_early_boot definition */
430 if (!ssb_is_early_boot
)
431 mutex_lock(&buses_mutex
);
434 static void ssb_buses_unlock(void)
436 /* See the comment at the ssb_is_early_boot definition */
437 if (!ssb_is_early_boot
)
438 mutex_unlock(&buses_mutex
);
441 static void ssb_devices_unregister(struct ssb_bus
*bus
)
443 struct ssb_device
*sdev
;
446 for (i
= bus
->nr_devices
- 1; i
>= 0; i
--) {
447 sdev
= &(bus
->devices
[i
]);
449 device_unregister(sdev
->dev
);
453 void ssb_bus_unregister(struct ssb_bus
*bus
)
456 ssb_devices_unregister(bus
);
457 list_del(&bus
->list
);
460 ssb_pcmcia_exit(bus
);
464 EXPORT_SYMBOL(ssb_bus_unregister
);
466 static void ssb_release_dev(struct device
*dev
)
468 struct __ssb_dev_wrapper
*devwrap
;
470 devwrap
= container_of(dev
, struct __ssb_dev_wrapper
, dev
);
474 static int ssb_devices_register(struct ssb_bus
*bus
)
476 struct ssb_device
*sdev
;
478 struct __ssb_dev_wrapper
*devwrap
;
482 for (i
= 0; i
< bus
->nr_devices
; i
++) {
483 sdev
= &(bus
->devices
[i
]);
485 /* We don't register SSB-system devices to the kernel,
486 * as the drivers for them are built into SSB. */
487 switch (sdev
->id
.coreid
) {
488 case SSB_DEV_CHIPCOMMON
:
493 case SSB_DEV_MIPS_3302
:
498 devwrap
= kzalloc(sizeof(*devwrap
), GFP_KERNEL
);
500 ssb_printk(KERN_ERR PFX
501 "Could not allocate device\n");
506 devwrap
->sdev
= sdev
;
508 dev
->release
= ssb_release_dev
;
509 dev
->bus
= &ssb_bustype
;
510 dev_set_name(dev
, "ssb%u:%d", bus
->busnumber
, dev_idx
);
512 switch (bus
->bustype
) {
513 case SSB_BUSTYPE_PCI
:
514 #ifdef CONFIG_SSB_PCIHOST
515 sdev
->irq
= bus
->host_pci
->irq
;
516 dev
->parent
= &bus
->host_pci
->dev
;
517 sdev
->dma_dev
= dev
->parent
;
520 case SSB_BUSTYPE_PCMCIA
:
521 #ifdef CONFIG_SSB_PCMCIAHOST
522 sdev
->irq
= bus
->host_pcmcia
->irq
;
523 dev
->parent
= &bus
->host_pcmcia
->dev
;
526 case SSB_BUSTYPE_SDIO
:
527 #ifdef CONFIG_SSB_SDIOHOST
528 dev
->parent
= &bus
->host_sdio
->dev
;
531 case SSB_BUSTYPE_SSB
:
532 dev
->dma_mask
= &dev
->coherent_dma_mask
;
538 err
= device_register(dev
);
540 ssb_printk(KERN_ERR PFX
541 "Could not register %s\n",
543 /* Set dev to NULL to not unregister
544 * dev on error unwinding. */
554 /* Unwind the already registered devices. */
555 ssb_devices_unregister(bus
);
559 /* Needs ssb_buses_lock() */
560 static int __devinit
ssb_attach_queued_buses(void)
562 struct ssb_bus
*bus
, *n
;
564 int drop_them_all
= 0;
566 list_for_each_entry_safe(bus
, n
, &attach_queue
, list
) {
568 list_del(&bus
->list
);
571 /* Can't init the PCIcore in ssb_bus_register(), as that
572 * is too early in boot for embedded systems
573 * (no udelay() available). So do it here in attach stage.
575 err
= ssb_bus_powerup(bus
, 0);
578 ssb_pcicore_init(&bus
->pcicore
);
579 ssb_bus_may_powerdown(bus
);
581 err
= ssb_devices_register(bus
);
585 list_del(&bus
->list
);
588 list_move_tail(&bus
->list
, &buses
);
594 static u8
ssb_ssb_read8(struct ssb_device
*dev
, u16 offset
)
596 struct ssb_bus
*bus
= dev
->bus
;
598 offset
+= dev
->core_index
* SSB_CORE_SIZE
;
599 return readb(bus
->mmio
+ offset
);
602 static u16
ssb_ssb_read16(struct ssb_device
*dev
, u16 offset
)
604 struct ssb_bus
*bus
= dev
->bus
;
606 offset
+= dev
->core_index
* SSB_CORE_SIZE
;
607 return readw(bus
->mmio
+ offset
);
610 static u32
ssb_ssb_read32(struct ssb_device
*dev
, u16 offset
)
612 struct ssb_bus
*bus
= dev
->bus
;
614 offset
+= dev
->core_index
* SSB_CORE_SIZE
;
615 return readl(bus
->mmio
+ offset
);
618 #ifdef CONFIG_SSB_BLOCKIO
619 static void ssb_ssb_block_read(struct ssb_device
*dev
, void *buffer
,
620 size_t count
, u16 offset
, u8 reg_width
)
622 struct ssb_bus
*bus
= dev
->bus
;
625 offset
+= dev
->core_index
* SSB_CORE_SIZE
;
626 addr
= bus
->mmio
+ offset
;
633 *buf
= __raw_readb(addr
);
640 __le16
*buf
= buffer
;
642 SSB_WARN_ON(count
& 1);
644 *buf
= (__force __le16
)__raw_readw(addr
);
651 __le32
*buf
= buffer
;
653 SSB_WARN_ON(count
& 3);
655 *buf
= (__force __le32
)__raw_readl(addr
);
665 #endif /* CONFIG_SSB_BLOCKIO */
667 static void ssb_ssb_write8(struct ssb_device
*dev
, u16 offset
, u8 value
)
669 struct ssb_bus
*bus
= dev
->bus
;
671 offset
+= dev
->core_index
* SSB_CORE_SIZE
;
672 writeb(value
, bus
->mmio
+ offset
);
675 static void ssb_ssb_write16(struct ssb_device
*dev
, u16 offset
, u16 value
)
677 struct ssb_bus
*bus
= dev
->bus
;
679 offset
+= dev
->core_index
* SSB_CORE_SIZE
;
680 writew(value
, bus
->mmio
+ offset
);
683 static void ssb_ssb_write32(struct ssb_device
*dev
, u16 offset
, u32 value
)
685 struct ssb_bus
*bus
= dev
->bus
;
687 offset
+= dev
->core_index
* SSB_CORE_SIZE
;
688 writel(value
, bus
->mmio
+ offset
);
691 #ifdef CONFIG_SSB_BLOCKIO
692 static void ssb_ssb_block_write(struct ssb_device
*dev
, const void *buffer
,
693 size_t count
, u16 offset
, u8 reg_width
)
695 struct ssb_bus
*bus
= dev
->bus
;
698 offset
+= dev
->core_index
* SSB_CORE_SIZE
;
699 addr
= bus
->mmio
+ offset
;
703 const u8
*buf
= buffer
;
706 __raw_writeb(*buf
, addr
);
713 const __le16
*buf
= buffer
;
715 SSB_WARN_ON(count
& 1);
717 __raw_writew((__force u16
)(*buf
), addr
);
724 const __le32
*buf
= buffer
;
726 SSB_WARN_ON(count
& 3);
728 __raw_writel((__force u32
)(*buf
), addr
);
738 #endif /* CONFIG_SSB_BLOCKIO */
740 /* Ops for the plain SSB bus without a host-device (no PCI or PCMCIA). */
741 static const struct ssb_bus_ops ssb_ssb_ops
= {
742 .read8
= ssb_ssb_read8
,
743 .read16
= ssb_ssb_read16
,
744 .read32
= ssb_ssb_read32
,
745 .write8
= ssb_ssb_write8
,
746 .write16
= ssb_ssb_write16
,
747 .write32
= ssb_ssb_write32
,
748 #ifdef CONFIG_SSB_BLOCKIO
749 .block_read
= ssb_ssb_block_read
,
750 .block_write
= ssb_ssb_block_write
,
754 static int ssb_fetch_invariants(struct ssb_bus
*bus
,
755 ssb_invariants_func_t get_invariants
)
757 struct ssb_init_invariants iv
;
760 memset(&iv
, 0, sizeof(iv
));
761 err
= get_invariants(bus
, &iv
);
764 memcpy(&bus
->boardinfo
, &iv
.boardinfo
, sizeof(iv
.boardinfo
));
765 memcpy(&bus
->sprom
, &iv
.sprom
, sizeof(iv
.sprom
));
766 bus
->has_cardbus_slot
= iv
.has_cardbus_slot
;
771 static int __devinit
ssb_bus_register(struct ssb_bus
*bus
,
772 ssb_invariants_func_t get_invariants
,
773 unsigned long baseaddr
)
777 spin_lock_init(&bus
->bar_lock
);
778 INIT_LIST_HEAD(&bus
->list
);
779 #ifdef CONFIG_SSB_EMBEDDED
780 spin_lock_init(&bus
->gpio_lock
);
783 /* Powerup the bus */
784 err
= ssb_pci_xtal(bus
, SSB_GPIO_XTAL
| SSB_GPIO_PLL
, 1);
788 /* Init SDIO-host device (if any), before the scan */
789 err
= ssb_sdio_init(bus
);
791 goto err_disable_xtal
;
794 bus
->busnumber
= next_busnumber
;
795 /* Scan for devices (cores) */
796 err
= ssb_bus_scan(bus
, baseaddr
);
800 /* Init PCI-host device (if any) */
801 err
= ssb_pci_init(bus
);
804 /* Init PCMCIA-host device (if any) */
805 err
= ssb_pcmcia_init(bus
);
809 /* Initialize basic system devices (if available) */
810 err
= ssb_bus_powerup(bus
, 0);
812 goto err_pcmcia_exit
;
813 ssb_chipcommon_init(&bus
->chipco
);
814 ssb_mipscore_init(&bus
->mipscore
);
815 err
= ssb_fetch_invariants(bus
, get_invariants
);
817 ssb_bus_may_powerdown(bus
);
818 goto err_pcmcia_exit
;
820 ssb_bus_may_powerdown(bus
);
822 /* Queue it for attach.
823 * See the comment at the ssb_is_early_boot definition. */
824 list_add_tail(&bus
->list
, &attach_queue
);
825 if (!ssb_is_early_boot
) {
826 /* This is not early boot, so we must attach the bus now */
827 err
= ssb_attach_queued_buses();
838 list_del(&bus
->list
);
840 ssb_pcmcia_exit(bus
);
849 ssb_pci_xtal(bus
, SSB_GPIO_XTAL
| SSB_GPIO_PLL
, 0);
853 #ifdef CONFIG_SSB_PCIHOST
854 int __devinit
ssb_bus_pcibus_register(struct ssb_bus
*bus
,
855 struct pci_dev
*host_pci
)
859 bus
->bustype
= SSB_BUSTYPE_PCI
;
860 bus
->host_pci
= host_pci
;
861 bus
->ops
= &ssb_pci_ops
;
863 err
= ssb_bus_register(bus
, ssb_pci_get_invariants
, 0);
865 ssb_printk(KERN_INFO PFX
"Sonics Silicon Backplane found on "
866 "PCI device %s\n", dev_name(&host_pci
->dev
));
868 ssb_printk(KERN_ERR PFX
"Failed to register PCI version"
869 " of SSB with error %d\n", err
);
874 EXPORT_SYMBOL(ssb_bus_pcibus_register
);
875 #endif /* CONFIG_SSB_PCIHOST */
877 #ifdef CONFIG_SSB_PCMCIAHOST
878 int __devinit
ssb_bus_pcmciabus_register(struct ssb_bus
*bus
,
879 struct pcmcia_device
*pcmcia_dev
,
880 unsigned long baseaddr
)
884 bus
->bustype
= SSB_BUSTYPE_PCMCIA
;
885 bus
->host_pcmcia
= pcmcia_dev
;
886 bus
->ops
= &ssb_pcmcia_ops
;
888 err
= ssb_bus_register(bus
, ssb_pcmcia_get_invariants
, baseaddr
);
890 ssb_printk(KERN_INFO PFX
"Sonics Silicon Backplane found on "
891 "PCMCIA device %s\n", pcmcia_dev
->devname
);
896 EXPORT_SYMBOL(ssb_bus_pcmciabus_register
);
897 #endif /* CONFIG_SSB_PCMCIAHOST */
899 #ifdef CONFIG_SSB_SDIOHOST
900 int __devinit
ssb_bus_sdiobus_register(struct ssb_bus
*bus
,
901 struct sdio_func
*func
,
906 bus
->bustype
= SSB_BUSTYPE_SDIO
;
907 bus
->host_sdio
= func
;
908 bus
->ops
= &ssb_sdio_ops
;
909 bus
->quirks
= quirks
;
911 err
= ssb_bus_register(bus
, ssb_sdio_get_invariants
, ~0);
913 ssb_printk(KERN_INFO PFX
"Sonics Silicon Backplane found on "
914 "SDIO device %s\n", sdio_func_id(func
));
919 EXPORT_SYMBOL(ssb_bus_sdiobus_register
);
920 #endif /* CONFIG_SSB_PCMCIAHOST */
922 int __devinit
ssb_bus_ssbbus_register(struct ssb_bus
*bus
,
923 unsigned long baseaddr
,
924 ssb_invariants_func_t get_invariants
)
928 bus
->bustype
= SSB_BUSTYPE_SSB
;
929 bus
->ops
= &ssb_ssb_ops
;
931 err
= ssb_bus_register(bus
, get_invariants
, baseaddr
);
933 ssb_printk(KERN_INFO PFX
"Sonics Silicon Backplane found at "
934 "address 0x%08lX\n", baseaddr
);
940 int __ssb_driver_register(struct ssb_driver
*drv
, struct module
*owner
)
942 drv
->drv
.name
= drv
->name
;
943 drv
->drv
.bus
= &ssb_bustype
;
944 drv
->drv
.owner
= owner
;
946 return driver_register(&drv
->drv
);
948 EXPORT_SYMBOL(__ssb_driver_register
);
950 void ssb_driver_unregister(struct ssb_driver
*drv
)
952 driver_unregister(&drv
->drv
);
954 EXPORT_SYMBOL(ssb_driver_unregister
);
956 void ssb_set_devtypedata(struct ssb_device
*dev
, void *data
)
958 struct ssb_bus
*bus
= dev
->bus
;
959 struct ssb_device
*ent
;
962 for (i
= 0; i
< bus
->nr_devices
; i
++) {
963 ent
= &(bus
->devices
[i
]);
964 if (ent
->id
.vendor
!= dev
->id
.vendor
)
966 if (ent
->id
.coreid
!= dev
->id
.coreid
)
969 ent
->devtypedata
= data
;
972 EXPORT_SYMBOL(ssb_set_devtypedata
);
974 static u32
clkfactor_f6_resolve(u32 v
)
976 /* map the magic values */
978 case SSB_CHIPCO_CLK_F6_2
:
980 case SSB_CHIPCO_CLK_F6_3
:
982 case SSB_CHIPCO_CLK_F6_4
:
984 case SSB_CHIPCO_CLK_F6_5
:
986 case SSB_CHIPCO_CLK_F6_6
:
988 case SSB_CHIPCO_CLK_F6_7
:
994 /* Calculate the speed the backplane would run at a given set of clockcontrol values */
995 u32
ssb_calc_clock_rate(u32 plltype
, u32 n
, u32 m
)
997 u32 n1
, n2
, clock
, m1
, m2
, m3
, mc
;
999 n1
= (n
& SSB_CHIPCO_CLK_N1
);
1000 n2
= ((n
& SSB_CHIPCO_CLK_N2
) >> SSB_CHIPCO_CLK_N2_SHIFT
);
1003 case SSB_PLLTYPE_6
: /* 100/200 or 120/240 only */
1004 if (m
& SSB_CHIPCO_CLK_T6_MMASK
)
1005 return SSB_CHIPCO_CLK_T6_M1
;
1006 return SSB_CHIPCO_CLK_T6_M0
;
1007 case SSB_PLLTYPE_1
: /* 48Mhz base, 3 dividers */
1008 case SSB_PLLTYPE_3
: /* 25Mhz, 2 dividers */
1009 case SSB_PLLTYPE_4
: /* 48Mhz, 4 dividers */
1010 case SSB_PLLTYPE_7
: /* 25Mhz, 4 dividers */
1011 n1
= clkfactor_f6_resolve(n1
);
1012 n2
+= SSB_CHIPCO_CLK_F5_BIAS
;
1014 case SSB_PLLTYPE_2
: /* 48Mhz, 4 dividers */
1015 n1
+= SSB_CHIPCO_CLK_T2_BIAS
;
1016 n2
+= SSB_CHIPCO_CLK_T2_BIAS
;
1017 SSB_WARN_ON(!((n1
>= 2) && (n1
<= 7)));
1018 SSB_WARN_ON(!((n2
>= 5) && (n2
<= 23)));
1020 case SSB_PLLTYPE_5
: /* 25Mhz, 4 dividers */
1027 case SSB_PLLTYPE_3
: /* 25Mhz, 2 dividers */
1028 case SSB_PLLTYPE_7
: /* 25Mhz, 4 dividers */
1029 clock
= SSB_CHIPCO_CLK_BASE2
* n1
* n2
;
1032 clock
= SSB_CHIPCO_CLK_BASE1
* n1
* n2
;
1037 m1
= (m
& SSB_CHIPCO_CLK_M1
);
1038 m2
= ((m
& SSB_CHIPCO_CLK_M2
) >> SSB_CHIPCO_CLK_M2_SHIFT
);
1039 m3
= ((m
& SSB_CHIPCO_CLK_M3
) >> SSB_CHIPCO_CLK_M3_SHIFT
);
1040 mc
= ((m
& SSB_CHIPCO_CLK_MC
) >> SSB_CHIPCO_CLK_MC_SHIFT
);
1043 case SSB_PLLTYPE_1
: /* 48Mhz base, 3 dividers */
1044 case SSB_PLLTYPE_3
: /* 25Mhz, 2 dividers */
1045 case SSB_PLLTYPE_4
: /* 48Mhz, 4 dividers */
1046 case SSB_PLLTYPE_7
: /* 25Mhz, 4 dividers */
1047 m1
= clkfactor_f6_resolve(m1
);
1048 if ((plltype
== SSB_PLLTYPE_1
) ||
1049 (plltype
== SSB_PLLTYPE_3
))
1050 m2
+= SSB_CHIPCO_CLK_F5_BIAS
;
1052 m2
= clkfactor_f6_resolve(m2
);
1053 m3
= clkfactor_f6_resolve(m3
);
1056 case SSB_CHIPCO_CLK_MC_BYPASS
:
1058 case SSB_CHIPCO_CLK_MC_M1
:
1059 return (clock
/ m1
);
1060 case SSB_CHIPCO_CLK_MC_M1M2
:
1061 return (clock
/ (m1
* m2
));
1062 case SSB_CHIPCO_CLK_MC_M1M2M3
:
1063 return (clock
/ (m1
* m2
* m3
));
1064 case SSB_CHIPCO_CLK_MC_M1M3
:
1065 return (clock
/ (m1
* m3
));
1069 m1
+= SSB_CHIPCO_CLK_T2_BIAS
;
1070 m2
+= SSB_CHIPCO_CLK_T2M2_BIAS
;
1071 m3
+= SSB_CHIPCO_CLK_T2_BIAS
;
1072 SSB_WARN_ON(!((m1
>= 2) && (m1
<= 7)));
1073 SSB_WARN_ON(!((m2
>= 3) && (m2
<= 10)));
1074 SSB_WARN_ON(!((m3
>= 2) && (m3
<= 7)));
1076 if (!(mc
& SSB_CHIPCO_CLK_T2MC_M1BYP
))
1078 if (!(mc
& SSB_CHIPCO_CLK_T2MC_M2BYP
))
1080 if (!(mc
& SSB_CHIPCO_CLK_T2MC_M3BYP
))
1089 /* Get the current speed the backplane is running at */
1090 u32
ssb_clockspeed(struct ssb_bus
*bus
)
1094 u32 clkctl_n
, clkctl_m
;
1096 if (ssb_extif_available(&bus
->extif
))
1097 ssb_extif_get_clockcontrol(&bus
->extif
, &plltype
,
1098 &clkctl_n
, &clkctl_m
);
1099 else if (bus
->chipco
.dev
)
1100 ssb_chipco_get_clockcontrol(&bus
->chipco
, &plltype
,
1101 &clkctl_n
, &clkctl_m
);
1105 if (bus
->chip_id
== 0x5365) {
1108 rate
= ssb_calc_clock_rate(plltype
, clkctl_n
, clkctl_m
);
1109 if (plltype
== SSB_PLLTYPE_3
) /* 25Mhz, 2 dividers */
1115 EXPORT_SYMBOL(ssb_clockspeed
);
1117 static u32
ssb_tmslow_reject_bitmask(struct ssb_device
*dev
)
1119 u32 rev
= ssb_read32(dev
, SSB_IDLOW
) & SSB_IDLOW_SSBREV
;
1121 /* The REJECT bit seems to be different for Backplane rev 2.3 */
1123 case SSB_IDLOW_SSBREV_22
:
1124 case SSB_IDLOW_SSBREV_24
:
1125 case SSB_IDLOW_SSBREV_26
:
1126 return SSB_TMSLOW_REJECT
;
1127 case SSB_IDLOW_SSBREV_23
:
1128 return SSB_TMSLOW_REJECT_23
;
1129 case SSB_IDLOW_SSBREV_25
: /* TODO - find the proper REJECT bit */
1130 case SSB_IDLOW_SSBREV_27
: /* same here */
1131 return SSB_TMSLOW_REJECT
; /* this is a guess */
1133 printk(KERN_INFO
"ssb: Backplane Revision 0x%.8X\n", rev
);
1136 return (SSB_TMSLOW_REJECT
| SSB_TMSLOW_REJECT_23
);
1139 int ssb_device_is_enabled(struct ssb_device
*dev
)
1144 reject
= ssb_tmslow_reject_bitmask(dev
);
1145 val
= ssb_read32(dev
, SSB_TMSLOW
);
1146 val
&= SSB_TMSLOW_CLOCK
| SSB_TMSLOW_RESET
| reject
;
1148 return (val
== SSB_TMSLOW_CLOCK
);
1150 EXPORT_SYMBOL(ssb_device_is_enabled
);
1152 static void ssb_flush_tmslow(struct ssb_device
*dev
)
1154 /* Make _really_ sure the device has finished the TMSLOW
1155 * register write transaction, as we risk running into
1156 * a machine check exception otherwise.
1157 * Do this by reading the register back to commit the
1158 * PCI write and delay an additional usec for the device
1159 * to react to the change. */
1160 ssb_read32(dev
, SSB_TMSLOW
);
1164 void ssb_device_enable(struct ssb_device
*dev
, u32 core_specific_flags
)
1168 ssb_device_disable(dev
, core_specific_flags
);
1169 ssb_write32(dev
, SSB_TMSLOW
,
1170 SSB_TMSLOW_RESET
| SSB_TMSLOW_CLOCK
|
1171 SSB_TMSLOW_FGC
| core_specific_flags
);
1172 ssb_flush_tmslow(dev
);
1174 /* Clear SERR if set. This is a hw bug workaround. */
1175 if (ssb_read32(dev
, SSB_TMSHIGH
) & SSB_TMSHIGH_SERR
)
1176 ssb_write32(dev
, SSB_TMSHIGH
, 0);
1178 val
= ssb_read32(dev
, SSB_IMSTATE
);
1179 if (val
& (SSB_IMSTATE_IBE
| SSB_IMSTATE_TO
)) {
1180 val
&= ~(SSB_IMSTATE_IBE
| SSB_IMSTATE_TO
);
1181 ssb_write32(dev
, SSB_IMSTATE
, val
);
1184 ssb_write32(dev
, SSB_TMSLOW
,
1185 SSB_TMSLOW_CLOCK
| SSB_TMSLOW_FGC
|
1186 core_specific_flags
);
1187 ssb_flush_tmslow(dev
);
1189 ssb_write32(dev
, SSB_TMSLOW
, SSB_TMSLOW_CLOCK
|
1190 core_specific_flags
);
1191 ssb_flush_tmslow(dev
);
1193 EXPORT_SYMBOL(ssb_device_enable
);
1195 /* Wait for bitmask in a register to get set or cleared.
1196 * timeout is in units of ten-microseconds */
1197 static int ssb_wait_bits(struct ssb_device
*dev
, u16 reg
, u32 bitmask
,
1198 int timeout
, int set
)
1203 for (i
= 0; i
< timeout
; i
++) {
1204 val
= ssb_read32(dev
, reg
);
1206 if ((val
& bitmask
) == bitmask
)
1209 if (!(val
& bitmask
))
1214 printk(KERN_ERR PFX
"Timeout waiting for bitmask %08X on "
1215 "register %04X to %s.\n",
1216 bitmask
, reg
, (set
? "set" : "clear"));
1221 void ssb_device_disable(struct ssb_device
*dev
, u32 core_specific_flags
)
1225 if (ssb_read32(dev
, SSB_TMSLOW
) & SSB_TMSLOW_RESET
)
1228 reject
= ssb_tmslow_reject_bitmask(dev
);
1230 if (ssb_read32(dev
, SSB_TMSLOW
) & SSB_TMSLOW_CLOCK
) {
1231 ssb_write32(dev
, SSB_TMSLOW
, reject
| SSB_TMSLOW_CLOCK
);
1232 ssb_wait_bits(dev
, SSB_TMSLOW
, reject
, 1000, 1);
1233 ssb_wait_bits(dev
, SSB_TMSHIGH
, SSB_TMSHIGH_BUSY
, 1000, 0);
1235 if (ssb_read32(dev
, SSB_IDLOW
) & SSB_IDLOW_INITIATOR
) {
1236 val
= ssb_read32(dev
, SSB_IMSTATE
);
1237 val
|= SSB_IMSTATE_REJECT
;
1238 ssb_write32(dev
, SSB_IMSTATE
, val
);
1239 ssb_wait_bits(dev
, SSB_IMSTATE
, SSB_IMSTATE_BUSY
, 1000,
1243 ssb_write32(dev
, SSB_TMSLOW
,
1244 SSB_TMSLOW_FGC
| SSB_TMSLOW_CLOCK
|
1245 reject
| SSB_TMSLOW_RESET
|
1246 core_specific_flags
);
1247 ssb_flush_tmslow(dev
);
1249 if (ssb_read32(dev
, SSB_IDLOW
) & SSB_IDLOW_INITIATOR
) {
1250 val
= ssb_read32(dev
, SSB_IMSTATE
);
1251 val
&= ~SSB_IMSTATE_REJECT
;
1252 ssb_write32(dev
, SSB_IMSTATE
, val
);
1256 ssb_write32(dev
, SSB_TMSLOW
,
1257 reject
| SSB_TMSLOW_RESET
|
1258 core_specific_flags
);
1259 ssb_flush_tmslow(dev
);
1261 EXPORT_SYMBOL(ssb_device_disable
);
1263 u32
ssb_dma_translation(struct ssb_device
*dev
)
1265 switch (dev
->bus
->bustype
) {
1266 case SSB_BUSTYPE_SSB
:
1268 case SSB_BUSTYPE_PCI
:
1269 if (ssb_read32(dev
, SSB_TMSHIGH
) & SSB_TMSHIGH_DMA64
)
1270 return SSB_PCIE_DMA_H32
;
1274 __ssb_dma_not_implemented(dev
);
1278 EXPORT_SYMBOL(ssb_dma_translation
);
1280 int ssb_bus_may_powerdown(struct ssb_bus
*bus
)
1282 struct ssb_chipcommon
*cc
;
1285 /* On buses where more than one core may be working
1286 * at a time, we must not powerdown stuff if there are
1287 * still cores that may want to run. */
1288 if (bus
->bustype
== SSB_BUSTYPE_SSB
)
1295 if (cc
->dev
->id
.revision
< 5)
1298 ssb_chipco_set_clockmode(cc
, SSB_CLKMODE_SLOW
);
1299 err
= ssb_pci_xtal(bus
, SSB_GPIO_XTAL
| SSB_GPIO_PLL
, 0);
1303 #ifdef CONFIG_SSB_DEBUG
1304 bus
->powered_up
= 0;
1308 ssb_printk(KERN_ERR PFX
"Bus powerdown failed\n");
1311 EXPORT_SYMBOL(ssb_bus_may_powerdown
);
1313 int ssb_bus_powerup(struct ssb_bus
*bus
, bool dynamic_pctl
)
1316 enum ssb_clkmode mode
;
1318 err
= ssb_pci_xtal(bus
, SSB_GPIO_XTAL
| SSB_GPIO_PLL
, 1);
1322 #ifdef CONFIG_SSB_DEBUG
1323 bus
->powered_up
= 1;
1326 mode
= dynamic_pctl
? SSB_CLKMODE_DYNAMIC
: SSB_CLKMODE_FAST
;
1327 ssb_chipco_set_clockmode(&bus
->chipco
, mode
);
1331 ssb_printk(KERN_ERR PFX
"Bus powerup failed\n");
1334 EXPORT_SYMBOL(ssb_bus_powerup
);
1336 static void ssb_broadcast_value(struct ssb_device
*dev
,
1337 u32 address
, u32 data
)
1339 #ifdef CONFIG_SSB_DRIVER_PCICORE
1340 /* This is used for both, PCI and ChipCommon core, so be careful. */
1341 BUILD_BUG_ON(SSB_PCICORE_BCAST_ADDR
!= SSB_CHIPCO_BCAST_ADDR
);
1342 BUILD_BUG_ON(SSB_PCICORE_BCAST_DATA
!= SSB_CHIPCO_BCAST_DATA
);
1345 ssb_write32(dev
, SSB_CHIPCO_BCAST_ADDR
, address
);
1346 ssb_read32(dev
, SSB_CHIPCO_BCAST_ADDR
); /* flush */
1347 ssb_write32(dev
, SSB_CHIPCO_BCAST_DATA
, data
);
1348 ssb_read32(dev
, SSB_CHIPCO_BCAST_DATA
); /* flush */
1351 void ssb_commit_settings(struct ssb_bus
*bus
)
1353 struct ssb_device
*dev
;
1355 #ifdef CONFIG_SSB_DRIVER_PCICORE
1356 dev
= bus
->chipco
.dev
? bus
->chipco
.dev
: bus
->pcicore
.dev
;
1358 dev
= bus
->chipco
.dev
;
1362 /* This forces an update of the cached registers. */
1363 ssb_broadcast_value(dev
, 0xFD8, 0);
1365 EXPORT_SYMBOL(ssb_commit_settings
);
1367 u32
ssb_admatch_base(u32 adm
)
1371 switch (adm
& SSB_ADM_TYPE
) {
1373 base
= (adm
& SSB_ADM_BASE0
);
1376 SSB_WARN_ON(adm
& SSB_ADM_NEG
); /* unsupported */
1377 base
= (adm
& SSB_ADM_BASE1
);
1380 SSB_WARN_ON(adm
& SSB_ADM_NEG
); /* unsupported */
1381 base
= (adm
& SSB_ADM_BASE2
);
1389 EXPORT_SYMBOL(ssb_admatch_base
);
1391 u32
ssb_admatch_size(u32 adm
)
1395 switch (adm
& SSB_ADM_TYPE
) {
1397 size
= ((adm
& SSB_ADM_SZ0
) >> SSB_ADM_SZ0_SHIFT
);
1400 SSB_WARN_ON(adm
& SSB_ADM_NEG
); /* unsupported */
1401 size
= ((adm
& SSB_ADM_SZ1
) >> SSB_ADM_SZ1_SHIFT
);
1404 SSB_WARN_ON(adm
& SSB_ADM_NEG
); /* unsupported */
1405 size
= ((adm
& SSB_ADM_SZ2
) >> SSB_ADM_SZ2_SHIFT
);
1410 size
= (1 << (size
+ 1));
1414 EXPORT_SYMBOL(ssb_admatch_size
);
1416 static int __init
ssb_modinit(void)
1420 /* See the comment at the ssb_is_early_boot definition */
1421 ssb_is_early_boot
= 0;
1422 err
= bus_register(&ssb_bustype
);
1426 /* Maybe we already registered some buses at early boot.
1427 * Check for this and attach them
1430 err
= ssb_attach_queued_buses();
1433 bus_unregister(&ssb_bustype
);
1437 err
= b43_pci_ssb_bridge_init();
1439 ssb_printk(KERN_ERR
"Broadcom 43xx PCI-SSB-bridge "
1440 "initialization failed\n");
1441 /* don't fail SSB init because of this */
1444 err
= ssb_gige_init();
1446 ssb_printk(KERN_ERR
"SSB Broadcom Gigabit Ethernet "
1447 "driver initialization failed\n");
1448 /* don't fail SSB init because of this */
1454 /* ssb must be initialized after PCI but before the ssb drivers.
1455 * That means we must use some initcall between subsys_initcall
1456 * and device_initcall. */
1457 fs_initcall(ssb_modinit
);
1459 static void __exit
ssb_modexit(void)
1462 b43_pci_ssb_bridge_exit();
1463 bus_unregister(&ssb_bustype
);
1465 module_exit(ssb_modexit
)