blob: 87f0ddf4f3f3aab5b09b2b0362eb39437f3aeb69 [file] [log] [blame]
Michael Buesch61e115a2007-09-18 15:12:50 -04001/*
2 * Sonics Silicon Backplane
3 * Subsystem core
4 *
5 * Copyright 2005, Broadcom Corporation
Michael Büscheb032b92011-07-04 20:50:05 +02006 * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
Michael Buesch61e115a2007-09-18 15:12:50 -04007 *
8 * Licensed under the GNU/GPL. See COPYING for details.
9 */
10
11#include "ssb_private.h"
12
13#include <linux/delay.h>
Geert Uytterhoeven6faf0352007-10-13 14:31:31 +020014#include <linux/io.h>
Paul Gortmaker20a112d2011-07-17 16:03:40 -040015#include <linux/module.h>
Michael Buesch61e115a2007-09-18 15:12:50 -040016#include <linux/ssb/ssb.h>
17#include <linux/ssb/ssb_regs.h>
Michael Bueschaab547c2008-02-29 11:36:12 +010018#include <linux/ssb/ssb_driver_gige.h>
Michael Buesch61e115a2007-09-18 15:12:50 -040019#include <linux/dma-mapping.h>
20#include <linux/pci.h>
Albert Herranz24ea6022009-09-08 19:30:12 +020021#include <linux/mmc/sdio_func.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Michael Buesch61e115a2007-09-18 15:12:50 -040023
Michael Buesch61e115a2007-09-18 15:12:50 -040024#include <pcmcia/cistpl.h>
25#include <pcmcia/ds.h>
26
27
28MODULE_DESCRIPTION("Sonics Silicon Backplane driver");
29MODULE_LICENSE("GPL");
30
31
32/* Temporary list of yet-to-be-attached buses */
33static LIST_HEAD(attach_queue);
34/* List if running buses */
35static LIST_HEAD(buses);
36/* Software ID counter */
37static 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. */
40static 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. */
48static bool ssb_is_early_boot = 1;
49
50static void ssb_buses_lock(void);
51static void ssb_buses_unlock(void);
52
53
54#ifdef CONFIG_SSB_PCIHOST
55struct 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;
66found:
67 ssb_buses_unlock();
68
69 return bus;
70}
71#endif /* CONFIG_SSB_PCIHOST */
72
Michael Buesche7ec2e32008-03-10 17:26:32 +010073#ifdef CONFIG_SSB_PCMCIAHOST
74struct 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;
85found:
86 ssb_buses_unlock();
87
88 return bus;
89}
90#endif /* CONFIG_SSB_PCMCIAHOST */
91
Albert Herranz24ea6022009-09-08 19:30:12 +020092#ifdef CONFIG_SSB_SDIOHOST
93struct 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;
104found:
105 ssb_buses_unlock();
106
107 return bus;
108}
109#endif /* CONFIG_SSB_SDIOHOST */
110
Michael Bueschaab547c2008-02-29 11:36:12 +0100111int 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 Buesch61e115a2007-09-18 15:12:50 -0400130static struct ssb_device *ssb_device_get(struct ssb_device *dev)
131{
132 if (dev)
133 get_device(dev->dev);
134 return dev;
135}
136
137static void ssb_device_put(struct ssb_device *dev)
138{
139 if (dev)
140 put_device(dev->dev);
141}
142
Michael Buesch61e115a2007-09-18 15:12:50 -0400143static 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 Buesch61e115a2007-09-18 15:12:50 -0400147 int err = 0;
148
Michael Buesch61e115a2007-09-18 15:12:50 -0400149 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 }
156out:
157 return err;
158}
159
Michael Buesch61e115a2007-09-18 15:12:50 -0400160static 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 Buesch61e115a2007-09-18 15:12:50 -0400164 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 Buesch61e115a2007-09-18 15:12:50 -0400173out:
174 return err;
175}
176
Michael Buesch8fe2b652008-03-30 00:10:50 +0100177int 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}
201EXPORT_SYMBOL(ssb_bus_resume);
202
203int 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}
210EXPORT_SYMBOL(ssb_bus_suspend);
211
Michael Bueschd72bb402008-04-02 17:03:26 +0200212#ifdef CONFIG_SSB_SPROM
Michael Buesch3ba60182009-11-23 20:12:13 +0100213/** 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 */
222int ssb_devices_freeze(struct ssb_bus *bus, struct ssb_freeze_context *ctx)
Michael Buesch61e115a2007-09-18 15:12:50 -0400223{
Michael Buesch3ba60182009-11-23 20:12:13 +0100224 struct ssb_device *sdev;
225 struct ssb_driver *sdrv;
226 unsigned int i;
Michael Buesch61e115a2007-09-18 15:12:50 -0400227
Michael Buesch3ba60182009-11-23 20:12:13 +0100228 memset(ctx, 0, sizeof(*ctx));
229 ctx->bus = bus;
230 SSB_WARN_ON(bus->nr_devices > ARRAY_SIZE(ctx->device_frozen));
231
Michael Buesch61e115a2007-09-18 15:12:50 -0400232 for (i = 0; i < bus->nr_devices; i++) {
Michael Buesch3ba60182009-11-23 20:12:13 +0100233 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 Buesch61e115a2007-09-18 15:12:50 -0400238 continue;
Michael Buesch61e115a2007-09-18 15:12:50 -0400239 }
Alan Sternf3ff9242012-01-24 13:35:24 -0500240 sdrv = drv_to_ssb_drv(sdev->dev->driver);
241 if (SSB_WARN_ON(!sdrv->remove))
Michael Buesch61e115a2007-09-18 15:12:50 -0400242 continue;
Michael Buesch3ba60182009-11-23 20:12:13 +0100243 sdrv->remove(sdev);
244 ctx->device_frozen[i] = 1;
Michael Buesch61e115a2007-09-18 15:12:50 -0400245 }
246
247 return 0;
Michael Buesch61e115a2007-09-18 15:12:50 -0400248}
249
Michael Buesch3ba60182009-11-23 20:12:13 +0100250/** 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 */
256int ssb_devices_thaw(struct ssb_freeze_context *ctx)
Michael Buesch61e115a2007-09-18 15:12:50 -0400257{
Michael Buesch3ba60182009-11-23 20:12:13 +0100258 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 Buesch61e115a2007-09-18 15:12:50 -0400263
264 for (i = 0; i < bus->nr_devices; i++) {
Michael Buesch3ba60182009-11-23 20:12:13 +0100265 if (!ctx->device_frozen[i])
Michael Buesch61e115a2007-09-18 15:12:50 -0400266 continue;
Michael Buesch3ba60182009-11-23 20:12:13 +0100267 sdev = &bus->devices[i];
268
269 if (SSB_WARN_ON(!sdev->dev || !sdev->dev->driver))
Michael Buesch61e115a2007-09-18 15:12:50 -0400270 continue;
Michael Buesch3ba60182009-11-23 20:12:13 +0100271 sdrv = drv_to_ssb_drv(sdev->dev->driver);
272 if (SSB_WARN_ON(!sdrv || !sdrv->probe))
Michael Buesch61e115a2007-09-18 15:12:50 -0400273 continue;
Michael Buesch3ba60182009-11-23 20:12:13 +0100274
275 err = sdrv->probe(sdev, &sdev->id);
Michael Buesch61e115a2007-09-18 15:12:50 -0400276 if (err) {
277 ssb_printk(KERN_ERR PFX "Failed to thaw device %s\n",
Michael Buesch3ba60182009-11-23 20:12:13 +0100278 dev_name(sdev->dev));
279 result = err;
Michael Buesch61e115a2007-09-18 15:12:50 -0400280 }
Michael Buesch3ba60182009-11-23 20:12:13 +0100281 ssb_device_put(sdev);
Michael Buesch61e115a2007-09-18 15:12:50 -0400282 }
283
Michael Buesch3ba60182009-11-23 20:12:13 +0100284 return result;
Michael Buesch61e115a2007-09-18 15:12:50 -0400285}
Michael Bueschd72bb402008-04-02 17:03:26 +0200286#endif /* CONFIG_SSB_SPROM */
Michael Buesch61e115a2007-09-18 15:12:50 -0400287
288static 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
300static 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
312static 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
327static 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
342static 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 Viro7ac03262007-10-14 05:46:09 +0100358static int ssb_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Michael Buesch61e115a2007-09-18 15:12:50 -0400359{
360 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
Michael Buesch61e115a2007-09-18 15:12:50 -0400361
362 if (!dev)
363 return -ENODEV;
364
Al Viro7ac03262007-10-14 05:46:09 +0100365 return add_uevent_var(env,
Michael Buesch61e115a2007-09-18 15:12:50 -0400366 "MODALIAS=ssb:v%04Xid%04Xrev%02X",
367 ssb_dev->id.vendor, ssb_dev->id.coreid,
368 ssb_dev->id.revision);
Michael Buesch61e115a2007-09-18 15:12:50 -0400369}
370
Hauke Mehrtensaa3bf282010-11-28 12:22:52 +0100371#define ssb_config_attr(attrib, field, format_string) \
372static ssize_t \
373attrib##_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
378ssb_config_attr(core_num, core_index, "%u\n")
379ssb_config_attr(coreid, id.coreid, "0x%04x\n")
380ssb_config_attr(vendor, id.vendor, "0x%04x\n")
381ssb_config_attr(revision, id.revision, "%u\n")
382ssb_config_attr(irq, irq, "%u\n")
383static ssize_t
384name_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
390static 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 Buesch61e115a2007-09-18 15:12:50 -0400400static 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 Mehrtensaa3bf282010-11-28 12:22:52 +0100409 .dev_attrs = ssb_device_attrs,
Michael Buesch61e115a2007-09-18 15:12:50 -0400410};
411
412static 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
419static 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
426static 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
438void 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 Buesche7ec2e32008-03-10 17:26:32 +0100445 ssb_pcmcia_exit(bus);
Michael Buesch61e115a2007-09-18 15:12:50 -0400446 ssb_pci_exit(bus);
447 ssb_iounmap(bus);
448}
449EXPORT_SYMBOL(ssb_bus_unregister);
450
451static 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
459static 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 Sieversb7b05fe2008-10-30 15:51:57 +0100495 dev_set_name(dev, "ssb%u:%d", bus->busnumber, dev_idx);
Michael Buesch61e115a2007-09-18 15:12:50 -0400496
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 Tomonori14f92952010-06-03 19:37:27 -0700502 sdev->dma_dev = dev->parent;
Michael Buesch61e115a2007-09-18 15:12:50 -0400503#endif
504 break;
505 case SSB_BUSTYPE_PCMCIA:
506#ifdef CONFIG_SSB_PCMCIAHOST
Dominik Brodowskieb141202010-03-07 12:21:16 +0100507 sdev->irq = bus->host_pcmcia->irq;
Michael Buesch61e115a2007-09-18 15:12:50 -0400508 dev->parent = &bus->host_pcmcia->dev;
509#endif
510 break;
Albert Herranz24ea6022009-09-08 19:30:12 +0200511 case SSB_BUSTYPE_SDIO:
Michael Buesch391ae222010-02-03 18:24:35 +0100512#ifdef CONFIG_SSB_SDIOHOST
Albert Herranz24ea6022009-09-08 19:30:12 +0200513 dev->parent = &bus->host_sdio->dev;
514#endif
515 break;
Michael Buesch61e115a2007-09-18 15:12:50 -0400516 case SSB_BUSTYPE_SSB:
Aurelien Jarnoac82da32008-09-26 22:27:11 +0200517 dev->dma_mask = &dev->coherent_dma_mask;
FUJITA Tomonori14f92952010-06-03 19:37:27 -0700518 sdev->dma_dev = dev;
Michael Buesch61e115a2007-09-18 15:12:50 -0400519 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 Sieversb7b05fe2008-10-30 15:51:57 +0100527 dev_name(dev));
Michael Buesch61e115a2007-09-18 15:12:50 -0400528 /* 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;
538error:
539 /* Unwind the already registered devices. */
540 ssb_devices_unregister(bus);
541 return err;
542}
543
544/* Needs ssb_buses_lock() */
Hauke Mehrtenscd155982011-06-21 20:57:16 +0200545static int __devinit ssb_attach_queued_buses(void)
Michael Buesch61e115a2007-09-18 15:12:50 -0400546{
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);
567error:
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 Bueschffc76892008-02-20 19:08:10 +0100579static 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 Buesch61e115a2007-09-18 15:12:50 -0400587static 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
595static 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 Bueschd625a292008-04-02 19:46:56 +0200603#ifdef CONFIG_SSB_BLOCKIO
604static 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 Bueschffc76892008-02-20 19:08:10 +0100652static 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 Buesch61e115a2007-09-18 15:12:50 -0400660static 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
668static 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 Bueschd625a292008-04-02 19:46:56 +0200676#ifdef CONFIG_SSB_BLOCKIO
677static 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 Buesch61e115a2007-09-18 15:12:50 -0400725/* Ops for the plain SSB bus without a host-device (no PCI or PCMCIA). */
726static const struct ssb_bus_ops ssb_ssb_ops = {
Michael Bueschffc76892008-02-20 19:08:10 +0100727 .read8 = ssb_ssb_read8,
Michael Buesch61e115a2007-09-18 15:12:50 -0400728 .read16 = ssb_ssb_read16,
729 .read32 = ssb_ssb_read32,
Michael Bueschffc76892008-02-20 19:08:10 +0100730 .write8 = ssb_ssb_write8,
Michael Buesch61e115a2007-09-18 15:12:50 -0400731 .write16 = ssb_ssb_write16,
732 .write32 = ssb_ssb_write32,
Michael Bueschd625a292008-04-02 19:46:56 +0200733#ifdef CONFIG_SSB_BLOCKIO
734 .block_read = ssb_ssb_block_read,
735 .block_write = ssb_ssb_block_write,
736#endif
Michael Buesch61e115a2007-09-18 15:12:50 -0400737};
738
739static 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 Buesch7cb44612008-02-19 17:46:48 +0100751 bus->has_cardbus_slot = iv.has_cardbus_slot;
Michael Buesch61e115a2007-09-18 15:12:50 -0400752out:
753 return err;
754}
755
Hauke Mehrtenscd155982011-06-21 20:57:16 +0200756static int __devinit ssb_bus_register(struct ssb_bus *bus,
757 ssb_invariants_func_t get_invariants,
758 unsigned long baseaddr)
Michael Buesch61e115a2007-09-18 15:12:50 -0400759{
760 int err;
761
762 spin_lock_init(&bus->bar_lock);
763 INIT_LIST_HEAD(&bus->list);
Michael Buesch53521d82008-02-19 16:22:50 +0100764#ifdef CONFIG_SSB_EMBEDDED
765 spin_lock_init(&bus->gpio_lock);
766#endif
Michael Buesch61e115a2007-09-18 15:12:50 -0400767
768 /* Powerup the bus */
769 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
770 if (err)
771 goto out;
Albert Herranz24ea6022009-09-08 19:30:12 +0200772
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 Buesch61e115a2007-09-18 15:12:50 -0400778 ssb_buses_lock();
779 bus->busnumber = next_busnumber;
780 /* Scan for devices (cores) */
781 err = ssb_bus_scan(bus, baseaddr);
782 if (err)
Albert Herranz24ea6022009-09-08 19:30:12 +0200783 goto err_sdio_exit;
Michael Buesch61e115a2007-09-18 15:12:50 -0400784
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 Mehrtens394bc7e2012-11-20 22:24:32 +0000799 ssb_extif_init(&bus->extif);
Michael Buesch61e115a2007-09-18 15:12:50 -0400800 ssb_mipscore_init(&bus->mipscore);
Hauke Mehrtensec43b082012-11-20 22:24:33 +0000801 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 Buesch61e115a2007-09-18 15:12:50 -0400807 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
826out:
827 return err;
828
829err_dequeue:
830 list_del(&bus->list);
831err_pcmcia_exit:
Michael Buesche7ec2e32008-03-10 17:26:32 +0100832 ssb_pcmcia_exit(bus);
Michael Buesch61e115a2007-09-18 15:12:50 -0400833err_pci_exit:
834 ssb_pci_exit(bus);
835err_unmap:
836 ssb_iounmap(bus);
Albert Herranz24ea6022009-09-08 19:30:12 +0200837err_sdio_exit:
838 ssb_sdio_exit(bus);
Michael Buesch61e115a2007-09-18 15:12:50 -0400839err_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 Mehrtenscd155982011-06-21 20:57:16 +0200846int __devinit ssb_bus_pcibus_register(struct ssb_bus *bus,
847 struct pci_dev *host_pci)
Michael Buesch61e115a2007-09-18 15:12:50 -0400848{
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 Sieversb7b05fe2008-10-30 15:51:57 +0100858 "PCI device %s\n", dev_name(&host_pci->dev));
Larry Fingerce9626e2010-04-23 13:17:21 -0500859 } else {
860 ssb_printk(KERN_ERR PFX "Failed to register PCI version"
861 " of SSB with error %d\n", err);
Michael Buesch61e115a2007-09-18 15:12:50 -0400862 }
863
864 return err;
865}
866EXPORT_SYMBOL(ssb_bus_pcibus_register);
867#endif /* CONFIG_SSB_PCIHOST */
868
869#ifdef CONFIG_SSB_PCMCIAHOST
Hauke Mehrtenscd155982011-06-21 20:57:16 +0200870int __devinit ssb_bus_pcmciabus_register(struct ssb_bus *bus,
871 struct pcmcia_device *pcmcia_dev,
872 unsigned long baseaddr)
Michael Buesch61e115a2007-09-18 15:12:50 -0400873{
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}
888EXPORT_SYMBOL(ssb_bus_pcmciabus_register);
889#endif /* CONFIG_SSB_PCMCIAHOST */
890
Albert Herranz24ea6022009-09-08 19:30:12 +0200891#ifdef CONFIG_SSB_SDIOHOST
Hauke Mehrtenscd155982011-06-21 20:57:16 +0200892int __devinit ssb_bus_sdiobus_register(struct ssb_bus *bus,
893 struct sdio_func *func,
894 unsigned int quirks)
Albert Herranz24ea6022009-09-08 19:30:12 +0200895{
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}
911EXPORT_SYMBOL(ssb_bus_sdiobus_register);
912#endif /* CONFIG_SSB_PCMCIAHOST */
913
Hauke Mehrtenscd155982011-06-21 20:57:16 +0200914int __devinit ssb_bus_ssbbus_register(struct ssb_bus *bus,
915 unsigned long baseaddr,
916 ssb_invariants_func_t get_invariants)
Michael Buesch61e115a2007-09-18 15:12:50 -0400917{
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
932int __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}
940EXPORT_SYMBOL(__ssb_driver_register);
941
942void ssb_driver_unregister(struct ssb_driver *drv)
943{
944 driver_unregister(&drv->drv);
945}
946EXPORT_SYMBOL(ssb_driver_unregister);
947
948void 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}
964EXPORT_SYMBOL(ssb_set_devtypedata);
965
966static 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 */
987u32 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 Mehrtense913d462011-06-21 20:53:20 +0200997 return SSB_CHIPCO_CLK_T6_M1;
998 return SSB_CHIPCO_CLK_T6_M0;
Michael Buesch61e115a2007-09-18 15:12:50 -0400999 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 */
1082u32 ssb_clockspeed(struct ssb_bus *bus)
1083{
1084 u32 rate;
1085 u32 plltype;
1086 u32 clkctl_n, clkctl_m;
1087
Hauke Mehrtensd486a5b2012-02-01 00:13:56 +01001088 if (bus->chipco.capabilities & SSB_CHIPCO_CAP_PMU)
1089 return ssb_pmu_get_controlclock(&bus->chipco);
1090
Michael Buesch61e115a2007-09-18 15:12:50 -04001091 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}
1110EXPORT_SYMBOL(ssb_clockspeed);
1111
1112static u32 ssb_tmslow_reject_bitmask(struct ssb_device *dev)
1113{
Larry Fingerc272ef42007-11-09 16:56:25 -06001114 u32 rev = ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_SSBREV;
1115
Rafał Miłecki04ad1fb2011-04-23 19:30:29 +02001116 /* The REJECT bit seems to be different for Backplane rev 2.3 */
Larry Fingerc272ef42007-11-09 16:56:25 -06001117 switch (rev) {
Michael Buesch61e115a2007-09-18 15:12:50 -04001118 case SSB_IDLOW_SSBREV_22:
Rafał Miłecki04ad1fb2011-04-23 19:30:29 +02001119 case SSB_IDLOW_SSBREV_24:
1120 case SSB_IDLOW_SSBREV_26:
1121 return SSB_TMSLOW_REJECT;
Michael Buesch61e115a2007-09-18 15:12:50 -04001122 case SSB_IDLOW_SSBREV_23:
1123 return SSB_TMSLOW_REJECT_23;
Rafał Miłecki04ad1fb2011-04-23 19:30:29 +02001124 case SSB_IDLOW_SSBREV_25: /* TODO - find the proper REJECT bit */
Larry Fingerc272ef42007-11-09 16:56:25 -06001125 case SSB_IDLOW_SSBREV_27: /* same here */
Rafał Miłecki04ad1fb2011-04-23 19:30:29 +02001126 return SSB_TMSLOW_REJECT; /* this is a guess */
Michael Buesch61e115a2007-09-18 15:12:50 -04001127 default:
Larry Fingerc272ef42007-11-09 16:56:25 -06001128 printk(KERN_INFO "ssb: Backplane Revision 0x%.8X\n", rev);
Michael Buesch61e115a2007-09-18 15:12:50 -04001129 WARN_ON(1);
1130 }
Rafał Miłecki04ad1fb2011-04-23 19:30:29 +02001131 return (SSB_TMSLOW_REJECT | SSB_TMSLOW_REJECT_23);
Michael Buesch61e115a2007-09-18 15:12:50 -04001132}
1133
1134int 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}
1145EXPORT_SYMBOL(ssb_device_is_enabled);
1146
1147static 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
1159void 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}
1188EXPORT_SYMBOL(ssb_device_enable);
1189
Michael Büsch8c68bd42011-02-15 00:21:50 +01001190/* Wait for bitmask in a register to get set or cleared.
Michael Buesch61e115a2007-09-18 15:12:50 -04001191 * timeout is in units of ten-microseconds */
Michael Büsch8c68bd42011-02-15 00:21:50 +01001192static int ssb_wait_bits(struct ssb_device *dev, u16 reg, u32 bitmask,
1193 int timeout, int set)
Michael Buesch61e115a2007-09-18 15:12:50 -04001194{
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üsch8c68bd42011-02-15 00:21:50 +01001201 if ((val & bitmask) == bitmask)
Michael Buesch61e115a2007-09-18 15:12:50 -04001202 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
1216void ssb_device_disable(struct ssb_device *dev, u32 core_specific_flags)
1217{
Rafał Miłeckib1a1bcf2011-02-17 01:50:50 +01001218 u32 reject, val;
Michael Buesch61e115a2007-09-18 15:12:50 -04001219
1220 if (ssb_read32(dev, SSB_TMSLOW) & SSB_TMSLOW_RESET)
1221 return;
1222
1223 reject = ssb_tmslow_reject_bitmask(dev);
Rafał Miłeckib1a1bcf2011-02-17 01:50:50 +01001224
Rafał Miłecki011d1832011-02-17 01:50:51 +01001225 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łeckib1a1bcf2011-02-17 01:50:50 +01001229
Rafał Miłecki011d1832011-02-17 01:50:51 +01001230 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 Buesch61e115a2007-09-18 15:12:50 -04001237
Rafał Miłecki011d1832011-02-17 01:50:51 +01001238 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łeckib1a1bcf2011-02-17 01:50:50 +01001249 }
1250
Michael Buesch61e115a2007-09-18 15:12:50 -04001251 ssb_write32(dev, SSB_TMSLOW,
1252 reject | SSB_TMSLOW_RESET |
1253 core_specific_flags);
1254 ssb_flush_tmslow(dev);
1255}
1256EXPORT_SYMBOL(ssb_device_disable);
1257
Rafał Miłecki04023af2011-08-14 19:39:40 +02001258/* Some chipsets need routing known for PCIe and 64-bit DMA */
1259static 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 Buesch61e115a2007-09-18 15:12:50 -04001271u32 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łecki04023af2011-08-14 19:39:40 +02001277 if (pci_is_pcie(dev->bus->host_pci) &&
1278 ssb_read32(dev, SSB_TMSHIGH) & SSB_TMSHIGH_DMA64) {
Rafał Miłeckia9770a82011-07-20 19:52:14 +02001279 return SSB_PCIE_DMA_H32;
Rafał Miłecki04023af2011-08-14 19:39:40 +02001280 } else {
1281 if (ssb_dma_translation_special_bit(dev))
1282 return SSB_PCIE_DMA_H32;
1283 else
1284 return SSB_PCI_DMA;
1285 }
Michael Bueschf2257632008-06-20 11:50:29 +02001286 default:
1287 __ssb_dma_not_implemented(dev);
Michael Buesch61e115a2007-09-18 15:12:50 -04001288 }
1289 return 0;
1290}
1291EXPORT_SYMBOL(ssb_dma_translation);
1292
Michael Buesch61e115a2007-09-18 15:12:50 -04001293int 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 Brivio881400a2008-04-06 17:05:07 +02001305
1306 if (!cc->dev)
1307 goto out;
1308 if (cc->dev->id.revision < 5)
1309 goto out;
1310
Michael Buesch61e115a2007-09-18 15:12:50 -04001311 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;
1315out:
1316#ifdef CONFIG_SSB_DEBUG
1317 bus->powered_up = 0;
1318#endif
1319 return err;
1320error:
1321 ssb_printk(KERN_ERR PFX "Bus powerdown failed\n");
1322 goto out;
1323}
1324EXPORT_SYMBOL(ssb_bus_may_powerdown);
1325
1326int ssb_bus_powerup(struct ssb_bus *bus, bool dynamic_pctl)
1327{
Michael Buesch61e115a2007-09-18 15:12:50 -04001328 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 Buesch61e115a2007-09-18 15:12:50 -04001334
1335#ifdef CONFIG_SSB_DEBUG
1336 bus->powered_up = 1;
1337#endif
Rafał Miłeckia6ef8142011-04-23 19:30:28 +02001338
1339 mode = dynamic_pctl ? SSB_CLKMODE_DYNAMIC : SSB_CLKMODE_FAST;
1340 ssb_chipco_set_clockmode(&bus->chipco, mode);
1341
Michael Buesch61e115a2007-09-18 15:12:50 -04001342 return 0;
1343error:
1344 ssb_printk(KERN_ERR PFX "Bus powerup failed\n");
1345 return err;
1346}
1347EXPORT_SYMBOL(ssb_bus_powerup);
1348
Rafał Miłecki8576f812011-05-11 02:10:58 +02001349static void ssb_broadcast_value(struct ssb_device *dev,
1350 u32 address, u32 data)
1351{
John W. Linville11590242011-05-13 09:23:47 -04001352#ifdef CONFIG_SSB_DRIVER_PCICORE
Rafał Miłecki8576f812011-05-11 02:10:58 +02001353 /* 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. Linville11590242011-05-13 09:23:47 -04001356#endif
Rafał Miłecki8576f812011-05-11 02:10:58 +02001357
John W. Linville11590242011-05-13 09:23:47 -04001358 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łecki8576f812011-05-11 02:10:58 +02001362}
1363
1364void ssb_commit_settings(struct ssb_bus *bus)
1365{
1366 struct ssb_device *dev;
1367
John W. Linville11590242011-05-13 09:23:47 -04001368#ifdef CONFIG_SSB_DRIVER_PCICORE
Rafał Miłecki8576f812011-05-11 02:10:58 +02001369 dev = bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev;
John W. Linville11590242011-05-13 09:23:47 -04001370#else
1371 dev = bus->chipco.dev;
1372#endif
Rafał Miłecki8576f812011-05-11 02:10:58 +02001373 if (WARN_ON(!dev))
1374 return;
1375 /* This forces an update of the cached registers. */
1376 ssb_broadcast_value(dev, 0xFD8, 0);
1377}
1378EXPORT_SYMBOL(ssb_commit_settings);
1379
Michael Buesch61e115a2007-09-18 15:12:50 -04001380u32 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}
1402EXPORT_SYMBOL(ssb_admatch_base);
1403
1404u32 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}
1427EXPORT_SYMBOL(ssb_admatch_size);
1428
1429static 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 Buesche6c463e2009-09-05 11:18:47 +02001445 if (err) {
Michael Buesch61e115a2007-09-18 15:12:50 -04001446 bus_unregister(&ssb_bustype);
Michael Buesche6c463e2009-09-05 11:18:47 +02001447 goto out;
1448 }
Michael Buesch61e115a2007-09-18 15:12:50 -04001449
1450 err = b43_pci_ssb_bridge_init();
1451 if (err) {
1452 ssb_printk(KERN_ERR "Broadcom 43xx PCI-SSB-bridge "
Michael Bueschaab547c2008-02-29 11:36:12 +01001453 "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 Buesch61e115a2007-09-18 15:12:50 -04001461 /* don't fail SSB init because of this */
1462 err = 0;
1463 }
Michael Buesche6c463e2009-09-05 11:18:47 +02001464out:
Michael Buesch61e115a2007-09-18 15:12:50 -04001465 return err;
1466}
Michael Buesch8d8c90e2007-10-27 15:14:39 +02001467/* 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. */
1470fs_initcall(ssb_modinit);
Michael Buesch61e115a2007-09-18 15:12:50 -04001471
1472static void __exit ssb_modexit(void)
1473{
Michael Bueschaab547c2008-02-29 11:36:12 +01001474 ssb_gige_exit();
Michael Buesch61e115a2007-09-18 15:12:50 -04001475 b43_pci_ssb_bridge_exit();
1476 bus_unregister(&ssb_bustype);
1477}
1478module_exit(ssb_modexit)