Debian Codegraph @

src/ec/quanta/ene_kb3940q/ec.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <arch/io.h>
4 #include <console/console.h>
5 #include <device/device.h>
6 #include <device/pnp.h>
7 #include <delay.h>
8 #include <elog.h>
9 #include <pc80/keyboard.h>
10
11 #include "ec.h"
12 #include "chip.h"
13
14 /* kbc helper functions from drivers/pc80/keyboard.c */
15 static int ec_input_buffer_empty(u8 status_port)
16 {
17 u32 timeout;
18 for (timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(status_port) & KBD_IBF);
19 timeout--) {
20 mdelay(1);
21 }
22
23 if (!timeout) {
24 printk(BIOS_WARNING, "Unexpected EC/KBD input buffer full\n");
25 }
26 return !!timeout;
27 }
28
29 static int ec_output_buffer_full(u8 status_port)
30 {
31 u32 timeout;
32 for (timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(status_port)
33 & KBD_OBF) == 0); timeout--) {
34 mdelay(1);
35 }
36
37 if (!timeout) {
38 printk(BIOS_INFO, "EC/KBD output buffer result timeout\n");
39 }
40 return !!timeout;
41 }
42
43 /* The ENE 60/64 EC registers are the same command/status IB/OB KBC pair.
44 * Check status from 64 port before each command.
45 *
46 * Ex. Get panel ID command C43/D77
47 * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port
48 * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port
49 * Check OBF set. Then Get Data(0x03:panel ID) from 0x60
50 * Different commands return may or may not respond and may have multiple
51 * bytes. Keep it simple for now
52 */
53
54 u8 ec_kbc_read_ob(void)
55 {
56 if (!ec_output_buffer_full(KBD_STATUS)) return 0;
57 return inb(KBD_DATA);
58 }
59
60 void ec_kbc_write_cmd(u8 cmd)
61 {
62 if (!ec_input_buffer_empty(KBD_STATUS)) return;
63 outb(cmd, KBD_COMMAND);
64 }
65
66 void ec_kbc_write_ib(u8 data)
67 {
68 if (!ec_input_buffer_empty(KBD_STATUS)) return;
69 outb(data, KBD_DATA);
70 }
71
72 /* EC Host Control Protocol routines */
73 u8 ec_read_ob(void)
74 {
75 if (!ec_output_buffer_full(EC_SC)) return 0;
76 return inb(EC_DATA);
77 }
78
79 void ec_write_cmd(u8 cmd)
80 {
81 if (!ec_input_buffer_empty(EC_SC)) return;
82 outb(cmd, EC_COMMAND);
83 }
84
85 void ec_write_ib(u8 data)
86 {
87 if (!ec_input_buffer_empty(EC_SC)) return;
88 outb(data, EC_DATA);
89 }
90
91 /*
92 * These functions are for accessing the ENE932 device RAM space
93 */
94 u8 ec_mem_read(u8 addr)
95 {
96 ec_write_cmd(EC_CMD_READ_RAM);
97 ec_write_ib(addr);
98 return ec_read_ob();
99 }
100
101 void ec_mem_write(u8 addr, u8 data)
102 {
103 ec_write_cmd(EC_CMD_WRITE_RAM);
104 ec_write_ib(addr);
105 ec_write_ib(data);
106 }
107
108 static void ene_kb3940q_log_events(void)
109 {
110 if (CONFIG(ELOG)) {
111 u8 reason = ec_mem_read(EC_SHUTDOWN_REASON);
112 if (reason)
113 elog_add_event_byte(ELOG_TYPE_EC_SHUTDOWN, reason);
114 }
115 }
116
117 static void ene_kb3940q_init(struct device *dev)
118 {
119 if (!dev->enabled)
120 return;
121
122 printk(BIOS_DEBUG, "Quanta EnE KB3940Q: Initializing keyboard.\n");
123 pc_keyboard_init(NO_AUX_DEVICE);
124
125 ene_kb3940q_log_events();
126 }
127
128 static struct device_operations ops = {
129 .init = ene_kb3940q_init,
130 .read_resources = noop_read_resources,
131 .set_resources = noop_set_resources,
132 };
133
134 static struct pnp_info pnp_dev_info[] = {
135 { NULL, 0, 0, 0, }
136 };
137
138 static void enable_dev(struct device *dev)
139 {
140 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
141 }
142
143 struct chip_operations ec_quanta_ene_kb3940q_ops = {
144 .name = "QUANTA EnE KB3940Q EC",
145 .enable_dev = enable_dev
146 };