xref: /netbsd-src/sys/arch/hpcsh/dev/pfckbd.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: pfckbd.c,v 1.12 2003/12/14 04:03:16 uwe 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 <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: pfckbd.c,v 1.12 2003/12/14 04:03:16 uwe Exp $");
41 
42 #include "debug_hpcsh.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47 #include <sys/callout.h>
48 
49 #include <machine/bus.h>
50 #include <machine/platid.h>
51 #include <machine/platid_mask.h>
52 
53 #include <dev/hpc/hpckbdvar.h>
54 
55 #include <sh3/pfcreg.h>
56 
57 #include <hpcsh/dev/pfckbdvar.h>
58 
59 #ifdef	PFCKBD_DEBUG
60 #define DPRINTF_ENABLE
61 #define DPRINTF_DEBUG	pfckbd_debug
62 #endif
63 #include <machine/debug.h>
64 
65 STATIC int pfckbd_match(struct device *, struct cfdata *, void *);
66 STATIC void pfckbd_attach(struct device *, struct device *, void *);
67 STATIC void (*pfckbd_callout_lookup(void))(void *);
68 STATIC void pfckbd_callout_unknown(void *);
69 STATIC void pfckbd_callout_hp(void *);
70 STATIC void pfckbd_callout_hitachi(void *);
71 
72 STATIC struct pfckbd_core {
73 	int pc_attached;
74 	int pc_enabled;
75 	struct callout pc_soft_ch;
76 	struct hpckbd_ic_if pc_if;
77 	struct hpckbd_if *pc_hpckbd;
78 	u_int16_t pc_column[8];
79 	void (*pc_callout)(void *);
80 } pfckbd_core;
81 
82 /* callout function table. this function is platfrom specific. */
83 STATIC const struct {
84 	platid_mask_t *platform;
85 	void (*func)(void *);
86 } pfckbd_calloutfunc_table[] = {
87 	{ &platid_mask_MACH_HP		, pfckbd_callout_hp },
88 	{ &platid_mask_MACH_HITACHI	, pfckbd_callout_hitachi }
89 };
90 
91 CFATTACH_DECL(pfckbd, sizeof(struct device),
92     pfckbd_match, pfckbd_attach, NULL, NULL);
93 
94 STATIC int pfckbd_poll(void *);
95 STATIC void pfckbd_ifsetup(struct pfckbd_core *);
96 STATIC int pfckbd_input_establish(void *, struct hpckbd_if *);
97 STATIC void pfckbd_input(struct hpckbd_if *, u_int16_t *, u_int16_t, int);
98 
99 /*
100  * Matrix scan keyboard connected to SH7709, SH7709A PFC module.
101  * currently, HP Jornada 680/690, HITACHI PERSONA HPW-50PAD only.
102  */
103 void
104 pfckbd_cnattach()
105 {
106 	struct pfckbd_core *pc = &pfckbd_core;
107 
108 	if ((cpu_product != CPU_PRODUCT_7709) &&
109 	    (cpu_product != CPU_PRODUCT_7709A))
110 		return;
111 
112 	/* initialize interface */
113 	pfckbd_ifsetup(pc);
114 
115 	/* attach console */
116 	hpckbd_cnattach(&pc->pc_if);
117 }
118 
119 int
120 pfckbd_match(struct device *parent, struct cfdata *cf, void *aux)
121 {
122 
123 	if ((cpu_product != CPU_PRODUCT_7709) &&
124 	    (cpu_product != CPU_PRODUCT_7709A))
125 		return (0);
126 
127 	return (!pfckbd_core.pc_attached);
128 }
129 
130 void
131 pfckbd_attach(struct device *parent, struct device *self, void *aux)
132 {
133 	struct hpckbd_attach_args haa;
134 
135 	printf("\n");
136 
137 	/* pfckbd is singleton. no more attach */
138 	pfckbd_core.pc_attached = 1;
139 	pfckbd_ifsetup(&pfckbd_core);
140 
141 	/* attach hpckbd */
142 	haa.haa_ic = &pfckbd_core.pc_if; /* tell the hpckbd to my interface */
143 	config_found(self, &haa, hpckbd_print);
144 
145 	/* install callout handler */
146 	callout_init(&pfckbd_core.pc_soft_ch);
147 	callout_reset(&pfckbd_core.pc_soft_ch, 1, pfckbd_core.pc_callout,
148 	    &pfckbd_core);
149 }
150 
151 int
152 pfckbd_input_establish(void *ic, struct hpckbd_if *kbdif)
153 {
154 	struct pfckbd_core *pc = ic;
155 
156 	/* save hpckbd interface */
157 	pc->pc_hpckbd = kbdif;
158 	/* ok to transact hpckbd */
159 	pc->pc_enabled = 1;
160 
161 	return 0;
162 }
163 
164 int
165 pfckbd_poll(void *arg)
166 {
167 	struct pfckbd_core *pc = arg;
168 
169 	if (pc->pc_enabled)
170 		(*pc->pc_callout)(arg);
171 
172 	return 0;
173 }
174 
175 void
176 pfckbd_ifsetup(struct pfckbd_core *pc)
177 {
178 	int i;
179 
180 	pc->pc_if.hii_ctx = pc;
181 	pc->pc_if.hii_establish	= pfckbd_input_establish;
182 	pc->pc_if.hii_poll = pfckbd_poll;
183 	for (i = 0; i < 8; i++)
184 		pc->pc_column[i] = 0xdfff;
185 
186 	/* select PFC access method */
187 	pc->pc_callout = pfckbd_callout_lookup();
188 }
189 
190 void
191 pfckbd_input(struct hpckbd_if *hpckbd, u_int16_t *buf, u_int16_t data,
192     int column)
193 {
194 	int row, type, val;
195 	u_int16_t edge, mask;
196 
197 	if ((edge = (data ^ buf[column]))) {
198 		buf[column] = data;
199 
200 		for (row = 0, mask = 1; row < 16; row++, mask <<= 1) {
201 			if (mask & edge) {
202 				type = mask & data ? 0 : 1;
203 				val = row * 8 + column;
204 				DPRINTF("(%2d, %2d) %d \n",
205 				    row, column, type);
206 				hpckbd_input(hpckbd, type, val);
207 			}
208 		}
209 	}
210 }
211 
212 /*
213  * Platform dependent routines.
214  */
215 
216 /* Look up appropriate callback handler */
217 void (*pfckbd_callout_lookup())(void *)
218 {
219 	int i, n = sizeof(pfckbd_calloutfunc_table) /
220 	    sizeof(pfckbd_calloutfunc_table[0]);
221 
222 	for (i = 0; i < n; i++)
223 		if (platid_match(&platid,
224 		    pfckbd_calloutfunc_table[i].platform))
225 			return (pfckbd_calloutfunc_table[i].func);
226 
227 	return (pfckbd_callout_unknown);
228 }
229 
230 /* Placeholder for unknown platform */
231 void
232 pfckbd_callout_unknown(void *arg)
233 {
234 
235 	printf("%s: unknown keyboard switch\n", __FUNCTION__);
236 }
237 
238 /* HP Jornada680/690, HP620LX */
239 void
240 pfckbd_callout_hp(void *arg)
241 {
242 #define PFCKBD_HP_PDCR_MASK 0xcc0c
243 #define PFCKBD_HP_PECR_MASK 0xf0cf
244 
245 	/*
246 	 * Disable output on all lines but the n'th line in D.
247 	 * Pull the n'th scan line in D low.
248 	 */
249 #define PD(n)								\
250 	{ (u_int16_t)(PFCKBD_HP_PDCR_MASK & (~(1 << (2*(n)+1)))),	\
251 	  (u_int16_t)(PFCKBD_HP_PECR_MASK & 0xffff),			\
252 	  (u_int8_t)~(1 << (n)),					\
253 	  0xff }
254 
255 	/* Ditto for E */
256 #define PE(n)								\
257 	{ (u_int16_t)(PFCKBD_HP_PDCR_MASK & 0xffff),			\
258 	  (u_int16_t)(PFCKBD_HP_PECR_MASK & (~(1 << (2*(n)+1)))),	\
259 	  0xff,								\
260 	  (u_int8_t)~(1 << (n)) }
261 
262 	static const struct {
263 		u_int16_t dc, ec; u_int8_t d, e;
264 	} scan[] = {
265 		PD(1), PD(5), PE(1), PE(6), PE(7), PE(3), PE(0), PD(7)
266 	};
267 
268 #undef PD
269 #undef PE
270 
271 	struct pfckbd_core *pc = arg;
272 	u_int16_t dc, ec;
273 	int column;
274 	u_int16_t data;
275 
276 	if (!pc->pc_enabled)
277 		goto reinstall;
278 
279 	/* bits in D/E control regs we do not touch (XXX: can they change?) */
280 	dc = _reg_read_2(SH7709_PDCR) & ~PFCKBD_HP_PDCR_MASK;
281 	ec = _reg_read_2(SH7709_PECR) & ~PFCKBD_HP_PECR_MASK;
282 
283 	for (column = 0; column < 8; column++) {
284 		/* disable output to all lines except the one we scan */
285 		_reg_write_2(SH7709_PDCR, dc | scan[column].dc);
286 		_reg_write_2(SH7709_PECR, ec | scan[column].ec);
287 		delay(5);
288 
289 		/* pull the scan line low */
290 		_reg_write_1(SH7709_PDDR, scan[column].d);
291 		_reg_write_1(SH7709_PEDR, scan[column].e);
292 		delay(50);
293 
294 		/* read sense */
295 		data = _reg_read_1(SH7709_PFDR) |
296 		    (_reg_read_1(SH7709_PCDR) << 8);
297 
298 		pfckbd_input(pc->pc_hpckbd, pc->pc_column, data, column);
299 	}
300 
301 	/* scan no lines */
302 	_reg_write_1(SH7709_PDDR, 0xff);
303 	_reg_write_1(SH7709_PEDR, 0xff);
304 
305 	/* enable all scan lines */
306 	_reg_write_2(SH7709_PDCR, dc | (0x5555 & PFCKBD_HP_PDCR_MASK));
307 	_reg_write_2(SH7709_PECR, ec | (0x5555 & PFCKBD_HP_PECR_MASK));
308 
309 #if 0
310 	/* (ignore) extra keys/events (recorder buttons, lid, cable &c) */
311 	data = _reg_read_1(SH7709_PGDR) | (_reg_read_1(SH7709_PHDR) << 8);
312 #endif
313 
314  reinstall:
315 	callout_schedule(&pc->pc_soft_ch, 1);
316 }
317 
318 /* HITACH PERSONA (HPW-50PAD) */
319 void
320 pfckbd_callout_hitachi(void *arg)
321 {
322 	static const struct {
323 		u_int8_t d, e;
324 	} scan[] = {
325 		{ 0xf5, 0xff },
326 		{ 0xd7, 0xff },
327 		{ 0xf7, 0xfd },
328 		{ 0xf7, 0xbf },
329 		{ 0xf7, 0x7f },
330 		{ 0xf7, 0xf7 },
331 		{ 0xf7, 0xfe },
332 		{ 0x77, 0xff },
333 	};
334 	struct pfckbd_core *pc = arg;
335 	u_int16_t data;
336 	int column;
337 
338 	if (!pc->pc_enabled)
339 		goto reinstall;
340 
341 	for (column = 0; column < 8; column++) {
342 		_reg_write_1(SH7709_PCDR, ~(1 << column));
343 		delay(50);
344 		data = ((_reg_read_1(SH7709_PFDR) & 0xfe) |
345 		    (_reg_read_1(SH7709_PCDR) & 0x01)) << 8;
346 		_reg_write_1(SH7709_PCDR, 0xff);
347 		_reg_write_1(SH7709_PDDR, scan[column].d);
348 		_reg_write_1(SH7709_PEDR, scan[column].e);
349 		delay(50);
350 		data |= (_reg_read_1(SH7709_PFDR) & 0xfe) |
351 		    (_reg_read_1(SH7709_PCDR) & 0x01);
352 		_reg_write_1(SH7709_PDDR, 0xf7);
353 		_reg_write_1(SH7709_PEDR, 0xff);
354 
355 		pfckbd_input(pc->pc_hpckbd, pc->pc_column, data, column);
356 	}
357 
358 	_reg_write_1(SH7709_PDDR, 0xf7);
359 	_reg_write_1(SH7709_PEDR, 0xff);
360 	data = _reg_read_1(SH7709_PGDR) | (_reg_read_1(SH7709_PHDR) << 8);
361 
362  reinstall:
363 	callout_schedule(&pc->pc_soft_ch, 1);
364 }
365