xref: /freebsd/sys/dev/atkbdc/atkbdc_isa.c (revision 4f52dfbb8d6c4d446500c5b097e3806ec219fbd4)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer as
12  *    the first lines of this file unmodified.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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 OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_kbd.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/malloc.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42 #include <machine/bus.h>
43 
44 #include <dev/atkbdc/atkbdc_subr.h>
45 #include <dev/atkbdc/atkbdcreg.h>
46 
47 #include <isa/isareg.h>
48 #include <isa/isavar.h>
49 
50 static int	atkbdc_isa_probe(device_t dev);
51 static int	atkbdc_isa_attach(device_t dev);
52 static device_t	atkbdc_isa_add_child(device_t bus, u_int order, const char *name,
53 		    int unit);
54 static struct resource *atkbdc_isa_alloc_resource(device_t dev, device_t child,
55 		    int type, int *rid, rman_res_t start, rman_res_t end,
56 		    rman_res_t count, u_int flags);
57 static int	atkbdc_isa_release_resource(device_t dev, device_t child,
58 		    int type, int rid, struct resource *r);
59 
60 static device_method_t atkbdc_isa_methods[] = {
61 	DEVMETHOD(device_probe,		atkbdc_isa_probe),
62 	DEVMETHOD(device_attach,	atkbdc_isa_attach),
63 	DEVMETHOD(device_suspend,	bus_generic_suspend),
64 	DEVMETHOD(device_resume,	bus_generic_resume),
65 
66 	DEVMETHOD(bus_add_child,	atkbdc_isa_add_child),
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,	atkbdc_isa_alloc_resource),
72 	DEVMETHOD(bus_release_resource,	atkbdc_isa_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_isa_driver = {
85 	ATKBDC_DRIVER_NAME,
86 	atkbdc_isa_methods,
87 	sizeof(atkbdc_softc_t *),
88 };
89 
90 static struct isa_pnp_id atkbdc_ids[] = {
91 	{ 0x0303d041, "Keyboard controller (i8042)" },	/* PNP0303 */
92 	{ 0x0b03d041, "Keyboard controller (i8042)" },	/* PNP030B */
93 	{ 0x2003d041, "Keyboard controller (i8042)" },	/* PNP0320 */
94 	{ 0 }
95 };
96 
97 static int
98 atkbdc_isa_probe(device_t dev)
99 {
100 	struct resource	*port0;
101 	struct resource	*port1;
102 	rman_res_t	start;
103 	rman_res_t	count;
104 	int		error;
105 	int		rid;
106 #if defined(__i386__) || defined(__amd64__)
107 	bus_space_tag_t	tag;
108 	bus_space_handle_t ioh1;
109 	volatile int	i;
110 	register_t	flags;
111 #endif
112 
113 	/* check PnP IDs */
114 	if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO)
115 		return ENXIO;
116 
117 	device_set_desc(dev, "Keyboard controller (i8042)");
118 
119 	/*
120 	 * Adjust I/O port resources.
121 	 * The AT keyboard controller uses two ports (a command/data port
122 	 * 0x60 and a status port 0x64), which may be given to us in
123 	 * one resource (0x60 through 0x64) or as two separate resources
124 	 * (0x60 and 0x64). Some brain-damaged ACPI BIOS has reversed
125 	 * command/data port and status port. Furthermore, /boot/device.hints
126 	 * may contain just one port, 0x60. We shall adjust resource settings
127 	 * so that these two ports are available as two separate resources
128 	 * in correct order.
129 	 */
130 	device_quiet(dev);
131 	rid = 0;
132 	if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count) != 0)
133 		return ENXIO;
134 	if (start == IO_KBD + KBD_STATUS_PORT) {
135 		start = IO_KBD;
136 		count++;
137 	}
138 	if (count > 1)	/* adjust the count and/or start port */
139 		bus_set_resource(dev, SYS_RES_IOPORT, rid, start, 1);
140 	port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
141 	if (port0 == NULL)
142 		return ENXIO;
143 	rid = 1;
144 	if (bus_get_resource(dev, SYS_RES_IOPORT, rid, NULL, NULL) != 0)
145 		bus_set_resource(dev, SYS_RES_IOPORT, 1,
146 				 start + KBD_STATUS_PORT, 1);
147 	port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
148 	if (port1 == NULL) {
149 		bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
150 		return ENXIO;
151 	}
152 
153 #if defined(__i386__) || defined(__amd64__)
154 	/*
155 	 * Check if we really have AT keyboard controller. Poll status
156 	 * register until we get "all clear" indication. If no such
157 	 * indication comes, it probably means that there is no AT
158 	 * keyboard controller present. Give up in such case. Check relies
159 	 * on the fact that reading from non-existing in/out port returns
160 	 * 0xff on i386. May or may not be true on other platforms.
161 	 */
162 	tag = rman_get_bustag(port0);
163 	ioh1 = rman_get_bushandle(port1);
164 	flags = intr_disable();
165 	for (i = 0; i != 65535; i++) {
166 		if ((bus_space_read_1(tag, ioh1, 0) & 0x2) == 0)
167 			break;
168 	}
169 	intr_restore(flags);
170 	if (i == 65535) {
171 		bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
172 		bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
173 		if (bootverbose)
174 			device_printf(dev, "AT keyboard controller not found\n");
175 		return ENXIO;
176 	}
177 #endif
178 
179 	device_verbose(dev);
180 
181 	error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
182 
183 	bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
184 	bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
185 
186 	return error;
187 }
188 
189 static int
190 atkbdc_isa_attach(device_t dev)
191 {
192 	atkbdc_softc_t	*sc;
193 	int		unit;
194 	int		error;
195 	int		rid;
196 
197 	unit = device_get_unit(dev);
198 	sc = *(atkbdc_softc_t **)device_get_softc(dev);
199 	if (sc == NULL) {
200 		/*
201 		 * We have to maintain two copies of the kbdc_softc struct,
202 		 * as the low-level console needs to have access to the
203 		 * keyboard controller before kbdc is probed and attached.
204 		 * kbdc_soft[] contains the default entry for that purpose.
205 		 * See atkbdc.c. XXX
206 		 */
207 		sc = atkbdc_get_softc(unit);
208 		if (sc == NULL)
209 			return ENOMEM;
210 	}
211 
212 	rid = 0;
213 	sc->retry = 5000;
214 	sc->port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
215 					   RF_ACTIVE);
216 	if (sc->port0 == NULL)
217 		return ENXIO;
218 	rid = 1;
219 	sc->port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
220 					   RF_ACTIVE);
221 	if (sc->port1 == NULL) {
222 		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
223 		return ENXIO;
224 	}
225 
226 	/*
227 	 * If the device is not created by the PnP BIOS or ACPI, then
228 	 * the hint for the IRQ is on the child atkbd device, not the
229 	 * keyboard controller, so this can fail.
230 	 */
231 	rid = 0;
232 	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
233 
234 	error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1);
235 	if (error) {
236 		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
237 		bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->port1);
238 		if (sc->irq != NULL)
239 			bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
240 		return error;
241 	}
242 	*(atkbdc_softc_t **)device_get_softc(dev) = sc;
243 
244 	bus_generic_probe(dev);
245 	bus_generic_attach(dev);
246 
247 	return 0;
248 }
249 
250 static device_t
251 atkbdc_isa_add_child(device_t bus, u_int order, const char *name, int unit)
252 {
253 	atkbdc_device_t	*ivar;
254 	atkbdc_softc_t	*sc;
255 	device_t	child;
256 	int		t;
257 
258 	sc = *(atkbdc_softc_t **)device_get_softc(bus);
259 	ivar = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV,
260 		M_NOWAIT | M_ZERO);
261 	if (!ivar)
262 		return NULL;
263 
264 	child = device_add_child_ordered(bus, order, name, unit);
265 	if (child == NULL) {
266 		free(ivar, M_ATKBDDEV);
267 		return child;
268 	}
269 
270 	resource_list_init(&ivar->resources);
271 	ivar->rid = order;
272 
273 	/*
274 	 * If the device is not created by the PnP BIOS or ACPI, refer
275 	 * to device hints for IRQ.  We always populate the resource
276 	 * list entry so we can use a standard bus_get_resource()
277 	 * method.
278 	 */
279 	if (order == KBDC_RID_KBD) {
280 		if (sc->irq == NULL) {
281 			if (resource_int_value(name, unit, "irq", &t) != 0)
282 				t = -1;
283 		} else
284 			t = rman_get_start(sc->irq);
285 		if (t > 0)
286 			resource_list_add(&ivar->resources, SYS_RES_IRQ,
287 			    ivar->rid, t, t, 1);
288 	}
289 
290 	if (resource_disabled(name, unit))
291 		device_disable(child);
292 
293 	device_set_ivars(child, ivar);
294 
295 	return child;
296 }
297 
298 struct resource *
299 atkbdc_isa_alloc_resource(device_t dev, device_t child, int type, int *rid,
300     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
301 {
302 	atkbdc_softc_t	*sc;
303 
304 	sc = *(atkbdc_softc_t **)device_get_softc(dev);
305 	if (type == SYS_RES_IRQ && *rid == KBDC_RID_KBD && sc->irq != NULL)
306 		return (sc->irq);
307 	return (bus_generic_rl_alloc_resource(dev, child, type, rid, start,
308 	    end, count, flags));
309 }
310 
311 static int
312 atkbdc_isa_release_resource(device_t dev, device_t child, int type, int rid,
313     struct resource *r)
314 {
315 	atkbdc_softc_t	*sc;
316 
317 	sc = *(atkbdc_softc_t **)device_get_softc(dev);
318 	if (type == SYS_RES_IRQ && rid == KBDC_RID_KBD && r == sc->irq)
319 		return (0);
320 	return (bus_generic_rl_release_resource(dev, child, type, rid, r));
321 }
322 
323 DRIVER_MODULE(atkbdc, isa, atkbdc_isa_driver, atkbdc_devclass, 0, 0);
324 DRIVER_MODULE(atkbdc, acpi, atkbdc_isa_driver, atkbdc_devclass, 0, 0);
325 ISA_PNP_INFO(atkbdc_ids);
326