blob: 63c6e4829d4a55f50d13a2bd4f87d0a0a64bb396 [file] [log] [blame]
Xo Wang3f87de82016-09-22 11:17:01 -07001/**
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
Lei YU45cb4fc2016-11-29 01:48:26 +080018#include "gpio_configs.h"
Xo Wang3f87de82016-09-22 11:17:01 -070019
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <glib.h>
24
Lei YU75a18a22016-11-22 01:47:47 +080025gboolean read_gpios(GDBusConnection *connection, GpioConfigs *gpios)
Xo Wang3f87de82016-09-22 11:17:01 -070026{
27 GDBusProxy *proxy;
Lei YUd560aac2016-11-19 00:47:32 +080028 GError *error = NULL;
Xo Wang3f87de82016-09-22 11:17:01 -070029 GVariant *value;
Lei YU75a18a22016-11-22 01:47:47 +080030
Xo Wang3f87de82016-09-22 11:17:01 -070031 GVariantIter *power_up_outs_iter;
32 GVariantIter *reset_outs_iter;
Yi Li0475f652016-10-25 13:19:59 +080033 GVariantIter *pci_reset_outs_iter;
Xo Wang3f87de82016-09-22 11:17:01 -070034 gchar *power_up_out_name;
35 gchar *reset_out_name;
Yi Li0475f652016-10-25 13:19:59 +080036 gchar *pci_reset_out_name;
Xo Wang3f87de82016-09-22 11:17:01 -070037 gboolean power_up_polarity;
38 gboolean reset_out_polarity;
Yi Li0475f652016-10-25 13:19:59 +080039 gboolean pci_reset_out_polarity;
40 gboolean pci_reset_out_hold;
Lei YU75a18a22016-11-22 01:47:47 +080041
42 GVariantIter *optionals_iter;
43 gchar *optional_name;
44 gboolean optional_polarity;
Xo Wang3f87de82016-09-22 11:17:01 -070045 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,
Lei YU75a18a22016-11-22 01:47:47 +080062 "getGpioConfiguration",
Xo Wang3f87de82016-09-22 11:17:01 -070063 NULL,
64 G_DBUS_CALL_FLAGS_NONE,
65 -1,
66 NULL,
67 &error);
68 if(error != NULL) {
Lei YU75a18a22016-11-22 01:47:47 +080069 fprintf(stderr, "Power GPIO: call to getGpioConfiguration failed: %s\n",
Xo Wang3f87de82016-09-22 11:17:01 -070070 error->message);
71 g_error_free(error);
72 return FALSE;
73 }
74
75 g_assert(value != NULL);
Lei YU75a18a22016-11-22 01:47:47 +080076 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);
Xo Wang3f87de82016-09-22 11:17:01 -070084
Lei YU75a18a22016-11-22 01:47:47 +080085 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);
Xo Wangc1ce68b2016-09-22 16:34:37 -070088 }
Lei YU75a18a22016-11-22 01:47:47 +080089 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(
Xo Wang3f87de82016-09-22 11:17:01 -070095 power_up_outs_iter);
Lei YU75a18a22016-11-22 01:47:47 +080096 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,
Xo Wang3f87de82016-09-22 11:17:01 -070099 sizeof(GPIO));
Lei YU75a18a22016-11-22 01:47:47 +0800100 gpios->power_gpio.power_up_pols = g_malloc0_n(gpios->power_gpio.num_power_up_outs,
Xo Wang3f87de82016-09-22 11:17:01 -0700101 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");
Lei YU75a18a22016-11-22 01:47:47 +0800107 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;
Xo Wang3f87de82016-09-22 11:17:01 -0700109 }
Lei YU75a18a22016-11-22 01:47:47 +0800110 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,
Xo Wang3f87de82016-09-22 11:17:01 -0700114 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");
Lei YU75a18a22016-11-22 01:47:47 +0800119 gpios->power_gpio.reset_outs[i].name = g_strdup(reset_out_name);
120 gpios->power_gpio.reset_pols[i] = reset_out_polarity;
Xo Wang3f87de82016-09-22 11:17:01 -0700121 }
122
Lei YU75a18a22016-11-22 01:47:47 +0800123 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,
Yi Li0475f652016-10-25 13:19:59 +0800126 sizeof(GPIO));
Lei YU75a18a22016-11-22 01:47:47 +0800127 gpios->power_gpio.pci_reset_pols = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs,
Yi Li0475f652016-10-25 13:19:59 +0800128 sizeof(gboolean));
Lei YU75a18a22016-11-22 01:47:47 +0800129 gpios->power_gpio.pci_reset_holds = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs,
Yi Li0475f652016-10-25 13:19:59 +0800130 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");
Lei YU75a18a22016-11-22 01:47:47 +0800137 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;
Yi Li0475f652016-10-25 13:19:59 +0800163 }
164
Xo Wang3f87de82016-09-22 11:17:01 -0700165 g_variant_iter_free(power_up_outs_iter);
166 g_variant_iter_free(reset_outs_iter);
Yi Li0475f652016-10-25 13:19:59 +0800167 g_variant_iter_free(pci_reset_outs_iter);
Lei YU75a18a22016-11-22 01:47:47 +0800168 g_variant_iter_free(optionals_iter);
Xo Wang3f87de82016-09-22 11:17:01 -0700169 g_variant_unref(value);
170
171 return TRUE;
172}
173
Lei YU75a18a22016-11-22 01:47:47 +0800174void free_gpios(GpioConfigs *gpios) {
Xo Wang3f87de82016-09-22 11:17:01 -0700175 int i;
Lei YU75a18a22016-11-22 01:47:47 +0800176 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);
Xo Wang3f87de82016-09-22 11:17:01 -0700180 }
Lei YU75a18a22016-11-22 01:47:47 +0800181 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);
Xo Wang3f87de82016-09-22 11:17:01 -0700185 }
Lei YU75a18a22016-11-22 01:47:47 +0800186 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);
Yi Li0475f652016-10-25 13:19:59 +0800190 }
Lei YU75a18a22016-11-22 01:47:47 +0800191 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 }
Xo Wang3f87de82016-09-22 11:17:01 -0700202}