rewrite of memory detection code, should be fix #1909
[openwrt/openwrt.git] / target / linux / adm5120-2.6 / files / arch / mips / adm5120 / prom.c
1 /*****************************************************************************
2 * Carsten Langgaard, carstenl@mips.com
3 * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
4 * Copyright (C) 2003 ADMtek Incorporated.
5 * daniell@admtek.com.tw
6 * Copyright (C) 2007 OpenWrt.org
7 *
8 * This program is free software; you can distribute it and/or modify it
9 * under the terms of the GNU General Public License (Version 2) as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
20 *
21 *****************************************************************************/
22
23 #include <linux/init.h>
24 #include <linux/autoconf.h>
25 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/mm.h>
28 #include <linux/bootmem.h>
29
30 #include <asm/bootinfo.h>
31 #include <asm/addrspace.h>
32
33 #include <asm/mach-adm5120/adm5120_info.h>
34
35 static char **prom_envp = NULL;
36
37 void setup_prom_printf(int);
38 void prom_printf(char *, ...);
39 void prom_meminit(void);
40
41
42 #define READCSR(r) *(volatile unsigned long *)(0xB2600000+(r))
43 #define WRITECSR(r,v) *(volatile unsigned long *)(0xB2600000+(r)) = v
44
45 #define UART_DR_REG 0x00
46 #define UART_FR_REG 0x18
47 #define UART_TX_FIFO_FULL 0x20
48
49 int putPromChar(char c)
50 {
51 WRITECSR(UART_DR_REG, c);
52 while ( (READCSR(UART_FR_REG) & UART_TX_FIFO_FULL) );
53 return 0;
54 }
55
56 /*
57 * Ugly prom_printf used for debugging
58 */
59
60 void prom_printf(char *fmt, ...)
61 {
62 va_list args;
63 int l;
64 char *p, *buf_end;
65 char buf[1024];
66
67 va_start(args, fmt);
68 l = vsprintf(buf, fmt, args); /* hopefully i < sizeof(buf) */
69 va_end(args);
70
71 buf_end = buf + l;
72
73 for (p = buf; p < buf_end; p++) {
74 /* Crude cr/nl handling is better than none */
75 if (*p == '\n')
76 putPromChar('\r');
77 putPromChar(*p);
78 }
79 }
80
81 char *prom_getenv(char *envname)
82 {
83 char **env;
84 char *ret;
85
86 ret = NULL;
87
88 if (prom_envp== NULL)
89 return NULL;
90
91 for (env = prom_envp; *env != NULL; env++) {
92 if (strcmp(envname, *env++) == 0) {
93 ret = *env;
94 break;
95 }
96 }
97
98 return ret;
99 }
100
101 extern char _image_cmdline;
102 /*
103 * initialize the prom module.
104 */
105 void __init prom_init(void)
106 {
107 char *cmd;
108
109 if ((fw_arg2 & 3) == 0) {
110 prom_envp = (char **)fw_arg2;
111 }
112
113 adm5120_info_init();
114
115 /* you should these macros defined in include/asm/bootinfo.h */
116 mips_machgroup = MACH_GROUP_ADM5120;
117 mips_machtype = adm5120_board.mach_type;
118
119 /* init command line, register a default kernel command line */
120 cmd = &_image_cmdline + 8;
121 if( strlen(cmd) > 0) strcpy( &(arcs_cmdline[0]), cmd);
122 else strcpy(&(arcs_cmdline[0]), CONFIG_CMDLINE);
123
124 /* init memory map */
125 prom_meminit();
126 }