774291ab7908b1e4fdfc53441cd90f0d33058da8
[openwrt.git] / target / linux / ramips / files / arch / mips / ralink / rt305x / prom.c
1 /*
2  *  Ralink RT305x 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
14 #include <asm/bootinfo.h>
15
16 #include <asm/mach-ralink/common.h>
17 #include <asm/mach-ralink/machine.h>
18 #include <asm/mach-ralink/rt305x.h>
19 #include <asm/mach-ralink/rt305x_regs.h>
20
21 struct board_rec {
22         char                    *name;
23         enum ramips_mach_type   mach_type;
24 };
25
26 static int rt305x_prom_argc __initdata;
27 static char **rt305x_prom_argv __initdata;
28 static char **rt305x_prom_envp __initdata;
29
30 static struct board_rec boards[] __initdata = {
31         {
32                 .name           = "WHR-G300N",
33                 .mach_type      = RAMIPS_MACH_WHR_G300N,
34         }
35 };
36
37 static inline void *to_ram_addr(void *addr)
38 {
39         u32 base;
40
41         base = KSEG0ADDR(RT305X_SDRAM_BASE);
42         if (((u32) addr > base) &&
43             ((u32) addr < (base + RT305X_MEM_SIZE_MAX)))
44                 return addr;
45
46         base = KSEG1ADDR(RT305X_SDRAM_BASE);
47         if (((u32) addr > base) &&
48             ((u32) addr < (base + RT305X_MEM_SIZE_MAX)))
49                 return addr;
50
51         /* some U-Boot variants uses physical addresses */
52         base = RT305X_SDRAM_BASE;
53         if (((u32) addr > base) &&
54             ((u32) addr < (base + RT305X_MEM_SIZE_MAX)))
55                 return (void *)KSEG0ADDR(addr);
56
57         return NULL;
58 }
59
60 static __init char *rt305x_prom_getargv(const char *name)
61 {
62         int len = strlen(name);
63         int i;
64
65         if (!rt305x_prom_argv) {
66                 printk(KERN_DEBUG "argv=%p is invalid, skipping\n",
67                        rt305x_prom_argv);
68                 return NULL;
69         }
70
71         for (i = 0; i < rt305x_prom_argc; i++) {
72                 char *argv = to_ram_addr(rt305x_prom_argv[i]);
73
74                 if (!argv) {
75                         printk(KERN_DEBUG
76                                "argv[%d]=%p is invalid, skipping\n",
77                                i, rt305x_prom_argv[i]);
78                         continue;
79                 }
80
81                 printk(KERN_DEBUG "argv[i]: %s\n", argv);
82                 if (strncmp(name, argv, len) == 0 && (argv)[len] == '=')
83                         return argv + len + 1;
84         }
85
86         return NULL;
87 }
88
89 static __init char *rt305x_prom_getenv(const char *envname)
90 {
91         int len = strlen(envname);
92         char **env;
93         char *p;
94
95         env = rt305x_prom_envp;
96         if (!env) {
97                 printk(KERN_DEBUG "envp=%p is not in RAM, skipping\n",
98                        rt305x_prom_envp);
99                 return NULL;
100         }
101
102         for (p = to_ram_addr(*env); p; env++) {
103                 printk(KERN_DEBUG "env: %s\n", *env);
104                 if (strncmp(envname, p, len) == 0 && (p)[len] == '=')
105                         return p + len + 1;
106         }
107
108         return NULL;
109 }
110
111 static __init void find_board_byname(char *name)
112 {
113         int i;
114
115         for (i = 0; i < ARRAY_SIZE(boards); i++)
116                 if (strcmp(name, boards[i].name) == 0) {
117                         ramips_mach = boards[i].mach_type;
118                         break;
119                 }
120 }
121
122 void __init prom_init(void)
123 {
124         char *p;
125
126         printk(KERN_DEBUG
127                "prom: fw_arg0=%08x, fw_arg1=%08x, fw_arg2=%08x, fw_arg3=%08x\n",
128                (unsigned int)fw_arg0, (unsigned int)fw_arg1,
129                (unsigned int)fw_arg2, (unsigned int)fw_arg3);
130
131         rt305x_prom_argc = fw_arg0;
132         rt305x_prom_argv = to_ram_addr((void *)fw_arg1);
133         rt305x_prom_envp = to_ram_addr((void *)fw_arg2);
134
135         p = rt305x_prom_getargv("board");
136         if (!p)
137                 p = rt305x_prom_getenv("board");
138         if (p)
139                 find_board_byname(p);
140 }
141
142 void __init prom_free_prom_memory(void)
143 {
144         /* We do not have to prom memory to free */
145 }