machdep.c revision 1.100 1 /* $NetBSD: machdep.c,v 1.100 2007/12/27 17:25:02 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.100 2007/12/27 17:25:02 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
63 #include "com.h"
64 #if (NCOM > 0)
65 #include <sys/termios.h>
66 #include <dev/ic/comreg.h>
67 #include <dev/ic/comvar.h>
68 #endif
69
70 struct pmap ofw_pmap;
71 char bootpath[256];
72
73 void ofwppc_batinit(void);
74 void ofppc_bootstrap_console(void);
75
76 extern u_int l2cr_config;
77
78
79 void
80 initppc(u_int startkernel, u_int endkernel, char *args)
81 {
82 ofwoea_initppc(startkernel, endkernel, args);
83 }
84
85 /* perform model-specific actions at initppc() */
86 void
87 model_init(void)
88 {
89 int qhandle, phandle;
90
91 /* Pegasos1, Pegasos2 */
92 if (strncmp(model_name, "Pegasos", 7) == 0) {
93 static uint16_t modew[] = { 640, 800, 1024, 1280, 0 };
94 static uint16_t modeh[] = { 480, 600, 768, 1024, 0 };
95 uint32_t width, height, mode, fbaddr;
96 char buf[32];
97 int i;
98
99 /* the pegasos doesn't bother to set the L2 cache up*/
100 l2cr_config = L2CR_L2PE;
101
102 /* fix the device_type property of a graphics card */
103 for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
104 if (OF_getprop(qhandle, "name", buf, sizeof buf) > 0
105 && strncmp(buf, "display", 7) == 0) {
106 OF_setprop(qhandle, "device_type", "display", 8);
107 break;
108 }
109 if ((phandle = OF_child(qhandle)))
110 continue;
111 while (qhandle) {
112 if ((phandle = OF_peer(qhandle)))
113 break;
114 qhandle = OF_parent(qhandle);
115 }
116 }
117
118 /*
119 * Get screen width/height and switch to framebuffer mode.
120 * The default dimensions are: 800 x 600
121 */
122 OF_interpret("screen-width", 0, 1, &width);
123 if (width == 0)
124 width = 800;
125
126 OF_interpret("screen-height", 0, 1, &height);
127 if (height == 0)
128 height = 600;
129
130 /* find VESA mode */
131 for (i = 0, mode = 0; modew[i] != 0; i++) {
132 if (modew[i] == width && modeh[i] == height) {
133 mode = 0x101 + 2 * i;
134 break;
135 }
136 }
137 if (!mode) {
138 mode = 0x102;
139 width = 800;
140 height = 600;
141 }
142
143 /* init frame buffer mode */
144 sprintf(buf, "%x vesa-set-mode", mode);
145 OF_interpret(buf, 0, 0);
146
147 /* set dimensions and frame buffer address in OFW */
148 sprintf(buf, "%x to screen-width", width);
149 OF_interpret(buf, 0, 0);
150 sprintf(buf, "%x to screen-height", height);
151 OF_interpret(buf, 0, 0);
152 OF_interpret("vesa-frame-buffer-adr", 0, 1, &fbaddr);
153 if (fbaddr != 0) {
154 sprintf(buf, "%x to frame-buffer-adr", fbaddr);
155 OF_interpret(buf, 0, 0);
156 }
157 }
158 }
159
160 void
161 cpu_startup(void)
162 {
163 oea_startup(model_name[0] ? model_name : NULL);
164 }
165
166
167 void
168 consinit(void)
169 {
170 ofwoea_consinit();
171 }
172
173
174 void
175 dumpsys(void)
176 {
177 printf("dumpsys: TBD\n");
178 }
179
180 /*
181 * Halt or reboot the machine after syncing/dumping according to howto.
182 */
183 void rtas_reboot(void);
184
185 void
186 cpu_reboot(int howto, char *what)
187 {
188 static int syncing;
189 static char str[256];
190 char *ap = str, *ap1 = ap;
191
192 boothowto = howto;
193 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
194 syncing = 1;
195 vfs_shutdown(); /* sync */
196 resettodr(); /* set wall clock */
197 }
198 splhigh();
199 if (howto & RB_HALT) {
200 doshutdownhooks();
201 printf("halted\n\n");
202 ppc_exit();
203 }
204 if (!cold && (howto & RB_DUMP))
205 oea_dumpsys();
206 doshutdownhooks();
207 printf("rebooting\n\n");
208
209 rtas_reboot();
210 for(;;);
211
212 if (what && *what) {
213 if (strlen(what) > sizeof str - 5)
214 printf("boot string too large, ignored\n");
215 else {
216 strcpy(str, what);
217 ap1 = ap = str + strlen(str);
218 *ap++ = ' ';
219 }
220 }
221 *ap++ = '-';
222 if (howto & RB_SINGLE)
223 *ap++ = 's';
224 if (howto & RB_KDB)
225 *ap++ = 'd';
226 *ap++ = 0;
227 if (ap[-2] == '-')
228 *ap1 = 0;
229 ppc_boot(str);
230 }
231
232 /*
233 */
234
235 #define divrnd(n, q) (((n)*2/(q)+1)/2)
236
237 void
238 ofppc_init_comcons(int isa_node)
239 {
240 #if (NCOM > 0)
241 char name[64];
242 uint32_t reg[2], comfreq;
243 uint8_t dll, dlm;
244 int speed, rate, err, com_node, child;
245
246 /* if we have a serial cons, we have work to do */
247 memset(name, 0, sizeof(name));
248 OF_getprop(console_node, "device_type", name, sizeof(name));
249 if (strcmp(name, "serial") != 0)
250 return;
251
252 /* scan ISA children for serial devices to match our console */
253 com_node = -1;
254 for (child = OF_child(isa_node); child; child = OF_peer(child)) {
255 memset(name, 0, sizeof(name));
256 OF_getprop(child, "device_type", name, sizeof(name));
257 if (strcmp(name, "serial") == 0) {
258 /*
259 * Serial device even matches our console_node?
260 * Then we're done!
261 */
262 if (child == console_node) {
263 com_node = child;
264 break;
265 }
266 /* remember first serial device found */
267 if (com_node == -1)
268 com_node = child;
269 }
270 }
271
272 if (com_node == -1)
273 return;
274
275 if (OF_getprop(com_node, "reg", reg, sizeof(reg)) == -1)
276 return;
277
278 if (OF_getprop(com_node, "clock-frequency", &comfreq, 4) == -1)
279 comfreq = 0;
280
281 if (comfreq == 0)
282 comfreq = COM_FREQ;
283
284 isa_outb(reg[1] + com_cfcr, LCR_DLAB);
285 dll = isa_inb(reg[1] + com_dlbl);
286 dlm = isa_inb(reg[1] + com_dlbh);
287 rate = dll | (dlm << 8);
288 isa_outb(reg[1] + com_cfcr, LCR_8BITS);
289 speed = divrnd((comfreq / 16), rate);
290 err = speed - (speed + 150)/300 * 300;
291 speed -= err;
292 if (err < 0)
293 err = -err;
294 if (err > 50)
295 speed = 9600;
296
297 /* Now we can attach the comcons */
298 aprint_verbose("Switching to COM console at speed %d", speed);
299 if (comcnattach(&genppc_isa_io_space_tag, reg[1],
300 speed, comfreq, COM_TYPE_NORMAL,
301 ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)))
302 panic("Can't init serial console");
303 aprint_verbose("\n");
304 #endif /*NCOM*/
305 }
306
307 void
308 copy_disp_props(struct device *dev, int node, prop_dictionary_t dict)
309 {
310 uint32_t temp;
311 char typestr[32];
312
313 memset(typestr, 0, sizeof(typestr));
314 OF_getprop(console_node, "device_type", typestr, sizeof(typestr));
315 if (strcmp(typestr, "serial") != 0) {
316 /* this is our console, when we don't have a serial console */
317 prop_dictionary_set_bool(dict, "is_console", 1);
318 }
319
320 if (!of_to_uint32_prop(dict, node, "width", "width")) {
321
322 OF_interpret("screen-width", 0, 1, &temp);
323 prop_dictionary_set_uint32(dict, "width", temp);
324 }
325 if (!of_to_uint32_prop(dict, node, "height", "height")) {
326
327 OF_interpret("screen-height", 0, 1, &temp);
328 prop_dictionary_set_uint32(dict, "height", temp);
329 }
330 of_to_uint32_prop(dict, node, "linebytes", "linebytes");
331 if (!of_to_uint32_prop(dict, node, "depth", "depth")) {
332 /*
333 * XXX we should check linebytes vs. width but those
334 * FBs that don't have a depth property ( /chaos/control... )
335 * won't have linebytes either
336 */
337 prop_dictionary_set_uint32(dict, "depth", 8);
338 }
339 if (!of_to_uint32_prop(dict, node, "address", "address")) {
340 uint32_t fbaddr = 0;
341 OF_interpret("frame-buffer-adr", 0, 1, &fbaddr);
342 if (fbaddr != 0)
343 prop_dictionary_set_uint32(dict, "address", fbaddr);
344 }
345 }
346