1 // SPDX-License-Identifier: GPL-2.0
3 * Greybus Firmware Core Bundle Driver.
5 * Copyright 2016 Google Inc.
6 * Copyright 2016 Linaro Ltd.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/firmware.h>
11 #include <linux/greybus.h>
16 struct gb_connection *download_connection;
17 struct gb_connection *mgmt_connection;
18 struct gb_connection *spi_connection;
19 struct gb_connection *cap_connection;
22 static struct spilib_ops *spilib_ops;
24 struct gb_connection *to_fw_mgmt_connection(struct device *dev)
26 struct gb_fw_core *fw_core = dev_get_drvdata(dev);
28 return fw_core->mgmt_connection;
31 static int gb_fw_spi_connection_init(struct gb_connection *connection)
38 ret = gb_connection_enable(connection);
42 ret = gb_spilib_master_init(connection, &connection->bundle->dev,
45 gb_connection_disable(connection);
52 static void gb_fw_spi_connection_exit(struct gb_connection *connection)
57 gb_spilib_master_exit(connection);
58 gb_connection_disable(connection);
61 static int gb_fw_core_probe(struct gb_bundle *bundle,
62 const struct greybus_bundle_id *id)
64 struct greybus_descriptor_cport *cport_desc;
65 struct gb_connection *connection;
66 struct gb_fw_core *fw_core;
71 fw_core = kzalloc(sizeof(*fw_core), GFP_KERNEL);
75 /* Parse CPorts and create connections */
76 for (i = 0; i < bundle->num_cports; i++) {
77 cport_desc = &bundle->cport_desc[i];
78 cport_id = le16_to_cpu(cport_desc->id);
79 protocol_id = cport_desc->protocol_id;
81 switch (protocol_id) {
82 case GREYBUS_PROTOCOL_FW_MANAGEMENT:
83 /* Disallow multiple Firmware Management CPorts */
84 if (fw_core->mgmt_connection) {
86 "multiple management CPorts found\n");
88 goto err_destroy_connections;
91 connection = gb_connection_create(bundle, cport_id,
92 gb_fw_mgmt_request_handler);
93 if (IS_ERR(connection)) {
94 ret = PTR_ERR(connection);
96 "failed to create management connection (%d)\n",
98 goto err_destroy_connections;
101 fw_core->mgmt_connection = connection;
103 case GREYBUS_PROTOCOL_FW_DOWNLOAD:
104 /* Disallow multiple Firmware Download CPorts */
105 if (fw_core->download_connection) {
106 dev_err(&bundle->dev,
107 "multiple download CPorts found\n");
109 goto err_destroy_connections;
112 connection = gb_connection_create(bundle, cport_id,
113 gb_fw_download_request_handler);
114 if (IS_ERR(connection)) {
115 dev_err(&bundle->dev, "failed to create download connection (%ld)\n",
116 PTR_ERR(connection));
118 fw_core->download_connection = connection;
122 case GREYBUS_PROTOCOL_SPI:
123 /* Disallow multiple SPI CPorts */
124 if (fw_core->spi_connection) {
125 dev_err(&bundle->dev,
126 "multiple SPI CPorts found\n");
128 goto err_destroy_connections;
131 connection = gb_connection_create(bundle, cport_id,
133 if (IS_ERR(connection)) {
134 dev_err(&bundle->dev, "failed to create SPI connection (%ld)\n",
135 PTR_ERR(connection));
137 fw_core->spi_connection = connection;
141 case GREYBUS_PROTOCOL_AUTHENTICATION:
142 /* Disallow multiple CAP CPorts */
143 if (fw_core->cap_connection) {
144 dev_err(&bundle->dev, "multiple Authentication CPorts found\n");
146 goto err_destroy_connections;
149 connection = gb_connection_create(bundle, cport_id,
151 if (IS_ERR(connection)) {
152 dev_err(&bundle->dev, "failed to create Authentication connection (%ld)\n",
153 PTR_ERR(connection));
155 fw_core->cap_connection = connection;
160 dev_err(&bundle->dev, "invalid protocol id (0x%02x)\n",
163 goto err_destroy_connections;
167 /* Firmware Management connection is mandatory */
168 if (!fw_core->mgmt_connection) {
169 dev_err(&bundle->dev, "missing management connection\n");
171 goto err_destroy_connections;
174 ret = gb_fw_download_connection_init(fw_core->download_connection);
176 /* We may still be able to work with the Interface */
177 dev_err(&bundle->dev, "failed to initialize firmware download connection, disable it (%d)\n",
179 gb_connection_destroy(fw_core->download_connection);
180 fw_core->download_connection = NULL;
183 ret = gb_fw_spi_connection_init(fw_core->spi_connection);
185 /* We may still be able to work with the Interface */
186 dev_err(&bundle->dev, "failed to initialize SPI connection, disable it (%d)\n",
188 gb_connection_destroy(fw_core->spi_connection);
189 fw_core->spi_connection = NULL;
192 ret = gb_cap_connection_init(fw_core->cap_connection);
194 /* We may still be able to work with the Interface */
195 dev_err(&bundle->dev, "failed to initialize CAP connection, disable it (%d)\n",
197 gb_connection_destroy(fw_core->cap_connection);
198 fw_core->cap_connection = NULL;
201 ret = gb_fw_mgmt_connection_init(fw_core->mgmt_connection);
203 /* We may still be able to work with the Interface */
204 dev_err(&bundle->dev, "failed to initialize firmware management connection, disable it (%d)\n",
206 goto err_exit_connections;
209 greybus_set_drvdata(bundle, fw_core);
211 /* FIXME: Remove this after S2 Loader gets runtime PM support */
212 if (!(bundle->intf->quirks & GB_INTERFACE_QUIRK_NO_PM))
213 gb_pm_runtime_put_autosuspend(bundle);
217 err_exit_connections:
218 gb_cap_connection_exit(fw_core->cap_connection);
219 gb_fw_spi_connection_exit(fw_core->spi_connection);
220 gb_fw_download_connection_exit(fw_core->download_connection);
221 err_destroy_connections:
222 gb_connection_destroy(fw_core->mgmt_connection);
223 gb_connection_destroy(fw_core->cap_connection);
224 gb_connection_destroy(fw_core->spi_connection);
225 gb_connection_destroy(fw_core->download_connection);
231 static void gb_fw_core_disconnect(struct gb_bundle *bundle)
233 struct gb_fw_core *fw_core = greybus_get_drvdata(bundle);
236 /* FIXME: Remove this after S2 Loader gets runtime PM support */
237 if (!(bundle->intf->quirks & GB_INTERFACE_QUIRK_NO_PM)) {
238 ret = gb_pm_runtime_get_sync(bundle);
240 gb_pm_runtime_get_noresume(bundle);
243 gb_fw_mgmt_connection_exit(fw_core->mgmt_connection);
244 gb_cap_connection_exit(fw_core->cap_connection);
245 gb_fw_spi_connection_exit(fw_core->spi_connection);
246 gb_fw_download_connection_exit(fw_core->download_connection);
248 gb_connection_destroy(fw_core->mgmt_connection);
249 gb_connection_destroy(fw_core->cap_connection);
250 gb_connection_destroy(fw_core->spi_connection);
251 gb_connection_destroy(fw_core->download_connection);
256 static const struct greybus_bundle_id gb_fw_core_id_table[] = {
257 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_FW_MANAGEMENT) },
261 static struct greybus_driver gb_fw_core_driver = {
262 .name = "gb-firmware",
263 .probe = gb_fw_core_probe,
264 .disconnect = gb_fw_core_disconnect,
265 .id_table = gb_fw_core_id_table,
268 static int fw_core_init(void)
272 ret = fw_mgmt_init();
274 pr_err("Failed to initialize fw-mgmt core (%d)\n", ret);
280 pr_err("Failed to initialize component authentication core (%d)\n",
285 ret = greybus_register(&gb_fw_core_driver);
298 module_init(fw_core_init);
300 static void __exit fw_core_exit(void)
302 greybus_deregister(&gb_fw_core_driver);
306 module_exit(fw_core_exit);
308 MODULE_ALIAS("greybus:firmware");
309 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
310 MODULE_DESCRIPTION("Greybus Firmware Bundle Driver");
311 MODULE_LICENSE("GPL v2");