first round name simplification. drop the <component>_ prefix.
[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 static inline void smbus_delay(void)
26 {
27         outb(inb(0x80), 0x80);
28 }
29
30 static int smbus_wait_until_ready(u32 smbus_io_base)
31 {
32         u32 loops;
33         loops = SMBUS_TIMEOUT;
34         do {
35                 u8 val;
36                 val = inb(smbus_io_base + SMBHSTSTAT);
37                 val &= 0x1f;
38                 if (val == 0) { /* ready now */
39                         return 0;
40                 }
41                 outb(val, smbus_io_base + SMBHSTSTAT);
42         } while (--loops);
43         return -2;              /* time out */
44 }
45
46 static int smbus_wait_until_done(u32 smbus_io_base)
47 {
48         u32 loops;
49         loops = SMBUS_TIMEOUT;
50         do {
51                 u8 val;
52
53                 val = inb(smbus_io_base + SMBHSTSTAT);
54                 val &= 0x1f;    /* mask off reserved bits */
55                 if (val & 0x1c) {
56                         return -5;      /* error */
57                 }
58                 if (val == 0x02) {
59                         outb(val, smbus_io_base + SMBHSTSTAT);  /* clear status */
60                         return 0;
61                 }
62         } while (--loops);
63         return -3;              /* timeout */
64 }
65
66 int do_smbus_recv_byte(u32 smbus_io_base, u32 device)
67 {
68         u8 byte;
69
70         if (smbus_wait_until_ready(smbus_io_base) < 0) {
71                 return -2;      /* not ready */
72         }
73
74         /* set the device I'm talking too */
75         outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
76
77         byte = inb(smbus_io_base + SMBHSTCTRL);
78         byte &= 0xe3;           /* Clear [4:2] */
79         byte |= (1 << 2) | (1 << 6);    /* Byte data read/write command, start the command */
80         outb(byte, smbus_io_base + SMBHSTCTRL);
81
82         /* poll for transaction completion */
83         if (smbus_wait_until_done(smbus_io_base) < 0) {
84                 return -3;      /* timeout or error */
85         }
86
87         /* read results of transaction */
88         byte = inb(smbus_io_base + SMBHSTCMD);
89
90         return byte;
91 }
92
93 int do_smbus_send_byte(u32 smbus_io_base, u32 device, u8 val)
94 {
95         u8 byte;
96
97         if (smbus_wait_until_ready(smbus_io_base) < 0) {
98                 return -2;      /* not ready */
99         }
100
101         /* set the command... */
102         outb(val, smbus_io_base + SMBHSTCMD);
103
104         /* set the device I'm talking too */
105         outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
106
107         byte = inb(smbus_io_base + SMBHSTCTRL);
108         byte &= 0xe3;           /* Clear [4:2] */
109         byte |= (1 << 2) | (1 << 6);    /* Byte data read/write command, start the command */
110         outb(byte, smbus_io_base + SMBHSTCTRL);
111
112         /* poll for transaction completion */
113         if (smbus_wait_until_done(smbus_io_base) < 0) {
114                 return -3;      /* timeout or error */
115         }
116
117         return 0;
118 }
119
120 int do_smbus_read_byte(u32 smbus_io_base, u32 device, u32 address)
121 {
122         u8 byte;
123
124         if (smbus_wait_until_ready(smbus_io_base) < 0) {
125                 return -2;      /* not ready */
126         }
127
128         /* set the command/address... */
129         outb(address & 0xff, smbus_io_base + SMBHSTCMD);
130
131         /* set the device I'm talking too */
132         outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
133
134         byte = inb(smbus_io_base + SMBHSTCTRL);
135         byte &= 0xe3;           /* Clear [4:2] */
136         byte |= (1 << 3) | (1 << 6);    /* Byte data read/write command, start the command */
137         outb(byte, smbus_io_base + SMBHSTCTRL);
138
139         /* poll for transaction completion */
140         if (smbus_wait_until_done(smbus_io_base) < 0) {
141                 return -3;      /* timeout or error */
142         }
143
144         /* read results of transaction */
145         byte = inb(smbus_io_base + SMBHSTDAT0);
146
147         return byte;
148 }
149
150 int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val)
151 {
152         u8 byte;
153
154         if (smbus_wait_until_ready(smbus_io_base) < 0) {
155                 return -2;      /* not ready */
156         }
157
158         /* set the command/address... */
159         outb(address & 0xff, smbus_io_base + SMBHSTCMD);
160
161         /* set the device I'm talking too */
162         outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
163
164         /* output value */
165         outb(val, smbus_io_base + SMBHSTDAT0);
166
167         byte = inb(smbus_io_base + SMBHSTCTRL);
168         byte &= 0xe3;           /* Clear [4:2] */
169         byte |= (1 << 3) | (1 << 6);    /* Byte data read/write command, start the command */
170         outb(byte, smbus_io_base + SMBHSTCTRL);
171
172         /* poll for transaction completion */
173         if (smbus_wait_until_done(smbus_io_base) < 0) {
174                 return -3;      /* timeout or error */
175         }
176
177         return 0;
178 }
179
180 static void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val)
181 {
182         u32 tmp;
183
184         outl((reg_space & 0x3) << 30 | reg_addr, AB_INDX);
185         tmp = inl(AB_DATA);
186         /* rpr 4.2
187          * For certain revisions of the chip, the ABCFG registers,
188          * with an address of 0x100NN (where 'N' is any hexadecimal
189          * number), require an extra programming step.*/
190         reg_addr & 0x10000 ? outl(0, AB_INDX) : NULL;
191
192         tmp &= ~mask;
193         tmp |= val;
194
195         /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<30 | reg_addr); */
196         outl((reg_space & 0x3) << 30 | reg_addr, AB_INDX);      /* probably we dont have to do it again. */
197         outl(tmp, AB_DATA);
198         reg_addr & 0x10000 ? outl(0, AB_INDX) : NULL;
199 }
200
201 /* space = 0: AX_INDXC, AX_DATAC
202  * space = 1: AX_INDXP, AX_DATAP
203  */
204 static inline void alink_ax_indx(u32 space /*c or p? */ , u32 axindc,
205                           u32 mask, u32 val)
206 {
207         u32 tmp;
208
209         /* read axindc to tmp */
210         outl(space << 30 | space << 3 | 0x30, AB_INDX);
211         outl(axindc, AB_DATA);
212         outl(space << 30 | space << 3 | 0x34, AB_INDX);
213         tmp = inl(AB_DATA);
214
215         tmp &= ~mask;
216         tmp |= val;
217
218         /* write tmp */
219         outl(space << 30 | space << 3 | 0x30, AB_INDX);
220         outl(axindc, AB_DATA);
221         outl(space << 30 | space << 3 | 0x34, AB_INDX);
222         outl(tmp, AB_DATA);
223 }
224 #endif