1 /* SPDX-License-Identifier: GPL-2.0-only */
8 #if !(defined __NetBSD__ || defined __OpenBSD__)
13 static int ec_data
= 0x62;
14 static int ec_sc
= 0x66;
16 #if defined __NetBSD__ || defined __OpenBSD__
17 #include <machine/sysarch.h>
18 static uint8_t inb(unsigned port
)
21 __asm
volatile("inb %w1,%0" : "=a" (data
) : "d" (port
));
24 static __inline
void outb(uint8_t data
, unsigned port
)
26 __asm
volatile("outb %0,%w1" : : "a" (data
), "d" (port
));
32 #define debug(x...) if (verbose) printf(x)
34 int send_ec_command(uint8_t command
)
39 while ((inb(ec_sc
) & EC_IBF
) && --timeout
) {
41 if ((timeout
& 0xff) == 0)
45 debug("Timeout while sending command 0x%02x to EC!\n",
54 int send_ec_data(uint8_t data
)
59 while ((inb(ec_sc
) & EC_IBF
) && --timeout
) { // wait for IBF = 0
61 if ((timeout
& 0xff) == 0)
65 debug("Timeout while sending data 0x%02x to EC!\n", data
);
74 int send_ec_data_nowait(uint8_t data
)
81 uint8_t recv_ec_data(void)
87 while (--timeout
) { // Wait for OBF = 1
88 if (inb(ec_sc
) & EC_OBF
)
92 if ((timeout
& 0xff) == 0)
96 debug("\nTimeout while receiving data from EC!\n");
101 debug("recv_ec_data: 0x%02x\n", data
);
106 uint8_t ec_read(uint8_t addr
)
108 send_ec_command(RD_EC
);
111 return recv_ec_data();
114 uint8_t ec_ext_read(uint16_t addr
)
116 send_ec_command(WR_EC
);
118 send_ec_data(addr
& 0xff);
119 send_ec_command(RX_EC
);
120 send_ec_data(addr
>> 8);
122 return recv_ec_data();
125 int ec_ext_write(uint16_t addr
, uint8_t data
)
127 send_ec_command(WR_EC
);
129 send_ec_data(addr
& 0xff);
130 send_ec_command(WX_EC
);
131 send_ec_data(addr
>> 8);
133 return send_ec_data(data
);
136 int ec_write(uint8_t addr
, uint8_t data
)
138 send_ec_command(WR_EC
);
141 return send_ec_data(data
);
144 uint8_t ec_idx_read(uint16_t addr
)
146 uint16_t lpc_idx
= 0x380;
148 outb(addr
& 0xff, lpc_idx
+ 2);
149 outb(addr
>> 8, lpc_idx
+ 1);
151 return inb(lpc_idx
+ 3);
154 uint8_t ec_query(void)
156 send_ec_command(QR_EC
);
157 return recv_ec_data();
160 int get_ec_ports(void)
162 FILE *fp
= fopen("/proc/ioports", "r");
163 int data
= 0, cmd
= 0;
169 while (!feof(fp
) && (data
== 0 || cmd
== 0)) {
170 if (fgets(line
, sizeof(line
), fp
) == NULL
) {
171 fprintf(stderr
, "Can not read from /proc/ioports.\n");
174 if (strstr(line
, "EC data") != NULL
)
175 data
= strtol(line
, NULL
, 16);
177 if (strstr(line
, "EC cmd") != NULL
)
178 cmd
= strtol(line
, NULL
, 16);
182 if (data
!= 0 && cmd
!= 0) {
183 debug("EC data = 0x%x, EC cmd = 0x%x\n", data
, cmd
);