xref: /netbsd/sys/arch/sparc64/dev/com_ebus.c (revision c4a72b64)
1 /*	$NetBSD: com_ebus.c,v 1.17 2002/10/02 16:02:18 thorpej 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/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/tty.h>
40 #include <sys/conf.h>
41 
42 #include <machine/bus.h>
43 #include <machine/autoconf.h>
44 #include <machine/openfirm.h>
45 
46 #include <dev/ebus/ebusreg.h>
47 #include <dev/ebus/ebusvar.h>
48 
49 #include <dev/cons.h>
50 #include <dev/ic/comvar.h>
51 #include <dev/sun/kbd_ms_ttyvar.h>
52 
53 #include "kbd.h"
54 #include "ms.h"
55 
56 int	com_ebus_match __P((struct device *, struct cfdata *, void *));
57 void	com_ebus_attach __P((struct device *, struct device *, void *));
58 
59 CFATTACH_DECL(com_ebus, sizeof(struct com_softc),
60     com_ebus_match, com_ebus_attach, NULL, NULL);
61 
62 static char *com_names[] = {
63 	"su",
64 	"su_pnp",
65 	NULL
66 };
67 
68 int
69 com_ebus_match(parent, match, aux)
70 	struct device *parent;
71 	struct cfdata *match;
72 	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[80];
83 
84 		/* Could be anything. */
85 		if ((i = OF_getproplen(ea->ea_node, "compatible")) &&
86 			OF_getprop(ea->ea_node, "compatible", compat,
87 				sizeof(compat)) == i) {
88 			if (strcmp(compat, "su16550") == 0 ||
89 				strcmp(compat, "su") == 0) {
90 				return (1);
91 			}
92 		}
93 	}
94 	return (0);
95 }
96 
97 #define BAUD_BASE       (1846200)
98 
99 void
100 com_ebus_attach(parent, self, aux)
101 	struct device *parent, *self;
102 	void *aux;
103 {
104 	struct com_softc *sc = (void *)self;
105 	struct ebus_attach_args *ea = aux;
106 	struct kbd_ms_tty_attach_args kma;
107 #if (NKBD > 0) || (NMS > 0)
108 	int maj;
109 	extern const struct cdevsw com_cdevsw;
110 #endif
111 	int i;
112 	int com_is_input;
113 	int com_is_output;
114 
115 	sc->sc_iot = ea->ea_bustag;
116 	sc->sc_iobase = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
117 	/*
118 	 * Addresses that should be supplied by the prom:
119 	 *	- normal com registers
120 	 *	- ns873xx configuration registers
121 	 *	- DMA space
122 	 * The `com' driver does not use DMA accesses, so we can
123 	 * ignore that for now.  We should enable the com port in
124 	 * the ns873xx registers here. XXX
125 	 *
126 	 * Use the prom address if there.
127 	 */
128 
129 	if (ea->ea_nvaddr)
130 		sparc_promaddr_to_handle(sc->sc_iot, ea->ea_vaddr[0],
131 			&sc->sc_ioh);
132 	else if (bus_space_map(sc->sc_iot, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
133 		ea->ea_reg[0].size, 0, &sc->sc_ioh) != 0) {
134 		printf(": can't map register space\n");
135 		return;
136 	}
137 	sc->sc_hwflags = 0;
138 	sc->sc_frequency = BAUD_BASE;
139 
140 	for (i = 0; i < ea->ea_nintr; i++)
141 		bus_intr_establish(ea->ea_bustag, ea->ea_intr[i],
142 		    IPL_SERIAL, 0, comintr, sc);
143 
144 	kma.kmta_consdev = NULL;
145 
146 	/* Figure out if we're the console. */
147 	com_is_input = (ea->ea_node == OF_instance_to_package(OF_stdin()));
148 	com_is_output = (ea->ea_node == OF_instance_to_package(OF_stdout()));
149 
150 	if (com_is_input || com_is_output) {
151 		extern struct consdev comcons;
152 		struct consdev *cn_orig;
153 
154 		/* Record some info to attach console. */
155 		kma.kmta_baud = 9600;
156 		kma.kmta_cflag = (CREAD | CS8 | HUPCL);
157 
158 		/* Attach com as the console. */
159 		cn_orig = cn_tab;
160 		if (comcnattach(sc->sc_iot, sc->sc_iobase, kma.kmta_baud,
161 			sc->sc_frequency, kma.kmta_cflag)) {
162 			printf("Error: comcnattach failed\n");
163 		}
164 		cn_tab = cn_orig;
165 		if (com_is_input) {
166 			cn_tab->cn_dev = comcons.cn_dev;
167 			cn_tab->cn_probe = comcons.cn_probe;
168 			cn_tab->cn_init = comcons.cn_init;
169 			cn_tab->cn_getc = comcons.cn_getc;
170 			cn_tab->cn_pollc = comcons.cn_pollc;
171 		}
172 		if (com_is_output) {
173 			cn_tab->cn_putc = comcons.cn_putc;
174 		}
175 		kma.kmta_consdev = cn_tab;
176 	}
177 	/* Now attach the driver */
178 	com_attach_subr(sc);
179 
180 #if (NKBD > 0) || (NMS > 0)
181 	kma.kmta_tp = sc->sc_tty;
182 /* If we figure out we're the console we should point this to our consdev */
183 
184 	/* locate the major number */
185 	maj = cdevsw_lookup_major(&com_cdevsw);
186 
187 	kma.kmta_dev = makedev(maj, sc->sc_dev.dv_unit);
188 
189 /* Attach 'em if we got 'em. */
190 #if (NKBD > 0)
191 	kma.kmta_name = "keyboard";
192 	if (OF_getproplen(ea->ea_node, kma.kmta_name) == 0) {
193 		config_found(self, (void *)&kma, NULL);
194 	}
195 #endif
196 #if (NMS > 0)
197 	kma.kmta_name = "mouse";
198 	if (OF_getproplen(ea->ea_node, kma.kmta_name) == 0) {
199 		config_found(self, (void *)&kma, NULL);
200 	}
201 #endif
202 #endif
203 	if (kma.kmta_consdev) {
204 		/*
205 		 * If we're the keyboard then we need the original
206 		 * cn_tab w/prom I/O, which sunkbd copied into kma.
207 		 * Otherwise we want conscom which we copied into
208 		 * kma.kmta_consdev.
209 		 */
210 		cn_tab = kma.kmta_consdev;
211 	}
212 }
213 
214