xref: /netbsd-src/sys/arch/hpcmips/dev/tc5165buf.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: tc5165buf.c,v 1.7 2001/02/22 18:38:01 uch Exp $ */
2 
3 /*-
4  * Copyright (c) 1999-2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Device driver for TOSHIBA TC5165BFTS, PHILIPS 74ALVC16241/245
41  * buffer chip
42  */
43 
44 #include "opt_tx39_debug.h"
45 #include "opt_use_poll.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/callout.h>
50 #include <sys/device.h>
51 
52 #include <machine/bus.h>
53 #include <machine/intr.h>
54 
55 #include <dev/hpc/hpckbdvar.h>
56 
57 #include <hpcmips/tx/tx39var.h>
58 #include <hpcmips/tx/txcsbusvar.h>
59 #include <hpcmips/dev/tc5165bufvar.h>
60 
61 #define TC5165_ROW_MAX		16
62 #define TC5165_COLUMN_MAX	8
63 
64 #ifdef TC5165DEBUG
65 #define	DPRINTF(arg) printf arg
66 #else
67 #define	DPRINTF(arg)
68 #endif
69 
70 struct tc5165buf_chip {
71 	bus_space_tag_t		scc_cst;
72 	bus_space_handle_t	scc_csh;
73 	u_int16_t		scc_buf[TC5165_COLUMN_MAX];
74 	int			scc_enabled;
75 	int			scc_queued;
76 	struct callout		scc_soft_ch;
77 	struct hpckbd_ic_if	scc_if;
78 	struct hpckbd_if	*scc_hpckbd;
79 };
80 
81 struct tc5165buf_softc {
82 	struct device		sc_dev;
83 	struct tc5165buf_chip	*sc_chip;
84 	tx_chipset_tag_t	sc_tc;
85 	void			*sc_ih;
86 };
87 
88 int	tc5165buf_match	__P((struct device*, struct cfdata*, void*));
89 void	tc5165buf_attach __P((struct device*, struct device*, void*));
90 int	tc5165buf_intr __P((void*));
91 int	tc5165buf_poll __P((void*));
92 void	tc5165buf_soft __P((void*));
93 void	tc5165buf_ifsetup __P((struct tc5165buf_chip*));
94 
95 int	tc5165buf_input_establish __P((void*, struct hpckbd_if*));
96 
97 struct tc5165buf_chip tc5165buf_chip;
98 
99 struct cfattach tc5165buf_ca = {
100 	sizeof(struct tc5165buf_softc), tc5165buf_match, tc5165buf_attach
101 };
102 
103 int
104 tc5165buf_match(parent, cf, aux)
105 	struct device *parent;
106 	struct cfdata *cf;
107 	void *aux;
108 {
109 	return 1;
110 }
111 
112 void
113 tc5165buf_attach(parent, self, aux)
114 	struct device *parent;
115 	struct device *self;
116 	void *aux;
117 {
118 	struct cs_attach_args *ca = aux;
119 	struct tc5165buf_softc *sc = (void*)self;
120 	struct hpckbd_attach_args haa;
121 
122 	printf(": ");
123 	sc->sc_tc = ca->ca_tc;
124 	sc->sc_chip = &tc5165buf_chip;
125 
126 	callout_init(&sc->sc_chip->scc_soft_ch);
127 
128 	sc->sc_chip->scc_cst = ca->ca_csio.cstag;
129 
130 	if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase,
131 			  ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) {
132 		printf("can't map i/o space\n");
133 		return;
134 	}
135 
136 	sc->sc_chip->scc_enabled = 0;
137 
138 	if (ca->ca_irq1 != -1) {
139 		sc->sc_ih = tx_intr_establish(sc->sc_tc, ca->ca_irq1,
140 					      IST_EDGE, IPL_TTY,
141 					      tc5165buf_intr, sc);
142 		printf("interrupt mode");
143 	} else {
144 		sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1, IPL_TTY,
145 						tc5165buf_intr, sc);
146 		printf("polling mode");
147 	}
148 
149 	if (!sc->sc_ih) {
150 		printf(" can't establish interrupt\n");
151 		return;
152 	}
153 
154 	printf("\n");
155 
156 	/* setup upper interface */
157 	tc5165buf_ifsetup(sc->sc_chip);
158 
159 	haa.haa_ic = &sc->sc_chip->scc_if;
160 
161 	config_found(self, &haa, hpckbd_print);
162 }
163 
164 void
165 tc5165buf_ifsetup(scc)
166 	struct tc5165buf_chip *scc;
167 {
168 	scc->scc_if.hii_ctx		= scc;
169 	scc->scc_if.hii_establish	= tc5165buf_input_establish;
170 	scc->scc_if.hii_poll		= tc5165buf_poll;
171 }
172 
173 int
174 tc5165buf_cnattach(addr)
175 	paddr_t addr;
176 {
177 	struct tc5165buf_chip *scc = &tc5165buf_chip;
178 
179 	scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr);
180 
181 	tc5165buf_ifsetup(scc);
182 
183 	hpckbd_cnattach(&scc->scc_if);
184 
185 	return 0;
186 }
187 
188 int
189 tc5165buf_input_establish(ic, kbdif)
190 	void *ic;
191 	struct hpckbd_if *kbdif;
192 {
193 	struct tc5165buf_chip *scc = ic;
194 
195 	/* save hpckbd interface */
196 	scc->scc_hpckbd = kbdif;
197 
198 	scc->scc_enabled = 1;
199 
200 	return 0;
201 }
202 
203 int
204 tc5165buf_intr(arg)
205 	void *arg;
206 {
207 	struct tc5165buf_softc *sc = arg;
208 	struct tc5165buf_chip *scc = sc->sc_chip;
209 
210 	if (!scc->scc_enabled || scc->scc_queued)
211 		return 0;
212 
213 	scc->scc_queued = 1;
214 	callout_reset(&scc->scc_soft_ch, 1, tc5165buf_soft, scc);
215 
216 	return 0;
217 }
218 
219 int
220 tc5165buf_poll(arg)
221 	void *arg;
222 {
223 	struct tc5165buf_chip *scc = arg;
224 
225 	if (!scc->scc_enabled)
226 		return POLL_CONT;
227 
228 	tc5165buf_soft(arg);
229 
230 	return POLL_CONT;
231 }
232 
233 void
234 tc5165buf_soft(arg)
235 	void *arg;
236 {
237 	struct tc5165buf_chip *scc = arg;
238 	bus_space_tag_t t = scc->scc_cst;
239 	bus_space_handle_t h = scc->scc_csh;
240 	u_int16_t mask, rpat, edge;
241 	int i, j, type, val;
242 	int s;
243 
244 	hpckbd_input_hook(scc->scc_hpckbd);
245 
246 	/* clear scanlines */
247 	(void)bus_space_read_2(t, h, 0);
248 	delay(3);
249 
250 	for (i = 0; i < TC5165_COLUMN_MAX; i++) {
251 		rpat = bus_space_read_2(t, h, 2 << i);
252 		delay(3);
253 		(void)bus_space_read_2(t, h, 0);
254 		delay(3);
255 		if ((edge = (rpat ^ scc->scc_buf[i]))) {
256 			scc->scc_buf[i] = rpat;
257 			for (j = 0, mask = 1; j < TC5165_ROW_MAX;
258 			     j++, mask <<= 1) {
259 				if (mask & edge) {
260 					type = mask & rpat ? 1 : 0;
261 					val = j * TC5165_COLUMN_MAX + i;
262 					DPRINTF(("%d %d\n", j, i));
263 					hpckbd_input(scc->scc_hpckbd,
264 						     type, val);
265 				}
266 			}
267 		}
268 	}
269 
270 	s = spltty();
271 	scc->scc_queued = 0;
272 	splx(s);
273 }
274