xref: /netbsd-src/sys/arch/hpcsh/dev/pfckbd.c (revision 9fbd88883c38d0c0fbfcbe66d76fe6b0fab3f9de)
1 /*	$NetBSD: pfckbd.c,v 1.5 2002/01/29 18:53:23 uch Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2002 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 #include "debug_hpcsh.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/callout.h>
45 
46 #include <machine/bus.h>
47 #include <machine/platid.h>
48 #include <machine/platid_mask.h>
49 
50 #include <dev/hpc/hpckbdvar.h>
51 
52 #include <sh3/pfcreg.h>
53 
54 #include <hpcsh/dev/pfckbdvar.h>
55 
56 #ifdef	PFCKBD_DEBUG
57 #define DPRINTF_ENABLE
58 #define DPRINTF_DEBUG	pfckbd_debug
59 #endif
60 #include <machine/debug.h>
61 
62 STATIC int pfckbd_match(struct device *, struct cfdata *, void *);
63 STATIC void pfckbd_attach(struct device *, struct device *, void *);
64 STATIC void (*pfckbd_callout_lookup(void))(void *);
65 STATIC void pfckbd_callout_unknown(void *);
66 STATIC void pfckbd_callout_hp(void *);
67 STATIC void pfckbd_callout_hitachi(void *);
68 
69 STATIC struct pfckbd_core {
70 	int pc_attached;
71 	int pc_enabled;
72 	struct callout pc_soft_ch;
73 	struct hpckbd_ic_if pc_if;
74 	struct hpckbd_if *pc_hpckbd;
75 	u_int16_t pc_column[8];
76 	void (*pc_callout)(void *);
77 } pfckbd_core;
78 
79 /* callout function table. this function is platfrom specific. */
80 STATIC const struct {
81 	platid_mask_t *platform;
82 	void (*func)(void *);
83 } pfckbd_calloutfunc_table[] = {
84 	{ &platid_mask_MACH_HP		, pfckbd_callout_hp },
85 	{ &platid_mask_MACH_HITACHI	, pfckbd_callout_hitachi }
86 };
87 
88 struct cfattach pfckbd_ca = {
89 	sizeof(struct device), pfckbd_match, pfckbd_attach
90 };
91 
92 STATIC int pfckbd_poll(void *);
93 STATIC void pfckbd_ifsetup(struct pfckbd_core *);
94 STATIC int pfckbd_input_establish(void *, struct hpckbd_if *);
95 STATIC void pfckbd_input(struct hpckbd_if *, u_int16_t *, u_int16_t, int);
96 
97 /*
98  * matrix scan keyboard connected to SH3 PFC module.
99  * currently, HP Jornada 680/690, HITACHI PERSONA HPW-50PAD only.
100  */
101 void
102 pfckbd_cnattach()
103 {
104 	struct pfckbd_core *pc = &pfckbd_core;
105 
106 	/* initialize interface */
107 	pfckbd_ifsetup(pc);
108 
109 	/* attach console */
110 	hpckbd_cnattach(&pc->pc_if);
111 }
112 
113 int
114 pfckbd_match(struct device *parent, struct cfdata *cf, void *aux)
115 {
116 
117 	return (!pfckbd_core.pc_attached);
118 }
119 
120 void
121 pfckbd_attach(struct device *parent, struct device *self, void *aux)
122 {
123 	struct hpckbd_attach_args haa;
124 
125 	printf("\n");
126 
127 	/* pfckbd is singleton. no more attach */
128 	pfckbd_core.pc_attached = 1;
129 	pfckbd_ifsetup(&pfckbd_core);
130 
131 	/* attach hpckbd */
132 	haa.haa_ic = &pfckbd_core.pc_if; /* tell the hpckbd to my interface */
133 	config_found(self, &haa, hpckbd_print);
134 
135 	/* install callout handler */
136 	callout_init(&pfckbd_core.pc_soft_ch);
137 	callout_reset(&pfckbd_core.pc_soft_ch, 1, pfckbd_core.pc_callout,
138 	    &pfckbd_core);
139 }
140 
141 int
142 pfckbd_input_establish(void *ic, struct hpckbd_if *kbdif)
143 {
144 	struct pfckbd_core *pc = ic;
145 
146 	/* save hpckbd interface */
147 	pc->pc_hpckbd = kbdif;
148 	/* ok to transact hpckbd */
149 	pc->pc_enabled = 1;
150 
151 	return 0;
152 }
153 
154 int
155 pfckbd_poll(void *arg)
156 {
157 	struct pfckbd_core *pc = arg;
158 
159 	if (pc->pc_enabled)
160 		(*pc->pc_callout)(arg);
161 
162 	return 0;
163 }
164 
165 void
166 pfckbd_ifsetup(struct pfckbd_core *pc)
167 {
168 	int i;
169 
170 	pc->pc_if.hii_ctx = pc;
171 	pc->pc_if.hii_establish	= pfckbd_input_establish;
172 	pc->pc_if.hii_poll = pfckbd_poll;
173 	for (i = 0; i < 8; i++)
174 		pc->pc_column[i] = 0xdfff;
175 
176 	/* select PFC access method */
177 	pc->pc_callout = pfckbd_callout_lookup();
178 }
179 
180 void
181 pfckbd_input(struct hpckbd_if *hpckbd, u_int16_t *buf, u_int16_t data,
182     int column)
183 {
184 	int row, type, val;
185 	u_int16_t edge, mask;
186 
187 	if ((edge = (data ^ buf[column]))) {
188 		buf[column] = data;
189 
190 		for (row = 0, mask = 1; row < 16; row++, mask <<= 1) {
191 			if (mask & edge) {
192 				type = mask & data ? 0 : 1;
193 				val = row * 8 + column;
194 				DPRINTF("(%2d, %2d) %d \n",
195 				    row, column, type);
196 				hpckbd_input(hpckbd, type, val);
197 			}
198 		}
199 	}
200 }
201 
202 /*
203  * Platform dependent routines.
204  */
205 
206 /* Look up appropriate callback handler */
207 void (*pfckbd_callout_lookup())(void *)
208 {
209 	int i, n = sizeof(pfckbd_calloutfunc_table) /
210 	    sizeof(pfckbd_calloutfunc_table[0]);
211 
212 	for (i = 0; i < n; i++)
213 		if (platid_match(&platid,
214 		    pfckbd_calloutfunc_table[i].platform))
215 			return (pfckbd_calloutfunc_table[i].func);
216 
217 	return (pfckbd_callout_unknown);
218 }
219 
220 /* Placeholder for unknown platform */
221 void
222 pfckbd_callout_unknown(void *arg)
223 {
224 
225 	printf("%s: unknown keyboard switch\n", __FUNCTION__);
226 }
227 
228 /* HP Jornada680/690, HP620LX */
229 void
230 pfckbd_callout_hp(void *arg)
231 {
232 	static const struct {
233 		u_int8_t d, e;
234 	} scan[] = {
235 		{ 0xfd, 0xff },
236 		{ 0xdf, 0xff },
237 		{ 0xff, 0xfd },
238 		{ 0xff, 0xbf },
239 		{ 0xff, 0x7f },
240 		{ 0xff, 0xf7 },
241 		{ 0xff, 0xfe },
242 		{ 0x7f, 0xff },
243 	};
244 	struct pfckbd_core *pc = arg;
245 	int column;
246 	u_int16_t data;
247 
248 	if (!pc->pc_enabled)
249 		goto reinstall;
250 
251 	for (column = 0; column < 8; column++) {
252 		SHREG_PDDR = scan[column].d;
253 		SHREG_PEDR = scan[column].e;
254 		delay(50);
255 		data = SHREG_PFDR | (SHREG_PCDR << 8);
256 
257 		pfckbd_input(pc->pc_hpckbd, pc->pc_column, data, column);
258 	}
259 
260 	SHREG_PDDR = 0xff;
261 	SHREG_PEDR = 0xff;
262 	data = SHREG_PGDR | (SHREG_PHDR << 8);
263 
264  reinstall:
265 	callout_reset(&pc->pc_soft_ch, 1, pfckbd_callout_hp, pc);
266 }
267 
268 /* HITACH PERSONA (HPW-50PAD) */
269 void
270 pfckbd_callout_hitachi(void *arg)
271 {
272 	static const struct {
273 		u_int8_t d, e;
274 	} scan[] = {
275 		{ 0xf5, 0xff },
276 		{ 0xd7, 0xff },
277 		{ 0xf7, 0xfd },
278 		{ 0xf7, 0xbf },
279 		{ 0xf7, 0x7f },
280 		{ 0xf7, 0xf7 },
281 		{ 0xf7, 0xfe },
282 		{ 0x77, 0xff },
283 	};
284 	struct pfckbd_core *pc = arg;
285 	u_int16_t data;
286 	int column;
287 
288 	if (!pc->pc_enabled)
289 		goto reinstall;
290 
291 	for (column = 0; column < 8; column++) {
292 		SHREG_PCDR = ~(1 << column);
293 		delay(50);
294 		data = ((SHREG_PFDR & 0xfe) | (SHREG_PCDR & 0x01)) << 8;
295 		SHREG_PCDR = 0xff;
296 		SHREG_PDDR = scan[column].d;
297 		SHREG_PEDR = scan[column].e;
298 		delay(50);
299 		data |= (SHREG_PFDR & 0xfe) | (SHREG_PCDR & 0x01);
300 		SHREG_PDDR = 0xf7;
301 		SHREG_PEDR = 0xff;
302 
303 		pfckbd_input(pc->pc_hpckbd, pc->pc_column, data, column);
304 	}
305 
306 	SHREG_PDDR = 0xf7;
307 	SHREG_PEDR = 0xff;
308 	data = SHREG_PGDR | (SHREG_PHDR << 8);
309 
310  reinstall:
311 	callout_reset(&pc->pc_soft_ch, 1, pfckbd_callout_hitachi, pc);
312 }
313