Home | History | Annotate | Line # | Download | only in acpi
ipmi_acpi.c revision 1.3.4.2
      1  1.3.4.2  christos /* $NetBSD: ipmi_acpi.c,v 1.3.4.2 2019/06/10 22:07:05 christos Exp $ */
      2  1.3.4.2  christos 
      3  1.3.4.2  christos /*-
      4  1.3.4.2  christos  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.3.4.2  christos  * All rights reserved.
      6  1.3.4.2  christos  *
      7  1.3.4.2  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.3.4.2  christos  * by Michael van Elst
      9  1.3.4.2  christos  *
     10  1.3.4.2  christos  * Redistribution and use in source and binary forms, with or without
     11  1.3.4.2  christos  * modification, are permitted provided that the following conditions
     12  1.3.4.2  christos  * are met:
     13  1.3.4.2  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.3.4.2  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.3.4.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3.4.2  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.3.4.2  christos  *    documentation and/or other materials provided with the distribution.
     18  1.3.4.2  christos  *
     19  1.3.4.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.3.4.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.3.4.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.3.4.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.3.4.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.3.4.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.3.4.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.3.4.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.3.4.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.3.4.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.3.4.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.3.4.2  christos  */
     31  1.3.4.2  christos 
     32  1.3.4.2  christos #include <sys/cdefs.h>
     33  1.3.4.2  christos __KERNEL_RCSID(0, "$NetBSD: ipmi_acpi.c,v 1.3.4.2 2019/06/10 22:07:05 christos Exp $");
     34  1.3.4.2  christos 
     35  1.3.4.2  christos #include <sys/param.h>
     36  1.3.4.2  christos #include <sys/device.h>
     37  1.3.4.2  christos #include <sys/module.h>
     38  1.3.4.2  christos #include <sys/systm.h>
     39  1.3.4.2  christos 
     40  1.3.4.2  christos #include <dev/acpi/acpireg.h>
     41  1.3.4.2  christos #include <dev/acpi/acpivar.h>
     42  1.3.4.2  christos 
     43  1.3.4.2  christos #include <dev/ipmivar.h>
     44  1.3.4.2  christos 
     45  1.3.4.2  christos #define _COMPONENT		ACPI_RESOURCE_COMPONENT
     46  1.3.4.2  christos ACPI_MODULE_NAME		("ipmi_acpi")
     47  1.3.4.2  christos 
     48  1.3.4.2  christos typedef struct ipmi_acpi_softc {
     49  1.3.4.2  christos 	device_t		sc_dev;
     50  1.3.4.2  christos 	bool			sc_init;
     51  1.3.4.2  christos } ipmi_acpi_softc_t;
     52  1.3.4.2  christos 
     53  1.3.4.2  christos static int	ipmi_acpi_match(device_t, cfdata_t, void *);
     54  1.3.4.2  christos static void	ipmi_acpi_attach(device_t, device_t, void *);
     55  1.3.4.2  christos static int	ipmi_acpi_detach(device_t, int);
     56  1.3.4.2  christos 
     57  1.3.4.2  christos CFATTACH_DECL3_NEW(ipmi_acpi, sizeof(ipmi_acpi_softc_t),
     58  1.3.4.2  christos     ipmi_acpi_match, ipmi_acpi_attach, ipmi_acpi_detach, NULL, NULL, NULL,
     59  1.3.4.2  christos     DVF_DETACH_SHUTDOWN);
     60  1.3.4.2  christos 
     61  1.3.4.2  christos static const char * const ipmi_ids[] = {
     62  1.3.4.2  christos 	"IPI0001",
     63  1.3.4.2  christos 	NULL
     64  1.3.4.2  christos };
     65  1.3.4.2  christos 
     66  1.3.4.2  christos static int
     67  1.3.4.2  christos ipmi_acpi_match(device_t parent, cfdata_t match, void *opaque)
     68  1.3.4.2  christos {
     69  1.3.4.2  christos 	struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
     70  1.3.4.2  christos 
     71  1.3.4.2  christos 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     72  1.3.4.2  christos 		return 0;
     73  1.3.4.2  christos 
     74  1.3.4.2  christos 	if (!acpi_match_hid(aa->aa_node->ad_devinfo, ipmi_ids))
     75  1.3.4.2  christos 		return 0;
     76  1.3.4.2  christos 
     77  1.3.4.2  christos 	return 1;
     78  1.3.4.2  christos }
     79  1.3.4.2  christos 
     80  1.3.4.2  christos static void
     81  1.3.4.2  christos ipmi_acpi_attach(device_t parent, device_t self, void *opaque)
     82  1.3.4.2  christos {
     83  1.3.4.2  christos 	ipmi_acpi_softc_t *sc = device_private(self);
     84  1.3.4.2  christos 	struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
     85  1.3.4.2  christos 	ACPI_STATUS rv;
     86  1.3.4.2  christos 	ACPI_INTEGER itype, ivers, adr;
     87  1.3.4.2  christos 	struct acpi_resources res;
     88  1.3.4.2  christos 	struct acpi_io *io;
     89  1.3.4.2  christos 	struct acpi_mem *mem;
     90  1.3.4.2  christos #if notyet
     91  1.3.4.2  christos 	struct acpi_irq *irq;
     92  1.3.4.2  christos #endif
     93  1.3.4.2  christos 	struct ipmi_attach_args IA, *ia = &IA;
     94  1.3.4.2  christos 	bus_addr_t reg2;
     95  1.3.4.2  christos 	uint16_t i2caddr;
     96  1.3.4.2  christos 
     97  1.3.4.2  christos 	sc->sc_dev = self;
     98  1.3.4.2  christos 
     99  1.3.4.2  christos 	aprint_naive("\n");
    100  1.3.4.2  christos 
    101  1.3.4.2  christos 	rv = acpi_eval_integer(aa->aa_node->ad_handle, "_IFT", &itype);
    102  1.3.4.2  christos 	if (ACPI_FAILURE(rv)) {
    103  1.3.4.2  christos 		aprint_error("no _IFT\n");
    104  1.3.4.2  christos 		return;
    105  1.3.4.2  christos 	}
    106  1.3.4.2  christos 
    107  1.3.4.2  christos 	rv = acpi_eval_integer(aa->aa_node->ad_handle, "_SRV", &ivers);
    108  1.3.4.2  christos 	if (ACPI_FAILURE(rv)) {
    109  1.3.4.2  christos 		aprint_error("no _SRV\n");
    110  1.3.4.2  christos 		return;
    111  1.3.4.2  christos 	}
    112  1.3.4.2  christos 
    113  1.3.4.2  christos 	switch (itype) {
    114  1.3.4.2  christos 	case IPMI_IF_KCS:
    115  1.3.4.2  christos 	case IPMI_IF_SMIC:
    116  1.3.4.2  christos 	case IPMI_IF_BT:
    117  1.3.4.2  christos 		rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    118  1.3.4.2  christos 			&res, &acpi_resource_parse_ops_default);
    119  1.3.4.2  christos 		if (ACPI_FAILURE(rv)) {
    120  1.3.4.2  christos 			aprint_normal("\n");
    121  1.3.4.2  christos 			aprint_error_dev(self, "no resources\n");
    122  1.3.4.2  christos 			return;
    123  1.3.4.2  christos 		}
    124  1.3.4.2  christos 
    125  1.3.4.2  christos 		io = acpi_res_io(&res, 0);
    126  1.3.4.2  christos 		mem = acpi_res_mem(&res, 0);
    127  1.3.4.2  christos 		if (io == NULL && mem == NULL) {
    128  1.3.4.2  christos 			aprint_error_dev(self, "no resources\n");
    129  1.3.4.2  christos 			return;
    130  1.3.4.2  christos 		}
    131  1.3.4.2  christos 
    132  1.3.4.2  christos #if notyet
    133  1.3.4.2  christos 		if ((irq = acpi_res_irq(&res, 0)) != NULL) {
    134  1.3.4.2  christos 			aprint_normal_dev(self, "IRQ %d type %d\n",
    135  1.3.4.2  christos 				irq->ar_irq, irq->ar_type);
    136  1.3.4.2  christos 		} else {
    137  1.3.4.2  christos 			aprint_error_dev(self, "no interrupt\n");
    138  1.3.4.2  christos 		}
    139  1.3.4.2  christos #endif
    140  1.3.4.2  christos 		ia->iaa_iot = aa->aa_iot;
    141  1.3.4.2  christos 		ia->iaa_memt = aa->aa_memt;
    142  1.3.4.2  christos 
    143  1.3.4.2  christos 		ia->iaa_if_type = itype;
    144  1.3.4.2  christos 		ia->iaa_if_rev = ivers;
    145  1.3.4.2  christos 		ia->iaa_if_iotype = io ? 'i' : 'm';
    146  1.3.4.2  christos 		ia->iaa_if_iobase = io ? io->ar_base : mem->ar_base;
    147  1.3.4.2  christos 		ia->iaa_if_iospacing = 1;
    148  1.3.4.2  christos 		ia->iaa_if_irq = -1;
    149  1.3.4.2  christos 		ia->iaa_if_irqlvl = 0;
    150  1.3.4.2  christos 
    151  1.3.4.2  christos 		reg2 = 0;
    152  1.3.4.2  christos 		if (io) {
    153  1.3.4.2  christos 			io = acpi_res_io(&res, 1);
    154  1.3.4.2  christos 			if (io != NULL)
    155  1.3.4.2  christos 				reg2 = (bus_addr_t)io->ar_base;
    156  1.3.4.2  christos 		} else {
    157  1.3.4.2  christos 			mem = acpi_res_mem(&res, 1);
    158  1.3.4.2  christos 			if (mem != NULL)
    159  1.3.4.2  christos 				reg2 = mem->ar_base;
    160  1.3.4.2  christos 		}
    161  1.3.4.2  christos 
    162  1.3.4.2  christos 		if (reg2 > ia->iaa_if_iobase)
    163  1.3.4.2  christos 			ia->iaa_if_iospacing = reg2 - ia->iaa_if_iobase;
    164  1.3.4.2  christos 
    165  1.3.4.2  christos 		config_found_ia(self, "ipmibus", ia, 0);
    166  1.3.4.2  christos 
    167  1.3.4.2  christos 		break;
    168  1.3.4.2  christos 	case IPMI_IF_SSIF:
    169  1.3.4.2  christos 		rv = acpi_eval_integer(aa->aa_node->ad_handle, "_ADR", &adr);
    170  1.3.4.2  christos 		if (ACPI_FAILURE(rv)) {
    171  1.3.4.2  christos 			aprint_normal("\n");
    172  1.3.4.2  christos 			aprint_error_dev(self, "no resources\n");
    173  1.3.4.2  christos 			return;
    174  1.3.4.2  christos 		}
    175  1.3.4.2  christos 		if (adr > 65535) {
    176  1.3.4.2  christos 			aprint_normal("\n");
    177  1.3.4.2  christos 			aprint_error_dev(self, "i2c address out of range\n");
    178  1.3.4.2  christos 			return;
    179  1.3.4.2  christos 		}
    180  1.3.4.2  christos 		i2caddr = adr;
    181  1.3.4.2  christos 
    182  1.3.4.2  christos 		aprint_normal(": i2c 0x%x\n", i2caddr);
    183  1.3.4.2  christos 		break;
    184  1.3.4.2  christos 	default:
    185  1.3.4.2  christos 		aprint_normal("\n");
    186  1.3.4.2  christos 		aprint_error_dev(self, "unknown interface type\n");
    187  1.3.4.2  christos 		return;
    188  1.3.4.2  christos 	}
    189  1.3.4.2  christos 
    190  1.3.4.2  christos 	sc->sc_init = true;
    191  1.3.4.2  christos 
    192  1.3.4.2  christos 	if (!pmf_device_register(self, NULL, NULL))
    193  1.3.4.2  christos 		aprint_error_dev(self, "couldn't establish power handler\n");
    194  1.3.4.2  christos }
    195  1.3.4.2  christos 
    196  1.3.4.2  christos static int
    197  1.3.4.2  christos ipmi_acpi_detach(device_t self, int flags)
    198  1.3.4.2  christos {
    199  1.3.4.2  christos 	struct ipmi_acpi_softc *sc = device_private(self);
    200  1.3.4.2  christos 	int rc;
    201  1.3.4.2  christos 
    202  1.3.4.2  christos 	if (!sc->sc_init)
    203  1.3.4.2  christos 		return 0;
    204  1.3.4.2  christos 
    205  1.3.4.2  christos 	rc = config_detach_children(self, flags);
    206  1.3.4.2  christos 	if (rc)
    207  1.3.4.2  christos 		return rc;
    208  1.3.4.2  christos 
    209  1.3.4.2  christos 	pmf_device_deregister(self);
    210  1.3.4.2  christos 	return 0;
    211  1.3.4.2  christos }
    212