Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2005 Yinghai Lu <yinghailu@gmail.com> |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009 coresystems GmbH |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 6 | * |
Uwe Hermann | c70e9fc | 2010-02-15 23:10:19 +0000 | [diff] [blame] | 7 | * 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 Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 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 | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <device/smbus_def.h> |
Uwe Hermann | 4028ce7 | 2010-12-07 19:16:07 +0000 | [diff] [blame] | 18 | #include "i82801gx.h" |
Patrick Georgi | d083595 | 2010-10-05 09:07:10 +0000 | [diff] [blame] | 19 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 20 | static void smbus_delay(void) |
| 21 | { |
| 22 | inb(0x80); |
| 23 | } |
| 24 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 25 | static int smbus_wait_until_ready(u16 smbus_base) |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 26 | { |
| 27 | unsigned loops = SMBUS_TIMEOUT; |
| 28 | unsigned char byte; |
| 29 | do { |
| 30 | smbus_delay(); |
| 31 | if (--loops == 0) |
| 32 | break; |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 33 | byte = inb(smbus_base + SMBHSTSTAT); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 34 | } while (byte & 1); |
| 35 | return loops ? 0 : -1; |
| 36 | } |
| 37 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 38 | static int smbus_wait_until_done(u16 smbus_base) |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 39 | { |
| 40 | unsigned loops = SMBUS_TIMEOUT; |
| 41 | unsigned char byte; |
| 42 | do { |
| 43 | smbus_delay(); |
| 44 | if (--loops == 0) |
| 45 | break; |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 46 | byte = inb(smbus_base + SMBHSTSTAT); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 47 | } while ((byte & 1) || (byte & ~((1 << 6) | (1 << 0))) == 0); |
| 48 | return loops ? 0 : -1; |
| 49 | } |
| 50 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 51 | static int do_smbus_read_byte(unsigned smbus_base, unsigned device, unsigned address) |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 52 | { |
| 53 | unsigned char global_status_register; |
| 54 | unsigned char byte; |
| 55 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 56 | if (smbus_wait_until_ready(smbus_base) < 0) { |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 57 | return SMBUS_WAIT_UNTIL_READY_TIMEOUT; |
| 58 | } |
| 59 | /* Setup transaction */ |
| 60 | /* Disable interrupts */ |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 61 | outb(inb(smbus_base + SMBHSTCTL) & (~1), smbus_base + SMBHSTCTL); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 62 | /* Set the device I'm talking too */ |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 63 | outb(((device & 0x7f) << 1) | 1, smbus_base + SMBXMITADD); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 64 | /* Set the command/address... */ |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 65 | outb(address & 0xff, smbus_base + SMBHSTCMD); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 66 | /* Set up for a byte data read */ |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 67 | outb((inb(smbus_base + SMBHSTCTL) & 0xe3) | (0x2 << 2), |
| 68 | (smbus_base + SMBHSTCTL)); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 69 | /* Clear any lingering errors, so the transaction will run */ |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 70 | outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 71 | |
| 72 | /* Clear the data byte... */ |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 73 | outb(0, smbus_base + SMBHSTDAT0); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 74 | |
| 75 | /* Start the command */ |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 76 | outb((inb(smbus_base + SMBHSTCTL) | 0x40), |
| 77 | smbus_base + SMBHSTCTL); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 78 | |
| 79 | /* Poll for transaction completion */ |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 80 | if (smbus_wait_until_done(smbus_base) < 0) { |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 81 | return SMBUS_WAIT_UNTIL_DONE_TIMEOUT; |
| 82 | } |
| 83 | |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 84 | global_status_register = inb(smbus_base + SMBHSTSTAT); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 85 | |
| 86 | /* Ignore the "In Use" status... */ |
| 87 | global_status_register &= ~(3 << 5); |
| 88 | |
| 89 | /* Read results of transaction */ |
Stefan Reinauer | a8e1168 | 2009-03-11 14:54:18 +0000 | [diff] [blame] | 90 | byte = inb(smbus_base + SMBHSTDAT0); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 91 | if (global_status_register != (1 << 1)) { |
| 92 | return SMBUS_ERROR; |
| 93 | } |
| 94 | return byte; |
| 95 | } |