2 * i2c-ali1563.c - i2c driver for the ALi 1563 Southbridge
4 * Copyright (C) 2004 Patrick Mochel
5 * 2005 Rudolf Marek <r.marek@assembler.cz>
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
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.
16 * This file is released under the GPLv2
19 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/pci.h>
23 #include <linux/acpi.h>
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
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)
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
49 #define HST_CNTL1_TIMEOUT 0x80
50 #define HST_CNTL1_LAST 0x40
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
61 #define HST_CNTL2_SIZEMASK 0x38
63 static struct pci_driver ali1563_pci_driver
;
64 static unsigned short ali1563_smba
;
66 static int ali1563_transaction(struct i2c_adapter
* a
, int size
)
72 dev_dbg(&a
->dev
, "Transaction (pre): STS=%02x, CNTL1=%02x, "
73 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
74 inb_p(SMB_HST_STS
), inb_p(SMB_HST_CNTL1
), inb_p(SMB_HST_CNTL2
),
75 inb_p(SMB_HST_CMD
), inb_p(SMB_HST_ADD
), inb_p(SMB_HST_DAT0
),
78 data
= inb_p(SMB_HST_STS
);
79 if (data
& HST_STS_BAD
) {
80 dev_err(&a
->dev
, "ali1563: Trying to reset busy device\n");
81 outb_p(data
| HST_STS_BAD
,SMB_HST_STS
);
82 data
= inb_p(SMB_HST_STS
);
83 if (data
& HST_STS_BAD
)
86 outb_p(inb_p(SMB_HST_CNTL2
) | HST_CNTL2_START
, SMB_HST_CNTL2
);
88 timeout
= ALI1563_MAX_TIMEOUT
;
91 } while (((data
= inb_p(SMB_HST_STS
)) & HST_STS_BUSY
) && --timeout
);
93 dev_dbg(&a
->dev
, "Transaction (post): STS=%02x, CNTL1=%02x, "
94 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
95 inb_p(SMB_HST_STS
), inb_p(SMB_HST_CNTL1
), inb_p(SMB_HST_CNTL2
),
96 inb_p(SMB_HST_CMD
), inb_p(SMB_HST_ADD
), inb_p(SMB_HST_DAT0
),
99 if (timeout
&& !(data
& HST_STS_BAD
))
103 dev_err(&a
->dev
, "Timeout - Trying to KILL transaction!\n");
104 /* Issue 'kill' to host controller */
105 outb_p(HST_CNTL2_KILL
,SMB_HST_CNTL2
);
106 data
= inb_p(SMB_HST_STS
);
110 /* device error - no response, ignore the autodetection case */
111 if (data
& HST_STS_DEVERR
) {
112 if (size
!= HST_CNTL2_QUICK
)
113 dev_err(&a
->dev
, "Device error!\n");
117 if (data
& HST_STS_BUSERR
) {
118 dev_err(&a
->dev
, "Bus collision!\n");
119 /* Issue timeout, hoping it helps */
120 outb_p(HST_CNTL1_TIMEOUT
,SMB_HST_CNTL1
);
123 if (data
& HST_STS_FAIL
) {
124 dev_err(&a
->dev
, "Cleaning fail after KILL!\n");
125 outb_p(0x0,SMB_HST_CNTL2
);
131 static int ali1563_block_start(struct i2c_adapter
* a
)
137 dev_dbg(&a
->dev
, "Block (pre): STS=%02x, CNTL1=%02x, "
138 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
139 inb_p(SMB_HST_STS
), inb_p(SMB_HST_CNTL1
), inb_p(SMB_HST_CNTL2
),
140 inb_p(SMB_HST_CMD
), inb_p(SMB_HST_ADD
), inb_p(SMB_HST_DAT0
),
141 inb_p(SMB_HST_DAT1
));
143 data
= inb_p(SMB_HST_STS
);
144 if (data
& HST_STS_BAD
) {
145 dev_warn(&a
->dev
,"ali1563: Trying to reset busy device\n");
146 outb_p(data
| HST_STS_BAD
,SMB_HST_STS
);
147 data
= inb_p(SMB_HST_STS
);
148 if (data
& HST_STS_BAD
)
152 /* Clear byte-ready bit */
153 outb_p(data
| HST_STS_DONE
, SMB_HST_STS
);
155 /* Start transaction and wait for byte-ready bit to be set */
156 outb_p(inb_p(SMB_HST_CNTL2
) | HST_CNTL2_START
, SMB_HST_CNTL2
);
158 timeout
= ALI1563_MAX_TIMEOUT
;
161 } while (!((data
= inb_p(SMB_HST_STS
)) & HST_STS_DONE
) && --timeout
);
163 dev_dbg(&a
->dev
, "Block (post): STS=%02x, CNTL1=%02x, "
164 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
165 inb_p(SMB_HST_STS
), inb_p(SMB_HST_CNTL1
), inb_p(SMB_HST_CNTL2
),
166 inb_p(SMB_HST_CMD
), inb_p(SMB_HST_ADD
), inb_p(SMB_HST_DAT0
),
167 inb_p(SMB_HST_DAT1
));
169 if (timeout
&& !(data
& HST_STS_BAD
))
175 if (data
& HST_STS_DEVERR
)
178 dev_err(&a
->dev
, "SMBus Error: %s%s%s%s%s\n",
179 timeout
? "" : "Timeout ",
180 data
& HST_STS_FAIL
? "Transaction Failed " : "",
181 data
& HST_STS_BUSERR
? "No response or Bus Collision " : "",
182 data
& HST_STS_DEVERR
? "Device Error " : "",
183 !(data
& HST_STS_DONE
) ? "Transaction Never Finished " : "");
187 static int ali1563_block(struct i2c_adapter
* a
, union i2c_smbus_data
* data
, u8 rw
)
192 /* Do we need this? */
193 outb_p(HST_CNTL1_LAST
,SMB_HST_CNTL1
);
195 if (rw
== I2C_SMBUS_WRITE
) {
196 len
= data
->block
[0];
201 outb_p(len
,SMB_HST_DAT0
);
202 outb_p(data
->block
[1],SMB_BLK_DAT
);
206 outb_p(inb_p(SMB_HST_CNTL2
) | HST_CNTL2_BLOCK
, SMB_HST_CNTL2
);
208 for (i
= 0; i
< len
; i
++) {
209 if (rw
== I2C_SMBUS_WRITE
) {
210 outb_p(data
->block
[i
+ 1], SMB_BLK_DAT
);
211 if ((error
= ali1563_block_start(a
)))
214 if ((error
= ali1563_block_start(a
)))
217 len
= inb_p(SMB_HST_DAT0
);
223 data
->block
[i
+1] = inb_p(SMB_BLK_DAT
);
226 /* Do we need this? */
227 outb_p(HST_CNTL1_LAST
,SMB_HST_CNTL1
);
231 static s32
ali1563_access(struct i2c_adapter
* a
, u16 addr
,
232 unsigned short flags
, char rw
, u8 cmd
,
233 int size
, union i2c_smbus_data
* data
)
239 for (timeout
= ALI1563_MAX_TIMEOUT
; timeout
; timeout
--) {
240 if (!(reg
= inb_p(SMB_HST_STS
) & HST_STS_BUSY
))
244 dev_warn(&a
->dev
,"SMBus not idle. HST_STS = %02x\n",reg
);
245 outb_p(0xff,SMB_HST_STS
);
247 /* Map the size to what the chip understands */
249 case I2C_SMBUS_QUICK
:
250 size
= HST_CNTL2_QUICK
;
253 size
= HST_CNTL2_BYTE
;
255 case I2C_SMBUS_BYTE_DATA
:
256 size
= HST_CNTL2_BYTE_DATA
;
258 case I2C_SMBUS_WORD_DATA
:
259 size
= HST_CNTL2_WORD_DATA
;
261 case I2C_SMBUS_BLOCK_DATA
:
262 size
= HST_CNTL2_BLOCK
;
265 dev_warn(&a
->dev
, "Unsupported transaction %d\n", size
);
270 outb_p(((addr
& 0x7f) << 1) | (rw
& 0x01), SMB_HST_ADD
);
271 outb_p((inb_p(SMB_HST_CNTL2
) & ~HST_CNTL2_SIZEMASK
) | (size
<< 3), SMB_HST_CNTL2
);
273 /* Write the command register */
277 if (rw
== I2C_SMBUS_WRITE
)
278 /* Beware it uses DAT0 register and not CMD! */
279 outb_p(cmd
, SMB_HST_DAT0
);
281 case HST_CNTL2_BYTE_DATA
:
282 outb_p(cmd
, SMB_HST_CMD
);
283 if (rw
== I2C_SMBUS_WRITE
)
284 outb_p(data
->byte
, SMB_HST_DAT0
);
286 case HST_CNTL2_WORD_DATA
:
287 outb_p(cmd
, SMB_HST_CMD
);
288 if (rw
== I2C_SMBUS_WRITE
) {
289 outb_p(data
->word
& 0xff, SMB_HST_DAT0
);
290 outb_p((data
->word
& 0xff00) >> 8, SMB_HST_DAT1
);
293 case HST_CNTL2_BLOCK
:
294 outb_p(cmd
, SMB_HST_CMD
);
295 error
= ali1563_block(a
,data
,rw
);
299 if ((error
= ali1563_transaction(a
, size
)))
302 if ((rw
== I2C_SMBUS_WRITE
) || (size
== HST_CNTL2_QUICK
))
306 case HST_CNTL2_BYTE
: /* Result put in SMBHSTDAT0 */
307 data
->byte
= inb_p(SMB_HST_DAT0
);
309 case HST_CNTL2_BYTE_DATA
:
310 data
->byte
= inb_p(SMB_HST_DAT0
);
312 case HST_CNTL2_WORD_DATA
:
313 data
->word
= inb_p(SMB_HST_DAT0
) + (inb_p(SMB_HST_DAT1
) << 8);
320 static u32
ali1563_func(struct i2c_adapter
* a
)
322 return I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
323 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
|
324 I2C_FUNC_SMBUS_BLOCK_DATA
;
328 static int ali1563_setup(struct pci_dev
*dev
)
332 pci_read_config_word(dev
,ALI1563_SMBBA
,&ctrl
);
334 /* SMB I/O Base in high 12 bits and must be aligned with the
335 * size of the I/O space. */
336 ali1563_smba
= ctrl
& ~(ALI1563_SMB_IOSIZE
- 1);
338 dev_warn(&dev
->dev
,"ali1563_smba Uninitialized\n");
342 /* Check if device is enabled */
343 if (!(ctrl
& ALI1563_SMB_HOSTEN
)) {
344 dev_warn(&dev
->dev
, "Host Controller not enabled\n");
347 if (!(ctrl
& ALI1563_SMB_IOEN
)) {
348 dev_warn(&dev
->dev
, "I/O space not enabled, trying manually\n");
349 pci_write_config_word(dev
, ALI1563_SMBBA
,
350 ctrl
| ALI1563_SMB_IOEN
);
351 pci_read_config_word(dev
, ALI1563_SMBBA
, &ctrl
);
352 if (!(ctrl
& ALI1563_SMB_IOEN
)) {
353 dev_err(&dev
->dev
, "I/O space still not enabled, "
359 if (acpi_check_region(ali1563_smba
, ALI1563_SMB_IOSIZE
,
360 ali1563_pci_driver
.name
))
363 if (!request_region(ali1563_smba
, ALI1563_SMB_IOSIZE
,
364 ali1563_pci_driver
.name
)) {
365 dev_err(&dev
->dev
, "Could not allocate I/O space at 0x%04x\n",
369 dev_info(&dev
->dev
, "Found ALi1563 SMBus at 0x%04x\n", ali1563_smba
);
376 static void ali1563_shutdown(struct pci_dev
*dev
)
378 release_region(ali1563_smba
,ALI1563_SMB_IOSIZE
);
381 static const struct i2c_algorithm ali1563_algorithm
= {
382 .smbus_xfer
= ali1563_access
,
383 .functionality
= ali1563_func
,
386 static struct i2c_adapter ali1563_adapter
= {
387 .owner
= THIS_MODULE
,
388 .class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
,
389 .algo
= &ali1563_algorithm
,
392 static int ali1563_probe(struct pci_dev
*dev
,
393 const struct pci_device_id
*id_table
)
397 if ((error
= ali1563_setup(dev
)))
399 ali1563_adapter
.dev
.parent
= &dev
->dev
;
400 snprintf(ali1563_adapter
.name
, sizeof(ali1563_adapter
.name
),
401 "SMBus ALi 1563 Adapter @ %04x", ali1563_smba
);
402 if ((error
= i2c_add_adapter(&ali1563_adapter
)))
407 ali1563_shutdown(dev
);
409 dev_warn(&dev
->dev
, "ALi1563 SMBus probe failed (%d)\n", error
);
413 static void ali1563_remove(struct pci_dev
*dev
)
415 i2c_del_adapter(&ali1563_adapter
);
416 ali1563_shutdown(dev
);
419 static DEFINE_PCI_DEVICE_TABLE(ali1563_id_table
) = {
420 { PCI_DEVICE(PCI_VENDOR_ID_AL
, PCI_DEVICE_ID_AL_M1563
) },
424 MODULE_DEVICE_TABLE (pci
, ali1563_id_table
);
426 static struct pci_driver ali1563_pci_driver
= {
427 .name
= "ali1563_smbus",
428 .id_table
= ali1563_id_table
,
429 .probe
= ali1563_probe
,
430 .remove
= ali1563_remove
,
433 module_pci_driver(ali1563_pci_driver
);
435 MODULE_LICENSE("GPL");