1 /* SPDX-License-Identifier: BSD-3-Clause */
3 #include <device/mmio.h>
4 #include <console/console.h>
10 #define TIMEOUT_CNT 100
12 #define QUP_ADDR(id, reg) (blsp_qup_base(id) + (reg))
16 #define QUPDBG BIOS_ERR, "\t-> "
19 #define qup_write32(a, v) do { \
21 printk(QUPDBG "%s(%d): write32(%p, 0x%x)\n", \
22 __func__, __LINE__, a, v); \
25 #define qup_write32 write32
28 static qup_return_t
qup_i2c_master_status(blsp_qup_id_t id
)
30 uint32_t reg_val
= read32(QUP_ADDR(id
, QUP_I2C_MASTER_STATUS
));
32 if (read32(QUP_ADDR(id
, QUP_ERROR_FLAGS
)))
33 return QUP_ERR_XFER_FAIL
;
36 printk(QUPDBG
"%s: 0x%x\n", __func__
, reg_val
);
39 if (reg_val
& QUP_I2C_INVALID_READ_ADDR
)
40 return QUP_ERR_I2C_INVALID_SLAVE_ADDR
;
41 if (reg_val
& QUP_I2C_FAILED_MASK
)
42 return QUP_ERR_I2C_FAILED
;
43 if (reg_val
& QUP_I2C_ARB_LOST
)
44 return QUP_ERR_I2C_ARB_LOST
;
45 if (reg_val
& QUP_I2C_BUS_ERROR
)
46 return QUP_ERR_I2C_BUS_ERROR
;
47 if (reg_val
& QUP_I2C_INVALID_WRITE
)
48 return QUP_ERR_I2C_INVALID_WRITE
;
49 if (reg_val
& QUP_I2C_PACKET_NACK
)
50 return QUP_ERR_I2C_NACK
;
51 if (reg_val
& QUP_I2C_INVALID_TAG
)
52 return QUP_ERR_I2C_INVALID_TAG
;
57 static int check_bit_state(uint32_t *reg
, int wait_for
)
59 unsigned int count
= TIMEOUT_CNT
;
61 while ((read32(reg
) & (QUP_STATE_VALID_MASK
| QUP_STATE_MASK
)) !=
62 (QUP_STATE_VALID
| wait_for
)) {
64 return QUP_ERR_TIMEOUT
;
72 * Check whether GSBIn_QUP State is valid
74 static qup_return_t
qup_wait_for_state(blsp_qup_id_t id
, unsigned int wait_for
)
76 return check_bit_state(QUP_ADDR(id
, QUP_STATE
), wait_for
);
79 qup_return_t
qup_reset_i2c_master_status(blsp_qup_id_t id
)
82 * The I2C_STATUS is a status register.
83 * Writing any value clears the status bits.
85 qup_write32(QUP_ADDR(id
, QUP_I2C_MASTER_STATUS
), 0);
89 static qup_return_t
qup_reset_master_status(blsp_qup_id_t id
)
91 qup_write32(QUP_ADDR(id
, QUP_ERROR_FLAGS
), 0x3C);
92 qup_write32(QUP_ADDR(id
, QUP_ERROR_FLAGS_EN
), 0x3C);
93 qup_reset_i2c_master_status(id
);
97 static qup_return_t
qup_fifo_wait_for(blsp_qup_id_t id
, uint32_t status
,
98 struct stopwatch
*timeout
)
100 qup_return_t ret
= QUP_ERR_UNDEFINED
;
102 while (!(read32(QUP_ADDR(id
, QUP_OPERATIONAL
)) & status
)) {
103 ret
= qup_i2c_master_status(id
);
106 if (stopwatch_expired(timeout
))
107 return QUP_ERR_TIMEOUT
;
113 static qup_return_t
qup_fifo_wait_while(blsp_qup_id_t id
, uint32_t status
,
114 struct stopwatch
*timeout
)
116 qup_return_t ret
= QUP_ERR_UNDEFINED
;
118 while (read32(QUP_ADDR(id
, QUP_OPERATIONAL
)) & status
) {
119 ret
= qup_i2c_master_status(id
);
122 if (stopwatch_expired(timeout
))
123 return QUP_ERR_TIMEOUT
;
129 static inline uint32_t qup_i2c_create_output_tag(int stop
, u8 data
)
134 tag
= QUP_I2C_STOP_SEQ
| QUP_I2C_DATA(data
);
136 tag
= QUP_I2C_DATA_SEQ
| QUP_I2C_DATA(data
);
141 static inline qup_return_t
qup_i2c_write_fifo_flush(blsp_qup_id_t id
,
142 struct stopwatch
*timeout
)
144 qup_return_t ret
= QUP_ERR_UNDEFINED
;
146 qup_write32(QUP_ADDR(id
, QUP_OPERATIONAL
), OUTPUT_SERVICE_FLAG
);
148 mdelay(4); /* TPM seems to need this */
150 ret
= qup_fifo_wait_while(id
, OUTPUT_FIFO_NOT_EMPTY
, timeout
);
154 ret
= qup_i2c_master_status(id
);
157 printk(BIOS_DEBUG
, "%s: error\n", __func__
);
162 static qup_return_t
qup_i2c_write_fifo(blsp_qup_id_t id
, qup_data_t
*p_tx_obj
,
165 qup_return_t ret
= QUP_ERR_UNDEFINED
;
166 uint8_t addr
= p_tx_obj
->p
.iic
.addr
;
167 uint8_t *data_ptr
= p_tx_obj
->p
.iic
.data
;
168 unsigned int data_len
= p_tx_obj
->p
.iic
.data_len
;
169 unsigned int idx
= 0;
170 uint32_t tag
, *fifo
= QUP_ADDR(id
, QUP_OUTPUT_FIFO
);
171 struct stopwatch timeout
;
173 qup_reset_master_status(id
);
175 qup_write32(QUP_ADDR(id
, QUP_MX_OUTPUT_COUNT
), data_len
+ 1);
177 qup_set_state(id
, QUP_STATE_RUN
);
180 * Since UNPACK enable is set in io mode register, populate 2 tags
181 * for each fifo register.
183 * Create the first tag as follows, with the start tag and first byte
184 * of the data to be written
185 * +--------+--------+--------+--------+
186 * | STOP / | data | START | ADDR |
187 * |DATA tag| byte | tag | << 1 |
188 * +--------+--------+--------+--------+
189 * rest will be created in the following while loop.
191 tag
= qup_i2c_create_output_tag(data_len
== 1 && stop_seq
,
193 tag
= ((tag
<< 16) & 0xffff0000) |
194 (QUP_I2C_START_SEQ
| QUP_I2C_ADDR(addr
));
198 qup_write32(fifo
, tag
);
200 stopwatch_init_usecs_expire(&timeout
, CONFIG_I2C_TRANSFER_TIMEOUT_US
);
202 tag
= qup_i2c_create_output_tag(data_len
== 1 && stop_seq
,
208 tag
|= qup_i2c_create_output_tag(
209 data_len
== 1 && stop_seq
,
210 data_ptr
[idx
]) << 16;
215 qup_write32(fifo
, tag
);
217 ret
= qup_i2c_write_fifo_flush(id
, &timeout
);
220 printk(QUPDBG
"%s: error\n", __func__
);
225 ret
= qup_i2c_write_fifo_flush(id
, &timeout
);
227 qup_set_state(id
, QUP_STATE_RESET
);
232 static qup_return_t
qup_i2c_write(blsp_qup_id_t id
, uint8_t mode
,
233 qup_data_t
*p_tx_obj
, uint8_t stop_seq
)
235 qup_return_t ret
= QUP_ERR_UNDEFINED
;
240 ret
= qup_i2c_write_fifo(id
, p_tx_obj
, stop_seq
);
243 ret
= QUP_ERR_UNSUPPORTED
;
247 qup_set_state(id
, QUP_STATE_RESET
);
248 printk(QUPDBG
"%s() failed (%d)\n", __func__
, ret
);
254 static int qup_i2c_parse_tag(uint32_t data
, uint8_t *data_ptr
, uint32_t len
)
257 int max
= (len
> 2) ? 2 : len
;
259 for (i
= 0; i
< max
; i
++) {
260 switch (QUP_I2C_MI_TAG(data
)) {
261 case QUP_I2C_MIDATA_SEQ
:
262 data_ptr
[idx
] = QUP_I2C_DATA(data
);
265 case QUP_I2C_MISTOP_SEQ
:
266 data_ptr
[idx
] = QUP_I2C_DATA(data
);
270 printk(QUPDBG
"%s: Unexpected tag (0x%x)\n", __func__
,
271 QUP_I2C_MI_TAG(data
));
281 static qup_return_t
qup_i2c_read_fifo(blsp_qup_id_t id
, qup_data_t
*p_tx_obj
)
283 qup_return_t ret
= QUP_ERR_UNDEFINED
;
284 uint8_t addr
= p_tx_obj
->p
.iic
.addr
;
285 uint8_t *data_ptr
= p_tx_obj
->p
.iic
.data
;
286 unsigned int data_len
= p_tx_obj
->p
.iic
.data_len
;
287 unsigned int idx
= 0;
288 uint32_t *fifo
= QUP_ADDR(id
, QUP_OUTPUT_FIFO
);
289 struct stopwatch timeout
;
291 qup_reset_master_status(id
);
293 qup_write32(QUP_ADDR(id
, QUP_IO_MODES
),
294 QUP_UNPACK_EN
| QUP_PACK_EN
|
295 ((QUP_MODE_BLOCK
& QUP_MODE_MASK
) <<
296 QUP_OUTPUT_MODE_SHFT
) |
297 ((QUP_MODE_BLOCK
& QUP_MODE_MASK
) <<
298 QUP_INPUT_MODE_SHFT
));
300 qup_write32(QUP_ADDR(id
, QUP_MX_INPUT_COUNT
), data_len
);
302 qup_set_state(id
, QUP_STATE_RUN
);
304 qup_write32(fifo
, (QUP_I2C_START_SEQ
|
305 (QUP_I2C_ADDR(addr
) | QUP_I2C_SLAVE_READ
)) |
306 ((QUP_I2C_RECV_SEQ
| data_len
) << 16));
308 stopwatch_init_usecs_expire(&timeout
, CONFIG_I2C_TRANSFER_TIMEOUT_US
);
309 ret
= qup_i2c_write_fifo_flush(id
, &timeout
);
311 printk(QUPDBG
"%s: OUTPUT_FIFO_NOT_EMPTY\n", __func__
);
315 ret
= qup_fifo_wait_for(id
, INPUT_SERVICE_FLAG
, &timeout
);
317 printk(QUPDBG
"%s: INPUT_SERVICE_FLAG\n", __func__
);
321 fifo
= QUP_ADDR(id
, QUP_INPUT_FIFO
);
330 count
= qup_i2c_parse_tag(data
, data_ptr
+ idx
, data_len
);
333 printk(QUPDBG
"%s: Cannot parse tag 0x%x\n",
335 qup_set_state(id
, QUP_STATE_PAUSE
);
337 return QUP_ERR_I2C_INVALID_TAG
;
343 qup_write32(QUP_ADDR(id
, QUP_OPERATIONAL
), INPUT_SERVICE_FLAG
);
346 p_tx_obj
->p
.iic
.data_len
= idx
;
348 qup_write32(QUP_ADDR(id
, QUP_MX_READ_COUNT
), 0);
350 qup_set_state(id
, QUP_STATE_RESET
);
355 static qup_return_t
qup_i2c_read(blsp_qup_id_t id
, uint8_t mode
,
356 qup_data_t
*p_tx_obj
)
358 qup_return_t ret
= QUP_ERR_UNDEFINED
;
360 qup_set_state(id
, QUP_STATE_RESET
);
365 ret
= qup_i2c_read_fifo(id
, p_tx_obj
);
368 ret
= QUP_ERR_UNSUPPORTED
;
372 qup_set_state(id
, QUP_STATE_RESET
);
373 printk(QUPDBG
"%s() failed (%d)\n", __func__
, ret
);
379 qup_return_t
qup_init(blsp_qup_id_t id
, const qup_config_t
*config_ptr
)
381 qup_return_t ret
= QUP_ERR_UNDEFINED
;
384 /* Reset the QUP core.*/
385 qup_write32(QUP_ADDR(id
, QUP_SW_RESET
), 0x1);
387 /* Wait till the reset takes effect */
388 ret
= qup_wait_for_state(id
, QUP_STATE_RESET
);
392 /* Reset the config */
393 qup_write32(QUP_ADDR(id
, QUP_CONFIG
), 0);
395 /* Program the config register */
399 switch (config_ptr
->protocol
) {
400 case QUP_MINICORE_I2C_MASTER
:
401 reg_val
|= ((config_ptr
->protocol
&
402 QUP_MINI_CORE_PROTO_MASK
) <<
403 QUP_MINI_CORE_PROTO_SHFT
);
406 ret
= QUP_ERR_UNSUPPORTED
;
409 reg_val
|= QUP_APP_CLK_ON_EN
| QUP_CORE_CLK_ON_EN
;
410 qup_write32(QUP_ADDR(id
, QUP_CONFIG
), reg_val
);
412 /* Choose version 1 tag */
413 qup_write32(QUP_ADDR(id
, QUP_I2C_MASTER_CONFIG
), 0);
415 /* Reset i2c clk cntl register */
416 qup_write32(QUP_ADDR(id
, QUP_I2C_MASTER_CLK_CTL
), 0);
418 /* Set QUP IO Mode */
419 switch (config_ptr
->mode
) {
422 reg_val
= QUP_UNPACK_EN
| QUP_PACK_EN
|
423 ((config_ptr
->mode
& QUP_MODE_MASK
) <<
424 QUP_OUTPUT_MODE_SHFT
) |
425 ((config_ptr
->mode
& QUP_MODE_MASK
) <<
426 QUP_INPUT_MODE_SHFT
);
429 ret
= QUP_ERR_UNSUPPORTED
;
432 qup_write32(QUP_ADDR(id
, QUP_IO_MODES
), reg_val
);
435 reg_val
= (QUP_DIVIDER_MIN_VAL
<< QUP_HS_DIVIDER_SHFT
);
436 reg_val
|= ((((config_ptr
->src_frequency
/ config_ptr
->clk_frequency
)
437 / 2) - QUP_DIVIDER_MIN_VAL
) &
438 QUP_FS_DIVIDER_MASK
);
439 qup_write32(QUP_ADDR(id
, QUP_I2C_MASTER_CLK_CTL
), reg_val
);
441 qup_set_state(id
, QUP_STATE_RESET
);
444 printk(QUPDBG
"failed to init qup (%d)\n", ret
);
449 qup_return_t
qup_set_state(blsp_qup_id_t id
, uint32_t state
)
451 qup_return_t ret
= QUP_ERR_UNDEFINED
;
452 unsigned int curr_state
= read32(QUP_ADDR(id
, QUP_STATE
));
454 if (state
<= QUP_STATE_PAUSE
&& (curr_state
& QUP_STATE_VALID_MASK
)) {
456 * For PAUSE_STATE to RESET_STATE transition,
457 * two writes of 10[binary]) are required for the
458 * transition to complete.
460 if (QUP_STATE_PAUSE
== curr_state
&& QUP_STATE_RESET
== state
) {
461 qup_write32(QUP_ADDR(id
, QUP_STATE
), 0x2);
462 qup_write32(QUP_ADDR(id
, QUP_STATE
), 0x2);
464 qup_write32(QUP_ADDR(id
, QUP_STATE
), state
);
466 ret
= qup_wait_for_state(id
, state
);
472 static qup_return_t
qup_i2c_send_data(blsp_qup_id_t id
, qup_data_t
*p_tx_obj
,
475 qup_return_t ret
= QUP_ERR_UNDEFINED
;
476 uint8_t mode
= (read32(QUP_ADDR(id
, QUP_IO_MODES
)) >>
477 QUP_OUTPUT_MODE_SHFT
) & QUP_MODE_MASK
;
479 ret
= qup_i2c_write(id
, mode
, p_tx_obj
, stop_seq
);
483 printk(BIOS_DEBUG
, "i2c tx bus %d device %2.2x:",
484 id
, p_tx_obj
->p
.iic
.addr
);
485 for (i
= 0; i
< p_tx_obj
->p
.iic
.data_len
; i
++)
486 printk(BIOS_DEBUG
, " %2.2x", p_tx_obj
->p
.iic
.data
[i
]);
487 printk(BIOS_DEBUG
, "\n");
493 qup_return_t
qup_send_data(blsp_qup_id_t id
, qup_data_t
*p_tx_obj
,
496 qup_return_t ret
= QUP_ERR_UNDEFINED
;
498 if (p_tx_obj
->protocol
== ((read32(QUP_ADDR(id
, QUP_CONFIG
)) >>
499 QUP_MINI_CORE_PROTO_SHFT
) & QUP_MINI_CORE_PROTO_MASK
)) {
500 switch (p_tx_obj
->protocol
) {
501 case QUP_MINICORE_I2C_MASTER
:
502 ret
= qup_i2c_send_data(id
, p_tx_obj
, stop_seq
);
505 ret
= QUP_ERR_UNSUPPORTED
;
512 static qup_return_t
qup_i2c_recv_data(blsp_qup_id_t id
, qup_data_t
*p_rx_obj
)
514 qup_return_t ret
= QUP_ERR_UNDEFINED
;
515 uint8_t mode
= (read32(QUP_ADDR(id
, QUP_IO_MODES
)) >>
516 QUP_INPUT_MODE_SHFT
) & QUP_MODE_MASK
;
518 ret
= qup_i2c_read(id
, mode
, p_rx_obj
);
522 printk(BIOS_DEBUG
, "i2c rxed on bus %d device %2.2x:",
523 id
, p_rx_obj
->p
.iic
.addr
);
524 for (i
= 0; i
< p_rx_obj
->p
.iic
.data_len
; i
++)
525 printk(BIOS_DEBUG
, " %2.2x", p_rx_obj
->p
.iic
.data
[i
]);
526 printk(BIOS_DEBUG
, "\n");
532 qup_return_t
qup_recv_data(blsp_qup_id_t id
, qup_data_t
*p_rx_obj
)
534 qup_return_t ret
= QUP_ERR_UNDEFINED
;
536 if (p_rx_obj
->protocol
== ((read32(QUP_ADDR(id
, QUP_CONFIG
)) >>
537 QUP_MINI_CORE_PROTO_SHFT
) & QUP_MINI_CORE_PROTO_MASK
)) {
538 switch (p_rx_obj
->protocol
) {
539 case QUP_MINICORE_I2C_MASTER
:
540 ret
= qup_i2c_recv_data(id
, p_rx_obj
);
543 ret
= QUP_ERR_UNSUPPORTED
;