atkbdc_isa.c revision 136520
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: head/sys/dev/atkbdc/atkbdc_isa.c 136520 2004-10-14 22:21:59Z njl $");
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/bus_pio.h>
39#include <machine/bus.h>
40#include <machine/resource.h>
41#include <sys/rman.h>
42
43#include <dev/kbd/atkbdcreg.h>
44
45#include <isa/isareg.h>
46#include <isa/isavar.h>
47
48static MALLOC_DEFINE(M_ATKBDDEV, "atkbddev", "AT Keyboard device");
49
50/* children */
51typedef struct atkbdc_device {
52	struct resource_list resources;
53	int rid;
54	u_int32_t vendorid;
55	u_int32_t serial;
56	u_int32_t logicalid;
57	u_int32_t compatid;
58} atkbdc_device_t;
59
60/* kbdc */
61static devclass_t atkbdc_devclass;
62
63static int	atkbdc_probe(device_t dev);
64static int	atkbdc_attach(device_t dev);
65static device_t	atkbdc_add_child(device_t bus, int order, char *name,
66				 int unit);
67static int	atkbdc_print_child(device_t bus, device_t dev);
68static int	atkbdc_read_ivar(device_t bus, device_t dev, int index,
69				 uintptr_t *val);
70static int	atkbdc_write_ivar(device_t bus, device_t dev, int index,
71				  uintptr_t val);
72static struct resource_list
73		*atkbdc_get_resource_list (device_t bus, device_t dev);
74static struct resource
75		*atkbdc_alloc_resource(device_t bus, device_t dev, int type,
76				       int *rid, u_long start, u_long end,
77				       u_long count, u_int flags);
78static int	atkbdc_release_resource(device_t bus, device_t dev, int type,
79					int rid, struct resource *res);
80
81static device_method_t atkbdc_methods[] = {
82	DEVMETHOD(device_probe,		atkbdc_probe),
83	DEVMETHOD(device_attach,	atkbdc_attach),
84	DEVMETHOD(device_suspend,	bus_generic_suspend),
85	DEVMETHOD(device_resume,	bus_generic_resume),
86
87	DEVMETHOD(bus_add_child,	atkbdc_add_child),
88	DEVMETHOD(bus_print_child,	atkbdc_print_child),
89	DEVMETHOD(bus_read_ivar,	atkbdc_read_ivar),
90	DEVMETHOD(bus_write_ivar,	atkbdc_write_ivar),
91	DEVMETHOD(bus_get_resource_list,atkbdc_get_resource_list),
92	DEVMETHOD(bus_alloc_resource,	atkbdc_alloc_resource),
93	DEVMETHOD(bus_release_resource,	atkbdc_release_resource),
94	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
95	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
96	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
97	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
98	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
99	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
100	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
101
102	{ 0, 0 }
103};
104
105static driver_t atkbdc_driver = {
106	ATKBDC_DRIVER_NAME,
107	atkbdc_methods,
108	sizeof(atkbdc_softc_t *),
109};
110
111static struct isa_pnp_id atkbdc_ids[] = {
112	{ 0x0303d041, "Keyboard controller (i8042)" },	/* PNP0303 */
113	{ 0 }
114};
115
116static int
117atkbdc_probe(device_t dev)
118{
119	struct resource	*port0;
120	struct resource	*port1;
121	u_long		start;
122	u_long		count;
123	int		error;
124	int		rid;
125
126	/* check PnP IDs */
127	if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO)
128		return ENXIO;
129
130	device_set_desc(dev, "Keyboard controller (i8042)");
131
132	/*
133	 * Adjust I/O port resources.
134	 * The AT keyboard controller uses two ports (a command/data port
135	 * 0x60 and a status port 0x64), which may be given to us in
136	 * one resource (0x60 through 0x64) or as two separate resources
137	 * (0x60 and 0x64). Furthermore, /boot/device.hints may contain
138	 * just one port, 0x60. We shall adjust resource settings
139	 * so that these two ports are available as two separate resources.
140	 */
141	device_quiet(dev);
142	rid = 0;
143	if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count) != 0)
144		return ENXIO;
145	if (count > 1)	/* adjust the count */
146		bus_set_resource(dev, SYS_RES_IOPORT, rid, start, 1);
147	port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
148	if (port0 == NULL)
149		return ENXIO;
150	rid = 1;
151	if (bus_get_resource(dev, SYS_RES_IOPORT, rid, NULL, NULL) != 0)
152		bus_set_resource(dev, SYS_RES_IOPORT, 1,
153				 start + KBD_STATUS_PORT, 1);
154	port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
155	if (port1 == NULL) {
156		bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
157		return ENXIO;
158	}
159	device_verbose(dev);
160
161	error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
162	if (error == 0)
163		bus_generic_probe(dev);
164
165	bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
166	bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
167
168	return error;
169}
170
171static int
172atkbdc_attach(device_t dev)
173{
174	atkbdc_softc_t	*sc;
175	int		unit;
176	int		error;
177	int		rid;
178
179	unit = device_get_unit(dev);
180	sc = *(atkbdc_softc_t **)device_get_softc(dev);
181	if (sc == NULL) {
182		/*
183		 * We have to maintain two copies of the kbdc_softc struct,
184		 * as the low-level console needs to have access to the
185		 * keyboard controller before kbdc is probed and attached.
186		 * kbdc_soft[] contains the default entry for that purpose.
187		 * See atkbdc.c. XXX
188		 */
189		sc = atkbdc_get_softc(unit);
190		if (sc == NULL)
191			return ENOMEM;
192	}
193
194	rid = 0;
195	sc->port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
196					   RF_ACTIVE);
197	if (sc->port0 == NULL)
198		return ENXIO;
199	rid = 1;
200	sc->port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
201					   RF_ACTIVE);
202	if (sc->port1 == NULL) {
203		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
204		return ENXIO;
205	}
206
207	error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1);
208	if (error) {
209		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
210		bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->port1);
211		return error;
212	}
213	*(atkbdc_softc_t **)device_get_softc(dev) = sc;
214
215	bus_generic_attach(dev);
216
217	return 0;
218}
219
220static device_t
221atkbdc_add_child(device_t bus, int order, char *name, int unit)
222{
223	atkbdc_device_t	*ivar;
224	device_t	child;
225	int		t;
226
227	ivar = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV,
228		M_NOWAIT | M_ZERO);
229	if (!ivar)
230		return NULL;
231
232	child = device_add_child_ordered(bus, order, name, unit);
233	if (child == NULL) {
234		free(ivar, M_ATKBDDEV);
235		return child;
236	}
237
238	resource_list_init(&ivar->resources);
239	ivar->rid = order;
240
241	/*
242	 * If the device is not created by the PnP BIOS or ACPI,
243	 * refer to device hints for IRQ.
244	 */
245	if (ISA_PNP_PROBE(device_get_parent(bus), bus, atkbdc_ids) != 0) {
246		if (resource_int_value(name, unit, "irq", &t) != 0)
247			t = -1;
248	} else {
249		t = bus_get_resource_start(bus, SYS_RES_IRQ, ivar->rid);
250	}
251	if (t > 0)
252		resource_list_add(&ivar->resources, SYS_RES_IRQ, ivar->rid,
253				  t, t, 1);
254
255	if (resource_disabled(name, unit))
256		device_disable(child);
257
258	device_set_ivars(child, ivar);
259
260	return child;
261}
262
263static int
264atkbdc_print_child(device_t bus, device_t dev)
265{
266	atkbdc_device_t *kbdcdev;
267	u_long irq;
268	int flags;
269	int retval = 0;
270
271	kbdcdev = (atkbdc_device_t *)device_get_ivars(dev);
272
273	retval += bus_print_child_header(bus, dev);
274	flags = device_get_flags(dev);
275	if (flags != 0)
276		retval += printf(" flags 0x%x", flags);
277	irq = bus_get_resource_start(dev, SYS_RES_IRQ, kbdcdev->rid);
278	if (irq != 0)
279		retval += printf(" irq %ld", irq);
280	retval += bus_print_child_footer(bus, dev);
281
282	return (retval);
283}
284
285static int
286atkbdc_read_ivar(device_t bus, device_t dev, int index, uintptr_t *val)
287{
288	atkbdc_device_t *ivar;
289
290	ivar = (atkbdc_device_t *)device_get_ivars(dev);
291	switch (index) {
292	case KBDC_IVAR_VENDORID:
293		*val = (u_long)ivar->vendorid;
294		break;
295	case KBDC_IVAR_SERIAL:
296		*val = (u_long)ivar->serial;
297		break;
298	case KBDC_IVAR_LOGICALID:
299		*val = (u_long)ivar->logicalid;
300		break;
301	case KBDC_IVAR_COMPATID:
302		*val = (u_long)ivar->compatid;
303		break;
304	default:
305		return ENOENT;
306	}
307	return 0;
308}
309
310static int
311atkbdc_write_ivar(device_t bus, device_t dev, int index, uintptr_t val)
312{
313	atkbdc_device_t *ivar;
314
315	ivar = (atkbdc_device_t *)device_get_ivars(dev);
316	switch (index) {
317	case KBDC_IVAR_VENDORID:
318		ivar->vendorid = (u_int32_t)val;
319		break;
320	case KBDC_IVAR_SERIAL:
321		ivar->serial = (u_int32_t)val;
322		break;
323	case KBDC_IVAR_LOGICALID:
324		ivar->logicalid = (u_int32_t)val;
325		break;
326	case KBDC_IVAR_COMPATID:
327		ivar->compatid = (u_int32_t)val;
328		break;
329	default:
330		return ENOENT;
331	}
332	return 0;
333}
334
335static struct resource_list
336*atkbdc_get_resource_list (device_t bus, device_t dev)
337{
338	atkbdc_device_t *ivar;
339
340	ivar = (atkbdc_device_t *)device_get_ivars(dev);
341	return &ivar->resources;
342}
343
344static struct resource
345*atkbdc_alloc_resource(device_t bus, device_t dev, int type, int *rid,
346		       u_long start, u_long end, u_long count, u_int flags)
347{
348	atkbdc_device_t *ivar;
349
350	ivar = (atkbdc_device_t *)device_get_ivars(dev);
351	return resource_list_alloc(&ivar->resources, bus, dev, type, rid,
352				   start, end, count, flags);
353}
354
355static int
356atkbdc_release_resource(device_t bus, device_t dev, int type, int rid,
357			struct resource *res)
358{
359	atkbdc_device_t *ivar;
360
361	ivar = (atkbdc_device_t *)device_get_ivars(dev);
362	return resource_list_release(&ivar->resources, bus, dev, type, rid,
363				     res);
364}
365
366DRIVER_MODULE(atkbdc, isa, atkbdc_driver, atkbdc_devclass, 0, 0);
367DRIVER_MODULE(atkbdc, acpi, atkbdc_driver, atkbdc_devclass, 0, 0);
368