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