blob: aca7e166860546174c59557aead82d7d90c25428 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ------------------------------------------------------------------------- */
2/* i2c-iop3xx.c i2c driver algorithms for Intel XScale IOP3xx & IXP46x */
3/* ------------------------------------------------------------------------- */
4/* Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd
5 * <Peter dot Milne at D hyphen TACQ dot com>
6 *
7 * With acknowledgements to i2c-algo-ibm_ocp.c by
8 * Ian DaSilva, MontaVista Software, Inc. idasilva@mvista.com
9 *
10 * And i2c-algo-pcf.c, which was created by Simon G. Vogl and Hans Berglund:
11 *
12 * Copyright (C) 1995-1997 Simon G. Vogl, 1998-2000 Hans Berglund
13 *
Dan Williamsfbd9a6d2005-11-01 22:31:12 +000014 * And which acknowledged Kyösti Mälkki <kmalkki@cc.hut.fi>,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Frodo Looijaard <frodol@dds.nl>, Martin Bailey<mbailey@littlefeet-inc.com>
16 *
17 * Major cleanup by Deepak Saxena <dsaxena@plexity.net>, 01/2005:
18 *
19 * - Use driver model to pass per-chip info instead of hardcoding and #ifdefs
20 * - Use ioremap/__raw_readl/__raw_writel instead of direct dereference
21 * - Make it work with IXP46x chips
22 * - Cleanup function names, coding style, etc
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, version 2.
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h>
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/delay.h>
33#include <linux/slab.h>
34#include <linux/init.h>
35#include <linux/errno.h>
36#include <linux/sched.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010037#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/i2c.h>
39
40#include <asm/io.h>
41
42#include "i2c-iop3xx.h"
43
44/* global unit counter */
Jean Delvare60507092005-09-25 16:23:07 +020045static int i2c_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static inline unsigned char
48iic_cook_addr(struct i2c_msg *msg)
49{
50 unsigned char addr;
51
52 addr = (msg->addr << 1);
53
54 if (msg->flags & I2C_M_RD)
55 addr |= 1;
56
57 /*
58 * Read or Write?
59 */
60 if (msg->flags & I2C_M_REV_DIR_ADDR)
61 addr ^= 1;
62
63 return addr;
64}
65
66static void
67iop3xx_i2c_reset(struct i2c_algo_iop3xx_data *iop3xx_adap)
68{
69 /* Follows devman 9.3 */
70 __raw_writel(IOP3XX_ICR_UNIT_RESET, iop3xx_adap->ioaddr + CR_OFFSET);
71 __raw_writel(IOP3XX_ISR_CLEARBITS, iop3xx_adap->ioaddr + SR_OFFSET);
72 __raw_writel(0, iop3xx_adap->ioaddr + CR_OFFSET);
73}
74
75static void
76iop3xx_i2c_set_slave_addr(struct i2c_algo_iop3xx_data *iop3xx_adap)
77{
78 __raw_writel(MYSAR, iop3xx_adap->ioaddr + SAR_OFFSET);
79}
80
81static void
82iop3xx_i2c_enable(struct i2c_algo_iop3xx_data *iop3xx_adap)
83{
84 u32 cr = IOP3XX_ICR_GCD | IOP3XX_ICR_SCLEN | IOP3XX_ICR_UE;
85
86 /*
Steven Cole44bbe872005-05-03 18:21:25 -060087 * Every time unit enable is asserted, GPOD needs to be cleared
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * on IOP321 to avoid data corruption on the bus.
89 */
90#ifdef CONFIG_ARCH_IOP321
91#define IOP321_GPOD_I2C0 0x00c0 /* clear these bits to enable ch0 */
92#define IOP321_GPOD_I2C1 0x0030 /* clear these bits to enable ch1 */
93
94 *IOP321_GPOD &= (iop3xx_adap->id == 0) ? ~IOP321_GPOD_I2C0 :
95 ~IOP321_GPOD_I2C1;
96#endif
97 /* NB SR bits not same position as CR IE bits :-( */
98 iop3xx_adap->SR_enabled =
99 IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD |
100 IOP3XX_ISR_RXFULL | IOP3XX_ISR_TXEMPTY;
101
102 cr |= IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
103 IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE;
104
105 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
106}
107
108static void
109iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data *iop3xx_adap)
110{
111 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
112
113 cr &= ~(IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE |
114 IOP3XX_ICR_MSTOP | IOP3XX_ICR_SCLEN);
115
116 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
117}
118
119/*
120 * NB: the handler has to clear the source of the interrupt!
121 * Then it passes the SR flags of interest to BH via adap data
122 */
123static irqreturn_t
124iop3xx_i2c_irq_handler(int this_irq, void *dev_id, struct pt_regs *regs)
125{
126 struct i2c_algo_iop3xx_data *iop3xx_adap = dev_id;
127 u32 sr = __raw_readl(iop3xx_adap->ioaddr + SR_OFFSET);
128
129 if ((sr &= iop3xx_adap->SR_enabled)) {
130 __raw_writel(sr, iop3xx_adap->ioaddr + SR_OFFSET);
131 iop3xx_adap->SR_received |= sr;
132 wake_up_interruptible(&iop3xx_adap->waitq);
133 }
134 return IRQ_HANDLED;
135}
136
137/* check all error conditions, clear them , report most important */
138static int
139iop3xx_i2c_error(u32 sr)
140{
141 int rc = 0;
142
143 if ((sr & IOP3XX_ISR_BERRD)) {
144 if ( !rc ) rc = -I2C_ERR_BERR;
145 }
146 if ((sr & IOP3XX_ISR_ALD)) {
147 if ( !rc ) rc = -I2C_ERR_ALD;
148 }
149 return rc;
150}
151
152static inline u32
153iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data *iop3xx_adap)
154{
155 unsigned long flags;
156 u32 sr;
157
158 spin_lock_irqsave(&iop3xx_adap->lock, flags);
159 sr = iop3xx_adap->SR_received;
160 iop3xx_adap->SR_received = 0;
161 spin_unlock_irqrestore(&iop3xx_adap->lock, flags);
162
163 return sr;
164}
165
166/*
167 * sleep until interrupted, then recover and analyse the SR
168 * saved by handler
169 */
170typedef int (* compare_func)(unsigned test, unsigned mask);
171/* returns 1 on correct comparison */
172
173static int
174iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data *iop3xx_adap,
175 unsigned flags, unsigned* status,
176 compare_func compare)
177{
178 unsigned sr = 0;
179 int interrupted;
180 int done;
181 int rc = 0;
182
183 do {
184 interrupted = wait_event_interruptible_timeout (
185 iop3xx_adap->waitq,
Dan Williamsfbd9a6d2005-11-01 22:31:12 +0000186 (done = compare( sr = iop3xx_i2c_get_srstat(iop3xx_adap) ,flags )),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 1 * HZ;
188 );
189 if ((rc = iop3xx_i2c_error(sr)) < 0) {
190 *status = sr;
191 return rc;
192 } else if (!interrupted) {
193 *status = sr;
194 return -ETIMEDOUT;
195 }
196 } while(!done);
197
198 *status = sr;
199
200 return 0;
201}
202
203/*
204 * Concrete compare_funcs
205 */
206static int
207all_bits_clear(unsigned test, unsigned mask)
208{
209 return (test & mask) == 0;
210}
211
212static int
213any_bits_set(unsigned test, unsigned mask)
214{
215 return (test & mask) != 0;
216}
217
218static int
219iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
220{
221 return iop3xx_i2c_wait_event(
222 iop3xx_adap,
223 IOP3XX_ISR_TXEMPTY | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
224 status, any_bits_set);
225}
226
227static int
228iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
229{
230 return iop3xx_i2c_wait_event(
231 iop3xx_adap,
232 IOP3XX_ISR_RXFULL | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
233 status, any_bits_set);
234}
235
236static int
237iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
238{
239 return iop3xx_i2c_wait_event(
240 iop3xx_adap, IOP3XX_ISR_UNITBUSY, status, all_bits_clear);
241}
242
243static int
244iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data *iop3xx_adap,
245 struct i2c_msg* msg)
246{
247 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
248 int status;
249 int rc;
250
251 __raw_writel(iic_cook_addr(msg), iop3xx_adap->ioaddr + DBR_OFFSET);
252
253 cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
254 cr |= IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE;
255
256 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
257 rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
258
259 return rc;
260}
261
262static int
263iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char byte,
264 int stop)
265{
266 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
267 int status;
268 int rc = 0;
269
270 __raw_writel(byte, iop3xx_adap->ioaddr + DBR_OFFSET);
271 cr &= ~IOP3XX_ICR_MSTART;
272 if (stop) {
273 cr |= IOP3XX_ICR_MSTOP;
274 } else {
275 cr &= ~IOP3XX_ICR_MSTOP;
276 }
277 cr |= IOP3XX_ICR_TBYTE;
278 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
279 rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
280
281 return rc;
282}
283
284static int
285iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char* byte,
286 int stop)
287{
288 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
289 int status;
290 int rc = 0;
291
292 cr &= ~IOP3XX_ICR_MSTART;
293
294 if (stop) {
295 cr |= IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK;
296 } else {
297 cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
298 }
299 cr |= IOP3XX_ICR_TBYTE;
300 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
301
302 rc = iop3xx_i2c_wait_rx_done(iop3xx_adap, &status);
303
304 *byte = __raw_readl(iop3xx_adap->ioaddr + DBR_OFFSET);
305
306 return rc;
307}
308
309static int
310iop3xx_i2c_writebytes(struct i2c_adapter *i2c_adap, const char *buf, int count)
311{
312 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
313 int ii;
314 int rc = 0;
315
316 for (ii = 0; rc == 0 && ii != count; ++ii)
317 rc = iop3xx_i2c_write_byte(iop3xx_adap, buf[ii], ii==count-1);
318 return rc;
319}
320
321static int
322iop3xx_i2c_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count)
323{
324 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
325 int ii;
326 int rc = 0;
327
328 for (ii = 0; rc == 0 && ii != count; ++ii)
329 rc = iop3xx_i2c_read_byte(iop3xx_adap, &buf[ii], ii==count-1);
330
331 return rc;
332}
333
334/*
335 * Description: This function implements combined transactions. Combined
336 * transactions consist of combinations of reading and writing blocks of data.
337 * FROM THE SAME ADDRESS
338 * Each transfer (i.e. a read or a write) is separated by a repeated start
339 * condition.
340 */
341static int
342iop3xx_i2c_handle_msg(struct i2c_adapter *i2c_adap, struct i2c_msg* pmsg)
343{
344 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
345 int rc;
346
347 rc = iop3xx_i2c_send_target_addr(iop3xx_adap, pmsg);
348 if (rc < 0) {
349 return rc;
350 }
351
352 if ((pmsg->flags&I2C_M_RD)) {
353 return iop3xx_i2c_readbytes(i2c_adap, pmsg->buf, pmsg->len);
354 } else {
355 return iop3xx_i2c_writebytes(i2c_adap, pmsg->buf, pmsg->len);
356 }
357}
358
359/*
360 * master_xfer() - main read/write entry
361 */
362static int
363iop3xx_i2c_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
364 int num)
365{
366 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
367 int im = 0;
368 int ret = 0;
369 int status;
370
371 iop3xx_i2c_wait_idle(iop3xx_adap, &status);
372 iop3xx_i2c_reset(iop3xx_adap);
373 iop3xx_i2c_enable(iop3xx_adap);
374
375 for (im = 0; ret == 0 && im != num; im++) {
376 ret = iop3xx_i2c_handle_msg(i2c_adap, &msgs[im]);
377 }
378
379 iop3xx_i2c_transaction_cleanup(iop3xx_adap);
380
381 if(ret)
382 return ret;
383
384 return im;
385}
386
387static int
388iop3xx_i2c_algo_control(struct i2c_adapter *adapter, unsigned int cmd,
389 unsigned long arg)
390{
391 return 0;
392}
393
394static u32
395iop3xx_i2c_func(struct i2c_adapter *adap)
396{
397 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
398}
399
400static struct i2c_algorithm iop3xx_i2c_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 .master_xfer = iop3xx_i2c_master_xfer,
402 .algo_control = iop3xx_i2c_algo_control,
403 .functionality = iop3xx_i2c_func,
404};
405
406static int
Russell King3ae5eae2005-11-09 22:32:44 +0000407iop3xx_i2c_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Russell King3ae5eae2005-11-09 22:32:44 +0000409 struct i2c_adapter *padapter = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct i2c_algo_iop3xx_data *adapter_data =
411 (struct i2c_algo_iop3xx_data *)padapter->algo_data;
412 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
413 unsigned long cr = __raw_readl(adapter_data->ioaddr + CR_OFFSET);
414
415 /*
416 * Disable the actual HW unit
417 */
418 cr &= ~(IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
419 IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE);
420 __raw_writel(cr, adapter_data->ioaddr + CR_OFFSET);
421
422 iounmap((void __iomem*)adapter_data->ioaddr);
423 release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
424 kfree(adapter_data);
425 kfree(padapter);
426
Russell King3ae5eae2005-11-09 22:32:44 +0000427 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 return 0;
430}
431
432static int
Russell King3ae5eae2005-11-09 22:32:44 +0000433iop3xx_i2c_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct resource *res;
David Vrabel48944732006-01-19 17:56:29 +0000436 int ret, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct i2c_adapter *new_adapter;
438 struct i2c_algo_iop3xx_data *adapter_data;
439
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200440 new_adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!new_adapter) {
442 ret = -ENOMEM;
443 goto out;
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200446 adapter_data = kzalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (!adapter_data) {
448 ret = -ENOMEM;
449 goto free_adapter;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
453 if (!res) {
454 ret = -ENODEV;
455 goto free_both;
456 }
457
458 if (!request_mem_region(res->start, IOP3XX_I2C_IO_SIZE, pdev->name)) {
459 ret = -EBUSY;
460 goto free_both;
461 }
462
463 /* set the adapter enumeration # */
464 adapter_data->id = i2c_id++;
465
466 adapter_data->ioaddr = (u32)ioremap(res->start, IOP3XX_I2C_IO_SIZE);
467 if (!adapter_data->ioaddr) {
468 ret = -ENOMEM;
469 goto release_region;
470 }
471
David Vrabel48944732006-01-19 17:56:29 +0000472 irq = platform_get_irq(pdev, 0);
473 if (irq < 0) {
474 ret = -ENXIO;
475 goto unmap;
476 }
477 ret = request_irq(irq, iop3xx_i2c_irq_handler, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 pdev->name, adapter_data);
Dan Williamsfbd9a6d2005-11-01 22:31:12 +0000479
480 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 ret = -EIO;
482 goto unmap;
483 }
484
485 memcpy(new_adapter->name, pdev->name, strlen(pdev->name));
486 new_adapter->id = I2C_HW_IOP3XX;
487 new_adapter->owner = THIS_MODULE;
488 new_adapter->dev.parent = &pdev->dev;
489
490 /*
491 * Default values...should these come in from board code?
492 */
493 new_adapter->timeout = 100;
494 new_adapter->retries = 3;
495 new_adapter->algo = &iop3xx_i2c_algo;
496
497 init_waitqueue_head(&adapter_data->waitq);
498 spin_lock_init(&adapter_data->lock);
499
500 iop3xx_i2c_reset(adapter_data);
501 iop3xx_i2c_set_slave_addr(adapter_data);
502 iop3xx_i2c_enable(adapter_data);
503
Russell King3ae5eae2005-11-09 22:32:44 +0000504 platform_set_drvdata(pdev, new_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 new_adapter->algo_data = adapter_data;
506
507 i2c_add_adapter(new_adapter);
508
509 return 0;
510
511unmap:
512 iounmap((void __iomem*)adapter_data->ioaddr);
513
514release_region:
515 release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
516
517free_both:
518 kfree(adapter_data);
519
520free_adapter:
521 kfree(new_adapter);
522
523out:
524 return ret;
525}
526
527
Russell King3ae5eae2005-11-09 22:32:44 +0000528static struct platform_driver iop3xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 .probe = iop3xx_i2c_probe,
Russell King3ae5eae2005-11-09 22:32:44 +0000530 .remove = iop3xx_i2c_remove,
531 .driver = {
532 .owner = THIS_MODULE,
533 .name = "IOP3xx-I2C",
534 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535};
536
537static int __init
538i2c_iop3xx_init (void)
539{
Russell King3ae5eae2005-11-09 22:32:44 +0000540 return platform_driver_register(&iop3xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543static void __exit
544i2c_iop3xx_exit (void)
545{
Russell King3ae5eae2005-11-09 22:32:44 +0000546 platform_driver_unregister(&iop3xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return;
548}
549
550module_init (i2c_iop3xx_init);
551module_exit (i2c_iop3xx_exit);
552
553MODULE_AUTHOR("D-TACQ Solutions Ltd <www.d-tacq.com>");
554MODULE_DESCRIPTION("IOP3xx iic algorithm and driver");
555MODULE_LICENSE("GPL");