Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2008-2009 coresystems GmbH |
| 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 | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <console/console.h> |
Sven Schnelle | 7592e8b | 2011-01-27 11:43:03 +0000 | [diff] [blame] | 18 | #include <device/device.h> |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 19 | #include <arch/io.h> |
| 20 | #include <delay.h> |
| 21 | #include "ec.h" |
| 22 | |
Vladimir Serbinenko | fe50d0b | 2013-06-07 01:55:57 +0200 | [diff] [blame] | 23 | #ifdef __PRE_RAM__ |
| 24 | |
| 25 | static const int ec_cmd_reg = EC_SC; |
| 26 | static const int ec_data_reg = EC_DATA; |
| 27 | |
| 28 | #else |
| 29 | |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 30 | static int ec_cmd_reg = EC_SC; |
| 31 | static int ec_data_reg = EC_DATA; |
| 32 | |
Vladimir Serbinenko | fe50d0b | 2013-06-07 01:55:57 +0200 | [diff] [blame] | 33 | #endif |
| 34 | |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 35 | int send_ec_command(u8 command) |
| 36 | { |
| 37 | int timeout; |
| 38 | |
| 39 | timeout = 0x7ff; |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 40 | while ((inb(ec_cmd_reg) & EC_IBF) && --timeout) { |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 41 | udelay(10); |
| 42 | if ((timeout & 0xff) == 0) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 43 | printk(BIOS_SPEW, "."); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 44 | } |
| 45 | if (!timeout) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 46 | printk(BIOS_DEBUG, "Timeout while sending command 0x%02x to EC!\n", |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 47 | command); |
| 48 | // return -1; |
| 49 | } |
| 50 | |
Vladimir Serbinenko | ea9181c | 2014-01-05 06:50:10 +0100 | [diff] [blame] | 51 | udelay(10); |
| 52 | |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 53 | outb(command, ec_cmd_reg); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | int send_ec_data(u8 data) |
| 58 | { |
| 59 | int timeout; |
| 60 | |
| 61 | timeout = 0x7ff; |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 62 | while ((inb(ec_cmd_reg) & EC_IBF) && --timeout) { // wait for IBF = 0 |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 63 | udelay(10); |
| 64 | if ((timeout & 0xff) == 0) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 65 | printk(BIOS_SPEW, "."); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 66 | } |
| 67 | if (!timeout) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 68 | printk(BIOS_DEBUG, "Timeout while sending data 0x%02x to EC!\n", |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 69 | data); |
| 70 | // return -1; |
| 71 | } |
| 72 | |
Vladimir Serbinenko | ea9181c | 2014-01-05 06:50:10 +0100 | [diff] [blame] | 73 | udelay(10); |
| 74 | |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 75 | outb(data, ec_data_reg); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int send_ec_data_nowait(u8 data) |
| 81 | { |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 82 | outb(data, ec_data_reg); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | u8 recv_ec_data(void) |
| 88 | { |
| 89 | int timeout; |
| 90 | u8 data; |
| 91 | |
| 92 | timeout = 0x7fff; |
| 93 | while (--timeout) { // Wait for OBF = 1 |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 94 | if (inb(ec_cmd_reg) & EC_OBF) { |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 95 | break; |
| 96 | } |
| 97 | udelay(10); |
| 98 | if ((timeout & 0xff) == 0) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 99 | printk(BIOS_SPEW, "."); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 100 | } |
| 101 | if (!timeout) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 102 | printk(BIOS_DEBUG, "\nTimeout while receiving data from EC!\n"); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 103 | // return -1; |
| 104 | } |
| 105 | |
Vladimir Serbinenko | ea9181c | 2014-01-05 06:50:10 +0100 | [diff] [blame] | 106 | udelay(10); |
| 107 | |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 108 | data = inb(ec_data_reg); |
Sven Schnelle | 5d9a83c | 2011-06-12 16:53:25 +0200 | [diff] [blame] | 109 | printk(BIOS_SPEW, "recv_ec_data: 0x%02x\n", data); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 110 | |
| 111 | return data; |
| 112 | } |
| 113 | |
| 114 | u8 ec_read(u8 addr) |
| 115 | { |
| 116 | send_ec_command(0x80); |
| 117 | send_ec_data(addr); |
| 118 | |
| 119 | return recv_ec_data(); |
| 120 | } |
| 121 | |
| 122 | int ec_write(u8 addr, u8 data) |
| 123 | { |
| 124 | send_ec_command(0x81); |
| 125 | send_ec_data(addr); |
| 126 | return send_ec_data(data); |
| 127 | } |
| 128 | |
Sven Schnelle | f9a0b92 | 2011-03-15 09:52:07 +0000 | [diff] [blame] | 129 | u8 ec_query(void) |
| 130 | { |
| 131 | send_ec_command(0x84); |
| 132 | return recv_ec_data(); |
| 133 | } |
| 134 | |
Sven Schnelle | 8d94d7c | 2011-03-14 08:18:17 +0000 | [diff] [blame] | 135 | void ec_set_bit(u8 addr, u8 bit) |
| 136 | { |
| 137 | ec_write(addr, ec_read(addr) | (1 << bit)); |
| 138 | } |
| 139 | |
| 140 | void ec_clr_bit(u8 addr, u8 bit) |
| 141 | { |
| 142 | ec_write(addr, ec_read(addr) & ~(1 << bit)); |
| 143 | } |
| 144 | |
Vladimir Serbinenko | fe50d0b | 2013-06-07 01:55:57 +0200 | [diff] [blame] | 145 | #ifndef __PRE_RAM__ |
| 146 | |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 147 | void ec_set_ports(u16 cmd_reg, u16 data_reg) |
| 148 | { |
| 149 | ec_cmd_reg = cmd_reg; |
| 150 | ec_data_reg = data_reg; |
| 151 | } |
| 152 | |
Vladimir Serbinenko | fe50d0b | 2013-06-07 01:55:57 +0200 | [diff] [blame] | 153 | #endif |
| 154 | |
Stefan Reinauer | 24d1d4b | 2013-03-21 11:51:41 -0700 | [diff] [blame] | 155 | #if !defined(__SMM__) && !defined(__PRE_RAM__) |
Sven Schnelle | 7592e8b | 2011-01-27 11:43:03 +0000 | [diff] [blame] | 156 | struct chip_operations ec_acpi_ops = { |
| 157 | CHIP_NAME("ACPI Embedded Controller") |
| 158 | }; |
Stefan Reinauer | 24d1d4b | 2013-03-21 11:51:41 -0700 | [diff] [blame] | 159 | #endif |