blob: cb418140cbbe59512e2ec743452a71cd235ad070 [file] [log] [blame]
Rade Bozic85660f42010-01-28 12:47:07 -08001/*
2 * (C) Copyright 2009-2010
3 * Nokia Siemens Networks, michael.lawnick.ext@nsn.com
4 *
Jan Glauberdfcd8212016-03-18 09:46:26 +01005 * Portions Copyright (C) 2010 - 2016 Cavium, Inc.
Rade Bozic85660f42010-01-28 12:47:07 -08006 *
7 * This is a driver for the i2c adapter in Cavium Networks' OCTEON processors.
8 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
Jan Glauber4729cbe2016-04-25 16:33:35 +020014#include <linux/atomic.h>
David Daneyf353a212012-07-05 18:12:39 +020015#include <linux/platform_device.h>
16#include <linux/interrupt.h>
Rade Bozic85660f42010-01-28 12:47:07 -080017#include <linux/kernel.h>
18#include <linux/module.h>
David Daneyf353a212012-07-05 18:12:39 +020019#include <linux/delay.h>
Rade Bozic85660f42010-01-28 12:47:07 -080020#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Rade Bozic85660f42010-01-28 12:47:07 -080022#include <linux/i2c.h>
David Daneyf353a212012-07-05 18:12:39 +020023#include <linux/io.h>
24#include <linux/of.h>
Rade Bozic85660f42010-01-28 12:47:07 -080025
26#include <asm/octeon/octeon.h>
27
28#define DRV_NAME "i2c-octeon"
29
Jan Glauberdfcd8212016-03-18 09:46:26 +010030/* Register offsets */
31#define SW_TWSI 0x00
32#define TWSI_INT 0x10
David Daneyd1fbff82016-04-25 16:33:34 +020033#define SW_TWSI_EXT 0x18
Rade Bozic85660f42010-01-28 12:47:07 -080034
35/* Controller command patterns */
Jan Glauberdfcd8212016-03-18 09:46:26 +010036#define SW_TWSI_V BIT_ULL(63) /* Valid bit */
David Daneyd1fbff82016-04-25 16:33:34 +020037#define SW_TWSI_EIA BIT_ULL(61) /* Extended internal address */
Jan Glauberdfcd8212016-03-18 09:46:26 +010038#define SW_TWSI_R BIT_ULL(56) /* Result or read bit */
David Daneyd1fbff82016-04-25 16:33:34 +020039#define SW_TWSI_SOVR BIT_ULL(55) /* Size override */
40#define SW_TWSI_SIZE_SHIFT 52
41#define SW_TWSI_ADDR_SHIFT 40
42#define SW_TWSI_IA_SHIFT 32 /* Internal address */
Jan Glauberdfcd8212016-03-18 09:46:26 +010043
44/* Controller opcode word (bits 60:57) */
45#define SW_TWSI_OP_SHIFT 57
David Daneyd1fbff82016-04-25 16:33:34 +020046#define SW_TWSI_OP_7 (0ULL << SW_TWSI_OP_SHIFT)
47#define SW_TWSI_OP_7_IA (1ULL << SW_TWSI_OP_SHIFT)
48#define SW_TWSI_OP_10 (2ULL << SW_TWSI_OP_SHIFT)
49#define SW_TWSI_OP_10_IA (3ULL << SW_TWSI_OP_SHIFT)
Jan Glauberdfcd8212016-03-18 09:46:26 +010050#define SW_TWSI_OP_TWSI_CLK (4ULL << SW_TWSI_OP_SHIFT)
51#define SW_TWSI_OP_EOP (6ULL << SW_TWSI_OP_SHIFT) /* Extended opcode */
52
53/* Controller extended opcode word (bits 34:32) */
54#define SW_TWSI_EOP_SHIFT 32
55#define SW_TWSI_EOP_TWSI_DATA (SW_TWSI_OP_EOP | 1ULL << SW_TWSI_EOP_SHIFT)
56#define SW_TWSI_EOP_TWSI_CTL (SW_TWSI_OP_EOP | 2ULL << SW_TWSI_EOP_SHIFT)
57#define SW_TWSI_EOP_TWSI_CLKCTL (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
58#define SW_TWSI_EOP_TWSI_STAT (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
59#define SW_TWSI_EOP_TWSI_RST (SW_TWSI_OP_EOP | 7ULL << SW_TWSI_EOP_SHIFT)
Rade Bozic85660f42010-01-28 12:47:07 -080060
61/* Controller command and status bits */
David Daneyd1fbff82016-04-25 16:33:34 +020062#define TWSI_CTL_CE 0x80 /* High level controller enable */
Jan Glauberdfcd8212016-03-18 09:46:26 +010063#define TWSI_CTL_ENAB 0x40 /* Bus enable */
64#define TWSI_CTL_STA 0x20 /* Master-mode start, HW clears when done */
65#define TWSI_CTL_STP 0x10 /* Master-mode stop, HW clears when done */
66#define TWSI_CTL_IFLG 0x08 /* HW event, SW writes 0 to ACK */
67#define TWSI_CTL_AAK 0x04 /* Assert ACK */
Rade Bozic85660f42010-01-28 12:47:07 -080068
Jan Glauberb4c715d2016-04-25 16:33:30 +020069/* Status values */
70#define STAT_ERROR 0x00
Jan Glauberdfcd8212016-03-18 09:46:26 +010071#define STAT_START 0x08
Jan Glauberb4c715d2016-04-25 16:33:30 +020072#define STAT_REP_START 0x10
Jan Glauberdfcd8212016-03-18 09:46:26 +010073#define STAT_TXADDR_ACK 0x18
Jan Glauberb4c715d2016-04-25 16:33:30 +020074#define STAT_TXADDR_NAK 0x20
Jan Glauberdfcd8212016-03-18 09:46:26 +010075#define STAT_TXDATA_ACK 0x28
Jan Glauberb4c715d2016-04-25 16:33:30 +020076#define STAT_TXDATA_NAK 0x30
77#define STAT_LOST_ARB_38 0x38
Jan Glauberdfcd8212016-03-18 09:46:26 +010078#define STAT_RXADDR_ACK 0x40
Jan Glauberb4c715d2016-04-25 16:33:30 +020079#define STAT_RXADDR_NAK 0x48
Jan Glauberdfcd8212016-03-18 09:46:26 +010080#define STAT_RXDATA_ACK 0x50
Jan Glauberb4c715d2016-04-25 16:33:30 +020081#define STAT_RXDATA_NAK 0x58
82#define STAT_SLAVE_60 0x60
83#define STAT_LOST_ARB_68 0x68
84#define STAT_SLAVE_70 0x70
85#define STAT_LOST_ARB_78 0x78
86#define STAT_SLAVE_80 0x80
87#define STAT_SLAVE_88 0x88
88#define STAT_GENDATA_ACK 0x90
89#define STAT_GENDATA_NAK 0x98
90#define STAT_SLAVE_A0 0xA0
91#define STAT_SLAVE_A8 0xA8
92#define STAT_LOST_ARB_B0 0xB0
93#define STAT_SLAVE_LOST 0xB8
94#define STAT_SLAVE_NAK 0xC0
95#define STAT_SLAVE_ACK 0xC8
96#define STAT_AD2W_ACK 0xD0
97#define STAT_AD2W_NAK 0xD8
Jan Glauberdfcd8212016-03-18 09:46:26 +010098#define STAT_IDLE 0xF8
99
100/* TWSI_INT values */
David Daneyd1fbff82016-04-25 16:33:34 +0200101#define TWSI_INT_ST_INT BIT_ULL(0)
102#define TWSI_INT_TS_INT BIT_ULL(1)
103#define TWSI_INT_CORE_INT BIT_ULL(2)
104#define TWSI_INT_ST_EN BIT_ULL(4)
105#define TWSI_INT_TS_EN BIT_ULL(5)
Jan Glauberdfcd8212016-03-18 09:46:26 +0100106#define TWSI_INT_CORE_EN BIT_ULL(6)
107#define TWSI_INT_SDA_OVR BIT_ULL(8)
108#define TWSI_INT_SCL_OVR BIT_ULL(9)
Jan Glauberc981e342016-04-25 16:33:31 +0200109#define TWSI_INT_SDA BIT_ULL(10)
110#define TWSI_INT_SCL BIT_ULL(11)
Rade Bozic85660f42010-01-28 12:47:07 -0800111
Peter Swain1bb1ff32016-04-25 16:33:37 +0200112#define I2C_OCTEON_EVENT_WAIT 80 /* microseconds */
113
Rade Bozic85660f42010-01-28 12:47:07 -0800114struct octeon_i2c {
115 wait_queue_head_t queue;
116 struct i2c_adapter adap;
117 int irq;
Jan Glauber4729cbe2016-04-25 16:33:35 +0200118 int hlc_irq; /* For cn7890 only */
David Daneyf353a212012-07-05 18:12:39 +0200119 u32 twsi_freq;
Rade Bozic85660f42010-01-28 12:47:07 -0800120 int sys_freq;
Rade Bozic85660f42010-01-28 12:47:07 -0800121 void __iomem *twsi_base;
Rade Bozic85660f42010-01-28 12:47:07 -0800122 struct device *dev;
David Daneyd1fbff82016-04-25 16:33:34 +0200123 bool hlc_enabled;
Jan Glauber4729cbe2016-04-25 16:33:35 +0200124 void (*int_enable)(struct octeon_i2c *);
125 void (*int_disable)(struct octeon_i2c *);
126 void (*hlc_int_enable)(struct octeon_i2c *);
127 void (*hlc_int_disable)(struct octeon_i2c *);
128 atomic_t int_enable_cnt;
129 atomic_t hlc_int_enable_cnt;
Rade Bozic85660f42010-01-28 12:47:07 -0800130};
131
Peter Swain30c24b22016-04-25 16:33:33 +0200132static void octeon_i2c_writeq_flush(u64 val, void __iomem *addr)
133{
134 __raw_writeq(val, addr);
135 __raw_readq(addr); /* wait for write to land */
136}
137
Rade Bozic85660f42010-01-28 12:47:07 -0800138/**
Jan Glauber9cb94802016-04-11 17:28:34 +0200139 * octeon_i2c_reg_write - write an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100140 * @i2c: The struct octeon_i2c
141 * @eop_reg: Register selector
142 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -0800143 *
144 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
145 */
Jan Glauber9cb94802016-04-11 17:28:34 +0200146static void octeon_i2c_reg_write(struct octeon_i2c *i2c, u64 eop_reg, u8 data)
Rade Bozic85660f42010-01-28 12:47:07 -0800147{
148 u64 tmp;
149
150 __raw_writeq(SW_TWSI_V | eop_reg | data, i2c->twsi_base + SW_TWSI);
151 do {
152 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
153 } while ((tmp & SW_TWSI_V) != 0);
154}
155
Jan Glauberc57db702016-04-11 17:28:35 +0200156#define octeon_i2c_ctl_write(i2c, val) \
157 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CTL, val)
158#define octeon_i2c_data_write(i2c, val) \
159 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_DATA, val)
160
Rade Bozic85660f42010-01-28 12:47:07 -0800161/**
Jan Glauber9cb94802016-04-11 17:28:34 +0200162 * octeon_i2c_reg_read - read lower bits of an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100163 * @i2c: The struct octeon_i2c
164 * @eop_reg: Register selector
Rade Bozic85660f42010-01-28 12:47:07 -0800165 *
166 * Returns the data.
167 *
168 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
169 */
Jan Glauber9cb94802016-04-11 17:28:34 +0200170static u8 octeon_i2c_reg_read(struct octeon_i2c *i2c, u64 eop_reg)
Rade Bozic85660f42010-01-28 12:47:07 -0800171{
172 u64 tmp;
173
174 __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI);
175 do {
176 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
177 } while ((tmp & SW_TWSI_V) != 0);
178
179 return tmp & 0xFF;
180}
181
Jan Glauberc57db702016-04-11 17:28:35 +0200182#define octeon_i2c_ctl_read(i2c) \
183 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_CTL)
184#define octeon_i2c_data_read(i2c) \
185 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_DATA)
186#define octeon_i2c_stat_read(i2c) \
187 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_STAT)
188
Rade Bozic85660f42010-01-28 12:47:07 -0800189/**
Jan Glauberc981e342016-04-25 16:33:31 +0200190 * octeon_i2c_read_int - read the TWSI_INT register
191 * @i2c: The struct octeon_i2c
192 *
193 * Returns the value of the register.
194 */
195static u64 octeon_i2c_read_int(struct octeon_i2c *i2c)
196{
197 return __raw_readq(i2c->twsi_base + TWSI_INT);
198}
199
200/**
Rade Bozic85660f42010-01-28 12:47:07 -0800201 * octeon_i2c_write_int - write the TWSI_INT register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100202 * @i2c: The struct octeon_i2c
203 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -0800204 */
205static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
206{
Peter Swain30c24b22016-04-25 16:33:33 +0200207 octeon_i2c_writeq_flush(data, i2c->twsi_base + TWSI_INT);
Rade Bozic85660f42010-01-28 12:47:07 -0800208}
209
210/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100211 * octeon_i2c_int_enable - enable the CORE interrupt
212 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800213 *
214 * The interrupt will be asserted when there is non-STAT_IDLE state in
215 * the SW_TWSI_EOP_TWSI_STAT register.
216 */
217static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
218{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100219 octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
Rade Bozic85660f42010-01-28 12:47:07 -0800220}
221
Jan Glauberbd7784c2016-03-07 16:10:44 +0100222/* disable the CORE interrupt */
Rade Bozic85660f42010-01-28 12:47:07 -0800223static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
224{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100225 /* clear TS/ST/IFLG events */
Rade Bozic85660f42010-01-28 12:47:07 -0800226 octeon_i2c_write_int(i2c, 0);
227}
228
Jan Glauber4729cbe2016-04-25 16:33:35 +0200229/**
230 * octeon_i2c_int_enable78 - enable the CORE interrupt
231 * @i2c: The struct octeon_i2c
232 *
233 * The interrupt will be asserted when there is non-STAT_IDLE state in the
234 * SW_TWSI_EOP_TWSI_STAT register.
235 */
236static void octeon_i2c_int_enable78(struct octeon_i2c *i2c)
237{
238 atomic_inc_return(&i2c->int_enable_cnt);
239 enable_irq(i2c->irq);
240}
241
242static void __octeon_i2c_irq_disable(atomic_t *cnt, int irq)
243{
244 int count;
245
246 /*
247 * The interrupt can be disabled in two places, but we only
248 * want to make the disable_irq_nosync() call once, so keep
249 * track with the atomic variable.
250 */
251 count = atomic_dec_if_positive(cnt);
252 if (count >= 0)
253 disable_irq_nosync(irq);
254}
255
256/* disable the CORE interrupt */
257static void octeon_i2c_int_disable78(struct octeon_i2c *i2c)
258{
259 __octeon_i2c_irq_disable(&i2c->int_enable_cnt, i2c->irq);
260}
261
262/**
263 * octeon_i2c_hlc_int_enable78 - enable the ST interrupt
264 * @i2c: The struct octeon_i2c
265 *
266 * The interrupt will be asserted when there is non-STAT_IDLE state in
267 * the SW_TWSI_EOP_TWSI_STAT register.
268 */
269static void octeon_i2c_hlc_int_enable78(struct octeon_i2c *i2c)
270{
271 atomic_inc_return(&i2c->hlc_int_enable_cnt);
272 enable_irq(i2c->hlc_irq);
273}
274
275/* disable the ST interrupt */
276static void octeon_i2c_hlc_int_disable78(struct octeon_i2c *i2c)
277{
278 __octeon_i2c_irq_disable(&i2c->hlc_int_enable_cnt, i2c->hlc_irq);
279}
280
David Daneyd1fbff82016-04-25 16:33:34 +0200281/*
282 * Cleanup low-level state & enable high-level controller.
283 */
284static void octeon_i2c_hlc_enable(struct octeon_i2c *i2c)
285{
286 int try = 0;
287 u64 val;
288
289 if (i2c->hlc_enabled)
290 return;
291 i2c->hlc_enabled = true;
292
293 while (1) {
294 val = octeon_i2c_ctl_read(i2c);
295 if (!(val & (TWSI_CTL_STA | TWSI_CTL_STP)))
296 break;
297
298 /* clear IFLG event */
299 if (val & TWSI_CTL_IFLG)
300 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
301
302 if (try++ > 100) {
303 pr_err("%s: giving up\n", __func__);
304 break;
305 }
306
307 /* spin until any start/stop has finished */
308 udelay(10);
309 }
310 octeon_i2c_ctl_write(i2c, TWSI_CTL_CE | TWSI_CTL_AAK | TWSI_CTL_ENAB);
311}
312
313static void octeon_i2c_hlc_disable(struct octeon_i2c *i2c)
314{
315 if (!i2c->hlc_enabled)
316 return;
317
318 i2c->hlc_enabled = false;
319 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
320}
321
Jan Glauberbd7784c2016-03-07 16:10:44 +0100322/* interrupt service routine */
Rade Bozic85660f42010-01-28 12:47:07 -0800323static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
324{
325 struct octeon_i2c *i2c = dev_id;
326
Jan Glauber4729cbe2016-04-25 16:33:35 +0200327 i2c->int_disable(i2c);
328 wake_up(&i2c->queue);
329
330 return IRQ_HANDLED;
331}
332
333/* HLC interrupt service routine */
334static irqreturn_t octeon_i2c_hlc_isr78(int irq, void *dev_id)
335{
336 struct octeon_i2c *i2c = dev_id;
337
338 i2c->hlc_int_disable(i2c);
송은봉2637e5f2013-04-17 21:40:17 +0000339 wake_up(&i2c->queue);
Rade Bozic85660f42010-01-28 12:47:07 -0800340
341 return IRQ_HANDLED;
342}
343
Peter Swain1bb1ff32016-04-25 16:33:37 +0200344static bool octeon_i2c_test_iflg(struct octeon_i2c *i2c)
Rade Bozic85660f42010-01-28 12:47:07 -0800345{
Jan Glauberb69e5c62016-04-11 17:28:36 +0200346 return (octeon_i2c_ctl_read(i2c) & TWSI_CTL_IFLG);
Rade Bozic85660f42010-01-28 12:47:07 -0800347}
348
Peter Swain1bb1ff32016-04-25 16:33:37 +0200349static bool octeon_i2c_test_ready(struct octeon_i2c *i2c, bool *first)
350{
351 if (octeon_i2c_test_iflg(i2c))
352 return true;
353
354 if (*first) {
355 *first = false;
356 return false;
357 }
358
359 /*
360 * IRQ has signaled an event but IFLG hasn't changed.
361 * Sleep and retry once.
362 */
363 usleep_range(I2C_OCTEON_EVENT_WAIT, 2 * I2C_OCTEON_EVENT_WAIT);
364 return octeon_i2c_test_iflg(i2c);
365}
366
Rade Bozic85660f42010-01-28 12:47:07 -0800367/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100368 * octeon_i2c_wait - wait for the IFLG to be set
369 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800370 *
371 * Returns 0 on success, otherwise a negative errno.
372 */
373static int octeon_i2c_wait(struct octeon_i2c *i2c)
374{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100375 long time_left;
Peter Swain1bb1ff32016-04-25 16:33:37 +0200376 bool first = 1;
Rade Bozic85660f42010-01-28 12:47:07 -0800377
Jan Glauber4729cbe2016-04-25 16:33:35 +0200378 i2c->int_enable(i2c);
Peter Swain1bb1ff32016-04-25 16:33:37 +0200379 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_ready(i2c, &first),
Jan Glauberdfcd8212016-03-18 09:46:26 +0100380 i2c->adap.timeout);
Jan Glauber4729cbe2016-04-25 16:33:35 +0200381 i2c->int_disable(i2c);
Peter Swain1bb1ff32016-04-25 16:33:37 +0200382 if (!time_left)
Bernhard Wallecc33e542010-09-27 12:55:16 +0200383 return -ETIMEDOUT;
Rade Bozic85660f42010-01-28 12:47:07 -0800384
385 return 0;
386}
387
Jan Glauberb4c715d2016-04-25 16:33:30 +0200388static int octeon_i2c_check_status(struct octeon_i2c *i2c, int final_read)
389{
390 u8 stat = octeon_i2c_stat_read(i2c);
391
392 switch (stat) {
393 /* Everything is fine */
394 case STAT_IDLE:
395 case STAT_AD2W_ACK:
396 case STAT_RXADDR_ACK:
397 case STAT_TXADDR_ACK:
398 case STAT_TXDATA_ACK:
399 return 0;
400
401 /* ACK allowed on pre-terminal bytes only */
402 case STAT_RXDATA_ACK:
403 if (!final_read)
404 return 0;
405 return -EIO;
406
407 /* NAK allowed on terminal byte only */
408 case STAT_RXDATA_NAK:
409 if (final_read)
410 return 0;
411 return -EIO;
412
413 /* Arbitration lost */
414 case STAT_LOST_ARB_38:
415 case STAT_LOST_ARB_68:
416 case STAT_LOST_ARB_78:
417 case STAT_LOST_ARB_B0:
418 return -EAGAIN;
419
420 /* Being addressed as slave, should back off & listen */
421 case STAT_SLAVE_60:
422 case STAT_SLAVE_70:
423 case STAT_GENDATA_ACK:
424 case STAT_GENDATA_NAK:
425 return -EOPNOTSUPP;
426
427 /* Core busy as slave */
428 case STAT_SLAVE_80:
429 case STAT_SLAVE_88:
430 case STAT_SLAVE_A0:
431 case STAT_SLAVE_A8:
432 case STAT_SLAVE_LOST:
433 case STAT_SLAVE_NAK:
434 case STAT_SLAVE_ACK:
435 return -EOPNOTSUPP;
436
437 case STAT_TXDATA_NAK:
438 return -EIO;
439 case STAT_TXADDR_NAK:
440 case STAT_RXADDR_NAK:
441 case STAT_AD2W_NAK:
442 return -ENXIO;
443 default:
444 dev_err(i2c->dev, "unhandled state: %d\n", stat);
445 return -EIO;
446 }
447}
448
Peter Swain1bb1ff32016-04-25 16:33:37 +0200449static bool octeon_i2c_hlc_test_valid(struct octeon_i2c *i2c)
David Daneyd1fbff82016-04-25 16:33:34 +0200450{
Peter Swain1bb1ff32016-04-25 16:33:37 +0200451 return (__raw_readq(i2c->twsi_base + SW_TWSI) & SW_TWSI_V) == 0;
452}
David Daneyd1fbff82016-04-25 16:33:34 +0200453
Peter Swain1bb1ff32016-04-25 16:33:37 +0200454static bool octeon_i2c_hlc_test_ready(struct octeon_i2c *i2c, bool *first)
455{
456 /* check if valid bit is cleared */
457 if (octeon_i2c_hlc_test_valid(i2c))
458 return true;
459
460 if (*first) {
461 *first = false;
462 return false;
463 }
464
465 /*
466 * IRQ has signaled an event but valid bit isn't cleared.
467 * Sleep and retry once.
468 */
469 usleep_range(I2C_OCTEON_EVENT_WAIT, 2 * I2C_OCTEON_EVENT_WAIT);
470 return octeon_i2c_hlc_test_valid(i2c);
David Daneyd1fbff82016-04-25 16:33:34 +0200471}
472
473static void octeon_i2c_hlc_int_enable(struct octeon_i2c *i2c)
474{
475 octeon_i2c_write_int(i2c, TWSI_INT_ST_EN);
476}
477
478static void octeon_i2c_hlc_int_clear(struct octeon_i2c *i2c)
479{
480 /* clear ST/TS events, listen for neither */
481 octeon_i2c_write_int(i2c, TWSI_INT_ST_INT | TWSI_INT_TS_INT);
482}
483
484/**
485 * octeon_i2c_hlc_wait - wait for an HLC operation to complete
486 * @i2c: The struct octeon_i2c
487 *
488 * Returns 0 on success, otherwise -ETIMEDOUT.
489 */
490static int octeon_i2c_hlc_wait(struct octeon_i2c *i2c)
491{
Peter Swain1bb1ff32016-04-25 16:33:37 +0200492 bool first = 1;
David Daneyd1fbff82016-04-25 16:33:34 +0200493 int time_left;
494
Jan Glauber4729cbe2016-04-25 16:33:35 +0200495 i2c->hlc_int_enable(i2c);
David Daneyd1fbff82016-04-25 16:33:34 +0200496 time_left = wait_event_timeout(i2c->queue,
Peter Swain1bb1ff32016-04-25 16:33:37 +0200497 octeon_i2c_hlc_test_ready(i2c, &first),
David Daneyd1fbff82016-04-25 16:33:34 +0200498 i2c->adap.timeout);
Jan Glauber4729cbe2016-04-25 16:33:35 +0200499 i2c->hlc_int_disable(i2c);
David Daneyd1fbff82016-04-25 16:33:34 +0200500 if (!time_left) {
501 octeon_i2c_hlc_int_clear(i2c);
502 return -ETIMEDOUT;
503 }
504 return 0;
505}
506
507/* high-level-controller pure read of up to 8 bytes */
508static int octeon_i2c_hlc_read(struct octeon_i2c *i2c, struct i2c_msg *msgs)
509{
510 int i, j, ret = 0;
511 u64 cmd;
512
513 octeon_i2c_hlc_enable(i2c);
514 octeon_i2c_hlc_int_clear(i2c);
515
516 cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR;
517 /* SIZE */
518 cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT;
519 /* A */
520 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
521
522 if (msgs[0].flags & I2C_M_TEN)
523 cmd |= SW_TWSI_OP_10;
524 else
525 cmd |= SW_TWSI_OP_7;
526
527 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
528 ret = octeon_i2c_hlc_wait(i2c);
529 if (ret)
530 goto err;
531
532 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
533 if ((cmd & SW_TWSI_R) == 0)
534 return -EAGAIN;
535
536 for (i = 0, j = msgs[0].len - 1; i < msgs[0].len && i < 4; i++, j--)
537 msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff;
538
539 if (msgs[0].len > 4) {
540 cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT);
541 for (i = 0; i < msgs[0].len - 4 && i < 4; i++, j--)
542 msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff;
543 }
544
545err:
546 return ret;
547}
548
549/* high-level-controller pure write of up to 8 bytes */
550static int octeon_i2c_hlc_write(struct octeon_i2c *i2c, struct i2c_msg *msgs)
551{
552 int i, j, ret = 0;
553 u64 cmd;
554
555 octeon_i2c_hlc_enable(i2c);
556 octeon_i2c_hlc_int_clear(i2c);
557
558 cmd = SW_TWSI_V | SW_TWSI_SOVR;
559 /* SIZE */
560 cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT;
561 /* A */
562 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
563
564 if (msgs[0].flags & I2C_M_TEN)
565 cmd |= SW_TWSI_OP_10;
566 else
567 cmd |= SW_TWSI_OP_7;
568
569 for (i = 0, j = msgs[0].len - 1; i < msgs[0].len && i < 4; i++, j--)
570 cmd |= (u64)msgs[0].buf[j] << (8 * i);
571
572 if (msgs[0].len > 4) {
573 u64 ext = 0;
574
575 for (i = 0; i < msgs[0].len - 4 && i < 4; i++, j--)
576 ext |= (u64)msgs[0].buf[j] << (8 * i);
577 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT);
578 }
579
580 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
581 ret = octeon_i2c_hlc_wait(i2c);
582 if (ret)
583 goto err;
584
585 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
586 if ((cmd & SW_TWSI_R) == 0)
587 return -EAGAIN;
588
589 ret = octeon_i2c_check_status(i2c, false);
590
591err:
592 return ret;
593}
594
595/* high-level-controller composite write+read, msg0=addr, msg1=data */
596static int octeon_i2c_hlc_comp_read(struct octeon_i2c *i2c, struct i2c_msg *msgs)
597{
598 int i, j, ret = 0;
599 u64 cmd;
600
601 octeon_i2c_hlc_enable(i2c);
602
603 cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR;
604 /* SIZE */
605 cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT;
606 /* A */
607 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
608
609 if (msgs[0].flags & I2C_M_TEN)
610 cmd |= SW_TWSI_OP_10_IA;
611 else
612 cmd |= SW_TWSI_OP_7_IA;
613
614 if (msgs[0].len == 2) {
615 u64 ext = 0;
616
617 cmd |= SW_TWSI_EIA;
618 ext = (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
619 cmd |= (u64)msgs[0].buf[1] << SW_TWSI_IA_SHIFT;
620 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT);
621 } else {
622 cmd |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
623 }
624
625 octeon_i2c_hlc_int_clear(i2c);
626 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
627
628 ret = octeon_i2c_hlc_wait(i2c);
629 if (ret)
630 goto err;
631
632 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
633 if ((cmd & SW_TWSI_R) == 0)
634 return -EAGAIN;
635
636 for (i = 0, j = msgs[1].len - 1; i < msgs[1].len && i < 4; i++, j--)
637 msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff;
638
639 if (msgs[1].len > 4) {
640 cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT);
641 for (i = 0; i < msgs[1].len - 4 && i < 4; i++, j--)
642 msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff;
643 }
644
645err:
646 return ret;
647}
648
649/* high-level-controller composite write+write, m[0]len<=2, m[1]len<=8 */
650static int octeon_i2c_hlc_comp_write(struct octeon_i2c *i2c, struct i2c_msg *msgs)
651{
652 bool set_ext = false;
653 int i, j, ret = 0;
654 u64 cmd, ext = 0;
655
656 octeon_i2c_hlc_enable(i2c);
657
658 cmd = SW_TWSI_V | SW_TWSI_SOVR;
659 /* SIZE */
660 cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT;
661 /* A */
662 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
663
664 if (msgs[0].flags & I2C_M_TEN)
665 cmd |= SW_TWSI_OP_10_IA;
666 else
667 cmd |= SW_TWSI_OP_7_IA;
668
669 if (msgs[0].len == 2) {
670 cmd |= SW_TWSI_EIA;
671 ext |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
672 set_ext = true;
673 cmd |= (u64)msgs[0].buf[1] << SW_TWSI_IA_SHIFT;
674 } else {
675 cmd |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
676 }
677
678 for (i = 0, j = msgs[1].len - 1; i < msgs[1].len && i < 4; i++, j--)
679 cmd |= (u64)msgs[1].buf[j] << (8 * i);
680
681 if (msgs[1].len > 4) {
682 for (i = 0; i < msgs[1].len - 4 && i < 4; i++, j--)
683 ext |= (u64)msgs[1].buf[j] << (8 * i);
684 set_ext = true;
685 }
686 if (set_ext)
687 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT);
688
689 octeon_i2c_hlc_int_clear(i2c);
690 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
691
692 ret = octeon_i2c_hlc_wait(i2c);
693 if (ret)
694 goto err;
695
696 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
697 if ((cmd & SW_TWSI_R) == 0)
698 return -EAGAIN;
699
700 ret = octeon_i2c_check_status(i2c, false);
701
702err:
703 return ret;
704}
705
Jan Glauberf541bb32016-04-11 17:28:33 +0200706/* calculate and set clock divisors */
707static void octeon_i2c_set_clock(struct octeon_i2c *i2c)
708{
709 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
710 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
711
712 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
713 /*
714 * An mdiv value of less than 2 seems to not work well
715 * with ds1337 RTCs, so we constrain it to larger values.
716 */
717 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
718 /*
719 * For given ndiv and mdiv values check the
720 * two closest thp values.
721 */
722 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
723 tclk *= (1 << ndiv_idx);
724 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
725
726 for (inc = 0; inc <= 1; inc++) {
727 thp_idx = thp_base + inc;
728 if (thp_idx < 5 || thp_idx > 0xff)
729 continue;
730
731 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
732 foscl = foscl / (1 << ndiv_idx);
733 foscl = foscl / (mdiv_idx + 1) / 10;
734 diff = abs(foscl - i2c->twsi_freq);
735 if (diff < delta_hz) {
736 delta_hz = diff;
737 thp = thp_idx;
738 mdiv = mdiv_idx;
739 ndiv = ndiv_idx;
740 }
741 }
742 }
743 }
Jan Glauber9cb94802016-04-11 17:28:34 +0200744 octeon_i2c_reg_write(i2c, SW_TWSI_OP_TWSI_CLK, thp);
745 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
Jan Glauberf541bb32016-04-11 17:28:33 +0200746}
747
748static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
749{
David Daneyd1fbff82016-04-25 16:33:34 +0200750 u8 status = 0;
Jan Glauberf541bb32016-04-11 17:28:33 +0200751 int tries;
752
Jan Glauberf541bb32016-04-11 17:28:33 +0200753 /* reset controller */
Jan Glauber9cb94802016-04-11 17:28:34 +0200754 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0);
Jan Glauberf541bb32016-04-11 17:28:33 +0200755
David Daneyd1fbff82016-04-25 16:33:34 +0200756 for (tries = 10; tries && status != STAT_IDLE; tries--) {
Jan Glauberf541bb32016-04-11 17:28:33 +0200757 udelay(1);
Jan Glauberc57db702016-04-11 17:28:35 +0200758 status = octeon_i2c_stat_read(i2c);
Jan Glauberf541bb32016-04-11 17:28:33 +0200759 if (status == STAT_IDLE)
David Daneyd1fbff82016-04-25 16:33:34 +0200760 break;
Jan Glauberf541bb32016-04-11 17:28:33 +0200761 }
David Daneyd1fbff82016-04-25 16:33:34 +0200762
763 if (status != STAT_IDLE) {
764 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n",
765 __func__, status);
766 return -EIO;
767 }
768
769 /* toggle twice to force both teardowns */
770 octeon_i2c_hlc_enable(i2c);
771 octeon_i2c_hlc_disable(i2c);
772 return 0;
Jan Glauberf541bb32016-04-11 17:28:33 +0200773}
774
Jan Glauberc981e342016-04-25 16:33:31 +0200775static int octeon_i2c_recovery(struct octeon_i2c *i2c)
776{
777 int ret;
778
779 ret = i2c_recover_bus(&i2c->adap);
780 if (ret)
781 /* recover failed, try hardware re-init */
782 ret = octeon_i2c_init_lowlevel(i2c);
783 return ret;
784}
785
Rade Bozic85660f42010-01-28 12:47:07 -0800786/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100787 * octeon_i2c_start - send START to the bus
788 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800789 *
790 * Returns 0 on success, otherwise a negative errno.
791 */
792static int octeon_i2c_start(struct octeon_i2c *i2c)
793{
Jan Glauberc981e342016-04-25 16:33:31 +0200794 int ret;
795 u8 stat;
Rade Bozic85660f42010-01-28 12:47:07 -0800796
David Daneyd1fbff82016-04-25 16:33:34 +0200797 octeon_i2c_hlc_disable(i2c);
798
Jan Glauberc57db702016-04-11 17:28:35 +0200799 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
Jan Glauberc981e342016-04-25 16:33:31 +0200800 ret = octeon_i2c_wait(i2c);
801 if (ret)
802 goto error;
Rade Bozic85660f42010-01-28 12:47:07 -0800803
Jan Glauberc981e342016-04-25 16:33:31 +0200804 stat = octeon_i2c_stat_read(i2c);
805 if (stat == STAT_START || stat == STAT_REP_START)
806 /* START successful, bail out */
807 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800808
Jan Glauberc981e342016-04-25 16:33:31 +0200809error:
810 /* START failed, try to recover */
811 ret = octeon_i2c_recovery(i2c);
812 return (ret) ? ret : -EAGAIN;
Rade Bozic85660f42010-01-28 12:47:07 -0800813}
814
Jan Glauberdfcd8212016-03-18 09:46:26 +0100815/* send STOP to the bus */
816static void octeon_i2c_stop(struct octeon_i2c *i2c)
Rade Bozic85660f42010-01-28 12:47:07 -0800817{
Jan Glauberc57db702016-04-11 17:28:35 +0200818 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STP);
Rade Bozic85660f42010-01-28 12:47:07 -0800819}
820
821/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100822 * octeon_i2c_write - send data to the bus via low-level controller
823 * @i2c: The struct octeon_i2c
824 * @target: Target address
825 * @data: Pointer to the data to be sent
826 * @length: Length of the data
Rade Bozic85660f42010-01-28 12:47:07 -0800827 *
828 * The address is sent over the bus, then the data.
829 *
830 * Returns 0 on success, otherwise a negative errno.
831 */
832static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
833 const u8 *data, int length)
834{
835 int i, result;
Rade Bozic85660f42010-01-28 12:47:07 -0800836
Jan Glauberc57db702016-04-11 17:28:35 +0200837 octeon_i2c_data_write(i2c, target << 1);
838 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800839
840 result = octeon_i2c_wait(i2c);
841 if (result)
842 return result;
843
844 for (i = 0; i < length; i++) {
Jan Glauberb4c715d2016-04-25 16:33:30 +0200845 result = octeon_i2c_check_status(i2c, false);
846 if (result)
847 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800848
Jan Glauberc57db702016-04-11 17:28:35 +0200849 octeon_i2c_data_write(i2c, data[i]);
850 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800851
852 result = octeon_i2c_wait(i2c);
853 if (result)
854 return result;
855 }
856
857 return 0;
858}
859
860/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100861 * octeon_i2c_read - receive data from the bus via low-level controller
862 * @i2c: The struct octeon_i2c
863 * @target: Target address
864 * @data: Pointer to the location to store the data
David Daney886f6f82016-03-18 09:46:29 +0100865 * @rlength: Length of the data
866 * @recv_len: flag for length byte
Rade Bozic85660f42010-01-28 12:47:07 -0800867 *
868 * The address is sent over the bus, then the data is read.
869 *
870 * Returns 0 on success, otherwise a negative errno.
871 */
872static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
David Daney886f6f82016-03-18 09:46:29 +0100873 u8 *data, u16 *rlength, bool recv_len)
Rade Bozic85660f42010-01-28 12:47:07 -0800874{
David Daney886f6f82016-03-18 09:46:29 +0100875 int i, result, length = *rlength;
Jan Glauberb4c715d2016-04-25 16:33:30 +0200876 bool final_read = false;
Rade Bozic85660f42010-01-28 12:47:07 -0800877
Jan Glauberc57db702016-04-11 17:28:35 +0200878 octeon_i2c_data_write(i2c, (target << 1) | 1);
879 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800880
881 result = octeon_i2c_wait(i2c);
882 if (result)
883 return result;
884
Jan Glauberb4c715d2016-04-25 16:33:30 +0200885 /* address OK ? */
886 result = octeon_i2c_check_status(i2c, false);
887 if (result)
888 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800889
Jan Glauberb4c715d2016-04-25 16:33:30 +0200890 for (i = 0; i < length; i++) {
891 /* for the last byte TWSI_CTL_AAK must not be set */
892 if (i + 1 == length)
893 final_read = true;
894
895 /* clear iflg to allow next event */
896 if (final_read)
Jan Glauberc57db702016-04-11 17:28:35 +0200897 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Jan Glauberb4c715d2016-04-25 16:33:30 +0200898 else
899 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_AAK);
Rade Bozic85660f42010-01-28 12:47:07 -0800900
901 result = octeon_i2c_wait(i2c);
902 if (result)
903 return result;
904
Jan Glauberc57db702016-04-11 17:28:35 +0200905 data[i] = octeon_i2c_data_read(i2c);
David Daney886f6f82016-03-18 09:46:29 +0100906 if (recv_len && i == 0) {
907 if (data[i] > I2C_SMBUS_BLOCK_MAX + 1) {
908 dev_err(i2c->dev,
909 "%s: read len > I2C_SMBUS_BLOCK_MAX %d\n",
910 __func__, data[i]);
911 return -EPROTO;
912 }
913 length += data[i];
914 }
Jan Glauberb4c715d2016-04-25 16:33:30 +0200915
916 result = octeon_i2c_check_status(i2c, final_read);
917 if (result)
918 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800919 }
David Daney886f6f82016-03-18 09:46:29 +0100920 *rlength = length;
Rade Bozic85660f42010-01-28 12:47:07 -0800921 return 0;
922}
923
924/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100925 * octeon_i2c_xfer - The driver's master_xfer function
926 * @adap: Pointer to the i2c_adapter structure
927 * @msgs: Pointer to the messages to be processed
928 * @num: Length of the MSGS array
Rade Bozic85660f42010-01-28 12:47:07 -0800929 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100930 * Returns the number of messages processed, or a negative errno on failure.
Rade Bozic85660f42010-01-28 12:47:07 -0800931 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100932static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
Rade Bozic85660f42010-01-28 12:47:07 -0800933 int num)
934{
Rade Bozic85660f42010-01-28 12:47:07 -0800935 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100936 int i, ret = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800937
David Daneyd1fbff82016-04-25 16:33:34 +0200938 if (num == 1) {
939 if (msgs[0].len > 0 && msgs[0].len <= 8) {
940 if (msgs[0].flags & I2C_M_RD)
941 ret = octeon_i2c_hlc_read(i2c, msgs);
942 else
943 ret = octeon_i2c_hlc_write(i2c, msgs);
944 goto out;
945 }
946 } else if (num == 2) {
947 if ((msgs[0].flags & I2C_M_RD) == 0 &&
948 (msgs[1].flags & I2C_M_RECV_LEN) == 0 &&
949 msgs[0].len > 0 && msgs[0].len <= 2 &&
950 msgs[1].len > 0 && msgs[1].len <= 8 &&
951 msgs[0].addr == msgs[1].addr) {
952 if (msgs[1].flags & I2C_M_RD)
953 ret = octeon_i2c_hlc_comp_read(i2c, msgs);
954 else
955 ret = octeon_i2c_hlc_comp_write(i2c, msgs);
956 goto out;
957 }
958 }
959
Rade Bozic85660f42010-01-28 12:47:07 -0800960 for (i = 0; ret == 0 && i < num; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100961 struct i2c_msg *pmsg = &msgs[i];
962
Jan Glauber392d01d2016-04-26 16:26:52 +0200963 /* zero-length messages are not supported */
964 if (!pmsg->len) {
965 ret = -EOPNOTSUPP;
966 break;
967 }
968
Jan Glauberc981e342016-04-25 16:33:31 +0200969 ret = octeon_i2c_start(i2c);
970 if (ret)
971 return ret;
972
Rade Bozic85660f42010-01-28 12:47:07 -0800973 if (pmsg->flags & I2C_M_RD)
974 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
David Daney886f6f82016-03-18 09:46:29 +0100975 &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
Rade Bozic85660f42010-01-28 12:47:07 -0800976 else
977 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100978 pmsg->len);
Rade Bozic85660f42010-01-28 12:47:07 -0800979 }
980 octeon_i2c_stop(i2c);
David Daneyd1fbff82016-04-25 16:33:34 +0200981out:
Rade Bozic85660f42010-01-28 12:47:07 -0800982 return (ret != 0) ? ret : num;
983}
984
Jan Glauberc981e342016-04-25 16:33:31 +0200985static int octeon_i2c_get_scl(struct i2c_adapter *adap)
986{
987 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
988 u64 state;
989
990 state = octeon_i2c_read_int(i2c);
991 return state & TWSI_INT_SCL;
992}
993
994static void octeon_i2c_set_scl(struct i2c_adapter *adap, int val)
995{
996 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
997
998 octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
999}
1000
1001static int octeon_i2c_get_sda(struct i2c_adapter *adap)
1002{
1003 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
1004 u64 state;
1005
1006 state = octeon_i2c_read_int(i2c);
1007 return state & TWSI_INT_SDA;
1008}
1009
1010static void octeon_i2c_prepare_recovery(struct i2c_adapter *adap)
1011{
1012 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
1013
1014 /*
1015 * The stop resets the state machine, does not _transmit_ STOP unless
1016 * engine was active.
1017 */
1018 octeon_i2c_stop(i2c);
1019
David Daneyd1fbff82016-04-25 16:33:34 +02001020 octeon_i2c_hlc_disable(i2c);
Jan Glauberc981e342016-04-25 16:33:31 +02001021 octeon_i2c_write_int(i2c, 0);
1022}
1023
1024static void octeon_i2c_unprepare_recovery(struct i2c_adapter *adap)
1025{
1026 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
1027
1028 octeon_i2c_write_int(i2c, 0);
1029}
1030
1031static struct i2c_bus_recovery_info octeon_i2c_recovery_info = {
1032 .recover_bus = i2c_generic_scl_recovery,
1033 .get_scl = octeon_i2c_get_scl,
1034 .set_scl = octeon_i2c_set_scl,
1035 .get_sda = octeon_i2c_get_sda,
1036 .prepare_recovery = octeon_i2c_prepare_recovery,
1037 .unprepare_recovery = octeon_i2c_unprepare_recovery,
1038};
1039
Rade Bozic85660f42010-01-28 12:47:07 -08001040static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
1041{
Jan Glauber392d01d2016-04-26 16:26:52 +02001042 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
David Daney886f6f82016-03-18 09:46:29 +01001043 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
Rade Bozic85660f42010-01-28 12:47:07 -08001044}
1045
1046static const struct i2c_algorithm octeon_i2c_algo = {
1047 .master_xfer = octeon_i2c_xfer,
1048 .functionality = octeon_i2c_functionality,
1049};
1050
1051static struct i2c_adapter octeon_i2c_ops = {
1052 .owner = THIS_MODULE,
1053 .name = "OCTEON adapter",
1054 .algo = &octeon_i2c_algo,
Rade Bozic85660f42010-01-28 12:47:07 -08001055};
1056
Bill Pemberton0b255e92012-11-27 15:59:38 -05001057static int octeon_i2c_probe(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -08001058{
Jan Glauberdfcd8212016-03-18 09:46:26 +01001059 struct device_node *node = pdev->dev.of_node;
Jan Glauber4729cbe2016-04-25 16:33:35 +02001060 int irq, result = 0, hlc_irq = 0;
Rade Bozic85660f42010-01-28 12:47:07 -08001061 struct resource *res_mem;
Jan Glauberdfcd8212016-03-18 09:46:26 +01001062 struct octeon_i2c *i2c;
Jan Glauber4729cbe2016-04-25 16:33:35 +02001063 bool cn78xx_style;
Rade Bozic85660f42010-01-28 12:47:07 -08001064
Jan Glauber4729cbe2016-04-25 16:33:35 +02001065 cn78xx_style = of_device_is_compatible(node, "cavium,octeon-7890-twsi");
1066 if (cn78xx_style) {
1067 hlc_irq = platform_get_irq(pdev, 0);
1068 if (hlc_irq < 0)
1069 return hlc_irq;
1070
1071 irq = platform_get_irq(pdev, 2);
1072 if (irq < 0)
1073 return irq;
1074 } else {
1075 /* All adaptors have an irq. */
1076 irq = platform_get_irq(pdev, 0);
1077 if (irq < 0)
1078 return irq;
1079 }
Rade Bozic85660f42010-01-28 12:47:07 -08001080
David Daneyf353a212012-07-05 18:12:39 +02001081 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Rade Bozic85660f42010-01-28 12:47:07 -08001082 if (!i2c) {
Rade Bozic85660f42010-01-28 12:47:07 -08001083 result = -ENOMEM;
1084 goto out;
1085 }
1086 i2c->dev = &pdev->dev;
Rade Bozic85660f42010-01-28 12:47:07 -08001087
1088 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jan Glauber54108e52016-03-18 09:46:27 +01001089 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
1090 if (IS_ERR(i2c->twsi_base)) {
1091 result = PTR_ERR(i2c->twsi_base);
David Daneyf353a212012-07-05 18:12:39 +02001092 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001093 }
Rade Bozic85660f42010-01-28 12:47:07 -08001094
David Daneyf353a212012-07-05 18:12:39 +02001095 /*
1096 * "clock-rate" is a legacy binding, the official binding is
1097 * "clock-frequency". Try the official one first and then
1098 * fall back if it doesn't exist.
1099 */
Jan Glauberdfcd8212016-03-18 09:46:26 +01001100 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
1101 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
David Daneyf353a212012-07-05 18:12:39 +02001102 dev_err(i2c->dev,
1103 "no I2C 'clock-rate' or 'clock-frequency' property\n");
1104 result = -ENXIO;
1105 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001106 }
David Daneyf353a212012-07-05 18:12:39 +02001107
1108 i2c->sys_freq = octeon_get_io_clock_rate();
1109
Rade Bozic85660f42010-01-28 12:47:07 -08001110 init_waitqueue_head(&i2c->queue);
1111
1112 i2c->irq = irq;
1113
Jan Glauber4729cbe2016-04-25 16:33:35 +02001114 if (cn78xx_style) {
1115 i2c->hlc_irq = hlc_irq;
1116
1117 i2c->int_enable = octeon_i2c_int_enable78;
1118 i2c->int_disable = octeon_i2c_int_disable78;
1119 i2c->hlc_int_enable = octeon_i2c_hlc_int_enable78;
1120 i2c->hlc_int_disable = octeon_i2c_hlc_int_disable78;
1121
1122 irq_set_status_flags(i2c->irq, IRQ_NOAUTOEN);
1123 irq_set_status_flags(i2c->hlc_irq, IRQ_NOAUTOEN);
1124
1125 result = devm_request_irq(&pdev->dev, i2c->hlc_irq,
1126 octeon_i2c_hlc_isr78, 0,
1127 DRV_NAME, i2c);
1128 if (result < 0) {
1129 dev_err(i2c->dev, "failed to attach interrupt\n");
1130 goto out;
1131 }
1132 } else {
1133 i2c->int_enable = octeon_i2c_int_enable;
1134 i2c->int_disable = octeon_i2c_int_disable;
1135 i2c->hlc_int_enable = octeon_i2c_hlc_int_enable;
1136 i2c->hlc_int_disable = octeon_i2c_int_disable;
1137 }
1138
David Daneyf353a212012-07-05 18:12:39 +02001139 result = devm_request_irq(&pdev->dev, i2c->irq,
1140 octeon_i2c_isr, 0, DRV_NAME, i2c);
Rade Bozic85660f42010-01-28 12:47:07 -08001141 if (result < 0) {
1142 dev_err(i2c->dev, "failed to attach interrupt\n");
David Daneyf353a212012-07-05 18:12:39 +02001143 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001144 }
1145
Jan Glauberdfcd8212016-03-18 09:46:26 +01001146 result = octeon_i2c_init_lowlevel(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -08001147 if (result) {
1148 dev_err(i2c->dev, "init low level failed\n");
David Daneyf353a212012-07-05 18:12:39 +02001149 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001150 }
1151
Jan Glauberdfcd8212016-03-18 09:46:26 +01001152 octeon_i2c_set_clock(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -08001153
1154 i2c->adap = octeon_i2c_ops;
Jan Glaubera035d712016-04-11 17:28:32 +02001155 i2c->adap.timeout = msecs_to_jiffies(2);
1156 i2c->adap.retries = 5;
Jan Glauberc981e342016-04-25 16:33:31 +02001157 i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info;
Rade Bozic85660f42010-01-28 12:47:07 -08001158 i2c->adap.dev.parent = &pdev->dev;
Jan Glauberdfcd8212016-03-18 09:46:26 +01001159 i2c->adap.dev.of_node = node;
Rade Bozic85660f42010-01-28 12:47:07 -08001160 i2c_set_adapdata(&i2c->adap, i2c);
1161 platform_set_drvdata(pdev, i2c);
1162
David Daneyf353a212012-07-05 18:12:39 +02001163 result = i2c_add_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -08001164 if (result < 0) {
1165 dev_err(i2c->dev, "failed to add adapter\n");
Doug Anderson55827f42013-02-15 13:18:35 +00001166 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001167 }
Jan Glauberdfcd8212016-03-18 09:46:26 +01001168 dev_info(i2c->dev, "probed\n");
David Daneyf353a212012-07-05 18:12:39 +02001169 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -08001170
Rade Bozic85660f42010-01-28 12:47:07 -08001171out:
1172 return result;
1173};
1174
Bill Pemberton0b255e92012-11-27 15:59:38 -05001175static int octeon_i2c_remove(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -08001176{
1177 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
1178
1179 i2c_del_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -08001180 return 0;
1181};
1182
Jan Glauberdfcd8212016-03-18 09:46:26 +01001183static const struct of_device_id octeon_i2c_match[] = {
1184 { .compatible = "cavium,octeon-3860-twsi", },
Jan Glauber4729cbe2016-04-25 16:33:35 +02001185 { .compatible = "cavium,octeon-7890-twsi", },
David Daneyf353a212012-07-05 18:12:39 +02001186 {},
1187};
1188MODULE_DEVICE_TABLE(of, octeon_i2c_match);
1189
Rade Bozic85660f42010-01-28 12:47:07 -08001190static struct platform_driver octeon_i2c_driver = {
1191 .probe = octeon_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001192 .remove = octeon_i2c_remove,
Rade Bozic85660f42010-01-28 12:47:07 -08001193 .driver = {
Rade Bozic85660f42010-01-28 12:47:07 -08001194 .name = DRV_NAME,
David Daneyf353a212012-07-05 18:12:39 +02001195 .of_match_table = octeon_i2c_match,
Rade Bozic85660f42010-01-28 12:47:07 -08001196 },
1197};
1198
Axel Lina3664b52012-01-12 20:32:04 +01001199module_platform_driver(octeon_i2c_driver);
Rade Bozic85660f42010-01-28 12:47:07 -08001200
1201MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
1202MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
1203MODULE_LICENSE("GPL");