b7ba527dae87678b2e295553ba5e1eb3b8baa2f4
[coreboot.git] / src / southbridge / intel / i82801db / i82801db_smbus.h
1 /*
2  * This file is part of the LinuxBIOS project.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18
19 #include <device/smbus_def.h>
20
21 #define SMBHSTSTAT 0x0
22 #define SMBHSTCTL  0x2
23 #define SMBHSTCMD  0x3
24 #define SMBXMITADD 0x4
25 #define SMBHSTDAT0 0x5
26 #define SMBHSTDAT1 0x6
27 #define SMBBLKDAT  0x7
28 #define SMBTRNSADD 0x9
29 #define SMBSLVDATA 0xa
30 #define SMLINK_PIN_CTL 0xe
31 #define SMBUS_PIN_CTL  0xf 
32
33 #define SMBUS_TIMEOUT (100*1000*10)
34
35
36 static void smbus_delay(void)
37 {
38         outb(0x80, 0x80);
39 }
40
41 static int smbus_wait_until_ready(unsigned smbus_io_base)
42 {
43         unsigned loops = SMBUS_TIMEOUT;
44         unsigned char byte;
45         do {
46                 smbus_delay();
47                 if (--loops == 0)
48                         break;
49                 byte = inb(smbus_io_base + SMBHSTSTAT);
50         } while(byte & 1);
51         return loops?0:-1;
52 }
53
54 static int smbus_wait_until_done(unsigned smbus_io_base)
55 {
56         unsigned loops = SMBUS_TIMEOUT;
57         unsigned char byte;
58         do {
59                 smbus_delay();
60                 if (--loops == 0)
61                        break;
62                 byte = inb(smbus_io_base + SMBHSTSTAT);
63         } while((byte & 1) || (byte & ~((1<<6)|(1<<0))) == 0);
64         return loops?0:-1;
65 }
66
67 static int smbus_wait_until_blk_done(unsigned smbus_io_base)
68 {
69         unsigned loops = SMBUS_TIMEOUT;
70         unsigned char byte;
71         do {
72                 smbus_delay();
73                 if (--loops == 0)
74                        break;
75                 byte = inb(smbus_io_base + SMBHSTSTAT);
76         } while((byte&(1<<7)) == 0);
77         return loops?0:-1;
78 }
79
80 static int do_smbus_read_byte(unsigned smbus_io_base, unsigned device, unsigned address)
81 {
82         unsigned char global_status_register;
83         unsigned char byte;
84
85         if (smbus_wait_until_ready(smbus_io_base) < 0) {
86                 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
87         }
88         /* setup transaction */
89         /* disable interrupts */
90         outb(inb(smbus_io_base + SMBHSTCTL) & (~1), smbus_io_base + SMBHSTCTL);
91         /* set the device I'm talking too */
92         outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBXMITADD);
93         /* set the command/address... */
94         outb(address & 0xFF, smbus_io_base + SMBHSTCMD);
95         /* set up for a byte data read */
96         outb((inb(smbus_io_base + SMBHSTCTL) & 0xE3) | (0x2 << 2), smbus_io_base + SMBHSTCTL);
97         /* clear any lingering errors, so the transaction will run */
98         outb(inb(smbus_io_base + SMBHSTSTAT), smbus_io_base + SMBHSTSTAT);
99
100         /* clear the data byte...*/
101         outb(0, smbus_io_base + SMBHSTDAT0);
102
103         /* start the command */
104         outb((inb(smbus_io_base + SMBHSTCTL) | 0x40), smbus_io_base + SMBHSTCTL);
105
106         /* poll for transaction completion */
107         if (smbus_wait_until_done(smbus_io_base) < 0) {
108                 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
109         }
110
111         global_status_register = inb(smbus_io_base + SMBHSTSTAT);
112
113         /* Ignore the In Use Status... */
114         global_status_register &= ~(3 << 5);
115
116         /* read results of transaction */
117         byte = inb(smbus_io_base + SMBHSTDAT0);
118         if (global_status_register != (1 << 1)) {
119                 return SMBUS_ERROR;
120         }
121         return byte;
122 }
123