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