David S. Miller | 942a6bd | 2006-06-23 15:53:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Procedures for creating, accessing and interpreting the device tree. |
| 3 | * |
| 4 | * Paul Mackerras August 1996. |
| 5 | * Copyright (C) 1996-2005 Paul Mackerras. |
| 6 | * |
| 7 | * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. |
| 8 | * {engebret|bergner}@us.ibm.com |
| 9 | * |
| 10 | * Adapted for sparc32 by David S. Miller davem@davemloft.net |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/bootmem.h> |
| 23 | #include <linux/module.h> |
| 24 | |
| 25 | #include <asm/prom.h> |
| 26 | #include <asm/oplib.h> |
| 27 | |
David S. Miller | 657f201 | 2008-12-04 20:12:20 -0800 | [diff] [blame] | 28 | #include "prom.h" |
David S. Miller | fb7cd9d | 2006-06-25 23:18:36 -0700 | [diff] [blame] | 29 | |
David S. Miller | efeac2f | 2008-12-05 00:40:43 -0800 | [diff] [blame] | 30 | void * __init prom_early_alloc(unsigned long size) |
David S. Miller | 942a6bd | 2006-06-23 15:53:31 -0700 | [diff] [blame] | 31 | { |
| 32 | void *ret; |
| 33 | |
| 34 | ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL); |
| 35 | if (ret != NULL) |
| 36 | memset(ret, 0, size); |
| 37 | |
| 38 | prom_early_allocated += size; |
| 39 | |
| 40 | return ret; |
| 41 | } |
| 42 | |
David S. Miller | 942a6bd | 2006-06-23 15:53:31 -0700 | [diff] [blame] | 43 | /* The following routines deal with the black magic of fully naming a |
| 44 | * node. |
| 45 | * |
| 46 | * Certain well known named nodes are just the simple name string. |
| 47 | * |
| 48 | * Actual devices have an address specifier appended to the base name |
| 49 | * string, like this "foo@addr". The "addr" can be in any number of |
| 50 | * formats, and the platform plus the type of the node determine the |
| 51 | * format and how it is constructed. |
| 52 | * |
| 53 | * For children of the ROOT node, the naming convention is fixed and |
| 54 | * determined by whether this is a sun4u or sun4v system. |
| 55 | * |
| 56 | * For children of other nodes, it is bus type specific. So |
| 57 | * we walk up the tree until we discover a "device_type" property |
| 58 | * we recognize and we go from there. |
| 59 | */ |
| 60 | static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf) |
| 61 | { |
| 62 | struct linux_prom_registers *regs; |
| 63 | struct property *rprop; |
| 64 | |
| 65 | rprop = of_find_property(dp, "reg", NULL); |
| 66 | if (!rprop) |
| 67 | return; |
| 68 | |
| 69 | regs = rprop->value; |
| 70 | sprintf(tmp_buf, "%s@%x,%x", |
| 71 | dp->name, |
| 72 | regs->which_io, regs->phys_addr); |
| 73 | } |
| 74 | |
| 75 | /* "name@slot,offset" */ |
| 76 | static void __init sbus_path_component(struct device_node *dp, char *tmp_buf) |
| 77 | { |
| 78 | struct linux_prom_registers *regs; |
| 79 | struct property *prop; |
| 80 | |
| 81 | prop = of_find_property(dp, "reg", NULL); |
| 82 | if (!prop) |
| 83 | return; |
| 84 | |
| 85 | regs = prop->value; |
| 86 | sprintf(tmp_buf, "%s@%x,%x", |
| 87 | dp->name, |
| 88 | regs->which_io, |
| 89 | regs->phys_addr); |
| 90 | } |
| 91 | |
| 92 | /* "name@devnum[,func]" */ |
| 93 | static void __init pci_path_component(struct device_node *dp, char *tmp_buf) |
| 94 | { |
| 95 | struct linux_prom_pci_registers *regs; |
| 96 | struct property *prop; |
| 97 | unsigned int devfn; |
| 98 | |
| 99 | prop = of_find_property(dp, "reg", NULL); |
| 100 | if (!prop) |
| 101 | return; |
| 102 | |
| 103 | regs = prop->value; |
| 104 | devfn = (regs->phys_hi >> 8) & 0xff; |
| 105 | if (devfn & 0x07) { |
| 106 | sprintf(tmp_buf, "%s@%x,%x", |
| 107 | dp->name, |
| 108 | devfn >> 3, |
| 109 | devfn & 0x07); |
| 110 | } else { |
| 111 | sprintf(tmp_buf, "%s@%x", |
| 112 | dp->name, |
| 113 | devfn >> 3); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* "name@addrhi,addrlo" */ |
| 118 | static void __init ebus_path_component(struct device_node *dp, char *tmp_buf) |
| 119 | { |
| 120 | struct linux_prom_registers *regs; |
| 121 | struct property *prop; |
| 122 | |
| 123 | prop = of_find_property(dp, "reg", NULL); |
| 124 | if (!prop) |
| 125 | return; |
| 126 | |
| 127 | regs = prop->value; |
| 128 | |
| 129 | sprintf(tmp_buf, "%s@%x,%x", |
| 130 | dp->name, |
| 131 | regs->which_io, regs->phys_addr); |
| 132 | } |
| 133 | |
| 134 | static void __init __build_path_component(struct device_node *dp, char *tmp_buf) |
| 135 | { |
| 136 | struct device_node *parent = dp->parent; |
| 137 | |
| 138 | if (parent != NULL) { |
| 139 | if (!strcmp(parent->type, "pci") || |
| 140 | !strcmp(parent->type, "pciex")) |
| 141 | return pci_path_component(dp, tmp_buf); |
| 142 | if (!strcmp(parent->type, "sbus")) |
| 143 | return sbus_path_component(dp, tmp_buf); |
| 144 | if (!strcmp(parent->type, "ebus")) |
| 145 | return ebus_path_component(dp, tmp_buf); |
| 146 | |
| 147 | /* "isa" is handled with platform naming */ |
| 148 | } |
| 149 | |
| 150 | /* Use platform naming convention. */ |
| 151 | return sparc32_path_component(dp, tmp_buf); |
| 152 | } |
| 153 | |
David S. Miller | 6524036 | 2008-12-05 01:21:41 -0800 | [diff] [blame] | 154 | char * __init build_path_component(struct device_node *dp) |
David S. Miller | 942a6bd | 2006-06-23 15:53:31 -0700 | [diff] [blame] | 155 | { |
| 156 | char tmp_buf[64], *n; |
| 157 | |
| 158 | tmp_buf[0] = '\0'; |
| 159 | __build_path_component(dp, tmp_buf); |
| 160 | if (tmp_buf[0] == '\0') |
| 161 | strcpy(tmp_buf, dp->name); |
| 162 | |
| 163 | n = prom_early_alloc(strlen(tmp_buf) + 1); |
| 164 | strcpy(n, tmp_buf); |
| 165 | |
| 166 | return n; |
| 167 | } |
| 168 | |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 169 | struct device_node *of_console_device; |
| 170 | EXPORT_SYMBOL(of_console_device); |
| 171 | |
| 172 | char *of_console_path; |
| 173 | EXPORT_SYMBOL(of_console_path); |
| 174 | |
| 175 | char *of_console_options; |
| 176 | EXPORT_SYMBOL(of_console_options); |
| 177 | |
| 178 | extern void restore_current(void); |
| 179 | |
David S. Miller | 23dc758 | 2008-12-05 18:16:48 -0800 | [diff] [blame^] | 180 | void __init of_console_init(void) |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 181 | { |
| 182 | char *msg = "OF stdout device is: %s\n"; |
| 183 | struct device_node *dp; |
| 184 | unsigned long flags; |
| 185 | const char *type; |
| 186 | phandle node; |
David S. Miller | f623f38 | 2007-07-29 02:10:37 -0700 | [diff] [blame] | 187 | int skip, tmp, fd; |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 188 | |
| 189 | of_console_path = prom_early_alloc(256); |
| 190 | |
| 191 | switch (prom_vers) { |
| 192 | case PROM_V0: |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 193 | skip = 0; |
| 194 | switch (*romvec->pv_stdout) { |
| 195 | case PROMDEV_SCREEN: |
| 196 | type = "display"; |
| 197 | break; |
| 198 | |
| 199 | case PROMDEV_TTYB: |
| 200 | skip = 1; |
| 201 | /* FALLTHRU */ |
| 202 | |
| 203 | case PROMDEV_TTYA: |
| 204 | type = "serial"; |
| 205 | break; |
| 206 | |
| 207 | default: |
| 208 | prom_printf("Invalid PROM_V0 stdout value %u\n", |
| 209 | *romvec->pv_stdout); |
| 210 | prom_halt(); |
| 211 | } |
| 212 | |
David S. Miller | f623f38 | 2007-07-29 02:10:37 -0700 | [diff] [blame] | 213 | tmp = skip; |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 214 | for_each_node_by_type(dp, type) { |
David S. Miller | f623f38 | 2007-07-29 02:10:37 -0700 | [diff] [blame] | 215 | if (!tmp--) |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | if (!dp) { |
| 219 | prom_printf("Cannot find PROM_V0 console node.\n"); |
| 220 | prom_halt(); |
| 221 | } |
| 222 | of_console_device = dp; |
| 223 | |
| 224 | strcpy(of_console_path, dp->full_name); |
| 225 | if (!strcmp(type, "serial")) { |
| 226 | strcat(of_console_path, |
| 227 | (skip ? ":b" : ":a")); |
| 228 | } |
| 229 | break; |
| 230 | |
| 231 | default: |
| 232 | case PROM_V2: |
| 233 | case PROM_V3: |
| 234 | fd = *romvec->pv_v2bootargs.fd_stdout; |
| 235 | |
| 236 | spin_lock_irqsave(&prom_lock, flags); |
| 237 | node = (*romvec->pv_v2devops.v2_inst2pkg)(fd); |
| 238 | restore_current(); |
| 239 | spin_unlock_irqrestore(&prom_lock, flags); |
| 240 | |
| 241 | if (!node) { |
| 242 | prom_printf("Cannot resolve stdout node from " |
| 243 | "instance %08x.\n", fd); |
| 244 | prom_halt(); |
| 245 | } |
| 246 | dp = of_find_node_by_phandle(node); |
| 247 | type = of_get_property(dp, "device_type", NULL); |
| 248 | |
| 249 | if (!type) { |
| 250 | prom_printf("Console stdout lacks " |
| 251 | "device_type property.\n"); |
| 252 | prom_halt(); |
| 253 | } |
| 254 | |
| 255 | if (strcmp(type, "display") && strcmp(type, "serial")) { |
| 256 | prom_printf("Console device_type is neither display " |
| 257 | "nor serial.\n"); |
| 258 | prom_halt(); |
| 259 | } |
| 260 | |
| 261 | of_console_device = dp; |
| 262 | |
| 263 | if (prom_vers == PROM_V2) { |
| 264 | strcpy(of_console_path, dp->full_name); |
| 265 | switch (*romvec->pv_stdout) { |
| 266 | case PROMDEV_TTYA: |
| 267 | strcat(of_console_path, ":a"); |
| 268 | break; |
| 269 | case PROMDEV_TTYB: |
| 270 | strcat(of_console_path, ":b"); |
| 271 | break; |
| 272 | } |
| 273 | } else { |
| 274 | const char *path; |
| 275 | |
| 276 | dp = of_find_node_by_path("/"); |
| 277 | path = of_get_property(dp, "stdout-path", NULL); |
| 278 | if (!path) { |
| 279 | prom_printf("No stdout-path in root node.\n"); |
| 280 | prom_halt(); |
| 281 | } |
| 282 | strcpy(of_console_path, path); |
| 283 | } |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | of_console_options = strrchr(of_console_path, ':'); |
| 288 | if (of_console_options) { |
| 289 | of_console_options++; |
| 290 | if (*of_console_options == '\0') |
| 291 | of_console_options = NULL; |
| 292 | } |
| 293 | |
| 294 | prom_printf(msg, of_console_path); |
| 295 | printk(msg, of_console_path); |
| 296 | } |
| 297 | |
David S. Miller | 23dc758 | 2008-12-05 18:16:48 -0800 | [diff] [blame^] | 298 | void __init of_fill_in_cpu_data(void) |
David S. Miller | 942a6bd | 2006-06-23 15:53:31 -0700 | [diff] [blame] | 299 | { |
David S. Miller | 942a6bd | 2006-06-23 15:53:31 -0700 | [diff] [blame] | 300 | } |