summaryrefslogtreecommitdiff
path: root/drivers/mfd/m4sensorhub-stm32_401-fw.c
blob: 2779c07256f3bf087306423cdd7aa8a7a47051bd (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
787
788
789
790
791
792
793
/*
 * Copyright (C) 2013-2014 Motorola Mobility LLC.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307, USA
 */

#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/m4sensorhub.h>
#include <linux/slab.h>
/* --------------- Global Declarations -------------- */

/* ------------ Local Function Prototypes ----------- */
static int m4sensorhub_bl_ack(struct m4sensorhub_data *m4sensorhub);
static int m4sensorhub_bl_rm(struct m4sensorhub_data *m4sensorhub,
	int start_address, u8 *data, int len);
static int m4sensorhub_bl_go(struct m4sensorhub_data *m4sensorhub,
	int start_address);
static int m4sensorhub_bl_wm(struct m4sensorhub_data *m4sensorhub,
	int start_address, u8 *data, int len);
static int m4sensorhub_bl_erase_fw(struct m4sensorhub_data *m4sensorhub);
static int m4sensorhub_jump_to_user(struct m4sensorhub_data *m4sensorhub);

/* --------------- Local Declarations -------------- */

#define ACK		((uint8_t) (0x79))
#define NACK	((uint8_t) (0x1F))
#define BUSY    ((uint8_t) (0x76))

/* LIST OF OPCODES FOR BOOTLOADER */
/* opcode_get */
#define OPC_GET	((uint8_t) (0x00))
/* opcode_get_version */
#define OPC_GV	((uint8_t) (0x01))
/* opcode_get_id */
#define OPC_GID	((uint8_t) (0x02))
/* opcode_read_memory */
#define OPC_RM	((uint8_t) (0x11))
/* opcode_go */
#define OPC_GO	((uint8_t) (0x21))
/* opcode_write_memory */
#define OPC_WM	((uint8_t) (0x31))
/* opcode_no_stretch_write_memory */
#define OPC_NO_STRETCH_WM	((uint8_t) (0x32))
/* opcode_erase */
#define OPC_ER	((uint8_t) (0x44))
/* opcode_no_stretch_erase */
#define OPC_NO_STRETCH_ER	((uint8_t) (0x45))
/* opcode_write_protect */
#define OPC_WP	((uint8_t) (0x63))
/* opcode_write_unprotect */
#define OPC_WU	((uint8_t) (0x73))
/* opcode_readout_protect */
#define OPC_RP	((uint8_t) (0x82))
/* opcode_readout_unprotect */
#define OPC_RU	((uint8_t) (0x92))
/* opcode_no_stretch_readout_unprotect */
#define OPC_NO_STRETCH_RU	((uint8_t) (0x93))

/* We flash code in sector 6 and sector 7 */
#define USER_FLASH_FIRST_PAGE_ADDRESS	0x08040000
#define VERSION_OFFSET  0x200
#define VERSION_ADDRESS (USER_FLASH_FIRST_PAGE_ADDRESS + VERSION_OFFSET)

/* Sector 7 ends at 0x0807FFFF, and we put 4 byte barker */
/* at the end of Sector 7, giving BARKER_ADDRESS as (0x0807FFFF - 0x4 + 0x1) */
#define BARKER_ADDRESS	0x0807FFFC
#define BARKER_NUMBER	0xACEC0DE
/* The MAX_FILE_SIZE is the size of sectors 6 and 7  where the firmware code
 * will reside (minus the barker size).

 * From the Flash Programming Manual:
  Sector  Start Address End Address Size
  ------  ------------- ----------------
  0       0x0800 0000   0x0800 3FFF 16 Kbytes
  1       0x0800 4000   0x0800 7FFF 16 Kbytes
  2       0x0800 8000   0x8000 BFFF 16 Kbytes
  3       0x0800 C000   0x0800 FFFF 16 Kbytes
  4       0x0801 0000   0x0801 FFFF 64 Kbytes
  5       0x0802 0000   0x0803 FFFF 128 Kbytes
  6       0x0804 0000   0x0805 FFFF 128 Kbytes
  7       0x0806 0000   0x0807 FFFF 128 Kbytes

 */
/* Max file size is 128Kb + 128 Kb - 4 bytes for barker */
#define MAX_FILE_SIZE	(128*1024+128*1024-4) /* bytes */
/* Transfer is done in 256 bytes, or if data is less than 256 bytes, then
data is 16 bit aligned and sent out */
#define MAX_TRANSFER_SIZE	256 /* bytes */
/* If M4 returns back BUSY, then we give it 60 chances to execute the command
each attempt to check status is delayed by 100 ms, thus 60 attempts give it a
window of about 6 seconds. */
#define MAX_ATTEMPTS 60
/* We are flashing/erasing 2 sectors */
#define NUM_FLASH_TO_ERASE 2
#define SECTOR_TO_ERASE_1 6
#define SECTOR_TO_ERASE_2 7
int page_to_erase[2] = {SECTOR_TO_ERASE_1, SECTOR_TO_ERASE_2};
/* -------------- Local Data Structures ------------- */


/* -------------- Global Functions ----------------- */

/* m4sensorhub_401_load_firmware()
   This function uses I2C bootloader version 1.1 on M4
   Check firmware and load if different from what's already on the M4.
   Then jump to user code on M4.

   Returns 0 on success or negative error code on failure

     m4sensorhub - pointer to the main m4sensorhub data struct
*/

int m4sensorhub_401_load_firmware(struct m4sensorhub_data *m4sensorhub,
	unsigned short force_upgrade, const struct firmware *fm)
{
	const struct firmware *firmware = fm;
	int ret = 0;
	int bytes_left, bytes_to_write;
	int address_to_write;
	u8 *buf_to_read, *buf = NULL;
	u16 fw_version_file, fw_version_device;
	u32 barker_read_from_device;

	if (m4sensorhub == NULL) {
		KDEBUG(M4SH_ERROR, "%s: M4 data is NULL\n", __func__);
		ret = -ENODATA;
		goto done;
	}

	m4sensorhub_hw_reset(m4sensorhub);

	buf = kzalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
	if (!buf) {
		KDEBUG(M4SH_ERROR, "%s: Failed to allocate buf\n", __func__);
		ret = -ENOMEM;
		goto done;
	}

	if (firmware == NULL) {
		ret = request_firmware(&firmware, m4sensorhub->filename,
			&m4sensorhub->i2c_client->dev);
	}
	if (ret < 0) {
		KDEBUG(M4SH_ERROR, "%s: request_firmware failed for %s\n",
				__func__, m4sensorhub->filename);
		KDEBUG(M4SH_ERROR, "Trying to run firmware already on hw.\n");
		ret = 0;
		goto done;
	}

	KDEBUG(M4SH_INFO, "%s: Filename = %s", __func__, m4sensorhub->filename);

	if (firmware->size > MAX_FILE_SIZE) {
		KDEBUG(M4SH_ERROR, "%s: firmware file size is too big.\n",
			__func__);
		ret = -EINVAL;
		goto done;
	}

	fw_version_file = *(u16 *)(firmware->data +
		VERSION_ADDRESS - USER_FLASH_FIRST_PAGE_ADDRESS);

	if (!force_upgrade) {
		/* Verify Barker number from device */
		if (m4sensorhub_bl_rm(m4sensorhub, BARKER_ADDRESS,
				(u8 *)&barker_read_from_device,
				sizeof(barker_read_from_device)) != 0) {
			KDEBUG(M4SH_ERROR, "%s : %d : error reading Barker\n",
				__func__, __LINE__);
			ret = -EINVAL;
			goto done;
		}

		if (barker_read_from_device != BARKER_NUMBER) {
			KDEBUG(M4SH_NOTICE,
				"Barker Number read 0x%8x does not match 0x%8x\n",
				barker_read_from_device, BARKER_NUMBER);
			KDEBUG(M4SH_NOTICE,
				"forcing firmware update from file\n");
			/*
			 * This is likely a blank flash factory case, so
			 * skip doing any driver pre-flash callbacks.
			 */
			goto m4sensorhub_401_load_firmware_erase_flash;
		} else {
			/* Read firmware version from device */
			if (m4sensorhub_bl_rm(m4sensorhub, VERSION_ADDRESS,
					(u8 *)&fw_version_device,
					sizeof(fw_version_device)) != 0) {
				KDEBUG(M4SH_ERROR, "%s : %d : Read err\n",
					__func__, __LINE__);
				ret = -EINVAL;
				goto done;
			}

			if (fw_version_file == fw_version_device) {
				KDEBUG(M4SH_NOTICE,
					"Version of firmware on device is 0x%04x\n",
					fw_version_device);
				KDEBUG(M4SH_NOTICE,
					"Firmware on device same as file, not loading firmware.\n");
				goto done;
			}
			/* Print statement below isn't really an ERROR, but
			 * this ensures it is always printed */
			KDEBUG(M4SH_ERROR,
				"Version of firmware on device is 0x%04x\n",
				fw_version_device);
			KDEBUG(M4SH_ERROR,
				"Version of firmware on file is 0x%04x\n",
				fw_version_file);
			KDEBUG(M4SH_ERROR,
				"Firmware on device different from file, updating...\n");
		}
	} else {
		KDEBUG(M4SH_NOTICE, "Version of firmware on file is 0x%04x\n",
			fw_version_file);
		/*
		 * Currently no code uses the force_upgrade path,
		 * but in case it is used for some recovery (and because
		 * no numbers or error checking is done), we will skip
		 * trying to call any driver pre-flash callbacks.
		 */
		goto m4sensorhub_401_load_firmware_erase_flash;
	}

	/* Boot M4, execute any pre-flash callbacks, then reset for BL mode */
	if (m4sensorhub_preflash_callbacks_exist()) {
		KDEBUG(M4SH_ERROR, "%s: Booting M4 to execute callbacks...\n",
			__func__); /* Not an error (see similar above) */
		ret = m4sensorhub_jump_to_user(m4sensorhub);
		if (ret < 0) {
			KDEBUG(M4SH_ERROR, "%s: %s %s %d.\n", __func__,
				"Failed to boot M4 for callbacks",
				"with error code", ret);
			/*
			 * Since we don't know the status of M4 at this point,
			 * we will skip any callbacks, reset the IC to a known
			 * state, and go ahead with a reflash.
			 */
			m4sensorhub_hw_reset(m4sensorhub);
			goto m4sensorhub_401_load_firmware_erase_flash;
		}

		m4sensorhub_call_preflash_callbacks();
		KDEBUG(M4SH_ERROR, "%s: Callbacks complete, flashing M4...\n",
			__func__); /* Not an error (see similar above) */
		m4sensorhub_hw_reset(m4sensorhub);
	}

	/* The flash memory to update has to be erased before updating */
m4sensorhub_401_load_firmware_erase_flash:
	ret = m4sensorhub_bl_erase_fw(m4sensorhub);
	if (ret < 0) {
		pr_err("%s: erase failed\n", __func__);
		/* TODO : Not sure if this is critical error
		goto done; */
	}

	bytes_left = firmware->size;
	address_to_write = USER_FLASH_FIRST_PAGE_ADDRESS;
	buf_to_read = (u8 *)firmware->data;

	KDEBUG(M4SH_DEBUG, "%s: %d bytes to be written\n", __func__,
		firmware->size);

	while (bytes_left) {
		if (bytes_left > MAX_TRANSFER_SIZE)
			bytes_to_write = MAX_TRANSFER_SIZE;
		else
			bytes_to_write = bytes_left;

		if (m4sensorhub_bl_wm(m4sensorhub, address_to_write,
				buf_to_read, bytes_to_write) != 0) {
			KDEBUG(M4SH_ERROR, "%s : %d : error writing %d\n",
				__func__, __LINE__, address_to_write);
			ret = -EINVAL;
			goto done;
		}

		/* Relying on CRC to take care of errors */
		address_to_write += bytes_to_write;
		buf_to_read += bytes_to_write;
		bytes_left -= bytes_to_write;
	}

	/* Write barker number when firmware successfully written */
	buf[0] = BARKER_NUMBER & 0xFF;
	buf[1] = (BARKER_NUMBER >> 8) & 0xFF;
	buf[2] = (BARKER_NUMBER >> 16) & 0xFF;
	buf[3] = (BARKER_NUMBER >> 24) & 0xFF;
	if (m4sensorhub_bl_wm(m4sensorhub, BARKER_ADDRESS,
		buf, 4) != 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : error writing\n",
			__func__, __LINE__);
		ret = -EINVAL;
		goto done;
	}

	KDEBUG(M4SH_NOTICE, "%s: %d bytes written successfully\n",
		__func__, firmware->size);

	ret = 0;

done:
	release_firmware(firmware);

	/* irrespective of whether we upgraded FW or not,
	the firmware version will be the same as one in the file*/
	m4sensorhub->fw_version = fw_version_file;

	/* If ret is invalid, then we don't try to jump to user code */
	if (ret >= 0 && m4sensorhub_jump_to_user(m4sensorhub) < 0)
		/* If jump to user code fails, return failure */
		ret = -EINVAL;

	kfree(buf);

	return ret;
}
EXPORT_SYMBOL_GPL(m4sensorhub_401_load_firmware);

