root / hw / i2c.c @ 4870852c
History | View | Annotate | Download (2.9 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 | c701b35b | pbrook | int saved_address;
|
18 | 0ff596d0 | pbrook | }; |
19 | 0ff596d0 | pbrook | |
20 | c701b35b | pbrook | static void i2c_bus_save(QEMUFile *f, void *opaque) |
21 | c701b35b | pbrook | { |
22 | c701b35b | pbrook | i2c_bus *bus = (i2c_bus *)opaque; |
23 | c701b35b | pbrook | |
24 | c701b35b | pbrook | qemu_put_byte(f, bus->current_dev ? bus->current_dev->address : -1);
|
25 | c701b35b | pbrook | } |
26 | c701b35b | pbrook | |
27 | c701b35b | pbrook | static int i2c_bus_load(QEMUFile *f, void *opaque, int version_id) |
28 | c701b35b | pbrook | { |
29 | c701b35b | pbrook | i2c_bus *bus = (i2c_bus *)opaque; |
30 | c701b35b | pbrook | |
31 | c701b35b | pbrook | if (version_id != 1) |
32 | c701b35b | pbrook | return -EINVAL;
|
33 | c701b35b | pbrook | |
34 | c701b35b | pbrook | /* The bus is loaded before attached devices, so load and save the
|
35 | c701b35b | pbrook | current device id. Devices will check themselves as loaded. */
|
36 | d0c22f49 | balrog | bus->saved_address = (int8_t) qemu_get_byte(f); |
37 | c701b35b | pbrook | bus->current_dev = NULL;
|
38 | c701b35b | pbrook | |
39 | c701b35b | pbrook | return 0; |
40 | c701b35b | pbrook | } |
41 | c701b35b | pbrook | |
42 | 0ff596d0 | pbrook | /* Create a new I2C bus. */
|
43 | 0ff596d0 | pbrook | i2c_bus *i2c_init_bus(void)
|
44 | 0ff596d0 | pbrook | { |
45 | 0ff596d0 | pbrook | i2c_bus *bus; |
46 | 0ff596d0 | pbrook | |
47 | 0ff596d0 | pbrook | bus = (i2c_bus *)qemu_mallocz(sizeof(i2c_bus));
|
48 | c701b35b | pbrook | register_savevm("i2c_bus", -1, 1, i2c_bus_save, i2c_bus_load, bus); |
49 | 0ff596d0 | pbrook | return bus;
|
50 | 0ff596d0 | pbrook | } |
51 | 0ff596d0 | pbrook | |
52 | 0ff596d0 | pbrook | /* Create a new slave device. */
|
53 | 0ff596d0 | pbrook | i2c_slave *i2c_slave_init(i2c_bus *bus, int address, int size) |
54 | 0ff596d0 | pbrook | { |
55 | 0ff596d0 | pbrook | i2c_slave *dev; |
56 | 0ff596d0 | pbrook | |
57 | 0ff596d0 | pbrook | if (size < sizeof(i2c_slave)) |
58 | 87ecb68b | pbrook | hw_error("I2C struct too small");
|
59 | 0ff596d0 | pbrook | |
60 | 0ff596d0 | pbrook | dev = (i2c_slave *)qemu_mallocz(size); |
61 | 0ff596d0 | pbrook | dev->address = address; |
62 | 0ff596d0 | pbrook | dev->next = bus->dev; |
63 | 0ff596d0 | pbrook | bus->dev = dev; |
64 | c701b35b | pbrook | dev->bus = bus; |
65 | 0ff596d0 | pbrook | |
66 | 0ff596d0 | pbrook | return dev;
|
67 | 0ff596d0 | pbrook | } |
68 | 0ff596d0 | pbrook | |
69 | 0ff596d0 | pbrook | void i2c_set_slave_address(i2c_slave *dev, int address) |
70 | 0ff596d0 | pbrook | { |
71 | 0ff596d0 | pbrook | dev->address = address; |
72 | 0ff596d0 | pbrook | } |
73 | 0ff596d0 | pbrook | |
74 | 0ff596d0 | pbrook | /* Return nonzero if bus is busy. */
|
75 | 0ff596d0 | pbrook | int i2c_bus_busy(i2c_bus *bus)
|
76 | 0ff596d0 | pbrook | { |
77 | 0ff596d0 | pbrook | return bus->current_dev != NULL; |
78 | 0ff596d0 | pbrook | } |
79 | 0ff596d0 | pbrook | |
80 | 4a2c8ac2 | balrog | /* Returns non-zero if the address is not valid. */
|
81 | 0ff596d0 | pbrook | /* TODO: Make this handle multiple masters. */
|
82 | 0ff596d0 | pbrook | int i2c_start_transfer(i2c_bus *bus, int address, int recv) |
83 | 0ff596d0 | pbrook | { |
84 | 0ff596d0 | pbrook | i2c_slave *dev; |
85 | 0ff596d0 | pbrook | |
86 | 0ff596d0 | pbrook | for (dev = bus->dev; dev; dev = dev->next) {
|
87 | 0ff596d0 | pbrook | if (dev->address == address)
|
88 | 0ff596d0 | pbrook | break;
|
89 | 0ff596d0 | pbrook | } |
90 | 0ff596d0 | pbrook | |
91 | 0ff596d0 | pbrook | if (!dev)
|
92 | 0ff596d0 | pbrook | return 1; |
93 | 0ff596d0 | pbrook | |
94 | 0ff596d0 | pbrook | /* If the bus is already busy, assume this is a repeated
|
95 | 0ff596d0 | pbrook | start condition. */
|
96 | 0ff596d0 | pbrook | bus->current_dev = dev; |
97 | 0ff596d0 | pbrook | dev->event(dev, recv ? I2C_START_RECV : I2C_START_SEND); |
98 | 0ff596d0 | pbrook | return 0; |
99 | 0ff596d0 | pbrook | } |
100 | 0ff596d0 | pbrook | |
101 | 0ff596d0 | pbrook | void i2c_end_transfer(i2c_bus *bus)
|
102 | 0ff596d0 | pbrook | { |
103 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
104 | 0ff596d0 | pbrook | |
105 | 0ff596d0 | pbrook | if (!dev)
|
106 | 0ff596d0 | pbrook | return;
|
107 | 0ff596d0 | pbrook | |
108 | 0ff596d0 | pbrook | dev->event(dev, I2C_FINISH); |
109 | 0ff596d0 | pbrook | |
110 | 0ff596d0 | pbrook | bus->current_dev = NULL;
|
111 | 0ff596d0 | pbrook | } |
112 | 0ff596d0 | pbrook | |
113 | 0ff596d0 | pbrook | int i2c_send(i2c_bus *bus, uint8_t data)
|
114 | 0ff596d0 | pbrook | { |
115 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
116 | 0ff596d0 | pbrook | |
117 | 0ff596d0 | pbrook | if (!dev)
|
118 | 0ff596d0 | pbrook | return -1; |
119 | 0ff596d0 | pbrook | |
120 | 0ff596d0 | pbrook | return dev->send(dev, data);
|
121 | 0ff596d0 | pbrook | } |
122 | 0ff596d0 | pbrook | |
123 | 0ff596d0 | pbrook | int i2c_recv(i2c_bus *bus)
|
124 | 0ff596d0 | pbrook | { |
125 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
126 | 0ff596d0 | pbrook | |
127 | 0ff596d0 | pbrook | if (!dev)
|
128 | 0ff596d0 | pbrook | return -1; |
129 | 0ff596d0 | pbrook | |
130 | 0ff596d0 | pbrook | return dev->recv(dev);
|
131 | 0ff596d0 | pbrook | } |
132 | 0ff596d0 | pbrook | |
133 | 0ff596d0 | pbrook | void i2c_nack(i2c_bus *bus)
|
134 | 0ff596d0 | pbrook | { |
135 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
136 | 0ff596d0 | pbrook | |
137 | 0ff596d0 | pbrook | if (!dev)
|
138 | 0ff596d0 | pbrook | return;
|
139 | 0ff596d0 | pbrook | |
140 | 0ff596d0 | pbrook | dev->event(dev, I2C_NACK); |
141 | 0ff596d0 | pbrook | } |
142 | 0ff596d0 | pbrook | |
143 | aa941b94 | balrog | void i2c_slave_save(QEMUFile *f, i2c_slave *dev)
|
144 | aa941b94 | balrog | { |
145 | aa941b94 | balrog | qemu_put_byte(f, dev->address); |
146 | aa941b94 | balrog | } |
147 | aa941b94 | balrog | |
148 | aa941b94 | balrog | void i2c_slave_load(QEMUFile *f, i2c_slave *dev)
|
149 | aa941b94 | balrog | { |
150 | aa941b94 | balrog | dev->address = qemu_get_byte(f); |
151 | c701b35b | pbrook | if (dev->bus->saved_address == dev->address)
|
152 | c701b35b | pbrook | dev->bus->current_dev = dev; |
153 | aa941b94 | balrog | } |