4 * Copyright 2015 : GreenSocs Ltd
5 * http://www.greensocs.com/ , email: info@greensocs.com
8 * Frederic Konrad <fred.konrad@greensocs.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option)any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <http://www.gnu.org/licenses/>.
26 * This is an implementation of the AUX bus for VESA Display Port v1.1a.
29 #include "qemu/osdep.h"
30 #include "qemu/units.h"
32 #include "qemu/module.h"
33 #include "hw/misc/auxbus.h"
34 #include "hw/i2c/i2c.h"
35 #include "monitor/monitor.h"
36 #include "qapi/error.h"
42 #define DPRINTF(fmt, ...) do { \
44 qemu_log("aux: " fmt , ## __VA_ARGS__); \
49 static void aux_slave_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
);
50 static inline I2CBus
*aux_bridge_get_i2c_bus(AUXTOI2CState
*bridge
);
52 /* aux-bus implementation (internal not public) */
53 static void aux_bus_class_init(ObjectClass
*klass
, void *data
)
55 BusClass
*k
= BUS_CLASS(klass
);
57 /* AUXSlave has an MMIO so we need to change the way we print information
60 k
->print_dev
= aux_slave_dev_print
;
63 AUXBus
*aux_bus_init(DeviceState
*parent
, const char *name
)
68 bus
= AUX_BUS(qbus_create(TYPE_AUX_BUS
, parent
, name
));
69 auxtoi2c
= object_new_with_props(TYPE_AUXTOI2C
, OBJECT(bus
), "i2c",
72 bus
->bridge
= AUXTOI2C(auxtoi2c
);
75 bus
->aux_io
= g_malloc(sizeof(*bus
->aux_io
));
76 memory_region_init(bus
->aux_io
, OBJECT(bus
), "aux-io", 1 * MiB
);
77 address_space_init(&bus
->aux_addr_space
, bus
->aux_io
, "aux-io");
81 void aux_bus_realize(AUXBus
*bus
)
83 qdev_realize(DEVICE(bus
->bridge
), BUS(bus
), &error_fatal
);
86 void aux_map_slave(AUXSlave
*aux_dev
, hwaddr addr
)
88 DeviceState
*dev
= DEVICE(aux_dev
);
89 AUXBus
*bus
= AUX_BUS(qdev_get_parent_bus(dev
));
90 memory_region_add_subregion(bus
->aux_io
, addr
, aux_dev
->mmio
);
93 static bool aux_bus_is_bridge(AUXBus
*bus
, DeviceState
*dev
)
95 return (dev
== DEVICE(bus
->bridge
));
98 I2CBus
*aux_get_i2c_bus(AUXBus
*bus
)
100 return aux_bridge_get_i2c_bus(bus
->bridge
);
103 AUXReply
aux_request(AUXBus
*bus
, AUXCommand cmd
, uint32_t address
,
104 uint8_t len
, uint8_t *data
)
106 AUXReply ret
= AUX_NACK
;
107 I2CBus
*i2c_bus
= aux_get_i2c_bus(bus
);
110 DPRINTF("request at address 0x%" PRIX32
", command %u, len %u\n", address
,
115 * Forward the request on the AUX bus..
119 for (i
= 0; i
< len
; i
++) {
120 if (!address_space_rw(&bus
->aux_addr_space
, address
++,
121 MEMTXATTRS_UNSPECIFIED
, data
++, 1,
131 * Classic I2C transactions..
134 if (i2c_bus_busy(i2c_bus
)) {
135 i2c_end_transfer(i2c_bus
);
138 if (i2c_start_recv(i2c_bus
, address
)) {
144 for (i
= 0; i
< len
; i
++) {
145 data
[i
] = i2c_recv(i2c_bus
);
147 i2c_end_transfer(i2c_bus
);
150 if (i2c_bus_busy(i2c_bus
)) {
151 i2c_end_transfer(i2c_bus
);
154 if (i2c_start_send(i2c_bus
, address
)) {
160 for (i
= 0; i
< len
; i
++) {
161 if (i2c_send(i2c_bus
, data
[i
]) < 0) {
166 i2c_end_transfer(i2c_bus
);
169 * I2C MOT transactions.
171 * Here we send a start when:
172 * - We didn't start transaction yet.
173 * - We had a READ and we do a WRITE.
174 * - We changed the address.
178 if (!i2c_bus_busy(i2c_bus
)) {
180 * No transactions started..
182 if (i2c_start_send(i2c_bus
, address
)) {
185 } else if ((address
!= bus
->last_i2c_address
) ||
186 (bus
->last_transaction
!= cmd
)) {
188 * Transaction started but we need to restart..
190 i2c_end_transfer(i2c_bus
);
191 if (i2c_start_send(i2c_bus
, address
)) {
196 bus
->last_transaction
= cmd
;
197 bus
->last_i2c_address
= address
;
199 for (i
= 0; i
< len
; i
++) {
200 if (i2c_send(i2c_bus
, data
[i
]) < 0) {
201 i2c_end_transfer(i2c_bus
);
209 if (!i2c_bus_busy(i2c_bus
)) {
211 * No transactions started..
213 if (i2c_start_recv(i2c_bus
, address
)) {
216 } else if ((address
!= bus
->last_i2c_address
) ||
217 (bus
->last_transaction
!= cmd
)) {
219 * Transaction started but we need to restart..
221 i2c_end_transfer(i2c_bus
);
222 if (i2c_start_recv(i2c_bus
, address
)) {
227 bus
->last_transaction
= cmd
;
228 bus
->last_i2c_address
= address
;
229 for (i
= 0; i
< len
; i
++) {
230 data
[i
] = i2c_recv(i2c_bus
);
235 qemu_log_mask(LOG_UNIMP
, "AUX cmd=%u not implemented\n", cmd
);
239 DPRINTF("reply: %u\n", ret
);
243 static const TypeInfo aux_bus_info
= {
244 .name
= TYPE_AUX_BUS
,
246 .instance_size
= sizeof(AUXBus
),
247 .class_init
= aux_bus_class_init
250 /* aux-i2c implementation (internal not public) */
251 struct AUXTOI2CState
{
253 DeviceState parent_obj
;
259 static void aux_bridge_class_init(ObjectClass
*oc
, void *data
)
261 DeviceClass
*dc
= DEVICE_CLASS(oc
);
263 /* This device is private and is created only once for each
264 * aux-bus in aux_bus_init(..). So don't allow the user to add one.
266 dc
->user_creatable
= false;
269 static void aux_bridge_init(Object
*obj
)
271 AUXTOI2CState
*s
= AUXTOI2C(obj
);
273 s
->i2c_bus
= i2c_init_bus(DEVICE(obj
), "aux-i2c");
276 static inline I2CBus
*aux_bridge_get_i2c_bus(AUXTOI2CState
*bridge
)
278 return bridge
->i2c_bus
;
281 static const TypeInfo aux_to_i2c_type_info
= {
282 .name
= TYPE_AUXTOI2C
,
283 .parent
= TYPE_AUX_SLAVE
,
284 .class_init
= aux_bridge_class_init
,
285 .instance_size
= sizeof(AUXTOI2CState
),
286 .instance_init
= aux_bridge_init
289 /* aux-slave implementation */
290 static void aux_slave_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
292 AUXBus
*bus
= AUX_BUS(qdev_get_parent_bus(dev
));
295 /* Don't print anything if the device is I2C "bridge". */
296 if (aux_bus_is_bridge(bus
, dev
)) {
302 monitor_printf(mon
, "%*smemory " TARGET_FMT_plx
"/" TARGET_FMT_plx
"\n",
304 object_property_get_uint(OBJECT(s
->mmio
), "addr", NULL
),
305 memory_region_size(s
->mmio
));
308 void aux_init_mmio(AUXSlave
*aux_slave
, MemoryRegion
*mmio
)
310 assert(!aux_slave
->mmio
);
311 aux_slave
->mmio
= mmio
;
314 static void aux_slave_class_init(ObjectClass
*klass
, void *data
)
316 DeviceClass
*k
= DEVICE_CLASS(klass
);
318 set_bit(DEVICE_CATEGORY_MISC
, k
->categories
);
319 k
->bus_type
= TYPE_AUX_BUS
;
322 static const TypeInfo aux_slave_type_info
= {
323 .name
= TYPE_AUX_SLAVE
,
324 .parent
= TYPE_DEVICE
,
325 .instance_size
= sizeof(AUXSlave
),
327 .class_init
= aux_slave_class_init
,
330 static void aux_register_types(void)
332 type_register_static(&aux_bus_info
);
333 type_register_static(&aux_slave_type_info
);
334 type_register_static(&aux_to_i2c_type_info
);
337 type_init(aux_register_types
)