Home | History | Annotate | Line # | Download | only in ofppc
machdep.c revision 1.111
      1  1.111  kiyohara /*	$NetBSD: machdep.c,v 1.111 2010/03/10 18:06:57 kiyohara Exp $	*/
      2   1.96   garbled /*-
      3   1.96   garbled  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      4    1.1        ws  * All rights reserved.
      5    1.1        ws  *
      6   1.96   garbled  * This code is derived from software contributed to The NetBSD Foundation
      7   1.96   garbled  * by Tim Rightnour
      8   1.96   garbled  *
      9    1.1        ws  * Redistribution and use in source and binary forms, with or without
     10    1.1        ws  * modification, are permitted provided that the following conditions
     11    1.1        ws  * are met:
     12    1.1        ws  * 1. Redistributions of source code must retain the above copyright
     13    1.1        ws  *    notice, this list of conditions and the following disclaimer.
     14    1.1        ws  * 2. Redistributions in binary form must reproduce the above copyright
     15    1.1        ws  *    notice, this list of conditions and the following disclaimer in the
     16    1.1        ws  *    documentation and/or other materials provided with the distribution.
     17    1.1        ws  *
     18   1.96   garbled  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19   1.96   garbled  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20   1.96   garbled  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21   1.96   garbled  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22   1.96   garbled  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23   1.96   garbled  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24   1.96   garbled  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25   1.96   garbled  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26   1.96   garbled  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27   1.96   garbled  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28   1.96   garbled  * POSSIBILITY OF SUCH DAMAGE.
     29    1.1        ws  */
     30   1.86     lukem 
     31   1.86     lukem #include <sys/cdefs.h>
     32  1.111  kiyohara __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.111 2010/03/10 18:06:57 kiyohara Exp $");
     33    1.1        ws 
     34    1.1        ws #include <sys/param.h>
     35  1.107    dyoung #include <sys/systm.h>
     36    1.1        ws #include <sys/buf.h>
     37   1.93   garbled #include <sys/boot_flag.h>
     38    1.1        ws #include <sys/mount.h>
     39   1.43   thorpej #include <sys/kernel.h>
     40  1.107    dyoung #include <sys/device.h>
     41    1.1        ws 
     42   1.19  sakamoto #include <uvm/uvm_extern.h>
     43   1.19  sakamoto 
     44   1.70   thorpej #include <dev/ofw/openfirm.h>
     45   1.93   garbled #include <dev/cons.h>
     46   1.70   thorpej 
     47   1.68      matt #include <machine/autoconf.h>
     48    1.1        ws #include <machine/pmap.h>
     49    1.1        ws #include <machine/powerpc.h>
     50    1.1        ws #include <machine/trap.h>
     51   1.93   garbled #include <machine/bus.h>
     52   1.93   garbled #include <machine/isa_machdep.h>
     53   1.54   thorpej 
     54  1.110      matt #include <powerpc/spr.h>
     55  1.110      matt #include <powerpc/oea/spr.h>
     56   1.87      matt #include <powerpc/oea/bat.h>
     57   1.93   garbled #include <powerpc/ofw_cons.h>
     58  1.102   garbled #include <powerpc/rtas.h>
     59   1.71   thorpej 
     60   1.97   garbled #include "com.h"
     61   1.97   garbled #if (NCOM > 0)
     62   1.97   garbled #include <sys/termios.h>
     63   1.97   garbled #include <dev/ic/comreg.h>
     64   1.97   garbled #include <dev/ic/comvar.h>
     65   1.97   garbled #endif
     66  1.109       phx #include "rtas.h"
     67   1.97   garbled 
     68   1.93   garbled struct pmap ofw_pmap;
     69   1.78       chs char bootpath[256];
     70    1.1        ws 
     71   1.93   garbled void ofwppc_batinit(void);
     72  1.104   garbled void ofppc_bootstrap_console(void);
     73   1.77      matt 
     74   1.99   garbled extern u_int l2cr_config;
     75  1.108       phx #if (NRTAS > 0)
     76  1.103   garbled extern int machine_has_rtas;
     77  1.108       phx #endif
     78   1.99   garbled 
     79  1.105   garbled struct model_data modeldata;
     80  1.105   garbled 
     81    1.1        ws void
     82   1.93   garbled initppc(u_int startkernel, u_int endkernel, char *args)
     83    1.1        ws {
     84   1.93   garbled 	ofwoea_initppc(startkernel, endkernel, args);
     85    1.1        ws }
     86    1.1        ws 
     87   1.98   garbled /* perform model-specific actions at initppc() */
     88   1.98   garbled void
     89   1.98   garbled model_init(void)
     90   1.98   garbled {
     91  1.105   garbled 	int qhandle, phandle, j;
     92  1.105   garbled 
     93  1.105   garbled 	memset(&modeldata, 0, sizeof(struct model_data));
     94  1.105   garbled 	/* provide sane defaults */
     95  1.105   garbled 	for (j=0; j < MAX_PCI_BUSSES; j++) {
     96  1.105   garbled 		modeldata.pciiodata[j].start = 0x00008000;
     97  1.105   garbled 		modeldata.pciiodata[j].limit = 0x0000ffff;
     98  1.105   garbled 	}
     99  1.105   garbled 	modeldata.ranges_offset = 1;
    100  1.105   garbled 
    101  1.105   garbled 	if (strncmp(model_name, "FirePower,", 10) == 0) {
    102  1.105   garbled 		modeldata.ranges_offset = 0;
    103  1.105   garbled 	}
    104  1.105   garbled 	if (strcmp(model_name, "MOT,PowerStack_II_Pro4000") == 0) {
    105  1.105   garbled 		modeldata.ranges_offset = 0;
    106  1.105   garbled 	}
    107  1.105   garbled 
    108  1.105   garbled 	/* 7044-270 and 7044-170 */
    109  1.105   garbled 	if (strncmp(model_name, "IBM,7044", 8) == 0) {
    110  1.105   garbled 		for (j=0; j < MAX_PCI_BUSSES; j++) {
    111  1.105   garbled 			modeldata.pciiodata[j].start = 0x00fff000;
    112  1.105   garbled 			modeldata.pciiodata[j].limit = 0x00ffffff;
    113  1.105   garbled 		}
    114  1.105   garbled 	}
    115   1.98   garbled 
    116   1.98   garbled 	/* Pegasos1, Pegasos2 */
    117   1.98   garbled 	if (strncmp(model_name, "Pegasos", 7) == 0) {
    118   1.98   garbled 		static uint16_t modew[] = { 640, 800, 1024, 1280, 0 };
    119   1.98   garbled 		static uint16_t modeh[] = { 480, 600, 768, 1024, 0 };
    120   1.98   garbled 		uint32_t width, height, mode, fbaddr;
    121   1.98   garbled 		char buf[32];
    122   1.98   garbled 		int i;
    123   1.98   garbled 
    124  1.105   garbled 		modeldata.ranges_offset = 1;
    125  1.105   garbled 		modeldata.pciiodata[0].start = 0x00001400;
    126  1.105   garbled 		modeldata.pciiodata[0].limit = 0x0000ffff;
    127  1.105   garbled 
    128   1.99   garbled 		/* the pegasos doesn't bother to set the L2 cache up*/
    129   1.99   garbled 		l2cr_config = L2CR_L2PE;
    130   1.99   garbled 
    131   1.98   garbled 		/* fix the device_type property of a graphics card */
    132   1.98   garbled 		for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
    133   1.98   garbled 			if (OF_getprop(qhandle, "name", buf, sizeof buf) > 0
    134   1.98   garbled 			    && strncmp(buf, "display", 7) == 0) {
    135   1.98   garbled 				OF_setprop(qhandle, "device_type", "display", 8);
    136   1.98   garbled 				break;
    137   1.98   garbled 			}
    138   1.98   garbled 			if ((phandle = OF_child(qhandle)))
    139   1.98   garbled 				continue;
    140   1.98   garbled 			while (qhandle) {
    141   1.98   garbled 				if ((phandle = OF_peer(qhandle)))
    142   1.98   garbled 					break;
    143   1.98   garbled 				qhandle = OF_parent(qhandle);
    144   1.98   garbled 			}
    145   1.98   garbled 		}
    146   1.98   garbled 
    147   1.98   garbled 		/*
    148   1.98   garbled 		 * Get screen width/height and switch to framebuffer mode.
    149   1.98   garbled 		 * The default dimensions are: 800 x 600
    150   1.98   garbled 		 */
    151   1.98   garbled 		OF_interpret("screen-width", 0, 1, &width);
    152   1.98   garbled 		if (width == 0)
    153   1.98   garbled 			width = 800;
    154   1.98   garbled 
    155   1.98   garbled 		OF_interpret("screen-height", 0, 1, &height);
    156   1.98   garbled 		if (height == 0)
    157   1.98   garbled 			height = 600;
    158   1.98   garbled 
    159   1.98   garbled 		/* find VESA mode */
    160   1.98   garbled 		for (i = 0, mode = 0; modew[i] != 0; i++) {
    161   1.98   garbled 			if (modew[i] == width && modeh[i] == height) {
    162   1.98   garbled 				mode = 0x101 + 2 * i;
    163   1.98   garbled 				break;
    164   1.98   garbled 			}
    165   1.98   garbled 		}
    166   1.98   garbled 		if (!mode) {
    167   1.98   garbled 			mode = 0x102;
    168   1.98   garbled 			width = 800;
    169   1.98   garbled 			height = 600;
    170   1.98   garbled 		}
    171   1.98   garbled 
    172   1.98   garbled 		/* init frame buffer mode */
    173   1.98   garbled 		sprintf(buf, "%x vesa-set-mode", mode);
    174   1.98   garbled 		OF_interpret(buf, 0, 0);
    175   1.98   garbled 
    176   1.98   garbled 		/* set dimensions and frame buffer address in OFW */
    177   1.98   garbled 		sprintf(buf, "%x to screen-width", width);
    178   1.98   garbled 		OF_interpret(buf, 0, 0);
    179   1.98   garbled 		sprintf(buf, "%x to screen-height", height);
    180   1.98   garbled 		OF_interpret(buf, 0, 0);
    181   1.98   garbled 		OF_interpret("vesa-frame-buffer-adr", 0, 1, &fbaddr);
    182   1.98   garbled 		if (fbaddr != 0) {
    183   1.98   garbled 			sprintf(buf, "%x to frame-buffer-adr", fbaddr);
    184   1.98   garbled 			OF_interpret(buf, 0, 0);
    185   1.98   garbled 		}
    186   1.98   garbled 	}
    187   1.98   garbled }
    188   1.98   garbled 
    189    1.1        ws void
    190   1.93   garbled cpu_startup(void)
    191    1.1        ws {
    192   1.98   garbled 	oea_startup(model_name[0] ? model_name : NULL);
    193  1.104   garbled 	bus_space_mallocok();
    194    1.1        ws }
    195    1.1        ws 
    196   1.96   garbled 
    197    1.1        ws void
    198   1.93   garbled consinit(void)
    199    1.1        ws {
    200   1.93   garbled 	ofwoea_consinit();
    201   1.71   thorpej }
    202   1.96   garbled 
    203   1.71   thorpej 
    204   1.71   thorpej void
    205   1.93   garbled dumpsys(void)
    206   1.71   thorpej {
    207  1.101   garbled 	aprint_normal("dumpsys: TBD\n");
    208    1.1        ws }
    209    1.1        ws 
    210    1.1        ws /*
    211   1.93   garbled  * Halt or reboot the machine after syncing/dumping according to howto.
    212    1.1        ws  */
    213    1.1        ws 
    214    1.1        ws void
    215   1.93   garbled cpu_reboot(int howto, char *what)
    216    1.1        ws {
    217    1.1        ws 	static int syncing;
    218    1.1        ws 	static char str[256];
    219  1.108       phx 	char *ap = str, *ap1 = ap;
    220  1.108       phx #if (NRTAS > 0)
    221  1.102   garbled 	int junk;
    222  1.108       phx #endif
    223    1.1        ws 
    224    1.1        ws 	boothowto = howto;
    225    1.1        ws 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    226    1.1        ws 		syncing = 1;
    227   1.93   garbled 		vfs_shutdown();         /* sync */
    228   1.93   garbled 		resettodr();            /* set wall clock */
    229    1.1        ws 	}
    230    1.1        ws 	splhigh();
    231    1.1        ws 	if (howto & RB_HALT) {
    232    1.1        ws 		doshutdownhooks();
    233  1.107    dyoung 		pmf_system_shutdown(boothowto);
    234  1.101   garbled 		aprint_normal("halted\n\n");
    235  1.108       phx #if (NRTAS > 0)
    236  1.103   garbled 		if ((howto & 0x800) && machine_has_rtas &&
    237  1.103   garbled 		    rtas_has_func(RTAS_FUNC_POWER_OFF))
    238  1.103   garbled 			rtas_call(RTAS_FUNC_POWER_OFF, 2, 1, 0, 0, &junk);
    239  1.108       phx #endif
    240    1.1        ws 		ppc_exit();
    241    1.1        ws 	}
    242    1.1        ws 	if (!cold && (howto & RB_DUMP))
    243   1.82      matt 		oea_dumpsys();
    244    1.1        ws 	doshutdownhooks();
    245  1.107    dyoung 
    246  1.107    dyoung 	pmf_system_shutdown(boothowto);
    247  1.101   garbled 	aprint_normal("rebooting\n\n");
    248   1.95   garbled 
    249  1.108       phx #if (NRTAS > 0)
    250  1.103   garbled 	if (machine_has_rtas && rtas_has_func(RTAS_FUNC_SYSTEM_REBOOT)) {
    251  1.103   garbled 		rtas_call(RTAS_FUNC_SYSTEM_REBOOT, 0, 1, &junk);
    252  1.103   garbled 		for(;;);
    253  1.103   garbled 	}
    254  1.108       phx #endif
    255    1.1        ws 	if (what && *what) {
    256    1.1        ws 		if (strlen(what) > sizeof str - 5)
    257  1.101   garbled 			aprint_normal("boot string too large, ignored\n");
    258    1.1        ws 		else {
    259    1.1        ws 			strcpy(str, what);
    260    1.1        ws 			ap1 = ap = str + strlen(str);
    261    1.1        ws 			*ap++ = ' ';
    262    1.1        ws 		}
    263    1.1        ws 	}
    264    1.1        ws 	*ap++ = '-';
    265    1.1        ws 	if (howto & RB_SINGLE)
    266    1.1        ws 		*ap++ = 's';
    267    1.1        ws 	if (howto & RB_KDB)
    268    1.1        ws 		*ap++ = 'd';
    269    1.1        ws 	*ap++ = 0;
    270    1.1        ws 	if (ap[-2] == '-')
    271    1.1        ws 		*ap1 = 0;
    272    1.1        ws 	ppc_boot(str);
    273    1.1        ws }
    274   1.97   garbled 
    275   1.97   garbled /*
    276   1.97   garbled  */
    277   1.97   garbled 
    278   1.97   garbled #define divrnd(n, q)	(((n)*2/(q)+1)/2)
    279   1.97   garbled 
    280   1.97   garbled void
    281   1.98   garbled ofppc_init_comcons(int isa_node)
    282   1.97   garbled {
    283   1.97   garbled #if (NCOM > 0)
    284   1.97   garbled 	char name[64];
    285   1.97   garbled 	uint32_t reg[2], comfreq;
    286   1.97   garbled 	uint8_t dll, dlm;
    287   1.98   garbled 	int speed, rate, err, com_node, child;
    288  1.104   garbled 	bus_space_handle_t comh;
    289   1.97   garbled 
    290   1.97   garbled 	/* if we have a serial cons, we have work to do */
    291   1.97   garbled 	memset(name, 0, sizeof(name));
    292   1.97   garbled 	OF_getprop(console_node, "device_type", name, sizeof(name));
    293   1.97   garbled 	if (strcmp(name, "serial") != 0)
    294   1.97   garbled 		return;
    295   1.97   garbled 
    296   1.98   garbled 	/* scan ISA children for serial devices to match our console */
    297   1.98   garbled 	com_node = -1;
    298   1.98   garbled 	for (child = OF_child(isa_node); child; child = OF_peer(child)) {
    299   1.98   garbled 		memset(name, 0, sizeof(name));
    300   1.98   garbled 		OF_getprop(child, "device_type", name, sizeof(name));
    301   1.98   garbled 		if (strcmp(name, "serial") == 0) {
    302   1.98   garbled 			/*
    303   1.98   garbled 			 * Serial device even matches our console_node?
    304   1.98   garbled 			 * Then we're done!
    305   1.98   garbled 			 */
    306   1.98   garbled 			if (child == console_node) {
    307   1.98   garbled 				com_node = child;
    308   1.98   garbled 				break;
    309   1.98   garbled 			}
    310   1.98   garbled 			/* remember first serial device found */
    311   1.98   garbled 			if (com_node == -1)
    312   1.98   garbled 				com_node = child;
    313   1.98   garbled 		}
    314   1.98   garbled 	}
    315   1.98   garbled 
    316   1.98   garbled 	if (com_node == -1)
    317   1.98   garbled 		return;
    318   1.98   garbled 
    319   1.98   garbled 	if (OF_getprop(com_node, "reg", reg, sizeof(reg)) == -1)
    320   1.97   garbled 		return;
    321   1.97   garbled 
    322   1.98   garbled 	if (OF_getprop(com_node, "clock-frequency", &comfreq, 4) == -1)
    323   1.97   garbled 		comfreq = 0;
    324   1.97   garbled 
    325   1.97   garbled 	if (comfreq == 0)
    326   1.97   garbled 		comfreq = COM_FREQ;
    327   1.97   garbled 
    328  1.104   garbled 	/* we need to BSM this, and then undo that before calling
    329  1.104   garbled 	 * comcnattach.
    330  1.104   garbled 	 */
    331  1.104   garbled 
    332  1.104   garbled 	if (bus_space_map(&genppc_isa_io_space_tag, reg[1], 8, 0, &comh) != 0)
    333  1.104   garbled 		panic("Can't map isa serial\n");
    334  1.104   garbled 
    335  1.104   garbled 	bus_space_write_1(&genppc_isa_io_space_tag, comh, com_cfcr, LCR_DLAB);
    336  1.104   garbled 	dll = bus_space_read_1(&genppc_isa_io_space_tag, comh, com_dlbl);
    337  1.104   garbled 	dlm = bus_space_read_1(&genppc_isa_io_space_tag, comh, com_dlbh);
    338   1.97   garbled 	rate = dll | (dlm << 8);
    339  1.104   garbled 	bus_space_write_1(&genppc_isa_io_space_tag, comh, com_cfcr, LCR_8BITS);
    340   1.97   garbled 	speed = divrnd((comfreq / 16), rate);
    341   1.97   garbled 	err = speed - (speed + 150)/300 * 300;
    342   1.97   garbled 	speed -= err;
    343   1.97   garbled 	if (err < 0)
    344   1.97   garbled 		err = -err;
    345   1.97   garbled 	if (err > 50)
    346   1.97   garbled 		speed = 9600;
    347   1.97   garbled 
    348  1.104   garbled 	bus_space_unmap(&genppc_isa_io_space_tag, comh, 8);
    349  1.104   garbled 
    350   1.97   garbled 	/* Now we can attach the comcons */
    351   1.97   garbled 	aprint_verbose("Switching to COM console at speed %d", speed);
    352   1.98   garbled 	if (comcnattach(&genppc_isa_io_space_tag, reg[1],
    353   1.98   garbled 	    speed, comfreq, COM_TYPE_NORMAL,
    354   1.97   garbled 	    ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)))
    355   1.97   garbled 		panic("Can't init serial console");
    356   1.97   garbled 	aprint_verbose("\n");
    357   1.97   garbled #endif /*NCOM*/
    358   1.97   garbled }
    359   1.98   garbled 
    360   1.98   garbled void
    361   1.98   garbled copy_disp_props(struct device *dev, int node, prop_dictionary_t dict)
    362   1.98   garbled {
    363   1.98   garbled 	uint32_t temp;
    364   1.98   garbled 	char typestr[32];
    365   1.98   garbled 
    366   1.98   garbled 	memset(typestr, 0, sizeof(typestr));
    367   1.98   garbled 	OF_getprop(console_node, "device_type", typestr, sizeof(typestr));
    368   1.98   garbled 	if (strcmp(typestr, "serial") != 0) {
    369   1.98   garbled 		/* this is our console, when we don't have a serial console */
    370   1.98   garbled 		prop_dictionary_set_bool(dict, "is_console", 1);
    371   1.98   garbled 	}
    372   1.98   garbled 
    373   1.98   garbled 	if (!of_to_uint32_prop(dict, node, "width", "width")) {
    374   1.98   garbled 
    375   1.98   garbled 		OF_interpret("screen-width", 0, 1, &temp);
    376   1.98   garbled 		prop_dictionary_set_uint32(dict, "width", temp);
    377   1.98   garbled 	}
    378   1.98   garbled 	if (!of_to_uint32_prop(dict, node, "height", "height")) {
    379   1.98   garbled 
    380   1.98   garbled 		OF_interpret("screen-height", 0, 1, &temp);
    381   1.98   garbled 		prop_dictionary_set_uint32(dict, "height", temp);
    382   1.98   garbled 	}
    383   1.98   garbled 	of_to_uint32_prop(dict, node, "linebytes", "linebytes");
    384   1.98   garbled 	if (!of_to_uint32_prop(dict, node, "depth", "depth")) {
    385   1.98   garbled 		/*
    386   1.98   garbled 		 * XXX we should check linebytes vs. width but those
    387   1.98   garbled 		 * FBs that don't have a depth property ( /chaos/control... )
    388   1.98   garbled 		 * won't have linebytes either
    389   1.98   garbled 		 */
    390   1.98   garbled 		prop_dictionary_set_uint32(dict, "depth", 8);
    391   1.98   garbled 	}
    392   1.98   garbled 	if (!of_to_uint32_prop(dict, node, "address", "address")) {
    393   1.98   garbled 		uint32_t fbaddr = 0;
    394  1.111  kiyohara 
    395  1.111  kiyohara 		OF_interpret("frame-buffer-adr", 0, 1, &fbaddr);
    396   1.98   garbled 		if (fbaddr != 0)
    397   1.98   garbled 			prop_dictionary_set_uint32(dict, "address", fbaddr);
    398   1.98   garbled 	}
    399   1.98   garbled }
    400