1/* $NetBSD: apple_smc_acpi.c,v 1.5 2021/01/29 15:49:55 thorpej Exp $ */ 2 3/* 4 * Apple System Management Controller: ACPI Attachment 5 */ 6 7/*- 8 * Copyright (c) 2013 The NetBSD Foundation, Inc. 9 * All rights reserved. 10 * 11 * This code is derived from software contributed to The NetBSD Foundation 12 * by Taylor R. Campbell. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36#include <sys/cdefs.h> 37__KERNEL_RCSID(0, "$NetBSD: apple_smc_acpi.c,v 1.5 2021/01/29 15:49:55 thorpej Exp $"); 38 39#include <sys/types.h> 40#include <sys/param.h> 41#include <sys/bus.h> 42#include <sys/module.h> 43 44#include <dev/acpi/acpireg.h> 45#include <dev/acpi/acpivar.h> 46 47#include <dev/ic/apple_smcreg.h> 48#include <dev/ic/apple_smcvar.h> 49 50#define _COMPONENT ACPI_RESOURCE_COMPONENT 51ACPI_MODULE_NAME ("apple_smc_acpi") 52 53struct apple_smc_acpi_softc { 54 struct apple_smc_tag sc_smc; 55}; 56 57static int apple_smc_acpi_match(device_t, cfdata_t, void *); 58static void apple_smc_acpi_attach(device_t, device_t, void *); 59static int apple_smc_acpi_detach(device_t, int); 60static int apple_smc_acpi_rescan(device_t, const char *, const int *); 61static void apple_smc_acpi_child_detached(device_t, device_t); 62 63CFATTACH_DECL2_NEW(apple_smc_acpi, sizeof(struct apple_smc_acpi_softc), 64 apple_smc_acpi_match, 65 apple_smc_acpi_attach, 66 apple_smc_acpi_detach, 67 NULL /* activate */, 68 apple_smc_acpi_rescan, 69 apple_smc_acpi_child_detached); 70 71static const struct device_compatible_entry compat_data[] = { 72 { .compat = "APP0001" }, 73 DEVICE_COMPAT_EOL 74}; 75 76static int 77apple_smc_acpi_match(device_t parent, cfdata_t match, void *aux) 78{ 79 struct acpi_attach_args *aa = aux; 80 81 return acpi_compatible_match(aa, compat_data); 82} 83 84static void 85apple_smc_acpi_attach(device_t parent, device_t self, void *aux) 86{ 87 struct apple_smc_acpi_softc *sc = device_private(self); 88 struct apple_smc_tag *smc = &sc->sc_smc; 89 struct acpi_attach_args *aa = aux; 90 struct acpi_resources res; 91 struct acpi_io *io; 92 int rv; 93 94 smc->smc_dev = self; 95 96 aprint_normal("\n"); 97 aprint_naive("\n"); 98 99 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", 100 &res, &acpi_resource_parse_ops_default); 101 if (ACPI_FAILURE(rv)) { 102 aprint_error_dev(self, "couldn't parse SMC resources: %s\n", 103 AcpiFormatException(rv)); 104 goto out0; 105 } 106 107 io = acpi_res_io(&res, 0); 108 if (io == NULL) { 109 aprint_error_dev(self, "no I/O resource\n"); 110 goto out1; 111 } 112 113 if (io->ar_length < APPLE_SMC_REGSIZE) { 114 aprint_error_dev(self, "I/O resources too small: %"PRId32"\n", 115 io->ar_length); 116 goto out1; 117 } 118 119 if (bus_space_map(aa->aa_iot, io->ar_base, io->ar_length, 0, 120 &smc->smc_bsh) != 0) { 121 aprint_error_dev(self, "unable to map I/O registers\n"); 122 goto out1; 123 } 124 125 smc->smc_bst = aa->aa_iot; 126 smc->smc_size = io->ar_length; 127 128 apple_smc_attach(smc); 129 130out1: acpi_resource_cleanup(&res); 131out0: return; 132} 133 134static int 135apple_smc_acpi_detach(device_t self, int flags) 136{ 137 struct apple_smc_acpi_softc *sc = device_private(self); 138 struct apple_smc_tag *smc = &sc->sc_smc; 139 int error; 140 141 if (smc->smc_size != 0) { 142 error = apple_smc_detach(smc, flags); 143 if (error) 144 return error; 145 146 bus_space_unmap(smc->smc_bst, smc->smc_bsh, smc->smc_size); 147 smc->smc_size = 0; 148 } 149 150 return 0; 151} 152 153static int 154apple_smc_acpi_rescan(device_t self, const char *ifattr, const int *locs) 155{ 156 struct apple_smc_acpi_softc *const sc = device_private(self); 157 158 return apple_smc_rescan(&sc->sc_smc, ifattr, locs); 159} 160 161static void 162apple_smc_acpi_child_detached(device_t self, device_t child) 163{ 164 struct apple_smc_acpi_softc *const sc = device_private(self); 165 166 apple_smc_child_detached(&sc->sc_smc, child); 167} 168 169MODULE(MODULE_CLASS_DRIVER, apple_smc_acpi, "apple_smc"); 170 171#ifdef _MODULE 172#include "ioconf.c" 173#endif 174 175static int 176apple_smc_acpi_modcmd(modcmd_t cmd, void *arg __unused) 177{ 178#ifdef _MODULE 179 int error; 180#endif 181 182 switch (cmd) { 183 case MODULE_CMD_INIT: 184#ifdef _MODULE 185 error = config_init_component(cfdriver_ioconf_apple_smc_acpi, 186 cfattach_ioconf_apple_smc_acpi, 187 cfdata_ioconf_apple_smc_acpi); 188 if (error) 189 return error; 190#endif 191 return 0; 192 193 case MODULE_CMD_FINI: 194#ifdef _MODULE 195 error = config_fini_component(cfdriver_ioconf_apple_smc_acpi, 196 cfattach_ioconf_apple_smc_acpi, 197 cfdata_ioconf_apple_smc_acpi); 198 if (error) 199 return error; 200#endif 201 return 0; 202 203 default: 204 return ENOTTY; 205 } 206} 207