kb_kbc.c revision 1.9
1/*	$NetBSD: kb_kbc.c,v 1.9 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_kbc.c,v 1.9 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
46#include <news68k/dev/kbcreg.h>
47#include <news68k/dev/kbcvar.h>
48#include <news68k/dev/kbvar.h>
49
50#include <news68k/news68k/isr.h>
51
52static int kb_kbc_match(device_t, cfdata_t, void *);
53static void kb_kbc_attach(device_t, device_t, void *);
54static void kb_kbc_init(struct kb_softc *);
55int	kb_kbc_intr(void *);
56int	kb_kbc_cnattach(void);
57
58CFATTACH_DECL_NEW(kb_kbc, sizeof(struct kb_softc),
59    kb_kbc_match, kb_kbc_attach, NULL, NULL);
60
61struct console_softc kb_kbc_conssc;
62
63static int
64kb_kbc_match(device_t parent, cfdata_t cf, void *aux)
65{
66	struct kbc_attach_args *ka = aux;
67
68	if (strcmp(ka->ka_name, "kb"))
69		return 0;
70
71	return 1;
72}
73
74static void
75kb_kbc_attach(device_t parent, device_t self, void *aux)
76{
77	struct kb_softc *sc = device_private(self);
78	struct kbc_attach_args *ka = aux;
79	struct wskbddev_attach_args wsa;
80	int ipl;
81
82	sc->sc_dev = self;
83	aprint_normal("\n");
84
85	sc->sc_bt = ka->ka_bt;
86	sc->sc_bh = ka->ka_bh;
87	sc->sc_offset = KBC_KBREG_DATA;
88	sc->sc_conssc = &kb_kbc_conssc;
89	ipl = ka->ka_ipl;
90
91	kb_kbc_init(sc);
92
93	isrlink_autovec(kb_kbc_intr, (void *)sc, ipl, IPL_TTY);
94
95	wsa.console = kb_kbc_conssc.cs_isconsole;
96	wsa.keymap = &kb_keymapdata;
97	wsa.accessops = &kb_accessops;
98	wsa.accesscookie = sc;
99
100	sc->sc_wskbddev = config_found(self, &wsa, wskbddevprint);
101}
102
103static void
104kb_kbc_init(struct kb_softc *sc)
105{
106	bus_space_tag_t bt = sc->sc_bt;
107	bus_space_handle_t bh = sc->sc_bh;
108
109	bus_space_write_1(bt, bh, KBC_KBREG_RESET, 0);
110	bus_space_write_1(bt, bh, KBC_KBREG_INTE, KBC_INTE);
111}
112
113int
114kb_kbc_intr(void *arg)
115{
116	struct kb_softc *sc = (struct kb_softc *)arg;
117	struct console_softc *kb_conssc = sc->sc_conssc;
118	bus_space_tag_t bt = sc->sc_bt;
119	bus_space_handle_t bh = sc->sc_bh;
120	int stat, handled = 0;
121
122	kb_conssc->cs_nintr++;
123
124	stat = bus_space_read_1(bt, bh, KBC_KBREG_STAT);
125	if (stat & KBCSTAT_KBRDY) {
126		kb_intr(sc);
127		handled = 1;
128		bus_space_write_1(bt, bh, KBC_KBREG_INTE, KBC_INTE);
129	}
130
131	return handled;
132}
133
134int
135kb_kbc_cnattach(void)
136{
137
138	kb_kbc_conssc.cs_isconsole = 1;
139	return kb_cnattach(&kb_kbc_conssc);
140}
141