6edc3de80c467cb9ef0e9976808d825a465a9fd5
[coreboot.git] / src / southbridge / amd / sb700 / smbus.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2010 Advanced Micro Devices, Inc.
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; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #ifndef _SB700_SMBUS_C_
21 #define _SB700_SMBUS_C_
22
23 #include "smbus.h"
24
25 void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val)
26 {
27         u32 tmp;
28
29         outl((reg_space & 0x3) << 30 | reg_addr, AB_INDX);
30         tmp = inl(AB_DATA);
31         /* rpr 4.2
32          * For certain revisions of the chip, the ABCFG registers,
33          * with an address of 0x100NN (where 'N' is any hexadecimal
34          * number), require an extra programming step.*/
35         reg_addr & 0x10000 ? outl(0, AB_INDX) : NULL;
36
37         tmp &= ~mask;
38         tmp |= val;
39
40         /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<30 | reg_addr); */
41         outl((reg_space & 0x3) << 30 | reg_addr, AB_INDX);      /* probably we dont have to do it again. */
42         outl(tmp, AB_DATA);
43         reg_addr & 0x10000 ? outl(0, AB_INDX) : NULL;
44 }
45
46 /* space = 0: AX_INDXC, AX_DATAC
47  * space = 1: AX_INDXP, AX_DATAP
48  */
49 void alink_ax_indx(u32 space, u32 axindc, u32 mask, u32 val)
50 {
51         u32 tmp;
52
53         /* read axindc to tmp */
54         outl(space << 30 | space << 3 | 0x30, AB_INDX);
55         outl(axindc, AB_DATA);
56         outl(space << 30 | space << 3 | 0x34, AB_INDX);
57         tmp = inl(AB_DATA);
58
59         tmp &= ~mask;
60         tmp |= val;
61
62         /* write tmp */
63         outl(space << 30 | space << 3 | 0x30, AB_INDX);
64         outl(axindc, AB_DATA);
65         outl(space << 30 | space << 3 | 0x34, AB_INDX);
66         outl(tmp, AB_DATA);
67 }
68
69
70 static inline void smbus_delay(void)
71 {
72         outb(inb(0x80), 0x80);
73 }
74
75 static int smbus_wait_until_ready(u32 smbus_io_base)
76 {
77         u32 loops;
78         loops = SMBUS_TIMEOUT;
79         do {
80                 u8 val;
81                 val = inb(smbus_io_base + SMBHSTSTAT);
82                 val &= 0x1f;
83                 if (val == 0) { /* ready now */
84                         return 0;
85                 }
86                 outb(val, smbus_io_base + SMBHSTSTAT);
87         } while (--loops);
88         return -2;              /* time out */
89 }
90
91 static int smbus_wait_until_done(u32 smbus_io_base)
92 {
93         u32 loops;
94         loops = SMBUS_TIMEOUT;
95         do {
96                 u8 val;
97
98                 val = inb(smbus_io_base + SMBHSTSTAT);
99                 val &= 0x1f;    /* mask off reserved bits */
100                 if (val & 0x1c) {
101                         return -5;      /* error */
102                 }
103                 if (val == 0x02) {
104                         outb(val, smbus_io_base + SMBHSTSTAT);  /* clear status */
105                         return 0;
106                 }
107         } while (--loops);
108         return -3;              /* timeout */
109 }
110
111 int do_smbus_recv_byte(u32 smbus_io_base, u32 device)
112 {
113         u8 byte;
114
115         if (smbus_wait_until_ready(smbus_io_base) < 0) {
116                 return -2;      /* not ready */
117         }
118
119         /* set the device I'm talking too */
120         outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
121
122         byte = inb(smbus_io_base + SMBHSTCTRL);
123         byte &= 0xe3;           /* Clear [4:2] */
124         byte |= (1 << 2) | (1 << 6);    /* Byte data read/write command, start the command */
125         outb(byte, smbus_io_base + SMBHSTCTRL);
126
127         /* poll for transaction completion */
128         if (smbus_wait_until_done(smbus_io_base) < 0) {
129                 return -3;      /* timeout or error */
130         }
131
132         /* read results of transaction */
133         byte = inb(smbus_io_base + SMBHSTCMD);
134
135         return byte;
136 }
137
138 int do_smbus_send_byte(u32 smbus_io_base, u32 device, u8 val)
139 {
140         u8 byte;
141
142         if (smbus_wait_until_ready(smbus_io_base) < 0) {
143                 return -2;      /* not ready */
144         }
145
146         /* set the command... */
147         outb(val, smbus_io_base + SMBHSTCMD);
148
149         /* set the device I'm talking too */
150         outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
151
152         byte = inb(smbus_io_base + SMBHSTCTRL);
153         byte &= 0xe3;           /* Clear [4:2] */
154         byte |= (1 << 2) | (1 << 6);    /* Byte data read/write command, start the command */
155         outb(byte, smbus_io_base + SMBHSTCTRL);
156
157         /* poll for transaction completion */
158         if (smbus_wait_until_done(smbus_io_base) < 0) {
159                 return -3;      /* timeout or error */
160         }
161
162         return 0;
163 }
164
165 int do_smbus_read_byte(u32 smbus_io_base, u32 device, u32 address)
166 {
167         u8 byte;
168
169         if (smbus_wait_until_ready(smbus_io_base) < 0) {
170                 return -2;      /* not ready */
171         }
172
173         /* set the command/address... */
174         outb(address & 0xff, smbus_io_base + SMBHSTCMD);
175
176         /* set the device I'm talking too */
177         outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
178
179         byte = inb(smbus_io_base + SMBHSTCTRL);
180         byte &= 0xe3;           /* Clear [4:2] */
181         byte |= (1 << 3) | (1 << 6);    /* Byte data read/write command, start the command */
182         outb(byte, smbus_io_base + SMBHSTCTRL);
183
184         /* poll for transaction completion */
185         if (smbus_wait_until_done(smbus_io_base) < 0) {
186                 return -3;      /* timeout or error */
187         }
188
189         /* read results of transaction */
190         byte = inb(smbus_io_base + SMBHSTDAT0);
191
192         return byte;
193 }
194
195 int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val)
196 {
197         u8 byte;
198
199         if (smbus_wait_until_ready(smbus_io_base) < 0) {
200                 return -2;      /* not ready */
201         }
202
203         /* set the command/address... */
204         outb(address & 0xff, smbus_io_base + SMBHSTCMD);
205
206         /* set the device I'm talking too */
207         outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
208
209         /* output value */
210         outb(val, smbus_io_base + SMBHSTDAT0);
211
212         byte = inb(smbus_io_base + SMBHSTCTRL);
213         byte &= 0xe3;           /* Clear [4:2] */
214         byte |= (1 << 3) | (1 << 6);    /* Byte data read/write command, start the command */
215         outb(byte, smbus_io_base + SMBHSTCTRL);
216
217         /* poll for transaction completion */
218         if (smbus_wait_until_done(smbus_io_base) < 0) {
219                 return -3;      /* timeout or error */
220         }
221
222         return 0;
223 }
224
225 #endif