1 /* SPDX-License-Identifier: BSD-3-Clause */
3 #include <console/console.h>
4 #include <device/i2c_simple.h>
9 static qup_config_t gsbi1_qup_config
= {
10 QUP_MINICORE_I2C_MASTER
,
17 static qup_config_t gsbi4_qup_config
= {
18 QUP_MINICORE_I2C_MASTER
,
25 static qup_config_t gsbi7_qup_config
= {
26 QUP_MINICORE_I2C_MASTER
,
33 static int i2c_read(uint32_t gsbi_id
, uint8_t slave
,
34 uint8_t *data
, int data_len
)
37 qup_return_t qup_ret
= 0;
39 memset(&obj
, 0, sizeof(obj
));
40 obj
.protocol
= QUP_MINICORE_I2C_MASTER
;
41 obj
.p
.iic
.addr
= slave
;
42 obj
.p
.iic
.data_len
= data_len
;
43 obj
.p
.iic
.data
= data
;
44 qup_ret
= qup_recv_data(gsbi_id
, &obj
);
46 if (QUP_SUCCESS
!= qup_ret
)
52 static int i2c_write(uint32_t gsbi_id
, uint8_t slave
,
53 uint8_t *data
, int data_len
, uint8_t stop_seq
)
56 qup_return_t qup_ret
= 0;
58 memset(&obj
, 0, sizeof(obj
));
59 obj
.protocol
= QUP_MINICORE_I2C_MASTER
;
60 obj
.p
.iic
.addr
= slave
;
61 obj
.p
.iic
.data_len
= data_len
;
62 obj
.p
.iic
.data
= data
;
63 qup_ret
= qup_send_data(gsbi_id
, &obj
, stop_seq
);
65 if (QUP_SUCCESS
!= qup_ret
)
71 static int i2c_init(unsigned int bus
)
73 unsigned int gsbi_id
= bus
;
74 qup_config_t
*qup_config
;
78 qup_config
= &gsbi1_qup_config
;
81 qup_config
= &gsbi4_qup_config
;
84 qup_config
= &gsbi7_qup_config
;
87 printk(BIOS_ERR
, "QUP configuration not defined for GSBI%d.\n",
92 if (qup_config
->initialized
)
95 if (gsbi_init(gsbi_id
, GSBI_PROTO_I2C_ONLY
)) {
96 printk(BIOS_ERR
, "failed to initialize gsbi\n");
100 if (qup_init(gsbi_id
, qup_config
)) {
101 printk(BIOS_ERR
, "failed to initialize qup\n");
105 if (qup_reset_i2c_master_status(gsbi_id
)) {
106 printk(BIOS_ERR
, "failed to reset i2c master status\n");
110 qup_config
->initialized
= 1;
114 int platform_i2c_transfer(unsigned int bus
, struct i2c_msg
*segments
,
117 struct i2c_msg
*seg
= segments
;
123 while (!ret
&& seg_count
--) {
124 if (seg
->flags
& I2C_M_RD
)
125 ret
= i2c_read(bus
, seg
->slave
, seg
->buf
, seg
->len
);
127 ret
= i2c_write(bus
, seg
->slave
, seg
->buf
, seg
->len
,
128 (seg_count
? 0 : 1));
133 qup_set_state(bus
, QUP_STATE_RESET
);