1*42bd2e0cSthorpej /* $NetBSD: ofw_i2c_subr.c,v 1.1 2021/02/04 20:19:09 thorpej Exp $ */
2*42bd2e0cSthorpej
3*42bd2e0cSthorpej /*
4*42bd2e0cSthorpej * Copyright 1998
5*42bd2e0cSthorpej * Digital Equipment Corporation. All rights reserved.
6*42bd2e0cSthorpej *
7*42bd2e0cSthorpej * This software is furnished under license and may be used and
8*42bd2e0cSthorpej * copied only in accordance with the following terms and conditions.
9*42bd2e0cSthorpej * Subject to these conditions, you may download, copy, install,
10*42bd2e0cSthorpej * use, modify and distribute this software in source and/or binary
11*42bd2e0cSthorpej * form. No title or ownership is transferred hereby.
12*42bd2e0cSthorpej *
13*42bd2e0cSthorpej * 1) Any source code used, modified or distributed must reproduce
14*42bd2e0cSthorpej * and retain this copyright notice and list of conditions as
15*42bd2e0cSthorpej * they appear in the source file.
16*42bd2e0cSthorpej *
17*42bd2e0cSthorpej * 2) No right is granted to use any trade name, trademark, or logo of
18*42bd2e0cSthorpej * Digital Equipment Corporation. Neither the "Digital Equipment
19*42bd2e0cSthorpej * Corporation" name nor any trademark or logo of Digital Equipment
20*42bd2e0cSthorpej * Corporation may be used to endorse or promote products derived
21*42bd2e0cSthorpej * from this software without the prior written permission of
22*42bd2e0cSthorpej * Digital Equipment Corporation.
23*42bd2e0cSthorpej *
24*42bd2e0cSthorpej * 3) This software is provided "AS-IS" and any express or implied
25*42bd2e0cSthorpej * warranties, including but not limited to, any implied warranties
26*42bd2e0cSthorpej * of merchantability, fitness for a particular purpose, or
27*42bd2e0cSthorpej * non-infringement are disclaimed. In no event shall DIGITAL be
28*42bd2e0cSthorpej * liable for any damages whatsoever, and in particular, DIGITAL
29*42bd2e0cSthorpej * shall not be liable for special, indirect, consequential, or
30*42bd2e0cSthorpej * incidental damages or damages for lost profits, loss of
31*42bd2e0cSthorpej * revenue or loss of use, whether such damages arise in contract,
32*42bd2e0cSthorpej * negligence, tort, under statute, in equity, at law or otherwise,
33*42bd2e0cSthorpej * even if advised of the possibility of such damage.
34*42bd2e0cSthorpej */
35*42bd2e0cSthorpej
36*42bd2e0cSthorpej #include <sys/cdefs.h>
37*42bd2e0cSthorpej __KERNEL_RCSID(0, "$NetBSD: ofw_i2c_subr.c,v 1.1 2021/02/04 20:19:09 thorpej Exp $");
38*42bd2e0cSthorpej
39*42bd2e0cSthorpej #include <sys/param.h>
40*42bd2e0cSthorpej #include <sys/device.h>
41*42bd2e0cSthorpej #include <sys/kmem.h>
42*42bd2e0cSthorpej #include <sys/systm.h>
43*42bd2e0cSthorpej #include <dev/ofw/openfirm.h>
44*42bd2e0cSthorpej #include <dev/i2c/i2cvar.h>
45*42bd2e0cSthorpej
46*42bd2e0cSthorpej /*
47*42bd2e0cSthorpej * Iterate over the subtree of a i2c controller node.
48*42bd2e0cSthorpej * Add all sub-devices into an array as part of the controller's
49*42bd2e0cSthorpej * device properties.
50*42bd2e0cSthorpej * This is used by the i2c bus attach code to do direct configuration.
51*42bd2e0cSthorpej */
52*42bd2e0cSthorpej void
of_enter_i2c_devs(prop_dictionary_t props,int ofnode,size_t cell_size,int addr_shift)53*42bd2e0cSthorpej of_enter_i2c_devs(prop_dictionary_t props, int ofnode, size_t cell_size,
54*42bd2e0cSthorpej int addr_shift)
55*42bd2e0cSthorpej {
56*42bd2e0cSthorpej int node, len;
57*42bd2e0cSthorpej char name[32];
58*42bd2e0cSthorpej uint64_t reg64;
59*42bd2e0cSthorpej uint32_t reg32;
60*42bd2e0cSthorpej uint64_t addr;
61*42bd2e0cSthorpej prop_array_t array = NULL;
62*42bd2e0cSthorpej prop_dictionary_t dev;
63*42bd2e0cSthorpej
64*42bd2e0cSthorpej for (node = OF_child(ofnode); node; node = OF_peer(node)) {
65*42bd2e0cSthorpej if (OF_getprop(node, "name", name, sizeof(name)) <= 0)
66*42bd2e0cSthorpej continue;
67*42bd2e0cSthorpej len = OF_getproplen(node, "reg");
68*42bd2e0cSthorpej addr = 0;
69*42bd2e0cSthorpej if (cell_size == 8 && len >= sizeof(reg64)) {
70*42bd2e0cSthorpej if (OF_getprop(node, "reg", ®64, sizeof(reg64))
71*42bd2e0cSthorpej < sizeof(reg64))
72*42bd2e0cSthorpej continue;
73*42bd2e0cSthorpej addr = be64toh(reg64);
74*42bd2e0cSthorpej /*
75*42bd2e0cSthorpej * The i2c bus number (0 or 1) is encoded in bit 33
76*42bd2e0cSthorpej * of the register, but we encode it in bit 8 of
77*42bd2e0cSthorpej * i2c_addr_t.
78*42bd2e0cSthorpej */
79*42bd2e0cSthorpej if (addr & 0x100000000)
80*42bd2e0cSthorpej addr = (addr & 0xff) | 0x100;
81*42bd2e0cSthorpej } else if (cell_size == 4 && len >= sizeof(reg32)) {
82*42bd2e0cSthorpej if (OF_getprop(node, "reg", ®32, sizeof(reg32))
83*42bd2e0cSthorpej < sizeof(reg32))
84*42bd2e0cSthorpej continue;
85*42bd2e0cSthorpej addr = be32toh(reg32);
86*42bd2e0cSthorpej } else {
87*42bd2e0cSthorpej continue;
88*42bd2e0cSthorpej }
89*42bd2e0cSthorpej addr >>= addr_shift;
90*42bd2e0cSthorpej if (addr == 0) continue;
91*42bd2e0cSthorpej
92*42bd2e0cSthorpej if (array == NULL)
93*42bd2e0cSthorpej array = prop_array_create();
94*42bd2e0cSthorpej
95*42bd2e0cSthorpej dev = prop_dictionary_create();
96*42bd2e0cSthorpej prop_dictionary_set_string(dev, "name", name);
97*42bd2e0cSthorpej prop_dictionary_set_uint32(dev, "addr", addr);
98*42bd2e0cSthorpej prop_dictionary_set_uint64(dev, "cookie", node);
99*42bd2e0cSthorpej prop_dictionary_set_uint32(dev, "cookietype", I2C_COOKIE_OF);
100*42bd2e0cSthorpej of_to_dataprop(dev, node, "compatible", "compatible");
101*42bd2e0cSthorpej prop_array_add(array, dev);
102*42bd2e0cSthorpej prop_object_release(dev);
103*42bd2e0cSthorpej }
104*42bd2e0cSthorpej
105*42bd2e0cSthorpej if (array != NULL) {
106*42bd2e0cSthorpej prop_dictionary_set(props, "i2c-child-devices", array);
107*42bd2e0cSthorpej prop_object_release(array);
108*42bd2e0cSthorpej }
109*42bd2e0cSthorpej }
110