hpet_acpi.c revision 1.9
1/* $NetBSD: hpet_acpi.c,v 1.9 2011/06/15 04:52:52 jruoho Exp $ */
2
3/*
4 * Copyright (c) 2006, 2011 Nicolas Joly
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30#include <sys/cdefs.h>
31__KERNEL_RCSID(0, "$NetBSD: hpet_acpi.c,v 1.9 2011/06/15 04:52:52 jruoho Exp $");
32
33#include <sys/param.h>
34#include <sys/device.h>
35#include <sys/time.h>
36#include <sys/timetc.h>
37
38#include <dev/acpi/acpivar.h>
39
40#include <dev/ic/hpetreg.h>
41#include <dev/ic/hpetvar.h>
42
43#define _COMPONENT	ACPI_RESOURCE_COMPONENT
44ACPI_MODULE_NAME	("acpi_hpet")
45
46static int		hpet_acpi_dev_match(device_t, cfdata_t, void *);
47static void		hpet_acpi_dev_attach(device_t, device_t, void *);
48static int		hpet_acpi_tab_match(device_t, cfdata_t, void *);
49static void		hpet_acpi_tab_attach(device_t, device_t, void *);
50static int		hpet_acpi_detach(device_t, int);
51
52static const char * const hpet_acpi_ids[] = {
53	"PNP0103",
54        NULL
55};
56
57CFATTACH_DECL_NEW(hpet_acpi_tab, sizeof(struct hpet_softc),
58    hpet_acpi_tab_match, hpet_acpi_tab_attach, hpet_acpi_detach, NULL);
59
60CFATTACH_DECL_NEW(hpet_acpi_dev, sizeof(struct hpet_softc),
61    hpet_acpi_dev_match, hpet_acpi_dev_attach, hpet_acpi_detach, NULL);
62
63static int
64hpet_acpi_tab_match(device_t parent, cfdata_t match, void *aux)
65{
66	ACPI_TABLE_HPET *hpet;
67	ACPI_STATUS rv;
68
69	rv = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hpet);
70
71	if (ACPI_FAILURE(rv))
72		return 0;
73
74	if (hpet->Address.Address == 0)
75		return 0;
76
77	if (hpet->Address.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY)
78		return 0;
79
80	return 1;
81}
82
83static void
84hpet_acpi_tab_attach(device_t parent, device_t self, void *aux)
85{
86	struct hpet_softc *sc = device_private(self);
87	struct acpi_attach_args *aa = aux;
88	ACPI_TABLE_HPET *hpet;
89	ACPI_STATUS rv;
90
91	sc->sc_mapped = false;
92
93	rv = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hpet);
94
95	if (ACPI_FAILURE(rv))
96		return;
97
98	sc->sc_memt = aa->aa_memt;
99	sc->sc_mems = HPET_WINDOW_SIZE;
100
101	if (hpet->Address.Address == 0xfed0000000000000UL) /* A quirk. */
102		hpet->Address.Address >>= 32;
103
104	if (bus_space_map(sc->sc_memt, hpet->Address.Address,
105		sc->sc_mems, 0, &sc->sc_memh) != 0) {
106		aprint_error(": failed to map mem space\n");
107		return;
108	}
109
110	sc->sc_mapped = true;
111
112	aprint_naive("\n");
113	aprint_normal(": mem 0x%"PRIx64"-0x%"PRIx64"\n",
114	    hpet->Address.Address, hpet->Address.Address + sc->sc_mems);
115
116	hpet_attach_subr(self);
117}
118
119static int
120hpet_acpi_dev_match(device_t parent, cfdata_t match, void *aux)
121{
122	struct acpi_attach_args *aa = aux;
123
124	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
125		return 0;
126
127	return acpi_match_hid(aa->aa_node->ad_devinfo, hpet_acpi_ids);
128}
129
130static void
131hpet_acpi_dev_attach(device_t parent, device_t self, void *aux)
132{
133	struct hpet_softc *sc = device_private(self);
134	struct acpi_attach_args *aa = aux;
135	struct acpi_resources res;
136	struct acpi_mem *mem;
137	ACPI_STATUS rv;
138
139	sc->sc_mapped = false;
140
141	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
142	    &res, &acpi_resource_parse_ops_default);
143
144	if (ACPI_FAILURE(rv))
145		return;
146
147	mem = acpi_res_mem(&res, 0);
148
149	if (mem == NULL) {
150		aprint_error(": failed to find mem resource\n");
151		goto out;
152	}
153
154	if (mem->ar_length < HPET_WINDOW_SIZE) {
155		aprint_error(": invalid memory region size\n");
156		goto out;
157	}
158
159	sc->sc_memt = aa->aa_memt;
160	sc->sc_mems = mem->ar_length;
161
162	if (bus_space_map(sc->sc_memt, mem->ar_base,
163		sc->sc_mems, 0, &sc->sc_memh) != 0) {
164		aprint_error(": failed to map mem space\n");
165		goto out;
166	}
167
168	sc->sc_mapped = true;
169	hpet_attach_subr(self);
170
171out:
172	acpi_resource_cleanup(&res);
173}
174
175static int
176hpet_acpi_detach(device_t self, int flags)
177{
178	struct hpet_softc *sc = device_private(self);
179	int rv;
180
181	if (sc->sc_mapped != true)
182		return 0;
183
184	rv = hpet_detach(self, flags);
185
186	if (rv != 0)
187		return rv;
188
189	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
190
191	return 0;
192}
193