pckbc_ebus.c revision 1.1.2.2 1 1.1.2.2 yamt /* $NetBSD: pckbc_ebus.c,v 1.1.2.2 2012/10/30 17:20:24 yamt Exp $ */
2 1.1.2.2 yamt
3 1.1.2.2 yamt /*
4 1.1.2.2 yamt * Copyright (c) 2002 Valeriy E. Ushakov
5 1.1.2.2 yamt * All rights reserved.
6 1.1.2.2 yamt *
7 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 yamt * modification, are permitted provided that the following conditions
9 1.1.2.2 yamt * are met:
10 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 yamt * documentation and/or other materials provided with the distribution.
15 1.1.2.2 yamt * 3. The name of the author may not be used to endorse or promote products
16 1.1.2.2 yamt * derived from this software without specific prior written permission
17 1.1.2.2 yamt *
18 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1.2.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1.2.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1.2.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1.2.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1.2.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1.2.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1.2.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1.2.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1.2.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1.2.2 yamt */
29 1.1.2.2 yamt
30 1.1.2.2 yamt #include <sys/cdefs.h>
31 1.1.2.2 yamt __KERNEL_RCSID(0, "$NetBSD: pckbc_ebus.c,v 1.1.2.2 2012/10/30 17:20:24 yamt Exp $");
32 1.1.2.2 yamt
33 1.1.2.2 yamt #include <sys/param.h>
34 1.1.2.2 yamt #include <sys/systm.h>
35 1.1.2.2 yamt #include <sys/kernel.h>
36 1.1.2.2 yamt #include <sys/device.h>
37 1.1.2.2 yamt #include <sys/malloc.h>
38 1.1.2.2 yamt #include <sys/bus.h>
39 1.1.2.2 yamt #include <sys/intr.h>
40 1.1.2.2 yamt
41 1.1.2.2 yamt #include <machine/autoconf.h>
42 1.1.2.2 yamt
43 1.1.2.2 yamt #include <dev/ic/i8042reg.h>
44 1.1.2.2 yamt #include <dev/ic/pckbcvar.h>
45 1.1.2.2 yamt #include <dev/pckbport/pckbportvar.h>
46 1.1.2.2 yamt
47 1.1.2.2 yamt #include <dev/ebus/ebusreg.h>
48 1.1.2.2 yamt #include <dev/ebus/ebusvar.h>
49 1.1.2.2 yamt
50 1.1.2.2 yamt struct pckbc_ebus_softc {
51 1.1.2.2 yamt struct pckbc_softc psc_pckbc; /* real "pckbc" softc */
52 1.1.2.2 yamt uint32_t psc_intr[PCKBC_NSLOTS];
53 1.1.2.2 yamt };
54 1.1.2.2 yamt
55 1.1.2.2 yamt static int pckbc_ebus_match(device_t, cfdata_t, void *);
56 1.1.2.2 yamt static void pckbc_ebus_attach(device_t, device_t, void *);
57 1.1.2.2 yamt
58 1.1.2.2 yamt static void pckbc_ebus_intr_establish(struct pckbc_softc *, pckbport_slot_t);
59 1.1.2.2 yamt
60 1.1.2.2 yamt #define PCKBC_PROM_DEVICE_NAME "8042"
61 1.1.2.2 yamt
62 1.1.2.2 yamt CFATTACH_DECL_NEW(pckbc_ebus, sizeof(struct pckbc_ebus_softc),
63 1.1.2.2 yamt pckbc_ebus_match, pckbc_ebus_attach, NULL, NULL);
64 1.1.2.2 yamt
65 1.1.2.2 yamt
66 1.1.2.2 yamt static int
67 1.1.2.2 yamt pckbc_ebus_match(device_t parent, cfdata_t cf, void *aux)
68 1.1.2.2 yamt {
69 1.1.2.2 yamt struct ebus_attach_args *ea = aux;
70 1.1.2.2 yamt
71 1.1.2.2 yamt return (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0);
72 1.1.2.2 yamt }
73 1.1.2.2 yamt
74 1.1.2.2 yamt static void
75 1.1.2.2 yamt pckbc_ebus_attach(device_t parent, device_t self, void *aux)
76 1.1.2.2 yamt {
77 1.1.2.2 yamt struct pckbc_ebus_softc *sc = device_private(self);
78 1.1.2.2 yamt struct pckbc_softc *psc = (struct pckbc_softc *) sc;
79 1.1.2.2 yamt struct ebus_attach_args *ea = aux;
80 1.1.2.2 yamt struct pckbc_internal *t;
81 1.1.2.2 yamt bus_space_tag_t iot;
82 1.1.2.2 yamt bus_addr_t ioaddr;
83 1.1.2.2 yamt int stdin_node, node;
84 1.1.2.2 yamt int isconsole, i;
85 1.1.2.2 yamt
86 1.1.2.2 yamt psc->sc_dv = self;
87 1.1.2.2 yamt iot = ea->ea_bustag;
88 1.1.2.2 yamt ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
89 1.1.2.2 yamt
90 1.1.2.2 yamt stdin_node = prom_instance_to_package(prom_stdin());
91 1.1.2.2 yamt isconsole = 0;
92 1.1.2.2 yamt for (node = prom_firstchild(ea->ea_node);
93 1.1.2.2 yamt node != 0; node = prom_nextsibling(node))
94 1.1.2.2 yamt if (node == stdin_node) {
95 1.1.2.2 yamt isconsole = 1;
96 1.1.2.2 yamt break;
97 1.1.2.2 yamt }
98 1.1.2.2 yamt
99 1.1.2.2 yamt psc->intr_establish = pckbc_ebus_intr_establish;
100 1.1.2.2 yamt
101 1.1.2.2 yamt for (i = 0; i < PCKBC_NSLOTS; i++)
102 1.1.2.2 yamt sc->psc_intr[i] = ea->ea_intr[i];
103 1.1.2.2 yamt
104 1.1.2.2 yamt if (isconsole) {
105 1.1.2.2 yamt int status;
106 1.1.2.2 yamt
107 1.1.2.2 yamt status = pckbc_cnattach(iot, ioaddr, KBCMDP, 0,
108 1.1.2.2 yamt PCKBC_NEED_AUXWRITE | PCKBC_CANT_TRANSLATE);
109 1.1.2.2 yamt if (status == 0)
110 1.1.2.2 yamt aprint_normal(": cnattach ok");
111 1.1.2.2 yamt else
112 1.1.2.2 yamt aprint_error(": cnattach %d", status);
113 1.1.2.2 yamt }
114 1.1.2.2 yamt
115 1.1.2.2 yamt if (pckbc_is_console(iot, ioaddr)) {
116 1.1.2.2 yamt t = &pckbc_consdata;
117 1.1.2.2 yamt pckbc_console_attached = 1;
118 1.1.2.2 yamt } else {
119 1.1.2.2 yamt bus_space_handle_t ioh_d, ioh_c;
120 1.1.2.2 yamt
121 1.1.2.2 yamt if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
122 1.1.2.2 yamt aprint_error(": unable to map data register\n");
123 1.1.2.2 yamt return;
124 1.1.2.2 yamt }
125 1.1.2.2 yamt
126 1.1.2.2 yamt if (bus_space_map(iot, ioaddr + KBCMDP, 1, 0, &ioh_c) != 0) {
127 1.1.2.2 yamt bus_space_unmap(iot, ioh_d, 1);
128 1.1.2.2 yamt aprint_error(": unable to map cmd register\n");
129 1.1.2.2 yamt return;
130 1.1.2.2 yamt }
131 1.1.2.2 yamt
132 1.1.2.2 yamt t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
133 1.1.2.2 yamt memset(t, 0, sizeof(struct pckbc_internal));
134 1.1.2.2 yamt t->t_iot = iot;
135 1.1.2.2 yamt t->t_ioh_d = ioh_d;
136 1.1.2.2 yamt t->t_ioh_c = ioh_c;
137 1.1.2.2 yamt t->t_addr = ioaddr;
138 1.1.2.2 yamt t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
139 1.1.2.2 yamt callout_init(&t->t_cleanup, 0);
140 1.1.2.2 yamt
141 1.1.2.2 yamt (void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* flush */
142 1.1.2.2 yamt
143 1.1.2.2 yamt if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
144 1.1.2.2 yamt aprint_error(": unable to request self test");
145 1.1.2.2 yamt else {
146 1.1.2.2 yamt int response;
147 1.1.2.2 yamt
148 1.1.2.2 yamt response = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
149 1.1.2.2 yamt if (response == 0x55)
150 1.1.2.2 yamt aprint_normal(": selftest ok");
151 1.1.2.2 yamt else
152 1.1.2.2 yamt aprint_error(": selftest failed (0x%02x)",
153 1.1.2.2 yamt response);
154 1.1.2.2 yamt }
155 1.1.2.2 yamt }
156 1.1.2.2 yamt
157 1.1.2.2 yamt /* crosslink */
158 1.1.2.2 yamt t->t_sc = psc;
159 1.1.2.2 yamt psc->id = t;
160 1.1.2.2 yamt
161 1.1.2.2 yamt /* finish off the attach */
162 1.1.2.2 yamt aprint_normal("\n");
163 1.1.2.2 yamt pckbc_attach(psc);
164 1.1.2.2 yamt }
165 1.1.2.2 yamt
166 1.1.2.2 yamt
167 1.1.2.2 yamt static void
168 1.1.2.2 yamt pckbc_ebus_intr_establish(struct pckbc_softc *sc, pckbport_slot_t slot)
169 1.1.2.2 yamt {
170 1.1.2.2 yamt struct pckbc_ebus_softc *psc = (struct pckbc_ebus_softc *)sc;
171 1.1.2.2 yamt void *res;
172 1.1.2.2 yamt
173 1.1.2.2 yamt /* We assume that interrupt order is the same as slot order. */
174 1.1.2.2 yamt res = bus_intr_establish(sc->id->t_iot, psc->psc_intr[slot],
175 1.1.2.2 yamt IPL_TTY, pckbcintr, sc);
176 1.1.2.2 yamt if (res == NULL)
177 1.1.2.2 yamt aprint_error_dev(sc->sc_dv,
178 1.1.2.2 yamt "unable to establish %s slot interrupt\n",
179 1.1.2.2 yamt pckbc_slot_names[slot]);
180 1.1.2.2 yamt
181 1.1.2.2 yamt return;
182 1.1.2.2 yamt }
183