tree: drop last paragraph of GPL copyright header
[coreboot.git] / src / southbridge / amd / agesa / hudson / smbus.c
blob9aa152b2ea3f3a7bd14a297c25e9e331fe928d40
1 /*
2 * This file is part of the coreboot project.
4 * Copyright (C) 2010 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.
16 #ifndef _HUDSON_SMBUS_C_
17 #define _HUDSON_SMBUS_C_
19 #include <io.h>
20 #include <stdint.h>
21 #include "smbus.h"
23 static int smbus_wait_until_ready(u32 smbus_io_base)
25 u32 loops;
26 loops = SMBUS_TIMEOUT;
27 do {
28 u8 val;
29 val = inb(smbus_io_base + SMBHSTSTAT);
30 val &= 0x1f;
31 if (val == 0) { /* ready now */
32 return 0;
34 outb(val, smbus_io_base + SMBHSTSTAT);
35 } while (--loops);
36 return -2; /* time out */
39 static int smbus_wait_until_done(u32 smbus_io_base)
41 u32 loops;
42 loops = SMBUS_TIMEOUT;
43 do {
44 u8 val;
46 val = inb(smbus_io_base + SMBHSTSTAT);
47 val &= 0x1f; /* mask off reserved bits */
48 if (val & 0x1c) {
49 return -5; /* error */
51 if (val == 0x02) {
52 outb(val, smbus_io_base + SMBHSTSTAT); /* clear status */
53 return 0;
55 } while (--loops);
56 return -3; /* timeout */
59 int do_smbus_recv_byte(u32 smbus_io_base, u32 device)
61 u8 byte;
63 if (smbus_wait_until_ready(smbus_io_base) < 0) {
64 return -2; /* not ready */
67 /* set the device I'm talking too */
68 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
70 byte = inb(smbus_io_base + SMBHSTCTRL);
71 byte &= 0xe3; /* Clear [4:2] */
72 byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */
73 outb(byte, smbus_io_base + SMBHSTCTRL);
75 /* poll for transaction completion */
76 if (smbus_wait_until_done(smbus_io_base) < 0) {
77 return -3; /* timeout or error */
80 /* read results of transaction */
81 byte = inb(smbus_io_base + SMBHSTCMD);
83 return byte;
86 int do_smbus_send_byte(u32 smbus_io_base, u32 device, u8 val)
88 u8 byte;
90 if (smbus_wait_until_ready(smbus_io_base) < 0) {
91 return -2; /* not ready */
94 /* set the command... */
95 outb(val, smbus_io_base + SMBHSTCMD);
97 /* set the device I'm talking too */
98 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
100 byte = inb(smbus_io_base + SMBHSTCTRL);
101 byte &= 0xe3; /* Clear [4:2] */
102 byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */
103 outb(byte, smbus_io_base + SMBHSTCTRL);
105 /* poll for transaction completion */
106 if (smbus_wait_until_done(smbus_io_base) < 0) {
107 return -3; /* timeout or error */
110 return 0;
113 int do_smbus_read_byte(u32 smbus_io_base, u32 device,
114 u32 address)
116 u8 byte;
118 if (smbus_wait_until_ready(smbus_io_base) < 0) {
119 return -2; /* not ready */
122 /* set the command/address... */
123 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
125 /* set the device I'm talking too */
126 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
128 byte = inb(smbus_io_base + SMBHSTCTRL);
129 byte &= 0xe3; /* Clear [4:2] */
130 byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */
131 outb(byte, smbus_io_base + SMBHSTCTRL);
133 /* poll for transaction completion */
134 if (smbus_wait_until_done(smbus_io_base) < 0) {
135 return -3; /* timeout or error */
138 /* read results of transaction */
139 byte = inb(smbus_io_base + SMBHSTDAT0);
141 return byte;
144 int do_smbus_write_byte(u32 smbus_io_base, u32 device,
145 u32 address, u8 val)
147 u8 byte;
149 if (smbus_wait_until_ready(smbus_io_base) < 0) {
150 return -2; /* not ready */
153 /* set the command/address... */
154 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
156 /* set the device I'm talking too */
157 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
159 /* output value */
160 outb(val, smbus_io_base + SMBHSTDAT0);
162 byte = inb(smbus_io_base + SMBHSTCTRL);
163 byte &= 0xe3; /* Clear [4:2] */
164 byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */
165 outb(byte, smbus_io_base + SMBHSTCTRL);
167 /* poll for transaction completion */
168 if (smbus_wait_until_done(smbus_io_base) < 0) {
169 return -3; /* timeout or error */
172 return 0;
175 void alink_ab_indx(u32 reg_space, u32 reg_addr,
176 u32 mask, u32 val)
178 u32 tmp;
180 outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX);
181 tmp = inl(AB_DATA);
182 /* rpr 4.2
183 * For certain revisions of the chip, the ABCFG registers,
184 * with an address of 0x100NN (where 'N' is any hexadecimal
185 * number), require an extra programming step.*/
186 outl(0, AB_INDX);
188 tmp &= ~mask;
189 tmp |= val;
191 /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | reg_addr); */
192 outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX); /* probably we dont have to do it again. */
193 outl(tmp, AB_DATA);
194 outl(0, AB_INDX);
197 void alink_rc_indx(u32 reg_space, u32 reg_addr, u32 port,
198 u32 mask, u32 val)
200 u32 tmp;
202 outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX);
203 tmp = inl(AB_DATA);
204 /* rpr 4.2
205 * For certain revisions of the chip, the ABCFG registers,
206 * with an address of 0x100NN (where 'N' is any hexadecimal
207 * number), require an extra programming step.*/
208 outl(0, AB_INDX);
210 tmp &= ~mask;
211 tmp |= val;
213 //printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | (port&3) << 24 | reg_addr);
214 outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX); /* probably we dont have to do it again. */
215 outl(tmp, AB_DATA);
216 outl(0, AB_INDX);
219 /* space = 0: AX_INDXC, AX_DATAC
220 * space = 1: AX_INDXP, AX_DATAP
222 void alink_ax_indx(u32 space /*c or p? */ , u32 axindc,
223 u32 mask, u32 val)
225 u32 tmp;
227 /* read axindc to tmp */
228 outl(space << 29 | space << 3 | 0x30, AB_INDX);
229 outl(axindc, AB_DATA);
230 outl(0, AB_INDX);
231 outl(space << 29 | space << 3 | 0x34, AB_INDX);
232 tmp = inl(AB_DATA);
233 outl(0, AB_INDX);
235 tmp &= ~mask;
236 tmp |= val;
238 /* write tmp */
239 outl(space << 29 | space << 3 | 0x30, AB_INDX);
240 outl(axindc, AB_DATA);
241 outl(0, AB_INDX);
242 outl(space << 29 | space << 3 | 0x34, AB_INDX);
243 outl(tmp, AB_DATA);
244 outl(0, AB_INDX);
246 #endif