pfckbd.c revision 1.12 1 1.12 uwe /* $NetBSD: pfckbd.c,v 1.12 2003/12/14 04:03:16 uwe Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.4 uch * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uch * by UCHIYAMA Yasushi.
9 1.1 uch *
10 1.1 uch * Redistribution and use in source and binary forms, with or without
11 1.1 uch * modification, are permitted provided that the following conditions
12 1.1 uch * are met:
13 1.1 uch * 1. Redistributions of source code must retain the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer.
15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uch * notice, this list of conditions and the following disclaimer in the
17 1.1 uch * documentation and/or other materials provided with the distribution.
18 1.1 uch * 3. All advertising materials mentioning features or use of this software
19 1.1 uch * must display the following acknowledgement:
20 1.1 uch * This product includes software developed by the NetBSD
21 1.1 uch * Foundation, Inc. and its contributors.
22 1.1 uch * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 uch * contributors may be used to endorse or promote products derived
24 1.1 uch * from this software without specific prior written permission.
25 1.1 uch *
26 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
37 1.1 uch */
38 1.11 lukem
39 1.11 lukem #include <sys/cdefs.h>
40 1.12 uwe __KERNEL_RCSID(0, "$NetBSD: pfckbd.c,v 1.12 2003/12/14 04:03:16 uwe Exp $");
41 1.4 uch
42 1.4 uch #include "debug_hpcsh.h"
43 1.1 uch
44 1.1 uch #include <sys/param.h>
45 1.1 uch #include <sys/systm.h>
46 1.1 uch #include <sys/device.h>
47 1.1 uch #include <sys/callout.h>
48 1.1 uch
49 1.1 uch #include <machine/bus.h>
50 1.3 uch #include <machine/platid.h>
51 1.3 uch #include <machine/platid_mask.h>
52 1.3 uch
53 1.1 uch #include <dev/hpc/hpckbdvar.h>
54 1.1 uch
55 1.1 uch #include <sh3/pfcreg.h>
56 1.1 uch
57 1.1 uch #include <hpcsh/dev/pfckbdvar.h>
58 1.1 uch
59 1.4 uch #ifdef PFCKBD_DEBUG
60 1.4 uch #define DPRINTF_ENABLE
61 1.4 uch #define DPRINTF_DEBUG pfckbd_debug
62 1.1 uch #endif
63 1.5 uch #include <machine/debug.h>
64 1.1 uch
65 1.3 uch STATIC int pfckbd_match(struct device *, struct cfdata *, void *);
66 1.3 uch STATIC void pfckbd_attach(struct device *, struct device *, void *);
67 1.3 uch STATIC void (*pfckbd_callout_lookup(void))(void *);
68 1.3 uch STATIC void pfckbd_callout_unknown(void *);
69 1.3 uch STATIC void pfckbd_callout_hp(void *);
70 1.3 uch STATIC void pfckbd_callout_hitachi(void *);
71 1.1 uch
72 1.3 uch STATIC struct pfckbd_core {
73 1.1 uch int pc_attached;
74 1.1 uch int pc_enabled;
75 1.1 uch struct callout pc_soft_ch;
76 1.1 uch struct hpckbd_ic_if pc_if;
77 1.1 uch struct hpckbd_if *pc_hpckbd;
78 1.1 uch u_int16_t pc_column[8];
79 1.3 uch void (*pc_callout)(void *);
80 1.1 uch } pfckbd_core;
81 1.1 uch
82 1.3 uch /* callout function table. this function is platfrom specific. */
83 1.3 uch STATIC const struct {
84 1.3 uch platid_mask_t *platform;
85 1.3 uch void (*func)(void *);
86 1.3 uch } pfckbd_calloutfunc_table[] = {
87 1.3 uch { &platid_mask_MACH_HP , pfckbd_callout_hp },
88 1.3 uch { &platid_mask_MACH_HITACHI , pfckbd_callout_hitachi }
89 1.1 uch };
90 1.1 uch
91 1.9 thorpej CFATTACH_DECL(pfckbd, sizeof(struct device),
92 1.9 thorpej pfckbd_match, pfckbd_attach, NULL, NULL);
93 1.1 uch
94 1.3 uch STATIC int pfckbd_poll(void *);
95 1.3 uch STATIC void pfckbd_ifsetup(struct pfckbd_core *);
96 1.3 uch STATIC int pfckbd_input_establish(void *, struct hpckbd_if *);
97 1.3 uch STATIC void pfckbd_input(struct hpckbd_if *, u_int16_t *, u_int16_t, int);
98 1.1 uch
99 1.1 uch /*
100 1.7 uch * Matrix scan keyboard connected to SH7709, SH7709A PFC module.
101 1.3 uch * currently, HP Jornada 680/690, HITACHI PERSONA HPW-50PAD only.
102 1.1 uch */
103 1.1 uch void
104 1.1 uch pfckbd_cnattach()
105 1.1 uch {
106 1.1 uch struct pfckbd_core *pc = &pfckbd_core;
107 1.1 uch
108 1.7 uch if ((cpu_product != CPU_PRODUCT_7709) &&
109 1.7 uch (cpu_product != CPU_PRODUCT_7709A))
110 1.7 uch return;
111 1.7 uch
112 1.1 uch /* initialize interface */
113 1.1 uch pfckbd_ifsetup(pc);
114 1.3 uch
115 1.3 uch /* attach console */
116 1.1 uch hpckbd_cnattach(&pc->pc_if);
117 1.1 uch }
118 1.1 uch
119 1.3 uch int
120 1.1 uch pfckbd_match(struct device *parent, struct cfdata *cf, void *aux)
121 1.1 uch {
122 1.1 uch
123 1.7 uch if ((cpu_product != CPU_PRODUCT_7709) &&
124 1.7 uch (cpu_product != CPU_PRODUCT_7709A))
125 1.7 uch return (0);
126 1.7 uch
127 1.3 uch return (!pfckbd_core.pc_attached);
128 1.1 uch }
129 1.1 uch
130 1.3 uch void
131 1.1 uch pfckbd_attach(struct device *parent, struct device *self, void *aux)
132 1.1 uch {
133 1.1 uch struct hpckbd_attach_args haa;
134 1.1 uch
135 1.1 uch printf("\n");
136 1.1 uch
137 1.1 uch /* pfckbd is singleton. no more attach */
138 1.1 uch pfckbd_core.pc_attached = 1;
139 1.1 uch pfckbd_ifsetup(&pfckbd_core);
140 1.1 uch
141 1.1 uch /* attach hpckbd */
142 1.1 uch haa.haa_ic = &pfckbd_core.pc_if; /* tell the hpckbd to my interface */
143 1.1 uch config_found(self, &haa, hpckbd_print);
144 1.1 uch
145 1.1 uch /* install callout handler */
146 1.1 uch callout_init(&pfckbd_core.pc_soft_ch);
147 1.3 uch callout_reset(&pfckbd_core.pc_soft_ch, 1, pfckbd_core.pc_callout,
148 1.3 uch &pfckbd_core);
149 1.1 uch }
150 1.1 uch
151 1.3 uch int
152 1.1 uch pfckbd_input_establish(void *ic, struct hpckbd_if *kbdif)
153 1.1 uch {
154 1.1 uch struct pfckbd_core *pc = ic;
155 1.1 uch
156 1.1 uch /* save hpckbd interface */
157 1.1 uch pc->pc_hpckbd = kbdif;
158 1.1 uch /* ok to transact hpckbd */
159 1.1 uch pc->pc_enabled = 1;
160 1.1 uch
161 1.1 uch return 0;
162 1.1 uch }
163 1.1 uch
164 1.3 uch int
165 1.1 uch pfckbd_poll(void *arg)
166 1.1 uch {
167 1.1 uch struct pfckbd_core *pc = arg;
168 1.1 uch
169 1.1 uch if (pc->pc_enabled)
170 1.3 uch (*pc->pc_callout)(arg);
171 1.1 uch
172 1.1 uch return 0;
173 1.1 uch }
174 1.1 uch
175 1.3 uch void
176 1.1 uch pfckbd_ifsetup(struct pfckbd_core *pc)
177 1.1 uch {
178 1.1 uch int i;
179 1.1 uch
180 1.1 uch pc->pc_if.hii_ctx = pc;
181 1.1 uch pc->pc_if.hii_establish = pfckbd_input_establish;
182 1.1 uch pc->pc_if.hii_poll = pfckbd_poll;
183 1.1 uch for (i = 0; i < 8; i++)
184 1.2 uch pc->pc_column[i] = 0xdfff;
185 1.3 uch
186 1.3 uch /* select PFC access method */
187 1.3 uch pc->pc_callout = pfckbd_callout_lookup();
188 1.3 uch }
189 1.3 uch
190 1.3 uch void
191 1.3 uch pfckbd_input(struct hpckbd_if *hpckbd, u_int16_t *buf, u_int16_t data,
192 1.3 uch int column)
193 1.3 uch {
194 1.3 uch int row, type, val;
195 1.3 uch u_int16_t edge, mask;
196 1.3 uch
197 1.3 uch if ((edge = (data ^ buf[column]))) {
198 1.3 uch buf[column] = data;
199 1.3 uch
200 1.3 uch for (row = 0, mask = 1; row < 16; row++, mask <<= 1) {
201 1.3 uch if (mask & edge) {
202 1.3 uch type = mask & data ? 0 : 1;
203 1.3 uch val = row * 8 + column;
204 1.3 uch DPRINTF("(%2d, %2d) %d \n",
205 1.3 uch row, column, type);
206 1.3 uch hpckbd_input(hpckbd, type, val);
207 1.3 uch }
208 1.3 uch }
209 1.3 uch }
210 1.1 uch }
211 1.1 uch
212 1.3 uch /*
213 1.3 uch * Platform dependent routines.
214 1.3 uch */
215 1.3 uch
216 1.3 uch /* Look up appropriate callback handler */
217 1.3 uch void (*pfckbd_callout_lookup())(void *)
218 1.3 uch {
219 1.3 uch int i, n = sizeof(pfckbd_calloutfunc_table) /
220 1.3 uch sizeof(pfckbd_calloutfunc_table[0]);
221 1.3 uch
222 1.3 uch for (i = 0; i < n; i++)
223 1.3 uch if (platid_match(&platid,
224 1.3 uch pfckbd_calloutfunc_table[i].platform))
225 1.3 uch return (pfckbd_calloutfunc_table[i].func);
226 1.3 uch
227 1.3 uch return (pfckbd_callout_unknown);
228 1.3 uch }
229 1.3 uch
230 1.3 uch /* Placeholder for unknown platform */
231 1.3 uch void
232 1.3 uch pfckbd_callout_unknown(void *arg)
233 1.3 uch {
234 1.3 uch
235 1.3 uch printf("%s: unknown keyboard switch\n", __FUNCTION__);
236 1.3 uch }
237 1.3 uch
238 1.3 uch /* HP Jornada680/690, HP620LX */
239 1.3 uch void
240 1.3 uch pfckbd_callout_hp(void *arg)
241 1.1 uch {
242 1.10 uwe #define PFCKBD_HP_PDCR_MASK 0xcc0c
243 1.10 uwe #define PFCKBD_HP_PECR_MASK 0xf0cf
244 1.10 uwe
245 1.10 uwe /*
246 1.10 uwe * Disable output on all lines but the n'th line in D.
247 1.10 uwe * Pull the n'th scan line in D low.
248 1.10 uwe */
249 1.10 uwe #define PD(n) \
250 1.10 uwe { (u_int16_t)(PFCKBD_HP_PDCR_MASK & (~(1 << (2*(n)+1)))), \
251 1.10 uwe (u_int16_t)(PFCKBD_HP_PECR_MASK & 0xffff), \
252 1.10 uwe (u_int8_t)~(1 << (n)), \
253 1.10 uwe 0xff }
254 1.10 uwe
255 1.10 uwe /* Ditto for E */
256 1.10 uwe #define PE(n) \
257 1.10 uwe { (u_int16_t)(PFCKBD_HP_PDCR_MASK & 0xffff), \
258 1.10 uwe (u_int16_t)(PFCKBD_HP_PECR_MASK & (~(1 << (2*(n)+1)))), \
259 1.10 uwe 0xff, \
260 1.10 uwe (u_int8_t)~(1 << (n)) }
261 1.10 uwe
262 1.1 uch static const struct {
263 1.10 uwe u_int16_t dc, ec; u_int8_t d, e;
264 1.1 uch } scan[] = {
265 1.10 uwe PD(1), PD(5), PE(1), PE(6), PE(7), PE(3), PE(0), PD(7)
266 1.1 uch };
267 1.10 uwe
268 1.10 uwe #undef PD
269 1.10 uwe #undef PE
270 1.10 uwe
271 1.1 uch struct pfckbd_core *pc = arg;
272 1.10 uwe u_int16_t dc, ec;
273 1.3 uch int column;
274 1.3 uch u_int16_t data;
275 1.1 uch
276 1.1 uch if (!pc->pc_enabled)
277 1.1 uch goto reinstall;
278 1.1 uch
279 1.10 uwe /* bits in D/E control regs we do not touch (XXX: can they change?) */
280 1.10 uwe dc = _reg_read_2(SH7709_PDCR) & ~PFCKBD_HP_PDCR_MASK;
281 1.10 uwe ec = _reg_read_2(SH7709_PECR) & ~PFCKBD_HP_PECR_MASK;
282 1.10 uwe
283 1.1 uch for (column = 0; column < 8; column++) {
284 1.10 uwe /* disable output to all lines except the one we scan */
285 1.10 uwe _reg_write_2(SH7709_PDCR, dc | scan[column].dc);
286 1.10 uwe _reg_write_2(SH7709_PECR, ec | scan[column].ec);
287 1.10 uwe delay(5);
288 1.10 uwe
289 1.10 uwe /* pull the scan line low */
290 1.7 uch _reg_write_1(SH7709_PDDR, scan[column].d);
291 1.7 uch _reg_write_1(SH7709_PEDR, scan[column].e);
292 1.1 uch delay(50);
293 1.10 uwe
294 1.10 uwe /* read sense */
295 1.7 uch data = _reg_read_1(SH7709_PFDR) |
296 1.7 uch (_reg_read_1(SH7709_PCDR) << 8);
297 1.2 uch
298 1.3 uch pfckbd_input(pc->pc_hpckbd, pc->pc_column, data, column);
299 1.3 uch }
300 1.3 uch
301 1.10 uwe /* scan no lines */
302 1.7 uch _reg_write_1(SH7709_PDDR, 0xff);
303 1.7 uch _reg_write_1(SH7709_PEDR, 0xff);
304 1.10 uwe
305 1.10 uwe /* enable all scan lines */
306 1.10 uwe _reg_write_2(SH7709_PDCR, dc | (0x5555 & PFCKBD_HP_PDCR_MASK));
307 1.10 uwe _reg_write_2(SH7709_PECR, ec | (0x5555 & PFCKBD_HP_PECR_MASK));
308 1.10 uwe
309 1.12 uwe #if 0
310 1.10 uwe /* (ignore) extra keys/events (recorder buttons, lid, cable &c) */
311 1.7 uch data = _reg_read_1(SH7709_PGDR) | (_reg_read_1(SH7709_PHDR) << 8);
312 1.12 uwe #endif
313 1.3 uch
314 1.3 uch reinstall:
315 1.12 uwe callout_schedule(&pc->pc_soft_ch, 1);
316 1.3 uch }
317 1.3 uch
318 1.3 uch /* HITACH PERSONA (HPW-50PAD) */
319 1.3 uch void
320 1.3 uch pfckbd_callout_hitachi(void *arg)
321 1.3 uch {
322 1.3 uch static const struct {
323 1.3 uch u_int8_t d, e;
324 1.3 uch } scan[] = {
325 1.3 uch { 0xf5, 0xff },
326 1.3 uch { 0xd7, 0xff },
327 1.3 uch { 0xf7, 0xfd },
328 1.3 uch { 0xf7, 0xbf },
329 1.3 uch { 0xf7, 0x7f },
330 1.3 uch { 0xf7, 0xf7 },
331 1.3 uch { 0xf7, 0xfe },
332 1.3 uch { 0x77, 0xff },
333 1.3 uch };
334 1.3 uch struct pfckbd_core *pc = arg;
335 1.3 uch u_int16_t data;
336 1.3 uch int column;
337 1.3 uch
338 1.3 uch if (!pc->pc_enabled)
339 1.3 uch goto reinstall;
340 1.3 uch
341 1.3 uch for (column = 0; column < 8; column++) {
342 1.7 uch _reg_write_1(SH7709_PCDR, ~(1 << column));
343 1.3 uch delay(50);
344 1.7 uch data = ((_reg_read_1(SH7709_PFDR) & 0xfe) |
345 1.7 uch (_reg_read_1(SH7709_PCDR) & 0x01)) << 8;
346 1.7 uch _reg_write_1(SH7709_PCDR, 0xff);
347 1.7 uch _reg_write_1(SH7709_PDDR, scan[column].d);
348 1.7 uch _reg_write_1(SH7709_PEDR, scan[column].e);
349 1.3 uch delay(50);
350 1.7 uch data |= (_reg_read_1(SH7709_PFDR) & 0xfe) |
351 1.7 uch (_reg_read_1(SH7709_PCDR) & 0x01);
352 1.7 uch _reg_write_1(SH7709_PDDR, 0xf7);
353 1.7 uch _reg_write_1(SH7709_PEDR, 0xff);
354 1.1 uch
355 1.3 uch pfckbd_input(pc->pc_hpckbd, pc->pc_column, data, column);
356 1.1 uch }
357 1.1 uch
358 1.7 uch _reg_write_1(SH7709_PDDR, 0xf7);
359 1.7 uch _reg_write_1(SH7709_PEDR, 0xff);
360 1.7 uch data = _reg_read_1(SH7709_PGDR) | (_reg_read_1(SH7709_PHDR) << 8);
361 1.1 uch
362 1.1 uch reinstall:
363 1.12 uwe callout_schedule(&pc->pc_soft_ch, 1);
364 1.1 uch }
365