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