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