aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/sensorhub/ssp_firmware.c
blob: 62e5efe4843ff33ecaefc6f2fd769060ba1449b0 (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
/*
 *  Copyright (C) 2012, Samsung Electronics Co. Ltd. All Rights Reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 */
#include "ssp.h"

#define SSP_FIRMWARE_REVISION		92800

/* Bootload mode cmd */
#define BL_FW_NAME			"ssp.fw"
#define BL_CRASHED_FW_NAME		"ssp_crashed.fw"

#define APP_SLAVE_ADDR			0x18
#define BOOTLOADER_SLAVE_ADDR		0x26

/* Bootloader mode status */
#define BL_WAITING_BOOTLOAD_CMD		0xc0	/* valid 7 6 bit only */
#define BL_WAITING_FRAME_DATA		0x80	/* valid 7 6 bit only */
#define BL_FRAME_CRC_CHECK		0x02
#define BL_FRAME_CRC_FAIL		0x03
#define BL_FRAME_CRC_PASS		0x04
#define BL_APP_CRC_FAIL			0x40	/* valid 7 8 bit only */
#define BL_BOOT_STATUS_MASK		0x3f

/* Command to unlock bootloader */
#define BL_UNLOCK_CMD_MSB		0xaa
#define BL_UNLOCK_CMD_LSB		0xdc

unsigned int get_module_rev(void)
{
	return SSP_FIRMWARE_REVISION;
}

static int check_bootloader(struct i2c_client *client, unsigned int uState)
{
	u8 uVal;
	u8 uTemp;

recheck:
	if (i2c_master_recv(client, &uVal, 1) != 1)
		return -EIO;

	if (uVal & 0x20) {
		if (i2c_master_recv(client, &uTemp, 1) != 1) {
			pr_err("[SSP]: %s - i2c recv fail\n", __func__);
			return -EIO;
		}

		if (i2c_master_recv(client, &uTemp, 1) != 1) {
			pr_err("[SSP]: %s - i2c recv fail\n", __func__);
			return -EIO;
		}

		uVal &= ~0x20;
	}

	if ((uVal & 0xF0) == BL_APP_CRC_FAIL) {
		pr_info("[SSP] SSP_APP_CRC_FAIL - There is no bootloader.\n");
		if (i2c_master_recv(client, &uVal, 1) != 1) {
			pr_err("[SSP]: %s - i2c recv fail\n", __func__);
			return -EIO;
		}

		if (uVal & 0x20) {
			if (i2c_master_recv(client, &uTemp, 1) != 1) {
				pr_err("[SSP]: %s - i2c recv fail\n", __func__);
				return -EIO;
			}

			if (i2c_master_recv(client, &uTemp, 1) != 1) {
				pr_err("[SSP]: %s - i2c recv fail\n", __func__);
				return -EIO;
			}

			uVal &= ~0x20;
		}
	}

	switch (uState) {
	case BL_WAITING_BOOTLOAD_CMD:
	case BL_WAITING_FRAME_DATA:
		uVal &= ~BL_BOOT_STATUS_MASK;
		break;
	case BL_FRAME_CRC_PASS:
		if (uVal == BL_FRAME_CRC_CHECK)
			goto recheck;
		break;
	default:
		return -EINVAL;
	}

	if (uVal != uState) {
		pr_err("[SSP]: %s - Unvalid bootloader mode state\n", __func__);
		return -EINVAL;
	}

	return 0;
}

static int unlock_bootloader(struct i2c_client *client)
{
	u8 uBuf[2];

	uBuf[0] = BL_UNLOCK_CMD_LSB;
	uBuf[1] = BL_UNLOCK_CMD_MSB;

	if (i2c_master_send(client, uBuf, 2) != 2) {
		pr_err("[SSP]: %s - i2c send failed\n", __func__);
		return -EIO;
	}

	return 0;
}

static int fw_write(struct i2c_client *client,
	const u8 *pData, unsigned int uFrameSize)
{
	if (i2c_master_send(client, pData, uFrameSize) != uFrameSize) {
		pr_err("[SSP]: %s - i2c send failed\n", __func__);
		return -EIO;
	}

	return 0;
}

static int load_fw_bootmode(struct i2c_client *client, const char *pFn)
{
	const struct firmware *fw = NULL;
	unsigned int uFrameSize;
	unsigned int uPos = 0;
	int iRet;
	int iCheckFrameCrcError = 0;
	int iCheckWatingFrameDataError = 0;

	pr_info("[SSP] ssp_load_fw start!!!\n");

	iRet = request_firmware(&fw, pFn, &client->dev);
	if (iRet) {
		pr_err("[SSP]: %s - Unable to open firmware %s\n",
			__func__, pFn);
		return iRet;
	}

	/* Unlock bootloader */
	unlock_bootloader(client);

	while (uPos < fw->size) {
		iRet = check_bootloader(client, BL_WAITING_FRAME_DATA);
		if (iRet) {
			iCheckWatingFrameDataError++;
			if (iCheckWatingFrameDataError > 10) {
				pr_err("[SSP]: %s - F/W update fail\n",
					__func__);
				goto out;
			} else {
				pr_err("[SSP]: %s - F/W data_error %d, retry\n",
					__func__, iCheckWatingFrameDataError);
				continue;
			}
		}

		uFrameSize = ((*(fw->data + uPos) << 8) |
			*(fw->data + uPos + 1));

		/* We should add 2 at frame size as the the firmware data is not
		*  included the CRC bytes.
		*/
		uFrameSize += 2;

		/* Write one frame to device */
		fw_write(client, fw->data + uPos, uFrameSize);
		iRet = check_bootloader(client, BL_FRAME_CRC_PASS);
		if (iRet) {
			iCheckFrameCrcError++;
			if (iCheckFrameCrcError > 10) {
				pr_err("[SSP]: %s - F/W Update Fail. crc err\n",
					__func__);
				goto out;
			} else {
				pr_err("[SSP]: %s - F/W crc_error %d, retry\n",
					__func__, iCheckFrameCrcError);
				continue;
			}
		}

		uPos += uFrameSize;

		pr_info("[SSP] Updated %d bytes / %zd bytes\n", uPos, fw->size);
		mdelay(1);
	}

out:
	release_firmware(fw);
	return iRet;
}

static void change_to_bootmode(struct ssp_data *data)
{
	int iCnt = 0;

	for (iCnt = 0; iCnt < 10; iCnt++) {
		data->set_mcu_reset(0);
		udelay(10);

		data->set_mcu_reset(1);
		msleep(100);
	}

	msleep(50);
}

void toggle_mcu_reset(struct ssp_data *data)
{
	u8 uBuf[2];

	uBuf[0] = 0x00;
	uBuf[1] = 0x00;

	data->set_mcu_reset(0);
	udelay(10);
	data->set_mcu_reset(1);
	mdelay(50);

	data->client->addr = BOOTLOADER_SLAVE_ADDR;

	if (i2c_master_send(data->client, uBuf, 2) != 2)
		pr_err("[SSP]: %s - ssp_Normal Mode\n", __func__);
	else
		pr_err("[SSP]: %s - ssp_load_fw_bootmode\n", __func__);

	data->client->addr = APP_SLAVE_ADDR;
}

int update_mcu_bin(struct ssp_data *data)
{
	int iRet = 0;

	pr_info("[SSP] ssp_change_to_bootmode\n");
	change_to_bootmode(data);
	data->client->addr = BOOTLOADER_SLAVE_ADDR;
	iRet = load_fw_bootmode(data->client, BL_FW_NAME);

	msleep(SSP_SW_RESET_TIME);

	data->client->addr = APP_SLAVE_ADDR;

	if (iRet < 0)
		data->bSspShutdown = true;
	else
		data->bSspShutdown = false;

	return iRet;
}

int update_crashed_mcu_bin(struct ssp_data *data)
{
	int iRet = 0;
	pr_info("[SSP] ssp_change_to_bootmode\n");
	change_to_bootmode(data);
	data->client->addr = BOOTLOADER_SLAVE_ADDR;
	iRet = load_fw_bootmode(data->client, BL_CRASHED_FW_NAME);

	msleep(SSP_SW_RESET_TIME);

	data->client->addr = APP_SLAVE_ADDR;
	data->bSspShutdown = true;
	return iRet;
}

void check_fwbl(struct ssp_data *data)
{
	int iRet;

	data->client->addr = BOOTLOADER_SLAVE_ADDR;
	iRet = check_bootloader(data->client, BL_WAITING_BOOTLOAD_CMD);

	if (iRet >= 0) {
		pr_info("[SSP] ssp_load_fw_bootmode\n");
		load_fw_bootmode(data->client, BL_FW_NAME);
		msleep(SSP_SW_RESET_TIME);
	} else {
		data->client->addr = APP_SLAVE_ADDR;
		data->uCurFirmRev = get_firmware_rev(data);
		if (data->uCurFirmRev != SSP_FIRMWARE_REVISION) {
			pr_info("[SSP] MPU Firm Rev. : Old = %8u, New = %8u\n",
				data->uCurFirmRev, SSP_FIRMWARE_REVISION);
			update_mcu_bin(data);
		}
	}

	data->client->addr = APP_SLAVE_ADDR;
	data->uCurFirmRev = get_firmware_rev(data);
	pr_info("[SSP] MPU Firm Rev. : Old = %8u, New = %8u\n",
		data->uCurFirmRev, SSP_FIRMWARE_REVISION);
}