blob: cfe7c072f1a3b1b330af07eb6e48d19a25d86a6d [file] [log] [blame]
Paul Brook90d37232009-05-14 22:35:09 +01001/*
2 * QEMU Synchronous Serial Interface support
3 *
4 * Copyright (c) 2009 CodeSourcery.
5 * Written by Paul Brook
6 *
7 * This code is licenced under the GNU GPL v2.
8 */
9
10#include "ssi.h"
11
12struct SSIBus {
Paul Brook02e2da42009-05-23 00:05:19 +010013 BusState qbus;
Paul Brook90d37232009-05-14 22:35:09 +010014};
15
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020016static struct BusInfo ssi_bus_info = {
17 .name = "SSI",
18 .size = sizeof(SSIBus),
19};
20
Gerd Hoffmann81a322d2009-08-14 10:36:05 +020021static int ssi_slave_init(DeviceState *dev, DeviceInfo *base_info)
Paul Brook90d37232009-05-14 22:35:09 +010022{
Paul Brook02e2da42009-05-23 00:05:19 +010023 SSISlaveInfo *info = container_of(base_info, SSISlaveInfo, qdev);
Paul Brook90d37232009-05-14 22:35:09 +010024 SSISlave *s = SSI_SLAVE_FROM_QDEV(dev);
Paul Brook02e2da42009-05-23 00:05:19 +010025 SSIBus *bus;
Paul Brook90d37232009-05-14 22:35:09 +010026
Paul Brook02e2da42009-05-23 00:05:19 +010027 bus = FROM_QBUS(SSIBus, qdev_get_parent_bus(dev));
Blue Swirl72cf2d42009-09-12 07:36:22 +000028 if (QLIST_FIRST(&bus->qbus.children) != dev
29 || QLIST_NEXT(dev, sibling) != NULL) {
Paul Brook02e2da42009-05-23 00:05:19 +010030 hw_error("Too many devices on SSI bus");
31 }
32
Paul Brook90d37232009-05-14 22:35:09 +010033 s->info = info;
Gerd Hoffmann81a322d2009-08-14 10:36:05 +020034 return info->init(s);
Paul Brook90d37232009-05-14 22:35:09 +010035}
36
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +020037void ssi_register_slave(SSISlaveInfo *info)
Paul Brook90d37232009-05-14 22:35:09 +010038{
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +020039 assert(info->qdev.size >= sizeof(SSISlave));
Paul Brook02e2da42009-05-23 00:05:19 +010040 info->qdev.init = ssi_slave_init;
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020041 info->qdev.bus_info = &ssi_bus_info;
Gerd Hoffmann074f2ff2009-06-10 09:41:42 +020042 qdev_register(&info->qdev);
Paul Brook90d37232009-05-14 22:35:09 +010043}
44
45DeviceState *ssi_create_slave(SSIBus *bus, const char *name)
46{
47 DeviceState *dev;
Paul Brook02e2da42009-05-23 00:05:19 +010048 dev = qdev_create(&bus->qbus, name);
Markus Armbrustere23a1b32009-10-07 01:15:58 +020049 qdev_init_nofail(dev);
Paul Brook90d37232009-05-14 22:35:09 +010050 return dev;
51}
52
Paul Brook02e2da42009-05-23 00:05:19 +010053SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
Paul Brook90d37232009-05-14 22:35:09 +010054{
Paul Brook02e2da42009-05-23 00:05:19 +010055 BusState *bus;
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020056 bus = qbus_create(&ssi_bus_info, parent, name);
Paul Brook02e2da42009-05-23 00:05:19 +010057 return FROM_QBUS(SSIBus, bus);
Paul Brook90d37232009-05-14 22:35:09 +010058}
59
60uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
61{
Paul Brook02e2da42009-05-23 00:05:19 +010062 DeviceState *dev;
63 SSISlave *slave;
Blue Swirl72cf2d42009-09-12 07:36:22 +000064 dev = QLIST_FIRST(&bus->qbus.children);
Paul Brook02e2da42009-05-23 00:05:19 +010065 if (!dev) {
Paul Brook90d37232009-05-14 22:35:09 +010066 return 0;
67 }
Paul Brook02e2da42009-05-23 00:05:19 +010068 slave = SSI_SLAVE_FROM_QDEV(dev);
69 return slave->info->transfer(slave, val);
Paul Brook90d37232009-05-14 22:35:09 +010070}