blob: 80456bc9ff458c90ad7357b7957cb9ec385da434 [file] [log] [blame]
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2005 Yinghai Lu <yinghailu@gmail.com>
Stefan Reinauera8e11682009-03-11 14:54:18 +00005 * Copyright (C) 2009 coresystems GmbH
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00006 *
Uwe Hermannc70e9fc2010-02-15 23:10:19 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000010 *
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 Reinauerdebb11f2008-10-29 04:46:52 +000015 */
16
17#include <device/smbus_def.h>
Uwe Hermann4028ce72010-12-07 19:16:07 +000018#include "i82801gx.h"
Patrick Georgid0835952010-10-05 09:07:10 +000019
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000020static void smbus_delay(void)
21{
22 inb(0x80);
23}
24
Stefan Reinauera8e11682009-03-11 14:54:18 +000025static int smbus_wait_until_ready(u16 smbus_base)
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000026{
27 unsigned loops = SMBUS_TIMEOUT;
28 unsigned char byte;
29 do {
30 smbus_delay();
31 if (--loops == 0)
32 break;
Stefan Reinauera8e11682009-03-11 14:54:18 +000033 byte = inb(smbus_base + SMBHSTSTAT);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000034 } while (byte & 1);
35 return loops ? 0 : -1;
36}
37
Stefan Reinauera8e11682009-03-11 14:54:18 +000038static int smbus_wait_until_done(u16 smbus_base)
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000039{
40 unsigned loops = SMBUS_TIMEOUT;
41 unsigned char byte;
42 do {
43 smbus_delay();
44 if (--loops == 0)
45 break;
Stefan Reinauera8e11682009-03-11 14:54:18 +000046 byte = inb(smbus_base + SMBHSTSTAT);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000047 } while ((byte & 1) || (byte & ~((1 << 6) | (1 << 0))) == 0);
48 return loops ? 0 : -1;
49}
50
Stefan Reinauera8e11682009-03-11 14:54:18 +000051static int do_smbus_read_byte(unsigned smbus_base, unsigned device, unsigned address)
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000052{
53 unsigned char global_status_register;
54 unsigned char byte;
55
Stefan Reinauera8e11682009-03-11 14:54:18 +000056 if (smbus_wait_until_ready(smbus_base) < 0) {
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000057 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
58 }
59 /* Setup transaction */
60 /* Disable interrupts */
Stefan Reinauera8e11682009-03-11 14:54:18 +000061 outb(inb(smbus_base + SMBHSTCTL) & (~1), smbus_base + SMBHSTCTL);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000062 /* Set the device I'm talking too */
Stefan Reinauera8e11682009-03-11 14:54:18 +000063 outb(((device & 0x7f) << 1) | 1, smbus_base + SMBXMITADD);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000064 /* Set the command/address... */
Stefan Reinauera8e11682009-03-11 14:54:18 +000065 outb(address & 0xff, smbus_base + SMBHSTCMD);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000066 /* Set up for a byte data read */
Stefan Reinauera8e11682009-03-11 14:54:18 +000067 outb((inb(smbus_base + SMBHSTCTL) & 0xe3) | (0x2 << 2),
68 (smbus_base + SMBHSTCTL));
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000069 /* Clear any lingering errors, so the transaction will run */
Stefan Reinauera8e11682009-03-11 14:54:18 +000070 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000071
72 /* Clear the data byte... */
Stefan Reinauera8e11682009-03-11 14:54:18 +000073 outb(0, smbus_base + SMBHSTDAT0);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000074
75 /* Start the command */
Stefan Reinauera8e11682009-03-11 14:54:18 +000076 outb((inb(smbus_base + SMBHSTCTL) | 0x40),
77 smbus_base + SMBHSTCTL);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000078
79 /* Poll for transaction completion */
Stefan Reinauera8e11682009-03-11 14:54:18 +000080 if (smbus_wait_until_done(smbus_base) < 0) {
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000081 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
82 }
83
Stefan Reinauera8e11682009-03-11 14:54:18 +000084 global_status_register = inb(smbus_base + SMBHSTSTAT);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000085
86 /* Ignore the "In Use" status... */
87 global_status_register &= ~(3 << 5);
88
89 /* Read results of transaction */
Stefan Reinauera8e11682009-03-11 14:54:18 +000090 byte = inb(smbus_base + SMBHSTDAT0);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000091 if (global_status_register != (1 << 1)) {
92 return SMBUS_ERROR;
93 }
94 return byte;
95}