Home | History | Annotate | Line # | Download | only in dev
pfckbd.c revision 1.15.2.2
      1  1.15.2.2      yamt /*	$NetBSD: pfckbd.c,v 1.15.2.2 2007/09/03 14:26:15 yamt 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.15.2.1      yamt /*
     40  1.15.2.1      yamt  * Matrix scan keyboard connected to SH7709, SH7709A PFC module.
     41  1.15.2.1      yamt  * currently, HP Jornada 680/690, HITACHI PERSONA HPW-50PAD only.
     42  1.15.2.1      yamt  */
     43      1.11     lukem #include <sys/cdefs.h>
     44  1.15.2.2      yamt __KERNEL_RCSID(0, "$NetBSD: pfckbd.c,v 1.15.2.2 2007/09/03 14:26:15 yamt Exp $");
     45       1.4       uch 
     46       1.4       uch #include "debug_hpcsh.h"
     47       1.1       uch 
     48       1.1       uch #include <sys/param.h>
     49       1.1       uch #include <sys/systm.h>
     50       1.1       uch #include <sys/device.h>
     51       1.1       uch #include <sys/callout.h>
     52       1.1       uch 
     53       1.1       uch #include <machine/bus.h>
     54       1.3       uch #include <machine/platid.h>
     55       1.3       uch #include <machine/platid_mask.h>
     56       1.3       uch 
     57       1.1       uch #include <dev/hpc/hpckbdvar.h>
     58       1.1       uch 
     59       1.1       uch #include <sh3/pfcreg.h>
     60       1.1       uch 
     61       1.1       uch #include <hpcsh/dev/pfckbdvar.h>
     62       1.1       uch 
     63       1.4       uch #ifdef	PFCKBD_DEBUG
     64       1.4       uch #define DPRINTF_ENABLE
     65       1.4       uch #define DPRINTF_DEBUG	pfckbd_debug
     66       1.1       uch #endif
     67       1.5       uch #include <machine/debug.h>
     68       1.1       uch 
     69  1.15.2.1      yamt static struct pfckbd_core {
     70       1.1       uch 	int pc_attached;
     71       1.1       uch 	int pc_enabled;
     72       1.1       uch 	struct callout pc_soft_ch;
     73       1.1       uch 	struct hpckbd_ic_if pc_if;
     74       1.1       uch 	struct hpckbd_if *pc_hpckbd;
     75      1.14       uwe 	uint16_t pc_column[8];
     76       1.3       uch 	void (*pc_callout)(void *);
     77       1.1       uch } pfckbd_core;
     78       1.1       uch 
     79  1.15.2.1      yamt static int pfckbd_match(struct device *, struct cfdata *, void *);
     80  1.15.2.1      yamt static void pfckbd_attach(struct device *, struct device *, void *);
     81  1.15.2.1      yamt 
     82  1.15.2.1      yamt CFATTACH_DECL(pfckbd, sizeof(struct device),
     83  1.15.2.1      yamt     pfckbd_match, pfckbd_attach, NULL, NULL);
     84  1.15.2.1      yamt 
     85  1.15.2.1      yamt static void pfckbd_ifsetup(struct pfckbd_core *);
     86  1.15.2.1      yamt 
     87  1.15.2.1      yamt /* callbacks for hpckbd */
     88  1.15.2.1      yamt static int pfckbd_input_establish(void *, struct hpckbd_if *);
     89  1.15.2.1      yamt static int pfckbd_poll(void *);
     90  1.15.2.1      yamt 
     91  1.15.2.1      yamt static void pfckbd_input(struct pfckbd_core *, int, uint16_t);
     92  1.15.2.1      yamt 
     93  1.15.2.1      yamt static void (*pfckbd_callout_lookup(void))(void *);
     94  1.15.2.1      yamt static void pfckbd_callout_unknown(void *);
     95  1.15.2.1      yamt static void pfckbd_callout_hp(void *);
     96  1.15.2.1      yamt static void pfckbd_callout_hitachi(void *);
     97  1.15.2.1      yamt 
     98       1.3       uch /* callout function table. this function is platfrom specific. */
     99  1.15.2.1      yamt static const struct {
    100       1.3       uch 	platid_mask_t *platform;
    101       1.3       uch 	void (*func)(void *);
    102       1.3       uch } pfckbd_calloutfunc_table[] = {
    103       1.3       uch 	{ &platid_mask_MACH_HP		, pfckbd_callout_hp },
    104       1.3       uch 	{ &platid_mask_MACH_HITACHI	, pfckbd_callout_hitachi }
    105       1.1       uch };
    106       1.1       uch 
    107       1.1       uch 
    108       1.1       uch void
    109       1.1       uch pfckbd_cnattach()
    110       1.1       uch {
    111       1.1       uch 	struct pfckbd_core *pc = &pfckbd_core;
    112  1.15.2.1      yamt 
    113  1.15.2.1      yamt 	if ((cpu_product != CPU_PRODUCT_7709)
    114  1.15.2.1      yamt 	    && (cpu_product != CPU_PRODUCT_7709A))
    115       1.7       uch 		return;
    116       1.7       uch 
    117       1.1       uch 	/* initialize interface */
    118       1.1       uch 	pfckbd_ifsetup(pc);
    119       1.3       uch 
    120  1.15.2.1      yamt 	/* attach descendants */
    121       1.1       uch 	hpckbd_cnattach(&pc->pc_if);
    122       1.1       uch }
    123       1.1       uch 
    124  1.15.2.1      yamt static int
    125       1.1       uch pfckbd_match(struct device *parent, struct cfdata *cf, void *aux)
    126       1.1       uch {
    127       1.1       uch 
    128  1.15.2.1      yamt 	if ((cpu_product != CPU_PRODUCT_7709)
    129  1.15.2.1      yamt 	    && (cpu_product != CPU_PRODUCT_7709A))
    130  1.15.2.1      yamt 		return 0;
    131       1.7       uch 
    132  1.15.2.1      yamt 	return !pfckbd_core.pc_attached; /* attach only once */
    133       1.1       uch }
    134       1.1       uch 
    135  1.15.2.1      yamt static void
    136       1.1       uch pfckbd_attach(struct device *parent, struct device *self, void *aux)
    137       1.1       uch {
    138       1.1       uch 	struct hpckbd_attach_args haa;
    139  1.15.2.1      yamt 
    140       1.1       uch 	printf("\n");
    141       1.1       uch 
    142       1.1       uch 	pfckbd_core.pc_attached = 1;
    143  1.15.2.1      yamt 
    144       1.1       uch 	pfckbd_ifsetup(&pfckbd_core);
    145       1.1       uch 
    146       1.1       uch 	/* attach hpckbd */
    147  1.15.2.1      yamt 	haa.haa_ic = &pfckbd_core.pc_if; /* tell hpckbd our interface */
    148       1.1       uch 	config_found(self, &haa, hpckbd_print);
    149       1.1       uch 
    150       1.1       uch 	/* install callout handler */
    151  1.15.2.2      yamt 	callout_init(&pfckbd_core.pc_soft_ch, 0);
    152  1.15.2.1      yamt 	callout_reset(&pfckbd_core.pc_soft_ch, 1,
    153  1.15.2.1      yamt 		      pfckbd_core.pc_callout, &pfckbd_core);
    154       1.1       uch }
    155       1.1       uch 
    156  1.15.2.1      yamt static void
    157  1.15.2.1      yamt pfckbd_ifsetup(struct pfckbd_core *pc)
    158       1.1       uch {
    159  1.15.2.1      yamt 	int i;
    160  1.15.2.1      yamt 
    161  1.15.2.1      yamt 	pc->pc_if.hii_ctx = pc;
    162  1.15.2.1      yamt 	pc->pc_if.hii_establish	= pfckbd_input_establish;
    163  1.15.2.1      yamt 	pc->pc_if.hii_poll = pfckbd_poll;
    164       1.1       uch 
    165  1.15.2.1      yamt 	for (i = 0; i < 8; i++)
    166  1.15.2.1      yamt 		pc->pc_column[i] = 0xdfff;
    167       1.1       uch 
    168  1.15.2.1      yamt 	/* select PFC access method */
    169  1.15.2.1      yamt 	pc->pc_callout = pfckbd_callout_lookup();
    170       1.1       uch }
    171       1.1       uch 
    172  1.15.2.1      yamt 
    173  1.15.2.1      yamt /*
    174  1.15.2.1      yamt  * Callback for hpckbd_initif
    175  1.15.2.1      yamt  */
    176  1.15.2.1      yamt static int
    177  1.15.2.1      yamt pfckbd_input_establish(void *ic, struct hpckbd_if *kbdif)
    178       1.1       uch {
    179  1.15.2.1      yamt 	struct pfckbd_core *pc = ic;
    180       1.1       uch 
    181  1.15.2.1      yamt 	pc->pc_hpckbd = kbdif;	/* save hpckbd interface */
    182  1.15.2.1      yamt 	pc->pc_enabled = 1;	/* ok to talk to hpckbd */
    183       1.1       uch 
    184       1.1       uch 	return 0;
    185       1.1       uch }
    186       1.1       uch 
    187  1.15.2.1      yamt /*
    188  1.15.2.1      yamt  * Callback for hpckbd_cngetc
    189  1.15.2.1      yamt  */
    190  1.15.2.1      yamt static int
    191  1.15.2.1      yamt pfckbd_poll(void *ic)
    192       1.1       uch {
    193  1.15.2.1      yamt 	struct pfckbd_core *pc = ic;
    194       1.1       uch 
    195  1.15.2.1      yamt 	if (pc->pc_enabled)
    196  1.15.2.1      yamt 		(*pc->pc_callout)(pc);
    197       1.3       uch 
    198  1.15.2.1      yamt 	return 0;
    199       1.3       uch }
    200       1.3       uch 
    201  1.15.2.1      yamt /*
    202  1.15.2.1      yamt  * Called by platform specific scan routines to report key events to hpckbd
    203  1.15.2.1      yamt  */
    204  1.15.2.1      yamt static void
    205  1.15.2.1      yamt pfckbd_input(struct pfckbd_core *pc, int column, uint16_t data)
    206       1.3       uch {
    207       1.3       uch 	int row, type, val;
    208  1.15.2.1      yamt 	unsigned int edge, mask;
    209  1.15.2.1      yamt 
    210  1.15.2.1      yamt 	edge = data ^ pc->pc_column[column];
    211  1.15.2.1      yamt 	if (edge == 0)
    212  1.15.2.1      yamt 		return;		/* no changes in this column */
    213       1.3       uch 
    214  1.15.2.1      yamt 	pc->pc_column[column] = data;
    215       1.3       uch 
    216  1.15.2.1      yamt 	for (row = 0, mask = 1; row < 16; ++row, mask <<= 1) {
    217  1.15.2.1      yamt 		if (mask & edge) {
    218  1.15.2.1      yamt 			type = mask & data ? /* up */ 0 : /* down */ 1;
    219  1.15.2.1      yamt 			DPRINTF("(%2d, %2d) %d \n", row, column, type);
    220  1.15.2.1      yamt 
    221  1.15.2.1      yamt 			val = row * 8 + column;
    222  1.15.2.1      yamt 			hpckbd_input(pc->pc_hpckbd, type, val);
    223       1.3       uch 		}
    224       1.3       uch 	}
    225       1.1       uch }
    226       1.1       uch 
    227  1.15.2.1      yamt 
    228       1.3       uch /*
    229  1.15.2.1      yamt  * Platform dependent scan routines.
    230       1.3       uch  */
    231       1.3       uch 
    232       1.3       uch /* Look up appropriate callback handler */
    233  1.15.2.1      yamt static void
    234  1.15.2.1      yamt (*pfckbd_callout_lookup())(void *)
    235       1.3       uch {
    236  1.15.2.1      yamt 	int i, n;
    237  1.15.2.1      yamt 
    238  1.15.2.1      yamt 	n = sizeof(pfckbd_calloutfunc_table)
    239  1.15.2.1      yamt 		/ sizeof(pfckbd_calloutfunc_table[0]);
    240  1.15.2.1      yamt 
    241       1.3       uch 	for (i = 0; i < n; i++)
    242       1.3       uch 		if (platid_match(&platid,
    243  1.15.2.1      yamt 				 pfckbd_calloutfunc_table[i].platform))
    244  1.15.2.1      yamt 			return pfckbd_calloutfunc_table[i].func;
    245       1.3       uch 
    246  1.15.2.1      yamt 	return pfckbd_callout_unknown;
    247       1.3       uch }
    248       1.3       uch 
    249       1.3       uch /* Placeholder for unknown platform */
    250  1.15.2.1      yamt static void
    251       1.3       uch pfckbd_callout_unknown(void *arg)
    252       1.3       uch {
    253       1.3       uch 
    254  1.15.2.1      yamt 	printf("%s: unknown keyboard switch\n", __func__);
    255       1.3       uch }
    256       1.3       uch 
    257  1.15.2.1      yamt /*
    258  1.15.2.1      yamt  * HP Jornada680/690, HP620LX
    259  1.15.2.1      yamt  */
    260  1.15.2.1      yamt static void
    261       1.3       uch pfckbd_callout_hp(void *arg)
    262       1.1       uch {
    263      1.10       uwe #define PFCKBD_HP_PDCR_MASK 0xcc0c
    264      1.10       uwe #define PFCKBD_HP_PECR_MASK 0xf0cf
    265      1.10       uwe 
    266      1.10       uwe 	/*
    267      1.10       uwe 	 * Disable output on all lines but the n'th line in D.
    268      1.10       uwe 	 * Pull the n'th scan line in D low.
    269      1.10       uwe 	 */
    270      1.10       uwe #define PD(n)								\
    271      1.14       uwe 	{ (uint16_t)(PFCKBD_HP_PDCR_MASK & (~(1 << (2*(n)+1)))),	\
    272      1.14       uwe 	  (uint16_t)(PFCKBD_HP_PECR_MASK & 0xffff),			\
    273      1.14       uwe 	  (uint8_t)~(1 << (n)),						\
    274      1.10       uwe 	  0xff }
    275      1.10       uwe 
    276      1.10       uwe 	/* Ditto for E */
    277      1.10       uwe #define PE(n)								\
    278      1.14       uwe 	{ (uint16_t)(PFCKBD_HP_PDCR_MASK & 0xffff),			\
    279      1.14       uwe 	  (uint16_t)(PFCKBD_HP_PECR_MASK & (~(1 << (2*(n)+1)))),	\
    280      1.10       uwe 	  0xff,								\
    281      1.14       uwe 	  (uint8_t)~(1 << (n)) }
    282      1.10       uwe 
    283       1.1       uch 	static const struct {
    284      1.14       uwe 		uint16_t dc, ec; uint8_t d, e;
    285       1.1       uch 	} scan[] = {
    286      1.10       uwe 		PD(1), PD(5), PE(1), PE(6), PE(7), PE(3), PE(0), PD(7)
    287       1.1       uch 	};
    288      1.10       uwe 
    289      1.10       uwe #undef PD
    290      1.10       uwe #undef PE
    291      1.10       uwe 
    292       1.1       uch 	struct pfckbd_core *pc = arg;
    293      1.14       uwe 	uint16_t dc, ec;
    294       1.3       uch 	int column;
    295      1.14       uwe 	uint16_t data;
    296       1.1       uch 
    297       1.1       uch 	if (!pc->pc_enabled)
    298       1.1       uch 		goto reinstall;
    299       1.1       uch 
    300      1.10       uwe 	/* bits in D/E control regs we do not touch (XXX: can they change?) */
    301      1.10       uwe 	dc = _reg_read_2(SH7709_PDCR) & ~PFCKBD_HP_PDCR_MASK;
    302      1.10       uwe 	ec = _reg_read_2(SH7709_PECR) & ~PFCKBD_HP_PECR_MASK;
    303      1.10       uwe 
    304       1.1       uch 	for (column = 0; column < 8; column++) {
    305      1.10       uwe 		/* disable output to all lines except the one we scan */
    306      1.10       uwe 		_reg_write_2(SH7709_PDCR, dc | scan[column].dc);
    307      1.10       uwe 		_reg_write_2(SH7709_PECR, ec | scan[column].ec);
    308      1.10       uwe 		delay(5);
    309      1.10       uwe 
    310      1.10       uwe 		/* pull the scan line low */
    311       1.7       uch 		_reg_write_1(SH7709_PDDR, scan[column].d);
    312       1.7       uch 		_reg_write_1(SH7709_PEDR, scan[column].e);
    313       1.1       uch 		delay(50);
    314      1.10       uwe 
    315      1.10       uwe 		/* read sense */
    316  1.15.2.1      yamt 		data = _reg_read_1(SH7709_PFDR)
    317  1.15.2.1      yamt 			| (_reg_read_1(SH7709_PCDR) << 8);
    318       1.2       uch 
    319  1.15.2.1      yamt 		pfckbd_input(pc, column, data);
    320       1.3       uch 	}
    321       1.3       uch 
    322      1.10       uwe 	/* scan no lines */
    323       1.7       uch 	_reg_write_1(SH7709_PDDR, 0xff);
    324       1.7       uch 	_reg_write_1(SH7709_PEDR, 0xff);
    325      1.10       uwe 
    326      1.10       uwe 	/* enable all scan lines */
    327      1.10       uwe 	_reg_write_2(SH7709_PDCR, dc | (0x5555 & PFCKBD_HP_PDCR_MASK));
    328      1.10       uwe 	_reg_write_2(SH7709_PECR, ec | (0x5555 & PFCKBD_HP_PECR_MASK));
    329      1.10       uwe 
    330      1.12       uwe #if 0
    331      1.10       uwe 	/* (ignore) extra keys/events (recorder buttons, lid, cable &c) */
    332       1.7       uch 	data = _reg_read_1(SH7709_PGDR) | (_reg_read_1(SH7709_PHDR) << 8);
    333      1.12       uwe #endif
    334       1.3       uch 
    335       1.3       uch  reinstall:
    336      1.12       uwe 	callout_schedule(&pc->pc_soft_ch, 1);
    337       1.3       uch }
    338       1.3       uch 
    339  1.15.2.1      yamt /*
    340  1.15.2.1      yamt  * HITACH PERSONA (HPW-50PAD)
    341  1.15.2.1      yamt  */
    342  1.15.2.1      yamt static void
    343       1.3       uch pfckbd_callout_hitachi(void *arg)
    344       1.3       uch {
    345      1.15  kiyohara #define PFCKBD_HITACHI_PCCR_MASK 0xfff3
    346      1.13       uwe #define PFCKBD_HITACHI_PDCR_MASK 0x000c
    347      1.13       uwe #define PFCKBD_HITACHI_PECR_MASK 0x30cf
    348      1.13       uwe 
    349      1.15  kiyohara #define PFCKBD_HITACHI_PCDR_SCN_MASK 0xfd
    350      1.13       uwe #define PFCKBD_HITACHI_PDDR_SCN_MASK 0xf7
    351      1.13       uwe #define PFCKBD_HITACHI_PEDR_SCN_MASK 0xff
    352      1.13       uwe 
    353      1.13       uwe #define PFCKBD_HITACHI_PCDR_SNS_MASK 0x01
    354      1.13       uwe #define PFCKBD_HITACHI_PFDR_SNS_MASK 0xfe
    355      1.13       uwe 
    356      1.13       uwe 	/*
    357      1.13       uwe 	 * Disable output on all lines but the n'th line in C.
    358      1.13       uwe 	 * Pull the n'th scan line in C low.
    359      1.13       uwe 	 */
    360      1.13       uwe #define PC(n)								\
    361      1.14       uwe 	{ (uint16_t)(PFCKBD_HITACHI_PCCR_MASK & (~(1 << (2*(n)+1)))),	\
    362      1.14       uwe 	  (uint16_t)(PFCKBD_HITACHI_PDCR_MASK & 0xffff),		\
    363      1.14       uwe 	  (uint16_t)(PFCKBD_HITACHI_PECR_MASK & 0xffff),		\
    364      1.14       uwe 	  (uint8_t)(PFCKBD_HITACHI_PCDR_SCN_MASK & ~(1 << (n))),	\
    365      1.13       uwe 	  PFCKBD_HITACHI_PDDR_SCN_MASK,					\
    366      1.13       uwe 	  PFCKBD_HITACHI_PEDR_SCN_MASK }
    367      1.13       uwe 
    368      1.13       uwe 	/* Ditto for D */
    369      1.13       uwe #define PD(n)								\
    370      1.14       uwe 	{ (uint16_t)(PFCKBD_HITACHI_PCCR_MASK & 0xffff),		\
    371      1.14       uwe 	  (uint16_t)(PFCKBD_HITACHI_PDCR_MASK & (~(1 << (2*(n)+1)))),	\
    372      1.14       uwe 	  (uint16_t)(PFCKBD_HITACHI_PECR_MASK & 0xffff),		\
    373      1.13       uwe 	  PFCKBD_HITACHI_PCDR_SCN_MASK,					\
    374      1.14       uwe 	  (uint8_t)(PFCKBD_HITACHI_PDDR_SCN_MASK & ~(1 << (n))),	\
    375      1.13       uwe 	  PFCKBD_HITACHI_PEDR_SCN_MASK }
    376      1.13       uwe 
    377      1.13       uwe 	/* Ditto for E */
    378      1.13       uwe #define PE(n)								\
    379      1.14       uwe 	{ (uint16_t)(PFCKBD_HITACHI_PCCR_MASK & 0xffff),		\
    380      1.14       uwe 	  (uint16_t)(PFCKBD_HITACHI_PDCR_MASK & 0xffff),		\
    381      1.14       uwe 	  (uint16_t)(PFCKBD_HITACHI_PECR_MASK & (~(1 << (2*(n)+1)))),	\
    382      1.13       uwe 	  PFCKBD_HITACHI_PCDR_SCN_MASK,					\
    383      1.13       uwe 	  PFCKBD_HITACHI_PDDR_SCN_MASK,					\
    384      1.14       uwe 	  (uint8_t)(PFCKBD_HITACHI_PEDR_SCN_MASK & ~(1 << (n))) }
    385      1.13       uwe 
    386       1.3       uch 	static const struct {
    387      1.14       uwe 		uint16_t cc, dc, ec; uint8_t c, d, e;
    388       1.3       uch 	} scan[] = {
    389      1.13       uwe 		PE(6), PE(3), PE(1), PE(0), PC(7), PC(6), PC(5), PC(4),
    390      1.15  kiyohara 		PC(3), PC(2), PD(1), PC(0)
    391       1.3       uch 	};
    392      1.13       uwe 
    393      1.13       uwe #undef PC
    394      1.13       uwe #undef PD
    395      1.13       uwe #undef PE
    396      1.13       uwe 
    397       1.3       uch 	struct pfckbd_core *pc = arg;
    398      1.14       uwe 	uint16_t cc, dc, ec;
    399      1.15  kiyohara 	uint8_t data[2], cd, dd, ed;
    400      1.13       uwe 	int i;
    401       1.3       uch 
    402       1.3       uch 	if (!pc->pc_enabled)
    403       1.3       uch 		goto reinstall;
    404       1.3       uch 
    405      1.13       uwe 	/* bits in C/D/E control regs we do not touch (XXX: can they change?) */
    406      1.13       uwe 	cc = _reg_read_2(SH7709_PCCR) & ~PFCKBD_HITACHI_PCCR_MASK;
    407      1.13       uwe 	dc = _reg_read_2(SH7709_PDCR) & ~PFCKBD_HITACHI_PDCR_MASK;
    408      1.13       uwe 	ec = _reg_read_2(SH7709_PECR) & ~PFCKBD_HITACHI_PECR_MASK;
    409      1.13       uwe 
    410      1.15  kiyohara 	cd = _reg_read_1(SH7709_PCDR);
    411      1.15  kiyohara 	dd = _reg_read_1(SH7709_PDDR);
    412      1.15  kiyohara 	ed = _reg_read_1(SH7709_PEDR);
    413      1.15  kiyohara 
    414      1.13       uwe 	for (i = 0; i < 12; i++) {
    415      1.13       uwe 		/* disable output to all lines except the one we scan */
    416      1.15  kiyohara 		_reg_write_2(SH7709_PCCR, cc | scan[i].cc);
    417      1.13       uwe 		_reg_write_2(SH7709_PDCR, dc | scan[i].dc);
    418      1.13       uwe 		_reg_write_2(SH7709_PECR, ec | scan[i].ec);
    419      1.13       uwe 		delay(5);
    420      1.13       uwe 
    421      1.13       uwe 		/* pull the scan line low */
    422      1.13       uwe 		_reg_write_1(SH7709_PCDR, scan[i].c);
    423      1.13       uwe 		_reg_write_1(SH7709_PDDR, scan[i].d);
    424      1.13       uwe 		_reg_write_1(SH7709_PEDR, scan[i].e);
    425       1.3       uch 		delay(50);
    426       1.1       uch 
    427      1.13       uwe 		/* read sense */
    428      1.13       uwe 		data[i & 0x1] =
    429  1.15.2.1      yamt 		    (_reg_read_1(SH7709_PCDR) & PFCKBD_HITACHI_PCDR_SNS_MASK)
    430  1.15.2.1      yamt 		  | (_reg_read_1(SH7709_PFDR) & PFCKBD_HITACHI_PFDR_SNS_MASK);
    431      1.13       uwe 
    432      1.13       uwe 		if (i & 0x1)
    433  1.15.2.1      yamt 			pfckbd_input(pc, (i >> 1), (data[0] | (data[1] << 8)));
    434       1.1       uch 	}
    435       1.1       uch 
    436      1.13       uwe 	/* scan no lines */
    437      1.15  kiyohara 	_reg_write_1(SH7709_PCDR, cd);
    438      1.15  kiyohara 	_reg_write_1(SH7709_PDDR, dd);
    439      1.15  kiyohara 	_reg_write_1(SH7709_PEDR, ed);
    440      1.13       uwe 
    441      1.13       uwe 	/* enable all scan lines */
    442      1.13       uwe 	_reg_write_2(SH7709_PCCR, cc | (0x5555 & PFCKBD_HITACHI_PCCR_MASK));
    443      1.13       uwe 	_reg_write_2(SH7709_PDCR, dc | (0x5555 & PFCKBD_HITACHI_PDCR_MASK));
    444      1.13       uwe 	_reg_write_2(SH7709_PECR, ec | (0x5555 & PFCKBD_HITACHI_PECR_MASK));
    445       1.1       uch 
    446       1.1       uch  reinstall:
    447      1.12       uwe 	callout_schedule(&pc->pc_soft_ch, 1);
    448       1.1       uch }
    449