machdep.c revision 1.103 1 /* $NetBSD: machdep.c,v 1.103 2007/12/28 05:12:41 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.103 2007/12/28 05:12:41 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 extern int machine_has_rtas;
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 if ((howto & 0x800) && machine_has_rtas &&
204 rtas_has_func(RTAS_FUNC_POWER_OFF))
205 rtas_call(RTAS_FUNC_POWER_OFF, 2, 1, 0, 0, &junk);
206 ppc_exit();
207 }
208 if (!cold && (howto & RB_DUMP))
209 oea_dumpsys();
210 doshutdownhooks();
211 aprint_normal("rebooting\n\n");
212
213 if (machine_has_rtas && rtas_has_func(RTAS_FUNC_SYSTEM_REBOOT)) {
214 rtas_call(RTAS_FUNC_SYSTEM_REBOOT, 0, 1, &junk);
215 for(;;);
216 }
217
218 if (what && *what) {
219 if (strlen(what) > sizeof str - 5)
220 aprint_normal("boot string too large, ignored\n");
221 else {
222 strcpy(str, what);
223 ap1 = ap = str + strlen(str);
224 *ap++ = ' ';
225 }
226 }
227 *ap++ = '-';
228 if (howto & RB_SINGLE)
229 *ap++ = 's';
230 if (howto & RB_KDB)
231 *ap++ = 'd';
232 *ap++ = 0;
233 if (ap[-2] == '-')
234 *ap1 = 0;
235 ppc_boot(str);
236 }
237
238 /*
239 */
240
241 #define divrnd(n, q) (((n)*2/(q)+1)/2)
242
243 void
244 ofppc_init_comcons(int isa_node)
245 {
246 #if (NCOM > 0)
247 char name[64];
248 uint32_t reg[2], comfreq;
249 uint8_t dll, dlm;
250 int speed, rate, err, com_node, child;
251
252 /* if we have a serial cons, we have work to do */
253 memset(name, 0, sizeof(name));
254 OF_getprop(console_node, "device_type", name, sizeof(name));
255 if (strcmp(name, "serial") != 0)
256 return;
257
258 /* scan ISA children for serial devices to match our console */
259 com_node = -1;
260 for (child = OF_child(isa_node); child; child = OF_peer(child)) {
261 memset(name, 0, sizeof(name));
262 OF_getprop(child, "device_type", name, sizeof(name));
263 if (strcmp(name, "serial") == 0) {
264 /*
265 * Serial device even matches our console_node?
266 * Then we're done!
267 */
268 if (child == console_node) {
269 com_node = child;
270 break;
271 }
272 /* remember first serial device found */
273 if (com_node == -1)
274 com_node = child;
275 }
276 }
277
278 if (com_node == -1)
279 return;
280
281 if (OF_getprop(com_node, "reg", reg, sizeof(reg)) == -1)
282 return;
283
284 if (OF_getprop(com_node, "clock-frequency", &comfreq, 4) == -1)
285 comfreq = 0;
286
287 if (comfreq == 0)
288 comfreq = COM_FREQ;
289
290 isa_outb(reg[1] + com_cfcr, LCR_DLAB);
291 dll = isa_inb(reg[1] + com_dlbl);
292 dlm = isa_inb(reg[1] + com_dlbh);
293 rate = dll | (dlm << 8);
294 isa_outb(reg[1] + com_cfcr, LCR_8BITS);
295 speed = divrnd((comfreq / 16), rate);
296 err = speed - (speed + 150)/300 * 300;
297 speed -= err;
298 if (err < 0)
299 err = -err;
300 if (err > 50)
301 speed = 9600;
302
303 /* Now we can attach the comcons */
304 aprint_verbose("Switching to COM console at speed %d", speed);
305 if (comcnattach(&genppc_isa_io_space_tag, reg[1],
306 speed, comfreq, COM_TYPE_NORMAL,
307 ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)))
308 panic("Can't init serial console");
309 aprint_verbose("\n");
310 #endif /*NCOM*/
311 }
312
313 void
314 copy_disp_props(struct device *dev, int node, prop_dictionary_t dict)
315 {
316 uint32_t temp;
317 char typestr[32];
318
319 memset(typestr, 0, sizeof(typestr));
320 OF_getprop(console_node, "device_type", typestr, sizeof(typestr));
321 if (strcmp(typestr, "serial") != 0) {
322 /* this is our console, when we don't have a serial console */
323 prop_dictionary_set_bool(dict, "is_console", 1);
324 }
325
326 if (!of_to_uint32_prop(dict, node, "width", "width")) {
327
328 OF_interpret("screen-width", 0, 1, &temp);
329 prop_dictionary_set_uint32(dict, "width", temp);
330 }
331 if (!of_to_uint32_prop(dict, node, "height", "height")) {
332
333 OF_interpret("screen-height", 0, 1, &temp);
334 prop_dictionary_set_uint32(dict, "height", temp);
335 }
336 of_to_uint32_prop(dict, node, "linebytes", "linebytes");
337 if (!of_to_uint32_prop(dict, node, "depth", "depth")) {
338 /*
339 * XXX we should check linebytes vs. width but those
340 * FBs that don't have a depth property ( /chaos/control... )
341 * won't have linebytes either
342 */
343 prop_dictionary_set_uint32(dict, "depth", 8);
344 }
345 if (!of_to_uint32_prop(dict, node, "address", "address")) {
346 uint32_t fbaddr = 0;
347 OF_interpret("frame-buffer-adr", 0, 1, &fbaddr);
348 if (fbaddr != 0)
349 prop_dictionary_set_uint32(dict, "address", fbaddr);
350 }
351 }
352