Home | History | Annotate | Line # | Download | only in vr
com_vrip.c revision 1.6
      1 /*	$NetBSD: com_vrip.c,v 1.6 2001/03/11 16:45:13 uch Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 SASAKI Takesi. All rights reserved.
      5  * Copyright (c) 1999 PocketBSD Project. All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the PocketBSD project
     18  *	and its contributors.
     19  * 4. Neither the name of the project nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  */
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 #include <sys/reboot.h>
     41 
     42 #include <sys/termios.h>
     43 
     44 #include <machine/intr.h>
     45 #include <machine/bus.h>
     46 
     47 #include <machine/platid.h>
     48 #include <machine/platid_mask.h>
     49 #include <machine/config_hook.h>
     50 
     51 #include <hpcmips/vr/vr.h>
     52 #include <hpcmips/vr/vripvar.h>
     53 #include <hpcmips/vr/cmureg.h>
     54 #include <hpcmips/vr/siureg.h>
     55 
     56 #include <dev/ic/comvar.h>
     57 #include <dev/ic/comreg.h>
     58 
     59 #include <hpcmips/vr/com_vripvar.h>
     60 
     61 #include "locators.h"
     62 
     63 #define COMVRIPDEBUG
     64 #ifdef COMVRIPDEBUG
     65 int	com_vrip_debug = 0;
     66 #define	DPRINTF(arg) if (com_vrip_debug) printf arg;
     67 #define	VPRINTF(arg) if (com_vrip_debug || bootverbose) printf arg;
     68 #else
     69 #define	DPRINTF(arg)
     70 #define	VPRINTF(arg) if (bootverbose) printf arg;
     71 #endif
     72 
     73 struct com_vrip_softc {
     74 	struct com_softc	sc_com;
     75 	int sc_pwctl;
     76 };
     77 
     78 static int com_vrip_probe __P((struct device *, struct cfdata *, void *));
     79 static void com_vrip_attach __P((struct device *, struct device *, void *));
     80 static int com_vrip_common_probe __P((bus_space_tag_t iot, int iobase));
     81 int find_comenableport_from_cfdata __P((int *));
     82 
     83 void vrcmu_init __P((void));
     84 void vrcmu_supply __P((int));
     85 void vrcmu_mask __P((int));
     86 
     87 struct cfattach com_vrip_ca = {
     88 	sizeof(struct com_vrip_softc), com_vrip_probe, com_vrip_attach
     89 };
     90 
     91 /* For serial console */
     92 extern struct cfdata cfdata[];
     93 int
     94 find_comenableport_from_cfdata(int *port)
     95 {
     96 	platid_mask_t mask;
     97 	struct cfdata *cf;
     98 	int id;
     99 
    100 	printf ("COM enable port: ");
    101 	for (cf = cfdata; cf->cf_driver; cf++) {
    102 		if (strcmp(cf->cf_driver->cd_name, "pwctl"))
    103 			continue;
    104 		mask = PLATID_DEREF(cf->cf_loc[NEWGPBUSIFCF_PLATFORM]);
    105 		id = cf->cf_loc[NEWGPBUSIFCF_ID];
    106 		if (platid_match(&platid, &mask) &&
    107 		    id == CONFIG_HOOK_POWERCONTROL_COM0)
    108 			goto found;
    109 	}
    110 	*port = -1;
    111 	printf ("not found\n");
    112 	return 1;
    113  found:
    114 	*port = cf->cf_loc[NEWGPBUSIFCF_PORT];
    115 	printf ("#%d\n", *port);
    116 
    117 	return *port == GPBUSIFCF_COMCTRL_DEFAULT;
    118 }
    119 
    120 int
    121 com_vrip_cndb_attach(iot, iobase, rate, frequency, cflag, kgdb)
    122 	bus_space_tag_t iot;
    123 	int iobase;
    124 	int rate, frequency;
    125 	tcflag_t cflag;
    126 	int kgdb;
    127 {
    128 	int port;
    129 	/* Platform dependent setting */
    130 	__vrcmu_supply(CMUMSKSSIU | CMUMSKSIU, 1);
    131 	if (find_comenableport_from_cfdata(&port) == 0)
    132 		__vrgiu_out(port, 1);
    133 
    134 	if (!com_vrip_common_probe(iot, iobase))
    135 		return (EIO);	/* I can't find appropriate error number. */
    136 #ifdef KGDB
    137 	if (kgdb)
    138 		return (com_kgdb_attach(iot, iobase, rate, frequency, cflag));
    139 	else
    140 #endif
    141 		return (comcnattach(iot, iobase, rate, frequency, cflag));
    142 }
    143 
    144 static int
    145 com_vrip_common_probe(iot, iobase)
    146 	bus_space_tag_t iot;
    147 	int iobase;
    148 {
    149 	bus_space_handle_t ioh;
    150 	int rv;
    151 
    152 	if (bus_space_map(iot, iobase, 1, 0, &ioh)) {
    153 		printf(": can't map i/o space\n");
    154 		return 0;
    155 	}
    156 	rv = comprobe1(iot, ioh);
    157 	bus_space_unmap(iot, ioh, 1);
    158 	return (rv);
    159 }
    160 
    161 static int
    162 com_vrip_probe(parent, cf, aux)
    163 	struct device *parent;
    164 	struct cfdata *cf;
    165 	void *aux;
    166 {
    167 	struct vrip_attach_args *va = aux;
    168 	bus_space_tag_t iot = va->va_iot;
    169 	int rv;
    170 
    171 	DPRINTF(("==com_vrip_probe"));
    172 
    173 	if (va->va_addr == VRIPCF_ADDR_DEFAULT ||
    174 	    va->va_intr == VRIPCF_INTR_DEFAULT) {
    175 		printf(": need addr and intr.\n");
    176 		return (0);
    177 	}
    178 
    179 	if (!va->va_cf || !va->va_cf->cf_clock)
    180 		return 0; /* not yet CMU attached. Try again later. */
    181 
    182 	va->va_cf->cf_clock(va->va_cc, CMUMSKSSIU | CMUMSKSIU, 1);
    183 
    184 	if (com_is_console(iot, va->va_addr, 0)) {
    185 		/*
    186 		 *  We have alredy probed.
    187 		 */
    188 		rv = 1;
    189 	} else {
    190 		rv = com_vrip_common_probe(iot, va->va_addr);
    191 	}
    192 
    193 	DPRINTF((rv ? ": found COM ports\n" : ": can't probe COM device\n"));
    194 
    195 	if (rv) {
    196 		va->va_size = COM_NPORTS;
    197 	}
    198 	return (rv);
    199 }
    200 
    201 
    202 static void
    203 com_vrip_attach(parent, self, aux)
    204 	struct device *parent, *self;
    205 	void *aux;
    206 {
    207 	struct com_vrip_softc *vsc = (void *) self;
    208 	struct com_softc *sc = &vsc->sc_com;
    209 	struct vrip_attach_args *va = aux;
    210 
    211 	bus_space_tag_t iot = va->va_iot;
    212 	bus_space_handle_t ioh;
    213 
    214 	vsc->sc_pwctl = sc->sc_dev.dv_cfdata->cf_loc[VRIPCF_PWCTL];
    215 
    216 	DPRINTF(("==com_vrip_attach"));
    217 
    218 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
    219 		printf(": can't map bus space\n");
    220 		return;
    221 	}
    222 	sc->sc_iobase = va->va_addr;
    223 	sc->sc_iot = iot;
    224 	sc->sc_ioh = ioh;
    225 
    226 	sc->enable = NULL; /* XXX: CMU control */
    227 	sc->disable = NULL;
    228 
    229 	sc->sc_frequency = VRCOM_FREQ;
    230 	/* Power management */
    231 	va->va_cf->cf_clock(va->va_cc, CMUMSKSSIU | CMUMSKSIU, 1);
    232 	/*
    233 	va->va_gf->gf_portwrite(va->va_gc, GIUPORT_COM, 1);
    234 	*/
    235 	/* XXX, locale 'ID' must be need */
    236 	config_hook_call(CONFIG_HOOK_POWERCONTROL, vsc->sc_pwctl, (void*)1);
    237 
    238 	DPRINTF(("Try to attach com.\n"));
    239 	com_attach_subr(sc);
    240 
    241 	DPRINTF(("Establish intr"));
    242 	vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY, comintr, self);
    243 
    244 	DPRINTF((":return()"));
    245 	VPRINTF(("%s: pwctl %d\n", vsc->sc_com.sc_dev.dv_xname, vsc->sc_pwctl));
    246 }
    247