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, struct device_driver *drv)
328 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
329 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 = dev_to_ssb_dev(dev);
349 return add_uevent_var(env,
350 "MODALIAS=ssb:v%04Xid%04Xrev%02X",
351 ssb_dev->id.vendor, ssb_dev->id.coreid,
352 ssb_dev->id.revision);
355 #define ssb_config_attr(attrib, field, format_string) \
357 attrib##_show(struct device *dev, struct device_attribute *attr, char *buf) \
359 return sprintf(buf, format_string, dev_to_ssb_dev(dev)->field); \
361 static DEVICE_ATTR_RO(attrib);
363 ssb_config_attr(core_num, core_index, "%u\n")
364 ssb_config_attr(coreid, id.coreid, "0x%04x\n")
365 ssb_config_attr(vendor, id.vendor, "0x%04x\n")
366 ssb_config_attr(revision, id.revision, "%u\n")
367 ssb_config_attr(irq, irq, "%u\n")
369 name_show(struct device *dev, struct device_attribute *attr, char *buf)
371 return sprintf(buf, "%s\n",
372 ssb_core_name(dev_to_ssb_dev(dev)->id.coreid));
374 static DEVICE_ATTR_RO(name);
376 static struct attribute *ssb_device_attrs[] = {
378 &dev_attr_core_num.attr,
379 &dev_attr_coreid.attr,
380 &dev_attr_vendor.attr,
381 &dev_attr_revision.attr,
385 ATTRIBUTE_GROUPS(ssb_device);
387 static struct bus_type ssb_bustype = {
389 .match = ssb_bus_match,
390 .probe = ssb_device_probe,
391 .remove = ssb_device_remove,
392 .shutdown = ssb_device_shutdown,
393 .suspend = ssb_device_suspend,
394 .resume = ssb_device_resume,
395 .uevent = ssb_device_uevent,
396 .dev_groups = ssb_device_groups,
399 static void ssb_buses_lock(void)
401 /* See the comment at the ssb_is_early_boot definition */
402 if (!ssb_is_early_boot)
403 mutex_lock(&buses_mutex);
406 static void ssb_buses_unlock(void)
408 /* See the comment at the ssb_is_early_boot definition */
409 if (!ssb_is_early_boot)
410 mutex_unlock(&buses_mutex);
413 static void ssb_devices_unregister(struct ssb_bus *bus)
415 struct ssb_device *sdev;
418 for (i = bus->nr_devices - 1; i >= 0; i--) {
419 sdev = &(bus->devices[i]);
421 device_unregister(sdev->dev);
424 #ifdef CONFIG_SSB_EMBEDDED
425 if (bus->bustype == SSB_BUSTYPE_SSB)
426 platform_device_unregister(bus->watchdog);
430 void ssb_bus_unregister(struct ssb_bus *bus)
434 err = ssb_gpio_unregister(bus);
436 pr_debug("Can not unregister GPIO driver: %i\n", err);
439 ssb_devices_unregister(bus);
440 list_del(&bus->list);
443 ssb_pcmcia_exit(bus);
447 EXPORT_SYMBOL(ssb_bus_unregister);
449 static void ssb_release_dev(struct device *dev)
451 struct __ssb_dev_wrapper *devwrap;
453 devwrap = container_of(dev, struct __ssb_dev_wrapper, dev);
457 static int ssb_devices_register(struct ssb_bus *bus)
459 struct ssb_device *sdev;
461 struct __ssb_dev_wrapper *devwrap;
465 for (i = 0; i < bus->nr_devices; i++) {
466 sdev = &(bus->devices[i]);
468 /* We don't register SSB-system devices to the kernel,
469 * as the drivers for them are built into SSB.
471 switch (sdev->id.coreid) {
472 case SSB_DEV_CHIPCOMMON:
477 case SSB_DEV_MIPS_3302:
482 devwrap = kzalloc(sizeof(*devwrap), GFP_KERNEL);
488 devwrap->sdev = sdev;
490 dev->release = ssb_release_dev;
491 dev->bus = &ssb_bustype;
492 dev_set_name(dev, "ssb%u:%d", bus->busnumber, dev_idx);
494 switch (bus->bustype) {
495 case SSB_BUSTYPE_PCI:
496 #ifdef CONFIG_SSB_PCIHOST
497 sdev->irq = bus->host_pci->irq;
498 dev->parent = &bus->host_pci->dev;
499 sdev->dma_dev = dev->parent;
502 case SSB_BUSTYPE_PCMCIA:
503 #ifdef CONFIG_SSB_PCMCIAHOST
504 sdev->irq = bus->host_pcmcia->irq;
505 dev->parent = &bus->host_pcmcia->dev;
508 case SSB_BUSTYPE_SDIO:
509 #ifdef CONFIG_SSB_SDIOHOST
510 dev->parent = &bus->host_sdio->dev;
513 case SSB_BUSTYPE_SSB:
514 dev->dma_mask = &dev->coherent_dma_mask;
520 err = device_register(dev);
522 pr_err("Could not register %s\n", dev_name(dev));
523 /* Set dev to NULL to not unregister
524 * dev on error unwinding.
533 #ifdef CONFIG_SSB_DRIVER_MIPS
534 if (bus->mipscore.pflash.present) {
535 err = platform_device_register(&ssb_pflash_dev);
537 pr_err("Error registering parallel flash\n");
541 #ifdef CONFIG_SSB_SFLASH
542 if (bus->mipscore.sflash.present) {
543 err = platform_device_register(&ssb_sflash_dev);
545 pr_err("Error registering serial flash\n");
551 /* Unwind the already registered devices. */
552 ssb_devices_unregister(bus);
556 /* Needs ssb_buses_lock() */
557 static int ssb_attach_queued_buses(void)
559 struct ssb_bus *bus, *n;
561 int drop_them_all = 0;
563 list_for_each_entry_safe(bus, n, &attach_queue, list) {
565 list_del(&bus->list);
568 /* Can't init the PCIcore in ssb_bus_register(), as that
569 * is too early in boot for embedded systems
570 * (no udelay() available). So do it here in attach stage.
572 err = ssb_bus_powerup(bus, 0);
575 ssb_pcicore_init(&bus->pcicore);
576 if (bus->bustype == SSB_BUSTYPE_SSB)
577 ssb_watchdog_register(bus);
579 err = ssb_gpio_init(bus);
580 if (err == -ENOTSUPP)
581 pr_debug("GPIO driver not activated\n");
583 pr_debug("Error registering GPIO driver: %i\n", err);
585 ssb_bus_may_powerdown(bus);
587 err = ssb_devices_register(bus);
591 list_del(&bus->list);
594 list_move_tail(&bus->list, &buses);
600 static int ssb_fetch_invariants(struct ssb_bus *bus,
601 ssb_invariants_func_t get_invariants)
603 struct ssb_init_invariants iv;
606 memset(&iv, 0, sizeof(iv));
607 err = get_invariants(bus, &iv);
610 memcpy(&bus->boardinfo, &iv.boardinfo, sizeof(iv.boardinfo));
611 memcpy(&bus->sprom, &iv.sprom, sizeof(iv.sprom));
612 bus->has_cardbus_slot = iv.has_cardbus_slot;
617 static int __maybe_unused
618 ssb_bus_register(struct ssb_bus *bus,
619 ssb_invariants_func_t get_invariants,
620 unsigned long baseaddr)
624 spin_lock_init(&bus->bar_lock);
625 INIT_LIST_HEAD(&bus->list);
626 #ifdef CONFIG_SSB_EMBEDDED
627 spin_lock_init(&bus->gpio_lock);
630 /* Powerup the bus */
631 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
635 /* Init SDIO-host device (if any), before the scan */
636 err = ssb_sdio_init(bus);
638 goto err_disable_xtal;
641 bus->busnumber = next_busnumber;
642 /* Scan for devices (cores) */
643 err = ssb_bus_scan(bus, baseaddr);
647 /* Init PCI-host device (if any) */
648 err = ssb_pci_init(bus);
651 /* Init PCMCIA-host device (if any) */
652 err = ssb_pcmcia_init(bus);
656 /* Initialize basic system devices (if available) */
657 err = ssb_bus_powerup(bus, 0);
659 goto err_pcmcia_exit;
660 ssb_chipcommon_init(&bus->chipco);
661 ssb_extif_init(&bus->extif);
662 ssb_mipscore_init(&bus->mipscore);
663 err = ssb_fetch_invariants(bus, get_invariants);
665 ssb_bus_may_powerdown(bus);
666 goto err_pcmcia_exit;
668 ssb_bus_may_powerdown(bus);
670 /* Queue it for attach.
671 * See the comment at the ssb_is_early_boot definition.
673 list_add_tail(&bus->list, &attach_queue);
674 if (!ssb_is_early_boot) {
675 /* This is not early boot, so we must attach the bus now */
676 err = ssb_attach_queued_buses();
687 list_del(&bus->list);
689 ssb_pcmcia_exit(bus);
698 ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
702 #ifdef CONFIG_SSB_PCIHOST
703 int ssb_bus_pcibus_register(struct ssb_bus *bus, struct pci_dev *host_pci)
707 bus->bustype = SSB_BUSTYPE_PCI;
708 bus->host_pci = host_pci;
709 bus->ops = &ssb_pci_ops;
711 err = ssb_bus_register(bus, ssb_pci_get_invariants, 0);
713 dev_info(&host_pci->dev,
714 "Sonics Silicon Backplane found on PCI device %s\n",
715 dev_name(&host_pci->dev));
717 dev_err(&host_pci->dev,
718 "Failed to register PCI version of SSB with error %d\n",
724 #endif /* CONFIG_SSB_PCIHOST */
726 #ifdef CONFIG_SSB_PCMCIAHOST
727 int ssb_bus_pcmciabus_register(struct ssb_bus *bus,
728 struct pcmcia_device *pcmcia_dev,
729 unsigned long baseaddr)
733 bus->bustype = SSB_BUSTYPE_PCMCIA;
734 bus->host_pcmcia = pcmcia_dev;
735 bus->ops = &ssb_pcmcia_ops;
737 err = ssb_bus_register(bus, ssb_pcmcia_get_invariants, baseaddr);
739 dev_info(&pcmcia_dev->dev,
740 "Sonics Silicon Backplane found on PCMCIA device %s\n",
741 pcmcia_dev->devname);
746 #endif /* CONFIG_SSB_PCMCIAHOST */
748 #ifdef CONFIG_SSB_SDIOHOST
749 int ssb_bus_sdiobus_register(struct ssb_bus *bus, struct sdio_func *func,
754 bus->bustype = SSB_BUSTYPE_SDIO;
755 bus->host_sdio = func;
756 bus->ops = &ssb_sdio_ops;
757 bus->quirks = quirks;
759 err = ssb_bus_register(bus, ssb_sdio_get_invariants, ~0);
762 "Sonics Silicon Backplane found on SDIO device %s\n",
768 EXPORT_SYMBOL(ssb_bus_sdiobus_register);
769 #endif /* CONFIG_SSB_PCMCIAHOST */
771 #ifdef CONFIG_SSB_HOST_SOC
772 int ssb_bus_host_soc_register(struct ssb_bus *bus, unsigned long baseaddr)
776 bus->bustype = SSB_BUSTYPE_SSB;
777 bus->ops = &ssb_host_soc_ops;
779 err = ssb_bus_register(bus, ssb_host_soc_get_invariants, baseaddr);
781 pr_info("Sonics Silicon Backplane found at address 0x%08lX\n",
789 int __ssb_driver_register(struct ssb_driver *drv, struct module *owner)
791 drv->drv.name = drv->name;
792 drv->drv.bus = &ssb_bustype;
793 drv->drv.owner = owner;
795 return driver_register(&drv->drv);
797 EXPORT_SYMBOL(__ssb_driver_register);
799 void ssb_driver_unregister(struct ssb_driver *drv)
801 driver_unregister(&drv->drv);
803 EXPORT_SYMBOL(ssb_driver_unregister);
805 void ssb_set_devtypedata(struct ssb_device *dev, void *data)
807 struct ssb_bus *bus = dev->bus;
808 struct ssb_device *ent;
811 for (i = 0; i < bus->nr_devices; i++) {
812 ent = &(bus->devices[i]);
813 if (ent->id.vendor != dev->id.vendor)
815 if (ent->id.coreid != dev->id.coreid)
818 ent->devtypedata = data;
821 EXPORT_SYMBOL(ssb_set_devtypedata);
823 static u32 clkfactor_f6_resolve(u32 v)
825 /* map the magic values */
827 case SSB_CHIPCO_CLK_F6_2:
829 case SSB_CHIPCO_CLK_F6_3:
831 case SSB_CHIPCO_CLK_F6_4:
833 case SSB_CHIPCO_CLK_F6_5:
835 case SSB_CHIPCO_CLK_F6_6:
837 case SSB_CHIPCO_CLK_F6_7:
843 /* Calculate the speed the backplane would run at a given set of clockcontrol values */
844 u32 ssb_calc_clock_rate(u32 plltype, u32 n, u32 m)
846 u32 n1, n2, clock, m1, m2, m3, mc;
848 n1 = (n & SSB_CHIPCO_CLK_N1);
849 n2 = ((n & SSB_CHIPCO_CLK_N2) >> SSB_CHIPCO_CLK_N2_SHIFT);
852 case SSB_PLLTYPE_6: /* 100/200 or 120/240 only */
853 if (m & SSB_CHIPCO_CLK_T6_MMASK)
854 return SSB_CHIPCO_CLK_T6_M1;
855 return SSB_CHIPCO_CLK_T6_M0;
856 case SSB_PLLTYPE_1: /* 48Mhz base, 3 dividers */
857 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
858 case SSB_PLLTYPE_4: /* 48Mhz, 4 dividers */
859 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
860 n1 = clkfactor_f6_resolve(n1);
861 n2 += SSB_CHIPCO_CLK_F5_BIAS;
863 case SSB_PLLTYPE_2: /* 48Mhz, 4 dividers */
864 n1 += SSB_CHIPCO_CLK_T2_BIAS;
865 n2 += SSB_CHIPCO_CLK_T2_BIAS;
866 WARN_ON(!((n1 >= 2) && (n1 <= 7)));
867 WARN_ON(!((n2 >= 5) && (n2 <= 23)));
869 case SSB_PLLTYPE_5: /* 25Mhz, 4 dividers */
876 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
877 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
878 clock = SSB_CHIPCO_CLK_BASE2 * n1 * n2;
881 clock = SSB_CHIPCO_CLK_BASE1 * n1 * n2;
886 m1 = (m & SSB_CHIPCO_CLK_M1);
887 m2 = ((m & SSB_CHIPCO_CLK_M2) >> SSB_CHIPCO_CLK_M2_SHIFT);
888 m3 = ((m & SSB_CHIPCO_CLK_M3) >> SSB_CHIPCO_CLK_M3_SHIFT);
889 mc = ((m & SSB_CHIPCO_CLK_MC) >> SSB_CHIPCO_CLK_MC_SHIFT);
892 case SSB_PLLTYPE_1: /* 48Mhz base, 3 dividers */
893 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
894 case SSB_PLLTYPE_4: /* 48Mhz, 4 dividers */
895 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
896 m1 = clkfactor_f6_resolve(m1);
897 if ((plltype == SSB_PLLTYPE_1) ||
898 (plltype == SSB_PLLTYPE_3))
899 m2 += SSB_CHIPCO_CLK_F5_BIAS;
901 m2 = clkfactor_f6_resolve(m2);
902 m3 = clkfactor_f6_resolve(m3);
905 case SSB_CHIPCO_CLK_MC_BYPASS:
907 case SSB_CHIPCO_CLK_MC_M1:
909 case SSB_CHIPCO_CLK_MC_M1M2:
910 return (clock / (m1 * m2));
911 case SSB_CHIPCO_CLK_MC_M1M2M3:
912 return (clock / (m1 * m2 * m3));
913 case SSB_CHIPCO_CLK_MC_M1M3:
914 return (clock / (m1 * m3));
918 m1 += SSB_CHIPCO_CLK_T2_BIAS;
919 m2 += SSB_CHIPCO_CLK_T2M2_BIAS;
920 m3 += SSB_CHIPCO_CLK_T2_BIAS;
921 WARN_ON(!((m1 >= 2) && (m1 <= 7)));
922 WARN_ON(!((m2 >= 3) && (m2 <= 10)));
923 WARN_ON(!((m3 >= 2) && (m3 <= 7)));
925 if (!(mc & SSB_CHIPCO_CLK_T2MC_M1BYP))
927 if (!(mc & SSB_CHIPCO_CLK_T2MC_M2BYP))
929 if (!(mc & SSB_CHIPCO_CLK_T2MC_M3BYP))
938 /* Get the current speed the backplane is running at */
939 u32 ssb_clockspeed(struct ssb_bus *bus)
943 u32 clkctl_n, clkctl_m;
945 if (bus->chipco.capabilities & SSB_CHIPCO_CAP_PMU)
946 return ssb_pmu_get_controlclock(&bus->chipco);
948 if (ssb_extif_available(&bus->extif))
949 ssb_extif_get_clockcontrol(&bus->extif, &plltype,
950 &clkctl_n, &clkctl_m);
951 else if (bus->chipco.dev)
952 ssb_chipco_get_clockcontrol(&bus->chipco, &plltype,
953 &clkctl_n, &clkctl_m);
957 if (bus->chip_id == 0x5365) {
960 rate = ssb_calc_clock_rate(plltype, clkctl_n, clkctl_m);
961 if (plltype == SSB_PLLTYPE_3) /* 25Mhz, 2 dividers */
967 EXPORT_SYMBOL(ssb_clockspeed);
969 static u32 ssb_tmslow_reject_bitmask(struct ssb_device *dev)
971 u32 rev = ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_SSBREV;
973 /* The REJECT bit seems to be different for Backplane rev 2.3 */
975 case SSB_IDLOW_SSBREV_22:
976 case SSB_IDLOW_SSBREV_24:
977 case SSB_IDLOW_SSBREV_26:
978 return SSB_TMSLOW_REJECT;
979 case SSB_IDLOW_SSBREV_23:
980 return SSB_TMSLOW_REJECT_23;
981 case SSB_IDLOW_SSBREV_25: /* TODO - find the proper REJECT bit */
982 case SSB_IDLOW_SSBREV_27: /* same here */
983 return SSB_TMSLOW_REJECT; /* this is a guess */
984 case SSB_IDLOW_SSBREV:
987 WARN(1, KERN_INFO "ssb: Backplane Revision 0x%.8X\n", rev);
989 return (SSB_TMSLOW_REJECT | SSB_TMSLOW_REJECT_23);
992 int ssb_device_is_enabled(struct ssb_device *dev)
997 reject = ssb_tmslow_reject_bitmask(dev);
998 val = ssb_read32(dev, SSB_TMSLOW);
999 val &= SSB_TMSLOW_CLOCK | SSB_TMSLOW_RESET | reject;
1001 return (val == SSB_TMSLOW_CLOCK);
1003 EXPORT_SYMBOL(ssb_device_is_enabled);
1005 static void ssb_flush_tmslow(struct ssb_device *dev)
1007 /* Make _really_ sure the device has finished the TMSLOW
1008 * register write transaction, as we risk running into
1009 * a machine check exception otherwise.
1010 * Do this by reading the register back to commit the
1011 * PCI write and delay an additional usec for the device
1012 * to react to the change.
1014 ssb_read32(dev, SSB_TMSLOW);
1018 void ssb_device_enable(struct ssb_device *dev, u32 core_specific_flags)
1022 ssb_device_disable(dev, core_specific_flags);
1023 ssb_write32(dev, SSB_TMSLOW,
1024 SSB_TMSLOW_RESET | SSB_TMSLOW_CLOCK |
1025 SSB_TMSLOW_FGC | core_specific_flags);
1026 ssb_flush_tmslow(dev);
1028 /* Clear SERR if set. This is a hw bug workaround. */
1029 if (ssb_read32(dev, SSB_TMSHIGH) & SSB_TMSHIGH_SERR)
1030 ssb_write32(dev, SSB_TMSHIGH, 0);
1032 val = ssb_read32(dev, SSB_IMSTATE);
1033 if (val & (SSB_IMSTATE_IBE | SSB_IMSTATE_TO)) {
1034 val &= ~(SSB_IMSTATE_IBE | SSB_IMSTATE_TO);
1035 ssb_write32(dev, SSB_IMSTATE, val);
1038 ssb_write32(dev, SSB_TMSLOW,
1039 SSB_TMSLOW_CLOCK | SSB_TMSLOW_FGC |
1040 core_specific_flags);
1041 ssb_flush_tmslow(dev);
1043 ssb_write32(dev, SSB_TMSLOW, SSB_TMSLOW_CLOCK |
1044 core_specific_flags);
1045 ssb_flush_tmslow(dev);
1047 EXPORT_SYMBOL(ssb_device_enable);
1049 /* Wait for bitmask in a register to get set or cleared.
1050 * timeout is in units of ten-microseconds
1052 static int ssb_wait_bits(struct ssb_device *dev, u16 reg, u32 bitmask,
1053 int timeout, int set)
1058 for (i = 0; i < timeout; i++) {
1059 val = ssb_read32(dev, reg);
1061 if ((val & bitmask) == bitmask)
1064 if (!(val & bitmask))
1070 "Timeout waiting for bitmask %08X on register %04X to %s\n",
1071 bitmask, reg, set ? "set" : "clear");
1076 void ssb_device_disable(struct ssb_device *dev, u32 core_specific_flags)
1080 if (ssb_read32(dev, SSB_TMSLOW) & SSB_TMSLOW_RESET)
1083 reject = ssb_tmslow_reject_bitmask(dev);
1085 if (ssb_read32(dev, SSB_TMSLOW) & SSB_TMSLOW_CLOCK) {
1086 ssb_write32(dev, SSB_TMSLOW, reject | SSB_TMSLOW_CLOCK);
1087 ssb_wait_bits(dev, SSB_TMSLOW, reject, 1000, 1);
1088 ssb_wait_bits(dev, SSB_TMSHIGH, SSB_TMSHIGH_BUSY, 1000, 0);
1090 if (ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_INITIATOR) {
1091 val = ssb_read32(dev, SSB_IMSTATE);
1092 val |= SSB_IMSTATE_REJECT;
1093 ssb_write32(dev, SSB_IMSTATE, val);
1094 ssb_wait_bits(dev, SSB_IMSTATE, SSB_IMSTATE_BUSY, 1000,
1098 ssb_write32(dev, SSB_TMSLOW,
1099 SSB_TMSLOW_FGC | SSB_TMSLOW_CLOCK |
1100 reject | SSB_TMSLOW_RESET |
1101 core_specific_flags);
1102 ssb_flush_tmslow(dev);
1104 if (ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_INITIATOR) {
1105 val = ssb_read32(dev, SSB_IMSTATE);
1106 val &= ~SSB_IMSTATE_REJECT;
1107 ssb_write32(dev, SSB_IMSTATE, val);
1111 ssb_write32(dev, SSB_TMSLOW,
1112 reject | SSB_TMSLOW_RESET |
1113 core_specific_flags);
1114 ssb_flush_tmslow(dev);
1116 EXPORT_SYMBOL(ssb_device_disable);
1118 /* Some chipsets need routing known for PCIe and 64-bit DMA */
1119 static bool ssb_dma_translation_special_bit(struct ssb_device *dev)
1121 u16 chip_id = dev->bus->chip_id;
1123 if (dev->id.coreid == SSB_DEV_80211) {
1124 return (chip_id == 0x4322 || chip_id == 43221 ||
1125 chip_id == 43231 || chip_id == 43222);
1131 u32 ssb_dma_translation(struct ssb_device *dev)
1133 switch (dev->bus->bustype) {
1134 case SSB_BUSTYPE_SSB:
1136 case SSB_BUSTYPE_PCI:
1137 if (pci_is_pcie(dev->bus->host_pci) &&
1138 ssb_read32(dev, SSB_TMSHIGH) & SSB_TMSHIGH_DMA64) {
1139 return SSB_PCIE_DMA_H32;
1141 if (ssb_dma_translation_special_bit(dev))
1142 return SSB_PCIE_DMA_H32;
1147 __ssb_dma_not_implemented(dev);
1151 EXPORT_SYMBOL(ssb_dma_translation);
1153 int ssb_bus_may_powerdown(struct ssb_bus *bus)
1155 struct ssb_chipcommon *cc;
1158 /* On buses where more than one core may be working
1159 * at a time, we must not powerdown stuff if there are
1160 * still cores that may want to run.
1162 if (bus->bustype == SSB_BUSTYPE_SSB)
1169 if (cc->dev->id.revision < 5)
1172 ssb_chipco_set_clockmode(cc, SSB_CLKMODE_SLOW);
1173 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
1177 bus->powered_up = 0;
1180 pr_err("Bus powerdown failed\n");
1183 EXPORT_SYMBOL(ssb_bus_may_powerdown);
1185 int ssb_bus_powerup(struct ssb_bus *bus, bool dynamic_pctl)
1188 enum ssb_clkmode mode;
1190 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
1194 bus->powered_up = 1;
1196 mode = dynamic_pctl ? SSB_CLKMODE_DYNAMIC : SSB_CLKMODE_FAST;
1197 ssb_chipco_set_clockmode(&bus->chipco, mode);
1201 pr_err("Bus powerup failed\n");
1204 EXPORT_SYMBOL(ssb_bus_powerup);
1206 static void ssb_broadcast_value(struct ssb_device *dev,
1207 u32 address, u32 data)
1209 #ifdef CONFIG_SSB_DRIVER_PCICORE
1210 /* This is used for both, PCI and ChipCommon core, so be careful. */
1211 BUILD_BUG_ON(SSB_PCICORE_BCAST_ADDR != SSB_CHIPCO_BCAST_ADDR);
1212 BUILD_BUG_ON(SSB_PCICORE_BCAST_DATA != SSB_CHIPCO_BCAST_DATA);
1215 ssb_write32(dev, SSB_CHIPCO_BCAST_ADDR, address);
1216 ssb_read32(dev, SSB_CHIPCO_BCAST_ADDR); /* flush */
1217 ssb_write32(dev, SSB_CHIPCO_BCAST_DATA, data);
1218 ssb_read32(dev, SSB_CHIPCO_BCAST_DATA); /* flush */
1221 void ssb_commit_settings(struct ssb_bus *bus)
1223 struct ssb_device *dev;
1225 #ifdef CONFIG_SSB_DRIVER_PCICORE
1226 dev = bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev;
1228 dev = bus->chipco.dev;
1232 /* This forces an update of the cached registers. */
1233 ssb_broadcast_value(dev, 0xFD8, 0);
1235 EXPORT_SYMBOL(ssb_commit_settings);
1237 u32 ssb_admatch_base(u32 adm)
1241 switch (adm & SSB_ADM_TYPE) {
1243 base = (adm & SSB_ADM_BASE0);
1246 WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1247 base = (adm & SSB_ADM_BASE1);
1250 WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1251 base = (adm & SSB_ADM_BASE2);
1259 EXPORT_SYMBOL(ssb_admatch_base);
1261 u32 ssb_admatch_size(u32 adm)
1265 switch (adm & SSB_ADM_TYPE) {
1267 size = ((adm & SSB_ADM_SZ0) >> SSB_ADM_SZ0_SHIFT);
1270 WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1271 size = ((adm & SSB_ADM_SZ1) >> SSB_ADM_SZ1_SHIFT);
1274 WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1275 size = ((adm & SSB_ADM_SZ2) >> SSB_ADM_SZ2_SHIFT);
1280 size = (1 << (size + 1));
1284 EXPORT_SYMBOL(ssb_admatch_size);
1286 static int __init ssb_modinit(void)
1290 /* See the comment at the ssb_is_early_boot definition */
1291 ssb_is_early_boot = 0;
1292 err = bus_register(&ssb_bustype);
1296 /* Maybe we already registered some buses at early boot.
1297 * Check for this and attach them
1300 err = ssb_attach_queued_buses();
1303 bus_unregister(&ssb_bustype);
1307 err = b43_pci_ssb_bridge_init();
1309 pr_err("Broadcom 43xx PCI-SSB-bridge initialization failed\n");
1310 /* don't fail SSB init because of this */
1312 err = ssb_host_pcmcia_init();
1314 pr_err("PCMCIA host initialization failed\n");
1315 /* don't fail SSB init because of this */
1317 err = ssb_gige_init();
1319 pr_err("SSB Broadcom Gigabit Ethernet driver initialization failed\n");
1320 /* don't fail SSB init because of this */
1326 /* ssb must be initialized after PCI but before the ssb drivers.
1327 * That means we must use some initcall between subsys_initcall
1328 * and device_initcall.
1330 fs_initcall(ssb_modinit);
1332 static void __exit ssb_modexit(void)
1335 ssb_host_pcmcia_exit();
1336 b43_pci_ssb_bridge_exit();
1337 bus_unregister(&ssb_bustype);
1339 module_exit(ssb_modexit)