xref: /netbsd-src/sys/dev/sbus/sio16.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: sio16.c,v 1.6 2002/09/06 13:18:43 gehenna Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2001 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  * aurora technologies nova16 driver.  this board is an sbus card with
33  * an external 16 port serial box.  there are two cirrus logic cd180
34  * 8 port serial chips used in the implementation.
35  *
36  * thanks go to Oliver Aldulea <oli@morcov.bv.ro> for writing the
37  * linux driver of this that helped clear up a couple of issues.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: sio16.c,v 1.6 2002/09/06 13:18:43 gehenna Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/conf.h>
45 #include <sys/device.h>
46 #include <sys/systm.h>
47 
48 #include <machine/autoconf.h>
49 
50 #include <dev/ic/cd18xxvar.h>
51 #include <dev/ic/cd18xxreg.h>
52 
53 #include "ioconf.h"
54 #include "locators.h"
55 
56 /* 1600se configuration register bits */
57 #define SIO16_CONFIGREG_ENABLE_IO    8
58 #define SIO16_CONFIGREG_ENABLE_IRQ   4
59 
60 /* 1600se interrupt ackknowledge register bytes */
61 #define	SIO16_MINT_ACK		1	/* modem interrupt acknowledge */
62 #define	SIO16_TINT_ACK		2	/* tx interrupt acknowledge */
63 #define	SIO16_RINT_ACK		3	/* rx interrupt acknowledge */
64 
65 /*
66  * device cfattach and cfdriver definitions, plus the routine we pass
67  * to the cd18xx code or interrupt acknowledgement.
68  */
69 static int	sio16_match __P((struct device *, struct cfdata *, void *));
70 static void	sio16_attach __P((struct device *, struct device *, void *));
71 static u_char	sio16_ackfunc(void *, int who);
72 
73 /*
74  * define the sio16 per-device softc.
75  */
76 struct sio16_softc {
77 	struct device	sc_dev;			/* must be first */
78 	struct sbusdev	sc_sd;			/* for sbus drivers */
79 
80 	/* sbus information */
81 	bus_space_tag_t	sc_tag;			/* bus tag for below */
82 	bus_space_handle_t sc_configreg;	/* configuration register */
83 	bus_space_handle_t sc_reg[2];		/* cd180 register sets */
84 	bus_space_handle_t sc_ack;		/* interrupt acknowledgement */
85 #define	SIO16_1600SE	0x00000001
86 
87 	u_int           sc_clk;
88 
89 	/* cd180 information */
90 	int		sc_ncd180;
91 
92 };
93 
94 struct cfattach siosixteen_ca = {
95 	sizeof(struct sio16_softc), sio16_match, sio16_attach
96 };
97 
98 struct sio16_attach_args {
99 	bus_space_tag_t		cd_tag;
100 	bus_space_handle_t	cd_handle;
101 	u_char			(*cd_ackfunc)(void *, int);
102 	void			*cd_ackfunc_arg;
103 	u_int			cd_osc;
104 };
105 
106 /*
107  * device match routine:  is there an sio16 present?
108  *
109  * note that we can not put "sio16" in the cfdriver, as this is an
110  * illegal name, so we have to hard code it here.
111  */
112 #define	SIO16_ROM_NAME	"sio16"
113 int
114 sio16_match(parent, cf, aux)
115 	struct device *parent;
116 	struct cfdata *cf;
117 	void   *aux;
118 {
119 	struct sbus_attach_args *sa = aux;
120 
121 	/* does the prom think i'm an sio16? */
122 	if (strcmp(SIO16_ROM_NAME, sa->sa_name) != 0)
123 		return (0);
124 
125 	return (1);
126 }
127 
128 /*
129  * device attach routine:  go attach all sub devices.
130  */
131 void
132 sio16_attach(parent, self, aux)
133 	struct device *parent, *self;
134 	void   *aux;
135 {
136 	struct sbus_attach_args *sa = aux;
137 	struct sio16_softc *sc = (struct sio16_softc *)self;
138 	bus_space_handle_t h;
139 	char *mode, *model;
140 	int i;
141 
142 	if (sa->sa_nreg != 4)
143 		panic("sio16_attach: got %d registers intead of 4\n",
144 		    sa->sa_nreg);
145 
146 	/* copy our bus tag, we will need it */
147 	sc->sc_tag = sa->sa_bustag;
148 
149 	/*
150 	 * sio16 has 4 register mappings.  a single byte configuration
151 	 * register, 2 128 byte regions for the cd180 registers, and
152 	 * a 4 byte region for interrupt acknowledgement.
153 	 */
154 	if (sbus_bus_map(sa->sa_bustag,
155 			 sa->sa_reg[0].oa_space,
156 			 sa->sa_reg[0].oa_base,
157 			 sa->sa_reg[0].oa_size,
158 			 0, &h) != 0) {
159 		printf("%s at sbus: can not map registers 0\n",
160 		    self->dv_xname);
161 		return;
162 	}
163 	sc->sc_configreg = h;
164 	if (sbus_bus_map(sa->sa_bustag,
165 			 sa->sa_reg[1].sbr_slot,
166 			 sa->sa_reg[1].sbr_offset,
167 			 sa->sa_reg[1].sbr_size,
168 			 0, &h) != 0) {
169 		printf("%s at sbus: can not map registers 1\n",
170 		    self->dv_xname);
171 		return;
172 	}
173 	sc->sc_reg[0] = h;
174 	if (sbus_bus_map(sa->sa_bustag,
175 			 sa->sa_reg[2].sbr_slot,
176 			 sa->sa_reg[2].sbr_offset,
177 			 sa->sa_reg[2].sbr_size,
178 			 0, &h) != 0) {
179 		printf("%s at sbus: can not map registers 2\n",
180 		    self->dv_xname);
181 		return;
182 	}
183 	sc->sc_reg[1] = h;
184 	if (sbus_bus_map(sa->sa_bustag,
185 			 sa->sa_reg[3].sbr_slot,
186 			 sa->sa_reg[3].sbr_offset,
187 			 sa->sa_reg[3].sbr_size,
188 			 0, &h) != 0) {
189 		printf("%s at sbus: can not map registers 3\n",
190 		    self->dv_xname);
191 		return;
192 	}
193 	sc->sc_ack = h;
194 
195 	mode = PROM_getpropstring(sa->sa_node, "mode");
196 	if (mode)
197 		printf(", %s mode", mode);
198 
199 	/* get the clock frequency */
200 	sc->sc_clk = PROM_getpropint(sa->sa_node, "clk", 24000);
201 
202 	model = PROM_getpropstring(sa->sa_node, "model");
203 	if (model == 0) {
204 		printf(", no model property, bailing\n");
205 		return;
206 	}
207 
208 	/* are we an 1600se? */
209 	if (strcmp(model, "1600se") == 0) {
210 		printf(", 16 channels");
211 		sc->sc_ncd180 = 2;
212 	} else {
213 		printf(", don't know model %s, bailing\n", model);
214 		return;
215 	}
216 
217 	/* set up our sbus connections */
218 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
219 
220 	/* establish interrupt channel */
221 	(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_TTY, 0,
222 	    cd18xx_hardintr, sc);
223 
224 	/* reset the board, and turn on interrupts and I/O */
225 	bus_space_write_1(sa->sa_bustag, sc->sc_configreg, 0, 0);
226 	delay(100);
227 	bus_space_write_1(sa->sa_bustag, sc->sc_configreg, 0,
228 	    SIO16_CONFIGREG_ENABLE_IO | SIO16_CONFIGREG_ENABLE_IRQ |
229 	    (((sa->sa_pri) & 0x0f) >> 2));
230 	delay(10000);
231 
232 	printf("\n");
233 
234 	/* finally, configure the clcd's underneath */
235 	for (i = 0; i < sc->sc_ncd180; i++) {
236 		struct sio16_attach_args cd;
237 
238 		cd.cd_tag = sa->sa_bustag;
239 		cd.cd_osc = sc->sc_clk * 100;
240 		cd.cd_handle = (bus_space_handle_t)sc->sc_reg[i];
241 		cd.cd_ackfunc = sio16_ackfunc;
242 		cd.cd_ackfunc_arg = sc;
243 		(void)config_found(self, (void *)&cd, NULL);
244 	}
245 }
246 
247 /*
248  * note that the addresses used in this function match those assigned
249  * in clcd_attach() below, or the various service match routines.
250  */
251 u_char
252 sio16_ackfunc(v, who)
253 	void *v;
254 	int who;
255 {
256 	struct sio16_softc *sc = v;
257 	bus_size_t addr;
258 
259 	switch (who) {
260 	case CD18xx_INTRACK_RxINT:
261 	case CD18xx_INTRACK_REINT:
262 		addr = SIO16_RINT_ACK;
263 		break;
264 	case CD18xx_INTRACK_TxINT:
265 		addr = SIO16_TINT_ACK;
266 		break;
267 	case CD18xx_INTRACK_MxINT:
268 		addr = SIO16_MINT_ACK;
269 		break;
270 	default:
271 		panic("%s: sio16_ackfunc: unknown ackfunc %d",
272 		    sc->sc_dev.dv_xname, who);
273 	}
274 	return (bus_space_read_1(sc->sc_tag, sc->sc_ack, addr));
275 }
276 
277 /*
278  * we attach two `clcd' instances per 1600se, that each call the
279  * backend cd18xx driver for help.
280  */
281 static int	clcd_match(struct device *, struct cfdata *, void *);
282 static void	clcd_attach(struct device *, struct device *, void *);
283 
284 struct cfattach clcd_ca = {
285 	sizeof(struct cd18xx_softc), clcd_match, clcd_attach
286 };
287 
288 static int
289 clcd_match(parent, cf, aux)
290 	struct device *parent;
291 	struct cfdata *cf;
292 	void *aux;
293 {
294 
295 	/* XXX */
296 	return 1;
297 }
298 
299 static void
300 clcd_attach(parent, self, aux)
301 	struct device *parent, *self;
302 	void *aux;
303 {
304 	struct cd18xx_softc *sc = (struct cd18xx_softc *)self;
305 	struct sio16_attach_args *args = aux;
306 
307 	sc->sc_tag = args->cd_tag;
308 	sc->sc_handle = args->cd_handle;
309 	sc->sc_osc = args->cd_osc;
310 	sc->sc_ackfunc = args->cd_ackfunc;
311 	sc->sc_ackfunc_arg = args->cd_ackfunc_arg;
312 	sc->sc_msmr = SIO16_MINT_ACK;
313 	sc->sc_tsmr = SIO16_TINT_ACK;
314 	sc->sc_rsmr = SIO16_RINT_ACK;
315 
316 	/* call the common code */
317 	cd18xx_attach(sc);
318 }
319