hpet_acpi.c revision 1.6
1/* $NetBSD: hpet_acpi.c,v 1.6 2011/06/14 13:59:23 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.6 2011/06/14 13:59:23 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#include <dev/ic/hpetvar.h>
40
41#define _COMPONENT	ACPI_RESOURCE_COMPONENT
42ACPI_MODULE_NAME	("acpi_hpet")
43
44#define HPET_MEM_WIDTH	0x3ff   /* Expected memory region size. */
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 *);
50
51static const char * const hpet_acpi_ids[] = {
52	"PNP0103",
53        NULL
54};
55
56CFATTACH_DECL_NEW(hpet_acpi_tab, sizeof(struct hpet_softc),
57    hpet_acpi_tab_match, hpet_acpi_tab_attach, NULL, NULL);
58
59CFATTACH_DECL_NEW(hpet_acpi_dev, sizeof(struct hpet_softc),
60    hpet_acpi_dev_match, hpet_acpi_dev_attach, NULL, NULL);
61
62static int
63hpet_acpi_tab_match(device_t parent, cfdata_t match, void *aux)
64{
65	ACPI_TABLE_HPET *hpet;
66	ACPI_STATUS rv;
67
68	rv = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hpet);
69
70	if (ACPI_FAILURE(rv))
71		return 0;
72
73	if (hpet->Address.Address == 0)
74		return 0;
75
76	if (hpet->Address.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY)
77		return 0;
78
79	return 1;
80}
81
82static void
83hpet_acpi_tab_attach(device_t parent, device_t self, void *aux)
84{
85	struct hpet_softc *sc = device_private(self);
86	struct acpi_attach_args *aa = aux;
87	ACPI_TABLE_HPET *hpet;
88	ACPI_STATUS rv;
89
90	rv = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hpet);
91
92	if (ACPI_FAILURE(rv))
93		return;
94
95	sc->sc_memt = aa->aa_memt;
96
97	if (hpet->Address.Address == 0xfed0000000000000UL) /* A quirk. */
98		hpet->Address.Address >>= 32;
99
100	if (bus_space_map(sc->sc_memt, hpet->Address.Address,
101		HPET_MEM_WIDTH, 0, &sc->sc_memh) != 0) {
102		aprint_error(": failed to map mem space\n");
103		return;
104	}
105
106	aprint_naive("\n");
107	aprint_normal(": mem 0x%"PRIx64"-0x%"PRIx64"\n",
108	    hpet->Address.Address, hpet->Address.Address + HPET_MEM_WIDTH);
109
110	hpet_attach_subr(self);
111}
112
113static int
114hpet_acpi_dev_match(device_t parent, cfdata_t match, void *aux)
115{
116	struct acpi_attach_args *aa = aux;
117
118	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
119		return 0;
120
121	return acpi_match_hid(aa->aa_node->ad_devinfo, hpet_acpi_ids);
122}
123
124static void
125hpet_acpi_dev_attach(device_t parent, device_t self, void *aux)
126{
127	struct hpet_softc *sc = device_private(self);
128	struct acpi_attach_args *aa = aux;
129	struct acpi_resources res;
130	struct acpi_mem *mem;
131	ACPI_STATUS rv;
132
133	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
134	    &res, &acpi_resource_parse_ops_default);
135
136	if (ACPI_FAILURE(rv))
137		return;
138
139	mem = acpi_res_mem(&res, 0);
140
141	if (mem == NULL) {
142		aprint_error(": failed to find mem resource\n");
143		goto out;
144	}
145
146	if (mem->ar_length < HPET_MEM_WIDTH) {
147		aprint_error(": invalid memory region size\n");
148		goto out;
149	}
150
151	sc->sc_memt = aa->aa_memt;
152
153	if (bus_space_map(sc->sc_memt, mem->ar_base,
154		mem->ar_length, 0, &sc->sc_memh) != 0) {
155		aprint_error(": failed to map mem space\n");
156		goto out;
157	}
158
159	hpet_attach_subr(self);
160
161out:
162	acpi_resource_cleanup(&res);
163}
164