mb/google/brya/var/uldrenite: Generate RAM ID and SPD file
[coreboot.git] / src / soc / qualcomm / qcs405 / i2c.c
blob2d0ec91921de93c97638342c5269cc3d7cf3f702
1 /* SPDX-License-Identifier: BSD-3-Clause */
3 #include <console/console.h>
4 #include <device/i2c_simple.h>
5 #include <string.h>
6 #include <soc/blsp.h>
7 #include <soc/qup.h>
8 #include <soc/gpio.h>
10 static qup_config_t blsp1_qup0_config = {
11 QUP_MINICORE_I2C_MASTER,
12 400000,
13 19200000,
14 QUP_MODE_BLOCK,
18 static qup_config_t blsp1_qup1_config = {
19 QUP_MINICORE_I2C_MASTER,
20 400000,
21 19200000,
22 QUP_MODE_BLOCK,
26 static qup_config_t blsp1_qup2_config = {
27 QUP_MINICORE_I2C_MASTER,
28 400000,
29 19200000,
30 QUP_MODE_BLOCK,
34 static qup_config_t blsp1_qup3_config = {
35 QUP_MINICORE_I2C_MASTER,
36 400000,
37 19200000,
38 QUP_MODE_BLOCK,
42 static int i2c_read(uint32_t gsbi_id, uint8_t slave,
43 uint8_t *data, int data_len)
45 qup_data_t obj;
46 qup_return_t qup_ret = 0;
48 memset(&obj, 0, sizeof(obj));
49 obj.protocol = QUP_MINICORE_I2C_MASTER;
50 obj.p.iic.addr = slave;
51 obj.p.iic.data_len = data_len;
52 obj.p.iic.data = data;
53 qup_ret = qup_recv_data(gsbi_id, &obj);
55 if (qup_ret != QUP_SUCCESS)
56 return 1;
57 else
58 return 0;
61 static int i2c_write(uint32_t gsbi_id, uint8_t slave,
62 uint8_t *data, int data_len, uint8_t stop_seq)
64 qup_data_t obj;
65 qup_return_t qup_ret = 0;
67 memset(&obj, 0, sizeof(obj));
68 obj.protocol = QUP_MINICORE_I2C_MASTER;
69 obj.p.iic.addr = slave;
70 obj.p.iic.data_len = data_len;
71 obj.p.iic.data = data;
72 qup_ret = qup_send_data(gsbi_id, &obj, stop_seq);
74 if (qup_ret != QUP_SUCCESS)
75 return 1;
76 else
77 return 0;
80 static int i2c_init(blsp_qup_id_t id)
82 qup_config_t *qup_config;
84 switch (id) {
85 case BLSP_QUP_ID_0:
86 qup_config = &blsp1_qup0_config;
87 break;
88 case BLSP_QUP_ID_1:
89 qup_config = &blsp1_qup1_config;
90 break;
91 case BLSP_QUP_ID_2:
92 qup_config = &blsp1_qup2_config;
93 break;
94 case BLSP_QUP_ID_3:
95 qup_config = &blsp1_qup3_config;
96 break;
97 default:
98 printk(BIOS_ERR, "QUP configuration not defined for BLSP%d.\n",
99 id);
100 return 1;
103 if (qup_config->initialized)
104 return 0;
106 if (blsp_i2c_init(id)) {
107 printk(BIOS_ERR, "failed to initialize blsp\n");
108 return 1;
111 if (qup_init(id, qup_config)) {
112 printk(BIOS_ERR, "failed to initialize qup\n");
113 return 1;
116 if (qup_reset_i2c_master_status(id)) {
117 printk(BIOS_ERR, "failed to reset i2c master status\n");
118 return 1;
121 qup_config->initialized = 1;
122 return 0;
125 int platform_i2c_transfer(unsigned int bus, struct i2c_msg *segments,
126 int seg_count)
128 struct i2c_msg *seg = segments;
129 int ret = 0;
131 if (i2c_init(bus))
132 return 1;
134 while (!ret && seg_count--) {
135 if (seg->flags & I2C_M_RD)
136 ret = i2c_read(bus, seg->slave, seg->buf, seg->len);
137 else
138 ret = i2c_write(bus, seg->slave, seg->buf, seg->len,
139 (seg_count ? 0 : 1));
140 seg++;
143 if (ret) {
144 qup_set_state(bus, QUP_STATE_RESET);
145 return 1;
148 return 0;