]>
git.ipfire.org Git - people/ms/u-boot.git/blob - cpu/mpc5xxx/i2c.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #ifdef CONFIG_HARD_I2C
31 #if (CFG_I2C_MODULE == 2)
32 #define I2C_BASE MPC5XXX_I2C2
33 #elif (CFG_I2C_MODULE == 1)
34 #define I2C_BASE MPC5XXX_I2C1
36 #error CFG_I2C_MODULE is not properly configured
39 #define I2C_TIMEOUT 100
42 struct mpc5xxx_i2c_tap
{
47 static int mpc_reg_in (volatile u32
*reg
);
48 static void mpc_reg_out (volatile u32
*reg
, int val
, int mask
);
49 static int wait_for_bb (void);
50 static int wait_for_pin (int *status
);
51 static int do_address (uchar chip
, char rdwr_flag
);
52 static int send_bytes (uchar chip
, char *buf
, int len
);
53 static int receive_bytes (uchar chip
, char *buf
, int len
);
54 static int mpc_get_fdr (int);
56 static int mpc_reg_in(volatile u32
*reg
)
59 __asm__
__volatile__ ("eieio");
62 static void mpc_reg_out(volatile u32
*reg
, int val
, int mask
)
69 tmp
= mpc_reg_in(reg
);
70 *reg
= ((tmp
& ~mask
) | (val
& mask
)) << 24;
72 __asm__
__volatile__ ("eieio");
77 static int wait_for_bb(void)
79 struct mpc5xxx_i2c
*regs
= (struct mpc5xxx_i2c
*)I2C_BASE
;
80 int timeout
= I2C_TIMEOUT
;
83 status
= mpc_reg_in(®s
->msr
);
85 while (timeout
-- && (status
& I2C_BB
)) {
88 mpc_reg_out(®s
->mcr
, I2C_STA
, I2C_STA
);
89 temp
= mpc_reg_in(®s
->mdr
);
90 mpc_reg_out(®s
->mcr
, 0, I2C_STA
);
91 mpc_reg_out(®s
->mcr
, 0, 0);
92 mpc_reg_out(®s
->mcr
, I2C_EN
, 0);
95 status
= mpc_reg_in(®s
->msr
);
98 return (status
& I2C_BB
);
101 static int wait_for_pin(int *status
)
103 struct mpc5xxx_i2c
*regs
= (struct mpc5xxx_i2c
*)I2C_BASE
;
104 int timeout
= I2C_TIMEOUT
;
106 *status
= mpc_reg_in(®s
->msr
);
108 while (timeout
-- && !(*status
& I2C_IF
)) {
110 *status
= mpc_reg_in(®s
->msr
);
113 if (!(*status
& I2C_IF
)) {
117 mpc_reg_out(®s
->msr
, 0, I2C_IF
);
122 static int do_address(uchar chip
, char rdwr_flag
)
124 struct mpc5xxx_i2c
*regs
= (struct mpc5xxx_i2c
*)I2C_BASE
;
133 mpc_reg_out(®s
->mcr
, I2C_TX
, I2C_TX
);
134 mpc_reg_out(®s
->mdr
, chip
, 0);
136 if (wait_for_pin(&status
)) {
140 if (status
& I2C_RXAK
) {
147 static int send_bytes(uchar chip
, char *buf
, int len
)
149 struct mpc5xxx_i2c
*regs
= (struct mpc5xxx_i2c
*)I2C_BASE
;
153 for (wrcount
= 0; wrcount
< len
; ++wrcount
) {
155 mpc_reg_out(®s
->mdr
, buf
[wrcount
], 0);
157 if (wait_for_pin(&status
)) {
161 if (status
& I2C_RXAK
) {
167 return !(wrcount
== len
);
170 static int receive_bytes(uchar chip
, char *buf
, int len
)
172 struct mpc5xxx_i2c
*regs
= (struct mpc5xxx_i2c
*)I2C_BASE
;
178 mpc_reg_out(®s
->mcr
, 0, I2C_TX
);
180 for (i
= 0; i
< len
; ++i
) {
181 buf
[rdcount
] = mpc_reg_in(®s
->mdr
);
190 if (wait_for_pin(&status
)) {
195 mpc_reg_out(®s
->mcr
, I2C_TXAK
, I2C_TXAK
);
196 buf
[rdcount
++] = mpc_reg_in(®s
->mdr
);
198 if (wait_for_pin(&status
)) {
202 mpc_reg_out(®s
->mcr
, 0, I2C_TXAK
);
207 /**************** I2C API ****************/
209 void i2c_init(int speed
, int saddr
)
211 struct mpc5xxx_i2c
*regs
= (struct mpc5xxx_i2c
*)I2C_BASE
;
213 mpc_reg_out(®s
->mcr
, 0, 0);
214 mpc_reg_out(®s
->madr
, saddr
<< 1, 0);
218 mpc_reg_out(®s
->mfdr
, mpc_get_fdr(speed
), 0);
222 mpc_reg_out(®s
->mcr
, I2C_EN
, I2C_INIT_MASK
);
223 mpc_reg_out(®s
->msr
, 0, I2C_IF
);
228 static int mpc_get_fdr(int speed
)
230 DECLARE_GLOBAL_DATA_PTR
;
234 ulong best_speed
= 0;
237 ulong bestmatch
= 0xffffffffUL
;
238 int best_i
= 0, best_j
= 0, i
, j
;
239 int SCL_Tap
[] = { 9, 10, 12, 15, 5, 6, 7, 8};
240 struct mpc5xxx_i2c_tap scltap
[] = {
252 for (i
= 7; i
>= 0; i
--) {
253 for (j
= 7; j
>= 0; j
--) {
254 scl
= 2 * (scltap
[j
].scl2tap
+
255 (SCL_Tap
[i
] - 1) * scltap
[j
].tap2tap
+ 2);
256 if (ipb
<= speed
*scl
) {
257 if ((speed
*scl
- ipb
) < bestmatch
) {
258 bestmatch
= speed
*scl
- ipb
;
261 best_speed
= ipb
/scl
;
266 divider
= (best_i
& 3) | ((best_i
& 4) << 3) | (best_j
<< 2);
267 if (gd
->flags
& GD_FLG_RELOC
) {
270 printf("%ld kHz, ", best_speed
/ 1000);
278 int i2c_probe(uchar chip
)
280 struct mpc5xxx_i2c
*regs
= (struct mpc5xxx_i2c
*)I2C_BASE
;
283 for (i
= 0; i
< I2C_RETRIES
; i
++) {
284 mpc_reg_out(®s
->mcr
, I2C_STA
, I2C_STA
);
286 if (! do_address(chip
, 0)) {
287 mpc_reg_out(®s
->mcr
, 0, I2C_STA
);
291 mpc_reg_out(®s
->mcr
, 0, I2C_STA
);
295 return (i
== I2C_RETRIES
);
298 int i2c_read(uchar chip
, uint addr
, int alen
, uchar
*buf
, int len
)
301 struct mpc5xxx_i2c
* regs
= (struct mpc5xxx_i2c
*)I2C_BASE
;
304 xaddr
[0] = (addr
>> 24) & 0xFF;
305 xaddr
[1] = (addr
>> 16) & 0xFF;
306 xaddr
[2] = (addr
>> 8) & 0xFF;
307 xaddr
[3] = addr
& 0xFF;
310 printf("i2c_read: bus is busy\n");
314 mpc_reg_out(®s
->mcr
, I2C_STA
, I2C_STA
);
315 if (do_address(chip
, 0)) {
316 printf("i2c_read: failed to address chip\n");
320 if (send_bytes(chip
, &xaddr
[4-alen
], alen
)) {
321 printf("i2c_read: send_bytes failed\n");
325 mpc_reg_out(®s
->mcr
, I2C_RSTA
, I2C_RSTA
);
326 if (do_address(chip
, 1)) {
327 printf("i2c_read: failed to address chip\n");
331 if (receive_bytes(chip
, buf
, len
)) {
332 printf("i2c_read: receive_bytes failed\n");
338 mpc_reg_out(®s
->mcr
, 0, I2C_STA
);
342 int i2c_write(uchar chip
, uint addr
, int alen
, uchar
*buf
, int len
)
345 struct mpc5xxx_i2c
*regs
= (struct mpc5xxx_i2c
*)I2C_BASE
;
348 xaddr
[0] = (addr
>> 24) & 0xFF;
349 xaddr
[1] = (addr
>> 16) & 0xFF;
350 xaddr
[2] = (addr
>> 8) & 0xFF;
351 xaddr
[3] = addr
& 0xFF;
354 printf("i2c_write: bus is busy\n");
358 mpc_reg_out(®s
->mcr
, I2C_STA
, I2C_STA
);
359 if (do_address(chip
, 0)) {
360 printf("i2c_write: failed to address chip\n");
364 if (send_bytes(chip
, &xaddr
[4-alen
], alen
)) {
365 printf("i2c_write: send_bytes failed\n");
369 if (send_bytes(chip
, buf
, len
)) {
370 printf("i2c_write: send_bytes failed\n");
376 mpc_reg_out(®s
->mcr
, 0, I2C_STA
);
380 uchar
i2c_reg_read(uchar chip
, uchar reg
)
384 i2c_read(chip
, reg
, 1, &buf
, 1);
389 void i2c_reg_write(uchar chip
, uchar reg
, uchar val
)
391 i2c_write(chip
, reg
, 1, &val
, 1);
396 #endif /* CONFIG_HARD_I2C */