blob: 2a30979695dbe9a5f8a274d3cb43c6c33e5bb95f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <debug.h>
24#include <trace.h>
25#include <err.h>
26#include <reg.h>
27#include <string.h>
28#include <dev/i2c.h>
29#include <platform.h>
30#include <platform/omap3.h>
31
32#define LOCAL_TRACE 0
33
34#define I2C_TIMEOUT 200
35
36static const addr_t i2c_reg_base[] = {
37 I2C1_BASE,
38 I2C2_BASE,
39 I2C3_BASE,
40};
41
42#define I2C_REG_ADDR(bus, reg) (i2c_reg_base[bus] + (reg))
43#define I2C_REG(bus, reg) (*REG16(I2C_REG_ADDR(bus, reg)))
44#define I2C_RMW_REG(bus, reg, startbit, width, val) RMWREG16(I2C_REG_ADDR(bus, reg), startbit, width, val)
45
46static void i2c_dump_bus(int bus)
47{
48 hexdump((void *)i2c_reg_base[bus], 128);
49}
50
51static void i2c_reset_bus(int bus)
52{
53 I2C_REG(bus, I2C_CON) &= ~(1<<15); // make sure the bus is disabled
54
55 /* reset the bus */
56 I2C_REG(bus, I2C_SYSC) = (1<<1);
57 I2C_REG(bus, I2C_CON) = (1<<15); // enable the bus
58 while ((I2C_REG(bus, I2C_SYSS) & 1) == 0)
59 ;
60
61 /* disable the bus again and set up some internals */
62 I2C_REG(bus, I2C_CON) &= ~(1<<15); // make sure the bus is disabled
63
64 /* set up the clock */
65 I2C_REG(bus, I2C_PSC) = 23; // 96Mhz / 23 == 4Mhz
66 I2C_REG(bus, I2C_SCLL) = 13;
67 I2C_REG(bus, I2C_SCLH) = 15; // 4Mhz / combined divider of 40 (13+7 + 15+5) == 100khz
68
69 /* slave address */
70 I2C_REG(bus, I2C_OA0) = 1; // XXX made this up
71
72 /* fifo is set to 1 byte trigger */
73 I2C_REG(bus, I2C_BUF) = 0;
74
75 /* disable all interrupts */
76 I2C_REG(bus, I2C_IE) = 0;
77
78 /* enable the bus */
79 I2C_REG(bus, I2C_CON) = (1<<15)|(1<<10)|(1<<9); // enable, master, transmitter mode
80}
81
82static void i2c_wait_for_bb(int bus)
83{
84 I2C_REG(bus, I2C_STAT) = 0xffff; // clear whatever is pending
85 while (I2C_REG(bus, I2C_STAT) & (1<<12)) {
86 I2C_REG(bus, I2C_STAT) = 0xffff; // clear whatever is pending
87 }
88 I2C_REG(bus, I2C_STAT) = 0xffff; // clear whatever is pending
89}
90
91status_t i2c_transmit(int bus, uint8_t address, const void *buf, size_t count)
92{
93 status_t err;
94
95 LTRACEF("bus %d, address 0x%hhx, buf %p, count %zd\n", bus, address, buf, count);
96
97 i2c_wait_for_bb(bus);
98
99 I2C_REG(bus, I2C_SA) = address;
100 I2C_REG(bus, I2C_CNT) = count;
101 I2C_REG(bus, I2C_CON) = (1<<15)|(1<<10)|(1<<9)|(1<<1)|(1<<0); // enable, master, transmit, STP, STT
102
103 lk_time_t t = current_time();
104
105 const uint8_t *ptr = (const uint8_t *)buf;
106 for (;;) {
107 uint16_t stat = I2C_REG(bus, I2C_STAT);
108 if (stat & (1<<1)) {
109 // NACK
110// printf("NACK\n");
111 err = ERR_GENERIC;
112 goto out;
113 }
114 if (stat & (1<<0)) {
115 // AL (arbitration lost)
116// printf("arbitration lost!\n");
117 err = ERR_GENERIC;
118 goto out;
119 }
120 if (stat & (1<<2)) {
121 // ARDY
122// printf("ARDY, completed\n");
123 break;
124 }
125 if (stat & (1<<4)) {
126 // RRDY
127// printf("XRDY\n");
128
129 // transmit a byte
130 *REG8(I2C_REG_ADDR(bus, I2C_DATA)) = *ptr;
131 ptr++;
132 }
133 I2C_REG(bus, I2C_STAT) = stat;
134
135 if (current_time() - t > I2C_TIMEOUT) {
136// printf("i2c timeout\n");
137 err = ERR_TIMED_OUT;
138 goto out;
139 }
140 }
141
142 err = NO_ERROR;
143
144out:
145 I2C_REG(bus, I2C_STAT) = 0xffff;
146 I2C_REG(bus, I2C_CNT) = 0;
147
148 return err;
149}
150
151status_t i2c_receive(int bus, uint8_t address, void *buf, size_t count)
152{
153 status_t err;
154
155 LTRACEF("bus %d, address 0x%hhx, buf %p, count %zd\n", bus, address, buf, count);
156
157 i2c_wait_for_bb(bus);
158
159 I2C_REG(bus, I2C_SA) = address;
160 I2C_REG(bus, I2C_CNT) = count;
161 I2C_REG(bus, I2C_CON) = (1<<15)|(1<<10)|(1<<1)|(1<<0); // enable, master, STP, STT
162
163 lk_time_t t = current_time();
164
165 uint8_t *ptr = (uint8_t *)buf;
166 for (;;) {
167 uint16_t stat = I2C_REG(bus, I2C_STAT);
168 if (stat & (1<<1)) {
169 // NACK
170// printf("NACK\n");
171 err = ERR_GENERIC;
172 goto out;
173 }
174 if (stat & (1<<0)) {
175 // AL (arbitration lost)
176// printf("arbitration lost!\n");
177 err = ERR_GENERIC;
178 goto out;
179 }
180 if (stat & (1<<2)) {
181 // ARDY
182// printf("ARDY, completed\n");
183 break;
184 }
185 if (stat & (1<<3)) {
186 // RRDY
187// printf("RRDY\n");
188
189 // read a byte, since our fifo threshold is set to 1 byte
190 *ptr = *REG8(I2C_REG_ADDR(bus, I2C_DATA));
191 ptr++;
192 }
193 I2C_REG(bus, I2C_STAT) = stat;
194
195 if (current_time() - t > I2C_TIMEOUT) {
196// printf("i2c timeout\n");
197 err = ERR_TIMED_OUT;
198 goto out;
199 }
200 }
201
202 err = NO_ERROR;
203
204out:
205 I2C_REG(bus, I2C_STAT) = 0xffff;
206 I2C_REG(bus, I2C_CNT) = 0;
207
208 return err;
209}
210
211status_t i2c_write_reg_bytes(int bus, uint8_t address, uint8_t reg, const uint8_t* val, size_t cnt)
212{
213 uint8_t buf[16];
214
215 if (cnt > (sizeof(buf) - 1))
216 return ERR_TOO_BIG;
217
218 buf[0] = reg;
219 memcpy(buf + 1, val, cnt);
220
221 return i2c_transmit(bus, address, buf, cnt + 1);
222}
223
224status_t i2c_read_reg_bytes(int bus, uint8_t address, uint8_t reg, uint8_t *val, size_t cnt)
225{
226 int err = i2c_transmit(bus, address, &reg, 1);
227 if (err < 0)
228 return err;
229
230 return i2c_receive(bus, address, val, cnt);
231}
232
233
234void i2c_init_early(void)
235{
236 LTRACE_ENTRY;
237
238 /* enable clocks on i2c 0-2 */
239 RMWREG32(CM_FCLKEN1_CORE, 15, 3, 0x7),
240 RMWREG32(CM_ICLKEN1_CORE, 15, 3, 0x7),
241
242 i2c_reset_bus(0);
243 i2c_reset_bus(1);
244 i2c_reset_bus(2);
245
246#if 0
247 // write something into a reg
248 char buf[2];
249 i2c_write_reg(0, 0x4b, 0x14, 0x99);
250 i2c_write_reg(0, 0x4b, 0x15, 0x98);
251
252 i2c_read_reg(0, 0x4b, 0x15, buf);
253 printf("0x%hhx\n", buf[0]);
254 i2c_read_reg(0, 0x4b, 0x14, buf);
255 printf("0x%hhx\n", buf[0]);
256
257 int i;
258 for (i=0; i < 255; i++) {
259 char buf[1];
260 buf[0] = i;
261 i2c_transmit(0, 0x4b, buf, 1);
262 i2c_receive(0, 0x4b, buf, sizeof(buf));
263 printf("0x%hhx\n", buf[0]);
264 }
265#endif
266
267 LTRACE_EXIT;
268}
269
270void i2c_init(void)
271{
272}
273
274#if WITH_LIB_CONSOLE
275
276#include <lib/console.h>
277
278static int cmd_i2c(int argc, const cmd_args *argv);
279
280STATIC_COMMAND_START
281STATIC_COMMAND("i2c", "i2c read/write commands", &cmd_i2c)
282STATIC_COMMAND_END(i2c);
283
284static int cmd_i2c(int argc, const cmd_args *argv)
285{
286 int err;
287
288 if (argc < 5) {
289 printf("not enough arguments\n");
290usage:
291 printf("%s read_reg <bus> <i2c address> <register>\n", argv[0].str);
292 printf("%s write_reg <bus> <i2c address> <register> <val>\n", argv[0].str);
293 return -1;
294 }
295
296 int bus = argv[2].u;
297 uint8_t i2c_address = argv[3].u;
298
299 if (!strcmp(argv[1].str, "read_reg")) {
300 uint8_t reg = argv[4].u;
301 uint8_t val;
302
303 err = i2c_read_reg(bus, i2c_address, reg, &val);
304 printf("i2c_read_reg err %d, val 0x%hhx\n", err, val);
305 } else if (!strcmp(argv[1].str, "write_reg")) {
306 uint8_t reg = argv[4].u;
307 uint8_t val = argv[5].u;
308 err = i2c_write_reg(bus, i2c_address, reg, val);
309 printf("i2c_write_reg err %d\n", err);
310 } else {
311 printf("unrecognized subcommand\n");
312 goto usage;
313 }
314
315 return 0;
316}
317
318#endif // WITH_APP_CONSOLE
319
320