summaryrefslogtreecommitdiff
path: root/drivers/misc/mei/vsc-fw-loader.c
blob: 084d0205f97d6dd03c334102f377c343f2b4360f (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2023, Intel Corporation.
 * Intel Visual Sensing Controller Transport Layer Linux driver
 */

#include <linux/acpi.h>
#include <linux/align.h>
#include <linux/bitfield.h>
#include <linux/bits.h>
#include <linux/cleanup.h>
#include <linux/firmware.h>
#include <linux/sizes.h>
#include <linux/slab.h>
#include <linux/string_helpers.h>
#include <linux/types.h>

#include <asm-generic/unaligned.h>

#include "vsc-tp.h"

#define VSC_MAGIC_NUM			0x49505343 /* IPSC */
#define VSC_MAGIC_FW			0x49574653 /* IWFS */
#define VSC_MAGIC_FILE			0x46564353 /* FVCS */

#define VSC_ADDR_BASE			0xE0030000
#define VSC_EFUSE_ADDR			(VSC_ADDR_BASE + 0x038)
#define VSC_STRAP_ADDR			(VSC_ADDR_BASE + 0x100)

#define VSC_MAINSTEPPING_VERSION_MASK	GENMASK(7, 4)
#define VSC_MAINSTEPPING_VERSION_A	0

#define VSC_SUBSTEPPING_VERSION_MASK	GENMASK(3, 0)
#define VSC_SUBSTEPPING_VERSION_0	0
#define VSC_SUBSTEPPING_VERSION_1	2

#define VSC_BOOT_IMG_OPTION_MASK	GENMASK(15, 0)

#define VSC_SKU_CFG_LOCATION		0x5001A000
#define VSC_SKU_MAX_SIZE		4100u

#define VSC_ACE_IMG_CNT			2
#define VSC_CSI_IMG_CNT			4
#define VSC_IMG_CNT_MAX			6

#define VSC_ROM_PKG_SIZE		256u
#define VSC_FW_PKG_SIZE			512u

#define VSC_IMAGE_DIR			"intel/vsc/"

#define VSC_CSI_IMAGE_NAME		VSC_IMAGE_DIR "ivsc_fw.bin"
#define VSC_ACE_IMAGE_NAME_FMT		VSC_IMAGE_DIR "ivsc_pkg_%s_0.bin"
#define VSC_CFG_IMAGE_NAME_FMT		VSC_IMAGE_DIR "ivsc_skucfg_%s_0_1.bin"

#define VSC_IMAGE_PATH_MAX_LEN		64

#define VSC_SENSOR_NAME_MAX_LEN		16

/* command id */
enum {
	VSC_CMD_QUERY = 0,
	VSC_CMD_DL_SET = 1,
	VSC_CMD_DL_START = 2,
	VSC_CMD_DL_CONT = 3,
	VSC_CMD_DUMP_MEM = 4,
	VSC_CMD_GET_CONT = 8,
	VSC_CMD_CAM_BOOT = 10,
};

/* command ack token */
enum {
	VSC_TOKEN_BOOTLOADER_REQ = 1,
	VSC_TOKEN_DUMP_RESP = 4,
	VSC_TOKEN_ERROR = 7,
};

/* image type */
enum {
	VSC_IMG_BOOTLOADER_TYPE = 1,
	VSC_IMG_CSI_EM7D_TYPE,
	VSC_IMG_CSI_SEM_TYPE,
	VSC_IMG_CSI_RUNTIME_TYPE,
	VSC_IMG_ACE_VISION_TYPE,
	VSC_IMG_ACE_CFG_TYPE,
	VSC_IMG_SKU_CFG_TYPE,
};

/* image fragments */
enum {
	VSC_IMG_BOOTLOADER_FRAG,
	VSC_IMG_CSI_SEM_FRAG,
	VSC_IMG_CSI_RUNTIME_FRAG,
	VSC_IMG_ACE_VISION_FRAG,
	VSC_IMG_ACE_CFG_FRAG,
	VSC_IMG_CSI_EM7D_FRAG,
	VSC_IMG_SKU_CFG_FRAG,
	VSC_IMG_FRAG_MAX
};

struct vsc_rom_cmd {
	__le32 magic;
	__u8 cmd_id;
	union {
		/* download start */
		struct {
			__u8 img_type;
			__le16 option;
			__le32 img_len;
			__le32 img_loc;
			__le32 crc;
			DECLARE_FLEX_ARRAY(__u8, res);
		} __packed dl_start;
		/* download set */
		struct {
			__u8 option;
			__le16 img_cnt;
			DECLARE_FLEX_ARRAY(__le32, payload);
		} __packed dl_set;
		/* download continue */
		struct {
			__u8 end_flag;
			__le16 len;
			/* 8 is the offset of payload */
			__u8 payload[VSC_ROM_PKG_SIZE - 8];
		} __packed dl_cont;
		/* dump memory */
		struct {
			__u8 res;
			__le16 len;
			__le32 addr;
			DECLARE_FLEX_ARRAY(__u8, payload);
		} __packed dump_mem;
		/* 5 is the offset of padding */
		__u8 padding[VSC_ROM_PKG_SIZE - 5];
	} data;
};

struct vsc_rom_cmd_ack {
	__le32 magic;
	__u8 token;
	__u8 type;
	__u8 res[2];
	__u8 payload[];
};

struct vsc_fw_cmd {
	__le32 magic;
	__u8 cmd_id;
	union {
		struct {
			__le16 option;
			__u8 img_type;
			__le32 img_len;
			__le32 img_loc;
			__le32 crc;
			DECLARE_FLEX_ARRAY(__u8, res);
		} __packed dl_start;
		struct {
			__le16 option;
			__u8 img_cnt;
			DECLARE_FLEX_ARRAY(__le32, payload);
		} __packed dl_set;
		struct {
			__le32 addr;
			__u8 len;
			DECLARE_FLEX_ARRAY(__u8, payload);
		} __packed dump_mem;
		struct {
			__u8 resv[3];
			__le32 crc;
			DECLARE_FLEX_ARRAY(__u8, payload);
		} __packed boot;
		/* 5 is the offset of padding */
		__u8 padding[VSC_FW_PKG_SIZE - 5];
	} data;
};

struct vsc_img {
	__le32 magic;
	__le32 option;
	__le32 image_count;
	__le32 image_location[VSC_IMG_CNT_MAX];
};

struct vsc_fw_sign {
	__le32 magic;
	__le32 image_size;
	__u8 image[];
};

struct vsc_image_code_data {
	/* fragment index */
	u8 frag_index;
	/* image type */
	u8 image_type;
};

struct vsc_img_frag {
	u8 type;
	u32 location;
	const u8 *data;
	u32 size;
};

/**
 * struct vsc_fw_loader - represent vsc firmware loader
 * @dev: device used to request firmware
 * @tp: transport layer used with the firmware loader
 * @csi: CSI image
 * @ace: ACE image
 * @cfg: config image
 * @tx_buf: tx buffer
 * @rx_buf: rx buffer
 * @option: command option
 * @count: total image count
 * @sensor_name: camera sensor name
 * @frags: image fragments
 */
struct vsc_fw_loader {
	struct device *dev;
	struct vsc_tp *tp;

	const struct firmware *csi;
	const struct firmware *ace;
	const struct firmware *cfg;

	void *tx_buf;
	void *rx_buf;

	u16 option;
	u16 count;

	char sensor_name[VSC_SENSOR_NAME_MAX_LEN];

	struct vsc_img_frag frags[VSC_IMG_FRAG_MAX];
};

static inline u32 vsc_sum_crc(void *data, size_t size)
{
	u32 crc = 0;
	size_t i;

	for (i = 0; i < size; i++)
		crc += *((u8 *)data + i);

	return crc;
}

/* get sensor name to construct image name */
static int vsc_get_sensor_name(struct vsc_fw_loader *fw_loader,
			       struct device *dev)
{
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER };
	union acpi_object obj = {
		.integer.type = ACPI_TYPE_INTEGER,
		.integer.value = 1,
	};
	struct acpi_object_list arg_list = {
		.count = 1,
		.pointer = &obj,
	};
	union acpi_object *ret_obj;
	acpi_handle handle;
	acpi_status status;
	int ret = 0;

	handle = ACPI_HANDLE(dev);
	if (!handle)
		return -EINVAL;

	status = acpi_evaluate_object(handle, "SID", &arg_list, &buffer);
	if (ACPI_FAILURE(status)) {
		dev_err(dev, "can't evaluate SID method: %d\n", status);
		return -ENODEV;
	}

	ret_obj = buffer.pointer;
	if (!ret_obj) {
		dev_err(dev, "can't locate ACPI buffer\n");
		return -ENODEV;
	}

	if (ret_obj->type != ACPI_TYPE_STRING) {
		dev_err(dev, "found non-string entry\n");
		ret = -ENODEV;
		goto out_free_buff;
	}

	/* string length excludes trailing NUL */
	if (ret_obj->string.length >= sizeof(fw_loader->sensor_name)) {
		dev_err(dev, "sensor name buffer too small\n");
		ret = -EINVAL;
		goto out_free_buff;
	}

	memcpy(fw_loader->sensor_name, ret_obj->string.pointer,
	       ret_obj->string.length);

	string_lower(fw_loader->sensor_name, fw_loader->sensor_name);

out_free_buff:
	ACPI_FREE(buffer.pointer);

	return ret;
}

