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) {
135 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - smbus no ready.\n");
136 return -2; /* not ready */
139 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - Start.\n");
140 /* set the command/address... */
141 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
143 /* set the device I'm talking too */
144 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
146 byte = inb(smbus_io_base + SMBHSTCTRL);
147 byte &= 0xe3; /* Clear [4:2] */
148 byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */
149 outb(byte, smbus_io_base + SMBHSTCTRL);
151 /* poll for transaction completion */
152 if (smbus_wait_until_done(smbus_io_base) < 0) {
153 return -3; /* timeout or error */
156 /* read results of transaction */
157 byte = inb(smbus_io_base + SMBHSTDAT0);
159 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_read_byte - End.\n");
163 int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val)
167 if (smbus_wait_until_ready(smbus_io_base) < 0) {
168 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_write_byte - smbus no ready.\n");
169 return -2; /* not ready */
172 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_write_byte - Start.\n");
173 /* set the command/address... */
174 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
176 /* set the device I'm talking too */
177 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
180 outb(val, smbus_io_base + SMBHSTDAT0);
182 byte = inb(smbus_io_base + SMBHSTCTRL);
183 byte &= 0xe3; /* Clear [4:2] */
184 byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */
185 outb(byte, smbus_io_base + SMBHSTCTRL);
187 /* poll for transaction completion */
188 if (smbus_wait_until_done(smbus_io_base) < 0) {
189 return -3; /* timeout or error */
192 printk(BIOS_INFO, "SB900 - Smbus.c - do_smbus_write_byte - End.\n");
196 void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val)
200 printk(BIOS_INFO, "SB900 - Smbus.c - alink_ab_indx - Start.\n");
201 outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX);
204 * For certain revisions of the chip, the ABCFG registers,
205 * with an address of 0x100NN (where 'N' is any hexadecimal
206 * number), require an extra programming step.*/
212 /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | reg_addr); */
213 outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX); /* probably we dont have to do it again. */
216 printk(BIOS_INFO, "SB900 - Smbus.c - alink_ab_indx - End.\n");
219 void alink_rc_indx(u32 reg_space, u32 reg_addr, u32 port, u32 mask, u32 val)
223 printk(BIOS_INFO, "SB900 - Smbus.c - alink_rc_indx - Start.\n");
224 outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX);
227 * For certain revisions of the chip, the ABCFG registers,
228 * with an address of 0x100NN (where 'N' is any hexadecimal
229 * number), require an extra programming step.*/
235 //printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | (port&3) << 24 | reg_addr);
236 outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX); /* probably we dont have to do it again. */
239 printk(BIOS_INFO, "SB900 - Smbus.c - alink_rc_indx - End.\n");
242 /* space = 0: AX_INDXC, AX_DATAC
243 * space = 1: AX_INDXP, AX_DATAP
245 void alink_ax_indx(u32 space /*c or p? */ , u32 axindc, u32 mask, u32 val)
249 printk(BIOS_INFO, "SB900 - Smbus.c - alink_ax_indx - Start.\n");
250 /* read axindc to tmp */
251 outl(space << 29 | space << 3 | 0x30, AB_INDX);
252 outl(axindc, AB_DATA);
254 outl(space << 29 | space << 3 | 0x34, AB_INDX);
262 outl(space << 29 | space << 3 | 0x30, AB_INDX);
263 outl(axindc, AB_DATA);
265 outl(space << 29 | space << 3 | 0x34, AB_INDX);
268 printk(BIOS_INFO, "SB900 - Smbus.c - alink_ax_indx - End.\n");