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
44prop_dictionary_t board_properties;
45
46void
47board_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
59bool
60board_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
71void
72board_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
82uint64_t
83board_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
92void
93board_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
103void
104board_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
114const void *
115board_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
124void
125board_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
135void
136board_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
142void *
143board_info_get_object(const char *name)
144{
145	return prop_dictionary_get(board_properties, name);
146}
147