static int vsc_identify_silicon(struct vsc_fw_loader *fw_loader)
{
	struct vsc_rom_cmd_ack *ack = fw_loader->rx_buf;
	struct vsc_rom_cmd *cmd = fw_loader->tx_buf;
	u8 version, sub_version;
	int ret;

	/* identify stepping information */
	cmd->magic = cpu_to_le32(VSC_MAGIC_NUM);
	cmd->cmd_id = VSC_CMD_DUMP_MEM;
	cmd->data.dump_mem.addr = cpu_to_le32(VSC_EFUSE_ADDR);
	cmd->data.dump_mem.len = cpu_to_le16(sizeof(__le32));
	ret = vsc_tp_rom_xfer(fw_loader->tp, cmd, ack, VSC_ROM_PKG_SIZE);
	if (ret)
		return ret;
	if (ack->token == VSC_TOKEN_ERROR)
		return -EINVAL;

	cmd->magic = cpu_to_le32(VSC_MAGIC_NUM);
	cmd->cmd_id = VSC_CMD_GET_CONT;
	ret = vsc_tp_rom_xfer(fw_loader->tp, cmd, ack, VSC_ROM_PKG_SIZE);
	if (ret)
		return ret;
	if (ack->token != VSC_TOKEN_DUMP_RESP)
		return -EINVAL;

	version = FIELD_GET(VSC_MAINSTEPPING_VERSION_MASK, ack->payload[0]);
	sub_version = FIELD_GET(VSC_SUBSTEPPING_VERSION_MASK, ack->payload[0]);

	if (version != VSC_MAINSTEPPING_VERSION_A)
		return -EINVAL;

	if (sub_version != VSC_SUBSTEPPING_VERSION_0 &&
	    sub_version != VSC_SUBSTEPPING_VERSION_1)
		return -EINVAL;

	dev_info(fw_loader->dev, "silicon stepping version is %u:%u\n",
		 version, sub_version);

	/* identify strap information */
	cmd->magic = cpu_to_le32(VSC_MAGIC_NUM);
	cmd->cmd_id = VSC_CMD_DUMP_MEM;
	cmd->data.dump_mem.addr = cpu_to_le32(VSC_STRAP_ADDR);
	cmd->data.dump_mem.len = cpu_to_le16(sizeof(__le32));
	ret = vsc_tp_rom_xfer(fw_loader->tp, cmd, ack, VSC_ROM_PKG_SIZE);
	if (ret)
		return ret;
	if (ack->token == VSC_TOKEN_ERROR)
		return -EINVAL;

	cmd->magic = cpu_to_le32(VSC_MAGIC_NUM);
	cmd->cmd_id = VSC_CMD_GET_CONT;
	ret = vsc_tp_rom_xfer(fw_loader->tp, cmd, ack, VSC_ROM_PKG_SIZE);
	if (ret)
		return ret;
	if (ack->token != VSC_TOKEN_DUMP_RESP)
		return -EINVAL;

	return 0;
}

