kb_ap.c revision 1.6
1/*	$NetBSD: kb_ap.c,v 1.6 2005/02/06 02:18:02 tsutsui Exp $	*/
2
3/*-
4 * Copyright (c) 2000 Tsubai Masanari.  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 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: kb_ap.c,v 1.6 2005/02/06 02:18:02 tsutsui Exp $");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/device.h>
35
36#include <dev/wscons/wsconsio.h>
37#include <dev/wscons/wskbdvar.h>
38#include <dev/wscons/wsksymdef.h>
39
40#include <machine/adrsmap.h>
41#include <newsmips/apbus/apbusvar.h>
42
43struct kbreg {
44	u_int kb_rx_data;
45	u_int kb_rx_stat;
46	u_int kb_rx_intr_en;
47	u_int kb_rx_reset;
48	u_int kb_rx_speed;
49
50	u_int ms_rx_data;
51	u_int ms_rx_stat;
52	u_int ms_rx_intr_en;
53	u_int ms_rx_reset;
54	u_int ms_rx_speed;
55
56	u_int kb_buzzf;
57	u_int kb_buzz;
58
59	u_int kb_tx_data;
60	u_int kb_tx_stat;
61	u_int kb_tx_intr_en;
62	u_int kb_tx_reset;
63	u_int kb_tx_speed;
64};
65
66struct kb_ap_softc {
67	struct device sc_dev;
68	volatile struct kbreg *sc_reg;
69	struct device *sc_wskbddev;
70};
71
72int kb_ap_match(struct device *, struct cfdata *, void *);
73void kb_ap_attach(struct device *, struct device *, void *);
74int kb_ap_intr(void *);
75
76void kb_ap_cnattach(void);
77void kb_ap_cngetc(void *, u_int *, int *);
78void kb_ap_cnpollc(void *, int);
79
80int kb_ap_enable(void *, int);
81void kb_ap_setleds(void *, int);
82int kb_ap_ioctl(void *, u_long, caddr_t, int, struct proc *);
83
84extern struct wscons_keydesc newskb_keydesctab[];
85
86CFATTACH_DECL(kb_ap, sizeof(struct kb_ap_softc),
87    kb_ap_match, kb_ap_attach, NULL, NULL);
88
89struct wskbd_accessops kb_ap_accessops = {
90	kb_ap_enable,
91	kb_ap_setleds,
92	kb_ap_ioctl,
93};
94
95struct wskbd_consops kb_ap_consops = {
96	kb_ap_cngetc,
97	kb_ap_cnpollc,
98};
99
100struct wskbd_mapdata kb_ap_keymapdata = {
101	newskb_keydesctab,
102	KB_JP,
103};
104
105int
106kb_ap_match(struct device *parent, struct cfdata *cf, void *aux)
107{
108	struct apbus_attach_args *apa = aux;
109
110	if (strcmp(apa->apa_name, "kb") == 0)
111		return 1;
112
113	return 0;
114}
115
116void
117kb_ap_attach(struct device *parent, struct device *self, void *aux)
118{
119	struct kb_ap_softc *sc = (void *)self;
120	struct apbus_attach_args *apa = aux;
121	volatile struct kbreg *reg = (void *)apa->apa_hwbase;
122	volatile int *dipsw = (void *)NEWS5000_DIP_SWITCH;
123	struct wskbddev_attach_args waa;
124	int cons = 0;
125
126	printf(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
127
128	sc->sc_reg = reg;
129
130	if (*dipsw & 7) {
131		printf(" (console)");
132		cons = 1;
133	}
134	printf("\n");
135
136	reg->kb_rx_reset = 0x03;
137	reg->kb_tx_reset = 0x03;
138
139	reg->kb_rx_speed = 0x04;
140	reg->kb_tx_speed = 0x04;
141
142	reg->kb_rx_intr_en = 1;
143	reg->kb_tx_intr_en = 0;
144
145	apbus_intr_establish(1, NEWS5000_INT1_KBD, 0, kb_ap_intr, sc,
146	    "", apa->apa_ctlnum);
147
148	waa.console = cons;
149	waa.keymap = &kb_ap_keymapdata;
150	waa.accessops = &kb_ap_accessops;
151	waa.accesscookie = sc;
152
153	sc->sc_wskbddev = config_found(self, &waa, wskbddevprint);
154}
155
156int
157kb_ap_intr(void *v)
158{
159	struct kb_ap_softc *sc = v;
160	volatile struct kbreg *reg = sc->sc_reg;
161	int key, val, type, release;
162	int rv = 0;
163
164	while (reg->kb_rx_stat & RX_KBRDY) {
165		key = reg->kb_rx_data;
166		val = key & 0x7f;
167		release = key & 0x80;
168		type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
169
170		if (sc->sc_wskbddev)
171			wskbd_input(sc->sc_wskbddev, type, val);
172
173		rv = 1;
174	}
175
176	return rv;
177}
178
179void
180kb_ap_cnattach(void)
181{
182
183	wskbd_cnattach(&kb_ap_consops, (void *)0xbf900000, &kb_ap_keymapdata);
184}
185
186void
187kb_ap_cngetc(void *v, u_int *type, int *data)
188{
189	volatile struct kbreg *reg = v;
190	int key, release, ointr;
191
192	/* Disable keyboard interrupt. */
193	ointr = reg->kb_rx_intr_en;
194	reg->kb_rx_intr_en = 0;
195
196	/* Wait for key data. */
197	while ((reg->kb_rx_stat & RX_KBRDY) == 0);
198
199	key = reg->kb_rx_data;
200	release = key & 0x80;
201	*data = key & 0x7f;
202	*type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
203
204	reg->kb_rx_intr_en = ointr;
205}
206
207void
208kb_ap_cnpollc(void *v, int on)
209{
210}
211
212int
213kb_ap_enable(void *v, int on)
214{
215
216	return 0;
217}
218
219void
220kb_ap_setleds(void *v, int on)
221{
222}
223
224int
225kb_ap_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
226{
227
228	switch (cmd) {
229	case WSKBDIO_GTYPE:
230		*(int *)data = 0;	/* XXX */
231		return 0;
232	case WSKBDIO_SETLEDS:
233		return 0;
234	case WSKBDIO_GETLEDS:
235		*(int *)data = 0;
236		return 0;
237	}
238
239	return EPASSTHROUGH;
240}
241