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