/* TODO: Restructure reflash code so that this function can go in the core */
int m4sensorhub_test_m4_reboot(struct m4sensorhub_data *m4, bool reboot_first)
{
	int err = 0;
	int i;
	unsigned char buf[2];

	if (m4 == NULL) {
		KDEBUG(M4SH_ERROR, "%s: M4 data is missing.\n", __func__);
		err = -ENODATA;
		goto m4sensorhub_test_m4_reboot_exit;
	}

	for (i = 0; i < 3; i++) {
		if ((i > 0) || (reboot_first)) {
			m4sensorhub_hw_reset(m4);
			err = m4sensorhub_jump_to_user(m4);
			if (err < 0) {
				KDEBUG(M4SH_ERROR,
					"%s: M4 reboot failed (retries=%d)\n",
					__func__, i);
				continue;
			}
		}

		/* Wait progressively longer for M4 to become ready */
		if (i > 0)
			msleep(i * 100);

		/* Read M4 register to test if M4 is ready */
		buf[0] = 0x00; /* Bank */
		buf[1] = 0x00; /* Offset */
		err = m4sensorhub_i2c_write_read(m4, &(buf[0]), 2, 1);
		if (err < 0) {
			KDEBUG(M4SH_ERROR, "%s: %s (retries=%d).\n", __func__,
				"Failed initial I2C read", i);
			continue;
		} else {
			/* No failure so break loop */
			err = 0;
			break;
		}
	}

	if (err < 0)
		panic("%s: M4 has failed--forcing panic...\n", __func__);

m4sensorhub_test_m4_reboot_exit:
	return err;
}
EXPORT_SYMBOL_GPL(m4sensorhub_test_m4_reboot);

