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