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