/* -------------- Local Functions ----------------- */
/* m4sensorhub_bl_ack()

   Receive next byte from M4 and wait to get either NACK or ACK.
   if M4 sent back ACK, then return 1, else return 0.

     m4sensorhub - pointer to the main m4sensorhub data struct
*/
static int m4sensorhub_bl_ack(struct m4sensorhub_data *m4sensorhub)
{
	u8 buf_ack = 0;
	int ret = 0;
	int timeout = 0;

	for (timeout = 0; timeout <= MAX_ATTEMPTS; timeout++) {
		if (m4sensorhub_i2c_write_read(m4sensorhub,
				&buf_ack, 0, 1) < 0) {
			KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
				__func__, __LINE__);
			break;
		}
		/* Poll for ACK or NACK if M4 says its BUSY */
		if (buf_ack == BUSY) {
			KDEBUG(M4SH_DEBUG, "%s : %d : got BUSY from M4\n",
				__func__, __LINE__);
			msleep(100);
		} else if (buf_ack == ACK) {
			KDEBUG(M4SH_DEBUG, "%s : %d : got ACK from M4\n",
				__func__, __LINE__);
			ret = 1;
			break;
		} else if (buf_ack == NACK) {
			break;
		} else {
			KDEBUG(M4SH_ERROR, "%s : %d : unexpected response\n",
				__func__, __LINE__);
			break;
		}
	}

	return ret;
}

