David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame] | 1 | /* sbus.c: SBus support routines. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame] | 3 | * Copyright (C) 1995, 2006 David S. Miller (davem@davemloft.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/init.h> |
| 9 | #include <linux/pci.h> |
Fabio Massimo Di Nitto | cf69eab | 2006-12-20 09:22:28 -0800 | [diff] [blame] | 10 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | #include <asm/system.h> |
| 13 | #include <asm/sbus.h> |
| 14 | #include <asm/dma.h> |
| 15 | #include <asm/oplib.h> |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 16 | #include <asm/prom.h> |
| 17 | #include <asm/of_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/bpp.h> |
| 19 | #include <asm/irq.h> |
| 20 | |
Fabio Massimo Di Nitto | cf69eab | 2006-12-20 09:22:28 -0800 | [diff] [blame] | 21 | static ssize_t |
| 22 | show_sbusobppath_attr(struct device * dev, struct device_attribute * attr, char * buf) |
| 23 | { |
| 24 | struct sbus_dev *sbus; |
| 25 | |
| 26 | sbus = to_sbus_device(dev); |
| 27 | |
| 28 | return snprintf (buf, PAGE_SIZE, "%s\n", sbus->ofdev.node->full_name); |
| 29 | } |
| 30 | |
| 31 | static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_sbusobppath_attr, NULL); |
| 32 | |
David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame] | 33 | struct sbus_bus *sbus_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 35 | static void __init fill_sbus_device(struct device_node *dp, struct sbus_dev *sdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 37 | unsigned long base; |
| 38 | void *pval; |
Fabio Massimo Di Nitto | cf69eab | 2006-12-20 09:22:28 -0800 | [diff] [blame] | 39 | int len, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 41 | sdev->prom_node = dp->node; |
| 42 | strcpy(sdev->prom_name, dp->name); |
| 43 | |
| 44 | pval = of_get_property(dp, "reg", &len); |
David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame] | 45 | sdev->num_registers = 0; |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 46 | if (pval) { |
| 47 | memcpy(sdev->reg_addrs, pval, len); |
| 48 | |
David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame] | 49 | sdev->num_registers = |
| 50 | len / sizeof(struct linux_prom_registers); |
David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame] | 51 | |
| 52 | base = (unsigned long) sdev->reg_addrs[0].phys_addr; |
| 53 | |
| 54 | /* Compute the slot number. */ |
| 55 | if (base >= SUN_SBUS_BVADDR && sparc_cpu_model == sun4m) |
| 56 | sdev->slot = sbus_dev_slot(base); |
| 57 | else |
| 58 | sdev->slot = sdev->reg_addrs[0].which_io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 61 | pval = of_get_property(dp, "ranges", &len); |
David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame] | 62 | sdev->num_device_ranges = 0; |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 63 | if (pval) { |
| 64 | memcpy(sdev->device_ranges, pval, len); |
David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame] | 65 | sdev->num_device_ranges = |
| 66 | len / sizeof(struct linux_prom_ranges); |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 67 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame] | 69 | sbus_fill_device_irq(sdev); |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 70 | |
| 71 | sdev->ofdev.node = dp; |
| 72 | if (sdev->parent) |
| 73 | sdev->ofdev.dev.parent = &sdev->parent->ofdev.dev; |
| 74 | else |
| 75 | sdev->ofdev.dev.parent = &sdev->bus->ofdev.dev; |
| 76 | sdev->ofdev.dev.bus = &sbus_bus_type; |
David S. Miller | f5ef9d1 | 2006-10-27 01:03:31 -0700 | [diff] [blame] | 77 | sprintf(sdev->ofdev.dev.bus_id, "sbus[%08x]", dp->node); |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 78 | |
| 79 | if (of_device_register(&sdev->ofdev) != 0) |
| 80 | printk(KERN_DEBUG "sbus: device registration error for %s!\n", |
David S. Miller | f5ef9d1 | 2006-10-27 01:03:31 -0700 | [diff] [blame] | 81 | dp->path_component_name); |
Fabio Massimo Di Nitto | cf69eab | 2006-12-20 09:22:28 -0800 | [diff] [blame] | 82 | |
| 83 | /* WE HAVE BEEN INVADED BY ALIENS! */ |
| 84 | err = sysfs_create_file(&sdev->ofdev.dev.kobj, &dev_attr_obppath.attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 87 | static void __init sbus_bus_ranges_init(struct device_node *dp, struct sbus_bus *sbus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 89 | void *pval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | int len; |
| 91 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 92 | pval = of_get_property(dp, "ranges", &len); |
| 93 | sbus->num_sbus_ranges = 0; |
| 94 | if (pval) { |
| 95 | memcpy(sbus->sbus_ranges, pval, len); |
| 96 | sbus->num_sbus_ranges = |
| 97 | len / sizeof(struct linux_prom_ranges); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 99 | sbus_arch_bus_ranges_init(dp->parent, sbus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges, |
| 104 | int num_ranges, |
| 105 | struct linux_prom_registers *regs, |
| 106 | int num_regs) |
| 107 | { |
| 108 | if (num_ranges) { |
| 109 | int regnum; |
| 110 | |
| 111 | for (regnum = 0; regnum < num_regs; regnum++) { |
| 112 | int rngnum; |
| 113 | |
| 114 | for (rngnum = 0; rngnum < num_ranges; rngnum++) { |
| 115 | if (regs[regnum].which_io == ranges[rngnum].ot_child_space) |
| 116 | break; |
| 117 | } |
| 118 | if (rngnum == num_ranges) { |
| 119 | /* We used to flag this as an error. Actually |
| 120 | * some devices do not report the regs as we expect. |
| 121 | * For example, see SUNW,pln device. In that case |
| 122 | * the reg property is in a format internal to that |
| 123 | * node, ie. it is not in the SBUS register space |
| 124 | * per se. -DaveM |
| 125 | */ |
| 126 | return; |
| 127 | } |
| 128 | regs[regnum].which_io = ranges[rngnum].ot_parent_space; |
| 129 | regs[regnum].phys_addr -= ranges[rngnum].ot_child_base; |
| 130 | regs[regnum].phys_addr += ranges[rngnum].ot_parent_base; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static void __init __fixup_regs_sdev(struct sbus_dev *sdev) |
| 136 | { |
| 137 | if (sdev->num_registers != 0) { |
| 138 | struct sbus_dev *parent = sdev->parent; |
| 139 | int i; |
| 140 | |
| 141 | while (parent != NULL) { |
| 142 | __apply_ranges_to_regs(parent->device_ranges, |
| 143 | parent->num_device_ranges, |
| 144 | sdev->reg_addrs, |
| 145 | sdev->num_registers); |
| 146 | |
| 147 | parent = parent->parent; |
| 148 | } |
| 149 | |
| 150 | __apply_ranges_to_regs(sdev->bus->sbus_ranges, |
| 151 | sdev->bus->num_sbus_ranges, |
| 152 | sdev->reg_addrs, |
| 153 | sdev->num_registers); |
| 154 | |
| 155 | for (i = 0; i < sdev->num_registers; i++) { |
| 156 | struct resource *res = &sdev->resource[i]; |
| 157 | |
| 158 | res->start = sdev->reg_addrs[i].phys_addr; |
| 159 | res->end = (res->start + |
| 160 | (unsigned long)sdev->reg_addrs[i].reg_size - 1UL); |
| 161 | res->flags = IORESOURCE_IO | |
| 162 | (sdev->reg_addrs[i].which_io & 0xff); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev) |
| 168 | { |
| 169 | struct sbus_dev *sdev; |
| 170 | |
| 171 | for (sdev = first_sdev; sdev; sdev = sdev->next) { |
| 172 | if (sdev->child) |
| 173 | sbus_fixup_all_regs(sdev->child); |
| 174 | __fixup_regs_sdev(sdev); |
| 175 | } |
| 176 | } |
| 177 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 178 | /* We preserve the "probe order" of these bus and device lists to give |
| 179 | * the same ordering as the old code. |
| 180 | */ |
| 181 | static void __init sbus_insert(struct sbus_bus *sbus, struct sbus_bus **root) |
| 182 | { |
| 183 | while (*root) |
| 184 | root = &(*root)->next; |
| 185 | *root = sbus; |
| 186 | sbus->next = NULL; |
| 187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 189 | static void __init sdev_insert(struct sbus_dev *sdev, struct sbus_dev **root) |
| 190 | { |
| 191 | while (*root) |
| 192 | root = &(*root)->next; |
| 193 | *root = sdev; |
| 194 | sdev->next = NULL; |
| 195 | } |
| 196 | |
| 197 | static void __init walk_children(struct device_node *dp, struct sbus_dev *parent, struct sbus_bus *sbus) |
| 198 | { |
| 199 | dp = dp->child; |
| 200 | while (dp) { |
| 201 | struct sbus_dev *sdev; |
| 202 | |
| 203 | sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC); |
| 204 | if (sdev) { |
| 205 | sdev_insert(sdev, &parent->child); |
| 206 | |
| 207 | sdev->bus = sbus; |
| 208 | sdev->parent = parent; |
| 209 | |
| 210 | fill_sbus_device(dp, sdev); |
| 211 | |
| 212 | walk_children(dp, sdev, sbus); |
| 213 | } |
| 214 | dp = dp->sibling; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static void __init build_one_sbus(struct device_node *dp, int num_sbus) |
| 219 | { |
| 220 | struct sbus_bus *sbus; |
| 221 | unsigned int sbus_clock; |
| 222 | struct device_node *dev_dp; |
| 223 | |
| 224 | sbus = kzalloc(sizeof(struct sbus_bus), GFP_ATOMIC); |
| 225 | if (!sbus) |
| 226 | return; |
| 227 | |
| 228 | sbus_insert(sbus, &sbus_root); |
| 229 | sbus->prom_node = dp->node; |
| 230 | |
| 231 | sbus_setup_iommu(sbus, dp); |
| 232 | |
| 233 | printk("sbus%d: ", num_sbus); |
| 234 | |
| 235 | sbus_clock = of_getintprop_default(dp, "clock-frequency", |
| 236 | (25*1000*1000)); |
| 237 | sbus->clock_freq = sbus_clock; |
| 238 | |
| 239 | printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000), |
| 240 | (int) (((sbus_clock/1000)%1000 != 0) ? |
| 241 | (((sbus_clock/1000)%1000) + 1000) : 0)); |
| 242 | |
| 243 | strcpy(sbus->prom_name, dp->name); |
| 244 | |
| 245 | sbus_setup_arch_props(sbus, dp); |
| 246 | |
| 247 | sbus_bus_ranges_init(dp, sbus); |
| 248 | |
| 249 | sbus->ofdev.node = dp; |
| 250 | sbus->ofdev.dev.parent = NULL; |
| 251 | sbus->ofdev.dev.bus = &sbus_bus_type; |
David S. Miller | 3932932 | 2006-07-17 21:06:15 -0700 | [diff] [blame] | 252 | sprintf(sbus->ofdev.dev.bus_id, "sbus%d", num_sbus); |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 253 | |
| 254 | if (of_device_register(&sbus->ofdev) != 0) |
| 255 | printk(KERN_DEBUG "sbus: device registration error for %s!\n", |
| 256 | sbus->ofdev.dev.bus_id); |
| 257 | |
| 258 | dev_dp = dp->child; |
| 259 | while (dev_dp) { |
| 260 | struct sbus_dev *sdev; |
| 261 | |
| 262 | sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC); |
| 263 | if (sdev) { |
| 264 | sdev_insert(sdev, &sbus->devices); |
| 265 | |
| 266 | sdev->bus = sbus; |
| 267 | sdev->parent = NULL; |
| 268 | fill_sbus_device(dev_dp, sdev); |
| 269 | |
| 270 | walk_children(dev_dp, sdev, sbus); |
| 271 | } |
| 272 | dev_dp = dev_dp->sibling; |
| 273 | } |
| 274 | |
| 275 | sbus_fixup_all_regs(sbus->devices); |
| 276 | |
| 277 | dvma_init(sbus); |
| 278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | static int __init sbus_init(void) |
| 281 | { |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 282 | struct device_node *dp; |
| 283 | const char *sbus_name = "sbus"; |
| 284 | int num_sbus = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 286 | if (sbus_arch_preinit()) |
| 287 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 289 | if (sparc_cpu_model == sun4d) |
| 290 | sbus_name = "sbi"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 292 | for_each_node_by_name(dp, sbus_name) { |
| 293 | build_one_sbus(dp, num_sbus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | num_sbus++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 298 | sbus_arch_postinit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | subsys_initcall(sbus_init); |