blob: f03d97e254b5f6273e8ca077f42f177e56e4c86a [file] [log] [blame]
Evgeny Zinovievd54f8252018-08-01 15:53:18 +03001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2018 Evgeny Zinoviev <me@ch1p.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <getopt.h>
19#include <sys/io.h>
20#include <errno.h>
21#include <string.h>
22#include <unistd.h>
23#include <stdint.h>
24#include "pmh7tool.h"
25
26uint8_t pmh7_register_read(uint16_t reg)
27{
28 outb(reg & 0xff, EC_LENOVO_PMH7_ADDR_L);
29 outb((reg & 0xff00) >> 8, EC_LENOVO_PMH7_ADDR_H);
30 return inb(EC_LENOVO_PMH7_DATA);
31}
32
33void pmh7_register_write(uint16_t reg, uint8_t val)
34{
35 outb(reg & 0xff, EC_LENOVO_PMH7_ADDR_L);
36 outb((reg & 0xff00) >> 8, EC_LENOVO_PMH7_ADDR_H);
37 outb(val, EC_LENOVO_PMH7_DATA);
38}
39
40void pmh7_register_set_bit(uint16_t reg, uint8_t bit)
41{
42 uint8_t val;
43
44 val = pmh7_register_read(reg);
45 pmh7_register_write(reg, val | (1 << bit));
46}
47
48void pmh7_register_clear_bit(uint16_t reg, uint8_t bit)
49{
50 uint8_t val;
51
52 val = pmh7_register_read(reg);
53 pmh7_register_write(reg, val & ~(1 << bit));
54}
55
Evgeny Zinoviev672f5612018-08-29 22:24:11 +030056uint8_t pmh7_register_read_bit(int16_t reg, uint8_t bit)
57{
58 uint8_t val;
59
60 val = pmh7_register_read(reg);
61 return (val >> bit) & 1;
62}
63
Evgeny Zinovievd54f8252018-08-01 15:53:18 +030064void print_usage(const char *name)
65{
66 printf("usage: %s\n", name);
67 printf("\n"
Evgeny Zinoviev672f5612018-08-29 22:24:11 +030068 " -h, --help: print this help\n"
69 " -d, --dump: print registers\n"
70 " -w, --write <addr> <data>: write to register\n"
71 " -r, --read <addr>: read from register\n"
72 " -b, --read-bit <addr> <bit> read bit\n"
73 " -c, --clear-bit <addr> <bit> clear bit\n"
74 " -s, --set-bit <addr> <bit> set bit\n"
Evgeny Zinovievd54f8252018-08-01 15:53:18 +030075 "\n"
76 "Attention! Writing to PMH7 registers is very dangerous, as you\n"
77 " directly manipulate the power rails, enable lines,\n"
78 " interrupt lines or something else of the device.\n"
79 " Proceed with caution."
80 "\n");
81}
82
Evgeny Zinoviev672f5612018-08-29 22:24:11 +030083enum action {HELP, DUMP, WRITE, READ, READBIT, CLEAR, SET};
Evgeny Zinovievd54f8252018-08-01 15:53:18 +030084
85int main(int argc, char *argv[])
86{
87 enum action act = HELP;
88 int opt, option_index = 0;
89 long input_addr = 0, input_data = 0;
90
91 static struct option long_options[] = {
92 {"help", 0, 0, 'h'},
93 {"dump", 0, 0, 'd'},
94 {"write", 1, 0, 'w'},
95 {"read", 1, 0, 'r'},
Evgeny Zinoviev672f5612018-08-29 22:24:11 +030096 {"read-bit", 1, 0, 'b'},
Evgeny Zinovievd54f8252018-08-01 15:53:18 +030097 {"clear-bit", 1, 0, 'c'},
98 {"set-bit", 1, 0, 's'},
99 {0, 0, 0, 0}
100 };
101
102 if (argv[1] == NULL) {
103 print_usage(argv[0]);
104 exit(0);
105 }
106
Evgeny Zinoviev672f5612018-08-29 22:24:11 +0300107 while ((opt = getopt_long(argc, argv, "hdw:r:c:s:b:",
Evgeny Zinovievd54f8252018-08-01 15:53:18 +0300108 long_options, &option_index)) != EOF) {
109 switch (opt) {
110 case 'd':
111 act = DUMP;
112 break;
113
114 case 'r':
115 input_addr = strtoul(optarg, NULL, 16);
116 act = READ;
117 break;
118
119 case 'w':
Evgeny Zinoviev672f5612018-08-29 22:24:11 +0300120 case 'b':
Evgeny Zinovievd54f8252018-08-01 15:53:18 +0300121 case 'c':
122 case 's':
123 input_addr = strtoul(optarg, NULL, 16);
124
125 if (optind < argc && *argv[optind] != '-') {
126 input_data = strtoul(argv[optind], NULL, 16);
127 optind++;
128 } else {
129 fprintf(stderr,
130 "Error: -%c option requires two arguments\n",
131 opt);
132 exit(1);
133 }
134
135 switch (opt) {
136 case 'w':
137 act = WRITE;
138 break;
Evgeny Zinoviev672f5612018-08-29 22:24:11 +0300139 case 'b':
140 act = READBIT;
141 break;
Evgeny Zinovievd54f8252018-08-01 15:53:18 +0300142 case 'c':
143 act = CLEAR;
144 break;
145 case 's':
146 act = SET;
147 break;
148 }
149 break;
150 }
151 }
152
153 if (optind < argc) {
154 fprintf(stderr, "Error: Extra parameter found.\n");
155 print_usage(argv[0]);
156 exit(1);
157 }
158
159 if (act == HELP) {
160 print_usage(argv[0]);
161 exit(0);
162 }
163
164 if (input_addr > 0x1ff) {
165 fprintf(stderr,
166 "Error: <addr> cannot be greater than 9 bits long.\n");
167 exit(1);
168 }
169
Evgeny Zinoviev672f5612018-08-29 22:24:11 +0300170 if (act == SET || act == CLEAR || act == READBIT) {
Evgeny Zinovievd54f8252018-08-01 15:53:18 +0300171 if (input_data > 7) {
172 fprintf(stderr,
173 "Error: <bit> cannot be greater than 7.\n");
174 exit(1);
175 }
176 } else {
177 if (input_data > 0xff) {
178 fprintf(stderr,
179 "Error: <data> cannot be greater than 8 bits long.\n");
180 exit(1);
181 }
182 }
183
184 if (geteuid() != 0) {
185 fprintf(stderr, "You must be root.\n");
186 exit(1);
187 }
188
189 if (ioperm(EC_LENOVO_PMH7_BASE, 0x10, 1)) {
190 fprintf(stderr, "ioperm: %s\n", strerror(errno));
191 exit(1);
192 }
193
194 switch (act) {
195 case DUMP:
196 for (int i = 0; i < 0x200; i++) {
197 if ((i % 0x10) == 0) {
198 if (i != 0)
199 printf("\n");
200 printf("%04x: ", i);
201 }
202 printf("%02x ", pmh7_register_read(i));
203 }
204 printf("\n");
205 break;
206
207 case READ:
208 printf("%02x\n", pmh7_register_read(input_addr));
209 break;
210
211 case WRITE:
212 pmh7_register_write(input_addr, input_data);
213 break;
214
Evgeny Zinoviev672f5612018-08-29 22:24:11 +0300215 case READBIT:
216 printf("%d\n", pmh7_register_read_bit(input_addr, input_data));
217 break;
218
Evgeny Zinovievd54f8252018-08-01 15:53:18 +0300219 case CLEAR:
220 pmh7_register_clear_bit(input_addr, input_data);
221 break;
222
223 case SET:
224 pmh7_register_set_bit(input_addr, input_data);
225 break;
226
227 default:
228 break;
229 }
230
231 return 0;
232}