Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Sonics Silicon Backplane |
| 3 | * Subsystem core |
| 4 | * |
| 5 | * Copyright 2005, Broadcom Corporation |
Michael Büsch | eb032b9 | 2011-07-04 20:50:05 +0200 | [diff] [blame] | 6 | * Copyright 2006, 2007, Michael Buesch <m@bues.ch> |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 7 | * |
| 8 | * Licensed under the GNU/GPL. See COPYING for details. |
| 9 | */ |
| 10 | |
| 11 | #include "ssb_private.h" |
| 12 | |
| 13 | #include <linux/delay.h> |
Geert Uytterhoeven | 6faf035 | 2007-10-13 14:31:31 +0200 | [diff] [blame] | 14 | #include <linux/io.h> |
Paul Gortmaker | 20a112d | 2011-07-17 16:03:40 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 16 | #include <linux/ssb/ssb.h> |
| 17 | #include <linux/ssb/ssb_regs.h> |
Michael Buesch | aab547c | 2008-02-29 11:36:12 +0100 | [diff] [blame] | 18 | #include <linux/ssb/ssb_driver_gige.h> |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 19 | #include <linux/dma-mapping.h> |
| 20 | #include <linux/pci.h> |
Albert Herranz | 24ea602 | 2009-09-08 19:30:12 +0200 | [diff] [blame] | 21 | #include <linux/mmc/sdio_func.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 23 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 24 | #include <pcmcia/cistpl.h> |
| 25 | #include <pcmcia/ds.h> |
| 26 | |
| 27 | |
| 28 | MODULE_DESCRIPTION("Sonics Silicon Backplane driver"); |
| 29 | MODULE_LICENSE("GPL"); |
| 30 | |
| 31 | |
| 32 | /* Temporary list of yet-to-be-attached buses */ |
| 33 | static LIST_HEAD(attach_queue); |
| 34 | /* List if running buses */ |
| 35 | static LIST_HEAD(buses); |
| 36 | /* Software ID counter */ |
| 37 | static unsigned int next_busnumber; |
| 38 | /* buses_mutes locks the two buslists and the next_busnumber. |
| 39 | * Don't lock this directly, but use ssb_buses_[un]lock() below. */ |
| 40 | static DEFINE_MUTEX(buses_mutex); |
| 41 | |
| 42 | /* There are differences in the codeflow, if the bus is |
| 43 | * initialized from early boot, as various needed services |
| 44 | * are not available early. This is a mechanism to delay |
| 45 | * these initializations to after early boot has finished. |
| 46 | * It's also used to avoid mutex locking, as that's not |
| 47 | * available and needed early. */ |
| 48 | static bool ssb_is_early_boot = 1; |
| 49 | |
| 50 | static void ssb_buses_lock(void); |
| 51 | static void ssb_buses_unlock(void); |
| 52 | |
| 53 | |
| 54 | #ifdef CONFIG_SSB_PCIHOST |
| 55 | struct ssb_bus *ssb_pci_dev_to_bus(struct pci_dev *pdev) |
| 56 | { |
| 57 | struct ssb_bus *bus; |
| 58 | |
| 59 | ssb_buses_lock(); |
| 60 | list_for_each_entry(bus, &buses, list) { |
| 61 | if (bus->bustype == SSB_BUSTYPE_PCI && |
| 62 | bus->host_pci == pdev) |
| 63 | goto found; |
| 64 | } |
| 65 | bus = NULL; |
| 66 | found: |
| 67 | ssb_buses_unlock(); |
| 68 | |
| 69 | return bus; |
| 70 | } |
| 71 | #endif /* CONFIG_SSB_PCIHOST */ |
| 72 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 73 | #ifdef CONFIG_SSB_PCMCIAHOST |
| 74 | struct ssb_bus *ssb_pcmcia_dev_to_bus(struct pcmcia_device *pdev) |
| 75 | { |
| 76 | struct ssb_bus *bus; |
| 77 | |
| 78 | ssb_buses_lock(); |
| 79 | list_for_each_entry(bus, &buses, list) { |
| 80 | if (bus->bustype == SSB_BUSTYPE_PCMCIA && |
| 81 | bus->host_pcmcia == pdev) |
| 82 | goto found; |
| 83 | } |
| 84 | bus = NULL; |
| 85 | found: |
| 86 | ssb_buses_unlock(); |
| 87 | |
| 88 | return bus; |
| 89 | } |
| 90 | #endif /* CONFIG_SSB_PCMCIAHOST */ |
| 91 | |
Albert Herranz | 24ea602 | 2009-09-08 19:30:12 +0200 | [diff] [blame] | 92 | #ifdef CONFIG_SSB_SDIOHOST |
| 93 | struct ssb_bus *ssb_sdio_func_to_bus(struct sdio_func *func) |
| 94 | { |
| 95 | struct ssb_bus *bus; |
| 96 | |
| 97 | ssb_buses_lock(); |
| 98 | list_for_each_entry(bus, &buses, list) { |
| 99 | if (bus->bustype == SSB_BUSTYPE_SDIO && |
| 100 | bus->host_sdio == func) |
| 101 | goto found; |
| 102 | } |
| 103 | bus = NULL; |
| 104 | found: |
| 105 | ssb_buses_unlock(); |
| 106 | |
| 107 | return bus; |
| 108 | } |
| 109 | #endif /* CONFIG_SSB_SDIOHOST */ |
| 110 | |
Michael Buesch | aab547c | 2008-02-29 11:36:12 +0100 | [diff] [blame] | 111 | int ssb_for_each_bus_call(unsigned long data, |
| 112 | int (*func)(struct ssb_bus *bus, unsigned long data)) |
| 113 | { |
| 114 | struct ssb_bus *bus; |
| 115 | int res; |
| 116 | |
| 117 | ssb_buses_lock(); |
| 118 | list_for_each_entry(bus, &buses, list) { |
| 119 | res = func(bus, data); |
| 120 | if (res >= 0) { |
| 121 | ssb_buses_unlock(); |
| 122 | return res; |
| 123 | } |
| 124 | } |
| 125 | ssb_buses_unlock(); |
| 126 | |
| 127 | return -ENODEV; |
| 128 | } |
| 129 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 130 | static struct ssb_device *ssb_device_get(struct ssb_device *dev) |
| 131 | { |
| 132 | if (dev) |
| 133 | get_device(dev->dev); |
| 134 | return dev; |
| 135 | } |
| 136 | |
| 137 | static void ssb_device_put(struct ssb_device *dev) |
| 138 | { |
| 139 | if (dev) |
| 140 | put_device(dev->dev); |
| 141 | } |
| 142 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 143 | static int ssb_device_resume(struct device *dev) |
| 144 | { |
| 145 | struct ssb_device *ssb_dev = dev_to_ssb_dev(dev); |
| 146 | struct ssb_driver *ssb_drv; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 147 | int err = 0; |
| 148 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 149 | if (dev->driver) { |
| 150 | ssb_drv = drv_to_ssb_drv(dev->driver); |
| 151 | if (ssb_drv && ssb_drv->resume) |
| 152 | err = ssb_drv->resume(ssb_dev); |
| 153 | if (err) |
| 154 | goto out; |
| 155 | } |
| 156 | out: |
| 157 | return err; |
| 158 | } |
| 159 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 160 | static int ssb_device_suspend(struct device *dev, pm_message_t state) |
| 161 | { |
| 162 | struct ssb_device *ssb_dev = dev_to_ssb_dev(dev); |
| 163 | struct ssb_driver *ssb_drv; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 164 | int err = 0; |
| 165 | |
| 166 | if (dev->driver) { |
| 167 | ssb_drv = drv_to_ssb_drv(dev->driver); |
| 168 | if (ssb_drv && ssb_drv->suspend) |
| 169 | err = ssb_drv->suspend(ssb_dev, state); |
| 170 | if (err) |
| 171 | goto out; |
| 172 | } |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 173 | out: |
| 174 | return err; |
| 175 | } |
| 176 | |
Michael Buesch | 8fe2b65 | 2008-03-30 00:10:50 +0100 | [diff] [blame] | 177 | int ssb_bus_resume(struct ssb_bus *bus) |
| 178 | { |
| 179 | int err; |
| 180 | |
| 181 | /* Reset HW state information in memory, so that HW is |
| 182 | * completely reinitialized. */ |
| 183 | bus->mapped_device = NULL; |
| 184 | #ifdef CONFIG_SSB_DRIVER_PCICORE |
| 185 | bus->pcicore.setup_done = 0; |
| 186 | #endif |
| 187 | |
| 188 | err = ssb_bus_powerup(bus, 0); |
| 189 | if (err) |
| 190 | return err; |
| 191 | err = ssb_pcmcia_hardware_setup(bus); |
| 192 | if (err) { |
| 193 | ssb_bus_may_powerdown(bus); |
| 194 | return err; |
| 195 | } |
| 196 | ssb_chipco_resume(&bus->chipco); |
| 197 | ssb_bus_may_powerdown(bus); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | EXPORT_SYMBOL(ssb_bus_resume); |
| 202 | |
| 203 | int ssb_bus_suspend(struct ssb_bus *bus) |
| 204 | { |
| 205 | ssb_chipco_suspend(&bus->chipco); |
| 206 | ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0); |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | EXPORT_SYMBOL(ssb_bus_suspend); |
| 211 | |
Michael Buesch | d72bb40 | 2008-04-02 17:03:26 +0200 | [diff] [blame] | 212 | #ifdef CONFIG_SSB_SPROM |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 213 | /** ssb_devices_freeze - Freeze all devices on the bus. |
| 214 | * |
| 215 | * After freezing no device driver will be handling a device |
| 216 | * on this bus anymore. ssb_devices_thaw() must be called after |
| 217 | * a successful freeze to reactivate the devices. |
| 218 | * |
| 219 | * @bus: The bus. |
| 220 | * @ctx: Context structure. Pass this to ssb_devices_thaw(). |
| 221 | */ |
| 222 | int ssb_devices_freeze(struct ssb_bus *bus, struct ssb_freeze_context *ctx) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 223 | { |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 224 | struct ssb_device *sdev; |
| 225 | struct ssb_driver *sdrv; |
| 226 | unsigned int i; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 227 | |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 228 | memset(ctx, 0, sizeof(*ctx)); |
| 229 | ctx->bus = bus; |
| 230 | SSB_WARN_ON(bus->nr_devices > ARRAY_SIZE(ctx->device_frozen)); |
| 231 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 232 | for (i = 0; i < bus->nr_devices; i++) { |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 233 | sdev = ssb_device_get(&bus->devices[i]); |
| 234 | |
| 235 | if (!sdev->dev || !sdev->dev->driver || |
| 236 | !device_is_registered(sdev->dev)) { |
| 237 | ssb_device_put(sdev); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 238 | continue; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 239 | } |
Alan Stern | f3ff924 | 2012-01-24 13:35:24 -0500 | [diff] [blame] | 240 | sdrv = drv_to_ssb_drv(sdev->dev->driver); |
| 241 | if (SSB_WARN_ON(!sdrv->remove)) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 242 | continue; |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 243 | sdrv->remove(sdev); |
| 244 | ctx->device_frozen[i] = 1; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | return 0; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 250 | /** ssb_devices_thaw - Unfreeze all devices on the bus. |
| 251 | * |
| 252 | * This will re-attach the device drivers and re-init the devices. |
| 253 | * |
| 254 | * @ctx: The context structure from ssb_devices_freeze() |
| 255 | */ |
| 256 | int ssb_devices_thaw(struct ssb_freeze_context *ctx) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 257 | { |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 258 | struct ssb_bus *bus = ctx->bus; |
| 259 | struct ssb_device *sdev; |
| 260 | struct ssb_driver *sdrv; |
| 261 | unsigned int i; |
| 262 | int err, result = 0; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 263 | |
| 264 | for (i = 0; i < bus->nr_devices; i++) { |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 265 | if (!ctx->device_frozen[i]) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 266 | continue; |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 267 | sdev = &bus->devices[i]; |
| 268 | |
| 269 | if (SSB_WARN_ON(!sdev->dev || !sdev->dev->driver)) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 270 | continue; |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 271 | sdrv = drv_to_ssb_drv(sdev->dev->driver); |
| 272 | if (SSB_WARN_ON(!sdrv || !sdrv->probe)) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 273 | continue; |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 274 | |
| 275 | err = sdrv->probe(sdev, &sdev->id); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 276 | if (err) { |
| 277 | ssb_printk(KERN_ERR PFX "Failed to thaw device %s\n", |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 278 | dev_name(sdev->dev)); |
| 279 | result = err; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 280 | } |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 281 | ssb_device_put(sdev); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 284 | return result; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 285 | } |
Michael Buesch | d72bb40 | 2008-04-02 17:03:26 +0200 | [diff] [blame] | 286 | #endif /* CONFIG_SSB_SPROM */ |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 287 | |
| 288 | static void ssb_device_shutdown(struct device *dev) |
| 289 | { |
| 290 | struct ssb_device *ssb_dev = dev_to_ssb_dev(dev); |
| 291 | struct ssb_driver *ssb_drv; |
| 292 | |
| 293 | if (!dev->driver) |
| 294 | return; |
| 295 | ssb_drv = drv_to_ssb_drv(dev->driver); |
| 296 | if (ssb_drv && ssb_drv->shutdown) |
| 297 | ssb_drv->shutdown(ssb_dev); |
| 298 | } |
| 299 | |
| 300 | static int ssb_device_remove(struct device *dev) |
| 301 | { |
| 302 | struct ssb_device *ssb_dev = dev_to_ssb_dev(dev); |
| 303 | struct ssb_driver *ssb_drv = drv_to_ssb_drv(dev->driver); |
| 304 | |
| 305 | if (ssb_drv && ssb_drv->remove) |
| 306 | ssb_drv->remove(ssb_dev); |
| 307 | ssb_device_put(ssb_dev); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int ssb_device_probe(struct device *dev) |
| 313 | { |
| 314 | struct ssb_device *ssb_dev = dev_to_ssb_dev(dev); |
| 315 | struct ssb_driver *ssb_drv = drv_to_ssb_drv(dev->driver); |
| 316 | int err = 0; |
| 317 | |
| 318 | ssb_device_get(ssb_dev); |
| 319 | if (ssb_drv && ssb_drv->probe) |
| 320 | err = ssb_drv->probe(ssb_dev, &ssb_dev->id); |
| 321 | if (err) |
| 322 | ssb_device_put(ssb_dev); |
| 323 | |
| 324 | return err; |
| 325 | } |
| 326 | |
| 327 | static int ssb_match_devid(const struct ssb_device_id *tabid, |
| 328 | const struct ssb_device_id *devid) |
| 329 | { |
| 330 | if ((tabid->vendor != devid->vendor) && |
| 331 | tabid->vendor != SSB_ANY_VENDOR) |
| 332 | return 0; |
| 333 | if ((tabid->coreid != devid->coreid) && |
| 334 | tabid->coreid != SSB_ANY_ID) |
| 335 | return 0; |
| 336 | if ((tabid->revision != devid->revision) && |
| 337 | tabid->revision != SSB_ANY_REV) |
| 338 | return 0; |
| 339 | return 1; |
| 340 | } |
| 341 | |
| 342 | static int ssb_bus_match(struct device *dev, struct device_driver *drv) |
| 343 | { |
| 344 | struct ssb_device *ssb_dev = dev_to_ssb_dev(dev); |
| 345 | struct ssb_driver *ssb_drv = drv_to_ssb_drv(drv); |
| 346 | const struct ssb_device_id *id; |
| 347 | |
| 348 | for (id = ssb_drv->id_table; |
| 349 | id->vendor || id->coreid || id->revision; |
| 350 | id++) { |
| 351 | if (ssb_match_devid(id, &ssb_dev->id)) |
| 352 | return 1; /* found */ |
| 353 | } |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
Al Viro | 7ac0326 | 2007-10-14 05:46:09 +0100 | [diff] [blame] | 358 | static int ssb_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 359 | { |
| 360 | struct ssb_device *ssb_dev = dev_to_ssb_dev(dev); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 361 | |
| 362 | if (!dev) |
| 363 | return -ENODEV; |
| 364 | |
Al Viro | 7ac0326 | 2007-10-14 05:46:09 +0100 | [diff] [blame] | 365 | return add_uevent_var(env, |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 366 | "MODALIAS=ssb:v%04Xid%04Xrev%02X", |
| 367 | ssb_dev->id.vendor, ssb_dev->id.coreid, |
| 368 | ssb_dev->id.revision); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 369 | } |
| 370 | |
Hauke Mehrtens | aa3bf28 | 2010-11-28 12:22:52 +0100 | [diff] [blame] | 371 | #define ssb_config_attr(attrib, field, format_string) \ |
| 372 | static ssize_t \ |
| 373 | attrib##_show(struct device *dev, struct device_attribute *attr, char *buf) \ |
| 374 | { \ |
| 375 | return sprintf(buf, format_string, dev_to_ssb_dev(dev)->field); \ |
| 376 | } |
| 377 | |
| 378 | ssb_config_attr(core_num, core_index, "%u\n") |
| 379 | ssb_config_attr(coreid, id.coreid, "0x%04x\n") |
| 380 | ssb_config_attr(vendor, id.vendor, "0x%04x\n") |
| 381 | ssb_config_attr(revision, id.revision, "%u\n") |
| 382 | ssb_config_attr(irq, irq, "%u\n") |
| 383 | static ssize_t |
| 384 | name_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 385 | { |
| 386 | return sprintf(buf, "%s\n", |
| 387 | ssb_core_name(dev_to_ssb_dev(dev)->id.coreid)); |
| 388 | } |
| 389 | |
| 390 | static struct device_attribute ssb_device_attrs[] = { |
| 391 | __ATTR_RO(name), |
| 392 | __ATTR_RO(core_num), |
| 393 | __ATTR_RO(coreid), |
| 394 | __ATTR_RO(vendor), |
| 395 | __ATTR_RO(revision), |
| 396 | __ATTR_RO(irq), |
| 397 | __ATTR_NULL, |
| 398 | }; |
| 399 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 400 | static struct bus_type ssb_bustype = { |
| 401 | .name = "ssb", |
| 402 | .match = ssb_bus_match, |
| 403 | .probe = ssb_device_probe, |
| 404 | .remove = ssb_device_remove, |
| 405 | .shutdown = ssb_device_shutdown, |
| 406 | .suspend = ssb_device_suspend, |
| 407 | .resume = ssb_device_resume, |
| 408 | .uevent = ssb_device_uevent, |
Hauke Mehrtens | aa3bf28 | 2010-11-28 12:22:52 +0100 | [diff] [blame] | 409 | .dev_attrs = ssb_device_attrs, |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 410 | }; |
| 411 | |
| 412 | static void ssb_buses_lock(void) |
| 413 | { |
| 414 | /* See the comment at the ssb_is_early_boot definition */ |
| 415 | if (!ssb_is_early_boot) |
| 416 | mutex_lock(&buses_mutex); |
| 417 | } |
| 418 | |
| 419 | static void ssb_buses_unlock(void) |
| 420 | { |
| 421 | /* See the comment at the ssb_is_early_boot definition */ |
| 422 | if (!ssb_is_early_boot) |
| 423 | mutex_unlock(&buses_mutex); |
| 424 | } |
| 425 | |
| 426 | static void ssb_devices_unregister(struct ssb_bus *bus) |
| 427 | { |
| 428 | struct ssb_device *sdev; |
| 429 | int i; |
| 430 | |
| 431 | for (i = bus->nr_devices - 1; i >= 0; i--) { |
| 432 | sdev = &(bus->devices[i]); |
| 433 | if (sdev->dev) |
| 434 | device_unregister(sdev->dev); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | void ssb_bus_unregister(struct ssb_bus *bus) |
| 439 | { |
| 440 | ssb_buses_lock(); |
| 441 | ssb_devices_unregister(bus); |
| 442 | list_del(&bus->list); |
| 443 | ssb_buses_unlock(); |
| 444 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 445 | ssb_pcmcia_exit(bus); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 446 | ssb_pci_exit(bus); |
| 447 | ssb_iounmap(bus); |
| 448 | } |
| 449 | EXPORT_SYMBOL(ssb_bus_unregister); |
| 450 | |
| 451 | static void ssb_release_dev(struct device *dev) |
| 452 | { |
| 453 | struct __ssb_dev_wrapper *devwrap; |
| 454 | |
| 455 | devwrap = container_of(dev, struct __ssb_dev_wrapper, dev); |
| 456 | kfree(devwrap); |
| 457 | } |
| 458 | |
| 459 | static int ssb_devices_register(struct ssb_bus *bus) |
| 460 | { |
| 461 | struct ssb_device *sdev; |
| 462 | struct device *dev; |
| 463 | struct __ssb_dev_wrapper *devwrap; |
| 464 | int i, err = 0; |
| 465 | int dev_idx = 0; |
| 466 | |
| 467 | for (i = 0; i < bus->nr_devices; i++) { |
| 468 | sdev = &(bus->devices[i]); |
| 469 | |
| 470 | /* We don't register SSB-system devices to the kernel, |
| 471 | * as the drivers for them are built into SSB. */ |
| 472 | switch (sdev->id.coreid) { |
| 473 | case SSB_DEV_CHIPCOMMON: |
| 474 | case SSB_DEV_PCI: |
| 475 | case SSB_DEV_PCIE: |
| 476 | case SSB_DEV_PCMCIA: |
| 477 | case SSB_DEV_MIPS: |
| 478 | case SSB_DEV_MIPS_3302: |
| 479 | case SSB_DEV_EXTIF: |
| 480 | continue; |
| 481 | } |
| 482 | |
| 483 | devwrap = kzalloc(sizeof(*devwrap), GFP_KERNEL); |
| 484 | if (!devwrap) { |
| 485 | ssb_printk(KERN_ERR PFX |
| 486 | "Could not allocate device\n"); |
| 487 | err = -ENOMEM; |
| 488 | goto error; |
| 489 | } |
| 490 | dev = &devwrap->dev; |
| 491 | devwrap->sdev = sdev; |
| 492 | |
| 493 | dev->release = ssb_release_dev; |
| 494 | dev->bus = &ssb_bustype; |
Kay Sievers | b7b05fe | 2008-10-30 15:51:57 +0100 | [diff] [blame] | 495 | dev_set_name(dev, "ssb%u:%d", bus->busnumber, dev_idx); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 496 | |
| 497 | switch (bus->bustype) { |
| 498 | case SSB_BUSTYPE_PCI: |
| 499 | #ifdef CONFIG_SSB_PCIHOST |
| 500 | sdev->irq = bus->host_pci->irq; |
| 501 | dev->parent = &bus->host_pci->dev; |
FUJITA Tomonori | 14f9295 | 2010-06-03 19:37:27 -0700 | [diff] [blame] | 502 | sdev->dma_dev = dev->parent; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 503 | #endif |
| 504 | break; |
| 505 | case SSB_BUSTYPE_PCMCIA: |
| 506 | #ifdef CONFIG_SSB_PCMCIAHOST |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 507 | sdev->irq = bus->host_pcmcia->irq; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 508 | dev->parent = &bus->host_pcmcia->dev; |
| 509 | #endif |
| 510 | break; |
Albert Herranz | 24ea602 | 2009-09-08 19:30:12 +0200 | [diff] [blame] | 511 | case SSB_BUSTYPE_SDIO: |
Michael Buesch | 391ae22 | 2010-02-03 18:24:35 +0100 | [diff] [blame] | 512 | #ifdef CONFIG_SSB_SDIOHOST |
Albert Herranz | 24ea602 | 2009-09-08 19:30:12 +0200 | [diff] [blame] | 513 | dev->parent = &bus->host_sdio->dev; |
| 514 | #endif |
| 515 | break; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 516 | case SSB_BUSTYPE_SSB: |
Aurelien Jarno | ac82da3 | 2008-09-26 22:27:11 +0200 | [diff] [blame] | 517 | dev->dma_mask = &dev->coherent_dma_mask; |
FUJITA Tomonori | 14f9295 | 2010-06-03 19:37:27 -0700 | [diff] [blame] | 518 | sdev->dma_dev = dev; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 519 | break; |
| 520 | } |
| 521 | |
| 522 | sdev->dev = dev; |
| 523 | err = device_register(dev); |
| 524 | if (err) { |
| 525 | ssb_printk(KERN_ERR PFX |
| 526 | "Could not register %s\n", |
Kay Sievers | b7b05fe | 2008-10-30 15:51:57 +0100 | [diff] [blame] | 527 | dev_name(dev)); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 528 | /* Set dev to NULL to not unregister |
| 529 | * dev on error unwinding. */ |
| 530 | sdev->dev = NULL; |
| 531 | kfree(devwrap); |
| 532 | goto error; |
| 533 | } |
| 534 | dev_idx++; |
| 535 | } |
| 536 | |
| 537 | return 0; |
| 538 | error: |
| 539 | /* Unwind the already registered devices. */ |
| 540 | ssb_devices_unregister(bus); |
| 541 | return err; |
| 542 | } |
| 543 | |
| 544 | /* Needs ssb_buses_lock() */ |
Hauke Mehrtens | cd15598 | 2011-06-21 20:57:16 +0200 | [diff] [blame] | 545 | static int __devinit ssb_attach_queued_buses(void) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 546 | { |
| 547 | struct ssb_bus *bus, *n; |
| 548 | int err = 0; |
| 549 | int drop_them_all = 0; |
| 550 | |
| 551 | list_for_each_entry_safe(bus, n, &attach_queue, list) { |
| 552 | if (drop_them_all) { |
| 553 | list_del(&bus->list); |
| 554 | continue; |
| 555 | } |
| 556 | /* Can't init the PCIcore in ssb_bus_register(), as that |
| 557 | * is too early in boot for embedded systems |
| 558 | * (no udelay() available). So do it here in attach stage. |
| 559 | */ |
| 560 | err = ssb_bus_powerup(bus, 0); |
| 561 | if (err) |
| 562 | goto error; |
| 563 | ssb_pcicore_init(&bus->pcicore); |
| 564 | ssb_bus_may_powerdown(bus); |
| 565 | |
| 566 | err = ssb_devices_register(bus); |
| 567 | error: |
| 568 | if (err) { |
| 569 | drop_them_all = 1; |
| 570 | list_del(&bus->list); |
| 571 | continue; |
| 572 | } |
| 573 | list_move_tail(&bus->list, &buses); |
| 574 | } |
| 575 | |
| 576 | return err; |
| 577 | } |
| 578 | |
Michael Buesch | ffc7689 | 2008-02-20 19:08:10 +0100 | [diff] [blame] | 579 | static u8 ssb_ssb_read8(struct ssb_device *dev, u16 offset) |
| 580 | { |
| 581 | struct ssb_bus *bus = dev->bus; |
| 582 | |
| 583 | offset += dev->core_index * SSB_CORE_SIZE; |
| 584 | return readb(bus->mmio + offset); |
| 585 | } |
| 586 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 587 | static u16 ssb_ssb_read16(struct ssb_device *dev, u16 offset) |
| 588 | { |
| 589 | struct ssb_bus *bus = dev->bus; |
| 590 | |
| 591 | offset += dev->core_index * SSB_CORE_SIZE; |
| 592 | return readw(bus->mmio + offset); |
| 593 | } |
| 594 | |
| 595 | static u32 ssb_ssb_read32(struct ssb_device *dev, u16 offset) |
| 596 | { |
| 597 | struct ssb_bus *bus = dev->bus; |
| 598 | |
| 599 | offset += dev->core_index * SSB_CORE_SIZE; |
| 600 | return readl(bus->mmio + offset); |
| 601 | } |
| 602 | |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 603 | #ifdef CONFIG_SSB_BLOCKIO |
| 604 | static void ssb_ssb_block_read(struct ssb_device *dev, void *buffer, |
| 605 | size_t count, u16 offset, u8 reg_width) |
| 606 | { |
| 607 | struct ssb_bus *bus = dev->bus; |
| 608 | void __iomem *addr; |
| 609 | |
| 610 | offset += dev->core_index * SSB_CORE_SIZE; |
| 611 | addr = bus->mmio + offset; |
| 612 | |
| 613 | switch (reg_width) { |
| 614 | case sizeof(u8): { |
| 615 | u8 *buf = buffer; |
| 616 | |
| 617 | while (count) { |
| 618 | *buf = __raw_readb(addr); |
| 619 | buf++; |
| 620 | count--; |
| 621 | } |
| 622 | break; |
| 623 | } |
| 624 | case sizeof(u16): { |
| 625 | __le16 *buf = buffer; |
| 626 | |
| 627 | SSB_WARN_ON(count & 1); |
| 628 | while (count) { |
| 629 | *buf = (__force __le16)__raw_readw(addr); |
| 630 | buf++; |
| 631 | count -= 2; |
| 632 | } |
| 633 | break; |
| 634 | } |
| 635 | case sizeof(u32): { |
| 636 | __le32 *buf = buffer; |
| 637 | |
| 638 | SSB_WARN_ON(count & 3); |
| 639 | while (count) { |
| 640 | *buf = (__force __le32)__raw_readl(addr); |
| 641 | buf++; |
| 642 | count -= 4; |
| 643 | } |
| 644 | break; |
| 645 | } |
| 646 | default: |
| 647 | SSB_WARN_ON(1); |
| 648 | } |
| 649 | } |
| 650 | #endif /* CONFIG_SSB_BLOCKIO */ |
| 651 | |
Michael Buesch | ffc7689 | 2008-02-20 19:08:10 +0100 | [diff] [blame] | 652 | static void ssb_ssb_write8(struct ssb_device *dev, u16 offset, u8 value) |
| 653 | { |
| 654 | struct ssb_bus *bus = dev->bus; |
| 655 | |
| 656 | offset += dev->core_index * SSB_CORE_SIZE; |
| 657 | writeb(value, bus->mmio + offset); |
| 658 | } |
| 659 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 660 | static void ssb_ssb_write16(struct ssb_device *dev, u16 offset, u16 value) |
| 661 | { |
| 662 | struct ssb_bus *bus = dev->bus; |
| 663 | |
| 664 | offset += dev->core_index * SSB_CORE_SIZE; |
| 665 | writew(value, bus->mmio + offset); |
| 666 | } |
| 667 | |
| 668 | static void ssb_ssb_write32(struct ssb_device *dev, u16 offset, u32 value) |
| 669 | { |
| 670 | struct ssb_bus *bus = dev->bus; |
| 671 | |
| 672 | offset += dev->core_index * SSB_CORE_SIZE; |
| 673 | writel(value, bus->mmio + offset); |
| 674 | } |
| 675 | |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 676 | #ifdef CONFIG_SSB_BLOCKIO |
| 677 | static void ssb_ssb_block_write(struct ssb_device *dev, const void *buffer, |
| 678 | size_t count, u16 offset, u8 reg_width) |
| 679 | { |
| 680 | struct ssb_bus *bus = dev->bus; |
| 681 | void __iomem *addr; |
| 682 | |
| 683 | offset += dev->core_index * SSB_CORE_SIZE; |
| 684 | addr = bus->mmio + offset; |
| 685 | |
| 686 | switch (reg_width) { |
| 687 | case sizeof(u8): { |
| 688 | const u8 *buf = buffer; |
| 689 | |
| 690 | while (count) { |
| 691 | __raw_writeb(*buf, addr); |
| 692 | buf++; |
| 693 | count--; |
| 694 | } |
| 695 | break; |
| 696 | } |
| 697 | case sizeof(u16): { |
| 698 | const __le16 *buf = buffer; |
| 699 | |
| 700 | SSB_WARN_ON(count & 1); |
| 701 | while (count) { |
| 702 | __raw_writew((__force u16)(*buf), addr); |
| 703 | buf++; |
| 704 | count -= 2; |
| 705 | } |
| 706 | break; |
| 707 | } |
| 708 | case sizeof(u32): { |
| 709 | const __le32 *buf = buffer; |
| 710 | |
| 711 | SSB_WARN_ON(count & 3); |
| 712 | while (count) { |
| 713 | __raw_writel((__force u32)(*buf), addr); |
| 714 | buf++; |
| 715 | count -= 4; |
| 716 | } |
| 717 | break; |
| 718 | } |
| 719 | default: |
| 720 | SSB_WARN_ON(1); |
| 721 | } |
| 722 | } |
| 723 | #endif /* CONFIG_SSB_BLOCKIO */ |
| 724 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 725 | /* Ops for the plain SSB bus without a host-device (no PCI or PCMCIA). */ |
| 726 | static const struct ssb_bus_ops ssb_ssb_ops = { |
Michael Buesch | ffc7689 | 2008-02-20 19:08:10 +0100 | [diff] [blame] | 727 | .read8 = ssb_ssb_read8, |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 728 | .read16 = ssb_ssb_read16, |
| 729 | .read32 = ssb_ssb_read32, |
Michael Buesch | ffc7689 | 2008-02-20 19:08:10 +0100 | [diff] [blame] | 730 | .write8 = ssb_ssb_write8, |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 731 | .write16 = ssb_ssb_write16, |
| 732 | .write32 = ssb_ssb_write32, |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 733 | #ifdef CONFIG_SSB_BLOCKIO |
| 734 | .block_read = ssb_ssb_block_read, |
| 735 | .block_write = ssb_ssb_block_write, |
| 736 | #endif |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 737 | }; |
| 738 | |
| 739 | static int ssb_fetch_invariants(struct ssb_bus *bus, |
| 740 | ssb_invariants_func_t get_invariants) |
| 741 | { |
| 742 | struct ssb_init_invariants iv; |
| 743 | int err; |
| 744 | |
| 745 | memset(&iv, 0, sizeof(iv)); |
| 746 | err = get_invariants(bus, &iv); |
| 747 | if (err) |
| 748 | goto out; |
| 749 | memcpy(&bus->boardinfo, &iv.boardinfo, sizeof(iv.boardinfo)); |
| 750 | memcpy(&bus->sprom, &iv.sprom, sizeof(iv.sprom)); |
Michael Buesch | 7cb4461 | 2008-02-19 17:46:48 +0100 | [diff] [blame] | 751 | bus->has_cardbus_slot = iv.has_cardbus_slot; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 752 | out: |
| 753 | return err; |
| 754 | } |
| 755 | |
Hauke Mehrtens | cd15598 | 2011-06-21 20:57:16 +0200 | [diff] [blame] | 756 | static int __devinit ssb_bus_register(struct ssb_bus *bus, |
| 757 | ssb_invariants_func_t get_invariants, |
| 758 | unsigned long baseaddr) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 759 | { |
| 760 | int err; |
| 761 | |
| 762 | spin_lock_init(&bus->bar_lock); |
| 763 | INIT_LIST_HEAD(&bus->list); |
Michael Buesch | 53521d8 | 2008-02-19 16:22:50 +0100 | [diff] [blame] | 764 | #ifdef CONFIG_SSB_EMBEDDED |
| 765 | spin_lock_init(&bus->gpio_lock); |
| 766 | #endif |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 767 | |
| 768 | /* Powerup the bus */ |
| 769 | err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1); |
| 770 | if (err) |
| 771 | goto out; |
Albert Herranz | 24ea602 | 2009-09-08 19:30:12 +0200 | [diff] [blame] | 772 | |
| 773 | /* Init SDIO-host device (if any), before the scan */ |
| 774 | err = ssb_sdio_init(bus); |
| 775 | if (err) |
| 776 | goto err_disable_xtal; |
| 777 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 778 | ssb_buses_lock(); |
| 779 | bus->busnumber = next_busnumber; |
| 780 | /* Scan for devices (cores) */ |
| 781 | err = ssb_bus_scan(bus, baseaddr); |
| 782 | if (err) |
Albert Herranz | 24ea602 | 2009-09-08 19:30:12 +0200 | [diff] [blame] | 783 | goto err_sdio_exit; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 784 | |
| 785 | /* Init PCI-host device (if any) */ |
| 786 | err = ssb_pci_init(bus); |
| 787 | if (err) |
| 788 | goto err_unmap; |
| 789 | /* Init PCMCIA-host device (if any) */ |
| 790 | err = ssb_pcmcia_init(bus); |
| 791 | if (err) |
| 792 | goto err_pci_exit; |
| 793 | |
| 794 | /* Initialize basic system devices (if available) */ |
| 795 | err = ssb_bus_powerup(bus, 0); |
| 796 | if (err) |
| 797 | goto err_pcmcia_exit; |
| 798 | ssb_chipcommon_init(&bus->chipco); |
Hauke Mehrtens | 394bc7e | 2012-11-20 22:24:32 +0000 | [diff] [blame] | 799 | ssb_extif_init(&bus->extif); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 800 | ssb_mipscore_init(&bus->mipscore); |
Hauke Mehrtens | ec43b08 | 2012-11-20 22:24:33 +0000 | [diff] [blame^] | 801 | err = ssb_gpio_init(bus); |
| 802 | if (err == -ENOTSUPP) |
| 803 | ssb_dprintk(KERN_DEBUG PFX "GPIO driver not activated\n"); |
| 804 | else if (err) |
| 805 | ssb_dprintk(KERN_ERR PFX |
| 806 | "Error registering GPIO driver: %i\n", err); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 807 | err = ssb_fetch_invariants(bus, get_invariants); |
| 808 | if (err) { |
| 809 | ssb_bus_may_powerdown(bus); |
| 810 | goto err_pcmcia_exit; |
| 811 | } |
| 812 | ssb_bus_may_powerdown(bus); |
| 813 | |
| 814 | /* Queue it for attach. |
| 815 | * See the comment at the ssb_is_early_boot definition. */ |
| 816 | list_add_tail(&bus->list, &attach_queue); |
| 817 | if (!ssb_is_early_boot) { |
| 818 | /* This is not early boot, so we must attach the bus now */ |
| 819 | err = ssb_attach_queued_buses(); |
| 820 | if (err) |
| 821 | goto err_dequeue; |
| 822 | } |
| 823 | next_busnumber++; |
| 824 | ssb_buses_unlock(); |
| 825 | |
| 826 | out: |
| 827 | return err; |
| 828 | |
| 829 | err_dequeue: |
| 830 | list_del(&bus->list); |
| 831 | err_pcmcia_exit: |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 832 | ssb_pcmcia_exit(bus); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 833 | err_pci_exit: |
| 834 | ssb_pci_exit(bus); |
| 835 | err_unmap: |
| 836 | ssb_iounmap(bus); |
Albert Herranz | 24ea602 | 2009-09-08 19:30:12 +0200 | [diff] [blame] | 837 | err_sdio_exit: |
| 838 | ssb_sdio_exit(bus); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 839 | err_disable_xtal: |
| 840 | ssb_buses_unlock(); |
| 841 | ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0); |
| 842 | return err; |
| 843 | } |
| 844 | |
| 845 | #ifdef CONFIG_SSB_PCIHOST |
Hauke Mehrtens | cd15598 | 2011-06-21 20:57:16 +0200 | [diff] [blame] | 846 | int __devinit ssb_bus_pcibus_register(struct ssb_bus *bus, |
| 847 | struct pci_dev *host_pci) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 848 | { |
| 849 | int err; |
| 850 | |
| 851 | bus->bustype = SSB_BUSTYPE_PCI; |
| 852 | bus->host_pci = host_pci; |
| 853 | bus->ops = &ssb_pci_ops; |
| 854 | |
| 855 | err = ssb_bus_register(bus, ssb_pci_get_invariants, 0); |
| 856 | if (!err) { |
| 857 | ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on " |
Kay Sievers | b7b05fe | 2008-10-30 15:51:57 +0100 | [diff] [blame] | 858 | "PCI device %s\n", dev_name(&host_pci->dev)); |
Larry Finger | ce9626e | 2010-04-23 13:17:21 -0500 | [diff] [blame] | 859 | } else { |
| 860 | ssb_printk(KERN_ERR PFX "Failed to register PCI version" |
| 861 | " of SSB with error %d\n", err); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | return err; |
| 865 | } |
| 866 | EXPORT_SYMBOL(ssb_bus_pcibus_register); |
| 867 | #endif /* CONFIG_SSB_PCIHOST */ |
| 868 | |
| 869 | #ifdef CONFIG_SSB_PCMCIAHOST |
Hauke Mehrtens | cd15598 | 2011-06-21 20:57:16 +0200 | [diff] [blame] | 870 | int __devinit ssb_bus_pcmciabus_register(struct ssb_bus *bus, |
| 871 | struct pcmcia_device *pcmcia_dev, |
| 872 | unsigned long baseaddr) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 873 | { |
| 874 | int err; |
| 875 | |
| 876 | bus->bustype = SSB_BUSTYPE_PCMCIA; |
| 877 | bus->host_pcmcia = pcmcia_dev; |
| 878 | bus->ops = &ssb_pcmcia_ops; |
| 879 | |
| 880 | err = ssb_bus_register(bus, ssb_pcmcia_get_invariants, baseaddr); |
| 881 | if (!err) { |
| 882 | ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on " |
| 883 | "PCMCIA device %s\n", pcmcia_dev->devname); |
| 884 | } |
| 885 | |
| 886 | return err; |
| 887 | } |
| 888 | EXPORT_SYMBOL(ssb_bus_pcmciabus_register); |
| 889 | #endif /* CONFIG_SSB_PCMCIAHOST */ |
| 890 | |
Albert Herranz | 24ea602 | 2009-09-08 19:30:12 +0200 | [diff] [blame] | 891 | #ifdef CONFIG_SSB_SDIOHOST |
Hauke Mehrtens | cd15598 | 2011-06-21 20:57:16 +0200 | [diff] [blame] | 892 | int __devinit ssb_bus_sdiobus_register(struct ssb_bus *bus, |
| 893 | struct sdio_func *func, |
| 894 | unsigned int quirks) |
Albert Herranz | 24ea602 | 2009-09-08 19:30:12 +0200 | [diff] [blame] | 895 | { |
| 896 | int err; |
| 897 | |
| 898 | bus->bustype = SSB_BUSTYPE_SDIO; |
| 899 | bus->host_sdio = func; |
| 900 | bus->ops = &ssb_sdio_ops; |
| 901 | bus->quirks = quirks; |
| 902 | |
| 903 | err = ssb_bus_register(bus, ssb_sdio_get_invariants, ~0); |
| 904 | if (!err) { |
| 905 | ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on " |
| 906 | "SDIO device %s\n", sdio_func_id(func)); |
| 907 | } |
| 908 | |
| 909 | return err; |
| 910 | } |
| 911 | EXPORT_SYMBOL(ssb_bus_sdiobus_register); |
| 912 | #endif /* CONFIG_SSB_PCMCIAHOST */ |
| 913 | |
Hauke Mehrtens | cd15598 | 2011-06-21 20:57:16 +0200 | [diff] [blame] | 914 | int __devinit ssb_bus_ssbbus_register(struct ssb_bus *bus, |
| 915 | unsigned long baseaddr, |
| 916 | ssb_invariants_func_t get_invariants) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 917 | { |
| 918 | int err; |
| 919 | |
| 920 | bus->bustype = SSB_BUSTYPE_SSB; |
| 921 | bus->ops = &ssb_ssb_ops; |
| 922 | |
| 923 | err = ssb_bus_register(bus, get_invariants, baseaddr); |
| 924 | if (!err) { |
| 925 | ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found at " |
| 926 | "address 0x%08lX\n", baseaddr); |
| 927 | } |
| 928 | |
| 929 | return err; |
| 930 | } |
| 931 | |
| 932 | int __ssb_driver_register(struct ssb_driver *drv, struct module *owner) |
| 933 | { |
| 934 | drv->drv.name = drv->name; |
| 935 | drv->drv.bus = &ssb_bustype; |
| 936 | drv->drv.owner = owner; |
| 937 | |
| 938 | return driver_register(&drv->drv); |
| 939 | } |
| 940 | EXPORT_SYMBOL(__ssb_driver_register); |
| 941 | |
| 942 | void ssb_driver_unregister(struct ssb_driver *drv) |
| 943 | { |
| 944 | driver_unregister(&drv->drv); |
| 945 | } |
| 946 | EXPORT_SYMBOL(ssb_driver_unregister); |
| 947 | |
| 948 | void ssb_set_devtypedata(struct ssb_device *dev, void *data) |
| 949 | { |
| 950 | struct ssb_bus *bus = dev->bus; |
| 951 | struct ssb_device *ent; |
| 952 | int i; |
| 953 | |
| 954 | for (i = 0; i < bus->nr_devices; i++) { |
| 955 | ent = &(bus->devices[i]); |
| 956 | if (ent->id.vendor != dev->id.vendor) |
| 957 | continue; |
| 958 | if (ent->id.coreid != dev->id.coreid) |
| 959 | continue; |
| 960 | |
| 961 | ent->devtypedata = data; |
| 962 | } |
| 963 | } |
| 964 | EXPORT_SYMBOL(ssb_set_devtypedata); |
| 965 | |
| 966 | static u32 clkfactor_f6_resolve(u32 v) |
| 967 | { |
| 968 | /* map the magic values */ |
| 969 | switch (v) { |
| 970 | case SSB_CHIPCO_CLK_F6_2: |
| 971 | return 2; |
| 972 | case SSB_CHIPCO_CLK_F6_3: |
| 973 | return 3; |
| 974 | case SSB_CHIPCO_CLK_F6_4: |
| 975 | return 4; |
| 976 | case SSB_CHIPCO_CLK_F6_5: |
| 977 | return 5; |
| 978 | case SSB_CHIPCO_CLK_F6_6: |
| 979 | return 6; |
| 980 | case SSB_CHIPCO_CLK_F6_7: |
| 981 | return 7; |
| 982 | } |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | /* Calculate the speed the backplane would run at a given set of clockcontrol values */ |
| 987 | u32 ssb_calc_clock_rate(u32 plltype, u32 n, u32 m) |
| 988 | { |
| 989 | u32 n1, n2, clock, m1, m2, m3, mc; |
| 990 | |
| 991 | n1 = (n & SSB_CHIPCO_CLK_N1); |
| 992 | n2 = ((n & SSB_CHIPCO_CLK_N2) >> SSB_CHIPCO_CLK_N2_SHIFT); |
| 993 | |
| 994 | switch (plltype) { |
| 995 | case SSB_PLLTYPE_6: /* 100/200 or 120/240 only */ |
| 996 | if (m & SSB_CHIPCO_CLK_T6_MMASK) |
Hauke Mehrtens | e913d46 | 2011-06-21 20:53:20 +0200 | [diff] [blame] | 997 | return SSB_CHIPCO_CLK_T6_M1; |
| 998 | return SSB_CHIPCO_CLK_T6_M0; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 999 | case SSB_PLLTYPE_1: /* 48Mhz base, 3 dividers */ |
| 1000 | case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */ |
| 1001 | case SSB_PLLTYPE_4: /* 48Mhz, 4 dividers */ |
| 1002 | case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */ |
| 1003 | n1 = clkfactor_f6_resolve(n1); |
| 1004 | n2 += SSB_CHIPCO_CLK_F5_BIAS; |
| 1005 | break; |
| 1006 | case SSB_PLLTYPE_2: /* 48Mhz, 4 dividers */ |
| 1007 | n1 += SSB_CHIPCO_CLK_T2_BIAS; |
| 1008 | n2 += SSB_CHIPCO_CLK_T2_BIAS; |
| 1009 | SSB_WARN_ON(!((n1 >= 2) && (n1 <= 7))); |
| 1010 | SSB_WARN_ON(!((n2 >= 5) && (n2 <= 23))); |
| 1011 | break; |
| 1012 | case SSB_PLLTYPE_5: /* 25Mhz, 4 dividers */ |
| 1013 | return 100000000; |
| 1014 | default: |
| 1015 | SSB_WARN_ON(1); |
| 1016 | } |
| 1017 | |
| 1018 | switch (plltype) { |
| 1019 | case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */ |
| 1020 | case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */ |
| 1021 | clock = SSB_CHIPCO_CLK_BASE2 * n1 * n2; |
| 1022 | break; |
| 1023 | default: |
| 1024 | clock = SSB_CHIPCO_CLK_BASE1 * n1 * n2; |
| 1025 | } |
| 1026 | if (!clock) |
| 1027 | return 0; |
| 1028 | |
| 1029 | m1 = (m & SSB_CHIPCO_CLK_M1); |
| 1030 | m2 = ((m & SSB_CHIPCO_CLK_M2) >> SSB_CHIPCO_CLK_M2_SHIFT); |
| 1031 | m3 = ((m & SSB_CHIPCO_CLK_M3) >> SSB_CHIPCO_CLK_M3_SHIFT); |
| 1032 | mc = ((m & SSB_CHIPCO_CLK_MC) >> SSB_CHIPCO_CLK_MC_SHIFT); |
| 1033 | |
| 1034 | switch (plltype) { |
| 1035 | case SSB_PLLTYPE_1: /* 48Mhz base, 3 dividers */ |
| 1036 | case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */ |
| 1037 | case SSB_PLLTYPE_4: /* 48Mhz, 4 dividers */ |
| 1038 | case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */ |
| 1039 | m1 = clkfactor_f6_resolve(m1); |
| 1040 | if ((plltype == SSB_PLLTYPE_1) || |
| 1041 | (plltype == SSB_PLLTYPE_3)) |
| 1042 | m2 += SSB_CHIPCO_CLK_F5_BIAS; |
| 1043 | else |
| 1044 | m2 = clkfactor_f6_resolve(m2); |
| 1045 | m3 = clkfactor_f6_resolve(m3); |
| 1046 | |
| 1047 | switch (mc) { |
| 1048 | case SSB_CHIPCO_CLK_MC_BYPASS: |
| 1049 | return clock; |
| 1050 | case SSB_CHIPCO_CLK_MC_M1: |
| 1051 | return (clock / m1); |
| 1052 | case SSB_CHIPCO_CLK_MC_M1M2: |
| 1053 | return (clock / (m1 * m2)); |
| 1054 | case SSB_CHIPCO_CLK_MC_M1M2M3: |
| 1055 | return (clock / (m1 * m2 * m3)); |
| 1056 | case SSB_CHIPCO_CLK_MC_M1M3: |
| 1057 | return (clock / (m1 * m3)); |
| 1058 | } |
| 1059 | return 0; |
| 1060 | case SSB_PLLTYPE_2: |
| 1061 | m1 += SSB_CHIPCO_CLK_T2_BIAS; |
| 1062 | m2 += SSB_CHIPCO_CLK_T2M2_BIAS; |
| 1063 | m3 += SSB_CHIPCO_CLK_T2_BIAS; |
| 1064 | SSB_WARN_ON(!((m1 >= 2) && (m1 <= 7))); |
| 1065 | SSB_WARN_ON(!((m2 >= 3) && (m2 <= 10))); |
| 1066 | SSB_WARN_ON(!((m3 >= 2) && (m3 <= 7))); |
| 1067 | |
| 1068 | if (!(mc & SSB_CHIPCO_CLK_T2MC_M1BYP)) |
| 1069 | clock /= m1; |
| 1070 | if (!(mc & SSB_CHIPCO_CLK_T2MC_M2BYP)) |
| 1071 | clock /= m2; |
| 1072 | if (!(mc & SSB_CHIPCO_CLK_T2MC_M3BYP)) |
| 1073 | clock /= m3; |
| 1074 | return clock; |
| 1075 | default: |
| 1076 | SSB_WARN_ON(1); |
| 1077 | } |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | /* Get the current speed the backplane is running at */ |
| 1082 | u32 ssb_clockspeed(struct ssb_bus *bus) |
| 1083 | { |
| 1084 | u32 rate; |
| 1085 | u32 plltype; |
| 1086 | u32 clkctl_n, clkctl_m; |
| 1087 | |
Hauke Mehrtens | d486a5b | 2012-02-01 00:13:56 +0100 | [diff] [blame] | 1088 | if (bus->chipco.capabilities & SSB_CHIPCO_CAP_PMU) |
| 1089 | return ssb_pmu_get_controlclock(&bus->chipco); |
| 1090 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1091 | if (ssb_extif_available(&bus->extif)) |
| 1092 | ssb_extif_get_clockcontrol(&bus->extif, &plltype, |
| 1093 | &clkctl_n, &clkctl_m); |
| 1094 | else if (bus->chipco.dev) |
| 1095 | ssb_chipco_get_clockcontrol(&bus->chipco, &plltype, |
| 1096 | &clkctl_n, &clkctl_m); |
| 1097 | else |
| 1098 | return 0; |
| 1099 | |
| 1100 | if (bus->chip_id == 0x5365) { |
| 1101 | rate = 100000000; |
| 1102 | } else { |
| 1103 | rate = ssb_calc_clock_rate(plltype, clkctl_n, clkctl_m); |
| 1104 | if (plltype == SSB_PLLTYPE_3) /* 25Mhz, 2 dividers */ |
| 1105 | rate /= 2; |
| 1106 | } |
| 1107 | |
| 1108 | return rate; |
| 1109 | } |
| 1110 | EXPORT_SYMBOL(ssb_clockspeed); |
| 1111 | |
| 1112 | static u32 ssb_tmslow_reject_bitmask(struct ssb_device *dev) |
| 1113 | { |
Larry Finger | c272ef4 | 2007-11-09 16:56:25 -0600 | [diff] [blame] | 1114 | u32 rev = ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_SSBREV; |
| 1115 | |
Rafał Miłecki | 04ad1fb | 2011-04-23 19:30:29 +0200 | [diff] [blame] | 1116 | /* The REJECT bit seems to be different for Backplane rev 2.3 */ |
Larry Finger | c272ef4 | 2007-11-09 16:56:25 -0600 | [diff] [blame] | 1117 | switch (rev) { |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1118 | case SSB_IDLOW_SSBREV_22: |
Rafał Miłecki | 04ad1fb | 2011-04-23 19:30:29 +0200 | [diff] [blame] | 1119 | case SSB_IDLOW_SSBREV_24: |
| 1120 | case SSB_IDLOW_SSBREV_26: |
| 1121 | return SSB_TMSLOW_REJECT; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1122 | case SSB_IDLOW_SSBREV_23: |
| 1123 | return SSB_TMSLOW_REJECT_23; |
Rafał Miłecki | 04ad1fb | 2011-04-23 19:30:29 +0200 | [diff] [blame] | 1124 | case SSB_IDLOW_SSBREV_25: /* TODO - find the proper REJECT bit */ |
Larry Finger | c272ef4 | 2007-11-09 16:56:25 -0600 | [diff] [blame] | 1125 | case SSB_IDLOW_SSBREV_27: /* same here */ |
Rafał Miłecki | 04ad1fb | 2011-04-23 19:30:29 +0200 | [diff] [blame] | 1126 | return SSB_TMSLOW_REJECT; /* this is a guess */ |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1127 | default: |
Larry Finger | c272ef4 | 2007-11-09 16:56:25 -0600 | [diff] [blame] | 1128 | printk(KERN_INFO "ssb: Backplane Revision 0x%.8X\n", rev); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1129 | WARN_ON(1); |
| 1130 | } |
Rafał Miłecki | 04ad1fb | 2011-04-23 19:30:29 +0200 | [diff] [blame] | 1131 | return (SSB_TMSLOW_REJECT | SSB_TMSLOW_REJECT_23); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | int ssb_device_is_enabled(struct ssb_device *dev) |
| 1135 | { |
| 1136 | u32 val; |
| 1137 | u32 reject; |
| 1138 | |
| 1139 | reject = ssb_tmslow_reject_bitmask(dev); |
| 1140 | val = ssb_read32(dev, SSB_TMSLOW); |
| 1141 | val &= SSB_TMSLOW_CLOCK | SSB_TMSLOW_RESET | reject; |
| 1142 | |
| 1143 | return (val == SSB_TMSLOW_CLOCK); |
| 1144 | } |
| 1145 | EXPORT_SYMBOL(ssb_device_is_enabled); |
| 1146 | |
| 1147 | static void ssb_flush_tmslow(struct ssb_device *dev) |
| 1148 | { |
| 1149 | /* Make _really_ sure the device has finished the TMSLOW |
| 1150 | * register write transaction, as we risk running into |
| 1151 | * a machine check exception otherwise. |
| 1152 | * Do this by reading the register back to commit the |
| 1153 | * PCI write and delay an additional usec for the device |
| 1154 | * to react to the change. */ |
| 1155 | ssb_read32(dev, SSB_TMSLOW); |
| 1156 | udelay(1); |
| 1157 | } |
| 1158 | |
| 1159 | void ssb_device_enable(struct ssb_device *dev, u32 core_specific_flags) |
| 1160 | { |
| 1161 | u32 val; |
| 1162 | |
| 1163 | ssb_device_disable(dev, core_specific_flags); |
| 1164 | ssb_write32(dev, SSB_TMSLOW, |
| 1165 | SSB_TMSLOW_RESET | SSB_TMSLOW_CLOCK | |
| 1166 | SSB_TMSLOW_FGC | core_specific_flags); |
| 1167 | ssb_flush_tmslow(dev); |
| 1168 | |
| 1169 | /* Clear SERR if set. This is a hw bug workaround. */ |
| 1170 | if (ssb_read32(dev, SSB_TMSHIGH) & SSB_TMSHIGH_SERR) |
| 1171 | ssb_write32(dev, SSB_TMSHIGH, 0); |
| 1172 | |
| 1173 | val = ssb_read32(dev, SSB_IMSTATE); |
| 1174 | if (val & (SSB_IMSTATE_IBE | SSB_IMSTATE_TO)) { |
| 1175 | val &= ~(SSB_IMSTATE_IBE | SSB_IMSTATE_TO); |
| 1176 | ssb_write32(dev, SSB_IMSTATE, val); |
| 1177 | } |
| 1178 | |
| 1179 | ssb_write32(dev, SSB_TMSLOW, |
| 1180 | SSB_TMSLOW_CLOCK | SSB_TMSLOW_FGC | |
| 1181 | core_specific_flags); |
| 1182 | ssb_flush_tmslow(dev); |
| 1183 | |
| 1184 | ssb_write32(dev, SSB_TMSLOW, SSB_TMSLOW_CLOCK | |
| 1185 | core_specific_flags); |
| 1186 | ssb_flush_tmslow(dev); |
| 1187 | } |
| 1188 | EXPORT_SYMBOL(ssb_device_enable); |
| 1189 | |
Michael Büsch | 8c68bd4 | 2011-02-15 00:21:50 +0100 | [diff] [blame] | 1190 | /* Wait for bitmask in a register to get set or cleared. |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1191 | * timeout is in units of ten-microseconds */ |
Michael Büsch | 8c68bd4 | 2011-02-15 00:21:50 +0100 | [diff] [blame] | 1192 | static int ssb_wait_bits(struct ssb_device *dev, u16 reg, u32 bitmask, |
| 1193 | int timeout, int set) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1194 | { |
| 1195 | int i; |
| 1196 | u32 val; |
| 1197 | |
| 1198 | for (i = 0; i < timeout; i++) { |
| 1199 | val = ssb_read32(dev, reg); |
| 1200 | if (set) { |
Michael Büsch | 8c68bd4 | 2011-02-15 00:21:50 +0100 | [diff] [blame] | 1201 | if ((val & bitmask) == bitmask) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1202 | return 0; |
| 1203 | } else { |
| 1204 | if (!(val & bitmask)) |
| 1205 | return 0; |
| 1206 | } |
| 1207 | udelay(10); |
| 1208 | } |
| 1209 | printk(KERN_ERR PFX "Timeout waiting for bitmask %08X on " |
| 1210 | "register %04X to %s.\n", |
| 1211 | bitmask, reg, (set ? "set" : "clear")); |
| 1212 | |
| 1213 | return -ETIMEDOUT; |
| 1214 | } |
| 1215 | |
| 1216 | void ssb_device_disable(struct ssb_device *dev, u32 core_specific_flags) |
| 1217 | { |
Rafał Miłecki | b1a1bcf | 2011-02-17 01:50:50 +0100 | [diff] [blame] | 1218 | u32 reject, val; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1219 | |
| 1220 | if (ssb_read32(dev, SSB_TMSLOW) & SSB_TMSLOW_RESET) |
| 1221 | return; |
| 1222 | |
| 1223 | reject = ssb_tmslow_reject_bitmask(dev); |
Rafał Miłecki | b1a1bcf | 2011-02-17 01:50:50 +0100 | [diff] [blame] | 1224 | |
Rafał Miłecki | 011d183 | 2011-02-17 01:50:51 +0100 | [diff] [blame] | 1225 | if (ssb_read32(dev, SSB_TMSLOW) & SSB_TMSLOW_CLOCK) { |
| 1226 | ssb_write32(dev, SSB_TMSLOW, reject | SSB_TMSLOW_CLOCK); |
| 1227 | ssb_wait_bits(dev, SSB_TMSLOW, reject, 1000, 1); |
| 1228 | ssb_wait_bits(dev, SSB_TMSHIGH, SSB_TMSHIGH_BUSY, 1000, 0); |
Rafał Miłecki | b1a1bcf | 2011-02-17 01:50:50 +0100 | [diff] [blame] | 1229 | |
Rafał Miłecki | 011d183 | 2011-02-17 01:50:51 +0100 | [diff] [blame] | 1230 | if (ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_INITIATOR) { |
| 1231 | val = ssb_read32(dev, SSB_IMSTATE); |
| 1232 | val |= SSB_IMSTATE_REJECT; |
| 1233 | ssb_write32(dev, SSB_IMSTATE, val); |
| 1234 | ssb_wait_bits(dev, SSB_IMSTATE, SSB_IMSTATE_BUSY, 1000, |
| 1235 | 0); |
| 1236 | } |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1237 | |
Rafał Miłecki | 011d183 | 2011-02-17 01:50:51 +0100 | [diff] [blame] | 1238 | ssb_write32(dev, SSB_TMSLOW, |
| 1239 | SSB_TMSLOW_FGC | SSB_TMSLOW_CLOCK | |
| 1240 | reject | SSB_TMSLOW_RESET | |
| 1241 | core_specific_flags); |
| 1242 | ssb_flush_tmslow(dev); |
| 1243 | |
| 1244 | if (ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_INITIATOR) { |
| 1245 | val = ssb_read32(dev, SSB_IMSTATE); |
| 1246 | val &= ~SSB_IMSTATE_REJECT; |
| 1247 | ssb_write32(dev, SSB_IMSTATE, val); |
| 1248 | } |
Rafał Miłecki | b1a1bcf | 2011-02-17 01:50:50 +0100 | [diff] [blame] | 1249 | } |
| 1250 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1251 | ssb_write32(dev, SSB_TMSLOW, |
| 1252 | reject | SSB_TMSLOW_RESET | |
| 1253 | core_specific_flags); |
| 1254 | ssb_flush_tmslow(dev); |
| 1255 | } |
| 1256 | EXPORT_SYMBOL(ssb_device_disable); |
| 1257 | |
Rafał Miłecki | 04023af | 2011-08-14 19:39:40 +0200 | [diff] [blame] | 1258 | /* Some chipsets need routing known for PCIe and 64-bit DMA */ |
| 1259 | static bool ssb_dma_translation_special_bit(struct ssb_device *dev) |
| 1260 | { |
| 1261 | u16 chip_id = dev->bus->chip_id; |
| 1262 | |
| 1263 | if (dev->id.coreid == SSB_DEV_80211) { |
| 1264 | return (chip_id == 0x4322 || chip_id == 43221 || |
| 1265 | chip_id == 43231 || chip_id == 43222); |
| 1266 | } |
| 1267 | |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1271 | u32 ssb_dma_translation(struct ssb_device *dev) |
| 1272 | { |
| 1273 | switch (dev->bus->bustype) { |
| 1274 | case SSB_BUSTYPE_SSB: |
| 1275 | return 0; |
| 1276 | case SSB_BUSTYPE_PCI: |
Rafał Miłecki | 04023af | 2011-08-14 19:39:40 +0200 | [diff] [blame] | 1277 | if (pci_is_pcie(dev->bus->host_pci) && |
| 1278 | ssb_read32(dev, SSB_TMSHIGH) & SSB_TMSHIGH_DMA64) { |
Rafał Miłecki | a9770a8 | 2011-07-20 19:52:14 +0200 | [diff] [blame] | 1279 | return SSB_PCIE_DMA_H32; |
Rafał Miłecki | 04023af | 2011-08-14 19:39:40 +0200 | [diff] [blame] | 1280 | } else { |
| 1281 | if (ssb_dma_translation_special_bit(dev)) |
| 1282 | return SSB_PCIE_DMA_H32; |
| 1283 | else |
| 1284 | return SSB_PCI_DMA; |
| 1285 | } |
Michael Buesch | f225763 | 2008-06-20 11:50:29 +0200 | [diff] [blame] | 1286 | default: |
| 1287 | __ssb_dma_not_implemented(dev); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1288 | } |
| 1289 | return 0; |
| 1290 | } |
| 1291 | EXPORT_SYMBOL(ssb_dma_translation); |
| 1292 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1293 | int ssb_bus_may_powerdown(struct ssb_bus *bus) |
| 1294 | { |
| 1295 | struct ssb_chipcommon *cc; |
| 1296 | int err = 0; |
| 1297 | |
| 1298 | /* On buses where more than one core may be working |
| 1299 | * at a time, we must not powerdown stuff if there are |
| 1300 | * still cores that may want to run. */ |
| 1301 | if (bus->bustype == SSB_BUSTYPE_SSB) |
| 1302 | goto out; |
| 1303 | |
| 1304 | cc = &bus->chipco; |
Stefano Brivio | 881400a | 2008-04-06 17:05:07 +0200 | [diff] [blame] | 1305 | |
| 1306 | if (!cc->dev) |
| 1307 | goto out; |
| 1308 | if (cc->dev->id.revision < 5) |
| 1309 | goto out; |
| 1310 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1311 | ssb_chipco_set_clockmode(cc, SSB_CLKMODE_SLOW); |
| 1312 | err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0); |
| 1313 | if (err) |
| 1314 | goto error; |
| 1315 | out: |
| 1316 | #ifdef CONFIG_SSB_DEBUG |
| 1317 | bus->powered_up = 0; |
| 1318 | #endif |
| 1319 | return err; |
| 1320 | error: |
| 1321 | ssb_printk(KERN_ERR PFX "Bus powerdown failed\n"); |
| 1322 | goto out; |
| 1323 | } |
| 1324 | EXPORT_SYMBOL(ssb_bus_may_powerdown); |
| 1325 | |
| 1326 | int ssb_bus_powerup(struct ssb_bus *bus, bool dynamic_pctl) |
| 1327 | { |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1328 | int err; |
| 1329 | enum ssb_clkmode mode; |
| 1330 | |
| 1331 | err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1); |
| 1332 | if (err) |
| 1333 | goto error; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1334 | |
| 1335 | #ifdef CONFIG_SSB_DEBUG |
| 1336 | bus->powered_up = 1; |
| 1337 | #endif |
Rafał Miłecki | a6ef814 | 2011-04-23 19:30:28 +0200 | [diff] [blame] | 1338 | |
| 1339 | mode = dynamic_pctl ? SSB_CLKMODE_DYNAMIC : SSB_CLKMODE_FAST; |
| 1340 | ssb_chipco_set_clockmode(&bus->chipco, mode); |
| 1341 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1342 | return 0; |
| 1343 | error: |
| 1344 | ssb_printk(KERN_ERR PFX "Bus powerup failed\n"); |
| 1345 | return err; |
| 1346 | } |
| 1347 | EXPORT_SYMBOL(ssb_bus_powerup); |
| 1348 | |
Rafał Miłecki | 8576f81 | 2011-05-11 02:10:58 +0200 | [diff] [blame] | 1349 | static void ssb_broadcast_value(struct ssb_device *dev, |
| 1350 | u32 address, u32 data) |
| 1351 | { |
John W. Linville | 1159024 | 2011-05-13 09:23:47 -0400 | [diff] [blame] | 1352 | #ifdef CONFIG_SSB_DRIVER_PCICORE |
Rafał Miłecki | 8576f81 | 2011-05-11 02:10:58 +0200 | [diff] [blame] | 1353 | /* This is used for both, PCI and ChipCommon core, so be careful. */ |
| 1354 | BUILD_BUG_ON(SSB_PCICORE_BCAST_ADDR != SSB_CHIPCO_BCAST_ADDR); |
| 1355 | BUILD_BUG_ON(SSB_PCICORE_BCAST_DATA != SSB_CHIPCO_BCAST_DATA); |
John W. Linville | 1159024 | 2011-05-13 09:23:47 -0400 | [diff] [blame] | 1356 | #endif |
Rafał Miłecki | 8576f81 | 2011-05-11 02:10:58 +0200 | [diff] [blame] | 1357 | |
John W. Linville | 1159024 | 2011-05-13 09:23:47 -0400 | [diff] [blame] | 1358 | ssb_write32(dev, SSB_CHIPCO_BCAST_ADDR, address); |
| 1359 | ssb_read32(dev, SSB_CHIPCO_BCAST_ADDR); /* flush */ |
| 1360 | ssb_write32(dev, SSB_CHIPCO_BCAST_DATA, data); |
| 1361 | ssb_read32(dev, SSB_CHIPCO_BCAST_DATA); /* flush */ |
Rafał Miłecki | 8576f81 | 2011-05-11 02:10:58 +0200 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | void ssb_commit_settings(struct ssb_bus *bus) |
| 1365 | { |
| 1366 | struct ssb_device *dev; |
| 1367 | |
John W. Linville | 1159024 | 2011-05-13 09:23:47 -0400 | [diff] [blame] | 1368 | #ifdef CONFIG_SSB_DRIVER_PCICORE |
Rafał Miłecki | 8576f81 | 2011-05-11 02:10:58 +0200 | [diff] [blame] | 1369 | dev = bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev; |
John W. Linville | 1159024 | 2011-05-13 09:23:47 -0400 | [diff] [blame] | 1370 | #else |
| 1371 | dev = bus->chipco.dev; |
| 1372 | #endif |
Rafał Miłecki | 8576f81 | 2011-05-11 02:10:58 +0200 | [diff] [blame] | 1373 | if (WARN_ON(!dev)) |
| 1374 | return; |
| 1375 | /* This forces an update of the cached registers. */ |
| 1376 | ssb_broadcast_value(dev, 0xFD8, 0); |
| 1377 | } |
| 1378 | EXPORT_SYMBOL(ssb_commit_settings); |
| 1379 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1380 | u32 ssb_admatch_base(u32 adm) |
| 1381 | { |
| 1382 | u32 base = 0; |
| 1383 | |
| 1384 | switch (adm & SSB_ADM_TYPE) { |
| 1385 | case SSB_ADM_TYPE0: |
| 1386 | base = (adm & SSB_ADM_BASE0); |
| 1387 | break; |
| 1388 | case SSB_ADM_TYPE1: |
| 1389 | SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */ |
| 1390 | base = (adm & SSB_ADM_BASE1); |
| 1391 | break; |
| 1392 | case SSB_ADM_TYPE2: |
| 1393 | SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */ |
| 1394 | base = (adm & SSB_ADM_BASE2); |
| 1395 | break; |
| 1396 | default: |
| 1397 | SSB_WARN_ON(1); |
| 1398 | } |
| 1399 | |
| 1400 | return base; |
| 1401 | } |
| 1402 | EXPORT_SYMBOL(ssb_admatch_base); |
| 1403 | |
| 1404 | u32 ssb_admatch_size(u32 adm) |
| 1405 | { |
| 1406 | u32 size = 0; |
| 1407 | |
| 1408 | switch (adm & SSB_ADM_TYPE) { |
| 1409 | case SSB_ADM_TYPE0: |
| 1410 | size = ((adm & SSB_ADM_SZ0) >> SSB_ADM_SZ0_SHIFT); |
| 1411 | break; |
| 1412 | case SSB_ADM_TYPE1: |
| 1413 | SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */ |
| 1414 | size = ((adm & SSB_ADM_SZ1) >> SSB_ADM_SZ1_SHIFT); |
| 1415 | break; |
| 1416 | case SSB_ADM_TYPE2: |
| 1417 | SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */ |
| 1418 | size = ((adm & SSB_ADM_SZ2) >> SSB_ADM_SZ2_SHIFT); |
| 1419 | break; |
| 1420 | default: |
| 1421 | SSB_WARN_ON(1); |
| 1422 | } |
| 1423 | size = (1 << (size + 1)); |
| 1424 | |
| 1425 | return size; |
| 1426 | } |
| 1427 | EXPORT_SYMBOL(ssb_admatch_size); |
| 1428 | |
| 1429 | static int __init ssb_modinit(void) |
| 1430 | { |
| 1431 | int err; |
| 1432 | |
| 1433 | /* See the comment at the ssb_is_early_boot definition */ |
| 1434 | ssb_is_early_boot = 0; |
| 1435 | err = bus_register(&ssb_bustype); |
| 1436 | if (err) |
| 1437 | return err; |
| 1438 | |
| 1439 | /* Maybe we already registered some buses at early boot. |
| 1440 | * Check for this and attach them |
| 1441 | */ |
| 1442 | ssb_buses_lock(); |
| 1443 | err = ssb_attach_queued_buses(); |
| 1444 | ssb_buses_unlock(); |
Michael Buesch | e6c463e | 2009-09-05 11:18:47 +0200 | [diff] [blame] | 1445 | if (err) { |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1446 | bus_unregister(&ssb_bustype); |
Michael Buesch | e6c463e | 2009-09-05 11:18:47 +0200 | [diff] [blame] | 1447 | goto out; |
| 1448 | } |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1449 | |
| 1450 | err = b43_pci_ssb_bridge_init(); |
| 1451 | if (err) { |
| 1452 | ssb_printk(KERN_ERR "Broadcom 43xx PCI-SSB-bridge " |
Michael Buesch | aab547c | 2008-02-29 11:36:12 +0100 | [diff] [blame] | 1453 | "initialization failed\n"); |
| 1454 | /* don't fail SSB init because of this */ |
| 1455 | err = 0; |
| 1456 | } |
| 1457 | err = ssb_gige_init(); |
| 1458 | if (err) { |
| 1459 | ssb_printk(KERN_ERR "SSB Broadcom Gigabit Ethernet " |
| 1460 | "driver initialization failed\n"); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1461 | /* don't fail SSB init because of this */ |
| 1462 | err = 0; |
| 1463 | } |
Michael Buesch | e6c463e | 2009-09-05 11:18:47 +0200 | [diff] [blame] | 1464 | out: |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1465 | return err; |
| 1466 | } |
Michael Buesch | 8d8c90e | 2007-10-27 15:14:39 +0200 | [diff] [blame] | 1467 | /* ssb must be initialized after PCI but before the ssb drivers. |
| 1468 | * That means we must use some initcall between subsys_initcall |
| 1469 | * and device_initcall. */ |
| 1470 | fs_initcall(ssb_modinit); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1471 | |
| 1472 | static void __exit ssb_modexit(void) |
| 1473 | { |
Michael Buesch | aab547c | 2008-02-29 11:36:12 +0100 | [diff] [blame] | 1474 | ssb_gige_exit(); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1475 | b43_pci_ssb_bridge_exit(); |
| 1476 | bus_unregister(&ssb_bustype); |
| 1477 | } |
| 1478 | module_exit(ssb_modexit) |