/* m4sensorhub_bl_rm()

   Read data from M4 FLASH.

   Returns 0 on success or negative error code on failure

     m4sensorhub - pointer to the main m4sensorhub data struct
     start_address - address to read data from
     data - pointer of buffer to write read data
     len - length of data to read
*/
static int m4sensorhub_bl_rm(struct m4sensorhub_data *m4sensorhub,
	int start_address, u8 *data, int len)
{
	u8 buf[5] = {0};
	int num_bytes = 0;

	if ((data == NULL) || (len < 0) || (len > MAX_TRANSFER_SIZE)) {
		KDEBUG(M4SH_ERROR, "%s : %d : Invalid inputs\n",
			__func__, __LINE__);
		return -1;
	}

	buf[num_bytes++] = OPC_RM;
	buf[num_bytes++] = ~(OPC_RM);

	if (m4sensorhub_i2c_write_read(m4sensorhub, buf, num_bytes, 0) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		return -1;
	}

	if (!m4sensorhub_bl_ack(m4sensorhub)) {
		KDEBUG(M4SH_ERROR, "%s : %d : NACK received, command invalid\n",
			__func__, __LINE__);
		return -1;
	}

	num_bytes = 0;
	buf[num_bytes++] = (start_address >> 24) & 0xFF;
	buf[num_bytes++] = (start_address >> 16) & 0xFF;
	buf[num_bytes++] = (start_address >> 8) & 0xFF;
	buf[num_bytes++] = start_address & 0xFF;
	buf[num_bytes++] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3];

	if (m4sensorhub_i2c_write_read(m4sensorhub, buf, num_bytes, 0) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		return -1;
	}

	if (!m4sensorhub_bl_ack(m4sensorhub)) {
		KDEBUG(M4SH_ERROR, "%s : %d : NACK received, address invalid\n",
			__func__, __LINE__);
		return -1;
	}

	num_bytes = 0;
	buf[num_bytes++] = (len - 1);
	buf[num_bytes++] = (len - 1) ^ 0xFF;

	if (m4sensorhub_i2c_write_read(m4sensorhub, buf, num_bytes, 0) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		return -1;
	}

	if (!m4sensorhub_bl_ack(m4sensorhub)) {
		KDEBUG(M4SH_ERROR, "%s : %d : NACK received\n",
			__func__, __LINE__);
		return -1;
	}

	if (m4sensorhub_i2c_write_read(m4sensorhub, data, 0, len) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		return -1;
	}

	return 0;
}

