xref: /netbsd/sys/arch/sparc64/dev/com_ebus.c (revision beecddb6)
1 /*	$NetBSD: com_ebus.c,v 1.36 2021/08/07 16:19:05 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * NS Super I/O PC87332VLJ "com" to ebus attachment
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: com_ebus.c,v 1.36 2021/08/07 16:19:05 thorpej Exp $");
35 
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/tty.h>
41 #include <sys/conf.h>
42 
43 #include <sys/bus.h>
44 #include <machine/autoconf.h>
45 #include <machine/openfirm.h>
46 
47 #include <dev/ebus/ebusreg.h>
48 #include <dev/ebus/ebusvar.h>
49 
50 #include <dev/cons.h>
51 #include <dev/ic/comvar.h>
52 #include <dev/sun/kbd_ms_ttyvar.h>
53 
54 #include "kbd.h"
55 #include "ms.h"
56 
57 int	com_ebus_match(device_t, cfdata_t , void *);
58 void	com_ebus_attach(device_t, device_t, void *);
59 
60 CFATTACH_DECL_NEW(com_ebus, sizeof(struct com_softc),
61     com_ebus_match, com_ebus_attach, NULL, NULL);
62 
63 static const char *com_names[] = {
64 	"su",
65 	"su_pnp",
66 	"rsc-console",
67 	NULL
68 };
69 
70 int
com_ebus_match(device_t parent,cfdata_t match,void * aux)71 com_ebus_match(device_t parent, cfdata_t match, void *aux)
72 {
73 	struct ebus_attach_args *ea = aux;
74 	int i;
75 
76 	for (i = 0; com_names[i]; i++)
77 		if (strcmp(ea->ea_name, com_names[i]) == 0)
78 			return (1);
79 
80 	if (strcmp(ea->ea_name, "serial") == 0) {
81 		char *compat;
82 
83 		/* Could be anything. */
84 		compat = prom_getpropstring(ea->ea_node, "compatible");
85 		if (strcmp(compat, "su16550") == 0 ||
86 		    strcmp(compat, "su16552") == 0 ||
87 		    strcmp(compat, "su") == 0 ||
88 		    strcmp(compat, "FJSV,su") == 0) {
89 			return (1);
90 		}
91 	}
92 	return (0);
93 }
94 
95 #define BAUD_BASE       (1846200)
96 
97 void
com_ebus_attach(device_t parent,device_t self,void * aux)98 com_ebus_attach(device_t parent, device_t self, void *aux)
99 {
100 	struct com_softc *sc = device_private(self);
101 	struct ebus_attach_args *ea = aux;
102 	struct kbd_ms_tty_attach_args kma;
103 #if (NKBD > 0) || (NMS > 0)
104 	int maj;
105 	extern const struct cdevsw com_cdevsw;
106 #endif
107 	int i;
108 	int com_is_input;
109 	int com_is_output;
110 	bus_space_handle_t ioh;
111 	bus_space_tag_t iot;
112 	bus_addr_t iobase;
113 	int node, port;
114 	char buf[32];
115 
116 	sc->sc_dev = self;
117 	iot = ea->ea_bustag;
118 	iobase = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
119 
120 	/*
121 	 * Addresses that should be supplied by the prom:
122 	 *	- normal com registers
123 	 *	- ns873xx configuration registers
124 	 *	- DMA space
125 	 * The `com' driver does not use DMA accesses, so we can
126 	 * ignore that for now.  We should enable the com port in
127 	 * the ns873xx registers here. XXX
128 	 *
129 	 * Use the prom address if there.
130 	 */
131 
132 	if (ea->ea_nvaddr)
133 		sparc_promaddr_to_handle(iot, ea->ea_vaddr[0],
134 			&ioh);
135 	else if (bus_space_map(iot, iobase,
136 		ea->ea_reg[0].size, 0, &ioh) != 0) {
137 		aprint_error(": can't map register space\n");
138 		return;
139 	}
140 	com_init_regs(&sc->sc_regs, iot, ioh, iobase);
141 
142 	sc->sc_hwflags = 0;
143 	sc->sc_frequency = BAUD_BASE;
144 
145 	for (i = 0; i < ea->ea_nintr; i++)
146 		bus_intr_establish(ea->ea_bustag, ea->ea_intr[i],
147 		    IPL_SERIAL, comintr, sc);
148 
149 	kma.kmta_consdev = NULL;
150 
151 	/*
152 	 * Figure out if we're the console.
153 	 *
154 	 * The Fujitsu SPARC Enterprise M4000/M5000/M8000/M9000 has a
155 	 * serial port on each I/O board and a pseudo console that is
156 	 * redirected to one of these serial ports.  The board number
157 	 * of the serial port in question is encoded in the "tty-port#"
158 	 * property of the pseudo console, so we figure out what our
159 	 * board number is by walking up the device tree, and check
160 	 * for a match.
161 	 */
162 	node = prom_instance_to_package(prom_stdin());
163 	com_is_input = (ea->ea_node == node);
164 	if (OF_getprop(node, "name", buf, sizeof(buf)) > 0 &&
165 	    strcmp(buf, "pseudo-console") == 0) {
166 		port = prom_getpropint(node, "tty-port#", -1);
167 		node = OF_parent(OF_parent(ea->ea_node));
168 		com_is_input = (prom_getpropint(node, "board#", -2) == port);
169 	}
170 
171 	node = prom_instance_to_package(prom_stdout());
172 	com_is_output = (ea->ea_node == node);
173 	if (OF_getprop(node, "name", buf, sizeof(buf)) > 0 &&
174 	   strcmp(buf, "pseudo-console") == 0) {
175 		port = prom_getpropint(node, "tty-port#", -1);
176 		node = OF_parent(OF_parent(ea->ea_node));
177 		com_is_output = (prom_getpropint(node, "board#", -2) == port);
178 	}
179 
180 	if (com_is_input || com_is_output) {
181 		struct consdev *cn_orig;
182 
183 		/* Record some info to attach console. */
184 		if (strcmp(ea->ea_name, "rsc-console") == 0)
185 			kma.kmta_baud = 115200;
186 		else
187 			kma.kmta_baud = 9600;
188 		kma.kmta_cflag = (CREAD | CS8 | HUPCL);
189 
190 		/* Attach com as the console. */
191 		cn_orig = cn_tab;
192 		if (comcnattach(iot, iobase, kma.kmta_baud,
193 			sc->sc_frequency, COM_TYPE_NORMAL, kma.kmta_cflag)) {
194 			aprint_error("Error: comcnattach failed\n");
195 		}
196 		if (com_is_input) {
197 			cn_orig->cn_dev = cn_tab->cn_dev;
198 			cn_orig->cn_probe = cn_tab->cn_probe;
199 			cn_orig->cn_init = cn_tab->cn_init;
200 			cn_orig->cn_getc = cn_tab->cn_getc;
201 			cn_orig->cn_pollc = cn_tab->cn_pollc;
202 		}
203 		if (com_is_output) {
204 			cn_orig->cn_putc = cn_tab->cn_putc;
205 		}
206 		cn_tab = cn_orig;
207 		kma.kmta_consdev = cn_tab;
208 	}
209 
210 	/*
211 	 * Apparently shoving too much data down the TX FIFO on the
212 	 * Fujitsu SPARC Enterprise M4000/M5000 causes a hardware
213 	 * fault.  Avoid this issue by setting the FIFO depth to 1.
214 	 * This will effectively disable the TX FIFO, but will still
215 	 * enable the RX FIFO, which is what we really care about.
216 	 */
217 	if (OF_getprop(ea->ea_node, "compatible", buf, sizeof(buf)) > 0 &&
218 	    strcmp(buf, "FJSV,su") == 0)
219 		sc->sc_fifolen = 1;
220 
221 	/* Now attach the driver */
222 	com_attach_subr(sc);
223 
224 #if (NKBD > 0) || (NMS > 0)
225 	kma.kmta_tp = sc->sc_tty;
226 /* If we figure out we're the console we should point this to our consdev */
227 
228 	/* locate the major number */
229 	maj = cdevsw_lookup_major(&com_cdevsw);
230 
231 	kma.kmta_dev = makedev(maj, device_unit(sc->sc_dev));
232 
233 /* Attach 'em if we got 'em. */
234 #if (NKBD > 0)
235 	kma.kmta_name = "keyboard";
236 	if (prom_getproplen(ea->ea_node, kma.kmta_name) == 0) {
237 		config_found(self, (void *)&kma, NULL, CFARGS_NONE);
238 	}
239 #endif
240 #if (NMS > 0)
241 	kma.kmta_name = "mouse";
242 	if (prom_getproplen(ea->ea_node, kma.kmta_name) == 0) {
243 		config_found(self, (void *)&kma, NULL, CFARGS_NONE);
244 	}
245 #endif
246 #endif
247 	if (kma.kmta_consdev) {
248 		/*
249 		 * If we're the keyboard then we need the original
250 		 * cn_tab w/prom I/O, which sunkbd copied into kma.
251 		 * Otherwise we want conscom which we copied into
252 		 * kma.kmta_consdev.
253 		 */
254 		cn_tab = kma.kmta_consdev;
255 	}
256 }
257