remove trailing whitespace
[coreboot.git] / src / southbridge / amd / cimx / sb900 / smbus.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 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
21 #include <arch/io.h>
22 #include "smbus.h"
23 #include <console/console.h>    /* printk */
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
34         loops = SMBUS_TIMEOUT;
35         do {
36                 u8 val;
37                 val = inb(smbus_io_base + SMBHSTSTAT);
38                 val &= 0x1f;
39                 if (val == 0) { /* ready now */
40                         return 0;
41                 }
42                 outb(val, smbus_io_base + SMBHSTSTAT);
43         } while (--loops);
44
45         return -2;              /* time out */
46 }
47
48 static int smbus_wait_until_done(u32 smbus_io_base)
49 {
50         u32 loops;
51
52         loops = SMBUS_TIMEOUT;
53         do {
54                 u8 val;
55
56                 val = inb(smbus_io_base + SMBHSTSTAT);
57                 val &= 0x1f;    /* mask off reserved bits */
58                 if (val & 0x1c) {
59                         return -5;      /* error */
60                 }
61                 if (val == 0x02) {
62                         outb(val, smbus_io_base + SMBHSTSTAT);  /* clear status */
63                         return 0;
64                 }
65         } while (--loops);
66
67         return -3;              /* timeout */
68 }
69
70 int do_smbus_recv_byte(u32 smbus_io_base, u32 device)
71 {
72         u8 byte;
73
74         if (smbus_wait_until_ready(smbus_io_base) < 0) {
75         printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_recv_byte - smbus no ready.\n");
76                 return -2;      /* not ready */
77         }
78
79     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_recv_byte - Start.\n");
80         /* set the device I'm talking too */
81         outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
82
83         byte = inb(smbus_io_base + SMBHSTCTRL);
84         byte &= 0xe3;           /* Clear [4:2] */
85         byte |= (1 << 2) | (1 << 6);    /* Byte data read/write command, start the command */
86         outb(byte, smbus_io_base + SMBHSTCTRL);
87
88         /* poll for transaction completion */
89         if (smbus_wait_until_done(smbus_io_base) < 0) {
90                 return -3;      /* timeout or error */
91         }
92
93         /* read results of transaction */
94         byte = inb(smbus_io_base + SMBHSTCMD);
95
96     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_recv_byte - End.\n");
97         return byte;
98 }
99
100 int do_smbus_send_byte(u32 smbus_io_base, u32 device, u8 val)
101 {
102         u8 byte;
103
104         if (smbus_wait_until_ready(smbus_io_base) < 0) {
105         printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_send_byte - smbus no ready.\n");
106                 return -2;      /* not ready */
107         }
108
109     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_send_byte - Start.\n");
110         /* set the command... */
111         outb(val, smbus_io_base + SMBHSTCMD);
112
113         /* set the device I'm talking too */
114         outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
115
116         byte = inb(smbus_io_base + SMBHSTCTRL);
117         byte &= 0xe3;           /* Clear [4:2] */
118         byte |= (1 << 2) | (1 << 6);    /* Byte data read/write command, start the command */
119         outb(byte, smbus_io_base + SMBHSTCTRL);
120
121         /* poll for transaction completion */
122         if (smbus_wait_until_done(smbus_io_base) < 0) {
123                 return -3;      /* timeout or error */
124         }
125
126     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_send_byte - End.\n");
127         return 0;
128 }
129
130 int do_smbus_read_byte(u32 smbus_io_base, u32 device, u32 address)
131 {
132         u8 byte;
133
134         if (smbus_wait_until_ready(smbus_io_base) < 0) {
135         printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - smbus no ready.\n");
136                 return -2;      /* not ready */
137         }
138
139     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - Start.\n");
140         /* set the command/address... */
141         outb(address & 0xff, smbus_io_base + SMBHSTCMD);
142
143         /* set the device I'm talking too */
144         outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
145
146         byte = inb(smbus_io_base + SMBHSTCTRL);
147         byte &= 0xe3;           /* Clear [4:2] */
148         byte |= (1 << 3) | (1 << 6);    /* Byte data read/write command, start the command */
149         outb(byte, smbus_io_base + SMBHSTCTRL);
150
151         /* poll for transaction completion */
152         if (smbus_wait_until_done(smbus_io_base) < 0) {
153                 return -3;      /* timeout or error */
154         }
155
156         /* read results of transaction */
157         byte = inb(smbus_io_base + SMBHSTDAT0);
158
159     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - End.\n");
160         return byte;
161 }
162
163 int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val)
164 {
165         u8 byte;
166
167         if (smbus_wait_until_ready(smbus_io_base) < 0) {
168         printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_write_byte - smbus no ready.\n");
169                 return -2;      /* not ready */
170         }
171
172     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_write_byte - Start.\n");
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) | 0, smbus_io_base + SMBHSTADDR);
178
179         /* output value */
180         outb(val, smbus_io_base + SMBHSTDAT0);
181
182         byte = inb(smbus_io_base + SMBHSTCTRL);
183         byte &= 0xe3;           /* Clear [4:2] */
184         byte |= (1 << 3) | (1 << 6);    /* Byte data read/write command, start the command */
185         outb(byte, smbus_io_base + SMBHSTCTRL);
186
187         /* poll for transaction completion */
188         if (smbus_wait_until_done(smbus_io_base) < 0) {
189                 return -3;      /* timeout or error */
190         }
191
192     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_write_byte - End.\n");
193         return 0;
194 }
195
196 void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val)
197 {
198         u32 tmp;
199
200     printk(BIOS_INFO, "SB900 - Smbus.c - alink_ab_indx - Start.\n");
201         outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX);
202         tmp = inl(AB_DATA);
203         /* rpr 4.2
204          * For certain revisions of the chip, the ABCFG registers,
205          * with an address of 0x100NN (where 'N' is any hexadecimal
206          * number), require an extra programming step.*/
207         outl(0, AB_INDX);
208
209         tmp &= ~mask;
210         tmp |= val;
211
212         /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | reg_addr); */
213         outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX);      /* probably we dont have to do it again. */
214         outl(tmp, AB_DATA);
215         outl(0, AB_INDX);
216     printk(BIOS_INFO, "SB900 - Smbus.c - alink_ab_indx - End.\n");
217 }
218
219 void alink_rc_indx(u32 reg_space, u32 reg_addr, u32 port, u32 mask, u32 val)
220 {
221         u32 tmp;
222
223     printk(BIOS_INFO, "SB900 - Smbus.c - alink_rc_indx - Start.\n");
224         outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX);
225         tmp = inl(AB_DATA);
226         /* rpr 4.2
227          * For certain revisions of the chip, the ABCFG registers,
228          * with an address of 0x100NN (where 'N' is any hexadecimal
229          * number), require an extra programming step.*/
230         outl(0, AB_INDX);
231
232         tmp &= ~mask;
233         tmp |= val;
234
235         //printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | (port&3) << 24 | reg_addr);
236         outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX);   /* probably we dont have to do it again. */
237         outl(tmp, AB_DATA);
238         outl(0, AB_INDX);
239     printk(BIOS_INFO, "SB900 - Smbus.c - alink_rc_indx - End.\n");
240 }
241
242 /* space = 0: AX_INDXC, AX_DATAC
243  * space = 1: AX_INDXP, AX_DATAP
244  */
245 void alink_ax_indx(u32 space /*c or p? */ , u32 axindc, u32 mask, u32 val)
246 {
247         u32 tmp;
248
249     printk(BIOS_INFO, "SB900 - Smbus.c - alink_ax_indx - Start.\n");
250         /* read axindc to tmp */
251         outl(space << 29 | space << 3 | 0x30, AB_INDX);
252         outl(axindc, AB_DATA);
253         outl(0, AB_INDX);
254         outl(space << 29 | space << 3 | 0x34, AB_INDX);
255         tmp = inl(AB_DATA);
256         outl(0, AB_INDX);
257
258         tmp &= ~mask;
259         tmp |= val;
260
261         /* write tmp */
262         outl(space << 29 | space << 3 | 0x30, AB_INDX);
263         outl(axindc, AB_DATA);
264         outl(0, AB_INDX);
265         outl(space << 29 | space << 3 | 0x34, AB_INDX);
266         outl(tmp, AB_DATA);
267         outl(0, AB_INDX);
268     printk(BIOS_INFO, "SB900 - Smbus.c - alink_ax_indx - End.\n");
269 }
270