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