Stefan Reinauer | 020b22a | 2012-03-30 17:06:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2011 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 | 020b22a | 2012-03-30 17:06:43 -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 | 020b22a | 2012-03-30 17:06:43 -0700 | [diff] [blame] | 18 | #include <console/console.h> |
| 19 | #include <device/device.h> |
Stefan Reinauer | 020b22a | 2012-03-30 17:06:43 -0700 | [diff] [blame] | 20 | #include <delay.h> |
Edward O'Callaghan | b57fef9 | 2014-06-17 20:13:08 +1000 | [diff] [blame] | 21 | |
Stefan Reinauer | 020b22a | 2012-03-30 17:06:43 -0700 | [diff] [blame] | 22 | #include "ec.h" |
| 23 | #include "chip.h" |
| 24 | |
| 25 | static u16 ec_cmd_reg = 0; |
| 26 | static u16 ec_data_reg = 0; |
| 27 | |
| 28 | static inline u8 __ec_read(u8 addr) |
| 29 | { |
| 30 | outb(addr, ec_cmd_reg); |
| 31 | return inb(ec_data_reg); |
| 32 | } |
| 33 | |
| 34 | static inline void __ec_write(u8 addr, u8 data) |
| 35 | { |
| 36 | outb(addr, ec_cmd_reg); |
| 37 | outb(data, ec_data_reg); |
| 38 | } |
| 39 | |
| 40 | static int ec_ready(void) |
| 41 | { |
| 42 | u16 timeout = EC_TIMEOUT; |
| 43 | |
| 44 | if (!ec_cmd_reg || !ec_data_reg) { |
| 45 | printk(BIOS_DEBUG, "Invalid ports: cmd=0x%x data=0x%x\n", |
| 46 | ec_cmd_reg, ec_data_reg); |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | while (__ec_read(EC_MAILBOX_COMMAND) != 0 && --timeout) { |
| 51 | udelay(10); |
| 52 | if ((timeout & 0xff) == 0) |
| 53 | printk(BIOS_SPEW, "."); |
| 54 | } |
| 55 | if (!timeout) { |
| 56 | printk(BIOS_DEBUG, "Timeout waiting for EC to be ready.\n"); |
| 57 | return -1; |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int send_ec_command(u8 command) |
| 63 | { |
| 64 | if (ec_ready() < 0) |
| 65 | return -1; |
| 66 | __ec_write(EC_MAILBOX_COMMAND, command); |
| 67 | return ec_ready(); |
| 68 | } |
| 69 | |
| 70 | int send_ec_command_data(u8 command, u8 data) |
| 71 | { |
| 72 | if (ec_ready() < 0) |
| 73 | return -1; |
| 74 | __ec_write(EC_MAILBOX_DATA, data); |
| 75 | __ec_write(EC_MAILBOX_COMMAND, command); |
| 76 | return ec_ready(); |
| 77 | } |
| 78 | |
| 79 | u8 read_ec_command_byte(u8 command) |
| 80 | { |
| 81 | send_ec_command(command); |
| 82 | return __ec_read(EC_MAILBOX_DATA); |
| 83 | } |
| 84 | |
| 85 | u8 ec_read(u8 addr) |
| 86 | { |
| 87 | if (send_ec_command_data(EC_RAM_READ, addr) < 0) |
| 88 | return 0; |
| 89 | return __ec_read(EC_MAILBOX_DATA); |
| 90 | } |
| 91 | |
| 92 | int ec_write(u8 addr, u8 data) |
| 93 | { |
| 94 | if (ec_ready() < 0) |
| 95 | return -1; |
| 96 | __ec_write(EC_MAILBOX_DATA, addr); |
| 97 | __ec_write(EC_MAILBOX_DATA_H, data); |
| 98 | __ec_write(EC_MAILBOX_COMMAND, EC_RAM_WRITE); |
| 99 | return ec_ready(); |
| 100 | } |
| 101 | |
| 102 | void ec_set_bit(u8 addr, u8 bit) |
| 103 | { |
| 104 | ec_write(addr, ec_read(addr) | (1 << bit)); |
| 105 | } |
| 106 | |
| 107 | void ec_clr_bit(u8 addr, u8 bit) |
| 108 | { |
| 109 | ec_write(addr, ec_read(addr) & ~(1 << bit)); |
| 110 | } |
| 111 | |
| 112 | void ec_set_ports(u16 cmd_reg, u16 data_reg) |
| 113 | { |
| 114 | ec_cmd_reg = cmd_reg; |
| 115 | ec_data_reg = data_reg; |
| 116 | } |
| 117 | |
Stefan Reinauer | 24d1d4b | 2013-03-21 11:51:41 -0700 | [diff] [blame] | 118 | #if !defined(__PRE_RAM__) && !defined(__SMM__) |
Edward O'Callaghan | 2c9d2cf | 2014-10-27 23:29:29 +1100 | [diff] [blame] | 119 | static void mec1308_enable(struct device *dev) |
Stefan Reinauer | 020b22a | 2012-03-30 17:06:43 -0700 | [diff] [blame] | 120 | { |
| 121 | struct ec_smsc_mec1308_config *conf = dev->chip_info; |
| 122 | |
| 123 | if (conf->mailbox_port) { |
| 124 | ec_cmd_reg = conf->mailbox_port; |
| 125 | ec_data_reg = conf->mailbox_port + 1; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | struct chip_operations ec_smsc_mec1308_ops = { |
| 130 | CHIP_NAME("SMSC MEC1308 EC Mailbox Interface") |
| 131 | .enable_dev = mec1308_enable |
| 132 | }; |
Stefan Reinauer | 24d1d4b | 2013-03-21 11:51:41 -0700 | [diff] [blame] | 133 | #endif |