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