Home | History | Annotate | Line # | Download | only in acpi
apple_smc_acpi.c revision 1.3.12.1
      1  1.3.12.1     skrll /*	$NetBSD: apple_smc_acpi.c,v 1.3.12.1 2017/08/28 17:52:01 skrll 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.3.12.1     skrll  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      9       1.1  riastrad  * All rights reserved.
     10       1.1  riastrad  *
     11  1.3.12.1     skrll  * This code is derived from software contributed to The NetBSD Foundation
     12  1.3.12.1     skrll  * by Taylor R. Campbell.
     13  1.3.12.1     skrll  *
     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.3.12.1     skrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.3.12.1     skrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.3.12.1     skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.3.12.1     skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.3.12.1     skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.3.12.1     skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.3.12.1     skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.3.12.1     skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.3.12.1     skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.3.12.1     skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.3.12.1     skrll  * POSSIBILITY OF SUCH DAMAGE.
     34       1.1  riastrad  */
     35       1.1  riastrad 
     36       1.1  riastrad #include <sys/cdefs.h>
     37  1.3.12.1     skrll __KERNEL_RCSID(0, "$NetBSD: apple_smc_acpi.c,v 1.3.12.1 2017/08/28 17:52:01 skrll 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.1  riastrad 
     72       1.1  riastrad static const char *const apple_smc_ids[] = {
     73       1.1  riastrad 	"APP0001",
     74       1.1  riastrad 	NULL
     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.1  riastrad 
     82       1.1  riastrad 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     83       1.1  riastrad 		return 0;
     84       1.1  riastrad 
     85       1.1  riastrad 	if (!acpi_match_hid(aa->aa_node->ad_devinfo, apple_smc_ids))
     86       1.1  riastrad 		return 0;
     87       1.1  riastrad 
     88       1.1  riastrad 	return 1;
     89       1.1  riastrad }
     90       1.1  riastrad 
     91       1.1  riastrad static void
     92       1.1  riastrad apple_smc_acpi_attach(device_t parent, device_t self, void *aux)
     93       1.1  riastrad {
     94       1.1  riastrad 	struct apple_smc_acpi_softc *sc = device_private(self);
     95       1.1  riastrad 	struct apple_smc_tag *smc = &sc->sc_smc;
     96       1.1  riastrad 	struct acpi_attach_args *aa = aux;
     97       1.1  riastrad 	struct acpi_resources res;
     98       1.1  riastrad 	struct acpi_io *io;
     99       1.1  riastrad 	int rv;
    100       1.1  riastrad 
    101       1.1  riastrad 	smc->smc_dev = self;
    102       1.1  riastrad 
    103       1.1  riastrad 	aprint_normal("\n");
    104       1.1  riastrad 	aprint_naive("\n");
    105       1.1  riastrad 
    106       1.1  riastrad 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    107       1.1  riastrad 	    &res, &acpi_resource_parse_ops_default);
    108       1.1  riastrad 	if (ACPI_FAILURE(rv)) {
    109       1.1  riastrad 		aprint_error_dev(self, "couldn't parse SMC resources: %s\n",
    110       1.1  riastrad 		    AcpiFormatException(rv));
    111       1.1  riastrad 		goto out0;
    112       1.1  riastrad 	}
    113       1.1  riastrad 
    114       1.1  riastrad 	io = acpi_res_io(&res, 0);
    115       1.1  riastrad 	if (io == NULL) {
    116       1.1  riastrad 		aprint_error_dev(self, "no I/O resource\n");
    117       1.1  riastrad 		goto out1;
    118       1.1  riastrad 	}
    119       1.1  riastrad 
    120       1.1  riastrad 	if (io->ar_length < APPLE_SMC_REGSIZE) {
    121       1.1  riastrad 		aprint_error_dev(self, "I/O resources too small: %"PRId32"\n",
    122       1.1  riastrad 		    io->ar_length);
    123       1.1  riastrad 		goto out1;
    124       1.1  riastrad 	}
    125       1.1  riastrad 
    126       1.1  riastrad 	if (bus_space_map(aa->aa_iot, io->ar_base, io->ar_length, 0,
    127       1.1  riastrad 		&smc->smc_bsh) != 0) {
    128       1.1  riastrad 		aprint_error_dev(self, "unable to map I/O registers\n");
    129       1.1  riastrad 		goto out1;
    130       1.1  riastrad 	}
    131       1.1  riastrad 
    132       1.1  riastrad 	smc->smc_bst = aa->aa_iot;
    133       1.1  riastrad 	smc->smc_size = io->ar_length;
    134       1.1  riastrad 
    135       1.1  riastrad 	apple_smc_attach(smc);
    136       1.1  riastrad 
    137       1.1  riastrad out1:	acpi_resource_cleanup(&res);
    138       1.1  riastrad out0:	return;
    139       1.1  riastrad }
    140       1.1  riastrad 
    141       1.1  riastrad static int
    142       1.1  riastrad apple_smc_acpi_detach(device_t self, int flags)
    143       1.1  riastrad {
    144       1.1  riastrad 	struct apple_smc_acpi_softc *sc = device_private(self);
    145       1.1  riastrad 	struct apple_smc_tag *smc = &sc->sc_smc;
    146       1.1  riastrad 	int error;
    147       1.1  riastrad 
    148       1.1  riastrad 	if (smc->smc_size != 0) {
    149       1.1  riastrad 		error = apple_smc_detach(smc, flags);
    150       1.1  riastrad 		if (error)
    151       1.1  riastrad 			return error;
    152       1.1  riastrad 
    153       1.1  riastrad 		bus_space_unmap(smc->smc_bst, smc->smc_bsh, smc->smc_size);
    154       1.1  riastrad 		smc->smc_size = 0;
    155       1.1  riastrad 	}
    156       1.1  riastrad 
    157       1.1  riastrad 	return 0;
    158       1.2  riastrad }
    159       1.2  riastrad 
    160       1.2  riastrad static int
    161       1.2  riastrad apple_smc_acpi_rescan(device_t self, const char *ifattr, const int *locs)
    162       1.2  riastrad {
    163       1.2  riastrad 	struct apple_smc_acpi_softc *const sc = device_private(self);
    164       1.2  riastrad 
    165       1.2  riastrad 	return apple_smc_rescan(&sc->sc_smc, ifattr, locs);
    166       1.2  riastrad }
    167       1.2  riastrad 
    168       1.2  riastrad static void
    169       1.2  riastrad apple_smc_acpi_child_detached(device_t self, device_t child)
    170       1.2  riastrad {
    171       1.2  riastrad 	struct apple_smc_acpi_softc *const sc = device_private(self);
    172       1.2  riastrad 
    173       1.2  riastrad 	apple_smc_child_detached(&sc->sc_smc, child);
    174       1.2  riastrad }
    175       1.2  riastrad 
    176       1.2  riastrad MODULE(MODULE_CLASS_DRIVER, apple_smc_acpi, "apple_smc");
    178       1.2  riastrad 
    179       1.2  riastrad #ifdef _MODULE
    180       1.2  riastrad #include "ioconf.c"
    181       1.2  riastrad #endif
    182       1.2  riastrad 
    183       1.2  riastrad static int
    184       1.3  riastrad apple_smc_acpi_modcmd(modcmd_t cmd, void *arg __unused)
    185       1.2  riastrad {
    186       1.3  riastrad #ifdef _MODULE
    187       1.2  riastrad 	int error;
    188       1.2  riastrad #endif
    189       1.2  riastrad 
    190       1.2  riastrad 	switch (cmd) {
    191       1.2  riastrad 	case MODULE_CMD_INIT:
    192       1.2  riastrad #ifdef _MODULE
    193       1.2  riastrad 		error = config_init_component(cfdriver_ioconf_apple_smc_acpi,
    194       1.2  riastrad 		    cfattach_ioconf_apple_smc_acpi,
    195       1.2  riastrad 		    cfdata_ioconf_apple_smc_acpi);
    196       1.2  riastrad 		if (error)
    197       1.2  riastrad 			return error;
    198       1.2  riastrad #endif
    199       1.2  riastrad 		return 0;
    200       1.2  riastrad 
    201       1.2  riastrad 	case MODULE_CMD_FINI:
    202       1.2  riastrad #ifdef _MODULE
    203       1.2  riastrad 		error = config_fini_component(cfdriver_ioconf_apple_smc_acpi,
    204       1.2  riastrad 		    cfattach_ioconf_apple_smc_acpi,
    205       1.2  riastrad 		    cfdata_ioconf_apple_smc_acpi);
    206       1.2  riastrad 		if (error)
    207       1.2  riastrad 			return error;
    208       1.2  riastrad #endif
    209       1.2  riastrad 		return 0;
    210       1.2  riastrad 
    211       1.2  riastrad 	default:
    212       1.2  riastrad 		return ENOTTY;
    213                     	}
    214                     }
    215