aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/phone_svn/ipc_spi/spi_test.c
blob: 31f717f4e2e8a0401dd815e4cdde358228ede46c (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
#include "spi_main.h"
#include "spi_test.h"
#include "spi_os.h"
#include "spi_dev.h"
#include "spi_app.h"

enum SPI_TEST_SCENARIO_T spi_test_scenario_mode = SPI_TEST_SCENARIO_NONE;

static void _scenario_physical(void);
static void _scenario_sending(int param);


static void _scenario_physical(void)
{
	int count = 0;
	int failcount = 0;
	int i;
	char txbuf[SPI_DEV_MAX_PACKET_SIZE];
	char rxbuf[SPI_DEV_MAX_PACKET_SIZE];

	SPI_OS_TRACE_MID(("spi_scenario_physical\n"));

	spi_os_memset(txbuf, 0x00, sizeof(txbuf));
	for (i = 0 ; i < SPI_DEV_MAX_PACKET_SIZE ; i++)
		txbuf[i] = i%100;

	while (count < 100) {
		SPI_OS_TRACE_MID(("spi _scenario_physical %d\n", count));
		SPI_OS_TRACE_MID(("%s mrdy %d srdy %d submrdy %d subsrdy %d\n",
			"spi_scenario_physical test start",
			spi_dev_get_gpio(spi_dev_gpio_mrdy),
			spi_dev_get_gpio(spi_dev_gpio_srdy),
			spi_dev_get_gpio(spi_dev_gpio_submrdy),
			spi_dev_get_gpio(spi_dev_gpio_subsrdy)));

		txbuf[0] = count;
		spi_os_memset(rxbuf, 0x00, sizeof(rxbuf));

#ifdef SPI_FEATURE_MASTER
		spi_dev_set_gpio(spi_dev_gpio_mrdy, SPI_DEV_GPIOLEVEL_LOW);
		spi_os_sleep(SPI_FEATURE_TEST_DURATION);
		spi_dev_set_gpio(spi_dev_gpio_submrdy, SPI_DEV_GPIOLEVEL_LOW);
		spi_os_sleep(SPI_FEATURE_TEST_DURATION);

		SPI_OS_TRACE_MID(("%s, subsrdy high\n",
			"spi_scenario_physical test wait srdy"));
		spi_dev_set_gpio(spi_dev_gpio_mrdy, SPI_DEV_GPIOLEVEL_HIGH);

		while (spi_dev_get_gpio(spi_dev_gpio_srdy) ==
			SPI_DEV_GPIOLEVEL_LOW)
			;

		/* spreadtrum recommend. */
		/* master should send/receive after that slave is ready. */
		spi_os_sleep(20);

		if (count % 2 == 0) {
			spi_dev_send(txbuf, SPI_DEV_MAX_PACKET_SIZE);
		} else {
			spi_dev_receive(rxbuf, SPI_DEV_MAX_PACKET_SIZE);
			for (i = 1 ; i < SPI_DEV_MAX_PACKET_SIZE ; i++) {
				if (rxbuf[i] != txbuf[i]) {
					failcount++;
		spi_os_trace_dump("spi_scenario_physical receiving fail",
						&rxbuf[i-8], 16);
					SPI_OS_TRACE_MID(("%s %d count %d/%d\n",
				"spi_scenario_physical test receiving fail",
						i, failcount, count));
					i = sizeof(rxbuf);
					break;
				}
			}
		}

		spi_os_sleep(20);
		spi_dev_set_gpio(spi_dev_gpio_submrdy, SPI_DEV_GPIOLEVEL_HIGH);

		spi_os_sleep(SPI_FEATURE_TEST_DURATION);

#elif defined SPI_FEATURE_SLAVE
		spi_dev_set_gpio(spi_dev_gpio_srdy, SPI_DEV_GPIOLEVEL_LOW);
		spi_os_sleep(SPI_FEATURE_TEST_DURATION);
		spi_dev_set_gpio(spi_dev_gpio_subsrdy, SPI_DEV_GPIOLEVEL_LOW);
		spi_os_sleep(SPI_FEATURE_TEST_DURATION);

		while (spi_dev_get_gpio(spi_dev_gpio_mrdy) ==
			SPI_DEV_GPIOLEVEL_LOW)
			;

		spi_dev_set_gpio(spi_dev_gpio_srdy, SPI_DEV_GPIOLEVEL_HIGH);

		if (count % 2 == 0) {
			spi_dev_receive(rxbuf, SPI_DEV_MAX_PACKET_SIZE);
			for (i = 1 ; i < SPI_DEV_MAX_PACKET_SIZE ; i++) {
				if (rxbuf[i] != txbuf[i]) {
					failcount++;
			spi_os_trace_dump("spi_scenario_phy rx fail",
						&rxbuf[i-8], 16);
					SPI_OS_TRACE_MID(("%s %d count %d/%d\n",
						"spi_scenario_physical test receiving fail",
						i, failcount, count));
					i = sizeof(rxbuf);
					break;
				}
			}
		} else
			spi_dev_send(txbuf, SPI_DEV_MAX_PACKET_SIZE);

		spi_os_sleep(SPI_FEATURE_TEST_DURATION);

#endif
		count++;
		SPI_OS_TRACE_MID(("%s %d/%d\n",
			"spi_scenario_physical test receiving result count",
			failcount, count));
	}
}


SPI_OS_TIMER_CALLBACK(spi_test_timer_callback)
{
	SPI_OS_TRACE_MID(("spi_test_timer_callback\n"));
	_scenario_sending(0);
}

static char tempdata1[135]; /* temp code. becuase can not allocate */
static char tempdata2[367]; /* temp code. becuase can not allocate */
static char tempdata3[1057]; /* temp code. becuase can not allocate */
static char tempdata4[35]; /* temp code. becuase can not allocate */
static char tempdata5[2079]; /* temp code. becuase can not allocate */
static char tempdata6[200]; /* temp code. becuase can not allocate */
static char tempdata7[2052]; /* temp code. becuase can not allocate */

static void _scenario_sending(int param)
{
	#define NB_COUNT 50
	#define NB_STEP 7
	static int step;
	static int duration;
	static int count;
	static void *timer_id;

	char *data = NULL;
	int i, value;
	struct DATABYSTEP_T {
		SPI_MAIN_MSG_T type;
		unsigned int size;
		char *buf;
	} databystep[NB_STEP] = {
				{SPI_MAIN_MSG_IPC_SEND, 135, tempdata1},
				{SPI_MAIN_MSG_IPC_SEND, 187, tempdata2},
				{SPI_MAIN_MSG_RAW_SEND, 1057, tempdata3},
				{SPI_MAIN_MSG_IPC_SEND, 35, tempdata4},
				{SPI_MAIN_MSG_RAW_SEND, 2079, tempdata5},
				{SPI_MAIN_MSG_IPC_SEND, 100, tempdata6},
				{SPI_MAIN_MSG_RAW_SEND, 2052, tempdata7}
				};

	if (spi_test_scenario_mode == SPI_TEST_SCENARIO_SLAVE_SENDING)
		data = databystep[step].buf;

	/* param is 0 to fix duration. */
	/* call this function with param to change timer duration. */
	if (param != 0)
		duration = param;

	if (spi_test_scenario_mode != SPI_TEST_SCENARIO_SLAVE_SENDING)
		data = (char *)spi_os_malloc(databystep[step].size);

	spi_os_memset(data, 0x00, databystep[step].size);

	SPI_OS_TRACE_MID(("spi _scenario_sending step %d\n", step));

	/* generate data to send, It is serial number from 0 to 99 */
	for (i = 0 ; i < databystep[step].size ; i++)
		data[i] = i%100;

	do {
#ifdef SPI_FEATURE_OMAP4430
		if (spi_is_ready() != 0) {
			struct spi_os_msg *msg;
			SPI_MAIN_MSG_T signal_code;
			signal_code = databystep[step].type;

			msg = (struct spi_os_msg *)
				spi_os_malloc(sizeof(struct spi_os_msg));
			msg->signal_code = signal_code;
			msg->data_length = databystep[step].size;
			msg->data = data;

			spi_receive_msg_from_app(msg);
			spi_main_send_signal(signal_code);
			break;
		} else
			spi_os_sleep(50);
#elif defined SPI_FEATURE_SC8800G
		if (app_send_data_to_spi(databystep[step].type,
				data, databystep[step].size) == 0)
			/* send fail */
			spi_os_sleep(50);
		else
			/* send success */
			break;
#endif
	} while (1);

	if (spi_test_scenario_mode == SPI_TEST_SCENARIO_MASTER_SENDING)
		return;

	step++;
	count++;
	step %= NB_STEP;

	if (timer_id == 0)
		timer_id = spi_os_create_timer("spi_test_timer",
			spi_test_timer_callback, step, duration);

	if (timer_id == 0) {
		SPI_OS_TRACE_MID(("spi _scenario_sending invalid timer id\n"));
		return;
	}

	SPI_OS_TRACE_MID(("spi _scenario_sending timer %x\n", timer_id));
	SPI_OS_TRACE_MID(("spi _scenario_sending start timer count %d\n",
		count));

	if (count == NB_COUNT) {
		spi_os_stop_timer(timer_id);
		return;
	}

	value = spi_os_start_timer(timer_id, spi_test_timer_callback,
		step, duration);
	SPI_OS_TRACE_MID(("spi _scenario_sending start timer%d\n", value));

	#undef NB_STEP
}


void spi_test_run(enum SPI_TEST_SCENARIO_T scenario, int param)
{
	SPI_OS_TRACE_MID(("spi_test_run %d\n", (int) scenario));

	if (scenario == SPI_TEST_SCENARIO_NONE)
			return;

	spi_test_scenario_mode = scenario;

	switch ((int)scenario) {
	case SPI_TEST_SCENARIO_PHYSICAL:
		_scenario_physical();
		break;
	case SPI_TEST_SCENARIO_MASTER_SENDING:
#ifdef SPI_FEATURE_MASTER
		_scenario_sending(param);
#endif
		break;
	case SPI_TEST_SCENARIO_SLAVE_SENDING:
#ifdef SPI_FEATURE_SLAVE
		_scenario_sending(param);
#endif
		break;
	case SPI_TEST_SCENARIO_COMPLEX_SENDING:
		_scenario_sending(param);
		break;
	case SPI_TEST_SCENARIO_NONE:
	default:
		break;
	}
}