2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer as
13 * the first lines of this file unmodified.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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 OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * from: FreeBSD: src/sys/isa/atkbdc_isa.c,v 1.31 2005/05/29 04:42:28 nyan
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
43 #include <sys/malloc.h>
45 #include <dev/ofw/ofw_bus.h>
47 #include <machine/resource.h>
48 #include <machine/ver.h>
52 #include <dev/kbd/kbdreg.h>
53 #include <dev/atkbdc/atkbdreg.h>
54 #include <dev/atkbdc/atkbdc_subr.h>
55 #include <dev/atkbdc/atkbdcreg.h>
56 #include <dev/atkbdc/psm.h>
58 static device_probe_t atkbdc_ebus_probe;
59 static device_attach_t atkbdc_ebus_attach;
61 static device_method_t atkbdc_ebus_methods[] = {
62 DEVMETHOD(device_probe, atkbdc_ebus_probe),
63 DEVMETHOD(device_attach, atkbdc_ebus_attach),
64 DEVMETHOD(device_suspend, bus_generic_suspend),
65 DEVMETHOD(device_resume, bus_generic_resume),
67 DEVMETHOD(bus_print_child, atkbdc_print_child),
68 DEVMETHOD(bus_read_ivar, atkbdc_read_ivar),
69 DEVMETHOD(bus_write_ivar, atkbdc_write_ivar),
70 DEVMETHOD(bus_get_resource_list,atkbdc_get_resource_list),
71 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
72 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
73 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
74 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
75 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
76 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
77 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
78 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
79 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
84 static driver_t atkbdc_ebus_driver = {
87 sizeof(atkbdc_softc_t *),
90 DRIVER_MODULE(atkbdc, ebus, atkbdc_ebus_driver, atkbdc_devclass, 0, 0);
93 atkbdc_ebus_probe(device_t dev)
95 struct resource *port0, *port1;
96 rman_res_t count, start;
99 if (strcmp(ofw_bus_get_name(dev), "8042") != 0)
103 * On AXi and AXmp boards the NS16550 (used to connect keyboard/
104 * mouse) share their IRQ lines with the i8042. Any IRQ activity
105 * (typically during attach) of the NS16550 used to connect the
106 * keyboard when actually the PS/2 keyboard is selected in OFW
107 * causes interaction with the OBP i8042 driver resulting in a
108 * hang and vice versa. As RS232 keyboards and mice obviously
109 * aren't meant to be used in parallel with PS/2 ones on these
110 * boards don't attach to the i8042 in case the PS/2 keyboard
111 * isn't selected in order to prevent such hangs.
112 * Note that it's not sufficient here to rely on the '8042' node
113 * only showing up when a PS/2 keyboard is actually connected as
114 * the user still might have adjusted the 'keyboard' alias to
115 * point to the RS232 keyboard.
117 if ((!strcmp(sparc64_model, "SUNW,UltraAX-MP") ||
118 !strcmp(sparc64_model, "SUNW,UltraSPARC-IIi-Engine")) &&
119 OF_finddevice("keyboard") != ofw_bus_get_node(dev)) {
124 device_set_desc(dev, "Keyboard controller (i8042)");
127 * The '8042' node has two identical 8 addresses wide resources
128 * which are apparently meant to be used one for the keyboard
129 * half and the other one for the mouse half. To simplify matters
130 * we use one for the command/data port resource and the other
131 * one for the status port resource as the atkbdc(4) back-end
132 * expects two struct resource rather than two bus space handles.
135 if (bus_get_resource(dev, SYS_RES_MEMORY, rid, &start, &count) != 0) {
137 "cannot determine command/data port resource\n");
140 port0 = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, start, start, 1,
144 "cannot allocate command/data port resource\n");
149 if (bus_get_resource(dev, SYS_RES_MEMORY, rid, &start, &count) != 0) {
150 device_printf(dev, "cannot determine status port resource\n");
154 start += KBD_STATUS_PORT;
155 port1 = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, start, start, 1,
158 device_printf(dev, "cannot allocate status port resource\n");
163 error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
165 device_printf(dev, "atkbdc_porbe_unit failed\n");
167 bus_release_resource(dev, SYS_RES_MEMORY, 1, port1);
169 bus_release_resource(dev, SYS_RES_MEMORY, 0, port0);
175 atkbdc_ebus_attach(device_t dev)
178 atkbdc_device_t *adi;
181 rman_res_t count, intr, start;
182 int children, error, rid, unit;
185 unit = device_get_unit(dev);
186 sc = *(atkbdc_softc_t **)device_get_softc(dev);
189 * We have to maintain two copies of the kbdc_softc struct,
190 * as the low-level console needs to have access to the
191 * keyboard controller before kbdc is probed and attached.
192 * kbdc_soft[] contains the default entry for that purpose.
195 sc = atkbdc_get_softc(unit);
198 device_set_softc(dev, sc);
202 if (bus_get_resource(dev, SYS_RES_MEMORY, rid, &start, &count) != 0) {
204 "cannot determine command/data port resource\n");
208 sc->port0 = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, start, start,
210 if (sc->port0 == NULL) {
212 "cannot allocate command/data port resource\n");
217 if (bus_get_resource(dev, SYS_RES_MEMORY, rid, &start, &count) != 0) {
218 device_printf(dev, "cannot determine status port resource\n");
222 start += KBD_STATUS_PORT;
223 sc->port1 = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, start, start,
225 if (sc->port1 == NULL) {
226 device_printf(dev, "cannot allocate status port resource\n");
231 error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1);
233 device_printf(dev, "atkbdc_attach_unit failed\n");
237 /* Attach children. */
239 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
240 child = OF_peer(child)) {
241 if ((OF_getprop_alloc(child, "name", (void **)&cname)) == -1)
245 "<%s>: only two children per 8042 supported\n",
250 adi = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV,
253 device_printf(dev, "<%s>: malloc failed\n", cname);
257 if (strcmp(cname, "kb_ps2") == 0) {
258 adi->rid = KBDC_RID_KBD;
259 dname = ATKBD_DRIVER_NAME;
260 } else if (strcmp(cname, "kdmouse") == 0) {
261 adi->rid = KBDC_RID_AUX;
262 dname = PSM_DRIVER_NAME;
264 device_printf(dev, "<%s>: unknown device\n", cname);
265 free(adi, M_ATKBDDEV);
269 intr = bus_get_resource_start(dev, SYS_RES_IRQ, adi->rid);
272 "<%s>: cannot determine interrupt resource\n",
274 free(adi, M_ATKBDDEV);
278 resource_list_init(&adi->resources);
279 resource_list_add(&adi->resources, SYS_RES_IRQ, adi->rid,
281 if ((cdev = device_add_child(dev, dname, -1)) == NULL) {
282 device_printf(dev, "<%s>: device_add_child failed\n",
284 resource_list_free(&adi->resources);
285 free(adi, M_ATKBDDEV);
289 device_set_ivars(cdev, adi);
293 error = bus_generic_attach(dev);
295 device_printf(dev, "bus_generic_attach failed\n");
302 bus_release_resource(dev, SYS_RES_MEMORY, 1, sc->port1);
304 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->port0);