Home | History | Annotate | Line # | Download | only in mace
macekbc.c revision 1.2.36.1
      1  1.2.36.1   keiichi /* $NetBSD: macekbc.c,v 1.2.36.1 2008/03/24 07:15:03 keiichi Exp $ */
      2       1.1  jmcneill 
      3       1.1  jmcneill /*-
      4       1.1  jmcneill  * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
      5       1.1  jmcneill  * All rights reserved.
      6       1.1  jmcneill  *
      7       1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8       1.1  jmcneill  * modification, are permitted provided that the following conditions
      9       1.1  jmcneill  * are met:
     10       1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12       1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15       1.1  jmcneill  * 3. All advertising materials mentioning features or use of this software
     16       1.1  jmcneill  *    must display the following acknowledgement:
     17       1.1  jmcneill  *        This product includes software developed by Jared D. McNeill.
     18       1.1  jmcneill  * 4. Neither the name of The NetBSD Foundation nor the names of its
     19       1.1  jmcneill  *    contributors may be used to endorse or promote products derived
     20       1.1  jmcneill  *    from this software without specific prior written permission.
     21       1.1  jmcneill  *
     22       1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23       1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24       1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25       1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26       1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27       1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28       1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29       1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30       1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31       1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32       1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     33       1.1  jmcneill  */
     34       1.1  jmcneill 
     35       1.1  jmcneill /*
     36       1.1  jmcneill  * SGI MACE PS2 keyboard/mouse controller driver
     37       1.1  jmcneill  */
     38       1.1  jmcneill 
     39       1.1  jmcneill #include <sys/cdefs.h>
     40  1.2.36.1   keiichi __KERNEL_RCSID(0, "$NetBSD: macekbc.c,v 1.2.36.1 2008/03/24 07:15:03 keiichi Exp $");
     41       1.1  jmcneill 
     42       1.1  jmcneill #include <sys/param.h>
     43       1.1  jmcneill #include <sys/device.h>
     44       1.1  jmcneill #include <sys/syslog.h>
     45       1.1  jmcneill #include <sys/malloc.h>
     46       1.1  jmcneill 
     47       1.1  jmcneill #include <machine/bus.h>
     48       1.1  jmcneill #include <machine/intr.h>
     49       1.1  jmcneill 
     50       1.1  jmcneill #include <sgimips/mace/macevar.h>
     51       1.1  jmcneill 
     52       1.2  jmcneill #include <dev/arcbios/arcbios.h>
     53       1.2  jmcneill #include <dev/arcbios/arcbiosvar.h>
     54       1.1  jmcneill #include <dev/pckbport/pckbportvar.h>
     55       1.1  jmcneill 
     56       1.1  jmcneill #define	MACEKBC_TX	0x00
     57       1.1  jmcneill #define MACEKBC_RX	0x08
     58       1.1  jmcneill #define MACEKBC_CTRL	0x10
     59       1.1  jmcneill #define		MACEKBC_CTRL_TXCLKOFF	(1 << 0)
     60       1.1  jmcneill #define		MACEKBC_CTRL_TXON	(1 << 1)
     61       1.1  jmcneill #define		MACEKBC_CTRL_TXINTEN	(1 << 2)
     62       1.1  jmcneill #define		MACEKBC_CTRL_RXINTEN	(1 << 3)
     63       1.1  jmcneill #define		MACEKBC_CTRL_RXCLKON	(1 << 4)
     64       1.1  jmcneill #define		MACEKBC_CTRL_RESET	(1 << 5)
     65       1.1  jmcneill #define MACEKBC_STAT	0x18
     66       1.1  jmcneill #define		MACEKBC_STAT_TXEMPTY	(1 << 3)
     67       1.1  jmcneill #define		MACEKBC_STAT_RXFULL	(1 << 4)
     68       1.1  jmcneill 
     69       1.1  jmcneill struct macekbc_softc {
     70       1.1  jmcneill 	struct device 		sc_dev;
     71       1.1  jmcneill 	struct macekbc_internal	*sc_id;
     72       1.1  jmcneill 
     73       1.1  jmcneill 	bus_space_tag_t		sc_iot;
     74       1.1  jmcneill 	bus_space_handle_t	sc_ioh;
     75       1.1  jmcneill };
     76       1.1  jmcneill 
     77       1.1  jmcneill struct macekbc_internal {
     78       1.1  jmcneill 	struct macekbc_softc	*t_sc;
     79       1.1  jmcneill 	pckbport_tag_t 		t_pt;
     80       1.1  jmcneill 
     81       1.1  jmcneill 	bus_space_tag_t		t_iot;
     82       1.1  jmcneill 	bus_space_handle_t	t_ioh[PCKBPORT_NSLOTS];
     83       1.2  jmcneill 	int			t_present[PCKBPORT_NSLOTS];
     84       1.1  jmcneill 
     85       1.1  jmcneill 	void			*t_rxih;
     86       1.1  jmcneill };
     87       1.1  jmcneill 
     88       1.1  jmcneill static int 	macekbc_intr(void *);
     89       1.1  jmcneill static void 	macekbc_reset(struct macekbc_internal *, pckbport_slot_t);
     90       1.1  jmcneill 
     91       1.1  jmcneill static int 	macekbc_xt_translation(void *, pckbport_slot_t, int);
     92       1.1  jmcneill static int 	macekbc_send_devcmd(void *, pckbport_slot_t, u_char);
     93       1.1  jmcneill static int 	macekbc_poll_data1(void *, pckbport_slot_t);
     94       1.1  jmcneill static void 	macekbc_slot_enable(void *, pckbport_slot_t, int);
     95       1.1  jmcneill static void 	macekbc_intr_establish(void *, pckbport_slot_t);
     96       1.1  jmcneill static void 	macekbc_set_poll(void *, pckbport_slot_t, int);
     97       1.1  jmcneill 
     98       1.1  jmcneill static int	macekbc_match(struct device *, struct cfdata *, void *);
     99       1.1  jmcneill static void 	macekbc_attach(struct device *, struct device *, void *);
    100       1.1  jmcneill 
    101       1.1  jmcneill CFATTACH_DECL(macekbc, sizeof(struct macekbc_softc),
    102       1.1  jmcneill     macekbc_match, macekbc_attach, NULL, NULL);
    103       1.1  jmcneill 
    104       1.1  jmcneill static struct pckbport_accessops macekbc_ops = {
    105       1.1  jmcneill 	.t_xt_translation 	= macekbc_xt_translation,
    106       1.1  jmcneill 	.t_send_devcmd 		= macekbc_send_devcmd,
    107       1.1  jmcneill 	.t_poll_data1 		= macekbc_poll_data1,
    108       1.1  jmcneill 	.t_slot_enable 		= macekbc_slot_enable,
    109       1.1  jmcneill 	.t_intr_establish 	= macekbc_intr_establish,
    110       1.1  jmcneill 	.t_set_poll 		= macekbc_set_poll,
    111       1.1  jmcneill };
    112       1.1  jmcneill 
    113       1.1  jmcneill static int
    114       1.1  jmcneill macekbc_match(struct device *parent, struct cfdata *match, void *aux)
    115       1.1  jmcneill {
    116       1.2  jmcneill 
    117       1.1  jmcneill 	return 1;
    118       1.1  jmcneill }
    119       1.1  jmcneill 
    120       1.1  jmcneill static void
    121       1.1  jmcneill macekbc_attach(struct device *parent, struct device *self, void *aux)
    122       1.1  jmcneill {
    123       1.1  jmcneill 	struct mace_attach_args *maa;
    124       1.1  jmcneill 	struct macekbc_softc *sc;
    125       1.1  jmcneill 	struct macekbc_internal *t;
    126       1.2  jmcneill 	int slot;
    127  1.2.36.1   keiichi 	const char *consdev;
    128       1.1  jmcneill 
    129       1.1  jmcneill 	maa = aux;
    130       1.1  jmcneill 	sc = device_private(self);
    131       1.1  jmcneill 
    132       1.1  jmcneill 	aprint_normal(": PS2 controller\n");
    133       1.1  jmcneill 	aprint_naive("\n");
    134       1.1  jmcneill 
    135       1.1  jmcneill 	t = malloc(sizeof(struct macekbc_internal), M_DEVBUF, M_NOWAIT|M_ZERO);
    136       1.2  jmcneill 	if (t == NULL) {
    137       1.2  jmcneill 		aprint_error("%s: not enough memory\n", device_xname(self));
    138       1.2  jmcneill 		return;
    139       1.2  jmcneill 	}
    140       1.1  jmcneill 	t->t_iot = maa->maa_st;
    141       1.2  jmcneill 	for (slot = 0; slot < PCKBPORT_NSLOTS; slot++)
    142       1.2  jmcneill 		t->t_present[slot] = 0;
    143       1.1  jmcneill 	if (bus_space_subregion(t->t_iot, maa->maa_sh, maa->maa_offset,
    144       1.1  jmcneill 	    0, &t->t_ioh[PCKBPORT_KBD_SLOT]) != 0) {
    145       1.1  jmcneill 		aprint_error("%s: couldn't map kbd registers\n",
    146       1.1  jmcneill 		    device_xname(self));
    147       1.1  jmcneill 		return;
    148       1.1  jmcneill 	}
    149       1.1  jmcneill 	if (bus_space_subregion(t->t_iot, maa->maa_sh, maa->maa_offset + 32,
    150       1.1  jmcneill 	    0, &t->t_ioh[PCKBPORT_AUX_SLOT]) != 0) {
    151       1.1  jmcneill 		aprint_error("%s: couldn't map aux registers\n",
    152       1.1  jmcneill 		    device_xname(self));
    153       1.1  jmcneill 		return;
    154       1.1  jmcneill 	}
    155       1.1  jmcneill 
    156       1.1  jmcneill 	if ((t->t_rxih = cpu_intr_establish(maa->maa_intr, maa->maa_intrmask,
    157       1.1  jmcneill 	    macekbc_intr, t)) == NULL) {
    158       1.1  jmcneill 		printf("%s: couldn't establish interrupt\n",
    159       1.1  jmcneill 		    device_xname(self));
    160       1.1  jmcneill 		return;
    161       1.1  jmcneill 	}
    162       1.1  jmcneill 	sc->sc_id = t;
    163       1.1  jmcneill 	t->t_sc = sc;
    164       1.1  jmcneill 
    165       1.1  jmcneill 	macekbc_reset(t, PCKBPORT_KBD_SLOT);
    166       1.1  jmcneill 	macekbc_reset(t, PCKBPORT_AUX_SLOT);
    167       1.1  jmcneill 
    168  1.2.36.1   keiichi 	consdev = ARCBIOS->GetEnvironmentVariable("ConsoleIn");
    169  1.2.36.1   keiichi 	if (consdev != NULL && strcmp(consdev, "keyboard()") == 0)
    170  1.2.36.1   keiichi 		pckbport_cnattach(t, &macekbc_ops, PCKBPORT_KBD_SLOT);
    171  1.2.36.1   keiichi 
    172       1.1  jmcneill 	t->t_pt = pckbport_attach(t, &macekbc_ops);
    173       1.2  jmcneill 	if (pckbport_attach_slot(&sc->sc_dev, t->t_pt, PCKBPORT_KBD_SLOT))
    174       1.2  jmcneill 		t->t_present[PCKBPORT_KBD_SLOT] = 1;
    175       1.2  jmcneill 	if (pckbport_attach_slot(&sc->sc_dev, t->t_pt, PCKBPORT_AUX_SLOT))
    176       1.2  jmcneill 		t->t_present[PCKBPORT_AUX_SLOT] = 1;
    177       1.1  jmcneill 
    178       1.1  jmcneill 	return;
    179       1.1  jmcneill }
    180       1.1  jmcneill 
    181       1.1  jmcneill static int
    182       1.1  jmcneill macekbc_intr(void *opaque)
    183       1.1  jmcneill {
    184       1.1  jmcneill 	struct macekbc_internal	*t;
    185       1.1  jmcneill 	bus_space_tag_t iot;
    186       1.1  jmcneill 	bus_space_handle_t ioh;
    187       1.1  jmcneill 	uint64_t stat, val;
    188       1.1  jmcneill 	pckbport_slot_t slot;
    189       1.1  jmcneill 	int rv;
    190       1.1  jmcneill 
    191       1.1  jmcneill 	t = opaque;
    192       1.1  jmcneill 	iot = t->t_iot;
    193       1.1  jmcneill 	rv = 0;
    194       1.1  jmcneill 
    195       1.1  jmcneill 	for (slot = 0; slot < PCKBPORT_NSLOTS; slot++) {
    196       1.2  jmcneill 		if (t->t_present[slot] == 0)
    197       1.2  jmcneill 			continue;
    198       1.2  jmcneill 
    199       1.1  jmcneill 		ioh = t->t_ioh[slot];
    200       1.1  jmcneill 		stat = bus_space_read_8(iot, ioh, MACEKBC_STAT);
    201       1.1  jmcneill 		if (stat & MACEKBC_STAT_RXFULL) {
    202       1.1  jmcneill 			val = bus_space_read_8(iot, ioh, MACEKBC_RX);
    203       1.1  jmcneill 			pckbportintr(t->t_pt, slot, val & 0xff);
    204       1.1  jmcneill 			rv = 1;
    205       1.1  jmcneill 		}
    206       1.1  jmcneill 	}
    207       1.1  jmcneill 
    208  1.2.36.1   keiichi 	return rv;
    209       1.1  jmcneill }
    210       1.1  jmcneill 
    211       1.1  jmcneill static void
    212       1.1  jmcneill macekbc_reset(struct macekbc_internal *t, pckbport_slot_t slot)
    213       1.1  jmcneill {
    214       1.1  jmcneill 	bus_space_tag_t iot;
    215       1.1  jmcneill 	bus_space_handle_t ioh;
    216       1.1  jmcneill 	uint64_t val;
    217       1.1  jmcneill 
    218       1.1  jmcneill 	iot = t->t_iot;
    219       1.1  jmcneill 	ioh = t->t_ioh[slot];
    220       1.1  jmcneill 
    221       1.1  jmcneill 	val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
    222       1.1  jmcneill 	val |= MACEKBC_CTRL_TXCLKOFF | MACEKBC_CTRL_RESET;
    223       1.1  jmcneill 	bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
    224       1.1  jmcneill 
    225       1.1  jmcneill 	delay(10000);
    226       1.1  jmcneill 
    227       1.1  jmcneill 	val &= ~(MACEKBC_CTRL_TXCLKOFF | MACEKBC_CTRL_RESET);
    228       1.1  jmcneill 	val |= MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON | MACEKBC_CTRL_RXINTEN;
    229       1.1  jmcneill 	bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
    230       1.1  jmcneill 
    231       1.1  jmcneill 	return;
    232       1.1  jmcneill }
    233       1.1  jmcneill 
    234       1.1  jmcneill static int
    235       1.1  jmcneill macekbc_wait(struct macekbc_internal *t, pckbport_slot_t slot,
    236       1.1  jmcneill     uint64_t mask, bool set)
    237       1.1  jmcneill {
    238       1.1  jmcneill 	bus_space_tag_t iot;
    239       1.1  jmcneill 	bus_space_handle_t ioh;
    240       1.1  jmcneill 	uint64_t val, tmp;
    241       1.1  jmcneill 	int timeout;
    242       1.1  jmcneill 
    243       1.1  jmcneill 	iot = t->t_iot;
    244       1.1  jmcneill 	ioh = t->t_ioh[slot];
    245       1.1  jmcneill 	val = (set ? mask : 0);
    246       1.2  jmcneill 	timeout = 1000;
    247       1.1  jmcneill 
    248       1.1  jmcneill 	while (timeout-- > 0) {
    249       1.1  jmcneill 		tmp = bus_space_read_8(iot, ioh, MACEKBC_STAT);
    250       1.1  jmcneill 		if ((tmp & mask) == val)
    251       1.1  jmcneill 			return 0;
    252       1.1  jmcneill 		delay(10);
    253       1.1  jmcneill 	}
    254       1.1  jmcneill 
    255       1.1  jmcneill 	return 1;
    256       1.1  jmcneill }
    257       1.1  jmcneill 
    258       1.1  jmcneill static int
    259       1.1  jmcneill macekbc_xt_translation(void *opaque, pckbport_slot_t port, int on)
    260       1.1  jmcneill {
    261       1.1  jmcneill 
    262       1.1  jmcneill 	if (on)
    263       1.1  jmcneill 		return 0;
    264       1.1  jmcneill 
    265       1.1  jmcneill 	return 1;
    266       1.1  jmcneill }
    267       1.1  jmcneill 
    268       1.1  jmcneill static int
    269       1.1  jmcneill macekbc_send_devcmd(void *opaque, pckbport_slot_t slot, u_char byte)
    270       1.1  jmcneill {
    271       1.1  jmcneill 	struct macekbc_internal	*t;
    272       1.1  jmcneill 	bus_space_tag_t iot;
    273       1.1  jmcneill 	bus_space_handle_t ioh;
    274       1.1  jmcneill 
    275       1.1  jmcneill 	t = opaque;
    276       1.1  jmcneill 	iot = t->t_iot;
    277       1.1  jmcneill 	ioh = t->t_ioh[slot];
    278       1.1  jmcneill 
    279       1.1  jmcneill 	if (macekbc_wait(t, slot, MACEKBC_STAT_TXEMPTY, 1))
    280       1.1  jmcneill 		return 0;
    281       1.1  jmcneill 
    282       1.1  jmcneill 	bus_space_write_8(iot, ioh, MACEKBC_TX, byte & 0xff);
    283       1.1  jmcneill 
    284       1.1  jmcneill 	return 1;
    285       1.1  jmcneill }
    286       1.1  jmcneill 
    287       1.1  jmcneill static int
    288       1.1  jmcneill macekbc_poll_data1(void *opaque, pckbport_slot_t slot)
    289       1.1  jmcneill {
    290       1.1  jmcneill 	struct macekbc_internal	*t;
    291       1.1  jmcneill 	bus_space_tag_t iot;
    292       1.1  jmcneill 	bus_space_handle_t ioh;
    293       1.1  jmcneill 
    294       1.1  jmcneill 	t = opaque;
    295       1.1  jmcneill 	iot = t->t_iot;
    296       1.1  jmcneill 	ioh = t->t_ioh[slot];
    297       1.1  jmcneill 
    298       1.1  jmcneill 	if (macekbc_wait(t, slot, MACEKBC_STAT_RXFULL, 1)) /* rx full */
    299       1.1  jmcneill 		return -1;
    300       1.1  jmcneill 
    301       1.1  jmcneill 	return bus_space_read_8(iot, ioh, MACEKBC_RX) & 0xff;
    302       1.1  jmcneill }
    303       1.1  jmcneill 
    304       1.1  jmcneill static void
    305       1.1  jmcneill macekbc_slot_enable(void *opaque, pckbport_slot_t slot, int on)
    306       1.1  jmcneill {
    307       1.1  jmcneill 	struct macekbc_internal *t;
    308       1.1  jmcneill 	bus_space_tag_t iot;
    309       1.1  jmcneill 	bus_space_handle_t ioh;
    310       1.1  jmcneill 	uint64_t val;
    311       1.1  jmcneill 
    312       1.1  jmcneill 	t = opaque;
    313       1.1  jmcneill 	iot = t->t_iot;
    314       1.1  jmcneill 	ioh = t->t_ioh[slot];
    315       1.1  jmcneill 
    316       1.1  jmcneill 	val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
    317       1.1  jmcneill 	if (on)
    318       1.1  jmcneill 		val |= MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON;
    319       1.1  jmcneill 	else
    320       1.1  jmcneill 		val &= ~(MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON);
    321       1.1  jmcneill 	bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
    322       1.1  jmcneill 
    323       1.1  jmcneill 	return;
    324       1.1  jmcneill }
    325       1.1  jmcneill 
    326       1.1  jmcneill static void
    327       1.1  jmcneill macekbc_intr_establish(void *opaque, pckbport_slot_t slot)
    328       1.1  jmcneill {
    329       1.1  jmcneill 
    330       1.1  jmcneill 	/* XXX */
    331       1.1  jmcneill 	macekbc_set_poll(opaque, slot, 0);
    332       1.1  jmcneill 
    333       1.1  jmcneill 	return;
    334       1.1  jmcneill }
    335       1.1  jmcneill 
    336       1.1  jmcneill static void
    337       1.1  jmcneill macekbc_set_poll(void *opaque, pckbport_slot_t slot, int on)
    338       1.1  jmcneill {
    339       1.1  jmcneill 	struct macekbc_internal *t;
    340       1.1  jmcneill 	bus_space_tag_t iot;
    341       1.1  jmcneill 	bus_space_handle_t ioh;
    342       1.1  jmcneill 	uint64_t val;
    343       1.1  jmcneill 
    344       1.1  jmcneill 	t = opaque;
    345       1.1  jmcneill 	iot = t->t_iot;
    346       1.1  jmcneill 	ioh = t->t_ioh[slot];
    347       1.1  jmcneill 
    348       1.1  jmcneill 	val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
    349       1.1  jmcneill 	if (on)
    350       1.1  jmcneill 		val &= ~MACEKBC_CTRL_RXINTEN;
    351       1.1  jmcneill 	else
    352       1.1  jmcneill 		val |= MACEKBC_CTRL_RXINTEN;
    353       1.1  jmcneill 	bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
    354       1.1  jmcneill 
    355       1.1  jmcneill 	return;
    356       1.1  jmcneill }
    357