blob: 6a4f0581a7e0bb294ad91ffdc1401ea4ca7fc717 [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
841 if (length < 1)
842 return -EINVAL;
843
Jan Glauberc57db702016-04-11 17:28:35 +0200844 octeon_i2c_data_write(i2c, (target << 1) | 1);
845 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800846
847 result = octeon_i2c_wait(i2c);
848 if (result)
849 return result;
850
Jan Glauberb4c715d2016-04-25 16:33:30 +0200851 /* address OK ? */
852 result = octeon_i2c_check_status(i2c, false);
853 if (result)
854 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800855
Jan Glauberb4c715d2016-04-25 16:33:30 +0200856 for (i = 0; i < length; i++) {
857 /* for the last byte TWSI_CTL_AAK must not be set */
858 if (i + 1 == length)
859 final_read = true;
860
861 /* clear iflg to allow next event */
862 if (final_read)
Jan Glauberc57db702016-04-11 17:28:35 +0200863 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Jan Glauberb4c715d2016-04-25 16:33:30 +0200864 else
865 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_AAK);
Rade Bozic85660f42010-01-28 12:47:07 -0800866
867 result = octeon_i2c_wait(i2c);
868 if (result)
869 return result;
870
Jan Glauberc57db702016-04-11 17:28:35 +0200871 data[i] = octeon_i2c_data_read(i2c);
David Daney886f6f82016-03-18 09:46:29 +0100872 if (recv_len && i == 0) {
873 if (data[i] > I2C_SMBUS_BLOCK_MAX + 1) {
874 dev_err(i2c->dev,
875 "%s: read len > I2C_SMBUS_BLOCK_MAX %d\n",
876 __func__, data[i]);
877 return -EPROTO;
878 }
879 length += data[i];
880 }
Jan Glauberb4c715d2016-04-25 16:33:30 +0200881
882 result = octeon_i2c_check_status(i2c, final_read);
883 if (result)
884 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800885 }
David Daney886f6f82016-03-18 09:46:29 +0100886 *rlength = length;
Rade Bozic85660f42010-01-28 12:47:07 -0800887 return 0;
888}
889
890/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100891 * octeon_i2c_xfer - The driver's master_xfer function
892 * @adap: Pointer to the i2c_adapter structure
893 * @msgs: Pointer to the messages to be processed
894 * @num: Length of the MSGS array
Rade Bozic85660f42010-01-28 12:47:07 -0800895 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100896 * Returns the number of messages processed, or a negative errno on failure.
Rade Bozic85660f42010-01-28 12:47:07 -0800897 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100898static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
Rade Bozic85660f42010-01-28 12:47:07 -0800899 int num)
900{
Rade Bozic85660f42010-01-28 12:47:07 -0800901 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100902 int i, ret = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800903
David Daneyd1fbff82016-04-25 16:33:34 +0200904 if (num == 1) {
905 if (msgs[0].len > 0 && msgs[0].len <= 8) {
906 if (msgs[0].flags & I2C_M_RD)
907 ret = octeon_i2c_hlc_read(i2c, msgs);
908 else
909 ret = octeon_i2c_hlc_write(i2c, msgs);
910 goto out;
911 }
912 } else if (num == 2) {
913 if ((msgs[0].flags & I2C_M_RD) == 0 &&
914 (msgs[1].flags & I2C_M_RECV_LEN) == 0 &&
915 msgs[0].len > 0 && msgs[0].len <= 2 &&
916 msgs[1].len > 0 && msgs[1].len <= 8 &&
917 msgs[0].addr == msgs[1].addr) {
918 if (msgs[1].flags & I2C_M_RD)
919 ret = octeon_i2c_hlc_comp_read(i2c, msgs);
920 else
921 ret = octeon_i2c_hlc_comp_write(i2c, msgs);
922 goto out;
923 }
924 }
925
Rade Bozic85660f42010-01-28 12:47:07 -0800926 for (i = 0; ret == 0 && i < num; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100927 struct i2c_msg *pmsg = &msgs[i];
928
Jan Glauberc981e342016-04-25 16:33:31 +0200929 ret = octeon_i2c_start(i2c);
930 if (ret)
931 return ret;
932
Rade Bozic85660f42010-01-28 12:47:07 -0800933 if (pmsg->flags & I2C_M_RD)
934 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
David Daney886f6f82016-03-18 09:46:29 +0100935 &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
Rade Bozic85660f42010-01-28 12:47:07 -0800936 else
937 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100938 pmsg->len);
Rade Bozic85660f42010-01-28 12:47:07 -0800939 }
940 octeon_i2c_stop(i2c);
David Daneyd1fbff82016-04-25 16:33:34 +0200941out:
Rade Bozic85660f42010-01-28 12:47:07 -0800942 return (ret != 0) ? ret : num;
943}
944
Jan Glauberc981e342016-04-25 16:33:31 +0200945static int octeon_i2c_get_scl(struct i2c_adapter *adap)
946{
947 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
948 u64 state;
949
950 state = octeon_i2c_read_int(i2c);
951 return state & TWSI_INT_SCL;
952}
953
954static void octeon_i2c_set_scl(struct i2c_adapter *adap, int val)
955{
956 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
957
958 octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
959}
960
961static int octeon_i2c_get_sda(struct i2c_adapter *adap)
962{
963 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
964 u64 state;
965
966 state = octeon_i2c_read_int(i2c);
967 return state & TWSI_INT_SDA;
968}
969
970static void octeon_i2c_prepare_recovery(struct i2c_adapter *adap)
971{
972 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
973
974 /*
975 * The stop resets the state machine, does not _transmit_ STOP unless
976 * engine was active.
977 */
978 octeon_i2c_stop(i2c);
979
David Daneyd1fbff82016-04-25 16:33:34 +0200980 octeon_i2c_hlc_disable(i2c);
Jan Glauberc981e342016-04-25 16:33:31 +0200981 octeon_i2c_write_int(i2c, 0);
982}
983
984static void octeon_i2c_unprepare_recovery(struct i2c_adapter *adap)
985{
986 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
987
988 octeon_i2c_write_int(i2c, 0);
989}
990
991static struct i2c_bus_recovery_info octeon_i2c_recovery_info = {
992 .recover_bus = i2c_generic_scl_recovery,
993 .get_scl = octeon_i2c_get_scl,
994 .set_scl = octeon_i2c_set_scl,
995 .get_sda = octeon_i2c_get_sda,
996 .prepare_recovery = octeon_i2c_prepare_recovery,
997 .unprepare_recovery = octeon_i2c_unprepare_recovery,
998};
999
Rade Bozic85660f42010-01-28 12:47:07 -08001000static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
1001{
David Daney886f6f82016-03-18 09:46:29 +01001002 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1003 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
Rade Bozic85660f42010-01-28 12:47:07 -08001004}
1005
1006static const struct i2c_algorithm octeon_i2c_algo = {
1007 .master_xfer = octeon_i2c_xfer,
1008 .functionality = octeon_i2c_functionality,
1009};
1010
1011static struct i2c_adapter octeon_i2c_ops = {
1012 .owner = THIS_MODULE,
1013 .name = "OCTEON adapter",
1014 .algo = &octeon_i2c_algo,
Rade Bozic85660f42010-01-28 12:47:07 -08001015};
1016
Bill Pemberton0b255e92012-11-27 15:59:38 -05001017static int octeon_i2c_probe(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -08001018{
Jan Glauberdfcd8212016-03-18 09:46:26 +01001019 struct device_node *node = pdev->dev.of_node;
Jan Glauber4729cbe2016-04-25 16:33:35 +02001020 int irq, result = 0, hlc_irq = 0;
Rade Bozic85660f42010-01-28 12:47:07 -08001021 struct resource *res_mem;
Jan Glauberdfcd8212016-03-18 09:46:26 +01001022 struct octeon_i2c *i2c;
Jan Glauber4729cbe2016-04-25 16:33:35 +02001023 bool cn78xx_style;
Rade Bozic85660f42010-01-28 12:47:07 -08001024
Jan Glauber4729cbe2016-04-25 16:33:35 +02001025 cn78xx_style = of_device_is_compatible(node, "cavium,octeon-7890-twsi");
1026 if (cn78xx_style) {
1027 hlc_irq = platform_get_irq(pdev, 0);
1028 if (hlc_irq < 0)
1029 return hlc_irq;
1030
1031 irq = platform_get_irq(pdev, 2);
1032 if (irq < 0)
1033 return irq;
1034 } else {
1035 /* All adaptors have an irq. */
1036 irq = platform_get_irq(pdev, 0);
1037 if (irq < 0)
1038 return irq;
1039 }
Rade Bozic85660f42010-01-28 12:47:07 -08001040
David Daneyf353a212012-07-05 18:12:39 +02001041 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Rade Bozic85660f42010-01-28 12:47:07 -08001042 if (!i2c) {
Rade Bozic85660f42010-01-28 12:47:07 -08001043 result = -ENOMEM;
1044 goto out;
1045 }
1046 i2c->dev = &pdev->dev;
Rade Bozic85660f42010-01-28 12:47:07 -08001047
1048 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jan Glauber54108e52016-03-18 09:46:27 +01001049 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
1050 if (IS_ERR(i2c->twsi_base)) {
1051 result = PTR_ERR(i2c->twsi_base);
David Daneyf353a212012-07-05 18:12:39 +02001052 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001053 }
Rade Bozic85660f42010-01-28 12:47:07 -08001054
David Daneyf353a212012-07-05 18:12:39 +02001055 /*
1056 * "clock-rate" is a legacy binding, the official binding is
1057 * "clock-frequency". Try the official one first and then
1058 * fall back if it doesn't exist.
1059 */
Jan Glauberdfcd8212016-03-18 09:46:26 +01001060 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
1061 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
David Daneyf353a212012-07-05 18:12:39 +02001062 dev_err(i2c->dev,
1063 "no I2C 'clock-rate' or 'clock-frequency' property\n");
1064 result = -ENXIO;
1065 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001066 }
David Daneyf353a212012-07-05 18:12:39 +02001067
1068 i2c->sys_freq = octeon_get_io_clock_rate();
1069
Rade Bozic85660f42010-01-28 12:47:07 -08001070 init_waitqueue_head(&i2c->queue);
1071
1072 i2c->irq = irq;
1073
Jan Glauber4729cbe2016-04-25 16:33:35 +02001074 if (cn78xx_style) {
1075 i2c->hlc_irq = hlc_irq;
1076
1077 i2c->int_enable = octeon_i2c_int_enable78;
1078 i2c->int_disable = octeon_i2c_int_disable78;
1079 i2c->hlc_int_enable = octeon_i2c_hlc_int_enable78;
1080 i2c->hlc_int_disable = octeon_i2c_hlc_int_disable78;
1081
1082 irq_set_status_flags(i2c->irq, IRQ_NOAUTOEN);
1083 irq_set_status_flags(i2c->hlc_irq, IRQ_NOAUTOEN);
1084
1085 result = devm_request_irq(&pdev->dev, i2c->hlc_irq,
1086 octeon_i2c_hlc_isr78, 0,
1087 DRV_NAME, i2c);
1088 if (result < 0) {
1089 dev_err(i2c->dev, "failed to attach interrupt\n");
1090 goto out;
1091 }
1092 } else {
1093 i2c->int_enable = octeon_i2c_int_enable;
1094 i2c->int_disable = octeon_i2c_int_disable;
1095 i2c->hlc_int_enable = octeon_i2c_hlc_int_enable;
1096 i2c->hlc_int_disable = octeon_i2c_int_disable;
1097 }
1098
David Daneyf353a212012-07-05 18:12:39 +02001099 result = devm_request_irq(&pdev->dev, i2c->irq,
1100 octeon_i2c_isr, 0, DRV_NAME, i2c);
Rade Bozic85660f42010-01-28 12:47:07 -08001101 if (result < 0) {
1102 dev_err(i2c->dev, "failed to attach interrupt\n");
David Daneyf353a212012-07-05 18:12:39 +02001103 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001104 }
1105
Jan Glauberdfcd8212016-03-18 09:46:26 +01001106 result = octeon_i2c_init_lowlevel(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -08001107 if (result) {
1108 dev_err(i2c->dev, "init low level failed\n");
David Daneyf353a212012-07-05 18:12:39 +02001109 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001110 }
1111
Jan Glauberdfcd8212016-03-18 09:46:26 +01001112 octeon_i2c_set_clock(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -08001113
1114 i2c->adap = octeon_i2c_ops;
Jan Glaubera035d712016-04-11 17:28:32 +02001115 i2c->adap.timeout = msecs_to_jiffies(2);
1116 i2c->adap.retries = 5;
Jan Glauberc981e342016-04-25 16:33:31 +02001117 i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info;
Rade Bozic85660f42010-01-28 12:47:07 -08001118 i2c->adap.dev.parent = &pdev->dev;
Jan Glauberdfcd8212016-03-18 09:46:26 +01001119 i2c->adap.dev.of_node = node;
Rade Bozic85660f42010-01-28 12:47:07 -08001120 i2c_set_adapdata(&i2c->adap, i2c);
1121 platform_set_drvdata(pdev, i2c);
1122
David Daneyf353a212012-07-05 18:12:39 +02001123 result = i2c_add_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -08001124 if (result < 0) {
1125 dev_err(i2c->dev, "failed to add adapter\n");
Doug Anderson55827f42013-02-15 13:18:35 +00001126 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -08001127 }
Jan Glauberdfcd8212016-03-18 09:46:26 +01001128 dev_info(i2c->dev, "probed\n");
David Daneyf353a212012-07-05 18:12:39 +02001129 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -08001130
Rade Bozic85660f42010-01-28 12:47:07 -08001131out:
1132 return result;
1133};
1134
Bill Pemberton0b255e92012-11-27 15:59:38 -05001135static int octeon_i2c_remove(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -08001136{
1137 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
1138
1139 i2c_del_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -08001140 return 0;
1141};
1142
Jan Glauberdfcd8212016-03-18 09:46:26 +01001143static const struct of_device_id octeon_i2c_match[] = {
1144 { .compatible = "cavium,octeon-3860-twsi", },
Jan Glauber4729cbe2016-04-25 16:33:35 +02001145 { .compatible = "cavium,octeon-7890-twsi", },
David Daneyf353a212012-07-05 18:12:39 +02001146 {},
1147};
1148MODULE_DEVICE_TABLE(of, octeon_i2c_match);
1149
Rade Bozic85660f42010-01-28 12:47:07 -08001150static struct platform_driver octeon_i2c_driver = {
1151 .probe = octeon_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001152 .remove = octeon_i2c_remove,
Rade Bozic85660f42010-01-28 12:47:07 -08001153 .driver = {
Rade Bozic85660f42010-01-28 12:47:07 -08001154 .name = DRV_NAME,
David Daneyf353a212012-07-05 18:12:39 +02001155 .of_match_table = octeon_i2c_match,
Rade Bozic85660f42010-01-28 12:47:07 -08001156 },
1157};
1158
Axel Lina3664b52012-01-12 20:32:04 +01001159module_platform_driver(octeon_i2c_driver);
Rade Bozic85660f42010-01-28 12:47:07 -08001160
1161MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
1162MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
1163MODULE_LICENSE("GPL");