blob: 8736c024642aeab2d2e702b3e1fd8d1fbdc9de31 [file] [log] [blame]
Travis Geiselbrechtbea5a4a2008-09-06 01:32:02 -07001/*
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 <err.h>
25#include <reg.h>
26#include <string.h>
27#include <platform.h>
28#include <platform/omap3.h>
29
30#define LOCAL_TRACE 0
31
32#define I2C_TIMEOUT 200
33
34static const addr_t i2c_reg_base[] = {
35 I2C1_BASE,
36 I2C2_BASE,
37 I2C3_BASE,
38};
39
40#define I2C_REG_ADDR(bus, reg) (i2c_reg_base[bus] + (reg))
41#define I2C_REG(bus, reg) (*REG16(I2C_REG_ADDR(bus, reg)))
42#define I2C_RMW_REG(bus, reg, startbit, width, val) RMWREG16(I2C_REG_ADDR(bus, reg), startbit, width, val)
43
44static void i2c_dump_bus(int bus)
45{
46 hexdump((void *)i2c_reg_base[bus], 128);
47}
48
49static void i2c_reset_bus(int bus)
50{
51 I2C_REG(bus, I2C_CON) &= ~(1<<15); // make sure the bus is disabled
52
53 /* reset the bus */
54 I2C_REG(bus, I2C_SYSC) = (1<<1);
55 I2C_REG(bus, I2C_CON) = (1<<15); // enable the bus
56 while ((I2C_REG(bus, I2C_SYSS) & 1) == 0)
57 ;
58
59 /* disable the bus again and set up some internals */
60 I2C_REG(bus, I2C_CON) &= ~(1<<15); // make sure the bus is disabled
61
62 /* set up the clock */
63 I2C_REG(bus, I2C_PSC) = 23; // 96Mhz / 23 == 4Mhz
64 I2C_REG(bus, I2C_SCLL) = 13;
65 I2C_REG(bus, I2C_SCLH) = 15; // 4Mhz / combined divider of 40 (13+7 + 15+5) == 100khz
66
67 /* slave address */
68 I2C_REG(bus, I2C_OA0) = 1; // XXX made this up
69
70 /* fifo is set to 1 byte trigger */
71 I2C_REG(bus, I2C_BUF) = 0;
72
73 /* disable all interrupts */
74 I2C_REG(bus, I2C_IE) = 0;
75
76 /* enable the bus */
77 I2C_REG(bus, I2C_CON) = (1<<15)|(1<<10)|(1<<9); // enable, master, transmitter mode
78}
79
80static void i2c_wait_for_bb(int bus)
81{
82 I2C_REG(bus, I2C_STAT) = 0xffff; // clear whatever is pending
83 while (I2C_REG(bus, I2C_STAT) & (1<<12)) {
84 I2C_REG(bus, I2C_STAT) = 0xffff; // clear whatever is pending
85 }
86 I2C_REG(bus, I2C_STAT) = 0xffff; // clear whatever is pending
87}
88
89int i2c_transmit(int bus, uint8_t address, const void *buf, size_t count)
90{
91 int err;
92
93 LTRACEF("bus %d, address 0x%hhx, buf %p, count %zd\n", bus, address, buf, count);
94
95 i2c_wait_for_bb(bus);
96
97 I2C_REG(bus, I2C_SA) = address;
98 I2C_REG(bus, I2C_CNT) = count;
99 I2C_REG(bus, I2C_CON) = (1<<15)|(1<<10)|(1<<9)|(1<<1)|(1<<0); // enable, master, transmit, STP, STT
100
101 time_t t = current_time();
102
103 const uint8_t *ptr = (const uint8_t *)buf;
104 for(;;) {
105 uint16_t stat = I2C_REG(bus, I2C_STAT);
106 if (stat & (1<<1)) {
107 // NACK
108// printf("NACK\n");
109 err = -1;
110 goto out;
111 }
112 if (stat & (1<<0)) {
113 // AL (arbitration lost)
114// printf("arbitration lost!\n");
115 err = -1;
116 goto out;
117 }
118 if (stat & (1<<2)) {
119 // ARDY
120// printf("ARDY, completed\n");
121 break;
122 }
123 if (stat & (1<<4)) {
124 // RRDY
125// printf("XRDY\n");
126
127 // transmit a byte
128 *REG8(I2C_REG_ADDR(bus, I2C_DATA)) = *ptr;
129 ptr++;
130 }
131 I2C_REG(bus, I2C_STAT) = stat;
132
133 if (current_time() - t > I2C_TIMEOUT) {
134// printf("i2c timeout\n");
135 err = ERR_TIMED_OUT;
136 goto out;
137 }
138 }
139
140 err = 0;
141
142out:
143 I2C_REG(bus, I2C_STAT) = 0xffff;
144 I2C_REG(bus, I2C_CNT) = 0;
145
146 return err;
147}
148
149int i2c_receive(int bus, uint8_t address, void *buf, size_t count)
150{
151 int err;
152
153 LTRACEF("bus %d, address 0x%hhx, buf %p, count %zd\n", bus, address, buf, count);
154
155 i2c_wait_for_bb(bus);
156
157 I2C_REG(bus, I2C_SA) = address;
158 I2C_REG(bus, I2C_CNT) = count;
159 I2C_REG(bus, I2C_CON) = (1<<15)|(1<<10)|(1<<1)|(1<<0); // enable, master, STP, STT
160
161 time_t t = current_time();
162
163 uint8_t *ptr = (uint8_t *)buf;
164 for(;;) {
165 uint16_t stat = I2C_REG(bus, I2C_STAT);
166 if (stat & (1<<1)) {
167 // NACK
168// printf("NACK\n");
169 err = -1;
170 goto out;
171 }
172 if (stat & (1<<0)) {
173 // AL (arbitration lost)
174// printf("arbitration lost!\n");
175 err = -1;
176 goto out;
177 }
178 if (stat & (1<<2)) {
179 // ARDY
180// printf("ARDY, completed\n");
181 break;
182 }
183 if (stat & (1<<3)) {
184 // RRDY
185// printf("RRDY\n");
186
187 // read a byte, since our fifo threshold is set to 1 byte
188 *ptr = *REG8(I2C_REG_ADDR(bus, I2C_DATA));
189 ptr++;
190 }
191 I2C_REG(bus, I2C_STAT) = stat;
192
193 if (current_time() - t > I2C_TIMEOUT) {
194// printf("i2c timeout\n");
195 err = ERR_TIMED_OUT;
196 goto out;
197 }
198 }
199
200 err = 0;
201
202out:
203 I2C_REG(bus, I2C_STAT) = 0xffff;
204 I2C_REG(bus, I2C_CNT) = 0;
205
206 return err;
207}
208
209int i2c_write_reg(int bus, uint8_t address, uint8_t reg, uint8_t val)
210{
211 uint8_t buf[2];
212
213 buf[0] = reg;
214 buf[1] = val;
215
216 return i2c_transmit(bus, address, buf, 2);
217}
218
219int i2c_read_reg(int bus, uint8_t address, uint8_t reg, uint8_t *val)
220{
221 int err = i2c_transmit(bus, address, &reg, 1);
222 if (err < 0)
223 return err;
224
225 return i2c_receive(bus, address, val, 1);
226}
227
228
229void i2c_init_early(void)
230{
231 LTRACE_ENTRY;
232
Travis Geiselbrecht2c691e82008-09-04 02:41:01 -0700233 /* enable clocks on i2c 0-2 */
234 RMWREG32(CM_FCLKEN1_CORE, 15, 3, 0x7),
235 RMWREG32(CM_ICLKEN1_CORE, 15, 3, 0x7),
236
Travis Geiselbrechtbea5a4a2008-09-06 01:32:02 -0700237 i2c_reset_bus(0);
238 i2c_reset_bus(1);
239 i2c_reset_bus(2);
240
241#if 0
242 // write something into a reg
243 char buf[2];
244 i2c_write_reg(0, 0x4b, 0x14, 0x99);
245 i2c_write_reg(0, 0x4b, 0x15, 0x98);
246
247 i2c_read_reg(0, 0x4b, 0x15, buf);
248 printf("0x%hhx\n", buf[0]);
249 i2c_read_reg(0, 0x4b, 0x14, buf);
250 printf("0x%hhx\n", buf[0]);
251
252 int i;
253 for (i=0; i < 255; i++) {
254 char buf[1];
255 buf[0] = i;
256 i2c_transmit(0, 0x4b, buf, 1);
257 i2c_receive(0, 0x4b, buf, sizeof(buf));
258 printf("0x%hhx\n", buf[0]);
259 }
260#endif
261
262 LTRACE_EXIT;
263}
264
265void i2c_init(void)
266{
267}
268
Travis Geiselbrecht39deded2009-01-24 20:12:27 -0800269#if WITH_LIB_CONSOLE
Travis Geiselbrechtbea5a4a2008-09-06 01:32:02 -0700270
Travis Geiselbrecht39deded2009-01-24 20:12:27 -0800271#include <lib/console.h>
Travis Geiselbrechtbea5a4a2008-09-06 01:32:02 -0700272
273static int cmd_i2c(int argc, const cmd_args *argv);
274
275STATIC_COMMAND_START
276 { "i2c", "i2c read/write commands", &cmd_i2c },
277STATIC_COMMAND_END(i2c);
278
279static int cmd_i2c(int argc, const cmd_args *argv)
280{
281 int err;
282
283 if (argc < 5) {
284 printf("not enough arguments\n");
285usage:
286 printf("%s read_reg <bus> <i2c address> <register>\n", argv[0].str);
287 printf("%s write_reg <bus> <i2c address> <register> <val>\n", argv[0].str);
288 return -1;
289 }
290
291 int bus = argv[2].u;
292 uint8_t i2c_address = argv[3].u;
293
294 if (!strcmp(argv[1].str, "read_reg")) {
295 uint8_t reg = argv[4].u;
296 uint8_t val;
297
298 err = i2c_read_reg(bus, i2c_address, reg, &val);
299 printf("i2c_read_reg err %d, val 0x%hhx\n", err, val);
300 } else if (!strcmp(argv[1].str, "write_reg")) {
301 uint8_t reg = argv[4].u;
302 uint8_t val = argv[5].u;
303 err = i2c_write_reg(bus, i2c_address, reg, val);
304 printf("i2c_write_reg err %d\n", err);
305 } else {
306 printf("unrecognized subcommand\n");
307 goto usage;
308 }
309
310 return 0;
311}
312
313#endif // WITH_APP_CONSOLE
314
315