pio.c 9.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * (C) Copyright 2011 Henrik Nordstrom <henrik@henriknordstrom.net>
 *
 * 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.
 *
 * 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
 */

20
21
22
23
24
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
Henrik Nordstrom's avatar
Henrik Nordstrom committed
25
#include <stdlib.h>
26
#include <errno.h>
27
28
29
#ifndef NO_MMAP
  #include <sys/mman.h>
#endif
Henrik Nordstrom's avatar
Henrik Nordstrom committed
30
31
32
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
33

34
#include "common.h"
35
#include "portable_endian.h"
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#define PIO_REG_SIZE 0x228 /*0x300*/
#define PIO_PORT_SIZE 0x24

struct pio_status {
	int mul_sel;
	int pull;
	int drv_level;
	int data;
};

#define PIO_REG_CFG(B, N, I)	((B) + (N)*0x24 + ((I)<<2) + 0x00)
#define PIO_REG_DLEVEL(B, N, I)	((B) + (N)*0x24 + ((I)<<2) + 0x14)
#define PIO_REG_PULL(B, N, I)	((B) + (N)*0x24 + ((I)<<2) + 0x1C)
#define PIO_REG_DATA(B, N)	((B) + (N)*0x24 + 0x10)
Henrik Nordstrom's avatar
Henrik Nordstrom committed
51
#define PIO_NR_PORTS		9 /* A-I */
52
53
54
55
56
57
58
59
60
61

#define LE32TOH(X)		le32toh(*((uint32_t*)(X)))

static int pio_get(const char *buf, uint32_t port, uint32_t port_num, struct pio_status *pio)
{
	uint32_t val;
	uint32_t port_num_func, port_num_pull;
	uint32_t offset_func, offset_pull;

	port_num_func = port_num >> 3;
62
	offset_func = ((port_num & 0x07) << 2);
63
64

	port_num_pull = port_num >> 4;
65
	offset_pull = ((port_num & 0x0f) << 1);
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

	/* func */
	val = LE32TOH(PIO_REG_CFG(buf, port, port_num_func));
	pio->mul_sel = (val>>offset_func) & 0x07;

	/* pull */
	val = LE32TOH(PIO_REG_PULL(buf, port, port_num_pull));
	pio->pull = (val>>offset_pull) & 0x03;

	/* dlevel */
	val = LE32TOH(PIO_REG_DLEVEL(buf, port, port_num_pull));
	pio->drv_level = (val>>offset_pull) & 0x03;

	/* i/o data */
	if (pio->mul_sel > 1)
		pio->data = -1;
	else {
83
		val = LE32TOH(PIO_REG_DATA(buf, port));
84
85
86
87
88
		pio->data = (val >> port_num) & 0x01;
	}
	return 1;
}

Alejandro Mery's avatar
Alejandro Mery committed
89
90
91
92
93
94
95
static int pio_set(char *buf, uint32_t port, uint32_t port_num, struct pio_status *pio)
{
	uint32_t *addr, val;
	uint32_t port_num_func, port_num_pull;
	uint32_t offset_func, offset_pull;

	port_num_func = port_num >> 3;
96
	offset_func = ((port_num & 0x07) << 2);
Alejandro Mery's avatar
Alejandro Mery committed
97
98

	port_num_pull = port_num >> 4;
99
	offset_pull = ((port_num & 0x0f) << 1);
Alejandro Mery's avatar
Alejandro Mery committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

	/* func */
	if (pio->mul_sel >= 0) {
		addr = (uint32_t*)PIO_REG_CFG(buf, port, port_num_func);
		val = le32toh(*addr);
		val &= ~(0x07 << offset_func);
		val |=  (pio->mul_sel & 0x07) << offset_func;
		*addr = htole32(val);
	}

	/* pull */
	if (pio->pull >= 0) {
		addr = (uint32_t*)PIO_REG_PULL(buf, port, port_num_pull);
		val = le32toh(*addr);
		val &= ~(0x03 << offset_pull);
		val |=  (pio->pull & 0x03) << offset_pull;
		*addr = htole32(val);
	}

	/* dlevel */
	if (pio->drv_level >= 0) {
		addr = (uint32_t*)PIO_REG_DLEVEL(buf, port, port_num_pull);
		val = le32toh(*addr);
		val &= ~(0x03 << offset_pull);
124
		val |=  (pio->drv_level & 0x03) << offset_pull;
Alejandro Mery's avatar
Alejandro Mery committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
		*addr = htole32(val);
	}

	/* data */
	if (pio->data >= 0) {
		addr = (uint32_t*)PIO_REG_DATA(buf, port);
		val = le32toh(*addr);
		if (pio->data)
			val |= (0x01 << port_num);
		else
			val &= ~(0x01 << port_num);
		*addr = htole32(val);
	}

	return 1;
}

