2
0

auxbus.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_new(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. DPRINTF("request at address 0x%" PRIX32 ", command %u, len %u\n", address,
  93. cmd, len);
  94. switch (cmd) {
  95. /*
  96. * Forward the request on the AUX bus..
  97. */
  98. case WRITE_AUX:
  99. case READ_AUX:
  100. for (i = 0; i < len; i++) {
  101. if (!address_space_rw(&bus->aux_addr_space, address++,
  102. MEMTXATTRS_UNSPECIFIED, data++, 1,
  103. cmd == WRITE_AUX)) {
  104. ret = AUX_I2C_ACK;
  105. } else {
  106. ret = AUX_NACK;
  107. break;
  108. }
  109. }
  110. break;
  111. /*
  112. * Classic I2C transactions..
  113. */
  114. case READ_I2C:
  115. if (i2c_bus_busy(i2c_bus)) {
  116. i2c_end_transfer(i2c_bus);
  117. }
  118. if (i2c_start_recv(i2c_bus, address)) {
  119. ret = AUX_I2C_NACK;
  120. break;
  121. }
  122. ret = AUX_I2C_ACK;
  123. for (i = 0; i < len; i++) {
  124. data[i] = i2c_recv(i2c_bus);
  125. }
  126. i2c_end_transfer(i2c_bus);
  127. break;
  128. case WRITE_I2C:
  129. if (i2c_bus_busy(i2c_bus)) {
  130. i2c_end_transfer(i2c_bus);
  131. }
  132. if (i2c_start_send(i2c_bus, address)) {
  133. ret = AUX_I2C_NACK;
  134. break;
  135. }
  136. ret = AUX_I2C_ACK;
  137. for (i = 0; i < len; i++) {
  138. if (i2c_send(i2c_bus, data[i]) < 0) {
  139. ret = AUX_I2C_NACK;
  140. break;
  141. }
  142. }
  143. i2c_end_transfer(i2c_bus);
  144. break;
  145. /*
  146. * I2C MOT transactions.
  147. *
  148. * Here we send a start when:
  149. * - We didn't start transaction yet.
  150. * - We had a READ and we do a WRITE.
  151. * - We changed the address.
  152. */
  153. case WRITE_I2C_MOT:
  154. ret = AUX_I2C_NACK;
  155. if (!i2c_bus_busy(i2c_bus)) {
  156. /*
  157. * No transactions started..
  158. */
  159. if (i2c_start_send(i2c_bus, address)) {
  160. break;
  161. }
  162. } else if ((address != bus->last_i2c_address) ||
  163. (bus->last_transaction != cmd)) {
  164. /*
  165. * Transaction started but we need to restart..
  166. */
  167. i2c_end_transfer(i2c_bus);
  168. if (i2c_start_send(i2c_bus, address)) {
  169. break;
  170. }
  171. }
  172. bus->last_transaction = cmd;
  173. bus->last_i2c_address = address;
  174. ret = AUX_I2C_ACK;
  175. for (i = 0; i < len; i++) {
  176. if (i2c_send(i2c_bus, data[i]) < 0) {
  177. i2c_end_transfer(i2c_bus);
  178. ret = AUX_I2C_NACK;
  179. break;
  180. }
  181. }
  182. break;
  183. case READ_I2C_MOT:
  184. ret = AUX_I2C_NACK;
  185. if (!i2c_bus_busy(i2c_bus)) {
  186. /*
  187. * No transactions started..
  188. */
  189. if (i2c_start_recv(i2c_bus, address)) {
  190. break;
  191. }
  192. } else if ((address != bus->last_i2c_address) ||
  193. (bus->last_transaction != cmd)) {
  194. /*
  195. * Transaction started but we need to restart..
  196. */
  197. i2c_end_transfer(i2c_bus);
  198. if (i2c_start_recv(i2c_bus, address)) {
  199. break;
  200. }
  201. }
  202. bus->last_transaction = cmd;
  203. bus->last_i2c_address = address;
  204. for (i = 0; i < len; i++) {
  205. data[i] = i2c_recv(i2c_bus);
  206. }
  207. ret = AUX_I2C_ACK;
  208. break;
  209. default:
  210. qemu_log_mask(LOG_UNIMP, "AUX cmd=%u not implemented\n", cmd);
  211. return AUX_NACK;
  212. }
  213. DPRINTF("reply: %u\n", ret);
  214. return ret;
  215. }
  216. static const TypeInfo aux_bus_info = {
  217. .name = TYPE_AUX_BUS,
  218. .parent = TYPE_BUS,
  219. .instance_size = sizeof(AUXBus),
  220. .class_init = aux_bus_class_init
  221. };
  222. /* aux-i2c implementation (internal not public) */
  223. struct AUXTOI2CState {
  224. /*< private >*/
  225. DeviceState parent_obj;
  226. /*< public >*/
  227. I2CBus *i2c_bus;
  228. };
  229. static void aux_bridge_class_init(ObjectClass *oc, void *data)
  230. {
  231. DeviceClass *dc = DEVICE_CLASS(oc);
  232. /* This device is private and is created only once for each
  233. * aux-bus in aux_bus_init(..). So don't allow the user to add one.
  234. */
  235. dc->user_creatable = false;
  236. }
  237. static void aux_bridge_init(Object *obj)
  238. {
  239. AUXTOI2CState *s = AUXTOI2C(obj);
  240. s->i2c_bus = i2c_init_bus(DEVICE(obj), "aux-i2c");
  241. }
  242. static inline I2CBus *aux_bridge_get_i2c_bus(AUXTOI2CState *bridge)
  243. {
  244. return bridge->i2c_bus;
  245. }
  246. static const TypeInfo aux_to_i2c_type_info = {
  247. .name = TYPE_AUXTOI2C,
  248. .parent = TYPE_AUX_SLAVE,
  249. .class_init = aux_bridge_class_init,
  250. .instance_size = sizeof(AUXTOI2CState),
  251. .instance_init = aux_bridge_init
  252. };
  253. /* aux-slave implementation */
  254. static void aux_slave_dev_print(Monitor *mon, DeviceState *dev, int indent)
  255. {
  256. AUXBus *bus = AUX_BUS(qdev_get_parent_bus(dev));
  257. AUXSlave *s;
  258. /* Don't print anything if the device is I2C "bridge". */
  259. if (aux_bus_is_bridge(bus, dev)) {
  260. return;
  261. }
  262. s = AUX_SLAVE(dev);
  263. monitor_printf(mon, "%*smemory " HWADDR_FMT_plx "/" HWADDR_FMT_plx "\n",
  264. indent, "",
  265. object_property_get_uint(OBJECT(s->mmio), "addr", NULL),
  266. memory_region_size(s->mmio));
  267. }
  268. void aux_init_mmio(AUXSlave *aux_slave, MemoryRegion *mmio)
  269. {
  270. assert(!aux_slave->mmio);
  271. aux_slave->mmio = mmio;
  272. }
  273. static void aux_slave_class_init(ObjectClass *klass, void *data)
  274. {
  275. DeviceClass *k = DEVICE_CLASS(klass);
  276. set_bit(DEVICE_CATEGORY_MISC, k->categories);
  277. k->bus_type = TYPE_AUX_BUS;
  278. }
  279. static const TypeInfo aux_slave_type_info = {
  280. .name = TYPE_AUX_SLAVE,
  281. .parent = TYPE_DEVICE,
  282. .instance_size = sizeof(AUXSlave),
  283. .abstract = true,
  284. .class_init = aux_slave_class_init,
  285. };
  286. static void aux_register_types(void)
  287. {
  288. type_register_static(&aux_bus_info);
  289. type_register_static(&aux_slave_type_info);
  290. type_register_static(&aux_to_i2c_type_info);
  291. }
  292. type_init(aux_register_types)