edc3c5e46ab0b64c43a81273e90b35ebd32f0199
[openwrt/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 = "WHR-G300N",
37 .mach_type = RAMIPS_MACH_WHR_G300N,
38 }
39 };
40
41 static inline void *to_ram_addr(void *addr)
42 {
43 u32 base;
44
45 base = KSEG0ADDR(RALINK_SOC_SDRAM_BASE);
46 if (((u32) addr > base) &&
47 ((u32) addr < (base + RALINK_SOC_MEM_SIZE_MAX)))
48 return addr;
49
50 base = KSEG1ADDR(RALINK_SOC_SDRAM_BASE);
51 if (((u32) addr > base) &&
52 ((u32) addr < (base + RALINK_SOC_MEM_SIZE_MAX)))
53 return addr;
54
55 /* some U-Boot variants uses physical addresses */
56 base = RALINK_SOC_SDRAM_BASE;
57 if (((u32) addr > base) &&
58 ((u32) addr < (base + RALINK_SOC_MEM_SIZE_MAX)))
59 return (void *)KSEG0ADDR(addr);
60
61 return NULL;
62 }
63
64 static __init char *ramips_prom_getargv(const char *name)
65 {
66 int len = strlen(name);
67 int i;
68
69 if (!ramips_prom_argv) {
70 printk(KERN_DEBUG "argv=%p is invalid, skipping\n",
71 ramips_prom_argv);
72 return NULL;
73 }
74
75 for (i = 0; i < ramips_prom_argc; i++) {
76 char *argv = to_ram_addr(ramips_prom_argv[i]);
77
78 if (!argv) {
79 printk(KERN_DEBUG
80 "argv[%d]=%p is invalid, skipping\n",
81 i, ramips_prom_argv[i]);
82 continue;
83 }
84
85 printk(KERN_DEBUG "argv[%d]: %s\n", i, argv);
86 if (strncmp(name, argv, len) == 0 && (argv)[len] == '=')
87 return argv + len + 1;
88 }
89
90 return NULL;
91 }
92
93 static __init char *ramips_prom_getenv(const char *envname)
94 {
95 #define PROM_MAX_ENVS 256
96 int len = strlen(envname);
97 char **env;
98 int i;
99
100 env = ramips_prom_envp;
101 if (!env) {
102 printk(KERN_DEBUG "envp=%p is not in RAM, skipping\n",
103 ramips_prom_envp);
104 return NULL;
105 }
106
107 for (i = 0; i < PROM_MAX_ENVS; i++) {
108 char *p = to_ram_addr(env[i]);
109
110 if (!p)
111 break;
112
113 printk(KERN_DEBUG "env[%d]: %s\n", i, p);
114 if (strncmp(envname, p, len) == 0 && p[len] == '=')
115 return p + len + 1;
116 }
117
118 return NULL;
119 #undef PROM_MAX_ENVS
120 }
121
122 static __init void find_board_byname(char *name)
123 {
124 int i;
125
126 for (i = 0; i < ARRAY_SIZE(boards); i++)
127 if (strcmp(name, boards[i].name) == 0) {
128 ramips_mach = boards[i].mach_type;
129 break;
130 }
131 }
132
133 void __init prom_init(void)
134 {
135 char *p;
136
137 printk(KERN_DEBUG
138 "prom: fw_arg0=%08x, fw_arg1=%08x, fw_arg2=%08x, fw_arg3=%08x\n",
139 (unsigned int)fw_arg0, (unsigned int)fw_arg1,
140 (unsigned int)fw_arg2, (unsigned int)fw_arg3);
141
142 ramips_prom_argc = fw_arg0;
143 ramips_prom_argv = to_ram_addr((void *)fw_arg1);
144 ramips_prom_envp = to_ram_addr((void *)fw_arg2);
145
146 p = ramips_prom_getargv("board");
147 if (!p)
148 p = ramips_prom_getenv("board");
149 if (p)
150 find_board_byname(p);
151 }
152
153 void __init prom_free_prom_memory(void)
154 {
155 /* We do not have to prom memory to free */
156 }