static int vsc_identify_csi_image(struct vsc_fw_loader *fw_loader)
{
	const struct firmware *image;
	struct vsc_fw_sign *sign;
	struct vsc_img *img;
	unsigned int i;
	int ret;

	ret = request_firmware(&image, VSC_CSI_IMAGE_NAME, fw_loader->dev);
	if (ret)
		return ret;

	img = (struct vsc_img *)image->data;
	if (!img) {
		ret = -ENOENT;
		goto err_release_image;
	}

	if (le32_to_cpu(img->magic) != VSC_MAGIC_FILE) {
		ret = -EINVAL;
		goto err_release_image;
	}

	if (le32_to_cpu(img->image_count) != VSC_CSI_IMG_CNT) {
		ret = -EINVAL;
		goto err_release_image;
	}
	fw_loader->count += le32_to_cpu(img->image_count) - 1;

	fw_loader->option =
		FIELD_GET(VSC_BOOT_IMG_OPTION_MASK, le32_to_cpu(img->option));

	sign = (struct vsc_fw_sign *)
		(img->image_location + le32_to_cpu(img->image_count));

	for (i = 0; i < VSC_CSI_IMG_CNT; i++) {
		/* mapping from CSI image index to image code data */
		static const struct vsc_image_code_data csi_image_map[] = {
			{ VSC_IMG_BOOTLOADER_FRAG, VSC_IMG_BOOTLOADER_TYPE },
			{ VSC_IMG_CSI_SEM_FRAG, VSC_IMG_CSI_SEM_TYPE },
			{ VSC_IMG_CSI_RUNTIME_FRAG, VSC_IMG_CSI_RUNTIME_TYPE },
			{ VSC_IMG_CSI_EM7D_FRAG, VSC_IMG_CSI_EM7D_TYPE },
		};
		struct vsc_img_frag *frag;

		if ((u8 *)sign + sizeof(*sign) > image->data + image->size) {
			ret = -EINVAL;
			goto err_release_image;
		}

		if (le32_to_cpu(sign->magic) != VSC_MAGIC_FW) {
			ret = -EINVAL;
			goto err_release_image;
		}

		if (!le32_to_cpu(img->image_location[i])) {
			ret = -EINVAL;
			goto err_release_image;
		}

		frag = &fw_loader->frags[csi_image_map[i].frag_index];

		frag->data = sign->image;
		frag->size = le32_to_cpu(sign->image_size);
		frag->location = le32_to_cpu(img->image_location[i]);
		frag->type = csi_image_map[i].image_type;

		sign = (struct vsc_fw_sign *)
			(sign->image + le32_to_cpu(sign->image_size));
	}

	fw_loader->csi = image;

	return 0;

err_release_image:
	release_firmware(image);

	return ret;
}

