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