xref: /openbsd-src/sys/dev/sbus/apio.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: apio.c,v 1.7 2003/06/27 01:50:52 jason 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 parallel ports.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/conf.h>
44 #include <sys/timeout.h>
45 #include <sys/tty.h>
46 
47 #include <machine/bus.h>
48 #include <machine/autoconf.h>
49 #include <machine/openfirm.h>
50 
51 #include <dev/sbus/sbusvar.h>
52 #include <dev/sbus/asioreg.h>
53 #include <dev/ic/lptvar.h>
54 #include "apio.h"
55 #include "lpt.h"
56 
57 struct apio_softc {
58 	struct device		sc_dev;
59 	bus_space_tag_t		sc_bt;
60 	bus_space_handle_t	sc_csr_h;
61 	bus_space_handle_t	sc_clk_h;
62 	bus_space_handle_t	sc_lpt_h;
63 	void			*sc_ih;
64 	struct device		*sc_port;
65 };
66 
67 struct apio_attach_args {
68 	char *aaa_name;
69 	bus_space_tag_t aaa_iot;
70 	bus_space_handle_t aaa_ioh;
71 	bus_space_handle_t aaa_clkh;
72 	u_int32_t aaa_pri;
73 	u_int8_t aaa_inten;
74 };
75 
76 int	apio_match(struct device *, void *, void *);
77 void	apio_attach(struct device *, struct device *, void *);
78 int	apio_print(void *, const char *);
79 void	apio_intr_enable(struct device *, u_int8_t);
80 
81 struct cfattach apio_ca = {
82 	sizeof(struct apio_softc), apio_match, apio_attach
83 };
84 
85 struct cfdriver apio_cd = {
86 	NULL, "apio", DV_DULL
87 };
88 
89 int
90 apio_match(struct device *parent, void *match, void *aux)
91 {
92 	struct sbus_attach_args *sa = aux;
93 
94 	if (strcmp(sa->sa_name, "pio1") == 0)
95 		return (1);
96 	return (0);
97 }
98 
99 void
100 apio_attach(struct device *parent, struct device *self, void *aux)
101 {
102 	struct apio_softc *sc = (void *)self;
103 	struct sbus_attach_args *sa = aux;
104 	struct apio_attach_args aaa;
105 	char *model;
106 
107 	sc->sc_bt = sa->sa_bustag;
108 
109 	model = getpropstring(sa->sa_node, "model");
110 	if (model == NULL) {
111 		printf(": empty model, unsupported\n");
112 		return;
113 	}
114 	if (strcmp(model, "210sj") != 0) {
115 		printf(": unsupported model %s\n", model);
116 		return;
117 	}
118 
119 	if (sa->sa_nreg < 3) {
120 		printf(": %d registers expected, got %d\n",
121 		    3, sa->sa_nreg);
122 		return;
123 	}
124 
125 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
126 	    sa->sa_reg[0].sbr_offset, sa->sa_reg[0].sbr_size,
127 	    0, 0, &sc->sc_csr_h)) {
128 		printf(": couldn't map csr\n");
129 		return;
130 	}
131 
132 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[1].sbr_slot,
133 	    sa->sa_reg[1].sbr_offset, sa->sa_reg[1].sbr_size,
134 	    0, 0, &sc->sc_clk_h)) {
135 		printf(": couldn't map clk\n");
136 		return;
137 	}
138 
139 	if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[2].sbr_slot,
140 	    sa->sa_reg[2].sbr_offset, sa->sa_reg[2].sbr_size,
141 	    0, 0, &sc->sc_lpt_h)) {
142 		printf(": couldn't map clk\n");
143 		return;
144 	}
145 
146 	printf(": %s\n", model);
147 
148 	aaa.aaa_name = "lpt";
149 	aaa.aaa_iot = sc->sc_bt;
150 	aaa.aaa_ioh = sc->sc_lpt_h;
151 	aaa.aaa_clkh = sc->sc_clk_h;
152 	aaa.aaa_inten = ASIO_CSR_SJ_PAR_INTEN;
153 	aaa.aaa_pri = sa->sa_intr[0].sbi_pri;
154 	sc->sc_port = config_found(self, &aaa, apio_print);
155 }
156 
157 int
158 apio_print(void *aux, const char *name)
159 {
160 	struct apio_attach_args *aaa = aux;
161 
162 	if (name != NULL)
163 		printf("%s at %s", aaa->aaa_name, name);
164 	return (UNCONF);
165 }
166 
167 #if NLPT_APIO > 0
168 int	lpt_apio_match(struct device *, void *, void *);
169 void	lpt_apio_attach(struct device *, struct device *, void *);
170 int	lpt_apio_intr(void *);
171 
172 struct lpt_apio_softc {
173 	struct lpt_softc sc_lpt;
174 	bus_space_handle_t sc_clk_h;
175 	void *sc_ih;
176 };
177 
178 struct cfattach lpt_apio_ca = {
179 	sizeof(struct lpt_apio_softc), lpt_apio_match, lpt_apio_attach
180 };
181 
182 void
183 apio_intr_enable(struct device *dv, u_int8_t en)
184 {
185 	struct apio_softc *sc = (struct apio_softc *)dv;
186 	u_int8_t csr;
187 
188 	csr = bus_space_read_1(sc->sc_bt, sc->sc_csr_h, 0);
189 	csr &= ~(ASIO_CSR_SBUS_INT7 | ASIO_CSR_SBUS_INT6);
190 	csr |= ASIO_CSR_SBUS_INT5 | en;
191 	bus_space_write_1(sc->sc_bt, sc->sc_csr_h, 0, csr);
192 }
193 
194 int
195 lpt_apio_match(struct device *parent, void *match, void *aux)
196 {
197 	return (1);
198 }
199 
200 void
201 lpt_apio_attach(struct device *parent, struct device *self, void *aux)
202 {
203 	struct lpt_apio_softc *sc = (struct lpt_apio_softc *)self;
204 	struct apio_attach_args *aaa = aux;
205 
206 	sc->sc_lpt.sc_state = 0;
207 	sc->sc_lpt.sc_iot = aaa->aaa_iot;
208 	sc->sc_lpt.sc_ioh = aaa->aaa_ioh;
209 	sc->sc_clk_h = aaa->aaa_clkh;
210 	sc->sc_ih = bus_intr_establish(aaa->aaa_iot, aaa->aaa_pri,
211 	    IPL_TTY, 0, lpt_apio_intr, sc, self->dv_xname);
212 	if (sc->sc_ih == NULL) {
213 		printf(": cannot allocate intr\n");
214 		return;
215 	}
216 	apio_intr_enable(parent, aaa->aaa_inten);
217 
218 	lpt_attach_common(&sc->sc_lpt);
219 }
220 
221 int
222 lpt_apio_intr(void *vsc)
223 {
224 	struct lpt_apio_softc *sc = vsc;
225 	int r;
226 
227 	r = lptintr(&sc->sc_lpt);
228 	bus_space_read_1(sc->sc_lpt.sc_iot, sc->sc_clk_h, 0);
229 	return (r);
230 }
231 #endif
232