blob: c11d6ff93d17fa0799da8774506fdf18ca501756 [file] [log] [blame]
Suneel Garapati5c2c3e82020-05-26 14:13:07 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2018 Marvell International Ltd.
4 */
5
6#include <common.h>
7#include <clk.h>
8#include <dm.h>
9#include <i2c.h>
10#include <pci_ids.h>
11#include <asm/io.h>
12#include <linux/bitfield.h>
13#include <linux/compat.h>
14#include <linux/delay.h>
15
16#define TWSI_SW_TWSI 0x00
17#define TWSI_TWSI_SW 0x08
18#define TWSI_INT 0x10
19#define TWSI_SW_TWSI_EXT 0x18
20
21#define TWSI_SW_DATA_MASK GENMASK_ULL(31, 0)
22#define TWSI_SW_EOP_IA_MASK GENMASK_ULL(34, 32)
23#define TWSI_SW_IA_MASK GENMASK_ULL(39, 35)
24#define TWSI_SW_ADDR_MASK GENMASK_ULL(49, 40)
25#define TWSI_SW_SCR_MASK GENMASK_ULL(51, 50)
26#define TWSI_SW_SIZE_MASK GENMASK_ULL(54, 52)
27#define TWSI_SW_SOVR BIT_ULL(55)
28#define TWSI_SW_R BIT_ULL(56)
29#define TWSI_SW_OP_MASK GENMASK_ULL(60, 57)
30#define TWSI_SW_EIA GENMASK_ULL(61)
31#define TWSI_SW_SLONLY BIT_ULL(62)
32#define TWSI_SW_V BIT_ULL(63)
33
34#define TWSI_INT_SDA_OVR BIT_ULL(8)
35#define TWSI_INT_SCL_OVR BIT_ULL(9)
36#define TWSI_INT_SDA BIT_ULL(10)
37#define TWSI_INT_SCL BIT_ULL(11)
38
39enum {
40 TWSI_OP_WRITE = 0,
41 TWSI_OP_READ = 1,
42};
43
44enum {
45 TWSI_EOP_SLAVE_ADDR = 0,
46 TWSI_EOP_CLK_CTL = 3,
47 TWSI_SW_EOP_IA = 6,
48};
49
50enum {
51 TWSI_SLAVEADD = 0,
52 TWSI_DATA = 1,
53 TWSI_CTL = 2,
54 TWSI_CLKCTL = 3,
55 TWSI_STAT = 3,
56 TWSI_SLAVEADD_EXT = 4,
57 TWSI_RST = 7,
58};
59
60enum {
61 TWSI_CTL_AAK = BIT(2),
62 TWSI_CTL_IFLG = BIT(3),
63 TWSI_CTL_STP = BIT(4),
64 TWSI_CTL_STA = BIT(5),
65 TWSI_CTL_ENAB = BIT(6),
66 TWSI_CTL_CE = BIT(7),
67};
68
69/*
70 * Internal errors. When debugging is enabled, the driver will report the
71 * error number and the user / developer can check the table below for the
72 * detailed error description.
73 */
74enum {
75 /** Bus error */
76 TWSI_STAT_BUS_ERROR = 0x00,
77 /** Start condition transmitted */
78 TWSI_STAT_START = 0x08,
79 /** Repeat start condition transmitted */
80 TWSI_STAT_RSTART = 0x10,
81 /** Address + write bit transmitted, ACK received */
82 TWSI_STAT_TXADDR_ACK = 0x18,
83 /** Address + write bit transmitted, /ACK received */
84 TWSI_STAT_TXADDR_NAK = 0x20,
85 /** Data byte transmitted in master mode, ACK received */
86 TWSI_STAT_TXDATA_ACK = 0x28,
87 /** Data byte transmitted in master mode, ACK received */
88 TWSI_STAT_TXDATA_NAK = 0x30,
89 /** Arbitration lost in address or data byte */
90 TWSI_STAT_TX_ARB_LOST = 0x38,
91 /** Address + read bit transmitted, ACK received */
92 TWSI_STAT_RXADDR_ACK = 0x40,
93 /** Address + read bit transmitted, /ACK received */
94 TWSI_STAT_RXADDR_NAK = 0x48,
95 /** Data byte received in master mode, ACK transmitted */
96 TWSI_STAT_RXDATA_ACK_SENT = 0x50,
97 /** Data byte received, NACK transmitted */
98 TWSI_STAT_RXDATA_NAK_SENT = 0x58,
99 /** Slave address received, sent ACK */
100 TWSI_STAT_SLAVE_RXADDR_ACK = 0x60,
101 /**
102 * Arbitration lost in address as master, slave address + write bit
103 * received, ACK transmitted
104 */
105 TWSI_STAT_TX_ACK_ARB_LOST = 0x68,
106 /** General call address received, ACK transmitted */
107 TWSI_STAT_RX_GEN_ADDR_ACK = 0x70,
108 /**
109 * Arbitration lost in address as master, general call address
110 * received, ACK transmitted
111 */
112 TWSI_STAT_RX_GEN_ADDR_ARB_LOST = 0x78,
113 /** Data byte received after slave address received, ACK transmitted */
114 TWSI_STAT_SLAVE_RXDATA_ACK = 0x80,
115 /** Data byte received after slave address received, /ACK transmitted */
116 TWSI_STAT_SLAVE_RXDATA_NAK = 0x88,
117 /**
118 * Data byte received after general call address received, ACK
119 * transmitted
120 */
121 TWSI_STAT_GEN_RXADDR_ACK = 0x90,
122 /**
123 * Data byte received after general call address received, /ACK
124 * transmitted
125 */
126 TWSI_STAT_GEN_RXADDR_NAK = 0x98,
127 /** STOP or repeated START condition received in slave mode */
128 TWSI_STAT_STOP_MULTI_START = 0xa0,
129 /** Slave address + read bit received, ACK transmitted */
130 TWSI_STAT_SLAVE_RXADDR2_ACK = 0xa8,
131 /**
132 * Arbitration lost in address as master, slave address + read bit
133 * received, ACK transmitted
134 */
135 TWSI_STAT_RXDATA_ACK_ARB_LOST = 0xb0,
136 /** Data byte transmitted in slave mode, ACK received */
137 TWSI_STAT_SLAVE_TXDATA_ACK = 0xb8,
138 /** Data byte transmitted in slave mode, /ACK received */
139 TWSI_STAT_SLAVE_TXDATA_NAK = 0xc0,
140 /** Last byte transmitted in slave mode, ACK received */
141 TWSI_STAT_SLAVE_TXDATA_END_ACK = 0xc8,
142 /** Second address byte + write bit transmitted, ACK received */
143 TWSI_STAT_TXADDR2DATA_ACK = 0xd0,
144 /** Second address byte + write bit transmitted, /ACK received */
145 TWSI_STAT_TXADDR2DATA_NAK = 0xd8,
146 /** No relevant status information */
147 TWSI_STAT_IDLE = 0xf8
148};
149
150#define CONFIG_SYS_I2C_OCTEON_SLAVE_ADDR 0x77
151
152enum {
153 PROBE_PCI = 0, /* PCI based probing */
154 PROBE_DT, /* DT based probing */
155};
156
157enum {
158 CLK_METHOD_OCTEON = 0,
159 CLK_METHOD_OCTEONTX2,
160};
161
162/**
163 * struct octeon_i2c_data - SoC specific data of this driver
164 *
165 * @probe: Probing of this SoC (DT vs PCI)
166 * @reg_offs: Register offset
167 * @thp: THP define for divider calculation
168 * @clk_method: Clock calculation method
169 */
170struct octeon_i2c_data {
171 int probe;
172 u32 reg_offs;
173 int thp;
174 int clk_method;
175};
176
177/**
178 * struct octeon_twsi - Private data of this driver
179 *
180 * @base: Base address of i2c registers
181 * @data: Pointer to SoC specific data struct
182 */
183struct octeon_twsi {
184 void __iomem *base;
185 const struct octeon_i2c_data *data;
186 struct clk clk;
187};
188
189static void twsi_unblock(void *base);
190static int twsi_stop(void *base);
191
192/**
193 * Returns true if we lost arbitration
194 *
195 * @code status code
196 * @final_read true if this is the final read operation
197 * @return true if arbitration has been lost, false if it hasn't been lost.
198 */
199static int twsi_i2c_lost_arb(u8 code, int final_read)
200{
201 switch (code) {
202 case TWSI_STAT_TX_ARB_LOST:
203 case TWSI_STAT_TX_ACK_ARB_LOST:
204 case TWSI_STAT_RX_GEN_ADDR_ARB_LOST:
205 case TWSI_STAT_RXDATA_ACK_ARB_LOST:
206 /* Arbitration lost */
207 return -EAGAIN;
208
209 case TWSI_STAT_SLAVE_RXADDR_ACK:
210 case TWSI_STAT_RX_GEN_ADDR_ACK:
211 case TWSI_STAT_GEN_RXADDR_ACK:
212 case TWSI_STAT_GEN_RXADDR_NAK:
213 /* Being addressed as slave, should back off and listen */
214 return -EIO;
215
216 case TWSI_STAT_SLAVE_RXDATA_ACK:
217 case TWSI_STAT_SLAVE_RXDATA_NAK:
218 case TWSI_STAT_STOP_MULTI_START:
219 case TWSI_STAT_SLAVE_RXADDR2_ACK:
220 case TWSI_STAT_SLAVE_TXDATA_ACK:
221 case TWSI_STAT_SLAVE_TXDATA_NAK:
222 case TWSI_STAT_SLAVE_TXDATA_END_ACK:
223 /* Core busy as slave */
224 return -EIO;
225
226 case TWSI_STAT_RXDATA_ACK_SENT:
227 /* Ack allowed on pre-terminal bytes only */
228 if (!final_read)
229 return 0;
230 return -EAGAIN;
231
232 case TWSI_STAT_RXDATA_NAK_SENT:
233 /* NAK allowed on terminal byte only */
234 if (!final_read)
235 return 0;
236 return -EAGAIN;
237
238 case TWSI_STAT_TXDATA_NAK:
239 case TWSI_STAT_TXADDR_NAK:
240 case TWSI_STAT_RXADDR_NAK:
241 case TWSI_STAT_TXADDR2DATA_NAK:
242 return -EAGAIN;
243 }
244
245 return 0;
246}
247
248/**
249 * Writes to the MIO_TWS(0..5)_SW_TWSI register
250 *
251 * @base Base address of i2c registers
252 * @val value to write
253 * @return 0 for success, otherwise error
254 */
255static u64 twsi_write_sw(void __iomem *base, u64 val)
256{
257 unsigned long start = get_timer(0);
258
259 val &= ~TWSI_SW_R;
260 val |= TWSI_SW_V;
261
262 debug("%s(%p, 0x%llx)\n", __func__, base, val);
263 writeq(val, base + TWSI_SW_TWSI);
264 do {
265 val = readq(base + TWSI_SW_TWSI);
266 } while ((val & TWSI_SW_V) && (get_timer(start) < 50));
267
268 if (val & TWSI_SW_V)
269 debug("%s: timed out\n", __func__);
270 return val;
271}
272
273/**
274 * Reads the MIO_TWS(0..5)_SW_TWSI register
275 *
276 * @base Base address of i2c registers
277 * @val value for eia and op, etc. to read
278 * @return value of the register
279 */
280static u64 twsi_read_sw(void __iomem *base, u64 val)
281{
282 unsigned long start = get_timer(0);
283
284 val |= TWSI_SW_R | TWSI_SW_V;
285
286 debug("%s(%p, 0x%llx)\n", __func__, base, val);
287 writeq(val, base + TWSI_SW_TWSI);
288
289 do {
290 val = readq(base + TWSI_SW_TWSI);
291 } while ((val & TWSI_SW_V) && (get_timer(start) < 50));
292
293 if (val & TWSI_SW_V)
294 debug("%s: Error writing 0x%llx\n", __func__, val);
295
296 debug("%s: Returning 0x%llx\n", __func__, val);
297 return val;
298}
299
300/**
301 * Write control register
302 *
303 * @base Base address for i2c registers
304 * @data data to write
305 */
306static void twsi_write_ctl(void __iomem *base, u8 data)
307{
308 u64 val;
309
310 debug("%s(%p, 0x%x)\n", __func__, base, data);
311 val = data | FIELD_PREP(TWSI_SW_EOP_IA_MASK, TWSI_CTL) |
312 FIELD_PREP(TWSI_SW_OP_MASK, TWSI_SW_EOP_IA);
313 twsi_write_sw(base, val);
314}
315
316/**
317 * Reads the TWSI Control Register
318 *
319 * @base Base address for i2c
320 * @return 8-bit TWSI control register
321 */
322static u8 twsi_read_ctl(void __iomem *base)
323{
324 u64 val;
325
326 val = FIELD_PREP(TWSI_SW_EOP_IA_MASK, TWSI_CTL) |
327 FIELD_PREP(TWSI_SW_OP_MASK, TWSI_SW_EOP_IA);
328 val = twsi_read_sw(base, val);
329
330 debug("%s(%p): 0x%x\n", __func__, base, (u8)val);
331 return (u8)val;
332}
333
334/**
335 * Read i2c status register
336 *
337 * @base Base address of i2c registers
338 * @return value of status register
339 */
340static u8 twsi_read_status(void __iomem *base)
341{
342 u64 val;
343
344 val = FIELD_PREP(TWSI_SW_EOP_IA_MASK, TWSI_STAT) |
345 FIELD_PREP(TWSI_SW_OP_MASK, TWSI_SW_EOP_IA);
346
347 return twsi_read_sw(base, val);
348}
349
350/**
351 * Waits for an i2c operation to complete
352 *
353 * @param base Base address of registers
354 * @return 0 for success, 1 if timeout
355 */
356static int twsi_wait(void __iomem *base)
357{
358 unsigned long start = get_timer(0);
359 u8 twsi_ctl;
360
361 debug("%s(%p)\n", __func__, base);
362 do {
363 twsi_ctl = twsi_read_ctl(base);
364 twsi_ctl &= TWSI_CTL_IFLG;
365 } while (!twsi_ctl && get_timer(start) < 50);
366
367 debug(" return: %u\n", !twsi_ctl);
368 return !twsi_ctl;
369}
370
371/**
372 * Unsticks the i2c bus
373 *
374 * @base base address of registers
375 */
376static int twsi_start_unstick(void __iomem *base)
377{
378 twsi_stop(base);
379 twsi_unblock(base);
380
381 return 0;
382}
383
384/**
385 * Sends an i2c start condition
386 *
387 * @base base address of registers
388 * @return 0 for success, otherwise error
389 */
390static int twsi_start(void __iomem *base)
391{
392 int ret;
393 u8 stat;
394
395 debug("%s(%p)\n", __func__, base);
396 twsi_write_ctl(base, TWSI_CTL_STA | TWSI_CTL_ENAB);
397 ret = twsi_wait(base);
398 if (ret) {
399 stat = twsi_read_status(base);
400 debug("%s: ret: 0x%x, status: 0x%x\n", __func__, ret, stat);
401 switch (stat) {
402 case TWSI_STAT_START:
403 case TWSI_STAT_RSTART:
404 return 0;
405 case TWSI_STAT_RXADDR_ACK:
406 default:
407 return twsi_start_unstick(base);
408 }
409 }
410
411 debug("%s: success\n", __func__);
412 return 0;
413}
414
415/**
416 * Sends an i2c stop condition
417 *
418 * @base register base address
419 * @return 0 for success, -1 if error
420 */
421static int twsi_stop(void __iomem *base)
422{
423 u8 stat;
424
425 twsi_write_ctl(base, TWSI_CTL_STP | TWSI_CTL_ENAB);
426
427 stat = twsi_read_status(base);
428 if (stat != TWSI_STAT_IDLE) {
429 debug("%s: Bad status on bus@%p\n", __func__, base);
430 return -1;
431 }
432
433 return 0;
434}
435
436/**
437 * Writes data to the i2c bus
438 *
439 * @base register base address
440 * @slave_addr address of slave to write to
441 * @buffer Pointer to buffer to write
442 * @length Number of bytes in buffer to write
443 * @return 0 for success, otherwise error
444 */
445static int twsi_write_data(void __iomem *base, u8 slave_addr,
446 u8 *buffer, unsigned int length)
447{
448 unsigned int curr = 0;
449 u64 val;
450 int ret;
451
452 debug("%s(%p, 0x%x, %p, 0x%x)\n", __func__, base, slave_addr,
453 buffer, length);
454 ret = twsi_start(base);
455 if (ret) {
456 debug("%s: Could not start BUS transaction\n", __func__);
457 return -1;
458 }
459
460 ret = twsi_wait(base);
461 if (ret) {
462 debug("%s: wait failed\n", __func__);
463 return ret;
464 }
465
466 val = (u32)(slave_addr << 1) | TWSI_OP_WRITE |
467 FIELD_PREP(TWSI_SW_EOP_IA_MASK, TWSI_DATA) |
468 FIELD_PREP(TWSI_SW_OP_MASK, TWSI_SW_EOP_IA);
469 twsi_write_sw(base, val);
470 twsi_write_ctl(base, TWSI_CTL_ENAB);
471
472 debug("%s: Waiting\n", __func__);
473 ret = twsi_wait(base);
474 if (ret) {
475 debug("%s: Timed out writing slave address 0x%x to target\n",
476 __func__, slave_addr);
477 return ret;
478 }
479
480 ret = twsi_read_status(base);
481 debug("%s: status: 0x%x\n", __func__, ret);
482 if (ret != TWSI_STAT_TXADDR_ACK) {
483 debug("%s: status: 0x%x\n", __func__, ret);
484 twsi_stop(base);
485 return twsi_i2c_lost_arb(ret, 0);
486 }
487
488 while (curr < length) {
489 val = buffer[curr++] |
490 FIELD_PREP(TWSI_SW_EOP_IA_MASK, TWSI_DATA) |
491 FIELD_PREP(TWSI_SW_OP_MASK, TWSI_SW_EOP_IA);
492 twsi_write_sw(base, val);
493 twsi_write_ctl(base, TWSI_CTL_ENAB);
494
495 debug("%s: Writing 0x%llx\n", __func__, val);
496
497 ret = twsi_wait(base);
498 if (ret) {
499 debug("%s: Timed out writing data to 0x%x\n",
500 __func__, slave_addr);
501 return ret;
502 }
503 ret = twsi_read_status(base);
504 debug("%s: status: 0x%x\n", __func__, ret);
505 }
506
507 debug("%s: Stopping\n", __func__);
508 return twsi_stop(base);
509}
510
511/**
512 * Manually clear the I2C bus and send a stop
513 *
514 * @base register base address
515 */
516static void twsi_unblock(void __iomem *base)
517{
518 int i;
519
520 for (i = 0; i < 9; i++) {
521 writeq(0, base + TWSI_INT);
522 udelay(5);
523 writeq(TWSI_INT_SCL_OVR, base + TWSI_INT);
524 udelay(5);
525 }
526 writeq(TWSI_INT_SCL_OVR | TWSI_INT_SDA_OVR, base + TWSI_INT);
527 udelay(5);
528 writeq(TWSI_INT_SDA_OVR, base + TWSI_INT);
529 udelay(5);
530 writeq(0, base + TWSI_INT);
531 udelay(5);
532}
533
534/**
535 * Performs a read transaction on the i2c bus
536 *
537 * @base Base address of twsi registers
538 * @slave_addr i2c bus address to read from
539 * @buffer buffer to read into
540 * @length number of bytes to read
541 * @return 0 for success, otherwise error
542 */
543static int twsi_read_data(void __iomem *base, u8 slave_addr,
544 u8 *buffer, unsigned int length)
545{
546 unsigned int curr = 0;
547 u64 val;
548 int ret;
549
550 debug("%s(%p, 0x%x, %p, %u)\n", __func__, base, slave_addr,
551 buffer, length);
552 ret = twsi_start(base);
553 if (ret) {
554 debug("%s: start failed\n", __func__);
555 return ret;
556 }
557
558 ret = twsi_wait(base);
559 if (ret) {
560 debug("%s: wait failed\n", __func__);
561 return ret;
562 }
563
564 val = (u32)(slave_addr << 1) | TWSI_OP_READ |
565 FIELD_PREP(TWSI_SW_EOP_IA_MASK, TWSI_DATA) |
566 FIELD_PREP(TWSI_SW_OP_MASK, TWSI_SW_EOP_IA);
567 twsi_write_sw(base, val);
568 twsi_write_ctl(base, TWSI_CTL_ENAB);
569
570 ret = twsi_wait(base);
571 if (ret) {
572 debug("%s: waiting for sending addr failed\n", __func__);
573 return ret;
574 }
575
576 ret = twsi_read_status(base);
577 debug("%s: status: 0x%x\n", __func__, ret);
578 if (ret != TWSI_STAT_RXADDR_ACK) {
579 debug("%s: status: 0x%x\n", __func__, ret);
580 twsi_stop(base);
581 return twsi_i2c_lost_arb(ret, 0);
582 }
583
584 while (curr < length) {
585 twsi_write_ctl(base, TWSI_CTL_ENAB |
586 ((curr < length - 1) ? TWSI_CTL_AAK : 0));
587
588 ret = twsi_wait(base);
589 if (ret) {
590 debug("%s: waiting for data failed\n", __func__);
591 return ret;
592 }
593
594 val = twsi_read_sw(base, val);
595 buffer[curr++] = (u8)val;
596 }
597
598 twsi_stop(base);
599
600 return 0;
601}
602
603/**
604 * Calculate the divisor values
605 *
606 * @speed Speed to set
607 * @m_div Pointer to M divisor
608 * @n_div Pointer to N divisor
609 * @return 0 for success, otherwise error
610 */
611static void twsi_calc_div(struct udevice *bus, ulong sclk, unsigned int speed,
612 int *m_div, int *n_div)
613{
614 struct octeon_twsi *twsi = dev_get_priv(bus);
615 int thp = twsi->data->thp;
616 int tclk, fsamp;
617 int ndiv, mdiv;
618
619 if (twsi->data->clk_method == CLK_METHOD_OCTEON) {
620 tclk = sclk / (2 * (thp + 1));
621 } else {
622 /* Refclk src in mode register defaults to 100MHz clock */
623 sclk = 100000000; /* 100 Mhz */
624 tclk = sclk / (thp + 2);
625 }
626 debug("%s( io_clock %lu tclk %u)\n", __func__, sclk, tclk);
627
628 /*
629 * Compute the clocks M divider:
630 *
631 * TWSI freq = (core freq) / (10 x (M+1) x 2 * (thp+1) x 2^N)
632 * M = ((core freq) / (10 x (TWSI freq) x 2 * (thp+1) x 2^N)) - 1
633 *
634 * For OcteonTX2 -
635 * TWSI freq = (core freq) / (10 x (M+1) x (thp+2) x 2^N)
636 * M = ((core freq) / (10 x (TWSI freq) x (thp+2) x 2^N)) - 1
637 */
638 for (ndiv = 0; ndiv < 8; ndiv++) {
639 fsamp = tclk / (1 << ndiv);
640 mdiv = fsamp / speed / 10;
641 mdiv -= 1;
642 if (mdiv < 16)
643 break;
644 }
645
646 *m_div = mdiv;
647 *n_div = ndiv;
648}
649
650/**
651 * Init I2C controller
652 *
653 * @base Base address of twsi registers
654 * @slave_addr I2C slave address to configure this controller to
655 * @return 0 for success, otherwise error
656 */
657static int twsi_init(void __iomem *base, int slaveaddr)
658{
659 u64 val;
660
661 debug("%s (%p, 0x%x)\n", __func__, base, slaveaddr);
662
663 val = slaveaddr << 1 |
664 FIELD_PREP(TWSI_SW_EOP_IA_MASK, 0) |
665 FIELD_PREP(TWSI_SW_OP_MASK, TWSI_SW_EOP_IA) |
666 TWSI_SW_V;
667 twsi_write_sw(base, val);
668
669 /* Set slave address */
670 val = slaveaddr |
671 FIELD_PREP(TWSI_SW_EOP_IA_MASK, TWSI_EOP_SLAVE_ADDR) |
672 FIELD_PREP(TWSI_SW_OP_MASK, TWSI_SW_EOP_IA) |
673 TWSI_SW_V;
674 twsi_write_sw(base, val);
675
676 return 0;
677}
678
679/**
680 * Transfers data over the i2c bus
681 *
682 * @bus i2c bus to transfer data over
683 * @msg Array of i2c messages
684 * @nmsgs Number of messages to send/receive
685 * @return 0 for success, otherwise error
686 */
687static int octeon_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
688 int nmsgs)
689{
690 struct octeon_twsi *twsi = dev_get_priv(bus);
691 int ret;
692 int i;
693
694 debug("%s: %d messages\n", __func__, nmsgs);
695 for (i = 0; i < nmsgs; i++, msg++) {
696 debug("%s: chip=0x%x, len=0x%x\n", __func__, msg->addr,
697 msg->len);
698
699 if (msg->flags & I2C_M_RD) {
700 debug("%s: Reading data\n", __func__);
701 ret = twsi_read_data(twsi->base, msg->addr,
702 msg->buf, msg->len);
703 } else {
704 debug("%s: Writing data\n", __func__);
705 ret = twsi_write_data(twsi->base, msg->addr,
706 msg->buf, msg->len);
707 }
708 if (ret) {
709 debug("%s: error sending\n", __func__);
710 return -EREMOTEIO;
711 }
712 }
713
714 return 0;
715}
716
717/**
718 * Set I2C bus speed
719 *
720 * @bus i2c bus to transfer data over
721 * @speed Speed in Hz to set
722 * @return 0 for success, otherwise error
723 */
724static int octeon_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
725{
726 struct octeon_twsi *twsi = dev_get_priv(bus);
727 int m_div, n_div;
728 ulong clk_rate;
729 u64 val;
730
731 debug("%s(%p, %u)\n", __func__, bus, speed);
732
733 clk_rate = clk_get_rate(&twsi->clk);
734 if (IS_ERR_VALUE(clk_rate))
735 return -EINVAL;
736
737 twsi_calc_div(bus, clk_rate, speed, &m_div, &n_div);
738 if (m_div >= 16)
739 return -1;
740
741 val = (u32)(((m_div & 0xf) << 3) | ((n_div & 0x7) << 0)) |
742 FIELD_PREP(TWSI_SW_EOP_IA_MASK, TWSI_CLKCTL) |
743 FIELD_PREP(TWSI_SW_OP_MASK, TWSI_SW_EOP_IA) |
744 TWSI_SW_V;
745 /* Only init non-slave ports */
746 writeq(val, twsi->base + TWSI_SW_TWSI);
747
748 debug("%s: Wrote 0x%llx to sw_twsi\n", __func__, val);
749 return 0;
750}
751
752/**
753 * Driver probe function
754 *
755 * @dev I2C device to probe
756 * @return 0 for success, otherwise error
757 */
758static int octeon_i2c_probe(struct udevice *dev)
759{
760 struct octeon_twsi *twsi = dev_get_priv(dev);
761 u32 i2c_slave_addr;
762 int ret;
763
764 twsi->data = (const struct octeon_i2c_data *)dev_get_driver_data(dev);
765
766 if (twsi->data->probe == PROBE_PCI) {
767 pci_dev_t bdf = dm_pci_get_bdf(dev);
768
769 debug("TWSI PCI device: %x\n", bdf);
770 dev->req_seq = PCI_FUNC(bdf);
771
772 twsi->base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0,
773 PCI_REGION_MEM);
774 } else {
775 twsi->base = dev_remap_addr(dev);
776 }
777 twsi->base += twsi->data->reg_offs;
778
779 i2c_slave_addr = dev_read_u32_default(dev, "i2c-sda-hold-time-ns",
780 CONFIG_SYS_I2C_OCTEON_SLAVE_ADDR);
781
782 ret = clk_get_by_index(dev, 0, &twsi->clk);
783 if (ret < 0)
784 return ret;
785
786 ret = clk_enable(&twsi->clk);
787 if (ret)
788 return ret;
789
790 debug("TWSI bus %d at %p\n", dev->seq, twsi->base);
791
792 /* Start with standard speed, real speed set via DT or cmd */
793 return twsi_init(twsi->base, i2c_slave_addr);
794}
795
796static const struct dm_i2c_ops octeon_i2c_ops = {
797 .xfer = octeon_i2c_xfer,
798 .set_bus_speed = octeon_i2c_set_bus_speed,
799};
800
801static const struct octeon_i2c_data i2c_octeon_data = {
802 .probe = PROBE_DT,
803 .reg_offs = 0x0000,
804 .thp = 3,
805 .clk_method = CLK_METHOD_OCTEON,
806};
807
808static const struct octeon_i2c_data i2c_octeontx_data = {
809 .probe = PROBE_PCI,
810 .reg_offs = 0x8000,
811 .thp = 3,
812 .clk_method = CLK_METHOD_OCTEON,
813};
814
815static const struct octeon_i2c_data i2c_octeontx2_data = {
816 .probe = PROBE_PCI,
817 .reg_offs = 0x8000,
818 .thp = 24,
819 .clk_method = CLK_METHOD_OCTEONTX2,
820};
821
822static const struct udevice_id octeon_i2c_ids[] = {
823 { .compatible = "cavium,octeon-7890-twsi",
824 .data = (ulong)&i2c_octeon_data },
825 { .compatible = "cavium,thunder-8890-twsi",
826 .data = (ulong)&i2c_octeontx_data },
827 { .compatible = "cavium,thunder2-99xx-twsi",
828 .data = (ulong)&i2c_octeontx2_data },
829 { }
830};
831
832U_BOOT_DRIVER(octeon_pci_twsi) = {
833 .name = "i2c_octeon",
834 .id = UCLASS_I2C,
835 .of_match = octeon_i2c_ids,
836 .probe = octeon_i2c_probe,
837 .priv_auto_alloc_size = sizeof(struct octeon_twsi),
838 .ops = &octeon_i2c_ops,
839};
840
841static struct pci_device_id octeon_twsi_supported[] = {
842 { PCI_VDEVICE(CAVIUM, PCI_DEVICE_ID_CAVIUM_TWSI),
843 .driver_data = (ulong)&i2c_octeontx2_data },
844 { },
845};
846
847U_BOOT_PCI_DEVICE(octeon_pci_twsi, octeon_twsi_supported);