xref: /netbsd-src/sys/arch/newsmips/apbus/kb_ap.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: kb_ap.c,v 1.10 2012/10/13 06:25:20 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.10 2012/10/13 06:25:20 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 
43 struct kbreg {
44 	volatile uint32_t kb_rx_data;
45 	volatile uint32_t kb_rx_stat;
46 	volatile uint32_t kb_rx_intr_en;
47 	volatile uint32_t kb_rx_reset;
48 	volatile uint32_t kb_rx_speed;
49 
50 	volatile uint32_t ms_rx_data;
51 	volatile uint32_t ms_rx_stat;
52 	volatile uint32_t ms_rx_intr_en;
53 	volatile uint32_t ms_rx_reset;
54 	volatile uint32_t ms_rx_speed;
55 
56 	volatile uint32_t kb_buzzf;
57 	volatile uint32_t kb_buzz;
58 
59 	volatile uint32_t kb_tx_data;
60 	volatile uint32_t kb_tx_stat;
61 	volatile uint32_t kb_tx_intr_en;
62 	volatile uint32_t kb_tx_reset;
63 	volatile uint32_t kb_tx_speed;
64 };
65 
66 struct kb_ap_softc {
67 	device_t sc_dev;
68 	struct kbreg *sc_reg;
69 	device_t sc_wskbddev;
70 };
71 
72 int kb_ap_match(device_t, cfdata_t, void *);
73 void kb_ap_attach(device_t, device_t, void *);
74 int kb_ap_intr(void *);
75 
76 void kb_ap_cnattach(void);
77 void kb_ap_cngetc(void *, u_int *, int *);
78 void kb_ap_cnpollc(void *, int);
79 
80 int kb_ap_enable(void *, int);
81 void kb_ap_setleds(void *, int);
82 int kb_ap_ioctl(void *, u_long, void *, int, struct lwp *);
83 
84 extern struct wscons_keydesc newskb_keydesctab[];
85 
86 CFATTACH_DECL_NEW(kb_ap, sizeof(struct kb_ap_softc),
87     kb_ap_match, kb_ap_attach, NULL, NULL);
88 
89 struct wskbd_accessops kb_ap_accessops = {
90 	kb_ap_enable,
91 	kb_ap_setleds,
92 	kb_ap_ioctl,
93 };
94 
95 struct wskbd_consops kb_ap_consops = {
96 	kb_ap_cngetc,
97 	kb_ap_cnpollc,
98 };
99 
100 struct wskbd_mapdata kb_ap_keymapdata = {
101 	newskb_keydesctab,
102 	KB_JP,
103 };
104 
105 int
106 kb_ap_match(device_t parent, cfdata_t 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 
116 void
117 kb_ap_attach(device_t parent, device_t self, void *aux)
118 {
119 	struct kb_ap_softc *sc = device_private(self);
120 	struct apbus_attach_args *apa = aux;
121 	struct kbreg *reg = (void *)apa->apa_hwbase;
122 	volatile uint32_t *dipsw = (void *)NEWS5000_DIP_SWITCH;
123 	struct wskbddev_attach_args waa;
124 	int cons = 0;
125 
126 	sc->sc_dev = self;
127 	aprint_normal(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
128 
129 	sc->sc_reg = reg;
130 
131 	if (*dipsw & 7) {
132 		aprint_normal(" (console)");
133 		cons = 1;
134 	}
135 	aprint_normal("\n");
136 
137 	reg->kb_rx_reset = 0x03;
138 	reg->kb_tx_reset = 0x03;
139 
140 	reg->kb_rx_speed = 0x04;
141 	reg->kb_tx_speed = 0x04;
142 
143 	reg->kb_rx_intr_en = 1;
144 	reg->kb_tx_intr_en = 0;
145 
146 	apbus_intr_establish(1, NEWS5000_INT1_KBD, 0, kb_ap_intr, sc,
147 	    "", apa->apa_ctlnum);
148 
149 	waa.console = cons;
150 	waa.keymap = &kb_ap_keymapdata;
151 	waa.accessops = &kb_ap_accessops;
152 	waa.accesscookie = sc;
153 
154 	sc->sc_wskbddev = config_found(self, &waa, wskbddevprint);
155 }
156 
157 int
158 kb_ap_intr(void *v)
159 {
160 	struct kb_ap_softc *sc = v;
161 	struct kbreg *reg = sc->sc_reg;
162 	int key, val, type, release;
163 	int rv = 0;
164 
165 	while (reg->kb_rx_stat & RX_KBRDY) {
166 		key = reg->kb_rx_data;
167 		val = key & 0x7f;
168 		release = key & 0x80;
169 		type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
170 
171 		if (sc->sc_wskbddev)
172 			wskbd_input(sc->sc_wskbddev, type, val);
173 
174 		rv = 1;
175 	}
176 
177 	return rv;
178 }
179 
180 void
181 kb_ap_cnattach(void)
182 {
183 
184 	wskbd_cnattach(&kb_ap_consops, (void *)0xbf900000, &kb_ap_keymapdata);
185 }
186 
187 void
188 kb_ap_cngetc(void *v, u_int *type, int *data)
189 {
190 	struct kbreg *reg = v;
191 	int key, release, ointr;
192 
193 	/* Disable keyboard interrupt. */
194 	ointr = reg->kb_rx_intr_en;
195 	reg->kb_rx_intr_en = 0;
196 
197 	/* Wait for key data. */
198 	while ((reg->kb_rx_stat & RX_KBRDY) == 0)
199 		;
200 
201 	key = reg->kb_rx_data;
202 	release = key & 0x80;
203 	*data = key & 0x7f;
204 	*type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
205 
206 	reg->kb_rx_intr_en = ointr;
207 }
208 
209 void
210 kb_ap_cnpollc(void *v, int on)
211 {
212 }
213 
214 int
215 kb_ap_enable(void *v, int on)
216 {
217 
218 	return 0;
219 }
220 
221 void
222 kb_ap_setleds(void *v, int on)
223 {
224 }
225 
226 int
227 kb_ap_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
228 {
229 
230 	switch (cmd) {
231 	case WSKBDIO_GTYPE:
232 		*(int *)data = 0;	/* XXX */
233 		return 0;
234 	case WSKBDIO_SETLEDS:
235 		return 0;
236 	case WSKBDIO_GETLEDS:
237 		*(int *)data = 0;
238 		return 0;
239 	}
240 
241 	return EPASSTHROUGH;
242 }
243