Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / hw / sd / core.c
blob30ee62c510690658e354aade847bc5c8eb578033
1 /*
2 * SD card bus interface code.
4 * Copyright (c) 2015 Linaro Limited
6 * Author:
7 * Peter Maydell <peter.maydell@linaro.org>
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.
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.
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/>.
22 #include "qemu/osdep.h"
23 #include "hw/qdev-core.h"
24 #include "hw/sd/sd.h"
25 #include "qemu/module.h"
26 #include "qapi/error.h"
27 #include "trace.h"
29 static inline const char *sdbus_name(SDBus *sdbus)
31 return sdbus->qbus.name;
34 static SDState *get_card(SDBus *sdbus)
36 /* We only ever have one child on the bus so just return it */
37 BusChild *kid = QTAILQ_FIRST(&sdbus->qbus.children);
39 if (!kid) {
40 return NULL;
42 return SD_CARD(kid->child);
45 uint8_t sdbus_get_dat_lines(SDBus *sdbus)
47 SDState *slave = get_card(sdbus);
48 uint8_t dat_lines = 0b1111; /* 4 bit bus width */
50 if (slave) {
51 SDCardClass *sc = SD_CARD_GET_CLASS(slave);
53 if (sc->get_dat_lines) {
54 dat_lines = sc->get_dat_lines(slave);
57 trace_sdbus_get_dat_lines(sdbus_name(sdbus), dat_lines);
59 return dat_lines;
62 bool sdbus_get_cmd_line(SDBus *sdbus)
64 SDState *slave = get_card(sdbus);
65 bool cmd_line = true;
67 if (slave) {
68 SDCardClass *sc = SD_CARD_GET_CLASS(slave);
70 if (sc->get_cmd_line) {
71 cmd_line = sc->get_cmd_line(slave);
74 trace_sdbus_get_cmd_line(sdbus_name(sdbus), cmd_line);
76 return cmd_line;
79 void sdbus_set_voltage(SDBus *sdbus, uint16_t millivolts)
81 SDState *card = get_card(sdbus);
83 trace_sdbus_set_voltage(sdbus_name(sdbus), millivolts);
84 if (card) {
85 SDCardClass *sc = SD_CARD_GET_CLASS(card);
87 assert(sc->set_voltage);
88 sc->set_voltage(card, millivolts);
92 int sdbus_do_command(SDBus *sdbus, SDRequest *req, uint8_t *response)
94 SDState *card = get_card(sdbus);
96 trace_sdbus_command(sdbus_name(sdbus), req->cmd, req->arg);
97 if (card) {
98 SDCardClass *sc = SD_CARD_GET_CLASS(card);
100 return sc->do_command(card, req, response);
103 return 0;
106 void sdbus_write_byte(SDBus *sdbus, uint8_t value)
108 SDState *card = get_card(sdbus);
110 trace_sdbus_write(sdbus_name(sdbus), value);
111 if (card) {
112 SDCardClass *sc = SD_CARD_GET_CLASS(card);
114 sc->write_byte(card, value);
118 void sdbus_write_data(SDBus *sdbus, const void *buf, size_t length)
120 SDState *card = get_card(sdbus);
121 const uint8_t *data = buf;
123 if (card) {
124 SDCardClass *sc = SD_CARD_GET_CLASS(card);
126 for (size_t i = 0; i < length; i++) {
127 trace_sdbus_write(sdbus_name(sdbus), data[i]);
128 sc->write_byte(card, data[i]);
133 uint8_t sdbus_read_byte(SDBus *sdbus)
135 SDState *card = get_card(sdbus);
136 uint8_t value = 0;
138 if (card) {
139 SDCardClass *sc = SD_CARD_GET_CLASS(card);
141 value = sc->read_byte(card);
143 trace_sdbus_read(sdbus_name(sdbus), value);
145 return value;
148 void sdbus_read_data(SDBus *sdbus, void *buf, size_t length)
150 SDState *card = get_card(sdbus);
151 uint8_t *data = buf;
153 if (card) {
154 SDCardClass *sc = SD_CARD_GET_CLASS(card);
156 for (size_t i = 0; i < length; i++) {
157 data[i] = sc->read_byte(card);
158 trace_sdbus_read(sdbus_name(sdbus), data[i]);
163 bool sdbus_receive_ready(SDBus *sdbus)
165 SDState *card = get_card(sdbus);
167 if (card) {
168 SDCardClass *sc = SD_CARD_GET_CLASS(card);
170 return sc->receive_ready(card);
173 return false;
176 bool sdbus_data_ready(SDBus *sdbus)
178 SDState *card = get_card(sdbus);
180 if (card) {
181 SDCardClass *sc = SD_CARD_GET_CLASS(card);
183 return sc->data_ready(card);
186 return false;
189 bool sdbus_get_inserted(SDBus *sdbus)
191 SDState *card = get_card(sdbus);
193 if (card) {
194 SDCardClass *sc = SD_CARD_GET_CLASS(card);
196 return sc->get_inserted(card);
199 return false;
202 bool sdbus_get_readonly(SDBus *sdbus)
204 SDState *card = get_card(sdbus);
206 if (card) {
207 SDCardClass *sc = SD_CARD_GET_CLASS(card);
209 return sc->get_readonly(card);
212 return false;
215 void sdbus_set_inserted(SDBus *sdbus, bool inserted)
217 SDBusClass *sbc = SD_BUS_GET_CLASS(sdbus);
218 BusState *qbus = BUS(sdbus);
220 if (sbc->set_inserted) {
221 sbc->set_inserted(qbus->parent, inserted);
225 void sdbus_set_readonly(SDBus *sdbus, bool readonly)
227 SDBusClass *sbc = SD_BUS_GET_CLASS(sdbus);
228 BusState *qbus = BUS(sdbus);
230 if (sbc->set_readonly) {
231 sbc->set_readonly(qbus->parent, readonly);
235 void sdbus_reparent_card(SDBus *from, SDBus *to)
237 SDState *card = get_card(from);
238 SDCardClass *sc;
239 bool readonly;
241 /* We directly reparent the card object rather than implementing this
242 * as a hotpluggable connection because we don't want to expose SD cards
243 * to users as being hotpluggable, and we can get away with it in this
244 * limited use case. This could perhaps be implemented more cleanly in
245 * future by adding support to the hotplug infrastructure for "device
246 * can be hotplugged only via code, not by user".
249 if (!card) {
250 return;
253 sc = SD_CARD_GET_CLASS(card);
254 readonly = sc->get_readonly(card);
256 sdbus_set_inserted(from, false);
257 qdev_set_parent_bus(DEVICE(card), &to->qbus, &error_abort);
258 sdbus_set_inserted(to, true);
259 sdbus_set_readonly(to, readonly);
262 static const TypeInfo sd_bus_info = {
263 .name = TYPE_SD_BUS,
264 .parent = TYPE_BUS,
265 .instance_size = sizeof(SDBus),
266 .class_size = sizeof(SDBusClass),
269 static void sd_bus_register_types(void)
271 type_register_static(&sd_bus_info);
274 type_init(sd_bus_register_types)