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