hpet_acpi.c revision 1.11
11.11Sjruoho/* $NetBSD: hpet_acpi.c,v 1.11 2011/06/15 09:02:38 jruoho Exp $ */
21.1Snjoly
31.1Snjoly/*
41.10Sjruoho * Copyright (c) 2011 Jukka Ruohonen
51.10Sjruoho * Copyright (c) 2006 Nicolas Joly
61.1Snjoly * All rights reserved.
71.1Snjoly *
81.1Snjoly * Redistribution and use in source and binary forms, with or without
91.1Snjoly * modification, are permitted provided that the following conditions
101.1Snjoly * are met:
111.1Snjoly * 1. Redistributions of source code must retain the above copyright
121.1Snjoly *    notice, this list of conditions and the following disclaimer.
131.1Snjoly * 2. Redistributions in binary form must reproduce the above copyright
141.1Snjoly *    notice, this list of conditions and the following disclaimer in the
151.1Snjoly *    documentation and/or other materials provided with the distribution.
161.1Snjoly * 3. The name of the author may not be used to endorse or promote products
171.1Snjoly *    derived from this software without specific prior written permission.
181.1Snjoly *
191.1Snjoly * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
201.1Snjoly * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Snjoly * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Snjoly * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Snjoly * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Snjoly * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Snjoly * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Snjoly * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Snjoly * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Snjoly * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Snjoly * POSSIBILITY OF SUCH DAMAGE.
301.1Snjoly */
311.1Snjoly#include <sys/cdefs.h>
321.11Sjruoho__KERNEL_RCSID(0, "$NetBSD: hpet_acpi.c,v 1.11 2011/06/15 09:02:38 jruoho Exp $");
331.1Snjoly
341.1Snjoly#include <sys/param.h>
351.5Sjruoho#include <sys/device.h>
361.5Sjruoho#include <sys/time.h>
371.5Sjruoho#include <sys/timetc.h>
381.1Snjoly
391.1Snjoly#include <dev/acpi/acpivar.h>
401.9Sjruoho
411.9Sjruoho#include <dev/ic/hpetreg.h>
421.1Snjoly#include <dev/ic/hpetvar.h>
431.1Snjoly
441.6Sjruoho#define _COMPONENT	ACPI_RESOURCE_COMPONENT
451.6SjruohoACPI_MODULE_NAME	("acpi_hpet")
461.1Snjoly
471.10Sjruohostatic int		hpet_acpi_tab_match(device_t, cfdata_t, void *);
481.10Sjruohostatic void		hpet_acpi_tab_attach(device_t, device_t, void *);
491.10Sjruohostatic bus_addr_t	hpet_acpi_tab_addr(void);
501.6Sjruohostatic int		hpet_acpi_dev_match(device_t, cfdata_t, void *);
511.6Sjruohostatic void		hpet_acpi_dev_attach(device_t, device_t, void *);
521.10Sjruohostatic bus_addr_t	hpet_acpi_dev_addr(device_t, void *, bus_size_t *);
531.7Sjruohostatic int		hpet_acpi_detach(device_t, int);
541.1Snjoly
551.1Snjolystatic const char * const hpet_acpi_ids[] = {
561.1Snjoly	"PNP0103",
571.6Sjruoho        NULL
581.1Snjoly};
591.1Snjoly
601.6SjruohoCFATTACH_DECL_NEW(hpet_acpi_tab, sizeof(struct hpet_softc),
611.7Sjruoho    hpet_acpi_tab_match, hpet_acpi_tab_attach, hpet_acpi_detach, NULL);
621.6Sjruoho
631.6SjruohoCFATTACH_DECL_NEW(hpet_acpi_dev, sizeof(struct hpet_softc),
641.7Sjruoho    hpet_acpi_dev_match, hpet_acpi_dev_attach, hpet_acpi_detach, NULL);
651.6Sjruoho
661.6Sjruohostatic int
671.6Sjruohohpet_acpi_tab_match(device_t parent, cfdata_t match, void *aux)
681.6Sjruoho{
691.10Sjruoho	struct acpi_attach_args *aa = aux;
701.10Sjruoho	bus_space_handle_t bh;
711.10Sjruoho	bus_space_tag_t bt;
721.10Sjruoho	bus_addr_t addr;
731.6Sjruoho
741.10Sjruoho	addr = hpet_acpi_tab_addr();
751.6Sjruoho
761.10Sjruoho	if (addr == 0)
771.6Sjruoho		return 0;
781.6Sjruoho
791.10Sjruoho	bt = aa->aa_memt;
801.10Sjruoho
811.10Sjruoho	if (bus_space_map(bt, addr, HPET_WINDOW_SIZE, 0, &bh) != 0)
821.6Sjruoho		return 0;
831.6Sjruoho
841.10Sjruoho	bus_space_unmap(bt, bh, HPET_WINDOW_SIZE);
851.6Sjruoho
861.6Sjruoho	return 1;
871.6Sjruoho}
881.6Sjruoho
891.6Sjruohostatic void
901.6Sjruohohpet_acpi_tab_attach(device_t parent, device_t self, void *aux)
911.6Sjruoho{
921.6Sjruoho	struct hpet_softc *sc = device_private(self);
931.6Sjruoho	struct acpi_attach_args *aa = aux;
941.10Sjruoho	bus_addr_t addr;
951.6Sjruoho
961.7Sjruoho	sc->sc_mapped = false;
971.10Sjruoho	addr = hpet_acpi_tab_addr();
981.7Sjruoho
991.10Sjruoho	if (addr == 0) {
1001.10Sjruoho		aprint_error(": failed to get address\n");
1011.6Sjruoho		return;
1021.10Sjruoho	}
1031.6Sjruoho
1041.6Sjruoho	sc->sc_memt = aa->aa_memt;
1051.9Sjruoho	sc->sc_mems = HPET_WINDOW_SIZE;
1061.6Sjruoho
1071.10Sjruoho	if (bus_space_map(sc->sc_memt, addr, sc->sc_mems, 0, &sc->sc_memh)) {
1081.6Sjruoho		aprint_error(": failed to map mem space\n");
1091.6Sjruoho		return;
1101.6Sjruoho	}
1111.6Sjruoho
1121.10Sjruoho	aprint_naive("\n");
1131.10Sjruoho	aprint_normal(": high precision event timer (mem 0x%08x-0x%08x)\n",
1141.10Sjruoho	    (uint32_t)addr, (uint32_t)addr + HPET_WINDOW_SIZE);
1151.10Sjruoho
1161.7Sjruoho	sc->sc_mapped = true;
1171.10Sjruoho	hpet_attach_subr(self);
1181.10Sjruoho}
1191.10Sjruoho
1201.10Sjruohostatic bus_addr_t
1211.10Sjruohohpet_acpi_tab_addr(void)
1221.10Sjruoho{
1231.10Sjruoho	ACPI_TABLE_HPET *hpet;
1241.10Sjruoho	ACPI_STATUS rv;
1251.10Sjruoho
1261.10Sjruoho	rv = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hpet);
1271.10Sjruoho
1281.10Sjruoho	if (ACPI_FAILURE(rv))
1291.10Sjruoho		return 0;
1301.10Sjruoho
1311.10Sjruoho	if (hpet->Address.Address == 0)
1321.10Sjruoho		return 0;
1331.7Sjruoho
1341.10Sjruoho	if (hpet->Address.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY)
1351.10Sjruoho		return 0;
1361.10Sjruoho
1371.10Sjruoho	if (hpet->Address.Address == 0xfed0000000000000UL) /* A quirk. */
1381.10Sjruoho		hpet->Address.Address >>= 32;
1391.6Sjruoho
1401.10Sjruoho	return hpet->Address.Address;
1411.6Sjruoho}
1421.6Sjruoho
1431.1Snjolystatic int
1441.6Sjruohohpet_acpi_dev_match(device_t parent, cfdata_t match, void *aux)
1451.1Snjoly{
1461.1Snjoly	struct acpi_attach_args *aa = aux;
1471.10Sjruoho	bus_space_handle_t bh;
1481.10Sjruoho	bus_space_tag_t bt;
1491.10Sjruoho	bus_size_t len = 0;
1501.10Sjruoho	bus_addr_t addr;
1511.1Snjoly
1521.1Snjoly	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
1531.1Snjoly		return 0;
1541.1Snjoly
1551.10Sjruoho	if (acpi_match_hid(aa->aa_node->ad_devinfo, hpet_acpi_ids) == 0)
1561.10Sjruoho		return 0;
1571.10Sjruoho
1581.10Sjruoho	addr = hpet_acpi_dev_addr(parent, aa, &len);
1591.10Sjruoho
1601.10Sjruoho	if (addr == 0 || len == 0)
1611.10Sjruoho		return 0;
1621.10Sjruoho
1631.10Sjruoho	bt = aa->aa_memt;
1641.10Sjruoho
1651.10Sjruoho	if (bus_space_map(bt, addr, len, 0, &bh) == 0) {
1661.10Sjruoho		bus_space_unmap(bt, bh, len);
1671.10Sjruoho		return 1;
1681.10Sjruoho	}
1691.10Sjruoho
1701.10Sjruoho	return 0;
1711.1Snjoly}
1721.1Snjoly
1731.1Snjolystatic void
1741.6Sjruohohpet_acpi_dev_attach(device_t parent, device_t self, void *aux)
1751.1Snjoly{
1761.3Sxtraeme	struct hpet_softc *sc = device_private(self);
1771.1Snjoly	struct acpi_attach_args *aa = aux;
1781.10Sjruoho	bus_addr_t addr;
1791.10Sjruoho
1801.10Sjruoho	sc->sc_mapped = false;
1811.10Sjruoho	addr = hpet_acpi_dev_addr(self, aa, &sc->sc_mems);
1821.10Sjruoho
1831.10Sjruoho	if (addr == 0) {
1841.10Sjruoho		aprint_error(": failed to get address\n");
1851.10Sjruoho		return;
1861.10Sjruoho	}
1871.10Sjruoho
1881.10Sjruoho	sc->sc_memt = aa->aa_memt;
1891.10Sjruoho
1901.10Sjruoho	if (bus_space_map(sc->sc_memt, addr, sc->sc_mems, 0, &sc->sc_memh)) {
1911.10Sjruoho		aprint_error(": failed to map mem space\n");
1921.10Sjruoho		return;
1931.10Sjruoho	}
1941.10Sjruoho
1951.10Sjruoho	aprint_naive("\n");
1961.10Sjruoho	aprint_normal(": high precision event timer (mem 0x%08x-0x%08x)\n",
1971.10Sjruoho	    (uint32_t)addr, (uint32_t)(addr + sc->sc_mems));
1981.10Sjruoho
1991.10Sjruoho	sc->sc_mapped = true;
2001.10Sjruoho	hpet_attach_subr(self);
2011.10Sjruoho}
2021.10Sjruoho
2031.10Sjruohostatic bus_addr_t
2041.10Sjruohohpet_acpi_dev_addr(device_t self, void *aux, bus_size_t *len)
2051.10Sjruoho{
2061.10Sjruoho	struct acpi_attach_args *aa = aux;
2071.1Snjoly	struct acpi_resources res;
2081.1Snjoly	struct acpi_mem *mem;
2091.10Sjruoho	bus_addr_t addr = 0;
2101.1Snjoly	ACPI_STATUS rv;
2111.1Snjoly
2121.3Sxtraeme	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
2131.11Sjruoho	    &res, &acpi_resource_parse_ops_quiet);
2141.6Sjruoho
2151.1Snjoly	if (ACPI_FAILURE(rv))
2161.10Sjruoho		return 0;
2171.1Snjoly
2181.1Snjoly	mem = acpi_res_mem(&res, 0);
2191.6Sjruoho
2201.10Sjruoho	if (mem == NULL)
2211.6Sjruoho		goto out;
2221.6Sjruoho
2231.10Sjruoho	if (mem->ar_length < HPET_WINDOW_SIZE)
2241.1Snjoly		goto out;
2251.1Snjoly
2261.10Sjruoho	addr = mem->ar_base;
2271.10Sjruoho	*len = mem->ar_length;
2281.1Snjoly
2291.6Sjruohoout:
2301.1Snjoly	acpi_resource_cleanup(&res);
2311.10Sjruoho
2321.10Sjruoho	return addr;
2331.1Snjoly}
2341.7Sjruoho
2351.7Sjruohostatic int
2361.7Sjruohohpet_acpi_detach(device_t self, int flags)
2371.7Sjruoho{
2381.7Sjruoho	struct hpet_softc *sc = device_private(self);
2391.7Sjruoho	int rv;
2401.7Sjruoho
2411.7Sjruoho	if (sc->sc_mapped != true)
2421.7Sjruoho		return 0;
2431.7Sjruoho
2441.7Sjruoho	rv = hpet_detach(self, flags);
2451.8Sjruoho
2461.8Sjruoho	if (rv != 0)
2471.8Sjruoho		return rv;
2481.8Sjruoho
2491.7Sjruoho	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
2501.7Sjruoho
2511.8Sjruoho	return 0;
2521.7Sjruoho}
253