Stefan Reinauer | d8a5fd2 | 2012-12-11 15:51:47 -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 | d8a5fd2 | 2012-12-11 15:51:47 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #ifndef __PRE_RAM__ |
| 18 | |
| 19 | #include <console/console.h> |
| 20 | #include <device/device.h> |
| 21 | #include <device/pnp.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <arch/io.h> |
| 24 | #include <delay.h> |
Edward O'Callaghan | e1fe688 | 2014-04-30 20:41:41 +1000 | [diff] [blame] | 25 | #include <pc80/keyboard.h> |
Stefan Reinauer | d8a5fd2 | 2012-12-11 15:51:47 -0800 | [diff] [blame] | 26 | #include "ec.h" |
| 27 | #include "chip.h" |
| 28 | |
| 29 | /* kbc helper functions from drivers/pc80/keyboard.c. TODO: share functions. */ |
| 30 | static int kbc_input_buffer_empty(void) |
| 31 | { |
| 32 | u32 timeout; |
| 33 | for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) & KBD_IBF); |
| 34 | timeout--) { |
| 35 | mdelay(1); |
| 36 | } |
| 37 | |
| 38 | if (!timeout) { |
| 39 | printk(BIOS_WARNING, |
| 40 | "Unexpected Keyboard controller input buffer full\n"); |
| 41 | } |
| 42 | return !!timeout; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | static int kbc_output_buffer_full(void) |
| 47 | { |
| 48 | u32 timeout; |
| 49 | for(timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(KBD_STATUS) |
| 50 | & KBD_OBF) == 0); timeout--) { |
| 51 | mdelay(1); |
| 52 | } |
| 53 | |
| 54 | if (!timeout) { |
| 55 | printk(BIOS_INFO, "Keyboard controller output buffer result timeout\n"); |
| 56 | } |
| 57 | return !!timeout; |
| 58 | } |
| 59 | |
| 60 | int kbc_cleanup_buffers(void) |
| 61 | { |
| 62 | u32 timeout; |
| 63 | for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) |
| 64 | & (KBD_OBF | KBD_IBF)); timeout--) { |
| 65 | mdelay(1); |
| 66 | inb(KBD_DATA); |
| 67 | } |
| 68 | |
| 69 | if (!timeout) { |
| 70 | printk(BIOS_ERR, "Couldn't cleanup the keyboard controller buffers\n"); |
| 71 | printk(BIOS_ERR, "Status (0x%x): 0x%x, Buffer (0x%x): 0x%x\n", |
| 72 | KBD_STATUS, inb(KBD_STATUS), KBD_DATA, inb(KBD_DATA)); |
| 73 | } |
| 74 | |
| 75 | return !!timeout; |
| 76 | } |
| 77 | |
| 78 | |
Martin Roth | 8940d3e | 2013-07-09 21:52:41 -0600 | [diff] [blame] | 79 | /* The ENE 60/64 EC registers are the same command/status IB/OB KBC pair. |
Stefan Reinauer | d8a5fd2 | 2012-12-11 15:51:47 -0800 | [diff] [blame] | 80 | * Check status from 64 port before each command. |
| 81 | * |
| 82 | * Ex. Get panel ID command C43/D77 |
| 83 | * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port |
| 84 | * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port |
| 85 | * Check OBF set. Then Get Data(0x03:panel ID) from 0x60 |
Martin Roth | 8940d3e | 2013-07-09 21:52:41 -0600 | [diff] [blame] | 86 | * Different commands return may or may not respond and may have multiple |
Stefan Reinauer | d8a5fd2 | 2012-12-11 15:51:47 -0800 | [diff] [blame] | 87 | * bytes. Keep it simple for nor |
| 88 | */ |
| 89 | |
| 90 | u8 ec_kbc_read_ob(void) |
| 91 | { |
| 92 | if (!kbc_output_buffer_full()) return 0; |
| 93 | return inb(KBD_DATA); |
| 94 | } |
| 95 | |
| 96 | void ec_kbc_write_cmd(u8 cmd) |
| 97 | { |
| 98 | if (!kbc_input_buffer_empty()) return; |
| 99 | outb(cmd, KBD_COMMAND); |
| 100 | } |
| 101 | |
| 102 | void ec_kbc_write_ib(u8 data) |
| 103 | { |
| 104 | if (!kbc_input_buffer_empty()) return; |
| 105 | outb(data, KBD_DATA); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /* |
| 110 | * These functions are for accessing the ENE932 device space, but are not |
| 111 | * currently used. |
| 112 | */ |
| 113 | /* |
| 114 | static u8 ec_io_read(u16 addr) |
| 115 | { |
| 116 | outb(addr >> 8, EC_IO_HIGH); |
| 117 | outb(addr & 0xff, EC_IO_LOW); |
| 118 | return inb(EC_IO_DATA); |
| 119 | } |
| 120 | */ |
| 121 | /*static void ec_write(u16 addr, u8 data) |
| 122 | { |
| 123 | outb(addr >> 8, EC_IO_HIGH); |
| 124 | outb(addr & 0xff, EC_IO_LOW; |
| 125 | outb(data, EC_IO_DATA); |
| 126 | } |
| 127 | */ |
| 128 | |
| 129 | #ifndef __SMM__ |
Edward O'Callaghan | 2c9d2cf | 2014-10-27 23:29:29 +1100 | [diff] [blame] | 130 | static void ene932_init(struct device *dev) |
Stefan Reinauer | d8a5fd2 | 2012-12-11 15:51:47 -0800 | [diff] [blame] | 131 | { |
Stefan Reinauer | d8a5fd2 | 2012-12-11 15:51:47 -0800 | [diff] [blame] | 132 | if (!dev->enabled) |
| 133 | return; |
| 134 | |
| 135 | printk(BIOS_DEBUG, "Compal ENE932: Initializing keyboard.\n"); |
Edward O'Callaghan | def00be | 2014-04-30 05:01:52 +1000 | [diff] [blame] | 136 | pc_keyboard_init(); |
Stefan Reinauer | d8a5fd2 | 2012-12-11 15:51:47 -0800 | [diff] [blame] | 137 | |
| 138 | } |
| 139 | |
Stefan Reinauer | d8a5fd2 | 2012-12-11 15:51:47 -0800 | [diff] [blame] | 140 | static struct device_operations ops = { |
| 141 | .init = ene932_init, |
Edward O'Callaghan | 5f19eb6 | 2014-11-29 00:03:03 +1100 | [diff] [blame] | 142 | .read_resources = DEVICE_NOOP, |
| 143 | .enable_resources = DEVICE_NOOP, |
Stefan Reinauer | d8a5fd2 | 2012-12-11 15:51:47 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static struct pnp_info pnp_dev_info[] = { |
| 147 | { &ops, 0, 0, { 0, 0 }, } |
| 148 | }; |
| 149 | |
Edward O'Callaghan | 2c9d2cf | 2014-10-27 23:29:29 +1100 | [diff] [blame] | 150 | static void enable_dev(struct device *dev) |
Stefan Reinauer | d8a5fd2 | 2012-12-11 15:51:47 -0800 | [diff] [blame] | 151 | { |
| 152 | pnp_enable_devices(dev, &pnp_ops, ARRAY_SIZE(pnp_dev_info), |
| 153 | pnp_dev_info); |
| 154 | } |
| 155 | |
| 156 | struct chip_operations ec_compal_ene932_ops = { |
| 157 | CHIP_NAME("COMPAL ENE932 EC") |
| 158 | .enable_dev = enable_dev |
| 159 | }; |
| 160 | #endif /* ! __SMM__ */ |
| 161 | #endif /* ! __PRE_RAM__ */ |