Home | History | Annotate | Line # | Download | only in dev
      1  1.2  thorpej /*	$NetBSD: ofw_i2c_machdep.c,v 1.2 2025/09/21 19:12:45 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 2021, 2025 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  *
     16  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  thorpej  */
     28  1.1  thorpej 
     29  1.1  thorpej #include <sys/cdefs.h>
     30  1.2  thorpej __KERNEL_RCSID(0, "$NetBSD: ofw_i2c_machdep.c,v 1.2 2025/09/21 19:12:45 thorpej Exp $");
     31  1.1  thorpej 
     32  1.1  thorpej #include <sys/param.h>
     33  1.1  thorpej #include <sys/device.h>
     34  1.1  thorpej #include <sys/endian.h>
     35  1.1  thorpej #include <sys/kmem.h>
     36  1.1  thorpej #include <sys/systm.h>
     37  1.1  thorpej 
     38  1.1  thorpej #include <dev/ofw/openfirm.h>
     39  1.1  thorpej 
     40  1.1  thorpej #include <dev/i2c/i2cvar.h>
     41  1.1  thorpej #include <dev/i2c/i2c_calls.h>
     42  1.1  thorpej #include <dev/i2c/i2c_enum.h>
     43  1.1  thorpej 
     44  1.1  thorpej /*
     45  1.1  thorpej  * Apple OpenFirmware implementations have the i2c device
     46  1.1  thorpej  * address shifted left 1 bit to account for the r/w bit
     47  1.1  thorpej  * on the wire.  Some implementations also encode the channel
     48  1.1  thorpej  * number (for multi-channel controllers) in bit 8 of the
     49  1.1  thorpej  * address.
     50  1.1  thorpej  */
     51  1.1  thorpej #define	OFW_I2C_ADDRESS_MASK	__BITS(1,7)
     52  1.1  thorpej #define	OFW_I2C_ADDRESS_CHMASK	__BIT(8)
     53  1.1  thorpej 
     54  1.1  thorpej static bool
     55  1.1  thorpej of_i2c_get_address(device_t dev, int i2c_node, i2c_tag_t tag, int node,
     56  1.1  thorpej     uint32_t *addrp)
     57  1.1  thorpej {
     58  1.1  thorpej 	uint32_t reg;
     59  1.1  thorpej 	uint32_t addr;
     60  1.1  thorpej 	int channel;
     61  1.1  thorpej 
     62  1.1  thorpej 	/*
     63  1.1  thorpej 	 * dev is the iic bus instance.  We need the controller parent
     64  1.1  thorpej 	 * so we can compare their OFW nodes.
     65  1.1  thorpej 	 */
     66  1.1  thorpej 	int ctlr_node = devhandle_to_of(device_handle(device_parent(dev)));
     67  1.1  thorpej 
     68  1.1  thorpej 	if (OF_getprop(node, "reg", &reg, sizeof(reg)) != sizeof(reg)) {
     69  1.1  thorpej 		/*
     70  1.1  thorpej 		 * Some Apple OpenFirmware implementations use "i2c-address"
     71  1.1  thorpej 		 * instead of "reg".
     72  1.1  thorpej 		 */
     73  1.1  thorpej 		if (OF_getprop(node, "i2c-address", &reg,
     74  1.1  thorpej 			       sizeof(reg)) != sizeof(reg)) {
     75  1.1  thorpej 			/*
     76  1.1  thorpej 			 * No address property; reject the device.
     77  1.1  thorpej 			 */
     78  1.1  thorpej 			return false;
     79  1.1  thorpej 		}
     80  1.1  thorpej 	}
     81  1.1  thorpej 
     82  1.1  thorpej 	addr = __SHIFTOUT(reg, OFW_I2C_ADDRESS_MASK);
     83  1.1  thorpej 	channel = __SHIFTOUT(reg, OFW_I2C_ADDRESS_CHMASK);
     84  1.1  thorpej 
     85  1.2  thorpej 	aprint_debug_dev(dev, "%s: addr=0x%x channel=%u\n", __func__,
     86  1.2  thorpej 	    addr, channel);
     87  1.2  thorpej 
     88  1.1  thorpej 	/*
     89  1.1  thorpej 	 * If the controller supports multiple channels and the controller
     90  1.1  thorpej 	 * node and the i2c bus node are the same, then the devices for
     91  1.1  thorpej 	 * multiple channels are all mixed together in the device tree.
     92  1.1  thorpej 	 * We need to filter them by channel in this case.
     93  1.1  thorpej 	 */
     94  1.2  thorpej 	aprint_debug_dev(dev, "%s: ic_channel=%d i2c_node=%d ctlr_node=%d\n",
     95  1.2  thorpej 	    __func__, tag->ic_channel, i2c_node, ctlr_node);
     96  1.1  thorpej 	if (tag->ic_channel != I2C_CHANNEL_DEFAULT && i2c_node == ctlr_node &&
     97  1.1  thorpej 	    tag->ic_channel != channel) {
     98  1.1  thorpej 		return false;
     99  1.1  thorpej 	}
    100  1.1  thorpej 
    101  1.1  thorpej 	*addrp = addr;
    102  1.1  thorpej 	return true;
    103  1.1  thorpej }
    104  1.1  thorpej 
    105  1.1  thorpej static int
    106  1.1  thorpej of_i2c_enumerate_devices(device_t dev, devhandle_t call_handle, void *v)
    107  1.1  thorpej {
    108  1.1  thorpej 	struct i2c_enumerate_devices_args *args = v;
    109  1.1  thorpej 	int i2c_node, node;
    110  1.1  thorpej 	char name[32], compat_buf[32];
    111  1.1  thorpej 	uint32_t addr;
    112  1.1  thorpej 	char *clist;
    113  1.1  thorpej 	int clist_size;
    114  1.1  thorpej 	bool cbrv;
    115  1.1  thorpej 
    116  1.1  thorpej 	i2c_node = devhandle_to_of(call_handle);
    117  1.1  thorpej 
    118  1.1  thorpej 	for (node = OF_child(i2c_node); node != 0; node = OF_peer(node)) {
    119  1.1  thorpej 		if (OF_getprop(node, "name", name, sizeof(name)) <= 0) {
    120  1.1  thorpej 			continue;
    121  1.1  thorpej 		}
    122  1.1  thorpej 		if (!of_i2c_get_address(dev, i2c_node, args->ia->ia_tag,
    123  1.1  thorpej 					node, &addr)) {
    124  1.1  thorpej 			continue;
    125  1.1  thorpej 		}
    126  1.1  thorpej 
    127  1.1  thorpej 		/*
    128  1.1  thorpej 		 * Some of Apple's older OpenFirmware implementations are
    129  1.1  thorpej 		 * rife with nodes lacking "compatible" properties.
    130  1.1  thorpej 		 */
    131  1.1  thorpej 		clist_size = OF_getproplen(node, "compatible");
    132  1.1  thorpej 		if (clist_size <= 0) {
    133  1.1  thorpej 			clist = name;
    134  1.1  thorpej 			clist_size = 0;
    135  1.1  thorpej 		} else {
    136  1.1  thorpej 			clist = kmem_tmpbuf_alloc(clist_size,
    137  1.1  thorpej 			    compat_buf, sizeof(compat_buf), KM_SLEEP);
    138  1.1  thorpej 			if (OF_getprop(node, "compatible", clist, clist_size)
    139  1.1  thorpej 				       < clist_size) {
    140  1.1  thorpej 				kmem_tmpbuf_free(clist, clist_size, compat_buf);
    141  1.1  thorpej 				continue;
    142  1.1  thorpej 			}
    143  1.1  thorpej 		}
    144  1.1  thorpej 
    145  1.1  thorpej 		cbrv = i2c_enumerate_device(dev, args, name,
    146  1.1  thorpej 		    clist, clist_size, addr,
    147  1.1  thorpej 		    devhandle_from_of(call_handle, node));
    148  1.1  thorpej 
    149  1.1  thorpej 		if (clist != name) {
    150  1.1  thorpej 			kmem_tmpbuf_free(clist, clist_size, compat_buf);
    151  1.1  thorpej 		}
    152  1.1  thorpej 
    153  1.1  thorpej 		if (!cbrv) {
    154  1.1  thorpej 			break;
    155  1.1  thorpej 		}
    156  1.1  thorpej 	}
    157  1.1  thorpej 
    158  1.1  thorpej 	return 0;
    159  1.1  thorpej }
    160  1.1  thorpej OF_DEVICE_CALL_REGISTER(I2C_ENUMERATE_DEVICES_STR,
    161  1.1  thorpej 			of_i2c_enumerate_devices);
    162