1 /* This file is part of the ectool project. */
2 /* SPDX-License-Identifier: GPL-2.0-only */
9 #if !(defined __NetBSD__ || defined __OpenBSD__)
14 static int ec_data
= 0x62;
15 static int ec_sc
= 0x66;
17 #if defined __NetBSD__ || defined __OpenBSD__
18 #include <machine/sysarch.h>
19 static uint8_t inb(unsigned port
)
22 __asm
volatile("inb %w1,%0" : "=a" (data
) : "d" (port
));
25 static __inline
void outb(uint8_t data
, unsigned port
)
27 __asm
volatile("outb %0,%w1" : : "a" (data
), "d" (port
));
33 #define debug(x...) if (verbose) printf(x)
35 int send_ec_command(uint8_t command
)
40 while ((inb(ec_sc
) & EC_IBF
) && --timeout
) {
42 if ((timeout
& 0xff) == 0)
46 debug("Timeout while sending command 0x%02x to EC!\n",
55 int send_ec_data(uint8_t data
)
60 while ((inb(ec_sc
) & EC_IBF
) && --timeout
) { // wait for IBF = 0
62 if ((timeout
& 0xff) == 0)
66 debug("Timeout while sending data 0x%02x to EC!\n", data
);
75 int send_ec_data_nowait(uint8_t data
)
82 uint8_t recv_ec_data(void)
88 while (--timeout
) { // Wait for OBF = 1
89 if (inb(ec_sc
) & EC_OBF
)
93 if ((timeout
& 0xff) == 0)
97 debug("\nTimeout while receiving data from EC!\n");
102 debug("recv_ec_data: 0x%02x\n", data
);
107 uint8_t ec_read(uint8_t addr
)
109 send_ec_command(RD_EC
);
112 return recv_ec_data();
115 uint8_t ec_ext_read(uint16_t addr
)
117 send_ec_command(WR_EC
);
119 send_ec_data(addr
& 0xff);
120 send_ec_command(RX_EC
);
121 send_ec_data(addr
>> 8);
123 return recv_ec_data();
126 int ec_ext_write(uint16_t addr
, uint8_t data
)
128 send_ec_command(WR_EC
);
130 send_ec_data(addr
& 0xff);
131 send_ec_command(WX_EC
);
132 send_ec_data(addr
>> 8);
134 return send_ec_data(data
);
137 int ec_write(uint8_t addr
, uint8_t data
)
139 send_ec_command(WR_EC
);
142 return send_ec_data(data
);
145 uint8_t ec_idx_read(uint16_t addr
)
147 uint16_t lpc_idx
= 0x380;
149 outb(addr
& 0xff, lpc_idx
+ 2);
150 outb(addr
>> 8, lpc_idx
+ 1);
152 return inb(lpc_idx
+ 3);
155 uint8_t ec_query(void)
157 send_ec_command(QR_EC
);
158 return recv_ec_data();
161 int get_ec_ports(void)
163 FILE *fp
= fopen("/proc/ioports", "r");
164 int data
= 0, cmd
= 0;
170 while (!feof(fp
) && (data
== 0 || cmd
== 0)) {
171 fgets(line
, sizeof(line
), fp
);
172 if (strstr(line
, "EC data") != NULL
)
173 data
= strtol(line
, NULL
, 16);
175 if (strstr(line
, "EC cmd") != NULL
)
176 cmd
= strtol(line
, NULL
, 16);
180 if (data
!= 0 && cmd
!= 0) {
181 debug("EC data = 0x%x, EC cmd = 0x%x\n", data
, cmd
);