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