summaryrefslogtreecommitdiff
path: root/src/southbridge/via/vt8231/vt8231_early_smbus.c
blob: e419d59b636388b354308e36bf1ca84e045eebce (plain)
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
227
228
229
230
231
#define SMBUS_IO_BASE 0x5000

#define SMBHSTSTAT 0x0
#define SMBSLVSTAT 0x1
#define SMBHSTCTL  0x2
#define SMBHSTCMD  0x3
#define SMBXMITADD 0x4
#define SMBHSTDAT0 0x5
#define SMBHSTDAT1 0x6
#define SMBBLKDAT  0x7
#define SMBSLVCTL  0x8
#define SMBTRNSADD 0x9
#define SMBSLVDATA 0xa
#define SMLINK_PIN_CTL 0xe
#define SMBUS_PIN_CTL  0xf 

/* Define register settings */
#define HOST_RESET 0xff
#define DIMM_BASE 0xa0        // 1010000 is base for DIMM in SMBus
#define READ_CMD  0x01        // 1 in the 0 bit of SMBHSTADD states to READ


#define SMBUS_TIMEOUT (100*1000*10)

static void enable_smbus(void)
{
	device_t dev;
	unsigned char c;
	/* Power management controller */
	dev = pci_locate_device(PCI_ID(0x1106,0x8235), 0);
	
	if (dev == PCI_DEV_INVALID) {
		die("SMBUS controller not found\r\n");
	}
	
	// set IO base address to SMBUS_IO_BASE
	pci_write_config32(dev, 0x90, SMBUS_IO_BASE|1);
	
	// Enable SMBus 
	c = pci_read_config8(dev, 0xd2);
	c |= 5;
	pci_write_config8(dev, 0xd2, c);
	
	/* make it work for I/O ...
	 */
	dev = pci_locate_device(PCI_ID(0x1106,0x8231), 0);
	c = pci_read_config8(dev, 4);
	c |= 1;
	pci_write_config8(dev, 4, c);
	print_err_hex8(c);
	print_err(" is the comm register\n");
	
	print_debug("SMBus controller enabled\r\n");
}


static inline void smbus_delay(void)
{
	outb(0x80, 0x80);
}

static int smbus_wait_until_ready(void)
{
	unsigned char c;
	unsigned long loops;
	loops = SMBUS_TIMEOUT;
	do {
		unsigned char val;
		smbus_delay();
		c = inb(SMBUS_IO_BASE + SMBHSTSTAT);
		while((c & 1) == 1) {
			print_err("c is ");
			print_err_hex8(c);
			print_err("\n");
			c = inb(SMBUS_IO_BASE + SMBHSTSTAT);
			/* nop */ 
		}

	} while(--loops);
	return loops?0:-1;
}

void smbus_reset(void)
{
	outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
	outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
	outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
	outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
	
	smbus_wait_until_ready();
	print_err("After reset status ");
	print_err_hex8( inb(SMBUS_IO_BASE + SMBHSTSTAT));
	print_err("\n");
}
  


static int smbus_wait_until_done(void)
{
	unsigned long loops;
	unsigned char byte;
	loops = SMBUS_TIMEOUT;
	do {
		unsigned char val;
		smbus_delay();
		
		byte = inb(SMBUS_IO_BASE + SMBHSTSTAT);
		if (byte & 1)
			break;
		
	} while(--loops);
	return loops?0:-1;
}

static void smbus_print_error(unsigned char host_status_register)
{

	print_err("smbus_error: ");
	print_err_hex8(host_status_register);
	print_err("\n");
	if (host_status_register & (1 << 4)) {
		print_err("Interrup/SMI# was Failed Bus Transaction\n");
	}
	if (host_status_register & (1 << 3)) {
		print_err("Bus Error\n");
	}
	if (host_status_register & (1 << 2)) {
		print_err("Device Error\n");
	}
	if (host_status_register & (1 << 1)) {
		print_err("Interrupt/SMI# was Successful Completion\n");
	}
	if (host_status_register & (1 << 0)) {
		print_err("Host Busy\n");
	}
}


/* SMBus routines borrowed from VIA's Trident Driver */
/* this works, so I am not going to touch it for now -- rgm */
static unsigned char smbus_read_byte(unsigned char devAdr, 
				unsigned char bIndex) 
{
	unsigned short i;
	unsigned char  bData;
	unsigned char  sts = 0;
	
	/* clear host status */
	outb(0xff, SMBUS_IO_BASE);
	
	/* check SMBUS ready */
	for ( i = 0; i < 0xFFFF; i++ )
		if ( (inb(SMBUS_IO_BASE) & 0x01) == 0 )
			break;
	
	/* set host command */
	outb(bIndex, SMBUS_IO_BASE+3);
	
	/* set slave address */
	outb(devAdr | 0x01, SMBUS_IO_BASE+4);
	
	/* start */
	outb(0x48, SMBUS_IO_BASE+2);
	
	/* SMBUS Wait Ready */
	for ( i = 0; i < 0xFFFF; i++ )
		if ( ((sts = inb(SMBUS_IO_BASE)) & 0x01) == 0 )
			break;
	if ((sts & ~3) != 0) {
		smbus_print_error(sts);
		return 0;
	}
	bData=inb(SMBUS_IO_BASE+5);
	
	return bData;
	
}

/* for reference, here is the fancier version which we will use at some 
 * point
 */
# if 0
int smbus_read_byte(unsigned device, unsigned address, unsigned char *result)
{
	unsigned char host_status_register;
	unsigned char byte;
	
	reset();
	
	smbus_wait_until_ready();
	
	/* setup transaction */
	/* disable interrupts */
	outb(inb(SMBUS_IO_BASE + SMBHSTCTL) & (~1), SMBUS_IO_BASE + SMBHSTCTL);
	/* set the device I'm talking too */
	outb(((device & 0x7f) << 1) | 1, SMBUS_IO_BASE + SMBXMITADD);
	/* set the command/address... */
	outb(address & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
	/* set up for a byte data read */
	outb((inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xE3) | (0x2 << 2),
		SMBUS_IO_BASE + SMBHSTCTL);
	
	/* clear any lingering errors, so the transaction will run */
	outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
	
	/* clear the data byte...*/
	outb(0, SMBUS_IO_BASE + SMBHSTDAT0);
	
	/* start the command */
	outb((inb(SMBUS_IO_BASE + SMBHSTCTL) | 0x40),
		SMBUS_IO_BASE + SMBHSTCTL);
	
	/* poll for transaction completion */
	smbus_wait_until_done();
	
	host_status_register = inb(SMBUS_IO_BASE + SMBHSTSTAT);
	
	/* Ignore the In Use Status... */
	host_status_register &= ~(1 << 6);
	
	/* read results of transaction */
	byte = inb(SMBUS_IO_BASE + SMBHSTDAT0);
	smbus_print_error(byte);
	
	*result = byte;
	return host_status_register != 0x02;
}


#endif