/* m4sensorhub_bl_go()

   Cause M4 to jump to new address and start execution fresh.

   Returns 0 on success or negative error code on failure

     m4sensorhub - pointer to the main m4sensorhub data struct
     start_address - address to jump M4 code to
*/
static int m4sensorhub_bl_go(struct m4sensorhub_data *m4sensorhub,
	int start_address)
{
	u8 buf[5] = {0};
	int num_bytes = 0;

	buf[num_bytes++] = OPC_GO;
	buf[num_bytes++] = ~(OPC_GO);

	if (m4sensorhub_i2c_write_read(m4sensorhub, buf, num_bytes, 0) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		return -1;
	}

	if (!m4sensorhub_bl_ack(m4sensorhub)) {
		KDEBUG(M4SH_ERROR, "%s : %d : NACK received, command invalid\n",
			__func__, __LINE__);
		return -1;
	}

	num_bytes = 0;
	buf[num_bytes++] = (start_address >> 24) & 0xFF;
	buf[num_bytes++] = (start_address >> 16) & 0xFF;
	buf[num_bytes++] = (start_address >> 8) & 0xFF;
	buf[num_bytes++] = start_address & 0xFF;
	buf[num_bytes++] = buf[0] ^ buf[1] ^ buf[2] ^ buf[3];

	if (m4sensorhub_i2c_write_read(m4sensorhub, buf, num_bytes, 0) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		return -1;
	}

	if (!m4sensorhub_bl_ack(m4sensorhub)) {
		KDEBUG(M4SH_ERROR, "%s : %d : NACK received, address invalid\n",
			__func__, __LINE__);
		return -1;
	}

	return 0;
}

