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