summaryrefslogtreecommitdiffstats
path: root/external/lpc/lpc.c
blob: b8a4fa1bd22dc813781b042ceaabd71bda3562fa (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
/* Copyright 2014-2016 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * imitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <byteswap.h>
#include <stdint.h>
#include <stdbool.h>
#include <getopt.h>
#include <limits.h>
#include <arpa/inet.h>
#include <assert.h>
#include <endian.h>
#include <byteswap.h>

#define SYSFS_PREFIX "/sys/kernel/debug/powerpc/lpc"

int main(int argc, char *argv[])
{
	char		path[256];
	char		*dot;
	char		*eq;
	int		fd, size = 4;
	bool		do_write = false;
	bool		big_endian = false;
	uint32_t	addr, val;
	ssize_t		rc;

	if (argc < 3) {
		printf("Usage: %s <space> <addr>[.lLwWbBd[,size]][=value]\n", argv[0]);
		return 0;
	}

	eq = strchr(argv[2], '=');
	if (eq) {
		do_write = true;
		val = strtoul(eq + 1, NULL, 0);
		*eq = 0;
	}
	dot = strchr(argv[2], '.');
	if (dot) {
		*(dot++) = 0;
		switch(*dot) {
		case 'L':
			big_endian  = true;
		case 'l':
			break;
		case 'W':
			big_endian  = true;
		case 'w':
			size = 2;
			break;
		case 'B':
			big_endian  = true;
		case 'b':
			size = 1;
			break;
		default:
			fprintf(stderr, "Invalid size specifier\n");
			exit(1);
		}
	}
	addr = strtoul(argv[2], NULL, 0);

	memset(path, 0, sizeof(path));
	snprintf(path, 255, SYSFS_PREFIX "/%s", argv[1]);
	fd = open(path, O_RDWR);
	if (fd < 0) {
		perror("Failed to open sysfs file");
		exit(1);
	}

	lseek(fd, addr, SEEK_SET);
	if (do_write)  {
		uint8_t v8;
		uint16_t v16;
		uint32_t v32;

		switch(size) {
		case 1:
			val &= 0xff;
			v8 = val;
			rc = write(fd, &v8, 1);
			if (rc != 1) {
				perror("Failed to write to LPC");
				exit(1);
			}
			printf("[%s] W 0x%08x.%c=0x%02x\n",
			       argv[1], addr, big_endian ? 'B' : 'b', val);
			break;
		case 2:
			val &= 0xffff;
#if __BYTE_ORDER == __LITTLE_ENDIAN
			v16 = big_endian ? bswap_16(val) : val;
#else
			v16 = big_endian ? val : bswap_16(val);
#endif
			rc = write(fd, &v16, 2);
			if (rc != 2) {
				perror("Failed to write to LPC");
				exit(1);
			}
			printf("[%s] W 0x%08x.%c=0x%04x\n",
			       argv[1], addr, big_endian ? 'W' : 'w', val);
			break;
		default:
#if __BYTE_ORDER == __LITTLE_ENDIAN
			v32 = big_endian ? bswap_32(val) : val;
#else
			v32 = big_endian ? val : bswap_32(val);
#endif
			rc = write(fd, &v32, 4);
			if (rc != 4) {
				perror("Failed to write to LPC");
				exit(1);
			}
			printf("[%s] W 0x%08x.%c=0x%08x\n",
			       argv[1], addr, big_endian ? 'L' : 'l', val);
			break;
		}
	} else {
		uint8_t v8;
		uint16_t v16;
		uint32_t v32;

		switch(size) {
		case 1:
			rc = read(fd, &v8, 1);
			if (rc != 1) {
				perror("Failed to read from LPC");
				exit(1);
			}
			printf("[%s] R 0x%08x.%c=0x%02x\n", argv[1], addr,
			       big_endian ? 'B' : 'b', v8);
			break;
		case 2:
			rc = read(fd, &v16, 2);
			if (rc != 2) {
				perror("Failed to read from LPC");
				exit(1);
			}
#if __BYTE_ORDER == __LITTLE_ENDIAN
			v16 = big_endian ? bswap_16(v16) : v16;
#else
			v16 = big_endian ? v16 : bswap_16(v16);
#endif
			printf("[%s] R 0x%08x.%c=0x%04x\n", argv[1], addr,
			       big_endian ? 'W' : 'w', v16);
			break;
		default:
			rc = read(fd, &v32, 4);
			if (rc != 4) {
				perror("Failed to read from LPC");
				exit(1);
			}
#if __BYTE_ORDER == __LITTLE_ENDIAN
			v32 = big_endian ? bswap_32(v32) : v32;
#else
			v32 = big_endian ? v32 : bswap_32(v32);
#endif
			printf("[%s] R 0x%08x.%c=0x%08x\n", argv[1], addr,
			       big_endian ? 'L' : 'l', v32);
			break;
		}
	}
	return 0;
}
OpenPOWER on IntegriCloud