1 /* $NetBSD: attimer_acpi.c,v 1.15 2021/01/29 15:49:55 thorpej 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.15 2021/01/29 15:49:55 thorpej 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 struct device_compatible_entry compat_data[] = {
84 { .compat = "PNP0100" }, /* AT Timer */
85 DEVICE_COMPAT_EOL
86 };
87
88 /*
89 * attimer_acpi_match: autoconf(9) match routine
90 */
91 static int
attimer_acpi_match(device_t parent,cfdata_t match,void * aux)92 attimer_acpi_match(device_t parent, cfdata_t match, void *aux)
93 {
94 struct acpi_attach_args *aa = aux;
95
96 return acpi_compatible_match(aa, compat_data);
97 }
98
99 /*
100 * attimer_acpi_attach: autoconf(9) attach routine
101 */
102 static void
attimer_acpi_attach(device_t parent,device_t self,void * aux)103 attimer_acpi_attach(device_t parent, device_t self, void *aux)
104 {
105 struct attimer_softc *sc = device_private(self);
106 struct acpi_attach_args *aa = aux;
107 struct acpi_resources res;
108 struct acpi_io *io;
109 ACPI_STATUS rv;
110
111 sc->sc_dev = self;
112
113 /* parse resources */
114 rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
115 &res, &acpi_resource_parse_ops_default);
116 if (ACPI_FAILURE(rv))
117 return;
118
119 /* find our i/o registers */
120 io = acpi_res_io(&res, 0);
121 if (io == NULL) {
122 aprint_error_dev(self,
123 "unable to find i/o register resource\n");
124 goto out;
125 }
126
127 sc->sc_iot = aa->aa_iot;
128 sc->sc_size = io->ar_length;
129 if (bus_space_map(sc->sc_iot, io->ar_base, sc->sc_size,
130 0, &sc->sc_ioh) != 0) {
131 aprint_error_dev(self, "can't map i/o space\n");
132 goto out;
133 }
134
135 attimer_attach(sc);
136
137 out:
138 acpi_resource_cleanup(&res);
139 }
140