Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google Inc. |
| 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 | |
Edward O'Callaghan | b57fef9 | 2014-06-17 20:13:08 +1000 | [diff] [blame] | 22 | #include <arch/io.h> |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 23 | #include <console/console.h> |
Edward O'Callaghan | b57fef9 | 2014-06-17 20:13:08 +1000 | [diff] [blame] | 24 | #include <delay.h> |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 25 | #include <device/device.h> |
| 26 | #include <device/pnp.h> |
Edward O'Callaghan | b57fef9 | 2014-06-17 20:13:08 +1000 | [diff] [blame] | 27 | #include <pc80/keyboard.h> |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 28 | #include <stdlib.h> |
Edward O'Callaghan | b57fef9 | 2014-06-17 20:13:08 +1000 | [diff] [blame] | 29 | |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 30 | #include "ec.h" |
| 31 | #include "chip.h" |
| 32 | |
| 33 | /* helper functions from drivers/pc80/keyboard.c */ |
| 34 | static int input_buffer_empty(u16 status_reg) |
| 35 | { |
| 36 | u32 timeout; |
| 37 | for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(status_reg) & KBD_IBF); |
| 38 | timeout--) { |
Idwer Vollering | d26da9c | 2013-12-22 21:38:18 +0000 | [diff] [blame] | 39 | udelay(1000); |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | if (!timeout) { |
| 43 | printk(BIOS_WARNING, "EC-IT8518 Unexpected input buffer full\n"); |
| 44 | printk(BIOS_WARNING, " Status (0x%x): 0x%x\n", status_reg, inb(status_reg)); |
| 45 | } |
| 46 | return !!timeout; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | static int output_buffer_full(u16 status_reg) |
| 51 | { |
| 52 | u32 timeout; |
| 53 | for(timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(status_reg) |
| 54 | & KBD_OBF) == 0); timeout--) { |
Idwer Vollering | d26da9c | 2013-12-22 21:38:18 +0000 | [diff] [blame] | 55 | udelay(1000); |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | if (!timeout) { |
| 59 | printk(BIOS_INFO, "EC-IT8518 output buffer result timeout\n"); |
| 60 | printk(BIOS_INFO, " Status (0x%x): 0x%x\n", status_reg, inb(status_reg)); |
| 61 | } |
| 62 | return !!timeout; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | /* The IT8518 60/64 EC registers are the same command/status IB/OB KBC pair. |
| 68 | * Check status from 64 port before each command. |
| 69 | * |
| 70 | * Ex. Get panel ID command C43/D77 |
| 71 | * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port |
| 72 | * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port |
| 73 | * Check OBF set. Then Get Data(0x03:panel ID) from 0x60 |
| 74 | * Different commands return may or may not respond and may have multiple |
| 75 | * bytes. Keep it simple for nor |
| 76 | */ |
| 77 | |
| 78 | u8 ec_kbc_read_ob(void) |
| 79 | { |
| 80 | if (!output_buffer_full(KBD_STATUS)) return 0; |
| 81 | return inb(KBD_DATA); |
| 82 | } |
| 83 | |
| 84 | void ec_kbc_write_cmd(u8 cmd) |
| 85 | { |
| 86 | if (!input_buffer_empty(KBD_STATUS)) return; |
| 87 | outb(cmd, KBD_COMMAND); |
| 88 | } |
| 89 | |
| 90 | void ec_kbc_write_ib(u8 data) |
| 91 | { |
| 92 | if (!input_buffer_empty(KBD_STATUS)) return; |
| 93 | outb(data, KBD_DATA); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /* |
| 98 | * These functions are for accessing the IT8518 device RAM space via 0x66/0x68 |
| 99 | */ |
| 100 | |
| 101 | u8 ec_read_ob(void) |
| 102 | { |
| 103 | if (!output_buffer_full(EC_SC)) return 0; |
| 104 | return inb(EC_DATA); |
| 105 | } |
| 106 | |
| 107 | void ec_write_cmd(u8 cmd) |
| 108 | { |
| 109 | if (!input_buffer_empty(EC_SC)) return; |
| 110 | outb(cmd, EC_SC); |
| 111 | } |
| 112 | |
| 113 | void ec_write_ib(u8 data) |
| 114 | { |
| 115 | if (!input_buffer_empty(EC_SC)) return; |
| 116 | outb(data, EC_DATA); |
| 117 | } |
| 118 | |
| 119 | u8 ec_read(u16 addr) |
| 120 | { |
| 121 | ec_write_cmd(RD_EC); |
| 122 | ec_write_ib(addr); |
| 123 | return ec_read_ob(); |
| 124 | } |
| 125 | |
| 126 | void ec_write(u16 addr, u8 data) |
| 127 | { |
| 128 | ec_write_cmd(WR_EC); |
| 129 | ec_write_ib(addr); |
| 130 | ec_write_ib(data); |
| 131 | } |
| 132 | |
| 133 | #ifndef __PRE_RAM__ |
| 134 | |
| 135 | u8 ec_it8518_get_event(void) |
| 136 | { |
| 137 | u8 cmd = 0; |
| 138 | u8 status = inb(EC_SC); |
| 139 | if (status & SCI_EVT) { |
| 140 | ec_write_cmd(QR_EC); |
| 141 | cmd = ec_read_ob(); |
| 142 | } else if ( status & SMI_EVT) { |
| 143 | ec_kbc_write_cmd(EC_KBD_SMI_EVENT); |
| 144 | cmd = ec_kbc_read_ob(); |
| 145 | } |
| 146 | return cmd; |
| 147 | } |
| 148 | |
| 149 | void ec_it8518_enable_wake_events(void) |
| 150 | { |
| 151 | /* |
| 152 | * Set the bit in ECRAM that will enable the Lid switch as a wake source |
| 153 | */ |
| 154 | u8 reg8 = ec_read(EC_WAKE_SRC_ENABLE); |
| 155 | ec_write(EC_WAKE_SRC_ENABLE, reg8 | EC_LID_WAKE_ENABLE); |
| 156 | } |
| 157 | |
| 158 | #ifndef __SMM__ |
Edward O'Callaghan | 2c9d2cf | 2014-10-27 23:29:29 +1100 | [diff] [blame] | 159 | static void it8518_init(struct device *dev) |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 160 | { |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 161 | if (!dev->enabled) |
| 162 | return; |
| 163 | |
| 164 | printk(BIOS_DEBUG, "Quanta IT8518: Initializing keyboard.\n"); |
Edward O'Callaghan | def00be | 2014-04-30 05:01:52 +1000 | [diff] [blame] | 165 | pc_keyboard_init(); |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 168 | static struct device_operations ops = { |
| 169 | .init = it8518_init, |
Edward O'Callaghan | 5f19eb6 | 2014-11-29 00:03:03 +1100 | [diff] [blame^] | 170 | .read_resources = DEVICE_NOOP, |
| 171 | .enable_resources = DEVICE_NOOP, |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | static struct pnp_info pnp_dev_info[] = { |
| 175 | { &ops, 0, 0, { 0, 0 }, } |
| 176 | }; |
| 177 | |
Edward O'Callaghan | 2c9d2cf | 2014-10-27 23:29:29 +1100 | [diff] [blame] | 178 | static void enable_dev(struct device *dev) |
Stefan Reinauer | 7e56855 | 2013-03-13 17:03:04 -0700 | [diff] [blame] | 179 | { |
| 180 | pnp_enable_devices(dev, &pnp_ops, ARRAY_SIZE(pnp_dev_info), |
| 181 | pnp_dev_info); |
| 182 | } |
| 183 | |
| 184 | struct chip_operations ec_quanta_it8518_ops = { |
| 185 | CHIP_NAME("QUANTA IT8518 EC") |
| 186 | .enable_dev = enable_dev |
| 187 | }; |
| 188 | #endif /* ! __SMM__ */ |
| 189 | #endif /* ! __PRE_RAM__ */ |