xref: /linux/arch/mips/alchemy/common/prom.c (revision 36f353a1ebf88280f58d1ebfe2731251d9159456)
1 /*
2  *
3  * BRIEF MODULE DESCRIPTION
4  *    PROM library initialisation code, supports YAMON and U-Boot.
5  *
6  * Copyright 2000-2001, 2006, 2008 MontaVista Software Inc.
7  * Author: MontaVista Software, Inc. <source@mvista.com>
8  *
9  * This file was derived from Carsten Langgaard's
10  * arch/mips/mips-boards/xx files.
11  *
12  * Carsten Langgaard, carstenl@mips.com
13  * Copyright (C) 1999,2000 MIPS Technologies, Inc.  All rights reserved.
14  *
15  *  This program is free software; you can redistribute  it and/or modify it
16  *  under  the terms of  the GNU General  Public License as published by the
17  *  Free Software Foundation;  either version 2 of the  License, or (at your
18  *  option) any later version.
19  *
20  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
21  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
22  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
23  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
24  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
26  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
28  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  *  You should have received a copy of the  GNU General Public License along
32  *  with this program; if not, write  to the Free Software Foundation, Inc.,
33  *  675 Mass Ave, Cambridge, MA 02139, USA.
34  */
35 
36 #include <linux/init.h>
37 #include <linux/kernel.h>
38 #include <linux/memblock.h>
39 #include <linux/sizes.h>
40 #include <linux/string.h>
41 
42 #include <asm/bootinfo.h>
43 #include <prom.h>
44 
45 int prom_argc;
46 char **prom_argv;
47 char **prom_envp;
48 
49 void __init prom_init_cmdline(void)
50 {
51 	int i;
52 
53 	for (i = 1; i < prom_argc; i++) {
54 		strlcat(arcs_cmdline, prom_argv[i], COMMAND_LINE_SIZE);
55 		if (i < (prom_argc - 1))
56 			strlcat(arcs_cmdline, " ", COMMAND_LINE_SIZE);
57 	}
58 }
59 
60 char *prom_getenv(char *envname)
61 {
62 	/*
63 	 * Return a pointer to the given environment variable.
64 	 * YAMON uses "name", "value" pairs, while U-Boot uses "name=value".
65 	 */
66 
67 	char **env = prom_envp;
68 	int i = strlen(envname);
69 	int yamon = (*env && strchr(*env, '=') == NULL);
70 
71 	while (*env) {
72 		if (yamon) {
73 			if (strcmp(envname, *env++) == 0)
74 				return *env;
75 		} else if (strncmp(envname, *env, i) == 0 && (*env)[i] == '=')
76 			return *env + i + 1;
77 		env++;
78 	}
79 
80 	return NULL;
81 }
82 
83 void __init prom_init(void)
84 {
85 	unsigned char *memsize_str;
86 	unsigned long memsize;
87 
88 	prom_argc = (int)fw_arg0;
89 	prom_argv = (char **)fw_arg1;
90 	prom_envp = (char **)fw_arg2;
91 
92 	prom_init_cmdline();
93 
94 	memsize_str = prom_getenv("memsize");
95 	if (!memsize_str || kstrtoul(memsize_str, 0, &memsize))
96 		memsize = SZ_64M; /* minimum memsize is 64MB RAM */
97 
98 	memblock_add(0, memsize);
99 }
100 
101 static inline unsigned char str2hexnum(unsigned char c)
102 {
103 	if (c >= '0' && c <= '9')
104 		return c - '0';
105 	if (c >= 'a' && c <= 'f')
106 		return c - 'a' + 10;
107 	if (c >= 'A' && c <= 'F')
108 		return c - 'A' + 10;
109 
110 	return 0; /* foo */
111 }
112 
113 static inline void str2eaddr(unsigned char *ea, unsigned char *str)
114 {
115 	int i;
116 
117 	for (i = 0; i < 6; i++) {
118 		unsigned char num;
119 
120 		if ((*str == '.') || (*str == ':'))
121 			str++;
122 		num  = str2hexnum(*str++) << 4;
123 		num |= str2hexnum(*str++);
124 		ea[i] = num;
125 	}
126 }
127 
128 int __init prom_get_ethernet_addr(char *ethernet_addr)
129 {
130 	char *ethaddr_str;
131 
132 	/* Check the environment variables first */
133 	ethaddr_str = prom_getenv("ethaddr");
134 	if (!ethaddr_str) {
135 		/* Check command line */
136 		ethaddr_str = strstr(arcs_cmdline, "ethaddr=");
137 		if (!ethaddr_str)
138 			return -1;
139 
140 		ethaddr_str += strlen("ethaddr=");
141 	}
142 
143 	str2eaddr(ethernet_addr, ethaddr_str);
144 
145 	return 0;
146 }
147