Home | History | Annotate | Line # | Download | only in acpi
      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
     51 ACPI_MODULE_NAME		("apple_smc_acpi")
     52 
     53 struct apple_smc_acpi_softc {
     54 	struct apple_smc_tag	sc_smc;
     55 };
     56 
     57 static int	apple_smc_acpi_match(device_t, cfdata_t, void *);
     58 static void	apple_smc_acpi_attach(device_t, device_t, void *);
     59 static int	apple_smc_acpi_detach(device_t, int);
     60 static int	apple_smc_acpi_rescan(device_t, const char *, const int *);
     61 static void	apple_smc_acpi_child_detached(device_t, device_t);
     62 
     63 CFATTACH_DECL2_NEW(apple_smc_acpi, sizeof(struct apple_smc_acpi_softc),
     65     apple_smc_acpi_match,
     66     apple_smc_acpi_attach,
     67     apple_smc_acpi_detach,
     68     NULL /* activate */,
     69     apple_smc_acpi_rescan,
     70     apple_smc_acpi_child_detached);
     71 
     72 static const struct device_compatible_entry compat_data[] = {
     73 	{ .compat = "APP0001" },
     74 	DEVICE_COMPAT_EOL
     75 };
     76 
     77 static int
     78 apple_smc_acpi_match(device_t parent, cfdata_t match, void *aux)
     79 {
     80 	struct acpi_attach_args *aa = aux;
     81 
     82 	return acpi_compatible_match(aa, compat_data);
     83 }
     84 
     85 static void
     86 apple_smc_acpi_attach(device_t parent, device_t self, void *aux)
     87 {
     88 	struct apple_smc_acpi_softc *sc = device_private(self);
     89 	struct apple_smc_tag *smc = &sc->sc_smc;
     90 	struct acpi_attach_args *aa = aux;
     91 	struct acpi_resources res;
     92 	struct acpi_io *io;
     93 	int rv;
     94 
     95 	smc->smc_dev = self;
     96 
     97 	aprint_normal("\n");
     98 	aprint_naive("\n");
     99 
    100 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    101 	    &res, &acpi_resource_parse_ops_default);
    102 	if (ACPI_FAILURE(rv)) {
    103 		aprint_error_dev(self, "couldn't parse SMC resources: %s\n",
    104 		    AcpiFormatException(rv));
    105 		goto out0;
    106 	}
    107 
    108 	io = acpi_res_io(&res, 0);
    109 	if (io == NULL) {
    110 		aprint_error_dev(self, "no I/O resource\n");
    111 		goto out1;
    112 	}
    113 
    114 	if (io->ar_length < APPLE_SMC_REGSIZE) {
    115 		aprint_error_dev(self, "I/O resources too small: %"PRId32"\n",
    116 		    io->ar_length);
    117 		goto out1;
    118 	}
    119 
    120 	if (bus_space_map(aa->aa_iot, io->ar_base, io->ar_length, 0,
    121 		&smc->smc_bsh) != 0) {
    122 		aprint_error_dev(self, "unable to map I/O registers\n");
    123 		goto out1;
    124 	}
    125 
    126 	smc->smc_bst = aa->aa_iot;
    127 	smc->smc_size = io->ar_length;
    128 
    129 	apple_smc_attach(smc);
    130 
    131 out1:	acpi_resource_cleanup(&res);
    132 out0:	return;
    133 }
    134 
    135 static int
    136 apple_smc_acpi_detach(device_t self, int flags)
    137 {
    138 	struct apple_smc_acpi_softc *sc = device_private(self);
    139 	struct apple_smc_tag *smc = &sc->sc_smc;
    140 	int error;
    141 
    142 	if (smc->smc_size != 0) {
    143 		error = apple_smc_detach(smc, flags);
    144 		if (error)
    145 			return error;
    146 
    147 		bus_space_unmap(smc->smc_bst, smc->smc_bsh, smc->smc_size);
    148 		smc->smc_size = 0;
    149 	}
    150 
    151 	return 0;
    152 }
    153 
    154 static int
    155 apple_smc_acpi_rescan(device_t self, const char *ifattr, const int *locs)
    156 {
    157 	struct apple_smc_acpi_softc *const sc = device_private(self);
    158 
    159 	return apple_smc_rescan(&sc->sc_smc, ifattr, locs);
    160 }
    161 
    162 static void
    163 apple_smc_acpi_child_detached(device_t self, device_t child)
    164 {
    165 	struct apple_smc_acpi_softc *const sc = device_private(self);
    166 
    167 	apple_smc_child_detached(&sc->sc_smc, child);
    168 }
    169 
    170 MODULE(MODULE_CLASS_DRIVER, apple_smc_acpi, "apple_smc");
    172 
    173 #ifdef _MODULE
    174 #include "ioconf.c"
    175 #endif
    176 
    177 static int
    178 apple_smc_acpi_modcmd(modcmd_t cmd, void *arg __unused)
    179 {
    180 #ifdef _MODULE
    181 	int error;
    182 #endif
    183 
    184 	switch (cmd) {
    185 	case MODULE_CMD_INIT:
    186 #ifdef _MODULE
    187 		error = config_init_component(cfdriver_ioconf_apple_smc_acpi,
    188 		    cfattach_ioconf_apple_smc_acpi,
    189 		    cfdata_ioconf_apple_smc_acpi);
    190 		if (error)
    191 			return error;
    192 #endif
    193 		return 0;
    194 
    195 	case MODULE_CMD_FINI:
    196 #ifdef _MODULE
    197 		error = config_fini_component(cfdriver_ioconf_apple_smc_acpi,
    198 		    cfattach_ioconf_apple_smc_acpi,
    199 		    cfdata_ioconf_apple_smc_acpi);
    200 		if (error)
    201 			return error;
    202 #endif
    203 		return 0;
    204 
    205 	default:
    206 		return ENOTTY;
    207 	}
    208 }
    209