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