blob: d56216067549397e1c88041e3697e5f8349d425c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +11006 * Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * TODO:
14 *
15 * - Don't probe below media bay by default, but instead provide
16 * some hooks for media bay to dynamically add/remove it's own
17 * sub-devices.
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/string.h>
21#include <linux/kernel.h>
22#include <linux/pci.h>
23#include <linux/pci_ids.h>
24#include <linux/init.h>
25#include <linux/module.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080026#include <linux/slab.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/machdep.h>
29#include <asm/macio.h>
30#include <asm/pmac_feature.h>
31#include <asm/prom.h>
32#include <asm/pci-bridge.h>
33
34#undef DEBUG
35
36#define MAX_NODE_NAME_SIZE (BUS_ID_SIZE - 12)
37
38static struct macio_chip *macio_on_hold;
39
40static int macio_bus_match(struct device *dev, struct device_driver *drv)
41{
42 struct macio_dev * macio_dev = to_macio_device(dev);
43 struct macio_driver * macio_drv = to_macio_driver(drv);
Jeff Mahoney5e655772005-07-06 15:44:41 -040044 const struct of_device_id * matches = macio_drv->match_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 if (!matches)
47 return 0;
48
49 return of_match_device(matches, &macio_dev->ofdev) != NULL;
50}
51
52struct macio_dev *macio_dev_get(struct macio_dev *dev)
53{
54 struct device *tmp;
55
56 if (!dev)
57 return NULL;
58 tmp = get_device(&dev->ofdev.dev);
59 if (tmp)
60 return to_macio_device(tmp);
61 else
62 return NULL;
63}
64
65void macio_dev_put(struct macio_dev *dev)
66{
67 if (dev)
68 put_device(&dev->ofdev.dev);
69}
70
71
72static int macio_device_probe(struct device *dev)
73{
74 int error = -ENODEV;
75 struct macio_driver *drv;
76 struct macio_dev *macio_dev;
Jeff Mahoney5e655772005-07-06 15:44:41 -040077 const struct of_device_id *match;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 drv = to_macio_driver(dev->driver);
80 macio_dev = to_macio_device(dev);
81
82 if (!drv->probe)
83 return error;
84
85 macio_dev_get(macio_dev);
86
87 match = of_match_device(drv->match_table, &macio_dev->ofdev);
88 if (match)
89 error = drv->probe(macio_dev, match);
90 if (error)
91 macio_dev_put(macio_dev);
92
93 return error;
94}
95
96static int macio_device_remove(struct device *dev)
97{
98 struct macio_dev * macio_dev = to_macio_device(dev);
99 struct macio_driver * drv = to_macio_driver(dev->driver);
100
101 if (dev->driver && drv->remove)
102 drv->remove(macio_dev);
103 macio_dev_put(macio_dev);
104
105 return 0;
106}
107
108static void macio_device_shutdown(struct device *dev)
109{
110 struct macio_dev * macio_dev = to_macio_device(dev);
111 struct macio_driver * drv = to_macio_driver(dev->driver);
112
113 if (dev->driver && drv->shutdown)
114 drv->shutdown(macio_dev);
115}
116
Pavel Machekf4513902005-04-16 15:25:32 -0700117static int macio_device_suspend(struct device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct macio_dev * macio_dev = to_macio_device(dev);
120 struct macio_driver * drv = to_macio_driver(dev->driver);
121
122 if (dev->driver && drv->suspend)
123 return drv->suspend(macio_dev, state);
124 return 0;
125}
126
127static int macio_device_resume(struct device * dev)
128{
129 struct macio_dev * macio_dev = to_macio_device(dev);
130 struct macio_driver * drv = to_macio_driver(dev->driver);
131
132 if (dev->driver && drv->resume)
133 return drv->resume(macio_dev);
134 return 0;
135}
136
Kay Sievers312c0042005-11-16 09:00:00 +0100137static int macio_uevent(struct device *dev, char **envp, int num_envp,
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400138 char *buffer, int buffer_size)
139{
140 struct macio_dev * macio_dev;
141 struct of_device * of;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000142 char *scratch;
143 const char *compat, *compat2;
144
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400145 int i = 0;
Olaf Heringe6199782006-02-01 03:05:58 -0800146 int length, cplen, cplen2, seen = 0;
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400147
148 if (!dev)
149 return -ENODEV;
150
151 macio_dev = to_macio_device(dev);
152 if (!macio_dev)
153 return -ENODEV;
154
155 of = &macio_dev->ofdev;
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400156
157 /* stuff we want to pass to /sbin/hotplug */
Olaf Heringe6199782006-02-01 03:05:58 -0800158 envp[i++] = scratch = buffer;
159 length = scnprintf (scratch, buffer_size, "OF_NAME=%s", of->node->name);
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400160 ++length;
Olaf Heringe6199782006-02-01 03:05:58 -0800161 buffer_size -= length;
162 if ((buffer_size <= 0) || (i >= num_envp))
163 return -ENOMEM;
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400164 scratch += length;
165
166 envp[i++] = scratch;
Olaf Heringe6199782006-02-01 03:05:58 -0800167 length = scnprintf (scratch, buffer_size, "OF_TYPE=%s", of->node->type);
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400168 ++length;
Olaf Heringe6199782006-02-01 03:05:58 -0800169 buffer_size -= length;
170 if ((buffer_size <= 0) || (i >= num_envp))
171 return -ENOMEM;
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400172 scratch += length;
173
174 /* Since the compatible field can contain pretty much anything
175 * it's not really legal to split it out with commas. We split it
176 * up using a number of environment variables instead. */
177
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000178 compat = get_property(of->node, "compatible", &cplen);
Olaf Heringe6199782006-02-01 03:05:58 -0800179 compat2 = compat;
180 cplen2= cplen;
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400181 while (compat && cplen > 0) {
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400182 envp[i++] = scratch;
Olaf Heringe6199782006-02-01 03:05:58 -0800183 length = scnprintf (scratch, buffer_size,
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400184 "OF_COMPATIBLE_%d=%s", seen, compat);
Olaf Heringe6199782006-02-01 03:05:58 -0800185 ++length;
186 buffer_size -= length;
187 if ((buffer_size <= 0) || (i >= num_envp))
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400188 return -ENOMEM;
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400189 scratch += length;
Olaf Heringe6199782006-02-01 03:05:58 -0800190 length = strlen (compat) + 1;
191 compat += length;
192 cplen -= length;
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400193 seen++;
194 }
195
196 envp[i++] = scratch;
Olaf Heringe6199782006-02-01 03:05:58 -0800197 length = scnprintf (scratch, buffer_size, "OF_COMPATIBLE_N=%d", seen);
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400198 ++length;
Olaf Heringe6199782006-02-01 03:05:58 -0800199 buffer_size -= length;
200 if ((buffer_size <= 0) || (i >= num_envp))
201 return -ENOMEM;
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400202 scratch += length;
203
Olaf Heringe6199782006-02-01 03:05:58 -0800204 envp[i++] = scratch;
205 length = scnprintf (scratch, buffer_size, "MODALIAS=of:N%sT%s",
206 of->node->name, of->node->type);
207 /* overwrite '\0' */
208 buffer_size -= length;
209 if ((buffer_size <= 0) || (i >= num_envp))
210 return -ENOMEM;
211 scratch += length;
212
213 if (!compat2) {
214 compat2 = "";
215 cplen2 = 1;
216 }
217 while (cplen2 > 0) {
218 length = snprintf (scratch, buffer_size, "C%s", compat2);
219 buffer_size -= length;
220 if (buffer_size <= 0)
221 return -ENOMEM;
222 scratch += length;
223 length = strlen (compat2) + 1;
224 compat2 += length;
225 cplen2 -= length;
226 }
227
Jeff Mahoney184f6eb2005-07-06 15:45:09 -0400228 envp[i] = NULL;
229
230 return 0;
231}
232
Jeff Mahoneyb5bf5b62005-07-06 15:26:27 -0400233extern struct device_attribute macio_dev_attrs[];
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235struct bus_type macio_bus_type = {
236 .name = "macio",
237 .match = macio_bus_match,
Kay Sievers312c0042005-11-16 09:00:00 +0100238 .uevent = macio_uevent,
Russell King4866b122006-01-05 14:39:24 +0000239 .probe = macio_device_probe,
240 .remove = macio_device_remove,
241 .shutdown = macio_device_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .suspend = macio_device_suspend,
243 .resume = macio_device_resume,
Jeff Mahoneyb5bf5b62005-07-06 15:26:27 -0400244 .dev_attrs = macio_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
247static int __init macio_bus_driver_init(void)
248{
249 return bus_register(&macio_bus_type);
250}
251
252postcore_initcall(macio_bus_driver_init);
253
254
255/**
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100256 * macio_release_dev - free a macio device structure when all users of it are
257 * finished.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * @dev: device that's been disconnected
259 *
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100260 * Will be called only by the device core when all users of this macio device
261 * are done. This currently means never as we don't hot remove any macio
262 * device yet, though that will happen with mediabay based devices in a later
263 * implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
265static void macio_release_dev(struct device *dev)
266{
267 struct macio_dev *mdev;
268
269 mdev = to_macio_device(dev);
270 kfree(mdev);
271}
272
273/**
274 * macio_resource_quirks - tweak or skip some resources for a device
275 * @np: pointer to the device node
276 * @res: resulting resource
277 * @index: index of resource in node
278 *
279 * If this routine returns non-null, then the resource is completely
280 * skipped.
281 */
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100282static int macio_resource_quirks(struct device_node *np, struct resource *res,
283 int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000285 /* Only quirks for memory resources for now */
286 if ((res->flags & IORESOURCE_MEM) == 0)
287 return 0;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100288
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000289 /* Grand Central has too large resource 0 on some machines */
290 if (index == 0 && !strcmp(np->name, "gc"))
291 res->end = res->start + 0x1ffff;
292
293 /* Airport has bogus resource 2 */
294 if (index >= 2 && !strcmp(np->name, "radio"))
295 return 1;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100296
297#ifndef CONFIG_PPC64
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000298 /* DBDMAs may have bogus sizes */
299 if ((res->start & 0x0001f000) == 0x00008000)
300 res->end = res->start + 0xff;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100301#endif /* CONFIG_PPC64 */
302
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000303 /* ESCC parent eats child resources. We could have added a
304 * level of hierarchy, but I don't really feel the need
305 * for it
306 */
307 if (!strcmp(np->name, "escc"))
308 return 1;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100309
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000310 /* ESCC has bogus resources >= 3 */
311 if (index >= 3 && !(strcmp(np->name, "ch-a") &&
312 strcmp(np->name, "ch-b")))
313 return 1;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100314
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000315 /* Media bay has too many resources, keep only first one */
316 if (index > 0 && !strcmp(np->name, "media-bay"))
317 return 1;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100318
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000319 /* Some older IDE resources have bogus sizes */
320 if (!(strcmp(np->name, "IDE") && strcmp(np->name, "ATA") &&
321 strcmp(np->type, "ide") && strcmp(np->type, "ata"))) {
322 if (index == 0 && (res->end - res->start) > 0xfff)
323 res->end = res->start + 0xfff;
324 if (index == 1 && (res->end - res->start) > 0xff)
325 res->end = res->start + 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 return 0;
328}
329
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000330static void macio_create_fixup_irq(struct macio_dev *dev, int index,
331 unsigned int line)
332{
333 unsigned int irq;
334
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700335 irq = irq_create_mapping(NULL, line);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000336 if (irq != NO_IRQ) {
337 dev->interrupt[index].start = irq;
338 dev->interrupt[index].flags = IORESOURCE_IRQ;
339 dev->interrupt[index].name = dev->ofdev.dev.bus_id;
340 }
341 if (dev->n_interrupts <= index)
342 dev->n_interrupts = index + 1;
343}
344
345static void macio_add_missing_resources(struct macio_dev *dev)
346{
347 struct device_node *np = dev->ofdev.node;
348 unsigned int irq_base;
349
350 /* Gatwick has some missing interrupts on child nodes */
351 if (dev->bus->chip->type != macio_gatwick)
352 return;
353
354 /* irq_base is always 64 on gatwick. I have no cleaner way to get
355 * that value from here at this point
356 */
357 irq_base = 64;
358
359 /* Fix SCC */
360 if (strcmp(np->name, "ch-a") == 0) {
361 macio_create_fixup_irq(dev, 0, 15 + irq_base);
362 macio_create_fixup_irq(dev, 1, 4 + irq_base);
363 macio_create_fixup_irq(dev, 2, 5 + irq_base);
364 printk(KERN_INFO "macio: fixed SCC irqs on gatwick\n");
365 }
366
367 /* Fix media-bay */
368 if (strcmp(np->name, "media-bay") == 0) {
369 macio_create_fixup_irq(dev, 0, 29 + irq_base);
370 printk(KERN_INFO "macio: fixed media-bay irq on gatwick\n");
371 }
372
373 /* Fix left media bay childs */
374 if (dev->media_bay != NULL && strcmp(np->name, "floppy") == 0) {
375 macio_create_fixup_irq(dev, 0, 19 + irq_base);
376 macio_create_fixup_irq(dev, 1, 1 + irq_base);
377 printk(KERN_INFO "macio: fixed left floppy irqs\n");
378 }
379 if (dev->media_bay != NULL && strcasecmp(np->name, "ata4") == 0) {
380 macio_create_fixup_irq(dev, 0, 14 + irq_base);
381 macio_create_fixup_irq(dev, 0, 3 + irq_base);
382 printk(KERN_INFO "macio: fixed left ide irqs\n");
383 }
384}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100386static void macio_setup_interrupts(struct macio_dev *dev)
387{
388 struct device_node *np = dev->ofdev.node;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000389 unsigned int irq;
390 int i = 0, j = 0;
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100391
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000392 for (;;) {
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100393 struct resource *res = &dev->interrupt[j];
394
395 if (j >= MACIO_DEV_COUNT_IRQS)
396 break;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000397 irq = irq_of_parse_and_map(np, i++);
398 if (irq == NO_IRQ)
399 break;
400 res->start = irq;
401 res->flags = IORESOURCE_IRQ;
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100402 res->name = dev->ofdev.dev.bus_id;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000403 if (macio_resource_quirks(np, res, i - 1)) {
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100404 memset(res, 0, sizeof(struct resource));
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000405 continue;
406 } else
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100407 j++;
408 }
409 dev->n_interrupts = j;
410}
411
412static void macio_setup_resources(struct macio_dev *dev,
413 struct resource *parent_res)
414{
415 struct device_node *np = dev->ofdev.node;
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100416 struct resource r;
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100417 int index;
418
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100419 for (index = 0; of_address_to_resource(np, index, &r) == 0; index++) {
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100420 struct resource *res = &dev->resource[index];
421 if (index >= MACIO_DEV_COUNT_RESOURCES)
422 break;
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100423 *res = r;
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100424 res->name = dev->ofdev.dev.bus_id;
425
426 if (macio_resource_quirks(np, res, index)) {
427 memset(res, 0, sizeof(struct resource));
428 continue;
429 }
430 /* Currently, we consider failure as harmless, this may
431 * change in the future, once I've found all the device
432 * tree bugs in older machines & worked around them
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100433 */
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100434 if (insert_resource(parent_res, res)) {
435 printk(KERN_WARNING "Can't request resource "
436 "%d for MacIO device %s\n",
437 index, dev->ofdev.dev.bus_id);
438 }
439 }
440 dev->n_resources = index;
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/**
444 * macio_add_one_device - Add one device from OF node to the device tree
445 * @chip: pointer to the macio_chip holding the device
446 * @np: pointer to the device node in the OF tree
447 * @in_bay: set to 1 if device is part of a media-bay
448 *
449 * When media-bay is changed to hotswap drivers, this function will
450 * be exposed to the bay driver some way...
451 */
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100452static struct macio_dev * macio_add_one_device(struct macio_chip *chip,
453 struct device *parent,
454 struct device_node *np,
455 struct macio_dev *in_bay,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct resource *parent_res)
457{
458 struct macio_dev *dev;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000459 const u32 *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (np == NULL)
462 return NULL;
463
464 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
465 if (!dev)
466 return NULL;
467 memset(dev, 0, sizeof(*dev));
468
469 dev->bus = &chip->lbus;
470 dev->media_bay = in_bay;
471 dev->ofdev.node = np;
472 dev->ofdev.dma_mask = 0xffffffffUL;
473 dev->ofdev.dev.dma_mask = &dev->ofdev.dma_mask;
474 dev->ofdev.dev.parent = parent;
475 dev->ofdev.dev.bus = &macio_bus_type;
476 dev->ofdev.dev.release = macio_release_dev;
477
478#ifdef DEBUG
479 printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
480 dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj);
481#endif
482
483 /* MacIO itself has a different reg, we use it's PCI base */
484 if (np == chip->of_node) {
Benjamin Herrenschmidtd63fb6c2006-07-02 11:55:03 +1000485 sprintf(dev->ofdev.dev.bus_id, "%1d.%08x:%.*s",
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100486 chip->lbus.index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487#ifdef CONFIG_PCI
Benjamin Herrenschmidtd63fb6c2006-07-02 11:55:03 +1000488 (unsigned int)pci_resource_start(chip->lbus.pdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489#else
490 0, /* NuBus may want to do something better here */
491#endif
492 MAX_NODE_NAME_SIZE, np->name);
493 } else {
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000494 reg = get_property(np, "reg", NULL);
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100495 sprintf(dev->ofdev.dev.bus_id, "%1d.%08x:%.*s",
496 chip->lbus.index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 reg ? *reg : 0, MAX_NODE_NAME_SIZE, np->name);
498 }
499
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100500 /* Setup interrupts & resources */
501 macio_setup_interrupts(dev);
502 macio_setup_resources(dev, parent_res);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000503 macio_add_missing_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100505 /* Register with core */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (of_device_register(&dev->ofdev) != 0) {
507 printk(KERN_DEBUG"macio: device registration error for %s!\n",
508 dev->ofdev.dev.bus_id);
509 kfree(dev);
510 return NULL;
511 }
512
513 return dev;
514}
515
516static int macio_skip_device(struct device_node *np)
517{
518 if (strncmp(np->name, "battery", 7) == 0)
519 return 1;
520 if (strncmp(np->name, "escc-legacy", 11) == 0)
521 return 1;
522 return 0;
523}
524
525/**
526 * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree
527 * @chip: pointer to the macio_chip holding the devices
528 *
529 * This function will do the job of extracting devices from the
530 * Open Firmware device tree, build macio_dev structures and add
531 * them to the Linux device tree.
532 *
533 * For now, childs of media-bay are added now as well. This will
534 * change rsn though.
535 */
536static void macio_pci_add_devices(struct macio_chip *chip)
537{
538 struct device_node *np, *pnode;
539 struct macio_dev *rdev, *mdev, *mbdev = NULL, *sdev = NULL;
540 struct device *parent = NULL;
541 struct resource *root_res = &iomem_resource;
542
543 /* Add a node for the macio bus itself */
544#ifdef CONFIG_PCI
545 if (chip->lbus.pdev) {
546 parent = &chip->lbus.pdev->dev;
547 root_res = &chip->lbus.pdev->resource[0];
548 }
549#endif
550 pnode = of_node_get(chip->of_node);
551 if (pnode == NULL)
552 return;
553
554 /* Add macio itself to hierarchy */
555 rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res);
556 if (rdev == NULL)
557 return;
558 root_res = &rdev->resource[0];
559
560 /* First scan 1st level */
561 for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100562 if (macio_skip_device(np))
563 continue;
564 of_node_get(np);
565 mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL,
566 root_res);
567 if (mdev == NULL)
568 of_node_put(np);
569 else if (strncmp(np->name, "media-bay", 9) == 0)
570 mbdev = mdev;
571 else if (strncmp(np->name, "escc", 4) == 0)
572 sdev = mdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
575 /* Add media bay devices if any */
576 if (mbdev)
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100577 for (np = NULL; (np = of_get_next_child(mbdev->ofdev.node, np))
578 != NULL;) {
579 if (macio_skip_device(np))
580 continue;
581 of_node_get(np);
582 if (macio_add_one_device(chip, &mbdev->ofdev.dev, np,
583 mbdev, root_res) == NULL)
584 of_node_put(np);
585 }
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Add serial ports if any */
588 if (sdev) {
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100589 for (np = NULL; (np = of_get_next_child(sdev->ofdev.node, np))
590 != NULL;) {
591 if (macio_skip_device(np))
592 continue;
593 of_node_get(np);
594 if (macio_add_one_device(chip, &sdev->ofdev.dev, np,
595 NULL, root_res) == NULL)
596 of_node_put(np);
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599}
600
601
602/**
603 * macio_register_driver - Registers a new MacIO device driver
604 * @drv: pointer to the driver definition structure
605 */
606int macio_register_driver(struct macio_driver *drv)
607{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* initialize common driver fields */
609 drv->driver.name = drv->name;
610 drv->driver.bus = &macio_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 /* register with core */
Bjorn Helgaasd56a3e32006-03-21 23:20:28 -0800613 return driver_register(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/**
617 * macio_unregister_driver - Unregisters a new MacIO device driver
618 * @drv: pointer to the driver definition structure
619 */
620void macio_unregister_driver(struct macio_driver *drv)
621{
622 driver_unregister(&drv->driver);
623}
624
625/**
626 * macio_request_resource - Request an MMIO resource
627 * @dev: pointer to the device holding the resource
628 * @resource_no: resource number to request
629 * @name: resource name
630 *
631 * Mark memory region number @resource_no associated with MacIO
632 * device @dev as being reserved by owner @name. Do not access
633 * any address inside the memory regions unless this call returns
634 * successfully.
635 *
636 * Returns 0 on success, or %EBUSY on error. A warning
637 * message is also printed on failure.
638 */
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100639int macio_request_resource(struct macio_dev *dev, int resource_no,
640 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 if (macio_resource_len(dev, resource_no) == 0)
643 return 0;
644
645 if (!request_mem_region(macio_resource_start(dev, resource_no),
646 macio_resource_len(dev, resource_no),
647 name))
648 goto err_out;
649
650 return 0;
651
652err_out:
653 printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx"
654 " for device %s\n",
655 resource_no,
656 macio_resource_len(dev, resource_no),
657 macio_resource_start(dev, resource_no),
658 dev->ofdev.dev.bus_id);
659 return -EBUSY;
660}
661
662/**
663 * macio_release_resource - Release an MMIO resource
664 * @dev: pointer to the device holding the resource
665 * @resource_no: resource number to release
666 */
667void macio_release_resource(struct macio_dev *dev, int resource_no)
668{
669 if (macio_resource_len(dev, resource_no) == 0)
670 return;
671 release_mem_region(macio_resource_start(dev, resource_no),
672 macio_resource_len(dev, resource_no));
673}
674
675/**
676 * macio_request_resources - Reserve all memory resources
677 * @dev: MacIO device whose resources are to be reserved
678 * @name: Name to be associated with resource.
679 *
680 * Mark all memory regions associated with MacIO device @dev as
681 * being reserved by owner @name. Do not access any address inside
682 * the memory regions unless this call returns successfully.
683 *
684 * Returns 0 on success, or %EBUSY on error. A warning
685 * message is also printed on failure.
686 */
687int macio_request_resources(struct macio_dev *dev, const char *name)
688{
689 int i;
690
691 for (i = 0; i < dev->n_resources; i++)
692 if (macio_request_resource(dev, i, name))
693 goto err_out;
694 return 0;
695
696err_out:
697 while(--i >= 0)
698 macio_release_resource(dev, i);
699
700 return -EBUSY;
701}
702
703/**
704 * macio_release_resources - Release reserved memory resources
705 * @dev: MacIO device whose resources were previously reserved
706 */
707
708void macio_release_resources(struct macio_dev *dev)
709{
710 int i;
711
712 for (i = 0; i < dev->n_resources; i++)
713 macio_release_resource(dev, i);
714}
715
716
717#ifdef CONFIG_PCI
718
719static int __devinit macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
720{
721 struct device_node* np;
722 struct macio_chip* chip;
723
724 if (ent->vendor != PCI_VENDOR_ID_APPLE)
725 return -ENODEV;
726
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100727 /* Note regarding refcounting: We assume pci_device_to_OF_node() is
728 * ported to new OF APIs and returns a node with refcount incremented.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
730 np = pci_device_to_OF_node(pdev);
731 if (np == NULL)
732 return -ENODEV;
733
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100734 /* The above assumption is wrong !!!
735 * fix that here for now until I fix the arch code
736 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 of_node_get(np);
738
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100739 /* We also assume that pmac_feature will have done a get() on nodes
740 * stored in the macio chips array
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
742 chip = macio_find(np, macio_unknown);
743 of_node_put(np);
744 if (chip == NULL)
745 return -ENODEV;
746
747 /* XXX Need locking ??? */
748 if (chip->lbus.pdev == NULL) {
749 chip->lbus.pdev = pdev;
750 chip->lbus.chip = chip;
751 pci_set_drvdata(pdev, &chip->lbus);
752 pci_set_master(pdev);
753 }
754
755 printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n",
756 chip->name);
757
758 /*
759 * HACK ALERT: The WallStreet PowerBook and some OHare based machines
Benjamin Herrenschmidt2c5bd012005-11-23 17:59:04 +1100760 * have 2 macio ASICs. I must probe the "main" one first or IDE
761 * ordering will be incorrect. So I put on "hold" the second one since
762 * it seem to appear first on PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
764 if (chip->type == macio_gatwick || chip->type == macio_ohareII)
765 if (macio_chips[0].lbus.pdev == NULL) {
766 macio_on_hold = chip;
767 return 0;
768 }
769
770 macio_pci_add_devices(chip);
771 if (macio_on_hold && macio_chips[0].lbus.pdev != NULL) {
772 macio_pci_add_devices(macio_on_hold);
773 macio_on_hold = NULL;
774 }
775
776 return 0;
777}
778
779static void __devexit macio_pci_remove(struct pci_dev* pdev)
780{
781 panic("removing of macio-asic not supported !\n");
782}
783
784/*
785 * MacIO is matched against any Apple ID, it's probe() function
786 * will then decide wether it applies or not
787 */
788static const struct pci_device_id __devinitdata pci_ids [] = { {
789 .vendor = PCI_VENDOR_ID_APPLE,
790 .device = PCI_ANY_ID,
791 .subvendor = PCI_ANY_ID,
792 .subdevice = PCI_ANY_ID,
793
794 }, { /* end: all zeroes */ }
795};
796MODULE_DEVICE_TABLE (pci, pci_ids);
797
798/* pci driver glue; this is a "new style" PCI driver module */
799static struct pci_driver macio_pci_driver = {
800 .name = (char *) "macio",
801 .id_table = pci_ids,
802
803 .probe = macio_pci_probe,
804 .remove = macio_pci_remove,
805};
806
807#endif /* CONFIG_PCI */
808
809static int __init macio_module_init (void)
810{
811#ifdef CONFIG_PCI
812 int rc;
813
814 rc = pci_register_driver(&macio_pci_driver);
815 if (rc)
816 return rc;
817#endif /* CONFIG_PCI */
818 return 0;
819}
820
821module_init(macio_module_init);
822
823EXPORT_SYMBOL(macio_register_driver);
824EXPORT_SYMBOL(macio_unregister_driver);
825EXPORT_SYMBOL(macio_dev_get);
826EXPORT_SYMBOL(macio_dev_put);
827EXPORT_SYMBOL(macio_request_resource);
828EXPORT_SYMBOL(macio_release_resource);
829EXPORT_SYMBOL(macio_request_resources);
830EXPORT_SYMBOL(macio_release_resources);