Home | History | Annotate | Line # | Download | only in ofw
ofw_i2c_subr.c revision 1.1.6.6
      1 /*	$NetBSD: ofw_i2c_subr.c,v 1.1.6.6 2021/05/14 02:51:43 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Copyright 1998
     31  * Digital Equipment Corporation. All rights reserved.
     32  *
     33  * This software is furnished under license and may be used and
     34  * copied only in accordance with the following terms and conditions.
     35  * Subject to these conditions, you may download, copy, install,
     36  * use, modify and distribute this software in source and/or binary
     37  * form. No title or ownership is transferred hereby.
     38  *
     39  * 1) Any source code used, modified or distributed must reproduce
     40  *    and retain this copyright notice and list of conditions as
     41  *    they appear in the source file.
     42  *
     43  * 2) No right is granted to use any trade name, trademark, or logo of
     44  *    Digital Equipment Corporation. Neither the "Digital Equipment
     45  *    Corporation" name nor any trademark or logo of Digital Equipment
     46  *    Corporation may be used to endorse or promote products derived
     47  *    from this software without the prior written permission of
     48  *    Digital Equipment Corporation.
     49  *
     50  * 3) This software is provided "AS-IS" and any express or implied
     51  *    warranties, including but not limited to, any implied warranties
     52  *    of merchantability, fitness for a particular purpose, or
     53  *    non-infringement are disclaimed. In no event shall DIGITAL be
     54  *    liable for any damages whatsoever, and in particular, DIGITAL
     55  *    shall not be liable for special, indirect, consequential, or
     56  *    incidental damages or damages for lost profits, loss of
     57  *    revenue or loss of use, whether such damages arise in contract,
     58  *    negligence, tort, under statute, in equity, at law or otherwise,
     59  *    even if advised of the possibility of such damage.
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: ofw_i2c_subr.c,v 1.1.6.6 2021/05/14 02:51:43 thorpej Exp $");
     64 
     65 #include <sys/param.h>
     66 #include <sys/device.h>
     67 #include <sys/endian.h>
     68 #include <sys/kmem.h>
     69 #include <sys/systm.h>
     70 #include <dev/ofw/openfirm.h>
     71 #include <dev/i2c/i2cvar.h>
     72 
     73 #ifdef __HAVE_OPENFIRMWARE_VARIANT_AAPL
     74 /*
     75  * Apple OpenFirmware implementations have the i2c device
     76  * address shifted left 1 bit to account for the r/w bit
     77  * on the wire.  We also want to look at only the least-
     78  * significant 8 bits of the address cell.
     79  */
     80 #define	OFW_I2C_ADDRESS_MASK	__BITS(0,7)
     81 #define	OFW_I2C_ADDRESS_SHIFT	1
     82 #endif /* __HAVE_OPENFIRMWARE_VARIANT_AAPL */
     83 
     84 #ifdef __HAVE_OPENFIRMWARE_VARIANT_SUNW
     85 /*
     86  * Sun OpenFirmware implementations use 2 cells for the
     87  * i2c device "reg" property, the first containing the
     88  * channel number, the second containing the i2c device
     89  * address shifted left 1 bit to account for the r/w bit
     90  * on the wire.
     91  */
     92 #define	OFW_I2C_REG_NCELLS	2
     93 #define	OFW_I2C_REG_CHANNEL	0
     94 #define	OFW_I2C_REG_ADDRESS	1
     95 #define	OFW_I2C_ADDRESS_SHIFT	1
     96 #endif /* __HAVE_OPENFIRMWARE_VARIANT_SUNW */
     97 
     98 #ifndef OFW_I2C_REG_NCELLS
     99 #define	OFW_I2C_REG_NCELLS	1
    100 #endif
    101 
    102 #ifndef OFW_I2C_REG_ADDRESS
    103 #define	OFW_I2C_REG_ADDRESS	0
    104 #endif
    105 
    106 /* No default for OFW_I2C_REG_CHANNEL. */
    107 
    108 #ifndef OFW_I2C_ADDRESS_MASK
    109 #define	OFW_I2C_ADDRESS_MASK	__BITS(0,31)
    110 #endif
    111 
    112 #ifndef OFW_I2C_ADDRESS_SHIFT
    113 #define	OFW_I2C_ADDRESS_SHIFT	0
    114 #endif
    115 
    116 static bool
    117 of_i2c_get_address(i2c_tag_t tag, int node, uint32_t *addrp)
    118 {
    119 	uint32_t reg[OFW_I2C_REG_NCELLS];
    120 	uint32_t addr;
    121 #ifdef OFW_I2C_REG_CHANNEL
    122 	uint32_t channel;
    123 #endif
    124 
    125 	if (OF_getprop(node, "reg", reg, sizeof(reg)) != sizeof(reg)) {
    126 		/*
    127 		 * "reg" property is malformed; reject the device.
    128 		 */
    129 		return false;
    130 	}
    131 
    132 	addr = be32toh(reg[OFW_I2C_REG_ADDRESS]);
    133 	addr = (addr & OFW_I2C_ADDRESS_MASK) >> OFW_I2C_ADDRESS_SHIFT;
    134 
    135 #ifdef OFW_I2C_REG_CHANNEL
    136 	/*
    137 	 * If the channel in the "reg" property does not match,
    138 	 * reject the device.
    139 	 */
    140 	channel = be32toh(reg[OFW_I2C_REG_CHANNEL]);
    141 	if (channel != tag->ic_channel) {
    142 		return false;
    143 	}
    144 #endif
    145 
    146 	*addrp = addr;
    147 	return true;
    148 }
    149 
    150 /*
    151  * This follows the Device Tree bindings for i2c, which for the most part
    152  * work for the classical OpenFirmware implementations, as well.  There are
    153  * some quirks do deal with for different OpenFirmware implementations, which
    154  * are mainly in how the "reg" property is interpreted.
    155  */
    156 static int
    157 of_i2c_enumerate_devices(device_t dev, devhandle_t call_handle, void *v)
    158 {
    159 	struct i2c_enumerate_devices_args *args = v;
    160 	int i2c_node, node;
    161 	char name[32], compat_buf[32];
    162 	prop_dictionary_t props;
    163 	uint32_t addr;
    164 	char *clist;
    165 	int clist_size;
    166 	bool cbrv;
    167 
    168 	i2c_node = devhandle_to_of(call_handle);
    169 
    170 	for (node = OF_child(i2c_node); node != 0; node = OF_peer(node)) {
    171 		if (OF_getprop(node, "name", name, sizeof(name)) <= 0) {
    172 			continue;
    173 		}
    174 		if (!of_i2c_get_address(args->ia->ia_tag, node, &addr)) {
    175 			continue;
    176 		}
    177 
    178 		clist_size = OF_getproplen(node, "compatible");
    179 		clist = kmem_tmpbuf_alloc(clist_size,
    180 		    compat_buf, sizeof(compat_buf), KM_SLEEP);
    181 		if (OF_getprop(node, "compatible", clist, clist_size) <
    182 		    clist_size) {
    183 			kmem_tmpbuf_free(clist, clist_size, compat_buf);
    184 			continue;
    185 		}
    186 		props = prop_dictionary_create();
    187 
    188 		args->ia->ia_addr = (i2c_addr_t)addr;
    189 		args->ia->ia_name = name;
    190 		args->ia->ia_clist = clist;
    191 		args->ia->ia_clist_size = clist_size;
    192 		args->ia->ia_prop = props;
    193 		args->ia->ia_devhandle = devhandle_from_of(node);
    194 
    195 		cbrv = args->callback(dev, args);
    196 
    197 		prop_object_release(props);
    198 		kmem_tmpbuf_free(clist, clist_size, compat_buf);
    199 
    200 		if (!cbrv) {
    201 			break;
    202 		}
    203 	}
    204 
    205 	return 0;
    206 }
    207 OF_DEVICE_CALL_REGISTER("i2c-enumerate-devices", of_i2c_enumerate_devices);
    208