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