Support for the Intel ICH7 southbridge.
[coreboot.git] / src / southbridge / intel / i82801gx / i82801gx_smbus.h
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2005 Yinghai Lu <yinghailu@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <device/smbus_def.h>
22
23 static void smbus_delay(void)
24 {
25         inb(0x80);
26 }
27
28 static int smbus_wait_until_ready(void)
29 {
30         unsigned loops = SMBUS_TIMEOUT;
31         unsigned char byte;
32         do {
33                 smbus_delay();
34                 if (--loops == 0)
35                         break;
36                 byte = inb(SMBUS_IO_BASE + SMBHSTSTAT);
37         } while (byte & 1);
38         return loops ? 0 : -1;
39 }
40
41 static int smbus_wait_until_done(void)
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) || (byte & ~((1 << 6) | (1 << 0))) == 0);
51         return loops ? 0 : -1;
52 }
53
54 static int smbus_wait_until_blk_done(void)
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 << 7)) == 0);
64         return loops ? 0 : -1;
65 }
66
67 static int do_smbus_read_byte(unsigned device, unsigned address)
68 {
69         unsigned char global_status_register;
70         unsigned char byte;
71
72         if (smbus_wait_until_ready() < 0) {
73                 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
74         }
75         /* Setup transaction */
76         /* Disable interrupts */
77         outb(inb(SMBUS_IO_BASE + SMBHSTCTL) & (~1), SMBUS_IO_BASE + SMBHSTCTL);
78         /* Set the device I'm talking too */
79         outb(((device & 0x7f) << 1) | 1, SMBUS_IO_BASE + SMBXMITADD);
80         /* Set the command/address... */
81         outb(address & 0xff, SMBUS_IO_BASE + SMBHSTCMD);
82         /* Set up for a byte data read */
83         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xe3) | (0x2 << 2),
84              (SMBUS_IO_BASE + SMBHSTCTL));
85         /* Clear any lingering errors, so the transaction will run */
86         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
87
88         /* Clear the data byte... */
89         outb(0, SMBUS_IO_BASE + SMBHSTDAT0);
90
91         /* Start the command */
92         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) | 0x40),
93              SMBUS_IO_BASE + SMBHSTCTL);
94
95         /* Poll for transaction completion */
96         if (smbus_wait_until_done() < 0) {
97                 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
98         }
99
100         global_status_register = inb(SMBUS_IO_BASE + SMBHSTSTAT);
101
102         /* Ignore the "In Use" status... */
103         global_status_register &= ~(3 << 5);
104
105         /* Read results of transaction */
106         byte = inb(SMBUS_IO_BASE + SMBHSTDAT0);
107         if (global_status_register != (1 << 1)) {
108                 return SMBUS_ERROR;
109         }
110         return byte;
111 }
112
113 /* This function is neither used nor tested by me (Corey Osgood), the author 
114 (Yinghai) probably tested/used it on i82801er */
115 static int do_smbus_write_block(unsigned device, unsigned length, unsigned cmd,
116                                 unsigned data1, unsigned data2)
117 {
118         unsigned char byte;
119         unsigned char stat;
120         int i;
121
122 #if CONFIG_USE_PRINTK_IN_CAR
123         printk_err("Untested smbus_write_block called\r\n");
124 #else
125         print_err("Untested smbus_write_block called\r\n");
126 #endif
127
128         /* Clear the PM timeout flags, SECOND_TO_STS */
129         outw(inw(0x0400 + 0x66), 0x0400 + 0x66);
130
131         if (smbus_wait_until_ready() < 0) {
132                 return -2;
133         }
134
135         /* Setup transaction */
136         /* Obtain ownership */
137         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
138         for (stat = 0; (stat & 0x40) == 0;) {
139                 stat = inb(SMBUS_IO_BASE + SMBHSTSTAT);
140         }
141         /* Clear the done bit */
142         outb(0x80, SMBUS_IO_BASE + SMBHSTSTAT);
143         /* Disable interrupts */
144         outb(inb(SMBUS_IO_BASE + SMBHSTCTL) & (~1), SMBUS_IO_BASE + SMBHSTCTL);
145
146         /* Set the device I'm talking too */
147         outb(((device & 0x7f) << 1), SMBUS_IO_BASE + SMBXMITADD);
148
149         /* Set the command address */
150         outb(cmd & 0xff, SMBUS_IO_BASE + SMBHSTCMD);
151
152         /* Set the block length */
153         outb(length & 0xff, SMBUS_IO_BASE + SMBHSTDAT0);
154
155         /* Try sending out the first byte of data here */
156         byte = (data1 >> (0)) & 0x0ff;
157         outb(byte, SMBUS_IO_BASE + SMBBLKDAT);
158         /* Issue a block write command */
159         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xe3) | (0x5 << 2) | 0x40,
160              SMBUS_IO_BASE + SMBHSTCTL);
161
162         for (i = 0; i < length; i++) {
163
164                 /* Poll for transaction completion */
165                 if (smbus_wait_until_blk_done() < 0) {
166                         return -3;
167                 }
168
169                 /* Load the next byte */
170                 if (i > 3)
171                         byte = (data2 >> (i % 4)) & 0x0ff;
172                 else
173                         byte = (data1 >> (i)) & 0x0ff;
174                 outb(byte, SMBUS_IO_BASE + SMBBLKDAT);
175
176                 /* Clear the done bit */
177                 outb(inb(SMBUS_IO_BASE + SMBHSTSTAT),
178                      SMBUS_IO_BASE + SMBHSTSTAT);
179         }
180
181 #if CONFIG_USE_PRINTK_IN_CAR
182         printk_debug("SMBUS Block complete\r\n");
183 #else
184         print_debug("SMBUS Block complete\r\n");
185 #endif
186         return 0;
187 }