Henrik Nordstrom's avatar
Henrik Nordstrom committed
142
143
144
145
146
147
148
149
150
151
152
static void pio_print(int port, int port_nr, struct pio_status *pio)
{
	printf("P%c%d", 'A'+port, port_nr);
	printf("<%x>", pio->mul_sel);
	printf("<%x>", pio->pull);
	printf("<%x>", pio->drv_level);
	if (pio->data >= 0)
		printf("<%x>", pio->data);
	fputc('\n', stdout);
}

153
154
155
156
static void print(const char *buf)
{
	int port, i;
	struct pio_status pio;
Henrik Nordstrom's avatar
Henrik Nordstrom committed
157
158
	for (port=0; port < PIO_NR_PORTS; port++) {
		for (i=0; i<32; i++) {
159
			if (pio_get(buf, port, i, &pio)) {
Henrik Nordstrom's avatar
Henrik Nordstrom committed
160
				pio_print(port, i, &pio);
161
162
163
164
			}
		}
	}
}
Henrik Nordstrom's avatar
Henrik Nordstrom committed
165
166
167
168

static const char *argv0;

static void usage(int rc )
169
{
170
	fputs("sunxi-pio " VERSION "\n\n", stderr);
171
172
173
174
	fprintf(stderr, "usage: %s -m|-i input [-o output] pin..\n", argv0);
	fprintf(stderr," -m				mmap - read pin state from system\n");
	fprintf(stderr," -i				read pin state from file\n");
	fprintf(stderr," -o				save pin state data to file\n");
175
176
177
178
	fprintf(stderr," print				Show all pins\n");
	fprintf(stderr," Pxx				Show pin\n");
	fprintf(stderr," Pxx<mode><pull><drive><data>	Configure pin\n");
	fprintf(stderr," Pxx=data,drive			Configure GPIO output\n");
Henrik Nordstrom's avatar
Henrik Nordstrom committed
179
	fprintf(stderr," Pxx*count			Oscillate GPIO output (mmap mode only)\n");
180
181
	fprintf(stderr," Pxx?pull			Configure GPIO input\n");
	fprintf(stderr," clean				Clean input pins\n");
Andre Przywara's avatar
Andre Przywara committed
182
	fprintf(stderr, "\n	mode 0-7, 0=input, 1=output, 2-7 I/O function\n");
Henrik Nordstrom's avatar
Henrik Nordstrom committed
183
184
	fprintf(stderr, "	pull 0=none, 1=up, 2=down\n");
	fprintf(stderr, "	drive 0-3, I/O drive level\n");
185

Henrik Nordstrom's avatar
Henrik Nordstrom committed
186
187
	exit(rc);
}
188

Henrik Nordstrom's avatar
Henrik Nordstrom committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
static void parse_pin(int *port, int *pin, const char *name)
{
	if (*name == 'P') name++;
	*port = *name++ - 'A';
	*pin = atoi(name);
}

static void cmd_show_pin(char *buf, const char *pin)
{
	int port, port_nr;
	struct pio_status pio;
	parse_pin(&port, &port_nr, pin);
	if (!pio_get(buf, port, port_nr, &pio))
		usage(1);
    	pio_print(port, port_nr, &pio);
}