/* m4sensorhub_bl_wm()

   Write data to M4 FLASH.

   Returns 0 on success or negative error code on failure

     m4sensorhub - pointer to the main m4sensorhub data struct
     start_address - address to write data to
     data - pointer of data to write
     len - length of data to write, must be 16-bit aligned
*/
static int m4sensorhub_bl_wm(struct m4sensorhub_data *m4sensorhub,
	int start_address, u8 *data, int len)
{
	u8 buf[5] = {0};
	u8 *temp_buf = NULL;
	u8 checksum = 0;
	int i, retval = -1;
	int malloc_size = 0;
	int num_bytes = 0;

	if ((data == NULL) || (len < 0) || (len > MAX_TRANSFER_SIZE)) {
		KDEBUG(M4SH_ERROR, "%s : %d : Invalid inputs\n",
			__func__, __LINE__);
		goto done;
	}

	/* add 2 bytes for data, one for length and last for checksum
	as per spec, data is 16 bit aligned, padding of 0xFF if needed */
	if (len % 2)
		malloc_size = len + 1 + 2;
	else
		malloc_size = len + 2;


	temp_buf = kzalloc(malloc_size, GFP_KERNEL);
	if (!temp_buf) {
		pr_err("Can't allocate memory %s\n", __func__);
		retval = -ENOMEM;
		goto done;
	}

	buf[num_bytes++] = OPC_NO_STRETCH_WM;
	buf[num_bytes++] = ~(OPC_NO_STRETCH_WM);

	if (m4sensorhub_i2c_write_read(m4sensorhub, buf, num_bytes, 0) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		goto done;
	}

	if (!m4sensorhub_bl_ack(m4sensorhub)) {
		KDEBUG(M4SH_ERROR, "%s : %d : NACK received, command invalid\n",
			__func__, __LINE__);
		goto done;
	}

	num_bytes = 0;
	checksum = 0;

	buf[num_bytes++] = (start_address >> 24) & 0xFF;
	checksum = checksum ^ ((start_address >> 24) & 0xFF);
	buf[num_bytes++] = (start_address >> 16) & 0xFF;
	checksum = checksum ^ ((start_address >> 16) & 0xFF);
	buf[num_bytes++] = (start_address >> 8) & 0xFF;
	checksum = checksum ^ ((start_address >> 8) & 0xFF);
	buf[num_bytes++] = start_address & 0xFF;
	checksum = checksum ^ ((start_address) & 0xFF);
	buf[num_bytes++] = checksum;

	if (m4sensorhub_i2c_write_read(m4sensorhub, buf, num_bytes, 0) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		goto done;
	}

	if (!m4sensorhub_bl_ack(m4sensorhub)) {
		KDEBUG(M4SH_ERROR, "%s : %d : NACK received, address invalid\n",
			__func__, __LINE__);
		goto done;
	}


	checksum = 0;
	num_bytes = 0;
	/* (malloc_size -2 - 1) is the number of data bytes -1 to be sent */
	checksum = checksum ^ (malloc_size - 2 - 1);
	/* Calculate checksum of data */
	for (i = 0; i < len; i++)
		checksum ^= data[i];

	if (len % 2)
		checksum ^= 0xFF;

	temp_buf[num_bytes++] = malloc_size - 2 - 1;
	memcpy(&temp_buf[num_bytes], data, len);
	if (len % 2)
		temp_buf[len + 1] = 0xFF;
	temp_buf[malloc_size-1] = checksum;

	if (m4sensorhub_i2c_write_read(m4sensorhub, temp_buf,
			malloc_size, 0) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		goto done;
	}

	if (!m4sensorhub_bl_ack(m4sensorhub)) {
		KDEBUG(M4SH_ERROR, "%s : %d : NACK received, data invalid\n",
			__func__, __LINE__);
		goto done;
	}

	retval = 0;

done:
	kfree(temp_buf);

	return retval;
}

