File: smbus_amd.c

package info (click to toggle)
xmbmon 2.05-8
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 928 kB
  • ctags: 910
  • sloc: ansic: 6,089; sh: 2,151; perl: 87; makefile: 79
file content (226 lines) | stat: -rw-r--r-- 6,059 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

/* SMB handling routines for AMD7xx, by YRS 2002.07.
	Information on how to access SMBus is provided
	by ":p araffin.(Yoneya)", MANY THANKS!!

	Common to AMD756/766/768 and NVidia nForce

	SMBus IO method:

	smb_base  : Base Address
	addr      : Slave Device Address
	cmd       : Command Register 

	Note that SMBus Slave Device Address is totall 1byte data,
	whose upper 7bits is for address and the lowest bit for read (=1)
	and for write (=0).
	The input "addr" in the following routines is this 1byte data,
	where the lowest bit can be either 0 or 1.

            7             0
           +-+-+-+-+-+-+-+-+
           | Slave addr. |f|    f = 0 for write, =1 for read.
           +-+-+-+-+-+-+-+-+

	Do not confuse "Slave address" which is "addr(here)"/2!

 */

#ifdef SMB_DEBUG
extern int debug_flag;
#include <stdio.h>
#endif

#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>

#include "smbuses.h"
#include "io_cpu.h"

#define LSB 	0x01

#define LOOP_COUNT	0x2000

/* command address */
#define SMBHSTS 	0x0
#define SMBHCTRL	0x2
#define SMBHADDR	0x4
#define SMBHDATA	0x6
#define SMBHCMD 	0x8

/* status flag */
#define SMBHSTS_TOSTS	0x20	/* ? not used */
#define SMBHSTS_DONE	0x10	/* host cycle (command) completed */
#define SMBHSTS_BUSY	0x08	/* host busy */
#define SMBHSTS_ERROR	0x04	/* device error */
#define SMBHSTS_COLLID	0x02	/* bus collision */
#define SMBHSTS_ABORT	0x01	/* abort */
#define SMBHSTS_CLEAR	(SMBHSTS_TOSTS|SMBHSTS_DONE|SMBHSTS_ERROR|\
							SMBHSTS_COLLID|SMBHSTS_ABORT)	/* clear status*/

/* control command number */
#define SMBHCTRL_ABORT	0x20	/* abort */
#define SMBHCTRL_START	0x08	/* start command */
#define SMBHCTRL_WORD	0x03	/* word I/O */
#define SMBHCTRL_BYTE	0x02	/* byte I/O */


static int readbyte(int smb_base, int addr, int cmd)
{
	u_char saddr = 2*(addr/2);
	u_short dat;
	int i;

top:
	OUTw((u_short) smb_base, SMBHSTS_CLEAR); WAIT; WAIT;
	for (i = 0; i < LOOP_COUNT; ++i) {
		dat = INw((u_short) smb_base); WAIT; WAIT;
		OUTw((u_short) smb_base, dat); WAIT; WAIT;
		if (!(dat & (SMBHSTS_COLLID | SMBHSTS_BUSY)))
			goto step;
		if (!(dat & SMBHSTS_BUSY))
			break;
	}
	OUTw((u_short) (smb_base + SMBHCTRL), SMBHCTRL_ABORT); WAIT; WAIT;

step:
	OUTw((u_short) (smb_base + SMBHADDR), (saddr | LSB)); WAIT; WAIT;
	OUTb((u_short) (smb_base + SMBHCMD), (u_char) cmd); WAIT; WAIT;
	OUTw((u_short) (smb_base + SMBHCTRL),
		(SMBHCTRL_START | SMBHCTRL_BYTE)); WAIT; WAIT;

	for (i = 0; i < LOOP_COUNT; ++i) {
		dat = INw((u_short) smb_base); WAIT; WAIT;
		if (dat & SMBHSTS_COLLID) {
#ifdef SMB_DEBUG
if (debug_flag > 1)
fprintf(stderr, "\n   OH! collision! = 0x%02X\n", dat);
#endif
			goto top;
		}
		if (!(dat & SMBHSTS_BUSY) && (dat & (SMBHSTS_DONE | SMBHSTS_ERROR)))
			break;
	}
#ifdef SMB_DEBUG
if (debug_flag > 1)
fprintf(stderr, "   Readbyte: flag = 0x%02X, loop#:%04d", dat, i);
#endif
	if (dat & SMBHSTS_DONE) {
		dat = INw((u_short) (smb_base + SMBHDATA)); WAIT; WAIT;
		return (dat & 0x00FF);
	} else
		return -1;
}

