xref: /openbsd/sys/arch/sparc64/dev/com_ebus.c (revision eb7eaf8d)
1 /*	$OpenBSD: com_ebus.c,v 1.25 2021/10/24 17:05:03 mpi Exp $	*/
2 /*	$NetBSD: com_ebus.c,v 1.6 2001/07/24 19:27:10 eeh Exp $	*/
3 
4 /*
5  * Copyright (c) 1999, 2000 Matthew R. Green
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * NS Super I/O PC87332VLJ "com" to ebus attachment
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/tty.h>
38 #include <sys/conf.h>
39 
40 #include <machine/bus.h>
41 #include <machine/autoconf.h>
42 #include <machine/openfirm.h>
43 
44 #include <sparc64/dev/ebusreg.h>
45 #include <sparc64/dev/ebusvar.h>
46 
47 #include <dev/cons.h>
48 #include <dev/ic/comvar.h>
49 
50 cdev_decl(com); /* XXX this belongs elsewhere */
51 
52 int	com_ebus_match(struct device *, void *, void *);
53 void	com_ebus_attach(struct device *, struct device *, void *);
54 int	com_ebus_speed(struct ebus_attach_args *);
55 
56 const struct cfattach com_ebus_ca = {
57 	sizeof(struct com_softc), com_ebus_match, com_ebus_attach
58 };
59 
60 static char *com_names[] = {
61 	"su",
62 	"su_pnp",
63 	"rsc-console",
64 	"lom-console",
65 	NULL
66 };
67 
68 static inline int
com_match_ikkaku(void)69 com_match_ikkaku(void)
70 {
71 	char model[80];
72 	int i;
73 
74 	i = OF_getproplen(findroot(), "model");
75 	if (i == 0)
76 		return (0);
77 	if (OF_getprop(findroot(), "model", model, sizeof(model)) != i)
78 		return (0);
79 
80 	return (strcmp(model, "IKKAKU") == 0);
81 }
82 
83 int
com_ebus_match(struct device * parent,void * match,void * aux)84 com_ebus_match(struct device *parent, void *match, void *aux)
85 {
86 	struct ebus_attach_args *ea = aux;
87 	int i;
88 
89 	for (i=0; com_names[i]; i++)
90 		if (strcmp(ea->ea_name, com_names[i]) == 0)
91 			return (1);
92 
93 	if (strcmp(ea->ea_name, "serial") == 0) {
94 		char compat[80];
95 
96 		/* blacklist com on m3000s because it causes hardware faults */
97 		if (com_match_ikkaku())
98 			return (0);
99 
100 		/* Could be anything. */
101 		if ((i = OF_getproplen(ea->ea_node, "compatible")) &&
102 			OF_getprop(ea->ea_node, "compatible", compat,
103 				sizeof(compat)) == i) {
104 			if (strcmp(compat, "su16552") == 0 ||
105 			    strcmp(compat, "su16550") == 0 ||
106 			    strcmp(compat, "FJSV,su") == 0 ||
107 			    strcmp(compat, "su") == 0) {
108 				return (1);
109 			}
110 		}
111 	}
112 	return (0);
113 }
114 
115 /* XXXART - was 1846200 */
116 #define BAUD_BASE       (1843200)
117 
118 void
com_ebus_attach(struct device * parent,struct device * self,void * aux)119 com_ebus_attach(struct device *parent, struct device *self, void *aux)
120 {
121 	struct com_softc *sc = (void *)self;
122 	struct ebus_attach_args *ea = aux;
123 	int i, com_is_input, com_is_output;
124 	int node, port;
125 	char buf[32];
126 
127 	sc->sc_iobase = EBUS_PADDR_FROM_REG(&ea->ea_regs[0]);
128 	/*
129 	 * Addresses that should be supplied by the prom:
130 	 *	- normal com registers
131 	 *	- ns873xx configuration registers
132 	 *	- DMA space
133 	 * The `com' driver does not use DMA accesses, so we can
134 	 * ignore that for now.  We should enable the com port in
135 	 * the ns873xx registers here. XXX
136 	 *
137 	 * Use the prom address if there.
138 	 */
139 	if (ea->ea_nvaddrs) {
140 		if (bus_space_map(ea->ea_memtag, ea->ea_vaddrs[0], 0,
141 		    BUS_SPACE_MAP_PROMADDRESS, &sc->sc_ioh) != 0) {
142 			printf(": can't map register space\n");
143 			return;
144 		}
145 		sc->sc_iot = ea->ea_memtag;
146 	} else if (ebus_bus_map(ea->ea_memtag, 0,
147 	    EBUS_PADDR_FROM_REG(&ea->ea_regs[0]),
148 	    ea->ea_regs[0].size, 0, 0, &sc->sc_ioh) == 0) {
149 		sc->sc_iot = ea->ea_memtag;
150 	} else if (ebus_bus_map(ea->ea_iotag, 0,
151 	    EBUS_PADDR_FROM_REG(&ea->ea_regs[0]),
152 	    ea->ea_regs[0].size, 0, 0, &sc->sc_ioh) == 0) {
153 		sc->sc_iot = ea->ea_iotag;
154 	} else {
155 		printf(": can't map register space\n");
156                	return;
157 	}
158 	sc->sc_hwflags = 0;
159 	sc->sc_swflags = 0;
160 	sc->sc_frequency = BAUD_BASE;
161 
162 	for (i = 0; i < ea->ea_nintrs; i++)
163 		bus_intr_establish(sc->sc_iot, ea->ea_intrs[i],
164 		    IPL_TTY, 0, comintr, sc, self->dv_xname);
165 
166 	/*
167 	 * Figure out if we're the console.
168 	 *
169 	 * The Fujitsu SPARC Enterprise M4000/M5000/M8000/M9000 has a
170 	 * serial port on each I/O board and a pseudo console that is
171 	 * redirected to one of these serial ports.  The board number
172 	 * of the serial port in question is encoded in the "tty-port#"
173 	 * property of the pseudo console, so we figure out what our
174 	 * board number is by walking up the device tree, and check
175 	 * for a match.
176 	 */
177 
178 	node = OF_instance_to_package(OF_stdin());
179 	com_is_input = (ea->ea_node == node);
180 	if (OF_getprop(node, "name", buf, sizeof(buf)) > 0 &&
181 	    strcmp(buf, "pseudo-console") == 0) {
182 		port = getpropint(node, "tty-port#", -1);
183 		node = OF_parent(OF_parent(ea->ea_node));
184 		com_is_input = (getpropint(node, "board#", -2) == port);
185 	}
186 
187 	node = OF_instance_to_package(OF_stdout());
188 	com_is_output = (ea->ea_node == node);
189 	if (OF_getprop(node, "name", buf, sizeof(buf)) > 0 &&
190 	    strcmp(buf, "pseudo-console") == 0) {
191 		port = getpropint(node, "tty-port#", -1);
192 		node = OF_parent(OF_parent(ea->ea_node));
193 		com_is_output = (getpropint(node, "board#", -2) == port);
194 	}
195 
196 	if (com_is_input || com_is_output) {
197 		struct consdev *cn_orig;
198 		int speed;
199 
200 		speed = com_ebus_speed(ea);
201 
202 		comconsioh = sc->sc_ioh;
203 		cn_orig = cn_tab;
204 		/* Attach com as the console. */
205 		if (comcnattach(sc->sc_iot, sc->sc_iobase, speed,
206 		    sc->sc_frequency,
207 		    ((TTYDEF_CFLAG & ~(CSIZE | PARENB))|CREAD | CS8 | HUPCL))) {
208 			printf("Error: comcnattach failed\n");
209 		}
210 		cn_tab = cn_orig;
211 		if (com_is_input) {
212 			cn_tab->cn_dev = /*XXX*/makedev(36, sc->sc_dev.dv_unit);
213 			cn_tab->cn_probe = comcnprobe;
214 			cn_tab->cn_init = comcninit;
215 			cn_tab->cn_getc = comcngetc;
216 			cn_tab->cn_pollc = comcnpollc;
217 		}
218 		if (com_is_output)
219 			cn_tab->cn_putc = comcnputc;
220 	}
221 
222 	/*
223 	 * Apparently shoving too much data down the TX FIFO on the
224 	 * Fujitsu SPARC Enterprise M4000/M5000 causes a hardware
225 	 * fault.  Avoid this issue by setting the FIFO depth to 1.
226 	 * This will effectively disable the TX FIFO, but will still
227 	 * enable the RX FIFO, which is what we really care about.
228 	 */
229 	if (OF_getprop(ea->ea_node, "compatible", buf, sizeof(buf)) > 0 &&
230 	    strcmp(buf, "FJSV,su") == 0)
231 		sc->sc_uarttype = COM_UART_16550;
232 
233         if (OF_getproplen(ea->ea_node, "keyboard") == 0)
234 		printf(": keyboard");
235 	else if (OF_getproplen(ea->ea_node, "mouse") == 0)
236 		printf(": mouse");
237 
238 	/* Now attach the driver */
239 	com_attach_subr(sc);
240 }
241 
242 int
com_ebus_speed(struct ebus_attach_args * ea)243 com_ebus_speed(struct ebus_attach_args *ea)
244 {
245 	char buf[128];
246 	char *name = NULL;
247 	int aliases, options;
248 
249 	if (strcmp(ea->ea_name, "rsc-console") == 0)
250 		return 115200;
251 
252 	aliases = OF_finddevice("/aliases");
253 	if (OF_getprop(aliases, "ttya", buf, sizeof(buf)) != -1 &&
254 	    OF_finddevice(buf) == ea->ea_node)
255 		name = "ttya-mode";
256 	if (OF_getprop(aliases, "ttyb", buf, sizeof(buf)) != -1 &&
257 	    OF_finddevice(buf) == ea->ea_node)
258 		name = "ttyb-mode";
259 
260 	if (name == NULL)
261 		return TTYDEF_SPEED;
262 
263 	options = OF_finddevice("/options");
264 	return (getpropspeed(options, name));
265 }
266