xref: /netbsd-src/sys/dev/sbus/sio16.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: sio16.c,v 1.17 2008/05/29 14:51:27 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * aurora technologies nova16 driver.  this board is an sbus card with
31  * an external 16 port serial box.  there are two cirrus logic cd180
32  * 8 port serial chips used in the implementation.
33  *
34  * thanks go to Oliver Aldulea <oli@morcov.bv.ro> for writing the
35  * linux driver of this that helped clear up a couple of issues.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: sio16.c,v 1.17 2008/05/29 14:51:27 mrg Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/device.h>
44 #include <sys/systm.h>
45 
46 #include <machine/autoconf.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(struct device *, struct cfdata *, void *);
68 static void	sio16_attach(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 CFATTACH_DECL(siosixteen, sizeof(struct sio16_softc),
93     sio16_match, sio16_attach, NULL, NULL);
94 
95 struct sio16_attach_args {
96 	bus_space_tag_t		cd_tag;
97 	bus_space_handle_t	cd_handle;
98 	u_char			(*cd_ackfunc)(void *, int);
99 	void			*cd_ackfunc_arg;
100 	u_int			cd_osc;
101 };
102 
103 /*
104  * device match routine:  is there an sio16 present?
105  *
106  * note that we can not put "sio16" in the cfdriver, as this is an
107  * illegal name, so we have to hard code it here.
108  */
109 #define	SIO16_ROM_NAME	"sio16"
110 int
111 sio16_match(parent, cf, aux)
112 	struct device *parent;
113 	struct cfdata *cf;
114 	void   *aux;
115 {
116 	struct sbus_attach_args *sa = aux;
117 
118 	/* does the prom think i'm an sio16? */
119 	if (strcmp(SIO16_ROM_NAME, sa->sa_name) != 0)
120 		return (0);
121 
122 	return (1);
123 }
124 
125 /*
126  * device attach routine:  go attach all sub devices.
127  */
128 void
129 sio16_attach(parent, self, aux)
130 	struct device *parent, *self;
131 	void   *aux;
132 {
133 	struct sbus_attach_args *sa = aux;
134 	struct sio16_softc *sc = (struct sio16_softc *)self;
135 	bus_space_handle_t h;
136 	char *mode, *model;
137 	int i;
138 
139 	if (sa->sa_nreg != 4)
140 		panic("sio16_attach: got %d registers intead of 4",
141 		    sa->sa_nreg);
142 
143 	/* copy our bus tag, we will need it */
144 	sc->sc_tag = sa->sa_bustag;
145 
146 	/*
147 	 * sio16 has 4 register mappings.  a single byte configuration
148 	 * register, 2 128 byte regions for the cd180 registers, and
149 	 * a 4 byte region for interrupt acknowledgement.
150 	 */
151 	if (sbus_bus_map(sa->sa_bustag,
152 			 sa->sa_reg[0].oa_space,
153 			 sa->sa_reg[0].oa_base,
154 			 sa->sa_reg[0].oa_size,
155 			 0, &h) != 0) {
156 		printf("%s at sbus: can not map registers 0\n",
157 		    device_xname(self));
158 		return;
159 	}
160 	sc->sc_configreg = h;
161 	if (sbus_bus_map(sa->sa_bustag,
162 			 sa->sa_reg[1].sbr_slot,
163 			 sa->sa_reg[1].sbr_offset,
164 			 sa->sa_reg[1].sbr_size,
165 			 0, &h) != 0) {
166 		printf("%s at sbus: can not map registers 1\n",
167 		    device_xname(self));
168 		return;
169 	}
170 	sc->sc_reg[0] = h;
171 	if (sbus_bus_map(sa->sa_bustag,
172 			 sa->sa_reg[2].sbr_slot,
173 			 sa->sa_reg[2].sbr_offset,
174 			 sa->sa_reg[2].sbr_size,
175 			 0, &h) != 0) {
176 		printf("%s at sbus: can not map registers 2\n",
177 		    device_xname(self));
178 		return;
179 	}
180 	sc->sc_reg[1] = h;
181 	if (sbus_bus_map(sa->sa_bustag,
182 			 sa->sa_reg[3].sbr_slot,
183 			 sa->sa_reg[3].sbr_offset,
184 			 sa->sa_reg[3].sbr_size,
185 			 0, &h) != 0) {
186 		printf("%s at sbus: can not map registers 3\n",
187 		    device_xname(self));
188 		return;
189 	}
190 	sc->sc_ack = h;
191 
192 	mode = prom_getpropstring(sa->sa_node, "mode");
193 	if (mode)
194 		printf(", %s mode", mode);
195 
196 	/* get the clock frequency */
197 	sc->sc_clk = prom_getpropint(sa->sa_node, "clk", 24000);
198 
199 	model = prom_getpropstring(sa->sa_node, "model");
200 	if (model == 0) {
201 		printf(", no model property, bailing\n");
202 		return;
203 	}
204 
205 	/* are we an 1600se? */
206 	if (strcmp(model, "1600se") == 0) {
207 		printf(", 16 channels");
208 		sc->sc_ncd180 = 2;
209 	} else {
210 		printf(", don't know model %s, bailing\n", model);
211 		return;
212 	}
213 
214 	/* set up our sbus connections */
215 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
216 
217 	/* establish interrupt channel */
218 	(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_TTY,
219 	    cd18xx_hardintr, sc);
220 
221 	/* reset the board, and turn on interrupts and I/O */
222 	bus_space_write_1(sa->sa_bustag, sc->sc_configreg, 0, 0);
223 	delay(100);
224 	bus_space_write_1(sa->sa_bustag, sc->sc_configreg, 0,
225 	    SIO16_CONFIGREG_ENABLE_IO | SIO16_CONFIGREG_ENABLE_IRQ |
226 	    (((sa->sa_pri) & 0x0f) >> 2));
227 	delay(10000);
228 
229 	printf("\n");
230 
231 	/* finally, configure the clcd's underneath */
232 	for (i = 0; i < sc->sc_ncd180; i++) {
233 		struct sio16_attach_args cd;
234 
235 		cd.cd_tag = sa->sa_bustag;
236 		cd.cd_osc = sc->sc_clk * 100;
237 		cd.cd_handle = (bus_space_handle_t)sc->sc_reg[i];
238 		cd.cd_ackfunc = sio16_ackfunc;
239 		cd.cd_ackfunc_arg = sc;
240 		(void)config_found(self, (void *)&cd, NULL);
241 	}
242 }
243 
244 /*
245  * note that the addresses used in this function match those assigned
246  * in clcd_attach() below, or the various service match routines.
247  */
248 u_char
249 sio16_ackfunc(v, who)
250 	void *v;
251 	int who;
252 {
253 	struct sio16_softc *sc = v;
254 	bus_size_t addr;
255 
256 	switch (who) {
257 	case CD18xx_INTRACK_RxINT:
258 	case CD18xx_INTRACK_REINT:
259 		addr = SIO16_RINT_ACK;
260 		break;
261 	case CD18xx_INTRACK_TxINT:
262 		addr = SIO16_TINT_ACK;
263 		break;
264 	case CD18xx_INTRACK_MxINT:
265 		addr = SIO16_MINT_ACK;
266 		break;
267 	default:
268 		panic("%s: sio16_ackfunc: unknown ackfunc %d",
269 		    device_xname(&sc->sc_dev), who);
270 	}
271 	return (bus_space_read_1(sc->sc_tag, sc->sc_ack, addr));
272 }
273 
274 /*
275  * we attach two `clcd' instances per 1600se, that each call the
276  * backend cd18xx driver for help.
277  */
278 static int	clcd_match(struct device *, struct cfdata *, void *);
279 static void	clcd_attach(struct device *, struct device *, void *);
280 
281 CFATTACH_DECL(clcd, sizeof(struct cd18xx_softc),
282     clcd_match, clcd_attach, NULL, NULL);
283 
284 static int
285 clcd_match(parent, cf, aux)
286 	struct device *parent;
287 	struct cfdata *cf;
288 	void *aux;
289 {
290 
291 	/* XXX */
292 	return 1;
293 }
294 
295 static void
296 clcd_attach(parent, self, aux)
297 	struct device *parent, *self;
298 	void *aux;
299 {
300 	struct cd18xx_softc *sc = (struct cd18xx_softc *)self;
301 	struct sio16_attach_args *args = aux;
302 
303 	sc->sc_tag = args->cd_tag;
304 	sc->sc_handle = args->cd_handle;
305 	sc->sc_osc = args->cd_osc;
306 	sc->sc_ackfunc = args->cd_ackfunc;
307 	sc->sc_ackfunc_arg = args->cd_ackfunc_arg;
308 	sc->sc_msmr = SIO16_MINT_ACK;
309 	sc->sc_tsmr = SIO16_TINT_ACK;
310 	sc->sc_rsmr = SIO16_RINT_ACK;
311 
312 	/* call the common code */
313 	cd18xx_attach(sc);
314 }
315