blob: 820b8480f2223850f56169e6570e50fed0ae8de3 [file] [log] [blame]
David VomLehna3a0f8c2009-08-30 17:15:11 -07001/*
2 * Carsten Langgaard, carstenl@mips.com
3 * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved.
4 * Portions copyright (C) 2009 Cisco Systems, Inc.
5 *
6 * This program is free software; you can distribute it and/or modify it
7 * under the terms of the GNU General Public License (Version 2) as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
18 */
19#include <linux/init.h>
20#include <linux/sched.h>
21#include <linux/ioport.h>
22#include <linux/pci.h>
23#include <linux/screen_info.h>
24#include <linux/notifier.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
27#include <linux/ctype.h>
David VomLehna3a0f8c2009-08-30 17:15:11 -070028#include <linux/cpu.h>
David VomLehn81fc0172010-02-22 16:31:13 -080029#include <linux/time.h>
30
David VomLehna3a0f8c2009-08-30 17:15:11 -070031#include <asm/bootinfo.h>
32#include <asm/irq.h>
33#include <asm/mips-boards/generic.h>
34#include <asm/mips-boards/prom.h>
35#include <asm/dma.h>
David VomLehn81fc0172010-02-22 16:31:13 -080036#include <asm/asm.h>
David VomLehna3a0f8c2009-08-30 17:15:11 -070037#include <asm/traps.h>
38#include <asm/asm-offsets.h>
39#include "reset.h"
40
41#define VAL(n) STR(n)
42
43/*
44 * Macros for loading addresses and storing registers:
David VomLehn81fc0172010-02-22 16:31:13 -080045 * LONG_L_ Stringified version of LONG_L for use in asm() statement
46 * LONG_S_ Stringified version of LONG_S for use in asm() statement
47 * PTR_LA_ Stringified version of PTR_LA for use in asm() statement
David VomLehna3a0f8c2009-08-30 17:15:11 -070048 * REG_SIZE Number of 8-bit bytes in a full width register
49 */
David VomLehn81fc0172010-02-22 16:31:13 -080050#define LONG_L_ VAL(LONG_L) " "
51#define LONG_S_ VAL(LONG_S) " "
52#define PTR_LA_ VAL(PTR_LA) " "
53
David VomLehna3a0f8c2009-08-30 17:15:11 -070054#ifdef CONFIG_64BIT
55#warning TODO: 64-bit code needs to be verified
David VomLehna3a0f8c2009-08-30 17:15:11 -070056#define REG_SIZE "8" /* In bytes */
57#endif
58
59#ifdef CONFIG_32BIT
David VomLehna3a0f8c2009-08-30 17:15:11 -070060#define REG_SIZE "4" /* In bytes */
61#endif
62
David VomLehna3a0f8c2009-08-30 17:15:11 -070063static void register_panic_notifier(void);
64static int panic_handler(struct notifier_block *notifier_block,
65 unsigned long event, void *cause_string);
66
67const char *get_system_type(void)
68{
69 return "PowerTV";
70}
71
72void __init plat_mem_setup(void)
73{
74 panic_on_oops = 1;
75 register_panic_notifier();
76
77#if 0
78 mips_pcibios_init();
79#endif
80 mips_reboot_setup();
81}
82
83/*
84 * Install a panic notifier for platform-specific diagnostics
85 */
86static void register_panic_notifier()
87{
88 static struct notifier_block panic_notifier = {
89 .notifier_call = panic_handler,
90 .next = NULL,
91 .priority = INT_MAX
92 };
93 atomic_notifier_chain_register(&panic_notifier_list, &panic_notifier);
94}
95
96static int panic_handler(struct notifier_block *notifier_block,
97 unsigned long event, void *cause_string)
98{
99 struct pt_regs my_regs;
100
101 /* Save all of the registers */
102 {
103 unsigned long at, v0, v1; /* Must be on the stack */
104
105 /* Start by saving $at and v0 on the stack. We use $at
106 * ourselves, but it looks like the compiler may use v0 or v1
107 * to load the address of the pt_regs structure. We'll come
108 * back later to store the registers in the pt_regs
109 * structure. */
110 __asm__ __volatile__ (
111 ".set noat\n"
David VomLehn81fc0172010-02-22 16:31:13 -0800112 LONG_S_ "$at, %[at]\n"
113 LONG_S_ "$2, %[v0]\n"
114 LONG_S_ "$3, %[v1]\n"
David VomLehna3a0f8c2009-08-30 17:15:11 -0700115 :
116 [at] "=m" (at),
117 [v0] "=m" (v0),
118 [v1] "=m" (v1)
119 :
120 : "at"
121 );
122
123 __asm__ __volatile__ (
124 ".set noat\n"
125 "move $at, %[pt_regs]\n"
126
127 /* Argument registers */
David VomLehn81fc0172010-02-22 16:31:13 -0800128 LONG_S_ "$4, " VAL(PT_R4) "($at)\n"
129 LONG_S_ "$5, " VAL(PT_R5) "($at)\n"
130 LONG_S_ "$6, " VAL(PT_R6) "($at)\n"
131 LONG_S_ "$7, " VAL(PT_R7) "($at)\n"
David VomLehna3a0f8c2009-08-30 17:15:11 -0700132
133 /* Temporary regs */
David VomLehn81fc0172010-02-22 16:31:13 -0800134 LONG_S_ "$8, " VAL(PT_R8) "($at)\n"
135 LONG_S_ "$9, " VAL(PT_R9) "($at)\n"
136 LONG_S_ "$10, " VAL(PT_R10) "($at)\n"
137 LONG_S_ "$11, " VAL(PT_R11) "($at)\n"
138 LONG_S_ "$12, " VAL(PT_R12) "($at)\n"
139 LONG_S_ "$13, " VAL(PT_R13) "($at)\n"
140 LONG_S_ "$14, " VAL(PT_R14) "($at)\n"
141 LONG_S_ "$15, " VAL(PT_R15) "($at)\n"
David VomLehna3a0f8c2009-08-30 17:15:11 -0700142
143 /* "Saved" registers */
David VomLehn81fc0172010-02-22 16:31:13 -0800144 LONG_S_ "$16, " VAL(PT_R16) "($at)\n"
145 LONG_S_ "$17, " VAL(PT_R17) "($at)\n"
146 LONG_S_ "$18, " VAL(PT_R18) "($at)\n"
147 LONG_S_ "$19, " VAL(PT_R19) "($at)\n"
148 LONG_S_ "$20, " VAL(PT_R20) "($at)\n"
149 LONG_S_ "$21, " VAL(PT_R21) "($at)\n"
150 LONG_S_ "$22, " VAL(PT_R22) "($at)\n"
151 LONG_S_ "$23, " VAL(PT_R23) "($at)\n"
David VomLehna3a0f8c2009-08-30 17:15:11 -0700152
153 /* Add'l temp regs */
David VomLehn81fc0172010-02-22 16:31:13 -0800154 LONG_S_ "$24, " VAL(PT_R24) "($at)\n"
155 LONG_S_ "$25, " VAL(PT_R25) "($at)\n"
David VomLehna3a0f8c2009-08-30 17:15:11 -0700156
157 /* Kernel temp regs */
David VomLehn81fc0172010-02-22 16:31:13 -0800158 LONG_S_ "$26, " VAL(PT_R26) "($at)\n"
159 LONG_S_ "$27, " VAL(PT_R27) "($at)\n"
David VomLehna3a0f8c2009-08-30 17:15:11 -0700160
161 /* Global pointer, stack pointer, frame pointer and
162 * return address */
David VomLehn81fc0172010-02-22 16:31:13 -0800163 LONG_S_ "$gp, " VAL(PT_R28) "($at)\n"
164 LONG_S_ "$sp, " VAL(PT_R29) "($at)\n"
165 LONG_S_ "$fp, " VAL(PT_R30) "($at)\n"
166 LONG_S_ "$ra, " VAL(PT_R31) "($at)\n"
David VomLehna3a0f8c2009-08-30 17:15:11 -0700167
168 /* Now we can get the $at and v0 registers back and
169 * store them */
David VomLehn81fc0172010-02-22 16:31:13 -0800170 LONG_L_ "$8, %[at]\n"
171 LONG_S_ "$8, " VAL(PT_R1) "($at)\n"
172 LONG_L_ "$8, %[v0]\n"
173 LONG_S_ "$8, " VAL(PT_R2) "($at)\n"
174 LONG_L_ "$8, %[v1]\n"
175 LONG_S_ "$8, " VAL(PT_R3) "($at)\n"
David VomLehna3a0f8c2009-08-30 17:15:11 -0700176 :
177 :
178 [at] "m" (at),
179 [v0] "m" (v0),
180 [v1] "m" (v1),
181 [pt_regs] "r" (&my_regs)
182 : "at", "t0"
183 );
184
185 /* Set the current EPC value to be the current location in this
186 * function */
187 __asm__ __volatile__ (
188 ".set noat\n"
189 "1:\n"
David VomLehn81fc0172010-02-22 16:31:13 -0800190 PTR_LA_ "$at, 1b\n"
191 LONG_S_ "$at, %[cp0_epc]\n"
David VomLehna3a0f8c2009-08-30 17:15:11 -0700192 :
193 [cp0_epc] "=m" (my_regs.cp0_epc)
194 :
195 : "at"
196 );
197
198 my_regs.cp0_cause = read_c0_cause();
199 my_regs.cp0_status = read_c0_status();
200 }
201
David VomLehna3a0f8c2009-08-30 17:15:11 -0700202 pr_crit("I'm feeling a bit sleepy. hmmmmm... perhaps a nap would... "
203 "zzzz... \n");
David VomLehna3a0f8c2009-08-30 17:15:11 -0700204
205 return NOTIFY_DONE;
206}
207
David VomLehna3a0f8c2009-08-30 17:15:11 -0700208/* Information about the RF MAC address, if one was supplied on the
209 * command line. */
210static bool have_rfmac;
211static u8 rfmac[ETH_ALEN];
212
213static int rfmac_param(char *p)
214{
215 u8 *q;
216 bool is_high_nibble;
217 int c;
218
219 /* Skip a leading "0x", if present */
220 if (*p == '0' && *(p+1) == 'x')
221 p += 2;
222
223 q = rfmac;
224 is_high_nibble = true;
225
226 for (c = (unsigned char) *p++;
227 isxdigit(c) && q - rfmac < ETH_ALEN;
228 c = (unsigned char) *p++) {
229 int nibble;
230
231 nibble = (isdigit(c) ? (c - '0') :
232 (isupper(c) ? c - 'A' + 10 : c - 'a' + 10));
233
234 if (is_high_nibble)
235 *q = nibble << 4;
236 else
237 *q++ |= nibble;
238
239 is_high_nibble = !is_high_nibble;
240 }
241
242 /* If we parsed all the way to the end of the parameter value and
243 * parsed all ETH_ALEN bytes, we have a usable RF MAC address */
244 have_rfmac = (c == '\0' && q - rfmac == ETH_ALEN);
245
246 return 0;
247}
248
249early_param("rfmac", rfmac_param);
250
251/*
252 * Generate an Ethernet MAC address that has a good chance of being unique.
253 * @addr: Pointer to six-byte array containing the Ethernet address
254 * Generates an Ethernet MAC address that is highly likely to be unique for
255 * this particular system on a network with other systems of the same type.
256 *
Joe Perches6e5928f2012-07-12 22:33:12 -0700257 * The problem we are solving is that, when eth_random_addr() is used to
David VomLehna3a0f8c2009-08-30 17:15:11 -0700258 * generate MAC addresses at startup, there isn't much entropy for the random
259 * number generator to use and the addresses it produces are fairly likely to
260 * be the same as those of other identical systems on the same local network.
261 * This is true even for relatively small numbers of systems (for the reason
262 * why, see the Wikipedia entry for "Birthday problem" at:
263 * http://en.wikipedia.org/wiki/Birthday_problem
264 *
265 * The good news is that we already have a MAC address known to be unique, the
266 * RF MAC address. The bad news is that this address is already in use on the
267 * RF interface. Worse, the obvious trick, taking the RF MAC address and
268 * turning on the locally managed bit, has already been used for other devices.
269 * Still, this does give us something to work with.
270 *
271 * The approach we take is:
Joe Perches6e5928f2012-07-12 22:33:12 -0700272 * 1. If we can't get the RF MAC Address, just call eth_random_addr.
David VomLehna3a0f8c2009-08-30 17:15:11 -0700273 * 2. Use the 24-bit NIC-specific bits of the RF MAC address as the last 24
274 * bits of the new address. This is very likely to be unique, except for
275 * the current box.
276 * 3. To avoid using addresses already on the current box, we set the top
277 * six bits of the address with a value different from any currently
278 * registered Scientific Atlanta organizationally unique identifyer
279 * (OUI). This avoids duplication with any addresses on the system that
280 * were generated from valid Scientific Atlanta-registered address by
281 * simply flipping the locally managed bit.
282 * 4. We aren't generating a multicast address, so we leave the multicast
283 * bit off. Since we aren't using a registered address, we have to set
284 * the locally managed bit.
285 * 5. We then randomly generate the remaining 16-bits. This does two
286 * things:
287 * a. It allows us to call this function for more than one device
288 * in this system
289 * b. It ensures that things will probably still work even if
290 * some device on the device network has a locally managed
291 * address that matches the top six bits from step 2.
292 */
293void platform_random_ether_addr(u8 addr[ETH_ALEN])
294{
295 const int num_random_bytes = 2;
296 const unsigned char non_sciatl_oui_bits = 0xc0u;
297 const unsigned char mac_addr_locally_managed = (1 << 1);
298
299 if (!have_rfmac) {
300 pr_warning("rfmac not available on command line; "
301 "generating random MAC address\n");
Joe Perches6e5928f2012-07-12 22:33:12 -0700302 eth_random_addr(addr);
David VomLehna3a0f8c2009-08-30 17:15:11 -0700303 }
304
305 else {
306 int i;
307
308 /* Set the first byte to something that won't match a Scientific
309 * Atlanta OUI, is locally managed, and isn't a multicast
310 * address */
311 addr[0] = non_sciatl_oui_bits | mac_addr_locally_managed;
312
313 /* Get some bytes of random address information */
314 get_random_bytes(&addr[1], num_random_bytes);
315
316 /* Copy over the NIC-specific bits of the RF MAC address */
317 for (i = 1 + num_random_bytes; i < ETH_ALEN; i++)
318 addr[i] = rfmac[i];
319 }
320}