m5a99x-evo: ugly quirks, but WOOT: ohai seabios :-)
[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 #if 0
136         printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - smbus no ready.\n");
137 #endif
138                 return -2;      /* not ready */
139         }
140
141 #if 0
142     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - Start.\n");
143 #endif
144         /* set the command/address... */
145         outb(address & 0xff, smbus_io_base + SMBHSTCMD);
146
147         /* set the device I'm talking too */
148         outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
149
150         byte = inb(smbus_io_base + SMBHSTCTRL);
151         byte &= 0xe3;           /* Clear [4:2] */
152         byte |= (1 << 3) | (1 << 6);    /* Byte data read/write command, start the command */
153         outb(byte, smbus_io_base + SMBHSTCTRL);
154
155         /* poll for transaction completion */
156         if (smbus_wait_until_done(smbus_io_base) < 0) {
157                 return -3;      /* timeout or error */
158         }
159
160         /* read results of transaction */
161         byte = inb(smbus_io_base + SMBHSTDAT0);
162
163 #if 0
164     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - End.\n");
165 #endif
166         return byte;
167 }
168
169 int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val)
170 {
171         u8 byte;
172
173         if (smbus_wait_until_ready(smbus_io_base) < 0) {
174         printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_write_byte - smbus no ready.\n");
175                 return -2;      /* not ready */
176         }
177
178     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_write_byte - Start.\n");
179         /* set the command/address... */
180         outb(address & 0xff, smbus_io_base + SMBHSTCMD);
181
182         /* set the device I'm talking too */
183         outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
184
185         /* output value */
186         outb(val, smbus_io_base + SMBHSTDAT0);
187
188         byte = inb(smbus_io_base + SMBHSTCTRL);
189         byte &= 0xe3;           /* Clear [4:2] */
190         byte |= (1 << 3) | (1 << 6);    /* Byte data read/write command, start the command */
191         outb(byte, smbus_io_base + SMBHSTCTRL);
192
193         /* poll for transaction completion */
194         if (smbus_wait_until_done(smbus_io_base) < 0) {
195                 return -3;      /* timeout or error */
196         }
197
198     printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_write_byte - End.\n");
199         return 0;
200 }
201
202 void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val)
203 {
204         u32 tmp;
205
206     printk(BIOS_INFO, "SB900 - Smbus.c - alink_ab_indx - Start.\n");
207         outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX);
208         tmp = inl(AB_DATA);
209         /* rpr 4.2
210          * For certain revisions of the chip, the ABCFG registers,
211          * with an address of 0x100NN (where 'N' is any hexadecimal
212          * number), require an extra programming step.*/
213         outl(0, AB_INDX);
214
215         tmp &= ~mask;
216         tmp |= val;
217
218         /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | reg_addr); */
219         outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX);      /* probably we dont have to do it again. */
220         outl(tmp, AB_DATA);
221         outl(0, AB_INDX);
222     printk(BIOS_INFO, "SB900 - Smbus.c - alink_ab_indx - End.\n");
223 }
224
225 void alink_rc_indx(u32 reg_space, u32 reg_addr, u32 port, u32 mask, u32 val)
226 {
227         u32 tmp;
228
229     printk(BIOS_INFO, "SB900 - Smbus.c - alink_rc_indx - Start.\n");
230         outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX);
231         tmp = inl(AB_DATA);
232         /* rpr 4.2
233          * For certain revisions of the chip, the ABCFG registers,
234          * with an address of 0x100NN (where 'N' is any hexadecimal
235          * number), require an extra programming step.*/
236         outl(0, AB_INDX);
237
238         tmp &= ~mask;
239         tmp |= val;
240
241         //printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | (port&3) << 24 | reg_addr);
242         outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX);   /* probably we dont have to do it again. */
243         outl(tmp, AB_DATA);
244         outl(0, AB_INDX);
245     printk(BIOS_INFO, "SB900 - Smbus.c - alink_rc_indx - End.\n");
246 }
247
248 /* space = 0: AX_INDXC, AX_DATAC
249  * space = 1: AX_INDXP, AX_DATAP
250  */
251 void alink_ax_indx(u32 space /*c or p? */ , u32 axindc, u32 mask, u32 val)
252 {
253         u32 tmp;
254
255     printk(BIOS_INFO, "SB900 - Smbus.c - alink_ax_indx - Start.\n");
256         /* read axindc to tmp */
257         outl(space << 29 | space << 3 | 0x30, AB_INDX);
258         outl(axindc, AB_DATA);
259         outl(0, AB_INDX);
260         outl(space << 29 | space << 3 | 0x34, AB_INDX);
261         tmp = inl(AB_DATA);
262         outl(0, AB_INDX);
263
264         tmp &= ~mask;
265         tmp |= val;
266
267         /* write tmp */
268         outl(space << 29 | space << 3 | 0x30, AB_INDX);
269         outl(axindc, AB_DATA);
270         outl(0, AB_INDX);
271         outl(space << 29 | space << 3 | 0x34, AB_INDX);
272         outl(tmp, AB_DATA);
273         outl(0, AB_INDX);
274     printk(BIOS_INFO, "SB900 - Smbus.c - alink_ax_indx - End.\n");
275 }
276