Commit | Line | Data |
---|---|---|
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 | * | |
2c5bd01f BH |
6 | * 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 | * | |
1da177e4 LT |
13 | * 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 | ||
1da177e4 LT |
20 | #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> | |
4e57b681 TS |
26 | #include <linux/slab.h> |
27 | ||
1da177e4 LT |
28 | #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 | ||
38 | static struct macio_chip *macio_on_hold; | |
39 | ||
40 | static 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); | |
5e655772 | 44 | const struct of_device_id * matches = macio_drv->match_table; |
1da177e4 LT |
45 | |
46 | if (!matches) | |
47 | return 0; | |
48 | ||
49 | return of_match_device(matches, &macio_dev->ofdev) != NULL; | |
50 | } | |
51 | ||
52 | struct 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 | ||
65 | void macio_dev_put(struct macio_dev *dev) | |
66 | { | |
67 | if (dev) | |
68 | put_device(&dev->ofdev.dev); | |
69 | } | |
70 | ||
71 | ||
72 | static int macio_device_probe(struct device *dev) | |
73 | { | |
74 | int error = -ENODEV; | |
75 | struct macio_driver *drv; | |
76 | struct macio_dev *macio_dev; | |
5e655772 | 77 | const struct of_device_id *match; |
1da177e4 LT |
78 | |
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 | ||
96 | static 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 | ||
108 | static 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 | ||
f4513904 | 117 | static int macio_device_suspend(struct device *dev, pm_message_t state) |
1da177e4 LT |
118 | { |
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 | ||
127 | static 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 | ||
312c004d | 137 | static int macio_uevent(struct device *dev, char **envp, int num_envp, |
184f6eb8 JM |
138 | char *buffer, int buffer_size) |
139 | { | |
140 | struct macio_dev * macio_dev; | |
141 | struct of_device * of; | |
e6199788 | 142 | char *scratch, *compat, *compat2; |
184f6eb8 | 143 | int i = 0; |
e6199788 | 144 | int length, cplen, cplen2, seen = 0; |
184f6eb8 JM |
145 | |
146 | if (!dev) | |
147 | return -ENODEV; | |
148 | ||
149 | macio_dev = to_macio_device(dev); | |
150 | if (!macio_dev) | |
151 | return -ENODEV; | |
152 | ||
153 | of = &macio_dev->ofdev; | |
184f6eb8 JM |
154 | |
155 | /* stuff we want to pass to /sbin/hotplug */ | |
e6199788 OH |
156 | envp[i++] = scratch = buffer; |
157 | length = scnprintf (scratch, buffer_size, "OF_NAME=%s", of->node->name); | |
184f6eb8 | 158 | ++length; |
e6199788 OH |
159 | buffer_size -= length; |
160 | if ((buffer_size <= 0) || (i >= num_envp)) | |
161 | return -ENOMEM; | |
184f6eb8 JM |
162 | scratch += length; |
163 | ||
164 | envp[i++] = scratch; | |
e6199788 | 165 | length = scnprintf (scratch, buffer_size, "OF_TYPE=%s", of->node->type); |
184f6eb8 | 166 | ++length; |
e6199788 OH |
167 | buffer_size -= length; |
168 | if ((buffer_size <= 0) || (i >= num_envp)) | |
169 | return -ENOMEM; | |
184f6eb8 JM |
170 | scratch += length; |
171 | ||
172 | /* Since the compatible field can contain pretty much anything | |
173 | * it's not really legal to split it out with commas. We split it | |
174 | * up using a number of environment variables instead. */ | |
175 | ||
176 | compat = (char *) get_property(of->node, "compatible", &cplen); | |
e6199788 OH |
177 | compat2 = compat; |
178 | cplen2= cplen; | |
184f6eb8 | 179 | while (compat && cplen > 0) { |
184f6eb8 | 180 | envp[i++] = scratch; |
e6199788 | 181 | length = scnprintf (scratch, buffer_size, |
184f6eb8 | 182 | "OF_COMPATIBLE_%d=%s", seen, compat); |
e6199788 OH |
183 | ++length; |
184 | buffer_size -= length; | |
185 | if ((buffer_size <= 0) || (i >= num_envp)) | |
184f6eb8 | 186 | return -ENOMEM; |
184f6eb8 | 187 | scratch += length; |
e6199788 OH |
188 | length = strlen (compat) + 1; |
189 | compat += length; | |
190 | cplen -= length; | |
184f6eb8 JM |
191 | seen++; |
192 | } | |
193 | ||
194 | envp[i++] = scratch; | |
e6199788 | 195 | length = scnprintf (scratch, buffer_size, "OF_COMPATIBLE_N=%d", seen); |
184f6eb8 | 196 | ++length; |
e6199788 OH |
197 | buffer_size -= length; |
198 | if ((buffer_size <= 0) || (i >= num_envp)) | |
199 | return -ENOMEM; | |
200 | scratch += length; | |
201 | ||
202 | envp[i++] = scratch; | |
203 | length = scnprintf (scratch, buffer_size, "MODALIAS=of:N%sT%s", | |
204 | of->node->name, of->node->type); | |
205 | /* overwrite '\0' */ | |
206 | buffer_size -= length; | |
207 | if ((buffer_size <= 0) || (i >= num_envp)) | |
208 | return -ENOMEM; | |
184f6eb8 JM |
209 | scratch += length; |
210 | ||
e6199788 OH |
211 | if (!compat2) { |
212 | compat2 = ""; | |
213 | cplen2 = 1; | |
214 | } | |
215 | while (cplen2 > 0) { | |
216 | length = snprintf (scratch, buffer_size, "C%s", compat2); | |
217 | buffer_size -= length; | |
218 | if (buffer_size <= 0) | |
219 | return -ENOMEM; | |
220 | scratch += length; | |
221 | length = strlen (compat2) + 1; | |
222 | compat2 += length; | |
223 | cplen2 -= length; | |
224 | } | |
225 | ||
184f6eb8 JM |
226 | envp[i] = NULL; |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
b5bf5b67 JM |
231 | extern struct device_attribute macio_dev_attrs[]; |
232 | ||
1da177e4 LT |
233 | struct bus_type macio_bus_type = { |
234 | .name = "macio", | |
235 | .match = macio_bus_match, | |
312c004d | 236 | .uevent = macio_uevent, |
4866b124 RK |
237 | .probe = macio_device_probe, |
238 | .remove = macio_device_remove, | |
239 | .shutdown = macio_device_shutdown, | |
1da177e4 LT |
240 | .suspend = macio_device_suspend, |
241 | .resume = macio_device_resume, | |
b5bf5b67 | 242 | .dev_attrs = macio_dev_attrs, |
1da177e4 LT |
243 | }; |
244 | ||
245 | static int __init macio_bus_driver_init(void) | |
246 | { | |
247 | return bus_register(&macio_bus_type); | |
248 | } | |
249 | ||
250 | postcore_initcall(macio_bus_driver_init); | |
251 | ||
252 | ||
253 | /** | |
2c5bd01f BH |
254 | * macio_release_dev - free a macio device structure when all users of it are |
255 | * finished. | |
1da177e4 LT |
256 | * @dev: device that's been disconnected |
257 | * | |
2c5bd01f BH |
258 | * Will be called only by the device core when all users of this macio device |
259 | * are done. This currently means never as we don't hot remove any macio | |
260 | * device yet, though that will happen with mediabay based devices in a later | |
261 | * implementation. | |
1da177e4 LT |
262 | */ |
263 | static void macio_release_dev(struct device *dev) | |
264 | { | |
265 | struct macio_dev *mdev; | |
266 | ||
267 | mdev = to_macio_device(dev); | |
268 | kfree(mdev); | |
269 | } | |
270 | ||
271 | /** | |
272 | * macio_resource_quirks - tweak or skip some resources for a device | |
273 | * @np: pointer to the device node | |
274 | * @res: resulting resource | |
275 | * @index: index of resource in node | |
276 | * | |
277 | * If this routine returns non-null, then the resource is completely | |
278 | * skipped. | |
279 | */ | |
2c5bd01f BH |
280 | static int macio_resource_quirks(struct device_node *np, struct resource *res, |
281 | int index) | |
1da177e4 | 282 | { |
0ebfff14 BH |
283 | /* Only quirks for memory resources for now */ |
284 | if ((res->flags & IORESOURCE_MEM) == 0) | |
285 | return 0; | |
286 | ||
287 | /* Grand Central has too large resource 0 on some machines */ | |
288 | if (index == 0 && !strcmp(np->name, "gc")) | |
289 | res->end = res->start + 0x1ffff; | |
cc5d0189 | 290 | |
0ebfff14 BH |
291 | /* Airport has bogus resource 2 */ |
292 | if (index >= 2 && !strcmp(np->name, "radio")) | |
293 | return 1; | |
cc5d0189 BH |
294 | |
295 | #ifndef CONFIG_PPC64 | |
0ebfff14 BH |
296 | /* DBDMAs may have bogus sizes */ |
297 | if ((res->start & 0x0001f000) == 0x00008000) | |
298 | res->end = res->start + 0xff; | |
cc5d0189 BH |
299 | #endif /* CONFIG_PPC64 */ |
300 | ||
0ebfff14 BH |
301 | /* ESCC parent eats child resources. We could have added a |
302 | * level of hierarchy, but I don't really feel the need | |
303 | * for it | |
304 | */ | |
305 | if (!strcmp(np->name, "escc")) | |
306 | return 1; | |
307 | ||
308 | /* ESCC has bogus resources >= 3 */ | |
309 | if (index >= 3 && !(strcmp(np->name, "ch-a") && | |
310 | strcmp(np->name, "ch-b"))) | |
311 | return 1; | |
312 | ||
313 | /* Media bay has too many resources, keep only first one */ | |
314 | if (index > 0 && !strcmp(np->name, "media-bay")) | |
315 | return 1; | |
316 | ||
317 | /* Some older IDE resources have bogus sizes */ | |
318 | if (!(strcmp(np->name, "IDE") && strcmp(np->name, "ATA") && | |
319 | strcmp(np->type, "ide") && strcmp(np->type, "ata"))) { | |
320 | if (index == 0 && (res->end - res->start) > 0xfff) | |
321 | res->end = res->start + 0xfff; | |
322 | if (index == 1 && (res->end - res->start) > 0xff) | |
323 | res->end = res->start + 0xff; | |
1da177e4 LT |
324 | } |
325 | return 0; | |
326 | } | |
327 | ||
0ebfff14 BH |
328 | static void macio_create_fixup_irq(struct macio_dev *dev, int index, |
329 | unsigned int line) | |
330 | { | |
331 | unsigned int irq; | |
1da177e4 | 332 | |
6e99e458 | 333 | irq = irq_create_mapping(NULL, line); |
0ebfff14 BH |
334 | if (irq != NO_IRQ) { |
335 | dev->interrupt[index].start = irq; | |
336 | dev->interrupt[index].flags = IORESOURCE_IRQ; | |
337 | dev->interrupt[index].name = dev->ofdev.dev.bus_id; | |
338 | } | |
339 | if (dev->n_interrupts <= index) | |
340 | dev->n_interrupts = index + 1; | |
341 | } | |
342 | ||
343 | static void macio_add_missing_resources(struct macio_dev *dev) | |
2c5bd01f BH |
344 | { |
345 | struct device_node *np = dev->ofdev.node; | |
0ebfff14 BH |
346 | unsigned int irq_base; |
347 | ||
348 | /* Gatwick has some missing interrupts on child nodes */ | |
349 | if (dev->bus->chip->type != macio_gatwick) | |
350 | return; | |
2c5bd01f | 351 | |
0ebfff14 BH |
352 | /* irq_base is always 64 on gatwick. I have no cleaner way to get |
353 | * that value from here at this point | |
2c5bd01f | 354 | */ |
0ebfff14 BH |
355 | irq_base = 64; |
356 | ||
357 | /* Fix SCC */ | |
358 | if (strcmp(np->name, "ch-a") == 0) { | |
359 | macio_create_fixup_irq(dev, 0, 15 + irq_base); | |
360 | macio_create_fixup_irq(dev, 1, 4 + irq_base); | |
361 | macio_create_fixup_irq(dev, 2, 5 + irq_base); | |
362 | printk(KERN_INFO "macio: fixed SCC irqs on gatwick\n"); | |
363 | } | |
364 | ||
365 | /* Fix media-bay */ | |
366 | if (strcmp(np->name, "media-bay") == 0) { | |
367 | macio_create_fixup_irq(dev, 0, 29 + irq_base); | |
368 | printk(KERN_INFO "macio: fixed media-bay irq on gatwick\n"); | |
369 | } | |
370 | ||
371 | /* Fix left media bay childs */ | |
372 | if (dev->media_bay != NULL && strcmp(np->name, "floppy") == 0) { | |
373 | macio_create_fixup_irq(dev, 0, 19 + irq_base); | |
374 | macio_create_fixup_irq(dev, 1, 1 + irq_base); | |
375 | printk(KERN_INFO "macio: fixed left floppy irqs\n"); | |
376 | } | |
377 | if (dev->media_bay != NULL && strcasecmp(np->name, "ata4") == 0) { | |
378 | macio_create_fixup_irq(dev, 0, 14 + irq_base); | |
379 | macio_create_fixup_irq(dev, 0, 3 + irq_base); | |
380 | printk(KERN_INFO "macio: fixed left ide irqs\n"); | |
381 | } | |
382 | } | |
383 | ||
384 | static void macio_setup_interrupts(struct macio_dev *dev) | |
385 | { | |
386 | struct device_node *np = dev->ofdev.node; | |
387 | unsigned int irq; | |
388 | int i = 0, j = 0; | |
389 | ||
390 | for (;;) { | |
2c5bd01f BH |
391 | struct resource *res = &dev->interrupt[j]; |
392 | ||
393 | if (j >= MACIO_DEV_COUNT_IRQS) | |
394 | break; | |
0ebfff14 BH |
395 | irq = irq_of_parse_and_map(np, i++); |
396 | if (irq == NO_IRQ) | |
397 | break; | |
398 | res->start = irq; | |
399 | res->flags = IORESOURCE_IRQ; | |
2c5bd01f | 400 | res->name = dev->ofdev.dev.bus_id; |
0ebfff14 | 401 | if (macio_resource_quirks(np, res, i - 1)) { |
2c5bd01f | 402 | memset(res, 0, sizeof(struct resource)); |
0ebfff14 BH |
403 | continue; |
404 | } else | |
2c5bd01f BH |
405 | j++; |
406 | } | |
407 | dev->n_interrupts = j; | |
408 | } | |
409 | ||
410 | static void macio_setup_resources(struct macio_dev *dev, | |
411 | struct resource *parent_res) | |
412 | { | |
413 | struct device_node *np = dev->ofdev.node; | |
d2dd482b | 414 | struct resource r; |
2c5bd01f BH |
415 | int index; |
416 | ||
d2dd482b | 417 | for (index = 0; of_address_to_resource(np, index, &r) == 0; index++) { |
2c5bd01f BH |
418 | struct resource *res = &dev->resource[index]; |
419 | if (index >= MACIO_DEV_COUNT_RESOURCES) | |
420 | break; | |
d2dd482b | 421 | *res = r; |
2c5bd01f BH |
422 | res->name = dev->ofdev.dev.bus_id; |
423 | ||
424 | if (macio_resource_quirks(np, res, index)) { | |
425 | memset(res, 0, sizeof(struct resource)); | |
426 | continue; | |
427 | } | |
428 | /* Currently, we consider failure as harmless, this may | |
429 | * change in the future, once I've found all the device | |
430 | * tree bugs in older machines & worked around them | |
cc5d0189 | 431 | */ |
2c5bd01f BH |
432 | if (insert_resource(parent_res, res)) { |
433 | printk(KERN_WARNING "Can't request resource " | |
434 | "%d for MacIO device %s\n", | |
435 | index, dev->ofdev.dev.bus_id); | |
436 | } | |
437 | } | |
438 | dev->n_resources = index; | |
439 | } | |
440 | ||
1da177e4 LT |
441 | /** |
442 | * macio_add_one_device - Add one device from OF node to the device tree | |
443 | * @chip: pointer to the macio_chip holding the device | |
444 | * @np: pointer to the device node in the OF tree | |
445 | * @in_bay: set to 1 if device is part of a media-bay | |
446 | * | |
447 | * When media-bay is changed to hotswap drivers, this function will | |
448 | * be exposed to the bay driver some way... | |
449 | */ | |
2c5bd01f BH |
450 | static struct macio_dev * macio_add_one_device(struct macio_chip *chip, |
451 | struct device *parent, | |
452 | struct device_node *np, | |
453 | struct macio_dev *in_bay, | |
1da177e4 LT |
454 | struct resource *parent_res) |
455 | { | |
456 | struct macio_dev *dev; | |
1da177e4 LT |
457 | u32 *reg; |
458 | ||
459 | if (np == NULL) | |
460 | return NULL; | |
461 | ||
462 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); | |
463 | if (!dev) | |
464 | return NULL; | |
465 | memset(dev, 0, sizeof(*dev)); | |
466 | ||
467 | dev->bus = &chip->lbus; | |
468 | dev->media_bay = in_bay; | |
469 | dev->ofdev.node = np; | |
470 | dev->ofdev.dma_mask = 0xffffffffUL; | |
471 | dev->ofdev.dev.dma_mask = &dev->ofdev.dma_mask; | |
472 | dev->ofdev.dev.parent = parent; | |
473 | dev->ofdev.dev.bus = &macio_bus_type; | |
474 | dev->ofdev.dev.release = macio_release_dev; | |
475 | ||
476 | #ifdef DEBUG | |
477 | printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n", | |
478 | dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj); | |
479 | #endif | |
480 | ||
481 | /* MacIO itself has a different reg, we use it's PCI base */ | |
482 | if (np == chip->of_node) { | |
d63fb6c5 | 483 | sprintf(dev->ofdev.dev.bus_id, "%1d.%08x:%.*s", |
2c5bd01f | 484 | chip->lbus.index, |
1da177e4 | 485 | #ifdef CONFIG_PCI |
d63fb6c5 | 486 | (unsigned int)pci_resource_start(chip->lbus.pdev, 0), |
1da177e4 LT |
487 | #else |
488 | 0, /* NuBus may want to do something better here */ | |
489 | #endif | |
490 | MAX_NODE_NAME_SIZE, np->name); | |
491 | } else { | |
492 | reg = (u32 *)get_property(np, "reg", NULL); | |
2c5bd01f BH |
493 | sprintf(dev->ofdev.dev.bus_id, "%1d.%08x:%.*s", |
494 | chip->lbus.index, | |
1da177e4 LT |
495 | reg ? *reg : 0, MAX_NODE_NAME_SIZE, np->name); |
496 | } | |
497 | ||
2c5bd01f BH |
498 | /* Setup interrupts & resources */ |
499 | macio_setup_interrupts(dev); | |
500 | macio_setup_resources(dev, parent_res); | |
0ebfff14 | 501 | macio_add_missing_resources(dev); |
1da177e4 | 502 | |
2c5bd01f | 503 | /* Register with core */ |
1da177e4 LT |
504 | if (of_device_register(&dev->ofdev) != 0) { |
505 | printk(KERN_DEBUG"macio: device registration error for %s!\n", | |
506 | dev->ofdev.dev.bus_id); | |
507 | kfree(dev); | |
508 | return NULL; | |
509 | } | |
510 | ||
511 | return dev; | |
512 | } | |
513 | ||
514 | static int macio_skip_device(struct device_node *np) | |
515 | { | |
516 | if (strncmp(np->name, "battery", 7) == 0) | |
517 | return 1; | |
518 | if (strncmp(np->name, "escc-legacy", 11) == 0) | |
519 | return 1; | |
520 | return 0; | |
521 | } | |
522 | ||
523 | /** | |
524 | * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree | |
525 | * @chip: pointer to the macio_chip holding the devices | |
526 | * | |
527 | * This function will do the job of extracting devices from the | |
528 | * Open Firmware device tree, build macio_dev structures and add | |
529 | * them to the Linux device tree. | |
530 | * | |
531 | * For now, childs of media-bay are added now as well. This will | |
532 | * change rsn though. | |
533 | */ | |
534 | static void macio_pci_add_devices(struct macio_chip *chip) | |
535 | { | |
536 | struct device_node *np, *pnode; | |
537 | struct macio_dev *rdev, *mdev, *mbdev = NULL, *sdev = NULL; | |
538 | struct device *parent = NULL; | |
539 | struct resource *root_res = &iomem_resource; | |
540 | ||
541 | /* Add a node for the macio bus itself */ | |
542 | #ifdef CONFIG_PCI | |
543 | if (chip->lbus.pdev) { | |
544 | parent = &chip->lbus.pdev->dev; | |
545 | root_res = &chip->lbus.pdev->resource[0]; | |
546 | } | |
547 | #endif | |
548 | pnode = of_node_get(chip->of_node); | |
549 | if (pnode == NULL) | |
550 | return; | |
551 | ||
552 | /* Add macio itself to hierarchy */ | |
553 | rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res); | |
554 | if (rdev == NULL) | |
555 | return; | |
556 | root_res = &rdev->resource[0]; | |
557 | ||
558 | /* First scan 1st level */ | |
559 | for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) { | |
2c5bd01f BH |
560 | if (macio_skip_device(np)) |
561 | continue; | |
562 | of_node_get(np); | |
563 | mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL, | |
564 | root_res); | |
565 | if (mdev == NULL) | |
566 | of_node_put(np); | |
567 | else if (strncmp(np->name, "media-bay", 9) == 0) | |
568 | mbdev = mdev; | |
569 | else if (strncmp(np->name, "escc", 4) == 0) | |
570 | sdev = mdev; | |
1da177e4 LT |
571 | } |
572 | ||
573 | /* Add media bay devices if any */ | |
574 | if (mbdev) | |
2c5bd01f BH |
575 | for (np = NULL; (np = of_get_next_child(mbdev->ofdev.node, np)) |
576 | != NULL;) { | |
577 | if (macio_skip_device(np)) | |
578 | continue; | |
579 | of_node_get(np); | |
580 | if (macio_add_one_device(chip, &mbdev->ofdev.dev, np, | |
581 | mbdev, root_res) == NULL) | |
582 | of_node_put(np); | |
583 | } | |
584 | ||
1da177e4 LT |
585 | /* Add serial ports if any */ |
586 | if (sdev) { | |
2c5bd01f BH |
587 | for (np = NULL; (np = of_get_next_child(sdev->ofdev.node, np)) |
588 | != NULL;) { | |
589 | if (macio_skip_device(np)) | |
590 | continue; | |
591 | of_node_get(np); | |
592 | if (macio_add_one_device(chip, &sdev->ofdev.dev, np, | |
593 | NULL, root_res) == NULL) | |
594 | of_node_put(np); | |
595 | } | |
1da177e4 LT |
596 | } |
597 | } | |
598 | ||
599 | ||
600 | /** | |
601 | * macio_register_driver - Registers a new MacIO device driver | |
602 | * @drv: pointer to the driver definition structure | |
603 | */ | |
604 | int macio_register_driver(struct macio_driver *drv) | |
605 | { | |
1da177e4 LT |
606 | /* initialize common driver fields */ |
607 | drv->driver.name = drv->name; | |
608 | drv->driver.bus = &macio_bus_type; | |
1da177e4 LT |
609 | |
610 | /* register with core */ | |
d56a3e38 | 611 | return driver_register(&drv->driver); |
1da177e4 LT |
612 | } |
613 | ||
614 | /** | |
615 | * macio_unregister_driver - Unregisters a new MacIO device driver | |
616 | * @drv: pointer to the driver definition structure | |
617 | */ | |
618 | void macio_unregister_driver(struct macio_driver *drv) | |
619 | { | |
620 | driver_unregister(&drv->driver); | |
621 | } | |
622 | ||
623 | /** | |
624 | * macio_request_resource - Request an MMIO resource | |
625 | * @dev: pointer to the device holding the resource | |
626 | * @resource_no: resource number to request | |
627 | * @name: resource name | |
628 | * | |
629 | * Mark memory region number @resource_no associated with MacIO | |
630 | * device @dev as being reserved by owner @name. Do not access | |
631 | * any address inside the memory regions unless this call returns | |
632 | * successfully. | |
633 | * | |
634 | * Returns 0 on success, or %EBUSY on error. A warning | |
635 | * message is also printed on failure. | |
636 | */ | |
2c5bd01f BH |
637 | int macio_request_resource(struct macio_dev *dev, int resource_no, |
638 | const char *name) | |
1da177e4 LT |
639 | { |
640 | if (macio_resource_len(dev, resource_no) == 0) | |
641 | return 0; | |
642 | ||
643 | if (!request_mem_region(macio_resource_start(dev, resource_no), | |
644 | macio_resource_len(dev, resource_no), | |
645 | name)) | |
646 | goto err_out; | |
647 | ||
648 | return 0; | |
649 | ||
650 | err_out: | |
651 | printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx" | |
652 | " for device %s\n", | |
653 | resource_no, | |
654 | macio_resource_len(dev, resource_no), | |
655 | macio_resource_start(dev, resource_no), | |
656 | dev->ofdev.dev.bus_id); | |
657 | return -EBUSY; | |
658 | } | |
659 | ||
660 | /** | |
661 | * macio_release_resource - Release an MMIO resource | |
662 | * @dev: pointer to the device holding the resource | |
663 | * @resource_no: resource number to release | |
664 | */ | |
665 | void macio_release_resource(struct macio_dev *dev, int resource_no) | |
666 | { | |
667 | if (macio_resource_len(dev, resource_no) == 0) | |
668 | return; | |
669 | release_mem_region(macio_resource_start(dev, resource_no), | |
670 | macio_resource_len(dev, resource_no)); | |
671 | } | |
672 | ||
673 | /** | |
674 | * macio_request_resources - Reserve all memory resources | |
675 | * @dev: MacIO device whose resources are to be reserved | |
676 | * @name: Name to be associated with resource. | |
677 | * | |
678 | * Mark all memory regions associated with MacIO device @dev as | |
679 | * being reserved by owner @name. Do not access any address inside | |
680 | * the memory regions unless this call returns successfully. | |
681 | * | |
682 | * Returns 0 on success, or %EBUSY on error. A warning | |
683 | * message is also printed on failure. | |
684 | */ | |
685 | int macio_request_resources(struct macio_dev *dev, const char *name) | |
686 | { | |
687 | int i; | |
688 | ||
689 | for (i = 0; i < dev->n_resources; i++) | |
690 | if (macio_request_resource(dev, i, name)) | |
691 | goto err_out; | |
692 | return 0; | |
693 | ||
694 | err_out: | |
695 | while(--i >= 0) | |
696 | macio_release_resource(dev, i); | |
697 | ||
698 | return -EBUSY; | |
699 | } | |
700 | ||
701 | /** | |
702 | * macio_release_resources - Release reserved memory resources | |
703 | * @dev: MacIO device whose resources were previously reserved | |
704 | */ | |
705 | ||
706 | void macio_release_resources(struct macio_dev *dev) | |
707 | { | |
708 | int i; | |
709 | ||
710 | for (i = 0; i < dev->n_resources; i++) | |
711 | macio_release_resource(dev, i); | |
712 | } | |
713 | ||
714 | ||
715 | #ifdef CONFIG_PCI | |
716 | ||
717 | static int __devinit macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) | |
718 | { | |
719 | struct device_node* np; | |
720 | struct macio_chip* chip; | |
721 | ||
722 | if (ent->vendor != PCI_VENDOR_ID_APPLE) | |
723 | return -ENODEV; | |
724 | ||
2c5bd01f BH |
725 | /* Note regarding refcounting: We assume pci_device_to_OF_node() is |
726 | * ported to new OF APIs and returns a node with refcount incremented. | |
1da177e4 LT |
727 | */ |
728 | np = pci_device_to_OF_node(pdev); | |
729 | if (np == NULL) | |
730 | return -ENODEV; | |
731 | ||
2c5bd01f BH |
732 | /* The above assumption is wrong !!! |
733 | * fix that here for now until I fix the arch code | |
734 | */ | |
1da177e4 LT |
735 | of_node_get(np); |
736 | ||
2c5bd01f BH |
737 | /* We also assume that pmac_feature will have done a get() on nodes |
738 | * stored in the macio chips array | |
1da177e4 LT |
739 | */ |
740 | chip = macio_find(np, macio_unknown); | |
741 | of_node_put(np); | |
742 | if (chip == NULL) | |
743 | return -ENODEV; | |
744 | ||
745 | /* XXX Need locking ??? */ | |
746 | if (chip->lbus.pdev == NULL) { | |
747 | chip->lbus.pdev = pdev; | |
748 | chip->lbus.chip = chip; | |
749 | pci_set_drvdata(pdev, &chip->lbus); | |
750 | pci_set_master(pdev); | |
751 | } | |
752 | ||
753 | printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n", | |
754 | chip->name); | |
755 | ||
756 | /* | |
757 | * HACK ALERT: The WallStreet PowerBook and some OHare based machines | |
2c5bd01f BH |
758 | * have 2 macio ASICs. I must probe the "main" one first or IDE |
759 | * ordering will be incorrect. So I put on "hold" the second one since | |
760 | * it seem to appear first on PCI | |
1da177e4 LT |
761 | */ |
762 | if (chip->type == macio_gatwick || chip->type == macio_ohareII) | |
763 | if (macio_chips[0].lbus.pdev == NULL) { | |
764 | macio_on_hold = chip; | |
765 | return 0; | |
766 | } | |
767 | ||
768 | macio_pci_add_devices(chip); | |
769 | if (macio_on_hold && macio_chips[0].lbus.pdev != NULL) { | |
770 | macio_pci_add_devices(macio_on_hold); | |
771 | macio_on_hold = NULL; | |
772 | } | |
773 | ||
774 | return 0; | |
775 | } | |
776 | ||
777 | static void __devexit macio_pci_remove(struct pci_dev* pdev) | |
778 | { | |
779 | panic("removing of macio-asic not supported !\n"); | |
780 | } | |
781 | ||
782 | /* | |
783 | * MacIO is matched against any Apple ID, it's probe() function | |
784 | * will then decide wether it applies or not | |
785 | */ | |
786 | static const struct pci_device_id __devinitdata pci_ids [] = { { | |
787 | .vendor = PCI_VENDOR_ID_APPLE, | |
788 | .device = PCI_ANY_ID, | |
789 | .subvendor = PCI_ANY_ID, | |
790 | .subdevice = PCI_ANY_ID, | |
791 | ||
792 | }, { /* end: all zeroes */ } | |
793 | }; | |
794 | MODULE_DEVICE_TABLE (pci, pci_ids); | |
795 | ||
796 | /* pci driver glue; this is a "new style" PCI driver module */ | |
797 | static struct pci_driver macio_pci_driver = { | |
798 | .name = (char *) "macio", | |
799 | .id_table = pci_ids, | |
800 | ||
801 | .probe = macio_pci_probe, | |
802 | .remove = macio_pci_remove, | |
803 | }; | |
804 | ||
805 | #endif /* CONFIG_PCI */ | |
806 | ||
807 | static int __init macio_module_init (void) | |
808 | { | |
809 | #ifdef CONFIG_PCI | |
810 | int rc; | |
811 | ||
812 | rc = pci_register_driver(&macio_pci_driver); | |
813 | if (rc) | |
814 | return rc; | |
815 | #endif /* CONFIG_PCI */ | |
816 | return 0; | |
817 | } | |
818 | ||
819 | module_init(macio_module_init); | |
820 | ||
821 | EXPORT_SYMBOL(macio_register_driver); | |
822 | EXPORT_SYMBOL(macio_unregister_driver); | |
823 | EXPORT_SYMBOL(macio_dev_get); | |
824 | EXPORT_SYMBOL(macio_dev_put); | |
825 | EXPORT_SYMBOL(macio_request_resource); | |
826 | EXPORT_SYMBOL(macio_release_resource); | |
827 | EXPORT_SYMBOL(macio_request_resources); | |
828 | EXPORT_SYMBOL(macio_release_resources); |