Debian Codegraph @

src/ec/lenovo/mec1653/debug.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <arch/io.h>
4 #include <ec/acpi/ec.h>
5 #include "debug.h"
6 #include "mec1653.h"
7
8
9 static void debug_cmd(uint8_t cmd)
10 {
11 ec_set_ports(EC_SC, EC_DATA);
12 ec_write(EC_DEBUG_CMD, cmd);
13 while (ec_read(EC_DEBUG_CMD) & 0x80)
14 ;
15 }
16
17 uint32_t debug_read_dword(uint32_t addr)
18 {
19 ec_set_ports(EC3_CMD, EC3_DATA);
20 ec_clear_out_queue();
21 ec_ready_send(EC_SEND_TIMEOUT_US);
22 outl(addr << 8 | 0xE2, EC3_DATA);
23 ec_ready_recv(EC_SEND_TIMEOUT_US);
24 return inl(EC3_DATA);
25 }
26
27 void debug_write_dword(uint32_t addr, uint32_t val)
28 {
29 ec_set_ports(EC3_CMD, EC3_DATA);
30 ec_clear_out_queue();
31 ec_ready_send(EC_SEND_TIMEOUT_US);
32 outl(addr << 8 | 0xEA, EC3_DATA);
33 ec_ready_send(EC_SEND_TIMEOUT_US);
34 outl(val, EC3_DATA);
35 }
36
37 // Helper function to convert hex character to byte
38 static uint8_t hex_char_to_byte(char c)
39 {
40 if (c >= '0' && c <= '9')
41 return c - '0';
42 if (c >= 'a' && c <= 'f')
43 return c - 'a' + 10;
44 if (c >= 'A' && c <= 'F')
45 return c - 'A' + 10;
46 return 0; // Invalid character
47 }
48
49 // Helper function to convert 16-char hex string to 8 bytes
50 static void hex_string_to_bytes(const char *hex_str, uint8_t *bytes)
51 {
52 for (int i = 0; i < 8; ++i) {
53 bytes[i] = (hex_char_to_byte(hex_str[i * 2]) << 4) |
54 hex_char_to_byte(hex_str[i * 2 + 1]);
55 }
56 }
57
58 void debug_write_key(uint8_t i, const char *hex_key)
59 {
60 uint8_t key_bytes[8];
61
62 ec_set_ports(EC_SC, EC_DATA);
63 hex_string_to_bytes(hex_key, key_bytes);
64
65 for (int j = 0; j < 8; ++j)
66 ec_write(0x3e + j, key_bytes[j]);
67 debug_cmd(0xc0 | (i & 0xf));
68 }
69
70 void debug_read_key(uint8_t i, uint8_t *key)
71 {
72 debug_cmd(0x80 | (i & 0xf));
73 for (int j = 0; j < 8; ++j)
74 key[j] = ec_read(0x3e + j);
75 }
76
77 uint16_t debug_loaded_keys(void)
78 {
79 ec_set_ports(EC_SC, EC_DATA);
80 return (uint16_t) ec_read(0x87) << 8 | (uint16_t) ec_read(0x86);
81 }