Home | History | Annotate | Line # | Download | only in dev
tc5165buf.c revision 1.17
      1  1.17       chs /*	$NetBSD: tc5165buf.c,v 1.17 2012/10/27 17:17:53 chs Exp $ */
      2   1.1       uch 
      3   1.7       uch /*-
      4   1.7       uch  * Copyright (c) 1999-2001 The NetBSD Foundation, Inc.
      5   1.1       uch  * All rights reserved.
      6   1.1       uch  *
      7   1.7       uch  * This code is derived from software contributed to The NetBSD Foundation
      8   1.7       uch  * by UCHIYAMA Yasushi.
      9   1.7       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.7       uch  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.7       uch  *    notice, this list of conditions and the following disclaimer in the
     17   1.7       uch  *    documentation and/or other materials provided with the distribution.
     18   1.1       uch  *
     19   1.7       uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.7       uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.7       uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.7       uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.7       uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.7       uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.7       uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.7       uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.7       uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.7       uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.7       uch  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       uch  */
     31   1.1       uch 
     32   1.1       uch /*
     33   1.3       uch  * Device driver for TOSHIBA TC5165BFTS, PHILIPS 74ALVC16241/245
     34   1.3       uch  * buffer chip
     35   1.1       uch  */
     36  1.12     lukem 
     37  1.12     lukem #include <sys/cdefs.h>
     38  1.17       chs __KERNEL_RCSID(0, "$NetBSD: tc5165buf.c,v 1.17 2012/10/27 17:17:53 chs Exp $");
     39   1.1       uch 
     40   1.1       uch #include "opt_use_poll.h"
     41   1.1       uch 
     42   1.1       uch #include <sys/param.h>
     43   1.1       uch #include <sys/systm.h>
     44   1.5   thorpej #include <sys/callout.h>
     45   1.1       uch #include <sys/device.h>
     46   1.1       uch 
     47   1.1       uch #include <machine/bus.h>
     48   1.1       uch #include <machine/intr.h>
     49   1.1       uch 
     50   1.7       uch #include <dev/hpc/hpckbdvar.h>
     51   1.7       uch 
     52   1.1       uch #include <hpcmips/tx/tx39var.h>
     53   1.1       uch #include <hpcmips/tx/txcsbusvar.h>
     54   1.1       uch #include <hpcmips/dev/tc5165bufvar.h>
     55   1.1       uch 
     56   1.1       uch #define TC5165_ROW_MAX		16
     57   1.1       uch #define TC5165_COLUMN_MAX	8
     58   1.1       uch 
     59   1.1       uch #ifdef TC5165DEBUG
     60   1.1       uch #define	DPRINTF(arg) printf arg
     61   1.1       uch #else
     62   1.1       uch #define	DPRINTF(arg)
     63   1.1       uch #endif
     64   1.1       uch 
     65   1.1       uch struct tc5165buf_chip {
     66   1.1       uch 	bus_space_tag_t		scc_cst;
     67   1.1       uch 	bus_space_handle_t	scc_csh;
     68   1.3       uch 	u_int16_t		scc_buf[TC5165_COLUMN_MAX];
     69   1.1       uch 	int			scc_enabled;
     70   1.4       uch 	int			scc_queued;
     71   1.5   thorpej 	struct callout		scc_soft_ch;
     72   1.6  takemura 	struct hpckbd_ic_if	scc_if;
     73   1.6  takemura 	struct hpckbd_if	*scc_hpckbd;
     74   1.1       uch };
     75   1.1       uch 
     76   1.1       uch struct tc5165buf_softc {
     77   1.1       uch 	struct tc5165buf_chip	*sc_chip;
     78   1.1       uch 	tx_chipset_tag_t	sc_tc;
     79   1.1       uch 	void			*sc_ih;
     80   1.1       uch };
     81   1.1       uch 
     82  1.17       chs int	tc5165buf_match(device_t, cfdata_t, void *);
     83  1.17       chs void	tc5165buf_attach(device_t, device_t, void *);
     84   1.8       uch int	tc5165buf_intr(void *);
     85   1.8       uch int	tc5165buf_poll(void *);
     86   1.8       uch void	tc5165buf_soft(void *);
     87   1.8       uch void	tc5165buf_ifsetup(struct tc5165buf_chip *);
     88   1.1       uch 
     89   1.8       uch int	tc5165buf_input_establish(void *, struct hpckbd_if *);
     90   1.1       uch 
     91   1.1       uch struct tc5165buf_chip tc5165buf_chip;
     92   1.1       uch 
     93  1.17       chs CFATTACH_DECL_NEW(tc5165buf, sizeof(struct tc5165buf_softc),
     94  1.11   thorpej     tc5165buf_match, tc5165buf_attach, NULL, NULL);
     95   1.1       uch 
     96   1.1       uch int
     97  1.17       chs tc5165buf_match(device_t parent, cfdata_t cf, void *aux)
     98   1.1       uch {
     99   1.8       uch 
    100   1.8       uch 	return (1);
    101   1.1       uch }
    102   1.1       uch 
    103   1.1       uch void
    104  1.17       chs tc5165buf_attach(device_t parent, device_t self, void *aux)
    105   1.1       uch {
    106   1.1       uch 	struct cs_attach_args *ca = aux;
    107  1.17       chs 	struct tc5165buf_softc *sc = device_private(self);
    108   1.6  takemura 	struct hpckbd_attach_args haa;
    109   1.1       uch 
    110   1.4       uch 	printf(": ");
    111   1.1       uch 	sc->sc_tc = ca->ca_tc;
    112   1.1       uch 	sc->sc_chip = &tc5165buf_chip;
    113   1.5   thorpej 
    114  1.14        ad 	callout_init(&sc->sc_chip->scc_soft_ch, 0);
    115   1.5   thorpej 
    116   1.1       uch 	sc->sc_chip->scc_cst = ca->ca_csio.cstag;
    117   1.1       uch 
    118   1.1       uch 	if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase,
    119   1.8       uch 	    ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) {
    120   1.4       uch 		printf("can't map i/o space\n");
    121   1.1       uch 		return;
    122   1.1       uch 	}
    123   1.1       uch 
    124   1.1       uch 	sc->sc_chip->scc_enabled = 0;
    125   1.4       uch 
    126   1.4       uch 	if (ca->ca_irq1 != -1) {
    127   1.4       uch 		sc->sc_ih = tx_intr_establish(sc->sc_tc, ca->ca_irq1,
    128   1.8       uch 		    IST_EDGE, IPL_TTY,
    129   1.8       uch 		    tc5165buf_intr, sc);
    130   1.4       uch 		printf("interrupt mode");
    131   1.4       uch 	} else {
    132   1.4       uch 		sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1, IPL_TTY,
    133   1.8       uch 		    tc5165buf_intr, sc);
    134   1.4       uch 		printf("polling mode");
    135   1.1       uch 	}
    136   1.4       uch 
    137   1.4       uch 	if (!sc->sc_ih) {
    138   1.4       uch 		printf(" can't establish interrupt\n");
    139   1.1       uch 		return;
    140   1.1       uch 	}
    141   1.1       uch 
    142   1.1       uch 	printf("\n");
    143   1.1       uch 
    144   1.1       uch 	/* setup upper interface */
    145   1.1       uch 	tc5165buf_ifsetup(sc->sc_chip);
    146   1.1       uch 
    147   1.6  takemura 	haa.haa_ic = &sc->sc_chip->scc_if;
    148   1.1       uch 
    149   1.6  takemura 	config_found(self, &haa, hpckbd_print);
    150   1.1       uch }
    151   1.1       uch 
    152   1.1       uch void
    153   1.8       uch tc5165buf_ifsetup(struct tc5165buf_chip *scc)
    154   1.1       uch {
    155   1.8       uch 
    156   1.6  takemura 	scc->scc_if.hii_ctx		= scc;
    157   1.6  takemura 	scc->scc_if.hii_establish	= tc5165buf_input_establish;
    158   1.6  takemura 	scc->scc_if.hii_poll		= tc5165buf_poll;
    159   1.1       uch }
    160   1.1       uch 
    161   1.1       uch int
    162   1.8       uch tc5165buf_cnattach(paddr_t addr)
    163   1.1       uch {
    164   1.1       uch 	struct tc5165buf_chip *scc = &tc5165buf_chip;
    165   1.1       uch 
    166   1.1       uch 	scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr);
    167   1.1       uch 
    168   1.1       uch 	tc5165buf_ifsetup(scc);
    169   1.1       uch 
    170   1.6  takemura 	hpckbd_cnattach(&scc->scc_if);
    171   1.1       uch 
    172   1.8       uch 	return (0);
    173   1.1       uch }
    174   1.1       uch 
    175   1.1       uch int
    176   1.8       uch tc5165buf_input_establish(void *ic, struct hpckbd_if *kbdif)
    177   1.1       uch {
    178   1.1       uch 	struct tc5165buf_chip *scc = ic;
    179   1.1       uch 
    180   1.6  takemura 	/* save hpckbd interface */
    181   1.6  takemura 	scc->scc_hpckbd = kbdif;
    182   1.1       uch 
    183   1.1       uch 	scc->scc_enabled = 1;
    184   1.1       uch 
    185   1.8       uch 	return (0);
    186   1.1       uch }
    187   1.1       uch 
    188   1.1       uch int
    189   1.8       uch tc5165buf_intr(void *arg)
    190   1.1       uch {
    191   1.1       uch 	struct tc5165buf_softc *sc = arg;
    192   1.4       uch 	struct tc5165buf_chip *scc = sc->sc_chip;
    193   1.1       uch 
    194   1.4       uch 	if (!scc->scc_enabled || scc->scc_queued)
    195   1.8       uch 		return (0);
    196   1.4       uch 
    197   1.4       uch 	scc->scc_queued = 1;
    198   1.5   thorpej 	callout_reset(&scc->scc_soft_ch, 1, tc5165buf_soft, scc);
    199   1.5   thorpej 
    200   1.8       uch 	return (0);
    201   1.1       uch }
    202   1.1       uch 
    203   1.1       uch int
    204   1.8       uch tc5165buf_poll(void *arg)
    205   1.1       uch {
    206   1.4       uch 	struct tc5165buf_chip *scc = arg;
    207   1.4       uch 
    208   1.4       uch 	if (!scc->scc_enabled)
    209   1.8       uch 		return (POLL_CONT);
    210   1.4       uch 
    211   1.4       uch 	tc5165buf_soft(arg);
    212   1.4       uch 
    213   1.8       uch 	return (POLL_CONT);
    214   1.4       uch }
    215   1.4       uch 
    216   1.4       uch void
    217   1.8       uch tc5165buf_soft(void *arg)
    218   1.4       uch {
    219   1.4       uch 	struct tc5165buf_chip *scc = arg;
    220   1.1       uch 	bus_space_tag_t t = scc->scc_cst;
    221   1.1       uch 	bus_space_handle_t h = scc->scc_csh;
    222   1.1       uch 	u_int16_t mask, rpat, edge;
    223   1.3       uch 	int i, j, type, val;
    224   1.4       uch 	int s;
    225   1.1       uch 
    226   1.6  takemura 	hpckbd_input_hook(scc->scc_hpckbd);
    227   1.1       uch 
    228   1.3       uch 	/* clear scanlines */
    229   1.3       uch 	(void)bus_space_read_2(t, h, 0);
    230   1.3       uch 	delay(3);
    231   1.3       uch 
    232   1.3       uch 	for (i = 0; i < TC5165_COLUMN_MAX; i++) {
    233   1.3       uch 		rpat = bus_space_read_2(t, h, 2 << i);
    234   1.3       uch 		delay(3);
    235   1.3       uch 		(void)bus_space_read_2(t, h, 0);
    236   1.3       uch 		delay(3);
    237   1.3       uch 		if ((edge = (rpat ^ scc->scc_buf[i]))) {
    238   1.1       uch 			scc->scc_buf[i] = rpat;
    239   1.3       uch 			for (j = 0, mask = 1; j < TC5165_ROW_MAX;
    240   1.8       uch 			    j++, mask <<= 1) {
    241   1.1       uch 				if (mask & edge) {
    242   1.1       uch 					type = mask & rpat ? 1 : 0;
    243   1.3       uch 					val = j * TC5165_COLUMN_MAX + i;
    244   1.3       uch 					DPRINTF(("%d %d\n", j, i));
    245   1.6  takemura 					hpckbd_input(scc->scc_hpckbd,
    246   1.8       uch 					    type, val);
    247   1.1       uch 				}
    248   1.1       uch 			}
    249   1.1       uch 		}
    250   1.3       uch 	}
    251   1.1       uch 
    252   1.4       uch 	s = spltty();
    253   1.4       uch 	scc->scc_queued = 0;
    254   1.4       uch 	splx(s);
    255   1.1       uch }
    256