]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/i2c/busses/scx200_acb.c
[PATCH] i2c: scx200_acb fix and speed up the poll loop
[mv-sheeva.git] / drivers / i2c / busses / scx200_acb.c
1 /*
2     Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
3
4     National Semiconductor SCx200 ACCESS.bus support
5     Also supports the AMD CS5535 and AMD CS5536
6
7     Based on i2c-keywest.c which is:
8         Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
9         Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
10
11     This program is free software; you can redistribute it and/or
12     modify it under the terms of the GNU General Public License as
13     published by the Free Software Foundation; either version 2 of the
14     License, or (at your option) any later version.
15
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
20
21     You should have received a copy of the GNU General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include <linux/module.h>
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/i2c.h>
31 #include <linux/smp_lock.h>
32 #include <linux/pci.h>
33 #include <linux/delay.h>
34 #include <asm/io.h>
35 #include <asm/msr.h>
36
37 #include <linux/scx200.h>
38
39 #define NAME "scx200_acb"
40
41 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
42 MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
43 MODULE_LICENSE("GPL");
44
45 #define MAX_DEVICES 4
46 static int base[MAX_DEVICES] = { 0x820, 0x840 };
47 module_param_array(base, int, NULL, 0);
48 MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
49
50 #define POLL_TIMEOUT    (HZ/5)
51
52 enum scx200_acb_state {
53         state_idle,
54         state_address,
55         state_command,
56         state_repeat_start,
57         state_quick,
58         state_read,
59         state_write,
60 };
61
62 static const char *scx200_acb_state_name[] = {
63         "idle",
64         "address",
65         "command",
66         "repeat_start",
67         "quick",
68         "read",
69         "write",
70 };
71
72 /* Physical interface */
73 struct scx200_acb_iface {
74         struct scx200_acb_iface *next;
75         struct i2c_adapter adapter;
76         unsigned base;
77         struct semaphore sem;
78
79         /* State machine data */
80         enum scx200_acb_state state;
81         int result;
82         u8 address_byte;
83         u8 command;
84         u8 *ptr;
85         char needs_reset;
86         unsigned len;
87 };
88
89 /* Register Definitions */
90 #define ACBSDA          (iface->base + 0)
91 #define ACBST           (iface->base + 1)
92 #define    ACBST_SDAST          0x40 /* SDA Status */
93 #define    ACBST_BER            0x20
94 #define    ACBST_NEGACK         0x10 /* Negative Acknowledge */
95 #define    ACBST_STASTR         0x08 /* Stall After Start */
96 #define    ACBST_MASTER         0x02
97 #define ACBCST          (iface->base + 2)
98 #define    ACBCST_BB            0x02
99 #define ACBCTL1         (iface->base + 3)
100 #define    ACBCTL1_STASTRE      0x80
101 #define    ACBCTL1_NMINTE       0x40
102 #define    ACBCTL1_ACK          0x10
103 #define    ACBCTL1_STOP         0x02
104 #define    ACBCTL1_START        0x01
105 #define ACBADDR         (iface->base + 4)
106 #define ACBCTL2         (iface->base + 5)
107 #define    ACBCTL2_ENABLE       0x01
108
109 /************************************************************************/
110
111 static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
112 {
113         const char *errmsg;
114
115         dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
116                 scx200_acb_state_name[iface->state], status);
117
118         if (status & ACBST_BER) {
119                 errmsg = "bus error";
120                 goto error;
121         }
122         if (!(status & ACBST_MASTER)) {
123                 errmsg = "not master";
124                 goto error;
125         }
126         if (status & ACBST_NEGACK) {
127                 dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
128                         scx200_acb_state_name[iface->state]);
129
130                 iface->state = state_idle;
131                 iface->result = -ENXIO;
132
133                 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
134                 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
135                 return;
136         }
137
138         switch (iface->state) {
139         case state_idle:
140                 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
141                 break;
142
143         case state_address:
144                 /* Do a pointer write first */
145                 outb(iface->address_byte & ~1, ACBSDA);
146
147                 iface->state = state_command;
148                 break;
149
150         case state_command:
151                 outb(iface->command, ACBSDA);
152
153                 if (iface->address_byte & 1)
154                         iface->state = state_repeat_start;
155                 else
156                         iface->state = state_write;
157                 break;
158
159         case state_repeat_start:
160                 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
161                 /* fallthrough */
162
163         case state_quick:
164                 if (iface->address_byte & 1) {
165                         if (iface->len == 1)
166                                 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
167                         else
168                                 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
169                         outb(iface->address_byte, ACBSDA);
170
171                         iface->state = state_read;
172                 } else {
173                         outb(iface->address_byte, ACBSDA);
174
175                         iface->state = state_write;
176                 }
177                 break;
178
179         case state_read:
180                 /* Set ACK if receiving the last byte */
181                 if (iface->len == 1)
182                         outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
183                 else
184                         outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
185
186                 *iface->ptr++ = inb(ACBSDA);
187                 --iface->len;
188
189                 if (iface->len == 0) {
190                         iface->result = 0;
191                         iface->state = state_idle;
192                         outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
193                 }
194
195                 break;
196
197         case state_write:
198                 if (iface->len == 0) {
199                         iface->result = 0;
200                         iface->state = state_idle;
201                         outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
202                         break;
203                 }
204
205                 outb(*iface->ptr++, ACBSDA);
206                 --iface->len;
207
208                 break;
209         }
210
211         return;
212
213  error:
214         dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg,
215                 scx200_acb_state_name[iface->state]);
216
217         iface->state = state_idle;
218         iface->result = -EIO;
219         iface->needs_reset = 1;
220 }
221
222 static void scx200_acb_poll(struct scx200_acb_iface *iface)
223 {
224         u8 status;
225         unsigned long timeout;
226
227         timeout = jiffies + POLL_TIMEOUT;
228         while (time_before(jiffies, timeout)) {
229                 status = inb(ACBST);
230                 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
231                         scx200_acb_machine(iface, status);
232                         return;
233                 }
234                 yield();
235         }
236
237         dev_err(&iface->adapter.dev, "timeout in state %s\n",
238                 scx200_acb_state_name[iface->state]);
239
240         iface->state = state_idle;
241         iface->result = -EIO;
242         iface->needs_reset = 1;
243 }
244
245 static void scx200_acb_reset(struct scx200_acb_iface *iface)
246 {
247         /* Disable the ACCESS.bus device and Configure the SCL
248            frequency: 16 clock cycles */
249         outb(0x70, ACBCTL2);
250         /* Polling mode */
251         outb(0, ACBCTL1);
252         /* Disable slave address */
253         outb(0, ACBADDR);
254         /* Enable the ACCESS.bus device */
255         outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
256         /* Free STALL after START */
257         outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
258         /* Send a STOP */
259         outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
260         /* Clear BER, NEGACK and STASTR bits */
261         outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
262         /* Clear BB bit */
263         outb(inb(ACBCST) | ACBCST_BB, ACBCST);
264 }
265
266 static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
267                                  u16 address, unsigned short flags,
268                                  char rw, u8 command, int size,
269                                  union i2c_smbus_data *data)
270 {
271         struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
272         int len;
273         u8 *buffer;
274         u16 cur_word;
275         int rc;
276
277         switch (size) {
278         case I2C_SMBUS_QUICK:
279                 len = 0;
280                 buffer = NULL;
281                 break;
282
283         case I2C_SMBUS_BYTE:
284                 len = 1;
285                 buffer = rw ? &data->byte : &command;
286                 break;
287
288         case I2C_SMBUS_BYTE_DATA:
289                 len = 1;
290                 buffer = &data->byte;
291                 break;
292
293         case I2C_SMBUS_WORD_DATA:
294                 len = 2;
295                 cur_word = cpu_to_le16(data->word);
296                 buffer = (u8 *)&cur_word;
297                 break;
298
299         case I2C_SMBUS_BLOCK_DATA:
300                 len = data->block[0];
301                 buffer = &data->block[1];
302                 break;
303
304         default:
305                 return -EINVAL;
306         }
307
308         dev_dbg(&adapter->dev,
309                 "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
310                 size, address, command, len, rw);
311
312         if (!len && rw == I2C_SMBUS_READ) {
313                 dev_dbg(&adapter->dev, "zero length read\n");
314                 return -EINVAL;
315         }
316
317         down(&iface->sem);
318
319         iface->address_byte = (address << 1) | rw;
320         iface->command = command;
321         iface->ptr = buffer;
322         iface->len = len;
323         iface->result = -EINVAL;
324         iface->needs_reset = 0;
325
326         outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
327
328         if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
329                 iface->state = state_quick;
330         else
331                 iface->state = state_address;
332
333         while (iface->state != state_idle)
334                 scx200_acb_poll(iface);
335
336         if (iface->needs_reset)
337                 scx200_acb_reset(iface);
338
339         rc = iface->result;
340
341         up(&iface->sem);
342
343         if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
344                 data->word = le16_to_cpu(cur_word);
345
346 #ifdef DEBUG
347         dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
348         if (buffer) {
349                 int i;
350                 printk(" data:");
351                 for (i = 0; i < len; ++i)
352                         printk(" %02x", buffer[i]);
353         }
354         printk("\n");
355 #endif
356
357         return rc;
358 }
359
360 static u32 scx200_acb_func(struct i2c_adapter *adapter)
361 {
362         return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
363                I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
364                I2C_FUNC_SMBUS_BLOCK_DATA;
365 }
366
367 /* For now, we only handle combined mode (smbus) */
368 static struct i2c_algorithm scx200_acb_algorithm = {
369         .smbus_xfer     = scx200_acb_smbus_xfer,
370         .functionality  = scx200_acb_func,
371 };
372
373 static struct scx200_acb_iface *scx200_acb_list;
374 static DECLARE_MUTEX(scx200_acb_list_mutex);
375
376 static int scx200_acb_probe(struct scx200_acb_iface *iface)
377 {
378         u8 val;
379
380         /* Disable the ACCESS.bus device and Configure the SCL
381            frequency: 16 clock cycles */
382         outb(0x70, ACBCTL2);
383
384         if (inb(ACBCTL2) != 0x70) {
385                 pr_debug(NAME ": ACBCTL2 readback failed\n");
386                 return -ENXIO;
387         }
388
389         outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
390
391         val = inb(ACBCTL1);
392         if (val) {
393                 pr_debug(NAME ": disabled, but ACBCTL1=0x%02x\n",
394                         val);
395                 return -ENXIO;
396         }
397
398         outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
399
400         outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
401
402         val = inb(ACBCTL1);
403         if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
404                 pr_debug(NAME ": enabled, but NMINTE won't be set, "
405                          "ACBCTL1=0x%02x\n", val);
406                 return -ENXIO;
407         }
408
409         return 0;
410 }
411
412 static int  __init scx200_acb_create(const char *text, int base, int index)
413 {
414         struct scx200_acb_iface *iface;
415         struct i2c_adapter *adapter;
416         int rc;
417         char description[64];
418
419         iface = kzalloc(sizeof(*iface), GFP_KERNEL);
420         if (!iface) {
421                 printk(KERN_ERR NAME ": can't allocate memory\n");
422                 rc = -ENOMEM;
423                 goto errout;
424         }
425
426         adapter = &iface->adapter;
427         i2c_set_adapdata(adapter, iface);
428         snprintf(adapter->name, I2C_NAME_SIZE, "%s ACB%d", text, index);
429         adapter->owner = THIS_MODULE;
430         adapter->id = I2C_HW_SMBUS_SCX200;
431         adapter->algo = &scx200_acb_algorithm;
432         adapter->class = I2C_CLASS_HWMON;
433
434         init_MUTEX(&iface->sem);
435
436         snprintf(description, sizeof(description), "%s ACCESS.bus [%s]",
437                  text, adapter->name);
438
439         if (request_region(base, 8, description) == 0) {
440                 printk(KERN_ERR NAME ": can't allocate io 0x%x-0x%x\n",
441                         base, base + 8-1);
442                 rc = -EBUSY;
443                 goto errout_free;
444         }
445         iface->base = base;
446
447         rc = scx200_acb_probe(iface);
448         if (rc) {
449                 printk(KERN_WARNING NAME ": probe failed\n");
450                 goto errout_release;
451         }
452
453         scx200_acb_reset(iface);
454
455         if (i2c_add_adapter(adapter) < 0) {
456                 printk(KERN_ERR NAME ": failed to register\n");
457                 rc = -ENODEV;
458                 goto errout_release;
459         }
460
461         down(&scx200_acb_list_mutex);
462         iface->next = scx200_acb_list;
463         scx200_acb_list = iface;
464         up(&scx200_acb_list_mutex);
465
466         return 0;
467
468  errout_release:
469         release_region(iface->base, 8);
470  errout_free:
471         kfree(iface);
472  errout:
473         return rc;
474 }
475
476 static struct pci_device_id scx200[] = {
477         { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
478         { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
479         { },
480 };
481
482 static struct pci_device_id divil_pci[] = {
483         { PCI_DEVICE(PCI_VENDOR_ID_NS,  PCI_DEVICE_ID_NS_CS5535_ISA) },
484         { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
485         { } /* NULL entry */
486 };
487
488 #define MSR_LBAR_SMB            0x5140000B
489
490 static int scx200_add_cs553x(void)
491 {
492         u32     low, hi;
493         u32     smb_base;
494
495         /* Grab & reserve the SMB I/O range */
496         rdmsr(MSR_LBAR_SMB, low, hi);
497
498         /* Check the IO mask and whether SMB is enabled */
499         if (hi != 0x0000F001) {
500                 printk(KERN_WARNING NAME ": SMBus not enabled\n");
501                 return -ENODEV;
502         }
503
504         /* SMBus IO size is 8 bytes */
505         smb_base = low & 0x0000FFF8;
506
507         return scx200_acb_create("CS5535", smb_base, 0);
508 }
509
510 static int __init scx200_acb_init(void)
511 {
512         int i;
513         int     rc = -ENODEV;
514
515         pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
516
517         /* Verify that this really is a SCx200 processor */
518         if (pci_dev_present(scx200)) {
519                 for (i = 0; i < MAX_DEVICES; ++i) {
520                         if (base[i] > 0)
521                                 rc = scx200_acb_create("SCx200", base[i], i);
522                 }
523         } else if (pci_dev_present(divil_pci))
524                 rc = scx200_add_cs553x();
525
526         return rc;
527 }
528
529 static void __exit scx200_acb_cleanup(void)
530 {
531         struct scx200_acb_iface *iface;
532
533         down(&scx200_acb_list_mutex);
534         while ((iface = scx200_acb_list) != NULL) {
535                 scx200_acb_list = iface->next;
536                 up(&scx200_acb_list_mutex);
537
538                 i2c_del_adapter(&iface->adapter);
539                 release_region(iface->base, 8);
540                 kfree(iface);
541                 down(&scx200_acb_list_mutex);
542         }
543         up(&scx200_acb_list_mutex);
544 }
545
546 module_init(scx200_acb_init);
547 module_exit(scx200_acb_cleanup);