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