blob: 9787379dbfe750dd23051928a196d4d35b8dcfbb [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
58/* Some status values */
Jan Glauberdfcd8212016-03-18 09:46:26 +010059#define STAT_START 0x08
60#define STAT_RSTART 0x10
61#define STAT_TXADDR_ACK 0x18
62#define STAT_TXDATA_ACK 0x28
63#define STAT_RXADDR_ACK 0x40
64#define STAT_RXDATA_ACK 0x50
65#define STAT_IDLE 0xF8
66
67/* TWSI_INT values */
68#define TWSI_INT_CORE_EN BIT_ULL(6)
69#define TWSI_INT_SDA_OVR BIT_ULL(8)
70#define TWSI_INT_SCL_OVR BIT_ULL(9)
Rade Bozic85660f42010-01-28 12:47:07 -080071
72struct octeon_i2c {
73 wait_queue_head_t queue;
74 struct i2c_adapter adap;
75 int irq;
David Daneyf353a212012-07-05 18:12:39 +020076 u32 twsi_freq;
Rade Bozic85660f42010-01-28 12:47:07 -080077 int sys_freq;
78 resource_size_t twsi_phys;
79 void __iomem *twsi_base;
80 resource_size_t regsize;
81 struct device *dev;
82};
83
84/**
Jan Glauberbd7784c2016-03-07 16:10:44 +010085 * octeon_i2c_write_sw - write an I2C core register
86 * @i2c: The struct octeon_i2c
87 * @eop_reg: Register selector
88 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -080089 *
90 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
91 */
Jan Glauberdfcd8212016-03-18 09:46:26 +010092static void octeon_i2c_write_sw(struct octeon_i2c *i2c, u64 eop_reg, u8 data)
Rade Bozic85660f42010-01-28 12:47:07 -080093{
94 u64 tmp;
95
96 __raw_writeq(SW_TWSI_V | eop_reg | data, i2c->twsi_base + SW_TWSI);
97 do {
98 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
99 } while ((tmp & SW_TWSI_V) != 0);
100}
101
102/**
Jan Glauberdfcd8212016-03-18 09:46:26 +0100103 * octeon_i2c_read_sw - read lower bits of an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100104 * @i2c: The struct octeon_i2c
105 * @eop_reg: Register selector
Rade Bozic85660f42010-01-28 12:47:07 -0800106 *
107 * Returns the data.
108 *
109 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
110 */
111static u8 octeon_i2c_read_sw(struct octeon_i2c *i2c, u64 eop_reg)
112{
113 u64 tmp;
114
115 __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI);
116 do {
117 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
118 } while ((tmp & SW_TWSI_V) != 0);
119
120 return tmp & 0xFF;
121}
122
123/**
124 * octeon_i2c_write_int - write the TWSI_INT register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100125 * @i2c: The struct octeon_i2c
126 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -0800127 */
128static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
129{
Rade Bozic85660f42010-01-28 12:47:07 -0800130 __raw_writeq(data, i2c->twsi_base + TWSI_INT);
David Daneyf353a212012-07-05 18:12:39 +0200131 __raw_readq(i2c->twsi_base + TWSI_INT);
Rade Bozic85660f42010-01-28 12:47:07 -0800132}
133
134/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100135 * octeon_i2c_int_enable - enable the CORE interrupt
136 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800137 *
138 * The interrupt will be asserted when there is non-STAT_IDLE state in
139 * the SW_TWSI_EOP_TWSI_STAT register.
140 */
141static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
142{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100143 octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
Rade Bozic85660f42010-01-28 12:47:07 -0800144}
145
Jan Glauberbd7784c2016-03-07 16:10:44 +0100146/* disable the CORE interrupt */
Rade Bozic85660f42010-01-28 12:47:07 -0800147static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
148{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100149 /* clear TS/ST/IFLG events */
Rade Bozic85660f42010-01-28 12:47:07 -0800150 octeon_i2c_write_int(i2c, 0);
151}
152
153/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100154 * octeon_i2c_unblock - unblock the bus
155 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800156 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100157 * If there was a reset while a device was driving 0 to bus, bus is blocked.
158 * We toggle it free manually by some clock cycles and send a stop.
Rade Bozic85660f42010-01-28 12:47:07 -0800159 */
160static void octeon_i2c_unblock(struct octeon_i2c *i2c)
161{
162 int i;
163
164 dev_dbg(i2c->dev, "%s\n", __func__);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100165
Rade Bozic85660f42010-01-28 12:47:07 -0800166 for (i = 0; i < 9; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100167 octeon_i2c_write_int(i2c, 0);
Rade Bozic85660f42010-01-28 12:47:07 -0800168 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100169 octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800170 udelay(5);
171 }
Jan Glauberdfcd8212016-03-18 09:46:26 +0100172 /* hand-crank a STOP */
173 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR | TWSI_INT_SCL_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800174 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100175 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800176 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100177 octeon_i2c_write_int(i2c, 0);
Rade Bozic85660f42010-01-28 12:47:07 -0800178}
179
Jan Glauberbd7784c2016-03-07 16:10:44 +0100180/* interrupt service routine */
Rade Bozic85660f42010-01-28 12:47:07 -0800181static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
182{
183 struct octeon_i2c *i2c = dev_id;
184
185 octeon_i2c_int_disable(i2c);
송은봉2637e5f2013-04-17 21:40:17 +0000186 wake_up(&i2c->queue);
Rade Bozic85660f42010-01-28 12:47:07 -0800187
188 return IRQ_HANDLED;
189}
190
191
192static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
193{
194 return (octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_CTL) & TWSI_CTL_IFLG) != 0;
195}
196
197/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100198 * octeon_i2c_wait - wait for the IFLG to be set
199 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800200 *
201 * Returns 0 on success, otherwise a negative errno.
202 */
203static int octeon_i2c_wait(struct octeon_i2c *i2c)
204{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100205 long time_left;
Rade Bozic85660f42010-01-28 12:47:07 -0800206
207 octeon_i2c_int_enable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100208 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
209 i2c->adap.timeout);
Rade Bozic85660f42010-01-28 12:47:07 -0800210 octeon_i2c_int_disable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100211 if (!time_left) {
Rade Bozic85660f42010-01-28 12:47:07 -0800212 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
Bernhard Wallecc33e542010-09-27 12:55:16 +0200213 return -ETIMEDOUT;
Rade Bozic85660f42010-01-28 12:47:07 -0800214 }
215
216 return 0;
217}
218
219/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100220 * octeon_i2c_start - send START to the bus
221 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800222 *
223 * Returns 0 on success, otherwise a negative errno.
224 */
225static int octeon_i2c_start(struct octeon_i2c *i2c)
226{
Rade Bozic85660f42010-01-28 12:47:07 -0800227 int result;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100228 u8 data;
Rade Bozic85660f42010-01-28 12:47:07 -0800229
230 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100231 TWSI_CTL_ENAB | TWSI_CTL_STA);
Rade Bozic85660f42010-01-28 12:47:07 -0800232
233 result = octeon_i2c_wait(i2c);
234 if (result) {
235 if (octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT) == STAT_IDLE) {
236 /*
237 * Controller refused to send start flag May
238 * be a client is holding SDA low - let's try
239 * to free it.
240 */
241 octeon_i2c_unblock(i2c);
242 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
243 TWSI_CTL_ENAB | TWSI_CTL_STA);
Rade Bozic85660f42010-01-28 12:47:07 -0800244 result = octeon_i2c_wait(i2c);
245 }
246 if (result)
247 return result;
248 }
249
250 data = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
251 if ((data != STAT_START) && (data != STAT_RSTART)) {
252 dev_err(i2c->dev, "%s: bad status (0x%x)\n", __func__, data);
253 return -EIO;
254 }
255
256 return 0;
257}
258
Jan Glauberdfcd8212016-03-18 09:46:26 +0100259/* send STOP to the bus */
260static void octeon_i2c_stop(struct octeon_i2c *i2c)
Rade Bozic85660f42010-01-28 12:47:07 -0800261{
Rade Bozic85660f42010-01-28 12:47:07 -0800262 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
263 TWSI_CTL_ENAB | TWSI_CTL_STP);
Rade Bozic85660f42010-01-28 12:47:07 -0800264}
265
266/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100267 * octeon_i2c_write - send data to the bus via low-level controller
268 * @i2c: The struct octeon_i2c
269 * @target: Target address
270 * @data: Pointer to the data to be sent
271 * @length: Length of the data
Rade Bozic85660f42010-01-28 12:47:07 -0800272 *
273 * The address is sent over the bus, then the data.
274 *
275 * Returns 0 on success, otherwise a negative errno.
276 */
277static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
278 const u8 *data, int length)
279{
280 int i, result;
281 u8 tmp;
282
283 result = octeon_i2c_start(i2c);
284 if (result)
285 return result;
286
287 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, target << 1);
288 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
289
290 result = octeon_i2c_wait(i2c);
291 if (result)
292 return result;
293
294 for (i = 0; i < length; i++) {
295 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100296
Rade Bozic85660f42010-01-28 12:47:07 -0800297 if ((tmp != STAT_TXADDR_ACK) && (tmp != STAT_TXDATA_ACK)) {
298 dev_err(i2c->dev,
299 "%s: bad status before write (0x%x)\n",
300 __func__, tmp);
301 return -EIO;
302 }
303
304 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, data[i]);
305 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
306
307 result = octeon_i2c_wait(i2c);
308 if (result)
309 return result;
310 }
311
312 return 0;
313}
314
315/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100316 * octeon_i2c_read - receive data from the bus via low-level controller
317 * @i2c: The struct octeon_i2c
318 * @target: Target address
319 * @data: Pointer to the location to store the data
320 * @length: Length of the data
Rade Bozic85660f42010-01-28 12:47:07 -0800321 *
322 * The address is sent over the bus, then the data is read.
323 *
324 * Returns 0 on success, otherwise a negative errno.
325 */
326static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
327 u8 *data, int length)
328{
329 int i, result;
330 u8 tmp;
331
332 if (length < 1)
333 return -EINVAL;
334
335 result = octeon_i2c_start(i2c);
336 if (result)
337 return result;
338
Jan Glauberdfcd8212016-03-18 09:46:26 +0100339 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, (target << 1) | 1);
Rade Bozic85660f42010-01-28 12:47:07 -0800340 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
341
342 result = octeon_i2c_wait(i2c);
343 if (result)
344 return result;
345
346 for (i = 0; i < length; i++) {
347 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100348
Rade Bozic85660f42010-01-28 12:47:07 -0800349 if ((tmp != STAT_RXDATA_ACK) && (tmp != STAT_RXADDR_ACK)) {
350 dev_err(i2c->dev,
351 "%s: bad status before read (0x%x)\n",
352 __func__, tmp);
353 return -EIO;
354 }
355
Jan Glauberdfcd8212016-03-18 09:46:26 +0100356 if (i + 1 < length)
Rade Bozic85660f42010-01-28 12:47:07 -0800357 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100358 TWSI_CTL_ENAB | TWSI_CTL_AAK);
Rade Bozic85660f42010-01-28 12:47:07 -0800359 else
360 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100361 TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800362
363 result = octeon_i2c_wait(i2c);
364 if (result)
365 return result;
366
367 data[i] = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_DATA);
368 }
369 return 0;
370}
371
372/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100373 * octeon_i2c_xfer - The driver's master_xfer function
374 * @adap: Pointer to the i2c_adapter structure
375 * @msgs: Pointer to the messages to be processed
376 * @num: Length of the MSGS array
Rade Bozic85660f42010-01-28 12:47:07 -0800377 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100378 * Returns the number of messages processed, or a negative errno on failure.
Rade Bozic85660f42010-01-28 12:47:07 -0800379 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100380static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
Rade Bozic85660f42010-01-28 12:47:07 -0800381 int num)
382{
Rade Bozic85660f42010-01-28 12:47:07 -0800383 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100384 int i, ret = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800385
386 for (i = 0; ret == 0 && i < num; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100387 struct i2c_msg *pmsg = &msgs[i];
388
Rade Bozic85660f42010-01-28 12:47:07 -0800389 dev_dbg(i2c->dev,
390 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
391 pmsg->flags & I2C_M_RD ? "read" : "write",
392 pmsg->len, pmsg->addr, i + 1, num);
393 if (pmsg->flags & I2C_M_RD)
394 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100395 pmsg->len);
Rade Bozic85660f42010-01-28 12:47:07 -0800396 else
397 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100398 pmsg->len);
Rade Bozic85660f42010-01-28 12:47:07 -0800399 }
400 octeon_i2c_stop(i2c);
401
402 return (ret != 0) ? ret : num;
403}
404
405static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
406{
407 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
408}
409
410static const struct i2c_algorithm octeon_i2c_algo = {
411 .master_xfer = octeon_i2c_xfer,
412 .functionality = octeon_i2c_functionality,
413};
414
415static struct i2c_adapter octeon_i2c_ops = {
416 .owner = THIS_MODULE,
417 .name = "OCTEON adapter",
418 .algo = &octeon_i2c_algo,
송은봉73f37dc2013-04-18 14:01:05 +0000419 .timeout = HZ / 50,
Rade Bozic85660f42010-01-28 12:47:07 -0800420};
421
Jan Glauberbd7784c2016-03-07 16:10:44 +0100422/* calculate and set clock divisors */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100423static void octeon_i2c_set_clock(struct octeon_i2c *i2c)
Rade Bozic85660f42010-01-28 12:47:07 -0800424{
425 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
426 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
427
428 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
429 /*
430 * An mdiv value of less than 2 seems to not work well
Jan Glauberdfcd8212016-03-18 09:46:26 +0100431 * with ds1337 RTCs, so we constrain it to larger values.
Rade Bozic85660f42010-01-28 12:47:07 -0800432 */
433 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
434 /*
435 * For given ndiv and mdiv values check the
436 * two closest thp values.
437 */
438 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
439 tclk *= (1 << ndiv_idx);
440 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100441
Rade Bozic85660f42010-01-28 12:47:07 -0800442 for (inc = 0; inc <= 1; inc++) {
443 thp_idx = thp_base + inc;
444 if (thp_idx < 5 || thp_idx > 0xff)
445 continue;
446
447 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
448 foscl = foscl / (1 << ndiv_idx);
449 foscl = foscl / (mdiv_idx + 1) / 10;
450 diff = abs(foscl - i2c->twsi_freq);
451 if (diff < delta_hz) {
452 delta_hz = diff;
453 thp = thp_idx;
454 mdiv = mdiv_idx;
455 ndiv = ndiv_idx;
456 }
457 }
458 }
459 }
460 octeon_i2c_write_sw(i2c, SW_TWSI_OP_TWSI_CLK, thp);
461 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
Rade Bozic85660f42010-01-28 12:47:07 -0800462}
463
Jan Glauberdfcd8212016-03-18 09:46:26 +0100464static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
Rade Bozic85660f42010-01-28 12:47:07 -0800465{
466 u8 status;
467 int tries;
468
469 /* disable high level controller, enable bus access */
470 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
471
472 /* reset controller */
473 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_RST, 0);
474
475 for (tries = 10; tries; tries--) {
476 udelay(1);
477 status = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
478 if (status == STAT_IDLE)
479 return 0;
480 }
481 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", __func__, status);
482 return -EIO;
483}
484
Bill Pemberton0b255e92012-11-27 15:59:38 -0500485static int octeon_i2c_probe(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800486{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100487 struct device_node *node = pdev->dev.of_node;
Rade Bozic85660f42010-01-28 12:47:07 -0800488 struct resource *res_mem;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100489 struct octeon_i2c *i2c;
490 int irq, result = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800491
492 /* All adaptors have an irq. */
493 irq = platform_get_irq(pdev, 0);
494 if (irq < 0)
495 return irq;
496
David Daneyf353a212012-07-05 18:12:39 +0200497 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Rade Bozic85660f42010-01-28 12:47:07 -0800498 if (!i2c) {
Rade Bozic85660f42010-01-28 12:47:07 -0800499 result = -ENOMEM;
500 goto out;
501 }
502 i2c->dev = &pdev->dev;
Rade Bozic85660f42010-01-28 12:47:07 -0800503
504 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
505
506 if (res_mem == NULL) {
507 dev_err(i2c->dev, "found no memory resource\n");
508 result = -ENXIO;
David Daneyf353a212012-07-05 18:12:39 +0200509 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800510 }
Rade Bozic85660f42010-01-28 12:47:07 -0800511 i2c->twsi_phys = res_mem->start;
512 i2c->regsize = resource_size(res_mem);
Rade Bozic85660f42010-01-28 12:47:07 -0800513
David Daneyf353a212012-07-05 18:12:39 +0200514 /*
515 * "clock-rate" is a legacy binding, the official binding is
516 * "clock-frequency". Try the official one first and then
517 * fall back if it doesn't exist.
518 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100519 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
520 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
David Daneyf353a212012-07-05 18:12:39 +0200521 dev_err(i2c->dev,
522 "no I2C 'clock-rate' or 'clock-frequency' property\n");
523 result = -ENXIO;
524 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800525 }
David Daneyf353a212012-07-05 18:12:39 +0200526
527 i2c->sys_freq = octeon_get_io_clock_rate();
528
529 if (!devm_request_mem_region(&pdev->dev, i2c->twsi_phys, i2c->regsize,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100530 res_mem->name)) {
David Daneyf353a212012-07-05 18:12:39 +0200531 dev_err(i2c->dev, "request_mem_region failed\n");
532 goto out;
533 }
534 i2c->twsi_base = devm_ioremap(&pdev->dev, i2c->twsi_phys, i2c->regsize);
Rade Bozic85660f42010-01-28 12:47:07 -0800535
536 init_waitqueue_head(&i2c->queue);
537
538 i2c->irq = irq;
539
David Daneyf353a212012-07-05 18:12:39 +0200540 result = devm_request_irq(&pdev->dev, i2c->irq,
541 octeon_i2c_isr, 0, DRV_NAME, i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800542 if (result < 0) {
543 dev_err(i2c->dev, "failed to attach interrupt\n");
David Daneyf353a212012-07-05 18:12:39 +0200544 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800545 }
546
Jan Glauberdfcd8212016-03-18 09:46:26 +0100547 result = octeon_i2c_init_lowlevel(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800548 if (result) {
549 dev_err(i2c->dev, "init low level failed\n");
David Daneyf353a212012-07-05 18:12:39 +0200550 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800551 }
552
Jan Glauberdfcd8212016-03-18 09:46:26 +0100553 octeon_i2c_set_clock(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800554
555 i2c->adap = octeon_i2c_ops;
556 i2c->adap.dev.parent = &pdev->dev;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100557 i2c->adap.dev.of_node = node;
Rade Bozic85660f42010-01-28 12:47:07 -0800558 i2c_set_adapdata(&i2c->adap, i2c);
559 platform_set_drvdata(pdev, i2c);
560
David Daneyf353a212012-07-05 18:12:39 +0200561 result = i2c_add_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800562 if (result < 0) {
563 dev_err(i2c->dev, "failed to add adapter\n");
Doug Anderson55827f42013-02-15 13:18:35 +0000564 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800565 }
Jan Glauberdfcd8212016-03-18 09:46:26 +0100566 dev_info(i2c->dev, "probed\n");
David Daneyf353a212012-07-05 18:12:39 +0200567 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800568
Rade Bozic85660f42010-01-28 12:47:07 -0800569out:
570 return result;
571};
572
Bill Pemberton0b255e92012-11-27 15:59:38 -0500573static int octeon_i2c_remove(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800574{
575 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
576
577 i2c_del_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800578 return 0;
579};
580
Jan Glauberdfcd8212016-03-18 09:46:26 +0100581static const struct of_device_id octeon_i2c_match[] = {
582 { .compatible = "cavium,octeon-3860-twsi", },
David Daneyf353a212012-07-05 18:12:39 +0200583 {},
584};
585MODULE_DEVICE_TABLE(of, octeon_i2c_match);
586
Rade Bozic85660f42010-01-28 12:47:07 -0800587static struct platform_driver octeon_i2c_driver = {
588 .probe = octeon_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500589 .remove = octeon_i2c_remove,
Rade Bozic85660f42010-01-28 12:47:07 -0800590 .driver = {
Rade Bozic85660f42010-01-28 12:47:07 -0800591 .name = DRV_NAME,
David Daneyf353a212012-07-05 18:12:39 +0200592 .of_match_table = octeon_i2c_match,
Rade Bozic85660f42010-01-28 12:47:07 -0800593 },
594};
595
Axel Lina3664b52012-01-12 20:32:04 +0100596module_platform_driver(octeon_i2c_driver);
Rade Bozic85660f42010-01-28 12:47:07 -0800597
598MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
599MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
600MODULE_LICENSE("GPL");