2 * This file is part of the coreboot project.
4 * Copyright 2018 Facebook, Inc.
5 * Copyright 2003-2017 Cavium Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * Derived from Cavium's BSD-3 Clause OCTEONTX-SDK-6.2.0.
18 #include <console/console.h>
20 #include <soc/clock.h>
21 #include <device/i2c.h>
22 #include <device/i2c_simple.h>
24 #include <device/mmio.h>
25 #include <soc/addressmap.h>
29 #define TWSI_SW_TWSI 0x1000
30 #define TWSI_TWSI_SW 0x1008
31 #define TWSI_INT 0x1010
32 #define TWSI_SW_TWSI_EXT 0x1018
52 union twsx_sw_twsi_ext
{
64 u64 st_int
:1; /** TWSX_SW_TWSI register update int */
65 u64 ts_int
:1; /** TWSX_TWSI_SW register update int */
66 u64 core_int
:1; /** TWSI core interrupt, ignored for HLC */
67 u64
:5; /** Reserved */
68 u64 sda_ovr
:1; /** SDA testing override */
69 u64 scl_ovr
:1; /** SCL testing override */
70 u64 sda
:1; /** SDA signal */
71 u64 scl
:1; /** SCL signal */
72 u64
:52; /** Reserved */
82 TWSI_EOP_SLAVE_ADDR
= 0,
93 TWSI_SLAVEADD_EXT
= 4,
98 TWSI_CTL_AAK
= (1 << 2),
99 TWSI_CTL_IFLG
= (1 << 3),
100 TWSI_CTL_STP
= (1 << 4),
101 TWSI_CTL_STA
= (1 << 5),
102 TWSI_CTL_ENAB
= (1 << 6),
103 TWSI_CTL_CE
= (1 << 7),
108 TWSI_STAT_BUS_ERROR
= 0x00,
109 /** Start condition transmitted */
110 TWSI_STAT_START
= 0x08,
111 /** Repeat start condition transmitted */
112 TWSI_STAT_RSTART
= 0x10,
113 /** Address + write bit transmitted, ACK received */
114 TWSI_STAT_TXADDR_ACK
= 0x18,
115 /** Address + write bit transmitted, /ACK received */
116 TWSI_STAT_TXADDR_NAK
= 0x20,
117 /** Data byte transmitted in master mode, ACK received */
118 TWSI_STAT_TXDATA_ACK
= 0x28,
119 /** Data byte transmitted in master mode, ACK received */
120 TWSI_STAT_TXDATA_NAK
= 0x30,
121 /** Arbitration lost in address or data byte */
122 TWSI_STAT_TX_ARB_LOST
= 0x38,
123 /** Address + read bit transmitted, ACK received */
124 TWSI_STAT_RXADDR_ACK
= 0x40,
125 /** Address + read bit transmitted, /ACK received */
126 TWSI_STAT_RXADDR_NAK
= 0x48,
127 /** Data byte received in master mode, ACK transmitted */
128 TWSI_STAT_RXDATA_ACK_SENT
= 0x50,
129 /** Data byte received, NACK transmitted */
130 TWSI_STAT_RXDATA_NAK_SENT
= 0x58,
131 /** Slave address received, sent ACK */
132 TWSI_STAT_SLAVE_RXADDR_ACK
= 0x60,
134 * Arbitration lost in address as master, slave address + write bit
135 * received, ACK transmitted
137 TWSI_STAT_TX_ACK_ARB_LOST
= 0x68,
138 /** General call address received, ACK transmitted */
139 TWSI_STAT_RX_GEN_ADDR_ACK
= 0x70,
141 * Arbitration lost in address as master, general call address
142 * received, ACK transmitted
144 TWSI_STAT_RX_GEN_ADDR_ARB_LOST
= 0x78,
145 /** Data byte received after slave address received, ACK transmitted */
146 TWSI_STAT_SLAVE_RXDATA_ACK
= 0x80,
147 /** Data byte received after slave address received, /ACK transmitted */
148 TWSI_STAT_SLAVE_RXDATA_NAK
= 0x88,
150 * Data byte received after general call address received, ACK
153 TWSI_STAT_GEN_RXADDR_ACK
= 0x90,
155 * Data byte received after general call address received, /ACK
158 TWSI_STAT_GEN_RXADDR_NAK
= 0x98,
159 /** STOP or repeated START condition received in slave mode */
160 TWSI_STAT_STOP_MULTI_START
= 0xA0,
161 /** Slave address + read bit received, ACK transmitted */
162 TWSI_STAT_SLAVE_RXADDR2_ACK
= 0xA8,
164 * Arbitration lost in address as master, slave address + read bit
165 * received, ACK transmitted
167 TWSI_STAT_RXDATA_ACK_ARB_LOST
= 0xB0,
168 /** Data byte transmitted in slave mode, ACK received */
169 TWSI_STAT_SLAVE_TXDATA_ACK
= 0xB8,
170 /** Data byte transmitted in slave mode, /ACK received */
171 TWSI_STAT_SLAVE_TXDATA_NAK
= 0xC0,
172 /** Last byte transmitted in slave mode, ACK received */
173 TWSI_STAT_SLAVE_TXDATA_END_ACK
= 0xC8,
174 /** Second address byte + write bit transmitted, ACK received */
175 TWSI_STAT_TXADDR2DATA_ACK
= 0xD0,
176 /** Second address byte + write bit transmitted, /ACK received */
177 TWSI_STAT_TXADDR2DATA_NAK
= 0xD8,
178 /** No relevant status information */
179 TWSI_STAT_IDLE
= 0xF8
183 * Returns true if we lost arbitration
185 * @param code status code
186 * @param final_read true if this is the final read operation
188 * @return true if arbitration has been lost, false if it hasn't been lost.
190 static int twsi_i2c_lost_arb(u8 code
, int final_read
)
193 /* Arbitration lost */
194 case TWSI_STAT_TX_ARB_LOST
:
195 case TWSI_STAT_TX_ACK_ARB_LOST
:
196 case TWSI_STAT_RX_GEN_ADDR_ARB_LOST
:
197 case TWSI_STAT_RXDATA_ACK_ARB_LOST
:
200 /* Being addressed as slave, should back off and listen */
201 case TWSI_STAT_SLAVE_RXADDR_ACK
:
202 case TWSI_STAT_RX_GEN_ADDR_ACK
:
203 case TWSI_STAT_GEN_RXADDR_ACK
:
204 case TWSI_STAT_GEN_RXADDR_NAK
:
207 /* Core busy as slave */
208 case TWSI_STAT_SLAVE_RXDATA_ACK
:
209 case TWSI_STAT_SLAVE_RXDATA_NAK
:
210 case TWSI_STAT_STOP_MULTI_START
:
211 case TWSI_STAT_SLAVE_RXADDR2_ACK
:
212 case TWSI_STAT_SLAVE_TXDATA_ACK
:
213 case TWSI_STAT_SLAVE_TXDATA_NAK
:
214 case TWSI_STAT_SLAVE_TXDATA_END_ACK
:
217 /* Ack allowed on pre-terminal bytes only */
218 case TWSI_STAT_RXDATA_ACK_SENT
:
223 /* NAK allowed on terminal byte only */
224 case TWSI_STAT_RXDATA_NAK_SENT
:
229 case TWSI_STAT_TXDATA_NAK
:
230 case TWSI_STAT_TXADDR_NAK
:
231 case TWSI_STAT_RXADDR_NAK
:
232 case TWSI_STAT_TXADDR2DATA_NAK
:
238 #define RST_BOOT_PNR_MUL(Val) ((Val >> 33) & 0x1F)
241 * Writes to the MIO_TWS(0..5)_SW_TWSI register
243 * @param baseaddr Base address of i2c registers
244 * @param sw_twsi value to write
246 * @return 0 for success, otherwise error
248 static u64
twsi_write_sw(void *baseaddr
, union twsx_sw_twsi sw_twsi
)
250 unsigned long timeout
= 500000;
255 printk(BIOS_SPEW
, "%s(%p, 0x%llx)\n", __func__
, baseaddr
, sw_twsi
.u
);
256 write64(baseaddr
+ TWSI_SW_TWSI
, sw_twsi
.u
);
258 sw_twsi
.u
= read64(baseaddr
+ TWSI_SW_TWSI
);
260 } while (sw_twsi
.s
.v
!= 0 && timeout
> 0);
263 printk(BIOS_ERR
, "%s: timed out\n", __func__
);
268 * Reads the MIO_TWS(0..5)_SW_TWSI register
270 * @param baseaddr Base address of i2c registers
271 * @param sw_twsi value for eia and op, etc. to read
273 * @return value of the register
275 static u64
twsi_read_sw(void *baseaddr
, union twsx_sw_twsi sw_twsi
)
277 unsigned long timeout
= 500000;
281 printk(BIOS_SPEW
, "%s(%p, 0x%llx)\n", __func__
, baseaddr
, sw_twsi
.u
);
282 write64(baseaddr
+ TWSI_SW_TWSI
, sw_twsi
.u
);
285 sw_twsi
.u
= read64(baseaddr
+ TWSI_SW_TWSI
);
287 } while (sw_twsi
.s
.v
!= 0 && timeout
> 0);
290 printk(BIOS_ERR
, "%s: Error writing 0x%llx\n", __func__
,
293 printk(BIOS_SPEW
, "%s: Returning 0x%llx\n", __func__
, sw_twsi
.u
);
298 * Write control register
300 * @param baseaddr Base address for i2c registers
301 * @param data data to write
303 static void twsi_write_ctl(void *baseaddr
, const u8 data
)
305 union twsx_sw_twsi twsi_sw
;
307 printk(BIOS_SPEW
, "%s(%p, 0x%x)\n", __func__
, baseaddr
, data
);
310 twsi_sw
.s
.op
= TWSI_SW_EOP_IA
;
311 twsi_sw
.s
.eop_ia
= TWSI_CTL
;
312 twsi_sw
.s
.data
= data
;
314 twsi_write_sw(baseaddr
, twsi_sw
);
318 * Reads the TWSI Control Register
320 * @param[in] baseaddr Base address for i2c
322 * @return 8-bit TWSI control register
324 static u32
twsi_read_ctl(void *baseaddr
)
326 union twsx_sw_twsi sw_twsi
;
329 sw_twsi
.s
.op
= TWSI_SW_EOP_IA
;
330 sw_twsi
.s
.eop_ia
= TWSI_CTL
;
332 sw_twsi
.u
= twsi_read_sw(baseaddr
, sw_twsi
);
333 printk(BIOS_SPEW
, "%s(%p): 0x%x\n", __func__
, baseaddr
, sw_twsi
.s
.data
);
334 return sw_twsi
.s
.data
;
338 * Read i2c status register
340 * @param baseaddr Base address of i2c registers
342 * @return value of status register
344 static u8
twsi_read_status(void *baseaddr
)
346 union twsx_sw_twsi twsi_sw
;
349 twsi_sw
.s
.op
= TWSI_SW_EOP_IA
;
350 twsi_sw
.s
.eop_ia
= TWSI_STAT
;
352 return twsi_read_sw(baseaddr
, twsi_sw
);
356 * Waits for an i2c operation to complete
358 * @param baseaddr Base address of registers
360 * @return 0 for success, 1 if timeout
362 static int twsi_wait(void *baseaddr
)
364 unsigned long timeout
= 500000;
367 printk(BIOS_SPEW
, "%s(%p)\n", __func__
, baseaddr
);
369 twsi_ctl
= twsi_read_ctl(baseaddr
);
370 twsi_ctl
&= TWSI_CTL_IFLG
;
372 } while (!twsi_ctl
&& timeout
> 0);
374 printk(BIOS_SPEW
, " return: %u\n", !twsi_ctl
);
379 * Sends an i2c stop condition
381 * @param baseaddr register base address
383 * @return 0 for success, -1 if error
385 static int twsi_stop(void *baseaddr
)
388 twsi_write_ctl(baseaddr
, TWSI_CTL_STP
| TWSI_CTL_ENAB
);
390 stat
= twsi_read_status(baseaddr
);
391 if (stat
!= TWSI_STAT_IDLE
) {
392 printk(BIOS_ERR
, "%s: Bad status on bus@%p\n", __func__
,
400 * Manually clear the I2C bus and send a stop
402 static void twsi_unblock(void *baseaddr
)
405 union twsx_int int_reg
;
408 for (i
= 0; i
< 9; i
++) {
409 int_reg
.s
.scl_ovr
= 0;
410 write64(baseaddr
+ TWSI_INT
, int_reg
.u
);
412 int_reg
.s
.scl_ovr
= 1;
413 write64(baseaddr
+ TWSI_INT
, int_reg
.u
);
416 int_reg
.s
.sda_ovr
= 1;
417 write64(baseaddr
+ TWSI_INT
, int_reg
.u
);
419 int_reg
.s
.scl_ovr
= 0;
420 write64(baseaddr
+ TWSI_INT
, int_reg
.u
);
423 write64(baseaddr
+ TWSI_INT
, int_reg
.u
);
428 * Unsticks the i2c bus
430 * @param baseaddr base address of registers
432 static int twsi_start_unstick(void *baseaddr
)
436 twsi_unblock(baseaddr
);
442 * Sends an i2c start condition
444 * @param baseaddr base address of registers
446 * @return 0 for success, otherwise error
448 static int twsi_start(void *baseaddr
)
453 printk(BIOS_SPEW
, "%s(%p)\n", __func__
, baseaddr
);
454 twsi_write_ctl(baseaddr
, TWSI_CTL_STA
| TWSI_CTL_ENAB
);
455 result
= twsi_wait(baseaddr
);
457 stat
= twsi_read_status(baseaddr
);
458 printk(BIOS_SPEW
, "%s: result: 0x%x, status: 0x%x\n", __func__
,
461 case TWSI_STAT_START
:
462 case TWSI_STAT_RSTART
:
464 case TWSI_STAT_RXADDR_ACK
:
466 return twsi_start_unstick(baseaddr
);
469 printk(BIOS_SPEW
, "%s: success\n", __func__
);
474 * Writes data to the i2c bus
476 * @param baseraddr register base address
477 * @param slave_addr address of slave to write to
478 * @param buffer Pointer to buffer to write
479 * @param length Number of bytes in buffer to write
481 * @return 0 for success, otherwise error
483 static int twsi_write_data(void *baseaddr
, const u8 slave_addr
,
484 const u8
*buffer
, const unsigned int length
)
486 union twsx_sw_twsi twsi_sw
;
487 unsigned int curr
= 0;
490 printk(BIOS_SPEW
, "%s(%p, 0x%x, %p, 0x%x)\n", __func__
, baseaddr
,
491 slave_addr
, buffer
, length
);
492 result
= twsi_start(baseaddr
);
494 printk(BIOS_ERR
, "%s: Could not start BUS transaction\n",
499 result
= twsi_wait(baseaddr
);
501 printk(BIOS_ERR
, "%s: wait failed\n", __func__
);
506 twsi_sw
.s
.op
= TWSI_SW_EOP_IA
;
507 twsi_sw
.s
.eop_ia
= TWSI_DATA
;
508 twsi_sw
.s
.data
= (u32
) (slave_addr
<< 1) | TWSI_OP_WRITE
;
510 twsi_write_sw(baseaddr
, twsi_sw
);
511 twsi_write_ctl(baseaddr
, TWSI_CTL_ENAB
);
513 printk(BIOS_SPEW
, "%s: Waiting\n", __func__
);
514 result
= twsi_wait(baseaddr
);
516 printk(BIOS_ERR
, "%s: Timed out writing slave address 0x%x\n",
517 __func__
, slave_addr
);
520 result
= twsi_read_status(baseaddr
);
521 if ((result
= twsi_read_status(baseaddr
)) != TWSI_STAT_TXADDR_ACK
) {
523 return twsi_i2c_lost_arb(result
, 0);
526 while (curr
< length
) {
528 twsi_sw
.s
.op
= TWSI_SW_EOP_IA
;
529 twsi_sw
.s
.eop_ia
= TWSI_DATA
;
530 twsi_sw
.s
.data
= buffer
[curr
++];
532 twsi_write_sw(baseaddr
, twsi_sw
);
533 twsi_write_ctl(baseaddr
, TWSI_CTL_ENAB
);
535 result
= twsi_wait(baseaddr
);
537 printk(BIOS_ERR
, "%s: Timed out writing data to 0x%x\n",
538 __func__
, slave_addr
);
543 printk(BIOS_SPEW
, "%s: Stopping\n", __func__
);
544 return twsi_stop(baseaddr
);
548 * Performs a read transaction on the i2c bus
550 * @param baseaddr Base address of twsi registers
551 * @param slave_addr i2c bus address to read from
552 * @param buffer buffer to read into
553 * @param length number of bytes to read
555 * @return 0 for success, otherwise error
557 static int twsi_read_data(void *baseaddr
, const u8 slave_addr
,
558 u8
*buffer
, const unsigned int length
)
560 union twsx_sw_twsi twsi_sw
;
561 unsigned int curr
= 0;
564 printk(BIOS_SPEW
, "%s(%p, 0x%x, %p, %u)\n", __func__
, baseaddr
,
565 slave_addr
, buffer
, length
);
566 result
= twsi_start(baseaddr
);
568 printk(BIOS_ERR
, "%s: start failed\n", __func__
);
572 result
= twsi_wait(baseaddr
);
574 printk(BIOS_ERR
, "%s: wait failed\n", __func__
);
580 twsi_sw
.s
.op
= TWSI_SW_EOP_IA
;
581 twsi_sw
.s
.eop_ia
= TWSI_DATA
;
583 twsi_sw
.s
.data
= (u32
) (slave_addr
<< 1) | TWSI_OP_READ
;
585 twsi_write_sw(baseaddr
, twsi_sw
);
586 twsi_write_ctl(baseaddr
, TWSI_CTL_ENAB
);
588 result
= twsi_wait(baseaddr
);
590 printk(BIOS_ERR
, "%s: waiting for sending addr failed\n", __func__
);
594 result
= twsi_read_status(baseaddr
);
595 if (result
!= TWSI_STAT_RXADDR_ACK
) {
597 return twsi_i2c_lost_arb(result
, 0);
600 while (curr
< length
) {
601 twsi_write_ctl(baseaddr
, TWSI_CTL_ENAB
|
602 ((curr
< length
- 1) ? TWSI_CTL_AAK
: 0));
604 result
= twsi_wait(baseaddr
);
606 printk(BIOS_ERR
, "%s: waiting for data failed\n",
611 twsi_sw
.u
= twsi_read_sw(baseaddr
, twsi_sw
);
612 buffer
[curr
++] = twsi_sw
.s
.data
;
620 static int twsi_set_speed(void *baseaddr
, const unsigned int speed
)
625 union twsx_sw_twsi sw_twsi
;
627 io_clock_hz
= thunderx_get_io_clock();
629 /* Set the TWSI clock to a conservative TWSI_BUS_FREQ. Compute the
630 * clocks M divider based on the SCLK.
631 * TWSI freq = (core freq) / (20 x (M+1) x (thp+1) x 2^N)
632 * M = ((core freq) / (20 x (TWSI freq) x (thp+1) x 2^N)) - 1
634 for (n_div
= 0; n_div
< 8; n_div
++) {
635 m_div
= io_clock_hz
/ (20 * speed
* (TWSI_THP
+ 1));
646 sw_twsi
.s
.op
= 0x6; /* See EOP field */
647 sw_twsi
.s
.r
= 0; /* Select CLKCTL when R = 0 */
648 sw_twsi
.s
.eop_ia
= 3; /* R=0 selects CLKCTL, R=1 selects STAT */
649 sw_twsi
.s
.data
= ((m_div
& 0xf) << 3) | ((n_div
& 0x7) << 0);
651 twsi_write_sw(baseaddr
, sw_twsi
);
655 int twsi_init(unsigned int bus
, enum i2c_speed hz
)
657 void *baseaddr
= (void *)MIO_TWSx_PF_BAR0(bus
);
661 if (twsi_set_speed(baseaddr
, hz
) < 0)
664 /* Enable TWSI, HLC disable, STOP, NAK */
665 twsi_write_ctl(baseaddr
, TWSI_CTL_ENAB
);
670 int platform_i2c_transfer(unsigned int bus
, struct i2c_msg
*segments
,
674 void *baseaddr
= (void *)MIO_TWSx_PF_BAR0(bus
);
678 printk(BIOS_SPEW
, "%s: %d messages\n", __func__
, seg_count
);
679 for (; seg_count
> 0; seg_count
--, segments
++) {
680 if (segments
->flags
& I2C_M_RD
) {
681 result
= twsi_read_data(baseaddr
, segments
->slave
,
682 segments
->buf
, segments
->len
);
684 result
= twsi_write_data(baseaddr
, segments
->slave
,
685 segments
->buf
, segments
->len
);
688 printk(BIOS_ERR
, "%s: error transmitting data\n",