Home | History | Annotate | Line # | Download | only in acpi
acpi_i2c.c revision 1.12.10.1
      1  1.12.10.1  perseant /* $NetBSD: acpi_i2c.c,v 1.12.10.1 2025/08/02 05:56:31 perseant Exp $ */
      2        1.1    bouyer 
      3        1.1    bouyer /*-
      4       1.11   thorpej  * Copyright (c) 2017, 2021 The NetBSD Foundation, Inc.
      5        1.1    bouyer  * All rights reserved.
      6        1.1    bouyer  *
      7        1.1    bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1    bouyer  * by Manuel Bouyer.
      9        1.1    bouyer  *
     10        1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     11        1.1    bouyer  * modification, are permitted provided that the following conditions
     12        1.1    bouyer  * are met:
     13        1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     14        1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     15        1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     17        1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     18        1.1    bouyer  *
     19        1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1    bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1    bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1    bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1    bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1    bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1    bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1    bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1    bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1    bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1    bouyer  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1    bouyer  */
     31        1.1    bouyer 
     32  1.12.10.1  perseant #include "iic.h"
     33  1.12.10.1  perseant 
     34        1.1    bouyer #include <sys/cdefs.h>
     35  1.12.10.1  perseant __KERNEL_RCSID(0, "$NetBSD: acpi_i2c.c,v 1.12.10.1 2025/08/02 05:56:31 perseant Exp $");
     36  1.12.10.1  perseant 
     37  1.12.10.1  perseant #include <sys/device.h>
     38        1.1    bouyer 
     39        1.1    bouyer #include <dev/acpi/acpireg.h>
     40        1.1    bouyer #include <dev/acpi/acpivar.h>
     41        1.1    bouyer #include <dev/acpi/acpi_i2c.h>
     42  1.12.10.1  perseant #include <external/bsd/acpica/dist/include/acinterp.h>
     43  1.12.10.1  perseant #include <external/bsd/acpica/dist/include/amlcode.h>
     44        1.9  jmcneill #include <dev/i2c/i2cvar.h>
     45        1.1    bouyer 
     46       1.11   thorpej #include <sys/kmem.h>
     47       1.11   thorpej 
     48        1.2    bouyer #define _COMPONENT	ACPI_BUS_COMPONENT
     49        1.2    bouyer ACPI_MODULE_NAME	("acpi_i2c")
     50        1.2    bouyer 
     51  1.12.10.1  perseant struct acpi_i2c_address_space_context {
     52  1.12.10.1  perseant 	ACPI_CONNECTION_INFO conn_info;	/* must be first */
     53  1.12.10.1  perseant 	i2c_tag_t tag;
     54  1.12.10.1  perseant };
     55  1.12.10.1  perseant 
     56  1.12.10.1  perseant static const struct device_compatible_entry hid_compat_data[] = {
     57  1.12.10.1  perseant 	{ .compat = "PNP0C50" },
     58  1.12.10.1  perseant 	DEVICE_COMPAT_EOL
     59  1.12.10.1  perseant };
     60  1.12.10.1  perseant 
     61  1.12.10.1  perseant #if NIIC > 0
     62        1.1    bouyer struct acpi_i2c_context {
     63        1.1    bouyer 	uint16_t i2c_addr;
     64  1.12.10.1  perseant 	struct acpi_devnode *res_src;
     65        1.1    bouyer };
     66  1.12.10.1  perseant #endif
     67  1.12.10.1  perseant 
     68  1.12.10.1  perseant static struct acpi_devnode *
     69  1.12.10.1  perseant acpi_i2c_resource_find_source(ACPI_RESOURCE_SOURCE *rs)
     70  1.12.10.1  perseant {
     71  1.12.10.1  perseant 	ACPI_STATUS rv;
     72  1.12.10.1  perseant 	ACPI_HANDLE hdl;
     73  1.12.10.1  perseant 	struct acpi_devnode *ad;
     74  1.12.10.1  perseant 
     75  1.12.10.1  perseant 	if (rs->StringPtr == NULL) {
     76  1.12.10.1  perseant 		return NULL;
     77  1.12.10.1  perseant 	}
     78  1.12.10.1  perseant 
     79  1.12.10.1  perseant 	rv = AcpiGetHandle(NULL, rs->StringPtr, &hdl);
     80  1.12.10.1  perseant 	if (ACPI_FAILURE(rv)) {
     81  1.12.10.1  perseant 		printf("%s: couldn't lookup '%s': %s\n", __func__,
     82  1.12.10.1  perseant 		    rs->StringPtr, AcpiFormatException(rv));
     83  1.12.10.1  perseant 		return NULL;
     84  1.12.10.1  perseant 	}
     85  1.12.10.1  perseant 
     86  1.12.10.1  perseant 	SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
     87  1.12.10.1  perseant 		if (ad->ad_handle == hdl) {
     88  1.12.10.1  perseant 			return ad;
     89  1.12.10.1  perseant 		}
     90  1.12.10.1  perseant 	}
     91  1.12.10.1  perseant 
     92  1.12.10.1  perseant 	printf("%s: no acpi devnode matching resource source '%s'\n",
     93  1.12.10.1  perseant 	    __func__, rs->StringPtr);
     94  1.12.10.1  perseant 	return NULL;
     95  1.12.10.1  perseant }
     96        1.1    bouyer 
     97        1.1    bouyer static ACPI_STATUS
     98        1.1    bouyer acpi_i2c_resource_parse_callback(ACPI_RESOURCE *res, void *context)
     99        1.1    bouyer {
    100        1.1    bouyer 	struct acpi_i2c_context *i2cc = context;
    101        1.1    bouyer 
    102        1.1    bouyer 	switch (res->Type) {
    103        1.1    bouyer 	case ACPI_RESOURCE_TYPE_END_TAG:
    104        1.1    bouyer 		break;
    105        1.1    bouyer 	case ACPI_RESOURCE_TYPE_SERIAL_BUS:
    106        1.1    bouyer 		switch (res->Data.I2cSerialBus.Type) {
    107        1.1    bouyer 		case ACPI_RESOURCE_SERIAL_TYPE_I2C:
    108        1.1    bouyer 			i2cc->i2c_addr = res->Data.I2cSerialBus.SlaveAddress;
    109  1.12.10.1  perseant 			i2cc->res_src = acpi_i2c_resource_find_source(
    110  1.12.10.1  perseant 			    &res->Data.I2cSerialBus.ResourceSource);
    111        1.1    bouyer 			break;
    112        1.1    bouyer 		}
    113        1.1    bouyer 		break;
    114        1.1    bouyer 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
    115        1.1    bouyer 		break;
    116        1.1    bouyer 	default:
    117  1.12.10.1  perseant 		break;
    118        1.1    bouyer 	}
    119        1.1    bouyer 	return_ACPI_STATUS(AE_OK);
    120        1.1    bouyer }
    121        1.1    bouyer 
    122        1.1    bouyer static void
    123        1.1    bouyer acpi_enter_i2c_device(struct acpi_devnode *ad, prop_array_t array)
    124        1.1    bouyer {
    125        1.1    bouyer 	prop_dictionary_t dev;
    126        1.1    bouyer 	struct acpi_i2c_context i2cc;
    127        1.1    bouyer 	ACPI_STATUS rv;
    128       1.11   thorpej 	char *clist;
    129       1.11   thorpej 	size_t clist_size;
    130        1.1    bouyer 
    131        1.1    bouyer 	memset(&i2cc, 0, sizeof(i2cc));
    132        1.1    bouyer 	rv = AcpiWalkResources(ad->ad_handle, "_CRS",
    133        1.1    bouyer 	     acpi_i2c_resource_parse_callback, &i2cc);
    134        1.1    bouyer 	if (ACPI_FAILURE(rv)) {
    135        1.1    bouyer 		return;
    136        1.1    bouyer 	}
    137        1.1    bouyer 	if (i2cc.i2c_addr == 0)
    138        1.1    bouyer 		return;
    139        1.1    bouyer 	dev = prop_dictionary_create();
    140        1.1    bouyer 	if (dev == NULL) {
    141        1.1    bouyer 		aprint_error("ignoring device %s (no memory)\n",
    142        1.1    bouyer 		    ad->ad_name);
    143        1.1    bouyer 		return;
    144        1.1    bouyer 	}
    145       1.12   thorpej 	clist = acpi_pack_compat_list(ad, &clist_size);
    146       1.11   thorpej 	if (clist == NULL) {
    147       1.11   thorpej 		prop_object_release(dev);
    148       1.11   thorpej 		aprint_error("ignoring device %s (no _HID or _CID)\n",
    149       1.11   thorpej 		    ad->ad_name);
    150       1.11   thorpej 		return;
    151       1.11   thorpej 	}
    152       1.12   thorpej 	prop_dictionary_set_string(dev, "name", ad->ad_name);
    153        1.1    bouyer 	prop_dictionary_set_uint32(dev, "addr", i2cc.i2c_addr);
    154        1.1    bouyer 	prop_dictionary_set_uint64(dev, "cookie", (uintptr_t)ad->ad_handle);
    155        1.9  jmcneill 	prop_dictionary_set_uint32(dev, "cookietype", I2C_COOKIE_ACPI);
    156       1.11   thorpej 	prop_dictionary_set_data(dev, "compatible", clist, clist_size);
    157       1.11   thorpej 	kmem_free(clist, clist_size);
    158       1.11   thorpej 
    159        1.1    bouyer 	prop_array_add(array, dev);
    160        1.1    bouyer 	prop_object_release(dev);
    161        1.1    bouyer }
    162        1.1    bouyer 
    163  1.12.10.1  perseant static void
    164  1.12.10.1  perseant acpi_enter_i2chid_devs(device_t dev, struct acpi_devnode *devnode,
    165  1.12.10.1  perseant     prop_array_t array)
    166  1.12.10.1  perseant {
    167  1.12.10.1  perseant 	struct acpi_devnode *ad;
    168  1.12.10.1  perseant 
    169  1.12.10.1  perseant 	KASSERT(dev != NULL);
    170  1.12.10.1  perseant 
    171  1.12.10.1  perseant 	SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
    172  1.12.10.1  perseant 		struct acpi_attach_args aa = {
    173  1.12.10.1  perseant 			.aa_node = ad
    174  1.12.10.1  perseant 		};
    175  1.12.10.1  perseant 		struct acpi_i2c_context i2cc;
    176  1.12.10.1  perseant 		ACPI_STATUS rv;
    177  1.12.10.1  perseant 
    178  1.12.10.1  perseant 		if (!acpi_device_present(ad->ad_handle))
    179  1.12.10.1  perseant 			continue;
    180  1.12.10.1  perseant 		if (ad->ad_device != NULL)
    181  1.12.10.1  perseant 			continue;
    182  1.12.10.1  perseant 		if (acpi_compatible_match(&aa, hid_compat_data) == 0)
    183  1.12.10.1  perseant 			continue;
    184  1.12.10.1  perseant 
    185  1.12.10.1  perseant 		memset(&i2cc, 0, sizeof(i2cc));
    186  1.12.10.1  perseant 		rv = AcpiWalkResources(ad->ad_handle, "_CRS",
    187  1.12.10.1  perseant 		    acpi_i2c_resource_parse_callback, &i2cc);
    188  1.12.10.1  perseant 		if (ACPI_SUCCESS(rv) &&
    189  1.12.10.1  perseant 		    i2cc.i2c_addr != 0 &&
    190  1.12.10.1  perseant 		    i2cc.res_src == devnode) {
    191  1.12.10.1  perseant 			aprint_debug_dev(dev, "claiming %s\n", ad->ad_name);
    192  1.12.10.1  perseant 			ad->ad_device = dev;
    193  1.12.10.1  perseant 			acpi_claim_childdevs(dev, ad, NULL);
    194  1.12.10.1  perseant 			acpi_enter_i2c_device(ad, array);
    195  1.12.10.1  perseant 		}
    196  1.12.10.1  perseant 	}
    197  1.12.10.1  perseant }
    198  1.12.10.1  perseant 
    199        1.1    bouyer prop_array_t
    200       1.10  jmcneill acpi_enter_i2c_devs(device_t dev, struct acpi_devnode *devnode)
    201        1.1    bouyer {
    202        1.1    bouyer 	struct acpi_devnode *ad;
    203        1.1    bouyer 	prop_array_t array = prop_array_create();
    204        1.1    bouyer 
    205        1.1    bouyer 	if (array == NULL)
    206        1.1    bouyer 		return NULL;
    207        1.1    bouyer 
    208        1.1    bouyer 	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
    209        1.1    bouyer 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
    210        1.1    bouyer 			continue;
    211        1.4  christos 		if (!acpi_device_present(ad->ad_handle))
    212        1.4  christos 			continue;
    213        1.1    bouyer 		acpi_enter_i2c_device(ad, array);
    214        1.1    bouyer 	}
    215       1.10  jmcneill 
    216       1.10  jmcneill 	if (dev != NULL) {
    217  1.12.10.1  perseant 		acpi_claim_childdevs(dev, devnode, "_CRS");
    218  1.12.10.1  perseant 		acpi_claim_childdevs(dev, devnode, "_ADR");
    219  1.12.10.1  perseant 		acpi_enter_i2chid_devs(dev, devnode, array);
    220       1.10  jmcneill 	}
    221       1.10  jmcneill 
    222        1.1    bouyer 	return array;
    223        1.1    bouyer }
    224  1.12.10.1  perseant 
    225  1.12.10.1  perseant #if NIIC > 0
    226  1.12.10.1  perseant static ACPI_STATUS
    227  1.12.10.1  perseant acpi_i2c_gsb_init(ACPI_HANDLE region_hdl, UINT32 function,
    228  1.12.10.1  perseant     void *handler_ctx, void **region_ctx)
    229  1.12.10.1  perseant {
    230  1.12.10.1  perseant 	if (function == ACPI_REGION_DEACTIVATE) {
    231  1.12.10.1  perseant 		*region_ctx = NULL;
    232  1.12.10.1  perseant 	} else {
    233  1.12.10.1  perseant 		*region_ctx = region_hdl;
    234  1.12.10.1  perseant 	}
    235  1.12.10.1  perseant 	return AE_OK;
    236  1.12.10.1  perseant }
    237  1.12.10.1  perseant 
    238  1.12.10.1  perseant static ACPI_STATUS
    239  1.12.10.1  perseant acpi_i2c_gsb_handler(UINT32 function, ACPI_PHYSICAL_ADDRESS address,
    240  1.12.10.1  perseant     UINT32 bit_width, UINT64 *value, void *handler_ctx,
    241  1.12.10.1  perseant     void *region_ctx)
    242  1.12.10.1  perseant {
    243  1.12.10.1  perseant 	ACPI_OPERAND_OBJECT *region_obj = region_ctx;
    244  1.12.10.1  perseant 	struct acpi_i2c_address_space_context *context = handler_ctx;
    245  1.12.10.1  perseant 	UINT8 *buf = ACPI_CAST_PTR(uint8_t, value);
    246  1.12.10.1  perseant 	ACPI_PHYSICAL_ADDRESS base_address;
    247  1.12.10.1  perseant 	ACPI_RESOURCE *res;
    248  1.12.10.1  perseant 	ACPI_STATUS rv;
    249  1.12.10.1  perseant 	ACPI_CONNECTION_INFO *conn_info = &context->conn_info;
    250  1.12.10.1  perseant 	i2c_tag_t tag = context->tag;
    251  1.12.10.1  perseant 	i2c_addr_t i2c_addr;
    252  1.12.10.1  perseant 	i2c_op_t op;
    253  1.12.10.1  perseant 	union {
    254  1.12.10.1  perseant 		uint8_t cmd8;
    255  1.12.10.1  perseant 		uint16_t cmd16;
    256  1.12.10.1  perseant 		uint32_t cmd32;
    257  1.12.10.1  perseant 	} cmd;
    258  1.12.10.1  perseant 	size_t buflen;
    259  1.12.10.1  perseant 	size_t cmdlen;
    260  1.12.10.1  perseant 	bool do_xfer = true;
    261  1.12.10.1  perseant 
    262  1.12.10.1  perseant 	if (region_obj->Region.Type != ACPI_TYPE_REGION) {
    263  1.12.10.1  perseant 		return AE_OK;
    264  1.12.10.1  perseant 	}
    265  1.12.10.1  perseant 
    266  1.12.10.1  perseant 	base_address = region_obj->Region.Address;
    267  1.12.10.1  perseant 	KASSERT(region_obj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS);
    268  1.12.10.1  perseant 
    269  1.12.10.1  perseant 	rv = AcpiBufferToResource(conn_info->Connection, conn_info->Length,
    270  1.12.10.1  perseant 	    &res);
    271  1.12.10.1  perseant 	if (ACPI_FAILURE(rv)) {
    272  1.12.10.1  perseant 		return rv;
    273  1.12.10.1  perseant 	}
    274  1.12.10.1  perseant 	if (res->Type != ACPI_RESOURCE_TYPE_SERIAL_BUS ||
    275  1.12.10.1  perseant 	    res->Data.CommonSerialBus.Type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
    276  1.12.10.1  perseant 		return AE_TYPE;
    277  1.12.10.1  perseant 	}
    278  1.12.10.1  perseant 
    279  1.12.10.1  perseant 	i2c_addr = res->Data.I2cSerialBus.SlaveAddress;
    280  1.12.10.1  perseant 	if ((function & ACPI_IO_MASK) != 0) {
    281  1.12.10.1  perseant 		op = I2C_OP_WRITE_WITH_STOP;
    282  1.12.10.1  perseant 	} else {
    283  1.12.10.1  perseant 		op = I2C_OP_READ_WITH_STOP;
    284  1.12.10.1  perseant 	}
    285  1.12.10.1  perseant 
    286  1.12.10.1  perseant #ifdef ACPI_I2C_DEBUG
    287  1.12.10.1  perseant 	UINT32 length;
    288  1.12.10.1  perseant 	rv = AcpiExGetProtocolBufferLength(function >> 16, &length);
    289  1.12.10.1  perseant 	if (ACPI_FAILURE(rv)) {
    290  1.12.10.1  perseant 		printf("%s AcpiExGetProtocolBufferLength failed: %s\n",
    291  1.12.10.1  perseant 		    __func__, AcpiFormatException(rv));
    292  1.12.10.1  perseant 		length = UINT32_MAX;
    293  1.12.10.1  perseant 	}
    294  1.12.10.1  perseant 	printf("%s %s: %s Attr %X Addr %.4X BaseAddr %.4X Length %.2X BitWidth %X BufLen %X",
    295  1.12.10.1  perseant 	       __func__, AcpiUtGetRegionName(region_obj->Region.SpaceId),
    296  1.12.10.1  perseant 	       (function & ACPI_IO_MASK) ? "Write" : "Read ",
    297  1.12.10.1  perseant 	       (UINT32) (function >> 16),
    298  1.12.10.1  perseant 	       (UINT32) address, (UINT32) base_address,
    299  1.12.10.1  perseant 	       length, bit_width, buf[1]);
    300  1.12.10.1  perseant 	printf(" [AccessLength %.2X Connection %p]\n",
    301  1.12.10.1  perseant 	       conn_info->AccessLength, conn_info->Connection);
    302  1.12.10.1  perseant #endif
    303  1.12.10.1  perseant 
    304  1.12.10.1  perseant 	switch ((UINT32)(function >> 16)) {
    305  1.12.10.1  perseant 	case AML_FIELD_ATTRIB_QUICK:
    306  1.12.10.1  perseant 		cmdlen = 0;
    307  1.12.10.1  perseant 		buflen = 0;
    308  1.12.10.1  perseant 		break;
    309  1.12.10.1  perseant 	case AML_FIELD_ATTRIB_SEND_RECEIVE:
    310  1.12.10.1  perseant 		cmdlen = 0;
    311  1.12.10.1  perseant 		buflen = 1;
    312  1.12.10.1  perseant 		break;
    313  1.12.10.1  perseant 	case AML_FIELD_ATTRIB_BYTE:
    314  1.12.10.1  perseant 		cmdlen = bit_width / NBBY;
    315  1.12.10.1  perseant 		buflen = 1;
    316  1.12.10.1  perseant 		break;
    317  1.12.10.1  perseant 	case AML_FIELD_ATTRIB_WORD:
    318  1.12.10.1  perseant 		cmdlen = bit_width / NBBY;
    319  1.12.10.1  perseant 		buflen = 2;
    320  1.12.10.1  perseant 		break;
    321  1.12.10.1  perseant 	case AML_FIELD_ATTRIB_BYTES:
    322  1.12.10.1  perseant 		cmdlen = bit_width / NBBY;
    323  1.12.10.1  perseant 		buflen = buf[1];
    324  1.12.10.1  perseant 		break;
    325  1.12.10.1  perseant 	case AML_FIELD_ATTRIB_BLOCK:
    326  1.12.10.1  perseant 		cmdlen = bit_width / NBBY;
    327  1.12.10.1  perseant 		buflen = buf[1];
    328  1.12.10.1  perseant 		op |= I2C_OPMASK_BLKMODE;
    329  1.12.10.1  perseant 		break;
    330  1.12.10.1  perseant 	case AML_FIELD_ATTRIB_RAW_BYTES:
    331  1.12.10.1  perseant 	case AML_FIELD_ATTRIB_RAW_PROCESS_BYTES:
    332  1.12.10.1  perseant 	case AML_FIELD_ATTRIB_PROCESS_CALL:
    333  1.12.10.1  perseant 	default:
    334  1.12.10.1  perseant 		cmdlen = 0;
    335  1.12.10.1  perseant 		do_xfer = false;
    336  1.12.10.1  perseant #ifdef ACPI_I2C_DEBUG
    337  1.12.10.1  perseant 		printf("field attrib 0x%x not supported\n",
    338  1.12.10.1  perseant 		    (UINT32)(function >> 16));
    339  1.12.10.1  perseant #endif
    340  1.12.10.1  perseant 		break;
    341  1.12.10.1  perseant 	}
    342  1.12.10.1  perseant 
    343  1.12.10.1  perseant 	switch (cmdlen) {
    344  1.12.10.1  perseant 	case 0:
    345  1.12.10.1  perseant 	case 1:
    346  1.12.10.1  perseant 		cmd.cmd8 = (uint8_t)(base_address + address);
    347  1.12.10.1  perseant 		break;
    348  1.12.10.1  perseant 	case 2:
    349  1.12.10.1  perseant 		cmd.cmd16 = (uint16_t)(base_address + address);
    350  1.12.10.1  perseant 		break;
    351  1.12.10.1  perseant 	case 4:
    352  1.12.10.1  perseant 		cmd.cmd32 = (uint32_t)(base_address + address);
    353  1.12.10.1  perseant 		break;
    354  1.12.10.1  perseant 	default:
    355  1.12.10.1  perseant 		do_xfer = false;
    356  1.12.10.1  perseant #ifdef ACPI_I2C_DEBUG
    357  1.12.10.1  perseant 		printf("cmdlen %zu not supported\n", cmdlen);
    358  1.12.10.1  perseant #endif
    359  1.12.10.1  perseant 		break;
    360  1.12.10.1  perseant 	}
    361  1.12.10.1  perseant 
    362  1.12.10.1  perseant 	if (!do_xfer) {
    363  1.12.10.1  perseant 		buf[0] = EINVAL;
    364  1.12.10.1  perseant 	} else {
    365  1.12.10.1  perseant 		const int flags = I2C_F_POLL;
    366  1.12.10.1  perseant 		iic_acquire_bus(tag, flags);
    367  1.12.10.1  perseant 		buf[0] = iic_exec(tag, op, i2c_addr,
    368  1.12.10.1  perseant 				  &cmd, cmdlen, &buf[2], buflen, flags);
    369  1.12.10.1  perseant 		iic_release_bus(tag, flags);
    370  1.12.10.1  perseant 		if (buf[0] == 0) {
    371  1.12.10.1  perseant 			buf[1] = buflen;
    372  1.12.10.1  perseant 		}
    373  1.12.10.1  perseant #ifdef ACPI_I2C_DEBUG
    374  1.12.10.1  perseant 		printf("%s iic_exec op %u addr 0x%x len %zu/%zu returned %d\n",
    375  1.12.10.1  perseant 		    __func__, op, res->Data.I2cSerialBus.SlaveAddress, cmdlen,
    376  1.12.10.1  perseant 		    buflen, buf[0]);
    377  1.12.10.1  perseant #endif
    378  1.12.10.1  perseant 	}
    379  1.12.10.1  perseant 
    380  1.12.10.1  perseant 	ACPI_FREE(res);
    381  1.12.10.1  perseant 
    382  1.12.10.1  perseant 	return AE_OK;
    383  1.12.10.1  perseant }
    384  1.12.10.1  perseant #endif
    385  1.12.10.1  perseant 
    386  1.12.10.1  perseant ACPI_STATUS
    387  1.12.10.1  perseant acpi_i2c_register(struct acpi_devnode *devnode, device_t dev, i2c_tag_t tag)
    388  1.12.10.1  perseant {
    389  1.12.10.1  perseant #if NIIC > 0
    390  1.12.10.1  perseant 	struct acpi_i2c_address_space_context *context;
    391  1.12.10.1  perseant 	ACPI_STATUS rv;
    392  1.12.10.1  perseant 
    393  1.12.10.1  perseant 	context = kmem_zalloc(sizeof(*context), KM_SLEEP);
    394  1.12.10.1  perseant 	context->tag = tag;
    395  1.12.10.1  perseant 
    396  1.12.10.1  perseant 	rv = AcpiInstallAddressSpaceHandler(devnode->ad_handle,
    397  1.12.10.1  perseant 	    ACPI_ADR_SPACE_GSBUS, acpi_i2c_gsb_handler, acpi_i2c_gsb_init,
    398  1.12.10.1  perseant 	    context);
    399  1.12.10.1  perseant 	if (ACPI_FAILURE(rv)) {
    400  1.12.10.1  perseant 		aprint_error_dev(dev,
    401  1.12.10.1  perseant 		    "couldn't install address space handler: %s",
    402  1.12.10.1  perseant 		    AcpiFormatException(rv));
    403  1.12.10.1  perseant 	}
    404  1.12.10.1  perseant 
    405  1.12.10.1  perseant 	return rv;
    406  1.12.10.1  perseant #else
    407  1.12.10.1  perseant 	return AE_NOT_CONFIGURED;
    408  1.12.10.1  perseant #endif
    409  1.12.10.1  perseant }
    410