summaryrefslogtreecommitdiff
path: root/drivers/misc/issp/issp_steps.c
blob: d552e9187c292811a65f188a54eadddfa46977fa (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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
 * Copyright (c) 2006-2013, Cypress Semiconductor Corporation
 * All rights reserved.
 *
 * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/gpio.h>
#include <linux/hrtimer.h>
#include <linux/platform_device.h>
#include <linux/delay.h>

#include "issp_priv.h"

#define ISSP_RESET_ASSERT_DELAY		(14270 + 10)
#define ISSP_RESET_PULSE_LENGTH		(300 + 10)
#define ISSP_RESET_POST_DELAY		(1)
#define ISSP_DATA_TRANS_TIMEOUT		(200000)

/* vectors */

static const int bits_id_setup_1 = 594;
static const uint8_t vec_id_setup_1[] = {
	0xCA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x0D, 0xEE, 0x21, 0xF7, 0xF0, 0x27, 0xDC, 0x40,
	0x9F, 0x70, 0x01, 0xFD, 0xEE, 0x01, 0xE7, 0xC1,
	0xD7, 0x9F, 0x20, 0x7E, 0x7D, 0x88, 0x7D, 0xEE,
	0x21, 0xF7, 0xF0, 0x07, 0xDC, 0x40, 0x1F, 0x70,
	0x01, 0xFD, 0xEE, 0x01, 0xF7, 0xA0, 0x1F, 0xDE,
	0xA0, 0x1F, 0x7B, 0x00, 0x7D, 0xE0, 0x13, 0xF7,
	0xC0, 0x07, 0xDF, 0x28, 0x1F, 0x7D, 0x18, 0x7D,
	0xFE, 0x25, 0x80
};

static const int bits_id_setup_2 = 418;
static const uint8_t vec_id_setup_2[] = {
	0xDE, 0xE2, 0x1F, 0x7F, 0x02, 0x7D, 0xC4, 0x09,
	0xF7, 0x00, 0x1F, 0x9F, 0x07, 0x5E, 0x7C, 0x81,
	0xF9, 0xF4, 0x01, 0xF7, 0xF0, 0x07, 0xDC, 0x40,
	0x1F, 0x70, 0x01, 0xFD, 0xEE, 0x01, 0xF7, 0xA0,
	0x1F, 0xDE, 0xA0, 0x1F, 0x7B, 0x00, 0x7D, 0xE0,
	0x0D, 0xF7, 0xC0, 0x07, 0xDF, 0x28, 0x1F, 0x7D,
	0x18, 0x7D, 0xFE, 0x25, 0x80
};

static const int bits_set_block_num = 11;
static const uint8_t vec_set_block_num[] = {
	0x9F, 0x40
};
static const int bits_set_block_num_end = 3;
static const uint8_t vec_set_block_num_end[] = {
	0xE0
};

static const int bits_checksum_setup = 418;
static const uint8_t vec_checksum_setup[] = {
	0xDE, 0xE2, 0x1F, 0x7F, 0x02, 0x7D, 0xC4, 0x09,
	0xF7, 0x00, 0x1F, 0x9F, 0x07, 0x5E, 0x7C, 0x81,
	0xF9, 0xF4, 0x01, 0xF7, 0xF0, 0x07, 0xDC, 0x40,
	0x1F, 0x70, 0x01, 0xFD, 0xEE, 0x01, 0xF7, 0xA0,
	0x1F, 0xDE, 0xA0, 0x1F, 0x7B, 0x00, 0x7D, 0xE0,
	0x0F, 0xF7, 0xC0, 0x07, 0xDF, 0x28, 0x1F, 0x7D,
	0x18, 0x7D, 0xFE, 0x25, 0x80
};

static const int bits_read_checksum_v[3] = {
	11, 12, 1
};
static const uint8_t vec_read_checksum_v[3][2] = {
	{0xBF, 0x20}, {0xDF, 0x80}, {0x80}
};

static const int bits_program_and_verify = 440;
static const uint8_t vec_program_and_verify[] = {
	0xDE, 0xE2, 0x1F, 0x7F, 0x02, 0x7D, 0xC4, 0x09,
	0xF7, 0x00, 0x1F, 0x9F, 0x07, 0x5E, 0x7C, 0x81,
	0xF9, 0xF7, 0x01, 0xF7, 0xF0, 0x07, 0xDC, 0x40,
	0x1F, 0x70, 0x01, 0xFD, 0xEE, 0x01, 0xF6, 0xA0,
	0x0F, 0xDE, 0x80, 0x7F, 0x7A, 0x80, 0x7D, 0xEC,
	0x01, 0xF7, 0x80, 0x57, 0xDF, 0x00, 0x1F, 0x7C,
	0xA0, 0x7D, 0xF4, 0x61, 0xF7, 0xF8, 0x96
};

static const int bits_erase = 396;
static const uint8_t vec_erase[] = {
	0xDE, 0xE2, 0x1F, 0x7F, 0x02, 0x7D, 0xC4, 0x09,
	0xF7, 0x00, 0x1F, 0x9F, 0x07, 0x5E, 0x7C, 0x85,
	0xFD, 0xFC, 0x01, 0xF7, 0x10, 0x07, 0xDC, 0x00,
	0x7F, 0x7B, 0x80, 0x7D, 0xE0, 0x0B, 0xF7, 0xA0,
	0x1F, 0xDE, 0xA0, 0x1F, 0x7B, 0x04, 0x7D, 0xF0,
	0x01, 0xF7, 0xC9, 0x87, 0xDF, 0x48, 0x1F, 0x7F,
	0x89, 0x60
};

static const int bits_secure = 440;
static const uint8_t vec_secure[] = {
	0xDE, 0xE2, 0x1F, 0x7F, 0x02, 0x7D, 0xC4, 0x09,
	0xF7, 0x00, 0x1F, 0x9F, 0x07, 0x5E, 0x7C, 0x81,
	0xF9, 0xF7, 0x01, 0xF7, 0xF0, 0x07, 0xDC, 0x40,
	0x1F, 0x70, 0x01, 0xFD, 0xEE, 0x01, 0xF6, 0xA0,
	0x0F, 0xDE, 0x80, 0x7F, 0x7A, 0x80, 0x7D, 0xEC,
	0x01, 0xF7, 0x80, 0x27, 0xDF, 0x00, 0x1F, 0x7C,
	0xA0, 0x7D, 0xF4, 0x61, 0xF7, 0xF8, 0x96
};

static const int bits_read_security_setup = 88;
static const uint8_t vec_read_security_setup[] = {
	0xDE, 0xE2, 0x1F, 0x60, 0x88, 0x7D, 0x84, 0x21,
	0xF7, 0xB8, 0x07
};

static const int bits_read_security_pt1 = 78;
static const uint8_t vec_read_security_pt1[] = {
	0xDE, 0xE2, 0x1F, 0x72, 0x87, 0x7D, 0xCA, 0x01,
	0xF7, 0x28
};

static const int bits_read_security_pt1_end = 25;
static const uint8_t vec_read_security_pt1_end[] = {
	0xFB, 0x94, 0x03, 0x80
};

static const int bits_read_security_pt2 = 198;
static const uint8_t vec_read_security_pt2[] = {
	0xDE, 0xE0, 0x1F, 0x7A, 0x01, 0xFD, 0xEA, 0x01,
	0xF7, 0xB0, 0x07, 0xDF, 0x0B, 0xBF, 0x7C, 0xF2,
	0xFD, 0xF4, 0x61, 0xF7, 0xB8, 0x87, 0xDF, 0xE2,
	0x58
};

static const int bits_read_security_pt3 = 122;
static const uint8_t vec_read_security_pt3[] = {
	0xDE, 0xE0, 0x1F, 0x7A, 0x01, 0xFD, 0xEA, 0x01,
	0xF7, 0xB0, 0x07, 0xDF, 0x0A, 0x7F, 0x7C, 0xC0
};

static const int bits_read_security_pt3_end = 47;
static const uint8_t vec_read_security_pt3_end[] = {
	0xFB, 0xE8, 0xC3, 0xEF, 0xF1, 0x2C
};

static const int bits_read_write_setup = 66;
static const uint8_t vec_read_write_setup[] = {
	0xDE, 0xF0, 0x1F, 0x78, 0x00, 0x7D, 0xA0, 0x03,
	0xC0
};

static const int bits_write_byte_start = 4;
static const uint8_t vec_write_byte_start[] = {
	0x90
};
static const int bits_write_byte_end = 3;
static const uint8_t vec_write_byte_end[] = {
	0xE0
};

static const int bits_read_id_v[5] = {
	11, 12, 12, 12, 1
};
static const uint8_t vec_read_id_v[5][2] = {
	{0xBF, 0x00}, {0xDF, 0x90}, {0xFF, 0x30}, {0xFF, 0x00}, {0x80}
};

static const int bits_read_status = 11;
static const uint8_t vec_read_status[] = {
	0xBF, 0x00
};
static const int bits_read_status_end = 1;
static const uint8_t vec_read_status_end[] = {
	0x80
};

static const int bits_verify_setup = 440;
static const uint8_t vec_verify_setup[] = {
	0xDE, 0xE2, 0x1F, 0x7F, 0x02, 0x7D, 0xC4, 0x09,
	0xF7, 0x00, 0x1F, 0x9F, 0x07, 0x5E, 0x7C, 0x81,
	0xF9, 0xF7, 0x01, 0xF7, 0xF0, 0x07, 0xDC, 0x40,
	0x1F, 0x70, 0x01, 0xFD, 0xEE, 0x01, 0xF6, 0xA8,
	0x0F, 0xDE, 0x80, 0x7F, 0x7A, 0x80, 0x7D, 0xEC,
	0x01, 0xF7, 0x80, 0x0F, 0xDF, 0x00, 0x1F, 0x7C,
	0xA0, 0x7D, 0xF4, 0x61, 0xF7, 0xF8, 0x96
};

static const int bits_read_byte_v[2] = {
	4, 1
};
static const uint8_t vec_read_byte_v[2][1] = {
	{0xB0}, {0x80}
};

static const int bits_sync_enable = 110;
static const uint8_t vec_sync_enable[] = {
	0xDE, 0xE2, 0x1F, 0x7F, 0x02, 0x7D, 0xC4, 0x09,
	0xF7, 0x00, 0x1F, 0xDE, 0xE0, 0x1C
};

static const int bits_sync_disable = 110;
static const uint8_t vec_sync_disable[] = {
	0xDE, 0xE2, 0x1F, 0x71, 0x00, 0x7D, 0xFC, 0x01,
	0xF7, 0x00, 0x1F, 0xDE, 0xE0, 0x1C
};

static const int bits_wait_and_poll = 30;
static const uint8_t vec_wait_and_poll[] = {
	0x00, 0x00, 0x00, 0x00
};
/* pin functions */

static inline void pin_reset_lo(struct issp_host *host)
{
	gpio_set_value(host->pdata->reset_gpio, 0);
}

static inline void pin_reset_hi(struct issp_host *host)
{
	gpio_set_value(host->pdata->reset_gpio, 1);
}

static inline void pin_data_lo(struct issp_host *host)
{
	gpio_set_value(host->pdata->data_gpio, 0);
}

static inline void pin_data_hi(struct issp_host *host)
{
	gpio_set_value(host->pdata->data_gpio, 1);
}

static inline void pin_data_in(struct issp_host *host)
{
	gpio_direction_input(host->pdata->data_gpio);
}

static inline void pin_data_out(struct issp_host *host)
{
	gpio_direction_output(host->pdata->data_gpio, 0);
}

static inline void pin_data_z(struct issp_host *host)
{
	pin_data_in(host);
}

static inline int pin_data(struct issp_host *host)
{
	return gpio_get_value(host->pdata->data_gpio);
}

static inline void pin_clk_lo(struct issp_host *host)
{
	gpio_set_value(host->pdata->clk_gpio, 0);
}

static inline void pin_clk_hi(struct issp_host *host)
{
	gpio_set_value(host->pdata->clk_gpio, 1);
}

/* simulate program steps */

static void send_bits(struct issp_host *host, const uint8_t *data, int bits)
{
	int bit_cnt = 0;
	uint8_t byte = 0;

	while (bit_cnt < bits) {
		if (!(bit_cnt & 0x7))
			byte = data[bit_cnt / 8];

		if (byte & 0x80)
			pin_data_hi(host);
		else
			pin_data_lo(host);

		pin_clk_hi(host);
		pin_clk_lo(host);
		byte <<= 1;
		bit_cnt++;
	}
}

static void receive_bits(struct issp_host *host, uint8_t *data, int bits)
{
	int bit_cnt = 0;

	while (bit_cnt < bits) {
		int index = bit_cnt / 8;

		if (!(bit_cnt & 0x7))
			data[index] = 0;
		else
			data[index] <<= 1;

		pin_clk_hi(host);
		if (pin_data(host))
			data[index] |= 0x1;
		pin_clk_lo(host);

		bit_cnt++;
	}
}

static uint8_t read_byte(struct issp_host *host)
{
	uint8_t byte;
	pin_data_in(host);
	receive_bits(host, &byte, 8);
	pin_data_z(host);
	return byte;
}

static void generate_clocks(struct issp_host *host, int num)
{
	pin_data_z(host);
	while (num--) {
		pin_clk_hi(host);
		pin_clk_lo(host);
	}
}

static void send_vector(struct issp_host *host, const uint8_t *pvec, int bits)
{
	pin_data_out(host);
	send_bits(host, pvec, bits);
	pin_data_z(host);
}

static int wait_and_poll(struct issp_host *host)
{
	ktime_t start;

	pin_data_in(host);

	/* wait for data pin go to high */
	start = ktime_get();
	while (1) {
		pin_clk_lo(host);
		if (pin_data(host))
			break;
		pin_clk_hi(host);

		if (ktime_us_delta(ktime_get(), start) >=
					ISSP_DATA_TRANS_TIMEOUT) {
			pin_data_z(host);
			pin_clk_lo(host);
			dev_err(&host->pdev->dev, "Poll high timeout!\n");
			return -ETIMEDOUT;
		}
	}

	/* wait for data pin go to low to finish runing vector */
	start = ktime_get();
	while (1) {
		if (!pin_data(host))
			break;

		if (ktime_us_delta(ktime_get(), start) >=
					ISSP_DATA_TRANS_TIMEOUT) {
			pin_data_z(host);
			dev_err(&host->pdev->dev, "Poll low timeout!\n");
			return -ETIMEDOUT;
		}
	}

	send_vector(host, vec_wait_and_poll, bits_wait_and_poll);

	return 0;
}

static int issp_get_id(struct issp_host *host)
{
	int i, ret;

	send_vector(host, vec_id_setup_1, bits_id_setup_1);
	ret = wait_and_poll(host);
	if (ret)
		return ret;
	send_vector(host, vec_id_setup_2, bits_id_setup_2);
	ret = wait_and_poll(host);
	if (ret)
		return ret;
	send_vector(host, vec_sync_enable, bits_sync_enable);

	for (i = 0; i < 4; i++) {
		send_vector(host, vec_read_id_v[i], bits_read_id_v[i]);
		generate_clocks(host, 2);
		host->si_id[i] = read_byte(host);
	}
	send_vector(host, vec_read_id_v[4], bits_read_id_v[4]);

	send_vector(host, vec_sync_disable, bits_sync_disable);

	return 0;
}

static int issp_erase(struct issp_host *host)
{
	send_vector(host, vec_erase, bits_erase);
	return wait_and_poll(host);
}

static void issp_write_byte(struct issp_host *host, uint8_t addr, uint8_t data)
{
	uint8_t address = addr << 1;

	send_bits(host, vec_write_byte_start, bits_write_byte_start);
	send_bits(host, &address, 7);
	send_bits(host, &data, 8);
	send_bits(host, vec_write_byte_end, bits_write_byte_end);
}

static void issp_send_block(struct issp_host *host)
{
	uint8_t addr = 0;

	send_vector(host, vec_sync_enable, bits_sync_enable);
	send_vector(host, vec_read_write_setup, bits_read_write_setup);

	pin_data_out(host);
	while (addr < host->pdata->block_size)
		issp_write_byte(host, addr++, issp_fw_get_byte(host));
	pin_data_z(host);
}

static int issp_prog_verify_block(struct issp_host *host, uint8_t idx)
{
	send_vector(host, vec_sync_enable, bits_sync_enable);
	send_vector(host, vec_set_block_num, bits_set_block_num);
	pin_data_out(host);
	send_bits(host, &idx, 8);
	send_vector(host, vec_set_block_num_end, bits_set_block_num_end);
	send_vector(host, vec_sync_disable, bits_sync_disable);
	send_vector(host, vec_program_and_verify, bits_program_and_verify);

	return wait_and_poll(host);
}

static int issp_check_status(struct issp_host *host)
{
	uint8_t status;
	int ret;

	send_vector(host, vec_sync_enable, bits_sync_enable);
	send_vector(host, vec_read_status, bits_read_status);
	generate_clocks(host, 2);
	status = read_byte(host);
	send_vector(host, vec_read_status_end, bits_read_status_end);
	send_vector(host, vec_sync_disable, bits_sync_disable);

	switch (status) {
	case 0x00:
		ret = 0;
		break;
	case 0x01:
		dev_err(&host->pdev->dev, "ReadStatus: " \
			"Not allowed because of block level protection\n");
		ret = -EACCES;
		break;
	case 0x03:
		dev_err(&host->pdev->dev, "ReadStatus: Fatal error\n");
		ret = -EIO;
		break;
	case 0x04:
		dev_err(&host->pdev->dev, "ReadStatus: Checksum failure\n");
		ret = -EIO;
		break;
	case 0x06:
		dev_err(&host->pdev->dev, "ReadStatus: Caliberate failure\n");
		ret = -EIO;
		break;
	default:
		dev_err(&host->pdev->dev,
			"ReadStatus: Failed 0x%02x\n", status);
		ret = -EIO;
		break;
	}

	return ret;
}

static int issp_program_block(struct issp_host *host, uint8_t idx)
{
	int ret;

	issp_send_block(host);

	ret = issp_prog_verify_block(host, idx);
	if (ret)
		return ret;

	return issp_check_status(host);
}

static int issp_block_verify_setup(struct issp_host *host, uint8_t idx)
{
	send_vector(host, vec_read_write_setup, bits_read_write_setup);
	send_vector(host, vec_sync_enable, bits_sync_enable);
	send_vector(host, vec_set_block_num, bits_set_block_num);
	pin_data_out(host);
	send_bits(host, &idx, 8);
	send_vector(host, vec_set_block_num_end, bits_set_block_num_end);
	send_vector(host, vec_sync_disable, bits_sync_disable);
	send_vector(host, vec_verify_setup, bits_verify_setup);

	return wait_and_poll(host);
}

static uint8_t issp_read_byte_at(struct issp_host *host, uint8_t addr)
{
	uint8_t address = addr << 1, data;

	send_vector(host, vec_read_byte_v[0], bits_read_byte_v[0]);
	pin_data_out(host);
	send_bits(host, &address, 7);
	generate_clocks(host, 2);
	data = read_byte(host);
	send_vector(host, vec_read_byte_v[1], bits_read_byte_v[1]);

	return data;
}

static int issp_block_read_compare(struct issp_host *host)
{
	uint8_t addr = 0;
	int ret = 0;

	send_vector(host, vec_sync_enable, bits_sync_enable);
	send_vector(host, vec_read_write_setup, bits_read_write_setup);

	while (addr < host->pdata->block_size) {
		uint8_t data = issp_read_byte_at(host, addr++);
		if (data != issp_fw_get_byte(host)) {
			dev_err(&host->pdev->dev, "Data compare failed!\n");
			ret =  -EIO;
			break;
		}
	}

	send_vector(host, vec_sync_disable, bits_sync_disable);

	return ret;
}

static int issp_verify_block(struct issp_host *host, uint8_t idx)
{
	int ret;

	ret = issp_block_verify_setup(host, idx);
	if (ret)
		return ret;

	ret = issp_check_status(host);
	if (ret)
		return ret;

	return issp_block_read_compare(host);
}

static int issp_set_security(struct issp_host *host)
{
	uint8_t addr = 0;

	send_vector(host, vec_sync_enable, bits_sync_enable);
	send_vector(host, vec_read_write_setup, bits_read_write_setup);

	pin_data_out(host);
	while (addr < host->pdata->security_size)
		issp_write_byte(host, addr++, issp_fw_get_byte(host));
	pin_data_z(host);

	send_vector(host, vec_secure, bits_secure);

	return wait_and_poll(host);
}

static int issp_verify_security(struct issp_host *host)
{
	uint8_t addr = 0;
	int ret = 0;

	send_vector(host, vec_read_security_setup, bits_read_security_setup);
	while (addr < host->pdata->security_size) {
		uint8_t address = addr << 1;
		addr++;

		send_vector(host, vec_sync_enable, bits_sync_enable);

		send_vector(host, vec_read_security_pt1,
				bits_read_security_pt1);
		pin_data_out(host);
		send_bits(host, &address, 7);
		send_vector(host, vec_read_security_pt1_end,
				bits_read_security_pt1_end);

		send_vector(host, vec_sync_disable, bits_sync_disable);

		send_vector(host, vec_read_security_pt2,
				bits_read_security_pt2);
		send_vector(host, vec_wait_and_poll, bits_wait_and_poll);

		send_vector(host, vec_read_security_pt3,
				bits_read_security_pt3);
		pin_data_out(host);
		send_bits(host, &address, 7);
		send_vector(host, vec_read_security_pt3_end,
				bits_read_security_pt3_end);
		send_vector(host, vec_wait_and_poll, bits_wait_and_poll);
	}

	addr = 0;
	send_vector(host, vec_sync_enable, bits_sync_enable);
	while (addr < host->pdata->security_size) {
		uint8_t data = issp_read_byte_at(host, addr++);
		if (data != issp_fw_get_byte(host)) {
			dev_err(&host->pdev->dev, "Data compare failed!\n");
			ret =  -EIO;
			break;
		}
	}

	send_vector(host, vec_sync_disable, bits_sync_disable);

	return ret;
}

static int issp_verify_checksum(struct issp_host *host)
{
	uint16_t uc_checksum;
	int ret;

	ret = issp_get_checksum(host, &uc_checksum);
	if (ret)
		return ret;

	if (uc_checksum == host->checksum_fw)
		return 0;
	else
		return -EIO;
}

/* global functions */

int issp_uc_program(struct issp_host *host)
{
	pin_data_z(host);
	pin_clk_lo(host);
	pin_reset_hi(host);

	/* reset */
	usleep_range(ISSP_RESET_ASSERT_DELAY, ISSP_RESET_ASSERT_DELAY);
	pin_reset_lo(host);
	udelay(ISSP_RESET_PULSE_LENGTH);
	pin_reset_hi(host);
	udelay(ISSP_RESET_POST_DELAY);

	return issp_get_id(host);
}

int issp_uc_run(struct issp_host *host)
{
	pin_reset_lo(host);
	udelay(ISSP_RESET_PULSE_LENGTH);
	pin_reset_hi(host);

	return 0;
}

int issp_get_checksum(struct issp_host *host, uint16_t *checksum)
{
	int ret;

	send_vector(host, vec_checksum_setup, bits_checksum_setup);
	ret = wait_and_poll(host);
	if (ret)
		return ret;
	send_vector(host, vec_sync_enable, bits_sync_enable);

	send_vector(host, vec_read_checksum_v[0], bits_read_checksum_v[0]);
	generate_clocks(host, 2);
	*checksum = read_byte(host);
	*checksum <<= 8;
	send_vector(host, vec_read_checksum_v[1], bits_read_checksum_v[1]);
	generate_clocks(host, 2);
	*checksum |= read_byte(host);
	send_vector(host, vec_read_checksum_v[2], bits_read_checksum_v[2]);

	send_vector(host, vec_sync_disable, bits_sync_disable);

	return 0;
}

int issp_program(struct issp_host *host)
{
	int i, ret;

	ret = issp_erase(host);
	if (ret) {
		dev_err(&host->pdev->dev, "Erase failed!\n");
		return ret;
	}

	issp_fw_rewind(host);
	for (i = 0; i < host->pdata->blocks; i++) {
		ret = issp_program_block(host, i);
		if (ret) {
			dev_err(&host->pdev->dev,
				"Program block %d failed!\n", i);
			return ret;
		}
	}

	issp_fw_rewind(host);
	for (i = 0; i < host->pdata->blocks; i++) {
		ret = issp_verify_block(host, i);
		if (ret) {
			dev_err(&host->pdev->dev,
				"Verify block %d failed!\n", i);
			return ret;
		}
	}

	issp_fw_seek_security(host);
	ret = issp_set_security(host);
	if (ret) {
		dev_err(&host->pdev->dev, "Set security failed!\n");
		return ret;
	}

	issp_fw_seek_security(host);
	ret = issp_verify_security(host);
	if (ret) {
		dev_err(&host->pdev->dev, "Verify security failed!\n");
		return ret;
	}

	ret = issp_verify_checksum(host);
	if (ret) {
		dev_err(&host->pdev->dev, "Verify checksum failed!\n");
		return ret;
	}

	return 0;
}

int issp_read_block(struct issp_host *host, uint8_t block_idx, uint8_t addr,
			uint8_t *buf, int len)
{
	int blk_size, i;
	int ret;

	blk_size = host->pdata->block_size;
	len = len + addr > blk_size ? blk_size - addr : len;
	if (!len)
		return 0;

	ret = issp_block_verify_setup(host, block_idx);
	if (ret)
		return ret;

	ret = issp_check_status(host);
	if (ret)
		return ret;

	send_vector(host, vec_sync_enable, bits_sync_enable);
	send_vector(host, vec_read_write_setup, bits_read_write_setup);

	for (i = 0; i < len; i++)
		buf[i] = issp_read_byte_at(host, addr++);

	send_vector(host, vec_sync_disable, bits_sync_disable);

	return len;
}