[U-Boot-Users] [Patch]: A small patch for PPC4xx I2C driver

Vladimir Gurevich vag at paulidav.org
Sat Feb 8 06:10:20 CET 2003

Hi Frank,

This code has been copied verbatim from common/soft_i2c.c
and is identical to the implementations in other drivers like
cpu/mpc8260/i2c.c for example. It is done this way
because the prototype for i2c_write is

int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);

As you can see it requires an address of the buffer.

Since this is a real function and not a macro,
it will always work even if the actual parameter is
a literal, there is nothing wrong about it. Because the
prototype is

void i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)

you don't have to pass an address of a value,
just the value itself.

If you look at the code that the compiler generates,
youll see it does the right thing (comments are mine):

(gdb) disas i2c_reg_write
Dump of assembler code for function i2c_reg_write:
# Make a stack frame
0x7fe8a80 <i2c_reg_write>:      stwu    r1,-16(r1)

# Store the return address on the stack
0x7fe8a84 <i2c_reg_write+4>:    mflr    r0
0x7fe8a88 <i2c_reg_write+8>:    stw     r0,20(r1)

# Store "val" on the stack
0x7fe8a8c <i2c_reg_write+12>:   stb     r5,8(r1)

# Set "alen" to 1
0x7fe8a90 <i2c_reg_write+16>:   li      r5,1

# set buffer = &val  (it is r1+8)
0x7fe8a94 <i2c_reg_write+20>:   addi    r6,r1,8

# set "len" to 1
0x7fe8a98 <i2c_reg_write+24>:   li      r7,1

# r3 has i2c_addr, that is chip
# r4 has reg
# i2c_write(chip, reg, 1, &val, 1)
0x7fe8a9c <i2c_reg_write+28>:   bl      0x7fe89a0 <i2c_write>

# restore the return address
0x7fe8aa0 <i2c_reg_write+32>:   lwz     r0,20(r1)
0x7fe8aa4 <i2c_reg_write+36>:   mtlr    r0

# remove the stack frame
0x7fe8aa8 <i2c_reg_write+40>:   addi    r1,r1,16

0x7fe8aac <i2c_reg_write+44>:   blr
End of assembler dump.

Happy hacking,

More information about the U-Boot mailing list