2 * This file is part of the coreboot project.
4 * Copyright (C) 2011 Advanced Micro Devices, Inc.
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.
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.
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
23 #include <console/console.h> /* printk */
25 static inline void smbus_delay(void)
27 outb(inb(0x80), 0x80);
30 static int smbus_wait_until_ready(u32 smbus_io_base)
34 loops = SMBUS_TIMEOUT;
37 val = inb(smbus_io_base + SMBHSTSTAT);
39 if (val == 0) { /* ready now */
42 outb(val, smbus_io_base + SMBHSTSTAT);
45 return -2; /* time out */
48 static int smbus_wait_until_done(u32 smbus_io_base)
52 loops = SMBUS_TIMEOUT;
56 val = inb(smbus_io_base + SMBHSTSTAT);
57 val &= 0x1f; /* mask off reserved bits */
59 return -5; /* error */
62 outb(val, smbus_io_base + SMBHSTSTAT); /* clear status */
67 return -3; /* timeout */
70 int do_smbus_recv_byte(u32 smbus_io_base, u32 device)
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 */
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);
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);
88 /* poll for transaction completion */
89 if (smbus_wait_until_done(smbus_io_base) < 0) {
90 return -3; /* timeout or error */
93 /* read results of transaction */
94 byte = inb(smbus_io_base + SMBHSTCMD);
96 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_recv_byte - End.\n");
100 int do_smbus_send_byte(u32 smbus_io_base, u32 device, u8 val)
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 */
109 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_send_byte - Start.\n");
110 /* set the command... */
111 outb(val, smbus_io_base + SMBHSTCMD);
113 /* set the device I'm talking too */
114 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
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);
121 /* poll for transaction completion */
122 if (smbus_wait_until_done(smbus_io_base) < 0) {
123 return -3; /* timeout or error */
126 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_send_byte - End.\n");
130 int do_smbus_read_byte(u32 smbus_io_base, u32 device, u32 address)
134 if (smbus_wait_until_ready(smbus_io_base) < 0) {
136 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - smbus no ready.\n");
138 return -2; /* not ready */
142 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - Start.\n");
144 /* set the command/address... */
145 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
147 /* set the device I'm talking too */
148 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
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);
155 /* poll for transaction completion */
156 if (smbus_wait_until_done(smbus_io_base) < 0) {
157 return -3; /* timeout or error */
160 /* read results of transaction */
161 byte = inb(smbus_io_base + SMBHSTDAT0);
164 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - End.\n");
169 int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val)
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 */
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);
182 /* set the device I'm talking too */
183 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
186 outb(val, smbus_io_base + SMBHSTDAT0);
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);
193 /* poll for transaction completion */
194 if (smbus_wait_until_done(smbus_io_base) < 0) {
195 return -3; /* timeout or error */
198 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_write_byte - End.\n");
202 void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val)
206 printk(BIOS_INFO, "SB900 - Smbus.c - alink_ab_indx - Start.\n");
207 outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX);
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.*/
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. */
222 printk(BIOS_INFO, "SB900 - Smbus.c - alink_ab_indx - End.\n");
225 void alink_rc_indx(u32 reg_space, u32 reg_addr, u32 port, u32 mask, u32 val)
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);
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.*/
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. */
245 printk(BIOS_INFO, "SB900 - Smbus.c - alink_rc_indx - End.\n");
248 /* space = 0: AX_INDXC, AX_DATAC
249 * space = 1: AX_INDXP, AX_DATAP
251 void alink_ax_indx(u32 space /*c or p? */ , u32 axindc, u32 mask, u32 val)
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);
260 outl(space << 29 | space << 3 | 0x34, AB_INDX);
268 outl(space << 29 | space << 3 | 0x30, AB_INDX);
269 outl(axindc, AB_DATA);
271 outl(space << 29 | space << 3 | 0x34, AB_INDX);
274 printk(BIOS_INFO, "SB900 - Smbus.c - alink_ax_indx - End.\n");