2 * i2c-ali1563.c - i2c driver for the ALi 1563 Southbridge
4 * Copyright (C) 2004 Patrick Mochel
6 * The 1563 southbridge is deceptively similar to the 1533, with a
7 * few notable exceptions. One of those happens to be the fact they
8 * upgraded the i2c core to be 2.0 compliant, and happens to be almost
9 * identical to the i2c controller found in the Intel 801 south
12 * This driver is based on a mix of the 15x3, 1535, and i801 drivers,
13 * with a little help from the ALi 1563 spec.
15 * This file is released under the GPLv2
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/pci.h>
21 #include <linux/init.h>
23 #define ALI1563_MAX_TIMEOUT 500
24 #define ALI1563_SMBBA 0x80
25 #define ALI1563_SMB_IOEN 1
26 #define ALI1563_SMB_HOSTEN 2
27 #define ALI1563_SMB_IOSIZE 16
29 #define SMB_HST_STS (ali1563_smba + 0)
30 #define SMB_HST_CNTL1 (ali1563_smba + 1)
31 #define SMB_HST_CNTL2 (ali1563_smba + 2)
32 #define SMB_HST_CMD (ali1563_smba + 3)
33 #define SMB_HST_ADD (ali1563_smba + 4)
34 #define SMB_HST_DAT0 (ali1563_smba + 5)
35 #define SMB_HST_DAT1 (ali1563_smba + 6)
36 #define SMB_BLK_DAT (ali1563_smba + 7)
38 #define HST_STS_BUSY 0x01
39 #define HST_STS_INTR 0x02
40 #define HST_STS_DEVERR 0x04
41 #define HST_STS_BUSERR 0x08
42 #define HST_STS_FAIL 0x10
43 #define HST_STS_DONE 0x80
44 #define HST_STS_BAD 0x1c
47 #define HST_CNTL1_TIMEOUT 0x80
48 #define HST_CNTL1_LAST 0x40
50 #define HST_CNTL2_KILL 0x04
51 #define HST_CNTL2_START 0x40
52 #define HST_CNTL2_QUICK 0x00
53 #define HST_CNTL2_BYTE 0x01
54 #define HST_CNTL2_BYTE_DATA 0x02
55 #define HST_CNTL2_WORD_DATA 0x03
56 #define HST_CNTL2_BLOCK 0x05
60 static unsigned short ali1563_smba;
62 static int ali1563_transaction(struct i2c_adapter * a)
67 dev_dbg(&a->dev, "Transaction (pre): STS=%02x, CNTL1=%02x, "
68 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
69 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
70 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
73 data = inb_p(SMB_HST_STS);
74 if (data & HST_STS_BAD) {
75 dev_warn(&a->dev,"ali1563: Trying to reset busy device\n");
76 outb_p(data | HST_STS_BAD,SMB_HST_STS);
77 data = inb_p(SMB_HST_STS);
78 if (data & HST_STS_BAD)
81 outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
83 timeout = ALI1563_MAX_TIMEOUT;
86 while (((data = inb_p(SMB_HST_STS)) & HST_STS_BUSY) && --timeout);
88 dev_dbg(&a->dev, "Transaction (post): STS=%02x, CNTL1=%02x, "
89 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
90 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
91 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
94 if (timeout && !(data & HST_STS_BAD))
96 dev_warn(&a->dev, "SMBus Error: %s%s%s%s%s\n",
97 timeout ? "Timeout " : "",
98 data & HST_STS_FAIL ? "Transaction Failed " : "",
99 data & HST_STS_BUSERR ? "No response or Bus Collision " : "",
100 data & HST_STS_DEVERR ? "Device Error " : "",
101 !(data & HST_STS_DONE) ? "Transaction Never Finished " : "");
103 if (!(data & HST_STS_DONE))
104 /* Issue 'kill' to host controller */
105 outb_p(HST_CNTL2_KILL,SMB_HST_CNTL2);
107 /* Issue timeout to reset all devices on bus */
108 outb_p(HST_CNTL1_TIMEOUT,SMB_HST_CNTL1);
112 static int ali1563_block_start(struct i2c_adapter * a)
117 dev_dbg(&a->dev, "Block (pre): STS=%02x, CNTL1=%02x, "
118 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
119 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
120 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
121 inb_p(SMB_HST_DAT1));
123 data = inb_p(SMB_HST_STS);
124 if (data & HST_STS_BAD) {
125 dev_warn(&a->dev,"ali1563: Trying to reset busy device\n");
126 outb_p(data | HST_STS_BAD,SMB_HST_STS);
127 data = inb_p(SMB_HST_STS);
128 if (data & HST_STS_BAD)
132 /* Clear byte-ready bit */
133 outb_p(data | HST_STS_DONE, SMB_HST_STS);
135 /* Start transaction and wait for byte-ready bit to be set */
136 outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
138 timeout = ALI1563_MAX_TIMEOUT;
141 while (!((data = inb_p(SMB_HST_STS)) & HST_STS_DONE) && --timeout);
143 dev_dbg(&a->dev, "Block (post): STS=%02x, CNTL1=%02x, "
144 "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
145 inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
146 inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
147 inb_p(SMB_HST_DAT1));
149 if (timeout && !(data & HST_STS_BAD))
151 dev_warn(&a->dev, "SMBus Error: %s%s%s%s%s\n",
152 timeout ? "Timeout " : "",
153 data & HST_STS_FAIL ? "Transaction Failed " : "",
154 data & HST_STS_BUSERR ? "No response or Bus Collision " : "",
155 data & HST_STS_DEVERR ? "Device Error " : "",
156 !(data & HST_STS_DONE) ? "Transaction Never Finished " : "");
160 static int ali1563_block(struct i2c_adapter * a, union i2c_smbus_data * data, u8 rw)
165 /* Do we need this? */
166 outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1);
168 if (rw == I2C_SMBUS_WRITE) {
169 len = data->block[0];
174 outb_p(len,SMB_HST_DAT0);
175 outb_p(data->block[1],SMB_BLK_DAT);
179 outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_BLOCK, SMB_HST_CNTL2);
181 for (i = 0; i < len; i++) {
182 if (rw == I2C_SMBUS_WRITE) {
183 outb_p(data->block[i + 1], SMB_BLK_DAT);
184 if ((error = ali1563_block_start(a)))
187 if ((error = ali1563_block_start(a)))
190 len = inb_p(SMB_HST_DAT0);
196 data->block[i+1] = inb_p(SMB_BLK_DAT);
199 /* Do we need this? */
200 outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1);
204 static s32 ali1563_access(struct i2c_adapter * a, u16 addr,
205 unsigned short flags, char rw, u8 cmd,
206 int size, union i2c_smbus_data * data)
212 for (timeout = ALI1563_MAX_TIMEOUT; timeout; timeout--) {
213 if (!(reg = inb_p(SMB_HST_STS) & HST_STS_BUSY))
217 dev_warn(&a->dev,"SMBus not idle. HST_STS = %02x\n",reg);
218 outb_p(0xff,SMB_HST_STS);
220 /* Map the size to what the chip understands */
222 case I2C_SMBUS_PROC_CALL:
223 dev_err(&a->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
226 case I2C_SMBUS_QUICK:
227 size = HST_CNTL2_QUICK;
230 size = HST_CNTL2_BYTE;
232 case I2C_SMBUS_BYTE_DATA:
233 size = HST_CNTL2_BYTE_DATA;
235 case I2C_SMBUS_WORD_DATA:
236 size = HST_CNTL2_WORD_DATA;
238 case I2C_SMBUS_BLOCK_DATA:
239 size = HST_CNTL2_BLOCK;
243 outb_p(((addr & 0x7f) << 1) | (rw & 0x01), SMB_HST_ADD);
244 outb_p(inb_p(SMB_HST_CNTL2) | (size << 3), SMB_HST_CNTL2);
246 /* Write the command register */
249 if (rw== I2C_SMBUS_WRITE)
250 outb_p(cmd, SMB_HST_CMD);
252 case HST_CNTL2_BYTE_DATA:
253 outb_p(cmd, SMB_HST_CMD);
254 if (rw == I2C_SMBUS_WRITE)
255 outb_p(data->byte, SMB_HST_DAT0);
257 case HST_CNTL2_WORD_DATA:
258 outb_p(cmd, SMB_HST_CMD);
259 if (rw == I2C_SMBUS_WRITE) {
260 outb_p(data->word & 0xff, SMB_HST_DAT0);
261 outb_p((data->word & 0xff00) >> 8, SMB_HST_DAT1);
264 case HST_CNTL2_BLOCK:
265 outb_p(cmd, SMB_HST_CMD);
266 error = ali1563_block(a,data,rw);
270 if ((error = ali1563_transaction(a)))
273 if ((rw == I2C_SMBUS_WRITE) || (size == HST_CNTL2_QUICK))
277 case HST_CNTL2_BYTE: /* Result put in SMBHSTDAT0 */
278 data->byte = inb_p(SMB_HST_DAT0);
280 case HST_CNTL2_BYTE_DATA:
281 data->byte = inb_p(SMB_HST_DAT0);
283 case HST_CNTL2_WORD_DATA:
284 data->word = inb_p(SMB_HST_DAT0) + (inb_p(SMB_HST_DAT1) << 8);
291 static u32 ali1563_func(struct i2c_adapter * a)
293 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
294 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
295 I2C_FUNC_SMBUS_BLOCK_DATA;
299 static void ali1563_enable(struct pci_dev * dev)
303 pci_read_config_word(dev,ALI1563_SMBBA,&ctrl);
305 pci_write_config_word(dev,ALI1563_SMBBA,ctrl);
308 static int __init ali1563_setup(struct pci_dev * dev)
312 pci_read_config_word(dev,ALI1563_SMBBA,&ctrl);
313 printk("ali1563: SMBus control = %04x\n",ctrl);
315 /* Check if device is even enabled first */
316 if (!(ctrl & ALI1563_SMB_IOEN)) {
317 dev_warn(&dev->dev,"I/O space not enabled, trying manually\n");
320 if (!(ctrl & ALI1563_SMB_IOEN)) {
321 dev_warn(&dev->dev,"I/O space still not enabled, giving up\n");
324 if (!(ctrl & ALI1563_SMB_HOSTEN)) {
325 dev_warn(&dev->dev,"Host Controller not enabled\n");
329 /* SMB I/O Base in high 12 bits and must be aligned with the
330 * size of the I/O space. */
331 ali1563_smba = ctrl & ~(ALI1563_SMB_IOSIZE - 1);
333 dev_warn(&dev->dev,"ali1563_smba Uninitialized\n");
336 if (!request_region(ali1563_smba,ALI1563_SMB_IOSIZE,"i2c-ali1563")) {
337 dev_warn(&dev->dev,"Could not allocate I/O space");
346 static void ali1563_shutdown(struct pci_dev *dev)
348 release_region(ali1563_smba,ALI1563_SMB_IOSIZE);
351 static struct i2c_algorithm ali1563_algorithm = {
352 .name = "Non-i2c SMBus adapter",
353 .id = I2C_ALGO_SMBUS,
354 .smbus_xfer = ali1563_access,
355 .functionality = ali1563_func,
358 static struct i2c_adapter ali1563_adapter = {
359 .owner = THIS_MODULE,
360 .class = I2C_ADAP_CLASS_SMBUS,
361 .algo = &ali1563_algorithm,
364 static int __init ali1563_probe(struct pci_dev * dev,
365 const struct pci_device_id * id_table)
369 if ((error = ali1563_setup(dev)))
371 ali1563_adapter.dev.parent = &dev->dev;
372 sprintf(ali1563_adapter.name,"SMBus ALi 1563 Adapter @ %04x",
374 if ((error = i2c_add_adapter(&ali1563_adapter)))
375 ali1563_shutdown(dev);
376 printk("%s: Returning %d\n",__FUNCTION__,error);
380 static void __exit ali1563_remove(struct pci_dev * dev)
382 i2c_del_adapter(&ali1563_adapter);
383 ali1563_shutdown(dev);
386 static struct pci_device_id __devinitdata ali1563_id_table[] = {
388 .vendor = PCI_VENDOR_ID_AL,
389 .device = PCI_DEVICE_ID_AL_M1563,
390 .subvendor = PCI_ANY_ID,
391 .subdevice = PCI_ANY_ID,
396 static struct pci_driver ali1563_pci_driver = {
397 .name = "i2c-ali1563",
398 .id_table = ali1563_id_table,
399 .probe = ali1563_probe,
400 .remove = ali1563_remove,
403 static int __init ali1563_init(void)
405 return pci_module_init(&ali1563_pci_driver);
408 module_init(ali1563_init);
410 static void __exit ali1563_exit(void)
412 pci_unregister_driver(&ali1563_pci_driver);
415 module_exit(ali1563_exit);
417 MODULE_LICENSE("GPL");