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