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"
25 #include "qemu/module.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 uint8_t sdbus_get_dat_lines(SDBus
*sdbus
)
46 SDState
*slave
= get_card(sdbus
);
47 uint8_t dat_lines
= 0b1111; /* 4 bit bus width */
50 SDCardClass
*sc
= SD_CARD_GET_CLASS(slave
);
52 if (sc
->get_dat_lines
) {
53 dat_lines
= sc
->get_dat_lines(slave
);
56 trace_sdbus_get_dat_lines(sdbus_name(sdbus
), dat_lines
);
61 bool sdbus_get_cmd_line(SDBus
*sdbus
)
63 SDState
*slave
= get_card(sdbus
);
67 SDCardClass
*sc
= SD_CARD_GET_CLASS(slave
);
69 if (sc
->get_cmd_line
) {
70 cmd_line
= sc
->get_cmd_line(slave
);
73 trace_sdbus_get_cmd_line(sdbus_name(sdbus
), cmd_line
);
78 void sdbus_set_voltage(SDBus
*sdbus
, uint16_t millivolts
)
80 SDState
*card
= get_card(sdbus
);
82 trace_sdbus_set_voltage(sdbus_name(sdbus
), millivolts
);
84 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
86 assert(sc
->set_voltage
);
87 sc
->set_voltage(card
, millivolts
);
91 int sdbus_do_command(SDBus
*sdbus
, SDRequest
*req
, uint8_t *response
)
93 SDState
*card
= get_card(sdbus
);
95 trace_sdbus_command(sdbus_name(sdbus
), req
->cmd
, req
->arg
);
97 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
99 return sc
->do_command(card
, req
, response
);
105 void sdbus_write_data(SDBus
*sdbus
, uint8_t value
)
107 SDState
*card
= get_card(sdbus
);
109 trace_sdbus_write(sdbus_name(sdbus
), value
);
111 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
113 sc
->write_data(card
, value
);
117 uint8_t sdbus_read_data(SDBus
*sdbus
)
119 SDState
*card
= get_card(sdbus
);
123 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
125 value
= sc
->read_data(card
);
127 trace_sdbus_read(sdbus_name(sdbus
), value
);
132 bool sdbus_data_ready(SDBus
*sdbus
)
134 SDState
*card
= get_card(sdbus
);
137 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
139 return sc
->data_ready(card
);
145 bool sdbus_get_inserted(SDBus
*sdbus
)
147 SDState
*card
= get_card(sdbus
);
150 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
152 return sc
->get_inserted(card
);
158 bool sdbus_get_readonly(SDBus
*sdbus
)
160 SDState
*card
= get_card(sdbus
);
163 SDCardClass
*sc
= SD_CARD_GET_CLASS(card
);
165 return sc
->get_readonly(card
);
171 void sdbus_set_inserted(SDBus
*sdbus
, bool inserted
)
173 SDBusClass
*sbc
= SD_BUS_GET_CLASS(sdbus
);
174 BusState
*qbus
= BUS(sdbus
);
176 if (sbc
->set_inserted
) {
177 sbc
->set_inserted(qbus
->parent
, inserted
);
181 void sdbus_set_readonly(SDBus
*sdbus
, bool readonly
)
183 SDBusClass
*sbc
= SD_BUS_GET_CLASS(sdbus
);
184 BusState
*qbus
= BUS(sdbus
);
186 if (sbc
->set_readonly
) {
187 sbc
->set_readonly(qbus
->parent
, readonly
);
191 void sdbus_reparent_card(SDBus
*from
, SDBus
*to
)
193 SDState
*card
= get_card(from
);
197 /* We directly reparent the card object rather than implementing this
198 * as a hotpluggable connection because we don't want to expose SD cards
199 * to users as being hotpluggable, and we can get away with it in this
200 * limited use case. This could perhaps be implemented more cleanly in
201 * future by adding support to the hotplug infrastructure for "device
202 * can be hotplugged only via code, not by user".
209 sc
= SD_CARD_GET_CLASS(card
);
210 readonly
= sc
->get_readonly(card
);
212 sdbus_set_inserted(from
, false);
213 qdev_set_parent_bus(DEVICE(card
), &to
->qbus
);
214 sdbus_set_inserted(to
, true);
215 sdbus_set_readonly(to
, readonly
);
218 static const TypeInfo sd_bus_info
= {
221 .instance_size
= sizeof(SDBus
),
222 .class_size
= sizeof(SDBusClass
),
225 static void sd_bus_register_types(void)
227 type_register_static(&sd_bus_info
);
230 type_init(sd_bus_register_types
)