2 * Sonics Silicon Backplane
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 "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>
22 #include <pcmcia/cs_types.h>
23 #include <pcmcia/cs.h>
24 #include <pcmcia/cistpl.h>
25 #include <pcmcia/ds.h>
28 MODULE_DESCRIPTION("Sonics Silicon Backplane driver");
29 MODULE_LICENSE("GPL");
32 /* Temporary list of yet-to-be-attached buses */
33 static LIST_HEAD(attach_queue);
34 /* List if running buses */
35 static LIST_HEAD(buses);
36 /* Software ID counter */
37 static unsigned int next_busnumber;
38 /* buses_mutes locks the two buslists and the next_busnumber.
39 * Don't lock this directly, but use ssb_buses_[un]lock() below. */
40 static DEFINE_MUTEX(buses_mutex);
42 /* There are differences in the codeflow, if the bus is
43 * initialized from early boot, as various needed services
44 * are not available early. This is a mechanism to delay
45 * these initializations to after early boot has finished.
46 * It's also used to avoid mutex locking, as that's not
47 * available and needed early. */
48 static bool ssb_is_early_boot = 1;
50 static void ssb_buses_lock(void);
51 static void ssb_buses_unlock(void);
54 #ifdef CONFIG_SSB_PCIHOST
55 struct ssb_bus *ssb_pci_dev_to_bus(struct pci_dev *pdev)
60 list_for_each_entry(bus, &buses, list) {
61 if (bus->bustype == SSB_BUSTYPE_PCI &&
62 bus->host_pci == pdev)
71 #endif /* CONFIG_SSB_PCIHOST */
73 #ifdef CONFIG_SSB_PCMCIAHOST
74 struct ssb_bus *ssb_pcmcia_dev_to_bus(struct pcmcia_device *pdev)
79 list_for_each_entry(bus, &buses, list) {
80 if (bus->bustype == SSB_BUSTYPE_PCMCIA &&
81 bus->host_pcmcia == pdev)
90 #endif /* CONFIG_SSB_PCMCIAHOST */
92 #ifdef CONFIG_SSB_SDIOHOST
93 struct ssb_bus *ssb_sdio_func_to_bus(struct sdio_func *func)
98 list_for_each_entry(bus, &buses, list) {
99 if (bus->bustype == SSB_BUSTYPE_SDIO &&
100 bus->host_sdio == func)
109 #endif /* CONFIG_SSB_SDIOHOST */
111 int ssb_for_each_bus_call(unsigned long data,
112 int (*func)(struct ssb_bus *bus, unsigned long data))
118 list_for_each_entry(bus, &buses, list) {
119 res = func(bus, data);
130 static struct ssb_device *ssb_device_get(struct ssb_device *dev)
133 get_device(dev->dev);
137 static void ssb_device_put(struct ssb_device *dev)
140 put_device(dev->dev);
143 static inline struct ssb_driver *ssb_driver_get(struct ssb_driver *drv)
146 get_driver(&drv->drv);
150 static inline void ssb_driver_put(struct ssb_driver *drv)
153 put_driver(&drv->drv);
156 static int ssb_device_resume(struct device *dev)
158 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
159 struct ssb_driver *ssb_drv;
163 ssb_drv = drv_to_ssb_drv(dev->driver);
164 if (ssb_drv && ssb_drv->resume)
165 err = ssb_drv->resume(ssb_dev);
173 static int ssb_device_suspend(struct device *dev, pm_message_t state)
175 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
176 struct ssb_driver *ssb_drv;
180 ssb_drv = drv_to_ssb_drv(dev->driver);
181 if (ssb_drv && ssb_drv->suspend)
182 err = ssb_drv->suspend(ssb_dev, state);
190 int ssb_bus_resume(struct ssb_bus *bus)
194 /* Reset HW state information in memory, so that HW is
195 * completely reinitialized. */
196 bus->mapped_device = NULL;
197 #ifdef CONFIG_SSB_DRIVER_PCICORE
198 bus->pcicore.setup_done = 0;
201 err = ssb_bus_powerup(bus, 0);
204 err = ssb_pcmcia_hardware_setup(bus);
206 ssb_bus_may_powerdown(bus);
209 ssb_chipco_resume(&bus->chipco);
210 ssb_bus_may_powerdown(bus);
214 EXPORT_SYMBOL(ssb_bus_resume);
216 int ssb_bus_suspend(struct ssb_bus *bus)
218 ssb_chipco_suspend(&bus->chipco);
219 ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
223 EXPORT_SYMBOL(ssb_bus_suspend);
225 #ifdef CONFIG_SSB_SPROM
226 /** ssb_devices_freeze - Freeze all devices on the bus.
228 * After freezing no device driver will be handling a device
229 * on this bus anymore. ssb_devices_thaw() must be called after
230 * a successful freeze to reactivate the devices.
233 * @ctx: Context structure. Pass this to ssb_devices_thaw().
235 int ssb_devices_freeze(struct ssb_bus *bus, struct ssb_freeze_context *ctx)
237 struct ssb_device *sdev;
238 struct ssb_driver *sdrv;
241 memset(ctx, 0, sizeof(*ctx));
243 SSB_WARN_ON(bus->nr_devices > ARRAY_SIZE(ctx->device_frozen));
245 for (i = 0; i < bus->nr_devices; i++) {
246 sdev = ssb_device_get(&bus->devices[i]);
248 if (!sdev->dev || !sdev->dev->driver ||
249 !device_is_registered(sdev->dev)) {
250 ssb_device_put(sdev);
253 sdrv = ssb_driver_get(drv_to_ssb_drv(sdev->dev->driver));
254 if (!sdrv || SSB_WARN_ON(!sdrv->remove)) {
255 ssb_device_put(sdev);
259 ctx->device_frozen[i] = 1;
265 /** ssb_devices_thaw - Unfreeze all devices on the bus.
267 * This will re-attach the device drivers and re-init the devices.
269 * @ctx: The context structure from ssb_devices_freeze()
271 int ssb_devices_thaw(struct ssb_freeze_context *ctx)
273 struct ssb_bus *bus = ctx->bus;
274 struct ssb_device *sdev;
275 struct ssb_driver *sdrv;
279 for (i = 0; i < bus->nr_devices; i++) {
280 if (!ctx->device_frozen[i])
282 sdev = &bus->devices[i];
284 if (SSB_WARN_ON(!sdev->dev || !sdev->dev->driver))
286 sdrv = drv_to_ssb_drv(sdev->dev->driver);
287 if (SSB_WARN_ON(!sdrv || !sdrv->probe))
290 err = sdrv->probe(sdev, &sdev->id);
292 ssb_printk(KERN_ERR PFX "Failed to thaw device %s\n",
293 dev_name(sdev->dev));
296 ssb_driver_put(sdrv);
297 ssb_device_put(sdev);
302 #endif /* CONFIG_SSB_SPROM */
304 static void ssb_device_shutdown(struct device *dev)
306 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
307 struct ssb_driver *ssb_drv;
311 ssb_drv = drv_to_ssb_drv(dev->driver);
312 if (ssb_drv && ssb_drv->shutdown)
313 ssb_drv->shutdown(ssb_dev);
316 static int ssb_device_remove(struct device *dev)
318 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
319 struct ssb_driver *ssb_drv = drv_to_ssb_drv(dev->driver);
321 if (ssb_drv && ssb_drv->remove)
322 ssb_drv->remove(ssb_dev);
323 ssb_device_put(ssb_dev);
328 static int ssb_device_probe(struct device *dev)
330 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
331 struct ssb_driver *ssb_drv = drv_to_ssb_drv(dev->driver);
334 ssb_device_get(ssb_dev);
335 if (ssb_drv && ssb_drv->probe)
336 err = ssb_drv->probe(ssb_dev, &ssb_dev->id);
338 ssb_device_put(ssb_dev);
343 static int ssb_match_devid(const struct ssb_device_id *tabid,
344 const struct ssb_device_id *devid)
346 if ((tabid->vendor != devid->vendor) &&
347 tabid->vendor != SSB_ANY_VENDOR)
349 if ((tabid->coreid != devid->coreid) &&
350 tabid->coreid != SSB_ANY_ID)
352 if ((tabid->revision != devid->revision) &&
353 tabid->revision != SSB_ANY_REV)
358 static int ssb_bus_match(struct device *dev, struct device_driver *drv)
360 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
361 struct ssb_driver *ssb_drv = drv_to_ssb_drv(drv);
362 const struct ssb_device_id *id;
364 for (id = ssb_drv->id_table;
365 id->vendor || id->coreid || id->revision;
367 if (ssb_match_devid(id, &ssb_dev->id))
368 return 1; /* found */
374 static int ssb_device_uevent(struct device *dev, struct kobj_uevent_env *env)
376 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
381 return add_uevent_var(env,
382 "MODALIAS=ssb:v%04Xid%04Xrev%02X",
383 ssb_dev->id.vendor, ssb_dev->id.coreid,
384 ssb_dev->id.revision);
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,
398 static void ssb_buses_lock(void)
400 /* See the comment at the ssb_is_early_boot definition */
401 if (!ssb_is_early_boot)
402 mutex_lock(&buses_mutex);
405 static void ssb_buses_unlock(void)
407 /* See the comment at the ssb_is_early_boot definition */
408 if (!ssb_is_early_boot)
409 mutex_unlock(&buses_mutex);
412 static void ssb_devices_unregister(struct ssb_bus *bus)
414 struct ssb_device *sdev;
417 for (i = bus->nr_devices - 1; i >= 0; i--) {
418 sdev = &(bus->devices[i]);
420 device_unregister(sdev->dev);
424 void ssb_bus_unregister(struct ssb_bus *bus)
427 ssb_devices_unregister(bus);
428 list_del(&bus->list);
431 ssb_pcmcia_exit(bus);
435 EXPORT_SYMBOL(ssb_bus_unregister);
437 static void ssb_release_dev(struct device *dev)
439 struct __ssb_dev_wrapper *devwrap;
441 devwrap = container_of(dev, struct __ssb_dev_wrapper, dev);
445 static int ssb_devices_register(struct ssb_bus *bus)
447 struct ssb_device *sdev;
449 struct __ssb_dev_wrapper *devwrap;
453 for (i = 0; i < bus->nr_devices; i++) {
454 sdev = &(bus->devices[i]);
456 /* We don't register SSB-system devices to the kernel,
457 * as the drivers for them are built into SSB. */
458 switch (sdev->id.coreid) {
459 case SSB_DEV_CHIPCOMMON:
464 case SSB_DEV_MIPS_3302:
469 devwrap = kzalloc(sizeof(*devwrap), GFP_KERNEL);
471 ssb_printk(KERN_ERR PFX
472 "Could not allocate device\n");
477 devwrap->sdev = sdev;
479 dev->release = ssb_release_dev;
480 dev->bus = &ssb_bustype;
481 dev_set_name(dev, "ssb%u:%d", bus->busnumber, dev_idx);
483 switch (bus->bustype) {
484 case SSB_BUSTYPE_PCI:
485 #ifdef CONFIG_SSB_PCIHOST
486 sdev->irq = bus->host_pci->irq;
487 dev->parent = &bus->host_pci->dev;
490 case SSB_BUSTYPE_PCMCIA:
491 #ifdef CONFIG_SSB_PCMCIAHOST
492 sdev->irq = bus->host_pcmcia->irq.AssignedIRQ;
493 dev->parent = &bus->host_pcmcia->dev;
496 case SSB_BUSTYPE_SDIO:
497 #ifdef CONFIG_SSB_SDIOHOST
498 dev->parent = &bus->host_sdio->dev;
501 case SSB_BUSTYPE_SSB:
502 dev->dma_mask = &dev->coherent_dma_mask;
507 err = device_register(dev);
509 ssb_printk(KERN_ERR PFX
510 "Could not register %s\n",
512 /* Set dev to NULL to not unregister
513 * dev on error unwinding. */
523 /* Unwind the already registered devices. */
524 ssb_devices_unregister(bus);
528 /* Needs ssb_buses_lock() */
529 static int ssb_attach_queued_buses(void)
531 struct ssb_bus *bus, *n;
533 int drop_them_all = 0;
535 list_for_each_entry_safe(bus, n, &attach_queue, list) {
537 list_del(&bus->list);
540 /* Can't init the PCIcore in ssb_bus_register(), as that
541 * is too early in boot for embedded systems
542 * (no udelay() available). So do it here in attach stage.
544 err = ssb_bus_powerup(bus, 0);
547 ssb_pcicore_init(&bus->pcicore);
548 ssb_bus_may_powerdown(bus);
550 err = ssb_devices_register(bus);
554 list_del(&bus->list);
557 list_move_tail(&bus->list, &buses);
563 static u8 ssb_ssb_read8(struct ssb_device *dev, u16 offset)
565 struct ssb_bus *bus = dev->bus;
567 offset += dev->core_index * SSB_CORE_SIZE;
568 return readb(bus->mmio + offset);
571 static u16 ssb_ssb_read16(struct ssb_device *dev, u16 offset)
573 struct ssb_bus *bus = dev->bus;
575 offset += dev->core_index * SSB_CORE_SIZE;
576 return readw(bus->mmio + offset);
579 static u32 ssb_ssb_read32(struct ssb_device *dev, u16 offset)
581 struct ssb_bus *bus = dev->bus;
583 offset += dev->core_index * SSB_CORE_SIZE;
584 return readl(bus->mmio + offset);
587 #ifdef CONFIG_SSB_BLOCKIO
588 static void ssb_ssb_block_read(struct ssb_device *dev, void *buffer,
589 size_t count, u16 offset, u8 reg_width)
591 struct ssb_bus *bus = dev->bus;
594 offset += dev->core_index * SSB_CORE_SIZE;
595 addr = bus->mmio + offset;
602 *buf = __raw_readb(addr);
609 __le16 *buf = buffer;
611 SSB_WARN_ON(count & 1);
613 *buf = (__force __le16)__raw_readw(addr);
620 __le32 *buf = buffer;
622 SSB_WARN_ON(count & 3);
624 *buf = (__force __le32)__raw_readl(addr);
634 #endif /* CONFIG_SSB_BLOCKIO */
636 static void ssb_ssb_write8(struct ssb_device *dev, u16 offset, u8 value)
638 struct ssb_bus *bus = dev->bus;
640 offset += dev->core_index * SSB_CORE_SIZE;
641 writeb(value, bus->mmio + offset);
644 static void ssb_ssb_write16(struct ssb_device *dev, u16 offset, u16 value)
646 struct ssb_bus *bus = dev->bus;
648 offset += dev->core_index * SSB_CORE_SIZE;
649 writew(value, bus->mmio + offset);
652 static void ssb_ssb_write32(struct ssb_device *dev, u16 offset, u32 value)
654 struct ssb_bus *bus = dev->bus;
656 offset += dev->core_index * SSB_CORE_SIZE;
657 writel(value, bus->mmio + offset);
660 #ifdef CONFIG_SSB_BLOCKIO
661 static void ssb_ssb_block_write(struct ssb_device *dev, const void *buffer,
662 size_t count, u16 offset, u8 reg_width)
664 struct ssb_bus *bus = dev->bus;
667 offset += dev->core_index * SSB_CORE_SIZE;
668 addr = bus->mmio + offset;
672 const u8 *buf = buffer;
675 __raw_writeb(*buf, addr);
682 const __le16 *buf = buffer;
684 SSB_WARN_ON(count & 1);
686 __raw_writew((__force u16)(*buf), addr);
693 const __le32 *buf = buffer;
695 SSB_WARN_ON(count & 3);
697 __raw_writel((__force u32)(*buf), addr);
707 #endif /* CONFIG_SSB_BLOCKIO */
709 /* Ops for the plain SSB bus without a host-device (no PCI or PCMCIA). */
710 static const struct ssb_bus_ops ssb_ssb_ops = {
711 .read8 = ssb_ssb_read8,
712 .read16 = ssb_ssb_read16,
713 .read32 = ssb_ssb_read32,
714 .write8 = ssb_ssb_write8,
715 .write16 = ssb_ssb_write16,
716 .write32 = ssb_ssb_write32,
717 #ifdef CONFIG_SSB_BLOCKIO
718 .block_read = ssb_ssb_block_read,
719 .block_write = ssb_ssb_block_write,
723 static int ssb_fetch_invariants(struct ssb_bus *bus,
724 ssb_invariants_func_t get_invariants)
726 struct ssb_init_invariants iv;
729 memset(&iv, 0, sizeof(iv));
730 err = get_invariants(bus, &iv);
733 memcpy(&bus->boardinfo, &iv.boardinfo, sizeof(iv.boardinfo));
734 memcpy(&bus->sprom, &iv.sprom, sizeof(iv.sprom));
735 bus->has_cardbus_slot = iv.has_cardbus_slot;
740 static int ssb_bus_register(struct ssb_bus *bus,
741 ssb_invariants_func_t get_invariants,
742 unsigned long baseaddr)
746 spin_lock_init(&bus->bar_lock);
747 INIT_LIST_HEAD(&bus->list);
748 #ifdef CONFIG_SSB_EMBEDDED
749 spin_lock_init(&bus->gpio_lock);
752 /* Powerup the bus */
753 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
757 /* Init SDIO-host device (if any), before the scan */
758 err = ssb_sdio_init(bus);
760 goto err_disable_xtal;
763 bus->busnumber = next_busnumber;
764 /* Scan for devices (cores) */
765 err = ssb_bus_scan(bus, baseaddr);
769 /* Init PCI-host device (if any) */
770 err = ssb_pci_init(bus);
773 /* Init PCMCIA-host device (if any) */
774 err = ssb_pcmcia_init(bus);
778 /* Initialize basic system devices (if available) */
779 err = ssb_bus_powerup(bus, 0);
781 goto err_pcmcia_exit;
782 ssb_chipcommon_init(&bus->chipco);
783 ssb_mipscore_init(&bus->mipscore);
784 err = ssb_fetch_invariants(bus, get_invariants);
786 ssb_bus_may_powerdown(bus);
787 goto err_pcmcia_exit;
789 ssb_bus_may_powerdown(bus);
791 /* Queue it for attach.
792 * See the comment at the ssb_is_early_boot definition. */
793 list_add_tail(&bus->list, &attach_queue);
794 if (!ssb_is_early_boot) {
795 /* This is not early boot, so we must attach the bus now */
796 err = ssb_attach_queued_buses();
807 list_del(&bus->list);
809 ssb_pcmcia_exit(bus);
818 ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
822 #ifdef CONFIG_SSB_PCIHOST
823 int ssb_bus_pcibus_register(struct ssb_bus *bus,
824 struct pci_dev *host_pci)
828 bus->bustype = SSB_BUSTYPE_PCI;
829 bus->host_pci = host_pci;
830 bus->ops = &ssb_pci_ops;
832 err = ssb_bus_register(bus, ssb_pci_get_invariants, 0);
834 ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on "
835 "PCI device %s\n", dev_name(&host_pci->dev));
837 ssb_printk(KERN_ERR PFX "Failed to register PCI version"
838 " of SSB with error %d\n", err);
843 EXPORT_SYMBOL(ssb_bus_pcibus_register);
844 #endif /* CONFIG_SSB_PCIHOST */
846 #ifdef CONFIG_SSB_PCMCIAHOST
847 int ssb_bus_pcmciabus_register(struct ssb_bus *bus,
848 struct pcmcia_device *pcmcia_dev,
849 unsigned long baseaddr)
853 bus->bustype = SSB_BUSTYPE_PCMCIA;
854 bus->host_pcmcia = pcmcia_dev;
855 bus->ops = &ssb_pcmcia_ops;
857 err = ssb_bus_register(bus, ssb_pcmcia_get_invariants, baseaddr);
859 ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on "
860 "PCMCIA device %s\n", pcmcia_dev->devname);
865 EXPORT_SYMBOL(ssb_bus_pcmciabus_register);
866 #endif /* CONFIG_SSB_PCMCIAHOST */
868 #ifdef CONFIG_SSB_SDIOHOST
869 int ssb_bus_sdiobus_register(struct ssb_bus *bus, struct sdio_func *func,
874 bus->bustype = SSB_BUSTYPE_SDIO;
875 bus->host_sdio = func;
876 bus->ops = &ssb_sdio_ops;
877 bus->quirks = quirks;
879 err = ssb_bus_register(bus, ssb_sdio_get_invariants, ~0);
881 ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on "
882 "SDIO device %s\n", sdio_func_id(func));
887 EXPORT_SYMBOL(ssb_bus_sdiobus_register);
888 #endif /* CONFIG_SSB_PCMCIAHOST */
890 int ssb_bus_ssbbus_register(struct ssb_bus *bus,
891 unsigned long baseaddr,
892 ssb_invariants_func_t get_invariants)
896 bus->bustype = SSB_BUSTYPE_SSB;
897 bus->ops = &ssb_ssb_ops;
899 err = ssb_bus_register(bus, get_invariants, baseaddr);
901 ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found at "
902 "address 0x%08lX\n", baseaddr);
908 int __ssb_driver_register(struct ssb_driver *drv, struct module *owner)
910 drv->drv.name = drv->name;
911 drv->drv.bus = &ssb_bustype;
912 drv->drv.owner = owner;
914 return driver_register(&drv->drv);
916 EXPORT_SYMBOL(__ssb_driver_register);
918 void ssb_driver_unregister(struct ssb_driver *drv)
920 driver_unregister(&drv->drv);
922 EXPORT_SYMBOL(ssb_driver_unregister);
924 void ssb_set_devtypedata(struct ssb_device *dev, void *data)
926 struct ssb_bus *bus = dev->bus;
927 struct ssb_device *ent;
930 for (i = 0; i < bus->nr_devices; i++) {
931 ent = &(bus->devices[i]);
932 if (ent->id.vendor != dev->id.vendor)
934 if (ent->id.coreid != dev->id.coreid)
937 ent->devtypedata = data;
940 EXPORT_SYMBOL(ssb_set_devtypedata);
942 static u32 clkfactor_f6_resolve(u32 v)
944 /* map the magic values */
946 case SSB_CHIPCO_CLK_F6_2:
948 case SSB_CHIPCO_CLK_F6_3:
950 case SSB_CHIPCO_CLK_F6_4:
952 case SSB_CHIPCO_CLK_F6_5:
954 case SSB_CHIPCO_CLK_F6_6:
956 case SSB_CHIPCO_CLK_F6_7:
962 /* Calculate the speed the backplane would run at a given set of clockcontrol values */
963 u32 ssb_calc_clock_rate(u32 plltype, u32 n, u32 m)
965 u32 n1, n2, clock, m1, m2, m3, mc;
967 n1 = (n & SSB_CHIPCO_CLK_N1);
968 n2 = ((n & SSB_CHIPCO_CLK_N2) >> SSB_CHIPCO_CLK_N2_SHIFT);
971 case SSB_PLLTYPE_6: /* 100/200 or 120/240 only */
972 if (m & SSB_CHIPCO_CLK_T6_MMASK)
973 return SSB_CHIPCO_CLK_T6_M0;
974 return SSB_CHIPCO_CLK_T6_M1;
975 case SSB_PLLTYPE_1: /* 48Mhz base, 3 dividers */
976 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
977 case SSB_PLLTYPE_4: /* 48Mhz, 4 dividers */
978 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
979 n1 = clkfactor_f6_resolve(n1);
980 n2 += SSB_CHIPCO_CLK_F5_BIAS;
982 case SSB_PLLTYPE_2: /* 48Mhz, 4 dividers */
983 n1 += SSB_CHIPCO_CLK_T2_BIAS;
984 n2 += SSB_CHIPCO_CLK_T2_BIAS;
985 SSB_WARN_ON(!((n1 >= 2) && (n1 <= 7)));
986 SSB_WARN_ON(!((n2 >= 5) && (n2 <= 23)));
988 case SSB_PLLTYPE_5: /* 25Mhz, 4 dividers */
995 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
996 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
997 clock = SSB_CHIPCO_CLK_BASE2 * n1 * n2;
1000 clock = SSB_CHIPCO_CLK_BASE1 * n1 * n2;
1005 m1 = (m & SSB_CHIPCO_CLK_M1);
1006 m2 = ((m & SSB_CHIPCO_CLK_M2) >> SSB_CHIPCO_CLK_M2_SHIFT);
1007 m3 = ((m & SSB_CHIPCO_CLK_M3) >> SSB_CHIPCO_CLK_M3_SHIFT);
1008 mc = ((m & SSB_CHIPCO_CLK_MC) >> SSB_CHIPCO_CLK_MC_SHIFT);
1011 case SSB_PLLTYPE_1: /* 48Mhz base, 3 dividers */
1012 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
1013 case SSB_PLLTYPE_4: /* 48Mhz, 4 dividers */
1014 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
1015 m1 = clkfactor_f6_resolve(m1);
1016 if ((plltype == SSB_PLLTYPE_1) ||
1017 (plltype == SSB_PLLTYPE_3))
1018 m2 += SSB_CHIPCO_CLK_F5_BIAS;
1020 m2 = clkfactor_f6_resolve(m2);
1021 m3 = clkfactor_f6_resolve(m3);
1024 case SSB_CHIPCO_CLK_MC_BYPASS:
1026 case SSB_CHIPCO_CLK_MC_M1:
1027 return (clock / m1);
1028 case SSB_CHIPCO_CLK_MC_M1M2:
1029 return (clock / (m1 * m2));
1030 case SSB_CHIPCO_CLK_MC_M1M2M3:
1031 return (clock / (m1 * m2 * m3));
1032 case SSB_CHIPCO_CLK_MC_M1M3:
1033 return (clock / (m1 * m3));
1037 m1 += SSB_CHIPCO_CLK_T2_BIAS;
1038 m2 += SSB_CHIPCO_CLK_T2M2_BIAS;
1039 m3 += SSB_CHIPCO_CLK_T2_BIAS;
1040 SSB_WARN_ON(!((m1 >= 2) && (m1 <= 7)));
1041 SSB_WARN_ON(!((m2 >= 3) && (m2 <= 10)));
1042 SSB_WARN_ON(!((m3 >= 2) && (m3 <= 7)));
1044 if (!(mc & SSB_CHIPCO_CLK_T2MC_M1BYP))
1046 if (!(mc & SSB_CHIPCO_CLK_T2MC_M2BYP))
1048 if (!(mc & SSB_CHIPCO_CLK_T2MC_M3BYP))
1057 /* Get the current speed the backplane is running at */
1058 u32 ssb_clockspeed(struct ssb_bus *bus)
1062 u32 clkctl_n, clkctl_m;
1064 if (ssb_extif_available(&bus->extif))
1065 ssb_extif_get_clockcontrol(&bus->extif, &plltype,
1066 &clkctl_n, &clkctl_m);
1067 else if (bus->chipco.dev)
1068 ssb_chipco_get_clockcontrol(&bus->chipco, &plltype,
1069 &clkctl_n, &clkctl_m);
1073 if (bus->chip_id == 0x5365) {
1076 rate = ssb_calc_clock_rate(plltype, clkctl_n, clkctl_m);
1077 if (plltype == SSB_PLLTYPE_3) /* 25Mhz, 2 dividers */
1083 EXPORT_SYMBOL(ssb_clockspeed);
1085 static u32 ssb_tmslow_reject_bitmask(struct ssb_device *dev)
1087 u32 rev = ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_SSBREV;
1089 /* The REJECT bit changed position in TMSLOW between
1090 * Backplane revisions. */
1092 case SSB_IDLOW_SSBREV_22:
1093 return SSB_TMSLOW_REJECT_22;
1094 case SSB_IDLOW_SSBREV_23:
1095 return SSB_TMSLOW_REJECT_23;
1096 case SSB_IDLOW_SSBREV_24: /* TODO - find the proper REJECT bits */
1097 case SSB_IDLOW_SSBREV_25: /* same here */
1098 case SSB_IDLOW_SSBREV_26: /* same here */
1099 case SSB_IDLOW_SSBREV_27: /* same here */
1100 return SSB_TMSLOW_REJECT_23; /* this is a guess */
1102 printk(KERN_INFO "ssb: Backplane Revision 0x%.8X\n", rev);
1105 return (SSB_TMSLOW_REJECT_22 | SSB_TMSLOW_REJECT_23);
1108 int ssb_device_is_enabled(struct ssb_device *dev)
1113 reject = ssb_tmslow_reject_bitmask(dev);
1114 val = ssb_read32(dev, SSB_TMSLOW);
1115 val &= SSB_TMSLOW_CLOCK | SSB_TMSLOW_RESET | reject;
1117 return (val == SSB_TMSLOW_CLOCK);
1119 EXPORT_SYMBOL(ssb_device_is_enabled);
1121 static void ssb_flush_tmslow(struct ssb_device *dev)
1123 /* Make _really_ sure the device has finished the TMSLOW
1124 * register write transaction, as we risk running into
1125 * a machine check exception otherwise.
1126 * Do this by reading the register back to commit the
1127 * PCI write and delay an additional usec for the device
1128 * to react to the change. */
1129 ssb_read32(dev, SSB_TMSLOW);
1133 void ssb_device_enable(struct ssb_device *dev, u32 core_specific_flags)
1137 ssb_device_disable(dev, core_specific_flags);
1138 ssb_write32(dev, SSB_TMSLOW,
1139 SSB_TMSLOW_RESET | SSB_TMSLOW_CLOCK |
1140 SSB_TMSLOW_FGC | core_specific_flags);
1141 ssb_flush_tmslow(dev);
1143 /* Clear SERR if set. This is a hw bug workaround. */
1144 if (ssb_read32(dev, SSB_TMSHIGH) & SSB_TMSHIGH_SERR)
1145 ssb_write32(dev, SSB_TMSHIGH, 0);
1147 val = ssb_read32(dev, SSB_IMSTATE);
1148 if (val & (SSB_IMSTATE_IBE | SSB_IMSTATE_TO)) {
1149 val &= ~(SSB_IMSTATE_IBE | SSB_IMSTATE_TO);
1150 ssb_write32(dev, SSB_IMSTATE, val);
1153 ssb_write32(dev, SSB_TMSLOW,
1154 SSB_TMSLOW_CLOCK | SSB_TMSLOW_FGC |
1155 core_specific_flags);
1156 ssb_flush_tmslow(dev);
1158 ssb_write32(dev, SSB_TMSLOW, SSB_TMSLOW_CLOCK |
1159 core_specific_flags);
1160 ssb_flush_tmslow(dev);
1162 EXPORT_SYMBOL(ssb_device_enable);
1164 /* Wait for a bit in a register to get set or unset.
1165 * timeout is in units of ten-microseconds */
1166 static int ssb_wait_bit(struct ssb_device *dev, u16 reg, u32 bitmask,
1167 int timeout, int set)
1172 for (i = 0; i < timeout; i++) {
1173 val = ssb_read32(dev, reg);
1178 if (!(val & bitmask))
1183 printk(KERN_ERR PFX "Timeout waiting for bitmask %08X on "
1184 "register %04X to %s.\n",
1185 bitmask, reg, (set ? "set" : "clear"));
1190 void ssb_device_disable(struct ssb_device *dev, u32 core_specific_flags)
1194 if (ssb_read32(dev, SSB_TMSLOW) & SSB_TMSLOW_RESET)
1197 reject = ssb_tmslow_reject_bitmask(dev);
1198 ssb_write32(dev, SSB_TMSLOW, reject | SSB_TMSLOW_CLOCK);
1199 ssb_wait_bit(dev, SSB_TMSLOW, reject, 1000, 1);
1200 ssb_wait_bit(dev, SSB_TMSHIGH, SSB_TMSHIGH_BUSY, 1000, 0);
1201 ssb_write32(dev, SSB_TMSLOW,
1202 SSB_TMSLOW_FGC | SSB_TMSLOW_CLOCK |
1203 reject | SSB_TMSLOW_RESET |
1204 core_specific_flags);
1205 ssb_flush_tmslow(dev);
1207 ssb_write32(dev, SSB_TMSLOW,
1208 reject | SSB_TMSLOW_RESET |
1209 core_specific_flags);
1210 ssb_flush_tmslow(dev);
1212 EXPORT_SYMBOL(ssb_device_disable);
1214 u32 ssb_dma_translation(struct ssb_device *dev)
1216 switch (dev->bus->bustype) {
1217 case SSB_BUSTYPE_SSB:
1219 case SSB_BUSTYPE_PCI:
1222 __ssb_dma_not_implemented(dev);
1226 EXPORT_SYMBOL(ssb_dma_translation);
1228 int ssb_dma_set_mask(struct ssb_device *dev, u64 mask)
1230 #ifdef CONFIG_SSB_PCIHOST
1234 switch (dev->bus->bustype) {
1235 case SSB_BUSTYPE_PCI:
1236 #ifdef CONFIG_SSB_PCIHOST
1237 err = pci_set_dma_mask(dev->bus->host_pci, mask);
1240 err = pci_set_consistent_dma_mask(dev->bus->host_pci, mask);
1243 case SSB_BUSTYPE_SSB:
1244 return dma_set_mask(dev->dev, mask);
1246 __ssb_dma_not_implemented(dev);
1250 EXPORT_SYMBOL(ssb_dma_set_mask);
1252 void * ssb_dma_alloc_consistent(struct ssb_device *dev, size_t size,
1253 dma_addr_t *dma_handle, gfp_t gfp_flags)
1255 switch (dev->bus->bustype) {
1256 case SSB_BUSTYPE_PCI:
1257 #ifdef CONFIG_SSB_PCIHOST
1258 if (gfp_flags & GFP_DMA) {
1259 /* Workaround: The PCI API does not support passing
1261 return dma_alloc_coherent(&dev->bus->host_pci->dev,
1262 size, dma_handle, gfp_flags);
1264 return pci_alloc_consistent(dev->bus->host_pci, size, dma_handle);
1266 case SSB_BUSTYPE_SSB:
1267 return dma_alloc_coherent(dev->dev, size, dma_handle, gfp_flags);
1269 __ssb_dma_not_implemented(dev);
1273 EXPORT_SYMBOL(ssb_dma_alloc_consistent);
1275 void ssb_dma_free_consistent(struct ssb_device *dev, size_t size,
1276 void *vaddr, dma_addr_t dma_handle,
1279 switch (dev->bus->bustype) {
1280 case SSB_BUSTYPE_PCI:
1281 #ifdef CONFIG_SSB_PCIHOST
1282 if (gfp_flags & GFP_DMA) {
1283 /* Workaround: The PCI API does not support passing
1285 dma_free_coherent(&dev->bus->host_pci->dev,
1286 size, vaddr, dma_handle);
1289 pci_free_consistent(dev->bus->host_pci, size,
1293 case SSB_BUSTYPE_SSB:
1294 dma_free_coherent(dev->dev, size, vaddr, dma_handle);
1297 __ssb_dma_not_implemented(dev);
1300 EXPORT_SYMBOL(ssb_dma_free_consistent);
1302 int ssb_bus_may_powerdown(struct ssb_bus *bus)
1304 struct ssb_chipcommon *cc;
1307 /* On buses where more than one core may be working
1308 * at a time, we must not powerdown stuff if there are
1309 * still cores that may want to run. */
1310 if (bus->bustype == SSB_BUSTYPE_SSB)
1317 if (cc->dev->id.revision < 5)
1320 ssb_chipco_set_clockmode(cc, SSB_CLKMODE_SLOW);
1321 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
1325 #ifdef CONFIG_SSB_DEBUG
1326 bus->powered_up = 0;
1330 ssb_printk(KERN_ERR PFX "Bus powerdown failed\n");
1333 EXPORT_SYMBOL(ssb_bus_may_powerdown);
1335 int ssb_bus_powerup(struct ssb_bus *bus, bool dynamic_pctl)
1337 struct ssb_chipcommon *cc;
1339 enum ssb_clkmode mode;
1341 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
1345 mode = dynamic_pctl ? SSB_CLKMODE_DYNAMIC : SSB_CLKMODE_FAST;
1346 ssb_chipco_set_clockmode(cc, mode);
1348 #ifdef CONFIG_SSB_DEBUG
1349 bus->powered_up = 1;
1353 ssb_printk(KERN_ERR PFX "Bus powerup failed\n");
1356 EXPORT_SYMBOL(ssb_bus_powerup);
1358 u32 ssb_admatch_base(u32 adm)
1362 switch (adm & SSB_ADM_TYPE) {
1364 base = (adm & SSB_ADM_BASE0);
1367 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1368 base = (adm & SSB_ADM_BASE1);
1371 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1372 base = (adm & SSB_ADM_BASE2);
1380 EXPORT_SYMBOL(ssb_admatch_base);
1382 u32 ssb_admatch_size(u32 adm)
1386 switch (adm & SSB_ADM_TYPE) {
1388 size = ((adm & SSB_ADM_SZ0) >> SSB_ADM_SZ0_SHIFT);
1391 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1392 size = ((adm & SSB_ADM_SZ1) >> SSB_ADM_SZ1_SHIFT);
1395 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1396 size = ((adm & SSB_ADM_SZ2) >> SSB_ADM_SZ2_SHIFT);
1401 size = (1 << (size + 1));
1405 EXPORT_SYMBOL(ssb_admatch_size);
1407 static int __init ssb_modinit(void)
1411 /* See the comment at the ssb_is_early_boot definition */
1412 ssb_is_early_boot = 0;
1413 err = bus_register(&ssb_bustype);
1417 /* Maybe we already registered some buses at early boot.
1418 * Check for this and attach them
1421 err = ssb_attach_queued_buses();
1424 bus_unregister(&ssb_bustype);
1428 err = b43_pci_ssb_bridge_init();
1430 ssb_printk(KERN_ERR "Broadcom 43xx PCI-SSB-bridge "
1431 "initialization failed\n");
1432 /* don't fail SSB init because of this */
1435 err = ssb_gige_init();
1437 ssb_printk(KERN_ERR "SSB Broadcom Gigabit Ethernet "
1438 "driver initialization failed\n");
1439 /* don't fail SSB init because of this */
1445 /* ssb must be initialized after PCI but before the ssb drivers.
1446 * That means we must use some initcall between subsys_initcall
1447 * and device_initcall. */
1448 fs_initcall(ssb_modinit);
1450 static void __exit ssb_modexit(void)
1453 b43_pci_ssb_bridge_exit();
1454 bus_unregister(&ssb_bustype);
1456 module_exit(ssb_modexit)