blob: 9b38c92364640656d30e20c5887f0c43e56b48aa [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 {
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +020053 struct device *dev;
Al Viro7366d362005-04-25 18:32:12 -070054 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 u32 interrupt;
56 wait_queue_head_t queue;
57 struct i2c_adapter adap;
58 int irq;
59 u32 flags;
60};
61
Wolfgang Grandegger8101a302009-04-07 10:20:53 +020062static inline void writeccr(struct mpc_i2c *i2c, u32 x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 writeb(x, i2c->base + MPC_I2C_CR);
65}
66
David Howells7d12e782006-10-05 14:55:46 +010067static irqreturn_t mpc_i2c_isr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct mpc_i2c *i2c = dev_id;
70 if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
71 /* Read again to allow register to stabilise */
72 i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
73 writeb(0, i2c->base + MPC_I2C_SR);
Timur Tabi1ab082d2009-02-06 08:00:37 -060074 wake_up(&i2c->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76 return IRQ_HANDLED;
77}
78
Domen Puncer254db9b2007-07-12 14:12:31 +020079/* Sometimes 9th clock pulse isn't generated, and slave doesn't release
80 * the bus, because it wants to send ACK.
81 * Following sequence of enabling/disabling and sending start/stop generates
82 * the pulse, so it's all OK.
83 */
84static void mpc_i2c_fixup(struct mpc_i2c *i2c)
85{
86 writeccr(i2c, 0);
87 udelay(30);
88 writeccr(i2c, CCR_MEN);
89 udelay(30);
90 writeccr(i2c, CCR_MSTA | CCR_MTX);
91 udelay(30);
92 writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
93 udelay(30);
94 writeccr(i2c, CCR_MEN);
95 udelay(30);
96}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
99{
100 unsigned long orig_jiffies = jiffies;
101 u32 x;
102 int result = 0;
103
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200104 if (i2c->irq == NO_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
106 schedule();
107 if (time_after(jiffies, orig_jiffies + timeout)) {
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200108 dev_dbg(i2c->dev, "timeout\n");
Domen Puncer5af0e072007-08-14 18:37:14 +0200109 writeccr(i2c, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 result = -EIO;
111 break;
112 }
113 }
114 x = readb(i2c->base + MPC_I2C_SR);
115 writeb(0, i2c->base + MPC_I2C_SR);
116 } else {
117 /* Interrupt mode */
Timur Tabi1ab082d2009-02-06 08:00:37 -0600118 result = wait_event_timeout(i2c->queue,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100119 (i2c->interrupt & CSR_MIF), timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Timur Tabi1ab082d2009-02-06 08:00:37 -0600121 if (unlikely(!(i2c->interrupt & CSR_MIF))) {
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200122 dev_dbg(i2c->dev, "wait timeout\n");
Domen Puncer5af0e072007-08-14 18:37:14 +0200123 writeccr(i2c, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 result = -ETIMEDOUT;
125 }
126
127 x = i2c->interrupt;
128 i2c->interrupt = 0;
129 }
130
131 if (result < 0)
132 return result;
133
134 if (!(x & CSR_MCF)) {
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200135 dev_dbg(i2c->dev, "unfinished\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return -EIO;
137 }
138
139 if (x & CSR_MAL) {
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200140 dev_dbg(i2c->dev, "MAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -EIO;
142 }
143
144 if (writing && (x & CSR_RXAK)) {
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200145 dev_dbg(i2c->dev, "No RXAK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /* generate stop */
147 writeccr(i2c, CCR_MEN);
148 return -EIO;
149 }
150 return 0;
151}
152
153static void mpc_i2c_setclock(struct mpc_i2c *i2c)
154{
155 /* Set clock and filters */
156 if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) {
157 writeb(0x31, i2c->base + MPC_I2C_FDR);
158 writeb(0x10, i2c->base + MPC_I2C_DFSRR);
159 } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200)
160 writeb(0x3f, i2c->base + MPC_I2C_FDR);
161 else
162 writel(0x1031, i2c->base + MPC_I2C_FDR);
163}
164
165static void mpc_i2c_start(struct mpc_i2c *i2c)
166{
167 /* Clear arbitration */
168 writeb(0, i2c->base + MPC_I2C_SR);
169 /* Start with MEN */
170 writeccr(i2c, CCR_MEN);
171}
172
173static void mpc_i2c_stop(struct mpc_i2c *i2c)
174{
175 writeccr(i2c, CCR_MEN);
176}
177
178static int mpc_write(struct mpc_i2c *i2c, int target,
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200179 const u8 *data, int length, int restart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100181 int i, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned timeout = i2c->adap.timeout;
183 u32 flags = restart ? CCR_RSTA : 0;
184
185 /* Start with MEN */
186 if (!restart)
187 writeccr(i2c, CCR_MEN);
188 /* Start as master */
189 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
190 /* Write target byte */
191 writeb((target << 1), i2c->base + MPC_I2C_DR);
192
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100193 result = i2c_wait(i2c, timeout, 1);
194 if (result < 0)
195 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 for (i = 0; i < length; i++) {
198 /* Write data byte */
199 writeb(data[i], i2c->base + MPC_I2C_DR);
200
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100201 result = i2c_wait(i2c, timeout, 1);
202 if (result < 0)
203 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 return 0;
207}
208
209static int mpc_read(struct mpc_i2c *i2c, int target,
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200210 u8 *data, int length, int restart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 unsigned timeout = i2c->adap.timeout;
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100213 int i, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 u32 flags = restart ? CCR_RSTA : 0;
215
216 /* Start with MEN */
217 if (!restart)
218 writeccr(i2c, CCR_MEN);
219 /* Switch to read - restart */
220 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
221 /* Write target address byte - this time with the read flag set */
222 writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
223
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100224 result = i2c_wait(i2c, timeout, 1);
225 if (result < 0)
226 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (length) {
229 if (length == 1)
230 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
231 else
232 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
233 /* Dummy read */
234 readb(i2c->base + MPC_I2C_DR);
235 }
236
237 for (i = 0; i < length; i++) {
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100238 result = i2c_wait(i2c, timeout, 0);
239 if (result < 0)
240 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* Generate txack on next to last byte */
243 if (i == length - 2)
244 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
245 /* Generate stop on last byte */
246 if (i == length - 1)
247 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK);
248 data[i] = readb(i2c->base + MPC_I2C_DR);
249 }
250
251 return length;
252}
253
254static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
255{
256 struct i2c_msg *pmsg;
257 int i;
258 int ret = 0;
259 unsigned long orig_jiffies = jiffies;
260 struct mpc_i2c *i2c = i2c_get_adapdata(adap);
261
262 mpc_i2c_start(i2c);
263
264 /* Allow bus up to 1s to become not busy */
265 while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
266 if (signal_pending(current)) {
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200267 dev_dbg(i2c->dev, "Interrupted\n");
Domen Puncer5af0e072007-08-14 18:37:14 +0200268 writeccr(i2c, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -EINTR;
270 }
271 if (time_after(jiffies, orig_jiffies + HZ)) {
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200272 dev_dbg(i2c->dev, "timeout\n");
Domen Puncer254db9b2007-07-12 14:12:31 +0200273 if (readb(i2c->base + MPC_I2C_SR) ==
274 (CSR_MCF | CSR_MBB | CSR_RXAK))
275 mpc_i2c_fixup(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EIO;
277 }
278 schedule();
279 }
280
281 for (i = 0; ret >= 0 && i < num; i++) {
282 pmsg = &msgs[i];
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200283 dev_dbg(i2c->dev,
284 "Doing %s %d bytes to 0x%02x - %d of %d messages\n",
285 pmsg->flags & I2C_M_RD ? "read" : "write",
286 pmsg->len, pmsg->addr, i + 1, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (pmsg->flags & I2C_M_RD)
288 ret =
289 mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
290 else
291 ret =
292 mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
293 }
294 mpc_i2c_stop(i2c);
295 return (ret < 0) ? ret : num;
296}
297
298static u32 mpc_functionality(struct i2c_adapter *adap)
299{
300 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
301}
302
Jean Delvare8f9082c2006-09-03 22:39:46 +0200303static const struct i2c_algorithm mpc_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 .master_xfer = mpc_xfer,
305 .functionality = mpc_functionality,
306};
307
308static struct i2c_adapter mpc_ops = {
309 .owner = THIS_MODULE,
310 .name = "MPC adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .algo = &mpc_algo,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100312 .timeout = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200315static int __devinit fsl_i2c_probe(struct of_device *op,
316 const struct of_device_id *match)
Kumar Gala8c86cb12005-07-27 11:43:26 -0700317{
318 int result = 0;
319 struct mpc_i2c *i2c;
Kumar Gala8c86cb12005-07-27 11:43:26 -0700320
Jon Smirl4bd28eb2008-01-27 18:14:52 +0100321 i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
322 if (!i2c)
Kumar Gala8c86cb12005-07-27 11:43:26 -0700323 return -ENOMEM;
Kumar Gala8c86cb12005-07-27 11:43:26 -0700324
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200325 i2c->dev = &op->dev; /* for debug and error output */
326
Jon Smirl0d1cde22008-06-30 19:01:26 -0400327 if (of_get_property(op->node, "dfsrr", NULL))
328 i2c->flags |= FSL_I2C_DEV_SEPARATE_DFSRR;
Jon Smirlf5fff362008-05-11 20:37:04 +0200329
Jon Smirl0d1cde22008-06-30 19:01:26 -0400330 if (of_device_is_compatible(op->node, "fsl,mpc5200-i2c") ||
331 of_device_is_compatible(op->node, "mpc5200-i2c"))
332 i2c->flags |= FSL_I2C_DEV_CLOCK_5200;
333
Kumar Gala8c86cb12005-07-27 11:43:26 -0700334 init_waitqueue_head(&i2c->queue);
335
Jon Smirl0d1cde22008-06-30 19:01:26 -0400336 i2c->base = of_iomap(op->node, 0);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700337 if (!i2c->base) {
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200338 dev_err(i2c->dev, "failed to map controller\n");
Kumar Gala8c86cb12005-07-27 11:43:26 -0700339 result = -ENOMEM;
340 goto fail_map;
341 }
342
Jon Smirl0d1cde22008-06-30 19:01:26 -0400343 i2c->irq = irq_of_parse_and_map(op->node, 0);
344 if (i2c->irq != NO_IRQ) { /* i2c->irq = NO_IRQ implies polling */
345 result = request_irq(i2c->irq, mpc_i2c_isr,
346 IRQF_SHARED, "i2c-mpc", i2c);
347 if (result < 0) {
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200348 dev_err(i2c->dev, "failed to attach interrupt\n");
Jon Smirl0d1cde22008-06-30 19:01:26 -0400349 goto fail_request;
Kumar Gala8c86cb12005-07-27 11:43:26 -0700350 }
Jon Smirl0d1cde22008-06-30 19:01:26 -0400351 }
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200352
Kumar Gala8c86cb12005-07-27 11:43:26 -0700353 mpc_i2c_setclock(i2c);
Jon Smirl0d1cde22008-06-30 19:01:26 -0400354
355 dev_set_drvdata(&op->dev, i2c);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700356
357 i2c->adap = mpc_ops;
358 i2c_set_adapdata(&i2c->adap, i2c);
Jon Smirl0d1cde22008-06-30 19:01:26 -0400359 i2c->adap.dev.parent = &op->dev;
360
361 result = i2c_add_adapter(&i2c->adap);
362 if (result < 0) {
Wolfgang Grandegger54377cd2009-04-07 10:20:54 +0200363 dev_err(i2c->dev, "failed to add adapter\n");
Kumar Gala8c86cb12005-07-27 11:43:26 -0700364 goto fail_add;
365 }
Jon Smirl0d1cde22008-06-30 19:01:26 -0400366 of_register_i2c_devices(&i2c->adap, op->node);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700367
368 return result;
369
Jon Smirl0d1cde22008-06-30 19:01:26 -0400370 fail_add:
371 dev_set_drvdata(&op->dev, NULL);
372 free_irq(i2c->irq, i2c);
373 fail_request:
374 irq_dispose_mapping(i2c->irq);
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200375 iounmap(i2c->base);
Jon Smirl0d1cde22008-06-30 19:01:26 -0400376 fail_map:
Kumar Gala8c86cb12005-07-27 11:43:26 -0700377 kfree(i2c);
378 return result;
379};
380
Jon Smirl0d1cde22008-06-30 19:01:26 -0400381static int __devexit fsl_i2c_remove(struct of_device *op)
Kumar Gala8c86cb12005-07-27 11:43:26 -0700382{
Jon Smirl0d1cde22008-06-30 19:01:26 -0400383 struct mpc_i2c *i2c = dev_get_drvdata(&op->dev);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700384
385 i2c_del_adapter(&i2c->adap);
Jon Smirl0d1cde22008-06-30 19:01:26 -0400386 dev_set_drvdata(&op->dev, NULL);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700387
Jon Smirlf5fff362008-05-11 20:37:04 +0200388 if (i2c->irq != NO_IRQ)
Kumar Gala8c86cb12005-07-27 11:43:26 -0700389 free_irq(i2c->irq, i2c);
390
Jon Smirl0d1cde22008-06-30 19:01:26 -0400391 irq_dispose_mapping(i2c->irq);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700392 iounmap(i2c->base);
393 kfree(i2c);
394 return 0;
395};
396
Jon Smirl0d1cde22008-06-30 19:01:26 -0400397static const struct of_device_id mpc_i2c_of_match[] = {
398 {.compatible = "fsl-i2c",},
399 {},
400};
401MODULE_DEVICE_TABLE(of, mpc_i2c_of_match);
402
Kay Sieversadd8eda2008-04-22 22:16:49 +0200403
Kumar Gala8c86cb12005-07-27 11:43:26 -0700404/* Structure for a device driver */
Jon Smirl0d1cde22008-06-30 19:01:26 -0400405static struct of_platform_driver mpc_i2c_driver = {
406 .match_table = mpc_i2c_of_match,
407 .probe = fsl_i2c_probe,
408 .remove = __devexit_p(fsl_i2c_remove),
409 .driver = {
410 .owner = THIS_MODULE,
411 .name = DRV_NAME,
Russell King3ae5eae2005-11-09 22:32:44 +0000412 },
Kumar Gala8c86cb12005-07-27 11:43:26 -0700413};
414
415static int __init fsl_i2c_init(void)
416{
Jon Smirl0d1cde22008-06-30 19:01:26 -0400417 int rv;
418
419 rv = of_register_platform_driver(&mpc_i2c_driver);
420 if (rv)
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200421 printk(KERN_ERR DRV_NAME
Jon Smirl0d1cde22008-06-30 19:01:26 -0400422 " of_register_platform_driver failed (%i)\n", rv);
423 return rv;
Kumar Gala8c86cb12005-07-27 11:43:26 -0700424}
425
426static void __exit fsl_i2c_exit(void)
427{
Jon Smirl0d1cde22008-06-30 19:01:26 -0400428 of_unregister_platform_driver(&mpc_i2c_driver);
Kumar Gala8c86cb12005-07-27 11:43:26 -0700429}
430
431module_init(fsl_i2c_init);
432module_exit(fsl_i2c_exit);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
Wolfgang Grandegger8101a302009-04-07 10:20:53 +0200435MODULE_DESCRIPTION("I2C-Bus adapter for MPC107 bridge and "
436 "MPC824x/85xx/52xx processors");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437MODULE_LICENSE("GPL");