xref: /netbsd-src/sys/arch/hpcmips/dev/tc5165buf.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: tc5165buf.c,v 1.11 2002/10/02 05:26:47 thorpej 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_use_poll.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/callout.h>
49 #include <sys/device.h>
50 
51 #include <machine/bus.h>
52 #include <machine/intr.h>
53 
54 #include <dev/hpc/hpckbdvar.h>
55 
56 #include <hpcmips/tx/tx39var.h>
57 #include <hpcmips/tx/txcsbusvar.h>
58 #include <hpcmips/dev/tc5165bufvar.h>
59 
60 #define TC5165_ROW_MAX		16
61 #define TC5165_COLUMN_MAX	8
62 
63 #ifdef TC5165DEBUG
64 #define	DPRINTF(arg) printf arg
65 #else
66 #define	DPRINTF(arg)
67 #endif
68 
69 struct tc5165buf_chip {
70 	bus_space_tag_t		scc_cst;
71 	bus_space_handle_t	scc_csh;
72 	u_int16_t		scc_buf[TC5165_COLUMN_MAX];
73 	int			scc_enabled;
74 	int			scc_queued;
75 	struct callout		scc_soft_ch;
76 	struct hpckbd_ic_if	scc_if;
77 	struct hpckbd_if	*scc_hpckbd;
78 };
79 
80 struct tc5165buf_softc {
81 	struct device		sc_dev;
82 	struct tc5165buf_chip	*sc_chip;
83 	tx_chipset_tag_t	sc_tc;
84 	void			*sc_ih;
85 };
86 
87 int	tc5165buf_match(struct device *, struct cfdata *, void *);
88 void	tc5165buf_attach(struct device *, struct device *, void *);
89 int	tc5165buf_intr(void *);
90 int	tc5165buf_poll(void *);
91 void	tc5165buf_soft(void *);
92 void	tc5165buf_ifsetup(struct tc5165buf_chip *);
93 
94 int	tc5165buf_input_establish(void *, struct hpckbd_if *);
95 
96 struct tc5165buf_chip tc5165buf_chip;
97 
98 CFATTACH_DECL(tc5165buf, sizeof(struct tc5165buf_softc),
99     tc5165buf_match, tc5165buf_attach, NULL, NULL);
100 
101 int
102 tc5165buf_match(struct device *parent, struct cfdata *cf, void *aux)
103 {
104 
105 	return (1);
106 }
107 
108 void
109 tc5165buf_attach(struct device *parent, struct device *self, void *aux)
110 {
111 	struct cs_attach_args *ca = aux;
112 	struct tc5165buf_softc *sc = (void*)self;
113 	struct hpckbd_attach_args haa;
114 
115 	printf(": ");
116 	sc->sc_tc = ca->ca_tc;
117 	sc->sc_chip = &tc5165buf_chip;
118 
119 	callout_init(&sc->sc_chip->scc_soft_ch);
120 
121 	sc->sc_chip->scc_cst = ca->ca_csio.cstag;
122 
123 	if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase,
124 	    ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) {
125 		printf("can't map i/o space\n");
126 		return;
127 	}
128 
129 	sc->sc_chip->scc_enabled = 0;
130 
131 	if (ca->ca_irq1 != -1) {
132 		sc->sc_ih = tx_intr_establish(sc->sc_tc, ca->ca_irq1,
133 		    IST_EDGE, IPL_TTY,
134 		    tc5165buf_intr, sc);
135 		printf("interrupt mode");
136 	} else {
137 		sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1, IPL_TTY,
138 		    tc5165buf_intr, sc);
139 		printf("polling mode");
140 	}
141 
142 	if (!sc->sc_ih) {
143 		printf(" can't establish interrupt\n");
144 		return;
145 	}
146 
147 	printf("\n");
148 
149 	/* setup upper interface */
150 	tc5165buf_ifsetup(sc->sc_chip);
151 
152 	haa.haa_ic = &sc->sc_chip->scc_if;
153 
154 	config_found(self, &haa, hpckbd_print);
155 }
156 
157 void
158 tc5165buf_ifsetup(struct tc5165buf_chip *scc)
159 {
160 
161 	scc->scc_if.hii_ctx		= scc;
162 	scc->scc_if.hii_establish	= tc5165buf_input_establish;
163 	scc->scc_if.hii_poll		= tc5165buf_poll;
164 }
165 
166 int
167 tc5165buf_cnattach(paddr_t addr)
168 {
169 	struct tc5165buf_chip *scc = &tc5165buf_chip;
170 
171 	scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr);
172 
173 	tc5165buf_ifsetup(scc);
174 
175 	hpckbd_cnattach(&scc->scc_if);
176 
177 	return (0);
178 }
179 
180 int
181 tc5165buf_input_establish(void *ic, struct hpckbd_if *kbdif)
182 {
183 	struct tc5165buf_chip *scc = ic;
184 
185 	/* save hpckbd interface */
186 	scc->scc_hpckbd = kbdif;
187 
188 	scc->scc_enabled = 1;
189 
190 	return (0);
191 }
192 
193 int
194 tc5165buf_intr(void *arg)
195 {
196 	struct tc5165buf_softc *sc = arg;
197 	struct tc5165buf_chip *scc = sc->sc_chip;
198 
199 	if (!scc->scc_enabled || scc->scc_queued)
200 		return (0);
201 
202 	scc->scc_queued = 1;
203 	callout_reset(&scc->scc_soft_ch, 1, tc5165buf_soft, scc);
204 
205 	return (0);
206 }
207 
208 int
209 tc5165buf_poll(void *arg)
210 {
211 	struct tc5165buf_chip *scc = arg;
212 
213 	if (!scc->scc_enabled)
214 		return (POLL_CONT);
215 
216 	tc5165buf_soft(arg);
217 
218 	return (POLL_CONT);
219 }
220 
221 void
222 tc5165buf_soft(void *arg)
223 {
224 	struct tc5165buf_chip *scc = arg;
225 	bus_space_tag_t t = scc->scc_cst;
226 	bus_space_handle_t h = scc->scc_csh;
227 	u_int16_t mask, rpat, edge;
228 	int i, j, type, val;
229 	int s;
230 
231 	hpckbd_input_hook(scc->scc_hpckbd);
232 
233 	/* clear scanlines */
234 	(void)bus_space_read_2(t, h, 0);
235 	delay(3);
236 
237 	for (i = 0; i < TC5165_COLUMN_MAX; i++) {
238 		rpat = bus_space_read_2(t, h, 2 << i);
239 		delay(3);
240 		(void)bus_space_read_2(t, h, 0);
241 		delay(3);
242 		if ((edge = (rpat ^ scc->scc_buf[i]))) {
243 			scc->scc_buf[i] = rpat;
244 			for (j = 0, mask = 1; j < TC5165_ROW_MAX;
245 			    j++, mask <<= 1) {
246 				if (mask & edge) {
247 					type = mask & rpat ? 1 : 0;
248 					val = j * TC5165_COLUMN_MAX + i;
249 					DPRINTF(("%d %d\n", j, i));
250 					hpckbd_input(scc->scc_hpckbd,
251 					    type, val);
252 				}
253 			}
254 		}
255 	}
256 
257 	s = spltty();
258 	scc->scc_queued = 0;
259 	splx(s);
260 }
261