1 /* 2 * Broadcom specific AMBA 3 * PCI Host 4 * 5 * Licensed under the GNU/GPL. See COPYING for details. 6 */ 7 8 #include "bcma_private.h" 9 #include <linux/bcma/bcma.h> 10 #include <linux/pci.h> 11 12 static void bcma_host_pci_switch_core(struct bcma_device *core) 13 { 14 pci_write_config_dword(core->bus->host_pci, BCMA_PCI_BAR0_WIN, 15 core->addr); 16 pci_write_config_dword(core->bus->host_pci, BCMA_PCI_BAR0_WIN2, 17 core->wrap); 18 core->bus->mapped_core = core; 19 pr_debug("Switched to core: 0x%X\n", core->id.id); 20 } 21 22 static u8 bcma_host_pci_read8(struct bcma_device *core, u16 offset) 23 { 24 if (core->bus->mapped_core != core) 25 bcma_host_pci_switch_core(core); 26 return ioread8(core->bus->mmio + offset); 27 } 28 29 static u16 bcma_host_pci_read16(struct bcma_device *core, u16 offset) 30 { 31 if (core->bus->mapped_core != core) 32 bcma_host_pci_switch_core(core); 33 return ioread16(core->bus->mmio + offset); 34 } 35 36 static u32 bcma_host_pci_read32(struct bcma_device *core, u16 offset) 37 { 38 if (core->bus->mapped_core != core) 39 bcma_host_pci_switch_core(core); 40 return ioread32(core->bus->mmio + offset); 41 } 42 43 static void bcma_host_pci_write8(struct bcma_device *core, u16 offset, 44 u8 value) 45 { 46 if (core->bus->mapped_core != core) 47 bcma_host_pci_switch_core(core); 48 iowrite8(value, core->bus->mmio + offset); 49 } 50 51 static void bcma_host_pci_write16(struct bcma_device *core, u16 offset, 52 u16 value) 53 { 54 if (core->bus->mapped_core != core) 55 bcma_host_pci_switch_core(core); 56 iowrite16(value, core->bus->mmio + offset); 57 } 58 59 static void bcma_host_pci_write32(struct bcma_device *core, u16 offset, 60 u32 value) 61 { 62 if (core->bus->mapped_core != core) 63 bcma_host_pci_switch_core(core); 64 iowrite32(value, core->bus->mmio + offset); 65 } 66 67 static u32 bcma_host_pci_aread32(struct bcma_device *core, u16 offset) 68 { 69 if (core->bus->mapped_core != core) 70 bcma_host_pci_switch_core(core); 71 return ioread32(core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset); 72 } 73 74 static void bcma_host_pci_awrite32(struct bcma_device *core, u16 offset, 75 u32 value) 76 { 77 if (core->bus->mapped_core != core) 78 bcma_host_pci_switch_core(core); 79 iowrite32(value, core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset); 80 } 81 82 const struct bcma_host_ops bcma_host_pci_ops = { 83 .read8 = bcma_host_pci_read8, 84 .read16 = bcma_host_pci_read16, 85 .read32 = bcma_host_pci_read32, 86 .write8 = bcma_host_pci_write8, 87 .write16 = bcma_host_pci_write16, 88 .write32 = bcma_host_pci_write32, 89 .aread32 = bcma_host_pci_aread32, 90 .awrite32 = bcma_host_pci_awrite32, 91 }; 92 93 static int bcma_host_pci_probe(struct pci_dev *dev, 94 const struct pci_device_id *id) 95 { 96 struct bcma_bus *bus; 97 int err = -ENOMEM; 98 const char *name; 99 u32 val; 100 101 /* Alloc */ 102 bus = kzalloc(sizeof(*bus), GFP_KERNEL); 103 if (!bus) 104 goto out; 105 106 /* Basic PCI configuration */ 107 err = pci_enable_device(dev); 108 if (err) 109 goto err_kfree_bus; 110 111 name = dev_name(&dev->dev); 112 if (dev->driver && dev->driver->name) 113 name = dev->driver->name; 114 err = pci_request_regions(dev, name); 115 if (err) 116 goto err_pci_disable; 117 pci_set_master(dev); 118 119 /* Disable the RETRY_TIMEOUT register (0x41) to keep 120 * PCI Tx retries from interfering with C3 CPU state */ 121 pci_read_config_dword(dev, 0x40, &val); 122 if ((val & 0x0000ff00) != 0) 123 pci_write_config_dword(dev, 0x40, val & 0xffff00ff); 124 125 /* SSB needed additional powering up, do we have any AMBA PCI cards? */ 126 if (!pci_is_pcie(dev)) 127 pr_err("PCI card detected, report problems.\n"); 128 129 /* Map MMIO */ 130 err = -ENOMEM; 131 bus->mmio = pci_iomap(dev, 0, ~0UL); 132 if (!bus->mmio) 133 goto err_pci_release_regions; 134 135 /* Host specific */ 136 bus->host_pci = dev; 137 bus->hosttype = BCMA_HOSTTYPE_PCI; 138 bus->ops = &bcma_host_pci_ops; 139 140 /* Register */ 141 err = bcma_bus_register(bus); 142 if (err) 143 goto err_pci_unmap_mmio; 144 145 pci_set_drvdata(dev, bus); 146 147 out: 148 return err; 149 150 err_pci_unmap_mmio: 151 pci_iounmap(dev, bus->mmio); 152 err_pci_release_regions: 153 pci_release_regions(dev); 154 err_pci_disable: 155 pci_disable_device(dev); 156 err_kfree_bus: 157 kfree(bus); 158 return err; 159 } 160 161 static void bcma_host_pci_remove(struct pci_dev *dev) 162 { 163 struct bcma_bus *bus = pci_get_drvdata(dev); 164 165 bcma_bus_unregister(bus); 166 pci_iounmap(dev, bus->mmio); 167 pci_release_regions(dev); 168 pci_disable_device(dev); 169 kfree(bus); 170 pci_set_drvdata(dev, NULL); 171 } 172 173 static DEFINE_PCI_DEVICE_TABLE(bcma_pci_bridge_tbl) = { 174 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4331) }, 175 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4353) }, 176 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4727) }, 177 { 0, }, 178 }; 179 MODULE_DEVICE_TABLE(pci, bcma_pci_bridge_tbl); 180 181 static struct pci_driver bcma_pci_bridge_driver = { 182 .name = "bcma-pci-bridge", 183 .id_table = bcma_pci_bridge_tbl, 184 .probe = bcma_host_pci_probe, 185 .remove = bcma_host_pci_remove, 186 }; 187 188 int __init bcma_host_pci_init(void) 189 { 190 return pci_register_driver(&bcma_pci_bridge_driver); 191 } 192 193 void __exit bcma_host_pci_exit(void) 194 { 195 pci_unregister_driver(&bcma_pci_bridge_driver); 196 } 197