I2C emulation in qemu-loongson

Apr 22nd, 2009 | Posted by yajin | Filed under emulation, loongson

I think most of you may think it is easy to emulate I2C device in qemu, for qemu has provided a framework of i2c, both the master and slave devices. You are right. Emulating the I2C is not difficult in qemu.

What I want to post here is not emulating I2C device directly, but emulating GPIO logic which pmon uses to emulate I2C. A little confused? Me too. Ok, let me talk the story from the began.

In pmon, it uses sm502 gpio pin 6 and 13 to emulate I2C master devices, which communicates with other slave device, such as SPD EEPROM and temperature sensors. I do not know why lemote uses gpio to emulate i2c, instead of using the I2C master in sm502. When the data is written to gpio pins according to I2C state machine, pmon expects the gpio pins acting like the I2C master devices. So qemu needs to emulate the I2C cycle accurate state machine, not only the I2C function.

I summarize the I2C emulation code logic of pmon here for reference.

(1) Start Bit

SCL=0 -> SDA=1 -> SCL=1 -> SDA=0 -> SCL=0

(2)Stop Bit

SCL=0 -> SDA=0 ->SCL=1 ->SDA=1 -> SCL=0

(3)Write a bit

Put data on sda -> SCL=1 -> SCL=0

(4)Write a byte

repeating (3) 8 times -> SDA=1

(5)Recv ACK

SCL=1 ->read sda until sda=0 -> SCL=0

(6)Read a bit

SCL=1 -> read data on SDA -> SCL=0

(7)Read a byte

repeating (6) 8 times

(8)Send ACK

put ACK on sda -> SCL=1 -> SCL=0

(9)Send a buffer

Do the following steps until the buffer is empty

  1. START
  2. Send slave address
  3. Recv ACK
  4. Send reg address
  5. Recv ACK
  6. Send one byte
  7. Recv ACK
  8. STOP

(10) Recv many bytes from slave deviceDo the following steps until finished

  1. START
  2. Send slave address
  3. Recv ACK
  4. Send reg address
  5. Recv ACK
  6. Repeat START
  7. Send slave address+1(tell slave we want to recv)
  8. Recv ACK
  9. Recv a byte
  10. Send ACK
  11. STOP
No comments yet.