summaryrefslogtreecommitdiffstats
path: root/tests.c
blob: 3d89b678807a72a0f98138bd62062dc759db7cbd (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
/*
 * Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
 *
 * 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 3 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#define _GNU_SOURCE /* For asprintf */

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "compatible_devices.h"
#include "exynos4412_gpios.h"
#include "exynos4412_gpios_data.h"

#include "tests.h"

static int run_command(int debug, char *command)
{
	char *silent_command;
	int rc;

	if (debug) {
		return system(command);
	} else {
		rc = asprintf(&silent_command, "%s 1>/dev/null", command);
		if (rc == -1) {
			rc = errno;
			printf("%s: asprintf failed with error %d: %s\n",
			       __func__, rc, strerror(rc));
			assert(false);
		}

		rc = system(silent_command);

		free(silent_command);

		return rc;
	}
}

static int test_output_value_with_libgpiod(int debug, char *devmem, int fd,
					   size_t page_size, char *bank,
					   int gpio_offset)
{
	char *gpioset_high_command;
	char *gpioset_low_command;
	int rc;


	rc = asprintf(&gpioset_high_command, "gpioset %s %d=1", bank,
		      gpio_offset);
	if (rc == -1) {
		rc = errno;
		printf("%s: asprintf failed with error %d: %s\n",
		       __func__, rc, strerror(rc));
		assert(false);
	}

	rc = run_command(debug, gpioset_high_command);
	assert(rc == 0);

	free(gpioset_high_command);

	rc = gpio_get_direction(devmem, fd, page_size, bank, gpio_offset);
	assert (rc == GPIO_OUTPUT);
	rc = gpio_get_output_value(devmem, fd, page_size, bank, gpio_offset);
	assert (rc == GPIO_VALUE_HIGH);

	rc = asprintf(&gpioset_low_command, "gpioset %s %d=0", bank,
		      gpio_offset);
	if (rc == -1) {
		rc = errno;
		printf("%s: asprintf failed with error %d: %s\n",
		       __func__, rc, strerror(rc));
		assert(false);
	}

	rc = run_command(debug, gpioset_low_command);
	assert(rc == 0);

	rc = gpio_get_direction(devmem, fd, page_size, bank, gpio_offset);
	assert (rc == GPIO_OUTPUT);

	rc = gpio_get_output_value(devmem, fd, page_size, bank, gpio_offset);
	assert (rc == GPIO_VALUE_LOW);

	free(gpioset_low_command);

	return 0;
}

static int test_flash_with_devmem2(int debug, char *devmem, int fd,
				   size_t page_size)
{
	int rc;

	/* Default values at boot:
	 * +--------------+------------+----------+
	 * | Distribution | Address    | Value[1] |
	 * +--------------+------------+----------+
	 * | Parabola     | 0x11400260 | 0x2000   |
	 * | Parabola     | 0x11400264 | 0x0000   |
	 * +--------------+------------+----------+
	 * [1]obtained with devmem2 <address> w
	 */

	/* Set the GPIO to input */
	if (debug)
		printf("       Starting devmem2 GPIO input test\n");
	/* GPJ1CON[4:7] with bit 4 to 7 at 0 => 0xnn0n (n == don't care) */
	rc = run_command(debug, "devmem2 0x11400260 w 0x2000");
	assert(rc == 0);
	rc = gpio_get_direction(devmem, fd, page_size, "gpj1", 1);
	assert (rc == GPIO_INPUT);
	printf("[ OK ] devmem2 GPIO input test\n");

	/* Set the GPIO to output */
	if (debug)
		printf("       Starting devmem2 GPIO output test\n");
	/* GPJ1CON[4:7] with bit 1 @ 1 and the rest at 0 => 1<<5 == 0x20 */
	rc = run_command(debug, "devmem2 0x11400260 w 0x2020");
	assert(rc == 0);
	rc = gpio_get_direction(devmem, fd, page_size, "gpj1", 1);
	assert (rc == GPIO_OUTPUT);
	printf("[ OK ] devmem2 GPIO output test\n");

	/* Set the pin LOW */
	/* GPJ1DAT[1] == 0 */
	if (debug)
		printf("       Starting devmem2 GPIO output low test\n");
	rc = run_command(debug, "devmem2 0x11400264 w 0");
	assert(rc == 0);
	rc = gpio_get_output_value(devmem, fd, page_size, "gpj1", 1);
	assert (rc == GPIO_VALUE_LOW);
	printf("[ OK ] devmem2 GPIO output low test\n");

	/* Set the pin HIGH */
	/* GPJ1DAT[1] == 1 => 1<<1 == 2 */
	if (debug)
		printf("       Starting devmem2 GPIO output high test\n");
	rc = run_command(debug, "devmem2 0x11400264 w 2");
	assert(rc == 0);
	rc = gpio_get_output_value(devmem, fd, page_size, "gpj1", 1);
	assert (rc == GPIO_VALUE_HIGH);
	printf("[ OK ] devmem2 GPIO output high test\n");

	/* Set it to low again */
	/* GPJ1DAT[1] == 0 */
	if (debug)
		printf("       Starting devmem2 GPIO output low test\n");
	rc = run_command(debug, "devmem2 0x11400264 w 0");
	assert(rc == 0);
	rc = gpio_get_output_value(devmem, fd, page_size, "gpj1", 1);
	assert (rc == GPIO_VALUE_LOW);
	printf("[ OK ] devmem2 GPIO output low test\n");

	return 0;

}

static int test_direction_with_libgpiod(int debug, char *devmem, int fd,
					size_t page_size, char *bank,
					int gpio_offset)
{
	char *gpioget_command;
	char *gpioset_command;
	int rc;

	rc = asprintf(&gpioget_command, "gpioget %s %d", bank,
		      gpio_offset);
	if (rc == -1) {
		rc = errno;
		printf("%s: asprintf failed with error %d: %s\n",
		       __func__, rc, strerror(rc));
		assert(false);
	}

	rc = run_command(debug, gpioget_command);
	assert(rc == 0);

	free(gpioget_command);

	rc = gpio_get_direction(devmem, fd, page_size, bank, gpio_offset);
	assert (rc == GPIO_INPUT);

	rc = asprintf(&gpioset_command, "gpioset %s %d=1", bank,
		      gpio_offset);
	if (rc == -1) {
		rc = errno;
		printf("%s: asprintf failed with error %d: %s\n",
		       __func__, rc, strerror(rc));
		assert(false);
	}

	rc = run_command(debug, gpioset_command);
	assert(rc == 0);

	rc = gpio_get_direction(devmem, fd, page_size, bank, gpio_offset);
	assert (rc == GPIO_OUTPUT);

	free(gpioset_command);

	return 0;
}

int run_tests(int debug, char *devmem, int fd, size_t page_size)
{
	bool has_devmem2;
	bool has_gpioget;
	bool has_gpioset;

	has_devmem2 = device_has_devmem2(debug);
	has_gpioget = device_has_gpioget(debug);
	has_gpioset = device_has_gpioset(debug);

	if (has_gpioget && has_gpioset) {
		test_direction_with_libgpiod(debug, devmem, fd, page_size,
					     "gpj1", 1);
		printf("[ OK ] libgpiod GPIO direction test\n");

		test_output_value_with_libgpiod(debug, devmem, fd, page_size,
						"gpj1", 1);
		printf("[ OK ] libgpiod GPIO output value test\n");
	} else {
		if (!has_gpioget)
			printf("[ !! ] gpioget not found: skipping"
			       " libgpiod direction and output tests\n");

		if (!has_gpioset)
			printf("[ !! ] gpioset not found: skipping"
			       " libgpiod direction and output tests\n");
	}

	if (has_devmem2) {
		test_flash_with_devmem2(debug, devmem, fd, page_size);
	} else {
		printf("[ !! ] devmem2 not found: skipping"
			       " devmem2 direction and output tests\n");
	}

	return 0;
}