Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 44 | const struct of_device_id * matches = macio_drv->match_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 77 | const struct of_device_id *match; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Pavel Machek | f451390 | 2005-04-16 15:25:32 -0700 | [diff] [blame] | 117 | static int macio_device_suspend(struct device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 137 | static int macio_uevent(struct device *dev, char **envp, int num_envp, |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 138 | char *buffer, int buffer_size) |
| 139 | { |
| 140 | struct macio_dev * macio_dev; |
| 141 | struct of_device * of; |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 142 | char *scratch, *compat, *compat2; |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 143 | int i = 0; |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 144 | int length, cplen, cplen2, seen = 0; |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 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; |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 154 | |
| 155 | /* stuff we want to pass to /sbin/hotplug */ |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 156 | envp[i++] = scratch = buffer; |
| 157 | length = scnprintf (scratch, buffer_size, "OF_NAME=%s", of->node->name); |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 158 | ++length; |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 159 | buffer_size -= length; |
| 160 | if ((buffer_size <= 0) || (i >= num_envp)) |
| 161 | return -ENOMEM; |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 162 | scratch += length; |
| 163 | |
| 164 | envp[i++] = scratch; |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 165 | length = scnprintf (scratch, buffer_size, "OF_TYPE=%s", of->node->type); |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 166 | ++length; |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 167 | buffer_size -= length; |
| 168 | if ((buffer_size <= 0) || (i >= num_envp)) |
| 169 | return -ENOMEM; |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 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); |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 177 | compat2 = compat; |
| 178 | cplen2= cplen; |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 179 | while (compat && cplen > 0) { |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 180 | envp[i++] = scratch; |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 181 | length = scnprintf (scratch, buffer_size, |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 182 | "OF_COMPATIBLE_%d=%s", seen, compat); |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 183 | ++length; |
| 184 | buffer_size -= length; |
| 185 | if ((buffer_size <= 0) || (i >= num_envp)) |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 186 | return -ENOMEM; |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 187 | scratch += length; |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 188 | length = strlen (compat) + 1; |
| 189 | compat += length; |
| 190 | cplen -= length; |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 191 | seen++; |
| 192 | } |
| 193 | |
| 194 | envp[i++] = scratch; |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 195 | length = scnprintf (scratch, buffer_size, "OF_COMPATIBLE_N=%d", seen); |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 196 | ++length; |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 197 | buffer_size -= length; |
| 198 | if ((buffer_size <= 0) || (i >= num_envp)) |
| 199 | return -ENOMEM; |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 200 | scratch += length; |
| 201 | |
Olaf Hering | e619978 | 2006-02-01 03:05:58 -0800 | [diff] [blame] | 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; |
| 209 | scratch += length; |
| 210 | |
| 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 | |
Jeff Mahoney | 184f6eb | 2005-07-06 15:45:09 -0400 | [diff] [blame] | 226 | envp[i] = NULL; |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
Jeff Mahoney | b5bf5b6 | 2005-07-06 15:26:27 -0400 | [diff] [blame] | 231 | extern struct device_attribute macio_dev_attrs[]; |
| 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | struct bus_type macio_bus_type = { |
| 234 | .name = "macio", |
| 235 | .match = macio_bus_match, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 236 | .uevent = macio_uevent, |
Russell King | 4866b12 | 2006-01-05 14:39:24 +0000 | [diff] [blame] | 237 | .probe = macio_device_probe, |
| 238 | .remove = macio_device_remove, |
| 239 | .shutdown = macio_device_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | .suspend = macio_device_suspend, |
| 241 | .resume = macio_device_resume, |
Jeff Mahoney | b5bf5b6 | 2005-07-06 15:26:27 -0400 | [diff] [blame] | 242 | .dev_attrs = macio_dev_attrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /** |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 254 | * macio_release_dev - free a macio device structure when all users of it are |
| 255 | * finished. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | * @dev: device that's been disconnected |
| 257 | * |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 280 | static int macio_resource_quirks(struct device_node *np, struct resource *res, |
| 281 | int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
| 283 | if (res->flags & IORESOURCE_MEM) { |
| 284 | /* Grand Central has too large resource 0 on some machines */ |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 285 | if (index == 0 && !strcmp(np->name, "gc")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | res->end = res->start + 0x1ffff; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | /* Airport has bogus resource 2 */ |
| 289 | if (index >= 2 && !strcmp(np->name, "radio")) |
| 290 | return 1; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 291 | |
| 292 | #ifndef CONFIG_PPC64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | /* DBDMAs may have bogus sizes */ |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 294 | if ((res->start & 0x0001f000) == 0x00008000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | res->end = res->start + 0xff; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 296 | #endif /* CONFIG_PPC64 */ |
| 297 | |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 298 | /* ESCC parent eats child resources. We could have added a |
| 299 | * level of hierarchy, but I don't really feel the need |
| 300 | * for it |
| 301 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (!strcmp(np->name, "escc")) |
| 303 | return 1; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | /* ESCC has bogus resources >= 3 */ |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 306 | if (index >= 3 && !(strcmp(np->name, "ch-a") && |
| 307 | strcmp(np->name, "ch-b"))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | return 1; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | /* Media bay has too many resources, keep only first one */ |
| 311 | if (index > 0 && !strcmp(np->name, "media-bay")) |
| 312 | return 1; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | /* Some older IDE resources have bogus sizes */ |
| 315 | if (!(strcmp(np->name, "IDE") && strcmp(np->name, "ATA") && |
| 316 | strcmp(np->type, "ide") && strcmp(np->type, "ata"))) { |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 317 | if (index == 0 && (res->end - res->start) > 0xfff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | res->end = res->start + 0xfff; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 319 | if (index == 1 && (res->end - res->start) > 0xff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | res->end = res->start + 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 327 | static void macio_setup_interrupts(struct macio_dev *dev) |
| 328 | { |
| 329 | struct device_node *np = dev->ofdev.node; |
| 330 | int i,j; |
| 331 | |
| 332 | /* For now, we use pre-parsed entries in the device-tree for |
| 333 | * interrupt routing and addresses, but we should change that |
| 334 | * to dynamically parsed entries and so get rid of most of the |
| 335 | * clutter in struct device_node |
| 336 | */ |
| 337 | for (i = j = 0; i < np->n_intrs; i++) { |
| 338 | struct resource *res = &dev->interrupt[j]; |
| 339 | |
| 340 | if (j >= MACIO_DEV_COUNT_IRQS) |
| 341 | break; |
| 342 | res->start = np->intrs[i].line; |
| 343 | res->flags = IORESOURCE_IO; |
| 344 | if (np->intrs[j].sense) |
| 345 | res->flags |= IORESOURCE_IRQ_LOWLEVEL; |
| 346 | else |
| 347 | res->flags |= IORESOURCE_IRQ_HIGHEDGE; |
| 348 | res->name = dev->ofdev.dev.bus_id; |
| 349 | if (macio_resource_quirks(np, res, i)) |
| 350 | memset(res, 0, sizeof(struct resource)); |
| 351 | else |
| 352 | j++; |
| 353 | } |
| 354 | dev->n_interrupts = j; |
| 355 | } |
| 356 | |
| 357 | static void macio_setup_resources(struct macio_dev *dev, |
| 358 | struct resource *parent_res) |
| 359 | { |
| 360 | struct device_node *np = dev->ofdev.node; |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 361 | struct resource r; |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 362 | int index; |
| 363 | |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 364 | for (index = 0; of_address_to_resource(np, index, &r) == 0; index++) { |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 365 | struct resource *res = &dev->resource[index]; |
| 366 | if (index >= MACIO_DEV_COUNT_RESOURCES) |
| 367 | break; |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 368 | *res = r; |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 369 | res->name = dev->ofdev.dev.bus_id; |
| 370 | |
| 371 | if (macio_resource_quirks(np, res, index)) { |
| 372 | memset(res, 0, sizeof(struct resource)); |
| 373 | continue; |
| 374 | } |
| 375 | /* Currently, we consider failure as harmless, this may |
| 376 | * change in the future, once I've found all the device |
| 377 | * tree bugs in older machines & worked around them |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 378 | */ |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 379 | if (insert_resource(parent_res, res)) { |
| 380 | printk(KERN_WARNING "Can't request resource " |
| 381 | "%d for MacIO device %s\n", |
| 382 | index, dev->ofdev.dev.bus_id); |
| 383 | } |
| 384 | } |
| 385 | dev->n_resources = index; |
| 386 | } |
| 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | /** |
| 389 | * macio_add_one_device - Add one device from OF node to the device tree |
| 390 | * @chip: pointer to the macio_chip holding the device |
| 391 | * @np: pointer to the device node in the OF tree |
| 392 | * @in_bay: set to 1 if device is part of a media-bay |
| 393 | * |
| 394 | * When media-bay is changed to hotswap drivers, this function will |
| 395 | * be exposed to the bay driver some way... |
| 396 | */ |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 397 | static struct macio_dev * macio_add_one_device(struct macio_chip *chip, |
| 398 | struct device *parent, |
| 399 | struct device_node *np, |
| 400 | struct macio_dev *in_bay, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | struct resource *parent_res) |
| 402 | { |
| 403 | struct macio_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | u32 *reg; |
| 405 | |
| 406 | if (np == NULL) |
| 407 | return NULL; |
| 408 | |
| 409 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); |
| 410 | if (!dev) |
| 411 | return NULL; |
| 412 | memset(dev, 0, sizeof(*dev)); |
| 413 | |
| 414 | dev->bus = &chip->lbus; |
| 415 | dev->media_bay = in_bay; |
| 416 | dev->ofdev.node = np; |
| 417 | dev->ofdev.dma_mask = 0xffffffffUL; |
| 418 | dev->ofdev.dev.dma_mask = &dev->ofdev.dma_mask; |
| 419 | dev->ofdev.dev.parent = parent; |
| 420 | dev->ofdev.dev.bus = &macio_bus_type; |
| 421 | dev->ofdev.dev.release = macio_release_dev; |
| 422 | |
| 423 | #ifdef DEBUG |
| 424 | printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n", |
| 425 | dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj); |
| 426 | #endif |
| 427 | |
| 428 | /* MacIO itself has a different reg, we use it's PCI base */ |
| 429 | if (np == chip->of_node) { |
Greg Kroah-Hartman | e29419f | 2006-06-12 15:20:16 -0700 | [diff] [blame] | 430 | sprintf(dev->ofdev.dev.bus_id, "%1d.%016llx:%.*s", |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 431 | chip->lbus.index, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | #ifdef CONFIG_PCI |
Greg Kroah-Hartman | e29419f | 2006-06-12 15:20:16 -0700 | [diff] [blame] | 433 | (unsigned long long)pci_resource_start(chip->lbus.pdev, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | #else |
| 435 | 0, /* NuBus may want to do something better here */ |
| 436 | #endif |
| 437 | MAX_NODE_NAME_SIZE, np->name); |
| 438 | } else { |
| 439 | reg = (u32 *)get_property(np, "reg", NULL); |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 440 | sprintf(dev->ofdev.dev.bus_id, "%1d.%08x:%.*s", |
| 441 | chip->lbus.index, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | reg ? *reg : 0, MAX_NODE_NAME_SIZE, np->name); |
| 443 | } |
| 444 | |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 445 | /* Setup interrupts & resources */ |
| 446 | macio_setup_interrupts(dev); |
| 447 | macio_setup_resources(dev, parent_res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 449 | /* Register with core */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | if (of_device_register(&dev->ofdev) != 0) { |
| 451 | printk(KERN_DEBUG"macio: device registration error for %s!\n", |
| 452 | dev->ofdev.dev.bus_id); |
| 453 | kfree(dev); |
| 454 | return NULL; |
| 455 | } |
| 456 | |
| 457 | return dev; |
| 458 | } |
| 459 | |
| 460 | static int macio_skip_device(struct device_node *np) |
| 461 | { |
| 462 | if (strncmp(np->name, "battery", 7) == 0) |
| 463 | return 1; |
| 464 | if (strncmp(np->name, "escc-legacy", 11) == 0) |
| 465 | return 1; |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree |
| 471 | * @chip: pointer to the macio_chip holding the devices |
| 472 | * |
| 473 | * This function will do the job of extracting devices from the |
| 474 | * Open Firmware device tree, build macio_dev structures and add |
| 475 | * them to the Linux device tree. |
| 476 | * |
| 477 | * For now, childs of media-bay are added now as well. This will |
| 478 | * change rsn though. |
| 479 | */ |
| 480 | static void macio_pci_add_devices(struct macio_chip *chip) |
| 481 | { |
| 482 | struct device_node *np, *pnode; |
| 483 | struct macio_dev *rdev, *mdev, *mbdev = NULL, *sdev = NULL; |
| 484 | struct device *parent = NULL; |
| 485 | struct resource *root_res = &iomem_resource; |
| 486 | |
| 487 | /* Add a node for the macio bus itself */ |
| 488 | #ifdef CONFIG_PCI |
| 489 | if (chip->lbus.pdev) { |
| 490 | parent = &chip->lbus.pdev->dev; |
| 491 | root_res = &chip->lbus.pdev->resource[0]; |
| 492 | } |
| 493 | #endif |
| 494 | pnode = of_node_get(chip->of_node); |
| 495 | if (pnode == NULL) |
| 496 | return; |
| 497 | |
| 498 | /* Add macio itself to hierarchy */ |
| 499 | rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res); |
| 500 | if (rdev == NULL) |
| 501 | return; |
| 502 | root_res = &rdev->resource[0]; |
| 503 | |
| 504 | /* First scan 1st level */ |
| 505 | for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) { |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 506 | if (macio_skip_device(np)) |
| 507 | continue; |
| 508 | of_node_get(np); |
| 509 | mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL, |
| 510 | root_res); |
| 511 | if (mdev == NULL) |
| 512 | of_node_put(np); |
| 513 | else if (strncmp(np->name, "media-bay", 9) == 0) |
| 514 | mbdev = mdev; |
| 515 | else if (strncmp(np->name, "escc", 4) == 0) |
| 516 | sdev = mdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* Add media bay devices if any */ |
| 520 | if (mbdev) |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 521 | for (np = NULL; (np = of_get_next_child(mbdev->ofdev.node, np)) |
| 522 | != NULL;) { |
| 523 | if (macio_skip_device(np)) |
| 524 | continue; |
| 525 | of_node_get(np); |
| 526 | if (macio_add_one_device(chip, &mbdev->ofdev.dev, np, |
| 527 | mbdev, root_res) == NULL) |
| 528 | of_node_put(np); |
| 529 | } |
| 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | /* Add serial ports if any */ |
| 532 | if (sdev) { |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 533 | for (np = NULL; (np = of_get_next_child(sdev->ofdev.node, np)) |
| 534 | != NULL;) { |
| 535 | if (macio_skip_device(np)) |
| 536 | continue; |
| 537 | of_node_get(np); |
| 538 | if (macio_add_one_device(chip, &sdev->ofdev.dev, np, |
| 539 | NULL, root_res) == NULL) |
| 540 | of_node_put(np); |
| 541 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | |
| 546 | /** |
| 547 | * macio_register_driver - Registers a new MacIO device driver |
| 548 | * @drv: pointer to the driver definition structure |
| 549 | */ |
| 550 | int macio_register_driver(struct macio_driver *drv) |
| 551 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | /* initialize common driver fields */ |
| 553 | drv->driver.name = drv->name; |
| 554 | drv->driver.bus = &macio_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | /* register with core */ |
Bjorn Helgaas | d56a3e3 | 2006-03-21 23:20:28 -0800 | [diff] [blame] | 557 | return driver_register(&drv->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /** |
| 561 | * macio_unregister_driver - Unregisters a new MacIO device driver |
| 562 | * @drv: pointer to the driver definition structure |
| 563 | */ |
| 564 | void macio_unregister_driver(struct macio_driver *drv) |
| 565 | { |
| 566 | driver_unregister(&drv->driver); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * macio_request_resource - Request an MMIO resource |
| 571 | * @dev: pointer to the device holding the resource |
| 572 | * @resource_no: resource number to request |
| 573 | * @name: resource name |
| 574 | * |
| 575 | * Mark memory region number @resource_no associated with MacIO |
| 576 | * device @dev as being reserved by owner @name. Do not access |
| 577 | * any address inside the memory regions unless this call returns |
| 578 | * successfully. |
| 579 | * |
| 580 | * Returns 0 on success, or %EBUSY on error. A warning |
| 581 | * message is also printed on failure. |
| 582 | */ |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 583 | int macio_request_resource(struct macio_dev *dev, int resource_no, |
| 584 | const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | { |
| 586 | if (macio_resource_len(dev, resource_no) == 0) |
| 587 | return 0; |
| 588 | |
| 589 | if (!request_mem_region(macio_resource_start(dev, resource_no), |
| 590 | macio_resource_len(dev, resource_no), |
| 591 | name)) |
| 592 | goto err_out; |
| 593 | |
| 594 | return 0; |
| 595 | |
| 596 | err_out: |
| 597 | printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx" |
| 598 | " for device %s\n", |
| 599 | resource_no, |
| 600 | macio_resource_len(dev, resource_no), |
| 601 | macio_resource_start(dev, resource_no), |
| 602 | dev->ofdev.dev.bus_id); |
| 603 | return -EBUSY; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * macio_release_resource - Release an MMIO resource |
| 608 | * @dev: pointer to the device holding the resource |
| 609 | * @resource_no: resource number to release |
| 610 | */ |
| 611 | void macio_release_resource(struct macio_dev *dev, int resource_no) |
| 612 | { |
| 613 | if (macio_resource_len(dev, resource_no) == 0) |
| 614 | return; |
| 615 | release_mem_region(macio_resource_start(dev, resource_no), |
| 616 | macio_resource_len(dev, resource_no)); |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * macio_request_resources - Reserve all memory resources |
| 621 | * @dev: MacIO device whose resources are to be reserved |
| 622 | * @name: Name to be associated with resource. |
| 623 | * |
| 624 | * Mark all memory regions associated with MacIO device @dev as |
| 625 | * being reserved by owner @name. Do not access any address inside |
| 626 | * the memory regions unless this call returns successfully. |
| 627 | * |
| 628 | * Returns 0 on success, or %EBUSY on error. A warning |
| 629 | * message is also printed on failure. |
| 630 | */ |
| 631 | int macio_request_resources(struct macio_dev *dev, const char *name) |
| 632 | { |
| 633 | int i; |
| 634 | |
| 635 | for (i = 0; i < dev->n_resources; i++) |
| 636 | if (macio_request_resource(dev, i, name)) |
| 637 | goto err_out; |
| 638 | return 0; |
| 639 | |
| 640 | err_out: |
| 641 | while(--i >= 0) |
| 642 | macio_release_resource(dev, i); |
| 643 | |
| 644 | return -EBUSY; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * macio_release_resources - Release reserved memory resources |
| 649 | * @dev: MacIO device whose resources were previously reserved |
| 650 | */ |
| 651 | |
| 652 | void macio_release_resources(struct macio_dev *dev) |
| 653 | { |
| 654 | int i; |
| 655 | |
| 656 | for (i = 0; i < dev->n_resources; i++) |
| 657 | macio_release_resource(dev, i); |
| 658 | } |
| 659 | |
| 660 | |
| 661 | #ifdef CONFIG_PCI |
| 662 | |
| 663 | static int __devinit macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 664 | { |
| 665 | struct device_node* np; |
| 666 | struct macio_chip* chip; |
| 667 | |
| 668 | if (ent->vendor != PCI_VENDOR_ID_APPLE) |
| 669 | return -ENODEV; |
| 670 | |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 671 | /* Note regarding refcounting: We assume pci_device_to_OF_node() is |
| 672 | * ported to new OF APIs and returns a node with refcount incremented. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | */ |
| 674 | np = pci_device_to_OF_node(pdev); |
| 675 | if (np == NULL) |
| 676 | return -ENODEV; |
| 677 | |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 678 | /* The above assumption is wrong !!! |
| 679 | * fix that here for now until I fix the arch code |
| 680 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | of_node_get(np); |
| 682 | |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 683 | /* We also assume that pmac_feature will have done a get() on nodes |
| 684 | * stored in the macio chips array |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | */ |
| 686 | chip = macio_find(np, macio_unknown); |
| 687 | of_node_put(np); |
| 688 | if (chip == NULL) |
| 689 | return -ENODEV; |
| 690 | |
| 691 | /* XXX Need locking ??? */ |
| 692 | if (chip->lbus.pdev == NULL) { |
| 693 | chip->lbus.pdev = pdev; |
| 694 | chip->lbus.chip = chip; |
| 695 | pci_set_drvdata(pdev, &chip->lbus); |
| 696 | pci_set_master(pdev); |
| 697 | } |
| 698 | |
| 699 | printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n", |
| 700 | chip->name); |
| 701 | |
| 702 | /* |
| 703 | * HACK ALERT: The WallStreet PowerBook and some OHare based machines |
Benjamin Herrenschmidt | 2c5bd01 | 2005-11-23 17:59:04 +1100 | [diff] [blame] | 704 | * have 2 macio ASICs. I must probe the "main" one first or IDE |
| 705 | * ordering will be incorrect. So I put on "hold" the second one since |
| 706 | * it seem to appear first on PCI |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | */ |
| 708 | if (chip->type == macio_gatwick || chip->type == macio_ohareII) |
| 709 | if (macio_chips[0].lbus.pdev == NULL) { |
| 710 | macio_on_hold = chip; |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | macio_pci_add_devices(chip); |
| 715 | if (macio_on_hold && macio_chips[0].lbus.pdev != NULL) { |
| 716 | macio_pci_add_devices(macio_on_hold); |
| 717 | macio_on_hold = NULL; |
| 718 | } |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | static void __devexit macio_pci_remove(struct pci_dev* pdev) |
| 724 | { |
| 725 | panic("removing of macio-asic not supported !\n"); |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | * MacIO is matched against any Apple ID, it's probe() function |
| 730 | * will then decide wether it applies or not |
| 731 | */ |
| 732 | static const struct pci_device_id __devinitdata pci_ids [] = { { |
| 733 | .vendor = PCI_VENDOR_ID_APPLE, |
| 734 | .device = PCI_ANY_ID, |
| 735 | .subvendor = PCI_ANY_ID, |
| 736 | .subdevice = PCI_ANY_ID, |
| 737 | |
| 738 | }, { /* end: all zeroes */ } |
| 739 | }; |
| 740 | MODULE_DEVICE_TABLE (pci, pci_ids); |
| 741 | |
| 742 | /* pci driver glue; this is a "new style" PCI driver module */ |
| 743 | static struct pci_driver macio_pci_driver = { |
| 744 | .name = (char *) "macio", |
| 745 | .id_table = pci_ids, |
| 746 | |
| 747 | .probe = macio_pci_probe, |
| 748 | .remove = macio_pci_remove, |
| 749 | }; |
| 750 | |
| 751 | #endif /* CONFIG_PCI */ |
| 752 | |
| 753 | static int __init macio_module_init (void) |
| 754 | { |
| 755 | #ifdef CONFIG_PCI |
| 756 | int rc; |
| 757 | |
| 758 | rc = pci_register_driver(&macio_pci_driver); |
| 759 | if (rc) |
| 760 | return rc; |
| 761 | #endif /* CONFIG_PCI */ |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | module_init(macio_module_init); |
| 766 | |
| 767 | EXPORT_SYMBOL(macio_register_driver); |
| 768 | EXPORT_SYMBOL(macio_unregister_driver); |
| 769 | EXPORT_SYMBOL(macio_dev_get); |
| 770 | EXPORT_SYMBOL(macio_dev_put); |
| 771 | EXPORT_SYMBOL(macio_request_resource); |
| 772 | EXPORT_SYMBOL(macio_release_resource); |
| 773 | EXPORT_SYMBOL(macio_request_resources); |
| 774 | EXPORT_SYMBOL(macio_release_resources); |