[ramips] initial support for the AP-R3052-V22RW-2X2 board
[10.03/openwrt.git] / target / linux / ramips / files / arch / mips / ralink / common / prom.c
1 /*
2  *  Ralink SoC specific prom routines
3  *
4  *  Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under the terms of the GNU General Public License version 2 as published
8  *  by the Free Software Foundation.
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/string.h>
14
15 #include <asm/bootinfo.h>
16 #include <asm/addrspace.h>
17
18 #include <asm/mach-ralink/common.h>
19 #include <asm/mach-ralink/machine.h>
20 #include <ralink_soc.h>
21
22 struct board_rec {
23         char                    *name;
24         enum ramips_mach_type   mach_type;
25 };
26
27 static int ramips_prom_argc __initdata;
28 static char **ramips_prom_argv __initdata;
29 static char **ramips_prom_envp __initdata;
30
31 static struct board_rec boards[] __initdata = {
32         {
33                 .name           = "RT-N15",
34                 .mach_type      = RAMIPS_MACH_RT_N15,
35         }, {
36                 .name           = "V22RW-2X2",
37                 .mach_type      = RAMIPS_MACH_V22RW_2X2,
38         }, {
39                 .name           = "WHR-G300N",
40                 .mach_type      = RAMIPS_MACH_WHR_G300N,
41         }
42 };
43
44 static inline void *to_ram_addr(void *addr)
45 {
46         u32 base;
47
48         base = KSEG0ADDR(RALINK_SOC_SDRAM_BASE);
49         if (((u32) addr > base) &&
50             ((u32) addr < (base + RALINK_SOC_MEM_SIZE_MAX)))
51                 return addr;
52
53         base = KSEG1ADDR(RALINK_SOC_SDRAM_BASE);
54         if (((u32) addr > base) &&
55             ((u32) addr < (base + RALINK_SOC_MEM_SIZE_MAX)))
56                 return addr;
57
58         /* some U-Boot variants uses physical addresses */
59         base = RALINK_SOC_SDRAM_BASE;
60         if (((u32) addr > base) &&
61             ((u32) addr < (base + RALINK_SOC_MEM_SIZE_MAX)))
62                 return (void *)KSEG0ADDR(addr);
63
64         return NULL;
65 }
66
67 static __init char *ramips_prom_getargv(const char *name)
68 {
69         int len = strlen(name);
70         int i;
71
72         if (!ramips_prom_argv) {
73                 printk(KERN_DEBUG "argv=%p is invalid, skipping\n",
74                        ramips_prom_argv);
75                 return NULL;
76         }
77
78         for (i = 0; i < ramips_prom_argc; i++) {
79                 char *argv = to_ram_addr(ramips_prom_argv[i]);
80
81                 if (!argv) {
82                         printk(KERN_DEBUG
83                                "argv[%d]=%p is invalid, skipping\n",
84                                i, ramips_prom_argv[i]);
85                         continue;
86                 }
87
88                 printk(KERN_DEBUG "argv[%d]: %s\n", i, argv);
89                 if (strncmp(name, argv, len) == 0 && (argv)[len] == '=')
90                         return argv + len + 1;
91         }
92
93         return NULL;
94 }
95
96 static __init char *ramips_prom_getenv(const char *envname)
97 {
98 #define PROM_MAX_ENVS   256
99         int len = strlen(envname);
100         char **env;
101         int i;
102
103         env = ramips_prom_envp;
104         if (!env) {
105                 printk(KERN_DEBUG "envp=%p is not in RAM, skipping\n",
106                        ramips_prom_envp);
107                 return NULL;
108         }
109
110         for (i = 0; i < PROM_MAX_ENVS; i++) {
111                 char *p = to_ram_addr(env[i]);
112
113                 if (!p)
114                         break;
115
116                 printk(KERN_DEBUG "env[%d]: %s\n", i, p);
117                 if (strncmp(envname, p, len) == 0 && p[len] == '=')
118                         return p + len + 1;
119         }
120
121         return NULL;
122 #undef PROM_MAX_ENVS
123 }
124
125 static __init void find_board_byname(char *name)
126 {
127         int i;
128
129         for (i = 0; i < ARRAY_SIZE(boards); i++)
130                 if (strcmp(name, boards[i].name) == 0) {
131                         ramips_mach = boards[i].mach_type;
132                         break;
133                 }
134 }
135
136 void __init prom_init(void)
137 {
138         char *p;
139
140         printk(KERN_DEBUG
141                "prom: fw_arg0=%08x, fw_arg1=%08x, fw_arg2=%08x, fw_arg3=%08x\n",
142                (unsigned int)fw_arg0, (unsigned int)fw_arg1,
143                (unsigned int)fw_arg2, (unsigned int)fw_arg3);
144
145         ramips_prom_argc = fw_arg0;
146         ramips_prom_argv = to_ram_addr((void *)fw_arg1);
147         ramips_prom_envp = to_ram_addr((void *)fw_arg2);
148
149         p = ramips_prom_getargv("board");
150         if (!p)
151                 p = ramips_prom_getenv("board");
152         if (p)
153                 find_board_byname(p);
154 }
155
156 void __init prom_free_prom_memory(void)
157 {
158         /* We do not have to prom memory to free */
159 }