kb_hb.c revision 1.11
1/*	$NetBSD: kb_hb.c,v 1.11 2008/03/29 05:48:33 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.11 2008/03/29 05:48:33 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	sc->sc_dev = self;
99
100	if (bus_space_map(bt, ha->ha_address, KB_SIZE, 0, &bh) != 0) {
101		aprint_error(": can't map device space\n");
102		return;
103	}
104
105	aprint_normal("\n");
106
107	sc->sc_bt = bt;
108	sc->sc_bh = bh;
109	sc->sc_offset = KB_REG_DATA;
110	sc->sc_conssc = &kb_hb_conssc;
111
112	ipl = ha->ha_ipl;
113	if (ipl == -1)
114		ipl = KB_PRI;
115
116	kb_hb_init(sc);
117
118	isrlink_autovec(kb_hb_intr, (void *)sc, ipl, IPL_TTY);
119
120	wsa.console = kb_hb_conssc.cs_isconsole;
121	wsa.keymap = &kb_keymapdata;
122	wsa.accessops = &kb_accessops;
123	wsa.accesscookie = sc;
124
125	sc->sc_wskbddev = config_found(self, &wsa, wskbddevprint);
126}
127
128static void
129kb_hb_init(struct kb_softc *sc)
130{
131	bus_space_tag_t bt = sc->sc_bt;
132	bus_space_handle_t bh = sc->sc_bh;
133
134	bus_space_write_1(bt, bh, KB_REG_RESET, 0);
135	bus_space_write_1(bt, bh, KB_REG_INTE, KB_INTE);
136}
137
138int
139kb_hb_intr(void *arg)
140{
141	struct kb_softc *sc = (struct kb_softc *)arg;
142	struct console_softc *kb_conssc = sc->sc_conssc;
143	bus_space_tag_t bt = sc->sc_bt;
144	bus_space_handle_t bh = sc->sc_bh;
145	int stat, handled = 0;
146
147	kb_conssc->cs_nintr++;
148
149	stat = bus_space_read_1(bt, bh, KB_REG_STAT);
150	if (stat & KBSTAT_RDY) {
151		kb_intr(sc);
152		handled = 1;
153		bus_space_write_1(bt, bh, KB_REG_INTE, KB_INTE);
154	}
155
156	return handled;
157}
158
159int
160kb_hb_cnattach(void)
161{
162
163	kb_hb_conssc.cs_isconsole = 1;
164	return kb_cnattach(&kb_hb_conssc);
165}
166