blob: f83f6b8892b5a6505fdaabe30dae48a43ed6dce7 [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
David Daneyf353a212012-07-05 18:12:39 +020014#include <linux/platform_device.h>
15#include <linux/interrupt.h>
Rade Bozic85660f42010-01-28 12:47:07 -080016#include <linux/kernel.h>
17#include <linux/module.h>
David Daneyf353a212012-07-05 18:12:39 +020018#include <linux/delay.h>
Rade Bozic85660f42010-01-28 12:47:07 -080019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Rade Bozic85660f42010-01-28 12:47:07 -080021#include <linux/i2c.h>
David Daneyf353a212012-07-05 18:12:39 +020022#include <linux/io.h>
23#include <linux/of.h>
Rade Bozic85660f42010-01-28 12:47:07 -080024
25#include <asm/octeon/octeon.h>
26
27#define DRV_NAME "i2c-octeon"
28
Jan Glauberdfcd8212016-03-18 09:46:26 +010029/* Register offsets */
30#define SW_TWSI 0x00
31#define TWSI_INT 0x10
Rade Bozic85660f42010-01-28 12:47:07 -080032
33/* Controller command patterns */
Jan Glauberdfcd8212016-03-18 09:46:26 +010034#define SW_TWSI_V BIT_ULL(63) /* Valid bit */
35#define SW_TWSI_R BIT_ULL(56) /* Result or read bit */
36
37/* Controller opcode word (bits 60:57) */
38#define SW_TWSI_OP_SHIFT 57
39#define SW_TWSI_OP_TWSI_CLK (4ULL << SW_TWSI_OP_SHIFT)
40#define SW_TWSI_OP_EOP (6ULL << SW_TWSI_OP_SHIFT) /* Extended opcode */
41
42/* Controller extended opcode word (bits 34:32) */
43#define SW_TWSI_EOP_SHIFT 32
44#define SW_TWSI_EOP_TWSI_DATA (SW_TWSI_OP_EOP | 1ULL << SW_TWSI_EOP_SHIFT)
45#define SW_TWSI_EOP_TWSI_CTL (SW_TWSI_OP_EOP | 2ULL << SW_TWSI_EOP_SHIFT)
46#define SW_TWSI_EOP_TWSI_CLKCTL (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
47#define SW_TWSI_EOP_TWSI_STAT (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
48#define SW_TWSI_EOP_TWSI_RST (SW_TWSI_OP_EOP | 7ULL << SW_TWSI_EOP_SHIFT)
Rade Bozic85660f42010-01-28 12:47:07 -080049
50/* Controller command and status bits */
Jan Glauberdfcd8212016-03-18 09:46:26 +010051#define TWSI_CTL_CE 0x80
52#define TWSI_CTL_ENAB 0x40 /* Bus enable */
53#define TWSI_CTL_STA 0x20 /* Master-mode start, HW clears when done */
54#define TWSI_CTL_STP 0x10 /* Master-mode stop, HW clears when done */
55#define TWSI_CTL_IFLG 0x08 /* HW event, SW writes 0 to ACK */
56#define TWSI_CTL_AAK 0x04 /* Assert ACK */
Rade Bozic85660f42010-01-28 12:47:07 -080057
Jan Glauberb4c715d2016-04-25 16:33:30 +020058/* Status values */
59#define STAT_ERROR 0x00
Jan Glauberdfcd8212016-03-18 09:46:26 +010060#define STAT_START 0x08
Jan Glauberb4c715d2016-04-25 16:33:30 +020061#define STAT_REP_START 0x10
Jan Glauberdfcd8212016-03-18 09:46:26 +010062#define STAT_TXADDR_ACK 0x18
Jan Glauberb4c715d2016-04-25 16:33:30 +020063#define STAT_TXADDR_NAK 0x20
Jan Glauberdfcd8212016-03-18 09:46:26 +010064#define STAT_TXDATA_ACK 0x28
Jan Glauberb4c715d2016-04-25 16:33:30 +020065#define STAT_TXDATA_NAK 0x30
66#define STAT_LOST_ARB_38 0x38
Jan Glauberdfcd8212016-03-18 09:46:26 +010067#define STAT_RXADDR_ACK 0x40
Jan Glauberb4c715d2016-04-25 16:33:30 +020068#define STAT_RXADDR_NAK 0x48
Jan Glauberdfcd8212016-03-18 09:46:26 +010069#define STAT_RXDATA_ACK 0x50
Jan Glauberb4c715d2016-04-25 16:33:30 +020070#define STAT_RXDATA_NAK 0x58
71#define STAT_SLAVE_60 0x60
72#define STAT_LOST_ARB_68 0x68
73#define STAT_SLAVE_70 0x70
74#define STAT_LOST_ARB_78 0x78
75#define STAT_SLAVE_80 0x80
76#define STAT_SLAVE_88 0x88
77#define STAT_GENDATA_ACK 0x90
78#define STAT_GENDATA_NAK 0x98
79#define STAT_SLAVE_A0 0xA0
80#define STAT_SLAVE_A8 0xA8
81#define STAT_LOST_ARB_B0 0xB0
82#define STAT_SLAVE_LOST 0xB8
83#define STAT_SLAVE_NAK 0xC0
84#define STAT_SLAVE_ACK 0xC8
85#define STAT_AD2W_ACK 0xD0
86#define STAT_AD2W_NAK 0xD8
Jan Glauberdfcd8212016-03-18 09:46:26 +010087#define STAT_IDLE 0xF8
88
89/* TWSI_INT values */
90#define TWSI_INT_CORE_EN BIT_ULL(6)
91#define TWSI_INT_SDA_OVR BIT_ULL(8)
92#define TWSI_INT_SCL_OVR BIT_ULL(9)
Jan Glauberc981e342016-04-25 16:33:31 +020093#define TWSI_INT_SDA BIT_ULL(10)
94#define TWSI_INT_SCL BIT_ULL(11)
Rade Bozic85660f42010-01-28 12:47:07 -080095
96struct octeon_i2c {
97 wait_queue_head_t queue;
98 struct i2c_adapter adap;
99 int irq;
David Daneyf353a212012-07-05 18:12:39 +0200100 u32 twsi_freq;
Rade Bozic85660f42010-01-28 12:47:07 -0800101 int sys_freq;
Rade Bozic85660f42010-01-28 12:47:07 -0800102 void __iomem *twsi_base;
Rade Bozic85660f42010-01-28 12:47:07 -0800103 struct device *dev;
104};
105
106/**
Jan Glauber9cb94802016-04-11 17:28:34 +0200107 * octeon_i2c_reg_write - write an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100108 * @i2c: The struct octeon_i2c
109 * @eop_reg: Register selector
110 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -0800111 *
112 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
113 */
Jan Glauber9cb94802016-04-11 17:28:34 +0200114static void octeon_i2c_reg_write(struct octeon_i2c *i2c, u64 eop_reg, u8 data)
Rade Bozic85660f42010-01-28 12:47:07 -0800115{
116 u64 tmp;
117
118 __raw_writeq(SW_TWSI_V | eop_reg | data, i2c->twsi_base + SW_TWSI);
119 do {
120 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
121 } while ((tmp & SW_TWSI_V) != 0);
122}
123
Jan Glauberc57db702016-04-11 17:28:35 +0200124#define octeon_i2c_ctl_write(i2c, val) \
125 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CTL, val)
126#define octeon_i2c_data_write(i2c, val) \
127 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_DATA, val)
128
Rade Bozic85660f42010-01-28 12:47:07 -0800129/**
Jan Glauber9cb94802016-04-11 17:28:34 +0200130 * octeon_i2c_reg_read - read lower bits of an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100131 * @i2c: The struct octeon_i2c
132 * @eop_reg: Register selector
Rade Bozic85660f42010-01-28 12:47:07 -0800133 *
134 * Returns the data.
135 *
136 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
137 */
Jan Glauber9cb94802016-04-11 17:28:34 +0200138static u8 octeon_i2c_reg_read(struct octeon_i2c *i2c, u64 eop_reg)
Rade Bozic85660f42010-01-28 12:47:07 -0800139{
140 u64 tmp;
141
142 __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI);
143 do {
144 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
145 } while ((tmp & SW_TWSI_V) != 0);
146
147 return tmp & 0xFF;
148}
149
Jan Glauberc57db702016-04-11 17:28:35 +0200150#define octeon_i2c_ctl_read(i2c) \
151 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_CTL)
152#define octeon_i2c_data_read(i2c) \
153 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_DATA)
154#define octeon_i2c_stat_read(i2c) \
155 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_STAT)
156
Rade Bozic85660f42010-01-28 12:47:07 -0800157/**
Jan Glauberc981e342016-04-25 16:33:31 +0200158 * octeon_i2c_read_int - read the TWSI_INT register
159 * @i2c: The struct octeon_i2c
160 *
161 * Returns the value of the register.
162 */
163static u64 octeon_i2c_read_int(struct octeon_i2c *i2c)
164{
165 return __raw_readq(i2c->twsi_base + TWSI_INT);
166}
167
168/**
Rade Bozic85660f42010-01-28 12:47:07 -0800169 * octeon_i2c_write_int - write the TWSI_INT register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100170 * @i2c: The struct octeon_i2c
171 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -0800172 */
173static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
174{
Rade Bozic85660f42010-01-28 12:47:07 -0800175 __raw_writeq(data, i2c->twsi_base + TWSI_INT);
David Daneyf353a212012-07-05 18:12:39 +0200176 __raw_readq(i2c->twsi_base + TWSI_INT);
Rade Bozic85660f42010-01-28 12:47:07 -0800177}
178
179/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100180 * octeon_i2c_int_enable - enable the CORE interrupt
181 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800182 *
183 * The interrupt will be asserted when there is non-STAT_IDLE state in
184 * the SW_TWSI_EOP_TWSI_STAT register.
185 */
186static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
187{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100188 octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
Rade Bozic85660f42010-01-28 12:47:07 -0800189}
190
Jan Glauberbd7784c2016-03-07 16:10:44 +0100191/* disable the CORE interrupt */
Rade Bozic85660f42010-01-28 12:47:07 -0800192static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
193{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100194 /* clear TS/ST/IFLG events */
Rade Bozic85660f42010-01-28 12:47:07 -0800195 octeon_i2c_write_int(i2c, 0);
196}
197
Jan Glauberbd7784c2016-03-07 16:10:44 +0100198/* interrupt service routine */
Rade Bozic85660f42010-01-28 12:47:07 -0800199static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
200{
201 struct octeon_i2c *i2c = dev_id;
202
203 octeon_i2c_int_disable(i2c);
송은봉2637e5f2013-04-17 21:40:17 +0000204 wake_up(&i2c->queue);
Rade Bozic85660f42010-01-28 12:47:07 -0800205
206 return IRQ_HANDLED;
207}
208
Rade Bozic85660f42010-01-28 12:47:07 -0800209static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
210{
Jan Glauberb69e5c62016-04-11 17:28:36 +0200211 return (octeon_i2c_ctl_read(i2c) & TWSI_CTL_IFLG);
Rade Bozic85660f42010-01-28 12:47:07 -0800212}
213
214/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100215 * octeon_i2c_wait - wait for the IFLG to be set
216 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800217 *
218 * Returns 0 on success, otherwise a negative errno.
219 */
220static int octeon_i2c_wait(struct octeon_i2c *i2c)
221{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100222 long time_left;
Rade Bozic85660f42010-01-28 12:47:07 -0800223
224 octeon_i2c_int_enable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100225 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
226 i2c->adap.timeout);
Rade Bozic85660f42010-01-28 12:47:07 -0800227 octeon_i2c_int_disable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100228 if (!time_left) {
Rade Bozic85660f42010-01-28 12:47:07 -0800229 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
Bernhard Wallecc33e542010-09-27 12:55:16 +0200230 return -ETIMEDOUT;
Rade Bozic85660f42010-01-28 12:47:07 -0800231 }
232
233 return 0;
234}
235
Jan Glauberb4c715d2016-04-25 16:33:30 +0200236static int octeon_i2c_check_status(struct octeon_i2c *i2c, int final_read)
237{
238 u8 stat = octeon_i2c_stat_read(i2c);
239
240 switch (stat) {
241 /* Everything is fine */
242 case STAT_IDLE:
243 case STAT_AD2W_ACK:
244 case STAT_RXADDR_ACK:
245 case STAT_TXADDR_ACK:
246 case STAT_TXDATA_ACK:
247 return 0;
248
249 /* ACK allowed on pre-terminal bytes only */
250 case STAT_RXDATA_ACK:
251 if (!final_read)
252 return 0;
253 return -EIO;
254
255 /* NAK allowed on terminal byte only */
256 case STAT_RXDATA_NAK:
257 if (final_read)
258 return 0;
259 return -EIO;
260
261 /* Arbitration lost */
262 case STAT_LOST_ARB_38:
263 case STAT_LOST_ARB_68:
264 case STAT_LOST_ARB_78:
265 case STAT_LOST_ARB_B0:
266 return -EAGAIN;
267
268 /* Being addressed as slave, should back off & listen */
269 case STAT_SLAVE_60:
270 case STAT_SLAVE_70:
271 case STAT_GENDATA_ACK:
272 case STAT_GENDATA_NAK:
273 return -EOPNOTSUPP;
274
275 /* Core busy as slave */
276 case STAT_SLAVE_80:
277 case STAT_SLAVE_88:
278 case STAT_SLAVE_A0:
279 case STAT_SLAVE_A8:
280 case STAT_SLAVE_LOST:
281 case STAT_SLAVE_NAK:
282 case STAT_SLAVE_ACK:
283 return -EOPNOTSUPP;
284
285 case STAT_TXDATA_NAK:
286 return -EIO;
287 case STAT_TXADDR_NAK:
288 case STAT_RXADDR_NAK:
289 case STAT_AD2W_NAK:
290 return -ENXIO;
291 default:
292 dev_err(i2c->dev, "unhandled state: %d\n", stat);
293 return -EIO;
294 }
295}
296
Jan Glauberf541bb32016-04-11 17:28:33 +0200297/* calculate and set clock divisors */
298static void octeon_i2c_set_clock(struct octeon_i2c *i2c)
299{
300 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
301 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
302
303 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
304 /*
305 * An mdiv value of less than 2 seems to not work well
306 * with ds1337 RTCs, so we constrain it to larger values.
307 */
308 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
309 /*
310 * For given ndiv and mdiv values check the
311 * two closest thp values.
312 */
313 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
314 tclk *= (1 << ndiv_idx);
315 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
316
317 for (inc = 0; inc <= 1; inc++) {
318 thp_idx = thp_base + inc;
319 if (thp_idx < 5 || thp_idx > 0xff)
320 continue;
321
322 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
323 foscl = foscl / (1 << ndiv_idx);
324 foscl = foscl / (mdiv_idx + 1) / 10;
325 diff = abs(foscl - i2c->twsi_freq);
326 if (diff < delta_hz) {
327 delta_hz = diff;
328 thp = thp_idx;
329 mdiv = mdiv_idx;
330 ndiv = ndiv_idx;
331 }
332 }
333 }
334 }
Jan Glauber9cb94802016-04-11 17:28:34 +0200335 octeon_i2c_reg_write(i2c, SW_TWSI_OP_TWSI_CLK, thp);
336 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
Jan Glauberf541bb32016-04-11 17:28:33 +0200337}
338
339static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
340{
341 u8 status;
342 int tries;
343
344 /* disable high level controller, enable bus access */
Jan Glauberc57db702016-04-11 17:28:35 +0200345 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Jan Glauberf541bb32016-04-11 17:28:33 +0200346
347 /* reset controller */
Jan Glauber9cb94802016-04-11 17:28:34 +0200348 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0);
Jan Glauberf541bb32016-04-11 17:28:33 +0200349
350 for (tries = 10; tries; tries--) {
351 udelay(1);
Jan Glauberc57db702016-04-11 17:28:35 +0200352 status = octeon_i2c_stat_read(i2c);
Jan Glauberf541bb32016-04-11 17:28:33 +0200353 if (status == STAT_IDLE)
354 return 0;
355 }
356 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", __func__, status);
357 return -EIO;
358}
359
Jan Glauberc981e342016-04-25 16:33:31 +0200360static int octeon_i2c_recovery(struct octeon_i2c *i2c)
361{
362 int ret;
363
364 ret = i2c_recover_bus(&i2c->adap);
365 if (ret)
366 /* recover failed, try hardware re-init */
367 ret = octeon_i2c_init_lowlevel(i2c);
368 return ret;
369}
370
Rade Bozic85660f42010-01-28 12:47:07 -0800371/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100372 * octeon_i2c_start - send START to the bus
373 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800374 *
375 * Returns 0 on success, otherwise a negative errno.
376 */
377static int octeon_i2c_start(struct octeon_i2c *i2c)
378{
Jan Glauberc981e342016-04-25 16:33:31 +0200379 int ret;
380 u8 stat;
Rade Bozic85660f42010-01-28 12:47:07 -0800381
Jan Glauberc57db702016-04-11 17:28:35 +0200382 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
Jan Glauberc981e342016-04-25 16:33:31 +0200383 ret = octeon_i2c_wait(i2c);
384 if (ret)
385 goto error;
Rade Bozic85660f42010-01-28 12:47:07 -0800386
Jan Glauberc981e342016-04-25 16:33:31 +0200387 stat = octeon_i2c_stat_read(i2c);
388 if (stat == STAT_START || stat == STAT_REP_START)
389 /* START successful, bail out */
390 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800391
Jan Glauberc981e342016-04-25 16:33:31 +0200392error:
393 /* START failed, try to recover */
394 ret = octeon_i2c_recovery(i2c);
395 return (ret) ? ret : -EAGAIN;
Rade Bozic85660f42010-01-28 12:47:07 -0800396}
397
Jan Glauberdfcd8212016-03-18 09:46:26 +0100398/* send STOP to the bus */
399static void octeon_i2c_stop(struct octeon_i2c *i2c)
Rade Bozic85660f42010-01-28 12:47:07 -0800400{
Jan Glauberc57db702016-04-11 17:28:35 +0200401 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STP);
Rade Bozic85660f42010-01-28 12:47:07 -0800402}
403
404/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100405 * octeon_i2c_write - send data to the bus via low-level controller
406 * @i2c: The struct octeon_i2c
407 * @target: Target address
408 * @data: Pointer to the data to be sent
409 * @length: Length of the data
Rade Bozic85660f42010-01-28 12:47:07 -0800410 *
411 * The address is sent over the bus, then the data.
412 *
413 * Returns 0 on success, otherwise a negative errno.
414 */
415static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
416 const u8 *data, int length)
417{
418 int i, result;
Rade Bozic85660f42010-01-28 12:47:07 -0800419
Jan Glauberc57db702016-04-11 17:28:35 +0200420 octeon_i2c_data_write(i2c, target << 1);
421 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800422
423 result = octeon_i2c_wait(i2c);
424 if (result)
425 return result;
426
427 for (i = 0; i < length; i++) {
Jan Glauberb4c715d2016-04-25 16:33:30 +0200428 result = octeon_i2c_check_status(i2c, false);
429 if (result)
430 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800431
Jan Glauberc57db702016-04-11 17:28:35 +0200432 octeon_i2c_data_write(i2c, data[i]);
433 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800434
435 result = octeon_i2c_wait(i2c);
436 if (result)
437 return result;
438 }
439
440 return 0;
441}
442
443/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100444 * octeon_i2c_read - receive data from the bus via low-level controller
445 * @i2c: The struct octeon_i2c
446 * @target: Target address
447 * @data: Pointer to the location to store the data
David Daney886f6f82016-03-18 09:46:29 +0100448 * @rlength: Length of the data
449 * @recv_len: flag for length byte
Rade Bozic85660f42010-01-28 12:47:07 -0800450 *
451 * The address is sent over the bus, then the data is read.
452 *
453 * Returns 0 on success, otherwise a negative errno.
454 */
455static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
David Daney886f6f82016-03-18 09:46:29 +0100456 u8 *data, u16 *rlength, bool recv_len)
Rade Bozic85660f42010-01-28 12:47:07 -0800457{
David Daney886f6f82016-03-18 09:46:29 +0100458 int i, result, length = *rlength;
Jan Glauberb4c715d2016-04-25 16:33:30 +0200459 bool final_read = false;
Rade Bozic85660f42010-01-28 12:47:07 -0800460
461 if (length < 1)
462 return -EINVAL;
463
Jan Glauberc57db702016-04-11 17:28:35 +0200464 octeon_i2c_data_write(i2c, (target << 1) | 1);
465 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800466
467 result = octeon_i2c_wait(i2c);
468 if (result)
469 return result;
470
Jan Glauberb4c715d2016-04-25 16:33:30 +0200471 /* address OK ? */
472 result = octeon_i2c_check_status(i2c, false);
473 if (result)
474 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800475
Jan Glauberb4c715d2016-04-25 16:33:30 +0200476 for (i = 0; i < length; i++) {
477 /* for the last byte TWSI_CTL_AAK must not be set */
478 if (i + 1 == length)
479 final_read = true;
480
481 /* clear iflg to allow next event */
482 if (final_read)
Jan Glauberc57db702016-04-11 17:28:35 +0200483 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Jan Glauberb4c715d2016-04-25 16:33:30 +0200484 else
485 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_AAK);
Rade Bozic85660f42010-01-28 12:47:07 -0800486
487 result = octeon_i2c_wait(i2c);
488 if (result)
489 return result;
490
Jan Glauberc57db702016-04-11 17:28:35 +0200491 data[i] = octeon_i2c_data_read(i2c);
David Daney886f6f82016-03-18 09:46:29 +0100492 if (recv_len && i == 0) {
493 if (data[i] > I2C_SMBUS_BLOCK_MAX + 1) {
494 dev_err(i2c->dev,
495 "%s: read len > I2C_SMBUS_BLOCK_MAX %d\n",
496 __func__, data[i]);
497 return -EPROTO;
498 }
499 length += data[i];
500 }
Jan Glauberb4c715d2016-04-25 16:33:30 +0200501
502 result = octeon_i2c_check_status(i2c, final_read);
503 if (result)
504 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800505 }
David Daney886f6f82016-03-18 09:46:29 +0100506 *rlength = length;
Rade Bozic85660f42010-01-28 12:47:07 -0800507 return 0;
508}
509
510/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100511 * octeon_i2c_xfer - The driver's master_xfer function
512 * @adap: Pointer to the i2c_adapter structure
513 * @msgs: Pointer to the messages to be processed
514 * @num: Length of the MSGS array
Rade Bozic85660f42010-01-28 12:47:07 -0800515 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100516 * Returns the number of messages processed, or a negative errno on failure.
Rade Bozic85660f42010-01-28 12:47:07 -0800517 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100518static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
Rade Bozic85660f42010-01-28 12:47:07 -0800519 int num)
520{
Rade Bozic85660f42010-01-28 12:47:07 -0800521 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100522 int i, ret = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800523
524 for (i = 0; ret == 0 && i < num; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100525 struct i2c_msg *pmsg = &msgs[i];
526
Jan Glauberc981e342016-04-25 16:33:31 +0200527 ret = octeon_i2c_start(i2c);
528 if (ret)
529 return ret;
530
Rade Bozic85660f42010-01-28 12:47:07 -0800531 if (pmsg->flags & I2C_M_RD)
532 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
David Daney886f6f82016-03-18 09:46:29 +0100533 &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
Rade Bozic85660f42010-01-28 12:47:07 -0800534 else
535 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100536 pmsg->len);
Rade Bozic85660f42010-01-28 12:47:07 -0800537 }
538 octeon_i2c_stop(i2c);
539
540 return (ret != 0) ? ret : num;
541}
542
Jan Glauberc981e342016-04-25 16:33:31 +0200543static int octeon_i2c_get_scl(struct i2c_adapter *adap)
544{
545 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
546 u64 state;
547
548 state = octeon_i2c_read_int(i2c);
549 return state & TWSI_INT_SCL;
550}
551
552static void octeon_i2c_set_scl(struct i2c_adapter *adap, int val)
553{
554 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
555
556 octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
557}
558
559static int octeon_i2c_get_sda(struct i2c_adapter *adap)
560{
561 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
562 u64 state;
563
564 state = octeon_i2c_read_int(i2c);
565 return state & TWSI_INT_SDA;
566}
567
568static void octeon_i2c_prepare_recovery(struct i2c_adapter *adap)
569{
570 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
571
572 /*
573 * The stop resets the state machine, does not _transmit_ STOP unless
574 * engine was active.
575 */
576 octeon_i2c_stop(i2c);
577
578 octeon_i2c_write_int(i2c, 0);
579}
580
581static void octeon_i2c_unprepare_recovery(struct i2c_adapter *adap)
582{
583 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
584
585 octeon_i2c_write_int(i2c, 0);
586}
587
588static struct i2c_bus_recovery_info octeon_i2c_recovery_info = {
589 .recover_bus = i2c_generic_scl_recovery,
590 .get_scl = octeon_i2c_get_scl,
591 .set_scl = octeon_i2c_set_scl,
592 .get_sda = octeon_i2c_get_sda,
593 .prepare_recovery = octeon_i2c_prepare_recovery,
594 .unprepare_recovery = octeon_i2c_unprepare_recovery,
595};
596
Rade Bozic85660f42010-01-28 12:47:07 -0800597static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
598{
David Daney886f6f82016-03-18 09:46:29 +0100599 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
600 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
Rade Bozic85660f42010-01-28 12:47:07 -0800601}
602
603static const struct i2c_algorithm octeon_i2c_algo = {
604 .master_xfer = octeon_i2c_xfer,
605 .functionality = octeon_i2c_functionality,
606};
607
608static struct i2c_adapter octeon_i2c_ops = {
609 .owner = THIS_MODULE,
610 .name = "OCTEON adapter",
611 .algo = &octeon_i2c_algo,
Rade Bozic85660f42010-01-28 12:47:07 -0800612};
613
Bill Pemberton0b255e92012-11-27 15:59:38 -0500614static int octeon_i2c_probe(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800615{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100616 struct device_node *node = pdev->dev.of_node;
Rade Bozic85660f42010-01-28 12:47:07 -0800617 struct resource *res_mem;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100618 struct octeon_i2c *i2c;
619 int irq, result = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800620
621 /* All adaptors have an irq. */
622 irq = platform_get_irq(pdev, 0);
623 if (irq < 0)
624 return irq;
625
David Daneyf353a212012-07-05 18:12:39 +0200626 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Rade Bozic85660f42010-01-28 12:47:07 -0800627 if (!i2c) {
Rade Bozic85660f42010-01-28 12:47:07 -0800628 result = -ENOMEM;
629 goto out;
630 }
631 i2c->dev = &pdev->dev;
Rade Bozic85660f42010-01-28 12:47:07 -0800632
633 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jan Glauber54108e52016-03-18 09:46:27 +0100634 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
635 if (IS_ERR(i2c->twsi_base)) {
636 result = PTR_ERR(i2c->twsi_base);
David Daneyf353a212012-07-05 18:12:39 +0200637 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800638 }
Rade Bozic85660f42010-01-28 12:47:07 -0800639
David Daneyf353a212012-07-05 18:12:39 +0200640 /*
641 * "clock-rate" is a legacy binding, the official binding is
642 * "clock-frequency". Try the official one first and then
643 * fall back if it doesn't exist.
644 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100645 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
646 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
David Daneyf353a212012-07-05 18:12:39 +0200647 dev_err(i2c->dev,
648 "no I2C 'clock-rate' or 'clock-frequency' property\n");
649 result = -ENXIO;
650 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800651 }
David Daneyf353a212012-07-05 18:12:39 +0200652
653 i2c->sys_freq = octeon_get_io_clock_rate();
654
Rade Bozic85660f42010-01-28 12:47:07 -0800655 init_waitqueue_head(&i2c->queue);
656
657 i2c->irq = irq;
658
David Daneyf353a212012-07-05 18:12:39 +0200659 result = devm_request_irq(&pdev->dev, i2c->irq,
660 octeon_i2c_isr, 0, DRV_NAME, i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800661 if (result < 0) {
662 dev_err(i2c->dev, "failed to attach interrupt\n");
David Daneyf353a212012-07-05 18:12:39 +0200663 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800664 }
665
Jan Glauberdfcd8212016-03-18 09:46:26 +0100666 result = octeon_i2c_init_lowlevel(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800667 if (result) {
668 dev_err(i2c->dev, "init low level failed\n");
David Daneyf353a212012-07-05 18:12:39 +0200669 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800670 }
671
Jan Glauberdfcd8212016-03-18 09:46:26 +0100672 octeon_i2c_set_clock(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800673
674 i2c->adap = octeon_i2c_ops;
Jan Glaubera035d712016-04-11 17:28:32 +0200675 i2c->adap.timeout = msecs_to_jiffies(2);
676 i2c->adap.retries = 5;
Jan Glauberc981e342016-04-25 16:33:31 +0200677 i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info;
Rade Bozic85660f42010-01-28 12:47:07 -0800678 i2c->adap.dev.parent = &pdev->dev;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100679 i2c->adap.dev.of_node = node;
Rade Bozic85660f42010-01-28 12:47:07 -0800680 i2c_set_adapdata(&i2c->adap, i2c);
681 platform_set_drvdata(pdev, i2c);
682
David Daneyf353a212012-07-05 18:12:39 +0200683 result = i2c_add_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800684 if (result < 0) {
685 dev_err(i2c->dev, "failed to add adapter\n");
Doug Anderson55827f42013-02-15 13:18:35 +0000686 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800687 }
Jan Glauberdfcd8212016-03-18 09:46:26 +0100688 dev_info(i2c->dev, "probed\n");
David Daneyf353a212012-07-05 18:12:39 +0200689 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800690
Rade Bozic85660f42010-01-28 12:47:07 -0800691out:
692 return result;
693};
694
Bill Pemberton0b255e92012-11-27 15:59:38 -0500695static int octeon_i2c_remove(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800696{
697 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
698
699 i2c_del_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800700 return 0;
701};
702
Jan Glauberdfcd8212016-03-18 09:46:26 +0100703static const struct of_device_id octeon_i2c_match[] = {
704 { .compatible = "cavium,octeon-3860-twsi", },
David Daneyf353a212012-07-05 18:12:39 +0200705 {},
706};
707MODULE_DEVICE_TABLE(of, octeon_i2c_match);
708
Rade Bozic85660f42010-01-28 12:47:07 -0800709static struct platform_driver octeon_i2c_driver = {
710 .probe = octeon_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500711 .remove = octeon_i2c_remove,
Rade Bozic85660f42010-01-28 12:47:07 -0800712 .driver = {
Rade Bozic85660f42010-01-28 12:47:07 -0800713 .name = DRV_NAME,
David Daneyf353a212012-07-05 18:12:39 +0200714 .of_match_table = octeon_i2c_match,
Rade Bozic85660f42010-01-28 12:47:07 -0800715 },
716};
717
Axel Lina3664b52012-01-12 20:32:04 +0100718module_platform_driver(octeon_i2c_driver);
Rade Bozic85660f42010-01-28 12:47:07 -0800719
720MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
721MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
722MODULE_LICENSE("GPL");