kb_hb.c revision 1.3
1/*	$NetBSD: kb_hb.c,v 1.3 2002/10/02 04:40:08 thorpej 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/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#define KB_SIZE 0x10 /* XXX */
49#define KB_PRI 5
50
51int	kb_hb_match(struct device *, struct cfdata *, void *);
52void	kb_hb_attach(struct device *, struct device *, void *);
53void	kb_hb_init(struct kb_softc *);
54int	kb_hb_intr(void *);
55int	kb_hb_cnattach(void);
56
57CFATTACH_DECL(kb_hb, sizeof(struct kb_softc),
58    kb_hb_match, kb_hb_attach, NULL, NULL);
59
60struct console_softc kb_hb_conssc;
61
62extern struct cfdriver kb_hb_cd;
63
64int
65kb_hb_match(parent, cf, aux)
66	struct device *parent;
67	struct cfdata *cf;
68	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 == -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
88void
89kb_hb_attach(parent, self, aux)
90	struct device *parent;
91	struct device *self;
92	void *aux;
93{
94	struct kb_softc *sc = (void *)self;
95	struct hb_attach_args *ha = aux;
96	bus_space_tag_t bt = ha->ha_bust;
97	bus_space_handle_t bh;
98	struct wskbddev_attach_args wsa;
99	int ipl;
100
101	if (bus_space_map(bt, ha->ha_address, KB_SIZE, 0, &bh) != 0) {
102		printf("can't map device space\n");
103		return;
104	}
105
106	printf("\n");
107
108	sc->sc_bt = bt;
109	sc->sc_bh = bh;
110	sc->sc_offset = KB_REG_DATA;
111	sc->sc_conssc = &kb_hb_conssc;
112
113	ipl = ha->ha_ipl;
114	if (ipl == -1)
115		ipl = KB_PRI;
116
117	kb_hb_init(sc);
118
119	isrlink_autovec(kb_hb_intr, (void *)sc, ipl, ISRPRI_TTY);
120
121	wsa.console = kb_hb_conssc.cs_isconsole;
122	wsa.keymap = &kb_keymapdata;
123	wsa.accessops = &kb_accessops;
124	wsa.accesscookie = sc;
125
126	sc->sc_wskbddev = config_found(self, &wsa, wskbddevprint);
127}
128
129void
130kb_hb_init(sc)
131	struct kb_softc *sc;
132{
133	bus_space_tag_t bt = sc->sc_bt;
134	bus_space_handle_t bh = sc->sc_bh;
135
136	bus_space_write_1(bt, bh, KB_REG_RESET, 0);
137	bus_space_write_1(bt, bh, KB_REG_INTE, KB_INTE);
138}
139
140int
141kb_hb_intr(arg)
142	void *arg;
143{
144	struct kb_softc *sc = (struct kb_softc *)arg;
145	struct console_softc *kb_conssc = sc->sc_conssc;
146	bus_space_tag_t bt = sc->sc_bt;
147	bus_space_handle_t bh = sc->sc_bh;
148	int stat, handled = 0;
149
150	kb_conssc->cs_nintr++;
151
152	stat = bus_space_read_1(bt, bh, KB_REG_STAT);
153	if (stat & KBSTAT_RDY) {
154		kb_intr(sc);
155		handled = 1;
156		bus_space_write_1(bt, bh, KB_REG_INTE, KB_INTE);
157	}
158
159	return handled;
160}
161
162int
163kb_hb_cnattach()
164{
165
166	kb_hb_conssc.cs_isconsole = 1;
167	return kb_cnattach(&kb_hb_conssc);
168}
169