summaryrefslogtreecommitdiff
path: root/util/pmh7tool/pmh7tool.c
blob: dc9df2d274cf515cac961aeba08e524826b4c8c9 (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
/* This file is part of the coreboot project. */
/* SPDX-License-Identifier: GPL-2.0-only */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/io.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include "pmh7tool.h"

uint8_t pmh7_register_read(uint16_t reg)
{
	outb(reg & 0xff, EC_LENOVO_PMH7_ADDR_L);
	outb((reg & 0xff00) >> 8, EC_LENOVO_PMH7_ADDR_H);
	return inb(EC_LENOVO_PMH7_DATA);
}

void pmh7_register_write(uint16_t reg, uint8_t val)
{
	outb(reg & 0xff, EC_LENOVO_PMH7_ADDR_L);
	outb((reg & 0xff00) >> 8, EC_LENOVO_PMH7_ADDR_H);
	outb(val, EC_LENOVO_PMH7_DATA);
}

void pmh7_register_set_bit(uint16_t reg, uint8_t bit)
{
	uint8_t val;

	val = pmh7_register_read(reg);
	pmh7_register_write(reg, val | (1 << bit));
}

void pmh7_register_clear_bit(uint16_t reg, uint8_t bit)
{
	uint8_t val;

	val = pmh7_register_read(reg);
	pmh7_register_write(reg, val & ~(1 << bit));
}

uint8_t pmh7_register_read_bit(int16_t reg, uint8_t bit)
{
	uint8_t val;

	val = pmh7_register_read(reg);
	return (val >> bit) & 1;
}

void print_usage(const char *name)
{
	printf("usage: %s\n", name);
	printf("\n"
		   "	-h, --help:                   print this help\n"
		   "	-d, --dump:                   print registers\n"
		   "	-w, --write <addr> <data>:    write to register\n"
		   "	-r, --read <addr>:            read from register\n"
		   "	-b, --read-bit <addr> <bit>   read bit\n"
		   "	-c, --clear-bit <addr> <bit>  clear bit\n"
		   "	-s, --set-bit <addr> <bit>    set bit\n"
		   "\n"
		   "Attention! Writing to PMH7 registers is very dangerous, as you\n"
		   "           directly manipulate the power rails, enable lines,\n"
		   "           interrupt lines or something else of the device.\n"
		   "           Proceed with caution."
		   "\n");
}

enum action {HELP, DUMP, WRITE, READ, READBIT, CLEAR, SET};

int main(int argc, char *argv[])
{
	enum action act = HELP;
	int opt, option_index = 0;
	long input_addr = 0, input_data = 0;

	static struct option long_options[] = {
		{"help",        0, 0, 'h'},
		{"dump",        0, 0, 'd'},
		{"write",       1, 0, 'w'},
		{"read",        1, 0, 'r'},
		{"read-bit",    1, 0, 'b'},
		{"clear-bit",   1, 0, 'c'},
		{"set-bit",     1, 0, 's'},
		{0, 0, 0, 0}
	};

	if (argv[1] == NULL) {
		print_usage(argv[0]);
		exit(0);
	}

	while ((opt = getopt_long(argc, argv, "hdw:r:c:s:b:",
				  long_options, &option_index)) != EOF) {
		switch (opt) {
		case 'd':
			act = DUMP;
			break;

		case 'r':
			input_addr = strtoul(optarg, NULL, 16);
			act = READ;
			break;

		case 'w':
		case 'b':
		case 'c':
		case 's':
			input_addr = strtoul(optarg, NULL, 16);

			if (optind < argc && *argv[optind] != '-') {
				input_data = strtoul(argv[optind], NULL, 16);
				optind++;
			} else {
				fprintf(stderr,
					"Error: -%c option requires two arguments\n",
					opt);
				exit(1);
			}

			switch (opt) {
			case 'w':
				act = WRITE;
				break;
			case 'b':
				act = READBIT;
				break;
			case 'c':
				act = CLEAR;
				break;
			case 's':
				act = SET;
				break;
			}
			break;
		}
	}

	if (optind < argc) {
		fprintf(stderr, "Error: Extra parameter found.\n");
		print_usage(argv[0]);
		exit(1);
	}

	if (act == HELP) {
		print_usage(argv[0]);
		exit(0);
	}

	if (input_addr > 0x1ff) {
		fprintf(stderr,
			"Error: <addr> cannot be greater than 9 bits long.\n");
		exit(1);
	}

	if (act == SET || act == CLEAR || act == READBIT) {
		if (input_data > 7) {
			fprintf(stderr,
				"Error: <bit> cannot be greater than 7.\n");
			exit(1);
		}
	} else {
		if (input_data > 0xff) {
			fprintf(stderr,
				"Error: <data> cannot be greater than 8 bits long.\n");
			exit(1);
		}
	}

	if (geteuid() != 0) {
		fprintf(stderr, "You must be root.\n");
		exit(1);
	}

	if (ioperm(EC_LENOVO_PMH7_BASE, 0x10, 1)) {
		fprintf(stderr, "ioperm: %s\n", strerror(errno));
		exit(1);
	}

	switch (act) {
	case DUMP:
		for (int i = 0; i < 0x200; i++) {
			if ((i % 0x10) == 0) {
				if (i != 0)
					printf("\n");
				printf("%04x: ", i);
			}
			printf("%02x ", pmh7_register_read(i));
		}
		printf("\n");
		break;

	case READ:
		printf("%02x\n", pmh7_register_read(input_addr));
		break;

	case WRITE:
		pmh7_register_write(input_addr, input_data);
		break;

	case READBIT:
		printf("%d\n", pmh7_register_read_bit(input_addr, input_data));
		break;

	case CLEAR:
		pmh7_register_clear_bit(input_addr, input_data);
		break;

	case SET:
		pmh7_register_set_bit(input_addr, input_data);
		break;

	default:
		break;
	}

	return 0;
}