1 /*        $NetBSD: board_prop.c,v 1.3 2022/07/22 19:52:29 thorpej Exp $         */
2 
3 /*
4  * Copyright (c) 2004 Shigeyuki Fukushima.
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
13  *    copyright notice, this list of conditions and the following
14  *    disclaimer in the documentation and/or other materials provided
15  *    with the distribution.
16  * 3. The name of the author may not be used to endorse or promote
17  *    products derived from this software without specific prior
18  *    written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: board_prop.c,v 1.3 2022/07/22 19:52:29 thorpej Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/cpu.h>
39 
40 #include <prop/proplib.h>
41 
42 #include <powerpc/booke/cpuvar.h>
43 
44 prop_dictionary_t board_properties;
45 
46 void
board_info_init(void)47 board_info_init(void)
48 {
49 
50           /*
51            * Set up the board properties dictionary.
52            */
53           if (board_properties != NULL)
54                     return;
55           board_properties = prop_dictionary_create();
56           KASSERT(board_properties != NULL);
57 }
58 
59 bool
board_info_get_bool(const char * name)60 board_info_get_bool(const char *name)
61 {
62           KASSERT(board_properties != NULL);
63           prop_bool_t pb = prop_dictionary_get(board_properties, name);
64           if (pb == NULL)
65                     return false;
66           const bool value = prop_bool_true(pb);
67           /* XXX -- do we need object release pb? */
68           return value;
69 }
70 
71 void
board_info_add_bool(const char * name)72 board_info_add_bool(const char *name)
73 {
74           KASSERT(board_properties != NULL);
75           prop_bool_t pb = prop_bool_create(true);
76           KASSERT(pb != NULL);
77           if (prop_dictionary_set(board_properties, name, pb) == false)
78                     panic("%s: setting %s", __func__, name);
79           prop_object_release(pb);
80 }
81 
82 uint64_t
board_info_get_number(const char * name)83 board_info_get_number(const char *name)
84 {
85           KASSERT(board_properties != NULL);
86           prop_number_t pn = prop_dictionary_get(board_properties, name);
87           KASSERT(pn != NULL);
88           const uint64_t number = prop_number_unsigned_value(pn);
89           return number;
90 }
91 
92 void
board_info_add_number(const char * name,uint64_t number)93 board_info_add_number(const char *name, uint64_t number)
94 {
95           KASSERT(board_properties != NULL);
96           prop_number_t pn = prop_number_create_unsigned(number);
97           KASSERT(pn != NULL);
98           if (prop_dictionary_set(board_properties, name, pn) == false)
99                     panic("%s: setting %s failed", __func__, name);
100           prop_object_release(pn);
101 }
102 
103 void
board_info_add_data(const char * name,const void * data,size_t len)104 board_info_add_data(const char *name, const void *data, size_t len)
105 {
106           KASSERT(board_properties != NULL);
107           prop_data_t pd = prop_data_create_copy(data, len);
108           KASSERT(pd != NULL);
109           if (prop_dictionary_set(board_properties, name, pd) == false)
110                     panic("%s: setting %s failed", __func__, name);
111           prop_object_release(pd);
112 }
113 
114 const void *
board_info_get_data(const char * name,size_t * lenp)115 board_info_get_data(const char *name, size_t *lenp)
116 {
117           KASSERT(board_properties != NULL);
118           prop_data_t pd = prop_dictionary_get(board_properties, name);
119           KASSERT(pd != NULL);
120           *lenp = prop_data_size(pd);
121           return prop_data_value(pd);
122 }
123 
124 void
board_info_add_string(const char * name,const char * data)125 board_info_add_string(const char *name, const char *data)
126 {
127           KASSERT(board_properties != NULL);
128           prop_string_t ps = prop_string_create_copy(data);
129           KASSERT(ps != NULL);
130           if (prop_dictionary_set(board_properties, name, ps) == false)
131                     panic("%s: setting %s failed", __func__, name);
132           prop_object_release(ps);
133 }
134 
135 void
board_info_add_object(const char * name,void * obj)136 board_info_add_object(const char *name, void *obj)
137 {
138           if (prop_dictionary_set(board_properties, name, obj) == false)
139                     panic("%s: setting %s failed", __func__, name);
140 }
141 
142 void *
board_info_get_object(const char * name)143 board_info_get_object(const char *name)
144 {
145           return prop_dictionary_get(board_properties, name);
146 }
147