]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/macintosh/macio_asic.c
[PATCH] fix missing includes
[mirror_ubuntu-bionic-kernel.git] / drivers / macintosh / macio_asic.c
CommitLineData
1da177e4
LT
1/*
2 * Bus & driver management routines for devices within
3 * a MacIO ASIC. Interface to new driver model mostly
4 * stolen from the PCI version.
5 *
6 * TODO:
7 *
8 * - Don't probe below media bay by default, but instead provide
9 * some hooks for media bay to dynamically add/remove it's own
10 * sub-devices.
11 */
12
13#include <linux/config.h>
14#include <linux/string.h>
15#include <linux/kernel.h>
16#include <linux/pci.h>
17#include <linux/pci_ids.h>
18#include <linux/init.h>
19#include <linux/module.h>
4e57b681
TS
20#include <linux/slab.h>
21
1da177e4
LT
22#include <asm/machdep.h>
23#include <asm/macio.h>
24#include <asm/pmac_feature.h>
25#include <asm/prom.h>
26#include <asm/pci-bridge.h>
27
28#undef DEBUG
29
30#define MAX_NODE_NAME_SIZE (BUS_ID_SIZE - 12)
31
32static struct macio_chip *macio_on_hold;
33
34static int macio_bus_match(struct device *dev, struct device_driver *drv)
35{
36 struct macio_dev * macio_dev = to_macio_device(dev);
37 struct macio_driver * macio_drv = to_macio_driver(drv);
5e655772 38 const struct of_device_id * matches = macio_drv->match_table;
1da177e4
LT
39
40 if (!matches)
41 return 0;
42
43 return of_match_device(matches, &macio_dev->ofdev) != NULL;
44}
45
46struct macio_dev *macio_dev_get(struct macio_dev *dev)
47{
48 struct device *tmp;
49
50 if (!dev)
51 return NULL;
52 tmp = get_device(&dev->ofdev.dev);
53 if (tmp)
54 return to_macio_device(tmp);
55 else
56 return NULL;
57}
58
59void macio_dev_put(struct macio_dev *dev)
60{
61 if (dev)
62 put_device(&dev->ofdev.dev);
63}
64
65
66static int macio_device_probe(struct device *dev)
67{
68 int error = -ENODEV;
69 struct macio_driver *drv;
70 struct macio_dev *macio_dev;
5e655772 71 const struct of_device_id *match;
1da177e4
LT
72
73 drv = to_macio_driver(dev->driver);
74 macio_dev = to_macio_device(dev);
75
76 if (!drv->probe)
77 return error;
78
79 macio_dev_get(macio_dev);
80
81 match = of_match_device(drv->match_table, &macio_dev->ofdev);
82 if (match)
83 error = drv->probe(macio_dev, match);
84 if (error)
85 macio_dev_put(macio_dev);
86
87 return error;
88}
89
90static int macio_device_remove(struct device *dev)
91{
92 struct macio_dev * macio_dev = to_macio_device(dev);
93 struct macio_driver * drv = to_macio_driver(dev->driver);
94
95 if (dev->driver && drv->remove)
96 drv->remove(macio_dev);
97 macio_dev_put(macio_dev);
98
99 return 0;
100}
101
102static void macio_device_shutdown(struct device *dev)
103{
104 struct macio_dev * macio_dev = to_macio_device(dev);
105 struct macio_driver * drv = to_macio_driver(dev->driver);
106
107 if (dev->driver && drv->shutdown)
108 drv->shutdown(macio_dev);
109}
110
f4513904 111static int macio_device_suspend(struct device *dev, pm_message_t state)
1da177e4
LT
112{
113 struct macio_dev * macio_dev = to_macio_device(dev);
114 struct macio_driver * drv = to_macio_driver(dev->driver);
115
116 if (dev->driver && drv->suspend)
117 return drv->suspend(macio_dev, state);
118 return 0;
119}
120
121static int macio_device_resume(struct device * dev)
122{
123 struct macio_dev * macio_dev = to_macio_device(dev);
124 struct macio_driver * drv = to_macio_driver(dev->driver);
125
126 if (dev->driver && drv->resume)
127 return drv->resume(macio_dev);
128 return 0;
129}
130
184f6eb8
JM
131static int macio_hotplug (struct device *dev, char **envp, int num_envp,
132 char *buffer, int buffer_size)
133{
134 struct macio_dev * macio_dev;
135 struct of_device * of;
136 char *scratch, *compat;
137 int i = 0;
138 int length = 0;
139 int cplen, seen = 0;
140
141 if (!dev)
142 return -ENODEV;
143
144 macio_dev = to_macio_device(dev);
145 if (!macio_dev)
146 return -ENODEV;
147
148 of = &macio_dev->ofdev;
149 scratch = buffer;
150
151 /* stuff we want to pass to /sbin/hotplug */
152 envp[i++] = scratch;
153 length += scnprintf (scratch, buffer_size - length, "OF_NAME=%s",
154 of->node->name);
155 if ((buffer_size - length <= 0) || (i >= num_envp))
156 return -ENOMEM;
157 ++length;
158 scratch += length;
159
160 envp[i++] = scratch;
161 length += scnprintf (scratch, buffer_size - length, "OF_TYPE=%s",
162 of->node->type);
163 if ((buffer_size - length <= 0) || (i >= num_envp))
164 return -ENOMEM;
165 ++length;
166 scratch += length;
167
168 /* Since the compatible field can contain pretty much anything
169 * it's not really legal to split it out with commas. We split it
170 * up using a number of environment variables instead. */
171
172 compat = (char *) get_property(of->node, "compatible", &cplen);
173 while (compat && cplen > 0) {
174 int l;
175 envp[i++] = scratch;
176 length += scnprintf (scratch, buffer_size - length,
177 "OF_COMPATIBLE_%d=%s", seen, compat);
178 if ((buffer_size - length <= 0) || (i >= num_envp))
179 return -ENOMEM;
180 length++;
181 scratch += length;
182 l = strlen (compat) + 1;
183 compat += l;
184 cplen -= l;
185 seen++;
186 }
187
188 envp[i++] = scratch;
189 length += scnprintf (scratch, buffer_size - length,
190 "OF_COMPATIBLE_N=%d", seen);
191 if ((buffer_size - length <= 0) || (i >= num_envp))
192 return -ENOMEM;
193 ++length;
194 scratch += length;
195
196 envp[i] = NULL;
197
198 return 0;
199}
200
b5bf5b67
JM
201extern struct device_attribute macio_dev_attrs[];
202
1da177e4
LT
203struct bus_type macio_bus_type = {
204 .name = "macio",
205 .match = macio_bus_match,
184f6eb8 206 .hotplug = macio_hotplug,
1da177e4
LT
207 .suspend = macio_device_suspend,
208 .resume = macio_device_resume,
b5bf5b67 209 .dev_attrs = macio_dev_attrs,
1da177e4
LT
210};
211
212static int __init macio_bus_driver_init(void)
213{
214 return bus_register(&macio_bus_type);
215}
216
217postcore_initcall(macio_bus_driver_init);
218
219
220/**
221 * macio_release_dev - free a macio device structure when all users of it are finished.
222 * @dev: device that's been disconnected
223 *
224 * Will be called only by the device core when all users of this macio device are
225 * done. This currently means never as we don't hot remove any macio device yet,
226 * though that will happen with mediabay based devices in a later implementation.
227 */
228static void macio_release_dev(struct device *dev)
229{
230 struct macio_dev *mdev;
231
232 mdev = to_macio_device(dev);
233 kfree(mdev);
234}
235
236/**
237 * macio_resource_quirks - tweak or skip some resources for a device
238 * @np: pointer to the device node
239 * @res: resulting resource
240 * @index: index of resource in node
241 *
242 * If this routine returns non-null, then the resource is completely
243 * skipped.
244 */
245static int macio_resource_quirks(struct device_node *np, struct resource *res, int index)
246{
247 if (res->flags & IORESOURCE_MEM) {
248 /* Grand Central has too large resource 0 on some machines */
249 if (index == 0 && !strcmp(np->name, "gc")) {
250 np->addrs[0].size = 0x20000;
251 res->end = res->start + 0x1ffff;
252 }
253 /* Airport has bogus resource 2 */
254 if (index >= 2 && !strcmp(np->name, "radio"))
255 return 1;
256 /* DBDMAs may have bogus sizes */
257 if ((res->start & 0x0001f000) == 0x00008000) {
258 np->addrs[index].size = 0x100;
259 res->end = res->start + 0xff;
260 }
261 /* ESCC parent eats child resources. We could have added a level of hierarchy,
262 * but I don't really feel the need for it */
263 if (!strcmp(np->name, "escc"))
264 return 1;
265 /* ESCC has bogus resources >= 3 */
266 if (index >= 3 && !(strcmp(np->name, "ch-a") && strcmp(np->name, "ch-b")))
267 return 1;
268 /* Media bay has too many resources, keep only first one */
269 if (index > 0 && !strcmp(np->name, "media-bay"))
270 return 1;
271 /* Some older IDE resources have bogus sizes */
272 if (!(strcmp(np->name, "IDE") && strcmp(np->name, "ATA") &&
273 strcmp(np->type, "ide") && strcmp(np->type, "ata"))) {
274 if (index == 0 && np->addrs[0].size > 0x1000) {
275 np->addrs[0].size = 0x1000;
276 res->end = res->start + 0xfff;
277 }
278 if (index == 1 && np->addrs[1].size > 0x100) {
279 np->addrs[1].size = 0x100;
280 res->end = res->start + 0xff;
281 }
282 }
283 }
284 return 0;
285}
286
287
288/**
289 * macio_add_one_device - Add one device from OF node to the device tree
290 * @chip: pointer to the macio_chip holding the device
291 * @np: pointer to the device node in the OF tree
292 * @in_bay: set to 1 if device is part of a media-bay
293 *
294 * When media-bay is changed to hotswap drivers, this function will
295 * be exposed to the bay driver some way...
296 */
297static struct macio_dev * macio_add_one_device(struct macio_chip *chip, struct device *parent,
298 struct device_node *np, struct macio_dev *in_bay,
299 struct resource *parent_res)
300{
301 struct macio_dev *dev;
302 int i, j;
303 u32 *reg;
304
305 if (np == NULL)
306 return NULL;
307
308 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
309 if (!dev)
310 return NULL;
311 memset(dev, 0, sizeof(*dev));
312
313 dev->bus = &chip->lbus;
314 dev->media_bay = in_bay;
315 dev->ofdev.node = np;
316 dev->ofdev.dma_mask = 0xffffffffUL;
317 dev->ofdev.dev.dma_mask = &dev->ofdev.dma_mask;
318 dev->ofdev.dev.parent = parent;
319 dev->ofdev.dev.bus = &macio_bus_type;
320 dev->ofdev.dev.release = macio_release_dev;
321
322#ifdef DEBUG
323 printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
324 dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj);
325#endif
326
327 /* MacIO itself has a different reg, we use it's PCI base */
328 if (np == chip->of_node) {
329 sprintf(dev->ofdev.dev.bus_id, "%1d.%08lx:%.*s", chip->lbus.index,
330#ifdef CONFIG_PCI
331 pci_resource_start(chip->lbus.pdev, 0),
332#else
333 0, /* NuBus may want to do something better here */
334#endif
335 MAX_NODE_NAME_SIZE, np->name);
336 } else {
337 reg = (u32 *)get_property(np, "reg", NULL);
338 sprintf(dev->ofdev.dev.bus_id, "%1d.%08x:%.*s", chip->lbus.index,
339 reg ? *reg : 0, MAX_NODE_NAME_SIZE, np->name);
340 }
341
342 /* For now, we use pre-parsed entries in the device-tree for
343 * interrupt routing and addresses, but we should change that
344 * to dynamically parsed entries and so get rid of most of the
345 * clutter in struct device_node
346 */
347 for (i = j = 0; i < np->n_intrs; i++) {
348 struct resource *res = &dev->interrupt[j];
349
350 if (j >= MACIO_DEV_COUNT_IRQS)
351 break;
352 res->start = np->intrs[i].line;
353 res->flags = IORESOURCE_IO;
354 if (np->intrs[j].sense)
355 res->flags |= IORESOURCE_IRQ_LOWLEVEL;
356 else
357 res->flags |= IORESOURCE_IRQ_HIGHEDGE;
358 res->name = dev->ofdev.dev.bus_id;
359 if (macio_resource_quirks(np, res, i))
360 memset(res, 0, sizeof(struct resource));
361 else
362 j++;
363 }
364 dev->n_interrupts = j;
365 for (i = j = 0; i < np->n_addrs; i++) {
366 struct resource *res = &dev->resource[j];
367
368 if (j >= MACIO_DEV_COUNT_RESOURCES)
369 break;
370 res->start = np->addrs[i].address;
371 res->end = np->addrs[i].address + np->addrs[i].size - 1;
372 res->flags = IORESOURCE_MEM;
373 res->name = dev->ofdev.dev.bus_id;
374 if (macio_resource_quirks(np, res, i))
375 memset(res, 0, sizeof(struct resource));
376 else {
377 j++;
378 /* Currently, we consider failure as harmless, this may
379 * change in the future, once I've found all the device
380 * tree bugs in older machines & worked around them
381 */
382 if (insert_resource(parent_res, res))
383 printk(KERN_WARNING "Can't request resource %d for MacIO"
384 " device %s\n", i, dev->ofdev.dev.bus_id);
385 }
386 }
387 dev->n_resources = j;
388
389 if (of_device_register(&dev->ofdev) != 0) {
390 printk(KERN_DEBUG"macio: device registration error for %s!\n",
391 dev->ofdev.dev.bus_id);
392 kfree(dev);
393 return NULL;
394 }
395
396 return dev;
397}
398
399static int macio_skip_device(struct device_node *np)
400{
401 if (strncmp(np->name, "battery", 7) == 0)
402 return 1;
403 if (strncmp(np->name, "escc-legacy", 11) == 0)
404 return 1;
405 return 0;
406}
407
408/**
409 * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree
410 * @chip: pointer to the macio_chip holding the devices
411 *
412 * This function will do the job of extracting devices from the
413 * Open Firmware device tree, build macio_dev structures and add
414 * them to the Linux device tree.
415 *
416 * For now, childs of media-bay are added now as well. This will
417 * change rsn though.
418 */
419static void macio_pci_add_devices(struct macio_chip *chip)
420{
421 struct device_node *np, *pnode;
422 struct macio_dev *rdev, *mdev, *mbdev = NULL, *sdev = NULL;
423 struct device *parent = NULL;
424 struct resource *root_res = &iomem_resource;
425
426 /* Add a node for the macio bus itself */
427#ifdef CONFIG_PCI
428 if (chip->lbus.pdev) {
429 parent = &chip->lbus.pdev->dev;
430 root_res = &chip->lbus.pdev->resource[0];
431 }
432#endif
433 pnode = of_node_get(chip->of_node);
434 if (pnode == NULL)
435 return;
436
437 /* Add macio itself to hierarchy */
438 rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res);
439 if (rdev == NULL)
440 return;
441 root_res = &rdev->resource[0];
442
443 /* First scan 1st level */
444 for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
445 if (!macio_skip_device(np)) {
446 of_node_get(np);
447 mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL, root_res);
448 if (mdev == NULL)
449 of_node_put(np);
450 else if (strncmp(np->name, "media-bay", 9) == 0)
451 mbdev = mdev;
452 else if (strncmp(np->name, "escc", 4) == 0)
453 sdev = mdev;
454 }
455 }
456
457 /* Add media bay devices if any */
458 if (mbdev)
459 for (np = NULL; (np = of_get_next_child(mbdev->ofdev.node, np)) != NULL;)
460 if (!macio_skip_device(np)) {
461 of_node_get(np);
462 if (macio_add_one_device(chip, &mbdev->ofdev.dev, np, mbdev,
463 root_res) == NULL)
464 of_node_put(np);
465 }
466 /* Add serial ports if any */
467 if (sdev) {
468 for (np = NULL; (np = of_get_next_child(sdev->ofdev.node, np)) != NULL;)
469 if (!macio_skip_device(np)) {
470 of_node_get(np);
471 if (macio_add_one_device(chip, &sdev->ofdev.dev, np, NULL,
472 root_res) == NULL)
473 of_node_put(np);
474 }
475 }
476}
477
478
479/**
480 * macio_register_driver - Registers a new MacIO device driver
481 * @drv: pointer to the driver definition structure
482 */
483int macio_register_driver(struct macio_driver *drv)
484{
485 int count = 0;
486
487 /* initialize common driver fields */
488 drv->driver.name = drv->name;
489 drv->driver.bus = &macio_bus_type;
490 drv->driver.probe = macio_device_probe;
491 drv->driver.remove = macio_device_remove;
492 drv->driver.shutdown = macio_device_shutdown;
493
494 /* register with core */
495 count = driver_register(&drv->driver);
496 return count ? count : 1;
497}
498
499/**
500 * macio_unregister_driver - Unregisters a new MacIO device driver
501 * @drv: pointer to the driver definition structure
502 */
503void macio_unregister_driver(struct macio_driver *drv)
504{
505 driver_unregister(&drv->driver);
506}
507
508/**
509 * macio_request_resource - Request an MMIO resource
510 * @dev: pointer to the device holding the resource
511 * @resource_no: resource number to request
512 * @name: resource name
513 *
514 * Mark memory region number @resource_no associated with MacIO
515 * device @dev as being reserved by owner @name. Do not access
516 * any address inside the memory regions unless this call returns
517 * successfully.
518 *
519 * Returns 0 on success, or %EBUSY on error. A warning
520 * message is also printed on failure.
521 */
522int macio_request_resource(struct macio_dev *dev, int resource_no, const char *name)
523{
524 if (macio_resource_len(dev, resource_no) == 0)
525 return 0;
526
527 if (!request_mem_region(macio_resource_start(dev, resource_no),
528 macio_resource_len(dev, resource_no),
529 name))
530 goto err_out;
531
532 return 0;
533
534err_out:
535 printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx"
536 " for device %s\n",
537 resource_no,
538 macio_resource_len(dev, resource_no),
539 macio_resource_start(dev, resource_no),
540 dev->ofdev.dev.bus_id);
541 return -EBUSY;
542}
543
544/**
545 * macio_release_resource - Release an MMIO resource
546 * @dev: pointer to the device holding the resource
547 * @resource_no: resource number to release
548 */
549void macio_release_resource(struct macio_dev *dev, int resource_no)
550{
551 if (macio_resource_len(dev, resource_no) == 0)
552 return;
553 release_mem_region(macio_resource_start(dev, resource_no),
554 macio_resource_len(dev, resource_no));
555}
556
557/**
558 * macio_request_resources - Reserve all memory resources
559 * @dev: MacIO device whose resources are to be reserved
560 * @name: Name to be associated with resource.
561 *
562 * Mark all memory regions associated with MacIO device @dev as
563 * being reserved by owner @name. Do not access any address inside
564 * the memory regions unless this call returns successfully.
565 *
566 * Returns 0 on success, or %EBUSY on error. A warning
567 * message is also printed on failure.
568 */
569int macio_request_resources(struct macio_dev *dev, const char *name)
570{
571 int i;
572
573 for (i = 0; i < dev->n_resources; i++)
574 if (macio_request_resource(dev, i, name))
575 goto err_out;
576 return 0;
577
578err_out:
579 while(--i >= 0)
580 macio_release_resource(dev, i);
581
582 return -EBUSY;
583}
584
585/**
586 * macio_release_resources - Release reserved memory resources
587 * @dev: MacIO device whose resources were previously reserved
588 */
589
590void macio_release_resources(struct macio_dev *dev)
591{
592 int i;
593
594 for (i = 0; i < dev->n_resources; i++)
595 macio_release_resource(dev, i);
596}
597
598
599#ifdef CONFIG_PCI
600
601static int __devinit macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
602{
603 struct device_node* np;
604 struct macio_chip* chip;
605
606 if (ent->vendor != PCI_VENDOR_ID_APPLE)
607 return -ENODEV;
608
609 /* Note regarding refcounting: We assume pci_device_to_OF_node() is ported
610 * to new OF APIs and returns a node with refcount incremented. This isn't
611 * the case today, but on the other hand ppc32 doesn't do refcounting. This
612 * will have to be fixed when going to ppc64. --BenH.
613 */
614 np = pci_device_to_OF_node(pdev);
615 if (np == NULL)
616 return -ENODEV;
617
618 /* This assumption is wrong, fix that here for now until I fix the arch */
619 of_node_get(np);
620
621 /* We also assume that pmac_feature will have done a get() on nodes stored
622 * in the macio chips array
623 */
624 chip = macio_find(np, macio_unknown);
625 of_node_put(np);
626 if (chip == NULL)
627 return -ENODEV;
628
629 /* XXX Need locking ??? */
630 if (chip->lbus.pdev == NULL) {
631 chip->lbus.pdev = pdev;
632 chip->lbus.chip = chip;
633 pci_set_drvdata(pdev, &chip->lbus);
634 pci_set_master(pdev);
635 }
636
637 printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n",
638 chip->name);
639
640 /*
641 * HACK ALERT: The WallStreet PowerBook and some OHare based machines
642 * have 2 macio ASICs. I must probe the "main" one first or IDE ordering
643 * will be incorrect. So I put on "hold" the second one since it seem to
644 * appear first on PCI
645 */
646 if (chip->type == macio_gatwick || chip->type == macio_ohareII)
647 if (macio_chips[0].lbus.pdev == NULL) {
648 macio_on_hold = chip;
649 return 0;
650 }
651
652 macio_pci_add_devices(chip);
653 if (macio_on_hold && macio_chips[0].lbus.pdev != NULL) {
654 macio_pci_add_devices(macio_on_hold);
655 macio_on_hold = NULL;
656 }
657
658 return 0;
659}
660
661static void __devexit macio_pci_remove(struct pci_dev* pdev)
662{
663 panic("removing of macio-asic not supported !\n");
664}
665
666/*
667 * MacIO is matched against any Apple ID, it's probe() function
668 * will then decide wether it applies or not
669 */
670static const struct pci_device_id __devinitdata pci_ids [] = { {
671 .vendor = PCI_VENDOR_ID_APPLE,
672 .device = PCI_ANY_ID,
673 .subvendor = PCI_ANY_ID,
674 .subdevice = PCI_ANY_ID,
675
676 }, { /* end: all zeroes */ }
677};
678MODULE_DEVICE_TABLE (pci, pci_ids);
679
680/* pci driver glue; this is a "new style" PCI driver module */
681static struct pci_driver macio_pci_driver = {
682 .name = (char *) "macio",
683 .id_table = pci_ids,
684
685 .probe = macio_pci_probe,
686 .remove = macio_pci_remove,
687};
688
689#endif /* CONFIG_PCI */
690
691static int __init macio_module_init (void)
692{
693#ifdef CONFIG_PCI
694 int rc;
695
696 rc = pci_register_driver(&macio_pci_driver);
697 if (rc)
698 return rc;
699#endif /* CONFIG_PCI */
700 return 0;
701}
702
703module_init(macio_module_init);
704
705EXPORT_SYMBOL(macio_register_driver);
706EXPORT_SYMBOL(macio_unregister_driver);
707EXPORT_SYMBOL(macio_dev_get);
708EXPORT_SYMBOL(macio_dev_put);
709EXPORT_SYMBOL(macio_request_resource);
710EXPORT_SYMBOL(macio_release_resource);
711EXPORT_SYMBOL(macio_request_resources);
712EXPORT_SYMBOL(macio_release_resources);