/* m4sensorhub_bl_erase_fw()

   Erases the M4 FLASH that is to be used for the firmware.

   Returns 0 on success or negative error code on failure

     m4sensorhub - pointer to the main m4sensorhub data struct
*/
static int m4sensorhub_bl_erase_fw(struct m4sensorhub_data *m4sensorhub)
{
	u8 buf[2*NUM_FLASH_TO_ERASE + 1] = {0};
	u8 checksum = 0;
	int i;
	int num_bytes = 0;

	buf[num_bytes++] = OPC_NO_STRETCH_ER;
	buf[num_bytes++] = ~(OPC_NO_STRETCH_ER);

	if (m4sensorhub_i2c_write_read(m4sensorhub, buf, num_bytes, 0) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		return -1;
	}

	if (!m4sensorhub_bl_ack(m4sensorhub)) {
		KDEBUG(M4SH_ERROR, "%s : %d : NACK received, command invalid\n",
			__func__, __LINE__);
		return -1;
	}

	num_bytes = 0;
	buf[num_bytes++] = ((NUM_FLASH_TO_ERASE - 1) >> 8) & 0xFF;
	buf[num_bytes++] = ((NUM_FLASH_TO_ERASE - 1) & 0xFF);
	buf[num_bytes++] = buf[0] ^ buf[1];

	if (m4sensorhub_i2c_write_read(m4sensorhub, buf, num_bytes,
			0) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		return -1;
	}

	if (!m4sensorhub_bl_ack(m4sensorhub)) {
		KDEBUG(M4SH_ERROR, "%s : %d : NACK received, command invalid\n",
			__func__, __LINE__);
		return -1;
	}

	checksum = 0;
	for (i = 0; i < NUM_FLASH_TO_ERASE; i++) {
		buf[2*i] = (page_to_erase[i] >> 8) & 0xFF;
		buf[2*i+1] = page_to_erase[i] & 0xFF;
		checksum ^= buf[2*i];
		checksum ^= buf[2*i+1];
	}
	buf[2*NUM_FLASH_TO_ERASE] = checksum;

	if (m4sensorhub_i2c_write_read(m4sensorhub, buf,
			(2*NUM_FLASH_TO_ERASE + 1), 0) < 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : I2C transfer error\n",
			__func__, __LINE__);
		return -1;
	}

	if (!m4sensorhub_bl_ack(m4sensorhub)) {
		KDEBUG(M4SH_ERROR, "%s : %d : NACK received, command invalid\n",
			__func__, __LINE__);
		return -1;
	}

	return 0;
}

/* m4sensorhub_jump_to_user()

   Jump to user code loaded in M4 FLASH.

   Returns 0 on success or negative error code on failure

     m4sensorhub - pointer to the main m4sensorhub data struct
*/
static int m4sensorhub_jump_to_user(struct m4sensorhub_data *m4sensorhub)
{
	int ret = -1;
	u32 barker_read_from_device = 0;

	if (m4sensorhub_bl_rm(m4sensorhub, BARKER_ADDRESS,
	    (u8 *)&barker_read_from_device, 4) != 0) {
		KDEBUG(M4SH_ERROR, "%s : %d : error reading from device\n",
		       __func__, __LINE__);
		KDEBUG(M4SH_ERROR, "*** Not executing M4 code ***\n");
		return -EIO;
	}

	if (barker_read_from_device == BARKER_NUMBER) {
		m4sensorhub_bl_go(m4sensorhub, USER_FLASH_FIRST_PAGE_ADDRESS);
		KDEBUG(M4SH_NOTICE, "Waiting for M4 setup\n");
		msleep(100);
		KDEBUG(M4SH_NOTICE, "Executing M4 code\n");
		ret = 0;
	} else {
		KDEBUG(M4SH_ERROR, "Wrong Barker Number read 0x%8x\n",
		       barker_read_from_device);
		KDEBUG(M4SH_ERROR, "*** Not executing M4 code ***\n");
	}

	return ret;
}