Home | History | Annotate | Line # | Download | only in dev
com_ebus.c revision 1.26
      1 /*	$NetBSD: com_ebus.c,v 1.26 2006/07/13 22:56:02 gdamore Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999, 2000 Matthew R. Green
      5  * 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. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * NS Super I/O PC87332VLJ "com" to ebus attachment
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: com_ebus.c,v 1.26 2006/07/13 22:56:02 gdamore Exp $");
     37 
     38 #include <sys/types.h>
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 #include <sys/tty.h>
     43 #include <sys/conf.h>
     44 
     45 #include <machine/bus.h>
     46 #include <machine/autoconf.h>
     47 #include <machine/openfirm.h>
     48 
     49 #include <dev/ebus/ebusreg.h>
     50 #include <dev/ebus/ebusvar.h>
     51 
     52 #include <dev/cons.h>
     53 #include <dev/ic/comvar.h>
     54 #include <dev/sun/kbd_ms_ttyvar.h>
     55 
     56 #include "kbd.h"
     57 #include "ms.h"
     58 
     59 int	com_ebus_match(struct device *, struct cfdata *, void *);
     60 void	com_ebus_attach(struct device *, struct device *, void *);
     61 
     62 CFATTACH_DECL(com_ebus, sizeof(struct com_softc),
     63     com_ebus_match, com_ebus_attach, NULL, NULL);
     64 
     65 static const char *com_names[] = {
     66 	"su",
     67 	"su_pnp",
     68 	NULL
     69 };
     70 
     71 int
     72 com_ebus_match(struct device *parent, struct cfdata *match, void *aux)
     73 {
     74 	struct ebus_attach_args *ea = aux;
     75 	int i;
     76 
     77 	for (i = 0; com_names[i]; i++)
     78 		if (strcmp(ea->ea_name, com_names[i]) == 0)
     79 			return (1);
     80 
     81 	if (strcmp(ea->ea_name, "serial") == 0) {
     82 		char *compat;
     83 
     84 		/* Could be anything. */
     85 		compat = prom_getpropstring(ea->ea_node, "compatible");
     86 		if (strcmp(compat, "su16550") == 0 ||
     87 		    strcmp(compat, "su") == 0) {
     88 			return (1);
     89 		}
     90 	}
     91 	return (0);
     92 }
     93 
     94 #define BAUD_BASE       (1846200)
     95 
     96 void
     97 com_ebus_attach(struct device *parent, struct device *self, void *aux)
     98 {
     99 	struct com_softc *sc = (void *)self;
    100 	struct ebus_attach_args *ea = aux;
    101 	struct kbd_ms_tty_attach_args kma;
    102 #if (NKBD > 0) || (NMS > 0)
    103 	int maj;
    104 	extern const struct cdevsw com_cdevsw;
    105 #endif
    106 	int i;
    107 	int com_is_input;
    108 	int com_is_output;
    109 	bus_space_handle_t ioh;
    110 	bus_space_tag_t iot;
    111 	bus_addr_t iobase;
    112 
    113 	iot = ea->ea_bustag;
    114 	iobase = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
    115 
    116 	/*
    117 	 * Addresses that should be supplied by the prom:
    118 	 *	- normal com registers
    119 	 *	- ns873xx configuration registers
    120 	 *	- DMA space
    121 	 * The `com' driver does not use DMA accesses, so we can
    122 	 * ignore that for now.  We should enable the com port in
    123 	 * the ns873xx registers here. XXX
    124 	 *
    125 	 * Use the prom address if there.
    126 	 */
    127 
    128 	if (ea->ea_nvaddr)
    129 		sparc_promaddr_to_handle(iot, ea->ea_vaddr[0],
    130 			&ioh);
    131 	else if (bus_space_map(iot, iobase,
    132 		ea->ea_reg[0].size, 0, &ioh) != 0) {
    133 		printf(": can't map register space\n");
    134 		return;
    135 	}
    136 	COM_INIT_REGS(sc->sc_regs, iot, ioh, iobase);
    137 
    138 	sc->sc_hwflags = 0;
    139 	sc->sc_frequency = BAUD_BASE;
    140 
    141 	for (i = 0; i < ea->ea_nintr; i++)
    142 		bus_intr_establish(ea->ea_bustag, ea->ea_intr[i],
    143 		    IPL_SERIAL, comintr, sc);
    144 
    145 	kma.kmta_consdev = NULL;
    146 
    147 	/* Figure out if we're the console. */
    148 	com_is_input = (ea->ea_node == prom_instance_to_package(prom_stdin()));
    149 	com_is_output = (ea->ea_node == prom_instance_to_package(prom_stdout()));
    150 
    151 	if (com_is_input || com_is_output) {
    152 		extern struct consdev comcons;
    153 		struct consdev *cn_orig;
    154 
    155 		/* Record some info to attach console. */
    156 		kma.kmta_baud = 9600;
    157 		kma.kmta_cflag = (CREAD | CS8 | HUPCL);
    158 
    159 		/* Attach com as the console. */
    160 		cn_orig = cn_tab;
    161 		if (comcnattach(iot, iobase, kma.kmta_baud,
    162 			sc->sc_frequency, COM_TYPE_NORMAL, kma.kmta_cflag)) {
    163 			printf("Error: comcnattach failed\n");
    164 		}
    165 		cn_tab = cn_orig;
    166 		if (com_is_input) {
    167 			cn_tab->cn_dev = comcons.cn_dev;
    168 			cn_tab->cn_probe = comcons.cn_probe;
    169 			cn_tab->cn_init = comcons.cn_init;
    170 			cn_tab->cn_getc = comcons.cn_getc;
    171 			cn_tab->cn_pollc = comcons.cn_pollc;
    172 		}
    173 		if (com_is_output) {
    174 			cn_tab->cn_putc = comcons.cn_putc;
    175 		}
    176 		kma.kmta_consdev = cn_tab;
    177 	}
    178 	/* Now attach the driver */
    179 	com_attach_subr(sc);
    180 
    181 #if (NKBD > 0) || (NMS > 0)
    182 	kma.kmta_tp = sc->sc_tty;
    183 /* If we figure out we're the console we should point this to our consdev */
    184 
    185 	/* locate the major number */
    186 	maj = cdevsw_lookup_major(&com_cdevsw);
    187 
    188 	kma.kmta_dev = makedev(maj, device_unit(&sc->sc_dev));
    189 
    190 /* Attach 'em if we got 'em. */
    191 #if (NKBD > 0)
    192 	kma.kmta_name = "keyboard";
    193 	if (prom_getproplen(ea->ea_node, kma.kmta_name) == 0) {
    194 		config_found(self, (void *)&kma, NULL);
    195 	}
    196 #endif
    197 #if (NMS > 0)
    198 	kma.kmta_name = "mouse";
    199 	if (prom_getproplen(ea->ea_node, kma.kmta_name) == 0) {
    200 		config_found(self, (void *)&kma, NULL);
    201 	}
    202 #endif
    203 #endif
    204 	if (kma.kmta_consdev) {
    205 		/*
    206 		 * If we're the keyboard then we need the original
    207 		 * cn_tab w/prom I/O, which sunkbd copied into kma.
    208 		 * Otherwise we want conscom which we copied into
    209 		 * kma.kmta_consdev.
    210 		 */
    211 		cn_tab = kma.kmta_consdev;
    212 	}
    213 }
    214