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