static int vsc_identify_ace_image(struct vsc_fw_loader *fw_loader)
{
	char path[VSC_IMAGE_PATH_MAX_LEN];
	const struct firmware *image;
	struct vsc_fw_sign *sign;
	struct vsc_img *img;
	unsigned int i;
	int ret;

	snprintf(path, sizeof(path), VSC_ACE_IMAGE_NAME_FMT,
		 fw_loader->sensor_name);

	ret = request_firmware(&image, path, fw_loader->dev);
	if (ret)
		return ret;

	img = (struct vsc_img *)image->data;
	if (!img) {
		ret = -ENOENT;
		goto err_release_image;
	}

	if (le32_to_cpu(img->magic) != VSC_MAGIC_FILE) {
		ret = -EINVAL;
		goto err_release_image;
	}

	if (le32_to_cpu(img->image_count) != VSC_ACE_IMG_CNT) {
		ret = -EINVAL;
		goto err_release_image;
	}
	fw_loader->count += le32_to_cpu(img->image_count);

	sign = (struct vsc_fw_sign *)
		(img->image_location + le32_to_cpu(img->image_count));

	for (i = 0; i < VSC_ACE_IMG_CNT; i++) {
		/* mapping from ACE image index to image code data */
		static const struct vsc_image_code_data ace_image_map[] = {
			{ VSC_IMG_ACE_VISION_FRAG, VSC_IMG_ACE_VISION_TYPE },
			{ VSC_IMG_ACE_CFG_FRAG, VSC_IMG_ACE_CFG_TYPE },
		};
		struct vsc_img_frag *frag, *last_frag;
		u8 frag_index;

		if ((u8 *)sign + sizeof(*sign) > image->data + image->size) {
			ret = -EINVAL;
			goto err_release_image;
		}

		if (le32_to_cpu(sign->magic) != VSC_MAGIC_FW) {
			ret = -EINVAL;
			goto err_release_image;
		}

		frag_index = ace_image_map[i].frag_index;
		frag = &fw_loader->frags[frag_index];

		frag->data = sign->image;
		frag->size = le32_to_cpu(sign->image_size);
		frag->location = le32_to_cpu(img->image_location[i]);
		frag->type = ace_image_map[i].image_type;

		if (!frag->location) {
			last_frag = &fw_loader->frags[frag_index - 1];
			frag->location =
				ALIGN(last_frag->location + last_frag->size, SZ_4K);
		}

		sign = (struct vsc_fw_sign *)
			(sign->image + le32_to_cpu(sign->image_size));
	}

	fw_loader->ace = image;

	return 0;

err_release_image:
	release_firmware(image);

	return ret;
}

