1*c7fb772bSthorpej /* $NetBSD: com_ebus.c,v 1.36 2021/08/07 16:19:05 thorpej Exp $ */
2ac3f2967Seeh
3ac3f2967Seeh /*
4ac3f2967Seeh * Copyright (c) 1999, 2000 Matthew R. Green
5ac3f2967Seeh * All rights reserved.
6ac3f2967Seeh *
7ac3f2967Seeh * Redistribution and use in source and binary forms, with or without
8ac3f2967Seeh * modification, are permitted provided that the following conditions
9ac3f2967Seeh * are met:
10ac3f2967Seeh * 1. Redistributions of source code must retain the above copyright
11ac3f2967Seeh * notice, this list of conditions and the following disclaimer.
12ac3f2967Seeh * 2. Redistributions in binary form must reproduce the above copyright
13ac3f2967Seeh * notice, this list of conditions and the following disclaimer in the
14ac3f2967Seeh * documentation and/or other materials provided with the distribution.
15ac3f2967Seeh *
16ac3f2967Seeh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17ac3f2967Seeh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ac3f2967Seeh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ac3f2967Seeh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20ac3f2967Seeh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21ac3f2967Seeh * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22ac3f2967Seeh * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23ac3f2967Seeh * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24ac3f2967Seeh * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ac3f2967Seeh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ac3f2967Seeh * SUCH DAMAGE.
27ac3f2967Seeh */
28ac3f2967Seeh
29ac3f2967Seeh /*
30ac3f2967Seeh * NS Super I/O PC87332VLJ "com" to ebus attachment
31ac3f2967Seeh */
32ac3f2967Seeh
33ed517291Slukem #include <sys/cdefs.h>
34*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: com_ebus.c,v 1.36 2021/08/07 16:19:05 thorpej Exp $");
35ed517291Slukem
36ac3f2967Seeh #include <sys/types.h>
37ac3f2967Seeh #include <sys/param.h>
38ac3f2967Seeh #include <sys/systm.h>
39ac3f2967Seeh #include <sys/device.h>
40ac3f2967Seeh #include <sys/tty.h>
41ac3f2967Seeh #include <sys/conf.h>
42ac3f2967Seeh
43b6584574Sdyoung #include <sys/bus.h>
44ac3f2967Seeh #include <machine/autoconf.h>
45ac3f2967Seeh #include <machine/openfirm.h>
46ac3f2967Seeh
47f9358deeSmrg #include <dev/ebus/ebusreg.h>
480eed6807Smrg #include <dev/ebus/ebusvar.h>
49ac3f2967Seeh
50ac3f2967Seeh #include <dev/cons.h>
51ac3f2967Seeh #include <dev/ic/comvar.h>
52ac3f2967Seeh #include <dev/sun/kbd_ms_ttyvar.h>
53ac3f2967Seeh
54ac3f2967Seeh #include "kbd.h"
55ac3f2967Seeh #include "ms.h"
56ac3f2967Seeh
57607ead0eScube int com_ebus_match(device_t, cfdata_t , void *);
58607ead0eScube void com_ebus_attach(device_t, device_t, void *);
59ac3f2967Seeh
60607ead0eScube CFATTACH_DECL_NEW(com_ebus, sizeof(struct com_softc),
614bf871a7Sthorpej com_ebus_match, com_ebus_attach, NULL, NULL);
62ac3f2967Seeh
631a18f24fSchristos static const char *com_names[] = {
64ac3f2967Seeh "su",
65ac3f2967Seeh "su_pnp",
664096a9f1Sjdc "rsc-console",
67ac3f2967Seeh NULL
68ac3f2967Seeh };
69ac3f2967Seeh
70ac3f2967Seeh int
com_ebus_match(device_t parent,cfdata_t match,void * aux)71607ead0eScube com_ebus_match(device_t parent, cfdata_t match, void *aux)
72ac3f2967Seeh {
73ac3f2967Seeh struct ebus_attach_args *ea = aux;
74ac3f2967Seeh int i;
75ac3f2967Seeh
76ac3f2967Seeh for (i = 0; com_names[i]; i++)
77ac3f2967Seeh if (strcmp(ea->ea_name, com_names[i]) == 0)
78ac3f2967Seeh return (1);
79ac3f2967Seeh
807e8421faSeeh if (strcmp(ea->ea_name, "serial") == 0) {
8105a2b230Spk char *compat;
827e8421faSeeh
837e8421faSeeh /* Could be anything. */
8405a2b230Spk compat = prom_getpropstring(ea->ea_node, "compatible");
857e8421faSeeh if (strcmp(compat, "su16550") == 0 ||
8679023f55Smrg strcmp(compat, "su16552") == 0 ||
870d643fd1Smrg strcmp(compat, "su") == 0 ||
880d643fd1Smrg strcmp(compat, "FJSV,su") == 0) {
897e8421faSeeh return (1);
907e8421faSeeh }
917e8421faSeeh }
92ac3f2967Seeh return (0);
93ac3f2967Seeh }
94ac3f2967Seeh
95ac3f2967Seeh #define BAUD_BASE (1846200)
96ac3f2967Seeh
97ac3f2967Seeh void
com_ebus_attach(device_t parent,device_t self,void * aux)98607ead0eScube com_ebus_attach(device_t parent, device_t self, void *aux)
99ac3f2967Seeh {
100607ead0eScube struct com_softc *sc = device_private(self);
101ac3f2967Seeh struct ebus_attach_args *ea = aux;
102ac3f2967Seeh struct kbd_ms_tty_attach_args kma;
103ac3f2967Seeh #if (NKBD > 0) || (NMS > 0)
104ac3f2967Seeh int maj;
10577a6b82bSgehenna extern const struct cdevsw com_cdevsw;
106ac3f2967Seeh #endif
107ac3f2967Seeh int i;
108cd58dffbSeeh int com_is_input;
109cd58dffbSeeh int com_is_output;
11034537908Sgdamore bus_space_handle_t ioh;
11134537908Sgdamore bus_space_tag_t iot;
11234537908Sgdamore bus_addr_t iobase;
1130d643fd1Smrg int node, port;
1140d643fd1Smrg char buf[32];
115ac3f2967Seeh
116607ead0eScube sc->sc_dev = self;
11734537908Sgdamore iot = ea->ea_bustag;
11834537908Sgdamore iobase = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
11934537908Sgdamore
120ac3f2967Seeh /*
121e00173a7Swiz * Addresses that should be supplied by the prom:
122ac3f2967Seeh * - normal com registers
123ac3f2967Seeh * - ns873xx configuration registers
124ac3f2967Seeh * - DMA space
125ac3f2967Seeh * The `com' driver does not use DMA accesses, so we can
126ac3f2967Seeh * ignore that for now. We should enable the com port in
127ac3f2967Seeh * the ns873xx registers here. XXX
128ac3f2967Seeh *
129ac3f2967Seeh * Use the prom address if there.
130ac3f2967Seeh */
13147ef6a51Seeh
1320eed6807Smrg if (ea->ea_nvaddr)
13334537908Sgdamore sparc_promaddr_to_handle(iot, ea->ea_vaddr[0],
13434537908Sgdamore &ioh);
13534537908Sgdamore else if (bus_space_map(iot, iobase,
13634537908Sgdamore ea->ea_reg[0].size, 0, &ioh) != 0) {
137607ead0eScube aprint_error(": can't map register space\n");
138ac3f2967Seeh return;
139ac3f2967Seeh }
1406d487047Sthorpej com_init_regs(&sc->sc_regs, iot, ioh, iobase);
14134537908Sgdamore
142ac3f2967Seeh sc->sc_hwflags = 0;
143ac3f2967Seeh sc->sc_frequency = BAUD_BASE;
144ac3f2967Seeh
1450eed6807Smrg for (i = 0; i < ea->ea_nintr; i++)
1460eed6807Smrg bus_intr_establish(ea->ea_bustag, ea->ea_intr[i],
147725a6aebSpk IPL_SERIAL, comintr, sc);
148ac3f2967Seeh
149ac3f2967Seeh kma.kmta_consdev = NULL;
150cd58dffbSeeh
1510d643fd1Smrg /*
1520d643fd1Smrg * Figure out if we're the console.
1530d643fd1Smrg *
1540d643fd1Smrg * The Fujitsu SPARC Enterprise M4000/M5000/M8000/M9000 has a
1550d643fd1Smrg * serial port on each I/O board and a pseudo console that is
1560d643fd1Smrg * redirected to one of these serial ports. The board number
1570d643fd1Smrg * of the serial port in question is encoded in the "tty-port#"
1580d643fd1Smrg * property of the pseudo console, so we figure out what our
1590d643fd1Smrg * board number is by walking up the device tree, and check
1600d643fd1Smrg * for a match.
1610d643fd1Smrg */
1620d643fd1Smrg node = prom_instance_to_package(prom_stdin());
1630d643fd1Smrg com_is_input = (ea->ea_node == node);
1640d643fd1Smrg if (OF_getprop(node, "name", buf, sizeof(buf)) > 0 &&
1650d643fd1Smrg strcmp(buf, "pseudo-console") == 0) {
1660d643fd1Smrg port = prom_getpropint(node, "tty-port#", -1);
1670d643fd1Smrg node = OF_parent(OF_parent(ea->ea_node));
1680d643fd1Smrg com_is_input = (prom_getpropint(node, "board#", -2) == port);
1690d643fd1Smrg }
1700d643fd1Smrg
1710d643fd1Smrg node = prom_instance_to_package(prom_stdout());
1720d643fd1Smrg com_is_output = (ea->ea_node == node);
1730d643fd1Smrg if (OF_getprop(node, "name", buf, sizeof(buf)) > 0 &&
1740d643fd1Smrg strcmp(buf, "pseudo-console") == 0) {
1750d643fd1Smrg port = prom_getpropint(node, "tty-port#", -1);
1760d643fd1Smrg node = OF_parent(OF_parent(ea->ea_node));
1770d643fd1Smrg com_is_output = (prom_getpropint(node, "board#", -2) == port);
1780d643fd1Smrg }
179cd58dffbSeeh
180cd58dffbSeeh if (com_is_input || com_is_output) {
181cd58dffbSeeh struct consdev *cn_orig;
182ac3f2967Seeh
183ac3f2967Seeh /* Record some info to attach console. */
1844096a9f1Sjdc if (strcmp(ea->ea_name, "rsc-console") == 0)
1854096a9f1Sjdc kma.kmta_baud = 115200;
1864096a9f1Sjdc else
187ac3f2967Seeh kma.kmta_baud = 9600;
188ac3f2967Seeh kma.kmta_cflag = (CREAD | CS8 | HUPCL);
1897e8421faSeeh
1907e8421faSeeh /* Attach com as the console. */
191cd58dffbSeeh cn_orig = cn_tab;
19234537908Sgdamore if (comcnattach(iot, iobase, kma.kmta_baud,
1930eff6718Sthorpej sc->sc_frequency, COM_TYPE_NORMAL, kma.kmta_cflag)) {
194607ead0eScube aprint_error("Error: comcnattach failed\n");
195ac3f2967Seeh }
196cd58dffbSeeh if (com_is_input) {
197933215c1Snakayama cn_orig->cn_dev = cn_tab->cn_dev;
198933215c1Snakayama cn_orig->cn_probe = cn_tab->cn_probe;
199933215c1Snakayama cn_orig->cn_init = cn_tab->cn_init;
200933215c1Snakayama cn_orig->cn_getc = cn_tab->cn_getc;
201933215c1Snakayama cn_orig->cn_pollc = cn_tab->cn_pollc;
202cd58dffbSeeh }
203cd58dffbSeeh if (com_is_output) {
204933215c1Snakayama cn_orig->cn_putc = cn_tab->cn_putc;
205cd58dffbSeeh }
206933215c1Snakayama cn_tab = cn_orig;
207cd58dffbSeeh kma.kmta_consdev = cn_tab;
2087e8421faSeeh }
2090d643fd1Smrg
2100d643fd1Smrg /*
2110d643fd1Smrg * Apparently shoving too much data down the TX FIFO on the
2120d643fd1Smrg * Fujitsu SPARC Enterprise M4000/M5000 causes a hardware
2130d643fd1Smrg * fault. Avoid this issue by setting the FIFO depth to 1.
2140d643fd1Smrg * This will effectively disable the TX FIFO, but will still
2150d643fd1Smrg * enable the RX FIFO, which is what we really care about.
2160d643fd1Smrg */
2170d643fd1Smrg if (OF_getprop(ea->ea_node, "compatible", buf, sizeof(buf)) > 0 &&
2180d643fd1Smrg strcmp(buf, "FJSV,su") == 0)
2190d643fd1Smrg sc->sc_fifolen = 1;
2200d643fd1Smrg
2217e8421faSeeh /* Now attach the driver */
2227e8421faSeeh com_attach_subr(sc);
223ac3f2967Seeh
224ac3f2967Seeh #if (NKBD > 0) || (NMS > 0)
225ac3f2967Seeh kma.kmta_tp = sc->sc_tty;
226ac3f2967Seeh /* If we figure out we're the console we should point this to our consdev */
227ac3f2967Seeh
228ac3f2967Seeh /* locate the major number */
22977a6b82bSgehenna maj = cdevsw_lookup_major(&com_cdevsw);
230ac3f2967Seeh
231607ead0eScube kma.kmta_dev = makedev(maj, device_unit(sc->sc_dev));
232ac3f2967Seeh
233ac3f2967Seeh /* Attach 'em if we got 'em. */
234ac3f2967Seeh #if (NKBD > 0)
235ac3f2967Seeh kma.kmta_name = "keyboard";
23605a2b230Spk if (prom_getproplen(ea->ea_node, kma.kmta_name) == 0) {
237*c7fb772bSthorpej config_found(self, (void *)&kma, NULL, CFARGS_NONE);
238ac3f2967Seeh }
239ac3f2967Seeh #endif
240ac3f2967Seeh #if (NMS > 0)
241ac3f2967Seeh kma.kmta_name = "mouse";
24205a2b230Spk if (prom_getproplen(ea->ea_node, kma.kmta_name) == 0) {
243*c7fb772bSthorpej config_found(self, (void *)&kma, NULL, CFARGS_NONE);
244ac3f2967Seeh }
245ac3f2967Seeh #endif
246ac3f2967Seeh #endif
247ac3f2967Seeh if (kma.kmta_consdev) {
248ac3f2967Seeh /*
249ac3f2967Seeh * If we're the keyboard then we need the original
250ac3f2967Seeh * cn_tab w/prom I/O, which sunkbd copied into kma.
251ac3f2967Seeh * Otherwise we want conscom which we copied into
252ac3f2967Seeh * kma.kmta_consdev.
253ac3f2967Seeh */
254ac3f2967Seeh cn_tab = kma.kmta_consdev;
255ac3f2967Seeh }
256ac3f2967Seeh }
257