2 * Greybus Firmware Core Bundle Driver.
4 * Copyright 2016 Google Inc.
5 * Copyright 2016 Linaro Ltd.
7 * Released under the GPLv2 only.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/firmware.h>
17 struct gb_connection *download_connection;
18 struct gb_connection *mgmt_connection;
19 struct gb_connection *spi_connection;
20 struct gb_connection *cap_connection;
23 static struct spilib_ops *spilib_ops;
25 struct gb_connection *to_fw_mgmt_connection(struct device *dev)
27 struct gb_fw_core *fw_core = dev_get_drvdata(dev);
29 return fw_core->mgmt_connection;
32 static int gb_fw_spi_connection_init(struct gb_connection *connection)
39 ret = gb_connection_enable(connection);
43 ret = gb_spilib_master_init(connection, &connection->bundle->dev,
46 gb_connection_disable(connection);
53 static void gb_fw_spi_connection_exit(struct gb_connection *connection)
58 gb_spilib_master_exit(connection);
59 gb_connection_disable(connection);
62 static int gb_fw_core_probe(struct gb_bundle *bundle,
63 const struct greybus_bundle_id *id)
65 struct greybus_descriptor_cport *cport_desc;
66 struct gb_connection *connection;
67 struct gb_fw_core *fw_core;
72 fw_core = kzalloc(sizeof(*fw_core), GFP_KERNEL);
76 /* Parse CPorts and create connections */
77 for (i = 0; i < bundle->num_cports; i++) {
78 cport_desc = &bundle->cport_desc[i];
79 cport_id = le16_to_cpu(cport_desc->id);
80 protocol_id = cport_desc->protocol_id;
82 switch (protocol_id) {
83 case GREYBUS_PROTOCOL_FW_MANAGEMENT:
84 /* Disallow multiple Firmware Management CPorts */
85 if (fw_core->mgmt_connection) {
87 "multiple management CPorts found\n");
89 goto err_destroy_connections;
92 connection = gb_connection_create(bundle, cport_id,
93 gb_fw_mgmt_request_handler);
94 if (IS_ERR(connection)) {
95 ret = PTR_ERR(connection);
97 "failed to create management connection (%d)\n",
99 goto err_destroy_connections;
102 fw_core->mgmt_connection = connection;
104 case GREYBUS_PROTOCOL_FW_DOWNLOAD:
105 /* Disallow multiple Firmware Download CPorts */
106 if (fw_core->download_connection) {
107 dev_err(&bundle->dev,
108 "multiple download CPorts found\n");
110 goto err_destroy_connections;
113 connection = gb_connection_create(bundle, cport_id,
114 gb_fw_download_request_handler);
115 if (IS_ERR(connection)) {
116 dev_err(&bundle->dev, "failed to create download connection (%ld)\n",
117 PTR_ERR(connection));
119 fw_core->download_connection = connection;
123 case GREYBUS_PROTOCOL_SPI:
124 /* Disallow multiple SPI CPorts */
125 if (fw_core->spi_connection) {
126 dev_err(&bundle->dev,
127 "multiple SPI CPorts found\n");
129 goto err_destroy_connections;
132 connection = gb_connection_create(bundle, cport_id,
134 if (IS_ERR(connection)) {
135 dev_err(&bundle->dev, "failed to create SPI connection (%ld)\n",
136 PTR_ERR(connection));
138 fw_core->spi_connection = connection;
142 case GREYBUS_PROTOCOL_AUTHENTICATION:
143 /* Disallow multiple CAP CPorts */
144 if (fw_core->cap_connection) {
145 dev_err(&bundle->dev, "multiple Authentication CPorts found\n");
147 goto err_destroy_connections;
150 connection = gb_connection_create(bundle, cport_id,
152 if (IS_ERR(connection)) {
153 dev_err(&bundle->dev, "failed to create Authentication connection (%ld)\n",
154 PTR_ERR(connection));
156 fw_core->cap_connection = connection;
161 dev_err(&bundle->dev, "invalid protocol id (0x%02x)\n",
164 goto err_destroy_connections;
168 /* Firmware Management connection is mandatory */
169 if (!fw_core->mgmt_connection) {
170 dev_err(&bundle->dev, "missing management connection\n");
172 goto err_destroy_connections;
175 ret = gb_fw_download_connection_init(fw_core->download_connection);
177 /* We may still be able to work with the Interface */
178 dev_err(&bundle->dev, "failed to initialize firmware download connection, disable it (%d)\n",
180 gb_connection_destroy(fw_core->download_connection);
181 fw_core->download_connection = NULL;
184 ret = gb_fw_spi_connection_init(fw_core->spi_connection);
186 /* We may still be able to work with the Interface */
187 dev_err(&bundle->dev, "failed to initialize SPI connection, disable it (%d)\n",
189 gb_connection_destroy(fw_core->spi_connection);
190 fw_core->spi_connection = NULL;
193 ret = gb_cap_connection_init(fw_core->cap_connection);
195 /* We may still be able to work with the Interface */
196 dev_err(&bundle->dev, "failed to initialize CAP connection, disable it (%d)\n",
198 gb_connection_destroy(fw_core->cap_connection);
199 fw_core->cap_connection = NULL;
202 ret = gb_fw_mgmt_connection_init(fw_core->mgmt_connection);
204 /* We may still be able to work with the Interface */
205 dev_err(&bundle->dev, "failed to initialize firmware management connection, disable it (%d)\n",
207 goto err_exit_connections;
210 greybus_set_drvdata(bundle, fw_core);
212 /* FIXME: Remove this after S2 Loader gets runtime PM support */
213 if (!(bundle->intf->quirks & GB_INTERFACE_QUIRK_NO_PM))
214 gb_pm_runtime_put_autosuspend(bundle);
218 err_exit_connections:
219 gb_cap_connection_exit(fw_core->cap_connection);
220 gb_fw_spi_connection_exit(fw_core->spi_connection);
221 gb_fw_download_connection_exit(fw_core->download_connection);
222 err_destroy_connections:
223 gb_connection_destroy(fw_core->mgmt_connection);
224 gb_connection_destroy(fw_core->cap_connection);
225 gb_connection_destroy(fw_core->spi_connection);
226 gb_connection_destroy(fw_core->download_connection);
232 static void gb_fw_core_disconnect(struct gb_bundle *bundle)
234 struct gb_fw_core *fw_core = greybus_get_drvdata(bundle);
237 /* FIXME: Remove this after S2 Loader gets runtime PM support */
238 if (!(bundle->intf->quirks & GB_INTERFACE_QUIRK_NO_PM)) {
239 ret = gb_pm_runtime_get_sync(bundle);
241 gb_pm_runtime_get_noresume(bundle);
244 gb_fw_mgmt_connection_exit(fw_core->mgmt_connection);
245 gb_cap_connection_exit(fw_core->cap_connection);
246 gb_fw_spi_connection_exit(fw_core->spi_connection);
247 gb_fw_download_connection_exit(fw_core->download_connection);
249 gb_connection_destroy(fw_core->mgmt_connection);
250 gb_connection_destroy(fw_core->cap_connection);
251 gb_connection_destroy(fw_core->spi_connection);
252 gb_connection_destroy(fw_core->download_connection);
257 static const struct greybus_bundle_id gb_fw_core_id_table[] = {
258 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_FW_MANAGEMENT) },
262 static struct greybus_driver gb_fw_core_driver = {
263 .name = "gb-firmware",
264 .probe = gb_fw_core_probe,
265 .disconnect = gb_fw_core_disconnect,
266 .id_table = gb_fw_core_id_table,
269 static int fw_core_init(void)
273 ret = fw_mgmt_init();
275 pr_err("Failed to initialize fw-mgmt core (%d)\n", ret);
281 pr_err("Failed to initialize component authentication core (%d)\n",
286 ret = greybus_register(&gb_fw_core_driver);
299 module_init(fw_core_init);
301 static void __exit fw_core_exit(void)
303 greybus_deregister(&gb_fw_core_driver);
307 module_exit(fw_core_exit);
309 MODULE_ALIAS("greybus:firmware");
310 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
311 MODULE_DESCRIPTION("Greybus Firmware Bundle Driver");
312 MODULE_LICENSE("GPL v2");