]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /** |
2 | * i2c-ali1563.c - i2c driver for the ALi 1563 Southbridge | |
3 | * | |
4 | * Copyright (C) 2004 Patrick Mochel | |
7188cc66 | 5 | * 2005 Rudolf Marek <r.marek@assembler.cz> |
1da177e4 LT |
6 | * |
7 | * The 1563 southbridge is deceptively similar to the 1533, with a | |
8 | * few notable exceptions. One of those happens to be the fact they | |
9 | * upgraded the i2c core to be 2.0 compliant, and happens to be almost | |
10 | * identical to the i2c controller found in the Intel 801 south | |
11 | * bridges. | |
12 | * | |
13 | * This driver is based on a mix of the 15x3, 1535, and i801 drivers, | |
14 | * with a little help from the ALi 1563 spec. | |
15 | * | |
16 | * This file is released under the GPLv2 | |
17 | */ | |
18 | ||
19 | #include <linux/module.h> | |
20 | #include <linux/delay.h> | |
21 | #include <linux/i2c.h> | |
22 | #include <linux/pci.h> | |
23 | #include <linux/init.h> | |
24 | ||
25 | #define ALI1563_MAX_TIMEOUT 500 | |
26 | #define ALI1563_SMBBA 0x80 | |
27 | #define ALI1563_SMB_IOEN 1 | |
28 | #define ALI1563_SMB_HOSTEN 2 | |
29 | #define ALI1563_SMB_IOSIZE 16 | |
30 | ||
31 | #define SMB_HST_STS (ali1563_smba + 0) | |
32 | #define SMB_HST_CNTL1 (ali1563_smba + 1) | |
33 | #define SMB_HST_CNTL2 (ali1563_smba + 2) | |
34 | #define SMB_HST_CMD (ali1563_smba + 3) | |
35 | #define SMB_HST_ADD (ali1563_smba + 4) | |
36 | #define SMB_HST_DAT0 (ali1563_smba + 5) | |
37 | #define SMB_HST_DAT1 (ali1563_smba + 6) | |
38 | #define SMB_BLK_DAT (ali1563_smba + 7) | |
39 | ||
40 | #define HST_STS_BUSY 0x01 | |
41 | #define HST_STS_INTR 0x02 | |
42 | #define HST_STS_DEVERR 0x04 | |
43 | #define HST_STS_BUSERR 0x08 | |
44 | #define HST_STS_FAIL 0x10 | |
45 | #define HST_STS_DONE 0x80 | |
46 | #define HST_STS_BAD 0x1c | |
47 | ||
48 | ||
49 | #define HST_CNTL1_TIMEOUT 0x80 | |
50 | #define HST_CNTL1_LAST 0x40 | |
51 | ||
52 | #define HST_CNTL2_KILL 0x04 | |
53 | #define HST_CNTL2_START 0x40 | |
54 | #define HST_CNTL2_QUICK 0x00 | |
55 | #define HST_CNTL2_BYTE 0x01 | |
56 | #define HST_CNTL2_BYTE_DATA 0x02 | |
57 | #define HST_CNTL2_WORD_DATA 0x03 | |
58 | #define HST_CNTL2_BLOCK 0x05 | |
59 | ||
60 | ||
4a4e5787 | 61 | #define HST_CNTL2_SIZEMASK 0x38 |
1da177e4 | 62 | |
d6072f84 | 63 | static struct pci_driver ali1563_pci_driver; |
1da177e4 LT |
64 | static unsigned short ali1563_smba; |
65 | ||
4a4e5787 | 66 | static int ali1563_transaction(struct i2c_adapter * a, int size) |
1da177e4 LT |
67 | { |
68 | u32 data; | |
69 | int timeout; | |
70 | ||
71 | dev_dbg(&a->dev, "Transaction (pre): STS=%02x, CNTL1=%02x, " | |
72 | "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n", | |
73 | inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2), | |
74 | inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0), | |
75 | inb_p(SMB_HST_DAT1)); | |
76 | ||
77 | data = inb_p(SMB_HST_STS); | |
78 | if (data & HST_STS_BAD) { | |
4a4e5787 | 79 | dev_err(&a->dev, "ali1563: Trying to reset busy device\n"); |
1da177e4 LT |
80 | outb_p(data | HST_STS_BAD,SMB_HST_STS); |
81 | data = inb_p(SMB_HST_STS); | |
82 | if (data & HST_STS_BAD) | |
83 | return -EBUSY; | |
84 | } | |
85 | outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2); | |
86 | ||
87 | timeout = ALI1563_MAX_TIMEOUT; | |
88 | do | |
89 | msleep(1); | |
90 | while (((data = inb_p(SMB_HST_STS)) & HST_STS_BUSY) && --timeout); | |
91 | ||
92 | dev_dbg(&a->dev, "Transaction (post): STS=%02x, CNTL1=%02x, " | |
93 | "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n", | |
94 | inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2), | |
95 | inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0), | |
96 | inb_p(SMB_HST_DAT1)); | |
97 | ||
98 | if (timeout && !(data & HST_STS_BAD)) | |
99 | return 0; | |
1da177e4 | 100 | |
4a4e5787 RM |
101 | if (!timeout) { |
102 | dev_err(&a->dev, "Timeout - Trying to KILL transaction!\n"); | |
1da177e4 LT |
103 | /* Issue 'kill' to host controller */ |
104 | outb_p(HST_CNTL2_KILL,SMB_HST_CNTL2); | |
4a4e5787 RM |
105 | data = inb_p(SMB_HST_STS); |
106 | } | |
107 | ||
108 | /* device error - no response, ignore the autodetection case */ | |
109 | if ((data & HST_STS_DEVERR) && (size != HST_CNTL2_QUICK)) { | |
110 | dev_err(&a->dev, "Device error!\n"); | |
111 | } | |
112 | ||
113 | /* bus collision */ | |
114 | if (data & HST_STS_BUSERR) { | |
115 | dev_err(&a->dev, "Bus collision!\n"); | |
116 | /* Issue timeout, hoping it helps */ | |
1da177e4 | 117 | outb_p(HST_CNTL1_TIMEOUT,SMB_HST_CNTL1); |
4a4e5787 RM |
118 | } |
119 | ||
120 | if (data & HST_STS_FAIL) { | |
121 | dev_err(&a->dev, "Cleaning fail after KILL!\n"); | |
122 | outb_p(0x0,SMB_HST_CNTL2); | |
123 | } | |
124 | ||
1da177e4 LT |
125 | return -1; |
126 | } | |
127 | ||
128 | static int ali1563_block_start(struct i2c_adapter * a) | |
129 | { | |
130 | u32 data; | |
131 | int timeout; | |
132 | ||
133 | dev_dbg(&a->dev, "Block (pre): STS=%02x, CNTL1=%02x, " | |
134 | "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n", | |
135 | inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2), | |
136 | inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0), | |
137 | inb_p(SMB_HST_DAT1)); | |
138 | ||
139 | data = inb_p(SMB_HST_STS); | |
140 | if (data & HST_STS_BAD) { | |
141 | dev_warn(&a->dev,"ali1563: Trying to reset busy device\n"); | |
142 | outb_p(data | HST_STS_BAD,SMB_HST_STS); | |
143 | data = inb_p(SMB_HST_STS); | |
144 | if (data & HST_STS_BAD) | |
145 | return -EBUSY; | |
146 | } | |
147 | ||
148 | /* Clear byte-ready bit */ | |
149 | outb_p(data | HST_STS_DONE, SMB_HST_STS); | |
150 | ||
151 | /* Start transaction and wait for byte-ready bit to be set */ | |
152 | outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2); | |
153 | ||
154 | timeout = ALI1563_MAX_TIMEOUT; | |
155 | do | |
156 | msleep(1); | |
157 | while (!((data = inb_p(SMB_HST_STS)) & HST_STS_DONE) && --timeout); | |
158 | ||
159 | dev_dbg(&a->dev, "Block (post): STS=%02x, CNTL1=%02x, " | |
160 | "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n", | |
161 | inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2), | |
162 | inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0), | |
163 | inb_p(SMB_HST_DAT1)); | |
164 | ||
165 | if (timeout && !(data & HST_STS_BAD)) | |
166 | return 0; | |
4a4e5787 | 167 | dev_err(&a->dev, "SMBus Error: %s%s%s%s%s\n", |
1da177e4 LT |
168 | timeout ? "Timeout " : "", |
169 | data & HST_STS_FAIL ? "Transaction Failed " : "", | |
170 | data & HST_STS_BUSERR ? "No response or Bus Collision " : "", | |
171 | data & HST_STS_DEVERR ? "Device Error " : "", | |
172 | !(data & HST_STS_DONE) ? "Transaction Never Finished " : ""); | |
173 | return -1; | |
174 | } | |
175 | ||
176 | static int ali1563_block(struct i2c_adapter * a, union i2c_smbus_data * data, u8 rw) | |
177 | { | |
178 | int i, len; | |
179 | int error = 0; | |
180 | ||
181 | /* Do we need this? */ | |
182 | outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1); | |
183 | ||
184 | if (rw == I2C_SMBUS_WRITE) { | |
185 | len = data->block[0]; | |
186 | if (len < 1) | |
187 | len = 1; | |
188 | else if (len > 32) | |
189 | len = 32; | |
190 | outb_p(len,SMB_HST_DAT0); | |
191 | outb_p(data->block[1],SMB_BLK_DAT); | |
192 | } else | |
193 | len = 32; | |
194 | ||
195 | outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_BLOCK, SMB_HST_CNTL2); | |
196 | ||
197 | for (i = 0; i < len; i++) { | |
198 | if (rw == I2C_SMBUS_WRITE) { | |
199 | outb_p(data->block[i + 1], SMB_BLK_DAT); | |
200 | if ((error = ali1563_block_start(a))) | |
201 | break; | |
202 | } else { | |
203 | if ((error = ali1563_block_start(a))) | |
204 | break; | |
205 | if (i == 0) { | |
206 | len = inb_p(SMB_HST_DAT0); | |
207 | if (len < 1) | |
208 | len = 1; | |
209 | else if (len > 32) | |
210 | len = 32; | |
211 | } | |
212 | data->block[i+1] = inb_p(SMB_BLK_DAT); | |
213 | } | |
214 | } | |
215 | /* Do we need this? */ | |
216 | outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1); | |
217 | return error; | |
218 | } | |
219 | ||
220 | static s32 ali1563_access(struct i2c_adapter * a, u16 addr, | |
221 | unsigned short flags, char rw, u8 cmd, | |
222 | int size, union i2c_smbus_data * data) | |
223 | { | |
224 | int error = 0; | |
225 | int timeout; | |
226 | u32 reg; | |
227 | ||
228 | for (timeout = ALI1563_MAX_TIMEOUT; timeout; timeout--) { | |
229 | if (!(reg = inb_p(SMB_HST_STS) & HST_STS_BUSY)) | |
230 | break; | |
231 | } | |
232 | if (!timeout) | |
233 | dev_warn(&a->dev,"SMBus not idle. HST_STS = %02x\n",reg); | |
234 | outb_p(0xff,SMB_HST_STS); | |
235 | ||
236 | /* Map the size to what the chip understands */ | |
237 | switch (size) { | |
238 | case I2C_SMBUS_PROC_CALL: | |
239 | dev_err(&a->dev, "I2C_SMBUS_PROC_CALL not supported!\n"); | |
240 | error = -EINVAL; | |
241 | break; | |
242 | case I2C_SMBUS_QUICK: | |
243 | size = HST_CNTL2_QUICK; | |
244 | break; | |
245 | case I2C_SMBUS_BYTE: | |
246 | size = HST_CNTL2_BYTE; | |
247 | break; | |
248 | case I2C_SMBUS_BYTE_DATA: | |
249 | size = HST_CNTL2_BYTE_DATA; | |
250 | break; | |
251 | case I2C_SMBUS_WORD_DATA: | |
252 | size = HST_CNTL2_WORD_DATA; | |
253 | break; | |
254 | case I2C_SMBUS_BLOCK_DATA: | |
255 | size = HST_CNTL2_BLOCK; | |
256 | break; | |
257 | } | |
258 | ||
259 | outb_p(((addr & 0x7f) << 1) | (rw & 0x01), SMB_HST_ADD); | |
4a4e5787 | 260 | outb_p((inb_p(SMB_HST_CNTL2) & ~HST_CNTL2_SIZEMASK) | (size << 3), SMB_HST_CNTL2); |
1da177e4 LT |
261 | |
262 | /* Write the command register */ | |
4a4e5787 | 263 | |
1da177e4 LT |
264 | switch(size) { |
265 | case HST_CNTL2_BYTE: | |
266 | if (rw== I2C_SMBUS_WRITE) | |
4a4e5787 RM |
267 | /* Beware it uses DAT0 register and not CMD! */ |
268 | outb_p(cmd, SMB_HST_DAT0); | |
1da177e4 LT |
269 | break; |
270 | case HST_CNTL2_BYTE_DATA: | |
271 | outb_p(cmd, SMB_HST_CMD); | |
272 | if (rw == I2C_SMBUS_WRITE) | |
273 | outb_p(data->byte, SMB_HST_DAT0); | |
274 | break; | |
275 | case HST_CNTL2_WORD_DATA: | |
276 | outb_p(cmd, SMB_HST_CMD); | |
277 | if (rw == I2C_SMBUS_WRITE) { | |
278 | outb_p(data->word & 0xff, SMB_HST_DAT0); | |
279 | outb_p((data->word & 0xff00) >> 8, SMB_HST_DAT1); | |
280 | } | |
281 | break; | |
282 | case HST_CNTL2_BLOCK: | |
283 | outb_p(cmd, SMB_HST_CMD); | |
284 | error = ali1563_block(a,data,rw); | |
285 | goto Done; | |
286 | } | |
287 | ||
4a4e5787 | 288 | if ((error = ali1563_transaction(a, size))) |
1da177e4 LT |
289 | goto Done; |
290 | ||
291 | if ((rw == I2C_SMBUS_WRITE) || (size == HST_CNTL2_QUICK)) | |
292 | goto Done; | |
293 | ||
294 | switch (size) { | |
295 | case HST_CNTL2_BYTE: /* Result put in SMBHSTDAT0 */ | |
296 | data->byte = inb_p(SMB_HST_DAT0); | |
297 | break; | |
298 | case HST_CNTL2_BYTE_DATA: | |
299 | data->byte = inb_p(SMB_HST_DAT0); | |
300 | break; | |
301 | case HST_CNTL2_WORD_DATA: | |
302 | data->word = inb_p(SMB_HST_DAT0) + (inb_p(SMB_HST_DAT1) << 8); | |
303 | break; | |
304 | } | |
305 | Done: | |
306 | return error; | |
307 | } | |
308 | ||
309 | static u32 ali1563_func(struct i2c_adapter * a) | |
310 | { | |
311 | return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | | |
312 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | | |
313 | I2C_FUNC_SMBUS_BLOCK_DATA; | |
314 | } | |
315 | ||
316 | ||
1da177e4 LT |
317 | static int __devinit ali1563_setup(struct pci_dev * dev) |
318 | { | |
319 | u16 ctrl; | |
320 | ||
321 | pci_read_config_word(dev,ALI1563_SMBBA,&ctrl); | |
1da177e4 | 322 | |
1da177e4 LT |
323 | /* SMB I/O Base in high 12 bits and must be aligned with the |
324 | * size of the I/O space. */ | |
325 | ali1563_smba = ctrl & ~(ALI1563_SMB_IOSIZE - 1); | |
326 | if (!ali1563_smba) { | |
327 | dev_warn(&dev->dev,"ali1563_smba Uninitialized\n"); | |
328 | goto Err; | |
329 | } | |
849be516 JD |
330 | |
331 | /* Check if device is enabled */ | |
332 | if (!(ctrl & ALI1563_SMB_HOSTEN)) { | |
333 | dev_warn(&dev->dev, "Host Controller not enabled\n"); | |
334 | goto Err; | |
335 | } | |
336 | if (!(ctrl & ALI1563_SMB_IOEN)) { | |
337 | dev_warn(&dev->dev, "I/O space not enabled, trying manually\n"); | |
338 | pci_write_config_word(dev, ALI1563_SMBBA, | |
339 | ctrl | ALI1563_SMB_IOEN); | |
340 | pci_read_config_word(dev, ALI1563_SMBBA, &ctrl); | |
341 | if (!(ctrl & ALI1563_SMB_IOEN)) { | |
342 | dev_err(&dev->dev, "I/O space still not enabled, " | |
343 | "giving up\n"); | |
344 | goto Err; | |
345 | } | |
346 | } | |
347 | ||
d6072f84 JD |
348 | if (!request_region(ali1563_smba, ALI1563_SMB_IOSIZE, |
349 | ali1563_pci_driver.name)) { | |
69735698 JD |
350 | dev_err(&dev->dev, "Could not allocate I/O space at 0x%04x\n", |
351 | ali1563_smba); | |
1da177e4 LT |
352 | goto Err; |
353 | } | |
69735698 | 354 | dev_info(&dev->dev, "Found ALi1563 SMBus at 0x%04x\n", ali1563_smba); |
1da177e4 LT |
355 | |
356 | return 0; | |
357 | Err: | |
358 | return -ENODEV; | |
359 | } | |
360 | ||
361 | static void ali1563_shutdown(struct pci_dev *dev) | |
362 | { | |
363 | release_region(ali1563_smba,ALI1563_SMB_IOSIZE); | |
364 | } | |
365 | ||
8f9082c5 | 366 | static const struct i2c_algorithm ali1563_algorithm = { |
1da177e4 LT |
367 | .smbus_xfer = ali1563_access, |
368 | .functionality = ali1563_func, | |
369 | }; | |
370 | ||
371 | static struct i2c_adapter ali1563_adapter = { | |
372 | .owner = THIS_MODULE, | |
9ace555d | 373 | .id = I2C_HW_SMBUS_ALI1563, |
1da177e4 LT |
374 | .class = I2C_CLASS_HWMON, |
375 | .algo = &ali1563_algorithm, | |
376 | }; | |
377 | ||
378 | static int __devinit ali1563_probe(struct pci_dev * dev, | |
379 | const struct pci_device_id * id_table) | |
380 | { | |
381 | int error; | |
382 | ||
383 | if ((error = ali1563_setup(dev))) | |
69735698 | 384 | goto exit; |
1da177e4 LT |
385 | ali1563_adapter.dev.parent = &dev->dev; |
386 | sprintf(ali1563_adapter.name,"SMBus ALi 1563 Adapter @ %04x", | |
387 | ali1563_smba); | |
388 | if ((error = i2c_add_adapter(&ali1563_adapter))) | |
69735698 JD |
389 | goto exit_shutdown; |
390 | return 0; | |
391 | ||
392 | exit_shutdown: | |
393 | ali1563_shutdown(dev); | |
394 | exit: | |
395 | dev_warn(&dev->dev, "ALi1563 SMBus probe failed (%d)\n", error); | |
1da177e4 LT |
396 | return error; |
397 | } | |
398 | ||
399 | static void __devexit ali1563_remove(struct pci_dev * dev) | |
400 | { | |
401 | i2c_del_adapter(&ali1563_adapter); | |
402 | ali1563_shutdown(dev); | |
403 | } | |
404 | ||
405 | static struct pci_device_id __devinitdata ali1563_id_table[] = { | |
406 | { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1563) }, | |
407 | {}, | |
408 | }; | |
409 | ||
410 | MODULE_DEVICE_TABLE (pci, ali1563_id_table); | |
411 | ||
412 | static struct pci_driver ali1563_pci_driver = { | |
d6072f84 | 413 | .name = "ali1563_smbus", |
1da177e4 LT |
414 | .id_table = ali1563_id_table, |
415 | .probe = ali1563_probe, | |
416 | .remove = __devexit_p(ali1563_remove), | |
417 | }; | |
418 | ||
419 | static int __init ali1563_init(void) | |
420 | { | |
421 | return pci_register_driver(&ali1563_pci_driver); | |
422 | } | |
423 | ||
424 | module_init(ali1563_init); | |
425 | ||
426 | static void __exit ali1563_exit(void) | |
427 | { | |
428 | pci_unregister_driver(&ali1563_pci_driver); | |
429 | } | |
430 | ||
431 | module_exit(ali1563_exit); | |
432 | ||
433 | MODULE_LICENSE("GPL"); |