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