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