kb_hb.c revision 1.10
1/*	$NetBSD: kb_hb.c,v 1.10 2008/03/28 18:19:56 tsutsui Exp $	*/
2
3/*
4 * Copyright (c) 2001 Izumi Tsutsui.
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
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__KERNEL_RCSID(0, "$NetBSD: kb_hb.c,v 1.10 2008/03/28 18:19:56 tsutsui Exp $");
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/device.h>
36
37#include <machine/bus.h>
38#include <machine/cpu.h>
39
40#include <dev/wscons/wsconsio.h>
41#include <dev/wscons/wskbdvar.h>
42#include <dev/wscons/wsksymdef.h>
43#include <dev/wscons/wsksymvar.h>
44
45#include <news68k/dev/hbvar.h>
46#include <news68k/dev/kb_hbreg.h>
47#include <news68k/dev/kbvar.h>
48
49#include <news68k/news68k/isr.h>
50
51#include "ioconf.h"
52
53#define KB_SIZE 0x10 /* XXX */
54#define KB_PRI 5
55
56static int kb_hb_match(device_t, cfdata_t, void *);
57static void kb_hb_attach(device_t, device_t, void *);
58static void kb_hb_init(struct kb_softc *);
59int	kb_hb_intr(void *);
60int	kb_hb_cnattach(void);
61
62CFATTACH_DECL_NEW(kb_hb, sizeof(struct kb_softc),
63    kb_hb_match, kb_hb_attach, NULL, NULL);
64
65struct console_softc kb_hb_conssc;
66
67static int
68kb_hb_match(device_t parent, cfdata_t cf, void *aux)
69{
70	struct hb_attach_args *ha = aux;
71	u_int addr;
72
73	if (strcmp(ha->ha_name, "kb"))
74		return 0;
75
76	/* XXX no default address */
77	if (ha->ha_address == (u_int)-1)
78		return 0;
79
80	addr = IIOV(ha->ha_address); /* XXX */
81
82	if (badaddr((void *)addr, 1))
83		return 0;
84
85	return 1;
86}
87
88static void
89kb_hb_attach(device_t parent, device_t self, void *aux)
90{
91	struct kb_softc *sc = device_private(self);
92	struct hb_attach_args *ha = aux;
93	bus_space_tag_t bt = ha->ha_bust;
94	bus_space_handle_t bh;
95	struct wskbddev_attach_args wsa;
96	int ipl;
97
98	if (bus_space_map(bt, ha->ha_address, KB_SIZE, 0, &bh) != 0) {
99		aprint_error(": can't map device space\n");
100		return;
101	}
102
103	aprint_normal("\n");
104
105	sc->sc_bt = bt;
106	sc->sc_bh = bh;
107	sc->sc_offset = KB_REG_DATA;
108	sc->sc_conssc = &kb_hb_conssc;
109
110	ipl = ha->ha_ipl;
111	if (ipl == -1)
112		ipl = KB_PRI;
113
114	kb_hb_init(sc);
115
116	isrlink_autovec(kb_hb_intr, (void *)sc, ipl, IPL_TTY);
117
118	wsa.console = kb_hb_conssc.cs_isconsole;
119	wsa.keymap = &kb_keymapdata;
120	wsa.accessops = &kb_accessops;
121	wsa.accesscookie = sc;
122
123	sc->sc_wskbddev = config_found(self, &wsa, wskbddevprint);
124}
125
126static void
127kb_hb_init(struct kb_softc *sc)
128{
129	bus_space_tag_t bt = sc->sc_bt;
130	bus_space_handle_t bh = sc->sc_bh;
131
132	bus_space_write_1(bt, bh, KB_REG_RESET, 0);
133	bus_space_write_1(bt, bh, KB_REG_INTE, KB_INTE);
134}
135
136int
137kb_hb_intr(void *arg)
138{
139	struct kb_softc *sc = (struct kb_softc *)arg;
140	struct console_softc *kb_conssc = sc->sc_conssc;
141	bus_space_tag_t bt = sc->sc_bt;
142	bus_space_handle_t bh = sc->sc_bh;
143	int stat, handled = 0;
144
145	kb_conssc->cs_nintr++;
146
147	stat = bus_space_read_1(bt, bh, KB_REG_STAT);
148	if (stat & KBSTAT_RDY) {
149		kb_intr(sc);
150		handled = 1;
151		bus_space_write_1(bt, bh, KB_REG_INTE, KB_INTE);
152	}
153
154	return handled;
155}
156
157int
158kb_hb_cnattach(void)
159{
160
161	kb_hb_conssc.cs_isconsole = 1;
162	return kb_cnattach(&kb_hb_conssc);
163}
164