2 * Sonics Silicon Backplane
5 * Copyright 2005, Broadcom Corporation
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 int ssb_device_resume(struct device *dev)
145 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
146 struct ssb_driver *ssb_drv;
150 ssb_drv = drv_to_ssb_drv(dev->driver);
151 if (ssb_drv && ssb_drv->resume)
152 err = ssb_drv->resume(ssb_dev);
160 static int ssb_device_suspend(struct device *dev, pm_message_t state)
162 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
163 struct ssb_driver *ssb_drv;
167 ssb_drv = drv_to_ssb_drv(dev->driver);
168 if (ssb_drv && ssb_drv->suspend)
169 err = ssb_drv->suspend(ssb_dev, state);
177 int ssb_bus_resume(struct ssb_bus *bus)
181 /* Reset HW state information in memory, so that HW is
182 * completely reinitialized. */
183 bus->mapped_device = NULL;
184 #ifdef CONFIG_SSB_DRIVER_PCICORE
185 bus->pcicore.setup_done = 0;
188 err = ssb_bus_powerup(bus, 0);
191 err = ssb_pcmcia_hardware_setup(bus);
193 ssb_bus_may_powerdown(bus);
196 ssb_chipco_resume(&bus->chipco);
197 ssb_bus_may_powerdown(bus);
201 EXPORT_SYMBOL(ssb_bus_resume);
203 int ssb_bus_suspend(struct ssb_bus *bus)
205 ssb_chipco_suspend(&bus->chipco);
206 ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
210 EXPORT_SYMBOL(ssb_bus_suspend);
212 #ifdef CONFIG_SSB_SPROM
213 int ssb_devices_freeze(struct ssb_bus *bus)
215 struct ssb_device *dev;
216 struct ssb_driver *drv;
219 pm_message_t state = PMSG_FREEZE;
221 /* First check that we are capable to freeze all devices. */
222 for (i = 0; i < bus->nr_devices; i++) {
223 dev = &(bus->devices[i]);
226 !device_is_registered(dev->dev))
228 drv = drv_to_ssb_drv(dev->dev->driver);
232 /* Nope, can't suspend this one. */
236 /* Now suspend all devices */
237 for (i = 0; i < bus->nr_devices; i++) {
238 dev = &(bus->devices[i]);
241 !device_is_registered(dev->dev))
243 drv = drv_to_ssb_drv(dev->dev->driver);
246 err = drv->suspend(dev, state);
248 ssb_printk(KERN_ERR PFX "Failed to freeze device %s\n",
256 for (i--; i >= 0; i--) {
257 dev = &(bus->devices[i]);
260 !device_is_registered(dev->dev))
262 drv = drv_to_ssb_drv(dev->dev->driver);
271 int ssb_devices_thaw(struct ssb_bus *bus)
273 struct ssb_device *dev;
274 struct ssb_driver *drv;
278 for (i = 0; i < bus->nr_devices; i++) {
279 dev = &(bus->devices[i]);
282 !device_is_registered(dev->dev))
284 drv = drv_to_ssb_drv(dev->dev->driver);
287 if (SSB_WARN_ON(!drv->resume))
289 err = drv->resume(dev);
291 ssb_printk(KERN_ERR PFX "Failed to thaw device %s\n",
298 #endif /* CONFIG_SSB_SPROM */
300 static void ssb_device_shutdown(struct device *dev)
302 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
303 struct ssb_driver *ssb_drv;
307 ssb_drv = drv_to_ssb_drv(dev->driver);
308 if (ssb_drv && ssb_drv->shutdown)
309 ssb_drv->shutdown(ssb_dev);
312 static int ssb_device_remove(struct device *dev)
314 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
315 struct ssb_driver *ssb_drv = drv_to_ssb_drv(dev->driver);
317 if (ssb_drv && ssb_drv->remove)
318 ssb_drv->remove(ssb_dev);
319 ssb_device_put(ssb_dev);
324 static int ssb_device_probe(struct device *dev)
326 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
327 struct ssb_driver *ssb_drv = drv_to_ssb_drv(dev->driver);
330 ssb_device_get(ssb_dev);
331 if (ssb_drv && ssb_drv->probe)
332 err = ssb_drv->probe(ssb_dev, &ssb_dev->id);
334 ssb_device_put(ssb_dev);
339 static int ssb_match_devid(const struct ssb_device_id *tabid,
340 const struct ssb_device_id *devid)
342 if ((tabid->vendor != devid->vendor) &&
343 tabid->vendor != SSB_ANY_VENDOR)
345 if ((tabid->coreid != devid->coreid) &&
346 tabid->coreid != SSB_ANY_ID)
348 if ((tabid->revision != devid->revision) &&
349 tabid->revision != SSB_ANY_REV)
354 static int ssb_bus_match(struct device *dev, struct device_driver *drv)
356 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
357 struct ssb_driver *ssb_drv = drv_to_ssb_drv(drv);
358 const struct ssb_device_id *id;
360 for (id = ssb_drv->id_table;
361 id->vendor || id->coreid || id->revision;
363 if (ssb_match_devid(id, &ssb_dev->id))
364 return 1; /* found */
370 static int ssb_device_uevent(struct device *dev, struct kobj_uevent_env *env)
372 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
377 return add_uevent_var(env,
378 "MODALIAS=ssb:v%04Xid%04Xrev%02X",
379 ssb_dev->id.vendor, ssb_dev->id.coreid,
380 ssb_dev->id.revision);
383 static struct bus_type ssb_bustype = {
385 .match = ssb_bus_match,
386 .probe = ssb_device_probe,
387 .remove = ssb_device_remove,
388 .shutdown = ssb_device_shutdown,
389 .suspend = ssb_device_suspend,
390 .resume = ssb_device_resume,
391 .uevent = ssb_device_uevent,
394 static void ssb_buses_lock(void)
396 /* See the comment at the ssb_is_early_boot definition */
397 if (!ssb_is_early_boot)
398 mutex_lock(&buses_mutex);
401 static void ssb_buses_unlock(void)
403 /* See the comment at the ssb_is_early_boot definition */
404 if (!ssb_is_early_boot)
405 mutex_unlock(&buses_mutex);
408 static void ssb_devices_unregister(struct ssb_bus *bus)
410 struct ssb_device *sdev;
413 for (i = bus->nr_devices - 1; i >= 0; i--) {
414 sdev = &(bus->devices[i]);
416 device_unregister(sdev->dev);
420 void ssb_bus_unregister(struct ssb_bus *bus)
423 ssb_devices_unregister(bus);
424 list_del(&bus->list);
427 ssb_pcmcia_exit(bus);
431 EXPORT_SYMBOL(ssb_bus_unregister);
433 static void ssb_release_dev(struct device *dev)
435 struct __ssb_dev_wrapper *devwrap;
437 devwrap = container_of(dev, struct __ssb_dev_wrapper, dev);
441 static int ssb_devices_register(struct ssb_bus *bus)
443 struct ssb_device *sdev;
445 struct __ssb_dev_wrapper *devwrap;
449 for (i = 0; i < bus->nr_devices; i++) {
450 sdev = &(bus->devices[i]);
452 /* We don't register SSB-system devices to the kernel,
453 * as the drivers for them are built into SSB. */
454 switch (sdev->id.coreid) {
455 case SSB_DEV_CHIPCOMMON:
460 case SSB_DEV_MIPS_3302:
465 devwrap = kzalloc(sizeof(*devwrap), GFP_KERNEL);
467 ssb_printk(KERN_ERR PFX
468 "Could not allocate device\n");
473 devwrap->sdev = sdev;
475 dev->release = ssb_release_dev;
476 dev->bus = &ssb_bustype;
477 dev_set_name(dev, "ssb%u:%d", bus->busnumber, dev_idx);
479 switch (bus->bustype) {
480 case SSB_BUSTYPE_PCI:
481 #ifdef CONFIG_SSB_PCIHOST
482 sdev->irq = bus->host_pci->irq;
483 dev->parent = &bus->host_pci->dev;
486 case SSB_BUSTYPE_PCMCIA:
487 #ifdef CONFIG_SSB_PCMCIAHOST
488 sdev->irq = bus->host_pcmcia->irq.AssignedIRQ;
489 dev->parent = &bus->host_pcmcia->dev;
492 case SSB_BUSTYPE_SDIO:
493 #ifdef CONFIG_SSB_SDIO
494 sdev->irq = bus->host_sdio->dev.irq;
495 dev->parent = &bus->host_sdio->dev;
498 case SSB_BUSTYPE_SSB:
499 dev->dma_mask = &dev->coherent_dma_mask;
504 err = device_register(dev);
506 ssb_printk(KERN_ERR PFX
507 "Could not register %s\n",
509 /* Set dev to NULL to not unregister
510 * dev on error unwinding. */
520 /* Unwind the already registered devices. */
521 ssb_devices_unregister(bus);
525 /* Needs ssb_buses_lock() */
526 static int ssb_attach_queued_buses(void)
528 struct ssb_bus *bus, *n;
530 int drop_them_all = 0;
532 list_for_each_entry_safe(bus, n, &attach_queue, list) {
534 list_del(&bus->list);
537 /* Can't init the PCIcore in ssb_bus_register(), as that
538 * is too early in boot for embedded systems
539 * (no udelay() available). So do it here in attach stage.
541 err = ssb_bus_powerup(bus, 0);
544 ssb_pcicore_init(&bus->pcicore);
545 ssb_bus_may_powerdown(bus);
547 err = ssb_devices_register(bus);
551 list_del(&bus->list);
554 list_move_tail(&bus->list, &buses);
560 static u8 ssb_ssb_read8(struct ssb_device *dev, u16 offset)
562 struct ssb_bus *bus = dev->bus;
564 offset += dev->core_index * SSB_CORE_SIZE;
565 return readb(bus->mmio + offset);
568 static u16 ssb_ssb_read16(struct ssb_device *dev, u16 offset)
570 struct ssb_bus *bus = dev->bus;
572 offset += dev->core_index * SSB_CORE_SIZE;
573 return readw(bus->mmio + offset);
576 static u32 ssb_ssb_read32(struct ssb_device *dev, u16 offset)
578 struct ssb_bus *bus = dev->bus;
580 offset += dev->core_index * SSB_CORE_SIZE;
581 return readl(bus->mmio + offset);
584 #ifdef CONFIG_SSB_BLOCKIO
585 static void ssb_ssb_block_read(struct ssb_device *dev, void *buffer,
586 size_t count, u16 offset, u8 reg_width)
588 struct ssb_bus *bus = dev->bus;
591 offset += dev->core_index * SSB_CORE_SIZE;
592 addr = bus->mmio + offset;
599 *buf = __raw_readb(addr);
606 __le16 *buf = buffer;
608 SSB_WARN_ON(count & 1);
610 *buf = (__force __le16)__raw_readw(addr);
617 __le32 *buf = buffer;
619 SSB_WARN_ON(count & 3);
621 *buf = (__force __le32)__raw_readl(addr);
631 #endif /* CONFIG_SSB_BLOCKIO */
633 static void ssb_ssb_write8(struct ssb_device *dev, u16 offset, u8 value)
635 struct ssb_bus *bus = dev->bus;
637 offset += dev->core_index * SSB_CORE_SIZE;
638 writeb(value, bus->mmio + offset);
641 static void ssb_ssb_write16(struct ssb_device *dev, u16 offset, u16 value)
643 struct ssb_bus *bus = dev->bus;
645 offset += dev->core_index * SSB_CORE_SIZE;
646 writew(value, bus->mmio + offset);
649 static void ssb_ssb_write32(struct ssb_device *dev, u16 offset, u32 value)
651 struct ssb_bus *bus = dev->bus;
653 offset += dev->core_index * SSB_CORE_SIZE;
654 writel(value, bus->mmio + offset);
657 #ifdef CONFIG_SSB_BLOCKIO
658 static void ssb_ssb_block_write(struct ssb_device *dev, const void *buffer,
659 size_t count, u16 offset, u8 reg_width)
661 struct ssb_bus *bus = dev->bus;
664 offset += dev->core_index * SSB_CORE_SIZE;
665 addr = bus->mmio + offset;
669 const u8 *buf = buffer;
672 __raw_writeb(*buf, addr);
679 const __le16 *buf = buffer;
681 SSB_WARN_ON(count & 1);
683 __raw_writew((__force u16)(*buf), addr);
690 const __le32 *buf = buffer;
692 SSB_WARN_ON(count & 3);
694 __raw_writel((__force u32)(*buf), addr);
704 #endif /* CONFIG_SSB_BLOCKIO */
706 /* Ops for the plain SSB bus without a host-device (no PCI or PCMCIA). */
707 static const struct ssb_bus_ops ssb_ssb_ops = {
708 .read8 = ssb_ssb_read8,
709 .read16 = ssb_ssb_read16,
710 .read32 = ssb_ssb_read32,
711 .write8 = ssb_ssb_write8,
712 .write16 = ssb_ssb_write16,
713 .write32 = ssb_ssb_write32,
714 #ifdef CONFIG_SSB_BLOCKIO
715 .block_read = ssb_ssb_block_read,
716 .block_write = ssb_ssb_block_write,
720 static int ssb_fetch_invariants(struct ssb_bus *bus,
721 ssb_invariants_func_t get_invariants)
723 struct ssb_init_invariants iv;
726 memset(&iv, 0, sizeof(iv));
727 err = get_invariants(bus, &iv);
730 memcpy(&bus->boardinfo, &iv.boardinfo, sizeof(iv.boardinfo));
731 memcpy(&bus->sprom, &iv.sprom, sizeof(iv.sprom));
732 bus->has_cardbus_slot = iv.has_cardbus_slot;
737 static int ssb_bus_register(struct ssb_bus *bus,
738 ssb_invariants_func_t get_invariants,
739 unsigned long baseaddr)
743 spin_lock_init(&bus->bar_lock);
744 INIT_LIST_HEAD(&bus->list);
745 #ifdef CONFIG_SSB_EMBEDDED
746 spin_lock_init(&bus->gpio_lock);
749 /* Powerup the bus */
750 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
754 /* Init SDIO-host device (if any), before the scan */
755 err = ssb_sdio_init(bus);
757 goto err_disable_xtal;
760 bus->busnumber = next_busnumber;
761 /* Scan for devices (cores) */
762 err = ssb_bus_scan(bus, baseaddr);
766 /* Init PCI-host device (if any) */
767 err = ssb_pci_init(bus);
770 /* Init PCMCIA-host device (if any) */
771 err = ssb_pcmcia_init(bus);
775 /* Initialize basic system devices (if available) */
776 err = ssb_bus_powerup(bus, 0);
778 goto err_pcmcia_exit;
779 ssb_chipcommon_init(&bus->chipco);
780 ssb_mipscore_init(&bus->mipscore);
781 err = ssb_fetch_invariants(bus, get_invariants);
783 ssb_bus_may_powerdown(bus);
784 goto err_pcmcia_exit;
786 ssb_bus_may_powerdown(bus);
788 /* Queue it for attach.
789 * See the comment at the ssb_is_early_boot definition. */
790 list_add_tail(&bus->list, &attach_queue);
791 if (!ssb_is_early_boot) {
792 /* This is not early boot, so we must attach the bus now */
793 err = ssb_attach_queued_buses();
804 list_del(&bus->list);
806 ssb_pcmcia_exit(bus);
815 ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
819 #ifdef CONFIG_SSB_PCIHOST
820 int ssb_bus_pcibus_register(struct ssb_bus *bus,
821 struct pci_dev *host_pci)
825 bus->bustype = SSB_BUSTYPE_PCI;
826 bus->host_pci = host_pci;
827 bus->ops = &ssb_pci_ops;
829 err = ssb_bus_register(bus, ssb_pci_get_invariants, 0);
831 ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on "
832 "PCI device %s\n", dev_name(&host_pci->dev));
837 EXPORT_SYMBOL(ssb_bus_pcibus_register);
838 #endif /* CONFIG_SSB_PCIHOST */
840 #ifdef CONFIG_SSB_PCMCIAHOST
841 int ssb_bus_pcmciabus_register(struct ssb_bus *bus,
842 struct pcmcia_device *pcmcia_dev,
843 unsigned long baseaddr)
847 bus->bustype = SSB_BUSTYPE_PCMCIA;
848 bus->host_pcmcia = pcmcia_dev;
849 bus->ops = &ssb_pcmcia_ops;
851 err = ssb_bus_register(bus, ssb_pcmcia_get_invariants, baseaddr);
853 ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on "
854 "PCMCIA device %s\n", pcmcia_dev->devname);
859 EXPORT_SYMBOL(ssb_bus_pcmciabus_register);
860 #endif /* CONFIG_SSB_PCMCIAHOST */
862 #ifdef CONFIG_SSB_SDIOHOST
863 int ssb_bus_sdiobus_register(struct ssb_bus *bus, struct sdio_func *func,
868 bus->bustype = SSB_BUSTYPE_SDIO;
869 bus->host_sdio = func;
870 bus->ops = &ssb_sdio_ops;
871 bus->quirks = quirks;
873 err = ssb_bus_register(bus, ssb_sdio_get_invariants, ~0);
875 ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on "
876 "SDIO device %s\n", sdio_func_id(func));
881 EXPORT_SYMBOL(ssb_bus_sdiobus_register);
882 #endif /* CONFIG_SSB_PCMCIAHOST */
884 int ssb_bus_ssbbus_register(struct ssb_bus *bus,
885 unsigned long baseaddr,
886 ssb_invariants_func_t get_invariants)
890 bus->bustype = SSB_BUSTYPE_SSB;
891 bus->ops = &ssb_ssb_ops;
893 err = ssb_bus_register(bus, get_invariants, baseaddr);
895 ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found at "
896 "address 0x%08lX\n", baseaddr);
902 int __ssb_driver_register(struct ssb_driver *drv, struct module *owner)
904 drv->drv.name = drv->name;
905 drv->drv.bus = &ssb_bustype;
906 drv->drv.owner = owner;
908 return driver_register(&drv->drv);
910 EXPORT_SYMBOL(__ssb_driver_register);
912 void ssb_driver_unregister(struct ssb_driver *drv)
914 driver_unregister(&drv->drv);
916 EXPORT_SYMBOL(ssb_driver_unregister);
918 void ssb_set_devtypedata(struct ssb_device *dev, void *data)
920 struct ssb_bus *bus = dev->bus;
921 struct ssb_device *ent;
924 for (i = 0; i < bus->nr_devices; i++) {
925 ent = &(bus->devices[i]);
926 if (ent->id.vendor != dev->id.vendor)
928 if (ent->id.coreid != dev->id.coreid)
931 ent->devtypedata = data;
934 EXPORT_SYMBOL(ssb_set_devtypedata);
936 static u32 clkfactor_f6_resolve(u32 v)
938 /* map the magic values */
940 case SSB_CHIPCO_CLK_F6_2:
942 case SSB_CHIPCO_CLK_F6_3:
944 case SSB_CHIPCO_CLK_F6_4:
946 case SSB_CHIPCO_CLK_F6_5:
948 case SSB_CHIPCO_CLK_F6_6:
950 case SSB_CHIPCO_CLK_F6_7:
956 /* Calculate the speed the backplane would run at a given set of clockcontrol values */
957 u32 ssb_calc_clock_rate(u32 plltype, u32 n, u32 m)
959 u32 n1, n2, clock, m1, m2, m3, mc;
961 n1 = (n & SSB_CHIPCO_CLK_N1);
962 n2 = ((n & SSB_CHIPCO_CLK_N2) >> SSB_CHIPCO_CLK_N2_SHIFT);
965 case SSB_PLLTYPE_6: /* 100/200 or 120/240 only */
966 if (m & SSB_CHIPCO_CLK_T6_MMASK)
967 return SSB_CHIPCO_CLK_T6_M0;
968 return SSB_CHIPCO_CLK_T6_M1;
969 case SSB_PLLTYPE_1: /* 48Mhz base, 3 dividers */
970 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
971 case SSB_PLLTYPE_4: /* 48Mhz, 4 dividers */
972 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
973 n1 = clkfactor_f6_resolve(n1);
974 n2 += SSB_CHIPCO_CLK_F5_BIAS;
976 case SSB_PLLTYPE_2: /* 48Mhz, 4 dividers */
977 n1 += SSB_CHIPCO_CLK_T2_BIAS;
978 n2 += SSB_CHIPCO_CLK_T2_BIAS;
979 SSB_WARN_ON(!((n1 >= 2) && (n1 <= 7)));
980 SSB_WARN_ON(!((n2 >= 5) && (n2 <= 23)));
982 case SSB_PLLTYPE_5: /* 25Mhz, 4 dividers */
989 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
990 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
991 clock = SSB_CHIPCO_CLK_BASE2 * n1 * n2;
994 clock = SSB_CHIPCO_CLK_BASE1 * n1 * n2;
999 m1 = (m & SSB_CHIPCO_CLK_M1);
1000 m2 = ((m & SSB_CHIPCO_CLK_M2) >> SSB_CHIPCO_CLK_M2_SHIFT);
1001 m3 = ((m & SSB_CHIPCO_CLK_M3) >> SSB_CHIPCO_CLK_M3_SHIFT);
1002 mc = ((m & SSB_CHIPCO_CLK_MC) >> SSB_CHIPCO_CLK_MC_SHIFT);
1005 case SSB_PLLTYPE_1: /* 48Mhz base, 3 dividers */
1006 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
1007 case SSB_PLLTYPE_4: /* 48Mhz, 4 dividers */
1008 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
1009 m1 = clkfactor_f6_resolve(m1);
1010 if ((plltype == SSB_PLLTYPE_1) ||
1011 (plltype == SSB_PLLTYPE_3))
1012 m2 += SSB_CHIPCO_CLK_F5_BIAS;
1014 m2 = clkfactor_f6_resolve(m2);
1015 m3 = clkfactor_f6_resolve(m3);
1018 case SSB_CHIPCO_CLK_MC_BYPASS:
1020 case SSB_CHIPCO_CLK_MC_M1:
1021 return (clock / m1);
1022 case SSB_CHIPCO_CLK_MC_M1M2:
1023 return (clock / (m1 * m2));
1024 case SSB_CHIPCO_CLK_MC_M1M2M3:
1025 return (clock / (m1 * m2 * m3));
1026 case SSB_CHIPCO_CLK_MC_M1M3:
1027 return (clock / (m1 * m3));
1031 m1 += SSB_CHIPCO_CLK_T2_BIAS;
1032 m2 += SSB_CHIPCO_CLK_T2M2_BIAS;
1033 m3 += SSB_CHIPCO_CLK_T2_BIAS;
1034 SSB_WARN_ON(!((m1 >= 2) && (m1 <= 7)));
1035 SSB_WARN_ON(!((m2 >= 3) && (m2 <= 10)));
1036 SSB_WARN_ON(!((m3 >= 2) && (m3 <= 7)));
1038 if (!(mc & SSB_CHIPCO_CLK_T2MC_M1BYP))
1040 if (!(mc & SSB_CHIPCO_CLK_T2MC_M2BYP))
1042 if (!(mc & SSB_CHIPCO_CLK_T2MC_M3BYP))
1051 /* Get the current speed the backplane is running at */
1052 u32 ssb_clockspeed(struct ssb_bus *bus)
1056 u32 clkctl_n, clkctl_m;
1058 if (ssb_extif_available(&bus->extif))
1059 ssb_extif_get_clockcontrol(&bus->extif, &plltype,
1060 &clkctl_n, &clkctl_m);
1061 else if (bus->chipco.dev)
1062 ssb_chipco_get_clockcontrol(&bus->chipco, &plltype,
1063 &clkctl_n, &clkctl_m);
1067 if (bus->chip_id == 0x5365) {
1070 rate = ssb_calc_clock_rate(plltype, clkctl_n, clkctl_m);
1071 if (plltype == SSB_PLLTYPE_3) /* 25Mhz, 2 dividers */
1077 EXPORT_SYMBOL(ssb_clockspeed);
1079 static u32 ssb_tmslow_reject_bitmask(struct ssb_device *dev)
1081 u32 rev = ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_SSBREV;
1083 /* The REJECT bit changed position in TMSLOW between
1084 * Backplane revisions. */
1086 case SSB_IDLOW_SSBREV_22:
1087 return SSB_TMSLOW_REJECT_22;
1088 case SSB_IDLOW_SSBREV_23:
1089 return SSB_TMSLOW_REJECT_23;
1090 case SSB_IDLOW_SSBREV_24: /* TODO - find the proper REJECT bits */
1091 case SSB_IDLOW_SSBREV_25: /* same here */
1092 case SSB_IDLOW_SSBREV_26: /* same here */
1093 case SSB_IDLOW_SSBREV_27: /* same here */
1094 return SSB_TMSLOW_REJECT_23; /* this is a guess */
1096 printk(KERN_INFO "ssb: Backplane Revision 0x%.8X\n", rev);
1099 return (SSB_TMSLOW_REJECT_22 | SSB_TMSLOW_REJECT_23);
1102 int ssb_device_is_enabled(struct ssb_device *dev)
1107 reject = ssb_tmslow_reject_bitmask(dev);
1108 val = ssb_read32(dev, SSB_TMSLOW);
1109 val &= SSB_TMSLOW_CLOCK | SSB_TMSLOW_RESET | reject;
1111 return (val == SSB_TMSLOW_CLOCK);
1113 EXPORT_SYMBOL(ssb_device_is_enabled);
1115 static void ssb_flush_tmslow(struct ssb_device *dev)
1117 /* Make _really_ sure the device has finished the TMSLOW
1118 * register write transaction, as we risk running into
1119 * a machine check exception otherwise.
1120 * Do this by reading the register back to commit the
1121 * PCI write and delay an additional usec for the device
1122 * to react to the change. */
1123 ssb_read32(dev, SSB_TMSLOW);
1127 void ssb_device_enable(struct ssb_device *dev, u32 core_specific_flags)
1131 ssb_device_disable(dev, core_specific_flags);
1132 ssb_write32(dev, SSB_TMSLOW,
1133 SSB_TMSLOW_RESET | SSB_TMSLOW_CLOCK |
1134 SSB_TMSLOW_FGC | core_specific_flags);
1135 ssb_flush_tmslow(dev);
1137 /* Clear SERR if set. This is a hw bug workaround. */
1138 if (ssb_read32(dev, SSB_TMSHIGH) & SSB_TMSHIGH_SERR)
1139 ssb_write32(dev, SSB_TMSHIGH, 0);
1141 val = ssb_read32(dev, SSB_IMSTATE);
1142 if (val & (SSB_IMSTATE_IBE | SSB_IMSTATE_TO)) {
1143 val &= ~(SSB_IMSTATE_IBE | SSB_IMSTATE_TO);
1144 ssb_write32(dev, SSB_IMSTATE, val);
1147 ssb_write32(dev, SSB_TMSLOW,
1148 SSB_TMSLOW_CLOCK | SSB_TMSLOW_FGC |
1149 core_specific_flags);
1150 ssb_flush_tmslow(dev);
1152 ssb_write32(dev, SSB_TMSLOW, SSB_TMSLOW_CLOCK |
1153 core_specific_flags);
1154 ssb_flush_tmslow(dev);
1156 EXPORT_SYMBOL(ssb_device_enable);
1158 /* Wait for a bit in a register to get set or unset.
1159 * timeout is in units of ten-microseconds */
1160 static int ssb_wait_bit(struct ssb_device *dev, u16 reg, u32 bitmask,
1161 int timeout, int set)
1166 for (i = 0; i < timeout; i++) {
1167 val = ssb_read32(dev, reg);
1172 if (!(val & bitmask))
1177 printk(KERN_ERR PFX "Timeout waiting for bitmask %08X on "
1178 "register %04X to %s.\n",
1179 bitmask, reg, (set ? "set" : "clear"));
1184 void ssb_device_disable(struct ssb_device *dev, u32 core_specific_flags)
1188 if (ssb_read32(dev, SSB_TMSLOW) & SSB_TMSLOW_RESET)
1191 reject = ssb_tmslow_reject_bitmask(dev);
1192 ssb_write32(dev, SSB_TMSLOW, reject | SSB_TMSLOW_CLOCK);
1193 ssb_wait_bit(dev, SSB_TMSLOW, reject, 1000, 1);
1194 ssb_wait_bit(dev, SSB_TMSHIGH, SSB_TMSHIGH_BUSY, 1000, 0);
1195 ssb_write32(dev, SSB_TMSLOW,
1196 SSB_TMSLOW_FGC | SSB_TMSLOW_CLOCK |
1197 reject | SSB_TMSLOW_RESET |
1198 core_specific_flags);
1199 ssb_flush_tmslow(dev);
1201 ssb_write32(dev, SSB_TMSLOW,
1202 reject | SSB_TMSLOW_RESET |
1203 core_specific_flags);
1204 ssb_flush_tmslow(dev);
1206 EXPORT_SYMBOL(ssb_device_disable);
1208 u32 ssb_dma_translation(struct ssb_device *dev)
1210 switch (dev->bus->bustype) {
1211 case SSB_BUSTYPE_SSB:
1213 case SSB_BUSTYPE_PCI:
1216 __ssb_dma_not_implemented(dev);
1220 EXPORT_SYMBOL(ssb_dma_translation);
1222 int ssb_dma_set_mask(struct ssb_device *dev, u64 mask)
1224 #ifdef CONFIG_SSB_PCIHOST
1228 switch (dev->bus->bustype) {
1229 case SSB_BUSTYPE_PCI:
1230 #ifdef CONFIG_SSB_PCIHOST
1231 err = pci_set_dma_mask(dev->bus->host_pci, mask);
1234 err = pci_set_consistent_dma_mask(dev->bus->host_pci, mask);
1237 case SSB_BUSTYPE_SSB:
1238 return dma_set_mask(dev->dev, mask);
1240 __ssb_dma_not_implemented(dev);
1244 EXPORT_SYMBOL(ssb_dma_set_mask);
1246 void * ssb_dma_alloc_consistent(struct ssb_device *dev, size_t size,
1247 dma_addr_t *dma_handle, gfp_t gfp_flags)
1249 switch (dev->bus->bustype) {
1250 case SSB_BUSTYPE_PCI:
1251 #ifdef CONFIG_SSB_PCIHOST
1252 if (gfp_flags & GFP_DMA) {
1253 /* Workaround: The PCI API does not support passing
1255 return dma_alloc_coherent(&dev->bus->host_pci->dev,
1256 size, dma_handle, gfp_flags);
1258 return pci_alloc_consistent(dev->bus->host_pci, size, dma_handle);
1260 case SSB_BUSTYPE_SSB:
1261 return dma_alloc_coherent(dev->dev, size, dma_handle, gfp_flags);
1263 __ssb_dma_not_implemented(dev);
1267 EXPORT_SYMBOL(ssb_dma_alloc_consistent);
1269 void ssb_dma_free_consistent(struct ssb_device *dev, size_t size,
1270 void *vaddr, dma_addr_t dma_handle,
1273 switch (dev->bus->bustype) {
1274 case SSB_BUSTYPE_PCI:
1275 #ifdef CONFIG_SSB_PCIHOST
1276 if (gfp_flags & GFP_DMA) {
1277 /* Workaround: The PCI API does not support passing
1279 dma_free_coherent(&dev->bus->host_pci->dev,
1280 size, vaddr, dma_handle);
1283 pci_free_consistent(dev->bus->host_pci, size,
1287 case SSB_BUSTYPE_SSB:
1288 dma_free_coherent(dev->dev, size, vaddr, dma_handle);
1291 __ssb_dma_not_implemented(dev);
1294 EXPORT_SYMBOL(ssb_dma_free_consistent);
1296 int ssb_bus_may_powerdown(struct ssb_bus *bus)
1298 struct ssb_chipcommon *cc;
1301 /* On buses where more than one core may be working
1302 * at a time, we must not powerdown stuff if there are
1303 * still cores that may want to run. */
1304 if (bus->bustype == SSB_BUSTYPE_SSB)
1311 if (cc->dev->id.revision < 5)
1314 ssb_chipco_set_clockmode(cc, SSB_CLKMODE_SLOW);
1315 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
1319 #ifdef CONFIG_SSB_DEBUG
1320 bus->powered_up = 0;
1324 ssb_printk(KERN_ERR PFX "Bus powerdown failed\n");
1327 EXPORT_SYMBOL(ssb_bus_may_powerdown);
1329 int ssb_bus_powerup(struct ssb_bus *bus, bool dynamic_pctl)
1331 struct ssb_chipcommon *cc;
1333 enum ssb_clkmode mode;
1335 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
1339 mode = dynamic_pctl ? SSB_CLKMODE_DYNAMIC : SSB_CLKMODE_FAST;
1340 ssb_chipco_set_clockmode(cc, mode);
1342 #ifdef CONFIG_SSB_DEBUG
1343 bus->powered_up = 1;
1347 ssb_printk(KERN_ERR PFX "Bus powerup failed\n");
1350 EXPORT_SYMBOL(ssb_bus_powerup);
1352 u32 ssb_admatch_base(u32 adm)
1356 switch (adm & SSB_ADM_TYPE) {
1358 base = (adm & SSB_ADM_BASE0);
1361 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1362 base = (adm & SSB_ADM_BASE1);
1365 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1366 base = (adm & SSB_ADM_BASE2);
1374 EXPORT_SYMBOL(ssb_admatch_base);
1376 u32 ssb_admatch_size(u32 adm)
1380 switch (adm & SSB_ADM_TYPE) {
1382 size = ((adm & SSB_ADM_SZ0) >> SSB_ADM_SZ0_SHIFT);
1385 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1386 size = ((adm & SSB_ADM_SZ1) >> SSB_ADM_SZ1_SHIFT);
1389 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1390 size = ((adm & SSB_ADM_SZ2) >> SSB_ADM_SZ2_SHIFT);
1395 size = (1 << (size + 1));
1399 EXPORT_SYMBOL(ssb_admatch_size);
1401 static int __init ssb_modinit(void)
1405 /* See the comment at the ssb_is_early_boot definition */
1406 ssb_is_early_boot = 0;
1407 err = bus_register(&ssb_bustype);
1411 /* Maybe we already registered some buses at early boot.
1412 * Check for this and attach them
1415 err = ssb_attach_queued_buses();
1418 bus_unregister(&ssb_bustype);
1422 err = b43_pci_ssb_bridge_init();
1424 ssb_printk(KERN_ERR "Broadcom 43xx PCI-SSB-bridge "
1425 "initialization failed\n");
1426 /* don't fail SSB init because of this */
1429 err = ssb_gige_init();
1431 ssb_printk(KERN_ERR "SSB Broadcom Gigabit Ethernet "
1432 "driver initialization failed\n");
1433 /* don't fail SSB init because of this */
1439 /* ssb must be initialized after PCI but before the ssb drivers.
1440 * That means we must use some initcall between subsys_initcall
1441 * and device_initcall. */
1442 fs_initcall(ssb_modinit);
1444 static void __exit ssb_modexit(void)
1447 b43_pci_ssb_bridge_exit();
1448 bus_unregister(&ssb_bustype);
1450 module_exit(ssb_modexit)