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