blob: e85ffae85dffe7186835cf7eaa324a61d361bd74 [file] [log] [blame]
Greg Kroah-Hartmaneb50fd32017-11-07 14:58:41 +01001// SPDX-License-Identifier: GPL-2.0
Viresh Kumar90f1b612015-08-12 09:19:33 +05302/*
Viresh Kumar5a53e022016-04-03 12:18:35 +05303 * BOOTROM Greybus driver.
Viresh Kumar90f1b612015-08-12 09:19:33 +05304 *
Viresh Kumar5a53e022016-04-03 12:18:35 +05305 * Copyright 2016 Google Inc.
6 * Copyright 2016 Linaro Ltd.
Viresh Kumar90f1b612015-08-12 09:19:33 +05307 */
8
9#include <linux/firmware.h>
Viresh Kumara956d932016-05-09 10:59:00 +053010#include <linux/jiffies.h>
11#include <linux/mutex.h>
12#include <linux/workqueue.h>
Viresh Kumar90f1b612015-08-12 09:19:33 +053013
14#include "greybus.h"
Greg Kroah-Hartman8a704562016-07-20 10:11:37 -070015#include "firmware.h"
Viresh Kumar90f1b612015-08-12 09:19:33 +053016
Viresh Kumara956d932016-05-09 10:59:00 +053017/* Timeout, in jiffies, within which the next request must be received */
Viresh Kumardbb8cfe2016-06-22 14:46:40 +053018#define NEXT_REQ_TIMEOUT_MS 1000
Johan Hovold8ec589b2016-01-29 15:42:31 +010019
Viresh Kumar326f98a2016-06-22 14:46:41 +053020/*
21 * FIXME: Reduce this timeout once svc core handles parallel processing of
22 * events from the SVC, which are handled sequentially today.
23 */
24#define MODE_SWITCH_TIMEOUT_MS 10000
25
Viresh Kumara4293e12016-06-22 14:46:39 +053026enum next_request_type {
27 NEXT_REQ_FIRMWARE_SIZE,
28 NEXT_REQ_GET_FIRMWARE,
29 NEXT_REQ_READY_TO_BOOT,
30 NEXT_REQ_MODE_SWITCH,
31};
32
Viresh Kumar5a53e022016-04-03 12:18:35 +053033struct gb_bootrom {
Viresh Kumar90f1b612015-08-12 09:19:33 +053034 struct gb_connection *connection;
35 const struct firmware *fw;
Johan Hovold8ec589b2016-01-29 15:42:31 +010036 u8 protocol_major;
37 u8 protocol_minor;
Viresh Kumara4293e12016-06-22 14:46:39 +053038 enum next_request_type next_request;
Viresh Kumara956d932016-05-09 10:59:00 +053039 struct delayed_work dwork;
40 struct mutex mutex; /* Protects bootrom->fw */
Viresh Kumar90f1b612015-08-12 09:19:33 +053041};
42
Viresh Kumar5a53e022016-04-03 12:18:35 +053043static void free_firmware(struct gb_bootrom *bootrom)
Viresh Kumar90f1b612015-08-12 09:19:33 +053044{
Viresh Kumara956d932016-05-09 10:59:00 +053045 if (!bootrom->fw)
46 return;
47
Viresh Kumar5a53e022016-04-03 12:18:35 +053048 release_firmware(bootrom->fw);
49 bootrom->fw = NULL;
Viresh Kumar90f1b612015-08-12 09:19:33 +053050}
51
Viresh Kumara956d932016-05-09 10:59:00 +053052static void gb_bootrom_timedout(struct work_struct *work)
53{
54 struct delayed_work *dwork = to_delayed_work(work);
Gioh Kim461ab802017-02-09 17:30:11 +010055 struct gb_bootrom *bootrom = container_of(dwork,
56 struct gb_bootrom, dwork);
Viresh Kumara956d932016-05-09 10:59:00 +053057 struct device *dev = &bootrom->connection->bundle->dev;
Viresh Kumara4293e12016-06-22 14:46:39 +053058 const char *reason;
Viresh Kumara956d932016-05-09 10:59:00 +053059
Viresh Kumara4293e12016-06-22 14:46:39 +053060 switch (bootrom->next_request) {
61 case NEXT_REQ_FIRMWARE_SIZE:
62 reason = "Firmware Size Request";
63 break;
64 case NEXT_REQ_GET_FIRMWARE:
65 reason = "Get Firmware Request";
66 break;
67 case NEXT_REQ_READY_TO_BOOT:
68 reason = "Ready to Boot Request";
69 break;
70 case NEXT_REQ_MODE_SWITCH:
71 reason = "Interface Mode Switch";
72 break;
73 default:
74 reason = NULL;
75 dev_err(dev, "Invalid next-request: %u", bootrom->next_request);
76 break;
77 }
78
79 dev_err(dev, "Timed out waiting for %s from the Module\n", reason);
Viresh Kumara956d932016-05-09 10:59:00 +053080
81 mutex_lock(&bootrom->mutex);
82 free_firmware(bootrom);
83 mutex_unlock(&bootrom->mutex);
84
85 /* TODO: Power-off Module ? */
86}
87
Viresh Kumara4293e12016-06-22 14:46:39 +053088static void gb_bootrom_set_timeout(struct gb_bootrom *bootrom,
89 enum next_request_type next, unsigned long timeout)
90{
91 bootrom->next_request = next;
Viresh Kumardbb8cfe2016-06-22 14:46:40 +053092 schedule_delayed_work(&bootrom->dwork, msecs_to_jiffies(timeout));
Viresh Kumara4293e12016-06-22 14:46:39 +053093}
94
Viresh Kumar40d276e2016-07-22 14:13:43 -070095static void gb_bootrom_cancel_timeout(struct gb_bootrom *bootrom)
96{
97 cancel_delayed_work_sync(&bootrom->dwork);
98}
99
Viresh Kumarf1e941a2015-11-26 15:33:46 +0530100/*
101 * The es2 chip doesn't have VID/PID programmed into the hardware and we need to
102 * hack that up to distinguish different modules and their firmware blobs.
103 *
Viresh Kumar5a53e022016-04-03 12:18:35 +0530104 * This fetches VID/PID (over bootrom protocol) for es2 chip only, when VID/PID
Viresh Kumarf1e941a2015-11-26 15:33:46 +0530105 * already sent during hotplug are 0.
106 *
Viresh Kumarf3e6c092016-01-22 16:16:08 +0530107 * Otherwise, we keep intf->vendor_id/product_id same as what's passed
Viresh Kumarf1e941a2015-11-26 15:33:46 +0530108 * during hotplug.
109 */
Viresh Kumar5a53e022016-04-03 12:18:35 +0530110static void bootrom_es2_fixup_vid_pid(struct gb_bootrom *bootrom)
Viresh Kumarf1e941a2015-11-26 15:33:46 +0530111{
Viresh Kumar5a53e022016-04-03 12:18:35 +0530112 struct gb_bootrom_get_vid_pid_response response;
113 struct gb_connection *connection = bootrom->connection;
Viresh Kumarf1e941a2015-11-26 15:33:46 +0530114 struct gb_interface *intf = connection->bundle->intf;
115 int ret;
116
Johan Hovold23931ff2016-07-28 11:40:53 +0200117 if (!(intf->quirks & GB_INTERFACE_QUIRK_NO_GMP_IDS))
Viresh Kumarf1e941a2015-11-26 15:33:46 +0530118 return;
119
Viresh Kumar5a53e022016-04-03 12:18:35 +0530120 ret = gb_operation_sync(connection, GB_BOOTROM_TYPE_GET_VID_PID,
Viresh Kumarf1e941a2015-11-26 15:33:46 +0530121 NULL, 0, &response, sizeof(response));
122 if (ret) {
123 dev_err(&connection->bundle->dev,
Viresh Kumar5a53e022016-04-03 12:18:35 +0530124 "Bootrom get vid/pid operation failed (%d)\n", ret);
Viresh Kumarf1e941a2015-11-26 15:33:46 +0530125 return;
126 }
127
Viresh Kumarf3e6c092016-01-22 16:16:08 +0530128 /*
129 * NOTE: This is hacked, so that the same values of VID/PID can be used
130 * by next firmware level as well. The uevent for bootrom will still
131 * have VID/PID as 0, though after this point the sysfs files will start
132 * showing the updated values. But yeah, that's a bit racy as the same
133 * sysfs files would be showing 0 before this point.
134 */
135 intf->vendor_id = le32_to_cpu(response.vendor_id);
136 intf->product_id = le32_to_cpu(response.product_id);
Eli Senneshfc41c2d2016-01-08 14:11:29 -0500137
Viresh Kumar5a53e022016-04-03 12:18:35 +0530138 dev_dbg(&connection->bundle->dev, "Bootrom got vid (0x%x)/pid (0x%x)\n",
Viresh Kumarf3e6c092016-01-22 16:16:08 +0530139 intf->vendor_id, intf->product_id);
Viresh Kumarf1e941a2015-11-26 15:33:46 +0530140}
141
Viresh Kumar90f1b612015-08-12 09:19:33 +0530142/* This returns path of the firmware blob on the disk */
Viresh Kumar68793c42016-07-22 14:13:44 -0700143static int find_firmware(struct gb_bootrom *bootrom, u8 stage)
Viresh Kumar90f1b612015-08-12 09:19:33 +0530144{
Viresh Kumar5a53e022016-04-03 12:18:35 +0530145 struct gb_connection *connection = bootrom->connection;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530146 struct gb_interface *intf = connection->bundle->intf;
Viresh Kumar56c78712016-07-16 09:57:30 -0700147 char firmware_name[49];
Eli Senneshfc41c2d2016-01-08 14:11:29 -0500148 int rc;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530149
150 /* Already have a firmware, free it */
Viresh Kumara956d932016-05-09 10:59:00 +0530151 free_firmware(bootrom);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530152
Viresh Kumar56c78712016-07-16 09:57:30 -0700153 /* Bootrom protocol is only supported for loading Stage 2 firmware */
154 if (stage != 2) {
155 dev_err(&connection->bundle->dev, "Invalid boot stage: %u\n",
156 stage);
157 return -EINVAL;
158 }
159
Viresh Kumar90f1b612015-08-12 09:19:33 +0530160 /*
161 * Create firmware name
162 *
163 * XXX Name it properly..
164 */
Johan Hovoldaf0b4d52015-08-28 11:58:24 +0200165 snprintf(firmware_name, sizeof(firmware_name),
Greg Kroah-Hartman8a704562016-07-20 10:11:37 -0700166 FW_NAME_PREFIX "%08x_%08x_%08x_%08x_s2l.tftf",
Viresh Kumarb32a5c52015-12-22 22:04:33 +0530167 intf->ddbl1_manufacturer_id, intf->ddbl1_product_id,
Viresh Kumar56c78712016-07-16 09:57:30 -0700168 intf->vendor_id, intf->product_id);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530169
Greg Kroah-Hartmaneb8fafd2016-01-20 09:27:05 -0800170 // FIXME:
171 // Turn to dev_dbg later after everyone has valid bootloaders with good
172 // ids, but leave this as dev_info for now to make it easier to track
173 // down "empty" vid/pid modules.
174 dev_info(&connection->bundle->dev, "Firmware file '%s' requested\n",
175 firmware_name);
176
Viresh Kumar5a53e022016-04-03 12:18:35 +0530177 rc = request_firmware(&bootrom->fw, firmware_name,
Eli Senneshfc41c2d2016-01-08 14:11:29 -0500178 &connection->bundle->dev);
Viresh Kumar2d6f1c22016-07-22 14:13:41 -0700179 if (rc) {
Viresh Kumar68793c42016-07-22 14:13:44 -0700180 dev_err(&connection->bundle->dev,
181 "failed to find %s firmware (%d)\n", firmware_name, rc);
Viresh Kumar2d6f1c22016-07-22 14:13:41 -0700182 }
183
Eli Senneshfc41c2d2016-01-08 14:11:29 -0500184 return rc;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530185}
186
Viresh Kumar5a53e022016-04-03 12:18:35 +0530187static int gb_bootrom_firmware_size_request(struct gb_operation *op)
Viresh Kumar90f1b612015-08-12 09:19:33 +0530188{
Viresh Kumar5a53e022016-04-03 12:18:35 +0530189 struct gb_bootrom *bootrom = gb_connection_get_data(op->connection);
Gioh Kim461ab802017-02-09 17:30:11 +0100190 struct gb_bootrom_firmware_size_request *size_request =
191 op->request->payload;
Viresh Kumar5a53e022016-04-03 12:18:35 +0530192 struct gb_bootrom_firmware_size_response *size_response;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400193 struct device *dev = &op->connection->bundle->dev;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530194 int ret;
195
Viresh Kumara956d932016-05-09 10:59:00 +0530196 /* Disable timeouts */
Viresh Kumar40d276e2016-07-22 14:13:43 -0700197 gb_bootrom_cancel_timeout(bootrom);
Viresh Kumara956d932016-05-09 10:59:00 +0530198
Viresh Kumar90f1b612015-08-12 09:19:33 +0530199 if (op->request->payload_size != sizeof(*size_request)) {
200 dev_err(dev, "%s: illegal size of firmware size request (%zu != %zu)\n",
201 __func__, op->request->payload_size,
202 sizeof(*size_request));
Viresh Kumara956d932016-05-09 10:59:00 +0530203 ret = -EINVAL;
204 goto queue_work;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530205 }
206
Viresh Kumara956d932016-05-09 10:59:00 +0530207 mutex_lock(&bootrom->mutex);
208
Viresh Kumar68793c42016-07-22 14:13:44 -0700209 ret = find_firmware(bootrom, size_request->stage);
Viresh Kumar2d6f1c22016-07-22 14:13:41 -0700210 if (ret)
Viresh Kumara956d932016-05-09 10:59:00 +0530211 goto unlock;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530212
213 if (!gb_operation_response_alloc(op, sizeof(*size_response),
214 GFP_KERNEL)) {
215 dev_err(dev, "%s: error allocating response\n", __func__);
Viresh Kumar5a53e022016-04-03 12:18:35 +0530216 free_firmware(bootrom);
Viresh Kumara956d932016-05-09 10:59:00 +0530217 ret = -ENOMEM;
218 goto unlock;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530219 }
220
221 size_response = op->response->payload;
Viresh Kumar5a53e022016-04-03 12:18:35 +0530222 size_response->size = cpu_to_le32(bootrom->fw->size);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530223
Gioh Kim461ab802017-02-09 17:30:11 +0100224 dev_dbg(dev, "%s: firmware size %d bytes\n",
225 __func__, size_response->size);
Eli Senneshfc41c2d2016-01-08 14:11:29 -0500226
Viresh Kumara956d932016-05-09 10:59:00 +0530227unlock:
228 mutex_unlock(&bootrom->mutex);
229
230queue_work:
Viresh Kumarcc90d6f2016-07-22 14:13:40 -0700231 if (!ret) {
232 /* Refresh timeout */
233 gb_bootrom_set_timeout(bootrom, NEXT_REQ_GET_FIRMWARE,
234 NEXT_REQ_TIMEOUT_MS);
235 }
Viresh Kumara956d932016-05-09 10:59:00 +0530236
237 return ret;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530238}
239
Viresh Kumar5a53e022016-04-03 12:18:35 +0530240static int gb_bootrom_get_firmware(struct gb_operation *op)
Viresh Kumar90f1b612015-08-12 09:19:33 +0530241{
Viresh Kumar5a53e022016-04-03 12:18:35 +0530242 struct gb_bootrom *bootrom = gb_connection_get_data(op->connection);
Viresh Kumara956d932016-05-09 10:59:00 +0530243 const struct firmware *fw;
Viresh Kumar5a53e022016-04-03 12:18:35 +0530244 struct gb_bootrom_get_firmware_request *firmware_request;
245 struct gb_bootrom_get_firmware_response *firmware_response;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400246 struct device *dev = &op->connection->bundle->dev;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530247 unsigned int offset, size;
Viresh Kumara4293e12016-06-22 14:46:39 +0530248 enum next_request_type next_request;
Viresh Kumara956d932016-05-09 10:59:00 +0530249 int ret = 0;
250
251 /* Disable timeouts */
Viresh Kumar40d276e2016-07-22 14:13:43 -0700252 gb_bootrom_cancel_timeout(bootrom);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530253
254 if (op->request->payload_size != sizeof(*firmware_request)) {
255 dev_err(dev, "%s: Illegal size of get firmware request (%zu %zu)\n",
256 __func__, op->request->payload_size,
257 sizeof(*firmware_request));
Viresh Kumara956d932016-05-09 10:59:00 +0530258 ret = -EINVAL;
259 goto queue_work;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530260 }
261
Viresh Kumara956d932016-05-09 10:59:00 +0530262 mutex_lock(&bootrom->mutex);
263
264 fw = bootrom->fw;
Johan Hovold98645a92015-11-19 18:27:59 +0100265 if (!fw) {
Viresh Kumar90f1b612015-08-12 09:19:33 +0530266 dev_err(dev, "%s: firmware not available\n", __func__);
Viresh Kumara956d932016-05-09 10:59:00 +0530267 ret = -EINVAL;
268 goto unlock;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530269 }
270
Johan Hovold87f6c972015-11-19 18:28:00 +0100271 firmware_request = op->request->payload;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530272 offset = le32_to_cpu(firmware_request->offset);
273 size = le32_to_cpu(firmware_request->size);
274
Johan Hovold98645a92015-11-19 18:27:59 +0100275 if (offset >= fw->size || size > fw->size - offset) {
276 dev_warn(dev, "bad firmware request (offs = %u, size = %u)\n",
277 offset, size);
Viresh Kumara956d932016-05-09 10:59:00 +0530278 ret = -EINVAL;
279 goto unlock;
Johan Hovold98645a92015-11-19 18:27:59 +0100280 }
281
Viresh Kumar90f1b612015-08-12 09:19:33 +0530282 if (!gb_operation_response_alloc(op, sizeof(*firmware_response) + size,
283 GFP_KERNEL)) {
284 dev_err(dev, "%s: error allocating response\n", __func__);
Viresh Kumara956d932016-05-09 10:59:00 +0530285 ret = -ENOMEM;
286 goto unlock;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530287 }
288
289 firmware_response = op->response->payload;
Johan Hovold98645a92015-11-19 18:27:59 +0100290 memcpy(firmware_response->data, fw->data + offset, size);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530291
Gioh Kim461ab802017-02-09 17:30:11 +0100292 dev_dbg(dev, "responding with firmware (offs = %u, size = %u)\n",
293 offset, size);
Eli Senneshfc41c2d2016-01-08 14:11:29 -0500294
Viresh Kumara956d932016-05-09 10:59:00 +0530295unlock:
296 mutex_unlock(&bootrom->mutex);
297
298queue_work:
299 /* Refresh timeout */
Viresh Kumara4293e12016-06-22 14:46:39 +0530300 if (!ret && (offset + size == fw->size))
301 next_request = NEXT_REQ_READY_TO_BOOT;
302 else
303 next_request = NEXT_REQ_GET_FIRMWARE;
304
Viresh Kumardbb8cfe2016-06-22 14:46:40 +0530305 gb_bootrom_set_timeout(bootrom, next_request, NEXT_REQ_TIMEOUT_MS);
Viresh Kumara956d932016-05-09 10:59:00 +0530306
307 return ret;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530308}
309
Viresh Kumar5a53e022016-04-03 12:18:35 +0530310static int gb_bootrom_ready_to_boot(struct gb_operation *op)
Viresh Kumar90f1b612015-08-12 09:19:33 +0530311{
312 struct gb_connection *connection = op->connection;
Viresh Kumara956d932016-05-09 10:59:00 +0530313 struct gb_bootrom *bootrom = gb_connection_get_data(connection);
Viresh Kumar5a53e022016-04-03 12:18:35 +0530314 struct gb_bootrom_ready_to_boot_request *rtb_request;
Greg Kroah-Hartman0a72bd32015-10-14 11:17:29 -0700315 struct device *dev = &connection->bundle->dev;
Viresh Kumar06986a22015-09-23 16:48:12 -0700316 u8 status;
Viresh Kumara956d932016-05-09 10:59:00 +0530317 int ret = 0;
318
319 /* Disable timeouts */
Viresh Kumar40d276e2016-07-22 14:13:43 -0700320 gb_bootrom_cancel_timeout(bootrom);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530321
322 if (op->request->payload_size != sizeof(*rtb_request)) {
323 dev_err(dev, "%s: Illegal size of ready to boot request (%zu %zu)\n",
324 __func__, op->request->payload_size,
325 sizeof(*rtb_request));
Viresh Kumara956d932016-05-09 10:59:00 +0530326 ret = -EINVAL;
327 goto queue_work;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530328 }
329
Johan Hovold87f6c972015-11-19 18:28:00 +0100330 rtb_request = op->request->payload;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530331 status = rtb_request->status;
332
333 /* Return error if the blob was invalid */
Viresh Kumara956d932016-05-09 10:59:00 +0530334 if (status == GB_BOOTROM_BOOT_STATUS_INVALID) {
335 ret = -EINVAL;
336 goto queue_work;
337 }
Viresh Kumar90f1b612015-08-12 09:19:33 +0530338
339 /*
340 * XXX Should we return error for insecure firmware?
341 */
Eli Senneshfc41c2d2016-01-08 14:11:29 -0500342 dev_dbg(dev, "ready to boot: 0x%x, 0\n", status);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530343
Viresh Kumara956d932016-05-09 10:59:00 +0530344queue_work:
345 /*
346 * Refresh timeout, the Interface shall load the new personality and
347 * send a new hotplug request, which shall get rid of the bootrom
348 * connection. As that can take some time, increase the timeout a bit.
349 */
Viresh Kumara4293e12016-06-22 14:46:39 +0530350 gb_bootrom_set_timeout(bootrom, NEXT_REQ_MODE_SWITCH,
Viresh Kumar326f98a2016-06-22 14:46:41 +0530351 MODE_SWITCH_TIMEOUT_MS);
Viresh Kumara956d932016-05-09 10:59:00 +0530352
353 return ret;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530354}
355
Viresh Kumar5a53e022016-04-03 12:18:35 +0530356static int gb_bootrom_request_handler(struct gb_operation *op)
Viresh Kumar90f1b612015-08-12 09:19:33 +0530357{
Johan Hovold8ec589b2016-01-29 15:42:31 +0100358 u8 type = op->type;
359
Viresh Kumar90f1b612015-08-12 09:19:33 +0530360 switch (type) {
Viresh Kumar5a53e022016-04-03 12:18:35 +0530361 case GB_BOOTROM_TYPE_FIRMWARE_SIZE:
362 return gb_bootrom_firmware_size_request(op);
363 case GB_BOOTROM_TYPE_GET_FIRMWARE:
364 return gb_bootrom_get_firmware(op);
365 case GB_BOOTROM_TYPE_READY_TO_BOOT:
366 return gb_bootrom_ready_to_boot(op);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530367 default:
Greg Kroah-Hartman0a72bd32015-10-14 11:17:29 -0700368 dev_err(&op->connection->bundle->dev,
Viresh Kumar2f3db922015-12-04 21:30:09 +0530369 "unsupported request: %u\n", type);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530370 return -EINVAL;
371 }
372}
373
Viresh Kumar5a53e022016-04-03 12:18:35 +0530374static int gb_bootrom_get_version(struct gb_bootrom *bootrom)
Viresh Kumar90f1b612015-08-12 09:19:33 +0530375{
Viresh Kumar5a53e022016-04-03 12:18:35 +0530376 struct gb_bundle *bundle = bootrom->connection->bundle;
377 struct gb_bootrom_version_request request;
378 struct gb_bootrom_version_response response;
Johan Hovold8ec589b2016-01-29 15:42:31 +0100379 int ret;
380
Viresh Kumar5a53e022016-04-03 12:18:35 +0530381 request.major = GB_BOOTROM_VERSION_MAJOR;
382 request.minor = GB_BOOTROM_VERSION_MINOR;
Johan Hovold8ec589b2016-01-29 15:42:31 +0100383
Viresh Kumar5a53e022016-04-03 12:18:35 +0530384 ret = gb_operation_sync(bootrom->connection,
385 GB_BOOTROM_TYPE_VERSION,
Johan Hovold8ec589b2016-01-29 15:42:31 +0100386 &request, sizeof(request), &response,
387 sizeof(response));
388 if (ret) {
389 dev_err(&bundle->dev,
390 "failed to get protocol version: %d\n",
391 ret);
392 return ret;
393 }
394
395 if (response.major > request.major) {
396 dev_err(&bundle->dev,
397 "unsupported major protocol version (%u > %u)\n",
398 response.major, request.major);
399 return -ENOTSUPP;
400 }
401
Viresh Kumar5a53e022016-04-03 12:18:35 +0530402 bootrom->protocol_major = response.major;
403 bootrom->protocol_minor = response.minor;
Johan Hovold8ec589b2016-01-29 15:42:31 +0100404
405 dev_dbg(&bundle->dev, "%s - %u.%u\n", __func__, response.major,
406 response.minor);
407
408 return 0;
409}
410
Viresh Kumar5a53e022016-04-03 12:18:35 +0530411static int gb_bootrom_probe(struct gb_bundle *bundle,
Johan Hovold8ec589b2016-01-29 15:42:31 +0100412 const struct greybus_bundle_id *id)
413{
414 struct greybus_descriptor_cport *cport_desc;
415 struct gb_connection *connection;
Viresh Kumar5a53e022016-04-03 12:18:35 +0530416 struct gb_bootrom *bootrom;
Viresh Kumar4c9e2282015-09-09 21:08:33 +0530417 int ret;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530418
Johan Hovold8ec589b2016-01-29 15:42:31 +0100419 if (bundle->num_cports != 1)
420 return -ENODEV;
421
422 cport_desc = &bundle->cport_desc[0];
Viresh Kumar5a53e022016-04-03 12:18:35 +0530423 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_BOOTROM)
Johan Hovold8ec589b2016-01-29 15:42:31 +0100424 return -ENODEV;
425
Viresh Kumar5a53e022016-04-03 12:18:35 +0530426 bootrom = kzalloc(sizeof(*bootrom), GFP_KERNEL);
427 if (!bootrom)
Viresh Kumar90f1b612015-08-12 09:19:33 +0530428 return -ENOMEM;
429
Johan Hovold8ec589b2016-01-29 15:42:31 +0100430 connection = gb_connection_create(bundle,
431 le16_to_cpu(cport_desc->id),
Viresh Kumar5a53e022016-04-03 12:18:35 +0530432 gb_bootrom_request_handler);
Johan Hovold8ec589b2016-01-29 15:42:31 +0100433 if (IS_ERR(connection)) {
434 ret = PTR_ERR(connection);
Viresh Kumar5a53e022016-04-03 12:18:35 +0530435 goto err_free_bootrom;
Johan Hovold8ec589b2016-01-29 15:42:31 +0100436 }
437
Viresh Kumar5a53e022016-04-03 12:18:35 +0530438 gb_connection_set_data(connection, bootrom);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530439
Viresh Kumar5a53e022016-04-03 12:18:35 +0530440 bootrom->connection = connection;
Johan Hovold8ec589b2016-01-29 15:42:31 +0100441
Viresh Kumara956d932016-05-09 10:59:00 +0530442 mutex_init(&bootrom->mutex);
443 INIT_DELAYED_WORK(&bootrom->dwork, gb_bootrom_timedout);
Viresh Kumar5a53e022016-04-03 12:18:35 +0530444 greybus_set_drvdata(bundle, bootrom);
Johan Hovold8ec589b2016-01-29 15:42:31 +0100445
446 ret = gb_connection_enable_tx(connection);
447 if (ret)
448 goto err_connection_destroy;
449
Viresh Kumar5a53e022016-04-03 12:18:35 +0530450 ret = gb_bootrom_get_version(bootrom);
Johan Hovold8ec589b2016-01-29 15:42:31 +0100451 if (ret)
452 goto err_connection_disable;
453
Viresh Kumar5a53e022016-04-03 12:18:35 +0530454 bootrom_es2_fixup_vid_pid(bootrom);
Viresh Kumarf1e941a2015-11-26 15:33:46 +0530455
Johan Hovold8ec589b2016-01-29 15:42:31 +0100456 ret = gb_connection_enable(connection);
457 if (ret)
458 goto err_connection_disable;
459
Viresh Kumar6d9e6ff2016-07-22 14:13:42 -0700460 /* Refresh timeout */
461 gb_bootrom_set_timeout(bootrom, NEXT_REQ_FIRMWARE_SIZE,
462 NEXT_REQ_TIMEOUT_MS);
463
Johan Hovold8eff5102016-01-21 17:34:26 +0100464 /* Tell bootrom we're ready. */
Viresh Kumar5a53e022016-04-03 12:18:35 +0530465 ret = gb_operation_sync(connection, GB_BOOTROM_TYPE_AP_READY, NULL, 0,
Viresh Kumar4c9e2282015-09-09 21:08:33 +0530466 NULL, 0);
Johan Hovold87f6c972015-11-19 18:28:00 +0100467 if (ret) {
468 dev_err(&connection->bundle->dev,
469 "failed to send AP READY: %d\n", ret);
Viresh Kumar6d9e6ff2016-07-22 14:13:42 -0700470 goto err_cancel_timeout;
Johan Hovold87f6c972015-11-19 18:28:00 +0100471 }
Viresh Kumar4c9e2282015-09-09 21:08:33 +0530472
Johan Hovold8ec589b2016-01-29 15:42:31 +0100473 dev_dbg(&bundle->dev, "AP_READY sent\n");
Eli Senneshfc41c2d2016-01-08 14:11:29 -0500474
Viresh Kumar90f1b612015-08-12 09:19:33 +0530475 return 0;
Johan Hovold8eff5102016-01-21 17:34:26 +0100476
Viresh Kumar6d9e6ff2016-07-22 14:13:42 -0700477err_cancel_timeout:
Viresh Kumar40d276e2016-07-22 14:13:43 -0700478 gb_bootrom_cancel_timeout(bootrom);
Johan Hovold8ec589b2016-01-29 15:42:31 +0100479err_connection_disable:
480 gb_connection_disable(connection);
481err_connection_destroy:
482 gb_connection_destroy(connection);
Viresh Kumar5a53e022016-04-03 12:18:35 +0530483err_free_bootrom:
484 kfree(bootrom);
Johan Hovold8eff5102016-01-21 17:34:26 +0100485
486 return ret;
Viresh Kumar90f1b612015-08-12 09:19:33 +0530487}
488
Viresh Kumar5a53e022016-04-03 12:18:35 +0530489static void gb_bootrom_disconnect(struct gb_bundle *bundle)
Viresh Kumar90f1b612015-08-12 09:19:33 +0530490{
Viresh Kumar5a53e022016-04-03 12:18:35 +0530491 struct gb_bootrom *bootrom = greybus_get_drvdata(bundle);
Johan Hovold8ec589b2016-01-29 15:42:31 +0100492
493 dev_dbg(&bundle->dev, "%s\n", __func__);
494
Viresh Kumar5a53e022016-04-03 12:18:35 +0530495 gb_connection_disable(bootrom->connection);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530496
Viresh Kumara956d932016-05-09 10:59:00 +0530497 /* Disable timeouts */
Viresh Kumar40d276e2016-07-22 14:13:43 -0700498 gb_bootrom_cancel_timeout(bootrom);
Viresh Kumara956d932016-05-09 10:59:00 +0530499
500 /*
501 * Release firmware:
502 *
503 * As the connection and the delayed work are already disabled, we don't
504 * need to lock access to bootrom->fw here.
505 */
506 free_firmware(bootrom);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530507
Viresh Kumar5a53e022016-04-03 12:18:35 +0530508 gb_connection_destroy(bootrom->connection);
509 kfree(bootrom);
Viresh Kumar90f1b612015-08-12 09:19:33 +0530510}
511
Viresh Kumar5a53e022016-04-03 12:18:35 +0530512static const struct greybus_bundle_id gb_bootrom_id_table[] = {
513 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_BOOTROM) },
Johan Hovold8ec589b2016-01-29 15:42:31 +0100514 { }
Viresh Kumar90f1b612015-08-12 09:19:33 +0530515};
Johan Hovold8ec589b2016-01-29 15:42:31 +0100516
Viresh Kumar5a53e022016-04-03 12:18:35 +0530517static struct greybus_driver gb_bootrom_driver = {
518 .name = "bootrom",
519 .probe = gb_bootrom_probe,
520 .disconnect = gb_bootrom_disconnect,
521 .id_table = gb_bootrom_id_table,
Johan Hovold8ec589b2016-01-29 15:42:31 +0100522};
523
Viresh Kumaraa62b5e2016-06-09 16:34:35 +0530524module_greybus_driver(gb_bootrom_driver);
Johan Hovold8ec589b2016-01-29 15:42:31 +0100525
Viresh Kumaraa62b5e2016-06-09 16:34:35 +0530526MODULE_LICENSE("GPL v2");