xref: /netbsd-src/sys/dev/sbus/sio16.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: sio16.c,v 1.12 2004/03/17 17:04:59 pk 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.12 2004/03/17 17:04:59 pk 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 CFATTACH_DECL(siosixteen, sizeof(struct sio16_softc),
95     sio16_match, sio16_attach, NULL, NULL);
96 
97 struct sio16_attach_args {
98 	bus_space_tag_t		cd_tag;
99 	bus_space_handle_t	cd_handle;
100 	u_char			(*cd_ackfunc)(void *, int);
101 	void			*cd_ackfunc_arg;
102 	u_int			cd_osc;
103 };
104 
105 /*
106  * device match routine:  is there an sio16 present?
107  *
108  * note that we can not put "sio16" in the cfdriver, as this is an
109  * illegal name, so we have to hard code it here.
110  */
111 #define	SIO16_ROM_NAME	"sio16"
112 int
113 sio16_match(parent, cf, aux)
114 	struct device *parent;
115 	struct cfdata *cf;
116 	void   *aux;
117 {
118 	struct sbus_attach_args *sa = aux;
119 
120 	/* does the prom think i'm an sio16? */
121 	if (strcmp(SIO16_ROM_NAME, sa->sa_name) != 0)
122 		return (0);
123 
124 	return (1);
125 }
126 
127 /*
128  * device attach routine:  go attach all sub devices.
129  */
130 void
131 sio16_attach(parent, self, aux)
132 	struct device *parent, *self;
133 	void   *aux;
134 {
135 	struct sbus_attach_args *sa = aux;
136 	struct sio16_softc *sc = (struct sio16_softc *)self;
137 	bus_space_handle_t h;
138 	char *mode, *model;
139 	int i;
140 
141 	if (sa->sa_nreg != 4)
142 		panic("sio16_attach: got %d registers intead of 4",
143 		    sa->sa_nreg);
144 
145 	/* copy our bus tag, we will need it */
146 	sc->sc_tag = sa->sa_bustag;
147 
148 	/*
149 	 * sio16 has 4 register mappings.  a single byte configuration
150 	 * register, 2 128 byte regions for the cd180 registers, and
151 	 * a 4 byte region for interrupt acknowledgement.
152 	 */
153 	if (sbus_bus_map(sa->sa_bustag,
154 			 sa->sa_reg[0].oa_space,
155 			 sa->sa_reg[0].oa_base,
156 			 sa->sa_reg[0].oa_size,
157 			 0, &h) != 0) {
158 		printf("%s at sbus: can not map registers 0\n",
159 		    self->dv_xname);
160 		return;
161 	}
162 	sc->sc_configreg = h;
163 	if (sbus_bus_map(sa->sa_bustag,
164 			 sa->sa_reg[1].sbr_slot,
165 			 sa->sa_reg[1].sbr_offset,
166 			 sa->sa_reg[1].sbr_size,
167 			 0, &h) != 0) {
168 		printf("%s at sbus: can not map registers 1\n",
169 		    self->dv_xname);
170 		return;
171 	}
172 	sc->sc_reg[0] = h;
173 	if (sbus_bus_map(sa->sa_bustag,
174 			 sa->sa_reg[2].sbr_slot,
175 			 sa->sa_reg[2].sbr_offset,
176 			 sa->sa_reg[2].sbr_size,
177 			 0, &h) != 0) {
178 		printf("%s at sbus: can not map registers 2\n",
179 		    self->dv_xname);
180 		return;
181 	}
182 	sc->sc_reg[1] = h;
183 	if (sbus_bus_map(sa->sa_bustag,
184 			 sa->sa_reg[3].sbr_slot,
185 			 sa->sa_reg[3].sbr_offset,
186 			 sa->sa_reg[3].sbr_size,
187 			 0, &h) != 0) {
188 		printf("%s at sbus: can not map registers 3\n",
189 		    self->dv_xname);
190 		return;
191 	}
192 	sc->sc_ack = h;
193 
194 	mode = prom_getpropstring(sa->sa_node, "mode");
195 	if (mode)
196 		printf(", %s mode", mode);
197 
198 	/* get the clock frequency */
199 	sc->sc_clk = prom_getpropint(sa->sa_node, "clk", 24000);
200 
201 	model = prom_getpropstring(sa->sa_node, "model");
202 	if (model == 0) {
203 		printf(", no model property, bailing\n");
204 		return;
205 	}
206 
207 	/* are we an 1600se? */
208 	if (strcmp(model, "1600se") == 0) {
209 		printf(", 16 channels");
210 		sc->sc_ncd180 = 2;
211 	} else {
212 		printf(", don't know model %s, bailing\n", model);
213 		return;
214 	}
215 
216 	/* set up our sbus connections */
217 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
218 
219 	/* establish interrupt channel */
220 	(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_TTY,
221 	    cd18xx_hardintr, sc);
222 
223 	/* reset the board, and turn on interrupts and I/O */
224 	bus_space_write_1(sa->sa_bustag, sc->sc_configreg, 0, 0);
225 	delay(100);
226 	bus_space_write_1(sa->sa_bustag, sc->sc_configreg, 0,
227 	    SIO16_CONFIGREG_ENABLE_IO | SIO16_CONFIGREG_ENABLE_IRQ |
228 	    (((sa->sa_pri) & 0x0f) >> 2));
229 	delay(10000);
230 
231 	printf("\n");
232 
233 	/* finally, configure the clcd's underneath */
234 	for (i = 0; i < sc->sc_ncd180; i++) {
235 		struct sio16_attach_args cd;
236 
237 		cd.cd_tag = sa->sa_bustag;
238 		cd.cd_osc = sc->sc_clk * 100;
239 		cd.cd_handle = (bus_space_handle_t)sc->sc_reg[i];
240 		cd.cd_ackfunc = sio16_ackfunc;
241 		cd.cd_ackfunc_arg = sc;
242 		(void)config_found(self, (void *)&cd, NULL);
243 	}
244 }
245 
246 /*
247  * note that the addresses used in this function match those assigned
248  * in clcd_attach() below, or the various service match routines.
249  */
250 u_char
251 sio16_ackfunc(v, who)
252 	void *v;
253 	int who;
254 {
255 	struct sio16_softc *sc = v;
256 	bus_size_t addr;
257 
258 	switch (who) {
259 	case CD18xx_INTRACK_RxINT:
260 	case CD18xx_INTRACK_REINT:
261 		addr = SIO16_RINT_ACK;
262 		break;
263 	case CD18xx_INTRACK_TxINT:
264 		addr = SIO16_TINT_ACK;
265 		break;
266 	case CD18xx_INTRACK_MxINT:
267 		addr = SIO16_MINT_ACK;
268 		break;
269 	default:
270 		panic("%s: sio16_ackfunc: unknown ackfunc %d",
271 		    sc->sc_dev.dv_xname, who);
272 	}
273 	return (bus_space_read_1(sc->sc_tag, sc->sc_ack, addr));
274 }
275 
276 /*
277  * we attach two `clcd' instances per 1600se, that each call the
278  * backend cd18xx driver for help.
279  */
280 static int	clcd_match(struct device *, struct cfdata *, void *);
281 static void	clcd_attach(struct device *, struct device *, void *);
282 
283 CFATTACH_DECL(clcd, sizeof(struct cd18xx_softc),
284     clcd_match, clcd_attach, NULL, NULL);
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