]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/atkbdc/atkbdc_ebus.c
Avoid relying on header pollution from sys/refcount.h.
[FreeBSD/FreeBSD.git] / sys / dev / atkbdc / atkbdc_ebus.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5  * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
17  *
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.
28  *
29  *      from: FreeBSD: src/sys/isa/atkbdc_isa.c,v 1.31 2005/05/29 04:42:28 nyan
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_kbd.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/bus.h>
42 #include <sys/kbio.h>
43 #include <sys/malloc.h>
44
45 #include <dev/ofw/ofw_bus.h>
46
47 #include <machine/resource.h>
48 #include <machine/ver.h>
49
50 #include <sys/rman.h>
51
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>
57
58 static device_probe_t atkbdc_ebus_probe;
59 static device_attach_t atkbdc_ebus_attach;
60
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),
66
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),
80
81         { 0, 0 }
82 };
83
84 static driver_t atkbdc_ebus_driver = {
85         ATKBDC_DRIVER_NAME,
86         atkbdc_ebus_methods,
87         sizeof(atkbdc_softc_t *),
88 };
89
90 DRIVER_MODULE(atkbdc, ebus, atkbdc_ebus_driver, atkbdc_devclass, 0, 0);
91
92 static int
93 atkbdc_ebus_probe(device_t dev)
94 {
95         struct resource *port0, *port1;
96         rman_res_t count, start;
97         int error, rid;
98
99         if (strcmp(ofw_bus_get_name(dev), "8042") != 0)
100                 return (ENXIO);
101
102         /*
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.
116          */
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)) {
120                 device_disable(dev);
121                 return (ENXIO);
122         }
123
124         device_set_desc(dev, "Keyboard controller (i8042)");
125
126         /*
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.
133          */
134         rid = 0;
135         if (bus_get_resource(dev, SYS_RES_MEMORY, rid, &start, &count) != 0) {
136                 device_printf(dev,
137                     "cannot determine command/data port resource\n");
138                 return (ENXIO);
139         }
140         port0 = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, start, start, 1,
141             RF_ACTIVE);
142         if (port0 == NULL) {
143                 device_printf(dev,
144                     "cannot allocate command/data port resource\n");
145                 return (ENXIO);
146         }
147
148         rid = 1;
149         if (bus_get_resource(dev, SYS_RES_MEMORY, rid, &start, &count) != 0) {
150                 device_printf(dev, "cannot determine status port resource\n");
151                 error = ENXIO;
152                 goto fail_port0;
153         }
154         start += KBD_STATUS_PORT;
155         port1 = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, start, start, 1,
156             RF_ACTIVE);
157         if (port1 == NULL) {
158                 device_printf(dev, "cannot allocate status port resource\n");
159                 error = ENXIO;
160                 goto fail_port0;
161         }
162
163         error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
164         if (error != 0)
165                 device_printf(dev, "atkbdc_porbe_unit failed\n");
166
167         bus_release_resource(dev, SYS_RES_MEMORY, 1, port1);
168  fail_port0:
169         bus_release_resource(dev, SYS_RES_MEMORY, 0, port0);
170
171         return (error);
172 }
173
174 static int
175 atkbdc_ebus_attach(device_t dev)
176 {
177         atkbdc_softc_t *sc;
178         atkbdc_device_t *adi;
179         device_t cdev;
180         phandle_t child;
181         rman_res_t count, intr, start;
182         int children, error, rid, unit;
183         char *cname, *dname;
184
185         unit = device_get_unit(dev);
186         sc = *(atkbdc_softc_t **)device_get_softc(dev);
187         if (sc == NULL) {
188                 /*
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.
193                  * See atkbdc.c. XXX
194                  */
195                 sc = atkbdc_get_softc(unit);
196                 if (sc == NULL)
197                         return (ENOMEM);
198                 device_set_softc(dev, sc);
199         }
200
201         rid = 0;
202         if (bus_get_resource(dev, SYS_RES_MEMORY, rid, &start, &count) != 0) {
203                 device_printf(dev,
204                     "cannot determine command/data port resource\n");
205                 return (ENXIO);
206         }
207         sc->retry = 5000;
208         sc->port0 = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, start, start,
209             1, RF_ACTIVE);
210         if (sc->port0 == NULL) {
211                 device_printf(dev,
212                     "cannot allocate command/data port resource\n");
213                 return (ENXIO);
214         }
215
216         rid = 1;
217         if (bus_get_resource(dev, SYS_RES_MEMORY, rid, &start, &count) != 0) {
218                 device_printf(dev, "cannot determine status port resource\n");
219                 error = ENXIO;
220                 goto fail_port0;
221         }
222         start += KBD_STATUS_PORT;
223         sc->port1 = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, start, start,
224             1, RF_ACTIVE);
225         if (sc->port1 == NULL) {
226                 device_printf(dev, "cannot allocate status port resource\n");
227                 error = ENXIO;
228                 goto fail_port0;
229         }
230
231         error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1);
232         if (error != 0) {
233                 device_printf(dev, "atkbdc_attach_unit failed\n");
234                 goto fail_port1;
235         }
236
237         /* Attach children. */
238         children = 0;
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)
242                         continue;
243                 if (children >= 2) {
244                         device_printf(dev,
245                             "<%s>: only two children per 8042 supported\n",
246                             cname);
247                         OF_prop_free(cname);
248                         continue;
249                 }
250                 adi = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV,
251                     M_NOWAIT | M_ZERO);
252                 if (adi == NULL) {
253                         device_printf(dev, "<%s>: malloc failed\n", cname);
254                         OF_prop_free(cname);
255                         continue;
256                 }
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;
263                 } else {
264                         device_printf(dev, "<%s>: unknown device\n", cname);
265                         free(adi, M_ATKBDDEV);
266                         OF_prop_free(cname);
267                         continue;
268                 }
269                 intr = bus_get_resource_start(dev, SYS_RES_IRQ, adi->rid);
270                 if (intr == 0) {
271                         device_printf(dev,
272                             "<%s>: cannot determine interrupt resource\n",
273                             cname);
274                         free(adi, M_ATKBDDEV);
275                         OF_prop_free(cname);
276                         continue;
277                 }
278                 resource_list_init(&adi->resources);
279                 resource_list_add(&adi->resources, SYS_RES_IRQ, adi->rid,
280                     intr, intr, 1);
281                 if ((cdev = device_add_child(dev, dname, -1)) == NULL) {
282                         device_printf(dev, "<%s>: device_add_child failed\n",
283                             cname);
284                         resource_list_free(&adi->resources);
285                         free(adi, M_ATKBDDEV);
286                         OF_prop_free(cname);
287                         continue;
288                 }
289                 device_set_ivars(cdev, adi);
290                 children++;
291         }
292
293         error = bus_generic_attach(dev);
294         if (error != 0) {
295                 device_printf(dev, "bus_generic_attach failed\n");
296                 goto fail_port1;
297         }
298
299         return (0);
300
301  fail_port1:
302         bus_release_resource(dev, SYS_RES_MEMORY, 1, sc->port1);
303  fail_port0:
304         bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->port0);
305
306         return (error);
307 }