[POWERPC] Add device tree utility functions to zImage
[linux-2.6] / arch / powerpc / boot / devtree.c
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 }