blob: af07ab22708feefe41a13dcf87087bf8e18e0b1c [file] [log] [blame]
Michael Buesch61e115a2007-09-18 15:12:50 -04001/*
2 * Sonics Silicon Backplane
3 * Subsystem core
4 *
5 * Copyright 2005, Broadcom Corporation
6 * Copyright 2006, 2007, Michael Buesch <mb@bu3sch.de>
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 Uytterhoeven6faf0352007-10-13 14:31:31 +020014#include <linux/io.h>
Michael Buesch61e115a2007-09-18 15:12:50 -040015#include <linux/ssb/ssb.h>
16#include <linux/ssb/ssb_regs.h>
17#include <linux/dma-mapping.h>
18#include <linux/pci.h>
19
20#include <pcmcia/cs_types.h>
21#include <pcmcia/cs.h>
22#include <pcmcia/cistpl.h>
23#include <pcmcia/ds.h>
24
25
26MODULE_DESCRIPTION("Sonics Silicon Backplane driver");
27MODULE_LICENSE("GPL");
28
29
30/* Temporary list of yet-to-be-attached buses */
31static LIST_HEAD(attach_queue);
32/* List if running buses */
33static LIST_HEAD(buses);
34/* Software ID counter */
35static unsigned int next_busnumber;
36/* buses_mutes locks the two buslists and the next_busnumber.
37 * Don't lock this directly, but use ssb_buses_[un]lock() below. */
38static DEFINE_MUTEX(buses_mutex);
39
40/* There are differences in the codeflow, if the bus is
41 * initialized from early boot, as various needed services
42 * are not available early. This is a mechanism to delay
43 * these initializations to after early boot has finished.
44 * It's also used to avoid mutex locking, as that's not
45 * available and needed early. */
46static bool ssb_is_early_boot = 1;
47
48static void ssb_buses_lock(void);
49static void ssb_buses_unlock(void);
50
51
52#ifdef CONFIG_SSB_PCIHOST
53struct ssb_bus *ssb_pci_dev_to_bus(struct pci_dev *pdev)
54{
55 struct ssb_bus *bus;
56
57 ssb_buses_lock();
58 list_for_each_entry(bus, &buses, list) {
59 if (bus->bustype == SSB_BUSTYPE_PCI &&
60 bus->host_pci == pdev)
61 goto found;
62 }
63 bus = NULL;
64found:
65 ssb_buses_unlock();
66
67 return bus;
68}
69#endif /* CONFIG_SSB_PCIHOST */
70
71static struct ssb_device *ssb_device_get(struct ssb_device *dev)
72{
73 if (dev)
74 get_device(dev->dev);
75 return dev;
76}
77
78static void ssb_device_put(struct ssb_device *dev)
79{
80 if (dev)
81 put_device(dev->dev);
82}
83
84static int ssb_bus_resume(struct ssb_bus *bus)
85{
86 int err;
87
88 ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
89 err = ssb_pcmcia_init(bus);
90 if (err) {
91 /* No need to disable XTAL, as we don't have one on PCMCIA. */
92 return err;
93 }
94 ssb_chipco_resume(&bus->chipco);
95
96 return 0;
97}
98
99static int ssb_device_resume(struct device *dev)
100{
101 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
102 struct ssb_driver *ssb_drv;
103 struct ssb_bus *bus;
104 int err = 0;
105
106 bus = ssb_dev->bus;
107 if (bus->suspend_cnt == bus->nr_devices) {
108 err = ssb_bus_resume(bus);
109 if (err)
110 return err;
111 }
112 bus->suspend_cnt--;
113 if (dev->driver) {
114 ssb_drv = drv_to_ssb_drv(dev->driver);
115 if (ssb_drv && ssb_drv->resume)
116 err = ssb_drv->resume(ssb_dev);
117 if (err)
118 goto out;
119 }
120out:
121 return err;
122}
123
124static void ssb_bus_suspend(struct ssb_bus *bus, pm_message_t state)
125{
126 ssb_chipco_suspend(&bus->chipco, state);
127 ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
128
129 /* Reset HW state information in memory, so that HW is
130 * completely reinitialized on resume. */
131 bus->mapped_device = NULL;
132#ifdef CONFIG_SSB_DRIVER_PCICORE
133 bus->pcicore.setup_done = 0;
134#endif
135#ifdef CONFIG_SSB_DEBUG
136 bus->powered_up = 0;
137#endif
138}
139
140static int ssb_device_suspend(struct device *dev, pm_message_t state)
141{
142 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
143 struct ssb_driver *ssb_drv;
144 struct ssb_bus *bus;
145 int err = 0;
146
147 if (dev->driver) {
148 ssb_drv = drv_to_ssb_drv(dev->driver);
149 if (ssb_drv && ssb_drv->suspend)
150 err = ssb_drv->suspend(ssb_dev, state);
151 if (err)
152 goto out;
153 }
154
155 bus = ssb_dev->bus;
156 bus->suspend_cnt++;
157 if (bus->suspend_cnt == bus->nr_devices) {
158 /* All devices suspended. Shutdown the bus. */
159 ssb_bus_suspend(bus, state);
160 }
161
162out:
163 return err;
164}
165
166#ifdef CONFIG_SSB_PCIHOST
167int ssb_devices_freeze(struct ssb_bus *bus)
168{
169 struct ssb_device *dev;
170 struct ssb_driver *drv;
171 int err = 0;
172 int i;
173 pm_message_t state = PMSG_FREEZE;
174
175 /* First check that we are capable to freeze all devices. */
176 for (i = 0; i < bus->nr_devices; i++) {
177 dev = &(bus->devices[i]);
178 if (!dev->dev ||
179 !dev->dev->driver ||
180 !device_is_registered(dev->dev))
181 continue;
182 drv = drv_to_ssb_drv(dev->dev->driver);
183 if (!drv)
184 continue;
185 if (!drv->suspend) {
186 /* Nope, can't suspend this one. */
187 return -EOPNOTSUPP;
188 }
189 }
190 /* Now suspend all devices */
191 for (i = 0; i < bus->nr_devices; i++) {
192 dev = &(bus->devices[i]);
193 if (!dev->dev ||
194 !dev->dev->driver ||
195 !device_is_registered(dev->dev))
196 continue;
197 drv = drv_to_ssb_drv(dev->dev->driver);
198 if (!drv)
199 continue;
200 err = drv->suspend(dev, state);
201 if (err) {
202 ssb_printk(KERN_ERR PFX "Failed to freeze device %s\n",
203 dev->dev->bus_id);
204 goto err_unwind;
205 }
206 }
207
208 return 0;
209err_unwind:
210 for (i--; i >= 0; i--) {
211 dev = &(bus->devices[i]);
212 if (!dev->dev ||
213 !dev->dev->driver ||
214 !device_is_registered(dev->dev))
215 continue;
216 drv = drv_to_ssb_drv(dev->dev->driver);
217 if (!drv)
218 continue;
219 if (drv->resume)
220 drv->resume(dev);
221 }
222 return err;
223}
224
225int ssb_devices_thaw(struct ssb_bus *bus)
226{
227 struct ssb_device *dev;
228 struct ssb_driver *drv;
229 int err;
230 int i;
231
232 for (i = 0; i < bus->nr_devices; i++) {
233 dev = &(bus->devices[i]);
234 if (!dev->dev ||
235 !dev->dev->driver ||
236 !device_is_registered(dev->dev))
237 continue;
238 drv = drv_to_ssb_drv(dev->dev->driver);
239 if (!drv)
240 continue;
241 if (SSB_WARN_ON(!drv->resume))
242 continue;
243 err = drv->resume(dev);
244 if (err) {
245 ssb_printk(KERN_ERR PFX "Failed to thaw device %s\n",
246 dev->dev->bus_id);
247 }
248 }
249
250 return 0;
251}
252#endif /* CONFIG_SSB_PCIHOST */
253
254static void ssb_device_shutdown(struct device *dev)
255{
256 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
257 struct ssb_driver *ssb_drv;
258
259 if (!dev->driver)
260 return;
261 ssb_drv = drv_to_ssb_drv(dev->driver);
262 if (ssb_drv && ssb_drv->shutdown)
263 ssb_drv->shutdown(ssb_dev);
264}
265
266static int ssb_device_remove(struct device *dev)
267{
268 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
269 struct ssb_driver *ssb_drv = drv_to_ssb_drv(dev->driver);
270
271 if (ssb_drv && ssb_drv->remove)
272 ssb_drv->remove(ssb_dev);
273 ssb_device_put(ssb_dev);
274
275 return 0;
276}
277
278static int ssb_device_probe(struct device *dev)
279{
280 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
281 struct ssb_driver *ssb_drv = drv_to_ssb_drv(dev->driver);
282 int err = 0;
283
284 ssb_device_get(ssb_dev);
285 if (ssb_drv && ssb_drv->probe)
286 err = ssb_drv->probe(ssb_dev, &ssb_dev->id);
287 if (err)
288 ssb_device_put(ssb_dev);
289
290 return err;
291}
292
293static int ssb_match_devid(const struct ssb_device_id *tabid,
294 const struct ssb_device_id *devid)
295{
296 if ((tabid->vendor != devid->vendor) &&
297 tabid->vendor != SSB_ANY_VENDOR)
298 return 0;
299 if ((tabid->coreid != devid->coreid) &&
300 tabid->coreid != SSB_ANY_ID)
301 return 0;
302 if ((tabid->revision != devid->revision) &&
303 tabid->revision != SSB_ANY_REV)
304 return 0;
305 return 1;
306}
307
308static int ssb_bus_match(struct device *dev, struct device_driver *drv)
309{
310 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
311 struct ssb_driver *ssb_drv = drv_to_ssb_drv(drv);
312 const struct ssb_device_id *id;
313
314 for (id = ssb_drv->id_table;
315 id->vendor || id->coreid || id->revision;
316 id++) {
317 if (ssb_match_devid(id, &ssb_dev->id))
318 return 1; /* found */
319 }
320
321 return 0;
322}
323
Al Viro7ac03262007-10-14 05:46:09 +0100324static int ssb_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Michael Buesch61e115a2007-09-18 15:12:50 -0400325{
326 struct ssb_device *ssb_dev = dev_to_ssb_dev(dev);
Michael Buesch61e115a2007-09-18 15:12:50 -0400327
328 if (!dev)
329 return -ENODEV;
330
Al Viro7ac03262007-10-14 05:46:09 +0100331 return add_uevent_var(env,
Michael Buesch61e115a2007-09-18 15:12:50 -0400332 "MODALIAS=ssb:v%04Xid%04Xrev%02X",
333 ssb_dev->id.vendor, ssb_dev->id.coreid,
334 ssb_dev->id.revision);
Michael Buesch61e115a2007-09-18 15:12:50 -0400335}
336
337static struct bus_type ssb_bustype = {
338 .name = "ssb",
339 .match = ssb_bus_match,
340 .probe = ssb_device_probe,
341 .remove = ssb_device_remove,
342 .shutdown = ssb_device_shutdown,
343 .suspend = ssb_device_suspend,
344 .resume = ssb_device_resume,
345 .uevent = ssb_device_uevent,
346};
347
348static void ssb_buses_lock(void)
349{
350 /* See the comment at the ssb_is_early_boot definition */
351 if (!ssb_is_early_boot)
352 mutex_lock(&buses_mutex);
353}
354
355static void ssb_buses_unlock(void)
356{
357 /* See the comment at the ssb_is_early_boot definition */
358 if (!ssb_is_early_boot)
359 mutex_unlock(&buses_mutex);
360}
361
362static void ssb_devices_unregister(struct ssb_bus *bus)
363{
364 struct ssb_device *sdev;
365 int i;
366
367 for (i = bus->nr_devices - 1; i >= 0; i--) {
368 sdev = &(bus->devices[i]);
369 if (sdev->dev)
370 device_unregister(sdev->dev);
371 }
372}
373
374void ssb_bus_unregister(struct ssb_bus *bus)
375{
376 ssb_buses_lock();
377 ssb_devices_unregister(bus);
378 list_del(&bus->list);
379 ssb_buses_unlock();
380
381 /* ssb_pcmcia_exit(bus); */
382 ssb_pci_exit(bus);
383 ssb_iounmap(bus);
384}
385EXPORT_SYMBOL(ssb_bus_unregister);
386
387static void ssb_release_dev(struct device *dev)
388{
389 struct __ssb_dev_wrapper *devwrap;
390
391 devwrap = container_of(dev, struct __ssb_dev_wrapper, dev);
392 kfree(devwrap);
393}
394
395static int ssb_devices_register(struct ssb_bus *bus)
396{
397 struct ssb_device *sdev;
398 struct device *dev;
399 struct __ssb_dev_wrapper *devwrap;
400 int i, err = 0;
401 int dev_idx = 0;
402
403 for (i = 0; i < bus->nr_devices; i++) {
404 sdev = &(bus->devices[i]);
405
406 /* We don't register SSB-system devices to the kernel,
407 * as the drivers for them are built into SSB. */
408 switch (sdev->id.coreid) {
409 case SSB_DEV_CHIPCOMMON:
410 case SSB_DEV_PCI:
411 case SSB_DEV_PCIE:
412 case SSB_DEV_PCMCIA:
413 case SSB_DEV_MIPS:
414 case SSB_DEV_MIPS_3302:
415 case SSB_DEV_EXTIF:
416 continue;
417 }
418
419 devwrap = kzalloc(sizeof(*devwrap), GFP_KERNEL);
420 if (!devwrap) {
421 ssb_printk(KERN_ERR PFX
422 "Could not allocate device\n");
423 err = -ENOMEM;
424 goto error;
425 }
426 dev = &devwrap->dev;
427 devwrap->sdev = sdev;
428
429 dev->release = ssb_release_dev;
430 dev->bus = &ssb_bustype;
431 snprintf(dev->bus_id, sizeof(dev->bus_id),
432 "ssb%u:%d", bus->busnumber, dev_idx);
433
434 switch (bus->bustype) {
435 case SSB_BUSTYPE_PCI:
436#ifdef CONFIG_SSB_PCIHOST
437 sdev->irq = bus->host_pci->irq;
438 dev->parent = &bus->host_pci->dev;
439#endif
440 break;
441 case SSB_BUSTYPE_PCMCIA:
442#ifdef CONFIG_SSB_PCMCIAHOST
Michael Buesch60d78c42007-11-07 19:03:35 +0100443 sdev->irq = bus->host_pcmcia->irq.AssignedIRQ;
Michael Buesch61e115a2007-09-18 15:12:50 -0400444 dev->parent = &bus->host_pcmcia->dev;
445#endif
446 break;
447 case SSB_BUSTYPE_SSB:
448 break;
449 }
450
451 sdev->dev = dev;
452 err = device_register(dev);
453 if (err) {
454 ssb_printk(KERN_ERR PFX
455 "Could not register %s\n",
456 dev->bus_id);
457 /* Set dev to NULL to not unregister
458 * dev on error unwinding. */
459 sdev->dev = NULL;
460 kfree(devwrap);
461 goto error;
462 }
463 dev_idx++;
464 }
465
466 return 0;
467error:
468 /* Unwind the already registered devices. */
469 ssb_devices_unregister(bus);
470 return err;
471}
472
473/* Needs ssb_buses_lock() */
474static int ssb_attach_queued_buses(void)
475{
476 struct ssb_bus *bus, *n;
477 int err = 0;
478 int drop_them_all = 0;
479
480 list_for_each_entry_safe(bus, n, &attach_queue, list) {
481 if (drop_them_all) {
482 list_del(&bus->list);
483 continue;
484 }
485 /* Can't init the PCIcore in ssb_bus_register(), as that
486 * is too early in boot for embedded systems
487 * (no udelay() available). So do it here in attach stage.
488 */
489 err = ssb_bus_powerup(bus, 0);
490 if (err)
491 goto error;
492 ssb_pcicore_init(&bus->pcicore);
493 ssb_bus_may_powerdown(bus);
494
495 err = ssb_devices_register(bus);
496error:
497 if (err) {
498 drop_them_all = 1;
499 list_del(&bus->list);
500 continue;
501 }
502 list_move_tail(&bus->list, &buses);
503 }
504
505 return err;
506}
507
508static u16 ssb_ssb_read16(struct ssb_device *dev, u16 offset)
509{
510 struct ssb_bus *bus = dev->bus;
511
512 offset += dev->core_index * SSB_CORE_SIZE;
513 return readw(bus->mmio + offset);
514}
515
516static u32 ssb_ssb_read32(struct ssb_device *dev, u16 offset)
517{
518 struct ssb_bus *bus = dev->bus;
519
520 offset += dev->core_index * SSB_CORE_SIZE;
521 return readl(bus->mmio + offset);
522}
523
524static void ssb_ssb_write16(struct ssb_device *dev, u16 offset, u16 value)
525{
526 struct ssb_bus *bus = dev->bus;
527
528 offset += dev->core_index * SSB_CORE_SIZE;
529 writew(value, bus->mmio + offset);
530}
531
532static void ssb_ssb_write32(struct ssb_device *dev, u16 offset, u32 value)
533{
534 struct ssb_bus *bus = dev->bus;
535
536 offset += dev->core_index * SSB_CORE_SIZE;
537 writel(value, bus->mmio + offset);
538}
539
540/* Ops for the plain SSB bus without a host-device (no PCI or PCMCIA). */
541static const struct ssb_bus_ops ssb_ssb_ops = {
542 .read16 = ssb_ssb_read16,
543 .read32 = ssb_ssb_read32,
544 .write16 = ssb_ssb_write16,
545 .write32 = ssb_ssb_write32,
546};
547
548static int ssb_fetch_invariants(struct ssb_bus *bus,
549 ssb_invariants_func_t get_invariants)
550{
551 struct ssb_init_invariants iv;
552 int err;
553
554 memset(&iv, 0, sizeof(iv));
555 err = get_invariants(bus, &iv);
556 if (err)
557 goto out;
558 memcpy(&bus->boardinfo, &iv.boardinfo, sizeof(iv.boardinfo));
559 memcpy(&bus->sprom, &iv.sprom, sizeof(iv.sprom));
560out:
561 return err;
562}
563
564static int ssb_bus_register(struct ssb_bus *bus,
565 ssb_invariants_func_t get_invariants,
566 unsigned long baseaddr)
567{
568 int err;
569
570 spin_lock_init(&bus->bar_lock);
571 INIT_LIST_HEAD(&bus->list);
Michael Buesch53521d82008-02-19 16:22:50 +0100572#ifdef CONFIG_SSB_EMBEDDED
573 spin_lock_init(&bus->gpio_lock);
574#endif
Michael Buesch61e115a2007-09-18 15:12:50 -0400575
576 /* Powerup the bus */
577 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
578 if (err)
579 goto out;
580 ssb_buses_lock();
581 bus->busnumber = next_busnumber;
582 /* Scan for devices (cores) */
583 err = ssb_bus_scan(bus, baseaddr);
584 if (err)
585 goto err_disable_xtal;
586
587 /* Init PCI-host device (if any) */
588 err = ssb_pci_init(bus);
589 if (err)
590 goto err_unmap;
591 /* Init PCMCIA-host device (if any) */
592 err = ssb_pcmcia_init(bus);
593 if (err)
594 goto err_pci_exit;
595
596 /* Initialize basic system devices (if available) */
597 err = ssb_bus_powerup(bus, 0);
598 if (err)
599 goto err_pcmcia_exit;
600 ssb_chipcommon_init(&bus->chipco);
601 ssb_mipscore_init(&bus->mipscore);
602 err = ssb_fetch_invariants(bus, get_invariants);
603 if (err) {
604 ssb_bus_may_powerdown(bus);
605 goto err_pcmcia_exit;
606 }
607 ssb_bus_may_powerdown(bus);
608
609 /* Queue it for attach.
610 * See the comment at the ssb_is_early_boot definition. */
611 list_add_tail(&bus->list, &attach_queue);
612 if (!ssb_is_early_boot) {
613 /* This is not early boot, so we must attach the bus now */
614 err = ssb_attach_queued_buses();
615 if (err)
616 goto err_dequeue;
617 }
618 next_busnumber++;
619 ssb_buses_unlock();
620
621out:
622 return err;
623
624err_dequeue:
625 list_del(&bus->list);
626err_pcmcia_exit:
627/* ssb_pcmcia_exit(bus); */
628err_pci_exit:
629 ssb_pci_exit(bus);
630err_unmap:
631 ssb_iounmap(bus);
632err_disable_xtal:
633 ssb_buses_unlock();
634 ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
635 return err;
636}
637
638#ifdef CONFIG_SSB_PCIHOST
639int ssb_bus_pcibus_register(struct ssb_bus *bus,
640 struct pci_dev *host_pci)
641{
642 int err;
643
644 bus->bustype = SSB_BUSTYPE_PCI;
645 bus->host_pci = host_pci;
646 bus->ops = &ssb_pci_ops;
647
648 err = ssb_bus_register(bus, ssb_pci_get_invariants, 0);
649 if (!err) {
650 ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on "
651 "PCI device %s\n", host_pci->dev.bus_id);
652 }
653
654 return err;
655}
656EXPORT_SYMBOL(ssb_bus_pcibus_register);
657#endif /* CONFIG_SSB_PCIHOST */
658
659#ifdef CONFIG_SSB_PCMCIAHOST
660int ssb_bus_pcmciabus_register(struct ssb_bus *bus,
661 struct pcmcia_device *pcmcia_dev,
662 unsigned long baseaddr)
663{
664 int err;
665
666 bus->bustype = SSB_BUSTYPE_PCMCIA;
667 bus->host_pcmcia = pcmcia_dev;
668 bus->ops = &ssb_pcmcia_ops;
669
670 err = ssb_bus_register(bus, ssb_pcmcia_get_invariants, baseaddr);
671 if (!err) {
672 ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found on "
673 "PCMCIA device %s\n", pcmcia_dev->devname);
674 }
675
676 return err;
677}
678EXPORT_SYMBOL(ssb_bus_pcmciabus_register);
679#endif /* CONFIG_SSB_PCMCIAHOST */
680
681int ssb_bus_ssbbus_register(struct ssb_bus *bus,
682 unsigned long baseaddr,
683 ssb_invariants_func_t get_invariants)
684{
685 int err;
686
687 bus->bustype = SSB_BUSTYPE_SSB;
688 bus->ops = &ssb_ssb_ops;
689
690 err = ssb_bus_register(bus, get_invariants, baseaddr);
691 if (!err) {
692 ssb_printk(KERN_INFO PFX "Sonics Silicon Backplane found at "
693 "address 0x%08lX\n", baseaddr);
694 }
695
696 return err;
697}
698
699int __ssb_driver_register(struct ssb_driver *drv, struct module *owner)
700{
701 drv->drv.name = drv->name;
702 drv->drv.bus = &ssb_bustype;
703 drv->drv.owner = owner;
704
705 return driver_register(&drv->drv);
706}
707EXPORT_SYMBOL(__ssb_driver_register);
708
709void ssb_driver_unregister(struct ssb_driver *drv)
710{
711 driver_unregister(&drv->drv);
712}
713EXPORT_SYMBOL(ssb_driver_unregister);
714
715void ssb_set_devtypedata(struct ssb_device *dev, void *data)
716{
717 struct ssb_bus *bus = dev->bus;
718 struct ssb_device *ent;
719 int i;
720
721 for (i = 0; i < bus->nr_devices; i++) {
722 ent = &(bus->devices[i]);
723 if (ent->id.vendor != dev->id.vendor)
724 continue;
725 if (ent->id.coreid != dev->id.coreid)
726 continue;
727
728 ent->devtypedata = data;
729 }
730}
731EXPORT_SYMBOL(ssb_set_devtypedata);
732
733static u32 clkfactor_f6_resolve(u32 v)
734{
735 /* map the magic values */
736 switch (v) {
737 case SSB_CHIPCO_CLK_F6_2:
738 return 2;
739 case SSB_CHIPCO_CLK_F6_3:
740 return 3;
741 case SSB_CHIPCO_CLK_F6_4:
742 return 4;
743 case SSB_CHIPCO_CLK_F6_5:
744 return 5;
745 case SSB_CHIPCO_CLK_F6_6:
746 return 6;
747 case SSB_CHIPCO_CLK_F6_7:
748 return 7;
749 }
750 return 0;
751}
752
753/* Calculate the speed the backplane would run at a given set of clockcontrol values */
754u32 ssb_calc_clock_rate(u32 plltype, u32 n, u32 m)
755{
756 u32 n1, n2, clock, m1, m2, m3, mc;
757
758 n1 = (n & SSB_CHIPCO_CLK_N1);
759 n2 = ((n & SSB_CHIPCO_CLK_N2) >> SSB_CHIPCO_CLK_N2_SHIFT);
760
761 switch (plltype) {
762 case SSB_PLLTYPE_6: /* 100/200 or 120/240 only */
763 if (m & SSB_CHIPCO_CLK_T6_MMASK)
764 return SSB_CHIPCO_CLK_T6_M0;
765 return SSB_CHIPCO_CLK_T6_M1;
766 case SSB_PLLTYPE_1: /* 48Mhz base, 3 dividers */
767 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
768 case SSB_PLLTYPE_4: /* 48Mhz, 4 dividers */
769 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
770 n1 = clkfactor_f6_resolve(n1);
771 n2 += SSB_CHIPCO_CLK_F5_BIAS;
772 break;
773 case SSB_PLLTYPE_2: /* 48Mhz, 4 dividers */
774 n1 += SSB_CHIPCO_CLK_T2_BIAS;
775 n2 += SSB_CHIPCO_CLK_T2_BIAS;
776 SSB_WARN_ON(!((n1 >= 2) && (n1 <= 7)));
777 SSB_WARN_ON(!((n2 >= 5) && (n2 <= 23)));
778 break;
779 case SSB_PLLTYPE_5: /* 25Mhz, 4 dividers */
780 return 100000000;
781 default:
782 SSB_WARN_ON(1);
783 }
784
785 switch (plltype) {
786 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
787 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
788 clock = SSB_CHIPCO_CLK_BASE2 * n1 * n2;
789 break;
790 default:
791 clock = SSB_CHIPCO_CLK_BASE1 * n1 * n2;
792 }
793 if (!clock)
794 return 0;
795
796 m1 = (m & SSB_CHIPCO_CLK_M1);
797 m2 = ((m & SSB_CHIPCO_CLK_M2) >> SSB_CHIPCO_CLK_M2_SHIFT);
798 m3 = ((m & SSB_CHIPCO_CLK_M3) >> SSB_CHIPCO_CLK_M3_SHIFT);
799 mc = ((m & SSB_CHIPCO_CLK_MC) >> SSB_CHIPCO_CLK_MC_SHIFT);
800
801 switch (plltype) {
802 case SSB_PLLTYPE_1: /* 48Mhz base, 3 dividers */
803 case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */
804 case SSB_PLLTYPE_4: /* 48Mhz, 4 dividers */
805 case SSB_PLLTYPE_7: /* 25Mhz, 4 dividers */
806 m1 = clkfactor_f6_resolve(m1);
807 if ((plltype == SSB_PLLTYPE_1) ||
808 (plltype == SSB_PLLTYPE_3))
809 m2 += SSB_CHIPCO_CLK_F5_BIAS;
810 else
811 m2 = clkfactor_f6_resolve(m2);
812 m3 = clkfactor_f6_resolve(m3);
813
814 switch (mc) {
815 case SSB_CHIPCO_CLK_MC_BYPASS:
816 return clock;
817 case SSB_CHIPCO_CLK_MC_M1:
818 return (clock / m1);
819 case SSB_CHIPCO_CLK_MC_M1M2:
820 return (clock / (m1 * m2));
821 case SSB_CHIPCO_CLK_MC_M1M2M3:
822 return (clock / (m1 * m2 * m3));
823 case SSB_CHIPCO_CLK_MC_M1M3:
824 return (clock / (m1 * m3));
825 }
826 return 0;
827 case SSB_PLLTYPE_2:
828 m1 += SSB_CHIPCO_CLK_T2_BIAS;
829 m2 += SSB_CHIPCO_CLK_T2M2_BIAS;
830 m3 += SSB_CHIPCO_CLK_T2_BIAS;
831 SSB_WARN_ON(!((m1 >= 2) && (m1 <= 7)));
832 SSB_WARN_ON(!((m2 >= 3) && (m2 <= 10)));
833 SSB_WARN_ON(!((m3 >= 2) && (m3 <= 7)));
834
835 if (!(mc & SSB_CHIPCO_CLK_T2MC_M1BYP))
836 clock /= m1;
837 if (!(mc & SSB_CHIPCO_CLK_T2MC_M2BYP))
838 clock /= m2;
839 if (!(mc & SSB_CHIPCO_CLK_T2MC_M3BYP))
840 clock /= m3;
841 return clock;
842 default:
843 SSB_WARN_ON(1);
844 }
845 return 0;
846}
847
848/* Get the current speed the backplane is running at */
849u32 ssb_clockspeed(struct ssb_bus *bus)
850{
851 u32 rate;
852 u32 plltype;
853 u32 clkctl_n, clkctl_m;
854
855 if (ssb_extif_available(&bus->extif))
856 ssb_extif_get_clockcontrol(&bus->extif, &plltype,
857 &clkctl_n, &clkctl_m);
858 else if (bus->chipco.dev)
859 ssb_chipco_get_clockcontrol(&bus->chipco, &plltype,
860 &clkctl_n, &clkctl_m);
861 else
862 return 0;
863
864 if (bus->chip_id == 0x5365) {
865 rate = 100000000;
866 } else {
867 rate = ssb_calc_clock_rate(plltype, clkctl_n, clkctl_m);
868 if (plltype == SSB_PLLTYPE_3) /* 25Mhz, 2 dividers */
869 rate /= 2;
870 }
871
872 return rate;
873}
874EXPORT_SYMBOL(ssb_clockspeed);
875
876static u32 ssb_tmslow_reject_bitmask(struct ssb_device *dev)
877{
Larry Fingerc272ef42007-11-09 16:56:25 -0600878 u32 rev = ssb_read32(dev, SSB_IDLOW) & SSB_IDLOW_SSBREV;
879
Michael Buesch61e115a2007-09-18 15:12:50 -0400880 /* The REJECT bit changed position in TMSLOW between
881 * Backplane revisions. */
Larry Fingerc272ef42007-11-09 16:56:25 -0600882 switch (rev) {
Michael Buesch61e115a2007-09-18 15:12:50 -0400883 case SSB_IDLOW_SSBREV_22:
884 return SSB_TMSLOW_REJECT_22;
885 case SSB_IDLOW_SSBREV_23:
886 return SSB_TMSLOW_REJECT_23;
Larry Fingerc272ef42007-11-09 16:56:25 -0600887 case SSB_IDLOW_SSBREV_24: /* TODO - find the proper REJECT bits */
888 case SSB_IDLOW_SSBREV_25: /* same here */
889 case SSB_IDLOW_SSBREV_26: /* same here */
890 case SSB_IDLOW_SSBREV_27: /* same here */
891 return SSB_TMSLOW_REJECT_23; /* this is a guess */
Michael Buesch61e115a2007-09-18 15:12:50 -0400892 default:
Larry Fingerc272ef42007-11-09 16:56:25 -0600893 printk(KERN_INFO "ssb: Backplane Revision 0x%.8X\n", rev);
Michael Buesch61e115a2007-09-18 15:12:50 -0400894 WARN_ON(1);
895 }
896 return (SSB_TMSLOW_REJECT_22 | SSB_TMSLOW_REJECT_23);
897}
898
899int ssb_device_is_enabled(struct ssb_device *dev)
900{
901 u32 val;
902 u32 reject;
903
904 reject = ssb_tmslow_reject_bitmask(dev);
905 val = ssb_read32(dev, SSB_TMSLOW);
906 val &= SSB_TMSLOW_CLOCK | SSB_TMSLOW_RESET | reject;
907
908 return (val == SSB_TMSLOW_CLOCK);
909}
910EXPORT_SYMBOL(ssb_device_is_enabled);
911
912static void ssb_flush_tmslow(struct ssb_device *dev)
913{
914 /* Make _really_ sure the device has finished the TMSLOW
915 * register write transaction, as we risk running into
916 * a machine check exception otherwise.
917 * Do this by reading the register back to commit the
918 * PCI write and delay an additional usec for the device
919 * to react to the change. */
920 ssb_read32(dev, SSB_TMSLOW);
921 udelay(1);
922}
923
924void ssb_device_enable(struct ssb_device *dev, u32 core_specific_flags)
925{
926 u32 val;
927
928 ssb_device_disable(dev, core_specific_flags);
929 ssb_write32(dev, SSB_TMSLOW,
930 SSB_TMSLOW_RESET | SSB_TMSLOW_CLOCK |
931 SSB_TMSLOW_FGC | core_specific_flags);
932 ssb_flush_tmslow(dev);
933
934 /* Clear SERR if set. This is a hw bug workaround. */
935 if (ssb_read32(dev, SSB_TMSHIGH) & SSB_TMSHIGH_SERR)
936 ssb_write32(dev, SSB_TMSHIGH, 0);
937
938 val = ssb_read32(dev, SSB_IMSTATE);
939 if (val & (SSB_IMSTATE_IBE | SSB_IMSTATE_TO)) {
940 val &= ~(SSB_IMSTATE_IBE | SSB_IMSTATE_TO);
941 ssb_write32(dev, SSB_IMSTATE, val);
942 }
943
944 ssb_write32(dev, SSB_TMSLOW,
945 SSB_TMSLOW_CLOCK | SSB_TMSLOW_FGC |
946 core_specific_flags);
947 ssb_flush_tmslow(dev);
948
949 ssb_write32(dev, SSB_TMSLOW, SSB_TMSLOW_CLOCK |
950 core_specific_flags);
951 ssb_flush_tmslow(dev);
952}
953EXPORT_SYMBOL(ssb_device_enable);
954
955/* Wait for a bit in a register to get set or unset.
956 * timeout is in units of ten-microseconds */
957static int ssb_wait_bit(struct ssb_device *dev, u16 reg, u32 bitmask,
958 int timeout, int set)
959{
960 int i;
961 u32 val;
962
963 for (i = 0; i < timeout; i++) {
964 val = ssb_read32(dev, reg);
965 if (set) {
966 if (val & bitmask)
967 return 0;
968 } else {
969 if (!(val & bitmask))
970 return 0;
971 }
972 udelay(10);
973 }
974 printk(KERN_ERR PFX "Timeout waiting for bitmask %08X on "
975 "register %04X to %s.\n",
976 bitmask, reg, (set ? "set" : "clear"));
977
978 return -ETIMEDOUT;
979}
980
981void ssb_device_disable(struct ssb_device *dev, u32 core_specific_flags)
982{
983 u32 reject;
984
985 if (ssb_read32(dev, SSB_TMSLOW) & SSB_TMSLOW_RESET)
986 return;
987
988 reject = ssb_tmslow_reject_bitmask(dev);
989 ssb_write32(dev, SSB_TMSLOW, reject | SSB_TMSLOW_CLOCK);
990 ssb_wait_bit(dev, SSB_TMSLOW, reject, 1000, 1);
991 ssb_wait_bit(dev, SSB_TMSHIGH, SSB_TMSHIGH_BUSY, 1000, 0);
992 ssb_write32(dev, SSB_TMSLOW,
993 SSB_TMSLOW_FGC | SSB_TMSLOW_CLOCK |
994 reject | SSB_TMSLOW_RESET |
995 core_specific_flags);
996 ssb_flush_tmslow(dev);
997
998 ssb_write32(dev, SSB_TMSLOW,
999 reject | SSB_TMSLOW_RESET |
1000 core_specific_flags);
1001 ssb_flush_tmslow(dev);
1002}
1003EXPORT_SYMBOL(ssb_device_disable);
1004
1005u32 ssb_dma_translation(struct ssb_device *dev)
1006{
1007 switch (dev->bus->bustype) {
1008 case SSB_BUSTYPE_SSB:
1009 return 0;
1010 case SSB_BUSTYPE_PCI:
1011 case SSB_BUSTYPE_PCMCIA:
1012 return SSB_PCI_DMA;
1013 }
1014 return 0;
1015}
1016EXPORT_SYMBOL(ssb_dma_translation);
1017
1018int ssb_dma_set_mask(struct ssb_device *ssb_dev, u64 mask)
1019{
1020 struct device *dev = ssb_dev->dev;
1021
1022#ifdef CONFIG_SSB_PCIHOST
1023 if (ssb_dev->bus->bustype == SSB_BUSTYPE_PCI &&
1024 !dma_supported(dev, mask))
1025 return -EIO;
1026#endif
1027 dev->coherent_dma_mask = mask;
1028 dev->dma_mask = &dev->coherent_dma_mask;
1029
1030 return 0;
1031}
1032EXPORT_SYMBOL(ssb_dma_set_mask);
1033
1034int ssb_bus_may_powerdown(struct ssb_bus *bus)
1035{
1036 struct ssb_chipcommon *cc;
1037 int err = 0;
1038
1039 /* On buses where more than one core may be working
1040 * at a time, we must not powerdown stuff if there are
1041 * still cores that may want to run. */
1042 if (bus->bustype == SSB_BUSTYPE_SSB)
1043 goto out;
1044
1045 cc = &bus->chipco;
1046 ssb_chipco_set_clockmode(cc, SSB_CLKMODE_SLOW);
1047 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 0);
1048 if (err)
1049 goto error;
1050out:
1051#ifdef CONFIG_SSB_DEBUG
1052 bus->powered_up = 0;
1053#endif
1054 return err;
1055error:
1056 ssb_printk(KERN_ERR PFX "Bus powerdown failed\n");
1057 goto out;
1058}
1059EXPORT_SYMBOL(ssb_bus_may_powerdown);
1060
1061int ssb_bus_powerup(struct ssb_bus *bus, bool dynamic_pctl)
1062{
1063 struct ssb_chipcommon *cc;
1064 int err;
1065 enum ssb_clkmode mode;
1066
1067 err = ssb_pci_xtal(bus, SSB_GPIO_XTAL | SSB_GPIO_PLL, 1);
1068 if (err)
1069 goto error;
1070 cc = &bus->chipco;
1071 mode = dynamic_pctl ? SSB_CLKMODE_DYNAMIC : SSB_CLKMODE_FAST;
1072 ssb_chipco_set_clockmode(cc, mode);
1073
1074#ifdef CONFIG_SSB_DEBUG
1075 bus->powered_up = 1;
1076#endif
1077 return 0;
1078error:
1079 ssb_printk(KERN_ERR PFX "Bus powerup failed\n");
1080 return err;
1081}
1082EXPORT_SYMBOL(ssb_bus_powerup);
1083
1084u32 ssb_admatch_base(u32 adm)
1085{
1086 u32 base = 0;
1087
1088 switch (adm & SSB_ADM_TYPE) {
1089 case SSB_ADM_TYPE0:
1090 base = (adm & SSB_ADM_BASE0);
1091 break;
1092 case SSB_ADM_TYPE1:
1093 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1094 base = (adm & SSB_ADM_BASE1);
1095 break;
1096 case SSB_ADM_TYPE2:
1097 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1098 base = (adm & SSB_ADM_BASE2);
1099 break;
1100 default:
1101 SSB_WARN_ON(1);
1102 }
1103
1104 return base;
1105}
1106EXPORT_SYMBOL(ssb_admatch_base);
1107
1108u32 ssb_admatch_size(u32 adm)
1109{
1110 u32 size = 0;
1111
1112 switch (adm & SSB_ADM_TYPE) {
1113 case SSB_ADM_TYPE0:
1114 size = ((adm & SSB_ADM_SZ0) >> SSB_ADM_SZ0_SHIFT);
1115 break;
1116 case SSB_ADM_TYPE1:
1117 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1118 size = ((adm & SSB_ADM_SZ1) >> SSB_ADM_SZ1_SHIFT);
1119 break;
1120 case SSB_ADM_TYPE2:
1121 SSB_WARN_ON(adm & SSB_ADM_NEG); /* unsupported */
1122 size = ((adm & SSB_ADM_SZ2) >> SSB_ADM_SZ2_SHIFT);
1123 break;
1124 default:
1125 SSB_WARN_ON(1);
1126 }
1127 size = (1 << (size + 1));
1128
1129 return size;
1130}
1131EXPORT_SYMBOL(ssb_admatch_size);
1132
1133static int __init ssb_modinit(void)
1134{
1135 int err;
1136
1137 /* See the comment at the ssb_is_early_boot definition */
1138 ssb_is_early_boot = 0;
1139 err = bus_register(&ssb_bustype);
1140 if (err)
1141 return err;
1142
1143 /* Maybe we already registered some buses at early boot.
1144 * Check for this and attach them
1145 */
1146 ssb_buses_lock();
1147 err = ssb_attach_queued_buses();
1148 ssb_buses_unlock();
1149 if (err)
1150 bus_unregister(&ssb_bustype);
1151
1152 err = b43_pci_ssb_bridge_init();
1153 if (err) {
1154 ssb_printk(KERN_ERR "Broadcom 43xx PCI-SSB-bridge "
1155 "initialization failed");
1156 /* don't fail SSB init because of this */
1157 err = 0;
1158 }
1159
1160 return err;
1161}
Michael Buesch8d8c90e2007-10-27 15:14:39 +02001162/* ssb must be initialized after PCI but before the ssb drivers.
1163 * That means we must use some initcall between subsys_initcall
1164 * and device_initcall. */
1165fs_initcall(ssb_modinit);
Michael Buesch61e115a2007-09-18 15:12:50 -04001166
1167static void __exit ssb_modexit(void)
1168{
1169 b43_pci_ssb_bridge_exit();
1170 bus_unregister(&ssb_bustype);
1171}
1172module_exit(ssb_modexit)