Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: kb_hb.c,v 1.17 2026/09/20 10:29:11 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 Izumi Tsutsui.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: kb_hb.c,v 1.17 2026/09/20 10:29:11 tsutsui Exp $");
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 #include <sys/device.h>
     33 
     34 #include <machine/bus.h>
     35 #include <machine/cpu.h>
     36 
     37 #include <dev/wscons/wsconsio.h>
     38 #include <dev/wscons/wskbdvar.h>
     39 #include <dev/wscons/wsksymdef.h>
     40 #include <dev/wscons/wsksymvar.h>
     41 
     42 #include <news68k/dev/hbvar.h>
     43 #include <news68k/dev/kb_hbreg.h>
     44 #include <news68k/dev/kbvar.h>
     45 
     46 #include <news68k/news68k/isr.h>
     47 
     48 #include "ioconf.h"
     49 
     50 #define KB_SIZE 0x10 /* XXX */
     51 #define KB_PRI 5
     52 
     53 #define NEWS1200_KB_BASE	((volatile uint8_t *)0xe1240000)
     54 
     55 static int kb_hb_match(device_t, cfdata_t, void *);
     56 static void kb_hb_attach(device_t, device_t, void *);
     57 static void kb_hb_init(struct kb_softc *);
     58 int	kb_hb_intr(void *);
     59 int	kb_hb_cnattach(void);
     60 
     61 CFATTACH_DECL_NEW(kb_hb, sizeof(struct kb_softc),
     62     kb_hb_match, kb_hb_attach, NULL, NULL);
     63 
     64 struct console_softc kb_hb_conssc;
     65 
     66 static int
     67 kb_hb_match(device_t parent, cfdata_t cf, void *aux)
     68 {
     69 	struct hb_attach_args *ha = aux;
     70 	u_int addr;
     71 
     72 	if (strcmp(ha->ha_name, "kb"))
     73 		return 0;
     74 
     75 	/* XXX no default address */
     76 	if (ha->ha_address == (u_int)-1)
     77 		return 0;
     78 
     79 	addr = (ha->ha_address); /* XXX */
     80 
     81 	if (badbaddr((void *)addr))
     82 		return 0;
     83 
     84 	return 1;
     85 }
     86 
     87 static void
     88 kb_hb_attach(device_t parent, device_t self, void *aux)
     89 {
     90 	struct kb_softc *sc = device_private(self);
     91 	struct hb_attach_args *ha = aux;
     92 	bus_space_tag_t bt = ha->ha_bust;
     93 	bus_space_handle_t bh;
     94 	struct wskbddev_attach_args wsa;
     95 	int ipl;
     96 
     97 	sc->sc_dev = self;
     98 
     99 	if (bus_space_map(bt, ha->ha_address, KB_SIZE, 0, &bh) != 0) {
    100 		aprint_error(": can't map device space\n");
    101 		return;
    102 	}
    103 
    104 	aprint_normal("\n");
    105 
    106 	sc->sc_bt = bt;
    107 	sc->sc_bh = bh;
    108 	sc->sc_offset = KB_REG_DATA;
    109 	sc->sc_conssc = &kb_hb_conssc;
    110 
    111 	ipl = ha->ha_ipl;
    112 	if (ipl == -1)
    113 		ipl = KB_PRI;
    114 
    115 	kb_hb_init(sc);
    116 
    117 	isrlink_autovec(kb_hb_intr, (void *)sc, ipl, IPL_TTY);
    118 
    119 	wsa.console = kb_hb_conssc.cs_isconsole;
    120 	wsa.keymap = &kb_keymapdata;
    121 	wsa.accessops = &kb_accessops;
    122 	wsa.accesscookie = sc;
    123 
    124 	sc->sc_wskbddev = config_found(self, &wsa, wskbddevprint, CFARGS_NONE);
    125 }
    126 
    127 static void
    128 kb_hb_init(struct kb_softc *sc)
    129 {
    130 	bus_space_tag_t bt = sc->sc_bt;
    131 	bus_space_handle_t bh = sc->sc_bh;
    132 
    133 	bus_space_write_1(bt, bh, KB_REG_RESET, 0);
    134 	bus_space_write_1(bt, bh, KB_REG_INTE, KB_INTE);
    135 }
    136 
    137 int
    138 kb_hb_intr(void *arg)
    139 {
    140 	struct kb_softc *sc = (struct kb_softc *)arg;
    141 	struct console_softc *kb_conssc = sc->sc_conssc;
    142 	bus_space_tag_t bt = sc->sc_bt;
    143 	bus_space_handle_t bh = sc->sc_bh;
    144 	int stat, handled = 0;
    145 
    146 	kb_conssc->cs_nintr++;
    147 
    148 	stat = bus_space_read_1(bt, bh, KB_REG_STAT);
    149 	if (stat & KBSTAT_RDY) {
    150 		kb_intr(sc);
    151 		handled = 1;
    152 		bus_space_write_1(bt, bh, KB_REG_INTE, KB_INTE);
    153 	}
    154 
    155 	return handled;
    156 }
    157 
    158 int
    159 kb_hb_cnattach(void)
    160 {
    161 
    162 	kb_hb_conssc.cs_data = NEWS1200_KB_BASE + KB_REG_DATA;
    163 	kb_hb_conssc.cs_stat = NEWS1200_KB_BASE + KB_REG_STAT;
    164 	kb_hb_conssc.cs_inte = NEWS1200_KB_BASE + KB_REG_INTE;
    165 
    166 	return kb_cnattach(&kb_hb_conssc);
    167 }
    168