static int readword(int smb_base, int addr, int cmd)
{
	u_char saddr = 2*(addr/2);
	u_short dat;
	int i;

top:
	OUTw((u_short) smb_base, SMBHSTS_CLEAR); WAIT; WAIT;
	for (i = 0; i < LOOP_COUNT; ++i) {
		dat = INw((u_short) smb_base); WAIT; WAIT;
		OUTw((u_short) smb_base, dat); WAIT; WAIT;
		if (!(dat & (SMBHSTS_COLLID | SMBHSTS_BUSY)))
			goto step;
		if (!(dat & SMBHSTS_BUSY))
			break;
	}
	OUTw((u_short) (smb_base + SMBHCTRL), SMBHCTRL_ABORT); WAIT; WAIT;

step:
	OUTw((u_short) (smb_base + SMBHADDR), (saddr | LSB)); WAIT; WAIT;
	OUTb((u_short) (smb_base + SMBHCMD), (u_char) cmd); WAIT; WAIT;
	OUTw((u_short) (smb_base + SMBHCTRL),
		(SMBHCTRL_START | SMBHCTRL_WORD)); WAIT; WAIT;

	for (i = 0; i < LOOP_COUNT; ++i) {
		dat = INw((u_short) smb_base); WAIT; WAIT;
		if (dat & SMBHSTS_COLLID)
			goto top;
		if (!(dat & SMBHSTS_BUSY) && (dat & (SMBHSTS_DONE | SMBHSTS_ERROR)))
			break;
	}
	if (dat & SMBHSTS_DONE) {
		dat = INw((u_short) (smb_base + SMBHDATA)); WAIT; WAIT;
		return (dat & 0xFFFF);
	} else
		return -1;
}

static int writebyte(int smb_base, int addr, int cmd, int value)
{
	u_char saddr = 2*(addr/2);
	u_short dat;
	int i;

	OUTw((u_short) smb_base, SMBHSTS_CLEAR); WAIT; WAIT;
	for (i = 0; i < LOOP_COUNT; ++i) {
		dat = INw((u_short) smb_base); WAIT; WAIT;
		OUTw((u_short) smb_base, dat); WAIT; WAIT;
		if (!(dat & (SMBHSTS_COLLID | SMBHSTS_BUSY)))
			goto step;
		if (!(dat & SMBHSTS_BUSY))
			break;
	}
	OUTw((u_short) (smb_base + SMBHCTRL), SMBHCTRL_ABORT); WAIT; WAIT;

step:
	OUTw((u_short) (smb_base + SMBHADDR), saddr); WAIT; WAIT;
	OUTb((u_short) (smb_base + SMBHCMD), (u_char) cmd); WAIT; WAIT;
	OUTw((u_short) (smb_base + SMBHDATA), (u_char) value); WAIT; WAIT;
	OUTw((u_short) (smb_base + SMBHCTRL),
		(SMBHCTRL_START | SMBHCTRL_BYTE)); WAIT; WAIT;

	for (i = 0; i < LOOP_COUNT; ++i) {
		dat = INw((u_short) smb_base); WAIT; WAIT;
		if (!(dat & SMBHSTS_BUSY) && (dat & (SMBHSTS_DONE | SMBHSTS_ERROR)))
			break;
	}
	if (dat & SMBHSTS_DONE)
		return 0;
	else
		return -1;
}

static int writeword(int smb_base, int addr, int cmd, int value)
{
	u_char saddr = 2*(addr/2);
	u_short dat;
	int i;

	OUTw((u_short) smb_base, SMBHSTS_CLEAR); WAIT; WAIT;
	for (i = 0; i < LOOP_COUNT; ++i) {
		dat = INw((u_short) smb_base); WAIT; WAIT;
		OUTw((u_short) smb_base, dat); WAIT; WAIT;
		if (!(dat & (SMBHSTS_COLLID | SMBHSTS_BUSY)))
			goto step;
		if (!(dat & SMBHSTS_BUSY))
			break;
	}
	OUTw((u_short) (smb_base + SMBHCTRL), SMBHCTRL_ABORT); WAIT; WAIT;

step:
	OUTw((u_short) (smb_base + SMBHADDR), saddr); WAIT; WAIT;
	OUTb((u_short) (smb_base + SMBHCMD), (u_char) cmd); WAIT; WAIT;
	OUTw((u_short) (smb_base + SMBHDATA), (u_short) value); WAIT; WAIT;
	OUTw((u_short) (smb_base + SMBHCTRL),
		(SMBHCTRL_START | SMBHCTRL_WORD)); WAIT; WAIT;

	for (i = 0; i < LOOP_COUNT; ++i) {
		dat = INw((u_short) smb_base); WAIT; WAIT;
		if (!(dat & SMBHSTS_BUSY) && (dat & (SMBHSTS_DONE | SMBHSTS_ERROR)))
			break;
	}
	if (dat & SMBHSTS_DONE)
		return 0;
	else
		return -1;
}

struct smbus_io smbus_amd = {
	readbyte, readword, writebyte, writeword
};