blob: 37f26b1f6948343e8f16b5139acf3848e6fd1a90 [file] [log] [blame]
Angel Pons210a0082020-04-02 23:48:24 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -08002
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -08003#include <console/console.h>
4#include <device/device.h>
5#include <device/pnp.h>
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -08006#include <arch/io.h>
7#include <delay.h>
Edward O'Callaghane1fe6882014-04-30 20:41:41 +10008#include <pc80/keyboard.h>
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -08009#include "ec.h"
10#include "chip.h"
11
12/* kbc helper functions from drivers/pc80/keyboard.c. TODO: share functions. */
13static int kbc_input_buffer_empty(void)
14{
15 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020016 for (timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) & KBD_IBF);
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -080017 timeout--) {
18 mdelay(1);
19 }
20
21 if (!timeout) {
22 printk(BIOS_WARNING,
23 "Unexpected Keyboard controller input buffer full\n");
24 }
25 return !!timeout;
26}
27
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -080028static int kbc_output_buffer_full(void)
29{
30 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020031 for (timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(KBD_STATUS)
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -080032 & KBD_OBF) == 0); timeout--) {
33 mdelay(1);
34 }
35
36 if (!timeout) {
37 printk(BIOS_INFO, "Keyboard controller output buffer result timeout\n");
38 }
39 return !!timeout;
40}
41
42int kbc_cleanup_buffers(void)
43{
44 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020045 for (timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS)
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -080046 & (KBD_OBF | KBD_IBF)); timeout--) {
47 mdelay(1);
48 inb(KBD_DATA);
49 }
50
51 if (!timeout) {
52 printk(BIOS_ERR, "Couldn't cleanup the keyboard controller buffers\n");
53 printk(BIOS_ERR, "Status (0x%x): 0x%x, Buffer (0x%x): 0x%x\n",
54 KBD_STATUS, inb(KBD_STATUS), KBD_DATA, inb(KBD_DATA));
55 }
56
57 return !!timeout;
58}
59
Martin Roth8940d3e2013-07-09 21:52:41 -060060/* The ENE 60/64 EC registers are the same command/status IB/OB KBC pair.
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -080061 * Check status from 64 port before each command.
62 *
63 * Ex. Get panel ID command C43/D77
64 * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port
65 * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port
66 * Check OBF set. Then Get Data(0x03:panel ID) from 0x60
Martin Roth8940d3e2013-07-09 21:52:41 -060067 * Different commands return may or may not respond and may have multiple
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -080068 * bytes. Keep it simple for nor
69 */
70
71u8 ec_kbc_read_ob(void)
72{
73 if (!kbc_output_buffer_full()) return 0;
74 return inb(KBD_DATA);
75}
76
77void ec_kbc_write_cmd(u8 cmd)
78{
79 if (!kbc_input_buffer_empty()) return;
80 outb(cmd, KBD_COMMAND);
81}
82
83void ec_kbc_write_ib(u8 data)
84{
85 if (!kbc_input_buffer_empty()) return;
86 outb(data, KBD_DATA);
87}
88
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -080089/*
90 * These functions are for accessing the ENE932 device space, but are not
91 * currently used.
92 */
93/*
94static u8 ec_io_read(u16 addr)
95{
96 outb(addr >> 8, EC_IO_HIGH);
97 outb(addr & 0xff, EC_IO_LOW);
98 return inb(EC_IO_DATA);
99}
100*/
101/*static void ec_write(u16 addr, u8 data)
102{
103 outb(addr >> 8, EC_IO_HIGH);
104 outb(addr & 0xff, EC_IO_LOW;
105 outb(data, EC_IO_DATA);
106}
107*/
108
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100109static void ene932_init(struct device *dev)
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -0800110{
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -0800111 if (!dev->enabled)
112 return;
113
114 printk(BIOS_DEBUG, "Compal ENE932: Initializing keyboard.\n");
Timothy Pearson448e3862015-11-24 14:12:01 -0600115 pc_keyboard_init(NO_AUX_DEVICE);
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -0800116}
117
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -0800118static struct device_operations ops = {
119 .init = ene932_init,
Nico Huber2f8ba692020-04-05 14:05:24 +0200120 .read_resources = noop_read_resources,
Elyes HAOUAS4d319c32020-04-07 13:51:13 +0200121 .set_resources = noop_set_resources,
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -0800122};
123
124static struct pnp_info pnp_dev_info[] = {
Felix Helddd770a82018-07-07 00:03:47 +0200125 { NULL, 0, 0, 0, }
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -0800126};
127
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100128static void enable_dev(struct device *dev)
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -0800129{
Felix Helddd770a82018-07-07 00:03:47 +0200130 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -0800131}
132
133struct chip_operations ec_compal_ene932_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900134 .name = "COMPAL ENE932 EC",
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -0800135 .enable_dev = enable_dev
136};