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/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/ssb/ssb.h>
18 #include <linux/ssb/ssb_regs.h>
19 #include <linux/ssb/ssb_driver_gige.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/pci.h>
22 #include <linux/mmc/sdio_func.h>
23 #include <linux/slab.h>
25 #include <pcmcia/cistpl.h>
26 #include <pcmcia/ds.h>
29 MODULE_DESCRIPTION("Sonics Silicon Backplane driver");
30 MODULE_LICENSE("GPL");
33 /* Temporary list of yet-to-be-attached buses */
34 static LIST_HEAD(attach_queue
);
35 /* List if running buses */
36 static LIST_HEAD(buses
);
37 /* Software ID counter */
38 static unsigned int next_busnumber
;
39 /* buses_mutes locks the two buslists and the next_busnumber.
40 * Don't lock this directly, but use ssb_buses_[un]lock() below.
42 static DEFINE_MUTEX(buses_mutex
);
44 /* There are differences in the codeflow, if the bus is
45 * initialized from early boot, as various needed services
46 * are not available early. This is a mechanism to delay
47 * these initializations to after early boot has finished.
48 * It's also used to avoid mutex locking, as that's not
49 * available and needed early.
51 static bool ssb_is_early_boot
= 1;
53 static void ssb_buses_lock(void);
54 static void ssb_buses_unlock(void);
57 #ifdef CONFIG_SSB_PCIHOST
58 struct ssb_bus
*ssb_pci_dev_to_bus(struct pci_dev
*pdev
)
63 list_for_each_entry(bus
, &buses
, list
) {
64 if (bus
->bustype
== SSB_BUSTYPE_PCI
&&
65 bus
->host_pci
== pdev
)
74 #endif /* CONFIG_SSB_PCIHOST */
76 #ifdef CONFIG_SSB_PCMCIAHOST
77 struct ssb_bus
*ssb_pcmcia_dev_to_bus(struct pcmcia_device
*pdev
)
82 list_for_each_entry(bus
, &buses
, list
) {
83 if (bus
->bustype
== SSB_BUSTYPE_PCMCIA
&&
84 bus
->host_pcmcia
== pdev
)
93 #endif /* CONFIG_SSB_PCMCIAHOST */
95 int ssb_for_each_bus_call(unsigned long data
,
96 int (*func
)(struct ssb_bus
*bus
, unsigned long data
))
102 list_for_each_entry(bus
, &buses
, list
) {
103 res
= func(bus
, data
);
114 static struct ssb_device
*ssb_device_get(struct ssb_device
*dev
)
117 get_device(dev
->dev
);
121 static void ssb_device_put(struct ssb_device
*dev
)
124 put_device(dev
->dev
);
127 static int ssb_device_resume(struct device
*dev
)
129 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
130 struct ssb_driver
*ssb_drv
;
134 ssb_drv
= drv_to_ssb_drv(dev
->driver
);
135 if (ssb_drv
&& ssb_drv
->resume
)
136 err
= ssb_drv
->resume(ssb_dev
);
144 static int ssb_device_suspend(struct device
*dev
, pm_message_t state
)
146 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
147 struct ssb_driver
*ssb_drv
;
151 ssb_drv
= drv_to_ssb_drv(dev
->driver
);
152 if (ssb_drv
&& ssb_drv
->suspend
)
153 err
= ssb_drv
->suspend(ssb_dev
, state
);
161 int ssb_bus_resume(struct ssb_bus
*bus
)
165 /* Reset HW state information in memory, so that HW is
166 * completely reinitialized.
168 bus
->mapped_device
= NULL
;
169 #ifdef CONFIG_SSB_DRIVER_PCICORE
170 bus
->pcicore
.setup_done
= 0;
173 err
= ssb_bus_powerup(bus
, 0);
176 err
= ssb_pcmcia_hardware_setup(bus
);
178 ssb_bus_may_powerdown(bus
);
181 ssb_chipco_resume(&bus
->chipco
);
182 ssb_bus_may_powerdown(bus
);
186 EXPORT_SYMBOL(ssb_bus_resume
);
188 int ssb_bus_suspend(struct ssb_bus
*bus
)
190 ssb_chipco_suspend(&bus
->chipco
);
191 ssb_pci_xtal(bus
, SSB_GPIO_XTAL
| SSB_GPIO_PLL
, 0);
195 EXPORT_SYMBOL(ssb_bus_suspend
);
197 #ifdef CONFIG_SSB_SPROM
198 /** ssb_devices_freeze - Freeze all devices on the bus.
200 * After freezing no device driver will be handling a device
201 * on this bus anymore. ssb_devices_thaw() must be called after
202 * a successful freeze to reactivate the devices.
205 * @ctx: Context structure. Pass this to ssb_devices_thaw().
207 int ssb_devices_freeze(struct ssb_bus
*bus
, struct ssb_freeze_context
*ctx
)
209 struct ssb_device
*sdev
;
210 struct ssb_driver
*sdrv
;
213 memset(ctx
, 0, sizeof(*ctx
));
215 WARN_ON(bus
->nr_devices
> ARRAY_SIZE(ctx
->device_frozen
));
217 for (i
= 0; i
< bus
->nr_devices
; i
++) {
218 sdev
= ssb_device_get(&bus
->devices
[i
]);
220 if (!sdev
->dev
|| !sdev
->dev
->driver
||
221 !device_is_registered(sdev
->dev
)) {
222 ssb_device_put(sdev
);
225 sdrv
= drv_to_ssb_drv(sdev
->dev
->driver
);
226 if (WARN_ON(!sdrv
->remove
))
229 ctx
->device_frozen
[i
] = 1;
235 /** ssb_devices_thaw - Unfreeze all devices on the bus.
237 * This will re-attach the device drivers and re-init the devices.
239 * @ctx: The context structure from ssb_devices_freeze()
241 int ssb_devices_thaw(struct ssb_freeze_context
*ctx
)
243 struct ssb_bus
*bus
= ctx
->bus
;
244 struct ssb_device
*sdev
;
245 struct ssb_driver
*sdrv
;
249 for (i
= 0; i
< bus
->nr_devices
; i
++) {
250 if (!ctx
->device_frozen
[i
])
252 sdev
= &bus
->devices
[i
];
254 if (WARN_ON(!sdev
->dev
|| !sdev
->dev
->driver
))
256 sdrv
= drv_to_ssb_drv(sdev
->dev
->driver
);
257 if (WARN_ON(!sdrv
|| !sdrv
->probe
))
260 err
= sdrv
->probe(sdev
, &sdev
->id
);
263 "Failed to thaw device %s\n",
264 dev_name(sdev
->dev
));
267 ssb_device_put(sdev
);
272 #endif /* CONFIG_SSB_SPROM */
274 static void ssb_device_shutdown(struct device
*dev
)
276 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
277 struct ssb_driver
*ssb_drv
;
281 ssb_drv
= drv_to_ssb_drv(dev
->driver
);
282 if (ssb_drv
&& ssb_drv
->shutdown
)
283 ssb_drv
->shutdown(ssb_dev
);
286 static void ssb_device_remove(struct device
*dev
)
288 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
289 struct ssb_driver
*ssb_drv
= drv_to_ssb_drv(dev
->driver
);
291 if (ssb_drv
&& ssb_drv
->remove
)
292 ssb_drv
->remove(ssb_dev
);
293 ssb_device_put(ssb_dev
);
296 static int ssb_device_probe(struct device
*dev
)
298 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
299 struct ssb_driver
*ssb_drv
= drv_to_ssb_drv(dev
->driver
);
302 ssb_device_get(ssb_dev
);
303 if (ssb_drv
&& ssb_drv
->probe
)
304 err
= ssb_drv
->probe(ssb_dev
, &ssb_dev
->id
);
306 ssb_device_put(ssb_dev
);
311 static int ssb_match_devid(const struct ssb_device_id
*tabid
,
312 const struct ssb_device_id
*devid
)
314 if ((tabid
->vendor
!= devid
->vendor
) &&
315 tabid
->vendor
!= SSB_ANY_VENDOR
)
317 if ((tabid
->coreid
!= devid
->coreid
) &&
318 tabid
->coreid
!= SSB_ANY_ID
)
320 if ((tabid
->revision
!= devid
->revision
) &&
321 tabid
->revision
!= SSB_ANY_REV
)
326 static int ssb_bus_match(struct device
*dev
, const struct device_driver
*drv
)
328 struct ssb_device
*ssb_dev
= dev_to_ssb_dev(dev
);
329 const struct ssb_driver
*ssb_drv
= drv_to_ssb_drv(drv
);
330 const struct ssb_device_id
*id
;
332 for (id
= ssb_drv
->id_table
;
333 id
->vendor
|| id
->coreid
|| id
->revision
;
335 if (ssb_match_devid(id
, &ssb_dev
->id
))
336 return 1; /* found */
342 static int ssb_device_uevent(const struct device
*dev
, struct kobj_uevent_env
*env
)
344 const struct ssb_device
*ssb_dev
;
349 ssb_dev
= dev_to_ssb_dev(dev
);
351 return add_uevent_var(env
,
352 "MODALIAS=ssb:v%04Xid%04Xrev%02X",
353 ssb_dev
->id
.vendor
, ssb_dev
->id
.coreid
,
354 ssb_dev
->id
.revision
);
357 #define ssb_config_attr(attrib, field, format_string) \
359 attrib##_show(struct device *dev, struct device_attribute *attr, char *buf) \
361 return sprintf(buf, format_string, dev_to_ssb_dev(dev)->field); \
363 static DEVICE_ATTR_RO(attrib);
365 ssb_config_attr(core_num
, core_index
, "%u\n")
366 ssb_config_attr(coreid
, id
.coreid
, "0x%04x\n")
367 ssb_config_attr(vendor
, id
.vendor
, "0x%04x\n")
368 ssb_config_attr(revision
, id
.revision
, "%u\n")
369 ssb_config_attr(irq
, irq
, "%u\n")
371 name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
373 return sprintf(buf
, "%s\n",
374 ssb_core_name(dev_to_ssb_dev(dev
)->id
.coreid
));
376 static DEVICE_ATTR_RO(name
);
378 static struct attribute
*ssb_device_attrs
[] = {
380 &dev_attr_core_num
.attr
,
381 &dev_attr_coreid
.attr
,
382 &dev_attr_vendor
.attr
,
383 &dev_attr_revision
.attr
,
387 ATTRIBUTE_GROUPS(ssb_device
);
389 static const struct bus_type ssb_bustype
= {
391 .match
= ssb_bus_match
,
392 .probe
= ssb_device_probe
,
393 .remove
= ssb_device_remove
,
394 .shutdown
= ssb_device_shutdown
,
395 .suspend
= ssb_device_suspend
,
396 .resume
= ssb_device_resume
,
397 .uevent
= ssb_device_uevent
,
398 .dev_groups
= ssb_device_groups
,
401 static void ssb_buses_lock(void)
403 /* See the comment at the ssb_is_early_boot definition */
404 if (!ssb_is_early_boot
)
405 mutex_lock(&buses_mutex
);
408 static void ssb_buses_unlock(void)
410 /* See the comment at the ssb_is_early_boot definition */
411 if (!ssb_is_early_boot
)
412 mutex_unlock(&buses_mutex
);
415 static void ssb_devices_unregister(struct ssb_bus
*bus
)
417 struct ssb_device
*sdev
;
420 for (i
= bus
->nr_devices
- 1; i
>= 0; i
--) {
421 sdev
= &(bus
->devices
[i
]);
423 device_unregister(sdev
->dev
);
426 #ifdef CONFIG_SSB_EMBEDDED
427 if (bus
->bustype
== SSB_BUSTYPE_SSB
)
428 platform_device_unregister(bus
->watchdog
);
432 void ssb_bus_unregister(struct ssb_bus
*bus
)
436 err
= ssb_gpio_unregister(bus
);
438 pr_debug("Can not unregister GPIO driver: %i\n", err
);
441 ssb_devices_unregister(bus
);
442 list_del(&bus
->list
);
445 ssb_pcmcia_exit(bus
);
449 EXPORT_SYMBOL(ssb_bus_unregister
);
451 static void ssb_release_dev(struct device
*dev
)
453 struct __ssb_dev_wrapper
*devwrap
;
455 devwrap
= container_of(dev
, struct __ssb_dev_wrapper
, dev
);
459 static int ssb_devices_register(struct ssb_bus
*bus
)
461 struct ssb_device
*sdev
;
463 struct __ssb_dev_wrapper
*devwrap
;
467 for (i
= 0; i
< bus
->nr_devices
; i
++) {
468 sdev
= &(bus
->devices
[i
]);
470 /* We don't register SSB-system devices to the kernel,
471 * as the drivers for them are built into SSB.
473 switch (sdev
->id
.coreid
) {
474 case SSB_DEV_CHIPCOMMON
:
479 case SSB_DEV_MIPS_3302
:
484 devwrap
= kzalloc(sizeof(*devwrap
), GFP_KERNEL
);
490 devwrap
->sdev
= sdev
;
492 dev
->release
= ssb_release_dev
;
493 dev
->bus
= &ssb_bustype
;
494 dev_set_name(dev
, "ssb%u:%d", bus
->busnumber
, dev_idx
);
496 switch (bus
->bustype
) {
497 case SSB_BUSTYPE_PCI
:
498 #ifdef CONFIG_SSB_PCIHOST
499 sdev
->irq
= bus
->host_pci
->irq
;
500 dev
->parent
= &bus
->host_pci
->dev
;
501 sdev
->dma_dev
= dev
->parent
;
504 case SSB_BUSTYPE_PCMCIA
:
505 #ifdef CONFIG_SSB_PCMCIAHOST
506 sdev
->irq
= bus
->host_pcmcia
->irq
;
507 dev
->parent
= &bus
->host_pcmcia
->dev
;
510 case SSB_BUSTYPE_SDIO
:
511 #ifdef CONFIG_SSB_SDIOHOST
512 dev
->parent
= &bus
->host_sdio
->dev
;
515 case SSB_BUSTYPE_SSB
:
516 dev
->dma_mask
= &dev
->coherent_dma_mask
;
522 err
= device_register(dev
);
524 pr_err("Could not register %s\n", dev_name(dev
));
525 /* Set dev to NULL to not unregister
526 * dev on error unwinding.
535 #ifdef CONFIG_SSB_DRIVER_MIPS
536 if (bus
->mipscore
.pflash
.present
) {
537 err
= platform_device_register(&ssb_pflash_dev
);
539 pr_err("Error registering parallel flash\n");
543 #ifdef CONFIG_SSB_SFLASH
544 if (bus
->mipscore
.sflash
.present
) {
545 err
= platform_device_register(&ssb_sflash_dev
);
547 pr_err("Error registering serial flash\n");
553 /* Unwind the already registered devices. */
554 ssb_devices_unregister(bus
);
558 /* Needs ssb_buses_lock() */
559 static int ssb_attach_queued_buses(void)
561 struct ssb_bus
*bus
, *n
;
563 int drop_them_all
= 0;
565 list_for_each_entry_safe(bus
, n
, &attach_queue
, list
) {
567 list_del(&bus
->list
);
570 /* Can't init the PCIcore in ssb_bus_register(), as that
571 * is too early in boot for embedded systems
572 * (no udelay() available). So do it here in attach stage.
574 err
= ssb_bus_powerup(bus
, 0);
577 ssb_pcicore_init(&bus
->pcicore
);
578 if (bus
->bustype
== SSB_BUSTYPE_SSB
)
579 ssb_watchdog_register(bus
);
581 err
= ssb_gpio_init(bus
);
582 if (err
== -ENOTSUPP
)
583 pr_debug("GPIO driver not activated\n");
585 pr_debug("Error registering GPIO driver: %i\n", err
);
587 ssb_bus_may_powerdown(bus
);
589 err
= ssb_devices_register(bus
);
593 list_del(&bus
->list
);
596 list_move_tail(&bus
->list
, &buses
);
602 static int ssb_fetch_invariants(struct ssb_bus
*bus
,
603 ssb_invariants_func_t get_invariants
)
605 struct ssb_init_invariants iv
;
608 memset(&iv
, 0, sizeof(iv
));
609 err
= get_invariants(bus
, &iv
);
612 memcpy(&bus
->boardinfo
, &iv
.boardinfo
, sizeof(iv
.boardinfo
));
613 memcpy(&bus
->sprom
, &iv
.sprom
, sizeof(iv
.sprom
));
614 bus
->has_cardbus_slot
= iv
.has_cardbus_slot
;
619 static int __maybe_unused
620 ssb_bus_register(struct ssb_bus
*bus
,
621 ssb_invariants_func_t get_invariants
,
622 unsigned long baseaddr
)
626 spin_lock_init(&bus
->bar_lock
);
627 INIT_LIST_HEAD(&bus
->list
);
628 #ifdef CONFIG_SSB_EMBEDDED
629 spin_lock_init(&bus
->gpio_lock
);
632 /* Powerup the bus */
633 err
= ssb_pci_xtal(bus
, SSB_GPIO_XTAL
| SSB_GPIO_PLL
, 1);
637 /* Init SDIO-host device (if any), before the scan */
638 err
= ssb_sdio_init(bus
);
640 goto err_disable_xtal
;
643 bus
->busnumber
= next_busnumber
;
644 /* Scan for devices (cores) */
645 err
= ssb_bus_scan(bus
, baseaddr
);
649 /* Init PCI-host device (if any) */
650 err
= ssb_pci_init(bus
);
653 /* Init PCMCIA-host device (if any) */
654 err
= ssb_pcmcia_init(bus
);
658 /* Initialize basic system devices (if available) */
659 err
= ssb_bus_powerup(bus
, 0);
661 goto err_pcmcia_exit
;
662 ssb_chipcommon_init(&bus
->chipco
);
663 ssb_extif_init(&bus
->extif
);
664 ssb_mipscore_init(&bus
->mipscore
);
665 err
= ssb_fetch_invariants(bus
, get_invariants
);
667 ssb_bus_may_powerdown(bus
);
668 goto err_pcmcia_exit
;
670 ssb_bus_may_powerdown(bus
);
672 /* Queue it for attach.
673 * See the comment at the ssb_is_early_boot definition.
675 list_add_tail(&bus
->list
, &attach_queue
);
676 if (!ssb_is_early_boot
) {
677 /* This is not early boot, so we must attach the bus now */
678 err
= ssb_attach_queued_buses();
689 list_del(&bus
->list
);
691 ssb_pcmcia_exit(bus
);
700 ssb_pci_xtal(bus
, SSB_GPIO_XTAL
| SSB_GPIO_PLL
, 0);
704 #ifdef CONFIG_SSB_PCIHOST
705 int ssb_bus_pcibus_register(struct ssb_bus
*bus
, struct pci_dev
*host_pci
)
709 bus
->bustype
= SSB_BUSTYPE_PCI
;
710 bus
->host_pci
= host_pci
;
711 bus
->ops
= &ssb_pci_ops
;
713 err
= ssb_bus_register(bus
, ssb_pci_get_invariants
, 0);
715 dev_info(&host_pci
->dev
,
716 "Sonics Silicon Backplane found on PCI device %s\n",
717 dev_name(&host_pci
->dev
));
719 dev_err(&host_pci
->dev
,
720 "Failed to register PCI version of SSB with error %d\n",
726 #endif /* CONFIG_SSB_PCIHOST */
728 #ifdef CONFIG_SSB_PCMCIAHOST
729 int ssb_bus_pcmciabus_register(struct ssb_bus
*bus
,
730 struct pcmcia_device
*pcmcia_dev
,
731 unsigned long baseaddr
)
735 bus
->bustype
= SSB_BUSTYPE_PCMCIA
;
736 bus
->host_pcmcia
= pcmcia_dev
;
737 bus
->ops
= &ssb_pcmcia_ops
;
739 err
= ssb_bus_register(bus
, ssb_pcmcia_get_invariants
, baseaddr
);
741 dev_info(&pcmcia_dev
->dev
,
742 "Sonics Silicon Backplane found on PCMCIA device %s\n",
743 pcmcia_dev
->devname
);
748 #endif /* CONFIG_SSB_PCMCIAHOST */
750 #ifdef CONFIG_SSB_SDIOHOST
751 int ssb_bus_sdiobus_register(struct ssb_bus
*bus
, struct sdio_func
*func
,
756 bus
->bustype
= SSB_BUSTYPE_SDIO
;
757 bus
->host_sdio
= func
;
758 bus
->ops
= &ssb_sdio_ops
;
759 bus
->quirks
= quirks
;
761 err
= ssb_bus_register(bus
, ssb_sdio_get_invariants
, ~0);
764 "Sonics Silicon Backplane found on SDIO device %s\n",
770 EXPORT_SYMBOL(ssb_bus_sdiobus_register
);
771 #endif /* CONFIG_SSB_PCMCIAHOST */
773 #ifdef CONFIG_SSB_HOST_SOC
774 int ssb_bus_host_soc_register(struct ssb_bus
*bus
, unsigned long baseaddr
)
778 bus
->bustype
= SSB_BUSTYPE_SSB
;
779 bus
->ops
= &ssb_host_soc_ops
;
781 err
= ssb_bus_register(bus
, ssb_host_soc_get_invariants
, baseaddr
);
783 pr_info("Sonics Silicon Backplane found at address 0x%08lX\n",
791 int __ssb_driver_register(struct ssb_driver
*drv
, struct module
*owner
)
793 drv
->drv
.name
= drv
->name
;
794 drv
->drv
.bus
= &ssb_bustype
;
795 drv
->drv
.owner
= owner
;
797 return driver_register(&drv
->drv
);
799 EXPORT_SYMBOL(__ssb_driver_register
);
801 void ssb_driver_unregister(struct ssb_driver
*drv
)
803 driver_unregister(&drv
->drv
);
805 EXPORT_SYMBOL(ssb_driver_unregister
);
807 void ssb_set_devtypedata(struct ssb_device
*dev
, void *data
)
809 struct ssb_bus
*bus
= dev
->bus
;
810 struct ssb_device
*ent
;
813 for (i
= 0; i
< bus
->nr_devices
; i
++) {
814 ent
= &(bus
->devices
[i
]);
815 if (ent
->id
.vendor
!= dev
->id
.vendor
)
817 if (ent
->id
.coreid
!= dev
->id
.coreid
)
820 ent
->devtypedata
= data
;
823 EXPORT_SYMBOL(ssb_set_devtypedata
);
825 static u32
clkfactor_f6_resolve(u32 v
)
827 /* map the magic values */
829 case SSB_CHIPCO_CLK_F6_2
:
831 case SSB_CHIPCO_CLK_F6_3
:
833 case SSB_CHIPCO_CLK_F6_4
:
835 case SSB_CHIPCO_CLK_F6_5
:
837 case SSB_CHIPCO_CLK_F6_6
:
839 case SSB_CHIPCO_CLK_F6_7
:
845 /* Calculate the speed the backplane would run at a given set of clockcontrol values */
846 u32
ssb_calc_clock_rate(u32 plltype
, u32 n
, u32 m
)
848 u32 n1
, n2
, clock
, m1
, m2
, m3
, mc
;
850 n1
= (n
& SSB_CHIPCO_CLK_N1
);
851 n2
= ((n
& SSB_CHIPCO_CLK_N2
) >> SSB_CHIPCO_CLK_N2_SHIFT
);
854 case SSB_PLLTYPE_6
: /* 100/200 or 120/240 only */
855 if (m
& SSB_CHIPCO_CLK_T6_MMASK
)
856 return SSB_CHIPCO_CLK_T6_M1
;
857 return SSB_CHIPCO_CLK_T6_M0
;
858 case SSB_PLLTYPE_1
: /* 48Mhz base, 3 dividers */
859 case SSB_PLLTYPE_3
: /* 25Mhz, 2 dividers */
860 case SSB_PLLTYPE_4
: /* 48Mhz, 4 dividers */
861 case SSB_PLLTYPE_7
: /* 25Mhz, 4 dividers */
862 n1
= clkfactor_f6_resolve(n1
);
863 n2
+= SSB_CHIPCO_CLK_F5_BIAS
;
865 case SSB_PLLTYPE_2
: /* 48Mhz, 4 dividers */
866 n1
+= SSB_CHIPCO_CLK_T2_BIAS
;
867 n2
+= SSB_CHIPCO_CLK_T2_BIAS
;
868 WARN_ON(!((n1
>= 2) && (n1
<= 7)));
869 WARN_ON(!((n2
>= 5) && (n2
<= 23)));
871 case SSB_PLLTYPE_5
: /* 25Mhz, 4 dividers */
878 case SSB_PLLTYPE_3
: /* 25Mhz, 2 dividers */
879 case SSB_PLLTYPE_7
: /* 25Mhz, 4 dividers */
880 clock
= SSB_CHIPCO_CLK_BASE2
* n1
* n2
;
883 clock
= SSB_CHIPCO_CLK_BASE1
* n1
* n2
;
888 m1
= (m
& SSB_CHIPCO_CLK_M1
);
889 m2
= ((m
& SSB_CHIPCO_CLK_M2
) >> SSB_CHIPCO_CLK_M2_SHIFT
);
890 m3
= ((m
& SSB_CHIPCO_CLK_M3
) >> SSB_CHIPCO_CLK_M3_SHIFT
);
891 mc
= ((m
& SSB_CHIPCO_CLK_MC
) >> SSB_CHIPCO_CLK_MC_SHIFT
);
894 case SSB_PLLTYPE_1
: /* 48Mhz base, 3 dividers */
895 case SSB_PLLTYPE_3
: /* 25Mhz, 2 dividers */
896 case SSB_PLLTYPE_4
: /* 48Mhz, 4 dividers */
897 case SSB_PLLTYPE_7
: /* 25Mhz, 4 dividers */
898 m1
= clkfactor_f6_resolve(m1
);
899 if ((plltype
== SSB_PLLTYPE_1
) ||
900 (plltype
== SSB_PLLTYPE_3
))
901 m2
+= SSB_CHIPCO_CLK_F5_BIAS
;
903 m2
= clkfactor_f6_resolve(m2
);
904 m3
= clkfactor_f6_resolve(m3
);
907 case SSB_CHIPCO_CLK_MC_BYPASS
:
909 case SSB_CHIPCO_CLK_MC_M1
:
911 case SSB_CHIPCO_CLK_MC_M1M2
:
912 return (clock
/ (m1
* m2
));
913 case SSB_CHIPCO_CLK_MC_M1M2M3
:
914 return (clock
/ (m1
* m2
* m3
));
915 case SSB_CHIPCO_CLK_MC_M1M3
:
916 return (clock
/ (m1
* m3
));
920 m1
+= SSB_CHIPCO_CLK_T2_BIAS
;
921 m2
+= SSB_CHIPCO_CLK_T2M2_BIAS
;
922 m3
+= SSB_CHIPCO_CLK_T2_BIAS
;
923 WARN_ON(!((m1
>= 2) && (m1
<= 7)));
924 WARN_ON(!((m2
>= 3) && (m2
<= 10)));
925 WARN_ON(!((m3
>= 2) && (m3
<= 7)));
927 if (!(mc
& SSB_CHIPCO_CLK_T2MC_M1BYP
))
929 if (!(mc
& SSB_CHIPCO_CLK_T2MC_M2BYP
))
931 if (!(mc
& SSB_CHIPCO_CLK_T2MC_M3BYP
))
940 /* Get the current speed the backplane is running at */
941 u32
ssb_clockspeed(struct ssb_bus
*bus
)
945 u32 clkctl_n
, clkctl_m
;
947 if (bus
->chipco
.capabilities
& SSB_CHIPCO_CAP_PMU
)
948 return ssb_pmu_get_controlclock(&bus
->chipco
);
950 if (ssb_extif_available(&bus
->extif
))
951 ssb_extif_get_clockcontrol(&bus
->extif
, &plltype
,
952 &clkctl_n
, &clkctl_m
);
953 else if (bus
->chipco
.dev
)
954 ssb_chipco_get_clockcontrol(&bus
->chipco
, &plltype
,
955 &clkctl_n
, &clkctl_m
);
959 if (bus
->chip_id
== 0x5365) {
962 rate
= ssb_calc_clock_rate(plltype
, clkctl_n
, clkctl_m
);
963 if (plltype
== SSB_PLLTYPE_3
) /* 25Mhz, 2 dividers */
969 EXPORT_SYMBOL(ssb_clockspeed
);
971 static u32
ssb_tmslow_reject_bitmask(struct ssb_device
*dev
)
973 u32 rev
= ssb_read32(dev
, SSB_IDLOW
) & SSB_IDLOW_SSBREV
;
975 /* The REJECT bit seems to be different for Backplane rev 2.3 */
977 case SSB_IDLOW_SSBREV_22
:
978 case SSB_IDLOW_SSBREV_24
:
979 case SSB_IDLOW_SSBREV_26
:
980 return SSB_TMSLOW_REJECT
;
981 case SSB_IDLOW_SSBREV_23
:
982 return SSB_TMSLOW_REJECT_23
;
983 case SSB_IDLOW_SSBREV_25
: /* TODO - find the proper REJECT bit */
984 case SSB_IDLOW_SSBREV_27
: /* same here */
985 return SSB_TMSLOW_REJECT
; /* this is a guess */
986 case SSB_IDLOW_SSBREV
:
989 WARN(1, KERN_INFO
"ssb: Backplane Revision 0x%.8X\n", rev
);
991 return (SSB_TMSLOW_REJECT
| SSB_TMSLOW_REJECT_23
);
994 int ssb_device_is_enabled(struct ssb_device
*dev
)
999 reject
= ssb_tmslow_reject_bitmask(dev
);
1000 val
= ssb_read32(dev
, SSB_TMSLOW
);
1001 val
&= SSB_TMSLOW_CLOCK
| SSB_TMSLOW_RESET
| reject
;
1003 return (val
== SSB_TMSLOW_CLOCK
);
1005 EXPORT_SYMBOL(ssb_device_is_enabled
);
1007 static void ssb_flush_tmslow(struct ssb_device
*dev
)
1009 /* Make _really_ sure the device has finished the TMSLOW
1010 * register write transaction, as we risk running into
1011 * a machine check exception otherwise.
1012 * Do this by reading the register back to commit the
1013 * PCI write and delay an additional usec for the device
1014 * to react to the change.
1016 ssb_read32(dev
, SSB_TMSLOW
);
1020 void ssb_device_enable(struct ssb_device
*dev
, u32 core_specific_flags
)
1024 ssb_device_disable(dev
, core_specific_flags
);
1025 ssb_write32(dev
, SSB_TMSLOW
,
1026 SSB_TMSLOW_RESET
| SSB_TMSLOW_CLOCK
|
1027 SSB_TMSLOW_FGC
| core_specific_flags
);
1028 ssb_flush_tmslow(dev
);
1030 /* Clear SERR if set. This is a hw bug workaround. */
1031 if (ssb_read32(dev
, SSB_TMSHIGH
) & SSB_TMSHIGH_SERR
)
1032 ssb_write32(dev
, SSB_TMSHIGH
, 0);
1034 val
= ssb_read32(dev
, SSB_IMSTATE
);
1035 if (val
& (SSB_IMSTATE_IBE
| SSB_IMSTATE_TO
)) {
1036 val
&= ~(SSB_IMSTATE_IBE
| SSB_IMSTATE_TO
);
1037 ssb_write32(dev
, SSB_IMSTATE
, val
);
1040 ssb_write32(dev
, SSB_TMSLOW
,
1041 SSB_TMSLOW_CLOCK
| SSB_TMSLOW_FGC
|
1042 core_specific_flags
);
1043 ssb_flush_tmslow(dev
);
1045 ssb_write32(dev
, SSB_TMSLOW
, SSB_TMSLOW_CLOCK
|
1046 core_specific_flags
);
1047 ssb_flush_tmslow(dev
);
1049 EXPORT_SYMBOL(ssb_device_enable
);
1051 /* Wait for bitmask in a register to get set or cleared.
1052 * timeout is in units of ten-microseconds
1054 static int ssb_wait_bits(struct ssb_device
*dev
, u16 reg
, u32 bitmask
,
1055 int timeout
, int set
)
1060 for (i
= 0; i
< timeout
; i
++) {
1061 val
= ssb_read32(dev
, reg
);
1063 if ((val
& bitmask
) == bitmask
)
1066 if (!(val
& bitmask
))
1072 "Timeout waiting for bitmask %08X on register %04X to %s\n",
1073 bitmask
, reg
, set
? "set" : "clear");
1078 void ssb_device_disable(struct ssb_device
*dev
, u32 core_specific_flags
)
1082 if (ssb_read32(dev
, SSB_TMSLOW
) & SSB_TMSLOW_RESET
)
1085 reject
= ssb_tmslow_reject_bitmask(dev
);
1087 if (ssb_read32(dev
, SSB_TMSLOW
) & SSB_TMSLOW_CLOCK
) {
1088 ssb_write32(dev
, SSB_TMSLOW
, reject
| SSB_TMSLOW_CLOCK
);
1089 ssb_wait_bits(dev
, SSB_TMSLOW
, reject
, 1000, 1);
1090 ssb_wait_bits(dev
, SSB_TMSHIGH
, SSB_TMSHIGH_BUSY
, 1000, 0);
1092 if (ssb_read32(dev
, SSB_IDLOW
) & SSB_IDLOW_INITIATOR
) {
1093 val
= ssb_read32(dev
, SSB_IMSTATE
);
1094 val
|= SSB_IMSTATE_REJECT
;
1095 ssb_write32(dev
, SSB_IMSTATE
, val
);
1096 ssb_wait_bits(dev
, SSB_IMSTATE
, SSB_IMSTATE_BUSY
, 1000,
1100 ssb_write32(dev
, SSB_TMSLOW
,
1101 SSB_TMSLOW_FGC
| SSB_TMSLOW_CLOCK
|
1102 reject
| SSB_TMSLOW_RESET
|
1103 core_specific_flags
);
1104 ssb_flush_tmslow(dev
);
1106 if (ssb_read32(dev
, SSB_IDLOW
) & SSB_IDLOW_INITIATOR
) {
1107 val
= ssb_read32(dev
, SSB_IMSTATE
);
1108 val
&= ~SSB_IMSTATE_REJECT
;
1109 ssb_write32(dev
, SSB_IMSTATE
, val
);
1113 ssb_write32(dev
, SSB_TMSLOW
,
1114 reject
| SSB_TMSLOW_RESET
|
1115 core_specific_flags
);
1116 ssb_flush_tmslow(dev
);
1118 EXPORT_SYMBOL(ssb_device_disable
);
1120 /* Some chipsets need routing known for PCIe and 64-bit DMA */
1121 static bool ssb_dma_translation_special_bit(struct ssb_device
*dev
)
1123 u16 chip_id
= dev
->bus
->chip_id
;
1125 if (dev
->id
.coreid
== SSB_DEV_80211
) {
1126 return (chip_id
== 0x4322 || chip_id
== 43221 ||
1127 chip_id
== 43231 || chip_id
== 43222);
1133 u32
ssb_dma_translation(struct ssb_device
*dev
)
1135 switch (dev
->bus
->bustype
) {
1136 case SSB_BUSTYPE_SSB
:
1138 case SSB_BUSTYPE_PCI
:
1139 if (pci_is_pcie(dev
->bus
->host_pci
) &&
1140 ssb_read32(dev
, SSB_TMSHIGH
) & SSB_TMSHIGH_DMA64
) {
1141 return SSB_PCIE_DMA_H32
;
1143 if (ssb_dma_translation_special_bit(dev
))
1144 return SSB_PCIE_DMA_H32
;
1153 EXPORT_SYMBOL(ssb_dma_translation
);
1155 int ssb_bus_may_powerdown(struct ssb_bus
*bus
)
1157 struct ssb_chipcommon
*cc
;
1160 /* On buses where more than one core may be working
1161 * at a time, we must not powerdown stuff if there are
1162 * still cores that may want to run.
1164 if (bus
->bustype
== SSB_BUSTYPE_SSB
)
1171 if (cc
->dev
->id
.revision
< 5)
1174 ssb_chipco_set_clockmode(cc
, SSB_CLKMODE_SLOW
);
1175 err
= ssb_pci_xtal(bus
, SSB_GPIO_XTAL
| SSB_GPIO_PLL
, 0);
1179 bus
->powered_up
= 0;
1182 pr_err("Bus powerdown failed\n");
1185 EXPORT_SYMBOL(ssb_bus_may_powerdown
);
1187 int ssb_bus_powerup(struct ssb_bus
*bus
, bool dynamic_pctl
)
1190 enum ssb_clkmode mode
;
1192 err
= ssb_pci_xtal(bus
, SSB_GPIO_XTAL
| SSB_GPIO_PLL
, 1);
1196 bus
->powered_up
= 1;
1198 mode
= dynamic_pctl
? SSB_CLKMODE_DYNAMIC
: SSB_CLKMODE_FAST
;
1199 ssb_chipco_set_clockmode(&bus
->chipco
, mode
);
1203 pr_err("Bus powerup failed\n");
1206 EXPORT_SYMBOL(ssb_bus_powerup
);
1208 static void ssb_broadcast_value(struct ssb_device
*dev
,
1209 u32 address
, u32 data
)
1211 #ifdef CONFIG_SSB_DRIVER_PCICORE
1212 /* This is used for both, PCI and ChipCommon core, so be careful. */
1213 BUILD_BUG_ON(SSB_PCICORE_BCAST_ADDR
!= SSB_CHIPCO_BCAST_ADDR
);
1214 BUILD_BUG_ON(SSB_PCICORE_BCAST_DATA
!= SSB_CHIPCO_BCAST_DATA
);
1217 ssb_write32(dev
, SSB_CHIPCO_BCAST_ADDR
, address
);
1218 ssb_read32(dev
, SSB_CHIPCO_BCAST_ADDR
); /* flush */
1219 ssb_write32(dev
, SSB_CHIPCO_BCAST_DATA
, data
);
1220 ssb_read32(dev
, SSB_CHIPCO_BCAST_DATA
); /* flush */
1223 void ssb_commit_settings(struct ssb_bus
*bus
)
1225 struct ssb_device
*dev
;
1227 #ifdef CONFIG_SSB_DRIVER_PCICORE
1228 dev
= bus
->chipco
.dev
? bus
->chipco
.dev
: bus
->pcicore
.dev
;
1230 dev
= bus
->chipco
.dev
;
1234 /* This forces an update of the cached registers. */
1235 ssb_broadcast_value(dev
, 0xFD8, 0);
1237 EXPORT_SYMBOL(ssb_commit_settings
);
1239 u32
ssb_admatch_base(u32 adm
)
1243 switch (adm
& SSB_ADM_TYPE
) {
1245 base
= (adm
& SSB_ADM_BASE0
);
1248 WARN_ON(adm
& SSB_ADM_NEG
); /* unsupported */
1249 base
= (adm
& SSB_ADM_BASE1
);
1252 WARN_ON(adm
& SSB_ADM_NEG
); /* unsupported */
1253 base
= (adm
& SSB_ADM_BASE2
);
1261 EXPORT_SYMBOL(ssb_admatch_base
);
1263 u32
ssb_admatch_size(u32 adm
)
1267 switch (adm
& SSB_ADM_TYPE
) {
1269 size
= ((adm
& SSB_ADM_SZ0
) >> SSB_ADM_SZ0_SHIFT
);
1272 WARN_ON(adm
& SSB_ADM_NEG
); /* unsupported */
1273 size
= ((adm
& SSB_ADM_SZ1
) >> SSB_ADM_SZ1_SHIFT
);
1276 WARN_ON(adm
& SSB_ADM_NEG
); /* unsupported */
1277 size
= ((adm
& SSB_ADM_SZ2
) >> SSB_ADM_SZ2_SHIFT
);
1282 size
= (1 << (size
+ 1));
1286 EXPORT_SYMBOL(ssb_admatch_size
);
1288 static int __init
ssb_modinit(void)
1292 /* See the comment at the ssb_is_early_boot definition */
1293 ssb_is_early_boot
= 0;
1294 err
= bus_register(&ssb_bustype
);
1298 /* Maybe we already registered some buses at early boot.
1299 * Check for this and attach them
1302 err
= ssb_attach_queued_buses();
1305 bus_unregister(&ssb_bustype
);
1309 err
= b43_pci_ssb_bridge_init();
1311 pr_err("Broadcom 43xx PCI-SSB-bridge initialization failed\n");
1312 /* don't fail SSB init because of this */
1314 err
= ssb_host_pcmcia_init();
1316 pr_err("PCMCIA host initialization failed\n");
1317 /* don't fail SSB init because of this */
1319 err
= ssb_gige_init();
1321 pr_err("SSB Broadcom Gigabit Ethernet driver initialization failed\n");
1322 /* don't fail SSB init because of this */
1328 /* ssb must be initialized after PCI but before the ssb drivers.
1329 * That means we must use some initcall between subsys_initcall
1330 * and device_initcall.
1332 fs_initcall(ssb_modinit
);
1334 static void __exit
ssb_modexit(void)
1337 ssb_host_pcmcia_exit();
1338 b43_pci_ssb_bridge_exit();
1339 bus_unregister(&ssb_bustype
);
1341 module_exit(ssb_modexit
)