static int vsc_identify_cfg_image(struct vsc_fw_loader *fw_loader)
{
	struct vsc_img_frag *frag = &fw_loader->frags[VSC_IMG_SKU_CFG_FRAG];
	char path[VSC_IMAGE_PATH_MAX_LEN];
	const struct firmware *image;
	u32 size;
	int ret;

	snprintf(path, sizeof(path), VSC_CFG_IMAGE_NAME_FMT,
		 fw_loader->sensor_name);

	ret = request_firmware(&image, path, fw_loader->dev);
	if (ret)
		return ret;

	/* identify image size */
	if (image->size <= sizeof(u32) || image->size > VSC_SKU_MAX_SIZE) {
		ret = -EINVAL;
		goto err_release_image;
	}

	size = le32_to_cpu(*((__le32 *)image->data)) + sizeof(u32);
	if (image->size != size) {
		ret = -EINVAL;
		goto err_release_image;
	}

	frag->data = image->data;
	frag->size = image->size;
	frag->type = VSC_IMG_SKU_CFG_TYPE;
	frag->location = VSC_SKU_CFG_LOCATION;

	fw_loader->cfg = image;

	return 0;

err_release_image:
	release_firmware(image);

	return ret;
}

static int vsc_download_bootloader(struct vsc_fw_loader *fw_loader)
{
	struct vsc_img_frag *frag = &fw_loader->frags[VSC_IMG_BOOTLOADER_FRAG];
	struct vsc_rom_cmd_ack *ack = fw_loader->rx_buf;
	struct vsc_rom_cmd *cmd = fw_loader->tx_buf;
	u32 len, c_len;
	size_t remain;
	const u8 *p;
	int ret;

	cmd->magic = cpu_to_le32(VSC_MAGIC_NUM);
	cmd->cmd_id = VSC_CMD_QUERY;
	ret = vsc_tp_rom_xfer(fw_loader->tp, cmd, ack, VSC_ROM_PKG_SIZE);
	if (ret)
		return ret;
	if (ack->token != VSC_TOKEN_DUMP_RESP &&
	    ack->token != VSC_TOKEN_BOOTLOADER_REQ)
		return -EINVAL;

	cmd->magic = cpu_to_le32(VSC_MAGIC_NUM);
	cmd->cmd_id = VSC_CMD_DL_START;
	cmd->data.dl_start.option = cpu_to_le16(fw_loader->option);
	cmd->data.dl_start.img_type = frag->type;
	cmd->data.dl_start.img_len = cpu_to_le32(frag->size);
	cmd->data.dl_start.img_loc = cpu_to_le32(frag->location);

	c_len = offsetof(struct vsc_rom_cmd, data.dl_start.crc);
	cmd->data.dl_start.crc = cpu_to_le32(vsc_sum_crc(cmd, c_len));

	ret = vsc_tp_rom_xfer(fw_loader->tp, cmd, NULL, VSC_ROM_PKG_SIZE);
	if (ret)
		return ret;

	p = frag->data;
	remain = frag->size;

	/* download image data */
	while (remain > 0) {
		len = min(remain, sizeof(cmd->data.dl_cont.payload));

		cmd->magic = cpu_to_le32(VSC_MAGIC_NUM);
		cmd->cmd_id = VSC_CMD_DL_CONT;
		cmd->data.dl_cont.len = cpu_to_le16(len);
		cmd->data.dl_cont.end_flag = remain == len;
		memcpy(cmd->data.dl_cont.payload, p, len);

		ret = vsc_tp_rom_xfer(fw_loader->tp, cmd, NULL, VSC_ROM_PKG_SIZE);
		if (ret)
			return ret;

		p += len;
		remain -= len;
	}

	return 0;
}

