blob: 6c61cf0f39882a5cc14bffce2dc25cb59e504e49 [file] [log] [blame]
Uwe Hermann97f56a42008-10-21 15:07:53 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008 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.
Uwe Hermann97f56a42008-10-21 15:07:53 +000014 */
15
stepan836ae292010-12-08 05:42:47 +000016#include "smbus.h"
Michael Xie7586cef2008-09-22 13:11:39 +000017
18static inline void smbus_delay(void)
19{
20 outb(0x80, 0x80);
21}
22
23static int smbus_wait_until_ready(u32 smbus_io_base)
24{
Joe Bao164463c2008-12-01 19:37:21 +000025 u32 loops;
Michael Xie7586cef2008-09-22 13:11:39 +000026 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;
33 }
34 outb(val, smbus_io_base + SMBHSTSTAT);
35 } while (--loops);
36 return -2; /* time out */
37}
38
39static int smbus_wait_until_done(u32 smbus_io_base)
40{
Joe Bao164463c2008-12-01 19:37:21 +000041 u32 loops;
Michael Xie7586cef2008-09-22 13:11:39 +000042 loops = SMBUS_TIMEOUT;
43 do {
44 u8 val;
45
46 val = inb(smbus_io_base + SMBHSTSTAT);
47 val &= 0x1f; /* mask off reserved bits */
48 if (val & 0x1c) {
49 return -5; /* error */
50 }
51 if (val == 0x02) {
52 outb(val, smbus_io_base + SMBHSTSTAT); /* clear status */
53 return 0;
54 }
55 } while (--loops);
56 return -3; /* timeout */
57}
58
Stefan Reinauer5e33e822010-07-07 21:59:06 +000059int do_smbus_recv_byte(u32 smbus_io_base, u32 device)
Michael Xie7586cef2008-09-22 13:11:39 +000060{
61 u8 byte;
62
63 if (smbus_wait_until_ready(smbus_io_base) < 0) {
64 return -2; /* not ready */
65 }
66
67 /* set the device I'm talking too */
68 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
69
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);
74
75 /* poll for transaction completion */
76 if (smbus_wait_until_done(smbus_io_base) < 0) {
77 return -3; /* timeout or error */
78 }
79
80 /* read results of transaction */
81 byte = inb(smbus_io_base + SMBHSTCMD);
82
83 return byte;
84}
85
Stefan Reinauer5e33e822010-07-07 21:59:06 +000086int do_smbus_send_byte(u32 smbus_io_base, u32 device, u8 val)
Michael Xie7586cef2008-09-22 13:11:39 +000087{
88 u8 byte;
89
90 if (smbus_wait_until_ready(smbus_io_base) < 0) {
91 return -2; /* not ready */
92 }
93
94 /* set the command... */
95 outb(val, smbus_io_base + SMBHSTCMD);
96
97 /* set the device I'm talking too */
98 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
99
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);
104
105 /* poll for transaction completion */
106 if (smbus_wait_until_done(smbus_io_base) < 0) {
107 return -3; /* timeout or error */
108 }
109
110 return 0;
111}
112
Uwe Hermannff492b12010-09-24 23:37:25 +0000113int do_smbus_read_byte(u32 smbus_io_base, u32 device, u32 address)
Michael Xie7586cef2008-09-22 13:11:39 +0000114{
115 u8 byte;
116
117 if (smbus_wait_until_ready(smbus_io_base) < 0) {
118 return -2; /* not ready */
119 }
120
121 /* set the command/address... */
122 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
123
124 /* set the device I'm talking too */
125 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
126
127 byte = inb(smbus_io_base + SMBHSTCTRL);
128 byte &= 0xe3; /* Clear [4:2] */
129 byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */
130 outb(byte, smbus_io_base + SMBHSTCTRL);
131
132 /* poll for transaction completion */
133 if (smbus_wait_until_done(smbus_io_base) < 0) {
134 return -3; /* timeout or error */
135 }
136
137 /* read results of transaction */
138 byte = inb(smbus_io_base + SMBHSTDAT0);
139
140 return byte;
141}
142
Uwe Hermannff492b12010-09-24 23:37:25 +0000143int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val)
Michael Xie7586cef2008-09-22 13:11:39 +0000144{
145 u8 byte;
146
147 if (smbus_wait_until_ready(smbus_io_base) < 0) {
148 return -2; /* not ready */
149 }
150
151 /* set the command/address... */
152 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
153
154 /* set the device I'm talking too */
155 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
156
157 /* output value */
158 outb(val, smbus_io_base + SMBHSTDAT0);
159
160 byte = inb(smbus_io_base + SMBHSTCTRL);
161 byte &= 0xe3; /* Clear [4:2] */
162 byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */
163 outb(byte, smbus_io_base + SMBHSTCTRL);
164
165 /* poll for transaction completion */
166 if (smbus_wait_until_done(smbus_io_base) < 0) {
167 return -3; /* timeout or error */
168 }
169
170 return 0;
171}
172
Uwe Hermannff492b12010-09-24 23:37:25 +0000173static void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val)
Michael Xie7586cef2008-09-22 13:11:39 +0000174{
Joe Bao164463c2008-12-01 19:37:21 +0000175 u32 tmp;
176
Michael Xie7586cef2008-09-22 13:11:39 +0000177 outl((reg_space & 0x3) << 30 | reg_addr, AB_INDX);
178 tmp = inl(AB_DATA);
179
180 tmp &= ~mask;
181 tmp |= val;
182
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000183 /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<30 | reg_addr); */
Martin Rothdcf253c2014-12-16 20:51:31 -0700184 outl((reg_space & 0x3) << 30 | reg_addr, AB_INDX); /* probably we don't have to do it again. */
Michael Xie7586cef2008-09-22 13:11:39 +0000185 outl(tmp, AB_DATA);
186}
187
188/* space = 0: AX_INDXC, AX_DATAC
189* space = 1: AX_INDXP, AX_DATAP
190 */
Joe Bao164463c2008-12-01 19:37:21 +0000191static void alink_ax_indx(u32 space /*c or p? */ , u32 axindc,
192 u32 mask, u32 val)
Michael Xie7586cef2008-09-22 13:11:39 +0000193{
Joe Bao164463c2008-12-01 19:37:21 +0000194 u32 tmp;
Michael Xie7586cef2008-09-22 13:11:39 +0000195
196 /* read axindc to tmp */
197 outl(space << 30 | space << 3 | 0x30, AB_INDX);
198 outl(axindc, AB_DATA);
199 outl(space << 30 | space << 3 | 0x34, AB_INDX);
200 tmp = inl(AB_DATA);
201
202 tmp &= ~mask;
203 tmp |= val;
204
205 /* write tmp */
206 outl(space << 30 | space << 3 | 0x30, AB_INDX);
207 outl(axindc, AB_DATA);
208 outl(space << 30 | space << 3 | 0x34, AB_INDX);
209 outl(tmp, AB_DATA);
210}