2 * SD card bus interface code.
4 * Copyright (c) 2015 Linaro Limited
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
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"
28 static inline const char *sdbus_name(SDBus
*sdbus
)
30 return sdbus
->qbus
.name
;
33 static SDState
*get_card(SDBus
*sdbus
)
35 /* We only ever have one child on the bus so just return it */
36 BusChild
*kid
= QTAILQ_FIRST(&sdbus
->qbus
.children
);
41 return SD_CARD(kid
->child
);
44 void sdbus_set_voltage(SDBus
*sdbus
, uint16_t millivolts
)
46 SDState
*card
= get_card(sdbus
);
48 trace_sdbus_set_voltage(sdbus_name(sdbus
), millivolts
);
50 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
52 assert(sc
->set_voltage
);
53 sc
->set_voltage(card
, millivolts
);
57 int sdbus_do_command(SDBus
*sdbus
, SDRequest
*req
, uint8_t *response
)
59 SDState
*card
= get_card(sdbus
);
61 trace_sdbus_command(sdbus_name(sdbus
), req
->cmd
, req
->arg
, req
->crc
);
63 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
65 return sc
->do_command(card
, req
, response
);
71 void sdbus_write_data(SDBus
*sdbus
, uint8_t value
)
73 SDState
*card
= get_card(sdbus
);
75 trace_sdbus_write(sdbus_name(sdbus
), value
);
77 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
79 sc
->write_data(card
, value
);
83 uint8_t sdbus_read_data(SDBus
*sdbus
)
85 SDState
*card
= get_card(sdbus
);
89 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
91 value
= sc
->read_data(card
);
93 trace_sdbus_read(sdbus_name(sdbus
), value
);
98 bool sdbus_data_ready(SDBus
*sdbus
)
100 SDState
*card
= get_card(sdbus
);
103 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
105 return sc
->data_ready(card
);
111 bool sdbus_get_inserted(SDBus
*sdbus
)
113 SDState
*card
= get_card(sdbus
);
116 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
118 return sc
->get_inserted(card
);
124 bool sdbus_get_readonly(SDBus
*sdbus
)
126 SDState
*card
= get_card(sdbus
);
129 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
131 return sc
->get_readonly(card
);
137 void sdbus_set_inserted(SDBus
*sdbus
, bool inserted
)
139 SDBusClass
*sbc
= SD_BUS_GET_CLASS(sdbus
);
140 BusState
*qbus
= BUS(sdbus
);
142 if (sbc
->set_inserted
) {
143 sbc
->set_inserted(qbus
->parent
, inserted
);
147 void sdbus_set_readonly(SDBus
*sdbus
, bool readonly
)
149 SDBusClass
*sbc
= SD_BUS_GET_CLASS(sdbus
);
150 BusState
*qbus
= BUS(sdbus
);
152 if (sbc
->set_readonly
) {
153 sbc
->set_readonly(qbus
->parent
, readonly
);
157 void sdbus_reparent_card(SDBus
*from
, SDBus
*to
)
159 SDState
*card
= get_card(from
);
163 /* We directly reparent the card object rather than implementing this
164 * as a hotpluggable connection because we don't want to expose SD cards
165 * to users as being hotpluggable, and we can get away with it in this
166 * limited use case. This could perhaps be implemented more cleanly in
167 * future by adding support to the hotplug infrastructure for "device
168 * can be hotplugged only via code, not by user".
175 sc
= SD_CARD_GET_CLASS(card
);
176 readonly
= sc
->get_readonly(card
);
178 sdbus_set_inserted(from
, false);
179 qdev_set_parent_bus(DEVICE(card
), &to
->qbus
);
180 sdbus_set_inserted(to
, true);
181 sdbus_set_readonly(to
, readonly
);
184 static const TypeInfo sd_bus_info
= {
187 .instance_size
= sizeof(SDBus
),
188 .class_size
= sizeof(SDBusClass
),
191 static void sd_bus_register_types(void)
193 type_register_static(&sd_bus_info
);
196 type_init(sd_bus_register_types
)