auxbus.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * auxbus.c
  3. *
  4. * Copyright 2015 : GreenSocs Ltd
  5. * http://www.greensocs.com/ , email: info@greensocs.com
  6. *
  7. * Developed by :
  8. * Frederic Konrad <fred.konrad@greensocs.com>
  9. *
  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.
  14. *
  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.
  19. *
  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/>.
  22. *
  23. */
  24. /*
  25. * This is an implementation of the AUX bus for VESA Display Port v1.1a.
  26. */
  27. #include "qemu/osdep.h"
  28. #include "qemu/units.h"
  29. #include "qemu/log.h"
  30. #include "qemu/module.h"
  31. #include "hw/misc/auxbus.h"
  32. #include "hw/i2c/i2c.h"
  33. #include "monitor/monitor.h"
  34. #include "qapi/error.h"
  35. #ifndef DEBUG_AUX
  36. #define DEBUG_AUX 0
  37. #endif
  38. #define DPRINTF(fmt, ...) do { \
  39. if (DEBUG_AUX) { \
  40. qemu_log("aux: " fmt , ## __VA_ARGS__); \
  41. } \
  42. } while (0)
  43. static void aux_slave_dev_print(Monitor *mon, DeviceState *dev, int indent);
  44. static inline I2CBus *aux_bridge_get_i2c_bus(AUXTOI2CState *bridge);
  45. /* aux-bus implementation (internal not public) */
  46. static void aux_bus_class_init(ObjectClass *klass, void *data)
  47. {
  48. BusClass *k = BUS_CLASS(klass);
  49. /* AUXSlave has an MMIO so we need to change the way we print information
  50. * in monitor.
  51. */
  52. k->print_dev = aux_slave_dev_print;
  53. }
  54. AUXBus *aux_bus_init(DeviceState *parent, const char *name)
  55. {
  56. AUXBus *bus;
  57. Object *auxtoi2c;
  58. bus = AUX_BUS(qbus_create(TYPE_AUX_BUS, parent, name));
  59. auxtoi2c = object_new_with_props(TYPE_AUXTOI2C, OBJECT(bus), "i2c",
  60. &error_abort, NULL);
  61. bus->bridge = AUXTOI2C(auxtoi2c);
  62. /* Memory related. */
  63. bus->aux_io = g_malloc(sizeof(*bus->aux_io));
  64. memory_region_init(bus->aux_io, OBJECT(bus), "aux-io", 1 * MiB);
  65. address_space_init(&bus->aux_addr_space, bus->aux_io, "aux-io");
  66. return bus;
  67. }
  68. void aux_bus_realize(AUXBus *bus)
  69. {
  70. qdev_realize(DEVICE(bus->bridge), BUS(bus), &error_fatal);
  71. }
  72. void aux_map_slave(AUXSlave *aux_dev, hwaddr addr)
  73. {
  74. DeviceState *dev = DEVICE(aux_dev);
  75. AUXBus *bus = AUX_BUS(qdev_get_parent_bus(dev));
  76. memory_region_add_subregion(bus->aux_io, addr, aux_dev->mmio);
  77. }
  78. static bool aux_bus_is_bridge(AUXBus *bus, DeviceState *dev)
  79. {
  80. return (dev == DEVICE(bus->bridge));
  81. }
  82. I2CBus *aux_get_i2c_bus(AUXBus *bus)
  83. {
  84. return aux_bridge_get_i2c_bus(bus->bridge);
  85. }
  86. AUXReply aux_request(AUXBus *bus, AUXCommand cmd, uint32_t address,
  87. uint8_t len, uint8_t *data)
  88. {
  89. AUXReply ret = AUX_NACK;
  90. I2CBus *i2c_bus = aux_get_i2c_bus(bus);
  91. size_t i;
  92. bool is_write = false;
  93. DPRINTF("request at address 0x%" PRIX32 ", command %u, len %u\n", address,
  94. cmd, len);
  95. switch (cmd) {
  96. /*
  97. * Forward the request on the AUX bus..
  98. */
  99. case WRITE_AUX:
  100. case READ_AUX:
  101. is_write = cmd == READ_AUX ? false : true;
  102. for (i = 0; i < len; i++) {
  103. if (!address_space_rw(&bus->aux_addr_space, address++,
  104. MEMTXATTRS_UNSPECIFIED, data++, 1,
  105. is_write)) {
  106. ret = AUX_I2C_ACK;
  107. } else {
  108. ret = AUX_NACK;
  109. break;
  110. }
  111. }
  112. break;
  113. /*
  114. * Classic I2C transactions..
  115. */
  116. case READ_I2C:
  117. case WRITE_I2C:
  118. is_write = cmd == READ_I2C ? false : true;
  119. if (i2c_bus_busy(i2c_bus)) {
  120. i2c_end_transfer(i2c_bus);
  121. }
  122. if (i2c_start_transfer(i2c_bus, address, is_write)) {
  123. ret = AUX_I2C_NACK;
  124. break;
  125. }
  126. ret = AUX_I2C_ACK;
  127. while (len > 0) {
  128. if (i2c_send_recv(i2c_bus, data++, is_write) < 0) {
  129. ret = AUX_I2C_NACK;
  130. break;
  131. }
  132. len--;
  133. }
  134. i2c_end_transfer(i2c_bus);
  135. break;
  136. /*
  137. * I2C MOT transactions.
  138. *
  139. * Here we send a start when:
  140. * - We didn't start transaction yet.
  141. * - We had a READ and we do a WRITE.
  142. * - We changed the address.
  143. */
  144. case WRITE_I2C_MOT:
  145. case READ_I2C_MOT:
  146. is_write = cmd == READ_I2C_MOT ? false : true;
  147. ret = AUX_I2C_NACK;
  148. if (!i2c_bus_busy(i2c_bus)) {
  149. /*
  150. * No transactions started..
  151. */
  152. if (i2c_start_transfer(i2c_bus, address, is_write)) {
  153. break;
  154. }
  155. } else if ((address != bus->last_i2c_address) ||
  156. (bus->last_transaction != cmd)) {
  157. /*
  158. * Transaction started but we need to restart..
  159. */
  160. i2c_end_transfer(i2c_bus);
  161. if (i2c_start_transfer(i2c_bus, address, is_write)) {
  162. break;
  163. }
  164. }
  165. bus->last_transaction = cmd;
  166. bus->last_i2c_address = address;
  167. while (len > 0) {
  168. if (i2c_send_recv(i2c_bus, data++, is_write) < 0) {
  169. i2c_end_transfer(i2c_bus);
  170. break;
  171. }
  172. len--;
  173. }
  174. if (len == 0) {
  175. ret = AUX_I2C_ACK;
  176. }
  177. break;
  178. default:
  179. qemu_log_mask(LOG_UNIMP, "AUX cmd=%u not implemented\n", cmd);
  180. return AUX_NACK;
  181. }
  182. DPRINTF("reply: %u\n", ret);
  183. return ret;
  184. }
  185. static const TypeInfo aux_bus_info = {
  186. .name = TYPE_AUX_BUS,
  187. .parent = TYPE_BUS,
  188. .instance_size = sizeof(AUXBus),
  189. .class_init = aux_bus_class_init
  190. };
  191. /* aux-i2c implementation (internal not public) */
  192. struct AUXTOI2CState {
  193. /*< private >*/
  194. DeviceState parent_obj;
  195. /*< public >*/
  196. I2CBus *i2c_bus;
  197. };
  198. static void aux_bridge_class_init(ObjectClass *oc, void *data)
  199. {
  200. DeviceClass *dc = DEVICE_CLASS(oc);
  201. /* This device is private and is created only once for each
  202. * aux-bus in aux_bus_init(..). So don't allow the user to add one.
  203. */
  204. dc->user_creatable = false;
  205. }
  206. static void aux_bridge_init(Object *obj)
  207. {
  208. AUXTOI2CState *s = AUXTOI2C(obj);
  209. s->i2c_bus = i2c_init_bus(DEVICE(obj), "aux-i2c");
  210. }
  211. static inline I2CBus *aux_bridge_get_i2c_bus(AUXTOI2CState *bridge)
  212. {
  213. return bridge->i2c_bus;
  214. }
  215. static const TypeInfo aux_to_i2c_type_info = {
  216. .name = TYPE_AUXTOI2C,
  217. .parent = TYPE_AUX_SLAVE,
  218. .class_init = aux_bridge_class_init,
  219. .instance_size = sizeof(AUXTOI2CState),
  220. .instance_init = aux_bridge_init
  221. };
  222. /* aux-slave implementation */
  223. static void aux_slave_dev_print(Monitor *mon, DeviceState *dev, int indent)
  224. {
  225. AUXBus *bus = AUX_BUS(qdev_get_parent_bus(dev));
  226. AUXSlave *s;
  227. /* Don't print anything if the device is I2C "bridge". */
  228. if (aux_bus_is_bridge(bus, dev)) {
  229. return;
  230. }
  231. s = AUX_SLAVE(dev);
  232. monitor_printf(mon, "%*smemory " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
  233. indent, "",
  234. object_property_get_uint(OBJECT(s->mmio), "addr", NULL),
  235. memory_region_size(s->mmio));
  236. }
  237. void aux_init_mmio(AUXSlave *aux_slave, MemoryRegion *mmio)
  238. {
  239. assert(!aux_slave->mmio);
  240. aux_slave->mmio = mmio;
  241. }
  242. static void aux_slave_class_init(ObjectClass *klass, void *data)
  243. {
  244. DeviceClass *k = DEVICE_CLASS(klass);
  245. set_bit(DEVICE_CATEGORY_MISC, k->categories);
  246. k->bus_type = TYPE_AUX_BUS;
  247. }
  248. static const TypeInfo aux_slave_type_info = {
  249. .name = TYPE_AUX_SLAVE,
  250. .parent = TYPE_DEVICE,
  251. .instance_size = sizeof(AUXSlave),
  252. .abstract = true,
  253. .class_init = aux_slave_class_init,
  254. };
  255. static void aux_register_types(void)
  256. {
  257. type_register_static(&aux_bus_info);
  258. type_register_static(&aux_slave_type_info);
  259. type_register_static(&aux_to_i2c_type_info);
  260. }
  261. type_init(aux_register_types)