Home | History | Annotate | Line # | Download | only in vr
com_vrip.c revision 1.2
      1 /*	$NetBSD: com_vrip.c,v 1.2 1999/12/23 06:26:10 takemura 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/ioctl.h>
     40 #include <sys/termios.h>
     41 #include <sys/select.h>
     42 #include <sys/tty.h>
     43 #include <sys/proc.h>
     44 #include <sys/user.h>
     45 #include <sys/conf.h>
     46 #include <sys/file.h>
     47 #include <sys/uio.h>
     48 #include <sys/kernel.h>
     49 #include <sys/syslog.h>
     50 #include <sys/types.h>
     51 #include <sys/device.h>
     52 #include <sys/reboot.h>
     53 
     54 #include <machine/intr.h>
     55 #include <machine/bus.h>
     56 /* For serial console */
     57 #include <machine/platid.h>
     58 #include <machine/platid_mask.h>
     59 #include <machine/config_hook.h>
     60 
     61 #include <hpcmips/vr/vr.h>
     62 #include <hpcmips/vr/vripvar.h>
     63 #include <hpcmips/vr/cmureg.h>
     64 #include <hpcmips/vr/siureg.h>
     65 
     66 #include <dev/ic/comvar.h>
     67 #include <dev/ic/comreg.h>
     68 
     69 #include <hpcmips/vr/com_vripvar.h>
     70 
     71 #include "locators.h"
     72 
     73 #ifdef COMVRIPDEBUG
     74 int	com_vrip_debug = 0;
     75 #define	DPRINTF(arg) if (com_vrip_debug) printf arg;
     76 #else
     77 #define	DPRINTF(arg)
     78 #endif
     79 
     80 struct com_vrip_softc {
     81 	struct com_softc	sc_com;
     82 	int sc_pwctl;
     83 };
     84 
     85 static int com_vrip_probe __P((struct device *, struct cfdata *, void *));
     86 static void com_vrip_attach __P((struct device *, struct device *, void *));
     87 static int com_vrip_common_probe __P((bus_space_tag_t iot, int iobase));
     88 int find_comenableport_from_cfdata __P((int *));
     89 
     90 void vrcmu_init __P((void));
     91 void vrcmu_supply __P((int));
     92 void vrcmu_mask __P((int));
     93 
     94 struct cfattach com_vrip_ca = {
     95 	sizeof(struct com_vrip_softc), com_vrip_probe, com_vrip_attach
     96 };
     97 
     98 /* For serial console */
     99 extern struct cfdata cfdata[];
    100 int
    101 find_comenableport_from_cfdata(int *port)
    102 {
    103 	platid_mask_t mask;
    104 	struct cfdata *cf;
    105 	int id;
    106 
    107 	printf ("COM enable port: ");
    108 	for (cf = cfdata; cf->cf_driver; cf++) {
    109 		if (strcmp(cf->cf_driver->cd_name, "pwctl"))
    110 			continue;
    111 		mask = PLATID_DEREF(cf->cf_loc[NEWGPBUSIFCF_PLATFORM]);
    112 		id = cf->cf_loc[NEWGPBUSIFCF_ID];
    113 		if (platid_match(&platid, &mask) &&
    114 		    id == CONFIG_HOOK_POWERCONTROL_COM0)
    115 			goto found;
    116 	}
    117 	*port = -1;
    118 	printf ("not found\n");
    119 	return 1;
    120  found:
    121 	*port = cf->cf_loc[NEWGPBUSIFCF_PORT];
    122 	printf ("#%d\n", *port);
    123 
    124 	return *port == GPBUSIFCF_COMCTRL_DEFAULT;
    125 }
    126 
    127 int
    128 com_vrip_cnattach(iot, iobase, rate, frequency, cflag)
    129 	bus_space_tag_t iot;
    130 	int iobase;
    131 	int rate, frequency;
    132 	tcflag_t cflag;
    133 {
    134 	int port;
    135 	/* Platform dependent setting */
    136 	__vrcmu_supply(CMUMSKSSIU | CMUMSKSIU, 1);
    137 	if (find_comenableport_from_cfdata(&port) == 0)
    138 		__vrgiu_out(port, 1);
    139 
    140 	if (!com_vrip_common_probe(iot, iobase))
    141 		return (EIO);	/* I can't find appropriate error number. */
    142 	return (comcnattach(iot, iobase, rate, frequency, cflag));
    143 }
    144 
    145 static int
    146 com_vrip_common_probe(iot, iobase)
    147 	bus_space_tag_t iot;
    148 	int iobase;
    149 {
    150 	bus_space_handle_t ioh;
    151 	int rv;
    152 
    153 	if (bus_space_map(iot, iobase, 1, 0, &ioh)) {
    154 		printf(": can't map i/o space\n");
    155 		return 0;
    156 	}
    157 	rv = comprobe1(iot, ioh);
    158 	bus_space_unmap(iot, ioh, 1);
    159 	return (rv);
    160 }
    161 
    162 static int
    163 com_vrip_probe(parent, cf, aux)
    164 	struct device *parent;
    165 	struct cfdata *cf;
    166 	void *aux;
    167 {
    168 	struct vrip_attach_args *va = aux;
    169 	bus_space_tag_t iot = va->va_iot;
    170 	int rv;
    171 
    172 	DPRINTF(("==com_vrip_probe"));
    173 
    174 	if (va->va_addr == VRIPCF_ADDR_DEFAULT ||
    175 	    va->va_intr == VRIPCF_INTR_DEFAULT) {
    176 		printf(": need addr and intr.\n");
    177 		return (0);
    178 	}
    179 
    180 	if (!va->va_cf || !va->va_cf->cf_clock)
    181 		return 0; /* not yet CMU attached. Try again later. */
    182 
    183 	va->va_cf->cf_clock(va->va_cc, CMUMSKSSIU | CMUMSKSIU, 1);
    184 
    185 	if (com_is_console(iot, va->va_addr, 0)) {
    186 		/*
    187 		 *  We have alredy probed.
    188 		 */
    189 		rv = 1;
    190 	} else {
    191 		rv = com_vrip_common_probe(iot, va->va_addr);
    192 	}
    193 
    194 	DPRINTF((rv ? ": found COM ports\n" : ": can't probe COM device\n"));
    195 
    196 	if (rv) {
    197 		va->va_size = COM_NPORTS;
    198 	}
    199 	return (rv);
    200 }
    201 
    202 
    203 static void
    204 com_vrip_attach(parent, self, aux)
    205 	struct device *parent, *self;
    206 	void *aux;
    207 {
    208 	struct com_vrip_softc *vsc = (void *) self;
    209 	struct com_softc *sc = &vsc->sc_com;
    210 	struct vrip_attach_args *va = aux;
    211 
    212 	bus_space_tag_t iot = va->va_iot;
    213 	bus_space_handle_t ioh;
    214 
    215 	vsc->sc_pwctl = sc->sc_dev.dv_cfdata->cf_loc[VRIPCF_PWCTL];
    216 
    217 	DPRINTF(("==com_vrip_attach"));
    218 
    219 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
    220 		printf(": can't map bus space\n");
    221 		return;
    222 	}
    223 	sc->sc_iobase = va->va_addr;
    224 	sc->sc_iot = iot;
    225 	sc->sc_ioh = ioh;
    226 
    227 	sc->enable = NULL; /* XXX: CMU control */
    228 	sc->disable = NULL;
    229 
    230 	sc->sc_frequency = VRCOM_FREQ;
    231 	/* Power management */
    232 	va->va_cf->cf_clock(va->va_cc, CMUMSKSSIU | CMUMSKSIU, 1);
    233 	/*
    234 	va->va_gf->gf_portwrite(va->va_gc, GIUPORT_COM, 1);
    235 	*/
    236 	/* XXX, locale 'ID' must be need */
    237 	config_hook_call(CONFIG_HOOK_POWERCONTROL, vsc->sc_pwctl, (void*)1);
    238 
    239 
    240 	DPRINTF(("Try to attach com.\n"));
    241 	com_attach_subr(sc);
    242 
    243 	DPRINTF(("Establish intr"));
    244 	vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY, comintr, self);
    245 
    246 	DPRINTF((":return()"));
    247 }
    248