1 /* SPDX-License-Identifier: BSD-3-Clause */
3 #include <device/mmio.h>
4 #include <console/console.h>
11 #define TIMEOUT_CNT 100
13 #define QUP_ADDR(id, reg) (blsp_qup_base(id) + (reg))
17 #define QUPDBG BIOS_ERR, "\t-> "
20 #define qup_write32(a, v) do { \
22 printk(QUPDBG "%s(%d): write32(%p, 0x%x)\n", \
23 __func__, __LINE__, a, v); \
26 #define qup_write32 write32
29 struct i2c_clk_div_fld
{
35 static struct i2c_clk_div_fld i2c_clk_div_map
[] = {
41 static void i2c_set_mstr_clk_ctl(unsigned int id
, unsigned int hz
)
44 struct i2c_clk_div_fld
*itr
= i2c_clk_div_map
;
49 for (i
= 0; i
< ARRAY_SIZE(i2c_clk_div_map
); ++i
, ++itr
) {
50 if (hz
== itr
->clk_freq_out
) {
59 /* format values in clk-ctl cache */
60 mstr_clk_ctl
= ((ht_div
& 0xff) << 16) | (fs_div
& 0xff);
61 qup_write32(QUP_ADDR(id
, QUP_I2C_MASTER_CLK_CTL
), mstr_clk_ctl
);
64 static qup_return_t
qup_i2c_master_status(blsp_qup_id_t id
)
66 uint32_t reg_val
= read32(QUP_ADDR(id
, QUP_I2C_MASTER_STATUS
));
68 if (read32(QUP_ADDR(id
, QUP_ERROR_FLAGS
)))
69 return QUP_ERR_XFER_FAIL
;
72 printk(QUPDBG
"%s: 0x%x\n", __func__
, reg_val
);
75 if (reg_val
& QUP_I2C_INVALID_READ_ADDR
)
76 return QUP_ERR_I2C_INVALID_SLAVE_ADDR
;
77 if (reg_val
& QUP_I2C_FAILED_MASK
)
78 return QUP_ERR_I2C_FAILED
;
79 if (reg_val
& QUP_I2C_ARB_LOST
)
80 return QUP_ERR_I2C_ARB_LOST
;
81 if (reg_val
& QUP_I2C_BUS_ERROR
)
82 return QUP_ERR_I2C_BUS_ERROR
;
83 if (reg_val
& QUP_I2C_INVALID_WRITE
)
84 return QUP_ERR_I2C_INVALID_WRITE
;
85 if (reg_val
& QUP_I2C_PACKET_NACK
)
86 return QUP_ERR_I2C_NACK
;
87 if (reg_val
& QUP_I2C_INVALID_TAG
)
88 return QUP_ERR_I2C_INVALID_TAG
;
93 static int check_bit_state(uint32_t *reg
, int wait_for
)
95 unsigned int count
= TIMEOUT_CNT
;
97 while ((read32(reg
) & (QUP_STATE_VALID_MASK
| QUP_STATE_MASK
)) !=
98 (QUP_STATE_VALID
| wait_for
)) {
100 return QUP_ERR_TIMEOUT
;
108 * Check whether GSBIn_QUP State is valid
110 static qup_return_t
qup_wait_for_state(blsp_qup_id_t id
, unsigned int wait_for
)
112 return check_bit_state(QUP_ADDR(id
, QUP_STATE
), wait_for
);
115 qup_return_t
qup_reset_i2c_master_status(blsp_qup_id_t id
)
118 * The I2C_STATUS is a status register.
119 * Writing any value clears the status bits.
121 qup_write32(QUP_ADDR(id
, QUP_I2C_MASTER_STATUS
), 0);
125 static qup_return_t
qup_reset_master_status(blsp_qup_id_t id
)
127 qup_write32(QUP_ADDR(id
, QUP_ERROR_FLAGS
), 0x3C);
128 qup_write32(QUP_ADDR(id
, QUP_ERROR_FLAGS_EN
), 0x3C);
129 qup_reset_i2c_master_status(id
);
133 static qup_return_t
qup_fifo_wait_for(blsp_qup_id_t id
, uint32_t status
,
134 struct stopwatch
*timeout
)
136 qup_return_t ret
= QUP_ERR_UNDEFINED
;
138 while (!(read32(QUP_ADDR(id
, QUP_OPERATIONAL
)) & status
)) {
139 ret
= qup_i2c_master_status(id
);
142 if (stopwatch_expired(timeout
))
143 return QUP_ERR_TIMEOUT
;
149 static qup_return_t
qup_fifo_wait_while(blsp_qup_id_t id
, uint32_t status
,
150 struct stopwatch
*timeout
)
152 qup_return_t ret
= QUP_ERR_UNDEFINED
;
154 while (read32(QUP_ADDR(id
, QUP_OPERATIONAL
)) & status
) {
155 ret
= qup_i2c_master_status(id
);
158 if (stopwatch_expired(timeout
))
159 return QUP_ERR_TIMEOUT
;
165 static inline uint32_t qup_i2c_create_output_tag(int stop
, u8 data
)
170 tag
= QUP_I2C_STOP_SEQ
| QUP_I2C_DATA(data
);
172 tag
= QUP_I2C_DATA_SEQ
| QUP_I2C_DATA(data
);
177 static inline qup_return_t
qup_i2c_write_fifo_flush(blsp_qup_id_t id
,
178 struct stopwatch
*timeout
)
180 qup_return_t ret
= QUP_ERR_UNDEFINED
;
182 qup_write32(QUP_ADDR(id
, QUP_OPERATIONAL
), OUTPUT_SERVICE_FLAG
);
184 mdelay(4); /* TPM seems to need this */
186 ret
= qup_fifo_wait_while(id
, OUTPUT_FIFO_NOT_EMPTY
, timeout
);
190 ret
= qup_i2c_master_status(id
);
193 printk(BIOS_DEBUG
, "%s: error\n", __func__
);
198 static qup_return_t
qup_i2c_write_fifo(blsp_qup_id_t id
, qup_data_t
*p_tx_obj
,
201 qup_return_t ret
= QUP_ERR_UNDEFINED
;
202 uint8_t addr
= p_tx_obj
->p
.iic
.addr
;
203 uint8_t *data_ptr
= p_tx_obj
->p
.iic
.data
;
204 unsigned int data_len
= p_tx_obj
->p
.iic
.data_len
;
205 unsigned int idx
= 0;
206 uint32_t tag
, *fifo
= QUP_ADDR(id
, QUP_OUTPUT_FIFO
);
207 struct stopwatch timeout
;
209 qup_reset_master_status(id
);
211 qup_write32(QUP_ADDR(id
, QUP_MX_OUTPUT_COUNT
), data_len
+ 1);
213 qup_set_state(id
, QUP_STATE_RUN
);
216 * Since UNPACK enable is set in io mode register, populate 2 tags
217 * for each fifo register.
219 * Create the first tag as follows, with the start tag and first byte
220 * of the data to be written
221 * +--------+--------+--------+--------+
222 * | STOP / | data | START | ADDR |
223 * |DATA tag| byte | tag | << 1 |
224 * +--------+--------+--------+--------+
225 * rest will be created in the following while loop.
227 tag
= qup_i2c_create_output_tag(data_len
== 1 && stop_seq
,
229 tag
= ((tag
<< 16) & 0xffff0000) |
230 (QUP_I2C_START_SEQ
| QUP_I2C_ADDR(addr
));
234 qup_write32(fifo
, tag
);
236 stopwatch_init_usecs_expire(&timeout
, CONFIG_I2C_TRANSFER_TIMEOUT_US
);
238 tag
= qup_i2c_create_output_tag(data_len
== 1 && stop_seq
,
244 tag
|= qup_i2c_create_output_tag(
245 data_len
== 1 && stop_seq
,
246 data_ptr
[idx
]) << 16;
251 qup_write32(fifo
, tag
);
253 ret
= qup_i2c_write_fifo_flush(id
, &timeout
);
256 printk(QUPDBG
"%s: error\n", __func__
);
261 ret
= qup_i2c_write_fifo_flush(id
, &timeout
);
263 qup_set_state(id
, QUP_STATE_RESET
);
268 static qup_return_t
qup_i2c_write(blsp_qup_id_t id
, uint8_t mode
,
269 qup_data_t
*p_tx_obj
, uint8_t stop_seq
)
271 qup_return_t ret
= QUP_ERR_UNDEFINED
;
276 ret
= qup_i2c_write_fifo(id
, p_tx_obj
, stop_seq
);
279 ret
= QUP_ERR_UNSUPPORTED
;
283 qup_set_state(id
, QUP_STATE_RESET
);
284 printk(QUPDBG
"%s() failed (%d)\n", __func__
, ret
);
290 static int qup_i2c_parse_tag(uint32_t data
, uint8_t *data_ptr
, uint32_t len
)
293 int max
= (len
> 2) ? 2 : len
;
295 for (i
= 0; i
< max
; i
++) {
296 switch (QUP_I2C_MI_TAG(data
)) {
297 case QUP_I2C_MIDATA_SEQ
:
298 data_ptr
[idx
] = QUP_I2C_DATA(data
);
301 case QUP_I2C_MISTOP_SEQ
:
302 data_ptr
[idx
] = QUP_I2C_DATA(data
);
306 printk(QUPDBG
"%s: Unexpected tag (0x%x)\n", __func__
,
307 QUP_I2C_MI_TAG(data
));
317 static qup_return_t
qup_i2c_read_fifo(blsp_qup_id_t id
, qup_data_t
*p_tx_obj
)
319 qup_return_t ret
= QUP_ERR_UNDEFINED
;
320 uint8_t addr
= p_tx_obj
->p
.iic
.addr
;
321 uint8_t *data_ptr
= p_tx_obj
->p
.iic
.data
;
322 unsigned int data_len
= p_tx_obj
->p
.iic
.data_len
;
323 unsigned int idx
= 0;
324 uint32_t *fifo
= QUP_ADDR(id
, QUP_OUTPUT_FIFO
);
325 struct stopwatch timeout
;
327 qup_reset_master_status(id
);
329 qup_write32(QUP_ADDR(id
, QUP_IO_MODES
),
330 QUP_UNPACK_EN
| QUP_PACK_EN
|
331 ((QUP_MODE_BLOCK
& QUP_MODE_MASK
) <<
332 QUP_OUTPUT_MODE_SHFT
) |
333 ((QUP_MODE_BLOCK
& QUP_MODE_MASK
) <<
334 QUP_INPUT_MODE_SHFT
));
336 qup_write32(QUP_ADDR(id
, QUP_MX_INPUT_COUNT
), data_len
);
338 qup_set_state(id
, QUP_STATE_RUN
);
340 qup_write32(fifo
, (QUP_I2C_START_SEQ
|
341 (QUP_I2C_ADDR(addr
) | QUP_I2C_SLAVE_READ
)) |
342 ((QUP_I2C_RECV_SEQ
| data_len
) << 16));
344 stopwatch_init_usecs_expire(&timeout
, CONFIG_I2C_TRANSFER_TIMEOUT_US
);
345 ret
= qup_i2c_write_fifo_flush(id
, &timeout
);
347 printk(QUPDBG
"%s: OUTPUT_FIFO_NOT_EMPTY\n", __func__
);
351 ret
= qup_fifo_wait_for(id
, INPUT_SERVICE_FLAG
, &timeout
);
353 printk(QUPDBG
"%s: INPUT_SERVICE_FLAG\n", __func__
);
357 fifo
= QUP_ADDR(id
, QUP_INPUT_FIFO
);
366 count
= qup_i2c_parse_tag(data
, data_ptr
+ idx
, data_len
);
369 printk(QUPDBG
"%s: Cannot parse tag 0x%x\n",
371 qup_set_state(id
, QUP_STATE_PAUSE
);
373 return QUP_ERR_I2C_INVALID_TAG
;
379 qup_write32(QUP_ADDR(id
, QUP_OPERATIONAL
), INPUT_SERVICE_FLAG
);
382 p_tx_obj
->p
.iic
.data_len
= idx
;
384 qup_write32(QUP_ADDR(id
, QUP_MX_READ_COUNT
), 0);
386 qup_set_state(id
, QUP_STATE_RESET
);
391 static qup_return_t
qup_i2c_read(blsp_qup_id_t id
, uint8_t mode
,
392 qup_data_t
*p_tx_obj
)
394 qup_return_t ret
= QUP_ERR_UNDEFINED
;
396 qup_set_state(id
, QUP_STATE_RESET
);
401 ret
= qup_i2c_read_fifo(id
, p_tx_obj
);
404 ret
= QUP_ERR_UNSUPPORTED
;
408 qup_set_state(id
, QUP_STATE_RESET
);
409 printk(QUPDBG
"%s() failed (%d)\n", __func__
, ret
);
415 qup_return_t
qup_init(blsp_qup_id_t id
, const qup_config_t
*config_ptr
)
417 qup_return_t ret
= QUP_ERR_UNDEFINED
;
420 /* Reset the QUP core.*/
421 qup_write32(QUP_ADDR(id
, QUP_SW_RESET
), 0x1);
423 /* Wait till the reset takes effect */
424 ret
= qup_wait_for_state(id
, QUP_STATE_RESET
);
428 /* Reset the config */
429 qup_write32(QUP_ADDR(id
, QUP_CONFIG
), 0);
431 /* Program the config register */
435 switch (config_ptr
->protocol
) {
436 case QUP_MINICORE_I2C_MASTER
:
437 reg_val
|= ((config_ptr
->protocol
&
438 QUP_MINI_CORE_PROTO_MASK
) <<
439 QUP_MINI_CORE_PROTO_SHFT
);
442 ret
= QUP_ERR_UNSUPPORTED
;
445 reg_val
|= QUP_APP_CLK_ON_EN
| QUP_CORE_CLK_ON_EN
;
446 qup_write32(QUP_ADDR(id
, QUP_CONFIG
), reg_val
);
448 /* Choose version 1 tag */
449 qup_write32(QUP_ADDR(id
, QUP_I2C_MASTER_CONFIG
), 0);
451 /* Reset i2c clk cntl register */
452 qup_write32(QUP_ADDR(id
, QUP_I2C_MASTER_CLK_CTL
), 0);
454 /* Set QUP IO Mode */
455 switch (config_ptr
->mode
) {
458 reg_val
= QUP_UNPACK_EN
| QUP_PACK_EN
|
459 ((config_ptr
->mode
& QUP_MODE_MASK
) <<
460 QUP_OUTPUT_MODE_SHFT
) |
461 ((config_ptr
->mode
& QUP_MODE_MASK
) <<
462 QUP_INPUT_MODE_SHFT
);
465 ret
= QUP_ERR_UNSUPPORTED
;
468 qup_write32(QUP_ADDR(id
, QUP_IO_MODES
), reg_val
);
471 i2c_set_mstr_clk_ctl(id
, 400000);
473 qup_set_state(id
, QUP_STATE_RESET
);
476 printk(QUPDBG
"failed to init qup (%d)\n", ret
);
481 qup_return_t
qup_set_state(blsp_qup_id_t id
, uint32_t state
)
483 qup_return_t ret
= QUP_ERR_UNDEFINED
;
484 unsigned int curr_state
= read32(QUP_ADDR(id
, QUP_STATE
));
486 if ((state
<= QUP_STATE_PAUSE
)
487 && (curr_state
& QUP_STATE_VALID_MASK
)) {
489 * For PAUSE_STATE to RESET_STATE transition,
490 * two writes of 10[binary]) are required for the
491 * transition to complete.
493 if (curr_state
== QUP_STATE_PAUSE
&& state
== QUP_STATE_RESET
) {
494 qup_write32(QUP_ADDR(id
, QUP_STATE
), 0x2);
495 qup_write32(QUP_ADDR(id
, QUP_STATE
), 0x2);
497 qup_write32(QUP_ADDR(id
, QUP_STATE
), state
);
499 ret
= qup_wait_for_state(id
, state
);
505 static qup_return_t
qup_i2c_send_data(blsp_qup_id_t id
, qup_data_t
*p_tx_obj
,
508 qup_return_t ret
= QUP_ERR_UNDEFINED
;
509 uint8_t mode
= (read32(QUP_ADDR(id
, QUP_IO_MODES
)) >>
510 QUP_OUTPUT_MODE_SHFT
) & QUP_MODE_MASK
;
512 ret
= qup_i2c_write(id
, mode
, p_tx_obj
, stop_seq
);
516 printk(BIOS_DEBUG
, "i2c tx bus %d device %2.2x:",
517 id
, p_tx_obj
->p
.iic
.addr
);
518 for (i
= 0; i
< p_tx_obj
->p
.iic
.data_len
; i
++)
519 printk(BIOS_DEBUG
, " %2.2x", p_tx_obj
->p
.iic
.data
[i
]);
520 printk(BIOS_DEBUG
, "\n");
526 qup_return_t
qup_send_data(blsp_qup_id_t id
, qup_data_t
*p_tx_obj
,
529 qup_return_t ret
= QUP_ERR_UNDEFINED
;
531 if (p_tx_obj
->protocol
== ((read32(QUP_ADDR(id
, QUP_CONFIG
)) >>
532 QUP_MINI_CORE_PROTO_SHFT
) & QUP_MINI_CORE_PROTO_MASK
)) {
533 switch (p_tx_obj
->protocol
) {
534 case QUP_MINICORE_I2C_MASTER
:
535 ret
= qup_i2c_send_data(id
, p_tx_obj
, stop_seq
);
538 ret
= QUP_ERR_UNSUPPORTED
;
545 static qup_return_t
qup_i2c_recv_data(blsp_qup_id_t id
, qup_data_t
*p_rx_obj
)
547 qup_return_t ret
= QUP_ERR_UNDEFINED
;
548 uint8_t mode
= (read32(QUP_ADDR(id
, QUP_IO_MODES
)) >>
549 QUP_INPUT_MODE_SHFT
) & QUP_MODE_MASK
;
551 ret
= qup_i2c_read(id
, mode
, p_rx_obj
);
555 printk(BIOS_DEBUG
, "i2c rxed on bus %d device %2.2x:",
556 id
, p_rx_obj
->p
.iic
.addr
);
557 for (i
= 0; i
< p_rx_obj
->p
.iic
.data_len
; i
++)
558 printk(BIOS_DEBUG
, " %2.2x", p_rx_obj
->p
.iic
.data
[i
]);
559 printk(BIOS_DEBUG
, "\n");
565 qup_return_t
qup_recv_data(blsp_qup_id_t id
, qup_data_t
*p_rx_obj
)
567 qup_return_t ret
= QUP_ERR_UNDEFINED
;
569 if (p_rx_obj
->protocol
== ((read32(QUP_ADDR(id
, QUP_CONFIG
)) >>
570 QUP_MINI_CORE_PROTO_SHFT
) & QUP_MINI_CORE_PROTO_MASK
)) {
571 switch (p_rx_obj
->protocol
) {
572 case QUP_MINICORE_I2C_MASTER
:
573 ret
= qup_i2c_recv_data(id
, p_rx_obj
);
576 ret
= QUP_ERR_UNSUPPORTED
;