11.12Sthorpej/* $NetBSD: hpet_acpi.c,v 1.12 2021/01/29 15:49:55 thorpej 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.12Sthorpej__KERNEL_RCSID(0, "$NetBSD: hpet_acpi.c,v 1.12 2021/01/29 15:49:55 thorpej 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.12Sthorpejstatic const struct device_compatible_entry compat_data[] = { 561.12Sthorpej { .compat = "PNP0103" }, 571.12Sthorpej DEVICE_COMPAT_EOL 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.12Sthorpej int ret; 1521.1Snjoly 1531.12Sthorpej ret = acpi_compatible_match(aa, compat_data); 1541.12Sthorpej if (ret == 0) 1551.10Sjruoho return 0; 1561.10Sjruoho 1571.10Sjruoho addr = hpet_acpi_dev_addr(parent, aa, &len); 1581.10Sjruoho 1591.10Sjruoho if (addr == 0 || len == 0) 1601.10Sjruoho return 0; 1611.10Sjruoho 1621.10Sjruoho bt = aa->aa_memt; 1631.10Sjruoho 1641.10Sjruoho if (bus_space_map(bt, addr, len, 0, &bh) == 0) { 1651.10Sjruoho bus_space_unmap(bt, bh, len); 1661.12Sthorpej return ret; 1671.10Sjruoho } 1681.10Sjruoho 1691.10Sjruoho return 0; 1701.1Snjoly} 1711.1Snjoly 1721.1Snjolystatic void 1731.6Sjruohohpet_acpi_dev_attach(device_t parent, device_t self, void *aux) 1741.1Snjoly{ 1751.3Sxtraeme struct hpet_softc *sc = device_private(self); 1761.1Snjoly struct acpi_attach_args *aa = aux; 1771.10Sjruoho bus_addr_t addr; 1781.10Sjruoho 1791.10Sjruoho sc->sc_mapped = false; 1801.10Sjruoho addr = hpet_acpi_dev_addr(self, aa, &sc->sc_mems); 1811.10Sjruoho 1821.10Sjruoho if (addr == 0) { 1831.10Sjruoho aprint_error(": failed to get address\n"); 1841.10Sjruoho return; 1851.10Sjruoho } 1861.10Sjruoho 1871.10Sjruoho sc->sc_memt = aa->aa_memt; 1881.10Sjruoho 1891.10Sjruoho if (bus_space_map(sc->sc_memt, addr, sc->sc_mems, 0, &sc->sc_memh)) { 1901.10Sjruoho aprint_error(": failed to map mem space\n"); 1911.10Sjruoho return; 1921.10Sjruoho } 1931.10Sjruoho 1941.10Sjruoho aprint_naive("\n"); 1951.10Sjruoho aprint_normal(": high precision event timer (mem 0x%08x-0x%08x)\n", 1961.10Sjruoho (uint32_t)addr, (uint32_t)(addr + sc->sc_mems)); 1971.10Sjruoho 1981.10Sjruoho sc->sc_mapped = true; 1991.10Sjruoho hpet_attach_subr(self); 2001.10Sjruoho} 2011.10Sjruoho 2021.10Sjruohostatic bus_addr_t 2031.10Sjruohohpet_acpi_dev_addr(device_t self, void *aux, bus_size_t *len) 2041.10Sjruoho{ 2051.10Sjruoho struct acpi_attach_args *aa = aux; 2061.1Snjoly struct acpi_resources res; 2071.1Snjoly struct acpi_mem *mem; 2081.10Sjruoho bus_addr_t addr = 0; 2091.1Snjoly ACPI_STATUS rv; 2101.1Snjoly 2111.3Sxtraeme rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", 2121.11Sjruoho &res, &acpi_resource_parse_ops_quiet); 2131.6Sjruoho 2141.1Snjoly if (ACPI_FAILURE(rv)) 2151.10Sjruoho return 0; 2161.1Snjoly 2171.1Snjoly mem = acpi_res_mem(&res, 0); 2181.6Sjruoho 2191.10Sjruoho if (mem == NULL) 2201.6Sjruoho goto out; 2211.6Sjruoho 2221.10Sjruoho if (mem->ar_length < HPET_WINDOW_SIZE) 2231.1Snjoly goto out; 2241.1Snjoly 2251.10Sjruoho addr = mem->ar_base; 2261.10Sjruoho *len = mem->ar_length; 2271.1Snjoly 2281.6Sjruohoout: 2291.1Snjoly acpi_resource_cleanup(&res); 2301.10Sjruoho 2311.10Sjruoho return addr; 2321.1Snjoly} 2331.7Sjruoho 2341.7Sjruohostatic int 2351.7Sjruohohpet_acpi_detach(device_t self, int flags) 2361.7Sjruoho{ 2371.7Sjruoho struct hpet_softc *sc = device_private(self); 2381.7Sjruoho int rv; 2391.7Sjruoho 2401.7Sjruoho if (sc->sc_mapped != true) 2411.7Sjruoho return 0; 2421.7Sjruoho 2431.7Sjruoho rv = hpet_detach(self, flags); 2441.8Sjruoho 2451.8Sjruoho if (rv != 0) 2461.8Sjruoho return rv; 2471.8Sjruoho 2481.7Sjruoho bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems); 2491.7Sjruoho 2501.8Sjruoho return 0; 2511.7Sjruoho} 252