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