206
207
208
209
210
211
212
213
214
215
216
217
218
static int parse_int(int *dst, const char *in)
{
	int value;
	char *next;
	errno = 0;
	value = strtol(in, &next, 0);
	if (!errno && next != in) {
		*dst = value;
		return 0;
	}
	return -1;
}

Henrik Nordstrom's avatar
Henrik Nordstrom committed
219
220
221
222
223
224
225
226
static void cmd_set_pin(char *buf, const char *pin)
{
	int port, port_nr;
	const char *t = pin;
	struct pio_status pio;
	parse_pin(&port, &port_nr, pin);
	if (!pio_get(buf, port, port_nr, &pio))
		usage(1);
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
	if ((t = strchr(pin, '='))) {
		pio.mul_sel = 1;
		if (t) {
			t++;
			parse_int(&pio.data, t);
		}
		if (t)
			t = strchr(t, ',');
		if (t) {
			t++;
			parse_int(&pio.drv_level, t);
		}
	} else if ((t = strchr(pin, '?'))) {
		pio.mul_sel = 0;
		pio.data = 0;
		pio.drv_level = 0;
		if (t) {
			t++;
			parse_int(&pio.pull, t);
		}
	} else if ((t = strchr(pin, '<'))) {
		if (t) {
			t++;
			parse_int(&pio.mul_sel, t);
		}
		if (t)
			t = strchr(t, '<');
		if (t) {
			t++;
			parse_int(&pio.pull, t);
		}
		if (t)
			t = strchr(t, '<');
		if (t) {
			t++;
			parse_int(&pio.drv_level, t);
		}
		if (t)
			t = strchr(t, '<');
		if (t) {
			t++;
			parse_int(&pio.data, t);
		}
Henrik Nordstrom's avatar
Henrik Nordstrom committed
270
271
272
273
	}
	pio_set(buf, port, port_nr, &pio);
}

Henrik Nordstrom's avatar
Henrik Nordstrom committed
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
static void cmd_oscillate(char *buf, const char *pin)
{
	int port, port_nr;
	const char *t = pin;
	int i, n = 0;
	uint32_t *addr, val;

	parse_pin(&port, &port_nr, pin);
	{
		struct pio_status pio;
		if (!pio_get(buf, port, port_nr, &pio))
			usage(1);
		pio.mul_sel = 1;
		pio_set(buf, port, port_nr, &pio);
	}

	addr = (uint32_t*)PIO_REG_DATA(buf, port);
	t = strchr(pin, '*');
	parse_int(&n, t+1);
	val = le32toh(*addr);
	for (i = 0; i < n; i++) {
		val ^= 1 << port_nr;
		*addr = htole32(val);
	}
}

Henrik Nordstrom's avatar
Henrik Nordstrom committed
300
301
302
303
304
305
306
307
308
309
310
311
312
static void cmd_clean(char *buf)
{
	int port, i;
	struct pio_status pio;
	for (port=0; port < PIO_NR_PORTS; port++) {
		for (i=0; i<32; i++) {
			if (pio_get(buf, port, i, &pio)) {
				if (pio.mul_sel == 0) {
					pio.data = 0;
					pio_set(buf, port, i, &pio);
				}
			}
		}
313
314
315
	}
}

316
static int do_command(char *buf, const char **args, int UNUSED(argc))
317
{
Henrik Nordstrom's avatar
Henrik Nordstrom committed
318
	const char *command = args[0];
319
320
321
322
323
324
325
	if (*command == 'P') {
		if (strchr(command, '<'))
			cmd_set_pin(buf, command);
		else if (strchr(command, '='))
			cmd_set_pin(buf, command);
		else if (strchr(command, '?'))
			cmd_set_pin(buf, command);
Henrik Nordstrom's avatar
Henrik Nordstrom committed
326
327
		else if (strchr(command, '*'))
			cmd_oscillate(buf, command);
328
329
330
		else
			cmd_show_pin(buf, command);
	}
Henrik Nordstrom's avatar
Henrik Nordstrom committed
331
332
333
334
335
	else if (strcmp(command, "print") == 0)
		print(buf);
	else if (strcmp(command, "clean") == 0)
		cmd_clean(buf);
	else	usage(1);
336
337
338
339
340
	return 1;
}

