target/sh4: implement tas.b using atomic helper
[qemu/ar7.git] / hw / sd / core.c
blob295dc44ab7889820bcf0de192a6305363df3c1dc
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 "sysemu/block-backend.h"
25 #include "hw/sd/sd.h"
27 static SDState *get_card(SDBus *sdbus)
29 /* We only ever have one child on the bus so just return it */
30 BusChild *kid = QTAILQ_FIRST(&sdbus->qbus.children);
32 if (!kid) {
33 return NULL;
35 return SD_CARD(kid->child);
38 int sdbus_do_command(SDBus *sdbus, SDRequest *req, uint8_t *response)
40 SDState *card = get_card(sdbus);
42 if (card) {
43 SDCardClass *sc = SD_CARD_GET_CLASS(card);
45 return sc->do_command(card, req, response);
48 return 0;
51 void sdbus_write_data(SDBus *sdbus, uint8_t value)
53 SDState *card = get_card(sdbus);
55 if (card) {
56 SDCardClass *sc = SD_CARD_GET_CLASS(card);
58 sc->write_data(card, value);
62 uint8_t sdbus_read_data(SDBus *sdbus)
64 SDState *card = get_card(sdbus);
66 if (card) {
67 SDCardClass *sc = SD_CARD_GET_CLASS(card);
69 return sc->read_data(card);
72 return 0;
75 bool sdbus_data_ready(SDBus *sdbus)
77 SDState *card = get_card(sdbus);
79 if (card) {
80 SDCardClass *sc = SD_CARD_GET_CLASS(card);
82 return sc->data_ready(card);
85 return false;
88 bool sdbus_get_inserted(SDBus *sdbus)
90 SDState *card = get_card(sdbus);
92 if (card) {
93 SDCardClass *sc = SD_CARD_GET_CLASS(card);
95 return sc->get_inserted(card);
98 return false;
101 bool sdbus_get_readonly(SDBus *sdbus)
103 SDState *card = get_card(sdbus);
105 if (card) {
106 SDCardClass *sc = SD_CARD_GET_CLASS(card);
108 return sc->get_readonly(card);
111 return false;
114 void sdbus_set_inserted(SDBus *sdbus, bool inserted)
116 SDBusClass *sbc = SD_BUS_GET_CLASS(sdbus);
117 BusState *qbus = BUS(sdbus);
119 if (sbc->set_inserted) {
120 sbc->set_inserted(qbus->parent, inserted);
124 void sdbus_set_readonly(SDBus *sdbus, bool readonly)
126 SDBusClass *sbc = SD_BUS_GET_CLASS(sdbus);
127 BusState *qbus = BUS(sdbus);
129 if (sbc->set_readonly) {
130 sbc->set_readonly(qbus->parent, readonly);
134 void sdbus_reparent_card(SDBus *from, SDBus *to)
136 SDState *card = get_card(from);
137 SDCardClass *sc;
138 bool readonly;
140 /* We directly reparent the card object rather than implementing this
141 * as a hotpluggable connection because we don't want to expose SD cards
142 * to users as being hotpluggable, and we can get away with it in this
143 * limited use case. This could perhaps be implemented more cleanly in
144 * future by adding support to the hotplug infrastructure for "device
145 * can be hotplugged only via code, not by user".
148 if (!card) {
149 return;
152 sc = SD_CARD_GET_CLASS(card);
153 readonly = sc->get_readonly(card);
155 sdbus_set_inserted(from, false);
156 qdev_set_parent_bus(DEVICE(card), &to->qbus);
157 sdbus_set_inserted(to, true);
158 sdbus_set_readonly(to, readonly);
161 static const TypeInfo sd_bus_info = {
162 .name = TYPE_SD_BUS,
163 .parent = TYPE_BUS,
164 .instance_size = sizeof(SDBus),
165 .class_size = sizeof(SDBusClass),
168 static void sd_bus_register_types(void)
170 type_register_static(&sd_bus_info);
173 type_init(sd_bus_register_types)