blob: 471d06eb84345da49f9bbbae272949c5b3c58f64 [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)
Rade Bozic85660f42010-01-28 12:47:07 -080093
94struct octeon_i2c {
95 wait_queue_head_t queue;
96 struct i2c_adapter adap;
97 int irq;
David Daneyf353a212012-07-05 18:12:39 +020098 u32 twsi_freq;
Rade Bozic85660f42010-01-28 12:47:07 -080099 int sys_freq;
Rade Bozic85660f42010-01-28 12:47:07 -0800100 void __iomem *twsi_base;
Rade Bozic85660f42010-01-28 12:47:07 -0800101 struct device *dev;
102};
103
104/**
Jan Glauber9cb94802016-04-11 17:28:34 +0200105 * octeon_i2c_reg_write - write an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100106 * @i2c: The struct octeon_i2c
107 * @eop_reg: Register selector
108 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -0800109 *
110 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
111 */
Jan Glauber9cb94802016-04-11 17:28:34 +0200112static void octeon_i2c_reg_write(struct octeon_i2c *i2c, u64 eop_reg, u8 data)
Rade Bozic85660f42010-01-28 12:47:07 -0800113{
114 u64 tmp;
115
116 __raw_writeq(SW_TWSI_V | eop_reg | data, i2c->twsi_base + SW_TWSI);
117 do {
118 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
119 } while ((tmp & SW_TWSI_V) != 0);
120}
121
Jan Glauberc57db702016-04-11 17:28:35 +0200122#define octeon_i2c_ctl_write(i2c, val) \
123 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CTL, val)
124#define octeon_i2c_data_write(i2c, val) \
125 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_DATA, val)
126
Rade Bozic85660f42010-01-28 12:47:07 -0800127/**
Jan Glauber9cb94802016-04-11 17:28:34 +0200128 * octeon_i2c_reg_read - read lower bits of an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100129 * @i2c: The struct octeon_i2c
130 * @eop_reg: Register selector
Rade Bozic85660f42010-01-28 12:47:07 -0800131 *
132 * Returns the data.
133 *
134 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
135 */
Jan Glauber9cb94802016-04-11 17:28:34 +0200136static u8 octeon_i2c_reg_read(struct octeon_i2c *i2c, u64 eop_reg)
Rade Bozic85660f42010-01-28 12:47:07 -0800137{
138 u64 tmp;
139
140 __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI);
141 do {
142 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
143 } while ((tmp & SW_TWSI_V) != 0);
144
145 return tmp & 0xFF;
146}
147
Jan Glauberc57db702016-04-11 17:28:35 +0200148#define octeon_i2c_ctl_read(i2c) \
149 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_CTL)
150#define octeon_i2c_data_read(i2c) \
151 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_DATA)
152#define octeon_i2c_stat_read(i2c) \
153 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_STAT)
154
Rade Bozic85660f42010-01-28 12:47:07 -0800155/**
156 * octeon_i2c_write_int - write the TWSI_INT register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100157 * @i2c: The struct octeon_i2c
158 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -0800159 */
160static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
161{
Rade Bozic85660f42010-01-28 12:47:07 -0800162 __raw_writeq(data, i2c->twsi_base + TWSI_INT);
David Daneyf353a212012-07-05 18:12:39 +0200163 __raw_readq(i2c->twsi_base + TWSI_INT);
Rade Bozic85660f42010-01-28 12:47:07 -0800164}
165
166/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100167 * octeon_i2c_int_enable - enable the CORE interrupt
168 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800169 *
170 * The interrupt will be asserted when there is non-STAT_IDLE state in
171 * the SW_TWSI_EOP_TWSI_STAT register.
172 */
173static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
174{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100175 octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
Rade Bozic85660f42010-01-28 12:47:07 -0800176}
177
Jan Glauberbd7784c2016-03-07 16:10:44 +0100178/* disable the CORE interrupt */
Rade Bozic85660f42010-01-28 12:47:07 -0800179static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
180{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100181 /* clear TS/ST/IFLG events */
Rade Bozic85660f42010-01-28 12:47:07 -0800182 octeon_i2c_write_int(i2c, 0);
183}
184
185/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100186 * octeon_i2c_unblock - unblock the bus
187 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800188 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100189 * If there was a reset while a device was driving 0 to bus, bus is blocked.
190 * We toggle it free manually by some clock cycles and send a stop.
Rade Bozic85660f42010-01-28 12:47:07 -0800191 */
192static void octeon_i2c_unblock(struct octeon_i2c *i2c)
193{
194 int i;
195
196 dev_dbg(i2c->dev, "%s\n", __func__);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100197
Rade Bozic85660f42010-01-28 12:47:07 -0800198 for (i = 0; i < 9; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100199 octeon_i2c_write_int(i2c, 0);
Rade Bozic85660f42010-01-28 12:47:07 -0800200 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100201 octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800202 udelay(5);
203 }
Jan Glauberdfcd8212016-03-18 09:46:26 +0100204 /* hand-crank a STOP */
205 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR | TWSI_INT_SCL_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800206 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100207 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800208 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100209 octeon_i2c_write_int(i2c, 0);
Rade Bozic85660f42010-01-28 12:47:07 -0800210}
211
Jan Glauberbd7784c2016-03-07 16:10:44 +0100212/* interrupt service routine */
Rade Bozic85660f42010-01-28 12:47:07 -0800213static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
214{
215 struct octeon_i2c *i2c = dev_id;
216
217 octeon_i2c_int_disable(i2c);
송은봉2637e5f2013-04-17 21:40:17 +0000218 wake_up(&i2c->queue);
Rade Bozic85660f42010-01-28 12:47:07 -0800219
220 return IRQ_HANDLED;
221}
222
Rade Bozic85660f42010-01-28 12:47:07 -0800223static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
224{
Jan Glauberb69e5c62016-04-11 17:28:36 +0200225 return (octeon_i2c_ctl_read(i2c) & TWSI_CTL_IFLG);
Rade Bozic85660f42010-01-28 12:47:07 -0800226}
227
228/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100229 * octeon_i2c_wait - wait for the IFLG to be set
230 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800231 *
232 * Returns 0 on success, otherwise a negative errno.
233 */
234static int octeon_i2c_wait(struct octeon_i2c *i2c)
235{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100236 long time_left;
Rade Bozic85660f42010-01-28 12:47:07 -0800237
238 octeon_i2c_int_enable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100239 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
240 i2c->adap.timeout);
Rade Bozic85660f42010-01-28 12:47:07 -0800241 octeon_i2c_int_disable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100242 if (!time_left) {
Rade Bozic85660f42010-01-28 12:47:07 -0800243 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
Bernhard Wallecc33e542010-09-27 12:55:16 +0200244 return -ETIMEDOUT;
Rade Bozic85660f42010-01-28 12:47:07 -0800245 }
246
247 return 0;
248}
249
Jan Glauberb4c715d2016-04-25 16:33:30 +0200250static int octeon_i2c_check_status(struct octeon_i2c *i2c, int final_read)
251{
252 u8 stat = octeon_i2c_stat_read(i2c);
253
254 switch (stat) {
255 /* Everything is fine */
256 case STAT_IDLE:
257 case STAT_AD2W_ACK:
258 case STAT_RXADDR_ACK:
259 case STAT_TXADDR_ACK:
260 case STAT_TXDATA_ACK:
261 return 0;
262
263 /* ACK allowed on pre-terminal bytes only */
264 case STAT_RXDATA_ACK:
265 if (!final_read)
266 return 0;
267 return -EIO;
268
269 /* NAK allowed on terminal byte only */
270 case STAT_RXDATA_NAK:
271 if (final_read)
272 return 0;
273 return -EIO;
274
275 /* Arbitration lost */
276 case STAT_LOST_ARB_38:
277 case STAT_LOST_ARB_68:
278 case STAT_LOST_ARB_78:
279 case STAT_LOST_ARB_B0:
280 return -EAGAIN;
281
282 /* Being addressed as slave, should back off & listen */
283 case STAT_SLAVE_60:
284 case STAT_SLAVE_70:
285 case STAT_GENDATA_ACK:
286 case STAT_GENDATA_NAK:
287 return -EOPNOTSUPP;
288
289 /* Core busy as slave */
290 case STAT_SLAVE_80:
291 case STAT_SLAVE_88:
292 case STAT_SLAVE_A0:
293 case STAT_SLAVE_A8:
294 case STAT_SLAVE_LOST:
295 case STAT_SLAVE_NAK:
296 case STAT_SLAVE_ACK:
297 return -EOPNOTSUPP;
298
299 case STAT_TXDATA_NAK:
300 return -EIO;
301 case STAT_TXADDR_NAK:
302 case STAT_RXADDR_NAK:
303 case STAT_AD2W_NAK:
304 return -ENXIO;
305 default:
306 dev_err(i2c->dev, "unhandled state: %d\n", stat);
307 return -EIO;
308 }
309}
310
Jan Glauberf541bb32016-04-11 17:28:33 +0200311/* calculate and set clock divisors */
312static void octeon_i2c_set_clock(struct octeon_i2c *i2c)
313{
314 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
315 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
316
317 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
318 /*
319 * An mdiv value of less than 2 seems to not work well
320 * with ds1337 RTCs, so we constrain it to larger values.
321 */
322 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
323 /*
324 * For given ndiv and mdiv values check the
325 * two closest thp values.
326 */
327 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
328 tclk *= (1 << ndiv_idx);
329 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
330
331 for (inc = 0; inc <= 1; inc++) {
332 thp_idx = thp_base + inc;
333 if (thp_idx < 5 || thp_idx > 0xff)
334 continue;
335
336 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
337 foscl = foscl / (1 << ndiv_idx);
338 foscl = foscl / (mdiv_idx + 1) / 10;
339 diff = abs(foscl - i2c->twsi_freq);
340 if (diff < delta_hz) {
341 delta_hz = diff;
342 thp = thp_idx;
343 mdiv = mdiv_idx;
344 ndiv = ndiv_idx;
345 }
346 }
347 }
348 }
Jan Glauber9cb94802016-04-11 17:28:34 +0200349 octeon_i2c_reg_write(i2c, SW_TWSI_OP_TWSI_CLK, thp);
350 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
Jan Glauberf541bb32016-04-11 17:28:33 +0200351}
352
353static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
354{
355 u8 status;
356 int tries;
357
358 /* disable high level controller, enable bus access */
Jan Glauberc57db702016-04-11 17:28:35 +0200359 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Jan Glauberf541bb32016-04-11 17:28:33 +0200360
361 /* reset controller */
Jan Glauber9cb94802016-04-11 17:28:34 +0200362 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0);
Jan Glauberf541bb32016-04-11 17:28:33 +0200363
364 for (tries = 10; tries; tries--) {
365 udelay(1);
Jan Glauberc57db702016-04-11 17:28:35 +0200366 status = octeon_i2c_stat_read(i2c);
Jan Glauberf541bb32016-04-11 17:28:33 +0200367 if (status == STAT_IDLE)
368 return 0;
369 }
370 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", __func__, status);
371 return -EIO;
372}
373
Rade Bozic85660f42010-01-28 12:47:07 -0800374/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100375 * octeon_i2c_start - send START to the bus
376 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800377 *
378 * Returns 0 on success, otherwise a negative errno.
379 */
380static int octeon_i2c_start(struct octeon_i2c *i2c)
381{
Rade Bozic85660f42010-01-28 12:47:07 -0800382 int result;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100383 u8 data;
Rade Bozic85660f42010-01-28 12:47:07 -0800384
Jan Glauberc57db702016-04-11 17:28:35 +0200385 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
Rade Bozic85660f42010-01-28 12:47:07 -0800386
387 result = octeon_i2c_wait(i2c);
388 if (result) {
Jan Glauberc57db702016-04-11 17:28:35 +0200389 if (octeon_i2c_stat_read(i2c) == STAT_IDLE) {
Rade Bozic85660f42010-01-28 12:47:07 -0800390 /*
391 * Controller refused to send start flag May
392 * be a client is holding SDA low - let's try
393 * to free it.
394 */
395 octeon_i2c_unblock(i2c);
Jan Glauberc57db702016-04-11 17:28:35 +0200396 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
Rade Bozic85660f42010-01-28 12:47:07 -0800397 result = octeon_i2c_wait(i2c);
398 }
399 if (result)
400 return result;
401 }
402
Jan Glauberc57db702016-04-11 17:28:35 +0200403 data = octeon_i2c_stat_read(i2c);
Jan Glauberb4c715d2016-04-25 16:33:30 +0200404 if ((data != STAT_START) && (data != STAT_REP_START)) {
Rade Bozic85660f42010-01-28 12:47:07 -0800405 dev_err(i2c->dev, "%s: bad status (0x%x)\n", __func__, data);
406 return -EIO;
407 }
408
409 return 0;
410}
411
Jan Glauberdfcd8212016-03-18 09:46:26 +0100412/* send STOP to the bus */
413static void octeon_i2c_stop(struct octeon_i2c *i2c)
Rade Bozic85660f42010-01-28 12:47:07 -0800414{
Jan Glauberc57db702016-04-11 17:28:35 +0200415 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STP);
Rade Bozic85660f42010-01-28 12:47:07 -0800416}
417
418/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100419 * octeon_i2c_write - send data to the bus via low-level controller
420 * @i2c: The struct octeon_i2c
421 * @target: Target address
422 * @data: Pointer to the data to be sent
423 * @length: Length of the data
Rade Bozic85660f42010-01-28 12:47:07 -0800424 *
425 * The address is sent over the bus, then the data.
426 *
427 * Returns 0 on success, otherwise a negative errno.
428 */
429static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
430 const u8 *data, int length)
431{
432 int i, result;
Rade Bozic85660f42010-01-28 12:47:07 -0800433
434 result = octeon_i2c_start(i2c);
435 if (result)
436 return result;
437
Jan Glauberc57db702016-04-11 17:28:35 +0200438 octeon_i2c_data_write(i2c, target << 1);
439 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800440
441 result = octeon_i2c_wait(i2c);
442 if (result)
443 return result;
444
445 for (i = 0; i < length; i++) {
Jan Glauberb4c715d2016-04-25 16:33:30 +0200446 result = octeon_i2c_check_status(i2c, false);
447 if (result)
448 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800449
Jan Glauberc57db702016-04-11 17:28:35 +0200450 octeon_i2c_data_write(i2c, data[i]);
451 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800452
453 result = octeon_i2c_wait(i2c);
454 if (result)
455 return result;
456 }
457
458 return 0;
459}
460
461/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100462 * octeon_i2c_read - receive data from the bus via low-level controller
463 * @i2c: The struct octeon_i2c
464 * @target: Target address
465 * @data: Pointer to the location to store the data
David Daney886f6f82016-03-18 09:46:29 +0100466 * @rlength: Length of the data
467 * @recv_len: flag for length byte
Rade Bozic85660f42010-01-28 12:47:07 -0800468 *
469 * The address is sent over the bus, then the data is read.
470 *
471 * Returns 0 on success, otherwise a negative errno.
472 */
473static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
David Daney886f6f82016-03-18 09:46:29 +0100474 u8 *data, u16 *rlength, bool recv_len)
Rade Bozic85660f42010-01-28 12:47:07 -0800475{
David Daney886f6f82016-03-18 09:46:29 +0100476 int i, result, length = *rlength;
Jan Glauberb4c715d2016-04-25 16:33:30 +0200477 bool final_read = false;
Rade Bozic85660f42010-01-28 12:47:07 -0800478
479 if (length < 1)
480 return -EINVAL;
481
482 result = octeon_i2c_start(i2c);
483 if (result)
484 return result;
485
Jan Glauberc57db702016-04-11 17:28:35 +0200486 octeon_i2c_data_write(i2c, (target << 1) | 1);
487 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800488
489 result = octeon_i2c_wait(i2c);
490 if (result)
491 return result;
492
Jan Glauberb4c715d2016-04-25 16:33:30 +0200493 /* address OK ? */
494 result = octeon_i2c_check_status(i2c, false);
495 if (result)
496 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800497
Jan Glauberb4c715d2016-04-25 16:33:30 +0200498 for (i = 0; i < length; i++) {
499 /* for the last byte TWSI_CTL_AAK must not be set */
500 if (i + 1 == length)
501 final_read = true;
502
503 /* clear iflg to allow next event */
504 if (final_read)
Jan Glauberc57db702016-04-11 17:28:35 +0200505 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Jan Glauberb4c715d2016-04-25 16:33:30 +0200506 else
507 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_AAK);
Rade Bozic85660f42010-01-28 12:47:07 -0800508
509 result = octeon_i2c_wait(i2c);
510 if (result)
511 return result;
512
Jan Glauberc57db702016-04-11 17:28:35 +0200513 data[i] = octeon_i2c_data_read(i2c);
David Daney886f6f82016-03-18 09:46:29 +0100514 if (recv_len && i == 0) {
515 if (data[i] > I2C_SMBUS_BLOCK_MAX + 1) {
516 dev_err(i2c->dev,
517 "%s: read len > I2C_SMBUS_BLOCK_MAX %d\n",
518 __func__, data[i]);
519 return -EPROTO;
520 }
521 length += data[i];
522 }
Jan Glauberb4c715d2016-04-25 16:33:30 +0200523
524 result = octeon_i2c_check_status(i2c, final_read);
525 if (result)
526 return result;
Rade Bozic85660f42010-01-28 12:47:07 -0800527 }
David Daney886f6f82016-03-18 09:46:29 +0100528 *rlength = length;
Rade Bozic85660f42010-01-28 12:47:07 -0800529 return 0;
530}
531
532/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100533 * octeon_i2c_xfer - The driver's master_xfer function
534 * @adap: Pointer to the i2c_adapter structure
535 * @msgs: Pointer to the messages to be processed
536 * @num: Length of the MSGS array
Rade Bozic85660f42010-01-28 12:47:07 -0800537 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100538 * Returns the number of messages processed, or a negative errno on failure.
Rade Bozic85660f42010-01-28 12:47:07 -0800539 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100540static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
Rade Bozic85660f42010-01-28 12:47:07 -0800541 int num)
542{
Rade Bozic85660f42010-01-28 12:47:07 -0800543 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100544 int i, ret = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800545
546 for (i = 0; ret == 0 && i < num; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100547 struct i2c_msg *pmsg = &msgs[i];
548
Rade Bozic85660f42010-01-28 12:47:07 -0800549 dev_dbg(i2c->dev,
550 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
551 pmsg->flags & I2C_M_RD ? "read" : "write",
552 pmsg->len, pmsg->addr, i + 1, num);
553 if (pmsg->flags & I2C_M_RD)
554 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
David Daney886f6f82016-03-18 09:46:29 +0100555 &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
Rade Bozic85660f42010-01-28 12:47:07 -0800556 else
557 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100558 pmsg->len);
Rade Bozic85660f42010-01-28 12:47:07 -0800559 }
560 octeon_i2c_stop(i2c);
561
562 return (ret != 0) ? ret : num;
563}
564
565static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
566{
David Daney886f6f82016-03-18 09:46:29 +0100567 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
568 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
Rade Bozic85660f42010-01-28 12:47:07 -0800569}
570
571static const struct i2c_algorithm octeon_i2c_algo = {
572 .master_xfer = octeon_i2c_xfer,
573 .functionality = octeon_i2c_functionality,
574};
575
576static struct i2c_adapter octeon_i2c_ops = {
577 .owner = THIS_MODULE,
578 .name = "OCTEON adapter",
579 .algo = &octeon_i2c_algo,
Rade Bozic85660f42010-01-28 12:47:07 -0800580};
581
Bill Pemberton0b255e92012-11-27 15:59:38 -0500582static int octeon_i2c_probe(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800583{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100584 struct device_node *node = pdev->dev.of_node;
Rade Bozic85660f42010-01-28 12:47:07 -0800585 struct resource *res_mem;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100586 struct octeon_i2c *i2c;
587 int irq, result = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800588
589 /* All adaptors have an irq. */
590 irq = platform_get_irq(pdev, 0);
591 if (irq < 0)
592 return irq;
593
David Daneyf353a212012-07-05 18:12:39 +0200594 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Rade Bozic85660f42010-01-28 12:47:07 -0800595 if (!i2c) {
Rade Bozic85660f42010-01-28 12:47:07 -0800596 result = -ENOMEM;
597 goto out;
598 }
599 i2c->dev = &pdev->dev;
Rade Bozic85660f42010-01-28 12:47:07 -0800600
601 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jan Glauber54108e52016-03-18 09:46:27 +0100602 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
603 if (IS_ERR(i2c->twsi_base)) {
604 result = PTR_ERR(i2c->twsi_base);
David Daneyf353a212012-07-05 18:12:39 +0200605 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800606 }
Rade Bozic85660f42010-01-28 12:47:07 -0800607
David Daneyf353a212012-07-05 18:12:39 +0200608 /*
609 * "clock-rate" is a legacy binding, the official binding is
610 * "clock-frequency". Try the official one first and then
611 * fall back if it doesn't exist.
612 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100613 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
614 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
David Daneyf353a212012-07-05 18:12:39 +0200615 dev_err(i2c->dev,
616 "no I2C 'clock-rate' or 'clock-frequency' property\n");
617 result = -ENXIO;
618 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800619 }
David Daneyf353a212012-07-05 18:12:39 +0200620
621 i2c->sys_freq = octeon_get_io_clock_rate();
622
Rade Bozic85660f42010-01-28 12:47:07 -0800623 init_waitqueue_head(&i2c->queue);
624
625 i2c->irq = irq;
626
David Daneyf353a212012-07-05 18:12:39 +0200627 result = devm_request_irq(&pdev->dev, i2c->irq,
628 octeon_i2c_isr, 0, DRV_NAME, i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800629 if (result < 0) {
630 dev_err(i2c->dev, "failed to attach interrupt\n");
David Daneyf353a212012-07-05 18:12:39 +0200631 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800632 }
633
Jan Glauberdfcd8212016-03-18 09:46:26 +0100634 result = octeon_i2c_init_lowlevel(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800635 if (result) {
636 dev_err(i2c->dev, "init low level failed\n");
David Daneyf353a212012-07-05 18:12:39 +0200637 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800638 }
639
Jan Glauberdfcd8212016-03-18 09:46:26 +0100640 octeon_i2c_set_clock(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800641
642 i2c->adap = octeon_i2c_ops;
Jan Glaubera035d712016-04-11 17:28:32 +0200643 i2c->adap.timeout = msecs_to_jiffies(2);
644 i2c->adap.retries = 5;
Rade Bozic85660f42010-01-28 12:47:07 -0800645 i2c->adap.dev.parent = &pdev->dev;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100646 i2c->adap.dev.of_node = node;
Rade Bozic85660f42010-01-28 12:47:07 -0800647 i2c_set_adapdata(&i2c->adap, i2c);
648 platform_set_drvdata(pdev, i2c);
649
David Daneyf353a212012-07-05 18:12:39 +0200650 result = i2c_add_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800651 if (result < 0) {
652 dev_err(i2c->dev, "failed to add adapter\n");
Doug Anderson55827f42013-02-15 13:18:35 +0000653 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800654 }
Jan Glauberdfcd8212016-03-18 09:46:26 +0100655 dev_info(i2c->dev, "probed\n");
David Daneyf353a212012-07-05 18:12:39 +0200656 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800657
Rade Bozic85660f42010-01-28 12:47:07 -0800658out:
659 return result;
660};
661
Bill Pemberton0b255e92012-11-27 15:59:38 -0500662static int octeon_i2c_remove(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800663{
664 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
665
666 i2c_del_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800667 return 0;
668};
669
Jan Glauberdfcd8212016-03-18 09:46:26 +0100670static const struct of_device_id octeon_i2c_match[] = {
671 { .compatible = "cavium,octeon-3860-twsi", },
David Daneyf353a212012-07-05 18:12:39 +0200672 {},
673};
674MODULE_DEVICE_TABLE(of, octeon_i2c_match);
675
Rade Bozic85660f42010-01-28 12:47:07 -0800676static struct platform_driver octeon_i2c_driver = {
677 .probe = octeon_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500678 .remove = octeon_i2c_remove,
Rade Bozic85660f42010-01-28 12:47:07 -0800679 .driver = {
Rade Bozic85660f42010-01-28 12:47:07 -0800680 .name = DRV_NAME,
David Daneyf353a212012-07-05 18:12:39 +0200681 .of_match_table = octeon_i2c_match,
Rade Bozic85660f42010-01-28 12:47:07 -0800682 },
683};
684
Axel Lina3664b52012-01-12 20:32:04 +0100685module_platform_driver(octeon_i2c_driver);
Rade Bozic85660f42010-01-28 12:47:07 -0800686
687MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
688MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
689MODULE_LICENSE("GPL");