Merge branch 'bkl/fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[safe/jmp/linux-2.6] / drivers / ssb / main.c
index 19ddd2b..51275aa 100644 (file)
@@ -17,6 +17,8 @@
 #include <linux/ssb/ssb_driver_gige.h>
 #include <linux/dma-mapping.h>
 #include <linux/pci.h>
+#include <linux/mmc/sdio_func.h>
+#include <linux/slab.h>
 
 #include <pcmcia/cs_types.h>
 #include <pcmcia/cs.h>
@@ -88,6 +90,25 @@ found:
 }
 #endif /* CONFIG_SSB_PCMCIAHOST */
 
+#ifdef CONFIG_SSB_SDIOHOST
+struct ssb_bus *ssb_sdio_func_to_bus(struct sdio_func *func)
+{
+       struct ssb_bus *bus;
+
+       ssb_buses_lock();
+       list_for_each_entry(bus, &buses, list) {
+               if (bus->bustype == SSB_BUSTYPE_SDIO &&
+                   bus->host_sdio == func)
+                       goto found;
+       }
+       bus = NULL;
+found:
+       ssb_buses_unlock();
+
+       return bus;
+}
+#endif /* CONFIG_SSB_SDIOHOST */
+
 int ssb_for_each_bus_call(unsigned long data,
                          int (*func)(struct ssb_bus *bus, unsigned long data))
 {
@@ -120,6 +141,19 @@ static void ssb_device_put(struct ssb_device *dev)
                put_device(dev->dev);
 }
 
