1 /**
2  * Copyright © 2016 Google Inc.
3  * Copyright © 2016 IBM Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "gpio_configs.h"
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 
25 gboolean read_gpios(GDBusConnection *connection, GpioConfigs *gpios)
26 {
27 	GDBusProxy *proxy;
28 	GError *error = NULL;
29 	GVariant *value;
30 
31 	GVariantIter *power_up_outs_iter;
32 	GVariantIter *reset_outs_iter;
33 	GVariantIter *pci_reset_outs_iter;
34 	gchar *power_up_out_name;
35 	gchar *reset_out_name;
36 	gchar *pci_reset_out_name;
37 	gboolean power_up_polarity;
38 	gboolean reset_out_polarity;
39 	gboolean pci_reset_out_polarity;
40 	gboolean pci_reset_out_hold;
41 
42 	GVariantIter *optionals_iter;
43 	gchar *optional_name;
44 	gboolean optional_polarity;
45 	int i;
46 
47 	proxy = g_dbus_proxy_new_sync(connection,
48 			G_DBUS_PROXY_FLAGS_NONE,
49 			NULL,							/* GDBusInterfaceInfo */
50 			"org.openbmc.managers.System",	/* name */
51 			"/org/openbmc/managers/System",	/* object path */
52 			"org.openbmc.managers.System",	/* interface */
53 			NULL,							/* GCancellable */
54 			&error);
55 	if(error != NULL) {
56 		fprintf(stderr, "Unable to create proxy: %s\n", error->message);
57 		g_error_free(error);
58 		return FALSE;
59 	}
60 
61 	value = g_dbus_proxy_call_sync(proxy,
62 			"getGpioConfiguration",
63 			NULL,
64 			G_DBUS_CALL_FLAGS_NONE,
65 			-1,
66 			NULL,
67 			&error);
68 	if(error != NULL) {
69 		fprintf(stderr, "Power GPIO: call to getGpioConfiguration failed: %s\n",
70 				error->message);
71 		g_error_free(error);
72 		return FALSE;
73 	}
74 
75 	g_assert(value != NULL);
76 	memset(gpios, 0, sizeof(*gpios));
77 	g_variant_get(
78 		value, "(&s&sa(sb)a(sb)a(sbb)&s&s&s&sa(sb))",
79 		&gpios->power_gpio.power_good_in.name, &gpios->power_gpio.latch_out.name,
80 		&power_up_outs_iter, &reset_outs_iter, &pci_reset_outs_iter,
81 		&gpios->hostctl_gpio.fsi_data.name, &gpios->hostctl_gpio.fsi_clk.name,
82 		&gpios->hostctl_gpio.fsi_enable.name, &gpios->hostctl_gpio.cronus_sel.name,
83 		&optionals_iter);
84 
85 	g_print("Power GPIO latch output: %s\n", gpios->power_gpio.latch_out.name);
86 	if(*gpios->power_gpio.latch_out.name != '\0') {  /* latch is optional */
87 		gpios->power_gpio.latch_out.name = strdup(gpios->power_gpio.latch_out.name);
88 	}
89 	else {
90 		gpios->power_gpio.latch_out.name = NULL;
91 	}
92 	g_print("Power GPIO power good input: %s\n", gpios->power_gpio.power_good_in.name);
93 	gpios->power_gpio.power_good_in.name = g_strdup(gpios->power_gpio.power_good_in.name);
94 	gpios->power_gpio.num_power_up_outs = g_variant_iter_n_children(
95 			power_up_outs_iter);
96 	g_print("Power GPIO %zu power_up outputs\n",
97 			gpios->power_gpio.num_power_up_outs);
98 	gpios->power_gpio.power_up_outs = g_malloc0_n(gpios->power_gpio.num_power_up_outs,
99 			sizeof(GPIO));
100 	gpios->power_gpio.power_up_pols = g_malloc0_n(gpios->power_gpio.num_power_up_outs,
101 			sizeof(gboolean));
102 	for(i = 0; g_variant_iter_next(power_up_outs_iter, "(&sb)",
103 				&power_up_out_name, &power_up_polarity);
104 			i++) {
105 		g_print("Power GPIO power_up[%d] = %s active %s\n", i,
106 				power_up_out_name, power_up_polarity ? "HIGH" : "LOW");
107 		gpios->power_gpio.power_up_outs[i].name = g_strdup(power_up_out_name);
108 		gpios->power_gpio.power_up_pols[i] = power_up_polarity;
109 	}
110 	gpios->power_gpio.num_reset_outs = g_variant_iter_n_children(reset_outs_iter);
111 	g_print("Power GPIO %zu reset outputs\n", gpios->power_gpio.num_reset_outs);
112 	gpios->power_gpio.reset_outs = g_malloc0_n(gpios->power_gpio.num_reset_outs, sizeof(GPIO));
113 	gpios->power_gpio.reset_pols = g_malloc0_n(gpios->power_gpio.num_reset_outs,
114 			sizeof(gboolean));
115 	for(i = 0; g_variant_iter_next(reset_outs_iter, "(&sb)", &reset_out_name,
116 				&reset_out_polarity); i++) {
117 		g_print("Power GPIO reset[%d] = %s active %s\n", i, reset_out_name,
118 				reset_out_polarity ? "HIGH" : "LOW");
119 		gpios->power_gpio.reset_outs[i].name = g_strdup(reset_out_name);
120 		gpios->power_gpio.reset_pols[i] = reset_out_polarity;
121 	}
122 
123 	gpios->power_gpio.num_pci_reset_outs = g_variant_iter_n_children(pci_reset_outs_iter);
124 	g_print("Power GPIO %d pci reset outputs\n", gpios->power_gpio.num_pci_reset_outs);
125 	gpios->power_gpio.pci_reset_outs = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs,
126 			sizeof(GPIO));
127 	gpios->power_gpio.pci_reset_pols = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs,
128 			sizeof(gboolean));
129 	gpios->power_gpio.pci_reset_holds = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs,
130 			sizeof(gboolean));
131 	for(i = 0; g_variant_iter_next(pci_reset_outs_iter, "(&sbb)", &pci_reset_out_name,
132 				&pci_reset_out_polarity, &pci_reset_out_hold); i++) {
133 		g_print("Power GPIO pci reset[%d] = %s active %s, hold - %s\n", i,
134 				pci_reset_out_name,
135 				pci_reset_out_polarity ? "HIGH" : "LOW",
136 				pci_reset_out_hold ? "Yes" : "No");
137 		gpios->power_gpio.pci_reset_outs[i].name = g_strdup(pci_reset_out_name);
138 		gpios->power_gpio.pci_reset_pols[i] = pci_reset_out_polarity;
139 		gpios->power_gpio.pci_reset_holds[i] = pci_reset_out_hold;
140 	}
141 
142 
143 	g_print("FSI DATA GPIO: %s\n", gpios->hostctl_gpio.fsi_data.name);
144 	gpios->hostctl_gpio.fsi_data.name = strdup(gpios->hostctl_gpio.fsi_data.name);
145 
146 	g_print("FSI CLK GPIO: %s\n", gpios->hostctl_gpio.fsi_clk.name);
147 	gpios->hostctl_gpio.fsi_clk.name = strdup(gpios->hostctl_gpio.fsi_clk.name);
148 
149 	g_print("FSI ENABLE GPIO: %s\n", gpios->hostctl_gpio.fsi_enable.name);
150 	gpios->hostctl_gpio.fsi_enable.name = strdup(gpios->hostctl_gpio.fsi_enable.name);
151 
152 	g_print("CRONUS SEL GPIO: %s\n", gpios->hostctl_gpio.cronus_sel.name);
153 	gpios->hostctl_gpio.cronus_sel.name = strdup(gpios->hostctl_gpio.cronus_sel.name);
154 
155 	gpios->hostctl_gpio.num_optionals = g_variant_iter_n_children(optionals_iter);
156 	g_print("Hostctl GPIO optionals: %zu\n", gpios->hostctl_gpio.num_optionals);
157 	gpios->hostctl_gpio.optionals = g_malloc0_n(gpios->hostctl_gpio.num_optionals, sizeof(GPIO));
158 	gpios->hostctl_gpio.optional_pols = g_malloc0_n(gpios->hostctl_gpio.num_optionals, sizeof(gboolean));
159 	for (i = 0; g_variant_iter_next(optionals_iter, "(&sb)", &optional_name, &optional_polarity); ++i) {
160 		g_print("Hostctl optional GPIO[%d] = %s active %s\n", i, optional_name, optional_polarity ? "HIGH" : "LOW");
161 		gpios->hostctl_gpio.optionals[i].name = g_strdup(optional_name);
162 		gpios->hostctl_gpio.optional_pols[i] = optional_polarity;
163 	}
164 
165 	g_variant_iter_free(power_up_outs_iter);
166 	g_variant_iter_free(reset_outs_iter);
167 	g_variant_iter_free(pci_reset_outs_iter);
168 	g_variant_iter_free(optionals_iter);
169 	g_variant_unref(value);
170 
171 	return TRUE;
172 }
173 
174 void free_gpios(GpioConfigs *gpios) {
175 	int i;
176 	g_free(gpios->power_gpio.latch_out.name);
177 	g_free(gpios->power_gpio.power_good_in.name);
178 	for(i = 0; i < gpios->power_gpio.num_power_up_outs; i++) {
179 		g_free(gpios->power_gpio.power_up_outs[i].name);
180 	}
181 	g_free(gpios->power_gpio.power_up_outs);
182 	g_free(gpios->power_gpio.power_up_pols);
183 	for(i = 0; i < gpios->power_gpio.num_reset_outs; i++) {
184 		g_free(gpios->power_gpio.reset_outs[i].name);
185 	}
186 	g_free(gpios->power_gpio.reset_outs);
187 	g_free(gpios->power_gpio.reset_pols);
188 	for(i = 0; i < gpios->power_gpio.num_pci_reset_outs; i++) {
189 		g_free(gpios->power_gpio.pci_reset_outs[i].name);
190 	}
191 	g_free(gpios->power_gpio.pci_reset_outs);
192 	g_free(gpios->power_gpio.pci_reset_pols);
193 	g_free(gpios->power_gpio.pci_reset_holds);
194 
195 	g_free(gpios->hostctl_gpio.fsi_data.name);
196 	g_free(gpios->hostctl_gpio.fsi_clk.name);
197 	g_free(gpios->hostctl_gpio.fsi_enable.name);
198 	g_free(gpios->hostctl_gpio.cronus_sel.name);
199 	for (i = 0; i < gpios->hostctl_gpio.num_optionals; ++i) {
200 		g_free(gpios->hostctl_gpio.optionals[i].name);
201 	}
202 }
203