2 * QEMU I2C bus interface.
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the LGPL.
14 i2c_slave
*current_dev
;
18 /* Create a new I2C bus. */
19 i2c_bus
*i2c_init_bus(void)
23 bus
= (i2c_bus
*)qemu_mallocz(sizeof(i2c_bus
));
27 /* Create a new slave device. */
28 i2c_slave
*i2c_slave_init(i2c_bus
*bus
, int address
, int size
)
32 if (size
< sizeof(i2c_slave
))
33 cpu_abort(cpu_single_env
, "I2C struct too small");
35 dev
= (i2c_slave
*)qemu_mallocz(size
);
36 dev
->address
= address
;
43 void i2c_set_slave_address(i2c_slave
*dev
, int address
)
45 dev
->address
= address
;
48 /* Return nonzero if bus is busy. */
49 int i2c_bus_busy(i2c_bus
*bus
)
51 return bus
->current_dev
!= NULL
;
54 /* Returns nonzero if the bus is already busy, or is the address is not
56 /* TODO: Make this handle multiple masters. */
57 int i2c_start_transfer(i2c_bus
*bus
, int address
, int recv
)
61 for (dev
= bus
->dev
; dev
; dev
= dev
->next
) {
62 if (dev
->address
== address
)
69 /* If the bus is already busy, assume this is a repeated
71 bus
->current_dev
= dev
;
72 dev
->event(dev
, recv
? I2C_START_RECV
: I2C_START_SEND
);
76 void i2c_end_transfer(i2c_bus
*bus
)
78 i2c_slave
*dev
= bus
->current_dev
;
83 dev
->event(dev
, I2C_FINISH
);
85 bus
->current_dev
= NULL
;
88 int i2c_send(i2c_bus
*bus
, uint8_t data
)
90 i2c_slave
*dev
= bus
->current_dev
;
95 return dev
->send(dev
, data
);
98 int i2c_recv(i2c_bus
*bus
)
100 i2c_slave
*dev
= bus
->current_dev
;
105 return dev
->recv(dev
);
108 void i2c_nack(i2c_bus
*bus
)
110 i2c_slave
*dev
= bus
->current_dev
;
115 dev
->event(dev
, I2C_NACK
);
118 void i2c_bus_save(QEMUFile
*f
, i2c_bus
*bus
)
120 qemu_put_byte(f
, bus
->current_dev
? bus
->current_dev
->address
: 0x00);
123 void i2c_bus_load(QEMUFile
*f
, i2c_bus
*bus
)
126 uint8_t address
= qemu_get_byte(f
);
129 for (dev
= bus
->dev
; dev
; dev
= dev
->next
)
130 if (dev
->address
== address
) {
131 bus
->current_dev
= dev
;
135 fprintf(stderr
, "%s: I2C slave with address %02x disappeared\n",
136 __FUNCTION__
, address
);
140 void i2c_slave_save(QEMUFile
*f
, i2c_slave
*dev
)
142 qemu_put_byte(f
, dev
->address
);
145 void i2c_slave_load(QEMUFile
*f
, i2c_slave
*dev
)
147 dev
->address
= qemu_get_byte(f
);