2
isaset.c - isaset, a user-space program to write ISA registers
3
Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl>, and
4
Mark D. Studebaker <mdsxyz123@yahoo.com>
5
Copyright (C) 2004-2011 Jean Delvare <jdelvare@suse.de>
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25
isaset 0x295 0x296 0x10 0x12 Write 0x12 to address 0x10 using address/data registers
26
isaset -f 0x5010 0x12 Write 0x12 to location 0x5010
37
unsigned long isa_io_base = 0; /* XXX for now */
38
#endif /* __powerpc__ */
40
static void help(void)
43
"Syntax for I2C-like access:\n"
44
" isaset [OPTIONS] ADDRREG DATAREG ADDRESS VALUE [MASK]\n"
45
"Syntax for flat address space:\n"
46
" isaset -f [OPTIONS] ADDRESS VALUE [MASK]\n"
48
" -f Enable flat address space mode\n"
49
" -y Assume affirmative answer to all questions\n"
50
" -W Write a word (16-bit) value\n"
51
" -L Write a long (32-bit) value\n");
54
int main(int argc, char *argv[])
56
int addrreg, datareg = 0, addr = 0;
57
unsigned long value, vmask = 0, maxval = 0xff, res;
59
int flat = 0, yes = 0, width = 1;
62
/* handle (optional) flags first */
63
while (1+flags < argc && argv[1+flags][0] == '-') {
64
switch (argv[1+flags][1]) {
65
case 'f': flat = 1; break;
66
case 'y': yes = 1; break;
67
case 'W': width = 2; maxval = 0xffff; break;
68
case 'L': width = 4; maxval = 0xffffffff; break;
70
fprintf(stderr, "Warning: Unsupported flag "
71
"\"-%c\"!\n", argv[1+flags][1]);
78
/* verify that the argument count is correct */
79
if ((!flat && (argc < 1+flags+4 || argc > 1+flags+5))
80
|| (flat && (argc < 1+flags+2 || argc > 1+flags+3))) {
85
addrreg = strtol(argv[1+flags], &end, 0);
87
fprintf(stderr, "Error: Invalid address!\n");
91
if (addrreg < 0 || addrreg > (flat?0xffff:0x3fff)) {
93
"Error: Address out of range (0x0000-0x%04x)!\n",
100
datareg = strtol(argv[1+flags+1], &end, 0);
102
fprintf(stderr, "Error: Invalid data register!\n");
106
if (datareg < 0 || datareg > 0x3fff) {
107
fprintf(stderr, "Error: Data register out of range "
108
"(0x0000-0x3fff)!\n");
113
addr = strtol(argv[1+flags+2], &end, 0);
115
fprintf(stderr, "Error: Invalid address!\n");
119
if (addr < 0 || addr > 0xff) {
120
fprintf(stderr, "Error: Address out of range "
127
/* rest is the same for both modes so we cheat on flags */
131
value = strtoul(argv[flags+2], &end, 0);
133
fprintf(stderr, "Error: Invalid value!\n");
137
if (value > maxval) {
138
fprintf(stderr, "Error: Value out of range "
139
"(0x%0*u-%0*lu)!\n", width * 2, 0, width * 2, maxval);
144
if (flags+3 < argc) {
145
vmask = strtoul(argv[flags+3], &end, 0);
147
fprintf(stderr, "Error: Invalid mask!\n");
151
if (vmask > maxval) {
152
fprintf(stderr, "Error: Mask out of range "
153
"(0x%0*u-%0*lu)!\n", width * 2, 0,
161
fprintf(stderr, "Error: Can only be run as root "
162
"(or make it suid root)\n");
167
fprintf(stderr, "WARNING! Running this program can cause "
168
"system crashes, data loss and worse!\n");
172
"I will write value 0x%0*lx%s to address "
173
"0x%x.\n", width * 2, value,
174
vmask ? " (masked)" : "", addrreg);
177
"I will write value 0x%0*lx%s to address "
178
"0x%02x of chip with address register 0x%x\n"
179
"and data register 0x%x.\n", width * 2,
180
value, vmask ? " (masked)" : "", addr,
183
fprintf(stderr, "Continue? [Y/n] ");
186
fprintf(stderr, "Aborting on user request.\n");
192
if (!flat && datareg < 0x400 && addrreg < 0x400) {
193
if (ioperm(datareg, 1, 1)) {
194
fprintf(stderr, "Error: Could not ioperm() data "
198
if (ioperm(addrreg, 1, 1)) {
199
fprintf(stderr, "Error: Could not ioperm() address "
205
fprintf(stderr, "Error: Could not do iopl(3)!\n");
212
unsigned long oldvalue;
215
oldvalue = inx(addrreg, width);
218
oldvalue = inx(datareg, width);
221
value = (value & vmask) | (oldvalue & ~vmask);
224
fprintf(stderr, "Old value 0x%0*lx, write mask "
225
"0x%0*lx: Will write 0x%0*lx to %s "
226
"0x%02x\n", width * 2, oldvalue,
227
width * 2, vmask, width * 2, value,
228
flat ? "address" : "register",
229
flat ? addrreg : addr);
231
fprintf(stderr, "Continue? [Y/n] ");
234
fprintf(stderr, "Aborting on user request.\n");
240
/* do the real thing */
243
outx(value, addrreg, width);
245
res = inx(addrreg, width);
249
outx(value, datareg, width);
251
res = inx(datareg, width);
255
fprintf(stderr, "Data mismatch, wrote 0x%0*lx, "
256
"read 0x%0*lx back.\n", width * 2, value,