diff options
author | David Gibson <david@gibson.dropbear.id.au> | 2007-03-22 02:02:21 -0400 |
---|---|---|
committer | Paul Mackerras <paulus@samba.org> | 2007-04-12 13:55:14 -0400 |
commit | 27fbaa9702e548e74dffd21855769f6cedad42bd (patch) | |
tree | 2af4decec01306e34031e2a62a3932991b899fbc /arch/powerpc/boot/devtree.c | |
parent | 3467bfd340f9ad48f3732415533a2e9c18240b62 (diff) |
[POWERPC] Add device tree utility functions to zImage
This patch adds a library of useful device tree manipulation functions
to the zImage library, for use by platform code. These functions are
based on the hooks already in dt_ops, so they're not dependent on a
particular device tree implementation. This patch also slightly
streamlines the code in main.c using these new functions.
This is a consolidation of my work in this area with Scott Wood's
patches to a very similar end.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Paul Mackerras <paulus@samba.org>
Diffstat (limited to 'arch/powerpc/boot/devtree.c')
-rw-r--r-- | arch/powerpc/boot/devtree.c | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/arch/powerpc/boot/devtree.c b/arch/powerpc/boot/devtree.c new file mode 100644 index 000000000000..708cadebeb46 --- /dev/null +++ b/arch/powerpc/boot/devtree.c | |||
@@ -0,0 +1,111 @@ | |||
1 | /* | ||
2 | * devtree.c - convenience functions for device tree manipulation | ||
3 | * Copyright 2007 David Gibson, IBM Corporation. | ||
4 | * Copyright (c) 2007 Freescale Semiconductor, Inc. | ||
5 | * | ||
6 | * Authors: David Gibson <david@gibson.dropbear.id.au> | ||
7 | * Scott Wood <scottwood@freescale.com> | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or | ||
10 | * modify it under the terms of the GNU General Public License | ||
11 | * as published by the Free Software Foundation; either version | ||
12 | * 2 of the License, or (at your option) any later version. | ||
13 | */ | ||
14 | #include <stdarg.h> | ||
15 | #include <stddef.h> | ||
16 | #include "types.h" | ||
17 | #include "string.h" | ||
18 | #include "stdio.h" | ||
19 | #include "ops.h" | ||
20 | |||
21 | void dt_fixup_memory(u64 start, u64 size) | ||
22 | { | ||
23 | void *root, *memory; | ||
24 | int naddr, nsize, i; | ||
25 | u32 memreg[4]; | ||
26 | |||
27 | root = finddevice("/"); | ||
28 | if (getprop(root, "#address-cells", &naddr, sizeof(naddr)) < 0) | ||
29 | naddr = 2; | ||
30 | if (naddr < 1 || naddr > 2) | ||
31 | fatal("Can't cope with #address-cells == %d in /\n\r", naddr); | ||
32 | |||
33 | if (getprop(root, "#size-cells", &nsize, sizeof(nsize)) < 0) | ||
34 | nsize = 1; | ||
35 | if (nsize < 1 || nsize > 2) | ||
36 | fatal("Can't cope with #size-cells == %d in /\n\r", nsize); | ||
37 | |||
38 | i = 0; | ||
39 | if (naddr == 2) | ||
40 | memreg[i++] = start >> 32; | ||
41 | memreg[i++] = start & 0xffffffff; | ||
42 | if (nsize == 2) | ||
43 | memreg[i++] = size >> 32; | ||
44 | memreg[i++] = size & 0xffffffff; | ||
45 | |||
46 | memory = finddevice("/memory"); | ||
47 | if (! memory) { | ||
48 | memory = create_node(NULL, "memory"); | ||
49 | setprop_str(memory, "device_type", "memory"); | ||
50 | } | ||
51 | |||
52 | printf("Memory <- <0x%x", memreg[0]); | ||
53 | for (i = 1; i < (naddr + nsize); i++) | ||
54 | printf(" 0x%x", memreg[i]); | ||
55 | printf("> (%ldMB)\n\r", (unsigned long)(size >> 20)); | ||
56 | |||
57 | setprop(memory, "reg", memreg, (naddr + nsize)*sizeof(u32)); | ||
58 | } | ||
59 | |||
60 | #define MHZ(x) ((x + 500000) / 1000000) | ||
61 | |||
62 | void dt_fixup_cpu_clocks(u32 cpu, u32 tb, u32 bus) | ||
63 | { | ||
64 | void *devp = NULL; | ||
65 | |||
66 | printf("CPU clock-frequency <- 0x%x (%dMHz)\n\r", cpu, MHZ(cpu)); | ||
67 | printf("CPU timebase-frequency <- 0x%x (%dMHz)\n\r", tb, MHZ(tb)); | ||
68 | if (bus > 0) | ||
69 | printf("CPU bus-frequency <- 0x%x (%dMHz)\n\r", bus, MHZ(bus)); | ||
70 | |||
71 | while ((devp = find_node_by_devtype(devp, "cpu"))) { | ||
72 | setprop_val(devp, "clock-frequency", cpu); | ||
73 | setprop_val(devp, "timebase-frequency", tb); | ||
74 | if (bus > 0) | ||
75 | setprop_val(devp, "bus-frequency", bus); | ||
76 | } | ||
77 | } | ||
78 | |||
79 | void dt_fixup_clock(const char *path, u32 freq) | ||
80 | { | ||
81 | void *devp = finddevice(path); | ||
82 | |||
83 | if (devp) { | ||
84 | printf("%s: clock-frequency <- %x (%dMHz)\n\r", path, freq, MHZ(freq)); | ||
85 | setprop_val(devp, "clock-frequency", freq); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | void __dt_fixup_mac_addresses(u32 startindex, ...) | ||
90 | { | ||
91 | va_list ap; | ||
92 | u32 index = startindex; | ||
93 | void *devp; | ||
94 | const u8 *addr; | ||
95 | |||
96 | va_start(ap, startindex); | ||
97 | while ((addr = va_arg(ap, const u8 *))) { | ||
98 | devp = find_node_by_prop_value(NULL, "linux,network-index", | ||
99 | (void*)&index, sizeof(index)); | ||
100 | |||
101 | printf("ENET%d: local-mac-address <-" | ||
102 | " %02x:%02x:%02x:%02x:%02x:%02x\n\r", index, | ||
103 | addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); | ||
104 | |||
105 | if (devp) | ||
106 | setprop(devp, "local-mac-address", addr, 6); | ||
107 | |||
108 | index++; | ||
109 | } | ||
110 | va_end(ap); | ||
111 | } | ||