2
0

core.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "qapi/error.h"
  26. #include "trace.h"
  27. static inline const char *sdbus_name(SDBus *sdbus)
  28. {
  29. return sdbus->qbus.name;
  30. }
  31. static SDState *get_card(SDBus *sdbus)
  32. {
  33. /* We only ever have one child on the bus so just return it */
  34. BusChild *kid = QTAILQ_FIRST(&sdbus->qbus.children);
  35. if (!kid) {
  36. return NULL;
  37. }
  38. return SD_CARD(kid->child);
  39. }
  40. uint8_t sdbus_get_dat_lines(SDBus *sdbus)
  41. {
  42. SDState *slave = get_card(sdbus);
  43. uint8_t dat_lines = 0b1111; /* 4 bit bus width */
  44. if (slave) {
  45. SDCardClass *sc = SD_CARD_GET_CLASS(slave);
  46. if (sc->get_dat_lines) {
  47. dat_lines = sc->get_dat_lines(slave);
  48. }
  49. }
  50. trace_sdbus_get_dat_lines(sdbus_name(sdbus), dat_lines);
  51. return dat_lines;
  52. }
  53. bool sdbus_get_cmd_line(SDBus *sdbus)
  54. {
  55. SDState *slave = get_card(sdbus);
  56. bool cmd_line = true;
  57. if (slave) {
  58. SDCardClass *sc = SD_CARD_GET_CLASS(slave);
  59. if (sc->get_cmd_line) {
  60. cmd_line = sc->get_cmd_line(slave);
  61. }
  62. }
  63. trace_sdbus_get_cmd_line(sdbus_name(sdbus), cmd_line);
  64. return cmd_line;
  65. }
  66. void sdbus_set_voltage(SDBus *sdbus, uint16_t millivolts)
  67. {
  68. SDState *card = get_card(sdbus);
  69. trace_sdbus_set_voltage(sdbus_name(sdbus), millivolts);
  70. if (card) {
  71. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  72. assert(sc->set_voltage);
  73. sc->set_voltage(card, millivolts);
  74. }
  75. }
  76. int sdbus_do_command(SDBus *sdbus, SDRequest *req, uint8_t *response)
  77. {
  78. SDState *card = get_card(sdbus);
  79. trace_sdbus_command(sdbus_name(sdbus), req->cmd, req->arg);
  80. if (card) {
  81. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  82. return sc->do_command(card, req, response);
  83. }
  84. return 0;
  85. }
  86. void sdbus_write_byte(SDBus *sdbus, uint8_t value)
  87. {
  88. SDState *card = get_card(sdbus);
  89. trace_sdbus_write(sdbus_name(sdbus), value);
  90. if (card) {
  91. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  92. sc->write_byte(card, value);
  93. }
  94. }
  95. void sdbus_write_data(SDBus *sdbus, const void *buf, size_t length)
  96. {
  97. SDState *card = get_card(sdbus);
  98. const uint8_t *data = buf;
  99. if (card) {
  100. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  101. for (size_t i = 0; i < length; i++) {
  102. trace_sdbus_write(sdbus_name(sdbus), data[i]);
  103. sc->write_byte(card, data[i]);
  104. }
  105. }
  106. }
  107. uint8_t sdbus_read_byte(SDBus *sdbus)
  108. {
  109. SDState *card = get_card(sdbus);
  110. uint8_t value = 0;
  111. if (card) {
  112. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  113. value = sc->read_byte(card);
  114. }
  115. trace_sdbus_read(sdbus_name(sdbus), value);
  116. return value;
  117. }
  118. void sdbus_read_data(SDBus *sdbus, void *buf, size_t length)
  119. {
  120. SDState *card = get_card(sdbus);
  121. uint8_t *data = buf;
  122. if (card) {
  123. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  124. for (size_t i = 0; i < length; i++) {
  125. data[i] = sc->read_byte(card);
  126. trace_sdbus_read(sdbus_name(sdbus), data[i]);
  127. }
  128. }
  129. }
  130. bool sdbus_data_ready(SDBus *sdbus)
  131. {
  132. SDState *card = get_card(sdbus);
  133. if (card) {
  134. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  135. return sc->data_ready(card);
  136. }
  137. return false;
  138. }
  139. bool sdbus_get_inserted(SDBus *sdbus)
  140. {
  141. SDState *card = get_card(sdbus);
  142. if (card) {
  143. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  144. return sc->get_inserted(card);
  145. }
  146. return false;
  147. }
  148. bool sdbus_get_readonly(SDBus *sdbus)
  149. {
  150. SDState *card = get_card(sdbus);
  151. if (card) {
  152. SDCardClass *sc = SD_CARD_GET_CLASS(card);
  153. return sc->get_readonly(card);
  154. }
  155. return false;
  156. }
  157. void sdbus_set_inserted(SDBus *sdbus, bool inserted)
  158. {
  159. SDBusClass *sbc = SD_BUS_GET_CLASS(sdbus);
  160. BusState *qbus = BUS(sdbus);
  161. if (sbc->set_inserted) {
  162. sbc->set_inserted(qbus->parent, inserted);
  163. }
  164. }
  165. void sdbus_set_readonly(SDBus *sdbus, bool readonly)
  166. {
  167. SDBusClass *sbc = SD_BUS_GET_CLASS(sdbus);
  168. BusState *qbus = BUS(sdbus);
  169. if (sbc->set_readonly) {
  170. sbc->set_readonly(qbus->parent, readonly);
  171. }
  172. }
  173. void sdbus_reparent_card(SDBus *from, SDBus *to)
  174. {
  175. SDState *card = get_card(from);
  176. SDCardClass *sc;
  177. bool readonly;
  178. /* We directly reparent the card object rather than implementing this
  179. * as a hotpluggable connection because we don't want to expose SD cards
  180. * to users as being hotpluggable, and we can get away with it in this
  181. * limited use case. This could perhaps be implemented more cleanly in
  182. * future by adding support to the hotplug infrastructure for "device
  183. * can be hotplugged only via code, not by user".
  184. */
  185. if (!card) {
  186. return;
  187. }
  188. sc = SD_CARD_GET_CLASS(card);
  189. readonly = sc->get_readonly(card);
  190. sdbus_set_inserted(from, false);
  191. qdev_set_parent_bus(DEVICE(card), &to->qbus, &error_abort);
  192. sdbus_set_inserted(to, true);
  193. sdbus_set_readonly(to, readonly);
  194. }
  195. static const TypeInfo sd_bus_info = {
  196. .name = TYPE_SD_BUS,
  197. .parent = TYPE_BUS,
  198. .instance_size = sizeof(SDBus),
  199. .class_size = sizeof(SDBusClass),
  200. };
  201. static void sd_bus_register_types(void)
  202. {
  203. type_register_static(&sd_bus_info);
  204. }
  205. type_init(sd_bus_register_types)