aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2007-03-22 02:02:21 -0400
committerPaul Mackerras <paulus@samba.org>2007-04-12 13:55:14 -0400
commit27fbaa9702e548e74dffd21855769f6cedad42bd (patch)
tree2af4decec01306e34031e2a62a3932991b899fbc
parent3467bfd340f9ad48f3732415533a2e9c18240b62 (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>
-rw-r--r--arch/powerpc/boot/Makefile2
-rw-r--r--arch/powerpc/boot/devtree.c111
-rw-r--r--arch/powerpc/boot/main.c27
-rw-r--r--arch/powerpc/boot/ops.h13
4 files changed, 136 insertions, 17 deletions
diff --git a/arch/powerpc/boot/Makefile b/arch/powerpc/boot/Makefile
index de80e47d1171..fac6ed0c5bcd 100644
--- a/arch/powerpc/boot/Makefile
+++ b/arch/powerpc/boot/Makefile
@@ -42,7 +42,7 @@ $(addprefix $(obj)/,$(zlib) main.o): $(addprefix $(obj)/,$(zliblinuxheader)) \
42 42
43src-wlib := string.S crt0.S stdio.c main.c flatdevtree.c flatdevtree_misc.c \ 43src-wlib := string.S crt0.S stdio.c main.c flatdevtree.c flatdevtree_misc.c \
44 ns16550.c serial.c simple_alloc.c div64.S util.S \ 44 ns16550.c serial.c simple_alloc.c div64.S util.S \
45 gunzip_util.c $(zlib) 45 gunzip_util.c $(zlib) devtree.c
46src-plat := of.c 46src-plat := of.c
47src-boot := $(src-wlib) $(src-plat) empty.c 47src-boot := $(src-wlib) $(src-plat) empty.c
48 48
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
21void 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
62void 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
79void 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
89void __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}
diff --git a/arch/powerpc/boot/main.c b/arch/powerpc/boot/main.c
index e1df8feaf16d..ab9dfe2b38f1 100644
--- a/arch/powerpc/boot/main.c
+++ b/arch/powerpc/boot/main.c
@@ -153,13 +153,10 @@ static struct addr_range prep_kernel(void)
153 return (struct addr_range){addr, ei.memsize}; 153 return (struct addr_range){addr, ei.memsize};
154} 154}
155 155
156static struct addr_range prep_initrd(struct addr_range vmlinux, 156static struct addr_range prep_initrd(struct addr_range vmlinux, void *chosen,
157 unsigned long initrd_addr, 157 unsigned long initrd_addr,
158 unsigned long initrd_size) 158 unsigned long initrd_size)
159{ 159{
160 void *devp;
161 u32 initrd_start, initrd_end;
162
163 /* If we have an image attached to us, it overrides anything 160 /* If we have an image attached to us, it overrides anything
164 * supplied by the loader. */ 161 * supplied by the loader. */
165 if (_initrd_end > _initrd_start) { 162 if (_initrd_end > _initrd_start) {
@@ -198,16 +195,8 @@ static struct addr_range prep_initrd(struct addr_range vmlinux,
198 printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd_addr)); 195 printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd_addr));
199 196
200 /* Tell the kernel initrd address via device tree */ 197 /* Tell the kernel initrd address via device tree */
201 devp = finddevice("/chosen"); 198 setprop_val(chosen, "linux,initrd-start", (u32)(initrd_addr));
202 if (! devp) 199 setprop_val(chosen, "linux,initrd-end", (u32)(initrd_addr+initrd_size));
203 fatal("Device tree has no chosen node!\n\r");
204
205 initrd_start = (u32)initrd_addr;
206 initrd_end = (u32)initrd_addr + initrd_size;
207
208 setprop(devp, "linux,initrd-start", &initrd_start,
209 sizeof(initrd_start));
210 setprop(devp, "linux,initrd-end", &initrd_end, sizeof(initrd_end));
211 200
212 return (struct addr_range){(void *)initrd_addr, initrd_size}; 201 return (struct addr_range){(void *)initrd_addr, initrd_size};
213} 202}
@@ -254,6 +243,7 @@ void start(void)
254 kernel_entry_t kentry; 243 kernel_entry_t kentry;
255 char cmdline[COMMAND_LINE_SIZE]; 244 char cmdline[COMMAND_LINE_SIZE];
256 unsigned long ft_addr = 0; 245 unsigned long ft_addr = 0;
246 void *chosen;
257 247
258 if (console_ops.open && (console_ops.open() < 0)) 248 if (console_ops.open && (console_ops.open() < 0))
259 exit(); 249 exit();
@@ -263,9 +253,14 @@ void start(void)
263 printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r", 253 printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
264 _start, get_sp()); 254 _start, get_sp());
265 255
256 /* Ensure that the device tree has a /chosen node */
257 chosen = finddevice("/chosen");
258 if (!chosen)
259 chosen = create_node(NULL, "chosen");
260
266 vmlinux = prep_kernel(); 261 vmlinux = prep_kernel();
267 initrd = prep_initrd(vmlinux, loader_info.initrd_addr, 262 initrd = prep_initrd(vmlinux, chosen,
268 loader_info.initrd_size); 263 loader_info.initrd_addr, loader_info.initrd_size);
269 264
270 /* If cmdline came from zimage wrapper or if we can edit the one 265 /* If cmdline came from zimage wrapper or if we can edit the one
271 * in the dt, print it out and edit it, if possible. 266 * in the dt, print it out and edit it, if possible.
diff --git a/arch/powerpc/boot/ops.h b/arch/powerpc/boot/ops.h
index 592dc6c20bdb..cc191e8e147f 100644
--- a/arch/powerpc/boot/ops.h
+++ b/arch/powerpc/boot/ops.h
@@ -96,6 +96,11 @@ static inline int setprop(void *devp, const char *name,
96{ 96{
97 return (dt_ops.setprop) ? dt_ops.setprop(devp, name, buf, buflen) : -1; 97 return (dt_ops.setprop) ? dt_ops.setprop(devp, name, buf, buflen) : -1;
98} 98}
99#define setprop_val(devp, name, val) \
100 do { \
101 typeof(val) x = (val); \
102 setprop((devp), (name), &x, sizeof(x)); \
103 } while (0)
99 104
100static inline int setprop_str(void *devp, const char *name, const char *buf) 105static inline int setprop_str(void *devp, const char *name, const char *buf)
101{ 106{
@@ -141,6 +146,14 @@ static inline void *find_node_by_devtype(const void *prev,
141 return find_node_by_prop_value_str(prev, "device_type", type); 146 return find_node_by_prop_value_str(prev, "device_type", type);
142} 147}
143 148
149void dt_fixup_memory(u64 start, u64 size);
150void dt_fixup_cpu_clocks(u32 cpufreq, u32 tbfreq, u32 busfreq);
151void dt_fixup_clock(const char *path, u32 freq);
152void __dt_fixup_mac_addresses(u32 startindex, ...);
153#define dt_fixup_mac_addresses(...) \
154 __dt_fixup_mac_addresses(0, __VA_ARGS__, NULL)
155
156
144static inline void *malloc(u32 size) 157static inline void *malloc(u32 size)
145{ 158{
146 return (platform_ops.malloc) ? platform_ops.malloc(size) : NULL; 159 return (platform_ops.malloc) ? platform_ops.malloc(size) : NULL;