1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*
* Greybus Firmware Core Bundle Driver.
*
* Copyright 2016 Google Inc.
* Copyright 2016 Linaro Ltd.
*
* Released under the GPLv2 only.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/firmware.h>
#include "firmware.h"
#include "greybus.h"
#include "spilib.h"
struct gb_fw_core {
struct gb_connection *download_connection;
struct gb_connection *mgmt_connection;
struct gb_connection *spi_connection;
struct gb_connection *cap_connection;
};
#ifndef SPI_CORE_SUPPORT_PM
static int fw_spi_prepare_transfer_hardware(struct device *dev)
{
return gb_pm_runtime_get_sync(to_gb_bundle(dev));
}
static void fw_spi_unprepare_transfer_hardware(struct device *dev)
{
gb_pm_runtime_put_autosuspend(to_gb_bundle(dev));
}
static struct spilib_ops __spilib_ops = {
.prepare_transfer_hardware = fw_spi_prepare_transfer_hardware,
.unprepare_transfer_hardware = fw_spi_unprepare_transfer_hardware,
};
static struct spilib_ops *spilib_ops = &__spilib_ops;
#else
static struct spilib_ops *spilib_ops = NULL;
#endif
struct gb_connection *to_fw_mgmt_connection(struct device *dev)
{
struct gb_fw_core *fw_core = dev_get_drvdata(dev);
return fw_core->mgmt_connection;
}
static int gb_fw_spi_connection_init(struct gb_connection *connection)
{
int ret;
if (!connection)
return 0;
ret = gb_connection_enable(connection);
if (ret)
return ret;
ret = gb_spilib_master_init(connection, &connection->bundle->dev,
spilib_ops);
if (ret) {
gb_connection_disable(connection);
return ret;
}
return 0;
}
static void gb_fw_spi_connection_exit(struct gb_connection *connection)
{
if (!connection)
return;
gb_spilib_master_exit(connection);
gb_connection_disable(connection);
}
static int gb_fw_core_probe(struct gb_bundle *bundle,
const struct greybus_bundle_id *id)
{
struct greybus_descriptor_cport *cport_desc;
struct gb_connection *connection;
struct gb_fw_core *fw_core;
int ret, i;
u16 cport_id;
u8 protocol_id;
fw_core = kzalloc(sizeof(*fw_core), GFP_KERNEL);
if (!fw_core)
return -ENOMEM;
/* Parse CPorts and create connections */
for (i = 0; i < bundle->num_cports; i++) {
cport_desc = &bundle->cport_desc[i];
cport_id = le16_to_cpu(cport_desc->id);
protocol_id = cport_desc->protocol_id;
switch (protocol_id) {
case GREYBUS_PROTOCOL_FW_MANAGEMENT:
/* Disallow multiple Firmware Management CPorts */
if (fw_core->mgmt_connection) {
dev_err(&bundle->dev,
"multiple management CPorts found\n");
ret = -EINVAL;
goto err_destroy_connections;
}
connection = gb_connection_create(bundle, cport_id,
gb_fw_mgmt_request_handler);
if (IS_ERR(connection)) {
ret = PTR_ERR(connection);
dev_err(&bundle->dev,
"failed to create management connection (%d)\n",
ret);
goto err_destroy_connections;
}
fw_core->mgmt_connection = connection;
break;
case GREYBUS_PROTOCOL_FW_DOWNLOAD:
/* Disallow multiple Firmware Download CPorts */
if (fw_core->download_connection) {
dev_err(&bundle->dev,
"multiple download CPorts found\n");
ret = -EINVAL;
goto err_destroy_connections;
}
connection = gb_connection_create(bundle, cport_id,
gb_fw_download_request_handler);
if (IS_ERR(connection)) {
dev_err(&bundle->dev, "failed to create download connection (%ld)\n",
PTR_ERR(connection));
} else {
fw_core->download_connection = connection;
}
break;
case GREYBUS_PROTOCOL_SPI:
/* Disallow multiple SPI CPorts */
if (fw_core->spi_connection) {
dev_err(&bundle->dev,
"multiple SPI CPorts found\n");
ret = -EINVAL;
goto err_destroy_connections;
}
connection = gb_connection_create(bundle, cport_id,
NULL);
if (IS_ERR(connection)) {
dev_err(&bundle->dev, "failed to create SPI connection (%ld)\n",
PTR_ERR(connection));
} else {
fw_core->spi_connection = connection;
}
break;
case GREYBUS_PROTOCOL_AUTHENTICATION:
/* Disallow multiple CAP CPorts */
if (fw_core->cap_connection) {
dev_err(&bundle->dev, "multiple Authentication CPorts found\n");
ret = -EINVAL;
goto err_destroy_connections;
}
connection = gb_connection_create(bundle, cport_id,
NULL);
if (IS_ERR(connection)) {
dev_err(&bundle->dev, "failed to create Authentication connection (%ld)\n",
PTR_ERR(connection));
} else {
fw_core->cap_connection = connection;
}
break;
default:
dev_err(&bundle->dev, "invalid protocol id (0x%02x)\n",
protocol_id);
ret = -EINVAL;
goto err_destroy_connections;
}
}
/* Firmware Management connection is mandatory */
if (!fw_core->mgmt_connection) {
dev_err(&bundle->dev, "missing management connection\n");
ret = -ENODEV;
goto err_destroy_connections;
}
ret = gb_fw_download_connection_init(fw_core->download_connection);
if (ret) {
/* We may still be able to work with the Interface */
dev_err(&bundle->dev, "failed to initialize firmware download connection, disable it (%d)\n",
ret);
gb_connection_destroy(fw_core->download_connection);
fw_core->download_connection = NULL;
}
ret = gb_fw_spi_connection_init(fw_core->spi_connection);
if (ret) {
/* We may still be able to work with the Interface */
dev_err(&bundle->dev, "failed to initialize SPI connection, disable it (%d)\n",
ret);
gb_connection_destroy(fw_core->spi_connection);
fw_core->spi_connection = NULL;
}
ret = gb_cap_connection_init(fw_core->cap_connection);
if (ret) {
/* We may still be able to work with the Interface */
dev_err(&bundle->dev, "failed to initialize CAP connection, disable it (%d)\n",
ret);
gb_connection_destroy(fw_core->cap_connection);
fw_core->cap_connection = NULL;
}
ret = gb_fw_mgmt_connection_init(fw_core->mgmt_connection);
if (ret) {
/* We may still be able to work with the Interface */
dev_err(&bundle->dev, "failed to initialize firmware management connection, disable it (%d)\n",
ret);
goto err_exit_connections;
}
greybus_set_drvdata(bundle, fw_core);
/* FIXME: Remove this after S2 Loader gets runtime PM support */
if (!(bundle->intf->quirks & GB_INTERFACE_QUIRK_NO_PM))
gb_pm_runtime_put_autosuspend(bundle);
return 0;
err_exit_connections:
gb_cap_connection_exit(fw_core->cap_connection);
gb_fw_spi_connection_exit(fw_core->spi_connection);
gb_fw_download_connection_exit(fw_core->download_connection);
err_destroy_connections:
gb_connection_destroy(fw_core->mgmt_connection);
gb_connection_destroy(fw_core->cap_connection);
gb_connection_destroy(fw_core->spi_connection);
gb_connection_destroy(fw_core->download_connection);
kfree(fw_core);
return ret;
}
static void gb_fw_core_disconnect(struct gb_bundle *bundle)
{
struct gb_fw_core *fw_core = greybus_get_drvdata(bundle);
int ret;
/* FIXME: Remove this after S2 Loader gets runtime PM support */
if (!(bundle->intf->quirks & GB_INTERFACE_QUIRK_NO_PM)) {
ret = gb_pm_runtime_get_sync(bundle);
if (ret)
gb_pm_runtime_get_noresume(bundle);
}
gb_fw_mgmt_connection_exit(fw_core->mgmt_connection);
gb_cap_connection_exit(fw_core->cap_connection);
gb_fw_spi_connection_exit(fw_core->spi_connection);
gb_fw_download_connection_exit(fw_core->download_connection);
gb_connection_destroy(fw_core->mgmt_connection);
gb_connection_destroy(fw_core->cap_connection);
gb_connection_destroy(fw_core->spi_connection);
gb_connection_destroy(fw_core->download_connection);
kfree(fw_core);
}
static const struct greybus_bundle_id gb_fw_core_id_table[] = {
{ GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_FW_MANAGEMENT) },
{ }
};
static struct greybus_driver gb_fw_core_driver = {
.name = "gb-firmware",
.probe = gb_fw_core_probe,
.disconnect = gb_fw_core_disconnect,
.id_table = gb_fw_core_id_table,
};
static int fw_core_init(void)
{
int ret;
ret = fw_mgmt_init();
if (ret) {
pr_err("Failed to initialize fw-mgmt core (%d)\n", ret);
return ret;
}
ret = cap_init();
if (ret) {
pr_err("Failed to initialize component authentication core (%d)\n",
ret);
goto fw_mgmt_exit;
}
ret = greybus_register(&gb_fw_core_driver);
if (ret)
goto cap_exit;
return 0;
cap_exit:
cap_exit();
fw_mgmt_exit:
fw_mgmt_exit();
return ret;
}
module_init(fw_core_init);
static void __exit fw_core_exit(void)
{
greybus_deregister(&gb_fw_core_driver);
cap_exit();
fw_mgmt_exit();
}
module_exit(fw_core_exit);
MODULE_ALIAS("greybus:firmware");
MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
MODULE_DESCRIPTION("Greybus Firmware Bundle Driver");
MODULE_LICENSE("GPL v2");
|