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