Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Broadcom specific AMBA |
| 3 | * Bus subsystem |
| 4 | * |
| 5 | * Licensed under the GNU/GPL. See COPYING for details. |
| 6 | */ |
| 7 | |
| 8 | #include "bcma_private.h" |
Paul Gortmaker | 200351c | 2011-07-01 16:06:37 -0400 | [diff] [blame] | 9 | #include <linux/module.h> |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 10 | #include <linux/bcma/bcma.h> |
Geert Uytterhoeven | eef7269 | 2011-06-26 10:19:44 +0200 | [diff] [blame] | 11 | #include <linux/slab.h> |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 12 | |
| 13 | MODULE_DESCRIPTION("Broadcom's specific AMBA driver"); |
| 14 | MODULE_LICENSE("GPL"); |
| 15 | |
| 16 | static int bcma_bus_match(struct device *dev, struct device_driver *drv); |
| 17 | static int bcma_device_probe(struct device *dev); |
| 18 | static int bcma_device_remove(struct device *dev); |
David Woodhouse | 886b66e | 2011-08-19 22:14:47 +0200 | [diff] [blame] | 19 | static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 20 | |
| 21 | static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 22 | { |
| 23 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 24 | return sprintf(buf, "0x%03X\n", core->id.manuf); |
| 25 | } |
| 26 | static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 27 | { |
| 28 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 29 | return sprintf(buf, "0x%03X\n", core->id.id); |
| 30 | } |
| 31 | static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 32 | { |
| 33 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 34 | return sprintf(buf, "0x%02X\n", core->id.rev); |
| 35 | } |
| 36 | static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 37 | { |
| 38 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 39 | return sprintf(buf, "0x%X\n", core->id.class); |
| 40 | } |
| 41 | static struct device_attribute bcma_device_attrs[] = { |
| 42 | __ATTR_RO(manuf), |
| 43 | __ATTR_RO(id), |
| 44 | __ATTR_RO(rev), |
| 45 | __ATTR_RO(class), |
| 46 | __ATTR_NULL, |
| 47 | }; |
| 48 | |
| 49 | static struct bus_type bcma_bus_type = { |
| 50 | .name = "bcma", |
| 51 | .match = bcma_bus_match, |
| 52 | .probe = bcma_device_probe, |
| 53 | .remove = bcma_device_remove, |
David Woodhouse | 886b66e | 2011-08-19 22:14:47 +0200 | [diff] [blame] | 54 | .uevent = bcma_device_uevent, |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 55 | .dev_attrs = bcma_device_attrs, |
| 56 | }; |
| 57 | |
| 58 | static struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid) |
| 59 | { |
| 60 | struct bcma_device *core; |
| 61 | |
| 62 | list_for_each_entry(core, &bus->cores, list) { |
| 63 | if (core->id.id == coreid) |
| 64 | return core; |
| 65 | } |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | static void bcma_release_core_dev(struct device *dev) |
| 70 | { |
| 71 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
Hauke Mehrtens | ecd177c | 2011-07-23 01:20:08 +0200 | [diff] [blame] | 72 | if (core->io_addr) |
| 73 | iounmap(core->io_addr); |
| 74 | if (core->io_wrap) |
| 75 | iounmap(core->io_wrap); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 76 | kfree(core); |
| 77 | } |
| 78 | |
| 79 | static int bcma_register_cores(struct bcma_bus *bus) |
| 80 | { |
| 81 | struct bcma_device *core; |
| 82 | int err, dev_id = 0; |
| 83 | |
| 84 | list_for_each_entry(core, &bus->cores, list) { |
| 85 | /* We support that cores ourself */ |
| 86 | switch (core->id.id) { |
| 87 | case BCMA_CORE_CHIPCOMMON: |
| 88 | case BCMA_CORE_PCI: |
| 89 | case BCMA_CORE_PCIE: |
Hauke Mehrtens | 21e0534 | 2011-07-23 01:20:09 +0200 | [diff] [blame] | 90 | case BCMA_CORE_MIPS_74K: |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 91 | continue; |
| 92 | } |
| 93 | |
| 94 | core->dev.release = bcma_release_core_dev; |
| 95 | core->dev.bus = &bcma_bus_type; |
| 96 | dev_set_name(&core->dev, "bcma%d:%d", 0/*bus->num*/, dev_id); |
| 97 | |
| 98 | switch (bus->hosttype) { |
| 99 | case BCMA_HOSTTYPE_PCI: |
| 100 | core->dev.parent = &bus->host_pci->dev; |
Rafał Miłecki | 1bdcd09 | 2011-05-18 11:40:22 +0200 | [diff] [blame] | 101 | core->dma_dev = &bus->host_pci->dev; |
| 102 | core->irq = bus->host_pci->irq; |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 103 | break; |
Hauke Mehrtens | ecd177c | 2011-07-23 01:20:08 +0200 | [diff] [blame] | 104 | case BCMA_HOSTTYPE_SOC: |
| 105 | core->dev.dma_mask = &core->dev.coherent_dma_mask; |
| 106 | core->dma_dev = &core->dev; |
| 107 | break; |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 108 | case BCMA_HOSTTYPE_SDIO: |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | err = device_register(&core->dev); |
| 113 | if (err) { |
| 114 | pr_err("Could not register dev for core 0x%03X\n", |
| 115 | core->id.id); |
| 116 | continue; |
| 117 | } |
| 118 | core->dev_registered = true; |
| 119 | dev_id++; |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static void bcma_unregister_cores(struct bcma_bus *bus) |
| 126 | { |
| 127 | struct bcma_device *core; |
| 128 | |
| 129 | list_for_each_entry(core, &bus->cores, list) { |
| 130 | if (core->dev_registered) |
| 131 | device_unregister(&core->dev); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | int bcma_bus_register(struct bcma_bus *bus) |
| 136 | { |
| 137 | int err; |
| 138 | struct bcma_device *core; |
| 139 | |
| 140 | /* Scan for devices (cores) */ |
| 141 | err = bcma_bus_scan(bus); |
| 142 | if (err) { |
| 143 | pr_err("Failed to scan: %d\n", err); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | /* Init CC core */ |
| 148 | core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON); |
| 149 | if (core) { |
| 150 | bus->drv_cc.core = core; |
| 151 | bcma_core_chipcommon_init(&bus->drv_cc); |
| 152 | } |
| 153 | |
Hauke Mehrtens | 21e0534 | 2011-07-23 01:20:09 +0200 | [diff] [blame] | 154 | /* Init MIPS core */ |
| 155 | core = bcma_find_core(bus, BCMA_CORE_MIPS_74K); |
| 156 | if (core) { |
| 157 | bus->drv_mips.core = core; |
| 158 | bcma_core_mips_init(&bus->drv_mips); |
| 159 | } |
| 160 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 161 | /* Init PCIE core */ |
| 162 | core = bcma_find_core(bus, BCMA_CORE_PCIE); |
| 163 | if (core) { |
| 164 | bus->drv_pci.core = core; |
| 165 | bcma_core_pci_init(&bus->drv_pci); |
| 166 | } |
| 167 | |
Rafał Miłecki | 27f18dc | 2011-06-02 02:08:51 +0200 | [diff] [blame] | 168 | /* Try to get SPROM */ |
| 169 | err = bcma_sprom_get(bus); |
Hauke Mehrtens | 534e7a4 | 2011-07-09 13:22:03 +0200 | [diff] [blame] | 170 | if (err == -ENOENT) { |
| 171 | pr_err("No SPROM available\n"); |
| 172 | } else if (err) { |
Rafał Miłecki | 27f18dc | 2011-06-02 02:08:51 +0200 | [diff] [blame] | 173 | pr_err("Failed to get SPROM: %d\n", err); |
| 174 | return -ENOENT; |
| 175 | } |
| 176 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 177 | /* Register found cores */ |
| 178 | bcma_register_cores(bus); |
| 179 | |
| 180 | pr_info("Bus registered\n"); |
| 181 | |
| 182 | return 0; |
| 183 | } |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 184 | |
| 185 | void bcma_bus_unregister(struct bcma_bus *bus) |
| 186 | { |
| 187 | bcma_unregister_cores(bus); |
| 188 | } |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 189 | |
Hauke Mehrtens | 517f43e | 2011-07-23 01:20:07 +0200 | [diff] [blame] | 190 | int __init bcma_bus_early_register(struct bcma_bus *bus, |
| 191 | struct bcma_device *core_cc, |
| 192 | struct bcma_device *core_mips) |
| 193 | { |
| 194 | int err; |
| 195 | struct bcma_device *core; |
| 196 | struct bcma_device_id match; |
| 197 | |
| 198 | bcma_init_bus(bus); |
| 199 | |
| 200 | match.manuf = BCMA_MANUF_BCM; |
| 201 | match.id = BCMA_CORE_CHIPCOMMON; |
| 202 | match.class = BCMA_CL_SIM; |
| 203 | match.rev = BCMA_ANY_REV; |
| 204 | |
| 205 | /* Scan for chip common core */ |
| 206 | err = bcma_bus_scan_early(bus, &match, core_cc); |
| 207 | if (err) { |
| 208 | pr_err("Failed to scan for common core: %d\n", err); |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | match.manuf = BCMA_MANUF_MIPS; |
| 213 | match.id = BCMA_CORE_MIPS_74K; |
| 214 | match.class = BCMA_CL_SIM; |
| 215 | match.rev = BCMA_ANY_REV; |
| 216 | |
| 217 | /* Scan for mips core */ |
| 218 | err = bcma_bus_scan_early(bus, &match, core_mips); |
| 219 | if (err) { |
| 220 | pr_err("Failed to scan for mips core: %d\n", err); |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | /* Init CC core */ |
| 225 | core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON); |
| 226 | if (core) { |
| 227 | bus->drv_cc.core = core; |
| 228 | bcma_core_chipcommon_init(&bus->drv_cc); |
| 229 | } |
| 230 | |
Hauke Mehrtens | 21e0534 | 2011-07-23 01:20:09 +0200 | [diff] [blame] | 231 | /* Init MIPS core */ |
| 232 | core = bcma_find_core(bus, BCMA_CORE_MIPS_74K); |
| 233 | if (core) { |
| 234 | bus->drv_mips.core = core; |
| 235 | bcma_core_mips_init(&bus->drv_mips); |
| 236 | } |
| 237 | |
Hauke Mehrtens | 517f43e | 2011-07-23 01:20:07 +0200 | [diff] [blame] | 238 | pr_info("Early bus registered\n"); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Rafał Miłecki | 775ab52 | 2011-12-09 22:16:07 +0100 | [diff] [blame] | 243 | #ifdef CONFIG_PM |
Linus Torvalds | 685a4ef | 2012-01-13 23:58:40 +0100 | [diff] [blame] | 244 | int bcma_bus_suspend(struct bcma_bus *bus) |
| 245 | { |
Linus Torvalds | 7d5869e | 2012-01-13 23:58:41 +0100 | [diff] [blame] | 246 | struct bcma_device *core; |
| 247 | |
| 248 | list_for_each_entry(core, &bus->cores, list) { |
| 249 | struct device_driver *drv = core->dev.driver; |
| 250 | if (drv) { |
| 251 | struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv); |
| 252 | if (adrv->suspend) |
| 253 | adrv->suspend(core); |
| 254 | } |
| 255 | } |
Linus Torvalds | 685a4ef | 2012-01-13 23:58:40 +0100 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
Rafał Miłecki | 775ab52 | 2011-12-09 22:16:07 +0100 | [diff] [blame] | 259 | int bcma_bus_resume(struct bcma_bus *bus) |
| 260 | { |
| 261 | struct bcma_device *core; |
| 262 | |
| 263 | /* Init CC core */ |
| 264 | core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON); |
| 265 | if (core) { |
| 266 | bus->drv_cc.setup_done = false; |
| 267 | bcma_core_chipcommon_init(&bus->drv_cc); |
| 268 | } |
| 269 | |
Linus Torvalds | 7d5869e | 2012-01-13 23:58:41 +0100 | [diff] [blame] | 270 | list_for_each_entry(core, &bus->cores, list) { |
| 271 | struct device_driver *drv = core->dev.driver; |
| 272 | if (drv) { |
| 273 | struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv); |
| 274 | if (adrv->resume) |
| 275 | adrv->resume(core); |
| 276 | } |
| 277 | } |
| 278 | |
Rafał Miłecki | 775ab52 | 2011-12-09 22:16:07 +0100 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | #endif |
| 282 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 283 | int __bcma_driver_register(struct bcma_driver *drv, struct module *owner) |
| 284 | { |
| 285 | drv->drv.name = drv->name; |
| 286 | drv->drv.bus = &bcma_bus_type; |
| 287 | drv->drv.owner = owner; |
| 288 | |
| 289 | return driver_register(&drv->drv); |
| 290 | } |
| 291 | EXPORT_SYMBOL_GPL(__bcma_driver_register); |
| 292 | |
| 293 | void bcma_driver_unregister(struct bcma_driver *drv) |
| 294 | { |
| 295 | driver_unregister(&drv->drv); |
| 296 | } |
| 297 | EXPORT_SYMBOL_GPL(bcma_driver_unregister); |
| 298 | |
| 299 | static int bcma_bus_match(struct device *dev, struct device_driver *drv) |
| 300 | { |
| 301 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 302 | struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv); |
| 303 | const struct bcma_device_id *cid = &core->id; |
| 304 | const struct bcma_device_id *did; |
| 305 | |
| 306 | for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) { |
| 307 | if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) && |
| 308 | (did->id == cid->id || did->id == BCMA_ANY_ID) && |
| 309 | (did->rev == cid->rev || did->rev == BCMA_ANY_REV) && |
| 310 | (did->class == cid->class || did->class == BCMA_ANY_CLASS)) |
| 311 | return 1; |
| 312 | } |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int bcma_device_probe(struct device *dev) |
| 317 | { |
| 318 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 319 | struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver, |
| 320 | drv); |
| 321 | int err = 0; |
| 322 | |
| 323 | if (adrv->probe) |
| 324 | err = adrv->probe(core); |
| 325 | |
| 326 | return err; |
| 327 | } |
| 328 | |
| 329 | static int bcma_device_remove(struct device *dev) |
| 330 | { |
| 331 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 332 | struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver, |
| 333 | drv); |
| 334 | |
| 335 | if (adrv->remove) |
| 336 | adrv->remove(core); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
David Woodhouse | 886b66e | 2011-08-19 22:14:47 +0200 | [diff] [blame] | 341 | static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 342 | { |
| 343 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 344 | |
| 345 | return add_uevent_var(env, |
| 346 | "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X", |
| 347 | core->id.manuf, core->id.id, |
| 348 | core->id.rev, core->id.class); |
| 349 | } |
| 350 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 351 | static int __init bcma_modinit(void) |
| 352 | { |
| 353 | int err; |
| 354 | |
| 355 | err = bus_register(&bcma_bus_type); |
| 356 | if (err) |
| 357 | return err; |
| 358 | |
| 359 | #ifdef CONFIG_BCMA_HOST_PCI |
| 360 | err = bcma_host_pci_init(); |
| 361 | if (err) { |
| 362 | pr_err("PCI host initialization failed\n"); |
| 363 | err = 0; |
| 364 | } |
| 365 | #endif |
| 366 | |
| 367 | return err; |
| 368 | } |
| 369 | fs_initcall(bcma_modinit); |
| 370 | |
| 371 | static void __exit bcma_modexit(void) |
| 372 | { |
| 373 | #ifdef CONFIG_BCMA_HOST_PCI |
| 374 | bcma_host_pci_exit(); |
| 375 | #endif |
| 376 | bus_unregister(&bcma_bus_type); |
| 377 | } |
| 378 | module_exit(bcma_modexit) |