machdep.c revision 1.102 1 /* $NetBSD: machdep.c,v 1.102 2007/12/28 04:47:37 garbled Exp $ */
2 /*-
3 * Copyright (c) 2007 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Tim Rightnour
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.102 2007/12/28 04:47:37 garbled Exp $");
40
41 #include <sys/param.h>
42 #include <sys/buf.h>
43 #include <sys/boot_flag.h>
44 #include <sys/mount.h>
45 #include <sys/kernel.h>
46
47 #include <uvm/uvm_extern.h>
48
49 #include <dev/ofw/openfirm.h>
50 #include <dev/cons.h>
51
52 #include <machine/autoconf.h>
53 #include <machine/pmap.h>
54 #include <machine/powerpc.h>
55 #include <machine/trap.h>
56 #include <machine/bus.h>
57 #include <machine/isa_machdep.h>
58 #include <machine/spr.h>
59
60 #include <powerpc/oea/bat.h>
61 #include <powerpc/ofw_cons.h>
62 #include <powerpc/rtas.h>
63
64 #include "com.h"
65 #if (NCOM > 0)
66 #include <sys/termios.h>
67 #include <dev/ic/comreg.h>
68 #include <dev/ic/comvar.h>
69 #endif
70
71 struct pmap ofw_pmap;
72 char bootpath[256];
73
74 void ofwppc_batinit(void);
75 void ofppc_bootstrap_console(void);
76
77 extern u_int l2cr_config;
78
79
80 void
81 initppc(u_int startkernel, u_int endkernel, char *args)
82 {
83 ofwoea_initppc(startkernel, endkernel, args);
84 }
85
86 /* perform model-specific actions at initppc() */
87 void
88 model_init(void)
89 {
90 int qhandle, phandle;
91
92 /* Pegasos1, Pegasos2 */
93 if (strncmp(model_name, "Pegasos", 7) == 0) {
94 static uint16_t modew[] = { 640, 800, 1024, 1280, 0 };
95 static uint16_t modeh[] = { 480, 600, 768, 1024, 0 };
96 uint32_t width, height, mode, fbaddr;
97 char buf[32];
98 int i;
99
100 /* the pegasos doesn't bother to set the L2 cache up*/
101 l2cr_config = L2CR_L2PE;
102
103 /* fix the device_type property of a graphics card */
104 for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
105 if (OF_getprop(qhandle, "name", buf, sizeof buf) > 0
106 && strncmp(buf, "display", 7) == 0) {
107 OF_setprop(qhandle, "device_type", "display", 8);
108 break;
109 }
110 if ((phandle = OF_child(qhandle)))
111 continue;
112 while (qhandle) {
113 if ((phandle = OF_peer(qhandle)))
114 break;
115 qhandle = OF_parent(qhandle);
116 }
117 }
118
119 /*
120 * Get screen width/height and switch to framebuffer mode.
121 * The default dimensions are: 800 x 600
122 */
123 OF_interpret("screen-width", 0, 1, &width);
124 if (width == 0)
125 width = 800;
126
127 OF_interpret("screen-height", 0, 1, &height);
128 if (height == 0)
129 height = 600;
130
131 /* find VESA mode */
132 for (i = 0, mode = 0; modew[i] != 0; i++) {
133 if (modew[i] == width && modeh[i] == height) {
134 mode = 0x101 + 2 * i;
135 break;
136 }
137 }
138 if (!mode) {
139 mode = 0x102;
140 width = 800;
141 height = 600;
142 }
143
144 /* init frame buffer mode */
145 sprintf(buf, "%x vesa-set-mode", mode);
146 OF_interpret(buf, 0, 0);
147
148 /* set dimensions and frame buffer address in OFW */
149 sprintf(buf, "%x to screen-width", width);
150 OF_interpret(buf, 0, 0);
151 sprintf(buf, "%x to screen-height", height);
152 OF_interpret(buf, 0, 0);
153 OF_interpret("vesa-frame-buffer-adr", 0, 1, &fbaddr);
154 if (fbaddr != 0) {
155 sprintf(buf, "%x to frame-buffer-adr", fbaddr);
156 OF_interpret(buf, 0, 0);
157 }
158 }
159 }
160
161 void
162 cpu_startup(void)
163 {
164 oea_startup(model_name[0] ? model_name : NULL);
165 }
166
167
168 void
169 consinit(void)
170 {
171 ofwoea_consinit();
172 }
173
174
175 void
176 dumpsys(void)
177 {
178 aprint_normal("dumpsys: TBD\n");
179 }
180
181 /*
182 * Halt or reboot the machine after syncing/dumping according to howto.
183 */
184
185 void
186 cpu_reboot(int howto, char *what)
187 {
188 static int syncing;
189 static char str[256];
190 int junk;
191 char *ap = str, *ap1 = ap;
192
193 boothowto = howto;
194 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
195 syncing = 1;
196 vfs_shutdown(); /* sync */
197 resettodr(); /* set wall clock */
198 }
199 splhigh();
200 if (howto & RB_HALT) {
201 doshutdownhooks();
202 aprint_normal("halted\n\n");
203 ppc_exit();
204 }
205 if (!cold && (howto & RB_DUMP))
206 oea_dumpsys();
207 doshutdownhooks();
208 aprint_normal("rebooting\n\n");
209
210 rtas_call(RTAS_FUNC_SYSTEM_REBOOT, 0, 1, &junk);
211 for(;;);
212
213 if (what && *what) {
214 if (strlen(what) > sizeof str - 5)
215 aprint_normal("boot string too large, ignored\n");
216 else {
217 strcpy(str, what);
218 ap1 = ap = str + strlen(str);
219 *ap++ = ' ';
220 }
221 }
222 *ap++ = '-';
223 if (howto & RB_SINGLE)
224 *ap++ = 's';
225 if (howto & RB_KDB)
226 *ap++ = 'd';
227 *ap++ = 0;
228 if (ap[-2] == '-')
229 *ap1 = 0;
230 ppc_boot(str);
231 }
232
233 /*
234 */
235
236 #define divrnd(n, q) (((n)*2/(q)+1)/2)
237
238 void
239 ofppc_init_comcons(int isa_node)
240 {
241 #if (NCOM > 0)
242 char name[64];
243 uint32_t reg[2], comfreq;
244 uint8_t dll, dlm;
245 int speed, rate, err, com_node, child;
246
247 /* if we have a serial cons, we have work to do */
248 memset(name, 0, sizeof(name));
249 OF_getprop(console_node, "device_type", name, sizeof(name));
250 if (strcmp(name, "serial") != 0)
251 return;
252
253 /* scan ISA children for serial devices to match our console */
254 com_node = -1;
255 for (child = OF_child(isa_node); child; child = OF_peer(child)) {
256 memset(name, 0, sizeof(name));
257 OF_getprop(child, "device_type", name, sizeof(name));
258 if (strcmp(name, "serial") == 0) {
259 /*
260 * Serial device even matches our console_node?
261 * Then we're done!
262 */
263 if (child == console_node) {
264 com_node = child;
265 break;
266 }
267 /* remember first serial device found */
268 if (com_node == -1)
269 com_node = child;
270 }
271 }
272
273 if (com_node == -1)
274 return;
275
276 if (OF_getprop(com_node, "reg", reg, sizeof(reg)) == -1)
277 return;
278
279 if (OF_getprop(com_node, "clock-frequency", &comfreq, 4) == -1)
280 comfreq = 0;
281
282 if (comfreq == 0)
283 comfreq = COM_FREQ;
284
285 isa_outb(reg[1] + com_cfcr, LCR_DLAB);
286 dll = isa_inb(reg[1] + com_dlbl);
287 dlm = isa_inb(reg[1] + com_dlbh);
288 rate = dll | (dlm << 8);
289 isa_outb(reg[1] + com_cfcr, LCR_8BITS);
290 speed = divrnd((comfreq / 16), rate);
291 err = speed - (speed + 150)/300 * 300;
292 speed -= err;
293 if (err < 0)
294 err = -err;
295 if (err > 50)
296 speed = 9600;
297
298 /* Now we can attach the comcons */
299 aprint_verbose("Switching to COM console at speed %d", speed);
300 if (comcnattach(&genppc_isa_io_space_tag, reg[1],
301 speed, comfreq, COM_TYPE_NORMAL,
302 ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)))
303 panic("Can't init serial console");
304 aprint_verbose("\n");
305 #endif /*NCOM*/
306 }
307
308 void
309 copy_disp_props(struct device *dev, int node, prop_dictionary_t dict)
310 {
311 uint32_t temp;
312 char typestr[32];
313
314 memset(typestr, 0, sizeof(typestr));
315 OF_getprop(console_node, "device_type", typestr, sizeof(typestr));
316 if (strcmp(typestr, "serial") != 0) {
317 /* this is our console, when we don't have a serial console */
318 prop_dictionary_set_bool(dict, "is_console", 1);
319 }
320
321 if (!of_to_uint32_prop(dict, node, "width", "width")) {
322
323 OF_interpret("screen-width", 0, 1, &temp);
324 prop_dictionary_set_uint32(dict, "width", temp);
325 }
326 if (!of_to_uint32_prop(dict, node, "height", "height")) {
327
328 OF_interpret("screen-height", 0, 1, &temp);
329 prop_dictionary_set_uint32(dict, "height", temp);
330 }
331 of_to_uint32_prop(dict, node, "linebytes", "linebytes");
332 if (!of_to_uint32_prop(dict, node, "depth", "depth")) {
333 /*
334 * XXX we should check linebytes vs. width but those
335 * FBs that don't have a depth property ( /chaos/control... )
336 * won't have linebytes either
337 */
338 prop_dictionary_set_uint32(dict, "depth", 8);
339 }
340 if (!of_to_uint32_prop(dict, node, "address", "address")) {
341 uint32_t fbaddr = 0;
342 OF_interpret("frame-buffer-adr", 0, 1, &fbaddr);
343 if (fbaddr != 0)
344 prop_dictionary_set_uint32(dict, "address", fbaddr);
345 }
346 }
347