Home | History | Annotate | Line # | Download | only in ofw
ofw_i2c_subr.c revision 1.1.6.3
      1 /*	$NetBSD: ofw_i2c_subr.c,v 1.1.6.3 2021/05/14 00:44:13 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.3 2021/05/14 00:44:13 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 static bool
     74 of_i2c_get_address(int node, uint32_t *addrp)
     75 {
     76 	uint32_t reg;
     77 
     78 	if (OF_getprop(node, "reg", &reg, sizeof(reg)) != sizeof(reg)) {
     79 		return false;
     80 	}
     81 
     82 	reg = be32toh(reg);
     83 
     84 #ifdef __HAVE_OPENFIRMWARE_VARIANT_AAPL
     85 	/*
     86 	 * Apple OpenFirmware implementations have the i2c device
     87 	 * address shifted left 1 bit to account for the r/w bit
     88 	 * on the wire.
     89 	 */
     90 	reg = (reg & 0xff) >> 1;
     91 #endif /* __HAVE_OPENFIRMWARE_VARIANT_AAPL */
     92 
     93 	*addrp = reg;
     94 	return true;
     95 }
     96 
     97 /*
     98  * This follows the Device Tree bindings for i2c, which for the most part
     99  * work for the classical OpenFirmware implementations, as well.  There are
    100  * some quirks do deal with for different OpenFirmware implementations, which
    101  * are mainly in how the "reg" property is interpreted.
    102  */
    103 static int
    104 of_i2c_enumerate_devices(device_t dev, devhandle_t call_handle, void *v)
    105 {
    106 	struct i2c_enumerate_devices_args *args = v;
    107 	int i2c_node, node;
    108 	char name[32], compat_buf[32];
    109 	prop_dictionary_t props;
    110 	uint32_t addr;
    111 	char *clist;
    112 	int clist_size;
    113 	bool cbrv;
    114 
    115 	i2c_node = devhandle_to_of(call_handle);
    116 
    117 	for (node = OF_child(i2c_node); node != 0; node = OF_peer(node)) {
    118 		if (OF_getprop(node, "name", name, sizeof(name)) <= 0) {
    119 			continue;
    120 		}
    121 		if (!of_i2c_get_address(node, &addr)) {
    122 			continue;
    123 		}
    124 
    125 		clist_size = OF_getproplen(node, "compatible");
    126 		clist = kmem_tmpbuf_alloc(clist_size,
    127 		    compat_buf, sizeof(compat_buf), KM_SLEEP);
    128 		if (OF_getprop(node, "compatible", clist, clist_size) <
    129 		    clist_size) {
    130 			kmem_tmpbuf_free(clist, clist_size, compat_buf);
    131 			continue;
    132 		}
    133 		props = prop_dictionary_create();
    134 
    135 		args->ia->ia_addr = (i2c_addr_t)addr;
    136 		args->ia->ia_name = name;
    137 		args->ia->ia_clist = clist;
    138 		args->ia->ia_clist_size = clist_size;
    139 		args->ia->ia_prop = props;
    140 		args->ia->ia_devhandle = devhandle_from_of(node);
    141 
    142 		cbrv = args->callback(dev, args);
    143 
    144 		prop_object_release(props);
    145 		kmem_tmpbuf_free(clist, clist_size, compat_buf);
    146 
    147 		if (!cbrv) {
    148 			break;
    149 		}
    150 	}
    151 
    152 	return 0;
    153 }
    154 OF_DEVICE_CALL_REGISTER("i2c-enumerate-devices", of_i2c_enumerate_devices);
    155