Home | History | Annotate | Line # | Download | only in ic
      1  1.2  jmcneill /* $NetBSD: pl050.c,v 1.2 2017/06/06 00:26:16 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2017 Jared 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  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: pl050.c,v 1.2 2017/06/06 00:26:16 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/systm.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/kmem.h>
     36  1.1  jmcneill #include <sys/bus.h>
     37  1.1  jmcneill 
     38  1.1  jmcneill #include <dev/pckbport/pckbportvar.h>
     39  1.1  jmcneill 
     40  1.1  jmcneill #include <dev/ic/pl050var.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #define	KMICR		0x00
     43  1.1  jmcneill #define	 KMIRXINTREN	__BIT(4)
     44  1.1  jmcneill #define	 KMIEN		__BIT(2)
     45  1.1  jmcneill #define	KMISTAT		0x04
     46  1.1  jmcneill #define	 TXEMPTY	__BIT(6)
     47  1.1  jmcneill #define	 RXFULL		__BIT(4)
     48  1.1  jmcneill #define	KMIDATA		0x08
     49  1.1  jmcneill #define	KMICLKDIV	0x0c
     50  1.1  jmcneill #define	KMIIR		0x10
     51  1.1  jmcneill #define	 KMIRXINTR	__BIT(0)
     52  1.1  jmcneill 
     53  1.1  jmcneill #define	PLKMI_READ(sc, reg)		\
     54  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     55  1.1  jmcneill #define	PLKMI_WRITE(sc, reg, val)	\
     56  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     57  1.1  jmcneill 
     58  1.1  jmcneill static int
     59  1.1  jmcneill plkmi_wait(struct plkmi_softc *sc, uint32_t mask, bool set)
     60  1.1  jmcneill {
     61  1.1  jmcneill 	int timeout = 1000;
     62  1.1  jmcneill 
     63  1.1  jmcneill 	const uint32_t val = (set ? mask : 0);
     64  1.1  jmcneill 
     65  1.1  jmcneill 	while (timeout-- > 0) {
     66  1.1  jmcneill 		const uint32_t stat = PLKMI_READ(sc, KMISTAT);
     67  1.1  jmcneill 		if ((stat & mask) == val)
     68  1.1  jmcneill 			return 0;
     69  1.1  jmcneill 		delay(10);
     70  1.1  jmcneill 	}
     71  1.1  jmcneill 
     72  1.1  jmcneill 	return ETIMEDOUT;
     73  1.1  jmcneill }
     74  1.1  jmcneill 
     75  1.1  jmcneill static int
     76  1.1  jmcneill plkmi_xt_translation(void *priv, pckbport_slot_t port, int on)
     77  1.1  jmcneill {
     78  1.1  jmcneill 	if (on)
     79  1.1  jmcneill 		return 0;
     80  1.1  jmcneill 	return 1;
     81  1.1  jmcneill }
     82  1.1  jmcneill 
     83  1.1  jmcneill static int
     84  1.1  jmcneill plkmi_send_devcmd(void *priv, pckbport_slot_t slot, u_char byte)
     85  1.1  jmcneill {
     86  1.1  jmcneill 	struct plkmi_softc * const sc = priv;
     87  1.1  jmcneill 
     88  1.1  jmcneill 	if (plkmi_wait(sc, TXEMPTY, true))
     89  1.1  jmcneill 		return 0;
     90  1.1  jmcneill 
     91  1.1  jmcneill 	PLKMI_WRITE(sc, KMIDATA, byte & 0xff);
     92  1.1  jmcneill 
     93  1.1  jmcneill 	return 1;
     94  1.1  jmcneill }
     95  1.1  jmcneill 
     96  1.1  jmcneill static int
     97  1.1  jmcneill plkmi_poll_data1(void *priv, pckbport_slot_t slot)
     98  1.1  jmcneill {
     99  1.1  jmcneill 	struct plkmi_softc * const sc = priv;
    100  1.1  jmcneill 
    101  1.1  jmcneill 	if (plkmi_wait(sc, RXFULL, true))
    102  1.1  jmcneill 		return -1;
    103  1.1  jmcneill 
    104  1.1  jmcneill 	return PLKMI_READ(sc, KMIDATA) & 0xff;
    105  1.1  jmcneill }
    106  1.1  jmcneill 
    107  1.1  jmcneill static void
    108  1.1  jmcneill plkmi_slot_enable(void *priv, pckbport_slot_t slot, int on)
    109  1.1  jmcneill {
    110  1.1  jmcneill 	struct plkmi_softc * const sc = priv;
    111  1.1  jmcneill 	uint32_t cr;
    112  1.1  jmcneill 
    113  1.1  jmcneill 	cr = PLKMI_READ(sc, KMICR);
    114  1.1  jmcneill 	if (on)
    115  1.1  jmcneill 		cr |= KMIEN;
    116  1.1  jmcneill 	else
    117  1.1  jmcneill 		cr &= ~KMIEN;
    118  1.1  jmcneill 	PLKMI_WRITE(sc, KMICR, cr);
    119  1.1  jmcneill }
    120  1.1  jmcneill 
    121  1.1  jmcneill static void
    122  1.1  jmcneill plkmi_set_poll(void *priv, pckbport_slot_t slot, int on)
    123  1.1  jmcneill {
    124  1.1  jmcneill 	struct plkmi_softc * const sc = priv;
    125  1.1  jmcneill 	uint32_t cr;
    126  1.1  jmcneill 
    127  1.1  jmcneill 	cr = PLKMI_READ(sc, KMICR);
    128  1.1  jmcneill 	if (on)
    129  1.1  jmcneill 		cr &= ~KMIRXINTREN;
    130  1.1  jmcneill 	else
    131  1.1  jmcneill 		cr |= KMIRXINTREN;
    132  1.1  jmcneill 	PLKMI_WRITE(sc, KMICR, cr);
    133  1.1  jmcneill }
    134  1.1  jmcneill 
    135  1.1  jmcneill static void
    136  1.1  jmcneill plkmi_intr_establish(void *priv, pckbport_slot_t slot)
    137  1.1  jmcneill {
    138  1.1  jmcneill 	/* XXX */
    139  1.1  jmcneill 	plkmi_set_poll(priv, slot, 0);
    140  1.1  jmcneill }
    141  1.1  jmcneill 
    142  1.1  jmcneill static struct pckbport_accessops plkmi_ops = {
    143  1.1  jmcneill 	.t_xt_translation = plkmi_xt_translation,
    144  1.1  jmcneill 	.t_send_devcmd = plkmi_send_devcmd,
    145  1.1  jmcneill 	.t_poll_data1 = plkmi_poll_data1,
    146  1.1  jmcneill 	.t_slot_enable = plkmi_slot_enable,
    147  1.1  jmcneill 	.t_set_poll = plkmi_set_poll,
    148  1.1  jmcneill 	.t_intr_establish = plkmi_intr_establish,
    149  1.1  jmcneill };
    150  1.1  jmcneill 
    151  1.1  jmcneill void
    152  1.1  jmcneill plkmi_attach(struct plkmi_softc *sc)
    153  1.1  jmcneill {
    154  1.1  jmcneill 	aprint_naive("\n");
    155  1.1  jmcneill 	aprint_normal(": PS2 controller\n");
    156  1.1  jmcneill 
    157  1.1  jmcneill 	sc->sc_pt = pckbport_attach(sc, &plkmi_ops);
    158  1.1  jmcneill 	sc->sc_slot = -1;
    159  1.1  jmcneill 
    160  1.1  jmcneill 	PLKMI_WRITE(sc, KMICR, KMIEN);
    161  1.1  jmcneill 
    162  1.1  jmcneill 	for (int slot = 0; slot < PCKBPORT_NSLOTS; slot++)
    163  1.1  jmcneill 		if (pckbport_attach_slot(sc->sc_dev, sc->sc_pt, slot)) {
    164  1.1  jmcneill 			sc->sc_slot = slot;
    165  1.1  jmcneill 			break;
    166  1.1  jmcneill 		}
    167  1.2  jmcneill 
    168  1.2  jmcneill 	if (sc->sc_slot == PCKBPORT_KBD_SLOT)
    169  1.2  jmcneill 		pckbport_cnattach(sc, &plkmi_ops, sc->sc_slot);
    170  1.1  jmcneill }
    171  1.1  jmcneill 
    172  1.1  jmcneill int
    173  1.1  jmcneill plkmi_intr(void *priv)
    174  1.1  jmcneill {
    175  1.1  jmcneill 	struct plkmi_softc * const sc = priv;
    176  1.1  jmcneill 	int handled = 0;
    177  1.1  jmcneill 
    178  1.1  jmcneill 	if (sc->sc_slot == -1)
    179  1.1  jmcneill 		return 0;
    180  1.1  jmcneill 
    181  1.1  jmcneill 	const uint32_t stat = PLKMI_READ(sc, KMISTAT);
    182  1.1  jmcneill 	if (stat & RXFULL) {
    183  1.1  jmcneill 		const uint32_t val = PLKMI_READ(sc, KMIDATA);
    184  1.1  jmcneill 		pckbportintr(sc->sc_pt, sc->sc_slot, val & 0xff);
    185  1.1  jmcneill 		handled = 1;
    186  1.1  jmcneill 	}
    187  1.1  jmcneill 
    188  1.1  jmcneill 	return handled;
    189  1.1  jmcneill }
    190