xref: /netbsd-src/sys/dev/acpi/attimer_acpi.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /* $NetBSD: attimer_acpi.c,v 1.14 2010/03/05 14:00:17 jruoho Exp $ */
2 
3 /*
4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Quentin Garnier.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 2002 Jared D. McNeill <jmcneill@invisible.ca>
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. The name of the author may not be used to endorse or promote products
42  *    derived from this software without specific prior written permission.
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
49  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
51  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54  * SUCH DAMAGE.
55  */
56 
57 /*
58  * ACPI attimer(4) attachment based on lpt_acpi.c by Jared D. McNeill.
59  */
60 
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: attimer_acpi.c,v 1.14 2010/03/05 14:00:17 jruoho Exp $");
63 
64 #include <sys/param.h>
65 #include <sys/device.h>
66 #include <sys/systm.h>
67 
68 #include <dev/acpi/acpivar.h>
69 
70 #include <dev/ic/attimervar.h>
71 
72 static int	attimer_acpi_match(device_t, cfdata_t, void *);
73 static void	attimer_acpi_attach(device_t, device_t, void *);
74 
75 CFATTACH_DECL3_NEW(attimer_acpi, sizeof(struct attimer_softc),
76     attimer_acpi_match, attimer_acpi_attach, attimer_detach, NULL, NULL, NULL,
77     DVF_DETACH_SHUTDOWN);
78 
79 /*
80  * Supported device IDs
81  */
82 
83 static const char * const attimer_acpi_ids[] = {
84 	"PNP0100",	/* AT Timer */
85 	NULL
86 };
87 
88 /*
89  * attimer_acpi_match: autoconf(9) match routine
90  */
91 static int
92 attimer_acpi_match(device_t parent, cfdata_t match, void *aux)
93 {
94 	struct acpi_attach_args *aa = aux;
95 
96 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
97 		return 0;
98 
99 	return acpi_match_hid(aa->aa_node->ad_devinfo, attimer_acpi_ids);
100 }
101 
102 /*
103  * attimer_acpi_attach: autoconf(9) attach routine
104  */
105 static void
106 attimer_acpi_attach(device_t parent, device_t self, void *aux)
107 {
108 	struct attimer_softc *sc = device_private(self);
109 	struct acpi_attach_args *aa = aux;
110 	struct acpi_resources res;
111 	struct acpi_io *io;
112 	ACPI_STATUS rv;
113 
114 	sc->sc_dev = self;
115 
116 	/* parse resources */
117 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
118 	    &res, &acpi_resource_parse_ops_default);
119 	if (ACPI_FAILURE(rv))
120 		return;
121 
122 	/* find our i/o registers */
123 	io = acpi_res_io(&res, 0);
124 	if (io == NULL) {
125 		aprint_error_dev(self,
126 		    "unable to find i/o register resource\n");
127 		goto out;
128 	}
129 
130 	sc->sc_iot = aa->aa_iot;
131 	sc->sc_size = io->ar_length;
132 	if (bus_space_map(sc->sc_iot, io->ar_base, sc->sc_size,
133 		    0, &sc->sc_ioh) != 0) {
134 		aprint_error_dev(self, "can't map i/o space\n");
135 		goto out;
136 	}
137 
138 	attimer_attach(sc);
139 
140  out:
141 	acpi_resource_cleanup(&res);
142 }
143