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. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef __PRE_RAM__ |
| 23 | |
Stefan Reinauer | 24d1d4b | 2013-03-21 11:51:41 -0700 | [diff] [blame] | 24 | #include <arch/io.h> |
Edward O'Callaghan | b57fef9 | 2014-06-17 20:13:08 +1000 | [diff] [blame] | 25 | #include <console/console.h> |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 26 | #include <device/device.h> |
| 27 | #include <device/pnp.h> |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 28 | #include <delay.h> |
| 29 | #include <elog.h> |
Edward O'Callaghan | b57fef9 | 2014-06-17 20:13:08 +1000 | [diff] [blame] | 30 | #include <stdlib.h> |
Edward O'Callaghan | e1fe688 | 2014-04-30 20:41:41 +1000 | [diff] [blame] | 31 | #include <pc80/keyboard.h> |
Edward O'Callaghan | b57fef9 | 2014-06-17 20:13:08 +1000 | [diff] [blame] | 32 | |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 33 | #include "ec.h" |
| 34 | #include "chip.h" |
| 35 | |
| 36 | /* kbc helper functions from drivers/pc80/keyboard.c */ |
| 37 | static int ec_input_buffer_empty(u8 status_port) |
| 38 | { |
| 39 | u32 timeout; |
| 40 | for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(status_port) & KBD_IBF); |
| 41 | timeout--) { |
| 42 | mdelay(1); |
| 43 | } |
| 44 | |
| 45 | if (!timeout) { |
| 46 | printk(BIOS_WARNING, "Unexpected EC/KBD input buffer full\n"); |
| 47 | } |
| 48 | return !!timeout; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | static int ec_output_buffer_full(u8 status_port) |
| 53 | { |
| 54 | u32 timeout; |
| 55 | for(timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(status_port) |
| 56 | & KBD_OBF) == 0); timeout--) { |
| 57 | mdelay(1); |
| 58 | } |
| 59 | |
| 60 | if (!timeout) { |
| 61 | printk(BIOS_INFO, "EC/KBD output buffer result timeout\n"); |
| 62 | } |
| 63 | return !!timeout; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | /* The ENE 60/64 EC registers are the same command/status IB/OB KBC pair. |
| 69 | * Check status from 64 port before each command. |
| 70 | * |
| 71 | * Ex. Get panel ID command C43/D77 |
| 72 | * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port |
| 73 | * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port |
| 74 | * Check OBF set. Then Get Data(0x03:panel ID) from 0x60 |
| 75 | * Different commands return may or may not respond and may have multiple |
| 76 | * bytes. Keep it simple for now |
| 77 | */ |
| 78 | |
| 79 | u8 ec_kbc_read_ob(void) |
| 80 | { |
| 81 | if (!ec_output_buffer_full(KBD_STATUS)) return 0; |
| 82 | return inb(KBD_DATA); |
| 83 | } |
| 84 | |
| 85 | void ec_kbc_write_cmd(u8 cmd) |
| 86 | { |
| 87 | if (!ec_input_buffer_empty(KBD_STATUS)) return; |
| 88 | outb(cmd, KBD_COMMAND); |
| 89 | } |
| 90 | |
| 91 | void ec_kbc_write_ib(u8 data) |
| 92 | { |
| 93 | if (!ec_input_buffer_empty(KBD_STATUS)) return; |
| 94 | outb(data, KBD_DATA); |
| 95 | } |
| 96 | |
| 97 | /* EC Host Control Protocol routines */ |
| 98 | u8 ec_read_ob(void) |
| 99 | { |
| 100 | if (!ec_output_buffer_full(EC_SC)) return 0; |
| 101 | return inb(EC_DATA); |
| 102 | } |
| 103 | |
| 104 | void ec_write_cmd(u8 cmd) |
| 105 | { |
| 106 | if (!ec_input_buffer_empty(EC_SC)) return; |
| 107 | outb(cmd, EC_COMMAND); |
| 108 | } |
| 109 | |
| 110 | void ec_write_ib(u8 data) |
| 111 | { |
| 112 | if (!ec_input_buffer_empty(EC_SC)) return; |
| 113 | outb(data, EC_DATA); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * These functions are for accessing the ENE932 device RAM space |
| 118 | */ |
| 119 | u8 ec_mem_read(u8 addr) |
| 120 | { |
| 121 | ec_write_cmd(EC_CMD_READ_RAM); |
| 122 | ec_write_ib(addr); |
| 123 | return ec_read_ob(); |
| 124 | } |
| 125 | |
| 126 | void ec_mem_write(u8 addr, u8 data) |
| 127 | { |
| 128 | ec_write_cmd(EC_CMD_WRITE_RAM); |
| 129 | ec_write_ib(addr); |
| 130 | ec_write_ib(data); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | #ifndef __SMM__ |
| 135 | static void ene_kb3940q_log_events(void) |
| 136 | { |
| 137 | #if CONFIG_ELOG |
| 138 | u8 reason = ec_mem_read(EC_SHUTDOWN_REASON); |
| 139 | if (reason) |
| 140 | elog_add_event_byte(ELOG_TYPE_EC_SHUTDOWN, reason); |
| 141 | #endif |
| 142 | } |
| 143 | |
| 144 | static void ene_kb3940q_init(device_t dev) |
| 145 | { |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 146 | if (!dev->enabled) |
| 147 | return; |
| 148 | |
| 149 | printk(BIOS_DEBUG, "Quanta EnE KB3940Q: Initializing keyboard.\n"); |
Edward O'Callaghan | def00be | 2014-04-30 05:01:52 +1000 | [diff] [blame] | 150 | pc_keyboard_init(); |
Stefan Reinauer | d7bd4eb | 2013-02-11 11:11:36 -0800 | [diff] [blame] | 151 | |
| 152 | ene_kb3940q_log_events(); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | static void ene_kb3940q_read_resources(device_t dev) |
| 157 | { |
| 158 | /* This function avoids an error on serial console. */ |
| 159 | } |
| 160 | |
| 161 | |
| 162 | static void ene_kb3940q_enable_resources(device_t dev) |
| 163 | { |
| 164 | /* This function avoids an error on serial console. */ |
| 165 | } |
| 166 | |
| 167 | static struct device_operations ops = { |
| 168 | .init = ene_kb3940q_init, |
| 169 | .read_resources = ene_kb3940q_read_resources, |
| 170 | .enable_resources = ene_kb3940q_enable_resources |
| 171 | }; |
| 172 | |
| 173 | static struct pnp_info pnp_dev_info[] = { |
| 174 | { &ops, 0, 0, { 0, 0 }, } |
| 175 | }; |
| 176 | |
| 177 | static void enable_dev(device_t dev) |
| 178 | { |
| 179 | pnp_enable_devices(dev, &pnp_ops, ARRAY_SIZE(pnp_dev_info), |
| 180 | pnp_dev_info); |
| 181 | } |
| 182 | |
| 183 | struct chip_operations ec_quanta_ene_kb3940q_ops = { |
| 184 | CHIP_NAME("QUANTA EnE KB3940Q EC") |
| 185 | .enable_dev = enable_dev |
| 186 | }; |
| 187 | #endif /* ! __SMM__ */ |
| 188 | #endif /* ! __PRE_RAM__ */ |