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