core.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * SD card bus interface code.
  3. *
  4. * Copyright (c) 2015 Linaro Limited
  5. *
  6. * Author:
  7. * Peter Maydell <peter.maydell@linaro.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2 or later, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "qemu/osdep.h"
  22. #include "hw/qdev-core.h"
  23. #include "hw/sd/sd.h"
  24. #include "qemu/module.h"
  25. #include "trace.h"
  26. static inline const char *sdbus_name(SDBus *sdbus)
  27. {
  28. return sdbus->qbus.name;
  29. }
  30. static SDState *get_card(SDBus *sdbus)
  31. {
  32. /* We only ever have one child on the bus so just return it */
  33. BusChild *kid = QTAILQ_FIRST(&sdbus->qbus.children);
  34. if (!kid) {
  35. return NULL;
  36. }
  37. return SD_CARD(kid->child);
  38. }
  39. uint8_t sdbus_get_dat_lines(SDBus *sdbus)
  40. {
  41. SDState *slave = get_card(sdbus);
  42. uint8_t dat_lines = 0b1111; /* 4 bit bus width */
  43. if (slave) {
  44. SDCardClass *sc = SD_CARD_GET_CLASS(slave);
  45. if (sc->get_dat_lines) {
  46. dat_lines = sc->get_dat_lines(slave);
  47. }
  48. }
  49. trace_sdbus_get_dat_lines(sdbus_name(sdbus), dat_lines);
  50. return dat_lines;
  51. }
  52. bool sdbus_get_cmd_line(SDBus *sdbus)
  53. {
  54. SDState *slave = get_card(sdbus);
  55. bool cmd_line = true;
  56. if (slave) {
  57. SDCardClass *sc = SD_CARD_GET_CLASS(slave);
  58. if (sc->get_cmd_line) {
  59. cmd_line = sc->get_cmd_line(slave);
  60. }
  61. }
  62. trace_sdbus_get_cmd_line(sdbus_name(sdbus), cmd_line);
  63. return cmd_line;
  64. }
  65. void sdbus_set_voltage(SDBus *sdbus, uint16_t millivolts)
  66. {
  67. SDState *card = get_card(sdbus);
  68. trace_sdbus_set_voltage(sdbus_name(sdbus), millivolts);
  69. if (card) {
  70. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  71. assert(sc->set_voltage);
  72. sc->set_voltage(card, millivolts);
  73. }
  74. }
  75. int sdbus_do_command(SDBus *sdbus, SDRequest *req, uint8_t *response)
  76. {
  77. SDState *card = get_card(sdbus);
  78. trace_sdbus_command(sdbus_name(sdbus), req->cmd, req->arg);
  79. if (card) {
  80. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  81. return sc->do_command(card, req, response);
  82. }
  83. return 0;
  84. }
  85. void sdbus_write_data(SDBus *sdbus, uint8_t value)
  86. {
  87. SDState *card = get_card(sdbus);
  88. trace_sdbus_write(sdbus_name(sdbus), value);
  89. if (card) {
  90. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  91. sc->write_data(card, value);
  92. }
  93. }
  94. uint8_t sdbus_read_data(SDBus *sdbus)
  95. {
  96. SDState *card = get_card(sdbus);
  97. uint8_t value = 0;
  98. if (card) {
  99. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  100. value = sc->read_data(card);
  101. }
  102. trace_sdbus_read(sdbus_name(sdbus), value);
  103. return value;
  104. }
  105. bool sdbus_data_ready(SDBus *sdbus)
  106. {
  107. SDState *card = get_card(sdbus);
  108. if (card) {
  109. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  110. return sc->data_ready(card);
  111. }
  112. return false;
  113. }
  114. bool sdbus_get_inserted(SDBus *sdbus)
  115. {
  116. SDState *card = get_card(sdbus);
  117. if (card) {
  118. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  119. return sc->get_inserted(card);
  120. }
  121. return false;
  122. }
  123. bool sdbus_get_readonly(SDBus *sdbus)
  124. {
  125. SDState *card = get_card(sdbus);
  126. if (card) {
  127. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  128. return sc->get_readonly(card);
  129. }
  130. return false;
  131. }
  132. void sdbus_set_inserted(SDBus *sdbus, bool inserted)
  133. {
  134. SDBusClass *sbc = SD_BUS_GET_CLASS(sdbus);
  135. BusState *qbus = BUS(sdbus);
  136. if (sbc->set_inserted) {
  137. sbc->set_inserted(qbus->parent, inserted);
  138. }
  139. }
  140. void sdbus_set_readonly(SDBus *sdbus, bool readonly)
  141. {
  142. SDBusClass *sbc = SD_BUS_GET_CLASS(sdbus);
  143. BusState *qbus = BUS(sdbus);
  144. if (sbc->set_readonly) {
  145. sbc->set_readonly(qbus->parent, readonly);
  146. }
  147. }
  148. void sdbus_reparent_card(SDBus *from, SDBus *to)
  149. {
  150. SDState *card = get_card(from);
  151. SDCardClass *sc;
  152. bool readonly;
  153. /* We directly reparent the card object rather than implementing this
  154. * as a hotpluggable connection because we don't want to expose SD cards
  155. * to users as being hotpluggable, and we can get away with it in this
  156. * limited use case. This could perhaps be implemented more cleanly in
  157. * future by adding support to the hotplug infrastructure for "device
  158. * can be hotplugged only via code, not by user".
  159. */
  160. if (!card) {
  161. return;
  162. }
  163. sc = SD_CARD_GET_CLASS(card);
  164. readonly = sc->get_readonly(card);
  165. sdbus_set_inserted(from, false);
  166. qdev_set_parent_bus(DEVICE(card), &to->qbus);
  167. sdbus_set_inserted(to, true);
  168. sdbus_set_readonly(to, readonly);
  169. }
  170. static const TypeInfo sd_bus_info = {
  171. .name = TYPE_SD_BUS,
  172. .parent = TYPE_BUS,
  173. .instance_size = sizeof(SDBus),
  174. .class_size = sizeof(SDBusClass),
  175. };
  176. static void sd_bus_register_types(void)
  177. {
  178. type_register_static(&sd_bus_info);
  179. }
  180. type_init(sd_bus_register_types)