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