1/*	$NetBSD: kb_hb.c,v 1.15 2021/08/07 16:19:00 thorpej 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.15 2021/08/07 16:19:00 thorpej 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
53static int kb_hb_match(device_t, cfdata_t, void *);
54static void kb_hb_attach(device_t, device_t, void *);
55static void kb_hb_init(struct kb_softc *);
56int	kb_hb_intr(void *);
57int	kb_hb_cnattach(void);
58
59CFATTACH_DECL_NEW(kb_hb, sizeof(struct kb_softc),
60    kb_hb_match, kb_hb_attach, NULL, NULL);
61
62struct console_softc kb_hb_conssc;
63
64static int
65kb_hb_match(device_t parent, cfdata_t cf, void *aux)
66{
67	struct hb_attach_args *ha = aux;
68	u_int addr;
69
70	if (strcmp(ha->ha_name, "kb"))
71		return 0;
72
73	/* XXX no default address */
74	if (ha->ha_address == (u_int)-1)
75		return 0;
76
77	addr = (ha->ha_address); /* XXX */
78
79	if (badaddr((void *)addr, 1))
80		return 0;
81
82	return 1;
83}
84
85static void
86kb_hb_attach(device_t parent, device_t self, void *aux)
87{
88	struct kb_softc *sc = device_private(self);
89	struct hb_attach_args *ha = aux;
90	bus_space_tag_t bt = ha->ha_bust;
91	bus_space_handle_t bh;
92	struct wskbddev_attach_args wsa;
93	int ipl;
94
95	sc->sc_dev = self;
96
97	if (bus_space_map(bt, ha->ha_address, KB_SIZE, 0, &bh) != 0) {
98		aprint_error(": can't map device space\n");
99		return;
100	}
101
102	aprint_normal("\n");
103
104	sc->sc_bt = bt;
105	sc->sc_bh = bh;
106	sc->sc_offset = KB_REG_DATA;
107	sc->sc_conssc = &kb_hb_conssc;
108
109	ipl = ha->ha_ipl;
110	if (ipl == -1)
111		ipl = KB_PRI;
112
113	kb_hb_init(sc);
114
115	isrlink_autovec(kb_hb_intr, (void *)sc, ipl, IPL_TTY);
116
117	wsa.console = kb_hb_conssc.cs_isconsole;
118	wsa.keymap = &kb_keymapdata;
119	wsa.accessops = &kb_accessops;
120	wsa.accesscookie = sc;
121
122	sc->sc_wskbddev = config_found(self, &wsa, wskbddevprint, CFARGS_NONE);
123}
124
125static void
126kb_hb_init(struct kb_softc *sc)
127{
128	bus_space_tag_t bt = sc->sc_bt;
129	bus_space_handle_t bh = sc->sc_bh;
130
131	bus_space_write_1(bt, bh, KB_REG_RESET, 0);
132	bus_space_write_1(bt, bh, KB_REG_INTE, KB_INTE);
133}
134
135int
136kb_hb_intr(void *arg)
137{
138	struct kb_softc *sc = (struct kb_softc *)arg;
139	struct console_softc *kb_conssc = sc->sc_conssc;
140	bus_space_tag_t bt = sc->sc_bt;
141	bus_space_handle_t bh = sc->sc_bh;
142	int stat, handled = 0;
143
144	kb_conssc->cs_nintr++;
145
146	stat = bus_space_read_1(bt, bh, KB_REG_STAT);
147	if (stat & KBSTAT_RDY) {
148		kb_intr(sc);
149		handled = 1;
150		bus_space_write_1(bt, bh, KB_REG_INTE, KB_INTE);
151	}
152
153	return handled;
154}
155
156int
157kb_hb_cnattach(void)
158{
159
160	kb_hb_conssc.cs_isconsole = 1;
161	return kb_cnattach(&kb_hb_conssc);
162}
163