+static inline struct ssb_driver *ssb_driver_get(struct ssb_driver *drv)
+{
+       if (drv)
+               get_driver(&drv->drv);
+       return drv;
+}
+
+static inline void ssb_driver_put(struct ssb_driver *drv)
+{
+       if (drv)
+               put_driver(&drv->drv);
+}
+
 static int ssb_device_resume(struct device *dev)
 {
        struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
@@ -190,90 +224,81 @@ int ssb_bus_suspend(struct ssb_bus *bus)
 EXPORT_SYMBOL(ssb_bus_suspend);
 
 #ifdef CONFIG_SSB_SPROM
-int ssb_devices_freeze(struct ssb_bus *bus)
+/** ssb_devices_freeze - Freeze all devices on the bus.
+ *
+ * After freezing no device driver will be handling a device
+ * on this bus anymore. ssb_devices_thaw() must be called after
+ * a successful freeze to reactivate the devices.
+ *
+ * @bus: The bus.
+ * @ctx: Context structure. Pass this to ssb_devices_thaw().
+ */
+int ssb_devices_freeze(struct ssb_bus *bus, struct ssb_freeze_context *ctx)
 {
-       struct ssb_device *dev;
-       struct ssb_driver *drv;
-       int err = 0;
-       int i;
-       pm_message_t state = PMSG_FREEZE;
+       struct ssb_device *sdev;
+       struct ssb_driver *sdrv;
+       unsigned int i;
+
+       memset(ctx, 0, sizeof(*ctx));
+       ctx->bus = bus;
+       SSB_WARN_ON(bus->nr_devices > ARRAY_SIZE(ctx->device_frozen));
 
-       /* First check that we are capable to freeze all devices. */
        for (i = 0; i < bus->nr_devices; i++) {
-               dev = &(bus->devices[i]);
-               if (!dev->dev ||
-                   !dev->dev->driver ||
-                   !device_is_registered(dev->dev))
-                       continue;
-               drv = drv_to_ssb_drv(dev->dev->driver);
-               if (!drv)
+               sdev = ssb_device_get(&bus->devices[i]);
+
+               if (!sdev->dev || !sdev->dev->driver ||
+                   !device_is_registered(sdev->dev)) {
+                       ssb_device_put(sdev);
                        continue;
-               if (!drv->suspend) {
-                       /* Nope, can't suspend this one. */
-                       return -EOPNOTSUPP;
                }
-       }
-       /* Now suspend all devices */
-       for (i = 0; i < bus->nr_devices; i++) {
-               dev = &(bus->devices[i]);
-               if (!dev->dev ||
-                   !dev->dev->driver ||
-                   !device_is_registered(dev->dev))
+               sdrv = ssb_driver_get(drv_to_ssb_drv(sdev->dev->driver));
+               if (!sdrv || SSB_WARN_ON(!sdrv->remove)) {
+                       ssb_device_put(sdev);
                        continue;
-               drv = drv_to_ssb_drv(dev->dev->driver);
-               if (!drv)
-                       continue;
-               err = drv->suspend(dev, state);
-               if (err) {
-                       ssb_printk(KERN_ERR PFX "Failed to freeze device %s\n",
-                                  dev->dev->bus_id);
-                       goto err_unwind;
                }
+               sdrv->remove(sdev);
+               ctx->device_frozen[i] = 1;
        }
 
        return 0;
-err_unwind:
-       for (i--; i >= 0; i--) {
-               dev = &(bus->devices[i]);
-               if (!dev->dev ||
-                   !dev->dev->driver ||
-                   !device_is_registered(dev->dev))
-                       continue;
-               drv = drv_to_ssb_drv(dev->dev->driver);
-               if (!drv)
-                       continue;
-               if (drv->resume)
-                       drv->resume(dev);
-       }
-       return err;
 }
 
-int ssb_devices_thaw(struct ssb_bus *bus)
+/** ssb_devices_thaw - Unfreeze all devices on the bus.
+ *
+ * This will re-attach the device drivers and re-init the devices.
+ *
+ * @ctx: The context structure from ssb_devices_freeze()
+ */
+int ssb_devices_thaw(struct ssb_freeze_context *ctx)
 {
-       struct ssb_device *dev;
-       struct ssb_driver *drv;
-       int err;
-       int i;
+       struct ssb_bus *bus = ctx->bus;
+       struct ssb_device *sdev;
+       struct ssb_driver *sdrv;
+       unsigned int i;
+       int err, result = 0;
 
        for (i = 0; i < bus->nr_devices; i++) {
-               dev = &(bus->devices[i]);
-               if (!dev->dev ||
-                   !dev->dev->driver ||
-                   !device_is_registered(dev->dev))
+               if (!ctx->device_frozen[i])
                        continue;
-               drv = drv_to_ssb_drv(dev->dev->driver);
-               if (!drv)
+               sdev = &bus->devices[i];
+
+               if (SSB_WARN_ON(!sdev->dev || !sdev->dev->driver))
                        continue;
-               if (SSB_WARN_ON(!drv->resume))
+               sdrv = drv_to_ssb_drv(sdev->dev->driver);
+               if (SSB_WARN_ON(!sdrv || !sdrv->probe))
                        continue;
-               err = drv->resume(dev);
+
+               err = sdrv->probe(sdev, &sdev->id);
                if (err) {
                        ssb_printk(KERN_ERR PFX "Failed to thaw device %s\n",
-                                  dev->dev->bus_id);
+                                  dev_name(sdev->dev));
+                       result = err;
                }
+               ssb_driver_put(sdrv);
+               ssb_device_put(sdev);
        }
 
-       return 0;
+       return result;
 }
 #endif /* CONFIG_SSB_SPROM */
 
@@ -454,8 +479,7 @@ static int ssb_devices_register(struct ssb_bus *bus)
 
                dev->release = ssb_release_dev;
                dev->bus = &ssb_bustype;
-               snprintf(dev->bus_id, sizeof(dev->bus_id),
-                        "ssb%u:%d", bus->busnumber, dev_idx);
+               dev_set_name(dev, "ssb%u:%d", bus->busnumber, dev_idx);
 
                switch (bus->bustype) {
                case SSB_BUSTYPE_PCI:
@@ -466,11 +490,17 @@ static int ssb_devices_register(struct ssb_bus *bus)
                        break;
                case SSB_BUSTYPE_PCMCIA:
 #ifdef CONFIG_SSB_PCMCIAHOST
-                       sdev->irq = bus->host_pcmcia->irq.AssignedIRQ;
+                       sdev->irq = bus->host_pcmcia->irq;
                        dev->parent = &bus->host_pcmcia->dev;
 #endif
                        break;
+               case SSB_BUSTYPE_SDIO:
+#ifdef CONFIG_SSB_SDIOHOST
+                       dev->parent = &bus->host_sdio->dev;
+#endif
+                       break;
                case SSB_BUSTYPE_SSB:
+                       dev->dma_mask = &dev->coherent_dma_mask;
                        break;
                }
 
@@ -479,7 +509,7 @@ static int ssb_devices_register(struct ssb_bus *bus)
                if (err) {
                        ssb_printk(KERN_ERR PFX
                                   "Could not register %s\n",
-                                  dev->bus_id);
+                                  dev_name(dev));
                        /* Set dev to NULL to not unregister
                         * dev on error unwinding. */
                        sdev->dev = NULL;
@@ -724,12 +754,18 @@ static int ssb_bus_register(struct ssb_bus *bus,
        err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
        if (err)
                goto out;
+
+       /* Init SDIO-host device (if any), before the scan */
+       err = ssb_sdio_init(bus);
+       if (err)
+               goto err_disable_xtal;
+
        ssb_buses_lock();
        bus->busnumber = next_busnumber;
        /* Scan for devices (cores) */
        err = ssb_bus_scan(bus, baseaddr);
        if (err)
-               goto err_disable_xtal;
+               goto err_sdio_exit;
 
        /* Init PCI-host device (if any) */
        err = ssb_pci_init(bus);
@@ -776,6 +812,8 @@ err_pci_exit:
        ssb_pci_exit(bus);
 err_unmap:
        ssb_iounmap(bus);
+err_sdio_exit:
+       ssb_sdio_exit(bus);
 err_disable_xtal:
        ssb_buses_unlock();
        ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
@@ -795,7 +833,10 @@ int ssb_bus_pcibus_register(struct ssb_bus *bus,
        err = ssb_bus_register(bus, ssb_pci_get_invariants, 0);
        if (!err) {
                ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on "
-                          "PCI device %s\n", host_pci->dev.bus_id);
+                          "PCI device %s\n", dev_name(&host_pci->dev));
+       } else {
+               ssb_printk(KERN_ERR PFX "Failed to register PCI version"
+                          " of SSB with error %d\n", err);
        }
 
        return err;
@@ -825,6 +866,28 @@ int ssb_bus_pcmciabus_register(struct ssb_bus *bus,
 EXPORT_SYMBOL(ssb_bus_pcmciabus_register);
 #endif /* CONFIG_SSB_PCMCIAHOST */
 
+#ifdef CONFIG_SSB_SDIOHOST
+int ssb_bus_sdiobus_register(struct ssb_bus *bus, struct sdio_func *func,
+                            unsigned int quirks)
+{
+       int err;
+
+       bus->bustype = SSB_BUSTYPE_SDIO;
+       bus->host_sdio = func;
+       bus->ops = &ssb_sdio_ops;
+       bus->quirks = quirks;
+
+       err = ssb_bus_register(bus, ssb_sdio_get_invariants, ~0);
+       if (!err) {
+               ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on "
+                          "SDIO device %s\n", sdio_func_id(func));
+       }
+
+       return err;
+}
+EXPORT_SYMBOL(ssb_bus_sdiobus_register);
+#endif /* CONFIG_SSB_PCMCIAHOST */
+
 int ssb_bus_ssbbus_register(struct ssb_bus *bus,
                            unsigned long baseaddr,
                            ssb_invariants_func_t get_invariants)
@@ -1153,31 +1216,90 @@ u32 ssb_dma_translation(struct ssb_device *dev)
 {
        switch (dev->bus->bustype) {
        case SSB_BUSTYPE_SSB:
-       case SSB_BUSTYPE_PCMCIA:
                return 0;
        case SSB_BUSTYPE_PCI:
                return SSB_PCI_DMA;
+       default:
+               __ssb_dma_not_implemented(dev);
        }
        return 0;
 }
 EXPORT_SYMBOL(ssb_dma_translation);
 
-int ssb_dma_set_mask(struct ssb_device *ssb_dev, u64 mask)
+int ssb_dma_set_mask(struct ssb_device *dev, u64 mask)
 {
-       struct device *dev = ssb_dev->dev;
-
 #ifdef CONFIG_SSB_PCIHOST
-       if (ssb_dev->bus->bustype == SSB_BUSTYPE_PCI &&
-           !dma_supported(dev, mask))
-               return -EIO;
+       int err;
 #endif
-       dev->coherent_dma_mask = mask;
-       dev->dma_mask = &dev->coherent_dma_mask;
 
-       return 0;
+       switch (dev->bus->bustype) {
+       case SSB_BUSTYPE_PCI:
+#ifdef CONFIG_SSB_PCIHOST
+               err = pci_set_dma_mask(dev->bus->host_pci, mask);
+               if (err)
+                       return err;
+               err = pci_set_consistent_dma_mask(dev->bus->host_pci, mask);
+               return err;
+#endif
+       case SSB_BUSTYPE_SSB:
+               return dma_set_mask(dev->dev, mask);
+       default:
+               __ssb_dma_not_implemented(dev);
+       }
+       return -ENOSYS;
 }
 EXPORT_SYMBOL(ssb_dma_set_mask);
 
+void * ssb_dma_alloc_consistent(struct ssb_device *dev, size_t size,
+                               dma_addr_t *dma_handle, gfp_t gfp_flags)
+{
+       switch (dev->bus->bustype) {
+       case SSB_BUSTYPE_PCI:
+#ifdef CONFIG_SSB_PCIHOST
+               if (gfp_flags & GFP_DMA) {
+                       /* Workaround: The PCI API does not support passing
+                        * a GFP flag. */
+                       return dma_alloc_coherent(&dev->bus->host_pci->dev,
+                                                 size, dma_handle, gfp_flags);
+               }
+               return pci_alloc_consistent(dev->bus->host_pci, size, dma_handle);
+#endif
+       case SSB_BUSTYPE_SSB:
+               return dma_alloc_coherent(dev->dev, size, dma_handle, gfp_flags);
+       default:
+               __ssb_dma_not_implemented(dev);
+       }
+       return NULL;
+}
+EXPORT_SYMBOL(ssb_dma_alloc_consistent);
+
+void ssb_dma_free_consistent(struct ssb_device *dev, size_t size,
+                            void *vaddr, dma_addr_t dma_handle,
+                            gfp_t gfp_flags)
+{
+       switch (dev->bus->bustype) {
+       case SSB_BUSTYPE_PCI:
+#ifdef CONFIG_SSB_PCIHOST
+               if (gfp_flags & GFP_DMA) {
+                       /* Workaround: The PCI API does not support passing
+                        * a GFP flag. */
+                       dma_free_coherent(&dev->bus->host_pci->dev,
+                                         size, vaddr, dma_handle);
+                       return;
+               }
+               pci_free_consistent(dev->bus->host_pci, size,
+                                   vaddr, dma_handle);
+               return;
+#endif
+       case SSB_BUSTYPE_SSB:
+               dma_free_coherent(dev->dev, size, vaddr, dma_handle);
+               return;
+       default:
+               __ssb_dma_not_implemented(dev);
+       }
+}
+EXPORT_SYMBOL(ssb_dma_free_consistent);
+
 int ssb_bus_may_powerdown(struct ssb_bus *bus)
 {
        struct ssb_chipcommon *cc;
@@ -1190,6 +1312,12 @@ int ssb_bus_may_powerdown(struct ssb_bus *bus)
                goto out;
 
        cc = &bus->chipco;
+
+       if (!cc->dev)
+               goto out;
+       if (cc->dev->id.revision < 5)
+               goto out;
+
        ssb_chipco_set_clockmode(cc, SSB_CLKMODE_SLOW);
        err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
        if (err)
@@ -1293,8 +1421,10 @@ static int __init ssb_modinit(void)
        ssb_buses_lock();
        err = ssb_attach_queued_buses();
        ssb_buses_unlock();
-       if (err)
+       if (err) {
                bus_unregister(&ssb_bustype);
+               goto out;
+       }
 
        err = b43_pci_ssb_bridge_init();
        if (err) {
@@ -1310,7 +1440,7 @@ static int __init ssb_modinit(void)
                /* don't fail SSB init because of this */
                err = 0;
        }
-
+out:
        return err;
 }
 /* ssb must be initialized after PCI but before the ssb drivers.