blob: 53c6494451020d88613c7f229f04fa043c329b0e [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
29#include <linux/config.h>
30#include <linux/interrupt.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/delay.h>
34#include <linux/slab.h>
35#include <linux/init.h>
36#include <linux/errno.h>
37#include <linux/sched.h>
38#include <linux/device.h>
39#include <linux/i2c.h>
40
41#include <asm/io.h>
42
43#include "i2c-iop3xx.h"
44
45/* global unit counter */
Jean Delvare60507092005-09-25 16:23:07 +020046static int i2c_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static inline unsigned char
49iic_cook_addr(struct i2c_msg *msg)
50{
51 unsigned char addr;
52
53 addr = (msg->addr << 1);
54
55 if (msg->flags & I2C_M_RD)
56 addr |= 1;
57
58 /*
59 * Read or Write?
60 */
61 if (msg->flags & I2C_M_REV_DIR_ADDR)
62 addr ^= 1;
63
64 return addr;
65}
66
67static void
68iop3xx_i2c_reset(struct i2c_algo_iop3xx_data *iop3xx_adap)
69{
70 /* Follows devman 9.3 */
71 __raw_writel(IOP3XX_ICR_UNIT_RESET, iop3xx_adap->ioaddr + CR_OFFSET);
72 __raw_writel(IOP3XX_ISR_CLEARBITS, iop3xx_adap->ioaddr + SR_OFFSET);
73 __raw_writel(0, iop3xx_adap->ioaddr + CR_OFFSET);
74}
75
76static void
77iop3xx_i2c_set_slave_addr(struct i2c_algo_iop3xx_data *iop3xx_adap)
78{
79 __raw_writel(MYSAR, iop3xx_adap->ioaddr + SAR_OFFSET);
80}
81
82static void
83iop3xx_i2c_enable(struct i2c_algo_iop3xx_data *iop3xx_adap)
84{
85 u32 cr = IOP3XX_ICR_GCD | IOP3XX_ICR_SCLEN | IOP3XX_ICR_UE;
86
87 /*
Steven Cole44bbe872005-05-03 18:21:25 -060088 * Every time unit enable is asserted, GPOD needs to be cleared
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * on IOP321 to avoid data corruption on the bus.
90 */
91#ifdef CONFIG_ARCH_IOP321
92#define IOP321_GPOD_I2C0 0x00c0 /* clear these bits to enable ch0 */
93#define IOP321_GPOD_I2C1 0x0030 /* clear these bits to enable ch1 */
94
95 *IOP321_GPOD &= (iop3xx_adap->id == 0) ? ~IOP321_GPOD_I2C0 :
96 ~IOP321_GPOD_I2C1;
97#endif
98 /* NB SR bits not same position as CR IE bits :-( */
99 iop3xx_adap->SR_enabled =
100 IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD |
101 IOP3XX_ISR_RXFULL | IOP3XX_ISR_TXEMPTY;
102
103 cr |= IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
104 IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE;
105
106 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
107}
108
109static void
110iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data *iop3xx_adap)
111{
112 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
113
114 cr &= ~(IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE |
115 IOP3XX_ICR_MSTOP | IOP3XX_ICR_SCLEN);
116
117 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
118}
119
120/*
121 * NB: the handler has to clear the source of the interrupt!
122 * Then it passes the SR flags of interest to BH via adap data
123 */
124static irqreturn_t
125iop3xx_i2c_irq_handler(int this_irq, void *dev_id, struct pt_regs *regs)
126{
127 struct i2c_algo_iop3xx_data *iop3xx_adap = dev_id;
128 u32 sr = __raw_readl(iop3xx_adap->ioaddr + SR_OFFSET);
129
130 if ((sr &= iop3xx_adap->SR_enabled)) {
131 __raw_writel(sr, iop3xx_adap->ioaddr + SR_OFFSET);
132 iop3xx_adap->SR_received |= sr;
133 wake_up_interruptible(&iop3xx_adap->waitq);
134 }
135 return IRQ_HANDLED;
136}
137
138/* check all error conditions, clear them , report most important */
139static int
140iop3xx_i2c_error(u32 sr)
141{
142 int rc = 0;
143
144 if ((sr & IOP3XX_ISR_BERRD)) {
145 if ( !rc ) rc = -I2C_ERR_BERR;
146 }
147 if ((sr & IOP3XX_ISR_ALD)) {
148 if ( !rc ) rc = -I2C_ERR_ALD;
149 }
150 return rc;
151}
152
153static inline u32
154iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data *iop3xx_adap)
155{
156 unsigned long flags;
157 u32 sr;
158
159 spin_lock_irqsave(&iop3xx_adap->lock, flags);
160 sr = iop3xx_adap->SR_received;
161 iop3xx_adap->SR_received = 0;
162 spin_unlock_irqrestore(&iop3xx_adap->lock, flags);
163
164 return sr;
165}
166
167/*
168 * sleep until interrupted, then recover and analyse the SR
169 * saved by handler
170 */
171typedef int (* compare_func)(unsigned test, unsigned mask);
172/* returns 1 on correct comparison */
173
174static int
175iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data *iop3xx_adap,
176 unsigned flags, unsigned* status,
177 compare_func compare)
178{
179 unsigned sr = 0;
180 int interrupted;
181 int done;
182 int rc = 0;
183
184 do {
185 interrupted = wait_event_interruptible_timeout (
186 iop3xx_adap->waitq,
Dan Williamsfbd9a6d2005-11-01 22:31:12 +0000187 (done = compare( sr = iop3xx_i2c_get_srstat(iop3xx_adap) ,flags )),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 1 * HZ;
189 );
190 if ((rc = iop3xx_i2c_error(sr)) < 0) {
191 *status = sr;
192 return rc;
193 } else if (!interrupted) {
194 *status = sr;
195 return -ETIMEDOUT;
196 }
197 } while(!done);
198
199 *status = sr;
200
201 return 0;
202}
203
204/*
205 * Concrete compare_funcs
206 */
207static int
208all_bits_clear(unsigned test, unsigned mask)
209{
210 return (test & mask) == 0;
211}
212
213static int
214any_bits_set(unsigned test, unsigned mask)
215{
216 return (test & mask) != 0;
217}
218
219static int
220iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
221{
222 return iop3xx_i2c_wait_event(
223 iop3xx_adap,
224 IOP3XX_ISR_TXEMPTY | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
225 status, any_bits_set);
226}
227
228static int
229iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
230{
231 return iop3xx_i2c_wait_event(
232 iop3xx_adap,
233 IOP3XX_ISR_RXFULL | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
234 status, any_bits_set);
235}
236
237static int
238iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
239{
240 return iop3xx_i2c_wait_event(
241 iop3xx_adap, IOP3XX_ISR_UNITBUSY, status, all_bits_clear);
242}
243
244static int
245iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data *iop3xx_adap,
246 struct i2c_msg* msg)
247{
248 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
249 int status;
250 int rc;
251
252 __raw_writel(iic_cook_addr(msg), iop3xx_adap->ioaddr + DBR_OFFSET);
253
254 cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
255 cr |= IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE;
256
257 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
258 rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
259
260 return rc;
261}
262
263static int
264iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char byte,
265 int stop)
266{
267 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
268 int status;
269 int rc = 0;
270
271 __raw_writel(byte, iop3xx_adap->ioaddr + DBR_OFFSET);
272 cr &= ~IOP3XX_ICR_MSTART;
273 if (stop) {
274 cr |= IOP3XX_ICR_MSTOP;
275 } else {
276 cr &= ~IOP3XX_ICR_MSTOP;
277 }
278 cr |= IOP3XX_ICR_TBYTE;
279 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
280 rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
281
282 return rc;
283}
284
285static int
286iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char* byte,
287 int stop)
288{
289 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
290 int status;
291 int rc = 0;
292
293 cr &= ~IOP3XX_ICR_MSTART;
294
295 if (stop) {
296 cr |= IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK;
297 } else {
298 cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
299 }
300 cr |= IOP3XX_ICR_TBYTE;
301 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
302
303 rc = iop3xx_i2c_wait_rx_done(iop3xx_adap, &status);
304
305 *byte = __raw_readl(iop3xx_adap->ioaddr + DBR_OFFSET);
306
307 return rc;
308}
309
310static int
311iop3xx_i2c_writebytes(struct i2c_adapter *i2c_adap, const char *buf, int count)
312{
313 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
314 int ii;
315 int rc = 0;
316
317 for (ii = 0; rc == 0 && ii != count; ++ii)
318 rc = iop3xx_i2c_write_byte(iop3xx_adap, buf[ii], ii==count-1);
319 return rc;
320}
321
322static int
323iop3xx_i2c_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count)
324{
325 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
326 int ii;
327 int rc = 0;
328
329 for (ii = 0; rc == 0 && ii != count; ++ii)
330 rc = iop3xx_i2c_read_byte(iop3xx_adap, &buf[ii], ii==count-1);
331
332 return rc;
333}
334
335/*
336 * Description: This function implements combined transactions. Combined
337 * transactions consist of combinations of reading and writing blocks of data.
338 * FROM THE SAME ADDRESS
339 * Each transfer (i.e. a read or a write) is separated by a repeated start
340 * condition.
341 */
342static int
343iop3xx_i2c_handle_msg(struct i2c_adapter *i2c_adap, struct i2c_msg* pmsg)
344{
345 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
346 int rc;
347
348 rc = iop3xx_i2c_send_target_addr(iop3xx_adap, pmsg);
349 if (rc < 0) {
350 return rc;
351 }
352
353 if ((pmsg->flags&I2C_M_RD)) {
354 return iop3xx_i2c_readbytes(i2c_adap, pmsg->buf, pmsg->len);
355 } else {
356 return iop3xx_i2c_writebytes(i2c_adap, pmsg->buf, pmsg->len);
357 }
358}
359
360/*
361 * master_xfer() - main read/write entry
362 */
363static int
364iop3xx_i2c_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
365 int num)
366{
367 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
368 int im = 0;
369 int ret = 0;
370 int status;
371
372 iop3xx_i2c_wait_idle(iop3xx_adap, &status);
373 iop3xx_i2c_reset(iop3xx_adap);
374 iop3xx_i2c_enable(iop3xx_adap);
375
376 for (im = 0; ret == 0 && im != num; im++) {
377 ret = iop3xx_i2c_handle_msg(i2c_adap, &msgs[im]);
378 }
379
380 iop3xx_i2c_transaction_cleanup(iop3xx_adap);
381
382 if(ret)
383 return ret;
384
385 return im;
386}
387
388static int
389iop3xx_i2c_algo_control(struct i2c_adapter *adapter, unsigned int cmd,
390 unsigned long arg)
391{
392 return 0;
393}
394
395static u32
396iop3xx_i2c_func(struct i2c_adapter *adap)
397{
398 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
399}
400
401static struct i2c_algorithm iop3xx_i2c_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 .master_xfer = iop3xx_i2c_master_xfer,
403 .algo_control = iop3xx_i2c_algo_control,
404 .functionality = iop3xx_i2c_func,
405};
406
407static int
408iop3xx_i2c_remove(struct device *device)
409{
410 struct platform_device *pdev = to_platform_device(device);
411 struct i2c_adapter *padapter = dev_get_drvdata(&pdev->dev);
412 struct i2c_algo_iop3xx_data *adapter_data =
413 (struct i2c_algo_iop3xx_data *)padapter->algo_data;
414 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
415 unsigned long cr = __raw_readl(adapter_data->ioaddr + CR_OFFSET);
416
417 /*
418 * Disable the actual HW unit
419 */
420 cr &= ~(IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
421 IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE);
422 __raw_writel(cr, adapter_data->ioaddr + CR_OFFSET);
423
424 iounmap((void __iomem*)adapter_data->ioaddr);
425 release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
426 kfree(adapter_data);
427 kfree(padapter);
428
429 dev_set_drvdata(&pdev->dev, NULL);
430
431 return 0;
432}
433
434static int
435iop3xx_i2c_probe(struct device *dev)
436{
437 struct platform_device *pdev = to_platform_device(dev);
438 struct resource *res;
439 int ret;
440 struct i2c_adapter *new_adapter;
441 struct i2c_algo_iop3xx_data *adapter_data;
442
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200443 new_adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (!new_adapter) {
445 ret = -ENOMEM;
446 goto out;
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200449 adapter_data = kzalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (!adapter_data) {
451 ret = -ENOMEM;
452 goto free_adapter;
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
456 if (!res) {
457 ret = -ENODEV;
458 goto free_both;
459 }
460
461 if (!request_mem_region(res->start, IOP3XX_I2C_IO_SIZE, pdev->name)) {
462 ret = -EBUSY;
463 goto free_both;
464 }
465
466 /* set the adapter enumeration # */
467 adapter_data->id = i2c_id++;
468
469 adapter_data->ioaddr = (u32)ioremap(res->start, IOP3XX_I2C_IO_SIZE);
470 if (!adapter_data->ioaddr) {
471 ret = -ENOMEM;
472 goto release_region;
473 }
474
Dan Williamsfbd9a6d2005-11-01 22:31:12 +0000475 ret = request_irq(platform_get_irq(pdev, 0), iop3xx_i2c_irq_handler, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 pdev->name, adapter_data);
Dan Williamsfbd9a6d2005-11-01 22:31:12 +0000477
478 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 ret = -EIO;
480 goto unmap;
481 }
482
483 memcpy(new_adapter->name, pdev->name, strlen(pdev->name));
484 new_adapter->id = I2C_HW_IOP3XX;
485 new_adapter->owner = THIS_MODULE;
486 new_adapter->dev.parent = &pdev->dev;
487
488 /*
489 * Default values...should these come in from board code?
490 */
491 new_adapter->timeout = 100;
492 new_adapter->retries = 3;
493 new_adapter->algo = &iop3xx_i2c_algo;
494
495 init_waitqueue_head(&adapter_data->waitq);
496 spin_lock_init(&adapter_data->lock);
497
498 iop3xx_i2c_reset(adapter_data);
499 iop3xx_i2c_set_slave_addr(adapter_data);
500 iop3xx_i2c_enable(adapter_data);
501
502 dev_set_drvdata(&pdev->dev, new_adapter);
503 new_adapter->algo_data = adapter_data;
504
505 i2c_add_adapter(new_adapter);
506
507 return 0;
508
509unmap:
510 iounmap((void __iomem*)adapter_data->ioaddr);
511
512release_region:
513 release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
514
515free_both:
516 kfree(adapter_data);
517
518free_adapter:
519 kfree(new_adapter);
520
521out:
522 return ret;
523}
524
525
526static struct device_driver iop3xx_i2c_driver = {
Laurent Riffard0cf36282005-10-17 22:51:37 +0200527 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 .name = "IOP3xx-I2C",
529 .bus = &platform_bus_type,
530 .probe = iop3xx_i2c_probe,
531 .remove = iop3xx_i2c_remove
532};
533
534static int __init
535i2c_iop3xx_init (void)
536{
537 return driver_register(&iop3xx_i2c_driver);
538}
539
540static void __exit
541i2c_iop3xx_exit (void)
542{
543 driver_unregister(&iop3xx_i2c_driver);
544 return;
545}
546
547module_init (i2c_iop3xx_init);
548module_exit (i2c_iop3xx_exit);
549
550MODULE_AUTHOR("D-TACQ Solutions Ltd <www.d-tacq.com>");
551MODULE_DESCRIPTION("IOP3xx iic algorithm and driver");
552MODULE_LICENSE("GPL");