pckbc_js.c revision 1.2.2.4 1 1.2.2.4 thorpej /* $NetBSD: pckbc_js.c,v 1.2.2.4 2002/12/11 06:12:04 thorpej Exp $ */
2 1.2.2.2 nathanw
3 1.2.2.2 nathanw /*
4 1.2.2.2 nathanw * Copyright (c) 2002 Valeriy E. Ushakov
5 1.2.2.2 nathanw * All rights reserved.
6 1.2.2.2 nathanw *
7 1.2.2.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 nathanw * modification, are permitted provided that the following conditions
9 1.2.2.2 nathanw * are met:
10 1.2.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.2.2.2 nathanw * 3. The name of the author may not be used to endorse or promote products
16 1.2.2.2 nathanw * derived from this software without specific prior written permission
17 1.2.2.2 nathanw *
18 1.2.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.2.2.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.2.2.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.2.2.2 nathanw * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.2.2.2 nathanw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.2.2.2 nathanw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.2.2.2 nathanw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.2.2.2 nathanw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.2.2.2 nathanw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.2.2.2 nathanw * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.2.2.2 nathanw */
29 1.2.2.2 nathanw
30 1.2.2.2 nathanw #include <sys/param.h>
31 1.2.2.2 nathanw #include <sys/systm.h>
32 1.2.2.2 nathanw #include <sys/kernel.h>
33 1.2.2.2 nathanw #include <sys/device.h>
34 1.2.2.2 nathanw #include <sys/malloc.h>
35 1.2.2.2 nathanw
36 1.2.2.2 nathanw #include <machine/autoconf.h>
37 1.2.2.2 nathanw #include <machine/bus.h>
38 1.2.2.2 nathanw #include <machine/intr.h>
39 1.2.2.2 nathanw
40 1.2.2.2 nathanw #include <dev/ic/i8042reg.h>
41 1.2.2.2 nathanw #include <dev/ic/pckbcvar.h>
42 1.2.2.2 nathanw #include <dev/pckbc/pckbdvar.h>
43 1.2.2.2 nathanw
44 1.2.2.2 nathanw #include <dev/ebus/ebusreg.h>
45 1.2.2.2 nathanw #include <dev/ebus/ebusvar.h>
46 1.2.2.2 nathanw
47 1.2.2.2 nathanw
48 1.2.2.2 nathanw struct pckbc_js_softc {
49 1.2.2.2 nathanw struct pckbc_softc jsc_pckbc; /* real "pckbc" softc */
50 1.2.2.2 nathanw
51 1.2.2.2 nathanw /* kbd and mouse share interrupt in both mr.coffee and krups */
52 1.2.2.2 nathanw u_int32_t jsc_intr;
53 1.2.2.2 nathanw int jsc_establised;
54 1.2.2.2 nathanw };
55 1.2.2.2 nathanw
56 1.2.2.2 nathanw
57 1.2.2.2 nathanw static int pckbc_obio_match(struct device *, struct cfdata *, void *);
58 1.2.2.2 nathanw static void pckbc_obio_attach(struct device *, struct device *, void *);
59 1.2.2.2 nathanw
60 1.2.2.2 nathanw static int pckbc_ebus_match(struct device *, struct cfdata *, void *);
61 1.2.2.2 nathanw static void pckbc_ebus_attach(struct device *, struct device *, void *);
62 1.2.2.2 nathanw
63 1.2.2.2 nathanw static void pckbc_js_attach_common( struct pckbc_js_softc *,
64 1.2.2.2 nathanw bus_space_tag_t, bus_addr_t, int, int);
65 1.2.2.2 nathanw static void pckbc_js_intr_establish(struct pckbc_softc *, pckbc_slot_t);
66 1.2.2.2 nathanw
67 1.2.2.2 nathanw /* Mr.Coffee */
68 1.2.2.3 nathanw CFATTACH_DECL(pckbc_obio, sizeof(struct pckbc_js_softc),
69 1.2.2.3 nathanw pckbc_obio_match, pckbc_obio_attach, NULL, NULL);
70 1.2.2.2 nathanw
71 1.2.2.2 nathanw /* ms-IIep */
72 1.2.2.3 nathanw CFATTACH_DECL(pckbc_ebus, sizeof(struct pckbc_js_softc),
73 1.2.2.3 nathanw pckbc_ebus_match, pckbc_ebus_attach, NULL, NULL);
74 1.2.2.2 nathanw
75 1.2.2.2 nathanw #define PCKBC_PROM_DEVICE_NAME "8042"
76 1.2.2.2 nathanw
77 1.2.2.2 nathanw static int
78 1.2.2.2 nathanw pckbc_obio_match(parent, cf, aux)
79 1.2.2.2 nathanw struct device *parent;
80 1.2.2.2 nathanw struct cfdata *cf;
81 1.2.2.2 nathanw void *aux;
82 1.2.2.2 nathanw {
83 1.2.2.2 nathanw union obio_attach_args *uoba = aux;
84 1.2.2.2 nathanw struct sbus_attach_args *sa = &uoba->uoba_sbus;
85 1.2.2.2 nathanw
86 1.2.2.2 nathanw return (strcmp(sa->sa_name, PCKBC_PROM_DEVICE_NAME) == 0);
87 1.2.2.2 nathanw }
88 1.2.2.2 nathanw
89 1.2.2.2 nathanw static int
90 1.2.2.2 nathanw pckbc_ebus_match(parent, cf, aux)
91 1.2.2.2 nathanw struct device *parent;
92 1.2.2.2 nathanw struct cfdata *cf;
93 1.2.2.2 nathanw void *aux;
94 1.2.2.2 nathanw {
95 1.2.2.2 nathanw struct ebus_attach_args *ea = aux;
96 1.2.2.2 nathanw
97 1.2.2.2 nathanw return (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0);
98 1.2.2.2 nathanw }
99 1.2.2.2 nathanw
100 1.2.2.2 nathanw
101 1.2.2.2 nathanw static void
102 1.2.2.2 nathanw pckbc_obio_attach(parent, self, aux)
103 1.2.2.2 nathanw struct device *parent, *self;
104 1.2.2.2 nathanw void *aux;
105 1.2.2.2 nathanw {
106 1.2.2.2 nathanw struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
107 1.2.2.2 nathanw union obio_attach_args *uoba = aux;
108 1.2.2.2 nathanw struct sbus_attach_args *sa = &uoba->uoba_sbus;
109 1.2.2.2 nathanw bus_space_tag_t iot;
110 1.2.2.2 nathanw bus_addr_t ioaddr;
111 1.2.2.2 nathanw int intr, isconsole;
112 1.2.2.2 nathanw
113 1.2.2.2 nathanw iot = sa->sa_bustag;
114 1.2.2.2 nathanw ioaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
115 1.2.2.2 nathanw intr = sa->sa_nintr ? sa->sa_pri : /* level */ 13;
116 1.2.2.2 nathanw
117 1.2.2.2 nathanw /*
118 1.2.2.2 nathanw * TODO:
119 1.2.2.2 nathanw * . on OFW machines stdin is keyboard node, not 8042 node
120 1.2.2.2 nathanw * . on OBP machines 8042 node is faked by boot's prompatch
121 1.2.2.2 nathanw * and PROM's stdin points to zs keyboard (add prom patch?)
122 1.2.2.2 nathanw * . we probably want the stdout node to control whether
123 1.2.2.2 nathanw * console goes to serial
124 1.2.2.2 nathanw */
125 1.2.2.2 nathanw isconsole = 1;
126 1.2.2.2 nathanw
127 1.2.2.2 nathanw pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
128 1.2.2.2 nathanw }
129 1.2.2.2 nathanw
130 1.2.2.2 nathanw static void
131 1.2.2.2 nathanw pckbc_ebus_attach(parent, self, aux)
132 1.2.2.2 nathanw struct device *parent, *self;
133 1.2.2.2 nathanw void *aux;
134 1.2.2.2 nathanw {
135 1.2.2.2 nathanw struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
136 1.2.2.2 nathanw struct ebus_attach_args *ea = aux;
137 1.2.2.2 nathanw bus_space_tag_t iot;
138 1.2.2.2 nathanw bus_addr_t ioaddr;
139 1.2.2.3 nathanw int intr;
140 1.2.2.3 nathanw int stdin_node, node;
141 1.2.2.3 nathanw int isconsole;
142 1.2.2.2 nathanw
143 1.2.2.2 nathanw iot = ea->ea_bustag;
144 1.2.2.2 nathanw ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
145 1.2.2.2 nathanw intr = ea->ea_nintr ? ea->ea_intr[0] : /* line */ 0;
146 1.2.2.2 nathanw
147 1.2.2.3 nathanw /* search children of "8042" node for stdin (keyboard) */
148 1.2.2.3 nathanw stdin_node = prom_instance_to_package(prom_stdin());
149 1.2.2.3 nathanw isconsole = 0;
150 1.2.2.3 nathanw
151 1.2.2.3 nathanw for (node = prom_firstchild(ea->ea_node);
152 1.2.2.3 nathanw node != 0; node = prom_nextsibling(node))
153 1.2.2.3 nathanw if (node == stdin_node) {
154 1.2.2.3 nathanw isconsole = 1;
155 1.2.2.3 nathanw break;
156 1.2.2.3 nathanw }
157 1.2.2.2 nathanw
158 1.2.2.2 nathanw pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
159 1.2.2.2 nathanw }
160 1.2.2.2 nathanw
161 1.2.2.2 nathanw
162 1.2.2.2 nathanw static void
163 1.2.2.2 nathanw pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole)
164 1.2.2.2 nathanw struct pckbc_js_softc *jsc;
165 1.2.2.2 nathanw bus_space_tag_t iot;
166 1.2.2.2 nathanw bus_addr_t ioaddr;
167 1.2.2.2 nathanw int intr;
168 1.2.2.2 nathanw int isconsole;
169 1.2.2.2 nathanw {
170 1.2.2.2 nathanw struct pckbc_softc *sc = (struct pckbc_softc *)jsc;
171 1.2.2.2 nathanw struct pckbc_internal *t;
172 1.2.2.2 nathanw
173 1.2.2.2 nathanw jsc->jsc_pckbc.intr_establish = pckbc_js_intr_establish;
174 1.2.2.2 nathanw jsc->jsc_intr = intr;
175 1.2.2.2 nathanw jsc->jsc_establised = 0;
176 1.2.2.2 nathanw
177 1.2.2.2 nathanw if (isconsole) {
178 1.2.2.2 nathanw int status;
179 1.2.2.2 nathanw
180 1.2.2.2 nathanw status = pckbc_cnattach(iot, ioaddr, KBCMDP, PCKBC_KBD_SLOT);
181 1.2.2.2 nathanw if (status == 0)
182 1.2.2.2 nathanw printf(": cnattach ok");
183 1.2.2.2 nathanw else
184 1.2.2.2 nathanw printf(": cnattach %d", status);
185 1.2.2.2 nathanw }
186 1.2.2.2 nathanw
187 1.2.2.2 nathanw if (pckbc_is_console(iot, ioaddr)) {
188 1.2.2.2 nathanw t = &pckbc_consdata;
189 1.2.2.2 nathanw pckbc_console_attached = 1;
190 1.2.2.2 nathanw } else {
191 1.2.2.2 nathanw bus_space_handle_t ioh_d, ioh_c;
192 1.2.2.2 nathanw
193 1.2.2.2 nathanw if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
194 1.2.2.2 nathanw printf(": unable to map data register\n");
195 1.2.2.2 nathanw return;
196 1.2.2.2 nathanw }
197 1.2.2.2 nathanw
198 1.2.2.2 nathanw if (bus_space_map(iot, ioaddr + KBCMDP, 1, 0, &ioh_c) != 0) {
199 1.2.2.2 nathanw bus_space_unmap(iot, ioh_d, 1);
200 1.2.2.2 nathanw printf(": unable to map cmd register\n");
201 1.2.2.2 nathanw return;
202 1.2.2.2 nathanw }
203 1.2.2.2 nathanw
204 1.2.2.2 nathanw t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
205 1.2.2.2 nathanw memset(t, 0, sizeof(struct pckbc_internal));
206 1.2.2.2 nathanw t->t_iot = iot;
207 1.2.2.2 nathanw t->t_ioh_d = ioh_d;
208 1.2.2.2 nathanw t->t_ioh_c = ioh_c;
209 1.2.2.2 nathanw t->t_addr = ioaddr;
210 1.2.2.2 nathanw t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
211 1.2.2.2 nathanw callout_init(&t->t_cleanup);
212 1.2.2.2 nathanw
213 1.2.2.2 nathanw (void) pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0); /* flush */
214 1.2.2.2 nathanw
215 1.2.2.2 nathanw if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
216 1.2.2.2 nathanw printf(": unable to request self test");
217 1.2.2.2 nathanw else {
218 1.2.2.2 nathanw int response;
219 1.2.2.2 nathanw
220 1.2.2.2 nathanw response = pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0);
221 1.2.2.2 nathanw if (response == 0x55)
222 1.2.2.2 nathanw printf(": selftest ok");
223 1.2.2.2 nathanw else
224 1.2.2.2 nathanw printf(": selftest failed (0x%02x)", response);
225 1.2.2.2 nathanw }
226 1.2.2.2 nathanw }
227 1.2.2.2 nathanw
228 1.2.2.2 nathanw /* crosslink */
229 1.2.2.2 nathanw t->t_sc = sc;
230 1.2.2.2 nathanw sc->id = t;
231 1.2.2.2 nathanw
232 1.2.2.2 nathanw /* finish off the attach */
233 1.2.2.2 nathanw printf("\n");
234 1.2.2.2 nathanw pckbc_attach(sc);
235 1.2.2.2 nathanw }
236 1.2.2.2 nathanw
237 1.2.2.2 nathanw
238 1.2.2.2 nathanw /*
239 1.2.2.2 nathanw * Keyboard and mouse share the interrupt
240 1.2.2.2 nathanw * so don't install interrupt handler twice.
241 1.2.2.2 nathanw */
242 1.2.2.2 nathanw static void
243 1.2.2.2 nathanw pckbc_js_intr_establish(sc, slot)
244 1.2.2.2 nathanw struct pckbc_softc *sc;
245 1.2.2.2 nathanw pckbc_slot_t slot;
246 1.2.2.2 nathanw {
247 1.2.2.2 nathanw struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)sc;
248 1.2.2.2 nathanw void *res;
249 1.2.2.2 nathanw
250 1.2.2.2 nathanw if (jsc->jsc_establised) {
251 1.2.2.2 nathanw #ifdef DEBUG
252 1.2.2.2 nathanw printf("%s: %s slot shares interrupt (already established)\n",
253 1.2.2.2 nathanw sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
254 1.2.2.2 nathanw #endif
255 1.2.2.2 nathanw return;
256 1.2.2.2 nathanw }
257 1.2.2.2 nathanw
258 1.2.2.2 nathanw res = bus_intr_establish(sc->id->t_iot, jsc->jsc_intr,
259 1.2.2.4 thorpej IPL_TTY, pckbcintr, sc);
260 1.2.2.2 nathanw if (res == NULL)
261 1.2.2.2 nathanw printf("%s: unable to establish %s slot interrupt\n",
262 1.2.2.2 nathanw sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
263 1.2.2.2 nathanw else
264 1.2.2.2 nathanw jsc->jsc_establised = 1;
265 1.2.2.2 nathanw }
266 1.2.2.2 nathanw
267 1.2.2.2 nathanw
268 1.2.2.2 nathanw /*
269 1.2.2.2 nathanw * MD hook for use without MI wscons.
270 1.2.2.2 nathanw * Called by pckbc_cnattach().
271 1.2.2.2 nathanw */
272 1.2.2.2 nathanw int
273 1.2.2.2 nathanw pckbc_machdep_cnattach(constag, slot)
274 1.2.2.2 nathanw pckbc_tag_t constag;
275 1.2.2.2 nathanw pckbc_slot_t slot;
276 1.2.2.2 nathanw {
277 1.2.2.2 nathanw
278 1.2.2.2 nathanw #if !defined(__HAVE_NWSCONS) && defined(MSIIEP)
279 1.2.2.2 nathanw /* we do use wscons on krups! */
280 1.2.2.2 nathanw return (pckbd_cnattach(constag, slot));
281 1.2.2.2 nathanw #else
282 1.2.2.2 nathanw return (0);
283 1.2.2.2 nathanw #endif
284 1.2.2.2 nathanw }
285