1 /*
2  * (C) Copyright 2009-2010
3  * Nokia Siemens Networks, michael.lawnick.ext@nsn.com
4  *
5  * Portions Copyright (C) 2010 - 2016 Cavium, Inc.
6  *
7  * This file contains the shared part of the driver for the i2c adapter in
8  * Cavium Networks' OCTEON processors and ThunderX SOCs.
9  *
10  * This file is licensed under the terms of the GNU General Public
11  * License version 2. This program is licensed "as is" without any
12  * warranty of any kind, whether express or implied.
13  */
14 
15 #include <linux/delay.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 
22 #include "i2c-octeon-core.h"
23 
24 #define INITIAL_DELTA_HZ		1000000
25 #define TWSI_MASTER_CLK_REG_DEF_VAL	0x18
26 #define TWSI_MASTER_CLK_REG_OTX2_VAL	0x3
27 
28 /* interrupt service routine */
octeon_i2c_isr(int irq,void * dev_id)29 irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
30 {
31 	struct octeon_i2c *i2c = dev_id;
32 
33 	i2c->int_disable(i2c);
34 	wake_up(&i2c->queue);
35 
36 	return IRQ_HANDLED;
37 }
38 
octeon_i2c_test_iflg(struct octeon_i2c * i2c)39 static bool octeon_i2c_test_iflg(struct octeon_i2c *i2c)
40 {
41 	return (octeon_i2c_ctl_read(i2c) & TWSI_CTL_IFLG);
42 }
43 
44 /**
45  * octeon_i2c_wait - wait for the IFLG to be set
46  * @i2c: The struct octeon_i2c
47  *
48  * Returns: 0 on success, otherwise a negative errno.
49  */
octeon_i2c_wait(struct octeon_i2c * i2c)50 static int octeon_i2c_wait(struct octeon_i2c *i2c)
51 {
52 	long time_left;
53 
54 	/*
55 	 * Some chip revisions don't assert the irq in the interrupt
56 	 * controller. So we must poll for the IFLG change.
57 	 */
58 	if (i2c->broken_irq_mode) {
59 		u64 end = get_jiffies_64() + i2c->adap.timeout;
60 
61 		while (!octeon_i2c_test_iflg(i2c) &&
62 		       time_before64(get_jiffies_64(), end))
63 			usleep_range(I2C_OCTEON_EVENT_WAIT / 2, I2C_OCTEON_EVENT_WAIT);
64 
65 		return octeon_i2c_test_iflg(i2c) ? 0 : -ETIMEDOUT;
66 	}
67 
68 	i2c->int_enable(i2c);
69 	time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
70 				       i2c->adap.timeout);
71 	i2c->int_disable(i2c);
72 
73 	if (i2c->broken_irq_check && !time_left &&
74 	    octeon_i2c_test_iflg(i2c)) {
75 		dev_err(i2c->dev, "broken irq connection detected, switching to polling mode.\n");
76 		i2c->broken_irq_mode = true;
77 		return 0;
78 	}
79 
80 	if (!time_left)
81 		return -ETIMEDOUT;
82 
83 	return 0;
84 }
85 
octeon_i2c_hlc_test_valid(struct octeon_i2c * i2c)86 static bool octeon_i2c_hlc_test_valid(struct octeon_i2c *i2c)
87 {
88 	return (__raw_readq(i2c->twsi_base + OCTEON_REG_SW_TWSI(i2c)) & SW_TWSI_V) == 0;
89 }
90 
octeon_i2c_hlc_int_clear(struct octeon_i2c * i2c)91 static void octeon_i2c_hlc_int_clear(struct octeon_i2c *i2c)
92 {
93 	/* clear ST/TS events, listen for neither */
94 	octeon_i2c_write_int(i2c, TWSI_INT_ST_INT | TWSI_INT_TS_INT);
95 }
96 
97 /*
98  * Cleanup low-level state & enable high-level controller.
99  */
octeon_i2c_hlc_enable(struct octeon_i2c * i2c)100 static void octeon_i2c_hlc_enable(struct octeon_i2c *i2c)
101 {
102 	int try = 0;
103 	u64 val;
104 
105 	if (i2c->hlc_enabled)
106 		return;
107 	i2c->hlc_enabled = true;
108 
109 	while (1) {
110 		val = octeon_i2c_ctl_read(i2c);
111 		if (!(val & (TWSI_CTL_STA | TWSI_CTL_STP)))
112 			break;
113 
114 		/* clear IFLG event */
115 		if (val & TWSI_CTL_IFLG)
116 			octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
117 
118 		if (try++ > 100) {
119 			pr_err("%s: giving up\n", __func__);
120 			break;
121 		}
122 
123 		/* spin until any start/stop has finished */
124 		udelay(10);
125 	}
126 	octeon_i2c_ctl_write(i2c, TWSI_CTL_CE | TWSI_CTL_AAK | TWSI_CTL_ENAB);
127 }
128 
octeon_i2c_hlc_disable(struct octeon_i2c * i2c)129 static void octeon_i2c_hlc_disable(struct octeon_i2c *i2c)
130 {
131 	if (!i2c->hlc_enabled)
132 		return;
133 
134 	i2c->hlc_enabled = false;
135 	octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
136 }
137 
138 /**
139  * octeon_i2c_hlc_wait - wait for an HLC operation to complete
140  * @i2c: The struct octeon_i2c
141  *
142  * Returns: 0 on success, otherwise -ETIMEDOUT.
143  */
octeon_i2c_hlc_wait(struct octeon_i2c * i2c)144 static int octeon_i2c_hlc_wait(struct octeon_i2c *i2c)
145 {
146 	int time_left;
147 
148 	/*
149 	 * Some cn38xx boards don't assert the irq in the interrupt
150 	 * controller. So we must poll for the valid bit change.
151 	 */
152 	if (i2c->broken_irq_mode) {
153 		u64 end = get_jiffies_64() + i2c->adap.timeout;
154 
155 		while (!octeon_i2c_hlc_test_valid(i2c) &&
156 		       time_before64(get_jiffies_64(), end))
157 			usleep_range(I2C_OCTEON_EVENT_WAIT / 2, I2C_OCTEON_EVENT_WAIT);
158 
159 		return octeon_i2c_hlc_test_valid(i2c) ? 0 : -ETIMEDOUT;
160 	}
161 
162 	i2c->hlc_int_enable(i2c);
163 	time_left = wait_event_timeout(i2c->queue,
164 				       octeon_i2c_hlc_test_valid(i2c),
165 				       i2c->adap.timeout);
166 	i2c->hlc_int_disable(i2c);
167 	if (!time_left)
168 		octeon_i2c_hlc_int_clear(i2c);
169 
170 	if (i2c->broken_irq_check && !time_left &&
171 	    octeon_i2c_hlc_test_valid(i2c)) {
172 		dev_err(i2c->dev, "broken irq connection detected, switching to polling mode.\n");
173 		i2c->broken_irq_mode = true;
174 		return 0;
175 	}
176 
177 	if (!time_left)
178 		return -ETIMEDOUT;
179 	return 0;
180 }
181 
octeon_i2c_check_status(struct octeon_i2c * i2c,int final_read)182 static int octeon_i2c_check_status(struct octeon_i2c *i2c, int final_read)
183 {
184 	u8 stat;
185 	u64 mode;
186 
187 	/*
188 	 * This is ugly... in HLC mode the status is not in the status register
189 	 * but in the lower 8 bits of OCTEON_REG_SW_TWSI.
190 	 */
191 	if (i2c->hlc_enabled)
192 		stat = __raw_readq(i2c->twsi_base + OCTEON_REG_SW_TWSI(i2c));
193 	else
194 		stat = octeon_i2c_stat_read(i2c);
195 
196 	switch (stat) {
197 	/* Everything is fine */
198 	case STAT_IDLE:
199 	case STAT_AD2W_ACK:
200 	case STAT_RXADDR_ACK:
201 	case STAT_TXADDR_ACK:
202 	case STAT_TXDATA_ACK:
203 		return 0;
204 
205 	/* ACK allowed on pre-terminal bytes only */
206 	case STAT_RXDATA_ACK:
207 		if (!final_read)
208 			return 0;
209 		return -EIO;
210 
211 	/* NAK allowed on terminal byte only */
212 	case STAT_RXDATA_NAK:
213 		if (final_read)
214 			return 0;
215 		return -EIO;
216 
217 	/* Arbitration lost */
218 	case STAT_LOST_ARB_38:
219 	case STAT_LOST_ARB_68:
220 	case STAT_LOST_ARB_78:
221 	case STAT_LOST_ARB_B0:
222 		return -EAGAIN;
223 
224 	/* Being addressed as local target, should back off & listen */
225 	case STAT_SLAVE_60:
226 	case STAT_SLAVE_70:
227 	case STAT_GENDATA_ACK:
228 	case STAT_GENDATA_NAK:
229 		return -EOPNOTSUPP;
230 
231 	/* Core busy as local target */
232 	case STAT_SLAVE_80:
233 	case STAT_SLAVE_88:
234 	case STAT_SLAVE_A0:
235 	case STAT_SLAVE_A8:
236 	case STAT_SLAVE_LOST:
237 	case STAT_SLAVE_NAK:
238 	case STAT_SLAVE_ACK:
239 		return -EOPNOTSUPP;
240 
241 	case STAT_TXDATA_NAK:
242 	case STAT_BUS_ERROR:
243 		return -EIO;
244 	case STAT_TXADDR_NAK:
245 	case STAT_RXADDR_NAK:
246 	case STAT_AD2W_NAK:
247 		return -ENXIO;
248 
249 	case STAT_WDOG_TOUT:
250 		mode = __raw_readq(i2c->twsi_base + OCTEON_REG_MODE(i2c));
251 		/* Set BUS_MON_RST to reset bus monitor */
252 		mode |= BUS_MON_RST_MASK;
253 		octeon_i2c_writeq_flush(mode, i2c->twsi_base + OCTEON_REG_MODE(i2c));
254 		return -EIO;
255 	default:
256 		dev_err(i2c->dev, "unhandled state: %d\n", stat);
257 		return -EIO;
258 	}
259 }
260 
octeon_i2c_recovery(struct octeon_i2c * i2c)261 static int octeon_i2c_recovery(struct octeon_i2c *i2c)
262 {
263 	int ret;
264 
265 	ret = i2c_recover_bus(&i2c->adap);
266 	if (ret)
267 		/* recover failed, try hardware re-init */
268 		ret = octeon_i2c_init_lowlevel(i2c);
269 	return ret;
270 }
271 
272 /**
273  * octeon_i2c_start - send START to the bus
274  * @i2c: The struct octeon_i2c
275  *
276  * Returns: 0 on success, otherwise a negative errno.
277  */
octeon_i2c_start(struct octeon_i2c * i2c)278 static int octeon_i2c_start(struct octeon_i2c *i2c)
279 {
280 	int ret;
281 	u8 stat;
282 
283 	octeon_i2c_hlc_disable(i2c);
284 
285 	octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
286 	ret = octeon_i2c_wait(i2c);
287 	if (ret)
288 		goto error;
289 
290 	stat = octeon_i2c_stat_read(i2c);
291 	if (stat == STAT_START || stat == STAT_REP_START)
292 		/* START successful, bail out */
293 		return 0;
294 
295 error:
296 	/* START failed, try to recover */
297 	ret = octeon_i2c_recovery(i2c);
298 	return (ret) ? ret : -EAGAIN;
299 }
300 
301 /* send STOP to the bus */
octeon_i2c_stop(struct octeon_i2c * i2c)302 static void octeon_i2c_stop(struct octeon_i2c *i2c)
303 {
304 	octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STP);
305 }
306 
307 /**
308  * octeon_i2c_read - receive data from the bus via low-level controller
309  * @i2c: The struct octeon_i2c
310  * @target: Target address
311  * @data: Pointer to the location to store the data
312  * @rlength: Length of the data
313  * @recv_len: flag for length byte
314  *
315  * The address is sent over the bus, then the data is read.
316  *
317  * Returns: 0 on success, otherwise a negative errno.
318  */
octeon_i2c_read(struct octeon_i2c * i2c,int target,u8 * data,u16 * rlength,bool recv_len)319 static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
320 			   u8 *data, u16 *rlength, bool recv_len)
321 {
322 	int i, result, length = *rlength;
323 	bool final_read = false;
324 
325 	octeon_i2c_data_write(i2c, (target << 1) | 1);
326 	octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
327 
328 	result = octeon_i2c_wait(i2c);
329 	if (result)
330 		return result;
331 
332 	/* address OK ? */
333 	result = octeon_i2c_check_status(i2c, false);
334 	if (result)
335 		return result;
336 
337 	for (i = 0; i < length; i++) {
338 		/*
339 		 * For the last byte to receive TWSI_CTL_AAK must not be set.
340 		 *
341 		 * A special case is I2C_M_RECV_LEN where we don't know the
342 		 * additional length yet. If recv_len is set we assume we're
343 		 * not reading the final byte and therefore need to set
344 		 * TWSI_CTL_AAK.
345 		 */
346 		if ((i + 1 == length) && !(recv_len && i == 0))
347 			final_read = true;
348 
349 		/* clear iflg to allow next event */
350 		if (final_read)
351 			octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
352 		else
353 			octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_AAK);
354 
355 		result = octeon_i2c_wait(i2c);
356 		if (result)
357 			return result;
358 
359 		data[i] = octeon_i2c_data_read(i2c, &result);
360 		if (result)
361 			return result;
362 		if (recv_len && i == 0) {
363 			if (data[i] > I2C_SMBUS_BLOCK_MAX)
364 				return -EPROTO;
365 			length += data[i];
366 		}
367 
368 		result = octeon_i2c_check_status(i2c, final_read);
369 		if (result)
370 			return result;
371 	}
372 	*rlength = length;
373 	return 0;
374 }
375 
376 /**
377  * octeon_i2c_write - send data to the bus via low-level controller
378  * @i2c: The struct octeon_i2c
379  * @target: Target address
380  * @data: Pointer to the data to be sent
381  * @length: Length of the data
382  *
383  * The address is sent over the bus, then the data.
384  *
385  * Returns: 0 on success, otherwise a negative errno.
386  */
octeon_i2c_write(struct octeon_i2c * i2c,int target,const u8 * data,int length)387 static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
388 			    const u8 *data, int length)
389 {
390 	int i, result;
391 
392 	octeon_i2c_data_write(i2c, target << 1);
393 	octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
394 
395 	result = octeon_i2c_wait(i2c);
396 	if (result)
397 		return result;
398 
399 	for (i = 0; i < length; i++) {
400 		result = octeon_i2c_check_status(i2c, false);
401 		if (result)
402 			return result;
403 
404 		octeon_i2c_data_write(i2c, data[i]);
405 		octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
406 
407 		result = octeon_i2c_wait(i2c);
408 		if (result)
409 			return result;
410 	}
411 
412 	return 0;
413 }
414 
415 /* high-level-controller pure read of up to 8 bytes */
octeon_i2c_hlc_read(struct octeon_i2c * i2c,struct i2c_msg * msgs)416 static int octeon_i2c_hlc_read(struct octeon_i2c *i2c, struct i2c_msg *msgs)
417 {
418 	int i, j, ret = 0;
419 	u64 cmd;
420 
421 	octeon_i2c_hlc_enable(i2c);
422 	octeon_i2c_hlc_int_clear(i2c);
423 
424 	cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR | SW_TWSI_OP_7;
425 	/* SIZE */
426 	cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT;
427 	/* A */
428 	cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
429 
430 	octeon_i2c_writeq_flush(cmd, i2c->twsi_base + OCTEON_REG_SW_TWSI(i2c));
431 	ret = octeon_i2c_hlc_wait(i2c);
432 	if (ret)
433 		goto err;
434 
435 	cmd = __raw_readq(i2c->twsi_base + OCTEON_REG_SW_TWSI(i2c));
436 	if ((cmd & SW_TWSI_R) == 0)
437 		return octeon_i2c_check_status(i2c, false);
438 
439 	for (i = 0, j = msgs[0].len - 1; i  < msgs[0].len && i < 4; i++, j--)
440 		msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff;
441 
442 	if (msgs[0].len > 4) {
443 		cmd = __raw_readq(i2c->twsi_base + OCTEON_REG_SW_TWSI_EXT(i2c));
444 		for (i = 0; i  < msgs[0].len - 4 && i < 4; i++, j--)
445 			msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff;
446 	}
447 
448 err:
449 	return ret;
450 }
451 
452 /* high-level-controller pure write of up to 8 bytes */
octeon_i2c_hlc_write(struct octeon_i2c * i2c,struct i2c_msg * msgs)453 static int octeon_i2c_hlc_write(struct octeon_i2c *i2c, struct i2c_msg *msgs)
454 {
455 	int i, j, ret = 0;
456 	u64 cmd;
457 
458 	octeon_i2c_hlc_enable(i2c);
459 	octeon_i2c_hlc_int_clear(i2c);
460 
461 	cmd = SW_TWSI_V | SW_TWSI_SOVR | SW_TWSI_OP_7;
462 	/* SIZE */
463 	cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT;
464 	/* A */
465 	cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
466 
467 	for (i = 0, j = msgs[0].len - 1; i  < msgs[0].len && i < 4; i++, j--)
468 		cmd |= (u64)msgs[0].buf[j] << (8 * i);
469 
470 	if (msgs[0].len > 4) {
471 		u64 ext = 0;
472 
473 		for (i = 0; i < msgs[0].len - 4 && i < 4; i++, j--)
474 			ext |= (u64)msgs[0].buf[j] << (8 * i);
475 		octeon_i2c_writeq_flush(ext, i2c->twsi_base + OCTEON_REG_SW_TWSI_EXT(i2c));
476 	}
477 
478 	octeon_i2c_writeq_flush(cmd, i2c->twsi_base + OCTEON_REG_SW_TWSI(i2c));
479 	ret = octeon_i2c_hlc_wait(i2c);
480 	if (ret)
481 		goto err;
482 
483 	cmd = __raw_readq(i2c->twsi_base + OCTEON_REG_SW_TWSI(i2c));
484 	if ((cmd & SW_TWSI_R) == 0)
485 		return octeon_i2c_check_status(i2c, false);
486 
487 err:
488 	return ret;
489 }
490 
491 /* Process hlc transaction */
octeon_i2c_hlc_cmd_send(struct octeon_i2c * i2c,u64 cmd)492 static int octeon_i2c_hlc_cmd_send(struct octeon_i2c *i2c, u64 cmd)
493 {
494 	octeon_i2c_hlc_int_clear(i2c);
495 	octeon_i2c_writeq_flush(cmd, i2c->twsi_base + OCTEON_REG_SW_TWSI(i2c));
496 
497 	return octeon_i2c_hlc_wait(i2c);
498 }
499 
500 /* Generic consideration for extended internal addresses in i2c hlc r/w ops */
octeon_i2c_hlc_ext(struct octeon_i2c * i2c,struct i2c_msg msg,u64 * cmd_in,u64 * ext)501 static bool octeon_i2c_hlc_ext(struct octeon_i2c *i2c, struct i2c_msg msg, u64 *cmd_in, u64 *ext)
502 {
503 	bool set_ext = false;
504 	u64 cmd = 0;
505 
506 	if (msg.len == 2) {
507 		cmd |= SW_TWSI_EIA;
508 		*ext = (u64)msg.buf[0] << SW_TWSI_IA_SHIFT;
509 		cmd |= (u64)msg.buf[1] << SW_TWSI_IA_SHIFT;
510 		set_ext = true;
511 	} else {
512 		cmd |= (u64)msg.buf[0] << SW_TWSI_IA_SHIFT;
513 	}
514 
515 	*cmd_in |= cmd;
516 	return set_ext;
517 }
518 
519 /* Construct and send i2c transaction core cmd for read ops */
octeon_i2c_hlc_read_cmd(struct octeon_i2c * i2c,struct i2c_msg msg,u64 cmd)520 static int octeon_i2c_hlc_read_cmd(struct octeon_i2c *i2c, struct i2c_msg msg, u64 cmd)
521 {
522 	u64 ext = 0;
523 
524 	if (octeon_i2c_hlc_ext(i2c, msg, &cmd, &ext))
525 		octeon_i2c_writeq_flush(ext, i2c->twsi_base + OCTEON_REG_SW_TWSI_EXT(i2c));
526 
527 	return octeon_i2c_hlc_cmd_send(i2c, cmd);
528 }
529 
530 /* high-level-controller composite write+read, msg0=addr, msg1=data */
octeon_i2c_hlc_comp_read(struct octeon_i2c * i2c,struct i2c_msg * msgs)531 static int octeon_i2c_hlc_comp_read(struct octeon_i2c *i2c, struct i2c_msg *msgs)
532 {
533 	int i, j, ret = 0;
534 	u64 cmd;
535 
536 	octeon_i2c_hlc_enable(i2c);
537 
538 	cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR | SW_TWSI_OP_7_IA;
539 	/* SIZE */
540 	cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT;
541 	/* A */
542 	cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
543 
544 	/* Send core command */
545 	ret = octeon_i2c_hlc_read_cmd(i2c, msgs[0], cmd);
546 	if (ret)
547 		goto err;
548 
549 	cmd = __raw_readq(i2c->twsi_base + OCTEON_REG_SW_TWSI(i2c));
550 	if ((cmd & SW_TWSI_R) == 0)
551 		return octeon_i2c_check_status(i2c, false);
552 
553 	for (i = 0, j = msgs[1].len - 1; i  < msgs[1].len && i < 4; i++, j--)
554 		msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff;
555 
556 	if (msgs[1].len > 4) {
557 		cmd = __raw_readq(i2c->twsi_base + OCTEON_REG_SW_TWSI_EXT(i2c));
558 		for (i = 0; i  < msgs[1].len - 4 && i < 4; i++, j--)
559 			msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff;
560 	}
561 
562 err:
563 	return ret;
564 }
565 
566 /* high-level-controller composite write+write, m[0]len<=2, m[1]len<=8 */
octeon_i2c_hlc_comp_write(struct octeon_i2c * i2c,struct i2c_msg * msgs)567 static int octeon_i2c_hlc_comp_write(struct octeon_i2c *i2c, struct i2c_msg *msgs)
568 {
569 	bool set_ext = false;
570 	int i, j, ret = 0;
571 	u64 cmd, ext = 0;
572 
573 	octeon_i2c_hlc_enable(i2c);
574 
575 	cmd = SW_TWSI_V | SW_TWSI_SOVR | SW_TWSI_OP_7_IA;
576 	/* SIZE */
577 	cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT;
578 	/* A */
579 	cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
580 
581 	/* Set parameters for extended message (if required) */
582 	set_ext = octeon_i2c_hlc_ext(i2c, msgs[0], &cmd, &ext);
583 
584 	for (i = 0, j = msgs[1].len - 1; i  < msgs[1].len && i < 4; i++, j--)
585 		cmd |= (u64)msgs[1].buf[j] << (8 * i);
586 
587 	if (msgs[1].len > 4) {
588 		for (i = 0; i < msgs[1].len - 4 && i < 4; i++, j--)
589 			ext |= (u64)msgs[1].buf[j] << (8 * i);
590 		set_ext = true;
591 	}
592 	if (set_ext)
593 		octeon_i2c_writeq_flush(ext, i2c->twsi_base + OCTEON_REG_SW_TWSI_EXT(i2c));
594 
595 	ret = octeon_i2c_hlc_cmd_send(i2c, cmd);
596 	if (ret)
597 		goto err;
598 
599 	cmd = __raw_readq(i2c->twsi_base + OCTEON_REG_SW_TWSI(i2c));
600 	if ((cmd & SW_TWSI_R) == 0)
601 		return octeon_i2c_check_status(i2c, false);
602 
603 err:
604 	return ret;
605 }
606 
607 /**
608  * octeon_i2c_xfer - The driver's xfer function
609  * @adap: Pointer to the i2c_adapter structure
610  * @msgs: Pointer to the messages to be processed
611  * @num: Length of the MSGS array
612  *
613  * Returns: the number of messages processed, or a negative errno on failure.
614  */
octeon_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)615 int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
616 {
617 	struct octeon_i2c *i2c = i2c_get_adapdata(adap);
618 	int i, ret = 0;
619 
620 	if (IS_LS_FREQ(i2c->twsi_freq)) {
621 		if (num == 1) {
622 			if (msgs[0].len > 0 && msgs[0].len <= 8) {
623 				if (msgs[0].flags & I2C_M_RD)
624 					ret = octeon_i2c_hlc_read(i2c, msgs);
625 				else
626 					ret = octeon_i2c_hlc_write(i2c, msgs);
627 				goto out;
628 			}
629 		} else if (num == 2) {
630 			if ((msgs[0].flags & I2C_M_RD) == 0 &&
631 			    (msgs[1].flags & I2C_M_RECV_LEN) == 0 &&
632 			    msgs[0].len > 0 && msgs[0].len <= 2 &&
633 			    msgs[1].len > 0 && msgs[1].len <= 8 &&
634 			    msgs[0].addr == msgs[1].addr) {
635 				if (msgs[1].flags & I2C_M_RD)
636 					ret = octeon_i2c_hlc_comp_read(i2c, msgs);
637 				else
638 					ret = octeon_i2c_hlc_comp_write(i2c, msgs);
639 				goto out;
640 			}
641 		}
642 	}
643 
644 	for (i = 0; ret == 0 && i < num; i++) {
645 		struct i2c_msg *pmsg = &msgs[i];
646 
647 		/* zero-length messages are not supported */
648 		if (!pmsg->len) {
649 			ret = -EOPNOTSUPP;
650 			break;
651 		}
652 
653 		ret = octeon_i2c_start(i2c);
654 		if (ret)
655 			return ret;
656 
657 		if (pmsg->flags & I2C_M_RD)
658 			ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
659 					      &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
660 		else
661 			ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
662 					       pmsg->len);
663 	}
664 	octeon_i2c_stop(i2c);
665 out:
666 	return (ret != 0) ? ret : num;
667 }
668 
669 /* calculate and set clock divisors */
octeon_i2c_set_clock(struct octeon_i2c * i2c)670 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 	bool is_plat_otx2;
674 	/*
675 	 * Find divisors to produce target frequency, start with large delta
676 	 * to cover wider range of divisors, note thp = TCLK half period and
677 	 * ds is OSCL output frequency divisor.
678 	 */
679 	unsigned int thp, mdiv_min, mdiv = 2, ndiv = 0, ds = 10;
680 	unsigned int delta_hz = INITIAL_DELTA_HZ;
681 
682 	is_plat_otx2 = octeon_i2c_is_otx2(to_pci_dev(i2c->dev));
683 
684 	if (is_plat_otx2) {
685 		thp = TWSI_MASTER_CLK_REG_OTX2_VAL;
686 		mdiv_min = 0;
687 		if (!IS_LS_FREQ(i2c->twsi_freq))
688 			ds = 15;
689 	} else {
690 		thp = TWSI_MASTER_CLK_REG_DEF_VAL;
691 		mdiv_min = 2;
692 	}
693 
694 	for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
695 		/*
696 		 * An mdiv value of less than 2 seems to not work well
697 		 * with ds1337 RTCs, so we constrain it to larger values.
698 		 */
699 		for (mdiv_idx = 15; mdiv_idx >= mdiv_min && delta_hz != 0; mdiv_idx--) {
700 			/*
701 			 * For given ndiv and mdiv values check the
702 			 * two closest thp values.
703 			 */
704 			tclk = i2c->twsi_freq * (mdiv_idx + 1) * ds;
705 			tclk *= (1 << ndiv_idx);
706 			if (is_plat_otx2)
707 				thp_base = (i2c->sys_freq / tclk) - 2;
708 			else
709 				thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
710 
711 			for (inc = 0; inc <= 1; inc++) {
712 				thp_idx = thp_base + inc;
713 				if (thp_idx < 5 || thp_idx > 0xff)
714 					continue;
715 
716 				if (is_plat_otx2)
717 					foscl = i2c->sys_freq / (thp_idx + 2);
718 				else
719 					foscl = i2c->sys_freq /
720 						(2 * (thp_idx + 1));
721 				foscl = foscl / (1 << ndiv_idx);
722 				foscl = foscl / (mdiv_idx + 1) / ds;
723 				if (foscl > i2c->twsi_freq)
724 					continue;
725 				diff = abs(foscl - i2c->twsi_freq);
726 				/*
727 				 * Diff holds difference between calculated frequency
728 				 * value vs desired frequency.
729 				 * Delta_hz is updated with last minimum diff.
730 				 */
731 				if (diff < delta_hz) {
732 					delta_hz = diff;
733 					thp = thp_idx;
734 					mdiv = mdiv_idx;
735 					ndiv = ndiv_idx;
736 				}
737 			}
738 		}
739 	}
740 	octeon_i2c_reg_write(i2c, SW_TWSI_OP_TWSI_CLK, thp);
741 	octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
742 	if (is_plat_otx2) {
743 		u64 mode;
744 
745 		mode = __raw_readq(i2c->twsi_base + OCTEON_REG_MODE(i2c));
746 		/* Set REFCLK_SRC and HS_MODE in TWSX_MODE register */
747 		if (!IS_LS_FREQ(i2c->twsi_freq))
748 			mode |= TWSX_MODE_HS_MASK;
749 		else
750 			mode &= ~TWSX_MODE_HS_MASK;
751 		octeon_i2c_writeq_flush(mode, i2c->twsi_base + OCTEON_REG_MODE(i2c));
752 	}
753 }
754 
octeon_i2c_init_lowlevel(struct octeon_i2c * i2c)755 int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
756 {
757 	u8 status = 0;
758 	int tries;
759 
760 	/* reset controller */
761 	octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0);
762 
763 	for (tries = 10; tries && status != STAT_IDLE; tries--) {
764 		udelay(1);
765 		status = octeon_i2c_stat_read(i2c);
766 		if (status == STAT_IDLE)
767 			break;
768 	}
769 
770 	if (status != STAT_IDLE) {
771 		dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n",
772 			__func__, status);
773 		return -EIO;
774 	}
775 
776 	/* toggle twice to force both teardowns */
777 	octeon_i2c_hlc_enable(i2c);
778 	octeon_i2c_hlc_disable(i2c);
779 	return 0;
780 }
781 
octeon_i2c_get_scl(struct i2c_adapter * adap)782 static int octeon_i2c_get_scl(struct i2c_adapter *adap)
783 {
784 	struct octeon_i2c *i2c = i2c_get_adapdata(adap);
785 	u64 state;
786 
787 	state = octeon_i2c_read_int(i2c);
788 	return state & TWSI_INT_SCL;
789 }
790 
octeon_i2c_set_scl(struct i2c_adapter * adap,int val)791 static void octeon_i2c_set_scl(struct i2c_adapter *adap, int val)
792 {
793 	struct octeon_i2c *i2c = i2c_get_adapdata(adap);
794 
795 	octeon_i2c_write_int(i2c, val ? 0 : TWSI_INT_SCL_OVR);
796 }
797 
octeon_i2c_get_sda(struct i2c_adapter * adap)798 static int octeon_i2c_get_sda(struct i2c_adapter *adap)
799 {
800 	struct octeon_i2c *i2c = i2c_get_adapdata(adap);
801 	u64 state;
802 
803 	state = octeon_i2c_read_int(i2c);
804 	return state & TWSI_INT_SDA;
805 }
806 
octeon_i2c_prepare_recovery(struct i2c_adapter * adap)807 static void octeon_i2c_prepare_recovery(struct i2c_adapter *adap)
808 {
809 	struct octeon_i2c *i2c = i2c_get_adapdata(adap);
810 
811 	octeon_i2c_hlc_disable(i2c);
812 	octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0);
813 	/* wait for software reset to settle */
814 	udelay(5);
815 
816 	/*
817 	 * Bring control register to a good state regardless
818 	 * of HLC state.
819 	 */
820 	octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
821 
822 	octeon_i2c_write_int(i2c, 0);
823 }
824 
octeon_i2c_unprepare_recovery(struct i2c_adapter * adap)825 static void octeon_i2c_unprepare_recovery(struct i2c_adapter *adap)
826 {
827 	struct octeon_i2c *i2c = i2c_get_adapdata(adap);
828 
829 	/*
830 	 * Generate STOP to finish the unfinished transaction.
831 	 * Can't generate STOP via the TWSI CTL register
832 	 * since it could bring the TWSI controller into an inoperable state.
833 	 */
834 	octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR | TWSI_INT_SCL_OVR);
835 	udelay(5);
836 	octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR);
837 	udelay(5);
838 	octeon_i2c_write_int(i2c, 0);
839 }
840 
841 struct i2c_bus_recovery_info octeon_i2c_recovery_info = {
842 	.recover_bus = i2c_generic_scl_recovery,
843 	.get_scl = octeon_i2c_get_scl,
844 	.set_scl = octeon_i2c_set_scl,
845 	.get_sda = octeon_i2c_get_sda,
846 	.prepare_recovery = octeon_i2c_prepare_recovery,
847 	.unprepare_recovery = octeon_i2c_unprepare_recovery,
848 };
849