static int vsc_download_firmware(struct vsc_fw_loader *fw_loader)
{
	struct vsc_fw_cmd *cmd = fw_loader->tx_buf;
	unsigned int i, index = 0;
	u32 c_len;
	int ret;

	cmd->magic = cpu_to_le32(VSC_MAGIC_NUM);
	cmd->cmd_id = VSC_CMD_DL_SET;
	cmd->data.dl_set.img_cnt = cpu_to_le16(fw_loader->count);
	put_unaligned_le16(fw_loader->option, &cmd->data.dl_set.option);

	for (i = VSC_IMG_CSI_SEM_FRAG; i <= VSC_IMG_CSI_EM7D_FRAG; i++) {
		struct vsc_img_frag *frag = &fw_loader->frags[i];

		cmd->data.dl_set.payload[index++] = cpu_to_le32(frag->location);
		cmd->data.dl_set.payload[index++] = cpu_to_le32(frag->size);
	}

	c_len = offsetof(struct vsc_fw_cmd, data.dl_set.payload[index]);
	cmd->data.dl_set.payload[index] = cpu_to_le32(vsc_sum_crc(cmd, c_len));

	ret = vsc_tp_rom_xfer(fw_loader->tp, cmd, NULL, VSC_FW_PKG_SIZE);
	if (ret)
		return ret;

	for (i = VSC_IMG_CSI_SEM_FRAG; i < VSC_IMG_FRAG_MAX; i++) {
		struct vsc_img_frag *frag = &fw_loader->frags[i];
		const u8 *p;
		u32 remain;

		cmd->magic = cpu_to_le32(VSC_MAGIC_NUM);
		cmd->cmd_id = VSC_CMD_DL_START;
		cmd->data.dl_start.img_type = frag->type;
		cmd->data.dl_start.img_len = cpu_to_le32(frag->size);
		cmd->data.dl_start.img_loc = cpu_to_le32(frag->location);
		put_unaligned_le16(fw_loader->option, &cmd->data.dl_start.option);

		c_len = offsetof(struct vsc_fw_cmd, data.dl_start.crc);
		cmd->data.dl_start.crc = cpu_to_le32(vsc_sum_crc(cmd, c_len));

		ret = vsc_tp_rom_xfer(fw_loader->tp, cmd, NULL, VSC_FW_PKG_SIZE);
		if (ret)
			return ret;

		p = frag->data;
		remain = frag->size;

		/* download image data */
		while (remain > 0) {
			u32 len = min(remain, VSC_FW_PKG_SIZE);

			memcpy(fw_loader->tx_buf, p, len);
			memset(fw_loader->tx_buf + len, 0, VSC_FW_PKG_SIZE - len);

			ret = vsc_tp_rom_xfer(fw_loader->tp, fw_loader->tx_buf,
					      NULL, VSC_FW_PKG_SIZE);
			if (ret)
				break;

			p += len;
			remain -= len;
		}
	}

	cmd->magic = cpu_to_le32(VSC_MAGIC_NUM);
	cmd->cmd_id = VSC_CMD_CAM_BOOT;

	c_len = offsetof(struct vsc_fw_cmd, data.dl_start.crc);
	cmd->data.boot.crc = cpu_to_le32(vsc_sum_crc(cmd, c_len));

	return vsc_tp_rom_xfer(fw_loader->tp, cmd, NULL, VSC_FW_PKG_SIZE);
}

/**
 * vsc_tp_init - init vsc_tp
 * @tp: vsc_tp device handle
 * @dev: device node for mei vsc device
 * Return: 0 in case of success, negative value in case of error
 */
int vsc_tp_init(struct vsc_tp *tp, struct device *dev)
{
	struct vsc_fw_loader *fw_loader __free(kfree) = NULL;
	void *tx_buf __free(kfree) = NULL;
	void *rx_buf __free(kfree) = NULL;
	int ret;

	fw_loader = kzalloc(sizeof(*fw_loader), GFP_KERNEL);
	if (!fw_loader)
		return -ENOMEM;

	tx_buf = kzalloc(VSC_FW_PKG_SIZE, GFP_KERNEL);
	if (!tx_buf)
		return -ENOMEM;

	rx_buf = kzalloc(VSC_FW_PKG_SIZE, GFP_KERNEL);
	if (!rx_buf)
		return -ENOMEM;

	fw_loader->tx_buf = tx_buf;
	fw_loader->rx_buf = rx_buf;

	fw_loader->tp = tp;
	fw_loader->dev = dev;

	ret = vsc_get_sensor_name(fw_loader, dev);
	if (ret)
		return ret;

	ret = vsc_identify_silicon(fw_loader);
	if (ret)
		return ret;

	ret = vsc_identify_csi_image(fw_loader);
	if (ret)
		return ret;

	ret = vsc_identify_ace_image(fw_loader);
	if (ret)
		goto err_release_csi;

	ret = vsc_identify_cfg_image(fw_loader);
	if (ret)
		goto err_release_ace;

	ret = vsc_download_bootloader(fw_loader);
	if (!ret)
		ret = vsc_download_firmware(fw_loader);

	release_firmware(fw_loader->cfg);

err_release_ace:
	release_firmware(fw_loader->ace);

err_release_csi:
	release_firmware(fw_loader->csi);

	return ret;
}
EXPORT_SYMBOL_NS_GPL(vsc_tp_init, VSC_TP);