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