2008-10-29 05:46:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the coreboot project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Yinghai Lu <yinghailu@gmail.com>
|
2009-03-11 15:54:18 +01:00
|
|
|
* Copyright (C) 2009 coresystems GmbH
|
2008-10-29 05:46:52 +01:00
|
|
|
*
|
2010-02-16 00:10:19 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
2008-10-29 05:46:52 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <device/smbus_def.h>
|
2010-12-07 20:16:07 +01:00
|
|
|
#include "i82801gx.h"
|
2010-10-05 11:07:10 +02:00
|
|
|
|
2008-10-29 05:46:52 +01:00
|
|
|
static void smbus_delay(void)
|
|
|
|
{
|
|
|
|
inb(0x80);
|
|
|
|
}
|
|
|
|
|
2009-03-11 15:54:18 +01:00
|
|
|
static int smbus_wait_until_ready(u16 smbus_base)
|
2008-10-29 05:46:52 +01:00
|
|
|
{
|
|
|
|
unsigned loops = SMBUS_TIMEOUT;
|
|
|
|
unsigned char byte;
|
|
|
|
do {
|
|
|
|
smbus_delay();
|
|
|
|
if (--loops == 0)
|
|
|
|
break;
|
2009-03-11 15:54:18 +01:00
|
|
|
byte = inb(smbus_base + SMBHSTSTAT);
|
2008-10-29 05:46:52 +01:00
|
|
|
} while (byte & 1);
|
|
|
|
return loops ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2009-03-11 15:54:18 +01:00
|
|
|
static int smbus_wait_until_done(u16 smbus_base)
|
2008-10-29 05:46:52 +01:00
|
|
|
{
|
|
|
|
unsigned loops = SMBUS_TIMEOUT;
|
|
|
|
unsigned char byte;
|
|
|
|
do {
|
|
|
|
smbus_delay();
|
|
|
|
if (--loops == 0)
|
|
|
|
break;
|
2009-03-11 15:54:18 +01:00
|
|
|
byte = inb(smbus_base + SMBHSTSTAT);
|
2008-10-29 05:46:52 +01:00
|
|
|
} while ((byte & 1) || (byte & ~((1 << 6) | (1 << 0))) == 0);
|
|
|
|
return loops ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2009-03-11 15:54:18 +01:00
|
|
|
static int do_smbus_read_byte(unsigned smbus_base, unsigned device, unsigned address)
|
2008-10-29 05:46:52 +01:00
|
|
|
{
|
|
|
|
unsigned char global_status_register;
|
|
|
|
unsigned char byte;
|
|
|
|
|
2009-03-11 15:54:18 +01:00
|
|
|
if (smbus_wait_until_ready(smbus_base) < 0) {
|
2008-10-29 05:46:52 +01:00
|
|
|
return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
|
|
|
|
}
|
|
|
|
/* Setup transaction */
|
|
|
|
/* Disable interrupts */
|
2009-03-11 15:54:18 +01:00
|
|
|
outb(inb(smbus_base + SMBHSTCTL) & (~1), smbus_base + SMBHSTCTL);
|
2008-10-29 05:46:52 +01:00
|
|
|
/* Set the device I'm talking too */
|
2009-03-11 15:54:18 +01:00
|
|
|
outb(((device & 0x7f) << 1) | 1, smbus_base + SMBXMITADD);
|
2008-10-29 05:46:52 +01:00
|
|
|
/* Set the command/address... */
|
2009-03-11 15:54:18 +01:00
|
|
|
outb(address & 0xff, smbus_base + SMBHSTCMD);
|
2008-10-29 05:46:52 +01:00
|
|
|
/* Set up for a byte data read */
|
2009-03-11 15:54:18 +01:00
|
|
|
outb((inb(smbus_base + SMBHSTCTL) & 0xe3) | (0x2 << 2),
|
|
|
|
(smbus_base + SMBHSTCTL));
|
2008-10-29 05:46:52 +01:00
|
|
|
/* Clear any lingering errors, so the transaction will run */
|
2009-03-11 15:54:18 +01:00
|
|
|
outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
|
2008-10-29 05:46:52 +01:00
|
|
|
|
|
|
|
/* Clear the data byte... */
|
2009-03-11 15:54:18 +01:00
|
|
|
outb(0, smbus_base + SMBHSTDAT0);
|
2008-10-29 05:46:52 +01:00
|
|
|
|
|
|
|
/* Start the command */
|
2009-03-11 15:54:18 +01:00
|
|
|
outb((inb(smbus_base + SMBHSTCTL) | 0x40),
|
|
|
|
smbus_base + SMBHSTCTL);
|
2008-10-29 05:46:52 +01:00
|
|
|
|
|
|
|
/* Poll for transaction completion */
|
2009-03-11 15:54:18 +01:00
|
|
|
if (smbus_wait_until_done(smbus_base) < 0) {
|
2008-10-29 05:46:52 +01:00
|
|
|
return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2009-03-11 15:54:18 +01:00
|
|
|
global_status_register = inb(smbus_base + SMBHSTSTAT);
|
2008-10-29 05:46:52 +01:00
|
|
|
|
|
|
|
/* Ignore the "In Use" status... */
|
|
|
|
global_status_register &= ~(3 << 5);
|
|
|
|
|
|
|
|
/* Read results of transaction */
|
2009-03-11 15:54:18 +01:00
|
|
|
byte = inb(smbus_base + SMBHSTDAT0);
|
2008-10-29 05:46:52 +01:00
|
|
|
if (global_status_register != (1 << 1)) {
|
|
|
|
return SMBUS_ERROR;
|
|
|
|
}
|
|
|
|
return byte;
|
|
|
|
}
|