add initial support for the WZR-AGL300NH - patch by Joonas Lahtinen <joonas.lahtinen...
[openwrt/openwrt.git] / target / linux / ramips / files / arch / mips / ralink / common / prom.c
1 /*
2 * Ralink SoC specific prom routines
3 *
4 * Copyright (C) 2010 Joonas Lahtinen <joonas.lahtinen@gmail.com>
5 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/string.h>
15
16 #include <asm/bootinfo.h>
17 #include <asm/addrspace.h>
18
19 #include <asm/mach-ralink/common.h>
20 #include <asm/mach-ralink/machine.h>
21 #include <ralink_soc.h>
22
23 struct board_rec {
24 char *name;
25 enum ramips_mach_type mach_type;
26 };
27
28 static struct board_rec boards[] __initdata = {
29 {
30 .name = "RT-N15",
31 .mach_type = RAMIPS_MACH_RT_N15,
32 }, {
33 .name = "WZR-AGL300NH",
34 .mach_type = RAMIPS_MACH_WZR_AGL300NH,
35 }, {
36 .name = "DIR-300-revB",
37 .mach_type = RAMIPS_MACH_DIR_300_REVB,
38 }, {
39 .name = "V22RW-2X2",
40 .mach_type = RAMIPS_MACH_V22RW_2X2,
41 }, {
42 .name = "WHR-G300N",
43 .mach_type = RAMIPS_MACH_WHR_G300N,
44 }, {
45 .name = "FONERA20N",
46 .mach_type = RAMIPS_MACH_FONERA20N,
47 }
48 };
49
50 static inline void *to_ram_addr(void *addr)
51 {
52 u32 base;
53
54 base = KSEG0ADDR(RALINK_SOC_SDRAM_BASE);
55 if (((u32) addr > base) &&
56 ((u32) addr < (base + RALINK_SOC_MEM_SIZE_MAX)))
57 return addr;
58
59 base = KSEG1ADDR(RALINK_SOC_SDRAM_BASE);
60 if (((u32) addr > base) &&
61 ((u32) addr < (base + RALINK_SOC_MEM_SIZE_MAX)))
62 return addr;
63
64 /* some U-Boot variants uses physical addresses */
65 base = RALINK_SOC_SDRAM_BASE;
66 if (((u32) addr > base) &&
67 ((u32) addr < (base + RALINK_SOC_MEM_SIZE_MAX)))
68 return (void *)KSEG0ADDR(addr);
69
70 return NULL;
71 }
72
73 static void __init prom_append_cmdline(const char *name,
74 const char *value)
75 {
76 char buf[CL_SIZE];
77
78 snprintf(buf, sizeof(buf), " %s=%s", name, value);
79 strlcat(arcs_cmdline, buf, sizeof(arcs_cmdline));
80 }
81
82 #ifdef CONFIG_IMAGE_CMDLINE_HACK
83 extern char __image_cmdline[];
84
85 static int __init use_image_cmdline(void)
86 {
87 char *p = __image_cmdline;
88 int replace = 0;
89
90 if (*p == '-') {
91 replace = 1;
92 p++;
93 }
94
95 if (*p == '\0')
96 return 0;
97
98 if (replace) {
99 strlcpy(arcs_cmdline, p, sizeof(arcs_cmdline));
100 } else {
101 strlcat(arcs_cmdline, " ", sizeof(arcs_cmdline));
102 strlcat(arcs_cmdline, p, sizeof(arcs_cmdline));
103 }
104
105 return 1;
106 }
107 #else
108 static int inline use_image_cmdline(void) { return 0; }
109 #endif
110
111 static __init void prom_init_cmdline(int argc, char **argv)
112 {
113 int i;
114
115 if (use_image_cmdline())
116 return;
117
118 if (!argv) {
119 printk(KERN_DEBUG "argv=%p is invalid, skipping\n",
120 argv);
121 return;
122 }
123
124 for (i = 0; i < argc; i++) {
125 char *p = to_ram_addr(argv[i]);
126
127 if (!p) {
128 printk(KERN_DEBUG
129 "argv[%d]=%p is invalid, skipping\n",
130 i, argv[i]);
131 continue;
132 }
133
134 printk(KERN_DEBUG "argv[%d]: %s\n", i, p);
135 strlcat(arcs_cmdline, " ", sizeof(arcs_cmdline));
136 strlcat(arcs_cmdline, p, sizeof(arcs_cmdline));
137 }
138 }
139
140 static __init char *prom_append_env(char **env, const char *envname)
141 {
142 #define PROM_MAX_ENVS 256
143 int len = strlen(envname);
144 int i;
145
146 if (!env) {
147 printk(KERN_DEBUG "env=%p is not in RAM, skipping\n",
148 env);
149 return NULL;
150 }
151
152 for (i = 0; i < PROM_MAX_ENVS; i++) {
153 char *p = to_ram_addr(env[i]);
154
155 if (!p)
156 break;
157
158 printk(KERN_DEBUG "env[%d]: %s\n", i, p);
159 if (strncmp(envname, p, len) == 0 && p[len] == '=')
160 prom_append_cmdline(envname, p + len + 1);
161 }
162
163 return NULL;
164 #undef PROM_MAX_ENVS
165 }
166
167 static __init int ramips_board_setup(char *name)
168 {
169 int i;
170
171 for (i = 0; i < ARRAY_SIZE(boards); i++)
172 if (strcmp(name, boards[i].name) == 0) {
173 ramips_mach = boards[i].mach_type;
174 break;
175 }
176
177 return 1;
178 }
179 __setup("board=", ramips_board_setup);
180
181 void __init prom_init(void)
182 {
183 int argc;
184 char **envp;
185 char **argv;
186
187 printk(KERN_DEBUG
188 "prom: fw_arg0=%08x, fw_arg1=%08x, fw_arg2=%08x, fw_arg3=%08x\n",
189 (unsigned int)fw_arg0, (unsigned int)fw_arg1,
190 (unsigned int)fw_arg2, (unsigned int)fw_arg3);
191
192 argc = fw_arg0;
193 argv = to_ram_addr((void *)fw_arg1);
194 prom_init_cmdline(argc, argv);
195
196 envp = to_ram_addr((void *)fw_arg2);
197 prom_append_env(envp, "board");
198 prom_append_env(envp, "ethaddr");
199 }
200
201 void __init prom_free_prom_memory(void)
202 {
203 /* We do not have to prom memory to free */
204 }