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 | d57ef3a | 2012-08-10 21:23:53 +0200 | [diff] [blame] | 10 | #include <linux/platform_device.h> |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 11 | #include <linux/bcma/bcma.h> |
Geert Uytterhoeven | eef7269 | 2011-06-26 10:19:44 +0200 | [diff] [blame] | 12 | #include <linux/slab.h> |
Hauke Mehrtens | 2101e53 | 2014-09-26 00:09:19 +0200 | [diff] [blame] | 13 | #include <linux/of_address.h> |
Hauke Mehrtens | 7178357 | 2014-11-01 16:54:56 +0100 | [diff] [blame] | 14 | #include <linux/of_irq.h> |
Rafał Miłecki | cae761b | 2015-06-28 17:17:13 +0200 | [diff] [blame^] | 15 | #include <linux/of_platform.h> |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 16 | |
| 17 | MODULE_DESCRIPTION("Broadcom's specific AMBA driver"); |
| 18 | MODULE_LICENSE("GPL"); |
| 19 | |
Hauke Mehrtens | 8f9ada4 | 2012-01-31 00:03:36 +0100 | [diff] [blame] | 20 | /* contains the number the next bus should get. */ |
| 21 | static unsigned int bcma_bus_next_num = 0; |
| 22 | |
| 23 | /* bcma_buses_mutex locks the bcma_bus_next_num */ |
| 24 | static DEFINE_MUTEX(bcma_buses_mutex); |
| 25 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 26 | static int bcma_bus_match(struct device *dev, struct device_driver *drv); |
| 27 | static int bcma_device_probe(struct device *dev); |
| 28 | static int bcma_device_remove(struct device *dev); |
David Woodhouse | 886b66e | 2011-08-19 22:14:47 +0200 | [diff] [blame] | 29 | 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] | 30 | |
| 31 | static ssize_t manuf_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%03X\n", core->id.manuf); |
| 35 | } |
Greg Kroah-Hartman | fdcf4f8 | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 36 | static DEVICE_ATTR_RO(manuf); |
| 37 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 38 | static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 39 | { |
| 40 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 41 | return sprintf(buf, "0x%03X\n", core->id.id); |
| 42 | } |
Greg Kroah-Hartman | fdcf4f8 | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 43 | static DEVICE_ATTR_RO(id); |
| 44 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 45 | static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 46 | { |
| 47 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 48 | return sprintf(buf, "0x%02X\n", core->id.rev); |
| 49 | } |
Greg Kroah-Hartman | fdcf4f8 | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 50 | static DEVICE_ATTR_RO(rev); |
| 51 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 52 | static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 53 | { |
| 54 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 55 | return sprintf(buf, "0x%X\n", core->id.class); |
| 56 | } |
Greg Kroah-Hartman | fdcf4f8 | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 57 | static DEVICE_ATTR_RO(class); |
| 58 | |
| 59 | static struct attribute *bcma_device_attrs[] = { |
| 60 | &dev_attr_manuf.attr, |
| 61 | &dev_attr_id.attr, |
| 62 | &dev_attr_rev.attr, |
| 63 | &dev_attr_class.attr, |
| 64 | NULL, |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 65 | }; |
Greg Kroah-Hartman | fdcf4f8 | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 66 | ATTRIBUTE_GROUPS(bcma_device); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 67 | |
| 68 | static struct bus_type bcma_bus_type = { |
| 69 | .name = "bcma", |
| 70 | .match = bcma_bus_match, |
| 71 | .probe = bcma_device_probe, |
| 72 | .remove = bcma_device_remove, |
David Woodhouse | 886b66e | 2011-08-19 22:14:47 +0200 | [diff] [blame] | 73 | .uevent = bcma_device_uevent, |
Greg Kroah-Hartman | fdcf4f8 | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 74 | .dev_groups = bcma_device_groups, |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 75 | }; |
| 76 | |
Rafał Miłecki | 6d5cfc9 | 2012-07-10 23:45:49 +0200 | [diff] [blame] | 77 | static u16 bcma_cc_core_id(struct bcma_bus *bus) |
| 78 | { |
| 79 | if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706) |
| 80 | return BCMA_CORE_4706_CHIPCOMMON; |
| 81 | return BCMA_CORE_CHIPCOMMON; |
| 82 | } |
| 83 | |
Hauke Mehrtens | 929a03a | 2013-01-04 00:51:20 +0100 | [diff] [blame] | 84 | struct bcma_device *bcma_find_core_unit(struct bcma_bus *bus, u16 coreid, |
| 85 | u8 unit) |
Hauke Mehrtens | dfae714 | 2012-09-29 20:40:18 +0200 | [diff] [blame] | 86 | { |
| 87 | struct bcma_device *core; |
| 88 | |
| 89 | list_for_each_entry(core, &bus->cores, list) { |
| 90 | if (core->id.id == coreid && core->core_unit == unit) |
| 91 | return core; |
| 92 | } |
| 93 | return NULL; |
| 94 | } |
Hauke Mehrtens | b2395b8 | 2014-01-05 01:10:43 +0100 | [diff] [blame] | 95 | EXPORT_SYMBOL_GPL(bcma_find_core_unit); |
Hauke Mehrtens | dfae714 | 2012-09-29 20:40:18 +0200 | [diff] [blame] | 96 | |
Rafał Miłecki | 88f9b65 | 2013-06-26 10:02:11 +0200 | [diff] [blame] | 97 | bool bcma_wait_value(struct bcma_device *core, u16 reg, u32 mask, u32 value, |
| 98 | int timeout) |
| 99 | { |
| 100 | unsigned long deadline = jiffies + timeout; |
| 101 | u32 val; |
| 102 | |
| 103 | do { |
| 104 | val = bcma_read32(core, reg); |
| 105 | if ((val & mask) == value) |
| 106 | return true; |
| 107 | cpu_relax(); |
| 108 | udelay(10); |
| 109 | } while (!time_after_eq(jiffies, deadline)); |
| 110 | |
| 111 | bcma_warn(core->bus, "Timeout waiting for register 0x%04X!\n", reg); |
| 112 | |
| 113 | return false; |
| 114 | } |
| 115 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 116 | static void bcma_release_core_dev(struct device *dev) |
| 117 | { |
| 118 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
Hauke Mehrtens | ecd177c | 2011-07-23 01:20:08 +0200 | [diff] [blame] | 119 | if (core->io_addr) |
| 120 | iounmap(core->io_addr); |
| 121 | if (core->io_wrap) |
| 122 | iounmap(core->io_wrap); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 123 | kfree(core); |
| 124 | } |
| 125 | |
Rafał Miłecki | 37a7f87 | 2014-09-05 00:18:49 +0200 | [diff] [blame] | 126 | static bool bcma_is_core_needed_early(u16 core_id) |
| 127 | { |
| 128 | switch (core_id) { |
| 129 | case BCMA_CORE_NS_NAND: |
| 130 | case BCMA_CORE_NS_QSPI: |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | return false; |
| 135 | } |
| 136 | |
Hauke Mehrtens | 78afe83 | 2014-10-09 23:39:41 +0200 | [diff] [blame] | 137 | #if defined(CONFIG_OF) && defined(CONFIG_OF_ADDRESS) |
Hauke Mehrtens | 2101e53 | 2014-09-26 00:09:19 +0200 | [diff] [blame] | 138 | static struct device_node *bcma_of_find_child_device(struct platform_device *parent, |
| 139 | struct bcma_device *core) |
| 140 | { |
| 141 | struct device_node *node; |
| 142 | u64 size; |
| 143 | const __be32 *reg; |
| 144 | |
| 145 | if (!parent || !parent->dev.of_node) |
| 146 | return NULL; |
| 147 | |
| 148 | for_each_child_of_node(parent->dev.of_node, node) { |
| 149 | reg = of_get_address(node, 0, &size, NULL); |
| 150 | if (!reg) |
| 151 | continue; |
| 152 | if (of_translate_address(node, reg) == core->addr) |
| 153 | return node; |
| 154 | } |
| 155 | return NULL; |
| 156 | } |
| 157 | |
Hauke Mehrtens | 7178357 | 2014-11-01 16:54:56 +0100 | [diff] [blame] | 158 | static int bcma_of_irq_parse(struct platform_device *parent, |
| 159 | struct bcma_device *core, |
| 160 | struct of_phandle_args *out_irq, int num) |
| 161 | { |
| 162 | __be32 laddr[1]; |
| 163 | int rc; |
| 164 | |
| 165 | if (core->dev.of_node) { |
| 166 | rc = of_irq_parse_one(core->dev.of_node, num, out_irq); |
| 167 | if (!rc) |
| 168 | return rc; |
| 169 | } |
| 170 | |
| 171 | out_irq->np = parent->dev.of_node; |
| 172 | out_irq->args_count = 1; |
| 173 | out_irq->args[0] = num; |
| 174 | |
| 175 | laddr[0] = cpu_to_be32(core->addr); |
| 176 | return of_irq_parse_raw(laddr, out_irq); |
| 177 | } |
| 178 | |
| 179 | static unsigned int bcma_of_get_irq(struct platform_device *parent, |
| 180 | struct bcma_device *core, int num) |
| 181 | { |
| 182 | struct of_phandle_args out_irq; |
| 183 | int ret; |
| 184 | |
| 185 | if (!parent || !parent->dev.of_node) |
| 186 | return 0; |
| 187 | |
| 188 | ret = bcma_of_irq_parse(parent, core, &out_irq, num); |
| 189 | if (ret) { |
| 190 | bcma_debug(core->bus, "bcma_of_get_irq() failed with rc=%d\n", |
| 191 | ret); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | return irq_create_of_mapping(&out_irq); |
| 196 | } |
| 197 | |
Hauke Mehrtens | 2101e53 | 2014-09-26 00:09:19 +0200 | [diff] [blame] | 198 | static void bcma_of_fill_device(struct platform_device *parent, |
| 199 | struct bcma_device *core) |
| 200 | { |
| 201 | struct device_node *node; |
| 202 | |
| 203 | node = bcma_of_find_child_device(parent, core); |
| 204 | if (node) |
| 205 | core->dev.of_node = node; |
Hauke Mehrtens | 7178357 | 2014-11-01 16:54:56 +0100 | [diff] [blame] | 206 | |
| 207 | core->irq = bcma_of_get_irq(parent, core, 0); |
Hauke Mehrtens | 2101e53 | 2014-09-26 00:09:19 +0200 | [diff] [blame] | 208 | } |
| 209 | #else |
| 210 | static void bcma_of_fill_device(struct platform_device *parent, |
| 211 | struct bcma_device *core) |
| 212 | { |
| 213 | } |
Hauke Mehrtens | 7178357 | 2014-11-01 16:54:56 +0100 | [diff] [blame] | 214 | static inline unsigned int bcma_of_get_irq(struct platform_device *parent, |
| 215 | struct bcma_device *core, int num) |
| 216 | { |
| 217 | return 0; |
| 218 | } |
Hauke Mehrtens | 2101e53 | 2014-09-26 00:09:19 +0200 | [diff] [blame] | 219 | #endif /* CONFIG_OF */ |
| 220 | |
Hauke Mehrtens | 85eb92e | 2014-11-01 16:54:55 +0100 | [diff] [blame] | 221 | unsigned int bcma_core_irq(struct bcma_device *core, int num) |
| 222 | { |
| 223 | struct bcma_bus *bus = core->bus; |
| 224 | unsigned int mips_irq; |
| 225 | |
| 226 | switch (bus->hosttype) { |
| 227 | case BCMA_HOSTTYPE_PCI: |
| 228 | return bus->host_pci->irq; |
| 229 | case BCMA_HOSTTYPE_SOC: |
| 230 | if (bus->drv_mips.core && num == 0) { |
| 231 | mips_irq = bcma_core_mips_irq(core); |
| 232 | return mips_irq <= 4 ? mips_irq + 2 : 0; |
| 233 | } |
Hauke Mehrtens | 7178357 | 2014-11-01 16:54:56 +0100 | [diff] [blame] | 234 | if (bus->host_pdev) |
| 235 | return bcma_of_get_irq(bus->host_pdev, core, num); |
| 236 | return 0; |
Hauke Mehrtens | 85eb92e | 2014-11-01 16:54:55 +0100 | [diff] [blame] | 237 | case BCMA_HOSTTYPE_SDIO: |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | EXPORT_SYMBOL(bcma_core_irq); |
| 244 | |
Rafał Miłecki | ab54bc8 | 2014-10-03 17:00:24 +0200 | [diff] [blame] | 245 | void bcma_prepare_core(struct bcma_bus *bus, struct bcma_device *core) |
Rafał Miłecki | 6e094bd | 2014-09-05 00:18:48 +0200 | [diff] [blame] | 246 | { |
Rafał Miłecki | 6e094bd | 2014-09-05 00:18:48 +0200 | [diff] [blame] | 247 | core->dev.release = bcma_release_core_dev; |
| 248 | core->dev.bus = &bcma_bus_type; |
| 249 | dev_set_name(&core->dev, "bcma%d:%d", bus->num, core->core_index); |
| 250 | |
| 251 | switch (bus->hosttype) { |
| 252 | case BCMA_HOSTTYPE_PCI: |
| 253 | core->dev.parent = &bus->host_pci->dev; |
| 254 | core->dma_dev = &bus->host_pci->dev; |
| 255 | core->irq = bus->host_pci->irq; |
| 256 | break; |
| 257 | case BCMA_HOSTTYPE_SOC: |
| 258 | core->dev.dma_mask = &core->dev.coherent_dma_mask; |
Hauke Mehrtens | 2101e53 | 2014-09-26 00:09:19 +0200 | [diff] [blame] | 259 | if (bus->host_pdev) { |
| 260 | core->dma_dev = &bus->host_pdev->dev; |
| 261 | core->dev.parent = &bus->host_pdev->dev; |
| 262 | bcma_of_fill_device(bus->host_pdev, core); |
| 263 | } else { |
| 264 | core->dma_dev = &core->dev; |
| 265 | } |
Rafał Miłecki | 6e094bd | 2014-09-05 00:18:48 +0200 | [diff] [blame] | 266 | break; |
| 267 | case BCMA_HOSTTYPE_SDIO: |
| 268 | break; |
| 269 | } |
Rafał Miłecki | ab54bc8 | 2014-10-03 17:00:24 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Rafał Miłecki | 799038e | 2015-01-16 20:59:39 +0100 | [diff] [blame] | 272 | void bcma_init_bus(struct bcma_bus *bus) |
| 273 | { |
| 274 | mutex_lock(&bcma_buses_mutex); |
| 275 | bus->num = bcma_bus_next_num++; |
| 276 | mutex_unlock(&bcma_buses_mutex); |
| 277 | |
| 278 | INIT_LIST_HEAD(&bus->cores); |
| 279 | bus->nr_cores = 0; |
| 280 | |
| 281 | bcma_detect_chip(bus); |
| 282 | } |
| 283 | |
Rafał Miłecki | ab54bc8 | 2014-10-03 17:00:24 +0200 | [diff] [blame] | 284 | static void bcma_register_core(struct bcma_bus *bus, struct bcma_device *core) |
| 285 | { |
| 286 | int err; |
Rafał Miłecki | 6e094bd | 2014-09-05 00:18:48 +0200 | [diff] [blame] | 287 | |
| 288 | err = device_register(&core->dev); |
| 289 | if (err) { |
| 290 | bcma_err(bus, "Could not register dev for core 0x%03X\n", |
| 291 | core->id.id); |
| 292 | put_device(&core->dev); |
| 293 | return; |
| 294 | } |
| 295 | core->dev_registered = true; |
| 296 | } |
| 297 | |
| 298 | static int bcma_register_devices(struct bcma_bus *bus) |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 299 | { |
| 300 | struct bcma_device *core; |
Rafał Miłecki | 6e094bd | 2014-09-05 00:18:48 +0200 | [diff] [blame] | 301 | int err; |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 302 | |
| 303 | list_for_each_entry(core, &bus->cores, list) { |
| 304 | /* We support that cores ourself */ |
| 305 | switch (core->id.id) { |
Rafał Miłecki | 6d5cfc9 | 2012-07-10 23:45:49 +0200 | [diff] [blame] | 306 | case BCMA_CORE_4706_CHIPCOMMON: |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 307 | case BCMA_CORE_CHIPCOMMON: |
Hauke Mehrtens | 1716bcf | 2014-09-08 22:53:36 +0200 | [diff] [blame] | 308 | case BCMA_CORE_NS_CHIPCOMMON_B: |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 309 | case BCMA_CORE_PCI: |
| 310 | case BCMA_CORE_PCIE: |
Rafał Miłecki | f473832 | 2014-07-05 01:10:41 +0200 | [diff] [blame] | 311 | case BCMA_CORE_PCIE2: |
Hauke Mehrtens | 21e0534 | 2011-07-23 01:20:09 +0200 | [diff] [blame] | 312 | case BCMA_CORE_MIPS_74K: |
Rafał Miłecki | e1ac4b4 | 2012-07-11 09:23:43 +0200 | [diff] [blame] | 313 | case BCMA_CORE_4706_MAC_GBIT_COMMON: |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 314 | continue; |
| 315 | } |
| 316 | |
Rafał Miłecki | 37a7f87 | 2014-09-05 00:18:49 +0200 | [diff] [blame] | 317 | /* Early cores were already registered */ |
| 318 | if (bcma_is_core_needed_early(core->id.id)) |
| 319 | continue; |
| 320 | |
Rafał Miłecki | 10419d0 | 2013-02-19 19:41:42 +0100 | [diff] [blame] | 321 | /* Only first GMAC core on BCM4706 is connected and working */ |
| 322 | if (core->id.id == BCMA_CORE_4706_MAC_GBIT && |
| 323 | core->core_unit > 0) |
| 324 | continue; |
| 325 | |
Rafał Miłecki | 6e094bd | 2014-09-05 00:18:48 +0200 | [diff] [blame] | 326 | bcma_register_core(bus, core); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Rafał Miłecki | 73e4dbe4 | 2013-01-25 11:37:26 +0100 | [diff] [blame] | 329 | #ifdef CONFIG_BCMA_DRIVER_MIPS |
| 330 | if (bus->drv_cc.pflash.present) { |
| 331 | err = platform_device_register(&bcma_pflash_dev); |
| 332 | if (err) |
| 333 | bcma_err(bus, "Error registering parallel flash\n"); |
| 334 | } |
| 335 | #endif |
| 336 | |
Rafał Miłecki | d57ef3a | 2012-08-10 21:23:53 +0200 | [diff] [blame] | 337 | #ifdef CONFIG_BCMA_SFLASH |
| 338 | if (bus->drv_cc.sflash.present) { |
| 339 | err = platform_device_register(&bcma_sflash_dev); |
| 340 | if (err) |
| 341 | bcma_err(bus, "Error registering serial flash\n"); |
| 342 | } |
| 343 | #endif |
| 344 | |
Rafał Miłecki | 371a004 | 2012-08-12 13:08:05 +0200 | [diff] [blame] | 345 | #ifdef CONFIG_BCMA_NFLASH |
| 346 | if (bus->drv_cc.nflash.present) { |
| 347 | err = platform_device_register(&bcma_nflash_dev); |
| 348 | if (err) |
| 349 | bcma_err(bus, "Error registering NAND flash\n"); |
| 350 | } |
| 351 | #endif |
Hauke Mehrtens | cf0936b | 2012-11-20 22:24:30 +0000 | [diff] [blame] | 352 | err = bcma_gpio_init(&bus->drv_cc); |
| 353 | if (err == -ENOTSUPP) |
| 354 | bcma_debug(bus, "GPIO driver not activated\n"); |
| 355 | else if (err) |
| 356 | bcma_err(bus, "Error registering GPIO driver: %i\n", err); |
Rafał Miłecki | 371a004 | 2012-08-12 13:08:05 +0200 | [diff] [blame] | 357 | |
Hauke Mehrtens | a4855f39 | 2012-12-05 18:46:02 +0100 | [diff] [blame] | 358 | if (bus->hosttype == BCMA_HOSTTYPE_SOC) { |
| 359 | err = bcma_chipco_watchdog_register(&bus->drv_cc); |
| 360 | if (err) |
| 361 | bcma_err(bus, "Error registering watchdog driver\n"); |
| 362 | } |
| 363 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
Rafał Miłecki | 9b6cc9a | 2015-02-08 17:11:50 +0100 | [diff] [blame] | 367 | void bcma_unregister_cores(struct bcma_bus *bus) |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 368 | { |
Piotr Haber | 1fffa90 | 2012-10-11 14:05:15 +0200 | [diff] [blame] | 369 | struct bcma_device *core, *tmp; |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 370 | |
Piotr Haber | 1fffa90 | 2012-10-11 14:05:15 +0200 | [diff] [blame] | 371 | list_for_each_entry_safe(core, tmp, &bus->cores, list) { |
Rafał Miłecki | 17fbaa6 | 2015-01-24 18:47:19 +0100 | [diff] [blame] | 372 | if (!core->dev_registered) |
| 373 | continue; |
Piotr Haber | 1fffa90 | 2012-10-11 14:05:15 +0200 | [diff] [blame] | 374 | list_del(&core->list); |
Rafał Miłecki | 17fbaa6 | 2015-01-24 18:47:19 +0100 | [diff] [blame] | 375 | device_unregister(&core->dev); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 376 | } |
Hauke Mehrtens | a4855f39 | 2012-12-05 18:46:02 +0100 | [diff] [blame] | 377 | if (bus->hosttype == BCMA_HOSTTYPE_SOC) |
| 378 | platform_device_unregister(bus->drv_cc.watchdog); |
Rafał Miłecki | 17fbaa6 | 2015-01-24 18:47:19 +0100 | [diff] [blame] | 379 | |
| 380 | /* Now noone uses internally-handled cores, we can free them */ |
| 381 | list_for_each_entry_safe(core, tmp, &bus->cores, list) { |
| 382 | list_del(&core->list); |
| 383 | kfree(core); |
| 384 | } |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Greg Kroah-Hartman | 0f58a01 | 2012-12-21 15:12:59 -0800 | [diff] [blame] | 387 | int bcma_bus_register(struct bcma_bus *bus) |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 388 | { |
| 389 | int err; |
| 390 | struct bcma_device *core; |
| 391 | |
| 392 | /* Scan for devices (cores) */ |
| 393 | err = bcma_bus_scan(bus); |
| 394 | if (err) { |
Rafał Miłecki | 3d9d8af | 2012-07-05 22:07:32 +0200 | [diff] [blame] | 395 | bcma_err(bus, "Failed to scan: %d\n", err); |
Hauke Mehrtens | 1cabf7d | 2013-07-15 13:15:07 +0200 | [diff] [blame] | 396 | return err; |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 397 | } |
| 398 | |
Hauke Mehrtens | 30cfb02 | 2012-09-29 20:29:50 +0200 | [diff] [blame] | 399 | /* Early init CC core */ |
| 400 | core = bcma_find_core(bus, bcma_cc_core_id(bus)); |
| 401 | if (core) { |
| 402 | bus->drv_cc.core = core; |
| 403 | bcma_core_chipcommon_early_init(&bus->drv_cc); |
| 404 | } |
| 405 | |
Rafał Miłecki | b504075 | 2015-01-25 11:11:14 +0100 | [diff] [blame] | 406 | /* Early init PCIE core */ |
| 407 | core = bcma_find_core(bus, BCMA_CORE_PCIE); |
| 408 | if (core) { |
| 409 | bus->drv_pci[0].core = core; |
| 410 | bcma_core_pci_early_init(&bus->drv_pci[0]); |
| 411 | } |
| 412 | |
Rafał Miłecki | cae761b | 2015-06-28 17:17:13 +0200 | [diff] [blame^] | 413 | if (bus->host_pdev) { |
| 414 | struct device *dev = &bus->host_pdev->dev; |
| 415 | |
| 416 | of_platform_populate(dev->of_node, of_default_bus_match_table, |
| 417 | NULL, dev); |
| 418 | } |
| 419 | |
Rafał Miłecki | 37a7f87 | 2014-09-05 00:18:49 +0200 | [diff] [blame] | 420 | /* Cores providing flash access go before SPROM init */ |
| 421 | list_for_each_entry(core, &bus->cores, list) { |
| 422 | if (bcma_is_core_needed_early(core->id.id)) |
| 423 | bcma_register_core(bus, core); |
| 424 | } |
| 425 | |
Hauke Mehrtens | 30cfb02 | 2012-09-29 20:29:50 +0200 | [diff] [blame] | 426 | /* Try to get SPROM */ |
| 427 | err = bcma_sprom_get(bus); |
| 428 | if (err == -ENOENT) { |
| 429 | bcma_err(bus, "No SPROM available\n"); |
| 430 | } else if (err) |
| 431 | bcma_err(bus, "Failed to get SPROM: %d\n", err); |
| 432 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 433 | /* Init CC core */ |
Rafał Miłecki | 6d5cfc9 | 2012-07-10 23:45:49 +0200 | [diff] [blame] | 434 | core = bcma_find_core(bus, bcma_cc_core_id(bus)); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 435 | if (core) { |
| 436 | bus->drv_cc.core = core; |
| 437 | bcma_core_chipcommon_init(&bus->drv_cc); |
| 438 | } |
| 439 | |
Hauke Mehrtens | 1716bcf | 2014-09-08 22:53:36 +0200 | [diff] [blame] | 440 | /* Init CC core */ |
| 441 | core = bcma_find_core(bus, BCMA_CORE_NS_CHIPCOMMON_B); |
| 442 | if (core) { |
| 443 | bus->drv_cc_b.core = core; |
| 444 | bcma_core_chipcommon_b_init(&bus->drv_cc_b); |
| 445 | } |
| 446 | |
Hauke Mehrtens | 21e0534 | 2011-07-23 01:20:09 +0200 | [diff] [blame] | 447 | /* Init MIPS core */ |
| 448 | core = bcma_find_core(bus, BCMA_CORE_MIPS_74K); |
| 449 | if (core) { |
| 450 | bus->drv_mips.core = core; |
| 451 | bcma_core_mips_init(&bus->drv_mips); |
| 452 | } |
| 453 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 454 | /* Init PCIE core */ |
Hauke Mehrtens | dfae714 | 2012-09-29 20:40:18 +0200 | [diff] [blame] | 455 | core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 0); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 456 | if (core) { |
Hauke Mehrtens | dfae714 | 2012-09-29 20:40:18 +0200 | [diff] [blame] | 457 | bus->drv_pci[0].core = core; |
| 458 | bcma_core_pci_init(&bus->drv_pci[0]); |
| 459 | } |
| 460 | |
| 461 | /* Init PCIE core */ |
| 462 | core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 1); |
| 463 | if (core) { |
| 464 | bus->drv_pci[1].core = core; |
| 465 | bcma_core_pci_init(&bus->drv_pci[1]); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Rafał Miłecki | f473832 | 2014-07-05 01:10:41 +0200 | [diff] [blame] | 468 | /* Init PCIe Gen 2 core */ |
| 469 | core = bcma_find_core_unit(bus, BCMA_CORE_PCIE2, 0); |
| 470 | if (core) { |
| 471 | bus->drv_pcie2.core = core; |
| 472 | bcma_core_pcie2_init(&bus->drv_pcie2); |
| 473 | } |
| 474 | |
Rafał Miłecki | e1ac4b4 | 2012-07-11 09:23:43 +0200 | [diff] [blame] | 475 | /* Init GBIT MAC COMMON core */ |
| 476 | core = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON); |
| 477 | if (core) { |
| 478 | bus->drv_gmac_cmn.core = core; |
| 479 | bcma_core_gmac_cmn_init(&bus->drv_gmac_cmn); |
| 480 | } |
| 481 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 482 | /* Register found cores */ |
Rafał Miłecki | 6e094bd | 2014-09-05 00:18:48 +0200 | [diff] [blame] | 483 | bcma_register_devices(bus); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 484 | |
Rafał Miłecki | 3d9d8af | 2012-07-05 22:07:32 +0200 | [diff] [blame] | 485 | bcma_info(bus, "Bus registered\n"); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 486 | |
| 487 | return 0; |
| 488 | } |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 489 | |
| 490 | void bcma_bus_unregister(struct bcma_bus *bus) |
| 491 | { |
Hauke Mehrtens | c50ae94 | 2013-02-03 23:25:33 +0100 | [diff] [blame] | 492 | int err; |
| 493 | |
| 494 | err = bcma_gpio_unregister(&bus->drv_cc); |
| 495 | if (err == -EBUSY) |
| 496 | bcma_err(bus, "Some GPIOs are still in use.\n"); |
| 497 | else if (err) |
| 498 | bcma_err(bus, "Can not unregister GPIO driver: %i\n", err); |
Saul St. John | ee91592 | 2012-08-16 15:42:30 -0500 | [diff] [blame] | 499 | |
Hauke Mehrtens | 1716bcf | 2014-09-08 22:53:36 +0200 | [diff] [blame] | 500 | bcma_core_chipcommon_b_free(&bus->drv_cc_b); |
| 501 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 502 | bcma_unregister_cores(bus); |
| 503 | } |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 504 | |
Rafał Miłecki | c5ed1df | 2015-01-19 08:30:30 +0100 | [diff] [blame] | 505 | /* |
| 506 | * This is a special version of bus registration function designed for SoCs. |
| 507 | * It scans bus and performs basic initialization of main cores only. |
| 508 | * Please note it requires memory allocation, however it won't try to sleep. |
| 509 | */ |
| 510 | int __init bcma_bus_early_register(struct bcma_bus *bus) |
Hauke Mehrtens | 517f43e | 2011-07-23 01:20:07 +0200 | [diff] [blame] | 511 | { |
| 512 | int err; |
| 513 | struct bcma_device *core; |
Hauke Mehrtens | 517f43e | 2011-07-23 01:20:07 +0200 | [diff] [blame] | 514 | |
Rafał Miłecki | c5ed1df | 2015-01-19 08:30:30 +0100 | [diff] [blame] | 515 | /* Scan for devices (cores) */ |
| 516 | err = bcma_bus_scan(bus); |
Hauke Mehrtens | 517f43e | 2011-07-23 01:20:07 +0200 | [diff] [blame] | 517 | if (err) { |
Rafał Miłecki | c5ed1df | 2015-01-19 08:30:30 +0100 | [diff] [blame] | 518 | bcma_err(bus, "Failed to scan bus: %d\n", err); |
Hauke Mehrtens | 517f43e | 2011-07-23 01:20:07 +0200 | [diff] [blame] | 519 | return -1; |
| 520 | } |
| 521 | |
Hauke Mehrtens | 49655bb | 2012-09-29 20:29:49 +0200 | [diff] [blame] | 522 | /* Early init CC core */ |
Rafał Miłecki | 6d5cfc9 | 2012-07-10 23:45:49 +0200 | [diff] [blame] | 523 | core = bcma_find_core(bus, bcma_cc_core_id(bus)); |
Hauke Mehrtens | 517f43e | 2011-07-23 01:20:07 +0200 | [diff] [blame] | 524 | if (core) { |
| 525 | bus->drv_cc.core = core; |
Hauke Mehrtens | 49655bb | 2012-09-29 20:29:49 +0200 | [diff] [blame] | 526 | bcma_core_chipcommon_early_init(&bus->drv_cc); |
Hauke Mehrtens | 517f43e | 2011-07-23 01:20:07 +0200 | [diff] [blame] | 527 | } |
| 528 | |
Hauke Mehrtens | 49655bb | 2012-09-29 20:29:49 +0200 | [diff] [blame] | 529 | /* Early init MIPS core */ |
Hauke Mehrtens | 21e0534 | 2011-07-23 01:20:09 +0200 | [diff] [blame] | 530 | core = bcma_find_core(bus, BCMA_CORE_MIPS_74K); |
| 531 | if (core) { |
| 532 | bus->drv_mips.core = core; |
Hauke Mehrtens | 49655bb | 2012-09-29 20:29:49 +0200 | [diff] [blame] | 533 | bcma_core_mips_early_init(&bus->drv_mips); |
Hauke Mehrtens | 21e0534 | 2011-07-23 01:20:09 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Rafał Miłecki | 3d9d8af | 2012-07-05 22:07:32 +0200 | [diff] [blame] | 536 | bcma_info(bus, "Early bus registered\n"); |
Hauke Mehrtens | 517f43e | 2011-07-23 01:20:07 +0200 | [diff] [blame] | 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
Rafał Miłecki | 775ab52 | 2011-12-09 22:16:07 +0100 | [diff] [blame] | 541 | #ifdef CONFIG_PM |
Linus Torvalds | 685a4ef | 2012-01-13 23:58:40 +0100 | [diff] [blame] | 542 | int bcma_bus_suspend(struct bcma_bus *bus) |
| 543 | { |
Linus Torvalds | 7d5869e | 2012-01-13 23:58:41 +0100 | [diff] [blame] | 544 | struct bcma_device *core; |
| 545 | |
| 546 | list_for_each_entry(core, &bus->cores, list) { |
| 547 | struct device_driver *drv = core->dev.driver; |
| 548 | if (drv) { |
| 549 | struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv); |
| 550 | if (adrv->suspend) |
| 551 | adrv->suspend(core); |
| 552 | } |
| 553 | } |
Linus Torvalds | 685a4ef | 2012-01-13 23:58:40 +0100 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
Rafał Miłecki | 775ab52 | 2011-12-09 22:16:07 +0100 | [diff] [blame] | 557 | int bcma_bus_resume(struct bcma_bus *bus) |
| 558 | { |
| 559 | struct bcma_device *core; |
| 560 | |
| 561 | /* Init CC core */ |
Rafał Miłecki | 6d5cfc9 | 2012-07-10 23:45:49 +0200 | [diff] [blame] | 562 | if (bus->drv_cc.core) { |
Rafał Miłecki | 775ab52 | 2011-12-09 22:16:07 +0100 | [diff] [blame] | 563 | bus->drv_cc.setup_done = false; |
| 564 | bcma_core_chipcommon_init(&bus->drv_cc); |
| 565 | } |
| 566 | |
Linus Torvalds | 7d5869e | 2012-01-13 23:58:41 +0100 | [diff] [blame] | 567 | list_for_each_entry(core, &bus->cores, list) { |
| 568 | struct device_driver *drv = core->dev.driver; |
| 569 | if (drv) { |
| 570 | struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv); |
| 571 | if (adrv->resume) |
| 572 | adrv->resume(core); |
| 573 | } |
| 574 | } |
| 575 | |
Rafał Miłecki | 775ab52 | 2011-12-09 22:16:07 +0100 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | #endif |
| 579 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 580 | int __bcma_driver_register(struct bcma_driver *drv, struct module *owner) |
| 581 | { |
| 582 | drv->drv.name = drv->name; |
| 583 | drv->drv.bus = &bcma_bus_type; |
| 584 | drv->drv.owner = owner; |
| 585 | |
| 586 | return driver_register(&drv->drv); |
| 587 | } |
| 588 | EXPORT_SYMBOL_GPL(__bcma_driver_register); |
| 589 | |
| 590 | void bcma_driver_unregister(struct bcma_driver *drv) |
| 591 | { |
| 592 | driver_unregister(&drv->drv); |
| 593 | } |
| 594 | EXPORT_SYMBOL_GPL(bcma_driver_unregister); |
| 595 | |
| 596 | static int bcma_bus_match(struct device *dev, struct device_driver *drv) |
| 597 | { |
| 598 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 599 | struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv); |
| 600 | const struct bcma_device_id *cid = &core->id; |
| 601 | const struct bcma_device_id *did; |
| 602 | |
| 603 | for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) { |
| 604 | if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) && |
| 605 | (did->id == cid->id || did->id == BCMA_ANY_ID) && |
| 606 | (did->rev == cid->rev || did->rev == BCMA_ANY_REV) && |
| 607 | (did->class == cid->class || did->class == BCMA_ANY_CLASS)) |
| 608 | return 1; |
| 609 | } |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static int bcma_device_probe(struct device *dev) |
| 614 | { |
| 615 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 616 | struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver, |
| 617 | drv); |
| 618 | int err = 0; |
| 619 | |
| 620 | if (adrv->probe) |
| 621 | err = adrv->probe(core); |
| 622 | |
| 623 | return err; |
| 624 | } |
| 625 | |
| 626 | static int bcma_device_remove(struct device *dev) |
| 627 | { |
| 628 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 629 | struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver, |
| 630 | drv); |
| 631 | |
| 632 | if (adrv->remove) |
| 633 | adrv->remove(core); |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
David Woodhouse | 886b66e | 2011-08-19 22:14:47 +0200 | [diff] [blame] | 638 | static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 639 | { |
| 640 | struct bcma_device *core = container_of(dev, struct bcma_device, dev); |
| 641 | |
| 642 | return add_uevent_var(env, |
| 643 | "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X", |
| 644 | core->id.manuf, core->id.id, |
| 645 | core->id.rev, core->id.class); |
| 646 | } |
| 647 | |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 648 | static int __init bcma_modinit(void) |
| 649 | { |
| 650 | int err; |
| 651 | |
| 652 | err = bus_register(&bcma_bus_type); |
| 653 | if (err) |
| 654 | return err; |
| 655 | |
Hauke Mehrtens | 2101e53 | 2014-09-26 00:09:19 +0200 | [diff] [blame] | 656 | err = bcma_host_soc_register_driver(); |
| 657 | if (err) { |
| 658 | pr_err("SoC host initialization failed\n"); |
| 659 | err = 0; |
| 660 | } |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 661 | #ifdef CONFIG_BCMA_HOST_PCI |
| 662 | err = bcma_host_pci_init(); |
| 663 | if (err) { |
| 664 | pr_err("PCI host initialization failed\n"); |
| 665 | err = 0; |
| 666 | } |
| 667 | #endif |
| 668 | |
| 669 | return err; |
| 670 | } |
| 671 | fs_initcall(bcma_modinit); |
| 672 | |
| 673 | static void __exit bcma_modexit(void) |
| 674 | { |
| 675 | #ifdef CONFIG_BCMA_HOST_PCI |
| 676 | bcma_host_pci_exit(); |
| 677 | #endif |
Hauke Mehrtens | 2101e53 | 2014-09-26 00:09:19 +0200 | [diff] [blame] | 678 | bcma_host_soc_unregister_driver(); |
Rafał Miłecki | 8369ae3 | 2011-05-09 18:56:46 +0200 | [diff] [blame] | 679 | bus_unregister(&bcma_bus_type); |
| 680 | } |
| 681 | module_exit(bcma_modexit) |