xref: /openbsd-src/sys/dev/sbus/asio.c (revision 22e452df2392a375fd2998343a374f72c327cac3)
1 /*	$OpenBSD: asio.c,v 1.12 2022/03/13 13:34:54 mpi Exp $	*/
2 
3 /*
4  * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
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
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Effort sponsored in part by the Defense Advanced Research Projects
29  * Agency (DARPA) and Air Force Research Laboratory, Air Force
30  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
31  *
32  */
33 
34 /*
35  * Driver for Aurora 210SJ serial ports.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/conf.h>
43 #include <sys/timeout.h>
44 #include <sys/tty.h>
45 
46 #include <machine/bus.h>
47 #include <machine/autoconf.h>
48 #include <machine/openfirm.h>
49 
50 #include <dev/sbus/sbusvar.h>
51 #include <dev/sbus/asioreg.h>
52 #include <dev/ic/comvar.h>
53 
54 #include "asio.h"
55 
56 #define BAUD_BASE       (1843200)
57 
58 struct asio_port {
59 	u_int8_t		ap_inten;
60 	bus_space_handle_t	ap_bh;
61 	struct device		*ap_dev;
62 };
63 
64 struct asio_softc {
65 	struct device		sc_dev;
66 	bus_space_tag_t		sc_bt;
67 	bus_space_handle_t	sc_csr_h;
68 	void			*sc_ih;
69 	struct asio_port	sc_ports[2];
70 	int			sc_nports;
71 };
72 
73 struct asio_attach_args {
74 	char *aaa_name;
75 	int aaa_port;
76 	bus_space_tag_t aaa_iot;
77 	bus_space_handle_t aaa_ioh;
78 	u_int32_t aaa_pri;
79 	u_int8_t aaa_inten;
80 };
81 
82 int	asio_match(struct device *, void *, void *);
83 void	asio_attach(struct device *, struct device *, void *);
84 int	asio_print(void *, const char *);
85 void	asio_intr_enable(struct device *, u_int8_t);
86 
87 const struct cfattach asio_ca = {
88 	sizeof(struct asio_softc), asio_match, asio_attach
89 };
90 
91 struct cfdriver asio_cd = {
92 	NULL, "asio", DV_DULL
93 };
94 
95 int
asio_match(struct device * parent,void * match,void * aux)96 asio_match(struct device *parent, void *match, void *aux)
97 {
98 	struct sbus_attach_args *sa = aux;
99 
100 	if (strcmp(sa->sa_name, "sio2") == 0)
101 		return (1);
102 	return (0);
103 }
104 
105 void
asio_attach(struct device * parent,struct device * self,void * aux)106 asio_attach(struct device *parent, struct device *self, void *aux)
107 {
108 	struct asio_softc *sc = (void *)self;
109 	struct sbus_attach_args *sa = aux;
110 	struct asio_attach_args aaa;
111 	int i;
112 	char *model;
113 
114 	sc->sc_bt = sa->sa_bustag;
115 	sc->sc_nports = 2;
116 
117 	model = getpropstring(sa->sa_node, "model");
118 	if (model == NULL) {
119 		printf(": empty model, unsupported\n");
120 		return;
121 	}
122 	if (strcmp(model, "210sj") != 0) {
123 		printf(": unsupported model %s\n", model);
124 		return;
125 	}
126 
127 	if (sa->sa_nreg < 3) {
128 		printf(": %d registers expected, got %d\n",
129 		    3, sa->sa_nreg);
130 		return;
131 	}
132 
133 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
134 	    sa->sa_reg[0].sbr_offset, sa->sa_reg[0].sbr_size,
135 	    0, 0, &sc->sc_csr_h)) {
136 		printf(": couldn't map csr\n");
137 		return;
138 	}
139 
140 	for (i = 0; i < sc->sc_nports; i++) {
141 		if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[i + 1].sbr_slot,
142 		    sa->sa_reg[i + 1].sbr_offset, sa->sa_reg[i + 1].sbr_size,
143 		    0, 0, &sc->sc_ports[i].ap_bh)) {
144 			printf(": couldn't map uart%d\n", i);
145 			return;
146 		}
147 	}
148 
149 	sc->sc_ports[0].ap_inten = ASIO_CSR_SJ_UART0_INTEN;
150 	sc->sc_ports[1].ap_inten = ASIO_CSR_UART1_INTEN;
151 
152 	printf(": %s\n", model);
153 
154 	for (i = 0; i < sc->sc_nports; i++) {
155 		aaa.aaa_name = "com";
156 		aaa.aaa_port = i;
157 		aaa.aaa_iot = sc->sc_bt;
158 		aaa.aaa_ioh = sc->sc_ports[i].ap_bh;
159 		aaa.aaa_inten = sc->sc_ports[i].ap_inten;
160 		aaa.aaa_pri = sa->sa_intr[0].sbi_pri;
161 		sc->sc_ports[i].ap_dev = config_found(self, &aaa, asio_print);
162 	}
163 }
164 
165 int
asio_print(void * aux,const char * name)166 asio_print(void *aux, const char *name)
167 {
168 	struct asio_attach_args *aaa = aux;
169 
170 	if (name != NULL)
171 		printf("%s at %s", aaa->aaa_name, name);
172 	printf(" port %d", aaa->aaa_port);
173 	return (UNCONF);
174 }
175 
176 #if NCOM_ASIO > 0
177 int	com_asio_match(struct device *, void *, void *);
178 void	com_asio_attach(struct device *, struct device *, void *);
179 
180 const struct cfattach com_asio_ca = {
181 	sizeof(struct com_softc), com_asio_match, com_asio_attach
182 };
183 
184 void
asio_intr_enable(struct device * dv,u_int8_t en)185 asio_intr_enable(struct device *dv, u_int8_t en)
186 {
187 	struct asio_softc *sc = (struct asio_softc *)dv;
188 	u_int8_t csr;
189 
190 	csr = bus_space_read_1(sc->sc_bt, sc->sc_csr_h, 0);
191 	csr &= ~(ASIO_CSR_SBUS_INT7 | ASIO_CSR_SBUS_INT6);
192 	csr |= ASIO_CSR_SBUS_INT5 | en;
193 	bus_space_write_1(sc->sc_bt, sc->sc_csr_h, 0, csr);
194 }
195 
196 int
com_asio_match(struct device * parent,void * match,void * aux)197 com_asio_match(struct device *parent, void *match, void *aux)
198 {
199 	return (1);
200 }
201 
202 void
com_asio_attach(struct device * parent,struct device * self,void * aux)203 com_asio_attach(struct device *parent, struct device *self, void *aux)
204 {
205 	struct com_softc *sc = (struct com_softc *)self;
206 	struct asio_attach_args *aaa = aux;
207 
208 	sc->sc_iot = aaa->aaa_iot;
209 	sc->sc_ioh = aaa->aaa_ioh;
210 	sc->sc_iobase = 0;   /* XXX WTF is iobase for? It used to be the lower 32 bits of ioh's vaddr... */
211 	sc->sc_hwflags = 0;
212 	sc->sc_swflags = 0;
213 	sc->sc_frequency = BAUD_BASE;
214 
215 	sc->sc_ih = bus_intr_establish(aaa->aaa_iot, aaa->aaa_pri,
216 	    IPL_TTY, 0, comintr, sc, self->dv_xname);
217 	if (sc->sc_ih == NULL) {
218 		printf(": cannot allocate intr\n");
219 		return;
220 	}
221 	asio_intr_enable(parent, aaa->aaa_inten);
222 
223 	com_attach_subr(sc);
224 }
225 #endif
226