xref: /netbsd-src/sys/arch/hpcsh/dev/pfckbd.c (revision 27527e67bbdf8d9ec84fd58803048ed6d181ece2)
1 /*	$NetBSD: pfckbd.c,v 1.18 2006/01/21 23:16:57 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.18 2006/01/21 23:16:57 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 	uint16_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 *, uint16_t *, uint16_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, uint16_t *buf, uint16_t data,
192     int column)
193 {
194 	int row, type, val;
195 	unsigned int edge, mask;
196 
197 	edge = data ^ buf[column];
198 	if (edge) {
199 		buf[column] = data;
200 
201 		for (row = 0, mask = 1; row < 16; row++, mask <<= 1) {
202 			if (mask & edge) {
203 				type = mask & data ? 0 : 1;
204 				val = row * 8 + column;
205 				DPRINTF("(%2d, %2d) %d \n",
206 				    row, column, type);
207 				hpckbd_input(hpckbd, type, val);
208 			}
209 		}
210 	}
211 }
212 
213 /*
214  * Platform dependent routines.
215  */
216 
217 /* Look up appropriate callback handler */
218 void (*pfckbd_callout_lookup())(void *)
219 {
220 	int i, n = sizeof(pfckbd_calloutfunc_table) /
221 	    sizeof(pfckbd_calloutfunc_table[0]);
222 
223 	for (i = 0; i < n; i++)
224 		if (platid_match(&platid,
225 		    pfckbd_calloutfunc_table[i].platform))
226 			return (pfckbd_calloutfunc_table[i].func);
227 
228 	return (pfckbd_callout_unknown);
229 }
230 
231 /* Placeholder for unknown platform */
232 void
233 pfckbd_callout_unknown(void *arg)
234 {
235 
236 	printf("%s: unknown keyboard switch\n", __func__);
237 }
238 
239 /* HP Jornada680/690, HP620LX */
240 void
241 pfckbd_callout_hp(void *arg)
242 {
243 #define PFCKBD_HP_PDCR_MASK 0xcc0c
244 #define PFCKBD_HP_PECR_MASK 0xf0cf
245 
246 	/*
247 	 * Disable output on all lines but the n'th line in D.
248 	 * Pull the n'th scan line in D low.
249 	 */
250 #define PD(n)								\
251 	{ (uint16_t)(PFCKBD_HP_PDCR_MASK & (~(1 << (2*(n)+1)))),	\
252 	  (uint16_t)(PFCKBD_HP_PECR_MASK & 0xffff),			\
253 	  (uint8_t)~(1 << (n)),						\
254 	  0xff }
255 
256 	/* Ditto for E */
257 #define PE(n)								\
258 	{ (uint16_t)(PFCKBD_HP_PDCR_MASK & 0xffff),			\
259 	  (uint16_t)(PFCKBD_HP_PECR_MASK & (~(1 << (2*(n)+1)))),	\
260 	  0xff,								\
261 	  (uint8_t)~(1 << (n)) }
262 
263 	static const struct {
264 		uint16_t dc, ec; uint8_t d, e;
265 	} scan[] = {
266 		PD(1), PD(5), PE(1), PE(6), PE(7), PE(3), PE(0), PD(7)
267 	};
268 
269 #undef PD
270 #undef PE
271 
272 	struct pfckbd_core *pc = arg;
273 	uint16_t dc, ec;
274 	int column;
275 	uint16_t data;
276 
277 	if (!pc->pc_enabled)
278 		goto reinstall;
279 
280 	/* bits in D/E control regs we do not touch (XXX: can they change?) */
281 	dc = _reg_read_2(SH7709_PDCR) & ~PFCKBD_HP_PDCR_MASK;
282 	ec = _reg_read_2(SH7709_PECR) & ~PFCKBD_HP_PECR_MASK;
283 
284 	for (column = 0; column < 8; column++) {
285 		/* disable output to all lines except the one we scan */
286 		_reg_write_2(SH7709_PDCR, dc | scan[column].dc);
287 		_reg_write_2(SH7709_PECR, ec | scan[column].ec);
288 		delay(5);
289 
290 		/* pull the scan line low */
291 		_reg_write_1(SH7709_PDDR, scan[column].d);
292 		_reg_write_1(SH7709_PEDR, scan[column].e);
293 		delay(50);
294 
295 		/* read sense */
296 		data = _reg_read_1(SH7709_PFDR) |
297 		    (_reg_read_1(SH7709_PCDR) << 8);
298 
299 		pfckbd_input(pc->pc_hpckbd, pc->pc_column, data, column);
300 	}
301 
302 	/* scan no lines */
303 	_reg_write_1(SH7709_PDDR, 0xff);
304 	_reg_write_1(SH7709_PEDR, 0xff);
305 
306 	/* enable all scan lines */
307 	_reg_write_2(SH7709_PDCR, dc | (0x5555 & PFCKBD_HP_PDCR_MASK));
308 	_reg_write_2(SH7709_PECR, ec | (0x5555 & PFCKBD_HP_PECR_MASK));
309 
310 #if 0
311 	/* (ignore) extra keys/events (recorder buttons, lid, cable &c) */
312 	data = _reg_read_1(SH7709_PGDR) | (_reg_read_1(SH7709_PHDR) << 8);
313 #endif
314 
315  reinstall:
316 	callout_schedule(&pc->pc_soft_ch, 1);
317 }
318 
319 /* HITACH PERSONA (HPW-50PAD) */
320 void
321 pfckbd_callout_hitachi(void *arg)
322 {
323 #define PFCKBD_HITACHI_PCCR_MASK 0xfff3
324 #define PFCKBD_HITACHI_PDCR_MASK 0x000c
325 #define PFCKBD_HITACHI_PECR_MASK 0x30cf
326 
327 #define PFCKBD_HITACHI_PCDR_SCN_MASK 0xfd
328 #define PFCKBD_HITACHI_PDDR_SCN_MASK 0xf7
329 #define PFCKBD_HITACHI_PEDR_SCN_MASK 0xff
330 
331 #define PFCKBD_HITACHI_PCDR_SNS_MASK 0x01
332 #define PFCKBD_HITACHI_PFDR_SNS_MASK 0xfe
333 
334 	/*
335 	 * Disable output on all lines but the n'th line in C.
336 	 * Pull the n'th scan line in C low.
337 	 */
338 #define PC(n)								\
339 	{ (uint16_t)(PFCKBD_HITACHI_PCCR_MASK & (~(1 << (2*(n)+1)))),	\
340 	  (uint16_t)(PFCKBD_HITACHI_PDCR_MASK & 0xffff),		\
341 	  (uint16_t)(PFCKBD_HITACHI_PECR_MASK & 0xffff),		\
342 	  (uint8_t)(PFCKBD_HITACHI_PCDR_SCN_MASK & ~(1 << (n))),	\
343 	  PFCKBD_HITACHI_PDDR_SCN_MASK,					\
344 	  PFCKBD_HITACHI_PEDR_SCN_MASK }
345 
346 	/* Ditto for D */
347 #define PD(n)								\
348 	{ (uint16_t)(PFCKBD_HITACHI_PCCR_MASK & 0xffff),		\
349 	  (uint16_t)(PFCKBD_HITACHI_PDCR_MASK & (~(1 << (2*(n)+1)))),	\
350 	  (uint16_t)(PFCKBD_HITACHI_PECR_MASK & 0xffff),		\
351 	  PFCKBD_HITACHI_PCDR_SCN_MASK,					\
352 	  (uint8_t)(PFCKBD_HITACHI_PDDR_SCN_MASK & ~(1 << (n))),	\
353 	  PFCKBD_HITACHI_PEDR_SCN_MASK }
354 
355 	/* Ditto for E */
356 #define PE(n)								\
357 	{ (uint16_t)(PFCKBD_HITACHI_PCCR_MASK & 0xffff),		\
358 	  (uint16_t)(PFCKBD_HITACHI_PDCR_MASK & 0xffff),		\
359 	  (uint16_t)(PFCKBD_HITACHI_PECR_MASK & (~(1 << (2*(n)+1)))),	\
360 	  PFCKBD_HITACHI_PCDR_SCN_MASK,					\
361 	  PFCKBD_HITACHI_PDDR_SCN_MASK,					\
362 	  (uint8_t)(PFCKBD_HITACHI_PEDR_SCN_MASK & ~(1 << (n))) }
363 
364 	static const struct {
365 		uint16_t cc, dc, ec; uint8_t c, d, e;
366 	} scan[] = {
367 		PE(6), PE(3), PE(1), PE(0), PC(7), PC(6), PC(5), PC(4),
368 		PC(3), PC(2), PD(1), PC(0)
369 	};
370 
371 #undef PC
372 #undef PD
373 #undef PE
374 
375 	struct pfckbd_core *pc = arg;
376 	uint16_t cc, dc, ec;
377 	uint8_t data[2], cd, dd, ed;
378 	int i;
379 
380 	if (!pc->pc_enabled)
381 		goto reinstall;
382 
383 	/* bits in C/D/E control regs we do not touch (XXX: can they change?) */
384 	cc = _reg_read_2(SH7709_PCCR) & ~PFCKBD_HITACHI_PCCR_MASK;
385 	dc = _reg_read_2(SH7709_PDCR) & ~PFCKBD_HITACHI_PDCR_MASK;
386 	ec = _reg_read_2(SH7709_PECR) & ~PFCKBD_HITACHI_PECR_MASK;
387 
388 	cd = _reg_read_1(SH7709_PCDR);
389 	dd = _reg_read_1(SH7709_PDDR);
390 	ed = _reg_read_1(SH7709_PEDR);
391 
392 	for (i = 0; i < 12; i++) {
393 		/* disable output to all lines except the one we scan */
394 		_reg_write_2(SH7709_PCCR, cc | scan[i].cc);
395 		_reg_write_2(SH7709_PDCR, dc | scan[i].dc);
396 		_reg_write_2(SH7709_PECR, ec | scan[i].ec);
397 		delay(5);
398 
399 		/* pull the scan line low */
400 		_reg_write_1(SH7709_PCDR, scan[i].c);
401 		_reg_write_1(SH7709_PDDR, scan[i].d);
402 		_reg_write_1(SH7709_PEDR, scan[i].e);
403 		delay(50);
404 
405 		/* read sense */
406 		data[i & 0x1] =
407 		    ((_reg_read_1(SH7709_PCDR) & PFCKBD_HITACHI_PCDR_SNS_MASK) |
408 		    (_reg_read_1(SH7709_PFDR) & PFCKBD_HITACHI_PFDR_SNS_MASK));
409 
410 		if (i & 0x1)
411 			pfckbd_input(pc->pc_hpckbd, pc->pc_column,
412 				     (data[0] | (data[1] << 8)), (i >> 1));
413 	}
414 
415 	/* scan no lines */
416 	_reg_write_1(SH7709_PCDR, cd);
417 	_reg_write_1(SH7709_PDDR, dd);
418 	_reg_write_1(SH7709_PEDR, ed);
419 
420 	/* enable all scan lines */
421 	_reg_write_2(SH7709_PCCR, cc | (0x5555 & PFCKBD_HITACHI_PCCR_MASK));
422 	_reg_write_2(SH7709_PDCR, dc | (0x5555 & PFCKBD_HITACHI_PDCR_MASK));
423 	_reg_write_2(SH7709_PECR, ec | (0x5555 & PFCKBD_HITACHI_PECR_MASK));
424 
425  reinstall:
426 	callout_schedule(&pc->pc_soft_ch, 1);
427 }
428