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