xref: /freebsd/sys/dev/atkbdc/atkbdc_isa.c (revision 3157ba21)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_kbd.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/malloc.h>
38 #include <machine/resource.h>
39 #include <sys/rman.h>
40 #include <machine/bus.h>
41 
42 #include <dev/atkbdc/atkbdc_subr.h>
43 #include <dev/atkbdc/atkbdcreg.h>
44 
45 #include <isa/isareg.h>
46 #include <isa/isavar.h>
47 
48 static int	atkbdc_isa_probe(device_t dev);
49 static int	atkbdc_isa_attach(device_t dev);
50 static device_t	atkbdc_isa_add_child(device_t bus, int order, const char *name,
51 		    int unit);
52 
53 static device_method_t atkbdc_isa_methods[] = {
54 	DEVMETHOD(device_probe,		atkbdc_isa_probe),
55 	DEVMETHOD(device_attach,	atkbdc_isa_attach),
56 	DEVMETHOD(device_suspend,	bus_generic_suspend),
57 	DEVMETHOD(device_resume,	bus_generic_resume),
58 
59 	DEVMETHOD(bus_add_child,	atkbdc_isa_add_child),
60 	DEVMETHOD(bus_print_child,	atkbdc_print_child),
61 	DEVMETHOD(bus_read_ivar,	atkbdc_read_ivar),
62 	DEVMETHOD(bus_write_ivar,	atkbdc_write_ivar),
63 	DEVMETHOD(bus_get_resource_list,atkbdc_get_resource_list),
64 	DEVMETHOD(bus_alloc_resource,	bus_generic_rl_alloc_resource),
65 	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
66 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
67 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
68 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
69 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
70 	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
71 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
72 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
73 
74 	{ 0, 0 }
75 };
76 
77 static driver_t atkbdc_isa_driver = {
78 	ATKBDC_DRIVER_NAME,
79 	atkbdc_isa_methods,
80 	sizeof(atkbdc_softc_t *),
81 };
82 
83 static struct isa_pnp_id atkbdc_ids[] = {
84 	{ 0x0303d041, "Keyboard controller (i8042)" },	/* PNP0303 */
85 	{ 0 }
86 };
87 
88 static int
89 atkbdc_isa_probe(device_t dev)
90 {
91 	struct resource	*port0;
92 	struct resource	*port1;
93 	u_long		start;
94 	u_long		count;
95 	int		error;
96 	int		rid;
97 #if defined(__i386__) || defined(__amd64__)
98 	bus_space_tag_t	tag;
99 	bus_space_handle_t ioh1;
100 	volatile int	i;
101 	register_t	flags;
102 #endif
103 
104 	/* check PnP IDs */
105 	if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO)
106 		return ENXIO;
107 
108 	device_set_desc(dev, "Keyboard controller (i8042)");
109 
110 	/*
111 	 * Adjust I/O port resources.
112 	 * The AT keyboard controller uses two ports (a command/data port
113 	 * 0x60 and a status port 0x64), which may be given to us in
114 	 * one resource (0x60 through 0x64) or as two separate resources
115 	 * (0x60 and 0x64). Some brain-damaged ACPI BIOS has reversed
116 	 * command/data port and status port. Furthermore, /boot/device.hints
117 	 * may contain just one port, 0x60. We shall adjust resource settings
118 	 * so that these two ports are available as two separate resources
119 	 * in correct order.
120 	 */
121 	device_quiet(dev);
122 	rid = 0;
123 	if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count) != 0)
124 		return ENXIO;
125 	if (start == IO_KBD + KBD_STATUS_PORT) {
126 		start = IO_KBD;
127 		count++;
128 	}
129 	if (count > 1)	/* adjust the count and/or start port */
130 		bus_set_resource(dev, SYS_RES_IOPORT, rid, start, 1);
131 	port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
132 	if (port0 == NULL)
133 		return ENXIO;
134 	rid = 1;
135 	if (bus_get_resource(dev, SYS_RES_IOPORT, rid, NULL, NULL) != 0)
136 		bus_set_resource(dev, SYS_RES_IOPORT, 1,
137 				 start + KBD_STATUS_PORT, 1);
138 	port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
139 	if (port1 == NULL) {
140 		bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
141 		return ENXIO;
142 	}
143 
144 #if defined(__i386__) || defined(__amd64__)
145 	/*
146 	 * Check if we really have AT keyboard controller. Poll status
147 	 * register until we get "all clear" indication. If no such
148 	 * indication comes, it probably means that there is no AT
149 	 * keyboard controller present. Give up in such case. Check relies
150 	 * on the fact that reading from non-existing in/out port returns
151 	 * 0xff on i386. May or may not be true on other platforms.
152 	 */
153 	tag = rman_get_bustag(port0);
154 	ioh1 = rman_get_bushandle(port1);
155 	flags = intr_disable();
156 	for (i = 0; i != 65535; i++) {
157 		if ((bus_space_read_1(tag, ioh1, 0) & 0x2) == 0)
158 			break;
159 	}
160 	intr_restore(flags);
161 	if (i == 65535) {
162 		bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
163 		bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
164 		if (bootverbose)
165 			device_printf(dev, "AT keyboard controller not found\n");
166 		return ENXIO;
167 	}
168 #endif
169 
170 	device_verbose(dev);
171 
172 	error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
173 	if (error == 0)
174 		bus_generic_probe(dev);
175 
176 	bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
177 	bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
178 
179 	return error;
180 }
181 
182 static int
183 atkbdc_isa_attach(device_t dev)
184 {
185 	atkbdc_softc_t	*sc;
186 	int		unit;
187 	int		error;
188 	int		rid;
189 
190 	unit = device_get_unit(dev);
191 	sc = *(atkbdc_softc_t **)device_get_softc(dev);
192 	if (sc == NULL) {
193 		/*
194 		 * We have to maintain two copies of the kbdc_softc struct,
195 		 * as the low-level console needs to have access to the
196 		 * keyboard controller before kbdc is probed and attached.
197 		 * kbdc_soft[] contains the default entry for that purpose.
198 		 * See atkbdc.c. XXX
199 		 */
200 		sc = atkbdc_get_softc(unit);
201 		if (sc == NULL)
202 			return ENOMEM;
203 	}
204 
205 	rid = 0;
206 	sc->retry = 5000;
207 	sc->port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
208 					   RF_ACTIVE);
209 	if (sc->port0 == NULL)
210 		return ENXIO;
211 	rid = 1;
212 	sc->port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
213 					   RF_ACTIVE);
214 	if (sc->port1 == NULL) {
215 		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
216 		return ENXIO;
217 	}
218 
219 	error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1);
220 	if (error) {
221 		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
222 		bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->port1);
223 		return error;
224 	}
225 	*(atkbdc_softc_t **)device_get_softc(dev) = sc;
226 
227 	bus_generic_attach(dev);
228 
229 	return 0;
230 }
231 
232 static device_t
233 atkbdc_isa_add_child(device_t bus, int order, const char *name, int unit)
234 {
235 	atkbdc_device_t	*ivar;
236 	device_t	child;
237 	int		t;
238 
239 	ivar = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV,
240 		M_NOWAIT | M_ZERO);
241 	if (!ivar)
242 		return NULL;
243 
244 	child = device_add_child_ordered(bus, order, name, unit);
245 	if (child == NULL) {
246 		free(ivar, M_ATKBDDEV);
247 		return child;
248 	}
249 
250 	resource_list_init(&ivar->resources);
251 	ivar->rid = order;
252 
253 	/*
254 	 * If the device is not created by the PnP BIOS or ACPI,
255 	 * refer to device hints for IRQ.
256 	 */
257 	if (ISA_PNP_PROBE(device_get_parent(bus), bus, atkbdc_ids) != 0) {
258 		if (resource_int_value(name, unit, "irq", &t) != 0)
259 			t = -1;
260 	} else {
261 		t = bus_get_resource_start(bus, SYS_RES_IRQ, ivar->rid);
262 	}
263 	if (t > 0)
264 		resource_list_add(&ivar->resources, SYS_RES_IRQ, ivar->rid,
265 				  t, t, 1);
266 
267 	if (resource_disabled(name, unit))
268 		device_disable(child);
269 
270 	device_set_ivars(child, ivar);
271 
272 	return child;
273 }
274 
275 DRIVER_MODULE(atkbdc, isa, atkbdc_isa_driver, atkbdc_devclass, 0, 0);
276 DRIVER_MODULE(atkbdc, acpi, atkbdc_isa_driver, atkbdc_devclass, 0, 0);
277