Merge branches 'amd-iommu/fixes' and 'dma-debug/fixes' into iommu/fixes
[linux-2.6-block.git] / arch / mips / powertv / powertv_setup.c
CommitLineData
a3a0f8c8
DV
1/*
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>
28
29#include <linux/cpu.h>
30#include <asm/bootinfo.h>
31#include <asm/irq.h>
32#include <asm/mips-boards/generic.h>
33#include <asm/mips-boards/prom.h>
34#include <asm/dma.h>
35#include <linux/time.h>
36#include <asm/traps.h>
37#include <asm/asm-offsets.h>
38#include "reset.h"
39
40#define VAL(n) STR(n)
41
42/*
43 * Macros for loading addresses and storing registers:
44 * PTR_LA Load the address into a register
45 * LONG_S Store the full width of the given register.
46 * LONG_L Load the full width of the given register
47 * PTR_ADDIU Add a constant value to a register used as a pointer
48 * REG_SIZE Number of 8-bit bytes in a full width register
49 */
50#ifdef CONFIG_64BIT
51#warning TODO: 64-bit code needs to be verified
52#define PTR_LA "dla "
53#define LONG_S "sd "
54#define LONG_L "ld "
55#define PTR_ADDIU "daddiu "
56#define REG_SIZE "8" /* In bytes */
57#endif
58
59#ifdef CONFIG_32BIT
60#define PTR_LA "la "
61#define LONG_S "sw "
62#define LONG_L "lw "
63#define PTR_ADDIU "addiu "
64#define REG_SIZE "4" /* In bytes */
65#endif
66
a3a0f8c8
DV
67static void register_panic_notifier(void);
68static int panic_handler(struct notifier_block *notifier_block,
69 unsigned long event, void *cause_string);
70
71const char *get_system_type(void)
72{
73 return "PowerTV";
74}
75
76void __init plat_mem_setup(void)
77{
78 panic_on_oops = 1;
79 register_panic_notifier();
80
81#if 0
82 mips_pcibios_init();
83#endif
84 mips_reboot_setup();
85}
86
87/*
88 * Install a panic notifier for platform-specific diagnostics
89 */
90static void register_panic_notifier()
91{
92 static struct notifier_block panic_notifier = {
93 .notifier_call = panic_handler,
94 .next = NULL,
95 .priority = INT_MAX
96 };
97 atomic_notifier_chain_register(&panic_notifier_list, &panic_notifier);
98}
99
100static int panic_handler(struct notifier_block *notifier_block,
101 unsigned long event, void *cause_string)
102{
103 struct pt_regs my_regs;
104
105 /* Save all of the registers */
106 {
107 unsigned long at, v0, v1; /* Must be on the stack */
108
109 /* Start by saving $at and v0 on the stack. We use $at
110 * ourselves, but it looks like the compiler may use v0 or v1
111 * to load the address of the pt_regs structure. We'll come
112 * back later to store the registers in the pt_regs
113 * structure. */
114 __asm__ __volatile__ (
115 ".set noat\n"
116 LONG_S "$at, %[at]\n"
117 LONG_S "$2, %[v0]\n"
118 LONG_S "$3, %[v1]\n"
119 :
120 [at] "=m" (at),
121 [v0] "=m" (v0),
122 [v1] "=m" (v1)
123 :
124 : "at"
125 );
126
127 __asm__ __volatile__ (
128 ".set noat\n"
129 "move $at, %[pt_regs]\n"
130
131 /* Argument registers */
132 LONG_S "$4, " VAL(PT_R4) "($at)\n"
133 LONG_S "$5, " VAL(PT_R5) "($at)\n"
134 LONG_S "$6, " VAL(PT_R6) "($at)\n"
135 LONG_S "$7, " VAL(PT_R7) "($at)\n"
136
137 /* Temporary regs */
138 LONG_S "$8, " VAL(PT_R8) "($at)\n"
139 LONG_S "$9, " VAL(PT_R9) "($at)\n"
140 LONG_S "$10, " VAL(PT_R10) "($at)\n"
141 LONG_S "$11, " VAL(PT_R11) "($at)\n"
142 LONG_S "$12, " VAL(PT_R12) "($at)\n"
143 LONG_S "$13, " VAL(PT_R13) "($at)\n"
144 LONG_S "$14, " VAL(PT_R14) "($at)\n"
145 LONG_S "$15, " VAL(PT_R15) "($at)\n"
146
147 /* "Saved" registers */
148 LONG_S "$16, " VAL(PT_R16) "($at)\n"
149 LONG_S "$17, " VAL(PT_R17) "($at)\n"
150 LONG_S "$18, " VAL(PT_R18) "($at)\n"
151 LONG_S "$19, " VAL(PT_R19) "($at)\n"
152 LONG_S "$20, " VAL(PT_R20) "($at)\n"
153 LONG_S "$21, " VAL(PT_R21) "($at)\n"
154 LONG_S "$22, " VAL(PT_R22) "($at)\n"
155 LONG_S "$23, " VAL(PT_R23) "($at)\n"
156
157 /* Add'l temp regs */
158 LONG_S "$24, " VAL(PT_R24) "($at)\n"
159 LONG_S "$25, " VAL(PT_R25) "($at)\n"
160
161 /* Kernel temp regs */
162 LONG_S "$26, " VAL(PT_R26) "($at)\n"
163 LONG_S "$27, " VAL(PT_R27) "($at)\n"
164
165 /* Global pointer, stack pointer, frame pointer and
166 * return address */
167 LONG_S "$gp, " VAL(PT_R28) "($at)\n"
168 LONG_S "$sp, " VAL(PT_R29) "($at)\n"
169 LONG_S "$fp, " VAL(PT_R30) "($at)\n"
170 LONG_S "$ra, " VAL(PT_R31) "($at)\n"
171
172 /* Now we can get the $at and v0 registers back and
173 * store them */
174 LONG_L "$8, %[at]\n"
175 LONG_S "$8, " VAL(PT_R1) "($at)\n"
176 LONG_L "$8, %[v0]\n"
177 LONG_S "$8, " VAL(PT_R2) "($at)\n"
178 LONG_L "$8, %[v1]\n"
179 LONG_S "$8, " VAL(PT_R3) "($at)\n"
180 :
181 :
182 [at] "m" (at),
183 [v0] "m" (v0),
184 [v1] "m" (v1),
185 [pt_regs] "r" (&my_regs)
186 : "at", "t0"
187 );
188
189 /* Set the current EPC value to be the current location in this
190 * function */
191 __asm__ __volatile__ (
192 ".set noat\n"
193 "1:\n"
194 PTR_LA "$at, 1b\n"
195 LONG_S "$at, %[cp0_epc]\n"
196 :
197 [cp0_epc] "=m" (my_regs.cp0_epc)
198 :
199 : "at"
200 );
201
202 my_regs.cp0_cause = read_c0_cause();
203 my_regs.cp0_status = read_c0_status();
204 }
205
206#ifdef CONFIG_DIAGNOSTICS
207 failure_report((char *) cause_string,
208 have_die_regs ? &die_regs : &my_regs);
209 have_die_regs = false;
210#else
211 pr_crit("I'm feeling a bit sleepy. hmmmmm... perhaps a nap would... "
212 "zzzz... \n");
213#endif
214
215 return NOTIFY_DONE;
216}
217
a3a0f8c8
DV
218/* Information about the RF MAC address, if one was supplied on the
219 * command line. */
220static bool have_rfmac;
221static u8 rfmac[ETH_ALEN];
222
223static int rfmac_param(char *p)
224{
225 u8 *q;
226 bool is_high_nibble;
227 int c;
228
229 /* Skip a leading "0x", if present */
230 if (*p == '0' && *(p+1) == 'x')
231 p += 2;
232
233 q = rfmac;
234 is_high_nibble = true;
235
236 for (c = (unsigned char) *p++;
237 isxdigit(c) && q - rfmac < ETH_ALEN;
238 c = (unsigned char) *p++) {
239 int nibble;
240
241 nibble = (isdigit(c) ? (c - '0') :
242 (isupper(c) ? c - 'A' + 10 : c - 'a' + 10));
243
244 if (is_high_nibble)
245 *q = nibble << 4;
246 else
247 *q++ |= nibble;
248
249 is_high_nibble = !is_high_nibble;
250 }
251
252 /* If we parsed all the way to the end of the parameter value and
253 * parsed all ETH_ALEN bytes, we have a usable RF MAC address */
254 have_rfmac = (c == '\0' && q - rfmac == ETH_ALEN);
255
256 return 0;
257}
258
259early_param("rfmac", rfmac_param);
260
261/*
262 * Generate an Ethernet MAC address that has a good chance of being unique.
263 * @addr: Pointer to six-byte array containing the Ethernet address
264 * Generates an Ethernet MAC address that is highly likely to be unique for
265 * this particular system on a network with other systems of the same type.
266 *
267 * The problem we are solving is that, when random_ether_addr() is used to
268 * generate MAC addresses at startup, there isn't much entropy for the random
269 * number generator to use and the addresses it produces are fairly likely to
270 * be the same as those of other identical systems on the same local network.
271 * This is true even for relatively small numbers of systems (for the reason
272 * why, see the Wikipedia entry for "Birthday problem" at:
273 * http://en.wikipedia.org/wiki/Birthday_problem
274 *
275 * The good news is that we already have a MAC address known to be unique, the
276 * RF MAC address. The bad news is that this address is already in use on the
277 * RF interface. Worse, the obvious trick, taking the RF MAC address and
278 * turning on the locally managed bit, has already been used for other devices.
279 * Still, this does give us something to work with.
280 *
281 * The approach we take is:
282 * 1. If we can't get the RF MAC Address, just call random_ether_addr.
283 * 2. Use the 24-bit NIC-specific bits of the RF MAC address as the last 24
284 * bits of the new address. This is very likely to be unique, except for
285 * the current box.
286 * 3. To avoid using addresses already on the current box, we set the top
287 * six bits of the address with a value different from any currently
288 * registered Scientific Atlanta organizationally unique identifyer
289 * (OUI). This avoids duplication with any addresses on the system that
290 * were generated from valid Scientific Atlanta-registered address by
291 * simply flipping the locally managed bit.
292 * 4. We aren't generating a multicast address, so we leave the multicast
293 * bit off. Since we aren't using a registered address, we have to set
294 * the locally managed bit.
295 * 5. We then randomly generate the remaining 16-bits. This does two
296 * things:
297 * a. It allows us to call this function for more than one device
298 * in this system
299 * b. It ensures that things will probably still work even if
300 * some device on the device network has a locally managed
301 * address that matches the top six bits from step 2.
302 */
303void platform_random_ether_addr(u8 addr[ETH_ALEN])
304{
305 const int num_random_bytes = 2;
306 const unsigned char non_sciatl_oui_bits = 0xc0u;
307 const unsigned char mac_addr_locally_managed = (1 << 1);
308
309 if (!have_rfmac) {
310 pr_warning("rfmac not available on command line; "
311 "generating random MAC address\n");
312 random_ether_addr(addr);
313 }
314
315 else {
316 int i;
317
318 /* Set the first byte to something that won't match a Scientific
319 * Atlanta OUI, is locally managed, and isn't a multicast
320 * address */
321 addr[0] = non_sciatl_oui_bits | mac_addr_locally_managed;
322
323 /* Get some bytes of random address information */
324 get_random_bytes(&addr[1], num_random_bytes);
325
326 /* Copy over the NIC-specific bits of the RF MAC address */
327 for (i = 1 + num_random_bytes; i < ETH_ALEN; i++)
328 addr[i] = rfmac[i];
329 }
330}