blob: 4fc471ce173e6bac53cd8b33618e7d6ea4c4f1ef [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
112struct octeon_i2c {
113 wait_queue_head_t queue;
114 struct i2c_adapter adap;
115 int irq;
Jan Glauber4729cbe2016-04-25 16:33:35 +0200116 int hlc_irq; /* For cn7890 only */
David Daneyf353a212012-07-05 18:12:39 +0200117 u32 twsi_freq;
Rade Bozic85660f42010-01-28 12:47:07 -0800118 int sys_freq;
Rade Bozic85660f42010-01-28 12:47:07 -0800119 void __iomem *twsi_base;
Rade Bozic85660f42010-01-28 12:47:07 -0800120 struct device *dev;
David Daneyd1fbff82016-04-25 16:33:34 +0200121 bool hlc_enabled;
Jan Glauber4729cbe2016-04-25 16:33:35 +0200122 void (*int_enable)(struct octeon_i2c *);
123 void (*int_disable)(struct octeon_i2c *);
124 void (*hlc_int_enable)(struct octeon_i2c *);
125 void (*hlc_int_disable)(struct octeon_i2c *);
126 atomic_t int_enable_cnt;
127 atomic_t hlc_int_enable_cnt;
Rade Bozic85660f42010-01-28 12:47:07 -0800128};
129
Peter Swain30c24b22016-04-25 16:33:33 +0200130static void octeon_i2c_writeq_flush(u64 val, void __iomem *addr)
131{
132 __raw_writeq(val, addr);
133 __raw_readq(addr); /* wait for write to land */
134}
135
Rade Bozic85660f42010-01-28 12:47:07 -0800136/**
Jan Glauber9cb94802016-04-11 17:28:34 +0200137 * octeon_i2c_reg_write - write an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100138 * @i2c: The struct octeon_i2c
139 * @eop_reg: Register selector
140 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -0800141 *
142 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
143 */
Jan Glauber9cb94802016-04-11 17:28:34 +0200144static void octeon_i2c_reg_write(struct octeon_i2c *i2c, u64 eop_reg, u8 data)
Rade Bozic85660f42010-01-28 12:47:07 -0800145{
146 u64 tmp;
147
148 __raw_writeq(SW_TWSI_V | eop_reg | data, i2c->twsi_base + SW_TWSI);
149 do {
150 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
151 } while ((tmp & SW_TWSI_V) != 0);
152}
153
Jan Glauberc57db702016-04-11 17:28:35 +0200154#define octeon_i2c_ctl_write(i2c, val) \
155 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CTL, val)
156#define octeon_i2c_data_write(i2c, val) \
157 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_DATA, val)
158
Rade Bozic85660f42010-01-28 12:47:07 -0800159/**
Jan Glauber9cb94802016-04-11 17:28:34 +0200160 * octeon_i2c_reg_read - read lower bits of an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100161 * @i2c: The struct octeon_i2c
162 * @eop_reg: Register selector
Rade Bozic85660f42010-01-28 12:47:07 -0800163 *
164 * Returns the data.
165 *
166 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
167 */
Jan Glauber9cb94802016-04-11 17:28:34 +0200168static u8 octeon_i2c_reg_read(struct octeon_i2c *i2c, u64 eop_reg)
Rade Bozic85660f42010-01-28 12:47:07 -0800169{
170 u64 tmp;
171
172 __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI);
173 do {
174 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
175 } while ((tmp & SW_TWSI_V) != 0);
176
177 return tmp & 0xFF;
178}
179
Jan Glauberc57db702016-04-11 17:28:35 +0200180#define octeon_i2c_ctl_read(i2c) \
181 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_CTL)
182#define octeon_i2c_data_read(i2c) \
183 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_DATA)
184#define octeon_i2c_stat_read(i2c) \
185 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_STAT)
186
Rade Bozic85660f42010-01-28 12:47:07 -0800187/**
Jan Glauberc981e342016-04-25 16:33:31 +0200188 * octeon_i2c_read_int - read the TWSI_INT register
189 * @i2c: The struct octeon_i2c
190 *
191 * Returns the value of the register.
192 */
193static u64 octeon_i2c_read_int(struct octeon_i2c *i2c)
194{
195 return __raw_readq(i2c->twsi_base + TWSI_INT);
196}
197
198/**
Rade Bozic85660f42010-01-28 12:47:07 -0800199 * octeon_i2c_write_int - write the TWSI_INT register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100200 * @i2c: The struct octeon_i2c
201 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -0800202 */
203static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
204{
Peter Swain30c24b22016-04-25 16:33:33 +0200205 octeon_i2c_writeq_flush(data, i2c->twsi_base + TWSI_INT);
Rade Bozic85660f42010-01-28 12:47:07 -0800206}
207
208/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100209 * octeon_i2c_int_enable - enable the CORE interrupt
210 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800211 *
212 * The interrupt will be asserted when there is non-STAT_IDLE state in
213 * the SW_TWSI_EOP_TWSI_STAT register.
214 */
215static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
216{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100217 octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
Rade Bozic85660f42010-01-28 12:47:07 -0800218}
219
Jan Glauberbd7784c2016-03-07 16:10:44 +0100220/* disable the CORE interrupt */
Rade Bozic85660f42010-01-28 12:47:07 -0800221static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
222{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100223 /* clear TS/ST/IFLG events */
Rade Bozic85660f42010-01-28 12:47:07 -0800224 octeon_i2c_write_int(i2c, 0);
225}
226
Jan Glauber4729cbe2016-04-25 16:33:35 +0200227/**
228 * octeon_i2c_int_enable78 - enable the CORE interrupt
229 * @i2c: The struct octeon_i2c
230 *
231 * The interrupt will be asserted when there is non-STAT_IDLE state in the
232 * SW_TWSI_EOP_TWSI_STAT register.
233 */
234static void octeon_i2c_int_enable78(struct octeon_i2c *i2c)
235{
236 atomic_inc_return(&i2c->int_enable_cnt);
237 enable_irq(i2c->irq);
238}
239
240static void __octeon_i2c_irq_disable(atomic_t *cnt, int irq)
241{
242 int count;
243
244 /*
245 * The interrupt can be disabled in two places, but we only
246 * want to make the disable_irq_nosync() call once, so keep
247 * track with the atomic variable.
248 */
249 count = atomic_dec_if_positive(cnt);
250 if (count >= 0)
251 disable_irq_nosync(irq);
252}
253
254/* disable the CORE interrupt */
255static void octeon_i2c_int_disable78(struct octeon_i2c *i2c)
256{
257 __octeon_i2c_irq_disable(&i2c->int_enable_cnt, i2c->irq);
258}
259
260/**
261 * octeon_i2c_hlc_int_enable78 - enable the ST interrupt
262 * @i2c: The struct octeon_i2c
263 *
264 * The interrupt will be asserted when there is non-STAT_IDLE state in
265 * the SW_TWSI_EOP_TWSI_STAT register.
266 */
267static void octeon_i2c_hlc_int_enable78(struct octeon_i2c *i2c)
268{
269 atomic_inc_return(&i2c->hlc_int_enable_cnt);
270 enable_irq(i2c->hlc_irq);
271}
272
273/* disable the ST interrupt */
274static void octeon_i2c_hlc_int_disable78(struct octeon_i2c *i2c)
275{
276 __octeon_i2c_irq_disable(&i2c->hlc_int_enable_cnt, i2c->hlc_irq);
277}
278
David Daneyd1fbff82016-04-25 16:33:34 +0200279/*
280 * Cleanup low-level state & enable high-level controller.
281 */
282static void octeon_i2c_hlc_enable(struct octeon_i2c *i2c)
283{
284 int try = 0;
285 u64 val;
286
287 if (i2c->hlc_enabled)
288 return;
289 i2c->hlc_enabled = true;
290
291 while (1) {
292 val = octeon_i2c_ctl_read(i2c);
293 if (!(val & (TWSI_CTL_STA | TWSI_CTL_STP)))
294 break;
295
296 /* clear IFLG event */
297 if (val & TWSI_CTL_IFLG)
298 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
299
300 if (try++ > 100) {
301 pr_err("%s: giving up\n", __func__);
302 break;
303 }
304
305 /* spin until any start/stop has finished */
306 udelay(10);
307 }
308 octeon_i2c_ctl_write(i2c, TWSI_CTL_CE | TWSI_CTL_AAK | TWSI_CTL_ENAB);
309}
310
311static void octeon_i2c_hlc_disable(struct octeon_i2c *i2c)
312{
313 if (!i2c->hlc_enabled)
314 return;
315
316 i2c->hlc_enabled = false;
317 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
318}
319
Jan Glauberbd7784c2016-03-07 16:10:44 +0100320/* interrupt service routine */
Rade Bozic85660f42010-01-28 12:47:07 -0800321static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
322{
323 struct octeon_i2c *i2c = dev_id;
324
Jan Glauber4729cbe2016-04-25 16:33:35 +0200325 i2c->int_disable(i2c);
326 wake_up(&i2c->queue);
327
328 return IRQ_HANDLED;
329}
330
331/* HLC interrupt service routine */
332static irqreturn_t octeon_i2c_hlc_isr78(int irq, void *dev_id)
333{
334 struct octeon_i2c *i2c = dev_id;
335
336 i2c->hlc_int_disable(i2c);
송은봉2637e5f2013-04-17 21:40:17 +0000337 wake_up(&i2c->queue);
Rade Bozic85660f42010-01-28 12:47:07 -0800338
339 return IRQ_HANDLED;
340}
341
Rade Bozic85660f42010-01-28 12:47:07 -0800342static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
343{
Jan Glauberb69e5c62016-04-11 17:28:36 +0200344 return (octeon_i2c_ctl_read(i2c) & TWSI_CTL_IFLG);
Rade Bozic85660f42010-01-28 12:47:07 -0800345}
346
347/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100348 * octeon_i2c_wait - wait for the IFLG to be set
349 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800350 *
351 * Returns 0 on success, otherwise a negative errno.
352 */
353static int octeon_i2c_wait(struct octeon_i2c *i2c)
354{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100355 long time_left;
Rade Bozic85660f42010-01-28 12:47:07 -0800356
Jan Glauber4729cbe2016-04-25 16:33:35 +0200357 i2c->int_enable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100358 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
359 i2c->adap.timeout);
Jan Glauber4729cbe2016-04-25 16:33:35 +0200360 i2c->int_disable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100361 if (!time_left) {
Rade Bozic85660f42010-01-28 12:47:07 -0800362 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
Bernhard Wallecc33e542010-09-27 12:55:16 +0200363 return -ETIMEDOUT;
Rade Bozic85660f42010-01-28 12:47:07 -0800364 }
365
366 return 0;
367}
368
Jan Glauberb4c715d2016-04-25 16:33:30 +0200369static int octeon_i2c_check_status(struct octeon_i2c *i2c, int final_read)
370{
371 u8 stat = octeon_i2c_stat_read(i2c);
372
373 switch (stat) {
374 /* Everything is fine */
375 case STAT_IDLE:
376 case STAT_AD2W_ACK:
377 case STAT_RXADDR_ACK:
378 case STAT_TXADDR_ACK:
379 case STAT_TXDATA_ACK:
380 return 0;
381
382 /* ACK allowed on pre-terminal bytes only */
383 case STAT_RXDATA_ACK:
384 if (!final_read)
385 return 0;
386 return -EIO;
387
388 /* NAK allowed on terminal byte only */
389 case STAT_RXDATA_NAK:
390 if (final_read)
391 return 0;
392 return -EIO;
393
394 /* Arbitration lost */
395 case STAT_LOST_ARB_38:
396 case STAT_LOST_ARB_68:
397 case STAT_LOST_ARB_78:
398 case STAT_LOST_ARB_B0:
399 return -EAGAIN;
400
401 /* Being addressed as slave, should back off & listen */
402 case STAT_SLAVE_60:
403 case STAT_SLAVE_70:
404 case STAT_GENDATA_ACK:
405 case STAT_GENDATA_NAK:
406 return -EOPNOTSUPP;
407
408 /* Core busy as slave */
409 case STAT_SLAVE_80:
410 case STAT_SLAVE_88:
411 case STAT_SLAVE_A0:
412 case STAT_SLAVE_A8:
413 case STAT_SLAVE_LOST:
414 case STAT_SLAVE_NAK:
415 case STAT_SLAVE_ACK:
416 return -EOPNOTSUPP;
417
418 case STAT_TXDATA_NAK:
419 return -EIO;
420 case STAT_TXADDR_NAK:
421 case STAT_RXADDR_NAK:
422 case STAT_AD2W_NAK:
423 return -ENXIO;
424 default:
425 dev_err(i2c->dev, "unhandled state: %d\n", stat);
426 return -EIO;
427 }
428}
429
David Daneyd1fbff82016-04-25 16:33:34 +0200430static bool octeon_i2c_hlc_test_ready(struct octeon_i2c *i2c)
431{
432 u64 val = __raw_readq(i2c->twsi_base + SW_TWSI);
433
434 return (val & SW_TWSI_V) == 0;
435}
436
437static void octeon_i2c_hlc_int_enable(struct octeon_i2c *i2c)
438{
439 octeon_i2c_write_int(i2c, TWSI_INT_ST_EN);
440}
441
442static void octeon_i2c_hlc_int_clear(struct octeon_i2c *i2c)
443{
444 /* clear ST/TS events, listen for neither */
445 octeon_i2c_write_int(i2c, TWSI_INT_ST_INT | TWSI_INT_TS_INT);
446}
447
448/**
449 * octeon_i2c_hlc_wait - wait for an HLC operation to complete
450 * @i2c: The struct octeon_i2c
451 *
452 * Returns 0 on success, otherwise -ETIMEDOUT.
453 */
454static int octeon_i2c_hlc_wait(struct octeon_i2c *i2c)
455{
456 int time_left;
457
Jan Glauber4729cbe2016-04-25 16:33:35 +0200458 i2c->hlc_int_enable(i2c);
David Daneyd1fbff82016-04-25 16:33:34 +0200459 time_left = wait_event_timeout(i2c->queue,
460 octeon_i2c_hlc_test_ready(i2c),
461 i2c->adap.timeout);
Jan Glauber4729cbe2016-04-25 16:33:35 +0200462 i2c->hlc_int_disable(i2c);
David Daneyd1fbff82016-04-25 16:33:34 +0200463 if (!time_left) {
464 octeon_i2c_hlc_int_clear(i2c);
465 return -ETIMEDOUT;
466 }
467 return 0;
468}
469
470/* high-level-controller pure read of up to 8 bytes */
471static int octeon_i2c_hlc_read(struct octeon_i2c *i2c, struct i2c_msg *msgs)
472{
473 int i, j, ret = 0;
474 u64 cmd;
475
476 octeon_i2c_hlc_enable(i2c);
477 octeon_i2c_hlc_int_clear(i2c);
478
479 cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR;
480 /* SIZE */
481 cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT;
482 /* A */
483 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
484
485 if (msgs[0].flags & I2C_M_TEN)
486 cmd |= SW_TWSI_OP_10;
487 else
488 cmd |= SW_TWSI_OP_7;
489
490 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
491 ret = octeon_i2c_hlc_wait(i2c);
492 if (ret)
493 goto err;
494
495 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
496 if ((cmd & SW_TWSI_R) == 0)
497 return -EAGAIN;
498
499 for (i = 0, j = msgs[0].len - 1; i < msgs[0].len && i < 4; i++, j--)
500 msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff;
501
502 if (msgs[0].len > 4) {
503 cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT);
504 for (i = 0; i < msgs[0].len - 4 && i < 4; i++, j--)
505 msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff;
506 }
507
508err:
509 return ret;
510}
511
512/* high-level-controller pure write of up to 8 bytes */
513static int octeon_i2c_hlc_write(struct octeon_i2c *i2c, struct i2c_msg *msgs)
514{
515 int i, j, ret = 0;
516 u64 cmd;
517
518 octeon_i2c_hlc_enable(i2c);
519 octeon_i2c_hlc_int_clear(i2c);
520
521 cmd = SW_TWSI_V | SW_TWSI_SOVR;
522 /* SIZE */
523 cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT;
524 /* A */
525 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
526
527 if (msgs[0].flags & I2C_M_TEN)
528 cmd |= SW_TWSI_OP_10;
529 else
530 cmd |= SW_TWSI_OP_7;
531
532 for (i = 0, j = msgs[0].len - 1; i < msgs[0].len && i < 4; i++, j--)
533 cmd |= (u64)msgs[0].buf[j] << (8 * i);
534
535 if (msgs[0].len > 4) {
536 u64 ext = 0;
537
538 for (i = 0; i < msgs[0].len - 4 && i < 4; i++, j--)
539 ext |= (u64)msgs[0].buf[j] << (8 * i);
540 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT);
541 }
542
543 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
544 ret = octeon_i2c_hlc_wait(i2c);
545 if (ret)
546 goto err;
547
548 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
549 if ((cmd & SW_TWSI_R) == 0)
550 return -EAGAIN;
551
552 ret = octeon_i2c_check_status(i2c, false);
553
554err:
555 return ret;
556}
557
558/* high-level-controller composite write+read, msg0=addr, msg1=data */
559static int octeon_i2c_hlc_comp_read(struct octeon_i2c *i2c, struct i2c_msg *msgs)
560{
561 int i, j, ret = 0;
562 u64 cmd;
563
564 octeon_i2c_hlc_enable(i2c);
565
566 cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR;
567 /* SIZE */
568 cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT;
569 /* A */
570 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
571
572 if (msgs[0].flags & I2C_M_TEN)
573 cmd |= SW_TWSI_OP_10_IA;
574 else
575 cmd |= SW_TWSI_OP_7_IA;
576
577 if (msgs[0].len == 2) {
578 u64 ext = 0;
579
580 cmd |= SW_TWSI_EIA;
581 ext = (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
582 cmd |= (u64)msgs[0].buf[1] << SW_TWSI_IA_SHIFT;
583 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT);
584 } else {
585 cmd |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
586 }
587
588 octeon_i2c_hlc_int_clear(i2c);
589 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
590
591 ret = octeon_i2c_hlc_wait(i2c);
592 if (ret)
593 goto err;
594
595 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
596 if ((cmd & SW_TWSI_R) == 0)
597 return -EAGAIN;
598
599 for (i = 0, j = msgs[1].len - 1; i < msgs[1].len && i < 4; i++, j--)
600 msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff;
601
602 if (msgs[1].len > 4) {
603 cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT);
604 for (i = 0; i < msgs[1].len - 4 && i < 4; i++, j--)
605 msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff;
606 }
607
608err:
609 return ret;
610}
611
612/* high-level-controller composite write+write, m[0]len<=2, m[1]len<=8 */
613static int octeon_i2c_hlc_comp_write(struct octeon_i2c *i2c, struct i2c_msg *msgs)
614{
615 bool set_ext = false;
616 int i, j, ret = 0;
617 u64 cmd, ext = 0;
618
619 octeon_i2c_hlc_enable(i2c);
620
621 cmd = SW_TWSI_V | SW_TWSI_SOVR;
622 /* SIZE */
623 cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT;
624 /* A */
625 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
626
627 if (msgs[0].flags & I2C_M_TEN)
628 cmd |= SW_TWSI_OP_10_IA;
629 else
630 cmd |= SW_TWSI_OP_7_IA;
631
632 if (msgs[0].len == 2) {
633 cmd |= SW_TWSI_EIA;
634 ext |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
635 set_ext = true;
636 cmd |= (u64)msgs[0].buf[1] << SW_TWSI_IA_SHIFT;
637 } else {
638 cmd |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
639 }
640
641 for (i = 0, j = msgs[1].len - 1; i < msgs[1].len && i < 4; i++, j--)
642 cmd |= (u64)msgs[1].buf[j] << (8 * i);
643
644 if (msgs[1].len > 4) {
645 for (i = 0; i < msgs[1].len - 4 && i < 4; i++, j--)
646 ext |= (u64)msgs[1].buf[j] << (8 * i);
647 set_ext = true;
648 }
649 if (set_ext)
650 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT);
651
652 octeon_i2c_hlc_int_clear(i2c);
653 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
654
655 ret = octeon_i2c_hlc_wait(i2c);
656 if (ret)
657 goto err;
658
659 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
660 if ((cmd & SW_TWSI_R) == 0)
661 return -EAGAIN;
662
663 ret = octeon_i2c_check_status(i2c, false);
664
665err:
666 return ret;
667}
668
Jan Glauberf541bb32016-04-11 17:28:33 +0200669/* calculate and set clock divisors */
670static void octeon_i2c_set_clock(struct octeon_i2c *i2c)
671{
672 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
673 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
674
675 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
676 /*
677 * An mdiv value of less than 2 seems to not work well
678 * with ds1337 RTCs, so we constrain it to larger values.
679 */
680 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
681 /*
682 * For given ndiv and mdiv values check the
683 * two closest thp values.
684 */
685 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
686 tclk *= (1 << ndiv_idx);
687 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
688
689 for (inc = 0; inc <= 1; inc++) {
690 thp_idx = thp_base + inc;
691 if (thp_idx < 5 || thp_idx > 0xff)
692 continue;
693
694 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
695 foscl = foscl / (1 << ndiv_idx);
696 foscl = foscl / (mdiv_idx + 1) / 10;
697 diff = abs(foscl - i2c->twsi_freq);
698 if (diff < delta_hz) {
699 delta_hz = diff;
700 thp = thp_idx;
701 mdiv = mdiv_idx;
702 ndiv = ndiv_idx;
703 }
704 }
705 }
706 }
Jan Glauber9cb94802016-04-11 17:28:34 +0200707 octeon_i2c_reg_write(i2c, SW_TWSI_OP_TWSI_CLK, thp);
708 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
Jan Glauberf541bb32016-04-11 17:28:33 +0200709}
710
711static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
712{
David Daneyd1fbff82016-04-25 16:33:34 +0200713 u8 status = 0;
Jan Glauberf541bb32016-04-11 17:28:33 +0200714 int tries;
715
Jan Glauberf541bb32016-04-11 17:28:33 +0200716 /* reset controller */
Jan Glauber9cb94802016-04-11 17:28:34 +0200717 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0);
Jan Glauberf541bb32016-04-11 17:28:33 +0200718
David Daneyd1fbff82016-04-25 16:33:34 +0200719 for (tries = 10; tries && status != STAT_IDLE; tries--) {
Jan Glauberf541bb32016-04-11 17:28:33 +0200720 udelay(1);
Jan Glauberc57db702016-04-11 17:28:35 +0200721 status = octeon_i2c_stat_read(i2c);
Jan Glauberf541bb32016-04-11 17:28:33 +0200722 if (status == STAT_IDLE)
David Daneyd1fbff82016-04-25 16:33:34 +0200723 break;
Jan Glauberf541bb32016-04-11 17:28:33 +0200724 }
David Daneyd1fbff82016-04-25 16:33:34 +0200725
726 if (status != STAT_IDLE) {
727 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n",
728 __func__, status);
729 return -EIO;
730 }
731
732 /* toggle twice to force both teardowns */
733 octeon_i2c_hlc_enable(i2c);
734 octeon_i2c_hlc_disable(i2c);
735 return 0;
Jan Glauberf541bb32016-04-11 17:28:33 +0200736}
737
Jan Glauberc981e342016-04-25 16:33:31 +0200738static int octeon_i2c_recovery(struct octeon_i2c *i2c)
739{
740 int ret;
741
742 ret = i2c_recover_bus(&i2c->adap);
743 if (ret)
744 /* recover failed, try hardware re-init */
745 ret = octeon_i2c_init_lowlevel(i2c);
746 return ret;
747}
748
Rade Bozic85660f42010-01-28 12:47:07 -0800749/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100750 * octeon_i2c_start - send START to the bus
751 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800752 *
753 * Returns 0 on success, otherwise a negative errno.
754 */
755static int octeon_i2c_start(struct octeon_i2c *i2c)
756{
Jan Glauberc981e342016-04-25 16:33:31 +0200757 int ret;
758 u8 stat;
Rade Bozic85660f42010-01-28 12:47:07 -0800759
David Daneyd1fbff82016-04-25 16:33:34 +0200760 octeon_i2c_hlc_disable(i2c);
761
Jan Glauberc57db702016-04-11 17:28:35 +0200762 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
Jan Glauberc981e342016-04-25 16:33:31 +0200763 ret = octeon_i2c_wait(i2c);
764 if (ret)
765 goto error;
Rade Bozic85660f42010-01-28 12:47:07 -0800766
Jan Glauberc981e342016-04-25 16:33:31 +0200767 stat = octeon_i2c_stat_read(i2c);
768 if (stat == STAT_START || stat == STAT_REP_START)
769 /* START successful, bail out */
770 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800771
Jan Glauberc981e342016-04-25 16:33:31 +0200772error:
773 /* START failed, try to recover */
774 ret = octeon_i2c_recovery(i2c);
775 return (ret) ? ret : -EAGAIN;
Rade Bozic85660f42010-01-28 12:47:07 -0800776}
777
Jan Glauberdfcd8212016-03-18 09:46:26 +0100778/* send STOP to the bus */
779static void octeon_i2c_stop(struct octeon_i2c *i2c)
Rade Bozic85660f42010-01-28 12:47:07 -0800780{
Jan Glauberc57db702016-04-11 17:28:35 +0200781 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STP);
Rade Bozic85660f42010-01-28 12:47:07 -0800782}
783
784/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100785 * octeon_i2c_write - send data to the bus via low-level controller
786 * @i2c: The struct octeon_i2c
787 * @target: Target address
788 * @data: Pointer to the data to be sent
789 * @length: Length of the data
Rade Bozic85660f42010-01-28 12:47:07 -0800790 *
791 * The address is sent over the bus, then the data.
792 *
793 * Returns 0 on success, otherwise a negative errno.
794 */
795static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
796 const u8 *data, int length)
797{
798 int i, result;
Rade Bozic85660f42010-01-28 12:47:07 -0800799
Jan Glauberc57db702016-04-11 17:28:35 +0200800 octeon_i2c_data_write(i2c, target << 1);
801 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800802
803 result = octeon_i2c_wait(i2c);
804 if (result)
805 return result;
806
807 for (i = 0; i < length; i++) {
Jan Glauberb4c715d2016-04-25 16:33:30 +0200808 result = octeon_i2c_check_status(i2c, false);
809 if (result)
810 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800811
Jan Glauberc57db702016-04-11 17:28:35 +0200812 octeon_i2c_data_write(i2c, data[i]);
813 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800814
815 result = octeon_i2c_wait(i2c);
816 if (result)
817 return result;
818 }
819
820 return 0;
821}
822
823/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100824 * octeon_i2c_read - receive data from the bus via low-level controller
825 * @i2c: The struct octeon_i2c
826 * @target: Target address
827 * @data: Pointer to the location to store the data
David Daney886f6f82016-03-18 09:46:29 +0100828 * @rlength: Length of the data
829 * @recv_len: flag for length byte
Rade Bozic85660f42010-01-28 12:47:07 -0800830 *
831 * The address is sent over the bus, then the data is read.
832 *
833 * Returns 0 on success, otherwise a negative errno.
834 */
835static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
David Daney886f6f82016-03-18 09:46:29 +0100836 u8 *data, u16 *rlength, bool recv_len)
Rade Bozic85660f42010-01-28 12:47:07 -0800837{
David Daney886f6f82016-03-18 09:46:29 +0100838 int i, result, length = *rlength;
Jan Glauberb4c715d2016-04-25 16:33:30 +0200839 bool final_read = false;
Rade Bozic85660f42010-01-28 12:47:07 -0800840
Jan Glauberc57db702016-04-11 17:28:35 +0200841 octeon_i2c_data_write(i2c, (target << 1) | 1);
842 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800843
844 result = octeon_i2c_wait(i2c);
845 if (result)
846 return result;
847
Jan Glauberb4c715d2016-04-25 16:33:30 +0200848 /* address OK ? */
849 result = octeon_i2c_check_status(i2c, false);
850 if (result)
851 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800852
Jan Glauberb4c715d2016-04-25 16:33:30 +0200853 for (i = 0; i < length; i++) {
854 /* for the last byte TWSI_CTL_AAK must not be set */
855 if (i + 1 == length)
856 final_read = true;
857
858 /* clear iflg to allow next event */
859 if (final_read)
Jan Glauberc57db702016-04-11 17:28:35 +0200860 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Jan Glauberb4c715d2016-04-25 16:33:30 +0200861 else
862 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_AAK);
Rade Bozic85660f42010-01-28 12:47:07 -0800863
864 result = octeon_i2c_wait(i2c);
865 if (result)
866 return result;
867
Jan Glauberc57db702016-04-11 17:28:35 +0200868 data[i] = octeon_i2c_data_read(i2c);
David Daney886f6f82016-03-18 09:46:29 +0100869 if (recv_len && i == 0) {
870 if (data[i] > I2C_SMBUS_BLOCK_MAX + 1) {
871 dev_err(i2c->dev,
872 "%s: read len > I2C_SMBUS_BLOCK_MAX %d\n",
873 __func__, data[i]);
874 return -EPROTO;
875 }
876 length += data[i];
877 }
Jan Glauberb4c715d2016-04-25 16:33:30 +0200878
879 result = octeon_i2c_check_status(i2c, final_read);
880 if (result)
881 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800882 }
David Daney886f6f82016-03-18 09:46:29 +0100883 *rlength = length;
Rade Bozic85660f42010-01-28 12:47:07 -0800884 return 0;
885}
886
887/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100888 * octeon_i2c_xfer - The driver's master_xfer function
889 * @adap: Pointer to the i2c_adapter structure
890 * @msgs: Pointer to the messages to be processed
891 * @num: Length of the MSGS array
Rade Bozic85660f42010-01-28 12:47:07 -0800892 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100893 * Returns the number of messages processed, or a negative errno on failure.
Rade Bozic85660f42010-01-28 12:47:07 -0800894 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100895static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
Rade Bozic85660f42010-01-28 12:47:07 -0800896 int num)
897{
Rade Bozic85660f42010-01-28 12:47:07 -0800898 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100899 int i, ret = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800900
David Daneyd1fbff82016-04-25 16:33:34 +0200901 if (num == 1) {
902 if (msgs[0].len > 0 && msgs[0].len <= 8) {
903 if (msgs[0].flags & I2C_M_RD)
904 ret = octeon_i2c_hlc_read(i2c, msgs);
905 else
906 ret = octeon_i2c_hlc_write(i2c, msgs);
907 goto out;
908 }
909 } else if (num == 2) {
910 if ((msgs[0].flags & I2C_M_RD) == 0 &&
911 (msgs[1].flags & I2C_M_RECV_LEN) == 0 &&
912 msgs[0].len > 0 && msgs[0].len <= 2 &&
913 msgs[1].len > 0 && msgs[1].len <= 8 &&
914 msgs[0].addr == msgs[1].addr) {
915 if (msgs[1].flags & I2C_M_RD)
916 ret = octeon_i2c_hlc_comp_read(i2c, msgs);
917 else
918 ret = octeon_i2c_hlc_comp_write(i2c, msgs);
919 goto out;
920 }
921 }
922
Rade Bozic85660f42010-01-28 12:47:07 -0800923 for (i = 0; ret == 0 && i < num; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100924 struct i2c_msg *pmsg = &msgs[i];
925
Jan Glauber392d01d2016-04-26 16:26:52 +0200926 /* zero-length messages are not supported */
927 if (!pmsg->len) {
928 ret = -EOPNOTSUPP;
929 break;
930 }
931
Jan Glauberc981e342016-04-25 16:33:31 +0200932 ret = octeon_i2c_start(i2c);
933 if (ret)
934 return ret;
935
Rade Bozic85660f42010-01-28 12:47:07 -0800936 if (pmsg->flags & I2C_M_RD)
937 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
David Daney886f6f82016-03-18 09:46:29 +0100938 &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
Rade Bozic85660f42010-01-28 12:47:07 -0800939 else
940 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100941 pmsg->len);
Rade Bozic85660f42010-01-28 12:47:07 -0800942 }
943 octeon_i2c_stop(i2c);
David Daneyd1fbff82016-04-25 16:33:34 +0200944out:
Rade Bozic85660f42010-01-28 12:47:07 -0800945 return (ret != 0) ? ret : num;
946}
947
Jan Glauberc981e342016-04-25 16:33:31 +0200948static int octeon_i2c_get_scl(struct i2c_adapter *adap)
949{
950 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
951 u64 state;
952
953 state = octeon_i2c_read_int(i2c);
954 return state & TWSI_INT_SCL;
955}
956
957static void octeon_i2c_set_scl(struct i2c_adapter *adap, int val)
958{
959 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
960
961 octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
962}
963
964static int octeon_i2c_get_sda(struct i2c_adapter *adap)
965{
966 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
967 u64 state;
968
969 state = octeon_i2c_read_int(i2c);
970 return state & TWSI_INT_SDA;
971}
972
973static void octeon_i2c_prepare_recovery(struct i2c_adapter *adap)
974{
975 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
976
977 /*
978 * The stop resets the state machine, does not _transmit_ STOP unless
979 * engine was active.
980 */
981 octeon_i2c_stop(i2c);
982
David Daneyd1fbff82016-04-25 16:33:34 +0200983 octeon_i2c_hlc_disable(i2c);
Jan Glauberc981e342016-04-25 16:33:31 +0200984 octeon_i2c_write_int(i2c, 0);
985}
986
987static void octeon_i2c_unprepare_recovery(struct i2c_adapter *adap)
988{
989 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
990
991 octeon_i2c_write_int(i2c, 0);
992}
993
994static struct i2c_bus_recovery_info octeon_i2c_recovery_info = {
995 .recover_bus = i2c_generic_scl_recovery,
996 .get_scl = octeon_i2c_get_scl,
997 .set_scl = octeon_i2c_set_scl,
998 .get_sda = octeon_i2c_get_sda,
999 .prepare_recovery = octeon_i2c_prepare_recovery,
1000 .unprepare_recovery = octeon_i2c_unprepare_recovery,
1001};
1002
Rade Bozic85660f42010-01-28 12:47:07 -08001003static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
1004{
Jan Glauber392d01d2016-04-26 16:26:52 +02001005 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
David Daney886f6f82016-03-18 09:46:29 +01001006 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
Rade Bozic85660f42010-01-28 12:47:07 -08001007}
1008
1009static const struct i2c_algorithm octeon_i2c_algo = {
1010 .master_xfer = octeon_i2c_xfer,
1011 .functionality = octeon_i2c_functionality,
1012};
1013
1014static struct i2c_adapter octeon_i2c_ops = {
1015 .owner = THIS_MODULE,
1016 .name = "OCTEON adapter",
1017 .algo = &octeon_i2c_algo,
Rade Bozic85660f42010-01-28 12:47:07 -08001018};
1019
Bill Pemberton0b255e92012-11-27 15:59:38 -05001020static int octeon_i2c_probe(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -08001021{
Jan Glauberdfcd8212016-03-18 09:46:26 +01001022 struct device_node *node = pdev->dev.of_node;
Jan Glauber4729cbe2016-04-25 16:33:35 +02001023 int irq, result = 0, hlc_irq = 0;
Rade Bozic85660f42010-01-28 12:47:07 -08001024 struct resource *res_mem;
Jan Glauberdfcd8212016-03-18 09:46:26 +01001025 struct octeon_i2c *i2c;
Jan Glauber4729cbe2016-04-25 16:33:35 +02001026 bool cn78xx_style;
Rade Bozic85660f42010-01-28 12:47:07 -08001027
Jan Glauber4729cbe2016-04-25 16:33:35 +02001028 cn78xx_style = of_device_is_compatible(node, "cavium,octeon-7890-twsi");
1029 if (cn78xx_style) {
1030 hlc_irq = platform_get_irq(pdev, 0);
1031 if (hlc_irq < 0)
1032 return hlc_irq;
1033
1034 irq = platform_get_irq(pdev, 2);
1035 if (irq < 0)
1036 return irq;
1037 } else {
1038 /* All adaptors have an irq. */
1039 irq = platform_get_irq(pdev, 0);
1040 if (irq < 0)
1041 return irq;
1042 }
Rade Bozic85660f42010-01-28 12:47:07 -08001043
David Daneyf353a212012-07-05 18:12:39 +02001044 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Rade Bozic85660f42010-01-28 12:47:07 -08001045 if (!i2c) {
Rade Bozic85660f42010-01-28 12:47:07 -08001046 result = -ENOMEM;
1047 goto out;
1048 }
1049 i2c->dev = &pdev->dev;
Rade Bozic85660f42010-01-28 12:47:07 -08001050
1051 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jan Glauber54108e52016-03-18 09:46:27 +01001052 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
1053 if (IS_ERR(i2c->twsi_base)) {
1054 result = PTR_ERR(i2c->twsi_base);
David Daneyf353a212012-07-05 18:12:39 +02001055 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001056 }
Rade Bozic85660f42010-01-28 12:47:07 -08001057
David Daneyf353a212012-07-05 18:12:39 +02001058 /*
1059 * "clock-rate" is a legacy binding, the official binding is
1060 * "clock-frequency". Try the official one first and then
1061 * fall back if it doesn't exist.
1062 */
Jan Glauberdfcd8212016-03-18 09:46:26 +01001063 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
1064 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
David Daneyf353a212012-07-05 18:12:39 +02001065 dev_err(i2c->dev,
1066 "no I2C 'clock-rate' or 'clock-frequency' property\n");
1067 result = -ENXIO;
1068 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001069 }
David Daneyf353a212012-07-05 18:12:39 +02001070
1071 i2c->sys_freq = octeon_get_io_clock_rate();
1072
Rade Bozic85660f42010-01-28 12:47:07 -08001073 init_waitqueue_head(&i2c->queue);
1074
1075 i2c->irq = irq;
1076
Jan Glauber4729cbe2016-04-25 16:33:35 +02001077 if (cn78xx_style) {
1078 i2c->hlc_irq = hlc_irq;
1079
1080 i2c->int_enable = octeon_i2c_int_enable78;
1081 i2c->int_disable = octeon_i2c_int_disable78;
1082 i2c->hlc_int_enable = octeon_i2c_hlc_int_enable78;
1083 i2c->hlc_int_disable = octeon_i2c_hlc_int_disable78;
1084
1085 irq_set_status_flags(i2c->irq, IRQ_NOAUTOEN);
1086 irq_set_status_flags(i2c->hlc_irq, IRQ_NOAUTOEN);
1087
1088 result = devm_request_irq(&pdev->dev, i2c->hlc_irq,
1089 octeon_i2c_hlc_isr78, 0,
1090 DRV_NAME, i2c);
1091 if (result < 0) {
1092 dev_err(i2c->dev, "failed to attach interrupt\n");
1093 goto out;
1094 }
1095 } else {
1096 i2c->int_enable = octeon_i2c_int_enable;
1097 i2c->int_disable = octeon_i2c_int_disable;
1098 i2c->hlc_int_enable = octeon_i2c_hlc_int_enable;
1099 i2c->hlc_int_disable = octeon_i2c_int_disable;
1100 }
1101
David Daneyf353a212012-07-05 18:12:39 +02001102 result = devm_request_irq(&pdev->dev, i2c->irq,
1103 octeon_i2c_isr, 0, DRV_NAME, i2c);
Rade Bozic85660f42010-01-28 12:47:07 -08001104 if (result < 0) {
1105 dev_err(i2c->dev, "failed to attach interrupt\n");
David Daneyf353a212012-07-05 18:12:39 +02001106 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001107 }
1108
Jan Glauberdfcd8212016-03-18 09:46:26 +01001109 result = octeon_i2c_init_lowlevel(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -08001110 if (result) {
1111 dev_err(i2c->dev, "init low level failed\n");
David Daneyf353a212012-07-05 18:12:39 +02001112 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001113 }
1114
Jan Glauberdfcd8212016-03-18 09:46:26 +01001115 octeon_i2c_set_clock(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -08001116
1117 i2c->adap = octeon_i2c_ops;
Jan Glaubera035d712016-04-11 17:28:32 +02001118 i2c->adap.timeout = msecs_to_jiffies(2);
1119 i2c->adap.retries = 5;
Jan Glauberc981e342016-04-25 16:33:31 +02001120 i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info;
Rade Bozic85660f42010-01-28 12:47:07 -08001121 i2c->adap.dev.parent = &pdev->dev;
Jan Glauberdfcd8212016-03-18 09:46:26 +01001122 i2c->adap.dev.of_node = node;
Rade Bozic85660f42010-01-28 12:47:07 -08001123 i2c_set_adapdata(&i2c->adap, i2c);
1124 platform_set_drvdata(pdev, i2c);
1125
David Daneyf353a212012-07-05 18:12:39 +02001126 result = i2c_add_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -08001127 if (result < 0) {
1128 dev_err(i2c->dev, "failed to add adapter\n");
Doug Anderson55827f42013-02-15 13:18:35 +00001129 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001130 }
Jan Glauberdfcd8212016-03-18 09:46:26 +01001131 dev_info(i2c->dev, "probed\n");
David Daneyf353a212012-07-05 18:12:39 +02001132 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -08001133
Rade Bozic85660f42010-01-28 12:47:07 -08001134out:
1135 return result;
1136};
1137
Bill Pemberton0b255e92012-11-27 15:59:38 -05001138static int octeon_i2c_remove(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -08001139{
1140 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
1141
1142 i2c_del_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -08001143 return 0;
1144};
1145
Jan Glauberdfcd8212016-03-18 09:46:26 +01001146static const struct of_device_id octeon_i2c_match[] = {
1147 { .compatible = "cavium,octeon-3860-twsi", },
Jan Glauber4729cbe2016-04-25 16:33:35 +02001148 { .compatible = "cavium,octeon-7890-twsi", },
David Daneyf353a212012-07-05 18:12:39 +02001149 {},
1150};
1151MODULE_DEVICE_TABLE(of, octeon_i2c_match);
1152
Rade Bozic85660f42010-01-28 12:47:07 -08001153static struct platform_driver octeon_i2c_driver = {
1154 .probe = octeon_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001155 .remove = octeon_i2c_remove,
Rade Bozic85660f42010-01-28 12:47:07 -08001156 .driver = {
Rade Bozic85660f42010-01-28 12:47:07 -08001157 .name = DRV_NAME,
David Daneyf353a212012-07-05 18:12:39 +02001158 .of_match_table = octeon_i2c_match,
Rade Bozic85660f42010-01-28 12:47:07 -08001159 },
1160};
1161
Axel Lina3664b52012-01-12 20:32:04 +01001162module_platform_driver(octeon_i2c_driver);
Rade Bozic85660f42010-01-28 12:47:07 -08001163
1164MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
1165MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
1166MODULE_LICENSE("GPL");