import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / i2c / xilinx_iic / i2c-algo-xilinx.c
1 /*
2  * i2c-algo-xilinx.c
3  *
4  * Xilinx IIC Adapter component to interface IIC component to Linux
5  *
6  * Author: MontaVista Software, Inc.
7  *         source@mvista.com
8  *
9  * 2002 (c) MontaVista, Software, Inc.  This file is licensed under the terms
10  * of the GNU General Public License version 2.1.  This program is licensed
11  * "as is" without any warranty of any kind, whether express or implied.
12  */
13
14 /*
15  * I2C drivers are split into two pieces: the adapter and the algorithm.
16  * The adapter is responsible for actually manipulating the hardware and
17  * the algorithm is the layer above that that handles the higher level
18  * tasks such as transmitting or receiving a buffer.  The best example
19  * (in my opinion) of this is the bit banging algorithm has a number of
20  * different adapters that can plug in under it to actually wiggle the
21  * SDA and SCL.
22  *
23  * The interesting part is that the drivers Xilinx provides with their
24  * IP are also split into two pieces where one part is the OS
25  * independent code and the other part is the OS dependent code.  All of
26  * the other sources in this directory are the OS independent files as
27  * provided by Xilinx with no changes made to them.
28  *
29  * As it turns out, this maps quite well into the I2C driver philosophy.
30  * This file is the I2C algorithm that communicates with the Xilinx OS
31  * independent function that will serve as our I2C adapter.  The
32  * unfortunate part is that the term "adapter" is overloaded in our
33  * context.  Xilinx refers to the OS dependent part of a driver as an
34  * adapter.  So from an I2C driver perspective, this file is not an
35  * adapter; that role is filled by the Xilinx OS independent files.
36  * From a Xilinx perspective, this file is an adapter; it adapts their
37  * OS independent code to Linux.
38  *
39  * Another thing to consider is that the Xilinx OS dependent code knows
40  * nothing about Linux I2C adapters, so even though this file is billed
41  * as the I2C algorithm, it takes care of the i2c_adapter structure.
42  *
43  * Fortunately, naming conventions will give you a clue as to what comes
44  * from where.  Functions beginning with XIic_ are provided by the
45  * Xilinx OS independent files.  Functions beginning with i2c_ are
46  * provided by the I2C Linux core.  All functions in this file that are
47  * called by Linux have names that begin with xiic_.  The functions in
48  * this file that have Handler in their name are registered as callbacks
49  * with the underlying Xilinx OS independent layer.  Any other functions
50  * are static helper functions.
51  */
52
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <asm/io.h>
57 #include <asm/irq.h>
58
59 #include <linux/i2c.h>
60
61 #include <xbasic_types.h>
62 #include "xiic.h"
63 #include "xiic_i.h"
64
65 MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
66 MODULE_DESCRIPTION("Xilinx IIC driver");
67 MODULE_LICENSE("GPL");
68 MODULE_PARM(scan, "i");
69 MODULE_PARM_DESC(scan, "Scan for active chips on the bus");
70 static int scan = 0;            /* have a look at what's hanging 'round */
71
72 /* SAATODO: actually use these? */
73 #define XIIC_TIMEOUT            100
74 #define XIIC_RETRY              3
75
76 /* Our private per device data. */
77 struct xiic_dev {
78         struct i2c_adapter adap;        /* The Linux I2C core data  */
79         struct xiic_dev *next_dev;      /* The next device in dev_list */
80         struct completion complete;     /* for waiting for interrupts */
81         int index;              /* Which interface is this */
82         u32 save_BaseAddress;   /* Saved physical base address */
83         unsigned int irq;       /* device IRQ number    */
84         /*
85          * The underlying OS independent code needs space as well.  A
86          * pointer to the following XIic structure will be passed to
87          * any XIic_ function that requires it.  However, we treat the
88          * data as an opaque object in this file (meaning that we never
89          * reference any of the fields inside of the structure).
90          */
91         XIic Iic;
92         XStatus interrupt_status;
93         /*
94          * The following bit fields are used to keep track of what
95          * all has been done to initialize the xiic_dev to make
96          * error handling out of probe() easier.
97          */
98         unsigned int reqirq:1;  /* Has request_irq() been called? */
99         unsigned int remapped:1;        /* Has ioremap() been called? */
100         unsigned int started:1; /* Has XIic_Start() been called? */
101         unsigned int added:1;   /* Has i2c_add_adapter() been called? */
102 };
103
104 /* List of devices we're handling and a lock to give us atomic access. */
105 static struct xiic_dev *dev_list = NULL;
106 static spinlock_t dev_lock = SPIN_LOCK_UNLOCKED;
107
108 /* SAATODO: This function will be moved into the Xilinx code. */
109 /*****************************************************************************/
110 /**
111 *
112 * Lookup the device configuration based on the iic instance.  The table
113 * XIic_ConfigTable contains the configuration info for each device in the system.
114 *
115 * @param Instance is the index of the interface being looked up.
116 *
117 * @return
118 *
119 * A pointer to the configuration table entry corresponding to the given
120 * device ID, or NULL if no match is found.
121 *
122 * @note
123 *
124 * None.
125 *
126 ******************************************************************************/
127 XIic_Config *XIic_GetConfig(int Instance)
128 {
129         if (Instance < 0 || Instance >= XPAR_XIIC_NUM_INSTANCES)
130         {
131                 return NULL;
132         }
133
134         return &XIic_ConfigTable[Instance];
135 }
136
137 static int
138 xiic_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
139 {
140         struct xiic_dev *dev = (struct xiic_dev *) i2c_adap;
141         struct i2c_msg *pmsg;
142         u32 options;
143         int i, retries;
144         XStatus Status;
145
146         for (i = 0; i < num; i++) {
147                 pmsg = &msgs[i];
148
149                 if (!pmsg->len) /* If length is zero */
150                      continue;  /* on to the next request. */
151
152                 options = 0;
153                 if (pmsg->flags & I2C_M_TEN)
154                         options |= XII_SEND_10_BIT_OPTION;
155                 if (i != num-1)
156                         options |= XII_REPEATED_START_OPTION;
157                 XIic_SetOptions(&dev->Iic, options);
158
159                 if (XIic_SetAddress(&dev->Iic, XII_ADDR_TO_SEND_TYPE,
160                                     pmsg->addr) != XST_SUCCESS) {
161                         printk(KERN_WARNING
162                                "%s #%d: Could not set address to 0x%2x.\n",
163                                dev->adap.name, dev->index, pmsg->addr);
164                         return -EIO;
165                 }
166                 dev->interrupt_status = ~(XStatus) 0;
167                 /*
168                  * The Xilinx layer does not handle bus busy conditions yet
169                  * so this code retries a request up to 16 times if it
170                  * receives a bus busy condition.  If and when the underlying
171                  * code is enhanced, the retry code can be removed.
172                  */
173                 retries = 16;
174                 if (pmsg->flags & I2C_M_RD) {
175                         while ((Status = XIic_MasterRecv(&dev->Iic,
176                                                          pmsg->buf, pmsg->len))
177                                == XST_IIC_BUS_BUSY && retries--) {
178                                 set_current_state(TASK_INTERRUPTIBLE);
179                                 schedule_timeout(HZ/10);
180                         }
181                 } else {
182                         while ((Status = XIic_MasterSend(&dev->Iic,
183                                                          pmsg->buf, pmsg->len))
184                                == XST_IIC_BUS_BUSY && retries--) {
185                                 set_current_state(TASK_INTERRUPTIBLE);
186                                 schedule_timeout(HZ/10);
187                         }
188                 }
189                 if (Status != XST_SUCCESS) {
190                         printk(KERN_WARNING
191                                "%s #%d: Unexpected error %d.\n",
192                                dev->adap.name, dev->index, Status);
193                         return -EIO;
194                 }
195                 wait_for_completion(&dev->complete);
196                 if (dev->interrupt_status != XST_SUCCESS) {
197                         printk(KERN_WARNING
198                                "%s #%d: Could not talk to device 0x%2x (%d).\n",
199                                dev->adap.name, dev->index, pmsg->addr,
200                                dev->interrupt_status);
201                         return -EIO;
202                 }
203         }
204         return num;
205 }
206
207 static int
208 xiic_algo_control(struct i2c_adapter *adapter,
209                   unsigned int cmd, unsigned long arg)
210 {
211         return 0;
212 }
213
214 static u32
215 xiic_bit_func(struct i2c_adapter *adap)
216 {
217         return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
218             I2C_FUNC_PROTOCOL_MANGLING;
219 }
220
221 static struct i2c_algorithm xiic_algo = {
222         "Xilinx IIC algorithm", /* name                 */
223         /*
224          * SAATODO: Get a real ID (perhaps I2C_ALGO_XILINX) after
225          * initial release.  Will need to email lm78@stimpy.netroedge.com
226          * per http://www2.lm-sensors.nu/~lm78/support.html
227          */
228         I2C_ALGO_EXP,           /* id                   */
229         xiic_xfer,              /* master_xfer          */
230         NULL,                   /* smbus_xfer           */
231         NULL,                   /* slave_send           */
232         NULL,                   /* slave_recv           */
233         xiic_algo_control,      /* algo_control         */
234         xiic_bit_func,          /* functionality        */
235 };
236
237 /*
238  * This routine is registered with the OS as the function to call when
239  * the IIC interrupts.  It in turn, calls the Xilinx OS independent
240  * interrupt function.  The Xilinx OS independent interrupt function
241  * will in turn call any callbacks that we have registered for various
242  * conditions.
243  */
244 static void
245 xiic_interrupt(int irq, void *dev_id, struct pt_regs *regs)
246 {
247         struct xiic_dev *dev = dev_id;
248         XIic_InterruptHandler(&dev->Iic);
249 }
250
251 static void
252 RecvHandler(void *CallbackRef, int ByteCount)
253 {
254         struct xiic_dev *dev = (struct xiic_dev *) CallbackRef;
255
256         if (ByteCount == 0) {
257                 dev->interrupt_status = XST_SUCCESS;
258                 complete(&dev->complete);
259         }
260 }
261 static void
262 SendHandler(void *CallbackRef, int ByteCount)
263 {
264         struct xiic_dev *dev = (struct xiic_dev *) CallbackRef;
265
266         if (ByteCount == 0) {
267                 dev->interrupt_status = XST_SUCCESS;
268                 complete(&dev->complete);
269         }
270 }
271 static void
272 StatusHandler(void *CallbackRef, XStatus Status)
273 {
274         struct xiic_dev *dev = (struct xiic_dev *) CallbackRef;
275
276         dev->interrupt_status = Status;
277         complete(&dev->complete);
278 }
279
280 static void
281 xiic_inc_use(struct i2c_adapter *adap)
282 {
283 #ifdef MODULE
284         MOD_INC_USE_COUNT;
285 #endif
286 }
287 static void
288 xiic_dec_use(struct i2c_adapter *adap)
289 {
290 #ifdef MODULE
291         MOD_DEC_USE_COUNT;
292 #endif
293 }
294
295 static void
296 remove_head_dev(void)
297 {
298         struct xiic_dev *dev;
299         XIic_Config *cfg;
300
301         /* Pull the head off of dev_list. */
302         spin_lock(&dev_lock);
303         dev = dev_list;
304         dev_list = dev->next_dev;
305         spin_unlock(&dev_lock);
306
307         /*
308          * If we've told the core I2C code about this dev, tell
309          * the core I2C code to forget the dev.
310          */
311         if (dev->added) {
312                 /*
313                  * If an error is returned, there's not a whole lot we can
314                  * do.  An error has already been printed out so we'll
315                  * just keep trundling along.
316                  */
317                 (void) i2c_del_adapter(&dev->adap);
318         }
319
320         /* Tell the Xilinx code to take this IIC interface down. */
321         if (dev->started) {
322                 while (XIic_Stop(&dev->Iic) != XST_SUCCESS) {
323                         /* The bus was busy.  Retry. */
324                         printk(KERN_WARNING
325                                "%s #%d: Could not stop device.  Will retry.\n",
326                                dev->adap.name, dev->index);
327                         set_current_state(TASK_INTERRUPTIBLE);
328                         schedule_timeout(HZ / 2);
329                 }
330         }
331
332         /*
333          * Now that the Xilinx code isn't using the IRQ or registers,
334          * unmap the registers and free the IRQ.
335          */
336         if (dev->remapped) {
337                 cfg = XIic_GetConfig(dev->index);
338                 iounmap((void *) cfg->BaseAddress);
339                 cfg->BaseAddress = dev->save_BaseAddress;
340         };
341         if (dev->reqirq) {
342                 disable_irq(dev->irq);
343                 free_irq(dev->irq, dev);
344         }
345
346         kfree(dev);
347 }
348
349 static int __init
350 probe(int index)
351 {
352         static const unsigned long remap_size
353             = XPAR_IIC_0_HIGHADDR - XPAR_IIC_0_BASEADDR + 1;
354         struct xiic_dev *dev;
355         XIic_Config *cfg;
356         unsigned int irq;
357         int retval;
358
359         switch (index) {
360 #if defined(XPAR_INTC_0_IIC_0_VEC_ID)
361         case 0:
362                 irq = 31 - XPAR_INTC_0_IIC_0_VEC_ID;
363                 break;
364 #if defined(XPAR_INTC_0_IIC_1_VEC_ID)
365         case 1:
366                 irq = 31 - XPAR_INTC_0_IIC_1_VEC_ID;
367                 break;
368 #if defined(XPAR_INTC_0_IIC_2_VEC_ID)
369         case 2:
370                 irq = 31 - XPAR_INTC_0_IIC_2_VEC_ID;
371                 break;
372 #if defined(XPAR_INTC_0_IIC_3_VEC_ID)
373 #error Edit this file to add more devices.
374 #endif                          /* 3 */
375 #endif                          /* 2 */
376 #endif                          /* 1 */
377 #endif                          /* 0 */
378         default:
379                 return -ENODEV;
380         }
381
382         /* Find the config for our device. */
383         cfg = XIic_GetConfig(index);
384         if (!cfg)
385                 return -ENODEV;
386
387         /* Allocate the dev and zero it out. */
388         dev = (struct xiic_dev *) kmalloc(sizeof (struct xiic_dev), GFP_KERNEL);
389         if (!dev) {
390                 printk(KERN_ERR "%s #%d: Could not allocate device.\n",
391                        "Xilinx IIC adapter", index);
392                 return -ENOMEM;
393         }
394         memset(dev, 0, sizeof (struct xiic_dev));
395         strcpy(dev->adap.name, "Xilinx IIC adapter");
396         dev->index = index;
397
398         /* Make it the head of dev_list. */
399         spin_lock(&dev_lock);
400         dev->next_dev = dev_list;
401         dev_list = dev;
402         spin_unlock(&dev_lock);
403
404         init_completion(&dev->complete);
405
406         /* Change the addresses to be virtual; save the old ones to restore. */
407         dev->save_BaseAddress = cfg->BaseAddress;
408         cfg->BaseAddress = (u32) ioremap(dev->save_BaseAddress, remap_size);
409         dev->remapped = 1;
410
411         /* Tell the Xilinx code to bring this IIC interface up. */
412         if (XIic_Initialize(&dev->Iic, cfg->DeviceId) != XST_SUCCESS) {
413                 printk(KERN_ERR "%s #%d: Could not initialize device.\n",
414                        dev->adap.name, dev->index);
415                 remove_head_dev();
416                 return -ENODEV;
417         }
418         XIic_SetRecvHandler(&dev->Iic, (void *) dev, RecvHandler);
419         XIic_SetSendHandler(&dev->Iic, (void *) dev, SendHandler);
420         XIic_SetStatusHandler(&dev->Iic, (void *) dev, StatusHandler);
421
422         /* Grab the IRQ */
423         dev->irq = irq;
424         retval = request_irq(dev->irq, xiic_interrupt, 0, dev->adap.name, dev);
425         if (retval) {
426                 printk(KERN_ERR "%s #%d: Could not allocate interrupt %d.\n",
427                        dev->adap.name, dev->index, dev->irq);
428                 remove_head_dev();
429                 return retval;
430         }
431         dev->reqirq = 1;
432
433         if (XIic_Start(&dev->Iic) != XST_SUCCESS) {
434                 printk(KERN_ERR "%s #%d: Could not start device.\n",
435                        dev->adap.name, dev->index);
436                 remove_head_dev();
437                 return -ENODEV;
438         }
439         dev->started = 1;
440
441         /* Now tell the core I2C code about our new device. */
442         /*
443          * SAATODO: Get a real ID (perhaps I2C_HW_XILINX) after
444          * initial release.  Will need to email lm78@stimpy.netroedge.com
445          * per http://www2.lm-sensors.nu/~lm78/support.html
446          */
447         dev->adap.id = xiic_algo.id | I2C_DRIVERID_EXP0;
448         dev->adap.algo = &xiic_algo;
449         dev->adap.algo_data = NULL;
450         dev->adap.inc_use = xiic_inc_use;
451         dev->adap.dec_use = xiic_dec_use;
452         dev->adap.timeout = XIIC_TIMEOUT;
453         dev->adap.retries = XIIC_RETRY;
454         retval = i2c_add_adapter(&dev->adap);
455         if (retval) {
456                 printk(KERN_ERR "%s #%d: Could not add I2C adapter.\n",
457                        dev->adap.name, dev->index);
458                 remove_head_dev();
459                 return retval;
460         }
461         dev->added = 1;
462
463         printk(KERN_INFO "%s #%d at 0x%08X mapped to 0x%08X, irq=%d\n",
464                dev->adap.name, dev->index,
465                dev->save_BaseAddress, cfg->BaseAddress, dev->irq);
466
467         /* scan bus */
468         if (scan) {
469                 int i, anyfound;
470
471                 printk(KERN_INFO "%s #%d Bus scan:",
472                        dev->adap.name, dev->index);
473                 /*
474                  * The Philips I2C-bus specification defines special addresses:
475                  *      0x00       General Call
476                  *      0x01       CBUS Address
477                  *      0x02       Reserved for different bus format
478                  *      0x03       Reserved for future purposes
479                  *   0x04 to 0x07  Hs-mode master code
480                  *   0x78 to 0x7B  10-bit slave addressing
481                  *   0x7C to 0x7F  Reserved for future purposes
482                  *
483                  * We don't bother scanning any of these addresses.
484                  */
485                 anyfound = 0;
486                 for (i = 0x08; i < 0x78; i++) {
487                         u8 data;
488                         XStatus Status;
489
490                         if (XIic_SetAddress(&dev->Iic, XII_ADDR_TO_SEND_TYPE,
491                                             i) != XST_SUCCESS) {
492                                 printk(KERN_WARNING
493                                        "%s #%d: Could not set address to %2x.\n",
494                                        dev->adap.name, dev->index, i);
495                                 continue;
496                         }
497                         dev->interrupt_status = ~(XStatus) 0;
498                         Status = XIic_MasterRecv(&dev->Iic, &data, 1);
499                         if (Status != XST_SUCCESS) {
500                                 printk(KERN_WARNING
501                                        "%s #%d: Unexpected error %d.\n",
502                                        dev->adap.name, dev->index, Status);
503                                 continue;
504                         }
505                         wait_for_completion(&dev->complete);
506                         if (dev->interrupt_status == XST_SUCCESS) {
507                                 printk(" 0x%02x", i);
508                                 anyfound = 1;
509                         }
510                 }
511                 printk(anyfound ? " responded.\n" : " Nothing responded.\n");
512         }
513
514         return 0;
515 }
516 static int __init
517 xiic_init(void)
518 {
519         int index = 0;
520
521         while (probe(index++) == 0) ;
522         /* If we found at least one, report success. */
523         return (index > 1) ? 0 : -ENODEV;
524 }
525
526 static void __exit
527 xiic_cleanup(void)
528 {
529         while (dev_list)
530                 remove_head_dev();
531 }
532
533 EXPORT_NO_SYMBOLS;
534
535 module_init(xiic_init);
536 module_exit(xiic_cleanup);