blob: c3183951f736ed9294f2a6331a01099f802a1c4f [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Frank Vibrans63e62b02011-02-14 18:38:14 +00003
4
5#include <arch/io.h>
6#include "smbus.h"
Kerry Shefeed3292011-08-18 18:03:44 +08007#include <console/console.h> /* printk */
Frank Vibrans63e62b02011-02-14 18:38:14 +00008
Frank Vibrans63e62b02011-02-14 18:38:14 +00009static int smbus_wait_until_ready(u32 smbus_io_base)
10{
11 u32 loops;
12
13 loops = SMBUS_TIMEOUT;
14 do {
15 u8 val;
16 val = inb(smbus_io_base + SMBHSTSTAT);
17 val &= 0x1f;
18 if (val == 0) { /* ready now */
19 return 0;
20 }
21 outb(val, smbus_io_base + SMBHSTSTAT);
22 } while (--loops);
23
24 return -2; /* time out */
25}
26
27static int smbus_wait_until_done(u32 smbus_io_base)
28{
29 u32 loops;
30
31 loops = SMBUS_TIMEOUT;
32 do {
33 u8 val;
34
35 val = inb(smbus_io_base + SMBHSTSTAT);
36 val &= 0x1f; /* mask off reserved bits */
37 if (val & 0x1c) {
38 return -5; /* error */
39 }
40 if (val == 0x02) {
41 outb(val, smbus_io_base + SMBHSTSTAT); /* clear status */
42 return 0;
43 }
44 } while (--loops);
45
46 return -3; /* timeout */
47}
48
49int do_smbus_recv_byte(u32 smbus_io_base, u32 device)
50{
51 u8 byte;
52
53 if (smbus_wait_until_ready(smbus_io_base) < 0) {
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020054 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_recv_byte - smbus not ready.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +000055 return -2; /* not ready */
56 }
57
Kerry Shefeed3292011-08-18 18:03:44 +080058 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_recv_byte - Start.\n");
Jonathan Neuschäfer70903772017-09-23 21:39:02 +020059 /* set the device I'm talking to */
Frank Vibrans63e62b02011-02-14 18:38:14 +000060 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
61
62 byte = inb(smbus_io_base + SMBHSTCTRL);
63 byte &= 0xe3; /* Clear [4:2] */
64 byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */
65 outb(byte, smbus_io_base + SMBHSTCTRL);
66
67 /* poll for transaction completion */
68 if (smbus_wait_until_done(smbus_io_base) < 0) {
69 return -3; /* timeout or error */
70 }
71
72 /* read results of transaction */
73 byte = inb(smbus_io_base + SMBHSTCMD);
74
Kerry Shefeed3292011-08-18 18:03:44 +080075 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_recv_byte - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +000076 return byte;
77}
78
79int do_smbus_send_byte(u32 smbus_io_base, u32 device, u8 val)
80{
81 u8 byte;
82
83 if (smbus_wait_until_ready(smbus_io_base) < 0) {
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020084 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_send_byte - smbus not ready.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +000085 return -2; /* not ready */
86 }
87
Kerry Shefeed3292011-08-18 18:03:44 +080088 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_send_byte - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +000089 /* set the command... */
90 outb(val, smbus_io_base + SMBHSTCMD);
91
Jonathan Neuschäfer70903772017-09-23 21:39:02 +020092 /* set the device I'm talking to */
Frank Vibrans63e62b02011-02-14 18:38:14 +000093 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
94
95 byte = inb(smbus_io_base + SMBHSTCTRL);
96 byte &= 0xe3; /* Clear [4:2] */
97 byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */
98 outb(byte, smbus_io_base + SMBHSTCTRL);
99
100 /* poll for transaction completion */
101 if (smbus_wait_until_done(smbus_io_base) < 0) {
102 return -3; /* timeout or error */
103 }
104
Kerry Shefeed3292011-08-18 18:03:44 +0800105 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_send_byte - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000106 return 0;
107}
108
109int do_smbus_read_byte(u32 smbus_io_base, u32 device, u32 address)
110{
111 u8 byte;
112
113 if (smbus_wait_until_ready(smbus_io_base) < 0) {
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200114 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_read_byte - smbus not ready.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000115 return -2; /* not ready */
116 }
117
Kerry Shefeed3292011-08-18 18:03:44 +0800118 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_read_byte - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000119 /* set the command/address... */
120 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
121
Jonathan Neuschäfer70903772017-09-23 21:39:02 +0200122 /* set the device I'm talking to */
Frank Vibrans63e62b02011-02-14 18:38:14 +0000123 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR);
124
125 byte = inb(smbus_io_base + SMBHSTCTRL);
126 byte &= 0xe3; /* Clear [4:2] */
127 byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */
128 outb(byte, smbus_io_base + SMBHSTCTRL);
129
130 /* poll for transaction completion */
131 if (smbus_wait_until_done(smbus_io_base) < 0) {
132 return -3; /* timeout or error */
133 }
134
135 /* read results of transaction */
136 byte = inb(smbus_io_base + SMBHSTDAT0);
137
Kerry Shefeed3292011-08-18 18:03:44 +0800138 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_read_byte - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000139 return byte;
140}
141
142int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val)
143{
144 u8 byte;
145
146 if (smbus_wait_until_ready(smbus_io_base) < 0) {
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200147 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_write_byte - smbus not ready.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000148 return -2; /* not ready */
149 }
150
Kerry Shefeed3292011-08-18 18:03:44 +0800151 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_write_byte - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000152 /* set the command/address... */
153 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
154
Jonathan Neuschäfer70903772017-09-23 21:39:02 +0200155 /* set the device I'm talking to */
Frank Vibrans63e62b02011-02-14 18:38:14 +0000156 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR);
157
158 /* output value */
159 outb(val, smbus_io_base + SMBHSTDAT0);
160
161 byte = inb(smbus_io_base + SMBHSTCTRL);
162 byte &= 0xe3; /* Clear [4:2] */
163 byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */
164 outb(byte, smbus_io_base + SMBHSTCTRL);
165
166 /* poll for transaction completion */
167 if (smbus_wait_until_done(smbus_io_base) < 0) {
168 return -3; /* timeout or error */
169 }
170
Kerry Shefeed3292011-08-18 18:03:44 +0800171 printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_write_byte - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000172 return 0;
173}
174
175void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val)
176{
177 u32 tmp;
178
Kerry Shefeed3292011-08-18 18:03:44 +0800179 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ab_indx - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000180 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);
187
188 tmp &= ~mask;
189 tmp |= val;
190
191 /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | reg_addr); */
Martin Roth3c3a50c2014-12-16 20:50:26 -0700192 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 +0000193 outl(tmp, AB_DATA);
194 outl(0, AB_INDX);
Kerry Shefeed3292011-08-18 18:03:44 +0800195 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ab_indx - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000196}
197
198void alink_rc_indx(u32 reg_space, u32 reg_addr, u32 port, u32 mask, u32 val)
199{
200 u32 tmp;
201
Kerry Shefeed3292011-08-18 18:03:44 +0800202 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_rc_indx - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000203 outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX);
204 tmp = inl(AB_DATA);
205 /* rpr 4.2
206 * For certain revisions of the chip, the ABCFG registers,
207 * with an address of 0x100NN (where 'N' is any hexadecimal
208 * number), require an extra programming step.*/
209 outl(0, AB_INDX);
210
211 tmp &= ~mask;
212 tmp |= val;
213
214 //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 -0700215 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 +0000216 outl(tmp, AB_DATA);
217 outl(0, AB_INDX);
Kerry Shefeed3292011-08-18 18:03:44 +0800218 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_rc_indx - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000219}
220
221/* space = 0: AX_INDXC, AX_DATAC
222 * space = 1: AX_INDXP, AX_DATAP
223 */
Elyes HAOUASa342f392018-10-17 10:56:26 +0200224void alink_ax_indx(u32 space /*c or p? */, u32 axindc, u32 mask, u32 val)
Frank Vibrans63e62b02011-02-14 18:38:14 +0000225{
226 u32 tmp;
227
Kerry Shefeed3292011-08-18 18:03:44 +0800228 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ax_indx - Start.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000229 /* read axindc to tmp */
230 outl(space << 29 | space << 3 | 0x30, AB_INDX);
231 outl(axindc, AB_DATA);
232 outl(0, AB_INDX);
233 outl(space << 29 | space << 3 | 0x34, AB_INDX);
234 tmp = inl(AB_DATA);
235 outl(0, AB_INDX);
236
237 tmp &= ~mask;
238 tmp |= val;
239
240 /* write tmp */
241 outl(space << 29 | space << 3 | 0x30, AB_INDX);
242 outl(axindc, AB_DATA);
243 outl(0, AB_INDX);
244 outl(space << 29 | space << 3 | 0x34, AB_INDX);
245 outl(tmp, AB_DATA);
246 outl(0, AB_INDX);
Kerry Shefeed3292011-08-18 18:03:44 +0800247 printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ax_indx - End.\n");
Frank Vibrans63e62b02011-02-14 18:38:14 +0000248}