4 * Xilinx IIC Adapter component to interface IIC component to Linux
6 * Author: MontaVista Software, Inc.
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.
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
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.
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.
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.
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.
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/slab.h>
59 #include <linux/i2c.h>
61 #include <xbasic_types.h>
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 */
72 /* SAATODO: actually use these? */
73 #define XIIC_TIMEOUT 100
76 /* Our private per device data. */
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 */
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).
92 XStatus interrupt_status;
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.
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? */
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;
108 /* SAATODO: This function will be moved into the Xilinx code. */
109 /*****************************************************************************/
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.
115 * @param Instance is the index of the interface being looked up.
119 * A pointer to the configuration table entry corresponding to the given
120 * device ID, or NULL if no match is found.
126 ******************************************************************************/
127 XIic_Config *XIic_GetConfig(int Instance)
129 if (Instance < 0 || Instance >= XPAR_XIIC_NUM_INSTANCES)
134 return &XIic_ConfigTable[Instance];
138 xiic_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
140 struct xiic_dev *dev = (struct xiic_dev *) i2c_adap;
141 struct i2c_msg *pmsg;
146 for (i = 0; i < num; i++) {
149 if (!pmsg->len) /* If length is zero */
150 continue; /* on to the next request. */
153 if (pmsg->flags & I2C_M_TEN)
154 options |= XII_SEND_10_BIT_OPTION;
156 options |= XII_REPEATED_START_OPTION;
157 XIic_SetOptions(&dev->Iic, options);
159 if (XIic_SetAddress(&dev->Iic, XII_ADDR_TO_SEND_TYPE,
160 pmsg->addr) != XST_SUCCESS) {
162 "%s #%d: Could not set address to 0x%2x.\n",
163 dev->adap.name, dev->index, pmsg->addr);
166 dev->interrupt_status = ~(XStatus) 0;
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.
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);
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);
189 if (Status != XST_SUCCESS) {
191 "%s #%d: Unexpected error %d.\n",
192 dev->adap.name, dev->index, Status);
195 wait_for_completion(&dev->complete);
196 if (dev->interrupt_status != XST_SUCCESS) {
198 "%s #%d: Could not talk to device 0x%2x (%d).\n",
199 dev->adap.name, dev->index, pmsg->addr,
200 dev->interrupt_status);
208 xiic_algo_control(struct i2c_adapter *adapter,
209 unsigned int cmd, unsigned long arg)
215 xiic_bit_func(struct i2c_adapter *adap)
217 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
218 I2C_FUNC_PROTOCOL_MANGLING;
221 static struct i2c_algorithm xiic_algo = {
222 "Xilinx IIC algorithm", /* name */
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
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 */
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
245 xiic_interrupt(int irq, void *dev_id, struct pt_regs *regs)
247 struct xiic_dev *dev = dev_id;
248 XIic_InterruptHandler(&dev->Iic);
252 RecvHandler(void *CallbackRef, int ByteCount)
254 struct xiic_dev *dev = (struct xiic_dev *) CallbackRef;
256 if (ByteCount == 0) {
257 dev->interrupt_status = XST_SUCCESS;
258 complete(&dev->complete);
262 SendHandler(void *CallbackRef, int ByteCount)
264 struct xiic_dev *dev = (struct xiic_dev *) CallbackRef;
266 if (ByteCount == 0) {
267 dev->interrupt_status = XST_SUCCESS;
268 complete(&dev->complete);
272 StatusHandler(void *CallbackRef, XStatus Status)
274 struct xiic_dev *dev = (struct xiic_dev *) CallbackRef;
276 dev->interrupt_status = Status;
277 complete(&dev->complete);
281 xiic_inc_use(struct i2c_adapter *adap)
288 xiic_dec_use(struct i2c_adapter *adap)
296 remove_head_dev(void)
298 struct xiic_dev *dev;
301 /* Pull the head off of dev_list. */
302 spin_lock(&dev_lock);
304 dev_list = dev->next_dev;
305 spin_unlock(&dev_lock);
308 * If we've told the core I2C code about this dev, tell
309 * the core I2C code to forget the dev.
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.
317 (void) i2c_del_adapter(&dev->adap);
320 /* Tell the Xilinx code to take this IIC interface down. */
322 while (XIic_Stop(&dev->Iic) != XST_SUCCESS) {
323 /* The bus was busy. Retry. */
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);
333 * Now that the Xilinx code isn't using the IRQ or registers,
334 * unmap the registers and free the IRQ.
337 cfg = XIic_GetConfig(dev->index);
338 iounmap((void *) cfg->BaseAddress);
339 cfg->BaseAddress = dev->save_BaseAddress;
342 disable_irq(dev->irq);
343 free_irq(dev->irq, dev);
352 static const unsigned long remap_size
353 = XPAR_IIC_0_HIGHADDR - XPAR_IIC_0_BASEADDR + 1;
354 struct xiic_dev *dev;
360 #if defined(XPAR_INTC_0_IIC_0_VEC_ID)
362 irq = 31 - XPAR_INTC_0_IIC_0_VEC_ID;
364 #if defined(XPAR_INTC_0_IIC_1_VEC_ID)
366 irq = 31 - XPAR_INTC_0_IIC_1_VEC_ID;
368 #if defined(XPAR_INTC_0_IIC_2_VEC_ID)
370 irq = 31 - XPAR_INTC_0_IIC_2_VEC_ID;
372 #if defined(XPAR_INTC_0_IIC_3_VEC_ID)
373 #error Edit this file to add more devices.
382 /* Find the config for our device. */
383 cfg = XIic_GetConfig(index);
387 /* Allocate the dev and zero it out. */
388 dev = (struct xiic_dev *) kmalloc(sizeof (struct xiic_dev), GFP_KERNEL);
390 printk(KERN_ERR "%s #%d: Could not allocate device.\n",
391 "Xilinx IIC adapter", index);
394 memset(dev, 0, sizeof (struct xiic_dev));
395 strcpy(dev->adap.name, "Xilinx IIC adapter");
398 /* Make it the head of dev_list. */
399 spin_lock(&dev_lock);
400 dev->next_dev = dev_list;
402 spin_unlock(&dev_lock);
404 init_completion(&dev->complete);
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);
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);
418 XIic_SetRecvHandler(&dev->Iic, (void *) dev, RecvHandler);
419 XIic_SetSendHandler(&dev->Iic, (void *) dev, SendHandler);
420 XIic_SetStatusHandler(&dev->Iic, (void *) dev, StatusHandler);
424 retval = request_irq(dev->irq, xiic_interrupt, 0, dev->adap.name, dev);
426 printk(KERN_ERR "%s #%d: Could not allocate interrupt %d.\n",
427 dev->adap.name, dev->index, dev->irq);
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);
441 /* Now tell the core I2C code about our new device. */
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
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);
456 printk(KERN_ERR "%s #%d: Could not add I2C adapter.\n",
457 dev->adap.name, dev->index);
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);
471 printk(KERN_INFO "%s #%d Bus scan:",
472 dev->adap.name, dev->index);
474 * The Philips I2C-bus specification defines special addresses:
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
483 * We don't bother scanning any of these addresses.
486 for (i = 0x08; i < 0x78; i++) {
490 if (XIic_SetAddress(&dev->Iic, XII_ADDR_TO_SEND_TYPE,
493 "%s #%d: Could not set address to %2x.\n",
494 dev->adap.name, dev->index, i);
497 dev->interrupt_status = ~(XStatus) 0;
498 Status = XIic_MasterRecv(&dev->Iic, &data, 1);
499 if (Status != XST_SUCCESS) {
501 "%s #%d: Unexpected error %d.\n",
502 dev->adap.name, dev->index, Status);
505 wait_for_completion(&dev->complete);
506 if (dev->interrupt_status == XST_SUCCESS) {
507 printk(" 0x%02x", i);
511 printk(anyfound ? " responded.\n" : " Nothing responded.\n");
521 while (probe(index++) == 0) ;
522 /* If we found at least one, report success. */
523 return (index > 1) ? 0 : -ENODEV;
535 module_init(xiic_init);
536 module_exit(xiic_cleanup);