blob: bfe99c0776b0896b93365c7fd9656ee13793ab64 [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
Paul Menzela46a7122013-02-23 18:37:27 +010017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Frank Vibrans63e62b02011-02-14 18:38:14 +000018 */
19
20
21#include <arch/io.h>
22#include "smbus.h"
Kerry Shefeed3292011-08-18 18:03:44 +080023#include <console/console.h> /* printk */
Frank Vibrans63e62b02011-02-14 18:38:14 +000024
Frank Vibrans63e62b02011-02-14 18:38:14 +000025static int smbus_wait_until_ready(u32 smbus_io_base)
26{
27 u32 loops;
28
29 loops = SMBUS_TIMEOUT;
30 do {
31 u8 val;
32 val = inb(smbus_io_base + SMBHSTSTAT);
33 val &= 0x1f;
34 if (val == 0) { /* ready now */
35 return 0;
36 }
37 outb(val, smbus_io_base + SMBHSTSTAT);
38 } while (--loops);
39
40 return -2; /* time out */
41}
42
43static int smbus_wait_until_done(u32 smbus_io_base)
44{
45 u32 loops;
46
47 loops = SMBUS_TIMEOUT;
48 do {
49 u8 val;
50
51 val = inb(smbus_io_base + SMBHSTSTAT);
52 val &= 0x1f; /* mask off reserved bits */
53 if (val & 0x1c) {
54 return -5; /* error */
55 }
56 if (val == 0x02) {
57 outb(val, smbus_io_base + SMBHSTSTAT); /* clear status */
58 return 0;
59 }
60 } while (--loops);
61
62 return -3; /* timeout */
63}
64
65int do_smbus_recv_byte(u32 smbus_io_base, u32 device)
66{
67 u8 byte;
68
69 if (smbus_wait_until_ready(smbus_io_base) < 0) {
Kerry Shefeed3292011-08-18 18:03:44 +080070 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_recv_byte - smbus not ready.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +000071 return -2; /* not ready */
72 }
73
Kerry Shefeed3292011-08-18 18:03:44 +080074 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_recv_byte - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +000075 /* set the device I'm talking too */
76 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
77
78 byte = inb(smbus_io_base + SMBHSTCTRL);
79 byte &= 0xe3; /* Clear [4:2] */
80 byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */
81 outb(byte, smbus_io_base + SMBHSTCTRL);
82
83 /* poll for transaction completion */
84 if (smbus_wait_until_done(smbus_io_base) < 0) {
85 return -3; /* timeout or error */
86 }
87
88 /* read results of transaction */
89 byte = inb(smbus_io_base + SMBHSTCMD);
90
Kerry Shefeed3292011-08-18 18:03:44 +080091 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_recv_byte - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +000092 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) {
Kerry Shefeed3292011-08-18 18:03:44 +0800100 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_send_byte - smbus not ready.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000101 return -2; /* not ready */
102 }
103
Kerry Shefeed3292011-08-18 18:03:44 +0800104 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_send_byte - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000105 /* set the command... */
106 outb(val, smbus_io_base + SMBHSTCMD);
107
108 /* set the device I'm talking too */
109 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
110
111 byte = inb(smbus_io_base + SMBHSTCTRL);
112 byte &= 0xe3; /* Clear [4:2] */
113 byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */
114 outb(byte, smbus_io_base + SMBHSTCTRL);
115
116 /* poll for transaction completion */
117 if (smbus_wait_until_done(smbus_io_base) < 0) {
118 return -3; /* timeout or error */
119 }
120
Kerry Shefeed3292011-08-18 18:03:44 +0800121 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_send_byte - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000122 return 0;
123}
124
125int do_smbus_read_byte(u32 smbus_io_base, u32 device, u32 address)
126{
127 u8 byte;
128
129 if (smbus_wait_until_ready(smbus_io_base) < 0) {
Kerry Shefeed3292011-08-18 18:03:44 +0800130 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_read_byte - smbus not ready.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000131 return -2; /* not ready */
132 }
133
Kerry Shefeed3292011-08-18 18:03:44 +0800134 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_read_byte - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000135 /* set the command/address... */
136 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
137
138 /* set the device I'm talking too */
139 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
140
141 byte = inb(smbus_io_base + SMBHSTCTRL);
142 byte &= 0xe3; /* Clear [4:2] */
143 byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */
144 outb(byte, smbus_io_base + SMBHSTCTRL);
145
146 /* poll for transaction completion */
147 if (smbus_wait_until_done(smbus_io_base) < 0) {
148 return -3; /* timeout or error */
149 }
150
151 /* read results of transaction */
152 byte = inb(smbus_io_base + SMBHSTDAT0);
153
Kerry Shefeed3292011-08-18 18:03:44 +0800154 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_read_byte - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000155 return byte;
156}
157
158int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val)
159{
160 u8 byte;
161
162 if (smbus_wait_until_ready(smbus_io_base) < 0) {
Kerry Shefeed3292011-08-18 18:03:44 +0800163 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_write_byte - smbus not ready.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000164 return -2; /* not ready */
165 }
166
Kerry Shefeed3292011-08-18 18:03:44 +0800167 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_write_byte - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000168 /* set the command/address... */
169 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
170
171 /* set the device I'm talking too */
172 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
173
174 /* output value */
175 outb(val, smbus_io_base + SMBHSTDAT0);
176
177 byte = inb(smbus_io_base + SMBHSTCTRL);
178 byte &= 0xe3; /* Clear [4:2] */
179 byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */
180 outb(byte, smbus_io_base + SMBHSTCTRL);
181
182 /* poll for transaction completion */
183 if (smbus_wait_until_done(smbus_io_base) < 0) {
184 return -3; /* timeout or error */
185 }
186
Kerry Shefeed3292011-08-18 18:03:44 +0800187 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_write_byte - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000188 return 0;
189}
190
191void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val)
192{
193 u32 tmp;
194
Kerry Shefeed3292011-08-18 18:03:44 +0800195 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ab_indx - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000196 outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX);
197 tmp = inl(AB_DATA);
198 /* rpr 4.2
199 * For certain revisions of the chip, the ABCFG registers,
200 * with an address of 0x100NN (where 'N' is any hexadecimal
201 * number), require an extra programming step.*/
202 outl(0, AB_INDX);
203
204 tmp &= ~mask;
205 tmp |= val;
206
207 /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | reg_addr); */
Martin Roth3c3a50c2014-12-16 20:50:26 -0700208 outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX); /* probably we don't have to do it again. */
Frank Vibrans63e62b02011-02-14 18:38:14 +0000209 outl(tmp, AB_DATA);
210 outl(0, AB_INDX);
Kerry Shefeed3292011-08-18 18:03:44 +0800211 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ab_indx - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000212}
213
214void alink_rc_indx(u32 reg_space, u32 reg_addr, u32 port, u32 mask, u32 val)
215{
216 u32 tmp;
217
Kerry Shefeed3292011-08-18 18:03:44 +0800218 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_rc_indx - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000219 outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX);
220 tmp = inl(AB_DATA);
221 /* rpr 4.2
222 * For certain revisions of the chip, the ABCFG registers,
223 * with an address of 0x100NN (where 'N' is any hexadecimal
224 * number), require an extra programming step.*/
225 outl(0, AB_INDX);
226
227 tmp &= ~mask;
228 tmp |= val;
229
230 //printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | (port&3) << 24 | reg_addr);
Martin Roth3c3a50c2014-12-16 20:50:26 -0700231 outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX); /* probably we don't have to do it again. */
Frank Vibrans63e62b02011-02-14 18:38:14 +0000232 outl(tmp, AB_DATA);
233 outl(0, AB_INDX);
Kerry Shefeed3292011-08-18 18:03:44 +0800234 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_rc_indx - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000235}
236
237/* space = 0: AX_INDXC, AX_DATAC
238 * space = 1: AX_INDXP, AX_DATAP
239 */
240void alink_ax_indx(u32 space /*c or p? */ , u32 axindc, u32 mask, u32 val)
241{
242 u32 tmp;
243
Kerry Shefeed3292011-08-18 18:03:44 +0800244 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ax_indx - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000245 /* read axindc to tmp */
246 outl(space << 29 | space << 3 | 0x30, AB_INDX);
247 outl(axindc, AB_DATA);
248 outl(0, AB_INDX);
249 outl(space << 29 | space << 3 | 0x34, AB_INDX);
250 tmp = inl(AB_DATA);
251 outl(0, AB_INDX);
252
253 tmp &= ~mask;
254 tmp |= val;
255
256 /* write 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 outl(tmp, AB_DATA);
262 outl(0, AB_INDX);
Kerry Shefeed3292011-08-18 18:03:44 +0800263 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ax_indx - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000264}