xref: /netbsd-src/sys/arch/hpcmips/dev/tc5165buf.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: tc5165buf.c,v 1.6 2000/09/21 14:17:29 takemura Exp $ */
2 
3 /*
4  * Copyright (c) 1999, 2000, by UCHIYAMA Yasushi
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. The name of the developer may NOT be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 /*
30  * Device driver for TOSHIBA TC5165BFTS, PHILIPS 74ALVC16241/245
31  * buffer chip
32  */
33 
34 #include "opt_tx39_debug.h"
35 #include "opt_use_poll.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/callout.h>
40 #include <sys/device.h>
41 
42 #include <machine/bus.h>
43 #include <machine/intr.h>
44 
45 #include <hpcmips/tx/tx39var.h>
46 #include <hpcmips/tx/txcsbusvar.h>
47 
48 #include <hpcmips/dev/tc5165bufvar.h>
49 #include <hpcmips/dev/hpckbdvar.h>
50 
51 #define TC5165_ROW_MAX		16
52 #define TC5165_COLUMN_MAX	8
53 
54 #ifdef TC5165DEBUG
55 #define	DPRINTF(arg) printf arg
56 #else
57 #define	DPRINTF(arg)
58 #endif
59 
60 struct tc5165buf_chip {
61 	bus_space_tag_t		scc_cst;
62 	bus_space_handle_t	scc_csh;
63 	u_int16_t		scc_buf[TC5165_COLUMN_MAX];
64 	int			scc_enabled;
65 	int			scc_queued;
66 	struct callout		scc_soft_ch;
67 	struct hpckbd_ic_if	scc_if;
68 	struct hpckbd_if	*scc_hpckbd;
69 };
70 
71 struct tc5165buf_softc {
72 	struct device		sc_dev;
73 	struct tc5165buf_chip	*sc_chip;
74 	tx_chipset_tag_t	sc_tc;
75 	void			*sc_ih;
76 };
77 
78 int	tc5165buf_match	__P((struct device*, struct cfdata*, void*));
79 void	tc5165buf_attach __P((struct device*, struct device*, void*));
80 int	tc5165buf_intr __P((void*));
81 int	tc5165buf_poll __P((void*));
82 void	tc5165buf_soft __P((void*));
83 void	tc5165buf_ifsetup __P((struct tc5165buf_chip*));
84 
85 int	tc5165buf_input_establish __P((void*, struct hpckbd_if*));
86 
87 struct tc5165buf_chip tc5165buf_chip;
88 
89 struct cfattach tc5165buf_ca = {
90 	sizeof(struct tc5165buf_softc), tc5165buf_match, tc5165buf_attach
91 };
92 
93 int
94 tc5165buf_match(parent, cf, aux)
95 	struct device *parent;
96 	struct cfdata *cf;
97 	void *aux;
98 {
99 	return 1;
100 }
101 
102 void
103 tc5165buf_attach(parent, self, aux)
104 	struct device *parent;
105 	struct device *self;
106 	void *aux;
107 {
108 	struct cs_attach_args *ca = aux;
109 	struct tc5165buf_softc *sc = (void*)self;
110 	struct hpckbd_attach_args haa;
111 
112 	printf(": ");
113 	sc->sc_tc = ca->ca_tc;
114 	sc->sc_chip = &tc5165buf_chip;
115 
116 	callout_init(&sc->sc_chip->scc_soft_ch);
117 
118 	sc->sc_chip->scc_cst = ca->ca_csio.cstag;
119 
120 	if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase,
121 			  ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) {
122 		printf("can't map i/o space\n");
123 		return;
124 	}
125 
126 	sc->sc_chip->scc_enabled = 0;
127 
128 	if (ca->ca_irq1 != -1) {
129 		sc->sc_ih = tx_intr_establish(sc->sc_tc, ca->ca_irq1,
130 					      IST_EDGE, IPL_TTY,
131 					      tc5165buf_intr, sc);
132 		printf("interrupt mode");
133 	} else {
134 		sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1, IPL_TTY,
135 						tc5165buf_intr, sc);
136 		printf("polling mode");
137 	}
138 
139 	if (!sc->sc_ih) {
140 		printf(" can't establish interrupt\n");
141 		return;
142 	}
143 
144 	printf("\n");
145 
146 	/* setup upper interface */
147 	tc5165buf_ifsetup(sc->sc_chip);
148 
149 	haa.haa_ic = &sc->sc_chip->scc_if;
150 
151 	config_found(self, &haa, hpckbd_print);
152 }
153 
154 void
155 tc5165buf_ifsetup(scc)
156 	struct tc5165buf_chip *scc;
157 {
158 	scc->scc_if.hii_ctx		= scc;
159 	scc->scc_if.hii_establish	= tc5165buf_input_establish;
160 	scc->scc_if.hii_poll		= tc5165buf_poll;
161 }
162 
163 int
164 tc5165buf_cnattach(addr)
165 	paddr_t addr;
166 {
167 	struct tc5165buf_chip *scc = &tc5165buf_chip;
168 
169 	scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr);
170 
171 	tc5165buf_ifsetup(scc);
172 
173 	hpckbd_cnattach(&scc->scc_if);
174 
175 	return 0;
176 }
177 
178 int
179 tc5165buf_input_establish(ic, kbdif)
180 	void *ic;
181 	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(arg)
195 	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(arg)
211 	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(arg)
225 	void *arg;
226 {
227 	struct tc5165buf_chip *scc = arg;
228 	bus_space_tag_t t = scc->scc_cst;
229 	bus_space_handle_t h = scc->scc_csh;
230 	u_int16_t mask, rpat, edge;
231 	int i, j, type, val;
232 	int s;
233 
234 	hpckbd_input_hook(scc->scc_hpckbd);
235 
236 	/* clear scanlines */
237 	(void)bus_space_read_2(t, h, 0);
238 	delay(3);
239 
240 	for (i = 0; i < TC5165_COLUMN_MAX; i++) {
241 		rpat = bus_space_read_2(t, h, 2 << i);
242 		delay(3);
243 		(void)bus_space_read_2(t, h, 0);
244 		delay(3);
245 		if ((edge = (rpat ^ scc->scc_buf[i]))) {
246 			scc->scc_buf[i] = rpat;
247 			for (j = 0, mask = 1; j < TC5165_ROW_MAX;
248 			     j++, mask <<= 1) {
249 				if (mask & edge) {
250 					type = mask & rpat ? 1 : 0;
251 					val = j * TC5165_COLUMN_MAX + i;
252 					DPRINTF(("%d %d\n", j, i));
253 					hpckbd_input(scc->scc_hpckbd,
254 						     type, val);
255 				}
256 			}
257 		}
258 	}
259 
260 	s = spltty();
261 	scc->scc_queued = 0;
262 	splx(s);
263 }
264