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