atkbdc_isa.c revision 233619
1168054Sflz/*-
2168054Sflz * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3168054Sflz * All rights reserved.
4168054Sflz *
5168064Sflz * Redistribution and use in source and binary forms, with or without
6168064Sflz * modification, are permitted provided that the following conditions
7168064Sflz * are met:
8168064Sflz * 1. Redistributions of source code must retain the above copyright
9168064Sflz *    notice, this list of conditions and the following disclaimer as
10168064Sflz *    the first lines of this file unmodified.
11168064Sflz * 2. Redistributions in binary form must reproduce the above copyright
12168064Sflz *    notice, this list of conditions and the following disclaimer in the
13168064Sflz *    documentation and/or other materials provided with the distribution.
14168064Sflz *
15168064Sflz * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16168064Sflz * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17168064Sflz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18168064Sflz * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19168054Sflz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20168054Sflz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21168064Sflz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22168054Sflz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23168064Sflz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24168131Sbmah * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25168113Smarcus */
26168123Snetchild
27168064Sflz#include <sys/cdefs.h>
28168054Sflz__FBSDID("$FreeBSD: head/sys/dev/atkbdc/atkbdc_isa.c 233619 2012-03-28 17:58:37Z jkim $");
29168054Sflz
30168054Sflz#include "opt_kbd.h"
31168054Sflz
32168077Sflz#include <sys/param.h>
33168077Sflz#include <sys/systm.h>
34168126Sale#include <sys/kernel.h>
35168069Sgarga#include <sys/module.h>
36168113Smarcus#include <sys/bus.h>
37168098Skrion#include <sys/malloc.h>
38168123Snetchild#include <machine/resource.h>
39168082Sgarga#include <sys/rman.h>
40168116Sclsung#include <machine/bus.h>
41168072Sehaupt
42168108Srafan#include <dev/atkbdc/atkbdc_subr.h>
43168068Serwin#include <dev/atkbdc/atkbdcreg.h>
44168072Sehaupt
45168113Smarcus#include <isa/isareg.h>
46168059Sgabor#include <isa/isavar.h>
47168098Skrion
48168054Sflzstatic int	atkbdc_isa_probe(device_t dev);
49168059Sgaborstatic int	atkbdc_isa_attach(device_t dev);
50168054Sflzstatic device_t	atkbdc_isa_add_child(device_t bus, u_int order, const char *name,
51168076Sjmelo		    int unit);
52168123Snetchildstatic struct resource *atkbdc_isa_alloc_resource(device_t dev, device_t child,
53168054Sflz		    int type, int *rid, u_long start, u_long end,
54168055Spav		    u_long count, u_int flags);
55168055Spavstatic int	atkbdc_isa_release_resource(device_t dev, device_t child,
56168098Skrion		    int type, int rid, struct resource *r);
57168055Spav
58168161Sphilipstatic device_method_t atkbdc_isa_methods[] = {
59168054Sflz	DEVMETHOD(device_probe,		atkbdc_isa_probe),
60168068Serwin	DEVMETHOD(device_attach,	atkbdc_isa_attach),
61168068Serwin	DEVMETHOD(device_suspend,	bus_generic_suspend),
62168113Smarcus	DEVMETHOD(device_resume,	bus_generic_resume),
63168055Spav
64168098Skrion	DEVMETHOD(bus_add_child,	atkbdc_isa_add_child),
65168100Smnag	DEVMETHOD(bus_print_child,	atkbdc_print_child),
66168123Snetchild	DEVMETHOD(bus_read_ivar,	atkbdc_read_ivar),
67168134Snork	DEVMETHOD(bus_write_ivar,	atkbdc_write_ivar),
68168084Sehaupt	DEVMETHOD(bus_get_resource_list,atkbdc_get_resource_list),
69168054Sflz	DEVMETHOD(bus_alloc_resource,	atkbdc_isa_alloc_resource),
70168098Skrion	DEVMETHOD(bus_release_resource,	atkbdc_isa_release_resource),
71168108Srafan	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
72168098Skrion	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
73168098Skrion	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
74168098Skrion	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
75168055Spav	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
76168068Serwin	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
77168125Stdb	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
78168061Sahze
79168069Sgarga	{ 0, 0 }
80168054Sflz};
81168054Sflz
82168064Sflzstatic driver_t atkbdc_isa_driver = {
83168064Sflz	ATKBDC_DRIVER_NAME,
84168054Sflz	atkbdc_isa_methods,
85168055Spav	sizeof(atkbdc_softc_t *),
86168055Spav};
87168055Spav
88168055Spavstatic struct isa_pnp_id atkbdc_ids[] = {
89168055Spav	{ 0x0303d041, "Keyboard controller (i8042)" },	/* PNP0303 */
90168055Spav	{ 0x2003d041, "Keyboard controller (i8042)" },	/* PNP0320 */
91168057Sahze	{ 0 }
92168055Spav};
93168125Stdb
94168125Stdbstatic int
95168108Srafanatkbdc_isa_probe(device_t dev)
96168108Srafan{
97168068Serwin	struct resource	*port0;
98168068Serwin	struct resource	*port1;
99168072Sehaupt	u_long		start;
100168072Sehaupt	u_long		count;
101168068Serwin	int		error;
102168059Sgabor	int		rid;
103168068Serwin#if defined(__i386__) || defined(__amd64__)
104168068Serwin	bus_space_tag_t	tag;
105168068Serwin	bus_space_handle_t ioh1;
106168059Sgabor	volatile int	i;
107168098Skrion	register_t	flags;
108168098Skrion#endif
109168054Sflz
110168054Sflz	/* check PnP IDs */
111168054Sflz	if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO)
112168054Sflz		return ENXIO;
113168069Sgarga
114168069Sgarga	device_set_desc(dev, "Keyboard controller (i8042)");
115168069Sgarga
116168069Sgarga	/*
117168123Snetchild	 * Adjust I/O port resources.
118168123Snetchild	 * The AT keyboard controller uses two ports (a command/data port
119168134Snork	 * 0x60 and a status port 0x64), which may be given to us in
120168134Snork	 * one resource (0x60 through 0x64) or as two separate resources
121168134Snork	 * (0x60 and 0x64). Some brain-damaged ACPI BIOS has reversed
122168134Snork	 * command/data port and status port. Furthermore, /boot/device.hints
123168134Snork	 * may contain just one port, 0x60. We shall adjust resource settings
124168098Skrion	 * so that these two ports are available as two separate resources
125168098Skrion	 * in correct order.
126168098Skrion	 */
127168098Skrion	device_quiet(dev);
128168098Skrion	rid = 0;
129168098Skrion	if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count) != 0)
130168098Skrion		return ENXIO;
131168098Skrion	if (start == IO_KBD + KBD_STATUS_PORT) {
132168113Smarcus		start = IO_KBD;
133168113Smarcus		count++;
134168113Smarcus	}
135168113Smarcus	if (count > 1)	/* adjust the count and/or start port */
136168076Sjmelo		bus_set_resource(dev, SYS_RES_IOPORT, rid, start, 1);
137168076Sjmelo	port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
138168123Snetchild	if (port0 == NULL)
139168123Snetchild		return ENXIO;
140168126Sale	rid = 1;
141168126Sale	if (bus_get_resource(dev, SYS_RES_IOPORT, rid, NULL, NULL) != 0)
142168084Sehaupt		bus_set_resource(dev, SYS_RES_IOPORT, 1,
143168084Sehaupt				 start + KBD_STATUS_PORT, 1);
144168054Sflz	port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
145168055Spav	if (port1 == NULL) {
146168055Spav		bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
147168055Spav		return ENXIO;
148168054Sflz	}
149168161Sphilip
150168161Sphilip#if defined(__i386__) || defined(__amd64__)
151168108Srafan	/*
152168108Srafan	 * Check if we really have AT keyboard controller. Poll status
153168123Snetchild	 * register until we get "all clear" indication. If no such
154168123Snetchild	 * indication comes, it probably means that there is no AT
155168054Sflz	 * keyboard controller present. Give up in such case. Check relies
156	 * on the fact that reading from non-existing in/out port returns
157	 * 0xff on i386. May or may not be true on other platforms.
158	 */
159	tag = rman_get_bustag(port0);
160	ioh1 = rman_get_bushandle(port1);
161	flags = intr_disable();
162	for (i = 0; i != 65535; i++) {
163		if ((bus_space_read_1(tag, ioh1, 0) & 0x2) == 0)
164			break;
165	}
166	intr_restore(flags);
167	if (i == 65535) {
168		bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
169		bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
170		if (bootverbose)
171			device_printf(dev, "AT keyboard controller not found\n");
172		return ENXIO;
173	}
174#endif
175
176	device_verbose(dev);
177
178	error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
179
180	bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
181	bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
182
183	return error;
184}
185
186static int
187atkbdc_isa_attach(device_t dev)
188{
189	atkbdc_softc_t	*sc;
190	int		unit;
191	int		error;
192	int		rid;
193
194	unit = device_get_unit(dev);
195	sc = *(atkbdc_softc_t **)device_get_softc(dev);
196	if (sc == NULL) {
197		/*
198		 * We have to maintain two copies of the kbdc_softc struct,
199		 * as the low-level console needs to have access to the
200		 * keyboard controller before kbdc is probed and attached.
201		 * kbdc_soft[] contains the default entry for that purpose.
202		 * See atkbdc.c. XXX
203		 */
204		sc = atkbdc_get_softc(unit);
205		if (sc == NULL)
206			return ENOMEM;
207	}
208
209	rid = 0;
210	sc->retry = 5000;
211	sc->port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
212					   RF_ACTIVE);
213	if (sc->port0 == NULL)
214		return ENXIO;
215	rid = 1;
216	sc->port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
217					   RF_ACTIVE);
218	if (sc->port1 == NULL) {
219		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
220		return ENXIO;
221	}
222
223	/*
224	 * If the device is not created by the PnP BIOS or ACPI, then
225	 * the hint for the IRQ is on the child atkbd device, not the
226	 * keyboard controller, so this can fail.
227	 */
228	rid = 0;
229	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
230
231	error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1);
232	if (error) {
233		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
234		bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->port1);
235		if (sc->irq != NULL)
236			bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
237		return error;
238	}
239	*(atkbdc_softc_t **)device_get_softc(dev) = sc;
240
241	bus_generic_probe(dev);
242	bus_generic_attach(dev);
243
244	return 0;
245}
246
247static device_t
248atkbdc_isa_add_child(device_t bus, u_int order, const char *name, int unit)
249{
250	atkbdc_device_t	*ivar;
251	atkbdc_softc_t	*sc;
252	device_t	child;
253	int		t;
254
255	sc = *(atkbdc_softc_t **)device_get_softc(bus);
256	ivar = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV,
257		M_NOWAIT | M_ZERO);
258	if (!ivar)
259		return NULL;
260
261	child = device_add_child_ordered(bus, order, name, unit);
262	if (child == NULL) {
263		free(ivar, M_ATKBDDEV);
264		return child;
265	}
266
267	resource_list_init(&ivar->resources);
268	ivar->rid = order;
269
270	/*
271	 * If the device is not created by the PnP BIOS or ACPI, refer
272	 * to device hints for IRQ.  We always populate the resource
273	 * list entry so we can use a standard bus_get_resource()
274	 * method.
275	 */
276	if (order == KBDC_RID_KBD) {
277		if (sc->irq == NULL) {
278			if (resource_int_value(name, unit, "irq", &t) != 0)
279				t = -1;
280		} else
281			t = rman_get_start(sc->irq);
282		if (t > 0)
283			resource_list_add(&ivar->resources, SYS_RES_IRQ,
284			    ivar->rid, t, t, 1);
285	}
286
287	if (resource_disabled(name, unit))
288		device_disable(child);
289
290	device_set_ivars(child, ivar);
291
292	return child;
293}
294
295struct resource *
296atkbdc_isa_alloc_resource(device_t dev, device_t child, int type, int *rid,
297    u_long start, u_long end, u_long count, u_int flags)
298{
299	atkbdc_softc_t	*sc;
300
301	sc = *(atkbdc_softc_t **)device_get_softc(dev);
302	if (type == SYS_RES_IRQ && *rid == KBDC_RID_KBD && sc->irq != NULL)
303		return (sc->irq);
304	return (bus_generic_rl_alloc_resource(dev, child, type, rid, start,
305	    end, count, flags));
306}
307
308static int
309atkbdc_isa_release_resource(device_t dev, device_t child, int type, int rid,
310    struct resource *r)
311{
312	atkbdc_softc_t	*sc;
313
314	sc = *(atkbdc_softc_t **)device_get_softc(dev);
315	if (type == SYS_RES_IRQ && rid == KBDC_RID_KBD && r == sc->irq)
316		return (0);
317	return (bus_generic_rl_release_resource(dev, child, type, rid, r));
318}
319
320DRIVER_MODULE(atkbdc, isa, atkbdc_isa_driver, atkbdc_devclass, 0, 0);
321DRIVER_MODULE(atkbdc, acpi, atkbdc_isa_driver, atkbdc_devclass, 0, 0);
322