int main(int argc, char **argv)
{
Henrik Nordstrom's avatar
Henrik Nordstrom committed
341
342
343
344
345
	int opt;
	FILE *in = NULL;
	FILE *out = NULL;
	const char *in_name = NULL;
	const char *out_name = NULL;
Henrik Nordstrom's avatar
Henrik Nordstrom committed
346
347
348
	char buf_[PIO_REG_SIZE];
	char *buf = buf_;
	int do_mmap = 0;
Henrik Nordstrom's avatar
Henrik Nordstrom committed
349
350

	argv0 = argv[0];
351

Henrik Nordstrom's avatar
Henrik Nordstrom committed
352
	while ((opt = getopt(argc, argv, "i:o:m")) != -1) {
Henrik Nordstrom's avatar
Henrik Nordstrom committed
353
354
355
		switch(opt) {
		case '?':
			usage(0);
Henrik Nordstrom's avatar
Henrik Nordstrom committed
356
357
358
		case 'm':
			do_mmap = 1;
			break;
Henrik Nordstrom's avatar
Henrik Nordstrom committed
359
360
361
362
363
364
365
366
		case 'i':
			in_name = optarg;
			break;
		case 'o':
			out_name = optarg;
			break;
		}
	}
Henrik Nordstrom's avatar
Henrik Nordstrom committed
367
	if (!in_name && !do_mmap)
Henrik Nordstrom's avatar
Henrik Nordstrom committed
368
		usage(1);
Henrik Nordstrom's avatar
Henrik Nordstrom committed
369
	if (do_mmap) {
370
371
372
373
#ifdef NO_MMAP
		errno = ENOSYS; /* Function not implemented */
		perror("mmap PIO");
#else
Henrik Nordstrom's avatar
Henrik Nordstrom committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
		int pagesize = sysconf(_SC_PAGESIZE);
		int fd = open("/dev/mem",O_RDWR);
		int addr = 0x01c20800 & ~(pagesize-1);
		int offset = 0x01c20800 & (pagesize-1);
		if (fd == -1) {
			perror("open /dev/mem");
			exit(1);
		}
		buf = mmap(NULL, (0x800 + pagesize - 1) & ~(pagesize-1), PROT_WRITE|PROT_READ, MAP_SHARED, fd, addr);
		if (!buf) {
			perror("mmap PIO");
			exit(1);
		}
		close(fd);
		buf += offset;
389
#endif
Henrik Nordstrom's avatar
Henrik Nordstrom committed
390
	}
Henrik Nordstrom's avatar
Henrik Nordstrom committed
391
392
393
394
395
396
397
398
399
400
401
	if (in_name) {
		if (strcmp(in_name, "-") == 0) {
			in = stdin;
		} else {
			in = fopen(in_name, "rb");
			if (!in) {
				perror("open input");
				exit(1);
			}
		}
	}
Henrik Nordstrom's avatar
Henrik Nordstrom committed
402
	if (in) {
Angus Gratton's avatar
Angus Gratton committed
403
		if (fread(buf, PIO_REG_SIZE, 1, in) != 1) {
Henrik Nordstrom's avatar
Henrik Nordstrom committed
404
405
406
407
408
			perror("read input");
			exit(1);
		}
		if (in != stdin)
			fclose(in);
Henrik Nordstrom's avatar
Henrik Nordstrom committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	}

	while(optind < argc) {
		optind += do_command(buf, (const char **)(argv + optind), argc - optind);
	}

	if (out_name) {
		if (strcmp(out_name, "-") == 0) {
			out = stdout;
		} else {
			out = fopen(out_name, "wb");
			if (!out) {
				perror("open output");
				exit(1);
			}
		}
Angus Gratton's avatar
Angus Gratton committed
425
		if (fwrite(buf, PIO_REG_SIZE, 1, out) != 1) {
Henrik Nordstrom's avatar
Henrik Nordstrom committed
426
427
428
429
			perror("write output");
			exit(1);
		}
	}
430
431

	return 0;
Henrik Nordstrom's avatar
Henrik Nordstrom committed
432
}