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