blob: e428d43591616d3bb0d22ff3e9489e9fdb12f1a2 [file] [log] [blame]
zbao246e84b2012-07-13 18:47:03 +08001/*
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
Paul Menzela46a7122013-02-23 18:37:27 +010017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
zbao246e84b2012-07-13 18:47:03 +080018 */
19
Martin Rothf5726ea2013-01-18 12:55:40 -070020#ifndef _HUDSON_SMBUS_C_
21#define _HUDSON_SMBUS_C_
zbao246e84b2012-07-13 18:47:03 +080022
Paul Menzel883b03f2013-05-06 15:18:57 +020023#include <io.h>
24#include <stdint.h>
zbao246e84b2012-07-13 18:47:03 +080025#include "smbus.h"
26
27static inline void smbus_delay(void)
28{
29 outb(inb(0x80), 0x80);
30}
31
32static int smbus_wait_until_ready(u32 smbus_io_base)
33{
34 u32 loops;
35 loops = SMBUS_TIMEOUT;
36 do {
37 u8 val;
38 val = inb(smbus_io_base + SMBHSTSTAT);
39 val &= 0x1f;
40 if (val == 0) { /* ready now */
41 return 0;
42 }
43 outb(val, smbus_io_base + SMBHSTSTAT);
44 } while (--loops);
45 return -2; /* time out */
46}
47
48static int smbus_wait_until_done(u32 smbus_io_base)
49{
50 u32 loops;
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 return -3; /* timeout */
66}
67
68int do_smbus_recv_byte(u32 smbus_io_base, u32 device)
69{
70 u8 byte;
71
72 if (smbus_wait_until_ready(smbus_io_base) < 0) {
73 return -2; /* not ready */
74 }
75
76 /* set the device I'm talking too */
77 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
78
79 byte = inb(smbus_io_base + SMBHSTCTRL);
80 byte &= 0xe3; /* Clear [4:2] */
81 byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */
82 outb(byte, smbus_io_base + SMBHSTCTRL);
83
84 /* poll for transaction completion */
85 if (smbus_wait_until_done(smbus_io_base) < 0) {
86 return -3; /* timeout or error */
87 }
88
89 /* read results of transaction */
90 byte = inb(smbus_io_base + SMBHSTCMD);
91
92 return byte;
93}
94
95int do_smbus_send_byte(u32 smbus_io_base, u32 device, u8 val)
96{
97 u8 byte;
98
99 if (smbus_wait_until_ready(smbus_io_base) < 0) {
100 return -2; /* not ready */
101 }
102
103 /* set the command... */
104 outb(val, smbus_io_base + SMBHSTCMD);
105
106 /* set the device I'm talking too */
107 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
108
109 byte = inb(smbus_io_base + SMBHSTCTRL);
110 byte &= 0xe3; /* Clear [4:2] */
111 byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */
112 outb(byte, smbus_io_base + SMBHSTCTRL);
113
114 /* poll for transaction completion */
115 if (smbus_wait_until_done(smbus_io_base) < 0) {
116 return -3; /* timeout or error */
117 }
118
119 return 0;
120}
121
122int do_smbus_read_byte(u32 smbus_io_base, u32 device,
123 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,
154 u32 address, u8 val)
155{
156 u8 byte;
157
158 if (smbus_wait_until_ready(smbus_io_base) < 0) {
159 return -2; /* not ready */
160 }
161
162 /* set the command/address... */
163 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
164
165 /* set the device I'm talking too */
166 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
167
168 /* output value */
169 outb(val, smbus_io_base + SMBHSTDAT0);
170
171 byte = inb(smbus_io_base + SMBHSTCTRL);
172 byte &= 0xe3; /* Clear [4:2] */
173 byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */
174 outb(byte, smbus_io_base + SMBHSTCTRL);
175
176 /* poll for transaction completion */
177 if (smbus_wait_until_done(smbus_io_base) < 0) {
178 return -3; /* timeout or error */
179 }
180
181 return 0;
182}
183
184void alink_ab_indx(u32 reg_space, u32 reg_addr,
185 u32 mask, u32 val)
186{
187 u32 tmp;
188
189 outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX);
190 tmp = inl(AB_DATA);
191 /* rpr 4.2
192 * For certain revisions of the chip, the ABCFG registers,
193 * with an address of 0x100NN (where 'N' is any hexadecimal
194 * number), require an extra programming step.*/
195 outl(0, AB_INDX);
196
197 tmp &= ~mask;
198 tmp |= val;
199
200 /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | reg_addr); */
201 outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX); /* probably we dont have to do it again. */
202 outl(tmp, AB_DATA);
203 outl(0, AB_INDX);
204}
205
206void alink_rc_indx(u32 reg_space, u32 reg_addr, u32 port,
207 u32 mask, u32 val)
208{
209 u32 tmp;
210
211 outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX);
212 tmp = inl(AB_DATA);
213 /* rpr 4.2
214 * For certain revisions of the chip, the ABCFG registers,
215 * with an address of 0x100NN (where 'N' is any hexadecimal
216 * number), require an extra programming step.*/
217 outl(0, AB_INDX);
218
219 tmp &= ~mask;
220 tmp |= val;
221
222 //printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | (port&3) << 24 | reg_addr);
223 outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX); /* probably we dont have to do it again. */
224 outl(tmp, AB_DATA);
225 outl(0, AB_INDX);
226}
227
228/* space = 0: AX_INDXC, AX_DATAC
229 * space = 1: AX_INDXP, AX_DATAP
230 */
231void alink_ax_indx(u32 space /*c or p? */ , u32 axindc,
232 u32 mask, u32 val)
233{
234 u32 tmp;
235
236 /* read axindc to tmp */
237 outl(space << 29 | space << 3 | 0x30, AB_INDX);
238 outl(axindc, AB_DATA);
239 outl(0, AB_INDX);
240 outl(space << 29 | space << 3 | 0x34, AB_INDX);
241 tmp = inl(AB_DATA);
242 outl(0, AB_INDX);
243
244 tmp &= ~mask;
245 tmp |= val;
246
247 /* write tmp */
248 outl(space << 29 | space << 3 | 0x30, AB_INDX);
249 outl(axindc, AB_DATA);
250 outl(0, AB_INDX);
251 outl(space << 29 | space << 3 | 0x34, AB_INDX);
252 outl(tmp, AB_DATA);
253 outl(0, AB_INDX);
254}
255#endif