++ ssb_chipco_regctl_maskset(cc, addr, ~(mask << shift),
++ (voltage & mask) << shift);
++}
++
++void ssb_pmu_set_ldo_paref(struct ssb_chipcommon *cc, bool on)
++{
++ struct ssb_bus *bus = cc->dev->bus;
++ int ldo;
++
++ switch (bus->chip_id) {
++ case 0x4312:
++ ldo = SSB_PMURES_4312_PA_REF_LDO;
++ break;
++ case 0x4328:
++ ldo = SSB_PMURES_4328_PA_REF_LDO;
++ break;
++ case 0x5354:
++ ldo = SSB_PMURES_5354_PA_REF_LDO;
++ break;
++ default:
++ return;
++ }
++
++ if (on)
++ chipco_set32(cc, SSB_CHIPCO_PMU_MINRES_MSK, 1 << ldo);
++ else
++ chipco_mask32(cc, SSB_CHIPCO_PMU_MINRES_MSK, ~(1 << ldo));
++ chipco_read32(cc, SSB_CHIPCO_PMU_MINRES_MSK); //SPEC FIXME found via mmiotrace - dummy read?
++}
++
++EXPORT_SYMBOL(ssb_pmu_set_ldo_voltage);
++EXPORT_SYMBOL(ssb_pmu_set_ldo_paref);
+--- a/drivers/ssb/driver_gige.c
++++ b/drivers/ssb/driver_gige.c
+@@ -3,7 +3,7 @@
+ * Broadcom Gigabit Ethernet core driver
+ *
+ * Copyright 2008, Broadcom Corporation
+- * Copyright 2008, Michael Buesch <mb@bu3sch.de>
++ * Copyright 2008, Michael Buesch <m@bues.ch>
+ *
+ * Licensed under the GNU/GPL. See COPYING for details.
+ */
+@@ -12,6 +12,7 @@
+ #include <linux/ssb/ssb_driver_gige.h>
+ #include <linux/pci.h>
+ #include <linux/pci_regs.h>
++#include <linux/slab.h>
+
+
+ /*
+@@ -105,8 +106,9 @@ void gige_pcicfg_write32(struct ssb_gige
+ gige_write32(dev, SSB_GIGE_PCICFG + offset, value);
+ }
+
+-static int ssb_gige_pci_read_config(struct pci_bus *bus, unsigned int devfn,
+- int reg, int size, u32 *val)
++static int __devinit ssb_gige_pci_read_config(struct pci_bus *bus,
++ unsigned int devfn, int reg,
++ int size, u32 *val)
+ {
+ struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops);
+ unsigned long flags;
+@@ -135,8 +137,9 @@ static int ssb_gige_pci_read_config(stru
+ return PCIBIOS_SUCCESSFUL;
+ }
+
+-static int ssb_gige_pci_write_config(struct pci_bus *bus, unsigned int devfn,
+- int reg, int size, u32 val)
++static int __devinit ssb_gige_pci_write_config(struct pci_bus *bus,
++ unsigned int devfn, int reg,
++ int size, u32 val)
+ {
+ struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops);
+ unsigned long flags;
+@@ -165,7 +168,8 @@ static int ssb_gige_pci_write_config(str
+ return PCIBIOS_SUCCESSFUL;
+ }
+
+-static int ssb_gige_probe(struct ssb_device *sdev, const struct ssb_device_id *id)
++static int __devinit ssb_gige_probe(struct ssb_device *sdev,
++ const struct ssb_device_id *id)
+ {
+ struct ssb_gige *dev;
+ u32 base, tmslow, tmshigh;
+--- a/drivers/ssb/driver_mipscore.c
++++ b/drivers/ssb/driver_mipscore.c
+@@ -3,7 +3,7 @@
+ * Broadcom MIPS core driver
+ *
+ * Copyright 2005, Broadcom Corporation
+- * Copyright 2006, 2007, Michael Buesch <mb@bu3sch.de>
++ * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
+ *
+ * Licensed under the GNU/GPL. See COPYING for details.
+ */
+@@ -270,7 +270,6 @@ void ssb_mipscore_init(struct ssb_mipsco
+ set_irq(dev, irq++);
+ }
+ break;
+- /* fallthrough */
+ case SSB_DEV_PCI:
+ case SSB_DEV_ETHERNET:
+ case SSB_DEV_ETHERNET_GBIT:
+@@ -281,6 +280,10 @@ void ssb_mipscore_init(struct ssb_mipsco
+ set_irq(dev, irq++);
+ break;
+ }
++ /* fallthrough */
++ case SSB_DEV_EXTIF:
++ set_irq(dev, 0);
++ break;
+ }
+ }
+ ssb_dprintk(KERN_INFO PFX "after irq reconfiguration\n");
+--- a/drivers/ssb/driver_pcicore.c
++++ b/drivers/ssb/driver_pcicore.c
+@@ -3,7 +3,7 @@
+ * Broadcom PCI-core driver
+ *
+ * Copyright 2005, Broadcom Corporation
+- * Copyright 2006, 2007, Michael Buesch <mb@bu3sch.de>
++ * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
+ *
+ * Licensed under the GNU/GPL. See COPYING for details.
+ */
+@@ -15,6 +15,11 @@
+
+ #include "ssb_private.h"
+
++static u32 ssb_pcie_read(struct ssb_pcicore *pc, u32 address);
++static void ssb_pcie_write(struct ssb_pcicore *pc, u32 address, u32 data);
++static u16 ssb_pcie_mdio_read(struct ssb_pcicore *pc, u8 device, u8 address);
++static void ssb_pcie_mdio_write(struct ssb_pcicore *pc, u8 device,
++ u8 address, u16 data);
+
+ static inline
+ u32 pcicore_read32(struct ssb_pcicore *pc, u16 offset)
+@@ -246,20 +251,12 @@ static struct pci_controller ssb_pcicore
+ .pci_ops = &ssb_pcicore_pciops,
+ .io_resource = &ssb_pcicore_io_resource,
+ .mem_resource = &ssb_pcicore_mem_resource,
+- .mem_offset = 0x24000000,
+ };
+
+-static u32 ssb_pcicore_pcibus_iobase = 0x100;
+-static u32 ssb_pcicore_pcibus_membase = SSB_PCI_DMA;
+-
+ /* This function is called when doing a pci_enable_device().
+ * We must first check if the device is a device on the PCI-core bridge. */
+ int ssb_pcicore_plat_dev_init(struct pci_dev *d)
+ {
+- struct resource *res;
+- int pos, size;
+- u32 *base;
+-
+ if (d->bus->ops != &ssb_pcicore_pciops) {
+ /* This is not a device on the PCI-core bridge. */
+ return -ENODEV;
+@@ -268,27 +265,6 @@ int ssb_pcicore_plat_dev_init(struct pci
+ ssb_printk(KERN_INFO "PCI: Fixing up device %s\n",
+ pci_name(d));
+
+- /* Fix up resource bases */
+- for (pos = 0; pos < 6; pos++) {
+- res = &d->resource[pos];
+- if (res->flags & IORESOURCE_IO)
+- base = &ssb_pcicore_pcibus_iobase;
+- else
+- base = &ssb_pcicore_pcibus_membase;
+- res->flags |= IORESOURCE_PCI_FIXED;
+- if (res->end) {
+- size = res->end - res->start + 1;
+- if (*base & (size - 1))
+- *base = (*base + size) & ~(size - 1);
+- res->start = *base;
+- res->end = res->start + size - 1;
+- *base += size;
+- pci_write_config_dword(d, PCI_BASE_ADDRESS_0 + (pos << 2), res->start);
+- }
+- /* Fix up PCI bridge BAR0 only */
+- if (d->bus->number == 0 && PCI_SLOT(d->devfn) == 0)
+- break;
+- }
+ /* Fix up interrupt lines */
+ d->irq = ssb_mips_irq(extpci_core->dev) + 2;
+ pci_write_config_byte(d, PCI_INTERRUPT_LINE, d->irq);
+@@ -338,7 +314,7 @@ int ssb_pcicore_pcibios_map_irq(const st
+ return ssb_mips_irq(extpci_core->dev) + 2;
+ }
+
+-static void ssb_pcicore_init_hostmode(struct ssb_pcicore *pc)
++static void __devinit ssb_pcicore_init_hostmode(struct ssb_pcicore *pc)
+ {
+ u32 val;
+
+@@ -403,7 +379,7 @@ static void ssb_pcicore_init_hostmode(st
+ register_pci_controller(&ssb_pcicore_controller);
+ }
+
+-static int pcicore_is_in_hostmode(struct ssb_pcicore *pc)
++static int __devinit pcicore_is_in_hostmode(struct ssb_pcicore *pc)
+ {
+ struct ssb_bus *bus = pc->dev->bus;
+ u16 chipid_top;
+@@ -432,25 +408,137 @@ static int pcicore_is_in_hostmode(struct
+ }
+ #endif /* CONFIG_SSB_PCICORE_HOSTMODE */
+
++/**************************************************
++ * Workarounds.
++ **************************************************/
++
++static void __devinit ssb_pcicore_fix_sprom_core_index(struct ssb_pcicore *pc)
++{
++ u16 tmp = pcicore_read16(pc, SSB_PCICORE_SPROM(0));
++ if (((tmp & 0xF000) >> 12) != pc->dev->core_index) {
++ tmp &= ~0xF000;
++ tmp |= (pc->dev->core_index << 12);
++ pcicore_write16(pc, SSB_PCICORE_SPROM(0), tmp);
++ }
++}
++
++static u8 ssb_pcicore_polarity_workaround(struct ssb_pcicore *pc)
++{
++ return (ssb_pcie_read(pc, 0x204) & 0x10) ? 0xC0 : 0x80;
++}
++
++static void ssb_pcicore_serdes_workaround(struct ssb_pcicore *pc)
++{
++ const u8 serdes_pll_device = 0x1D;
++ const u8 serdes_rx_device = 0x1F;
++ u16 tmp;
++
++ ssb_pcie_mdio_write(pc, serdes_rx_device, 1 /* Control */,
++ ssb_pcicore_polarity_workaround(pc));
++ tmp = ssb_pcie_mdio_read(pc, serdes_pll_device, 1 /* Control */);
++ if (tmp & 0x4000)
++ ssb_pcie_mdio_write(pc, serdes_pll_device, 1, tmp & ~0x4000);
++}
++
++static void ssb_pcicore_pci_setup_workarounds(struct ssb_pcicore *pc)
++{
++ struct ssb_device *pdev = pc->dev;
++ struct ssb_bus *bus = pdev->bus;
++ u32 tmp;
++
++ tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
++ tmp |= SSB_PCICORE_SBTOPCI_PREF;
++ tmp |= SSB_PCICORE_SBTOPCI_BURST;
++ pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
++
++ if (pdev->id.revision < 5) {
++ tmp = ssb_read32(pdev, SSB_IMCFGLO);
++ tmp &= ~SSB_IMCFGLO_SERTO;
++ tmp |= 2;
++ tmp &= ~SSB_IMCFGLO_REQTO;
++ tmp |= 3 << SSB_IMCFGLO_REQTO_SHIFT;
++ ssb_write32(pdev, SSB_IMCFGLO, tmp);
++ ssb_commit_settings(bus);
++ } else if (pdev->id.revision >= 11) {
++ tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
++ tmp |= SSB_PCICORE_SBTOPCI_MRM;
++ pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
++ }
++}
++
++static void ssb_pcicore_pcie_setup_workarounds(struct ssb_pcicore *pc)
++{
++ u32 tmp;
++ u8 rev = pc->dev->id.revision;
++
++ if (rev == 0 || rev == 1) {
++ /* TLP Workaround register. */
++ tmp = ssb_pcie_read(pc, 0x4);
++ tmp |= 0x8;
++ ssb_pcie_write(pc, 0x4, tmp);
++ }
++ if (rev == 1) {
++ /* DLLP Link Control register. */
++ tmp = ssb_pcie_read(pc, 0x100);
++ tmp |= 0x40;
++ ssb_pcie_write(pc, 0x100, tmp);
++ }
++
++ if (rev == 0) {
++ const u8 serdes_rx_device = 0x1F;
++
++ ssb_pcie_mdio_write(pc, serdes_rx_device,
++ 2 /* Timer */, 0x8128);
++ ssb_pcie_mdio_write(pc, serdes_rx_device,
++ 6 /* CDR */, 0x0100);
++ ssb_pcie_mdio_write(pc, serdes_rx_device,
++ 7 /* CDR BW */, 0x1466);
++ } else if (rev == 3 || rev == 4 || rev == 5) {
++ /* TODO: DLLP Power Management Threshold */
++ ssb_pcicore_serdes_workaround(pc);
++ /* TODO: ASPM */
++ } else if (rev == 7) {
++ /* TODO: No PLL down */
++ }
++
++ if (rev >= 6) {
++ /* Miscellaneous Configuration Fixup */
++ tmp = pcicore_read16(pc, SSB_PCICORE_SPROM(5));
++ if (!(tmp & 0x8000))
++ pcicore_write16(pc, SSB_PCICORE_SPROM(5),
++ tmp | 0x8000);
++ }
++}
+
+ /**************************************************
+ * Generic and Clientmode operation code.
+ **************************************************/
+
+-static void ssb_pcicore_init_clientmode(struct ssb_pcicore *pc)
++static void __devinit ssb_pcicore_init_clientmode(struct ssb_pcicore *pc)
+ {
++ struct ssb_device *pdev = pc->dev;
++ struct ssb_bus *bus = pdev->bus;
++
++ if (bus->bustype == SSB_BUSTYPE_PCI)
++ ssb_pcicore_fix_sprom_core_index(pc);
++
+ /* Disable PCI interrupts. */
+- ssb_write32(pc->dev, SSB_INTVEC, 0);
++ ssb_write32(pdev, SSB_INTVEC, 0);
++
++ /* Additional PCIe always once-executed workarounds */
++ if (pc->dev->id.coreid == SSB_DEV_PCIE) {
++ ssb_pcicore_serdes_workaround(pc);
++ /* TODO: ASPM */
++ /* TODO: Clock Request Update */
++ }
+ }
+
+-void ssb_pcicore_init(struct ssb_pcicore *pc)
++void __devinit ssb_pcicore_init(struct ssb_pcicore *pc)
+ {
+ struct ssb_device *dev = pc->dev;
+- struct ssb_bus *bus;
+
+ if (!dev)
+ return;
+- bus = dev->bus;
+ if (!ssb_device_is_enabled(dev))
+ ssb_device_enable(dev, 0);
+
+@@ -475,58 +563,104 @@ static void ssb_pcie_write(struct ssb_pc
+ pcicore_write32(pc, 0x134, data);
+ }
+
+-static void ssb_pcie_mdio_write(struct ssb_pcicore *pc, u8 device,
+- u8 address, u16 data)
++static void ssb_pcie_mdio_set_phy(struct ssb_pcicore *pc, u8 phy)
+ {
+ const u16 mdio_control = 0x128;
+ const u16 mdio_data = 0x12C;
+ u32 v;
+ int i;
+
++ v = (1 << 30); /* Start of Transaction */
++ v |= (1 << 28); /* Write Transaction */
++ v |= (1 << 17); /* Turnaround */
++ v |= (0x1F << 18);
++ v |= (phy << 4);
++ pcicore_write32(pc, mdio_data, v);
++
++ udelay(10);
++ for (i = 0; i < 200; i++) {
++ v = pcicore_read32(pc, mdio_control);
++ if (v & 0x100 /* Trans complete */)
++ break;
++ msleep(1);
++ }
++}
++
++static u16 ssb_pcie_mdio_read(struct ssb_pcicore *pc, u8 device, u8 address)
++{
++ const u16 mdio_control = 0x128;
++ const u16 mdio_data = 0x12C;
++ int max_retries = 10;
++ u16 ret = 0;
++ u32 v;
++ int i;
++
+ v = 0x80; /* Enable Preamble Sequence */
+ v |= 0x2; /* MDIO Clock Divisor */
+ pcicore_write32(pc, mdio_control, v);
+
++ if (pc->dev->id.revision >= 10) {
++ max_retries = 200;
++ ssb_pcie_mdio_set_phy(pc, device);
++ }
++
+ v = (1 << 30); /* Start of Transaction */
+- v |= (1 << 28); /* Write Transaction */
++ v |= (1 << 29); /* Read Transaction */
+ v |= (1 << 17); /* Turnaround */
+- v |= (u32)device << 22;
++ if (pc->dev->id.revision < 10)
++ v |= (u32)device << 22;
+ v |= (u32)address << 18;
+- v |= data;
+ pcicore_write32(pc, mdio_data, v);
+ /* Wait for the device to complete the transaction */
+ udelay(10);
+- for (i = 0; i < 10; i++) {
++ for (i = 0; i < max_retries; i++) {
+ v = pcicore_read32(pc, mdio_control);
+- if (v & 0x100 /* Trans complete */)
++ if (v & 0x100 /* Trans complete */) {
++ udelay(10);
++ ret = pcicore_read32(pc, mdio_data);
+ break;
++ }
+ msleep(1);
+ }
+ pcicore_write32(pc, mdio_control, 0);
++ return ret;
+ }
+
+-static void ssb_broadcast_value(struct ssb_device *dev,
+- u32 address, u32 data)
++static void ssb_pcie_mdio_write(struct ssb_pcicore *pc, u8 device,
++ u8 address, u16 data)
+ {
+- /* This is used for both, PCI and ChipCommon core, so be careful. */
+- BUILD_BUG_ON(SSB_PCICORE_BCAST_ADDR != SSB_CHIPCO_BCAST_ADDR);
+- BUILD_BUG_ON(SSB_PCICORE_BCAST_DATA != SSB_CHIPCO_BCAST_DATA);
++ const u16 mdio_control = 0x128;
++ const u16 mdio_data = 0x12C;
++ int max_retries = 10;
++ u32 v;
++ int i;
+
+- ssb_write32(dev, SSB_PCICORE_BCAST_ADDR, address);
+- ssb_read32(dev, SSB_PCICORE_BCAST_ADDR); /* flush */
+- ssb_write32(dev, SSB_PCICORE_BCAST_DATA, data);
+- ssb_read32(dev, SSB_PCICORE_BCAST_DATA); /* flush */
+-}
++ v = 0x80; /* Enable Preamble Sequence */
++ v |= 0x2; /* MDIO Clock Divisor */
++ pcicore_write32(pc, mdio_control, v);
+
+-static void ssb_commit_settings(struct ssb_bus *bus)
+-{
+- struct ssb_device *dev;
++ if (pc->dev->id.revision >= 10) {
++ max_retries = 200;
++ ssb_pcie_mdio_set_phy(pc, device);
++ }
+
+- dev = bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev;
+- if (WARN_ON(!dev))
+- return;
+- /* This forces an update of the cached registers. */
+- ssb_broadcast_value(dev, 0xFD8, 0);
++ v = (1 << 30); /* Start of Transaction */
++ v |= (1 << 28); /* Write Transaction */
++ v |= (1 << 17); /* Turnaround */
++ if (pc->dev->id.revision < 10)
++ v |= (u32)device << 22;
++ v |= (u32)address << 18;
++ v |= data;
++ pcicore_write32(pc, mdio_data, v);
++ /* Wait for the device to complete the transaction */
++ udelay(10);
++ for (i = 0; i < max_retries; i++) {
++ v = pcicore_read32(pc, mdio_control);
++ if (v & 0x100 /* Trans complete */)
++ break;
++ msleep(1);
++ }
++ pcicore_write32(pc, mdio_control, 0);
+ }
+
+ int ssb_pcicore_dev_irqvecs_enable(struct ssb_pcicore *pc,
+@@ -551,13 +685,13 @@ int ssb_pcicore_dev_irqvecs_enable(struc
+ might_sleep_if(pdev->id.coreid != SSB_DEV_PCI);
+
+ /* Enable interrupts for this device. */
+- if (bus->host_pci &&
+- ((pdev->id.revision >= 6) || (pdev->id.coreid == SSB_DEV_PCIE))) {
++ if ((pdev->id.revision >= 6) || (pdev->id.coreid == SSB_DEV_PCIE)) {
+ u32 coremask;
+
+ /* Calculate the "coremask" for the device. */
+ coremask = (1 << dev->core_index);
+
++ SSB_WARN_ON(bus->bustype != SSB_BUSTYPE_PCI);
+ err = pci_read_config_dword(bus->host_pci, SSB_PCI_IRQMASK, &tmp);
+ if (err)
+ goto out;
+@@ -579,48 +713,10 @@ int ssb_pcicore_dev_irqvecs_enable(struc
+ if (pc->setup_done)
+ goto out;
+ if (pdev->id.coreid == SSB_DEV_PCI) {
+- tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
+- tmp |= SSB_PCICORE_SBTOPCI_PREF;
+- tmp |= SSB_PCICORE_SBTOPCI_BURST;
+- pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
+-
+- if (pdev->id.revision < 5) {
+- tmp = ssb_read32(pdev, SSB_IMCFGLO);
+- tmp &= ~SSB_IMCFGLO_SERTO;
+- tmp |= 2;
+- tmp &= ~SSB_IMCFGLO_REQTO;
+- tmp |= 3 << SSB_IMCFGLO_REQTO_SHIFT;
+- ssb_write32(pdev, SSB_IMCFGLO, tmp);
+- ssb_commit_settings(bus);
+- } else if (pdev->id.revision >= 11) {
+- tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
+- tmp |= SSB_PCICORE_SBTOPCI_MRM;
+- pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
+- }
++ ssb_pcicore_pci_setup_workarounds(pc);
+ } else {
+ WARN_ON(pdev->id.coreid != SSB_DEV_PCIE);
+- //TODO: Better make defines for all these magic PCIE values.
+- if ((pdev->id.revision == 0) || (pdev->id.revision == 1)) {
+- /* TLP Workaround register. */
+- tmp = ssb_pcie_read(pc, 0x4);
+- tmp |= 0x8;
+- ssb_pcie_write(pc, 0x4, tmp);
+- }
+- if (pdev->id.revision == 0) {
+- const u8 serdes_rx_device = 0x1F;
+-
+- ssb_pcie_mdio_write(pc, serdes_rx_device,
+- 2 /* Timer */, 0x8128);
+- ssb_pcie_mdio_write(pc, serdes_rx_device,
+- 6 /* CDR */, 0x0100);
+- ssb_pcie_mdio_write(pc, serdes_rx_device,
+- 7 /* CDR BW */, 0x1466);
+- } else if (pdev->id.revision == 1) {
+- /* DLLP Link Control register. */
+- tmp = ssb_pcie_read(pc, 0x100);
+- tmp |= 0x40;
+- ssb_pcie_write(pc, 0x100, tmp);
+- }
++ ssb_pcicore_pcie_setup_workarounds(pc);
+ }
+ pc->setup_done = 1;
+ out:
+--- a/drivers/ssb/main.c
++++ b/drivers/ssb/main.c
+@@ -3,7 +3,7 @@
+ * Subsystem core
+ *
+ * Copyright 2005, Broadcom Corporation
+- * Copyright 2006, 2007, Michael Buesch <mb@bu3sch.de>
++ * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
+ *
+ * Licensed under the GNU/GPL. See COPYING for details.
+ */
+@@ -12,11 +12,14 @@
+
+ #include <linux/delay.h>
+ #include <linux/io.h>
++#include <linux/module.h>
+ #include <linux/ssb/ssb.h>
+ #include <linux/ssb/ssb_regs.h>
+ #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 +91,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 +142,19 @@ static void ssb_device_put(struct ssb_de
+ 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 +225,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))
+- continue;
+- drv = drv_to_ssb_drv(dev->dev->driver);
+- if (!drv)
++ sdrv = ssb_driver_get(drv_to_ssb_drv(sdev->dev->driver));
++ if (!sdrv || SSB_WARN_ON(!sdrv->remove)) {
++ ssb_device_put(sdev);
+ continue;
+- err = drv->suspend(dev, state);
+- if (err) {
+- ssb_printk(KERN_ERR PFX "Failed to freeze device %s\n",
+- dev_name(dev->dev));
+- 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_name(dev->dev));
++ dev_name(sdev->dev));
++ result = err;
+ }
++ ssb_driver_put(sdrv);
++ ssb_device_put(sdev);
+ }
+
+- return 0;
++ return result;
+ }
+ #endif /* CONFIG_SSB_SPROM */
+
+@@ -360,6 +386,35 @@ static int ssb_device_uevent(struct devi
+ ssb_dev->id.revision);
+ }
+
++#define ssb_config_attr(attrib, field, format_string) \
++static ssize_t \
++attrib##_show(struct device *dev, struct device_attribute *attr, char *buf) \
++{ \
++ return sprintf(buf, format_string, dev_to_ssb_dev(dev)->field); \
++}
++
++ssb_config_attr(core_num, core_index, "%u\n")
++ssb_config_attr(coreid, id.coreid, "0x%04x\n")
++ssb_config_attr(vendor, id.vendor, "0x%04x\n")
++ssb_config_attr(revision, id.revision, "%u\n")
++ssb_config_attr(irq, irq, "%u\n")
++static ssize_t
++name_show(struct device *dev, struct device_attribute *attr, char *buf)
++{
++ return sprintf(buf, "%s\n",
++ ssb_core_name(dev_to_ssb_dev(dev)->id.coreid));
++}
++
++static struct device_attribute ssb_device_attrs[] = {
++ __ATTR_RO(name),
++ __ATTR_RO(core_num),
++ __ATTR_RO(coreid),
++ __ATTR_RO(vendor),
++ __ATTR_RO(revision),
++ __ATTR_RO(irq),
++ __ATTR_NULL,
++};
++
+ static struct bus_type ssb_bustype = {
+ .name = "ssb",
+ .match = ssb_bus_match,
+@@ -369,6 +424,7 @@ static struct bus_type ssb_bustype = {
+ .suspend = ssb_device_suspend,
+ .resume = ssb_device_resume,
+ .uevent = ssb_device_uevent,
++ .dev_attrs = ssb_device_attrs,
+ };
+
+ static void ssb_buses_lock(void)
+@@ -461,6 +517,7 @@ static int ssb_devices_register(struct s
+ #ifdef CONFIG_SSB_PCIHOST
+ sdev->irq = bus->host_pci->irq;
+ dev->parent = &bus->host_pci->dev;
++ sdev->dma_dev = dev->parent;
+ #endif
+ break;
+ case SSB_BUSTYPE_PCMCIA:
+@@ -469,8 +526,14 @@ static int ssb_devices_register(struct s
+ 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;
++ sdev->dma_dev = dev;
+ break;
+ }
+
+@@ -497,7 +560,7 @@ error:
+ }
+
+ /* Needs ssb_buses_lock() */
+-static int ssb_attach_queued_buses(void)
++static int __devinit ssb_attach_queued_buses(void)
+ {
+ struct ssb_bus *bus, *n;
+ int err = 0;
+@@ -708,9 +771,9 @@ out:
+ return err;
+ }
+
+-static int ssb_bus_register(struct ssb_bus *bus,
+- ssb_invariants_func_t get_invariants,
+- unsigned long baseaddr)
++static int __devinit ssb_bus_register(struct ssb_bus *bus,
++ ssb_invariants_func_t get_invariants,
++ unsigned long baseaddr)
+ {
+ int err;
+
+@@ -724,12 +787,18 @@ static int ssb_bus_register(struct ssb_b
+ 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 +845,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);
+@@ -783,8 +854,8 @@ err_disable_xtal:
+ }
+
+ #ifdef CONFIG_SSB_PCIHOST
+-int ssb_bus_pcibus_register(struct ssb_bus *bus,
+- struct pci_dev *host_pci)
++int __devinit ssb_bus_pcibus_register(struct ssb_bus *bus,
++ struct pci_dev *host_pci)
+ {
+ int err;
+
+@@ -796,6 +867,9 @@ int ssb_bus_pcibus_register(struct ssb_b
+ if (!err) {
+ ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on "
+ "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;
+@@ -804,9 +878,9 @@ EXPORT_SYMBOL(ssb_bus_pcibus_register);
+ #endif /* CONFIG_SSB_PCIHOST */
+
+ #ifdef CONFIG_SSB_PCMCIAHOST
+-int ssb_bus_pcmciabus_register(struct ssb_bus *bus,
+- struct pcmcia_device *pcmcia_dev,
+- unsigned long baseaddr)
++int __devinit ssb_bus_pcmciabus_register(struct ssb_bus *bus,
++ struct pcmcia_device *pcmcia_dev,
++ unsigned long baseaddr)
+ {
+ int err;
+
+@@ -825,9 +899,32 @@ int ssb_bus_pcmciabus_register(struct ss
+ EXPORT_SYMBOL(ssb_bus_pcmciabus_register);
+ #endif /* CONFIG_SSB_PCMCIAHOST */
+
+-int ssb_bus_ssbbus_register(struct ssb_bus *bus,
+- unsigned long baseaddr,
+- ssb_invariants_func_t get_invariants)
++#ifdef CONFIG_SSB_SDIOHOST
++int __devinit 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 __devinit ssb_bus_ssbbus_register(struct ssb_bus *bus,
++ unsigned long baseaddr,
++ ssb_invariants_func_t get_invariants)
+ {
+ int err;
+
+@@ -908,8 +1005,8 @@ u32 ssb_calc_clock_rate(u32 plltype, u32
+ switch (plltype) {
+ case SSB_PLLTYPE_6: /* 100/200 or 120/240 only */
+ if (m & SSB_CHIPCO_CLK_T6_MMASK)
+- return SSB_CHIPCO_CLK_T6_M0;
+- return SSB_CHIPCO_CLK_T6_M1;
++ return SSB_CHIPCO_CLK_T6_M1;
++ return SSB_CHIPCO_CLK_T6_M0;
+ case SSB_PLLTYPE_1: /* 48Mhz base, 3 dividers */
+ case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
+ case SSB_PLLTYPE_4: /* 48Mhz, 4 dividers */
+@@ -1024,23 +1121,22 @@ static u32 ssb_tmslow_reject_bitmask(str
+ {
+ u32 rev = ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_SSBREV;
+
+- /* The REJECT bit changed position in TMSLOW between
+- * Backplane revisions. */
++ /* The REJECT bit seems to be different for Backplane rev 2.3 */
+ switch (rev) {
+ case SSB_IDLOW_SSBREV_22:
+- return SSB_TMSLOW_REJECT_22;
++ case SSB_IDLOW_SSBREV_24:
++ case SSB_IDLOW_SSBREV_26:
++ return SSB_TMSLOW_REJECT;
+ case SSB_IDLOW_SSBREV_23:
+ return SSB_TMSLOW_REJECT_23;
+- case SSB_IDLOW_SSBREV_24: /* TODO - find the proper REJECT bits */
+- case SSB_IDLOW_SSBREV_25: /* same here */
+- case SSB_IDLOW_SSBREV_26: /* same here */
++ case SSB_IDLOW_SSBREV_25: /* TODO - find the proper REJECT bit */
+ case SSB_IDLOW_SSBREV_27: /* same here */
+- return SSB_TMSLOW_REJECT_23; /* this is a guess */
++ return SSB_TMSLOW_REJECT; /* this is a guess */
+ default:
+ printk(KERN_INFO "ssb: Backplane Revision 0x%.8X\n", rev);
+ WARN_ON(1);
+ }
+- return (SSB_TMSLOW_REJECT_22 | SSB_TMSLOW_REJECT_23);
++ return (SSB_TMSLOW_REJECT | SSB_TMSLOW_REJECT_23);
+ }
+
+ int ssb_device_is_enabled(struct ssb_device *dev)
+@@ -1099,10 +1195,10 @@ void ssb_device_enable(struct ssb_device
+ }
+ EXPORT_SYMBOL(ssb_device_enable);
+
+-/* Wait for a bit in a register to get set or unset.
++/* Wait for bitmask in a register to get set or cleared.
+ * timeout is in units of ten-microseconds */
+-static int ssb_wait_bit(struct ssb_device *dev, u16 reg, u32 bitmask,
+- int timeout, int set)
++static int ssb_wait_bits(struct ssb_device *dev, u16 reg, u32 bitmask,
++ int timeout, int set)
+ {
+ int i;
+ u32 val;
+@@ -1110,7 +1206,7 @@ static int ssb_wait_bit(struct ssb_devic
+ for (i = 0; i < timeout; i++) {
+ val = ssb_read32(dev, reg);
+ if (set) {
+- if (val & bitmask)
++ if ((val & bitmask) == bitmask)
+ return 0;
+ } else {
+ if (!(val & bitmask))
+@@ -1127,20 +1223,38 @@ static int ssb_wait_bit(struct ssb_devic
+
+ void ssb_device_disable(struct ssb_device *dev, u32 core_specific_flags)
+ {
+- u32 reject;
++ u32 reject, val;
+
+ if (ssb_read32(dev, SSB_TMSLOW) & SSB_TMSLOW_RESET)
+ return;
+
+ reject = ssb_tmslow_reject_bitmask(dev);
+- ssb_write32(dev, SSB_TMSLOW, reject | SSB_TMSLOW_CLOCK);
+- ssb_wait_bit(dev, SSB_TMSLOW, reject, 1000, 1);
+- ssb_wait_bit(dev, SSB_TMSHIGH, SSB_TMSHIGH_BUSY, 1000, 0);
+- ssb_write32(dev, SSB_TMSLOW,
+- SSB_TMSLOW_FGC | SSB_TMSLOW_CLOCK |
+- reject | SSB_TMSLOW_RESET |
+- core_specific_flags);
+- ssb_flush_tmslow(dev);
++
++ if (ssb_read32(dev, SSB_TMSLOW) & SSB_TMSLOW_CLOCK) {
++ ssb_write32(dev, SSB_TMSLOW, reject | SSB_TMSLOW_CLOCK);
++ ssb_wait_bits(dev, SSB_TMSLOW, reject, 1000, 1);
++ ssb_wait_bits(dev, SSB_TMSHIGH, SSB_TMSHIGH_BUSY, 1000, 0);
++
++ if (ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_INITIATOR) {
++ val = ssb_read32(dev, SSB_IMSTATE);
++ val |= SSB_IMSTATE_REJECT;
++ ssb_write32(dev, SSB_IMSTATE, val);
++ ssb_wait_bits(dev, SSB_IMSTATE, SSB_IMSTATE_BUSY, 1000,
++ 0);
++ }
++
++ ssb_write32(dev, SSB_TMSLOW,
++ SSB_TMSLOW_FGC | SSB_TMSLOW_CLOCK |
++ reject | SSB_TMSLOW_RESET |
++ core_specific_flags);
++ ssb_flush_tmslow(dev);
++
++ if (ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_INITIATOR) {
++ val = ssb_read32(dev, SSB_IMSTATE);
++ val &= ~SSB_IMSTATE_REJECT;
++ ssb_write32(dev, SSB_IMSTATE, val);
++ }
++ }
+
+ ssb_write32(dev, SSB_TMSLOW,
+ reject | SSB_TMSLOW_RESET |
+@@ -1149,13 +1263,34 @@ void ssb_device_disable(struct ssb_devic
+ }
+ EXPORT_SYMBOL(ssb_device_disable);
+
++/* Some chipsets need routing known for PCIe and 64-bit DMA */
++static bool ssb_dma_translation_special_bit(struct ssb_device *dev)
++{
++ u16 chip_id = dev->bus->chip_id;
++
++ if (dev->id.coreid == SSB_DEV_80211) {
++ return (chip_id == 0x4322 || chip_id == 43221 ||
++ chip_id == 43231 || chip_id == 43222);
++ }
++
++ return 0;
++}
++
+ u32 ssb_dma_translation(struct ssb_device *dev)
+ {
+ switch (dev->bus->bustype) {
+ case SSB_BUSTYPE_SSB:
+ return 0;
+ case SSB_BUSTYPE_PCI:
+- return SSB_PCI_DMA;
++ if (dev->bus->host_pci->is_pcie &&
++ ssb_read32(dev, SSB_TMSHIGH) & SSB_TMSHIGH_DMA64) {
++ return SSB_PCIE_DMA_H32;
++ } else {
++ if (ssb_dma_translation_special_bit(dev))
++ return SSB_PCIE_DMA_H32;
++ else
++ return SSB_PCI_DMA;
++ }
+ default:
+ __ssb_dma_not_implemented(dev);
+ }
+@@ -1272,20 +1407,20 @@ EXPORT_SYMBOL(ssb_bus_may_powerdown);
+
+ int ssb_bus_powerup(struct ssb_bus *bus, bool dynamic_pctl)
+ {
+- struct ssb_chipcommon *cc;
+ int err;
+ enum ssb_clkmode mode;
+
+ err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
+ if (err)
+ goto error;
+- cc = &bus->chipco;
+- mode = dynamic_pctl ? SSB_CLKMODE_DYNAMIC : SSB_CLKMODE_FAST;
+- ssb_chipco_set_clockmode(cc, mode);
+
+ #ifdef CONFIG_SSB_DEBUG
+ bus->powered_up = 1;
+ #endif
++
++ mode = dynamic_pctl ? SSB_CLKMODE_DYNAMIC : SSB_CLKMODE_FAST;
++ ssb_chipco_set_clockmode(&bus->chipco, mode);
++
+ return 0;
+ error:
+ ssb_printk(KERN_ERR PFX "Bus powerup failed\n");
+@@ -1293,6 +1428,37 @@ error:
+ }
+ EXPORT_SYMBOL(ssb_bus_powerup);
+
++static void ssb_broadcast_value(struct ssb_device *dev,
++ u32 address, u32 data)
++{
++#ifdef CONFIG_SSB_DRIVER_PCICORE
++ /* This is used for both, PCI and ChipCommon core, so be careful. */
++ BUILD_BUG_ON(SSB_PCICORE_BCAST_ADDR != SSB_CHIPCO_BCAST_ADDR);
++ BUILD_BUG_ON(SSB_PCICORE_BCAST_DATA != SSB_CHIPCO_BCAST_DATA);
++#endif
++
++ ssb_write32(dev, SSB_CHIPCO_BCAST_ADDR, address);
++ ssb_read32(dev, SSB_CHIPCO_BCAST_ADDR); /* flush */
++ ssb_write32(dev, SSB_CHIPCO_BCAST_DATA, data);
++ ssb_read32(dev, SSB_CHIPCO_BCAST_DATA); /* flush */
++}
++
++void ssb_commit_settings(struct ssb_bus *bus)
++{
++ struct ssb_device *dev;
++
++#ifdef CONFIG_SSB_DRIVER_PCICORE
++ dev = bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev;
++#else
++ dev = bus->chipco.dev;
++#endif
++ if (WARN_ON(!dev))
++ return;
++ /* This forces an update of the cached registers. */
++ ssb_broadcast_value(dev, 0xFD8, 0);
++}
++EXPORT_SYMBOL(ssb_commit_settings);
++
+ u32 ssb_admatch_base(u32 adm)
+ {
+ u32 base = 0;
+@@ -1358,8 +1524,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) {
+@@ -1375,7 +1543,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.
+--- a/drivers/ssb/pci.c
++++ b/drivers/ssb/pci.c
+@@ -1,7 +1,7 @@
+ /*
+ * Sonics Silicon Backplane PCI-Hostbus related functions.
+ *
+- * Copyright (C) 2005-2006 Michael Buesch <mb@bu3sch.de>
++ * Copyright (C) 2005-2006 Michael Buesch <m@bues.ch>
+ * Copyright (C) 2005 Martin Langer <martin-langer@gmx.de>
+ * Copyright (C) 2005 Stefano Brivio <st3@riseup.net>
+ * Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
+@@ -17,6 +17,7 @@
+
+ #include <linux/ssb/ssb.h>
+ #include <linux/ssb/ssb_regs.h>
++#include <linux/slab.h>
+ #include <linux/pci.h>
+ #include <linux/delay.h>
+
+@@ -167,10 +168,16 @@ err_pci:
+ }
+
+ /* Get the word-offset for a SSB_SPROM_XXX define. */
+-#define SPOFF(offset) (((offset) - SSB_SPROM_BASE) / sizeof(u16))
++#define SPOFF(offset) ((offset) / sizeof(u16))
+ /* Helper to extract some _offset, which is one of the SSB_SPROM_XXX defines. */
+-#define SPEX(_outvar, _offset, _mask, _shift) \
++#define SPEX16(_outvar, _offset, _mask, _shift) \
+ out->_outvar = ((in[SPOFF(_offset)] & (_mask)) >> (_shift))
++#define SPEX32(_outvar, _offset, _mask, _shift) \
++ out->_outvar = ((((u32)in[SPOFF((_offset)+2)] << 16 | \
++ in[SPOFF(_offset)]) & (_mask)) >> (_shift))
++#define SPEX(_outvar, _offset, _mask, _shift) \
++ SPEX16(_outvar, _offset, _mask, _shift)
++
+
+ static inline u8 ssb_crc8(u8 crc, u8 data)
+ {
+@@ -247,7 +254,7 @@ static int sprom_do_read(struct ssb_bus
+ int i;
+
+ for (i = 0; i < bus->sprom_size; i++)
+- sprom[i] = ioread16(bus->mmio + SSB_SPROM_BASE + (i * 2));
++ sprom[i] = ioread16(bus->mmio + bus->sprom_offset + (i * 2));
+
+ return 0;
+ }
+@@ -278,7 +285,7 @@ static int sprom_do_write(struct ssb_bus
+ ssb_printk("75%%");
+ else if (i % 2)
+ ssb_printk(".");
+- writew(sprom[i], bus->mmio + SSB_SPROM_BASE + (i * 2));
++ writew(sprom[i], bus->mmio + bus->sprom_offset + (i * 2));
+ mmiowb();
+ msleep(20);
+ }
+@@ -399,6 +406,46 @@ static void sprom_extract_r123(struct ss
+ out->antenna_gain.ghz5.a3 = gain;
+ }
+
++/* Revs 4 5 and 8 have partially shared layout */
++static void sprom_extract_r458(struct ssb_sprom *out, const u16 *in)
++{
++ SPEX(txpid2g[0], SSB_SPROM4_TXPID2G01,
++ SSB_SPROM4_TXPID2G0, SSB_SPROM4_TXPID2G0_SHIFT);
++ SPEX(txpid2g[1], SSB_SPROM4_TXPID2G01,
++ SSB_SPROM4_TXPID2G1, SSB_SPROM4_TXPID2G1_SHIFT);
++ SPEX(txpid2g[2], SSB_SPROM4_TXPID2G23,
++ SSB_SPROM4_TXPID2G2, SSB_SPROM4_TXPID2G2_SHIFT);
++ SPEX(txpid2g[3], SSB_SPROM4_TXPID2G23,
++ SSB_SPROM4_TXPID2G3, SSB_SPROM4_TXPID2G3_SHIFT);
++
++ SPEX(txpid5gl[0], SSB_SPROM4_TXPID5GL01,
++ SSB_SPROM4_TXPID5GL0, SSB_SPROM4_TXPID5GL0_SHIFT);
++ SPEX(txpid5gl[1], SSB_SPROM4_TXPID5GL01,
++ SSB_SPROM4_TXPID5GL1, SSB_SPROM4_TXPID5GL1_SHIFT);
++ SPEX(txpid5gl[2], SSB_SPROM4_TXPID5GL23,
++ SSB_SPROM4_TXPID5GL2, SSB_SPROM4_TXPID5GL2_SHIFT);
++ SPEX(txpid5gl[3], SSB_SPROM4_TXPID5GL23,
++ SSB_SPROM4_TXPID5GL3, SSB_SPROM4_TXPID5GL3_SHIFT);
++
++ SPEX(txpid5g[0], SSB_SPROM4_TXPID5G01,
++ SSB_SPROM4_TXPID5G0, SSB_SPROM4_TXPID5G0_SHIFT);
++ SPEX(txpid5g[1], SSB_SPROM4_TXPID5G01,
++ SSB_SPROM4_TXPID5G1, SSB_SPROM4_TXPID5G1_SHIFT);
++ SPEX(txpid5g[2], SSB_SPROM4_TXPID5G23,
++ SSB_SPROM4_TXPID5G2, SSB_SPROM4_TXPID5G2_SHIFT);
++ SPEX(txpid5g[3], SSB_SPROM4_TXPID5G23,
++ SSB_SPROM4_TXPID5G3, SSB_SPROM4_TXPID5G3_SHIFT);
++
++ SPEX(txpid5gh[0], SSB_SPROM4_TXPID5GH01,
++ SSB_SPROM4_TXPID5GH0, SSB_SPROM4_TXPID5GH0_SHIFT);
++ SPEX(txpid5gh[1], SSB_SPROM4_TXPID5GH01,
++ SSB_SPROM4_TXPID5GH1, SSB_SPROM4_TXPID5GH1_SHIFT);
++ SPEX(txpid5gh[2], SSB_SPROM4_TXPID5GH23,
++ SSB_SPROM4_TXPID5GH2, SSB_SPROM4_TXPID5GH2_SHIFT);
++ SPEX(txpid5gh[3], SSB_SPROM4_TXPID5GH23,
++ SSB_SPROM4_TXPID5GH3, SSB_SPROM4_TXPID5GH3_SHIFT);
++}
++
+ static void sprom_extract_r45(struct ssb_sprom *out, const u16 *in)
+ {
+ int i;
+@@ -421,10 +468,14 @@ static void sprom_extract_r45(struct ssb
+ SPEX(country_code, SSB_SPROM4_CCODE, 0xFFFF, 0);
+ SPEX(boardflags_lo, SSB_SPROM4_BFLLO, 0xFFFF, 0);
+ SPEX(boardflags_hi, SSB_SPROM4_BFLHI, 0xFFFF, 0);
++ SPEX(boardflags2_lo, SSB_SPROM4_BFL2LO, 0xFFFF, 0);
++ SPEX(boardflags2_hi, SSB_SPROM4_BFL2HI, 0xFFFF, 0);
+ } else {
+ SPEX(country_code, SSB_SPROM5_CCODE, 0xFFFF, 0);
+ SPEX(boardflags_lo, SSB_SPROM5_BFLLO, 0xFFFF, 0);
+ SPEX(boardflags_hi, SSB_SPROM5_BFLHI, 0xFFFF, 0);
++ SPEX(boardflags2_lo, SSB_SPROM5_BFL2LO, 0xFFFF, 0);
++ SPEX(boardflags2_hi, SSB_SPROM5_BFL2HI, 0xFFFF, 0);
+ }
+ SPEX(ant_available_a, SSB_SPROM4_ANTAVAIL, SSB_SPROM4_ANTAVAIL_A,
+ SSB_SPROM4_ANTAVAIL_A_SHIFT);
+@@ -464,22 +515,32 @@ static void sprom_extract_r45(struct ssb
+ memcpy(&out->antenna_gain.ghz5, &out->antenna_gain.ghz24,
+ sizeof(out->antenna_gain.ghz5));
+
++ sprom_extract_r458(out, in);
++
+ /* TODO - get remaining rev 4 stuff needed */
+ }
+
+ static void sprom_extract_r8(struct ssb_sprom *out, const u16 *in)
+ {
+ int i;
+- u16 v;
++ u16 v, o;
++ u16 pwr_info_offset[] = {
++ SSB_SROM8_PWR_INFO_CORE0, SSB_SROM8_PWR_INFO_CORE1,
++ SSB_SROM8_PWR_INFO_CORE2, SSB_SROM8_PWR_INFO_CORE3
++ };
++ BUILD_BUG_ON(ARRAY_SIZE(pwr_info_offset) !=
++ ARRAY_SIZE(out->core_pwr_info));
+
+ /* extract the MAC address */
+ for (i = 0; i < 3; i++) {
+- v = in[SPOFF(SSB_SPROM1_IL0MAC) + i];
++ v = in[SPOFF(SSB_SPROM8_IL0MAC) + i];
+ *(((__be16 *)out->il0mac) + i) = cpu_to_be16(v);
+ }
+ SPEX(country_code, SSB_SPROM8_CCODE, 0xFFFF, 0);
+ SPEX(boardflags_lo, SSB_SPROM8_BFLLO, 0xFFFF, 0);
+ SPEX(boardflags_hi, SSB_SPROM8_BFLHI, 0xFFFF, 0);
++ SPEX(boardflags2_lo, SSB_SPROM8_BFL2LO, 0xFFFF, 0);
++ SPEX(boardflags2_hi, SSB_SPROM8_BFL2HI, 0xFFFF, 0);
+ SPEX(ant_available_a, SSB_SPROM8_ANTAVAIL, SSB_SPROM8_ANTAVAIL_A,
+ SSB_SPROM8_ANTAVAIL_A_SHIFT);
+ SPEX(ant_available_bg, SSB_SPROM8_ANTAVAIL, SSB_SPROM8_ANTAVAIL_BG,
+@@ -490,12 +551,55 @@ static void sprom_extract_r8(struct ssb_
+ SPEX(maxpwr_a, SSB_SPROM8_MAXP_A, SSB_SPROM8_MAXP_A_MASK, 0);
+ SPEX(itssi_a, SSB_SPROM8_MAXP_A, SSB_SPROM8_ITSSI_A,
+ SSB_SPROM8_ITSSI_A_SHIFT);
++ SPEX(maxpwr_ah, SSB_SPROM8_MAXP_AHL, SSB_SPROM8_MAXP_AH_MASK, 0);
++ SPEX(maxpwr_al, SSB_SPROM8_MAXP_AHL, SSB_SPROM8_MAXP_AL_MASK,
++ SSB_SPROM8_MAXP_AL_SHIFT);
+ SPEX(gpio0, SSB_SPROM8_GPIOA, SSB_SPROM8_GPIOA_P0, 0);
+ SPEX(gpio1, SSB_SPROM8_GPIOA, SSB_SPROM8_GPIOA_P1,
+ SSB_SPROM8_GPIOA_P1_SHIFT);
+ SPEX(gpio2, SSB_SPROM8_GPIOB, SSB_SPROM8_GPIOB_P2, 0);
+ SPEX(gpio3, SSB_SPROM8_GPIOB, SSB_SPROM8_GPIOB_P3,
+ SSB_SPROM8_GPIOB_P3_SHIFT);
++ SPEX(tri2g, SSB_SPROM8_TRI25G, SSB_SPROM8_TRI2G, 0);
++ SPEX(tri5g, SSB_SPROM8_TRI25G, SSB_SPROM8_TRI5G,
++ SSB_SPROM8_TRI5G_SHIFT);
++ SPEX(tri5gl, SSB_SPROM8_TRI5GHL, SSB_SPROM8_TRI5GL, 0);
++ SPEX(tri5gh, SSB_SPROM8_TRI5GHL, SSB_SPROM8_TRI5GH,
++ SSB_SPROM8_TRI5GH_SHIFT);
++ SPEX(rxpo2g, SSB_SPROM8_RXPO, SSB_SPROM8_RXPO2G, 0);
++ SPEX(rxpo5g, SSB_SPROM8_RXPO, SSB_SPROM8_RXPO5G,
++ SSB_SPROM8_RXPO5G_SHIFT);
++ SPEX(rssismf2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_RSSISMF2G, 0);
++ SPEX(rssismc2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_RSSISMC2G,
++ SSB_SPROM8_RSSISMC2G_SHIFT);
++ SPEX(rssisav2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_RSSISAV2G,
++ SSB_SPROM8_RSSISAV2G_SHIFT);
++ SPEX(bxa2g, SSB_SPROM8_RSSIPARM2G, SSB_SPROM8_BXA2G,
++ SSB_SPROM8_BXA2G_SHIFT);
++ SPEX(rssismf5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_RSSISMF5G, 0);
++ SPEX(rssismc5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_RSSISMC5G,
++ SSB_SPROM8_RSSISMC5G_SHIFT);
++ SPEX(rssisav5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_RSSISAV5G,
++ SSB_SPROM8_RSSISAV5G_SHIFT);
++ SPEX(bxa5g, SSB_SPROM8_RSSIPARM5G, SSB_SPROM8_BXA5G,
++ SSB_SPROM8_BXA5G_SHIFT);
++ SPEX(pa0b0, SSB_SPROM8_PA0B0, 0xFFFF, 0);
++ SPEX(pa0b1, SSB_SPROM8_PA0B1, 0xFFFF, 0);
++ SPEX(pa0b2, SSB_SPROM8_PA0B2, 0xFFFF, 0);
++ SPEX(pa1b0, SSB_SPROM8_PA1B0, 0xFFFF, 0);
++ SPEX(pa1b1, SSB_SPROM8_PA1B1, 0xFFFF, 0);
++ SPEX(pa1b2, SSB_SPROM8_PA1B2, 0xFFFF, 0);
++ SPEX(pa1lob0, SSB_SPROM8_PA1LOB0, 0xFFFF, 0);
++ SPEX(pa1lob1, SSB_SPROM8_PA1LOB1, 0xFFFF, 0);
++ SPEX(pa1lob2, SSB_SPROM8_PA1LOB2, 0xFFFF, 0);
++ SPEX(pa1hib0, SSB_SPROM8_PA1HIB0, 0xFFFF, 0);
++ SPEX(pa1hib1, SSB_SPROM8_PA1HIB1, 0xFFFF, 0);
++ SPEX(pa1hib2, SSB_SPROM8_PA1HIB2, 0xFFFF, 0);
++ SPEX(cck2gpo, SSB_SPROM8_CCK2GPO, 0xFFFF, 0);
++ SPEX32(ofdm2gpo, SSB_SPROM8_OFDM2GPO, 0xFFFFFFFF, 0);
++ SPEX32(ofdm5glpo, SSB_SPROM8_OFDM5GLPO, 0xFFFFFFFF, 0);
++ SPEX32(ofdm5gpo, SSB_SPROM8_OFDM5GPO, 0xFFFFFFFF, 0);
++ SPEX32(ofdm5ghpo, SSB_SPROM8_OFDM5GHPO, 0xFFFFFFFF, 0);
+
+ /* Extract the antenna gain values. */
+ SPEX(antenna_gain.ghz24.a0, SSB_SPROM8_AGAIN01,
+@@ -509,6 +613,63 @@ static void sprom_extract_r8(struct ssb_
+ memcpy(&out->antenna_gain.ghz5, &out->antenna_gain.ghz24,
+ sizeof(out->antenna_gain.ghz5));
+
++ /* Extract cores power info info */
++ for (i = 0; i < ARRAY_SIZE(pwr_info_offset); i++) {
++ o = pwr_info_offset[i];
++ SPEX(core_pwr_info[i].itssi_2g, o + SSB_SROM8_2G_MAXP_ITSSI,
++ SSB_SPROM8_2G_ITSSI, SSB_SPROM8_2G_ITSSI_SHIFT);
++ SPEX(core_pwr_info[i].maxpwr_2g, o + SSB_SROM8_2G_MAXP_ITSSI,
++ SSB_SPROM8_2G_MAXP, 0);
++
++ SPEX(core_pwr_info[i].pa_2g[0], o + SSB_SROM8_2G_PA_0, ~0, 0);
++ SPEX(core_pwr_info[i].pa_2g[1], o + SSB_SROM8_2G_PA_1, ~0, 0);
++ SPEX(core_pwr_info[i].pa_2g[2], o + SSB_SROM8_2G_PA_2, ~0, 0);
++
++ SPEX(core_pwr_info[i].itssi_5g, o + SSB_SROM8_5G_MAXP_ITSSI,
++ SSB_SPROM8_5G_ITSSI, SSB_SPROM8_5G_ITSSI_SHIFT);
++ SPEX(core_pwr_info[i].maxpwr_5g, o + SSB_SROM8_5G_MAXP_ITSSI,
++ SSB_SPROM8_5G_MAXP, 0);
++ SPEX(core_pwr_info[i].maxpwr_5gh, o + SSB_SPROM8_5GHL_MAXP,
++ SSB_SPROM8_5GH_MAXP, 0);
++ SPEX(core_pwr_info[i].maxpwr_5gl, o + SSB_SPROM8_5GHL_MAXP,
++ SSB_SPROM8_5GL_MAXP, SSB_SPROM8_5GL_MAXP_SHIFT);
++
++ SPEX(core_pwr_info[i].pa_5gl[0], o + SSB_SROM8_5GL_PA_0, ~0, 0);
++ SPEX(core_pwr_info[i].pa_5gl[1], o + SSB_SROM8_5GL_PA_1, ~0, 0);
++ SPEX(core_pwr_info[i].pa_5gl[2], o + SSB_SROM8_5GL_PA_2, ~0, 0);
++ SPEX(core_pwr_info[i].pa_5g[0], o + SSB_SROM8_5G_PA_0, ~0, 0);
++ SPEX(core_pwr_info[i].pa_5g[1], o + SSB_SROM8_5G_PA_1, ~0, 0);
++ SPEX(core_pwr_info[i].pa_5g[2], o + SSB_SROM8_5G_PA_2, ~0, 0);
++ SPEX(core_pwr_info[i].pa_5gh[0], o + SSB_SROM8_5GH_PA_0, ~0, 0);
++ SPEX(core_pwr_info[i].pa_5gh[1], o + SSB_SROM8_5GH_PA_1, ~0, 0);
++ SPEX(core_pwr_info[i].pa_5gh[2], o + SSB_SROM8_5GH_PA_2, ~0, 0);
++ }
++
++ /* Extract FEM info */
++ SPEX(fem.ghz2.tssipos, SSB_SPROM8_FEM2G,
++ SSB_SROM8_FEM_TSSIPOS, SSB_SROM8_FEM_TSSIPOS_SHIFT);
++ SPEX(fem.ghz2.extpa_gain, SSB_SPROM8_FEM2G,
++ SSB_SROM8_FEM_EXTPA_GAIN, SSB_SROM8_FEM_EXTPA_GAIN_SHIFT);
++ SPEX(fem.ghz2.pdet_range, SSB_SPROM8_FEM2G,
++ SSB_SROM8_FEM_PDET_RANGE, SSB_SROM8_FEM_PDET_RANGE_SHIFT);
++ SPEX(fem.ghz2.tr_iso, SSB_SPROM8_FEM2G,
++ SSB_SROM8_FEM_TR_ISO, SSB_SROM8_FEM_TR_ISO_SHIFT);
++ SPEX(fem.ghz2.antswlut, SSB_SPROM8_FEM2G,
++ SSB_SROM8_FEM_ANTSWLUT, SSB_SROM8_FEM_ANTSWLUT_SHIFT);
++
++ SPEX(fem.ghz5.tssipos, SSB_SPROM8_FEM5G,
++ SSB_SROM8_FEM_TSSIPOS, SSB_SROM8_FEM_TSSIPOS_SHIFT);
++ SPEX(fem.ghz5.extpa_gain, SSB_SPROM8_FEM5G,
++ SSB_SROM8_FEM_EXTPA_GAIN, SSB_SROM8_FEM_EXTPA_GAIN_SHIFT);
++ SPEX(fem.ghz5.pdet_range, SSB_SPROM8_FEM5G,
++ SSB_SROM8_FEM_PDET_RANGE, SSB_SROM8_FEM_PDET_RANGE_SHIFT);
++ SPEX(fem.ghz5.tr_iso, SSB_SPROM8_FEM5G,
++ SSB_SROM8_FEM_TR_ISO, SSB_SROM8_FEM_TR_ISO_SHIFT);
++ SPEX(fem.ghz5.antswlut, SSB_SPROM8_FEM5G,
++ SSB_SROM8_FEM_ANTSWLUT, SSB_SROM8_FEM_ANTSWLUT_SHIFT);
++
++ sprom_extract_r458(out, in);
++
+ /* TODO - get remaining rev 8 stuff needed */
+ }
+
+@@ -521,36 +682,34 @@ static int sprom_extract(struct ssb_bus
+ ssb_dprintk(KERN_DEBUG PFX "SPROM revision %d detected.\n", out->revision);
+ memset(out->et0mac, 0xFF, 6); /* preset et0 and et1 mac */
+ memset(out->et1mac, 0xFF, 6);
++
+ if ((bus->chip_id & 0xFF00) == 0x4400) {
+ /* Workaround: The BCM44XX chip has a stupid revision
+ * number stored in the SPROM.
+ * Always extract r1. */
+ out->revision = 1;
++ ssb_dprintk(KERN_DEBUG PFX "SPROM treated as revision %d\n", out->revision);
++ }
++
++ switch (out->revision) {
++ case 1:
++ case 2:
++ case 3:
+ sprom_extract_r123(out, in);
+- } else if (bus->chip_id == 0x4321) {
+- /* the BCM4328 has a chipid == 0x4321 and a rev 4 SPROM */
+- out->revision = 4;
++ break;
++ case 4:
++ case 5:
+ sprom_extract_r45(out, in);
+- } else {
+- switch (out->revision) {
+- case 1:
+- case 2:
+- case 3:
+- sprom_extract_r123(out, in);
+- break;
+- case 4:
+- case 5:
+- sprom_extract_r45(out, in);
+- break;
+- case 8:
+- sprom_extract_r8(out, in);
+- break;
+- default:
+- ssb_printk(KERN_WARNING PFX "Unsupported SPROM"
+- " revision %d detected. Will extract"
+- " v1\n", out->revision);
+- sprom_extract_r123(out, in);
+- }
++ break;
++ case 8:
++ sprom_extract_r8(out, in);
++ break;
++ default:
++ ssb_printk(KERN_WARNING PFX "Unsupported SPROM"
++ " revision %d detected. Will extract"
++ " v1\n", out->revision);
++ out->revision = 1;
++ sprom_extract_r123(out, in);
+ }
+
+ if (out->boardflags_lo == 0xFFFF)
+@@ -564,13 +723,34 @@ static int sprom_extract(struct ssb_bus
+ static int ssb_pci_sprom_get(struct ssb_bus *bus,
+ struct ssb_sprom *sprom)
+ {
+- const struct ssb_sprom *fallback;
+- int err = -ENOMEM;
++ int err;
+ u16 *buf;
+
++ if (!ssb_is_sprom_available(bus)) {
++ ssb_printk(KERN_ERR PFX "No SPROM available!\n");
++ return -ENODEV;
++ }
++ if (bus->chipco.dev) { /* can be unavailable! */
++ /*
++ * get SPROM offset: SSB_SPROM_BASE1 except for
++ * chipcommon rev >= 31 or chip ID is 0x4312 and
++ * chipcommon status & 3 == 2
++ */
++ if (bus->chipco.dev->id.revision >= 31)
++ bus->sprom_offset = SSB_SPROM_BASE31;
++ else if (bus->chip_id == 0x4312 &&
++ (bus->chipco.status & 0x03) == 2)
++ bus->sprom_offset = SSB_SPROM_BASE31;
++ else
++ bus->sprom_offset = SSB_SPROM_BASE1;
++ } else {
++ bus->sprom_offset = SSB_SPROM_BASE1;
++ }
++ ssb_dprintk(KERN_INFO PFX "SPROM offset is 0x%x\n", bus->sprom_offset);
++
+ buf = kcalloc(SSB_SPROMSIZE_WORDS_R123, sizeof(u16), GFP_KERNEL);
+ if (!buf)
+- goto out;
++ return -ENOMEM;
+ bus->sprom_size = SSB_SPROMSIZE_WORDS_R123;
+ sprom_do_read(bus, buf);
+ err = sprom_check_crc(buf, bus->sprom_size);
+@@ -580,17 +760,24 @@ static int ssb_pci_sprom_get(struct ssb_
+ buf = kcalloc(SSB_SPROMSIZE_WORDS_R4, sizeof(u16),
+ GFP_KERNEL);
+ if (!buf)
+- goto out;
++ return -ENOMEM;
+ bus->sprom_size = SSB_SPROMSIZE_WORDS_R4;
+ sprom_do_read(bus, buf);
+ err = sprom_check_crc(buf, bus->sprom_size);
+ if (err) {
+ /* All CRC attempts failed.
+ * Maybe there is no SPROM on the device?
+- * If we have a fallback, use that. */
+- fallback = ssb_get_fallback_sprom();
+- if (fallback) {
+- memcpy(sprom, fallback, sizeof(*sprom));
++ * Now we ask the arch code if there is some sprom
++ * available for this device in some other storage */
++ err = ssb_fill_sprom_with_fallback(bus, sprom);
++ if (err) {
++ ssb_printk(KERN_WARNING PFX "WARNING: Using"
++ " fallback SPROM failed (err %d)\n",
++ err);
++ } else {
++ ssb_dprintk(KERN_DEBUG PFX "Using SPROM"
++ " revision %d provided by"
++ " platform.\n", sprom->revision);
+ err = 0;
+ goto out_free;
+ }
+@@ -602,19 +789,15 @@ static int ssb_pci_sprom_get(struct ssb_
+
+ out_free:
+ kfree(buf);
+-out:
+ return err;
+ }
+
+ static void ssb_pci_get_boardinfo(struct ssb_bus *bus,
+ struct ssb_boardinfo *bi)
+ {
+- pci_read_config_word(bus->host_pci, PCI_SUBSYSTEM_VENDOR_ID,
+- &bi->vendor);
+- pci_read_config_word(bus->host_pci, PCI_SUBSYSTEM_ID,
+- &bi->type);
+- pci_read_config_word(bus->host_pci, PCI_REVISION_ID,
+- &bi->rev);
++ bi->vendor = bus->host_pci->subsystem_vendor;
++ bi->type = bus->host_pci->subsystem_device;
++ bi->rev = bus->host_pci->revision;
+ }
+
+ int ssb_pci_get_invariants(struct ssb_bus *bus,
+--- a/drivers/ssb/pcihost_wrapper.c
++++ b/drivers/ssb/pcihost_wrapper.c
+@@ -6,12 +6,13 @@
+ * Copyright (c) 2005 Stefano Brivio <st3@riseup.net>
+ * Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
+ * Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
+- * Copyright (c) 2005-2007 Michael Buesch <mbuesch@freenet.de>
++ * Copyright (c) 2005-2007 Michael Buesch <m@bues.ch>
+ *
+ * Licensed under the GNU/GPL. See COPYING for details.
+ */
+
+ #include <linux/pci.h>
++#include <linux/slab.h>
+ #include <linux/ssb/ssb.h>
+
+
+@@ -52,12 +53,13 @@ static int ssb_pcihost_resume(struct pci
+ # define ssb_pcihost_resume NULL
+ #endif /* CONFIG_PM */
+
+-static int ssb_pcihost_probe(struct pci_dev *dev,
+- const struct pci_device_id *id)
++static int __devinit ssb_pcihost_probe(struct pci_dev *dev,
++ const struct pci_device_id *id)
+ {
+ struct ssb_bus *ssb;
+ int err = -ENOMEM;
+ const char *name;
++ u32 val;
+
+ ssb = kzalloc(sizeof(*ssb), GFP_KERNEL);
+ if (!ssb)
+@@ -73,6 +75,12 @@ static int ssb_pcihost_probe(struct pci_
+ goto err_pci_disable;
+ pci_set_master(dev);
+
++ /* Disable the RETRY_TIMEOUT register (0x41) to keep
++ * PCI Tx retries from interfering with C3 CPU state */
++ pci_read_config_dword(dev, 0x40, &val);
++ if ((val & 0x0000ff00) != 0)
++ pci_write_config_dword(dev, 0x40, val & 0xffff00ff);
++
+ err = ssb_bus_pcibus_register(ssb, dev);
+ if (err)
+ goto err_pci_release_regions;
+@@ -102,7 +110,7 @@ static void ssb_pcihost_remove(struct pc
+ pci_set_drvdata(dev, NULL);
+ }
+
+-int ssb_pcihost_register(struct pci_driver *driver)
++int __devinit ssb_pcihost_register(struct pci_driver *driver)
+ {
+ driver->probe = ssb_pcihost_probe;
+ driver->remove = ssb_pcihost_remove;
+--- a/drivers/ssb/pcmcia.c
++++ b/drivers/ssb/pcmcia.c
+@@ -3,7 +3,7 @@
+ * PCMCIA-Hostbus related functions
+ *
+ * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
+- * Copyright 2007-2008 Michael Buesch <mb@bu3sch.de>
++ * Copyright 2007-2008 Michael Buesch <m@bues.ch>
+ *
+ * Licensed under the GNU/GPL. See COPYING for details.
+ */
+@@ -617,136 +617,140 @@ static int ssb_pcmcia_sprom_check_crc(co
+ } \
+ } while (0)
+
+-int ssb_pcmcia_get_invariants(struct ssb_bus *bus,
+- struct ssb_init_invariants *iv)
++static int ssb_pcmcia_get_mac(struct pcmcia_device *p_dev,
++ tuple_t *tuple,
++ void *priv)
++{
++ struct ssb_sprom *sprom = priv;
++
++ if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
++ return -EINVAL;
++ if (tuple->TupleDataLen != ETH_ALEN + 2)
++ return -EINVAL;
++ if (tuple->TupleData[1] != ETH_ALEN)
++ return -EINVAL;
++ memcpy(sprom->il0mac, &tuple->TupleData[2], ETH_ALEN);
++ return 0;
++};
++
++static int ssb_pcmcia_do_get_invariants(struct pcmcia_device *p_dev,
++ tuple_t *tuple,
++ void *priv)
+ {
+- tuple_t tuple;
+- int res;
+- unsigned char buf[32];
++ struct ssb_init_invariants *iv = priv;
+ struct ssb_sprom *sprom = &iv->sprom;
+ struct ssb_boardinfo *bi = &iv->boardinfo;
+ const char *error_description;
+
++ GOTO_ERROR_ON(tuple->TupleDataLen < 1, "VEN tpl < 1");
++ switch (tuple->TupleData[0]) {
++ case SSB_PCMCIA_CIS_ID:
++ GOTO_ERROR_ON((tuple->TupleDataLen != 5) &&
++ (tuple->TupleDataLen != 7),
++ "id tpl size");
++ bi->vendor = tuple->TupleData[1] |
++ ((u16)tuple->TupleData[2] << 8);
++ break;
++ case SSB_PCMCIA_CIS_BOARDREV:
++ GOTO_ERROR_ON(tuple->TupleDataLen != 2,
++ "boardrev tpl size");
++ sprom->board_rev = tuple->TupleData[1];
++ break;
++ case SSB_PCMCIA_CIS_PA:
++ GOTO_ERROR_ON((tuple->TupleDataLen != 9) &&
++ (tuple->TupleDataLen != 10),
++ "pa tpl size");
++ sprom->pa0b0 = tuple->TupleData[1] |
++ ((u16)tuple->TupleData[2] << 8);
++ sprom->pa0b1 = tuple->TupleData[3] |
++ ((u16)tuple->TupleData[4] << 8);
++ sprom->pa0b2 = tuple->TupleData[5] |
++ ((u16)tuple->TupleData[6] << 8);
++ sprom->itssi_a = tuple->TupleData[7];
++ sprom->itssi_bg = tuple->TupleData[7];
++ sprom->maxpwr_a = tuple->TupleData[8];
++ sprom->maxpwr_bg = tuple->TupleData[8];
++ break;
++ case SSB_PCMCIA_CIS_OEMNAME:
++ /* We ignore this. */
++ break;
++ case SSB_PCMCIA_CIS_CCODE:
++ GOTO_ERROR_ON(tuple->TupleDataLen != 2,
++ "ccode tpl size");
++ sprom->country_code = tuple->TupleData[1];
++ break;
++ case SSB_PCMCIA_CIS_ANTENNA:
++ GOTO_ERROR_ON(tuple->TupleDataLen != 2,
++ "ant tpl size");
++ sprom->ant_available_a = tuple->TupleData[1];
++ sprom->ant_available_bg = tuple->TupleData[1];
++ break;
++ case SSB_PCMCIA_CIS_ANTGAIN:
++ GOTO_ERROR_ON(tuple->TupleDataLen != 2,
++ "antg tpl size");
++ sprom->antenna_gain.ghz24.a0 = tuple->TupleData[1];
++ sprom->antenna_gain.ghz24.a1 = tuple->TupleData[1];
++ sprom->antenna_gain.ghz24.a2 = tuple->TupleData[1];
++ sprom->antenna_gain.ghz24.a3 = tuple->TupleData[1];
++ sprom->antenna_gain.ghz5.a0 = tuple->TupleData[1];
++ sprom->antenna_gain.ghz5.a1 = tuple->TupleData[1];
++ sprom->antenna_gain.ghz5.a2 = tuple->TupleData[1];
++ sprom->antenna_gain.ghz5.a3 = tuple->TupleData[1];
++ break;
++ case SSB_PCMCIA_CIS_BFLAGS:
++ GOTO_ERROR_ON((tuple->TupleDataLen != 3) &&
++ (tuple->TupleDataLen != 5),
++ "bfl tpl size");
++ sprom->boardflags_lo = tuple->TupleData[1] |
++ ((u16)tuple->TupleData[2] << 8);
++ break;
++ case SSB_PCMCIA_CIS_LEDS:
++ GOTO_ERROR_ON(tuple->TupleDataLen != 5,
++ "leds tpl size");
++ sprom->gpio0 = tuple->TupleData[1];
++ sprom->gpio1 = tuple->TupleData[2];
++ sprom->gpio2 = tuple->TupleData[3];
++ sprom->gpio3 = tuple->TupleData[4];
++ break;
++ }
++ return -ENOSPC; /* continue with next entry */
++
++error:
++ ssb_printk(KERN_ERR PFX
++ "PCMCIA: Failed to fetch device invariants: %s\n",
++ error_description);
++ return -ENODEV;
++}
++
++
++int ssb_pcmcia_get_invariants(struct ssb_bus *bus,
++ struct ssb_init_invariants *iv)
++{
++ struct ssb_sprom *sprom = &iv->sprom;
++ int res;
++
+ memset(sprom, 0xFF, sizeof(*sprom));
+ sprom->revision = 1;
+ sprom->boardflags_lo = 0;
+ sprom->boardflags_hi = 0;
+
+ /* First fetch the MAC address. */
+- memset(&tuple, 0, sizeof(tuple));
+- tuple.DesiredTuple = CISTPL_FUNCE;
+- tuple.TupleData = buf;
+- tuple.TupleDataMax = sizeof(buf);
+- res = pcmcia_get_first_tuple(bus->host_pcmcia, &tuple);
+- GOTO_ERROR_ON(res != 0, "MAC first tpl");
+- res = pcmcia_get_tuple_data(bus->host_pcmcia, &tuple);
+- GOTO_ERROR_ON(res != 0, "MAC first tpl data");
+- while (1) {
+- GOTO_ERROR_ON(tuple.TupleDataLen < 1, "MAC tpl < 1");
+- if (tuple.TupleData[0] == CISTPL_FUNCE_LAN_NODE_ID)
+- break;
+- res = pcmcia_get_next_tuple(bus->host_pcmcia, &tuple);
+- GOTO_ERROR_ON(res != 0, "MAC next tpl");
+- res = pcmcia_get_tuple_data(bus->host_pcmcia, &tuple);
+- GOTO_ERROR_ON(res != 0, "MAC next tpl data");
++ res = pcmcia_loop_tuple(bus->host_pcmcia, CISTPL_FUNCE,
++ ssb_pcmcia_get_mac, sprom);
++ if (res != 0) {
++ ssb_printk(KERN_ERR PFX
++ "PCMCIA: Failed to fetch MAC address\n");
++ return -ENODEV;
+ }
+- GOTO_ERROR_ON(tuple.TupleDataLen != ETH_ALEN + 2, "MAC tpl size");
+- memcpy(sprom->il0mac, &tuple.TupleData[2], ETH_ALEN);
+
+ /* Fetch the vendor specific tuples. */
+- memset(&tuple, 0, sizeof(tuple));
+- tuple.DesiredTuple = SSB_PCMCIA_CIS;
+- tuple.TupleData = buf;
+- tuple.TupleDataMax = sizeof(buf);
+- res = pcmcia_get_first_tuple(bus->host_pcmcia, &tuple);
+- GOTO_ERROR_ON(res != 0, "VEN first tpl");
+- res = pcmcia_get_tuple_data(bus->host_pcmcia, &tuple);
+- GOTO_ERROR_ON(res != 0, "VEN first tpl data");
+- while (1) {
+- GOTO_ERROR_ON(tuple.TupleDataLen < 1, "VEN tpl < 1");
+- switch (tuple.TupleData[0]) {
+- case SSB_PCMCIA_CIS_ID:
+- GOTO_ERROR_ON((tuple.TupleDataLen != 5) &&
+- (tuple.TupleDataLen != 7),
+- "id tpl size");
+- bi->vendor = tuple.TupleData[1] |
+- ((u16)tuple.TupleData[2] << 8);
+- break;
+- case SSB_PCMCIA_CIS_BOARDREV:
+- GOTO_ERROR_ON(tuple.TupleDataLen != 2,
+- "boardrev tpl size");
+- sprom->board_rev = tuple.TupleData[1];
+- break;
+- case SSB_PCMCIA_CIS_PA:
+- GOTO_ERROR_ON((tuple.TupleDataLen != 9) &&
+- (tuple.TupleDataLen != 10),
+- "pa tpl size");
+- sprom->pa0b0 = tuple.TupleData[1] |
+- ((u16)tuple.TupleData[2] << 8);
+- sprom->pa0b1 = tuple.TupleData[3] |
+- ((u16)tuple.TupleData[4] << 8);
+- sprom->pa0b2 = tuple.TupleData[5] |
+- ((u16)tuple.TupleData[6] << 8);
+- sprom->itssi_a = tuple.TupleData[7];
+- sprom->itssi_bg = tuple.TupleData[7];
+- sprom->maxpwr_a = tuple.TupleData[8];
+- sprom->maxpwr_bg = tuple.TupleData[8];
+- break;
+- case SSB_PCMCIA_CIS_OEMNAME:
+- /* We ignore this. */
+- break;
+- case SSB_PCMCIA_CIS_CCODE:
+- GOTO_ERROR_ON(tuple.TupleDataLen != 2,
+- "ccode tpl size");
+- sprom->country_code = tuple.TupleData[1];
+- break;
+- case SSB_PCMCIA_CIS_ANTENNA:
+- GOTO_ERROR_ON(tuple.TupleDataLen != 2,
+- "ant tpl size");
+- sprom->ant_available_a = tuple.TupleData[1];
+- sprom->ant_available_bg = tuple.TupleData[1];
+- break;
+- case SSB_PCMCIA_CIS_ANTGAIN:
+- GOTO_ERROR_ON(tuple.TupleDataLen != 2,
+- "antg tpl size");
+- sprom->antenna_gain.ghz24.a0 = tuple.TupleData[1];
+- sprom->antenna_gain.ghz24.a1 = tuple.TupleData[1];
+- sprom->antenna_gain.ghz24.a2 = tuple.TupleData[1];
+- sprom->antenna_gain.ghz24.a3 = tuple.TupleData[1];
+- sprom->antenna_gain.ghz5.a0 = tuple.TupleData[1];
+- sprom->antenna_gain.ghz5.a1 = tuple.TupleData[1];
+- sprom->antenna_gain.ghz5.a2 = tuple.TupleData[1];
+- sprom->antenna_gain.ghz5.a3 = tuple.TupleData[1];
+- break;
+- case SSB_PCMCIA_CIS_BFLAGS:
+- GOTO_ERROR_ON((tuple.TupleDataLen != 3) &&
+- (tuple.TupleDataLen != 5),
+- "bfl tpl size");
+- sprom->boardflags_lo = tuple.TupleData[1] |
+- ((u16)tuple.TupleData[2] << 8);
+- break;
+- case SSB_PCMCIA_CIS_LEDS:
+- GOTO_ERROR_ON(tuple.TupleDataLen != 5,
+- "leds tpl size");
+- sprom->gpio0 = tuple.TupleData[1];
+- sprom->gpio1 = tuple.TupleData[2];
+- sprom->gpio2 = tuple.TupleData[3];
+- sprom->gpio3 = tuple.TupleData[4];
+- break;
+- }
+- res = pcmcia_get_next_tuple(bus->host_pcmcia, &tuple);
+- if (res == -ENOSPC)
+- break;
+- GOTO_ERROR_ON(res != 0, "VEN next tpl");
+- res = pcmcia_get_tuple_data(bus->host_pcmcia, &tuple);
+- GOTO_ERROR_ON(res != 0, "VEN next tpl data");
+- }
++ res = pcmcia_loop_tuple(bus->host_pcmcia, SSB_PCMCIA_CIS,
++ ssb_pcmcia_do_get_invariants, iv);
++ if ((res == 0) || (res == -ENOSPC))
++ return 0;
+
+- return 0;
+-error:
+ ssb_printk(KERN_ERR PFX
+- "PCMCIA: Failed to fetch device invariants: %s\n",
+- error_description);
++ "PCMCIA: Failed to fetch device invariants\n");
+ return -ENODEV;
+ }
+
+--- a/drivers/ssb/scan.c
++++ b/drivers/ssb/scan.c
+@@ -2,7 +2,7 @@
+ * Sonics Silicon Backplane
+ * Bus scanning
+ *
+- * Copyright (C) 2005-2007 Michael Buesch <mb@bu3sch.de>
++ * Copyright (C) 2005-2007 Michael Buesch <m@bues.ch>
+ * Copyright (C) 2005 Martin Langer <martin-langer@gmx.de>
+ * Copyright (C) 2005 Stefano Brivio <st3@riseup.net>
+ * Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
+@@ -162,6 +162,8 @@ static u8 chipid_to_nrcores(u16 chipid)
+ static u32 scan_read32(struct ssb_bus *bus, u8 current_coreidx,
+ u16 offset)
+ {
++ u32 lo, hi;
++
+ switch (bus->bustype) {
+ case SSB_BUSTYPE_SSB:
+ offset += current_coreidx * SSB_CORE_SIZE;
+@@ -174,7 +176,12 @@ static u32 scan_read32(struct ssb_bus *b
+ offset -= 0x800;
+ } else
+ ssb_pcmcia_switch_segment(bus, 0);
+- break;
++ lo = readw(bus->mmio + offset);
++ hi = readw(bus->mmio + offset + 2);
++ return lo | (hi << 16);
++ case SSB_BUSTYPE_SDIO:
++ offset += current_coreidx * SSB_CORE_SIZE;
++ return ssb_sdio_scan_read32(bus, offset);
+ }
+ return readl(bus->mmio + offset);
+ }
+@@ -188,6 +195,8 @@ static int scan_switchcore(struct ssb_bu
+ return ssb_pci_switch_coreidx(bus, coreidx);
+ case SSB_BUSTYPE_PCMCIA:
+ return ssb_pcmcia_switch_coreidx(bus, coreidx);
++ case SSB_BUSTYPE_SDIO:
++ return ssb_sdio_scan_switch_coreidx(bus, coreidx);
+ }
+ return 0;
+ }
+@@ -206,6 +215,8 @@ void ssb_iounmap(struct ssb_bus *bus)
+ SSB_BUG_ON(1); /* Can't reach this code. */
+ #endif
+ break;
++ case SSB_BUSTYPE_SDIO:
++ break;
+ }
+ bus->mmio = NULL;
+ bus->mapped_device = NULL;
+@@ -230,6 +241,10 @@ static void __iomem *ssb_ioremap(struct
+ SSB_BUG_ON(1); /* Can't reach this code. */
+ #endif
+ break;
++ case SSB_BUSTYPE_SDIO:
++ /* Nothing to ioremap in the SDIO case, just fake it */
++ mmio = (void __iomem *)baseaddr;
++ break;
+ }
+
+ return mmio;
+@@ -245,7 +260,10 @@ static int we_support_multiple_80211_cor
+ #ifdef CONFIG_SSB_PCIHOST
+ if (bus->bustype == SSB_BUSTYPE_PCI) {
+ if (bus->host_pci->vendor == PCI_VENDOR_ID_BROADCOM &&
+- bus->host_pci->device == 0x4324)
++ ((bus->host_pci->device == 0x4313) ||
++ (bus->host_pci->device == 0x431A) ||
++ (bus->host_pci->device == 0x4321) ||
++ (bus->host_pci->device == 0x4324)))
+ return 1;
+ }
+ #endif /* CONFIG_SSB_PCIHOST */
+@@ -294,8 +312,7 @@ int ssb_bus_scan(struct ssb_bus *bus,
+ } else {
+ if (bus->bustype == SSB_BUSTYPE_PCI) {
+ bus->chip_id = pcidev_to_chipid(bus->host_pci);
+- pci_read_config_word(bus->host_pci, PCI_REVISION_ID,
+- &bus->chip_rev);
++ bus->chip_rev = bus->host_pci->revision;
+ bus->chip_package = 0;
+ } else {
+ bus->chip_id = 0x4710;
+@@ -339,7 +356,7 @@ int ssb_bus_scan(struct ssb_bus *bus,
+ dev->bus = bus;
+ dev->ops = bus->ops;
+
+- ssb_dprintk(KERN_INFO PFX
++ printk(KERN_DEBUG PFX
+ "Core %d found: %s "
+ "(cc 0x%03X, rev 0x%02X, vendor 0x%04X)\n",
+ i, ssb_core_name(dev->id.coreid),
+@@ -407,6 +424,16 @@ int ssb_bus_scan(struct ssb_bus *bus,
+ bus->pcicore.dev = dev;
+ #endif /* CONFIG_SSB_DRIVER_PCICORE */
+ break;
++ case SSB_DEV_ETHERNET:
++ if (bus->bustype == SSB_BUSTYPE_PCI) {
++ if (bus->host_pci->vendor == PCI_VENDOR_ID_BROADCOM &&
++ (bus->host_pci->device & 0xFF00) == 0x4300) {
++ /* This is a dangling ethernet core on a
++ * wireless device. Ignore it. */
++ continue;
++ }
++ }
++ break;
+ default:
+ break;
+ }
+--- /dev/null
++++ b/drivers/ssb/sdio.c
+@@ -0,0 +1,610 @@
++/*
++ * Sonics Silicon Backplane
++ * SDIO-Hostbus related functions
++ *
++ * Copyright 2009 Albert Herranz <albert_herranz@yahoo.es>
++ *
++ * Based on drivers/ssb/pcmcia.c
++ * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
++ * Copyright 2007-2008 Michael Buesch <m@bues.ch>
++ *
++ * Licensed under the GNU/GPL. See COPYING for details.
++ *
++ */
++
++#include <linux/ssb/ssb.h>
++#include <linux/delay.h>
++#include <linux/io.h>
++#include <linux/etherdevice.h>
++#include <linux/mmc/sdio_func.h>
++
++#include "ssb_private.h"
++
++/* Define the following to 1 to enable a printk on each coreswitch. */
++#define SSB_VERBOSE_SDIOCORESWITCH_DEBUG 0
++
++
++/* Hardware invariants CIS tuples */
++#define SSB_SDIO_CIS 0x80
++#define SSB_SDIO_CIS_SROMREV 0x00
++#define SSB_SDIO_CIS_ID 0x01
++#define SSB_SDIO_CIS_BOARDREV 0x02
++#define SSB_SDIO_CIS_PA 0x03
++#define SSB_SDIO_CIS_PA_PA0B0_LO 0
++#define SSB_SDIO_CIS_PA_PA0B0_HI 1
++#define SSB_SDIO_CIS_PA_PA0B1_LO 2
++#define SSB_SDIO_CIS_PA_PA0B1_HI 3
++#define SSB_SDIO_CIS_PA_PA0B2_LO 4
++#define SSB_SDIO_CIS_PA_PA0B2_HI 5
++#define SSB_SDIO_CIS_PA_ITSSI 6
++#define SSB_SDIO_CIS_PA_MAXPOW 7
++#define SSB_SDIO_CIS_OEMNAME 0x04
++#define SSB_SDIO_CIS_CCODE 0x05
++#define SSB_SDIO_CIS_ANTENNA 0x06
++#define SSB_SDIO_CIS_ANTGAIN 0x07
++#define SSB_SDIO_CIS_BFLAGS 0x08
++#define SSB_SDIO_CIS_LEDS 0x09
++
++#define CISTPL_FUNCE_LAN_NODE_ID 0x04 /* same as in PCMCIA */
++
++
++/*
++ * Function 1 miscellaneous registers.
++ *
++ * Definitions match src/include/sbsdio.h from the
++ * Android Open Source Project
++ * http://android.git.kernel.org/?p=platform/system/wlan/broadcom.git
++ *
++ */
++#define SBSDIO_FUNC1_SBADDRLOW 0x1000a /* SB Address window Low (b15) */
++#define SBSDIO_FUNC1_SBADDRMID 0x1000b /* SB Address window Mid (b23-b16) */
++#define SBSDIO_FUNC1_SBADDRHIGH 0x1000c /* SB Address window High (b24-b31) */
++
++/* valid bits in SBSDIO_FUNC1_SBADDRxxx regs */
++#define SBSDIO_SBADDRLOW_MASK 0x80 /* Valid address bits in SBADDRLOW */
++#define SBSDIO_SBADDRMID_MASK 0xff /* Valid address bits in SBADDRMID */
++#define SBSDIO_SBADDRHIGH_MASK 0xff /* Valid address bits in SBADDRHIGH */
++
++#define SBSDIO_SB_OFT_ADDR_MASK 0x7FFF /* sb offset addr is <= 15 bits, 32k */
++
++/* REVISIT: this flag doesn't seem to matter */
++#define SBSDIO_SB_ACCESS_2_4B_FLAG 0x8000 /* forces 32-bit SB access */
++
++
++/*
++ * Address map within the SDIO function address space (128K).
++ *
++ * Start End Description
++ * ------- ------- ------------------------------------------
++ * 0x00000 0x0ffff selected backplane address window (64K)
++ * 0x10000 0x1ffff backplane control registers (max 64K)
++ *
++ * The current address window is configured by writing to registers
++ * SBADDRLOW, SBADDRMID and SBADDRHIGH.
++ *
++ * In order to access the contents of a 32-bit Silicon Backplane address
++ * the backplane address window must be first loaded with the highest
++ * 16 bits of the target address. Then, an access must be done to the
++ * SDIO function address space using the lower 15 bits of the address.
++ * Bit 15 of the address must be set when doing 32 bit accesses.
++ *
++ * 10987654321098765432109876543210
++ * WWWWWWWWWWWWWWWWW SB Address Window
++ * OOOOOOOOOOOOOOOO Offset within SB Address Window
++ * a 32-bit access flag
++ */
++
++
++/*
++ * SSB I/O via SDIO.
++ *
++ * NOTE: SDIO address @addr is 17 bits long (SDIO address space is 128K).
++ */
++
++static inline struct device *ssb_sdio_dev(struct ssb_bus *bus)
++{
++ return &bus->host_sdio->dev;
++}
++
++/* host claimed */
++static int ssb_sdio_writeb(struct ssb_bus *bus, unsigned int addr, u8 val)
++{
++ int error = 0;
++
++ sdio_writeb(bus->host_sdio, val, addr, &error);
++ if (unlikely(error)) {
++ dev_dbg(ssb_sdio_dev(bus), "%08X <- %02x, error %d\n",
++ addr, val, error);
++ }
++
++ return error;
++}
++
++#if 0
++static u8 ssb_sdio_readb(struct ssb_bus *bus, unsigned int addr)
++{
++ u8 val;
++ int error = 0;
++
++ val = sdio_readb(bus->host_sdio, addr, &error);
++ if (unlikely(error)) {
++ dev_dbg(ssb_sdio_dev(bus), "%08X -> %02x, error %d\n",
++ addr, val, error);
++ }
++
++ return val;
++}
++#endif
++
++/* host claimed */
++static int ssb_sdio_set_sbaddr_window(struct ssb_bus *bus, u32 address)
++{
++ int error;
++
++ error = ssb_sdio_writeb(bus, SBSDIO_FUNC1_SBADDRLOW,
++ (address >> 8) & SBSDIO_SBADDRLOW_MASK);
++ if (error)
++ goto out;
++ error = ssb_sdio_writeb(bus, SBSDIO_FUNC1_SBADDRMID,
++ (address >> 16) & SBSDIO_SBADDRMID_MASK);
++ if (error)
++ goto out;
++ error = ssb_sdio_writeb(bus, SBSDIO_FUNC1_SBADDRHIGH,
++ (address >> 24) & SBSDIO_SBADDRHIGH_MASK);
++ if (error)
++ goto out;
++ bus->sdio_sbaddr = address;
++out:
++ if (error) {
++ dev_dbg(ssb_sdio_dev(bus), "failed to set address window"
++ " to 0x%08x, error %d\n", address, error);
++ }
++
++ return error;
++}
++
++/* for enumeration use only */
++u32 ssb_sdio_scan_read32(struct ssb_bus *bus, u16 offset)
++{
++ u32 val;
++ int error;
++
++ sdio_claim_host(bus->host_sdio);
++ val = sdio_readl(bus->host_sdio, offset, &error);
++ sdio_release_host(bus->host_sdio);
++ if (unlikely(error)) {
++ dev_dbg(ssb_sdio_dev(bus), "%04X:%04X > %08x, error %d\n",
++ bus->sdio_sbaddr >> 16, offset, val, error);
++ }
++
++ return val;
++}
++
++/* for enumeration use only */
++int ssb_sdio_scan_switch_coreidx(struct ssb_bus *bus, u8 coreidx)
++{
++ u32 sbaddr;
++ int error;
++
++ sbaddr = (coreidx * SSB_CORE_SIZE) + SSB_ENUM_BASE;
++ sdio_claim_host(bus->host_sdio);
++ error = ssb_sdio_set_sbaddr_window(bus, sbaddr);
++ sdio_release_host(bus->host_sdio);
++ if (error) {
++ dev_err(ssb_sdio_dev(bus), "failed to switch to core %u,"
++ " error %d\n", coreidx, error);
++ goto out;
++ }
++out:
++ return error;
++}
++
++/* host must be already claimed */
++int ssb_sdio_switch_core(struct ssb_bus *bus, struct ssb_device *dev)
++{
++ u8 coreidx = dev->core_index;
++ u32 sbaddr;
++ int error = 0;
++
++ sbaddr = (coreidx * SSB_CORE_SIZE) + SSB_ENUM_BASE;
++ if (unlikely(bus->sdio_sbaddr != sbaddr)) {
++#if SSB_VERBOSE_SDIOCORESWITCH_DEBUG
++ dev_info(ssb_sdio_dev(bus),
++ "switching to %s core, index %d\n",
++ ssb_core_name(dev->id.coreid), coreidx);
++#endif
++ error = ssb_sdio_set_sbaddr_window(bus, sbaddr);
++ if (error) {
++ dev_dbg(ssb_sdio_dev(bus), "failed to switch to"
++ " core %u, error %d\n", coreidx, error);
++ goto out;
++ }
++ bus->mapped_device = dev;
++ }
++
++out:
++ return error;
++}
++
++static u8 ssb_sdio_read8(struct ssb_device *dev, u16 offset)
++{
++ struct ssb_bus *bus = dev->bus;
++ u8 val = 0xff;
++ int error = 0;
++
++ sdio_claim_host(bus->host_sdio);
++ if (unlikely(ssb_sdio_switch_core(bus, dev)))
++ goto out;
++ offset |= bus->sdio_sbaddr & 0xffff;
++ offset &= SBSDIO_SB_OFT_ADDR_MASK;
++ val = sdio_readb(bus->host_sdio, offset, &error);
++ if (error) {
++ dev_dbg(ssb_sdio_dev(bus), "%04X:%04X > %02x, error %d\n",
++ bus->sdio_sbaddr >> 16, offset, val, error);
++ }
++out:
++ sdio_release_host(bus->host_sdio);
++
++ return val;
++}
++
++static u16 ssb_sdio_read16(struct ssb_device *dev, u16 offset)
++{
++ struct ssb_bus *bus = dev->bus;
++ u16 val = 0xffff;
++ int error = 0;
++
++ sdio_claim_host(bus->host_sdio);
++ if (unlikely(ssb_sdio_switch_core(bus, dev)))
++ goto out;
++ offset |= bus->sdio_sbaddr & 0xffff;
++ offset &= SBSDIO_SB_OFT_ADDR_MASK;
++ val = sdio_readw(bus->host_sdio, offset, &error);
++ if (error) {
++ dev_dbg(ssb_sdio_dev(bus), "%04X:%04X > %04x, error %d\n",
++ bus->sdio_sbaddr >> 16, offset, val, error);
++ }
++out:
++ sdio_release_host(bus->host_sdio);
++
++ return val;
++}
++
++static u32 ssb_sdio_read32(struct ssb_device *dev, u16 offset)
++{
++ struct ssb_bus *bus = dev->bus;
++ u32 val = 0xffffffff;
++ int error = 0;
++
++ sdio_claim_host(bus->host_sdio);
++ if (unlikely(ssb_sdio_switch_core(bus, dev)))
++ goto out;
++ offset |= bus->sdio_sbaddr & 0xffff;
++ offset &= SBSDIO_SB_OFT_ADDR_MASK;
++ offset |= SBSDIO_SB_ACCESS_2_4B_FLAG; /* 32 bit data access */
++ val = sdio_readl(bus->host_sdio, offset, &error);
++ if (error) {
++ dev_dbg(ssb_sdio_dev(bus), "%04X:%04X > %08x, error %d\n",
++ bus->sdio_sbaddr >> 16, offset, val, error);
++ }
++out:
++ sdio_release_host(bus->host_sdio);
++
++ return val;
++}
++
++#ifdef CONFIG_SSB_BLOCKIO
++static void ssb_sdio_block_read(struct ssb_device *dev, void *buffer,
++ size_t count, u16 offset, u8 reg_width)
++{
++ size_t saved_count = count;
++ struct ssb_bus *bus = dev->bus;
++ int error = 0;
++
++ sdio_claim_host(bus->host_sdio);
++ if (unlikely(ssb_sdio_switch_core(bus, dev))) {
++ error = -EIO;
++ memset(buffer, 0xff, count);
++ goto err_out;
++ }
++ offset |= bus->sdio_sbaddr & 0xffff;
++ offset &= SBSDIO_SB_OFT_ADDR_MASK;
++
++ switch (reg_width) {
++ case sizeof(u8): {
++ error = sdio_readsb(bus->host_sdio, buffer, offset, count);
++ break;
++ }
++ case sizeof(u16): {
++ SSB_WARN_ON(count & 1);
++ error = sdio_readsb(bus->host_sdio, buffer, offset, count);
++ break;
++ }
++ case sizeof(u32): {
++ SSB_WARN_ON(count & 3);
++ offset |= SBSDIO_SB_ACCESS_2_4B_FLAG; /* 32 bit data access */
++ error = sdio_readsb(bus->host_sdio, buffer, offset, count);
++ break;
++ }
++ default:
++ SSB_WARN_ON(1);
++ }
++ if (!error)
++ goto out;
++
++err_out:
++ dev_dbg(ssb_sdio_dev(bus), "%04X:%04X (width=%u, len=%zu), error %d\n",
++ bus->sdio_sbaddr >> 16, offset, reg_width, saved_count, error);
++out:
++ sdio_release_host(bus->host_sdio);
++}
++#endif /* CONFIG_SSB_BLOCKIO */
++
++static void ssb_sdio_write8(struct ssb_device *dev, u16 offset, u8 val)
++{
++ struct ssb_bus *bus = dev->bus;
++ int error = 0;
++
++ sdio_claim_host(bus->host_sdio);
++ if (unlikely(ssb_sdio_switch_core(bus, dev)))
++ goto out;
++ offset |= bus->sdio_sbaddr & 0xffff;
++ offset &= SBSDIO_SB_OFT_ADDR_MASK;
++ sdio_writeb(bus->host_sdio, val, offset, &error);
++ if (error) {
++ dev_dbg(ssb_sdio_dev(bus), "%04X:%04X < %02x, error %d\n",
++ bus->sdio_sbaddr >> 16, offset, val, error);
++ }
++out:
++ sdio_release_host(bus->host_sdio);
++}
++
++static void ssb_sdio_write16(struct ssb_device *dev, u16 offset, u16 val)
++{
++ struct ssb_bus *bus = dev->bus;
++ int error = 0;
++
++ sdio_claim_host(bus->host_sdio);
++ if (unlikely(ssb_sdio_switch_core(bus, dev)))
++ goto out;
++ offset |= bus->sdio_sbaddr & 0xffff;
++ offset &= SBSDIO_SB_OFT_ADDR_MASK;
++ sdio_writew(bus->host_sdio, val, offset, &error);
++ if (error) {
++ dev_dbg(ssb_sdio_dev(bus), "%04X:%04X < %04x, error %d\n",
++ bus->sdio_sbaddr >> 16, offset, val, error);
++ }
++out:
++ sdio_release_host(bus->host_sdio);
++}
++
++static void ssb_sdio_write32(struct ssb_device *dev, u16 offset, u32 val)
++{
++ struct ssb_bus *bus = dev->bus;
++ int error = 0;
++
++ sdio_claim_host(bus->host_sdio);
++ if (unlikely(ssb_sdio_switch_core(bus, dev)))
++ goto out;
++ offset |= bus->sdio_sbaddr & 0xffff;
++ offset &= SBSDIO_SB_OFT_ADDR_MASK;
++ offset |= SBSDIO_SB_ACCESS_2_4B_FLAG; /* 32 bit data access */
++ sdio_writel(bus->host_sdio, val, offset, &error);
++ if (error) {
++ dev_dbg(ssb_sdio_dev(bus), "%04X:%04X < %08x, error %d\n",
++ bus->sdio_sbaddr >> 16, offset, val, error);
++ }
++ if (bus->quirks & SSB_QUIRK_SDIO_READ_AFTER_WRITE32)
++ sdio_readl(bus->host_sdio, 0, &error);
++out:
++ sdio_release_host(bus->host_sdio);
++}
++
++#ifdef CONFIG_SSB_BLOCKIO
++static void ssb_sdio_block_write(struct ssb_device *dev, const void *buffer,
++ size_t count, u16 offset, u8 reg_width)
++{
++ size_t saved_count = count;
++ struct ssb_bus *bus = dev->bus;
++ int error = 0;
++
++ sdio_claim_host(bus->host_sdio);
++ if (unlikely(ssb_sdio_switch_core(bus, dev))) {
++ error = -EIO;
++ memset((void *)buffer, 0xff, count);
++ goto err_out;
++ }
++ offset |= bus->sdio_sbaddr & 0xffff;
++ offset &= SBSDIO_SB_OFT_ADDR_MASK;
++
++ switch (reg_width) {
++ case sizeof(u8):
++ error = sdio_writesb(bus->host_sdio, offset,
++ (void *)buffer, count);
++ break;
++ case sizeof(u16):
++ SSB_WARN_ON(count & 1);
++ error = sdio_writesb(bus->host_sdio, offset,
++ (void *)buffer, count);
++ break;
++ case sizeof(u32):
++ SSB_WARN_ON(count & 3);
++ offset |= SBSDIO_SB_ACCESS_2_4B_FLAG; /* 32 bit data access */
++ error = sdio_writesb(bus->host_sdio, offset,
++ (void *)buffer, count);
++ break;
++ default:
++ SSB_WARN_ON(1);
++ }
++ if (!error)
++ goto out;
++
++err_out:
++ dev_dbg(ssb_sdio_dev(bus), "%04X:%04X (width=%u, len=%zu), error %d\n",
++ bus->sdio_sbaddr >> 16, offset, reg_width, saved_count, error);
++out:
++ sdio_release_host(bus->host_sdio);
++}
++
++#endif /* CONFIG_SSB_BLOCKIO */
++
++/* Not "static", as it's used in main.c */
++const struct ssb_bus_ops ssb_sdio_ops = {
++ .read8 = ssb_sdio_read8,
++ .read16 = ssb_sdio_read16,
++ .read32 = ssb_sdio_read32,
++ .write8 = ssb_sdio_write8,
++ .write16 = ssb_sdio_write16,
++ .write32 = ssb_sdio_write32,
++#ifdef CONFIG_SSB_BLOCKIO
++ .block_read = ssb_sdio_block_read,
++ .block_write = ssb_sdio_block_write,
++#endif
++};
++
++#define GOTO_ERROR_ON(condition, description) do { \
++ if (unlikely(condition)) { \
++ error_description = description; \
++ goto error; \
++ } \
++ } while (0)
++
++int ssb_sdio_get_invariants(struct ssb_bus *bus,
++ struct ssb_init_invariants *iv)
++{
++ struct ssb_sprom *sprom = &iv->sprom;
++ struct ssb_boardinfo *bi = &iv->boardinfo;
++ const char *error_description = "none";
++ struct sdio_func_tuple *tuple;
++ void *mac;
++
++ memset(sprom, 0xFF, sizeof(*sprom));
++ sprom->boardflags_lo = 0;
++ sprom->boardflags_hi = 0;
++
++ tuple = bus->host_sdio->tuples;
++ while (tuple) {
++ switch (tuple->code) {
++ case 0x22: /* extended function */
++ switch (tuple->data[0]) {
++ case CISTPL_FUNCE_LAN_NODE_ID:
++ GOTO_ERROR_ON((tuple->size != 7) &&
++ (tuple->data[1] != 6),
++ "mac tpl size");
++ /* fetch the MAC address. */
++ mac = tuple->data + 2;
++ memcpy(sprom->il0mac, mac, ETH_ALEN);
++ memcpy(sprom->et1mac, mac, ETH_ALEN);
++ break;
++ default:
++ break;
++ }
++ break;
++ case 0x80: /* vendor specific tuple */
++ switch (tuple->data[0]) {
++ case SSB_SDIO_CIS_SROMREV:
++ GOTO_ERROR_ON(tuple->size != 2,
++ "sromrev tpl size");
++ sprom->revision = tuple->data[1];
++ break;
++ case SSB_SDIO_CIS_ID:
++ GOTO_ERROR_ON((tuple->size != 5) &&
++ (tuple->size != 7),
++ "id tpl size");
++ bi->vendor = tuple->data[1] |
++ (tuple->data[2]<<8);
++ break;
++ case SSB_SDIO_CIS_BOARDREV:
++ GOTO_ERROR_ON(tuple->size != 2,
++ "boardrev tpl size");
++ sprom->board_rev = tuple->data[1];
++ break;
++ case SSB_SDIO_CIS_PA:
++ GOTO_ERROR_ON((tuple->size != 9) &&
++ (tuple->size != 10),
++ "pa tpl size");
++ sprom->pa0b0 = tuple->data[1] |
++ ((u16)tuple->data[2] << 8);
++ sprom->pa0b1 = tuple->data[3] |
++ ((u16)tuple->data[4] << 8);
++ sprom->pa0b2 = tuple->data[5] |
++ ((u16)tuple->data[6] << 8);
++ sprom->itssi_a = tuple->data[7];
++ sprom->itssi_bg = tuple->data[7];
++ sprom->maxpwr_a = tuple->data[8];
++ sprom->maxpwr_bg = tuple->data[8];
++ break;
++ case SSB_SDIO_CIS_OEMNAME:
++ /* Not present */
++ break;
++ case SSB_SDIO_CIS_CCODE:
++ GOTO_ERROR_ON(tuple->size != 2,
++ "ccode tpl size");
++ sprom->country_code = tuple->data[1];
++ break;
++ case SSB_SDIO_CIS_ANTENNA:
++ GOTO_ERROR_ON(tuple->size != 2,
++ "ant tpl size");
++ sprom->ant_available_a = tuple->data[1];
++ sprom->ant_available_bg = tuple->data[1];
++ break;
++ case SSB_SDIO_CIS_ANTGAIN:
++ GOTO_ERROR_ON(tuple->size != 2,
++ "antg tpl size");
++ sprom->antenna_gain.ghz24.a0 = tuple->data[1];
++ sprom->antenna_gain.ghz24.a1 = tuple->data[1];
++ sprom->antenna_gain.ghz24.a2 = tuple->data[1];
++ sprom->antenna_gain.ghz24.a3 = tuple->data[1];
++ sprom->antenna_gain.ghz5.a0 = tuple->data[1];
++ sprom->antenna_gain.ghz5.a1 = tuple->data[1];
++ sprom->antenna_gain.ghz5.a2 = tuple->data[1];
++ sprom->antenna_gain.ghz5.a3 = tuple->data[1];
++ break;
++ case SSB_SDIO_CIS_BFLAGS:
++ GOTO_ERROR_ON((tuple->size != 3) &&
++ (tuple->size != 5),
++ "bfl tpl size");
++ sprom->boardflags_lo = tuple->data[1] |
++ ((u16)tuple->data[2] << 8);
++ break;
++ case SSB_SDIO_CIS_LEDS:
++ GOTO_ERROR_ON(tuple->size != 5,
++ "leds tpl size");
++ sprom->gpio0 = tuple->data[1];
++ sprom->gpio1 = tuple->data[2];
++ sprom->gpio2 = tuple->data[3];
++ sprom->gpio3 = tuple->data[4];
++ break;
++ default:
++ break;
++ }
++ break;
++ default:
++ break;
++ }
++ tuple = tuple->next;
++ }
++
++ return 0;
++error:
++ dev_err(ssb_sdio_dev(bus), "failed to fetch device invariants: %s\n",
++ error_description);
++ return -ENODEV;
++}
++
++void ssb_sdio_exit(struct ssb_bus *bus)
++{
++ if (bus->bustype != SSB_BUSTYPE_SDIO)
++ return;
++ /* Nothing to do here. */