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