Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 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 | |
Lei YU | 45cb4fc | 2016-11-29 01:48:26 +0800 | [diff] [blame] | 18 | #include "gpio_configs.h" |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <glib.h> |
| 24 | |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 25 | gboolean read_gpios(GDBusConnection *connection, GpioConfigs *gpios) |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 26 | { |
| 27 | GDBusProxy *proxy; |
Lei YU | d560aac | 2016-11-19 00:47:32 +0800 | [diff] [blame] | 28 | GError *error = NULL; |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 29 | GVariant *value; |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 30 | |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 31 | GVariantIter *power_up_outs_iter; |
| 32 | GVariantIter *reset_outs_iter; |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 33 | GVariantIter *pci_reset_outs_iter; |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 34 | gchar *power_up_out_name; |
| 35 | gchar *reset_out_name; |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 36 | gchar *pci_reset_out_name; |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 37 | gboolean power_up_polarity; |
| 38 | gboolean reset_out_polarity; |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 39 | gboolean pci_reset_out_polarity; |
| 40 | gboolean pci_reset_out_hold; |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 41 | |
| 42 | GVariantIter *optionals_iter; |
| 43 | gchar *optional_name; |
| 44 | gboolean optional_polarity; |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 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, |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 62 | "getGpioConfiguration", |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 63 | NULL, |
| 64 | G_DBUS_CALL_FLAGS_NONE, |
| 65 | -1, |
| 66 | NULL, |
| 67 | &error); |
| 68 | if(error != NULL) { |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 69 | fprintf(stderr, "Power GPIO: call to getGpioConfiguration failed: %s\n", |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 70 | error->message); |
| 71 | g_error_free(error); |
| 72 | return FALSE; |
| 73 | } |
| 74 | |
| 75 | g_assert(value != NULL); |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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); |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 84 | |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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); |
Xo Wang | c1ce68b | 2016-09-22 16:34:37 -0700 | [diff] [blame] | 88 | } |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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( |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 95 | power_up_outs_iter); |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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, |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 99 | sizeof(GPIO)); |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 100 | gpios->power_gpio.power_up_pols = g_malloc0_n(gpios->power_gpio.num_power_up_outs, |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 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"); |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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; |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 109 | } |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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, |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 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"); |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 119 | gpios->power_gpio.reset_outs[i].name = g_strdup(reset_out_name); |
| 120 | gpios->power_gpio.reset_pols[i] = reset_out_polarity; |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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, |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 126 | sizeof(GPIO)); |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 127 | gpios->power_gpio.pci_reset_pols = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs, |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 128 | sizeof(gboolean)); |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 129 | gpios->power_gpio.pci_reset_holds = g_malloc0_n(gpios->power_gpio.num_pci_reset_outs, |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 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"); |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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; |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 163 | } |
| 164 | |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 165 | g_variant_iter_free(power_up_outs_iter); |
| 166 | g_variant_iter_free(reset_outs_iter); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 167 | g_variant_iter_free(pci_reset_outs_iter); |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 168 | g_variant_iter_free(optionals_iter); |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 169 | g_variant_unref(value); |
| 170 | |
| 171 | return TRUE; |
| 172 | } |
| 173 | |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 174 | void free_gpios(GpioConfigs *gpios) { |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 175 | int i; |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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); |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 180 | } |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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); |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 185 | } |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 190 | } |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 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 | } |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 202 | } |