root / hw / i2c.c @ 5439779e
History | View | Annotate | Download (2.7 kB)
1 | 5fafdf24 | ths | /*
|
---|---|---|---|
2 | 0ff596d0 | pbrook | * QEMU I2C bus interface.
|
3 | 0ff596d0 | pbrook | *
|
4 | 0ff596d0 | pbrook | * Copyright (c) 2007 CodeSourcery.
|
5 | 0ff596d0 | pbrook | * Written by Paul Brook
|
6 | 0ff596d0 | pbrook | *
|
7 | 0ff596d0 | pbrook | * This code is licenced under the LGPL.
|
8 | 0ff596d0 | pbrook | */
|
9 | 0ff596d0 | pbrook | |
10 | 87ecb68b | pbrook | #include "hw.h" |
11 | 87ecb68b | pbrook | #include "i2c.h" |
12 | 0ff596d0 | pbrook | |
13 | 0ff596d0 | pbrook | struct i2c_bus
|
14 | 0ff596d0 | pbrook | { |
15 | 0ff596d0 | pbrook | i2c_slave *current_dev; |
16 | 0ff596d0 | pbrook | i2c_slave *dev; |
17 | 0ff596d0 | pbrook | }; |
18 | 0ff596d0 | pbrook | |
19 | 0ff596d0 | pbrook | /* Create a new I2C bus. */
|
20 | 0ff596d0 | pbrook | i2c_bus *i2c_init_bus(void)
|
21 | 0ff596d0 | pbrook | { |
22 | 0ff596d0 | pbrook | i2c_bus *bus; |
23 | 0ff596d0 | pbrook | |
24 | 0ff596d0 | pbrook | bus = (i2c_bus *)qemu_mallocz(sizeof(i2c_bus));
|
25 | 0ff596d0 | pbrook | return bus;
|
26 | 0ff596d0 | pbrook | } |
27 | 0ff596d0 | pbrook | |
28 | 0ff596d0 | pbrook | /* Create a new slave device. */
|
29 | 0ff596d0 | pbrook | i2c_slave *i2c_slave_init(i2c_bus *bus, int address, int size) |
30 | 0ff596d0 | pbrook | { |
31 | 0ff596d0 | pbrook | i2c_slave *dev; |
32 | 0ff596d0 | pbrook | |
33 | 0ff596d0 | pbrook | if (size < sizeof(i2c_slave)) |
34 | 87ecb68b | pbrook | hw_error("I2C struct too small");
|
35 | 0ff596d0 | pbrook | |
36 | 0ff596d0 | pbrook | dev = (i2c_slave *)qemu_mallocz(size); |
37 | 0ff596d0 | pbrook | dev->address = address; |
38 | 0ff596d0 | pbrook | dev->next = bus->dev; |
39 | 0ff596d0 | pbrook | bus->dev = dev; |
40 | 0ff596d0 | pbrook | |
41 | 0ff596d0 | pbrook | return dev;
|
42 | 0ff596d0 | pbrook | } |
43 | 0ff596d0 | pbrook | |
44 | 0ff596d0 | pbrook | void i2c_set_slave_address(i2c_slave *dev, int address) |
45 | 0ff596d0 | pbrook | { |
46 | 0ff596d0 | pbrook | dev->address = address; |
47 | 0ff596d0 | pbrook | } |
48 | 0ff596d0 | pbrook | |
49 | 0ff596d0 | pbrook | /* Return nonzero if bus is busy. */
|
50 | 0ff596d0 | pbrook | int i2c_bus_busy(i2c_bus *bus)
|
51 | 0ff596d0 | pbrook | { |
52 | 0ff596d0 | pbrook | return bus->current_dev != NULL; |
53 | 0ff596d0 | pbrook | } |
54 | 0ff596d0 | pbrook | |
55 | 4a2c8ac2 | balrog | /* Returns non-zero if the address is not valid. */
|
56 | 0ff596d0 | pbrook | /* TODO: Make this handle multiple masters. */
|
57 | 0ff596d0 | pbrook | int i2c_start_transfer(i2c_bus *bus, int address, int recv) |
58 | 0ff596d0 | pbrook | { |
59 | 0ff596d0 | pbrook | i2c_slave *dev; |
60 | 0ff596d0 | pbrook | |
61 | 0ff596d0 | pbrook | for (dev = bus->dev; dev; dev = dev->next) {
|
62 | 0ff596d0 | pbrook | if (dev->address == address)
|
63 | 0ff596d0 | pbrook | break;
|
64 | 0ff596d0 | pbrook | } |
65 | 0ff596d0 | pbrook | |
66 | 0ff596d0 | pbrook | if (!dev)
|
67 | 0ff596d0 | pbrook | return 1; |
68 | 0ff596d0 | pbrook | |
69 | 0ff596d0 | pbrook | /* If the bus is already busy, assume this is a repeated
|
70 | 0ff596d0 | pbrook | start condition. */
|
71 | 0ff596d0 | pbrook | bus->current_dev = dev; |
72 | 0ff596d0 | pbrook | dev->event(dev, recv ? I2C_START_RECV : I2C_START_SEND); |
73 | 0ff596d0 | pbrook | return 0; |
74 | 0ff596d0 | pbrook | } |
75 | 0ff596d0 | pbrook | |
76 | 0ff596d0 | pbrook | void i2c_end_transfer(i2c_bus *bus)
|
77 | 0ff596d0 | pbrook | { |
78 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
79 | 0ff596d0 | pbrook | |
80 | 0ff596d0 | pbrook | if (!dev)
|
81 | 0ff596d0 | pbrook | return;
|
82 | 0ff596d0 | pbrook | |
83 | 0ff596d0 | pbrook | dev->event(dev, I2C_FINISH); |
84 | 0ff596d0 | pbrook | |
85 | 0ff596d0 | pbrook | bus->current_dev = NULL;
|
86 | 0ff596d0 | pbrook | } |
87 | 0ff596d0 | pbrook | |
88 | 0ff596d0 | pbrook | int i2c_send(i2c_bus *bus, uint8_t data)
|
89 | 0ff596d0 | pbrook | { |
90 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
91 | 0ff596d0 | pbrook | |
92 | 0ff596d0 | pbrook | if (!dev)
|
93 | 0ff596d0 | pbrook | return -1; |
94 | 0ff596d0 | pbrook | |
95 | 0ff596d0 | pbrook | return dev->send(dev, data);
|
96 | 0ff596d0 | pbrook | } |
97 | 0ff596d0 | pbrook | |
98 | 0ff596d0 | pbrook | int i2c_recv(i2c_bus *bus)
|
99 | 0ff596d0 | pbrook | { |
100 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
101 | 0ff596d0 | pbrook | |
102 | 0ff596d0 | pbrook | if (!dev)
|
103 | 0ff596d0 | pbrook | return -1; |
104 | 0ff596d0 | pbrook | |
105 | 0ff596d0 | pbrook | return dev->recv(dev);
|
106 | 0ff596d0 | pbrook | } |
107 | 0ff596d0 | pbrook | |
108 | 0ff596d0 | pbrook | void i2c_nack(i2c_bus *bus)
|
109 | 0ff596d0 | pbrook | { |
110 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
111 | 0ff596d0 | pbrook | |
112 | 0ff596d0 | pbrook | if (!dev)
|
113 | 0ff596d0 | pbrook | return;
|
114 | 0ff596d0 | pbrook | |
115 | 0ff596d0 | pbrook | dev->event(dev, I2C_NACK); |
116 | 0ff596d0 | pbrook | } |
117 | 0ff596d0 | pbrook | |
118 | aa941b94 | balrog | void i2c_bus_save(QEMUFile *f, i2c_bus *bus)
|
119 | aa941b94 | balrog | { |
120 | aa941b94 | balrog | qemu_put_byte(f, bus->current_dev ? bus->current_dev->address : 0x00);
|
121 | aa941b94 | balrog | } |
122 | aa941b94 | balrog | |
123 | aa941b94 | balrog | void i2c_bus_load(QEMUFile *f, i2c_bus *bus)
|
124 | aa941b94 | balrog | { |
125 | aa941b94 | balrog | i2c_slave *dev; |
126 | aa941b94 | balrog | uint8_t address = qemu_get_byte(f); |
127 | aa941b94 | balrog | |
128 | aa941b94 | balrog | if (address) {
|
129 | aa941b94 | balrog | for (dev = bus->dev; dev; dev = dev->next)
|
130 | aa941b94 | balrog | if (dev->address == address) {
|
131 | aa941b94 | balrog | bus->current_dev = dev; |
132 | aa941b94 | balrog | return;
|
133 | aa941b94 | balrog | } |
134 | aa941b94 | balrog | |
135 | aa941b94 | balrog | fprintf(stderr, "%s: I2C slave with address %02x disappeared\n",
|
136 | aa941b94 | balrog | __FUNCTION__, address); |
137 | aa941b94 | balrog | } |
138 | aa941b94 | balrog | } |
139 | aa941b94 | balrog | |
140 | aa941b94 | balrog | void i2c_slave_save(QEMUFile *f, i2c_slave *dev)
|
141 | aa941b94 | balrog | { |
142 | aa941b94 | balrog | qemu_put_byte(f, dev->address); |
143 | aa941b94 | balrog | } |
144 | aa941b94 | balrog | |
145 | aa941b94 | balrog | void i2c_slave_load(QEMUFile *f, i2c_slave *dev)
|
146 | aa941b94 | balrog | { |
147 | aa941b94 | balrog | dev->address = qemu_get_byte(f); |
148 | aa941b94 | balrog | } |