blob: 961c5f42d956f11fe1e87b95aaf50339959e7014 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * drivers/i2c/busses/i2c-ibm_iic.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Support for the IIC peripheral on IBM PPC 4xx
5 *
6 * Copyright (c) 2003, 2004 Zultys Technologies.
7 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
8 *
Sean MacLennan838349b2008-04-22 22:16:47 +02009 * Copyright (c) 2008 PIKA Technologies
10 * Sean MacLennan <smaclennan@pikatech.com>
11 *
Stefan Roese217bcec2008-01-27 18:14:45 +010012 * Based on original work by
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Ian DaSilva <idasilva@mvista.com>
14 * Armin Kuster <akuster@mvista.com>
15 * Matt Porter <mporter@mvista.com>
16 *
17 * Copyright 2000-2003 MontaVista Software Inc.
18 *
19 * Original driver version was highly leveraged from i2c-elektor.c
20 *
21 * Copyright 1995-97 Simon G. Vogl
22 * 1998-99 Hans Berglund
23 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020024 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * and even Frodo Looijaard <frodol@dds.nl>
26 *
27 * This program is free software; you can redistribute it and/or modify it
28 * under the terms of the GNU General Public License as published by the
29 * Free Software Foundation; either version 2 of the License, or (at your
30 * option) any later version.
31 *
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/kernel.h>
36#include <linux/ioport.h>
37#include <linux/delay.h>
38#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/interrupt.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010040#include <linux/sched/signal.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/irq.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020043#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/i2c.h>
Rob Herring5af50732013-09-17 14:28:33 -050045#include <linux/of_address.h>
46#include <linux/of_irq.h>
Sean MacLennan838349b2008-04-22 22:16:47 +020047#include <linux/of_platform.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include "i2c-ibm_iic.h"
50
Sean MacLennan838349b2008-04-22 22:16:47 +020051#define DRIVER_VERSION "2.2"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53MODULE_DESCRIPTION("IBM IIC driver v" DRIVER_VERSION);
54MODULE_LICENSE("GPL");
55
Rusty Russell90ab5ee2012-01-13 09:32:20 +103056static bool iic_force_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057module_param(iic_force_poll, bool, 0);
58MODULE_PARM_DESC(iic_force_poll, "Force polling mode");
59
Rusty Russell90ab5ee2012-01-13 09:32:20 +103060static bool iic_force_fast;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061module_param(iic_force_fast, bool, 0);
Paul Mundt852fb2a2008-04-11 12:07:04 +020062MODULE_PARM_DESC(iic_force_fast, "Force fast mode (400 kHz)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define DBG_LEVEL 0
65
66#ifdef DBG
67#undef DBG
68#endif
69
70#ifdef DBG2
71#undef DBG2
72#endif
73
74#if DBG_LEVEL > 0
75# define DBG(f,x...) printk(KERN_DEBUG "ibm-iic" f, ##x)
76#else
77# define DBG(f,x...) ((void)0)
78#endif
79#if DBG_LEVEL > 1
80# define DBG2(f,x...) DBG(f, ##x)
81#else
82# define DBG2(f,x...) ((void)0)
83#endif
84#if DBG_LEVEL > 2
85static void dump_iic_regs(const char* header, struct ibm_iic_private* dev)
86{
87 volatile struct iic_regs __iomem *iic = dev->vaddr;
88 printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header);
Joe Perchesad361c92009-07-06 13:05:40 -070089 printk(KERN_DEBUG
90 " cntl = 0x%02x, mdcntl = 0x%02x\n"
91 " sts = 0x%02x, extsts = 0x%02x\n"
92 " clkdiv = 0x%02x, xfrcnt = 0x%02x\n"
93 " xtcntlss = 0x%02x, directcntl = 0x%02x\n",
Stefan Roese217bcec2008-01-27 18:14:45 +010094 in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts),
95 in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 in_8(&iic->xtcntlss), in_8(&iic->directcntl));
97}
98# define DUMP_REGS(h,dev) dump_iic_regs((h),(dev))
99#else
100# define DUMP_REGS(h,dev) ((void)0)
101#endif
102
103/* Bus timings (in ns) for bit-banging */
Stephen Rothwellf199b262015-12-16 15:48:13 +1100104static struct ibm_iic_timings {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int hd_sta;
106 unsigned int su_sto;
107 unsigned int low;
108 unsigned int high;
109 unsigned int buf;
110} timings [] = {
111/* Standard mode (100 KHz) */
112{
113 .hd_sta = 4000,
114 .su_sto = 4000,
115 .low = 4700,
116 .high = 4000,
117 .buf = 4700,
118},
119/* Fast mode (400 KHz) */
120{
121 .hd_sta = 600,
122 .su_sto = 600,
123 .low = 1300,
124 .high = 600,
125 .buf = 1300,
126}};
127
128/* Enable/disable interrupt generation */
129static inline void iic_interrupt_mode(struct ibm_iic_private* dev, int enable)
130{
131 out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0);
132}
Stefan Roese217bcec2008-01-27 18:14:45 +0100133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
135 * Initialize IIC interface.
136 */
137static void iic_dev_init(struct ibm_iic_private* dev)
138{
139 volatile struct iic_regs __iomem *iic = dev->vaddr;
140
141 DBG("%d: init\n", dev->idx);
Stefan Roese217bcec2008-01-27 18:14:45 +0100142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* Clear master address */
144 out_8(&iic->lmadr, 0);
145 out_8(&iic->hmadr, 0);
146
147 /* Clear slave address */
148 out_8(&iic->lsadr, 0);
149 out_8(&iic->hsadr, 0);
150
151 /* Clear status & extended status */
152 out_8(&iic->sts, STS_SCMP | STS_IRQA);
153 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA
154 | EXTSTS_ICT | EXTSTS_XFRA);
155
156 /* Set clock divider */
157 out_8(&iic->clkdiv, dev->clckdiv);
158
159 /* Clear transfer count */
160 out_8(&iic->xfrcnt, 0);
161
162 /* Clear extended control and status */
163 out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC
164 | XTCNTLSS_SWS);
165
166 /* Clear control register */
167 out_8(&iic->cntl, 0);
Stefan Roese217bcec2008-01-27 18:14:45 +0100168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* Enable interrupts if possible */
170 iic_interrupt_mode(dev, dev->irq >= 0);
171
172 /* Set mode control */
173 out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS
174 | (dev->fast_mode ? MDCNTL_FSM : 0));
175
176 DUMP_REGS("iic_init", dev);
177}
178
Stefan Roese217bcec2008-01-27 18:14:45 +0100179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Reset IIC interface
181 */
182static void iic_dev_reset(struct ibm_iic_private* dev)
183{
184 volatile struct iic_regs __iomem *iic = dev->vaddr;
185 int i;
186 u8 dc;
Stefan Roese217bcec2008-01-27 18:14:45 +0100187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 DBG("%d: soft reset\n", dev->idx);
189 DUMP_REGS("reset", dev);
Stefan Roese217bcec2008-01-27 18:14:45 +0100190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Place chip in the reset state */
192 out_8(&iic->xtcntlss, XTCNTLSS_SRST);
Stefan Roese217bcec2008-01-27 18:14:45 +0100193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Check if bus is free */
Stefan Roese217bcec2008-01-27 18:14:45 +0100195 dc = in_8(&iic->directcntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!DIRCTNL_FREE(dc)){
197 DBG("%d: trying to regain bus control\n", dev->idx);
Stefan Roese217bcec2008-01-27 18:14:45 +0100198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Try to set bus free state */
Stefan Roese217bcec2008-01-27 18:14:45 +0100200 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Wait until we regain bus control */
203 for (i = 0; i < 100; ++i){
204 dc = in_8(&iic->directcntl);
205 if (DIRCTNL_FREE(dc))
206 break;
Stefan Roese217bcec2008-01-27 18:14:45 +0100207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* Toggle SCL line */
209 dc ^= DIRCNTL_SCC;
210 out_8(&iic->directcntl, dc);
211 udelay(10);
212 dc ^= DIRCNTL_SCC;
213 out_8(&iic->directcntl, dc);
Stefan Roese217bcec2008-01-27 18:14:45 +0100214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* be nice */
216 cond_resched();
217 }
218 }
Stefan Roese217bcec2008-01-27 18:14:45 +0100219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* Remove reset */
221 out_8(&iic->xtcntlss, 0);
Stefan Roese217bcec2008-01-27 18:14:45 +0100222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* Reinitialize interface */
224 iic_dev_init(dev);
225}
226
227/*
228 * Do 0-length transaction using bit-banging through IIC_DIRECTCNTL register.
229 */
230
231/* Wait for SCL and/or SDA to be high */
232static int iic_dc_wait(volatile struct iic_regs __iomem *iic, u8 mask)
233{
234 unsigned long x = jiffies + HZ / 28 + 2;
235 while ((in_8(&iic->directcntl) & mask) != mask){
236 if (unlikely(time_after(jiffies, x)))
237 return -1;
238 cond_resched();
239 }
240 return 0;
241}
242
243static int iic_smbus_quick(struct ibm_iic_private* dev, const struct i2c_msg* p)
244{
245 volatile struct iic_regs __iomem *iic = dev->vaddr;
Stephen Rothwellf199b262015-12-16 15:48:13 +1100246 const struct ibm_iic_timings *t = &timings[dev->fast_mode ? 1 : 0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 u8 mask, v, sda;
248 int i, res;
249
250 /* Only 7-bit addresses are supported */
251 if (unlikely(p->flags & I2C_M_TEN)){
252 DBG("%d: smbus_quick - 10 bit addresses are not supported\n",
253 dev->idx);
254 return -EINVAL;
255 }
256
257 DBG("%d: smbus_quick(0x%02x)\n", dev->idx, p->addr);
258
259 /* Reset IIC interface */
260 out_8(&iic->xtcntlss, XTCNTLSS_SRST);
261
262 /* Wait for bus to become free */
263 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
264 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSDA | DIRCNTL_MSC)))
265 goto err;
266 ndelay(t->buf);
267
268 /* START */
269 out_8(&iic->directcntl, DIRCNTL_SCC);
270 sda = 0;
271 ndelay(t->hd_sta);
272
273 /* Send address */
Wolfram Sang73fef212016-04-03 20:44:51 +0200274 v = i2c_8bit_addr_from_msg(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 for (i = 0, mask = 0x80; i < 8; ++i, mask >>= 1){
276 out_8(&iic->directcntl, sda);
277 ndelay(t->low / 2);
278 sda = (v & mask) ? DIRCNTL_SDAC : 0;
279 out_8(&iic->directcntl, sda);
280 ndelay(t->low / 2);
281
282 out_8(&iic->directcntl, DIRCNTL_SCC | sda);
283 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
284 goto err;
285 ndelay(t->high);
286 }
287
288 /* ACK */
289 out_8(&iic->directcntl, sda);
290 ndelay(t->low / 2);
291 out_8(&iic->directcntl, DIRCNTL_SDAC);
292 ndelay(t->low / 2);
293 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
294 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
295 goto err;
296 res = (in_8(&iic->directcntl) & DIRCNTL_MSDA) ? -EREMOTEIO : 1;
297 ndelay(t->high);
298
299 /* STOP */
300 out_8(&iic->directcntl, 0);
301 ndelay(t->low);
302 out_8(&iic->directcntl, DIRCNTL_SCC);
303 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
304 goto err;
305 ndelay(t->su_sto);
306 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
307
308 ndelay(t->buf);
309
310 DBG("%d: smbus_quick -> %s\n", dev->idx, res ? "NACK" : "ACK");
311out:
312 /* Remove reset */
313 out_8(&iic->xtcntlss, 0);
314
315 /* Reinitialize interface */
316 iic_dev_init(dev);
317
318 return res;
319err:
320 DBG("%d: smbus_quick - bus is stuck\n", dev->idx);
321 res = -EREMOTEIO;
322 goto out;
323}
324
325/*
326 * IIC interrupt handler
327 */
David Howells7d12e782006-10-05 14:55:46 +0100328static irqreturn_t iic_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 struct ibm_iic_private* dev = (struct ibm_iic_private*)dev_id;
331 volatile struct iic_regs __iomem *iic = dev->vaddr;
Stefan Roese217bcec2008-01-27 18:14:45 +0100332
333 DBG2("%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 dev->idx, in_8(&iic->sts), in_8(&iic->extsts));
Stefan Roese217bcec2008-01-27 18:14:45 +0100335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* Acknowledge IRQ and wakeup iic_wait_for_tc */
337 out_8(&iic->sts, STS_IRQA | STS_SCMP);
338 wake_up_interruptible(&dev->wq);
Stefan Roese217bcec2008-01-27 18:14:45 +0100339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return IRQ_HANDLED;
341}
342
343/*
344 * Get master transfer result and clear errors if any.
345 * Returns the number of actually transferred bytes or error (<0)
346 */
347static int iic_xfer_result(struct ibm_iic_private* dev)
348{
Stefan Roese217bcec2008-01-27 18:14:45 +0100349 volatile struct iic_regs __iomem *iic = dev->vaddr;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (unlikely(in_8(&iic->sts) & STS_ERR)){
Stefan Roese217bcec2008-01-27 18:14:45 +0100352 DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 in_8(&iic->extsts));
Stefan Roese217bcec2008-01-27 18:14:45 +0100354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /* Clear errors and possible pending IRQs */
Stefan Roese217bcec2008-01-27 18:14:45 +0100356 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 EXTSTS_LA | EXTSTS_ICT | EXTSTS_XFRA);
Stefan Roese217bcec2008-01-27 18:14:45 +0100358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* Flush master data buffer */
360 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
Stefan Roese217bcec2008-01-27 18:14:45 +0100361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Is bus free?
363 * If error happened during combined xfer
364 * IIC interface is usually stuck in some strange
365 * state, the only way out - soft reset.
366 */
367 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
368 DBG("%d: bus is stuck, resetting\n", dev->idx);
369 iic_dev_reset(dev);
370 }
371 return -EREMOTEIO;
372 }
373 else
374 return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK;
375}
376
377/*
378 * Try to abort active transfer.
379 */
380static void iic_abort_xfer(struct ibm_iic_private* dev)
381{
382 volatile struct iic_regs __iomem *iic = dev->vaddr;
383 unsigned long x;
Stefan Roese217bcec2008-01-27 18:14:45 +0100384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 DBG("%d: iic_abort_xfer\n", dev->idx);
Stefan Roese217bcec2008-01-27 18:14:45 +0100386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 out_8(&iic->cntl, CNTL_HMT);
Stefan Roese217bcec2008-01-27 18:14:45 +0100388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
390 * Wait for the abort command to complete.
391 * It's not worth to be optimized, just poll (timeout >= 1 tick)
392 */
393 x = jiffies + 2;
394 while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
395 if (time_after(jiffies, x)){
396 DBG("%d: abort timeout, resetting...\n", dev->idx);
397 iic_dev_reset(dev);
398 return;
399 }
400 schedule();
401 }
402
403 /* Just to clear errors */
404 iic_xfer_result(dev);
405}
406
407/*
408 * Wait for master transfer to complete.
409 * It puts current process to sleep until we get interrupt or timeout expires.
410 * Returns the number of transferred bytes or error (<0)
411 */
412static int iic_wait_for_tc(struct ibm_iic_private* dev){
Stefan Roese217bcec2008-01-27 18:14:45 +0100413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 volatile struct iic_regs __iomem *iic = dev->vaddr;
415 int ret = 0;
Stefan Roese217bcec2008-01-27 18:14:45 +0100416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (dev->irq >= 0){
418 /* Interrupt mode */
Stefan Roese217bcec2008-01-27 18:14:45 +0100419 ret = wait_event_interruptible_timeout(dev->wq,
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100420 !(in_8(&iic->sts) & STS_PT), dev->adap.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 if (unlikely(ret < 0))
423 DBG("%d: wait interrupted\n", dev->idx);
424 else if (unlikely(in_8(&iic->sts) & STS_PT)){
425 DBG("%d: wait timeout\n", dev->idx);
426 ret = -ETIMEDOUT;
427 }
428 }
429 else {
430 /* Polling mode */
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100431 unsigned long x = jiffies + dev->adap.timeout;
Stefan Roese217bcec2008-01-27 18:14:45 +0100432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 while (in_8(&iic->sts) & STS_PT){
434 if (unlikely(time_after(jiffies, x))){
435 DBG("%d: poll timeout\n", dev->idx);
436 ret = -ETIMEDOUT;
437 break;
438 }
Stefan Roese217bcec2008-01-27 18:14:45 +0100439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (unlikely(signal_pending(current))){
441 DBG("%d: poll interrupted\n", dev->idx);
442 ret = -ERESTARTSYS;
443 break;
444 }
445 schedule();
Stefan Roese217bcec2008-01-27 18:14:45 +0100446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Stefan Roese217bcec2008-01-27 18:14:45 +0100448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (unlikely(ret < 0))
450 iic_abort_xfer(dev);
451 else
452 ret = iic_xfer_result(dev);
Stefan Roese217bcec2008-01-27 18:14:45 +0100453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret);
Stefan Roese217bcec2008-01-27 18:14:45 +0100455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return ret;
457}
458
459/*
460 * Low level master transfer routine
461 */
Stefan Roese217bcec2008-01-27 18:14:45 +0100462static int iic_xfer_bytes(struct ibm_iic_private* dev, struct i2c_msg* pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int combined_xfer)
464{
465 volatile struct iic_regs __iomem *iic = dev->vaddr;
466 char* buf = pm->buf;
467 int i, j, loops, ret = 0;
468 int len = pm->len;
469
470 u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT;
471 if (pm->flags & I2C_M_RD)
472 cntl |= CNTL_RW;
Stefan Roese217bcec2008-01-27 18:14:45 +0100473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 loops = (len + 3) / 4;
475 for (i = 0; i < loops; ++i, len -= 4){
476 int count = len > 4 ? 4 : len;
477 u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT);
Stefan Roese217bcec2008-01-27 18:14:45 +0100478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (!(cntl & CNTL_RW))
480 for (j = 0; j < count; ++j)
481 out_8((void __iomem *)&iic->mdbuf, *buf++);
Stefan Roese217bcec2008-01-27 18:14:45 +0100482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (i < loops - 1)
484 cmd |= CNTL_CHT;
485 else if (combined_xfer)
486 cmd |= CNTL_RPST;
Stefan Roese217bcec2008-01-27 18:14:45 +0100487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd);
Stefan Roese217bcec2008-01-27 18:14:45 +0100489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* Start transfer */
491 out_8(&iic->cntl, cmd);
Stefan Roese217bcec2008-01-27 18:14:45 +0100492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* Wait for completion */
494 ret = iic_wait_for_tc(dev);
495
496 if (unlikely(ret < 0))
497 break;
498 else if (unlikely(ret != count)){
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300499 DBG("%d: xfer_bytes, requested %d, transferred %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 dev->idx, count, ret);
Stefan Roese217bcec2008-01-27 18:14:45 +0100501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* If it's not a last part of xfer, abort it */
503 if (combined_xfer || (i < loops - 1))
504 iic_abort_xfer(dev);
Stefan Roese217bcec2008-01-27 18:14:45 +0100505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 ret = -EREMOTEIO;
Stefan Roese217bcec2008-01-27 18:14:45 +0100507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Stefan Roese217bcec2008-01-27 18:14:45 +0100509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (cntl & CNTL_RW)
511 for (j = 0; j < count; ++j)
512 *buf++ = in_8((void __iomem *)&iic->mdbuf);
513 }
Stefan Roese217bcec2008-01-27 18:14:45 +0100514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return ret > 0 ? 0 : ret;
516}
517
518/*
519 * Set target slave address for master transfer
520 */
521static inline void iic_address(struct ibm_iic_private* dev, struct i2c_msg* msg)
522{
523 volatile struct iic_regs __iomem *iic = dev->vaddr;
524 u16 addr = msg->addr;
Stefan Roese217bcec2008-01-27 18:14:45 +0100525
526 DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 addr, msg->flags & I2C_M_TEN ? 10 : 7);
Stefan Roese217bcec2008-01-27 18:14:45 +0100528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (msg->flags & I2C_M_TEN){
530 out_8(&iic->cntl, CNTL_AMD);
531 out_8(&iic->lmadr, addr);
532 out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06));
533 }
534 else {
535 out_8(&iic->cntl, 0);
536 out_8(&iic->lmadr, addr << 1);
537 }
538}
539
540static inline int iic_invalid_address(const struct i2c_msg* p)
541{
542 return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f));
543}
544
Stefan Roese217bcec2008-01-27 18:14:45 +0100545static inline int iic_address_neq(const struct i2c_msg* p1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 const struct i2c_msg* p2)
547{
Stefan Roese217bcec2008-01-27 18:14:45 +0100548 return (p1->addr != p2->addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 || ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN));
Stefan Roese217bcec2008-01-27 18:14:45 +0100550}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552/*
Stefan Roese217bcec2008-01-27 18:14:45 +0100553 * Generic master transfer entrypoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * Returns the number of processed messages or error (<0)
555 */
556static int iic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
557{
558 struct ibm_iic_private* dev = (struct ibm_iic_private*)(i2c_get_adapdata(adap));
559 volatile struct iic_regs __iomem *iic = dev->vaddr;
560 int i, ret = 0;
Stefan Roese217bcec2008-01-27 18:14:45 +0100561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num);
Stefan Roese217bcec2008-01-27 18:14:45 +0100563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (!num)
565 return 0;
Stefan Roese217bcec2008-01-27 18:14:45 +0100566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Check the sanity of the passed messages.
568 * Uhh, generic i2c layer is more suitable place for such code...
569 */
570 if (unlikely(iic_invalid_address(&msgs[0]))){
Stefan Roese217bcec2008-01-27 18:14:45 +0100571 DBG("%d: invalid address 0x%03x (%d-bit)\n", dev->idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 msgs[0].addr, msgs[0].flags & I2C_M_TEN ? 10 : 7);
573 return -EINVAL;
Stefan Roese217bcec2008-01-27 18:14:45 +0100574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 for (i = 0; i < num; ++i){
576 if (unlikely(msgs[i].len <= 0)){
577 if (num == 1 && !msgs[0].len){
578 /* Special case for I2C_SMBUS_QUICK emulation.
579 * IBM IIC doesn't support 0-length transactions
580 * so we have to emulate them using bit-banging.
581 */
582 return iic_smbus_quick(dev, &msgs[0]);
583 }
Stefan Roese217bcec2008-01-27 18:14:45 +0100584 DBG("%d: invalid len %d in msg[%d]\n", dev->idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 msgs[i].len, i);
586 return -EINVAL;
587 }
588 if (unlikely(iic_address_neq(&msgs[0], &msgs[i]))){
589 DBG("%d: invalid addr in msg[%d]\n", dev->idx, i);
590 return -EINVAL;
591 }
592 }
Stefan Roese217bcec2008-01-27 18:14:45 +0100593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* Check bus state */
595 if (unlikely((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE)){
596 DBG("%d: iic_xfer, bus is not free\n", dev->idx);
Stefan Roese217bcec2008-01-27 18:14:45 +0100597
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300598 /* Usually it means something serious has happened.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * We *cannot* have unfinished previous transfer
600 * so it doesn't make any sense to try to stop it.
Stefan Roese217bcec2008-01-27 18:14:45 +0100601 * Probably we were not able to recover from the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * previous error.
603 * The only *reasonable* thing I can think of here
604 * is soft reset. --ebs
605 */
606 iic_dev_reset(dev);
Stefan Roese217bcec2008-01-27 18:14:45 +0100607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
609 DBG("%d: iic_xfer, bus is still not free\n", dev->idx);
610 return -EREMOTEIO;
611 }
Stefan Roese217bcec2008-01-27 18:14:45 +0100612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 else {
614 /* Flush master data buffer (just in case) */
615 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
616 }
Stefan Roese217bcec2008-01-27 18:14:45 +0100617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* Load slave address */
619 iic_address(dev, &msgs[0]);
Stefan Roese217bcec2008-01-27 18:14:45 +0100620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* Do real transfer */
622 for (i = 0; i < num && !ret; ++i)
623 ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1);
624
625 return ret < 0 ? ret : num;
626}
627
628static u32 iic_func(struct i2c_adapter *adap)
629{
630 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
631}
632
Jean Delvare8f9082c2006-09-03 22:39:46 +0200633static const struct i2c_algorithm iic_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 .master_xfer = iic_xfer,
635 .functionality = iic_func
636};
637
638/*
639 * Calculates IICx_CLCKDIV value for a specific OPB clock frequency
640 */
641static inline u8 iic_clckdiv(unsigned int opb)
642{
643 /* Compatibility kludge, should go away after all cards
644 * are fixed to fill correct value for opbfreq.
645 * Previous driver version used hardcoded divider value 4,
646 * it corresponds to OPB frequency from the range (40, 50] MHz
647 */
648 if (!opb){
649 printk(KERN_WARNING "ibm-iic: using compatibility value for OPB freq,"
650 " fix your board specific setup\n");
651 opb = 50000000;
652 }
653
654 /* Convert to MHz */
655 opb /= 1000000;
Stefan Roese217bcec2008-01-27 18:14:45 +0100656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (opb < 20 || opb > 150){
Sean MacLennan681aae822008-04-22 22:16:46 +0200658 printk(KERN_WARNING "ibm-iic: invalid OPB clock frequency %u MHz\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 opb);
660 opb = opb < 20 ? 20 : 150;
661 }
662 return (u8)((opb + 9) / 10 - 1);
663}
664
Bill Pemberton0b255e92012-11-27 15:59:38 -0500665static int iic_request_irq(struct platform_device *ofdev,
Sean MacLennan838349b2008-04-22 22:16:47 +0200666 struct ibm_iic_private *dev)
667{
Grant Likely61c7a082010-04-13 16:12:29 -0700668 struct device_node *np = ofdev->dev.of_node;
Sean MacLennan838349b2008-04-22 22:16:47 +0200669 int irq;
670
671 if (iic_force_poll)
Wolfram Sangf0ec9e22009-10-04 13:08:16 +0200672 return 0;
Sean MacLennan838349b2008-04-22 22:16:47 +0200673
674 irq = irq_of_parse_and_map(np, 0);
Wolfram Sangf0ec9e22009-10-04 13:08:16 +0200675 if (!irq) {
Sean MacLennan838349b2008-04-22 22:16:47 +0200676 dev_err(&ofdev->dev, "irq_of_parse_and_map failed\n");
Wolfram Sangf0ec9e22009-10-04 13:08:16 +0200677 return 0;
Sean MacLennan838349b2008-04-22 22:16:47 +0200678 }
679
680 /* Disable interrupts until we finish initialization, assumes
681 * level-sensitive IRQ setup...
682 */
683 iic_interrupt_mode(dev, 0);
684 if (request_irq(irq, iic_handler, 0, "IBM IIC", dev)) {
685 dev_err(&ofdev->dev, "request_irq %d failed\n", irq);
686 /* Fallback to the polling mode */
Wolfram Sangf0ec9e22009-10-04 13:08:16 +0200687 return 0;
Sean MacLennan838349b2008-04-22 22:16:47 +0200688 }
689
690 return irq;
691}
692
693/*
694 * Register single IIC interface
695 */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500696static int iic_probe(struct platform_device *ofdev)
Sean MacLennan838349b2008-04-22 22:16:47 +0200697{
Grant Likely61c7a082010-04-13 16:12:29 -0700698 struct device_node *np = ofdev->dev.of_node;
Sean MacLennan838349b2008-04-22 22:16:47 +0200699 struct ibm_iic_private *dev;
700 struct i2c_adapter *adap;
Sean MacLennanb1204e62008-07-14 22:38:36 +0200701 const u32 *freq;
Sean MacLennan838349b2008-04-22 22:16:47 +0200702 int ret;
703
704 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
705 if (!dev) {
706 dev_err(&ofdev->dev, "failed to allocate device data\n");
707 return -ENOMEM;
708 }
709
Jingoo Hanc2c64952013-05-23 19:22:40 +0900710 platform_set_drvdata(ofdev, dev);
Sean MacLennan838349b2008-04-22 22:16:47 +0200711
Sean MacLennan838349b2008-04-22 22:16:47 +0200712 dev->vaddr = of_iomap(np, 0);
713 if (dev->vaddr == NULL) {
714 dev_err(&ofdev->dev, "failed to iomap device\n");
715 ret = -ENXIO;
716 goto error_cleanup;
717 }
718
719 init_waitqueue_head(&dev->wq);
720
721 dev->irq = iic_request_irq(ofdev, dev);
Wolfram Sangf0ec9e22009-10-04 13:08:16 +0200722 if (!dev->irq)
Sean MacLennan838349b2008-04-22 22:16:47 +0200723 dev_warn(&ofdev->dev, "using polling mode\n");
724
725 /* Board specific settings */
726 if (iic_force_fast || of_get_property(np, "fast-mode", NULL))
727 dev->fast_mode = 1;
728
729 freq = of_get_property(np, "clock-frequency", NULL);
730 if (freq == NULL) {
731 freq = of_get_property(np->parent, "clock-frequency", NULL);
732 if (freq == NULL) {
733 dev_err(&ofdev->dev, "Unable to get bus frequency\n");
734 ret = -EINVAL;
735 goto error_cleanup;
736 }
737 }
738
739 dev->clckdiv = iic_clckdiv(*freq);
740 dev_dbg(&ofdev->dev, "clckdiv = %d\n", dev->clckdiv);
741
742 /* Initialize IIC interface */
743 iic_dev_init(dev);
744
745 /* Register it with i2c layer */
746 adap = &dev->adap;
747 adap->dev.parent = &ofdev->dev;
Grant Likely9fd04992010-06-08 07:48:18 -0600748 adap->dev.of_node = of_node_get(np);
Sean MacLennan838349b2008-04-22 22:16:47 +0200749 strlcpy(adap->name, "IBM IIC", sizeof(adap->name));
750 i2c_set_adapdata(adap, dev);
Jean Delvare3401b2f2008-07-14 22:38:29 +0200751 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Sean MacLennan838349b2008-04-22 22:16:47 +0200752 adap->algo = &iic_algo;
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100753 adap->timeout = HZ;
Sean MacLennan838349b2008-04-22 22:16:47 +0200754
Sean MacLennanb1204e62008-07-14 22:38:36 +0200755 ret = i2c_add_adapter(adap);
Wolfram Sangea734402016-08-09 13:36:17 +0200756 if (ret < 0)
Sean MacLennan838349b2008-04-22 22:16:47 +0200757 goto error_cleanup;
Sean MacLennan838349b2008-04-22 22:16:47 +0200758
759 dev_info(&ofdev->dev, "using %s mode\n",
760 dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");
761
762 return 0;
763
764error_cleanup:
Wolfram Sangf0ec9e22009-10-04 13:08:16 +0200765 if (dev->irq) {
Sean MacLennan838349b2008-04-22 22:16:47 +0200766 iic_interrupt_mode(dev, 0);
767 free_irq(dev->irq, dev);
768 }
769
770 if (dev->vaddr)
771 iounmap(dev->vaddr);
772
Sean MacLennan838349b2008-04-22 22:16:47 +0200773 kfree(dev);
774 return ret;
775}
776
777/*
778 * Cleanup initialized IIC interface
779 */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500780static int iic_remove(struct platform_device *ofdev)
Sean MacLennan838349b2008-04-22 22:16:47 +0200781{
Jingoo Hanc2c64952013-05-23 19:22:40 +0900782 struct ibm_iic_private *dev = platform_get_drvdata(ofdev);
Sean MacLennan838349b2008-04-22 22:16:47 +0200783
Sean MacLennan838349b2008-04-22 22:16:47 +0200784 i2c_del_adapter(&dev->adap);
785
Wolfram Sangf0ec9e22009-10-04 13:08:16 +0200786 if (dev->irq) {
Sean MacLennan838349b2008-04-22 22:16:47 +0200787 iic_interrupt_mode(dev, 0);
788 free_irq(dev->irq, dev);
789 }
790
791 iounmap(dev->vaddr);
792 kfree(dev);
793
794 return 0;
795}
796
797static const struct of_device_id ibm_iic_match[] = {
Stefan Roesed3dc6852008-07-14 22:38:30 +0200798 { .compatible = "ibm,iic", },
Sean MacLennan838349b2008-04-22 22:16:47 +0200799 {}
800};
Luis de Bethencourtd695e222015-10-20 15:16:27 +0100801MODULE_DEVICE_TABLE(of, ibm_iic_match);
Sean MacLennan838349b2008-04-22 22:16:47 +0200802
Grant Likely1c48a5c2011-02-17 02:43:24 -0700803static struct platform_driver ibm_iic_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700804 .driver = {
805 .name = "ibm-iic",
Grant Likely40182942010-04-13 16:13:02 -0700806 .of_match_table = ibm_iic_match,
807 },
Sean MacLennan838349b2008-04-22 22:16:47 +0200808 .probe = iic_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500809 .remove = iic_remove,
Sean MacLennan838349b2008-04-22 22:16:47 +0200810};
811
Axel Lina3664b52012-01-12 20:32:04 +0100812module_platform_driver(ibm_iic_driver);