blob: 1215686bd3c20dd120e16ac39f7e7cbf26b5220d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * (C) Copyright 2003-2004
3 * Humboldt Solutions Ltd, adrian@humboldt.co.uk.
4
5 * This is a combined i2c adapter and algorithm driver for the
6 * MPC107/Tsi107 PowerPC northbridge and processors that include
7 * the same I2C unit (8240, 8245, 85xx).
8 *
9 * Release 0.8
10 *
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/sched.h>
19#include <linux/init.h>
Jon Smirl0d1cde22008-06-30 19:01:26 -040020#include <linux/of_platform.h>
21#include <linux/of_i2c.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010022
Wolfgang Grandegger8101a302009-04-07 10:20:53 +020023#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/fsl_devices.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/i2c.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
28
Jon Smirl0d1cde22008-06-30 19:01:26 -040029#define DRV_NAME "mpc-i2c"
30
Wolfgang Grandegger8101a302009-04-07 10:20:53 +020031#define MPC_I2C_FDR 0x04
32#define MPC_I2C_CR 0x08
33#define MPC_I2C_SR 0x0c
34#define MPC_I2C_DR 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define MPC_I2C_DFSRR 0x14
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#define CCR_MEN 0x80
38#define CCR_MIEN 0x40
39#define CCR_MSTA 0x20
40#define CCR_MTX 0x10
41#define CCR_TXAK 0x08
42#define CCR_RSTA 0x04
43
44#define CSR_MCF 0x80
45#define CSR_MAAS 0x40
46#define CSR_MBB 0x20
47#define CSR_MAL 0x10
48#define CSR_SRW 0x04
49#define CSR_MIF 0x02
50#define CSR_RXAK 0x01
51
52struct mpc_i2c {
Al Viro7366d362005-04-25 18:32:12 -070053 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 u32 interrupt;
55 wait_queue_head_t queue;
56 struct i2c_adapter adap;
57 int irq;
58 u32 flags;
59};
60
Wolfgang Grandegger8101a302009-04-07 10:20:53 +020061static inline void writeccr(struct mpc_i2c *i2c, u32 x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 writeb(x, i2c->base + MPC_I2C_CR);
64}
65
David Howells7d12e782006-10-05 14:55:46 +010066static irqreturn_t mpc_i2c_isr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 struct mpc_i2c *i2c = dev_id;
69 if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
70 /* Read again to allow register to stabilise */
71 i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
72 writeb(0, i2c->base + MPC_I2C_SR);
Timur Tabi1ab082d2009-02-06 08:00:37 -060073 wake_up(&i2c->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 return IRQ_HANDLED;
76}
77
Domen Puncer254db9b2007-07-12 14:12:31 +020078/* Sometimes 9th clock pulse isn't generated, and slave doesn't release
79 * the bus, because it wants to send ACK.
80 * Following sequence of enabling/disabling and sending start/stop generates
81 * the pulse, so it's all OK.
82 */
83static void mpc_i2c_fixup(struct mpc_i2c *i2c)
84{
85 writeccr(i2c, 0);
86 udelay(30);
87 writeccr(i2c, CCR_MEN);
88 udelay(30);
89 writeccr(i2c, CCR_MSTA | CCR_MTX);
90 udelay(30);
91 writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
92 udelay(30);
93 writeccr(i2c, CCR_MEN);
94 udelay(30);
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
98{
99 unsigned long orig_jiffies = jiffies;
100 u32 x;
101 int result = 0;
102
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200103 if (i2c->irq == NO_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
105 schedule();
106 if (time_after(jiffies, orig_jiffies + timeout)) {
107 pr_debug("I2C: timeout\n");
Domen Puncer5af0e072007-08-14 18:37:14 +0200108 writeccr(i2c, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 result = -EIO;
110 break;
111 }
112 }
113 x = readb(i2c->base + MPC_I2C_SR);
114 writeb(0, i2c->base + MPC_I2C_SR);
115 } else {
116 /* Interrupt mode */
Timur Tabi1ab082d2009-02-06 08:00:37 -0600117 result = wait_event_timeout(i2c->queue,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100118 (i2c->interrupt & CSR_MIF), timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Timur Tabi1ab082d2009-02-06 08:00:37 -0600120 if (unlikely(!(i2c->interrupt & CSR_MIF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 pr_debug("I2C: wait timeout\n");
Domen Puncer5af0e072007-08-14 18:37:14 +0200122 writeccr(i2c, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 result = -ETIMEDOUT;
124 }
125
126 x = i2c->interrupt;
127 i2c->interrupt = 0;
128 }
129
130 if (result < 0)
131 return result;
132
133 if (!(x & CSR_MCF)) {
134 pr_debug("I2C: unfinished\n");
135 return -EIO;
136 }
137
138 if (x & CSR_MAL) {
139 pr_debug("I2C: MAL\n");
140 return -EIO;
141 }
142
143 if (writing && (x & CSR_RXAK)) {
144 pr_debug("I2C: No RXAK\n");
145 /* generate stop */
146 writeccr(i2c, CCR_MEN);
147 return -EIO;
148 }
149 return 0;
150}
151
152static void mpc_i2c_setclock(struct mpc_i2c *i2c)
153{
154 /* Set clock and filters */
155 if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) {
156 writeb(0x31, i2c->base + MPC_I2C_FDR);
157 writeb(0x10, i2c->base + MPC_I2C_DFSRR);
158 } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200)
159 writeb(0x3f, i2c->base + MPC_I2C_FDR);
160 else
161 writel(0x1031, i2c->base + MPC_I2C_FDR);
162}
163
164static void mpc_i2c_start(struct mpc_i2c *i2c)
165{
166 /* Clear arbitration */
167 writeb(0, i2c->base + MPC_I2C_SR);
168 /* Start with MEN */
169 writeccr(i2c, CCR_MEN);
170}
171
172static void mpc_i2c_stop(struct mpc_i2c *i2c)
173{
174 writeccr(i2c, CCR_MEN);
175}
176
177static int mpc_write(struct mpc_i2c *i2c, int target,
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200178 const u8 *data, int length, int restart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100180 int i, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 unsigned timeout = i2c->adap.timeout;
182 u32 flags = restart ? CCR_RSTA : 0;
183
184 /* Start with MEN */
185 if (!restart)
186 writeccr(i2c, CCR_MEN);
187 /* Start as master */
188 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
189 /* Write target byte */
190 writeb((target << 1), i2c->base + MPC_I2C_DR);
191
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100192 result = i2c_wait(i2c, timeout, 1);
193 if (result < 0)
194 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 for (i = 0; i < length; i++) {
197 /* Write data byte */
198 writeb(data[i], i2c->base + MPC_I2C_DR);
199
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100200 result = i2c_wait(i2c, timeout, 1);
201 if (result < 0)
202 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
205 return 0;
206}
207
208static int mpc_read(struct mpc_i2c *i2c, int target,
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200209 u8 *data, int length, int restart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 unsigned timeout = i2c->adap.timeout;
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100212 int i, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 u32 flags = restart ? CCR_RSTA : 0;
214
215 /* Start with MEN */
216 if (!restart)
217 writeccr(i2c, CCR_MEN);
218 /* Switch to read - restart */
219 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
220 /* Write target address byte - this time with the read flag set */
221 writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
222
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100223 result = i2c_wait(i2c, timeout, 1);
224 if (result < 0)
225 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 if (length) {
228 if (length == 1)
229 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
230 else
231 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
232 /* Dummy read */
233 readb(i2c->base + MPC_I2C_DR);
234 }
235
236 for (i = 0; i < length; i++) {
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100237 result = i2c_wait(i2c, timeout, 0);
238 if (result < 0)
239 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /* Generate txack on next to last byte */
242 if (i == length - 2)
243 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
244 /* Generate stop on last byte */
245 if (i == length - 1)
246 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK);
247 data[i] = readb(i2c->base + MPC_I2C_DR);
248 }
249
250 return length;
251}
252
253static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
254{
255 struct i2c_msg *pmsg;
256 int i;
257 int ret = 0;
258 unsigned long orig_jiffies = jiffies;
259 struct mpc_i2c *i2c = i2c_get_adapdata(adap);
260
261 mpc_i2c_start(i2c);
262
263 /* Allow bus up to 1s to become not busy */
264 while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
265 if (signal_pending(current)) {
266 pr_debug("I2C: Interrupted\n");
Domen Puncer5af0e072007-08-14 18:37:14 +0200267 writeccr(i2c, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -EINTR;
269 }
270 if (time_after(jiffies, orig_jiffies + HZ)) {
271 pr_debug("I2C: timeout\n");
Domen Puncer254db9b2007-07-12 14:12:31 +0200272 if (readb(i2c->base + MPC_I2C_SR) ==
273 (CSR_MCF | CSR_MBB | CSR_RXAK))
274 mpc_i2c_fixup(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -EIO;
276 }
277 schedule();
278 }
279
280 for (i = 0; ret >= 0 && i < num; i++) {
281 pmsg = &msgs[i];
282 pr_debug("Doing %s %d bytes to 0x%02x - %d of %d messages\n",
283 pmsg->flags & I2C_M_RD ? "read" : "write",
284 pmsg->len, pmsg->addr, i + 1, num);
285 if (pmsg->flags & I2C_M_RD)
286 ret =
287 mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
288 else
289 ret =
290 mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
291 }
292 mpc_i2c_stop(i2c);
293 return (ret < 0) ? ret : num;
294}
295
296static u32 mpc_functionality(struct i2c_adapter *adap)
297{
298 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
299}
300
Jean Delvare8f9082c2006-09-03 22:39:46 +0200301static const struct i2c_algorithm mpc_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 .master_xfer = mpc_xfer,
303 .functionality = mpc_functionality,
304};
305
306static struct i2c_adapter mpc_ops = {
307 .owner = THIS_MODULE,
308 .name = "MPC adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 .algo = &mpc_algo,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100310 .timeout = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200313static int __devinit fsl_i2c_probe(struct of_device *op,
314 const struct of_device_id *match)
Kumar Gala8c86cb12005-07-27 11:43:26 -0700315{
316 int result = 0;
317 struct mpc_i2c *i2c;
Kumar Gala8c86cb12005-07-27 11:43:26 -0700318
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100319 i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
320 if (!i2c)
Kumar Gala8c86cb12005-07-27 11:43:26 -0700321 return -ENOMEM;
Kumar Gala8c86cb12005-07-27 11:43:26 -0700322
Jon Smirl0d1cde22008-06-30 19:01:26 -0400323 if (of_get_property(op->node, "dfsrr", NULL))
324 i2c->flags |= FSL_I2C_DEV_SEPARATE_DFSRR;
Jon Smirlf5fff362008-05-11 20:37:04 +0200325
Jon Smirl0d1cde22008-06-30 19:01:26 -0400326 if (of_device_is_compatible(op->node, "fsl,mpc5200-i2c") ||
327 of_device_is_compatible(op->node, "mpc5200-i2c"))
328 i2c->flags |= FSL_I2C_DEV_CLOCK_5200;
329
Kumar Gala8c86cb12005-07-27 11:43:26 -0700330 init_waitqueue_head(&i2c->queue);
331
Jon Smirl0d1cde22008-06-30 19:01:26 -0400332 i2c->base = of_iomap(op->node, 0);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700333 if (!i2c->base) {
334 printk(KERN_ERR "i2c-mpc - failed to map controller\n");
335 result = -ENOMEM;
336 goto fail_map;
337 }
338
Jon Smirl0d1cde22008-06-30 19:01:26 -0400339 i2c->irq = irq_of_parse_and_map(op->node, 0);
340 if (i2c->irq != NO_IRQ) { /* i2c->irq = NO_IRQ implies polling */
341 result = request_irq(i2c->irq, mpc_i2c_isr,
342 IRQF_SHARED, "i2c-mpc", i2c);
343 if (result < 0) {
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200344 printk(KERN_ERR
345 "i2c-mpc - failed to attach interrupt\n");
Jon Smirl0d1cde22008-06-30 19:01:26 -0400346 goto fail_request;
Kumar Gala8c86cb12005-07-27 11:43:26 -0700347 }
Jon Smirl0d1cde22008-06-30 19:01:26 -0400348 }
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200349
Kumar Gala8c86cb12005-07-27 11:43:26 -0700350 mpc_i2c_setclock(i2c);
Jon Smirl0d1cde22008-06-30 19:01:26 -0400351
352 dev_set_drvdata(&op->dev, i2c);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700353
354 i2c->adap = mpc_ops;
355 i2c_set_adapdata(&i2c->adap, i2c);
Jon Smirl0d1cde22008-06-30 19:01:26 -0400356 i2c->adap.dev.parent = &op->dev;
357
358 result = i2c_add_adapter(&i2c->adap);
359 if (result < 0) {
Kumar Gala8c86cb12005-07-27 11:43:26 -0700360 printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
361 goto fail_add;
362 }
Jon Smirl0d1cde22008-06-30 19:01:26 -0400363 of_register_i2c_devices(&i2c->adap, op->node);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700364
365 return result;
366
Jon Smirl0d1cde22008-06-30 19:01:26 -0400367 fail_add:
368 dev_set_drvdata(&op->dev, NULL);
369 free_irq(i2c->irq, i2c);
370 fail_request:
371 irq_dispose_mapping(i2c->irq);
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200372 iounmap(i2c->base);
Jon Smirl0d1cde22008-06-30 19:01:26 -0400373 fail_map:
Kumar Gala8c86cb12005-07-27 11:43:26 -0700374 kfree(i2c);
375 return result;
376};
377
Jon Smirl0d1cde22008-06-30 19:01:26 -0400378static int __devexit fsl_i2c_remove(struct of_device *op)
Kumar Gala8c86cb12005-07-27 11:43:26 -0700379{
Jon Smirl0d1cde22008-06-30 19:01:26 -0400380 struct mpc_i2c *i2c = dev_get_drvdata(&op->dev);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700381
382 i2c_del_adapter(&i2c->adap);
Jon Smirl0d1cde22008-06-30 19:01:26 -0400383 dev_set_drvdata(&op->dev, NULL);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700384
Jon Smirlf5fff362008-05-11 20:37:04 +0200385 if (i2c->irq != NO_IRQ)
Kumar Gala8c86cb12005-07-27 11:43:26 -0700386 free_irq(i2c->irq, i2c);
387
Jon Smirl0d1cde22008-06-30 19:01:26 -0400388 irq_dispose_mapping(i2c->irq);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700389 iounmap(i2c->base);
390 kfree(i2c);
391 return 0;
392};
393
Jon Smirl0d1cde22008-06-30 19:01:26 -0400394static const struct of_device_id mpc_i2c_of_match[] = {
395 {.compatible = "fsl-i2c",},
396 {},
397};
398MODULE_DEVICE_TABLE(of, mpc_i2c_of_match);
399
Kay Sieversadd8eda2008-04-22 22:16:49 +0200400
Kumar Gala8c86cb12005-07-27 11:43:26 -0700401/* Structure for a device driver */
Jon Smirl0d1cde22008-06-30 19:01:26 -0400402static struct of_platform_driver mpc_i2c_driver = {
403 .match_table = mpc_i2c_of_match,
404 .probe = fsl_i2c_probe,
405 .remove = __devexit_p(fsl_i2c_remove),
406 .driver = {
407 .owner = THIS_MODULE,
408 .name = DRV_NAME,
Russell King3ae5eae2005-11-09 22:32:44 +0000409 },
Kumar Gala8c86cb12005-07-27 11:43:26 -0700410};
411
412static int __init fsl_i2c_init(void)
413{
Jon Smirl0d1cde22008-06-30 19:01:26 -0400414 int rv;
415
416 rv = of_register_platform_driver(&mpc_i2c_driver);
417 if (rv)
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200418 printk(KERN_ERR DRV_NAME
Jon Smirl0d1cde22008-06-30 19:01:26 -0400419 " of_register_platform_driver failed (%i)\n", rv);
420 return rv;
Kumar Gala8c86cb12005-07-27 11:43:26 -0700421}
422
423static void __exit fsl_i2c_exit(void)
424{
Jon Smirl0d1cde22008-06-30 19:01:26 -0400425 of_unregister_platform_driver(&mpc_i2c_driver);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700426}
427
428module_init(fsl_i2c_init);
429module_exit(fsl_i2c_exit);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200432MODULE_DESCRIPTION("I2C-Bus adapter for MPC107 bridge and "
433 "MPC824x/85xx/52xx processors");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434MODULE_LICENSE("GPL");