xref: /netbsd-src/sys/arch/hpcmips/dev/tc5165buf.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: tc5165buf.c,v 1.8 2001/09/15 12:47:07 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(struct device *, struct cfdata *, void *);
89 void	tc5165buf_attach(struct device *, struct device *, void *);
90 int	tc5165buf_intr(void *);
91 int	tc5165buf_poll(void *);
92 void	tc5165buf_soft(void *);
93 void	tc5165buf_ifsetup(struct tc5165buf_chip *);
94 
95 int	tc5165buf_input_establish(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(struct device *parent, struct cfdata *cf, void *aux)
105 {
106 
107 	return (1);
108 }
109 
110 void
111 tc5165buf_attach(struct device *parent, struct device *self, void *aux)
112 {
113 	struct cs_attach_args *ca = aux;
114 	struct tc5165buf_softc *sc = (void*)self;
115 	struct hpckbd_attach_args haa;
116 
117 	printf(": ");
118 	sc->sc_tc = ca->ca_tc;
119 	sc->sc_chip = &tc5165buf_chip;
120 
121 	callout_init(&sc->sc_chip->scc_soft_ch);
122 
123 	sc->sc_chip->scc_cst = ca->ca_csio.cstag;
124 
125 	if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase,
126 	    ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) {
127 		printf("can't map i/o space\n");
128 		return;
129 	}
130 
131 	sc->sc_chip->scc_enabled = 0;
132 
133 	if (ca->ca_irq1 != -1) {
134 		sc->sc_ih = tx_intr_establish(sc->sc_tc, ca->ca_irq1,
135 		    IST_EDGE, IPL_TTY,
136 		    tc5165buf_intr, sc);
137 		printf("interrupt mode");
138 	} else {
139 		sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1, IPL_TTY,
140 		    tc5165buf_intr, sc);
141 		printf("polling mode");
142 	}
143 
144 	if (!sc->sc_ih) {
145 		printf(" can't establish interrupt\n");
146 		return;
147 	}
148 
149 	printf("\n");
150 
151 	/* setup upper interface */
152 	tc5165buf_ifsetup(sc->sc_chip);
153 
154 	haa.haa_ic = &sc->sc_chip->scc_if;
155 
156 	config_found(self, &haa, hpckbd_print);
157 }
158 
159 void
160 tc5165buf_ifsetup(struct tc5165buf_chip *scc)
161 {
162 
163 	scc->scc_if.hii_ctx		= scc;
164 	scc->scc_if.hii_establish	= tc5165buf_input_establish;
165 	scc->scc_if.hii_poll		= tc5165buf_poll;
166 }
167 
168 int
169 tc5165buf_cnattach(paddr_t addr)
170 {
171 	struct tc5165buf_chip *scc = &tc5165buf_chip;
172 
173 	scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr);
174 
175 	tc5165buf_ifsetup(scc);
176 
177 	hpckbd_cnattach(&scc->scc_if);
178 
179 	return (0);
180 }
181 
182 int
183 tc5165buf_input_establish(void *ic, struct hpckbd_if *kbdif)
184 {
185 	struct tc5165buf_chip *scc = ic;
186 
187 	/* save hpckbd interface */
188 	scc->scc_hpckbd = kbdif;
189 
190 	scc->scc_enabled = 1;
191 
192 	return (0);
193 }
194 
195 int
196 tc5165buf_intr(void *arg)
197 {
198 	struct tc5165buf_softc *sc = arg;
199 	struct tc5165buf_chip *scc = sc->sc_chip;
200 
201 	if (!scc->scc_enabled || scc->scc_queued)
202 		return (0);
203 
204 	scc->scc_queued = 1;
205 	callout_reset(&scc->scc_soft_ch, 1, tc5165buf_soft, scc);
206 
207 	return (0);
208 }
209 
210 int
211 tc5165buf_poll(void *arg)
212 {
213 	struct tc5165buf_chip *scc = arg;
214 
215 	if (!scc->scc_enabled)
216 		return (POLL_CONT);
217 
218 	tc5165buf_soft(arg);
219 
220 	return (POLL_CONT);
221 }
222 
223 void
224 tc5165buf_soft(void *arg)
225 {
226 	struct tc5165buf_chip *scc = arg;
227 	bus_space_tag_t t = scc->scc_cst;
228 	bus_space_handle_t h = scc->scc_csh;
229 	u_int16_t mask, rpat, edge;
230 	int i, j, type, val;
231 	int s;
232 
233 	hpckbd_input_hook(scc->scc_hpckbd);
234 
235 	/* clear scanlines */
236 	(void)bus_space_read_2(t, h, 0);
237 	delay(3);
238 
239 	for (i = 0; i < TC5165_COLUMN_MAX; i++) {
240 		rpat = bus_space_read_2(t, h, 2 << i);
241 		delay(3);
242 		(void)bus_space_read_2(t, h, 0);
243 		delay(3);
244 		if ((edge = (rpat ^ scc->scc_buf[i]))) {
245 			scc->scc_buf[i] = rpat;
246 			for (j = 0, mask = 1; j < TC5165_ROW_MAX;
247 			    j++, mask <<= 1) {
248 				if (mask & edge) {
249 					type = mask & rpat ? 1 : 0;
250 					val = j * TC5165_COLUMN_MAX + i;
251 					DPRINTF(("%d %d\n", j, i));
252 					hpckbd_input(scc->scc_hpckbd,
253 					    type, val);
254 				}
255 			}
256 		}
257 	}
258 
259 	s = spltty();
260 	scc->scc_queued = 0;
261 	splx(s);
262 }
263