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