Home | History | Annotate | Line # | Download | only in iomd
      1 /* $NetBSD: iomdkbc.c,v 1.7 2020/11/20 18:18:51 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2004 Ben Harris
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: iomdkbc.c,v 1.7 2020/11/20 18:18:51 thorpej Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/device.h>
     35 #include <sys/kmem.h>
     36 #include <sys/systm.h>
     37 
     38 #include <dev/pckbport/pckbportvar.h>
     39 
     40 #include <sys/bus.h>
     41 #include <machine/intr.h>
     42 
     43 #include <arch/arm/iomd/iomdreg.h>
     44 #include <arch/arm/iomd/iomdvar.h>
     45 #include <arch/arm/iomd/iomdkbcvar.h>
     46 
     47 /* XXX belongs in iomdreg.h */
     48 #define IOMDKBC_TXE	0x80
     49 #define IOMDKBC_TXB	0x40
     50 #define IOMDKBC_RXF	0x20
     51 #define IOMDKBC_RXB	0x10
     52 #define IOMDKBC_ENA	0x08
     53 #define IOMDKBC_RXP	0x04
     54 #define IOMDKBC_DATA	0x02
     55 #define IOMDKBC_CLK	0x01
     56 
     57 #define IOMDKBC_DR	0x00
     58 #define IOMDKBC_CR	0x01
     59 
     60 #define KBC_DEVCMD_ACK 0xfa
     61 #define KBC_DEVCMD_RESEND 0xfe
     62 
     63 struct iomdkbc_softc {
     64 	device_t		sc_dev;
     65 	struct iomdkbc_internal *sc_id;
     66 };
     67 
     68 struct iomdkbc_internal {
     69 	struct iomdkbc_softc	*t_sc;
     70 	struct pckbport_tag	*t_pt;
     71 
     72 	int			t_haveport[PCKBPORT_NSLOTS];
     73 	bus_space_tag_t		t_iot;
     74 	bus_space_handle_t	t_ioh[PCKBPORT_NSLOTS];
     75 
     76 	void	*t_rxih[PCKBPORT_NSLOTS];
     77 	int	t_rxirq[PCKBPORT_NSLOTS];
     78 };
     79 
     80 static int iomdkbc_match(device_t , cfdata_t , void *);
     81 static void iomdkbc_attach(device_t , device_t , void *);
     82 
     83 static int iomdkbc_xt_translation(void *, pckbport_slot_t, int);
     84 static int iomdkbc_send_devcmd(void *, pckbport_slot_t, u_char);
     85 static int iomdkbc_poll_data1(void *, pckbport_slot_t);
     86 static void iomdkbc_slot_enable(void *, pckbport_slot_t, int);
     87 static void iomdkbc_intr_establish(void *, pckbport_slot_t);
     88 static void iomdkbc_set_poll(void *, pckbport_slot_t, int);
     89 
     90 static int iomdkbc_intr(void *);
     91 
     92 CFATTACH_DECL_NEW(iomdkbc, sizeof(struct iomdkbc_softc),
     93     iomdkbc_match, iomdkbc_attach, NULL, NULL);
     94 
     95 static struct pckbport_accessops const iomdkbc_ops = {
     96 	iomdkbc_xt_translation,
     97 	iomdkbc_send_devcmd,
     98 	iomdkbc_poll_data1,
     99 	iomdkbc_slot_enable,
    100 	iomdkbc_intr_establish,
    101 	iomdkbc_set_poll
    102 };
    103 
    104 static struct iomdkbc_internal iomdkbc_cntag;
    105 
    106 static int
    107 iomdkbc_match(device_t parent, cfdata_t cf, void *aux)
    108 {
    109 	struct kbd_attach_args *ka = aux;
    110 	struct opms_attach_args *pa = aux;
    111 
    112 	if (strcmp(ka->ka_name, "kbd") == 0 ||
    113 	    strcmp(pa->pa_name, "opms") == 0)
    114 		return 1;
    115 	return 0;
    116 }
    117 
    118 static void
    119 iomdkbc_attach(device_t parent, device_t self, void *aux)
    120 {
    121 	struct kbd_attach_args *ka = aux;
    122 	struct opms_attach_args *pa = aux;
    123 	struct iomdkbc_softc *sc = device_private(self);
    124 	struct iomdkbc_internal *t;
    125 
    126 	sc->sc_dev = self;
    127 	aprint_normal("\n");
    128 
    129 	t = NULL;
    130 	if (strcmp(ka->ka_name, "kbd") == 0) {
    131 		/* XXX not really right, but good enough. */
    132 		if (iomdkbc_cntag.t_haveport[PCKBPORT_KBD_SLOT]) {
    133 			/* Have an iomdkbc as console.  Assume it's this one.*/
    134 			t = &iomdkbc_cntag;
    135 		} else {
    136 			t = kmem_zalloc(sizeof(struct iomdkbc_internal),
    137 			    KM_SLEEP);
    138 			t->t_haveport[PCKBPORT_KBD_SLOT] = 1;
    139 			t->t_iot = ka->ka_iot;
    140 			t->t_ioh[PCKBPORT_KBD_SLOT] = ka->ka_ioh;
    141 		}
    142 		t->t_rxih[PCKBPORT_KBD_SLOT] = intr_claim(ka->ka_rxirq,
    143 		    IPL_TTY, device_xname(sc->sc_dev), iomdkbc_intr, t);
    144 		t->t_rxirq[PCKBPORT_KBD_SLOT] = ka->ka_rxirq;
    145 		disable_irq(t->t_rxirq[PCKBPORT_KBD_SLOT]);
    146 		sc->sc_id = t;
    147 		t->t_sc = sc;
    148 		t->t_pt = pckbport_attach(t, &iomdkbc_ops);
    149 		pckbport_attach_slot(sc->sc_dev, t->t_pt, PCKBPORT_KBD_SLOT);
    150 	}
    151 
    152 	if (strcmp(pa->pa_name, "opms") == 0) {
    153 		if (t == NULL) {
    154 			t = kmem_zalloc(sizeof(struct iomdkbc_internal),
    155 			    KM_SLEEP);
    156 		}
    157 		t->t_haveport[PCKBPORT_AUX_SLOT] = 1;
    158 		t->t_iot = pa->pa_iot;
    159 		t->t_ioh[PCKBPORT_AUX_SLOT] = pa->pa_ioh;
    160 		t->t_rxih[PCKBPORT_AUX_SLOT] = intr_claim(pa->pa_irq,
    161 		    IPL_TTY, device_xname(sc->sc_dev), iomdkbc_intr, t);
    162 		t->t_rxirq[PCKBPORT_AUX_SLOT] = pa->pa_irq;
    163 		disable_irq(t->t_rxirq[PCKBPORT_AUX_SLOT]);
    164 		sc->sc_id = t;
    165 		t->t_sc = sc;
    166 		if (t->t_pt == NULL)
    167 			t->t_pt = pckbport_attach(t, &iomdkbc_ops);
    168 		pckbport_attach_slot(sc->sc_dev, t->t_pt, PCKBPORT_AUX_SLOT);
    169 	}
    170 }
    171 
    172 static int
    173 iomdkbc_send_devcmd(void *cookie, pckbport_slot_t slot, u_char cmd)
    174 {
    175 	struct iomdkbc_internal *t = cookie;
    176 	bus_space_tag_t iot = t->t_iot;
    177 	bus_space_handle_t ioh = t->t_ioh[slot];
    178 	int timeout;
    179 
    180 	timeout = 10000;
    181 	while ((bus_space_read_1(iot, ioh, IOMDKBC_CR) &
    182 		   IOMDKBC_TXE) == 0) {
    183 		DELAY(10);
    184 		if (--timeout == 0) return 0;
    185 	}
    186 
    187         bus_space_write_1(iot, ioh, IOMDKBC_DR, cmd);
    188 	return 1;
    189 }
    190 
    191 static int
    192 iomdkbc_poll_data1(void *cookie, pckbport_slot_t slot)
    193 {
    194 	struct iomdkbc_internal *t = cookie;
    195 	bus_space_tag_t iot = t->t_iot;
    196 	bus_space_handle_t ioh = t->t_ioh[slot];
    197 	int timeout;
    198 
    199 	timeout = 10000;
    200 	while ((bus_space_read_1(iot, ioh, IOMDKBC_CR) &
    201 		   IOMDKBC_RXF) == 0) {
    202 		DELAY(10);
    203 		if (--timeout == 0) return -1;
    204 	}
    205 
    206         return bus_space_read_1(iot, ioh, IOMDKBC_DR);
    207 }
    208 
    209 /*
    210  * switch scancode translation on / off
    211  * return nonzero on success
    212  */
    213 static int
    214 iomdkbc_xt_translation(void *cookie, pckbport_slot_t slot, int on)
    215 {
    216 
    217 	if (on)
    218 		return 0; /* Can't do XT translation */
    219 	else
    220 		return 1;
    221 }
    222 
    223 static void
    224 iomdkbc_slot_enable(void *cookie, pckbport_slot_t slot, int on)
    225 {
    226 	struct iomdkbc_internal *t = cookie;
    227 	bus_space_tag_t iot = t->t_iot;
    228 	bus_space_handle_t ioh = t->t_ioh[slot];
    229 
    230 	bus_space_write_1(iot, ioh, IOMDKBC_CR, on ? IOMDKBC_ENA : 0);
    231 }
    232 
    233 
    234 static void
    235 iomdkbc_intr_establish(void *cookie, pckbport_slot_t slot)
    236 {
    237 	struct iomdkbc_internal *t = cookie;
    238 
    239 	enable_irq(t->t_rxirq[slot]);
    240 }
    241 
    242 static void
    243 iomdkbc_set_poll(void *cookie, pckbport_slot_t slot, int on)
    244 {
    245 	struct iomdkbc_internal *t = cookie;
    246 
    247 	if (on)
    248 		disable_irq(t->t_rxirq[slot]);
    249 	else
    250 		enable_irq(t->t_rxirq[slot]);
    251 }
    252 
    253 static int
    254 iomdkbc_intr(void *self)
    255 {
    256 	struct iomdkbc_internal *t = self;
    257 	bus_space_tag_t iot = t->t_iot;
    258 	bus_space_handle_t ioh;
    259 	unsigned i;
    260 	int stat, ret;
    261 
    262 	ret = 0;
    263 	for (i = 0; i < PCKBPORT_NSLOTS; i++)
    264 		if (t->t_haveport[i]) {
    265 			ioh = t->t_ioh[i];
    266 			stat = bus_space_read_1(iot, ioh, IOMDKBC_CR);
    267 			if ((stat & IOMDKBC_RXF) == 0)
    268 				continue;
    269 			pckbportintr(t->t_pt, i,
    270 			    bus_space_read_1(iot, ioh, IOMDKBC_DR));
    271 			ret = 1;
    272 		}
    273 
    274 	return ret;
    275 }
    276 
    277 int
    278 iomdkbc_cnattach(bus_space_tag_t iot, bus_addr_t addr, int slot)
    279 {
    280 	struct iomdkbc_internal *t = &iomdkbc_cntag;
    281 
    282 	t->t_iot = iot;
    283 	bus_space_map(iot, addr, 2, 0, &t->t_ioh[slot]);
    284 	t->t_haveport[slot] = 1;
    285 	iomdkbc_slot_enable(t, slot, 1);
    286 	return pckbport_cnattach(t, &iomdkbc_ops, slot);
    287 }
    288