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