File: | hw/sd/sdhci.c |
Location: | line 646, column 24 |
Description: | The left operand of '&' is a garbage value |
1 | /* | |||
2 | * SD Association Host Standard Specification v2.0 controller emulation | |||
3 | * | |||
4 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. | |||
5 | * Mitsyanko Igor <i.mitsyanko@samsung.com> | |||
6 | * Peter A.G. Crosthwaite <peter.crosthwaite@petalogix.com> | |||
7 | * | |||
8 | * Based on MMC controller for Samsung S5PC1xx-based board emulation | |||
9 | * by Alexey Merkulov and Vladimir Monakhov. | |||
10 | * | |||
11 | * This program is free software; you can redistribute it and/or modify it | |||
12 | * under the terms of the GNU General Public License as published by the | |||
13 | * Free Software Foundation; either version 2 of the License, or (at your | |||
14 | * option) any later version. | |||
15 | * | |||
16 | * This program is distributed in the hope that it will be useful, | |||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |||
19 | * See the GNU General Public License for more details. | |||
20 | * | |||
21 | * You should have received a copy of the GNU General Public License along | |||
22 | * with this program; if not, see <http://www.gnu.org/licenses/>. | |||
23 | */ | |||
24 | ||||
25 | #include "hw/hw.h" | |||
26 | #include "sysemu/blockdev.h" | |||
27 | #include "sysemu/dma.h" | |||
28 | #include "qemu/timer.h" | |||
29 | #include "block/block_int.h" | |||
30 | #include "qemu/bitops.h" | |||
31 | ||||
32 | #include "sdhci.h" | |||
33 | ||||
34 | /* host controller debug messages */ | |||
35 | #ifndef SDHC_DEBUG0 | |||
36 | #define SDHC_DEBUG0 0 | |||
37 | #endif | |||
38 | ||||
39 | #if SDHC_DEBUG0 == 0 | |||
40 | #define DPRINT_L1(fmt, args...)do { } while (0) do { } while (0) | |||
41 | #define DPRINT_L2(fmt, args...)do { } while (0) do { } while (0) | |||
42 | #define ERRPRINT(fmt, args...)do { } while (0) do { } while (0) | |||
43 | #elif SDHC_DEBUG0 == 1 | |||
44 | #define DPRINT_L1(fmt, args...)do { } while (0) \ | |||
45 | do {fprintf(stderrstderr, "QEMU SDHC: "fmt, ## args); } while (0) | |||
46 | #define DPRINT_L2(fmt, args...)do { } while (0) do { } while (0) | |||
47 | #define ERRPRINT(fmt, args...)do { } while (0) \ | |||
48 | do {fprintf(stderrstderr, "QEMU SDHC ERROR: "fmt, ## args); } while (0) | |||
49 | #else | |||
50 | #define DPRINT_L1(fmt, args...)do { } while (0) \ | |||
51 | do {fprintf(stderrstderr, "QEMU SDHC: "fmt, ## args); } while (0) | |||
52 | #define DPRINT_L2(fmt, args...)do { } while (0) \ | |||
53 | do {fprintf(stderrstderr, "QEMU SDHC: "fmt, ## args); } while (0) | |||
54 | #define ERRPRINT(fmt, args...)do { } while (0) \ | |||
55 | do {fprintf(stderrstderr, "QEMU SDHC ERROR: "fmt, ## args); } while (0) | |||
56 | #endif | |||
57 | ||||
58 | /* Default SD/MMC host controller features information, which will be | |||
59 | * presented in CAPABILITIES register of generic SD host controller at reset. | |||
60 | * If not stated otherwise: | |||
61 | * 0 - not supported, 1 - supported, other - prohibited. | |||
62 | */ | |||
63 | #define SDHC_CAPAB_64BITBUS0ul 0ul /* 64-bit System Bus Support */ | |||
64 | #define SDHC_CAPAB_18V1ul 1ul /* Voltage support 1.8v */ | |||
65 | #define SDHC_CAPAB_30V0ul 0ul /* Voltage support 3.0v */ | |||
66 | #define SDHC_CAPAB_33V1ul 1ul /* Voltage support 3.3v */ | |||
67 | #define SDHC_CAPAB_SUSPRESUME0ul 0ul /* Suspend/resume support */ | |||
68 | #define SDHC_CAPAB_SDMA1ul 1ul /* SDMA support */ | |||
69 | #define SDHC_CAPAB_HIGHSPEED1ul 1ul /* High speed support */ | |||
70 | #define SDHC_CAPAB_ADMA11ul 1ul /* ADMA1 support */ | |||
71 | #define SDHC_CAPAB_ADMA21ul 1ul /* ADMA2 support */ | |||
72 | /* Maximum host controller R/W buffers size | |||
73 | * Possible values: 512, 1024, 2048 bytes */ | |||
74 | #define SDHC_CAPAB_MAXBLOCKLENGTH512ul 512ul | |||
75 | /* Maximum clock frequency for SDclock in MHz | |||
76 | * value in range 10-63 MHz, 0 - not defined */ | |||
77 | #define SDHC_CAPAB_BASECLKFREQ0ul 0ul | |||
78 | #define SDHC_CAPAB_TOUNIT1ul 1ul /* Timeout clock unit 0 - kHz, 1 - MHz */ | |||
79 | /* Timeout clock frequency 1-63, 0 - not defined */ | |||
80 | #define SDHC_CAPAB_TOCLKFREQ0ul 0ul | |||
81 | ||||
82 | /* Now check all parameters and calculate CAPABILITIES REGISTER value */ | |||
83 | #if SDHC_CAPAB_64BITBUS0ul > 1 || SDHC_CAPAB_18V1ul > 1 || SDHC_CAPAB_30V0ul > 1 || \ | |||
84 | SDHC_CAPAB_33V1ul > 1 || SDHC_CAPAB_SUSPRESUME0ul > 1 || SDHC_CAPAB_SDMA1ul > 1 || \ | |||
85 | SDHC_CAPAB_HIGHSPEED1ul > 1 || SDHC_CAPAB_ADMA21ul > 1 || SDHC_CAPAB_ADMA11ul > 1 ||\ | |||
86 | SDHC_CAPAB_TOUNIT1ul > 1 | |||
87 | #error Capabilities features can have value 0 or 1 only! | |||
88 | #endif | |||
89 | ||||
90 | #if SDHC_CAPAB_MAXBLOCKLENGTH512ul == 512 | |||
91 | #define MAX_BLOCK_LENGTH0ul 0ul | |||
92 | #elif SDHC_CAPAB_MAXBLOCKLENGTH512ul == 1024 | |||
93 | #define MAX_BLOCK_LENGTH0ul 1ul | |||
94 | #elif SDHC_CAPAB_MAXBLOCKLENGTH512ul == 2048 | |||
95 | #define MAX_BLOCK_LENGTH0ul 2ul | |||
96 | #else | |||
97 | #error Max host controller block size can have value 512, 1024 or 2048 only! | |||
98 | #endif | |||
99 | ||||
100 | #if (SDHC_CAPAB_BASECLKFREQ0ul > 0 && SDHC_CAPAB_BASECLKFREQ0ul < 10) || \ | |||
101 | SDHC_CAPAB_BASECLKFREQ0ul > 63 | |||
102 | #error SDclock frequency can have value in range 0, 10-63 only! | |||
103 | #endif | |||
104 | ||||
105 | #if SDHC_CAPAB_TOCLKFREQ0ul > 63 | |||
106 | #error Timeout clock frequency can have value in range 0-63 only! | |||
107 | #endif | |||
108 | ||||
109 | #define SDHC_CAPAB_REG_DEFAULT((0ul << 28) | (1ul << 26) | (0ul << 25) | ( 1ul << 24) | (0ul << 23) | (1ul << 22) | (1ul << 21) | (1ul << 20) | (1ul << 19) | (0ul << 16) | (0ul << 8) | (1ul << 7) | (0ul)) \ | |||
110 | ((SDHC_CAPAB_64BITBUS0ul << 28) | (SDHC_CAPAB_18V1ul << 26) | \ | |||
111 | (SDHC_CAPAB_30V0ul << 25) | (SDHC_CAPAB_33V1ul << 24) | \ | |||
112 | (SDHC_CAPAB_SUSPRESUME0ul << 23) | (SDHC_CAPAB_SDMA1ul << 22) | \ | |||
113 | (SDHC_CAPAB_HIGHSPEED1ul << 21) | (SDHC_CAPAB_ADMA11ul << 20) | \ | |||
114 | (SDHC_CAPAB_ADMA21ul << 19) | (MAX_BLOCK_LENGTH0ul << 16) | \ | |||
115 | (SDHC_CAPAB_BASECLKFREQ0ul << 8) | (SDHC_CAPAB_TOUNIT1ul << 7) | \ | |||
116 | (SDHC_CAPAB_TOCLKFREQ0ul)) | |||
117 | ||||
118 | #define MASKED_WRITE(reg, mask, val)(reg = (reg & (mask)) | (val)) (reg = (reg & (mask)) | (val)) | |||
119 | ||||
120 | static uint8_t sdhci_slotint(SDHCIState *s) | |||
121 | { | |||
122 | return (s->norintsts & s->norintsigen) || (s->errintsts & s->errintsigen) || | |||
123 | ((s->norintsts & SDHC_NIS_INSERT0x0040) && (s->wakcon & SDHC_WKUP_ON_INS(1 << 1))) || | |||
124 | ((s->norintsts & SDHC_NIS_REMOVE0x0080) && (s->wakcon & SDHC_WKUP_ON_RMV(1 << 2))); | |||
125 | } | |||
126 | ||||
127 | static inline void sdhci_update_irq(SDHCIState *s) | |||
128 | { | |||
129 | qemu_set_irq(s->irq, sdhci_slotint(s)); | |||
130 | } | |||
131 | ||||
132 | static void sdhci_raise_insertion_irq(void *opaque) | |||
133 | { | |||
134 | SDHCIState *s = (SDHCIState *)opaque; | |||
135 | ||||
136 | if (s->norintsts & SDHC_NIS_REMOVE0x0080) { | |||
137 | timer_mod(s->insert_timer, | |||
138 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY(get_ticks_per_sec())); | |||
139 | } else { | |||
140 | s->prnsts = 0x1ff0000; | |||
141 | if (s->norintstsen & SDHC_NISEN_INSERT0x0040) { | |||
142 | s->norintsts |= SDHC_NIS_INSERT0x0040; | |||
143 | } | |||
144 | sdhci_update_irq(s); | |||
145 | } | |||
146 | } | |||
147 | ||||
148 | static void sdhci_insert_eject_cb(void *opaque, int irq, int level) | |||
149 | { | |||
150 | SDHCIState *s = (SDHCIState *)opaque; | |||
151 | DPRINT_L1("Card state changed: %s!\n", level ? "insert" : "eject")do { } while (0); | |||
152 | ||||
153 | if ((s->norintsts & SDHC_NIS_REMOVE0x0080) && level) { | |||
154 | /* Give target some time to notice card ejection */ | |||
155 | timer_mod(s->insert_timer, | |||
156 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_INSERTION_DELAY(get_ticks_per_sec())); | |||
157 | } else { | |||
158 | if (level) { | |||
159 | s->prnsts = 0x1ff0000; | |||
160 | if (s->norintstsen & SDHC_NISEN_INSERT0x0040) { | |||
161 | s->norintsts |= SDHC_NIS_INSERT0x0040; | |||
162 | } | |||
163 | } else { | |||
164 | s->prnsts = 0x1fa0000; | |||
165 | s->pwrcon &= ~SDHC_POWER_ON(1 << 0); | |||
166 | s->clkcon &= ~SDHC_CLOCK_SDCLK_EN(1 << 2); | |||
167 | if (s->norintstsen & SDHC_NISEN_REMOVE0x0080) { | |||
168 | s->norintsts |= SDHC_NIS_REMOVE0x0080; | |||
169 | } | |||
170 | } | |||
171 | sdhci_update_irq(s); | |||
172 | } | |||
173 | } | |||
174 | ||||
175 | static void sdhci_card_readonly_cb(void *opaque, int irq, int level) | |||
176 | { | |||
177 | SDHCIState *s = (SDHCIState *)opaque; | |||
178 | ||||
179 | if (level) { | |||
180 | s->prnsts &= ~SDHC_WRITE_PROTECT0x00080000; | |||
181 | } else { | |||
182 | /* Write enabled */ | |||
183 | s->prnsts |= SDHC_WRITE_PROTECT0x00080000; | |||
184 | } | |||
185 | } | |||
186 | ||||
187 | static void sdhci_reset(SDHCIState *s) | |||
188 | { | |||
189 | timer_del(s->insert_timer); | |||
190 | timer_del(s->transfer_timer); | |||
191 | /* Set all registers to 0. Capabilities registers are not cleared | |||
192 | * and assumed to always preserve their value, given to them during | |||
193 | * initialization */ | |||
194 | memset(&s->sdmasysad, 0, (uintptr_t)&s->capareg - (uintptr_t)&s->sdmasysad); | |||
195 | ||||
196 | sd_set_cb(s->card, s->ro_cb, s->eject_cb); | |||
197 | s->data_count = 0; | |||
198 | s->stopped_state = sdhc_not_stopped; | |||
199 | } | |||
200 | ||||
201 | static void sdhci_do_data_transfer(void *opaque) | |||
202 | { | |||
203 | SDHCIState *s = (SDHCIState *)opaque; | |||
204 | ||||
205 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 205, __func__ ))->data_transfer(s); | |||
206 | } | |||
207 | ||||
208 | static void sdhci_send_command(SDHCIState *s) | |||
209 | { | |||
210 | SDRequest request; | |||
211 | uint8_t response[16]; | |||
212 | int rlen; | |||
213 | ||||
214 | s->errintsts = 0; | |||
215 | s->acmd12errsts = 0; | |||
216 | request.cmd = s->cmdreg >> 8; | |||
217 | request.arg = s->argument; | |||
218 | DPRINT_L1("sending CMD%u ARG[0x%08x]\n", request.cmd, request.arg)do { } while (0); | |||
219 | rlen = sd_do_command(s->card, &request, response); | |||
220 | ||||
221 | if (s->cmdreg & SDHC_CMD_RESPONSE(3 << 0)) { | |||
222 | if (rlen == 4) { | |||
223 | s->rspreg[0] = (response[0] << 24) | (response[1] << 16) | | |||
224 | (response[2] << 8) | response[3]; | |||
225 | s->rspreg[1] = s->rspreg[2] = s->rspreg[3] = 0; | |||
226 | DPRINT_L1("Response: RSPREG[31..0]=0x%08x\n", s->rspreg[0])do { } while (0); | |||
227 | } else if (rlen == 16) { | |||
228 | s->rspreg[0] = (response[11] << 24) | (response[12] << 16) | | |||
229 | (response[13] << 8) | response[14]; | |||
230 | s->rspreg[1] = (response[7] << 24) | (response[8] << 16) | | |||
231 | (response[9] << 8) | response[10]; | |||
232 | s->rspreg[2] = (response[3] << 24) | (response[4] << 16) | | |||
233 | (response[5] << 8) | response[6]; | |||
234 | s->rspreg[3] = (response[0] << 16) | (response[1] << 8) | | |||
235 | response[2]; | |||
236 | DPRINT_L1("Response received:\n RSPREG[127..96]=0x%08x, RSPREG[95.."do { } while (0) | |||
237 | "64]=0x%08x,\n RSPREG[63..32]=0x%08x, RSPREG[31..0]=0x%08x\n",do { } while (0) | |||
238 | s->rspreg[3], s->rspreg[2], s->rspreg[1], s->rspreg[0])do { } while (0); | |||
239 | } else { | |||
240 | ERRPRINT("Timeout waiting for command response\n")do { } while (0); | |||
241 | if (s->errintstsen & SDHC_EISEN_CMDTIMEOUT0x0001) { | |||
242 | s->errintsts |= SDHC_EIS_CMDTIMEOUT0x0001; | |||
243 | s->norintsts |= SDHC_NIS_ERR0x8000; | |||
244 | } | |||
245 | } | |||
246 | ||||
247 | if ((s->norintstsen & SDHC_NISEN_TRSCMP0x0002) && | |||
248 | (s->cmdreg & SDHC_CMD_RESPONSE(3 << 0)) == SDHC_CMD_RSP_WITH_BUSY(3 << 0)) { | |||
249 | s->norintsts |= SDHC_NIS_TRSCMP0x0002; | |||
250 | } | |||
251 | } else if (rlen != 0 && (s->errintstsen & SDHC_EISEN_CMDIDX0x0008)) { | |||
252 | s->errintsts |= SDHC_EIS_CMDIDX0x0008; | |||
253 | s->norintsts |= SDHC_NIS_ERR0x8000; | |||
254 | } | |||
255 | ||||
256 | if (s->norintstsen & SDHC_NISEN_CMDCMP0x0001) { | |||
257 | s->norintsts |= SDHC_NIS_CMDCMP0x0001; | |||
258 | } | |||
259 | ||||
260 | sdhci_update_irq(s); | |||
261 | ||||
262 | if (s->blksize && (s->cmdreg & SDHC_CMD_DATA_PRESENT(1 << 5))) { | |||
263 | s->data_count = 0; | |||
264 | sdhci_do_data_transfer(s); | |||
265 | } | |||
266 | } | |||
267 | ||||
268 | static void sdhci_end_transfer(SDHCIState *s) | |||
269 | { | |||
270 | /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */ | |||
271 | if ((s->trnmod & SDHC_TRNS_ACMD120x0004) != 0) { | |||
272 | SDRequest request; | |||
273 | uint8_t response[16]; | |||
274 | ||||
275 | request.cmd = 0x0C; | |||
276 | request.arg = 0; | |||
277 | DPRINT_L1("Automatically issue CMD%d %08x\n", request.cmd, request.arg)do { } while (0); | |||
278 | sd_do_command(s->card, &request, response); | |||
279 | /* Auto CMD12 response goes to the upper Response register */ | |||
280 | s->rspreg[3] = (response[0] << 24) | (response[1] << 16) | | |||
281 | (response[2] << 8) | response[3]; | |||
282 | } | |||
283 | ||||
284 | s->prnsts &= ~(SDHC_DOING_READ0x00000200 | SDHC_DOING_WRITE0x00000100 | | |||
285 | SDHC_DAT_LINE_ACTIVE0x00000004 | SDHC_DATA_INHIBIT0x00000002 | | |||
286 | SDHC_SPACE_AVAILABLE0x00000400 | SDHC_DATA_AVAILABLE0x00000800); | |||
287 | ||||
288 | if (s->norintstsen & SDHC_NISEN_TRSCMP0x0002) { | |||
289 | s->norintsts |= SDHC_NIS_TRSCMP0x0002; | |||
290 | } | |||
291 | ||||
292 | sdhci_update_irq(s); | |||
293 | } | |||
294 | ||||
295 | /* | |||
296 | * Programmed i/o data transfer | |||
297 | */ | |||
298 | ||||
299 | /* Fill host controller's read buffer with BLKSIZE bytes of data from card */ | |||
300 | static void sdhci_read_block_from_card(SDHCIState *s) | |||
301 | { | |||
302 | int index = 0; | |||
303 | ||||
304 | if ((s->trnmod & SDHC_TRNS_MULTI0x0020) && | |||
305 | (s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) && (s->blkcnt == 0)) { | |||
306 | return; | |||
307 | } | |||
308 | ||||
309 | for (index = 0; index < (s->blksize & 0x0fff); index++) { | |||
310 | s->fifo_buffer[index] = sd_read_data(s->card); | |||
311 | } | |||
312 | ||||
313 | /* New data now available for READ through Buffer Port Register */ | |||
314 | s->prnsts |= SDHC_DATA_AVAILABLE0x00000800; | |||
315 | if (s->norintstsen & SDHC_NISEN_RBUFRDY0x0020) { | |||
316 | s->norintsts |= SDHC_NIS_RBUFRDY0x0020; | |||
317 | } | |||
318 | ||||
319 | /* Clear DAT line active status if that was the last block */ | |||
320 | if ((s->trnmod & SDHC_TRNS_MULTI0x0020) == 0 || | |||
321 | ((s->trnmod & SDHC_TRNS_MULTI0x0020) && s->blkcnt == 1)) { | |||
322 | s->prnsts &= ~SDHC_DAT_LINE_ACTIVE0x00000004; | |||
323 | } | |||
324 | ||||
325 | /* If stop at block gap request was set and it's not the last block of | |||
326 | * data - generate Block Event interrupt */ | |||
327 | if (s->stopped_state == sdhc_gap_read && (s->trnmod & SDHC_TRNS_MULTI0x0020) && | |||
328 | s->blkcnt != 1) { | |||
329 | s->prnsts &= ~SDHC_DAT_LINE_ACTIVE0x00000004; | |||
330 | if (s->norintstsen & SDHC_EISEN_BLKGAP0x0004) { | |||
331 | s->norintsts |= SDHC_EIS_BLKGAP0x0004; | |||
332 | } | |||
333 | } | |||
334 | ||||
335 | sdhci_update_irq(s); | |||
336 | } | |||
337 | ||||
338 | /* Read @size byte of data from host controller @s BUFFER DATA PORT register */ | |||
339 | static uint32_t sdhci_read_dataport(SDHCIState *s, unsigned size) | |||
340 | { | |||
341 | uint32_t value = 0; | |||
342 | int i; | |||
343 | ||||
344 | /* first check that a valid data exists in host controller input buffer */ | |||
345 | if ((s->prnsts & SDHC_DATA_AVAILABLE0x00000800) == 0) { | |||
346 | ERRPRINT("Trying to read from empty buffer\n")do { } while (0); | |||
347 | return 0; | |||
348 | } | |||
349 | ||||
350 | for (i = 0; i < size; i++) { | |||
351 | value |= s->fifo_buffer[s->data_count] << i * 8; | |||
352 | s->data_count++; | |||
353 | /* check if we've read all valid data (blksize bytes) from buffer */ | |||
354 | if ((s->data_count) >= (s->blksize & 0x0fff)) { | |||
355 | DPRINT_L2("All %u bytes of data have been read from input buffer\n",do { } while (0) | |||
356 | s->data_count)do { } while (0); | |||
357 | s->prnsts &= ~SDHC_DATA_AVAILABLE0x00000800; /* no more data in a buffer */ | |||
358 | s->data_count = 0; /* next buff read must start at position [0] */ | |||
359 | ||||
360 | if (s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) { | |||
361 | s->blkcnt--; | |||
362 | } | |||
363 | ||||
364 | /* if that was the last block of data */ | |||
365 | if ((s->trnmod & SDHC_TRNS_MULTI0x0020) == 0 || | |||
366 | ((s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) && (s->blkcnt == 0)) || | |||
367 | /* stop at gap request */ | |||
368 | (s->stopped_state == sdhc_gap_read && | |||
369 | !(s->prnsts & SDHC_DAT_LINE_ACTIVE0x00000004))) { | |||
370 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 370, __func__ ))->end_data_transfer(s); | |||
371 | } else { /* if there are more data, read next block from card */ | |||
372 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 372, __func__ ))->read_block_from_card(s); | |||
373 | } | |||
374 | break; | |||
375 | } | |||
376 | } | |||
377 | ||||
378 | return value; | |||
379 | } | |||
380 | ||||
381 | /* Write data from host controller FIFO to card */ | |||
382 | static void sdhci_write_block_to_card(SDHCIState *s) | |||
383 | { | |||
384 | int index = 0; | |||
385 | ||||
386 | if (s->prnsts & SDHC_SPACE_AVAILABLE0x00000400) { | |||
387 | if (s->norintstsen & SDHC_NISEN_WBUFRDY0x0010) { | |||
388 | s->norintsts |= SDHC_NIS_WBUFRDY0x0010; | |||
389 | } | |||
390 | sdhci_update_irq(s); | |||
391 | return; | |||
392 | } | |||
393 | ||||
394 | if (s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) { | |||
395 | if (s->blkcnt == 0) { | |||
396 | return; | |||
397 | } else { | |||
398 | s->blkcnt--; | |||
399 | } | |||
400 | } | |||
401 | ||||
402 | for (index = 0; index < (s->blksize & 0x0fff); index++) { | |||
403 | sd_write_data(s->card, s->fifo_buffer[index]); | |||
404 | } | |||
405 | ||||
406 | /* Next data can be written through BUFFER DATORT register */ | |||
407 | s->prnsts |= SDHC_SPACE_AVAILABLE0x00000400; | |||
408 | ||||
409 | /* Finish transfer if that was the last block of data */ | |||
410 | if ((s->trnmod & SDHC_TRNS_MULTI0x0020) == 0 || | |||
411 | ((s->trnmod & SDHC_TRNS_MULTI0x0020) && | |||
412 | (s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) && (s->blkcnt == 0))) { | |||
413 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 413, __func__ ))->end_data_transfer(s); | |||
414 | } else if (s->norintstsen & SDHC_NISEN_WBUFRDY0x0010) { | |||
415 | s->norintsts |= SDHC_NIS_WBUFRDY0x0010; | |||
416 | } | |||
417 | ||||
418 | /* Generate Block Gap Event if requested and if not the last block */ | |||
419 | if (s->stopped_state == sdhc_gap_write && (s->trnmod & SDHC_TRNS_MULTI0x0020) && | |||
420 | s->blkcnt > 0) { | |||
421 | s->prnsts &= ~SDHC_DOING_WRITE0x00000100; | |||
422 | if (s->norintstsen & SDHC_EISEN_BLKGAP0x0004) { | |||
423 | s->norintsts |= SDHC_EIS_BLKGAP0x0004; | |||
424 | } | |||
425 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 425, __func__ ))->end_data_transfer(s); | |||
426 | } | |||
427 | ||||
428 | sdhci_update_irq(s); | |||
429 | } | |||
430 | ||||
431 | /* Write @size bytes of @value data to host controller @s Buffer Data Port | |||
432 | * register */ | |||
433 | static void sdhci_write_dataport(SDHCIState *s, uint32_t value, unsigned size) | |||
434 | { | |||
435 | unsigned i; | |||
436 | ||||
437 | /* Check that there is free space left in a buffer */ | |||
438 | if (!(s->prnsts & SDHC_SPACE_AVAILABLE0x00000400)) { | |||
439 | ERRPRINT("Can't write to data buffer: buffer full\n")do { } while (0); | |||
440 | return; | |||
441 | } | |||
442 | ||||
443 | for (i = 0; i < size; i++) { | |||
444 | s->fifo_buffer[s->data_count] = value & 0xFF; | |||
445 | s->data_count++; | |||
446 | value >>= 8; | |||
447 | if (s->data_count >= (s->blksize & 0x0fff)) { | |||
448 | DPRINT_L2("write buffer filled with %u bytes of data\n",do { } while (0) | |||
449 | s->data_count)do { } while (0); | |||
450 | s->data_count = 0; | |||
451 | s->prnsts &= ~SDHC_SPACE_AVAILABLE0x00000400; | |||
452 | if (s->prnsts & SDHC_DOING_WRITE0x00000100) { | |||
453 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 453, __func__ ))->write_block_to_card(s); | |||
454 | } | |||
455 | } | |||
456 | } | |||
457 | } | |||
458 | ||||
459 | /* | |||
460 | * Single DMA data transfer | |||
461 | */ | |||
462 | ||||
463 | /* Multi block SDMA transfer */ | |||
464 | static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s) | |||
465 | { | |||
466 | bool_Bool page_aligned = false0; | |||
467 | unsigned int n, begin; | |||
468 | const uint16_t block_size = s->blksize & 0x0fff; | |||
469 | uint32_t boundary_chk = 1 << (((s->blksize & 0xf000) >> 12) + 12); | |||
470 | uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk); | |||
471 | ||||
472 | /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for | |||
473 | * possible stop at page boundary if initial address is not page aligned, | |||
474 | * allow them to work properly */ | |||
475 | if ((s->sdmasysad % boundary_chk) == 0) { | |||
476 | page_aligned = true1; | |||
477 | } | |||
478 | ||||
479 | if (s->trnmod & SDHC_TRNS_READ0x0010) { | |||
480 | s->prnsts |= SDHC_DOING_READ0x00000200 | SDHC_DATA_INHIBIT0x00000002 | | |||
481 | SDHC_DAT_LINE_ACTIVE0x00000004; | |||
482 | while (s->blkcnt) { | |||
483 | if (s->data_count == 0) { | |||
484 | for (n = 0; n < block_size; n++) { | |||
485 | s->fifo_buffer[n] = sd_read_data(s->card); | |||
486 | } | |||
487 | } | |||
488 | begin = s->data_count; | |||
489 | if (((boundary_count + begin) < block_size) && page_aligned) { | |||
490 | s->data_count = boundary_count + begin; | |||
491 | boundary_count = 0; | |||
492 | } else { | |||
493 | s->data_count = block_size; | |||
494 | boundary_count -= block_size - begin; | |||
495 | if (s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) { | |||
496 | s->blkcnt--; | |||
497 | } | |||
498 | } | |||
499 | dma_memory_write(&address_space_memory, s->sdmasysad, | |||
500 | &s->fifo_buffer[begin], s->data_count - begin); | |||
501 | s->sdmasysad += s->data_count - begin; | |||
502 | if (s->data_count == block_size) { | |||
503 | s->data_count = 0; | |||
504 | } | |||
505 | if (page_aligned && boundary_count == 0) { | |||
506 | break; | |||
507 | } | |||
508 | } | |||
509 | } else { | |||
510 | s->prnsts |= SDHC_DOING_WRITE0x00000100 | SDHC_DATA_INHIBIT0x00000002 | | |||
511 | SDHC_DAT_LINE_ACTIVE0x00000004; | |||
512 | while (s->blkcnt) { | |||
513 | begin = s->data_count; | |||
514 | if (((boundary_count + begin) < block_size) && page_aligned) { | |||
515 | s->data_count = boundary_count + begin; | |||
516 | boundary_count = 0; | |||
517 | } else { | |||
518 | s->data_count = block_size; | |||
519 | boundary_count -= block_size - begin; | |||
520 | } | |||
521 | dma_memory_read(&address_space_memory, s->sdmasysad, | |||
522 | &s->fifo_buffer[begin], s->data_count); | |||
523 | s->sdmasysad += s->data_count - begin; | |||
524 | if (s->data_count == block_size) { | |||
525 | for (n = 0; n < block_size; n++) { | |||
526 | sd_write_data(s->card, s->fifo_buffer[n]); | |||
527 | } | |||
528 | s->data_count = 0; | |||
529 | if (s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) { | |||
530 | s->blkcnt--; | |||
531 | } | |||
532 | } | |||
533 | if (page_aligned && boundary_count == 0) { | |||
534 | break; | |||
535 | } | |||
536 | } | |||
537 | } | |||
538 | ||||
539 | if (s->blkcnt == 0) { | |||
540 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 540, __func__ ))->end_data_transfer(s); | |||
541 | } else { | |||
542 | if (s->norintstsen & SDHC_NISEN_DMA0x0008) { | |||
543 | s->norintsts |= SDHC_NIS_DMA0x0008; | |||
544 | } | |||
545 | sdhci_update_irq(s); | |||
546 | } | |||
547 | } | |||
548 | ||||
549 | /* single block SDMA transfer */ | |||
550 | ||||
551 | static void sdhci_sdma_transfer_single_block(SDHCIState *s) | |||
552 | { | |||
553 | int n; | |||
554 | uint32_t datacnt = s->blksize & 0x0fff; | |||
555 | ||||
556 | if (s->trnmod & SDHC_TRNS_READ0x0010) { | |||
557 | for (n = 0; n < datacnt; n++) { | |||
558 | s->fifo_buffer[n] = sd_read_data(s->card); | |||
559 | } | |||
560 | dma_memory_write(&address_space_memory, s->sdmasysad, s->fifo_buffer, | |||
561 | datacnt); | |||
562 | } else { | |||
563 | dma_memory_read(&address_space_memory, s->sdmasysad, s->fifo_buffer, | |||
564 | datacnt); | |||
565 | for (n = 0; n < datacnt; n++) { | |||
566 | sd_write_data(s->card, s->fifo_buffer[n]); | |||
567 | } | |||
568 | } | |||
569 | ||||
570 | if (s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) { | |||
571 | s->blkcnt--; | |||
572 | } | |||
573 | ||||
574 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 574, __func__ ))->end_data_transfer(s); | |||
575 | } | |||
576 | ||||
577 | typedef struct ADMADescr { | |||
578 | hwaddr addr; | |||
579 | uint16_t length; | |||
580 | uint8_t attr; | |||
581 | uint8_t incr; | |||
582 | } ADMADescr; | |||
583 | ||||
584 | static void get_adma_description(SDHCIState *s, ADMADescr *dscr) | |||
585 | { | |||
586 | uint32_t adma1 = 0; | |||
587 | uint64_t adma2 = 0; | |||
588 | hwaddr entry_addr = (hwaddr)s->admasysaddr; | |||
589 | switch (SDHC_DMA_TYPE(s->hostctl)((s->hostctl) & 0x18)) { | |||
590 | case SDHC_CTRL_ADMA2_320x10: | |||
591 | dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma2, | |||
592 | sizeof(adma2)); | |||
593 | adma2 = le64_to_cpu(adma2); | |||
594 | /* The spec does not specify endianness of descriptor table. | |||
595 | * We currently assume that it is LE. | |||
596 | */ | |||
597 | dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull; | |||
598 | dscr->length = (uint16_t)extract64(adma2, 16, 16); | |||
599 | dscr->attr = (uint8_t)extract64(adma2, 0, 7); | |||
600 | dscr->incr = 8; | |||
601 | break; | |||
602 | case SDHC_CTRL_ADMA1_320x08: | |||
603 | dma_memory_read(&address_space_memory, entry_addr, (uint8_t *)&adma1, | |||
604 | sizeof(adma1)); | |||
605 | adma1 = le32_to_cpu(adma1); | |||
606 | dscr->addr = (hwaddr)(adma1 & 0xFFFFF000); | |||
607 | dscr->attr = (uint8_t)extract32(adma1, 0, 7); | |||
608 | dscr->incr = 4; | |||
609 | if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK((1 << 4)|(1 << 5))) == SDHC_ADMA_ATTR_SET_LEN(1 << 4)) { | |||
610 | dscr->length = (uint16_t)extract32(adma1, 12, 16); | |||
611 | } else { | |||
612 | dscr->length = 4096; | |||
613 | } | |||
614 | break; | |||
615 | case SDHC_CTRL_ADMA2_640x18: | |||
616 | dma_memory_read(&address_space_memory, entry_addr, | |||
617 | (uint8_t *)(&dscr->attr), 1); | |||
618 | dma_memory_read(&address_space_memory, entry_addr + 2, | |||
619 | (uint8_t *)(&dscr->length), 2); | |||
620 | dscr->length = le16_to_cpu(dscr->length); | |||
621 | dma_memory_read(&address_space_memory, entry_addr + 4, | |||
622 | (uint8_t *)(&dscr->addr), 8); | |||
623 | dscr->attr = le64_to_cpu(dscr->attr); | |||
624 | dscr->attr &= 0xfffffff8; | |||
625 | dscr->incr = 12; | |||
626 | break; | |||
627 | } | |||
628 | } | |||
629 | ||||
630 | /* Advanced DMA data transfer */ | |||
631 | ||||
632 | static void sdhci_do_adma(SDHCIState *s) | |||
633 | { | |||
634 | unsigned int n, begin, length; | |||
635 | const uint16_t block_size = s->blksize & 0x0fff; | |||
636 | ADMADescr dscr; | |||
637 | int i; | |||
638 | ||||
639 | for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY5; ++i) { | |||
| ||||
640 | s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH(1 << 2); | |||
641 | ||||
642 | get_adma_description(s, &dscr); | |||
643 | DPRINT_L2("ADMA loop: addr=" TARGET_FMT_plx ", len=%d, attr=%x\n",do { } while (0) | |||
644 | dscr.addr, dscr.length, dscr.attr)do { } while (0); | |||
645 | ||||
646 | if ((dscr.attr & SDHC_ADMA_ATTR_VALID(1 << 0)) == 0) { | |||
| ||||
647 | /* Indicate that error occurred in ST_FDS state */ | |||
648 | s->admaerr &= ~SDHC_ADMAERR_STATE_MASK(3 << 0); | |||
649 | s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS(1 << 0); | |||
650 | ||||
651 | /* Generate ADMA error interrupt */ | |||
652 | if (s->errintstsen & SDHC_EISEN_ADMAERR0x0200) { | |||
653 | s->errintsts |= SDHC_EIS_ADMAERR0x0200; | |||
654 | s->norintsts |= SDHC_NIS_ERR0x8000; | |||
655 | } | |||
656 | ||||
657 | sdhci_update_irq(s); | |||
658 | return; | |||
659 | } | |||
660 | ||||
661 | length = dscr.length ? dscr.length : 65536; | |||
662 | ||||
663 | switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK((1 << 4)|(1 << 5))) { | |||
664 | case SDHC_ADMA_ATTR_ACT_TRAN(1 << 5): /* data transfer */ | |||
665 | ||||
666 | if (s->trnmod & SDHC_TRNS_READ0x0010) { | |||
667 | while (length) { | |||
668 | if (s->data_count == 0) { | |||
669 | for (n = 0; n < block_size; n++) { | |||
670 | s->fifo_buffer[n] = sd_read_data(s->card); | |||
671 | } | |||
672 | } | |||
673 | begin = s->data_count; | |||
674 | if ((length + begin) < block_size) { | |||
675 | s->data_count = length + begin; | |||
676 | length = 0; | |||
677 | } else { | |||
678 | s->data_count = block_size; | |||
679 | length -= block_size - begin; | |||
680 | } | |||
681 | dma_memory_write(&address_space_memory, dscr.addr, | |||
682 | &s->fifo_buffer[begin], | |||
683 | s->data_count - begin); | |||
684 | dscr.addr += s->data_count - begin; | |||
685 | if (s->data_count == block_size) { | |||
686 | s->data_count = 0; | |||
687 | if (s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) { | |||
688 | s->blkcnt--; | |||
689 | if (s->blkcnt == 0) { | |||
690 | break; | |||
691 | } | |||
692 | } | |||
693 | } | |||
694 | } | |||
695 | } else { | |||
696 | while (length) { | |||
697 | begin = s->data_count; | |||
698 | if ((length + begin) < block_size) { | |||
699 | s->data_count = length + begin; | |||
700 | length = 0; | |||
701 | } else { | |||
702 | s->data_count = block_size; | |||
703 | length -= block_size - begin; | |||
704 | } | |||
705 | dma_memory_read(&address_space_memory, dscr.addr, | |||
706 | &s->fifo_buffer[begin], s->data_count); | |||
707 | dscr.addr += s->data_count - begin; | |||
708 | if (s->data_count == block_size) { | |||
709 | for (n = 0; n < block_size; n++) { | |||
710 | sd_write_data(s->card, s->fifo_buffer[n]); | |||
711 | } | |||
712 | s->data_count = 0; | |||
713 | if (s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) { | |||
714 | s->blkcnt--; | |||
715 | if (s->blkcnt == 0) { | |||
716 | break; | |||
717 | } | |||
718 | } | |||
719 | } | |||
720 | } | |||
721 | } | |||
722 | s->admasysaddr += dscr.incr; | |||
723 | break; | |||
724 | case SDHC_ADMA_ATTR_ACT_LINK(3 << 4): /* link to next descriptor table */ | |||
725 | s->admasysaddr = dscr.addr; | |||
726 | DPRINT_L1("ADMA link: admasysaddr=0x%lx\n", s->admasysaddr)do { } while (0); | |||
727 | break; | |||
728 | default: | |||
729 | s->admasysaddr += dscr.incr; | |||
730 | break; | |||
731 | } | |||
732 | ||||
733 | if (dscr.attr & SDHC_ADMA_ATTR_INT(1 << 2)) { | |||
734 | DPRINT_L1("ADMA interrupt: admasysaddr=0x%lx\n", s->admasysaddr)do { } while (0); | |||
735 | if (s->norintstsen & SDHC_NISEN_DMA0x0008) { | |||
736 | s->norintsts |= SDHC_NIS_DMA0x0008; | |||
737 | } | |||
738 | ||||
739 | sdhci_update_irq(s); | |||
740 | } | |||
741 | ||||
742 | /* ADMA transfer terminates if blkcnt == 0 or by END attribute */ | |||
743 | if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) && | |||
744 | (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END(1 << 1))) { | |||
745 | DPRINT_L2("ADMA transfer completed\n")do { } while (0); | |||
746 | if (length || ((dscr.attr & SDHC_ADMA_ATTR_END(1 << 1)) && | |||
747 | (s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) && | |||
748 | s->blkcnt != 0)) { | |||
749 | ERRPRINT("SD/MMC host ADMA length mismatch\n")do { } while (0); | |||
750 | s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH(1 << 2) | | |||
751 | SDHC_ADMAERR_STATE_ST_TFR(3 << 0); | |||
752 | if (s->errintstsen & SDHC_EISEN_ADMAERR0x0200) { | |||
753 | ERRPRINT("Set ADMA error flag\n")do { } while (0); | |||
754 | s->errintsts |= SDHC_EIS_ADMAERR0x0200; | |||
755 | s->norintsts |= SDHC_NIS_ERR0x8000; | |||
756 | } | |||
757 | ||||
758 | sdhci_update_irq(s); | |||
759 | } | |||
760 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 760, __func__ ))->end_data_transfer(s); | |||
761 | return; | |||
762 | } | |||
763 | ||||
764 | } | |||
765 | ||||
766 | /* we have unfinished business - reschedule to continue ADMA */ | |||
767 | timer_mod(s->transfer_timer, | |||
768 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + SDHC_TRANSFER_DELAY100); | |||
769 | } | |||
770 | ||||
771 | /* Perform data transfer according to controller configuration */ | |||
772 | ||||
773 | static void sdhci_data_transfer(SDHCIState *s) | |||
774 | { | |||
775 | SDHCIClass *k = SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 775, __func__ )); | |||
776 | ||||
777 | if (s->trnmod & SDHC_TRNS_DMA0x0001) { | |||
778 | switch (SDHC_DMA_TYPE(s->hostctl)((s->hostctl) & 0x18)) { | |||
779 | case SDHC_CTRL_SDMA0x00: | |||
780 | if ((s->trnmod & SDHC_TRNS_MULTI0x0020) && | |||
781 | (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN0x0002) || s->blkcnt == 0)) { | |||
782 | break; | |||
783 | } | |||
784 | ||||
785 | if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI0x0020)) { | |||
786 | k->do_sdma_single(s); | |||
787 | } else { | |||
788 | k->do_sdma_multi(s); | |||
789 | } | |||
790 | ||||
791 | break; | |||
792 | case SDHC_CTRL_ADMA1_320x08: | |||
793 | if (!(s->capareg & SDHC_CAN_DO_ADMA10x00100000)) { | |||
794 | ERRPRINT("ADMA1 not supported\n")do { } while (0); | |||
795 | break; | |||
796 | } | |||
797 | ||||
798 | k->do_adma(s); | |||
799 | break; | |||
800 | case SDHC_CTRL_ADMA2_320x10: | |||
801 | if (!(s->capareg & SDHC_CAN_DO_ADMA20x00080000)) { | |||
802 | ERRPRINT("ADMA2 not supported\n")do { } while (0); | |||
803 | break; | |||
804 | } | |||
805 | ||||
806 | k->do_adma(s); | |||
807 | break; | |||
808 | case SDHC_CTRL_ADMA2_640x18: | |||
809 | if (!(s->capareg & SDHC_CAN_DO_ADMA20x00080000) || | |||
810 | !(s->capareg & SDHC_64_BIT_BUS_SUPPORT(1 << 28))) { | |||
811 | ERRPRINT("64 bit ADMA not supported\n")do { } while (0); | |||
812 | break; | |||
813 | } | |||
814 | ||||
815 | k->do_adma(s); | |||
816 | break; | |||
817 | default: | |||
818 | ERRPRINT("Unsupported DMA type\n")do { } while (0); | |||
819 | break; | |||
820 | } | |||
821 | } else { | |||
822 | if ((s->trnmod & SDHC_TRNS_READ0x0010) && sd_data_ready(s->card)) { | |||
823 | s->prnsts |= SDHC_DOING_READ0x00000200 | SDHC_DATA_INHIBIT0x00000002 | | |||
824 | SDHC_DAT_LINE_ACTIVE0x00000004; | |||
825 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 825, __func__ ))->read_block_from_card(s); | |||
826 | } else { | |||
827 | s->prnsts |= SDHC_DOING_WRITE0x00000100 | SDHC_DAT_LINE_ACTIVE0x00000004 | | |||
828 | SDHC_SPACE_AVAILABLE0x00000400 | SDHC_DATA_INHIBIT0x00000002; | |||
829 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 829, __func__ ))->write_block_to_card(s); | |||
830 | } | |||
831 | } | |||
832 | } | |||
833 | ||||
834 | static bool_Bool sdhci_can_issue_command(SDHCIState *s) | |||
835 | { | |||
836 | if (!SDHC_CLOCK_IS_ON(s->clkcon)(((s->clkcon) & 0x0007) == 0x0007) || !(s->pwrcon & SDHC_POWER_ON(1 << 0)) || | |||
837 | (((s->prnsts & SDHC_DATA_INHIBIT0x00000002) || s->stopped_state) && | |||
838 | ((s->cmdreg & SDHC_CMD_DATA_PRESENT(1 << 5)) || | |||
839 | ((s->cmdreg & SDHC_CMD_RESPONSE(3 << 0)) == SDHC_CMD_RSP_WITH_BUSY(3 << 0) && | |||
840 | !(SDHC_COMMAND_TYPE(s->cmdreg)((s->cmdreg) & ((1 << 6)|(1 << 7))) == SDHC_CMD_ABORT((1 << 6)|(1 << 7))))))) { | |||
841 | return false0; | |||
842 | } | |||
843 | ||||
844 | return true1; | |||
845 | } | |||
846 | ||||
847 | /* The Buffer Data Port register must be accessed in sequential and | |||
848 | * continuous manner */ | |||
849 | static inline bool_Bool | |||
850 | sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num) | |||
851 | { | |||
852 | if ((s->data_count & 0x3) != byte_num) { | |||
853 | ERRPRINT("Non-sequential access to Buffer Data Port register"do { } while (0) | |||
854 | "is prohibited\n")do { } while (0); | |||
855 | return false0; | |||
856 | } | |||
857 | return true1; | |||
858 | } | |||
859 | ||||
860 | static uint32_t sdhci_read(SDHCIState *s, unsigned int offset, unsigned size) | |||
861 | { | |||
862 | uint32_t ret = 0; | |||
863 | ||||
864 | switch (offset & ~0x3) { | |||
865 | case SDHC_SYSAD0x00: | |||
866 | ret = s->sdmasysad; | |||
867 | break; | |||
868 | case SDHC_BLKSIZE0x04: | |||
869 | ret = s->blksize | (s->blkcnt << 16); | |||
870 | break; | |||
871 | case SDHC_ARGUMENT0x08: | |||
872 | ret = s->argument; | |||
873 | break; | |||
874 | case SDHC_TRNMOD0x0C: | |||
875 | ret = s->trnmod | (s->cmdreg << 16); | |||
876 | break; | |||
877 | case SDHC_RSPREG00x10 ... SDHC_RSPREG30x1C: | |||
878 | ret = s->rspreg[((offset & ~0x3) - SDHC_RSPREG00x10) >> 2]; | |||
879 | break; | |||
880 | case SDHC_BDATA0x20: | |||
881 | if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA0x20)) { | |||
882 | ret = SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 882, __func__ ))->bdata_read(s, size); | |||
883 | DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, offset,do { } while (0) | |||
884 | ret, ret)do { } while (0); | |||
885 | return ret; | |||
886 | } | |||
887 | break; | |||
888 | case SDHC_PRNSTS0x24: | |||
889 | ret = s->prnsts; | |||
890 | break; | |||
891 | case SDHC_HOSTCTL0x28: | |||
892 | ret = s->hostctl | (s->pwrcon << 8) | (s->blkgap << 16) | | |||
893 | (s->wakcon << 24); | |||
894 | break; | |||
895 | case SDHC_CLKCON0x2C: | |||
896 | ret = s->clkcon | (s->timeoutcon << 16); | |||
897 | break; | |||
898 | case SDHC_NORINTSTS0x30: | |||
899 | ret = s->norintsts | (s->errintsts << 16); | |||
900 | break; | |||
901 | case SDHC_NORINTSTSEN0x34: | |||
902 | ret = s->norintstsen | (s->errintstsen << 16); | |||
903 | break; | |||
904 | case SDHC_NORINTSIGEN0x38: | |||
905 | ret = s->norintsigen | (s->errintsigen << 16); | |||
906 | break; | |||
907 | case SDHC_ACMD12ERRSTS0x3C: | |||
908 | ret = s->acmd12errsts; | |||
909 | break; | |||
910 | case SDHC_CAPAREG0x40: | |||
911 | ret = s->capareg; | |||
912 | break; | |||
913 | case SDHC_MAXCURR0x48: | |||
914 | ret = s->maxcurr; | |||
915 | break; | |||
916 | case SDHC_ADMAERR0x54: | |||
917 | ret = s->admaerr; | |||
918 | break; | |||
919 | case SDHC_ADMASYSADDR0x58: | |||
920 | ret = (uint32_t)s->admasysaddr; | |||
921 | break; | |||
922 | case SDHC_ADMASYSADDR0x58 + 4: | |||
923 | ret = (uint32_t)(s->admasysaddr >> 32); | |||
924 | break; | |||
925 | case SDHC_SLOT_INT_STATUS0xFC: | |||
926 | ret = (SD_HOST_SPECv2_VERS0x2401 << 16) | sdhci_slotint(s); | |||
927 | break; | |||
928 | default: | |||
929 | ERRPRINT("bad %ub read: addr[0x%04x]\n", size, offset)do { } while (0); | |||
930 | break; | |||
931 | } | |||
932 | ||||
933 | ret >>= (offset & 0x3) * 8; | |||
934 | ret &= (1ULL << (size * 8)) - 1; | |||
935 | DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size, offset, ret, ret)do { } while (0); | |||
936 | return ret; | |||
937 | } | |||
938 | ||||
939 | static inline void sdhci_blkgap_write(SDHCIState *s, uint8_t value) | |||
940 | { | |||
941 | if ((value & SDHC_STOP_AT_GAP_REQ0x01) && (s->blkgap & SDHC_STOP_AT_GAP_REQ0x01)) { | |||
942 | return; | |||
943 | } | |||
944 | s->blkgap = value & SDHC_STOP_AT_GAP_REQ0x01; | |||
945 | ||||
946 | if ((value & SDHC_CONTINUE_REQ0x02) && s->stopped_state && | |||
947 | (s->blkgap & SDHC_STOP_AT_GAP_REQ0x01) == 0) { | |||
948 | if (s->stopped_state == sdhc_gap_read) { | |||
949 | s->prnsts |= SDHC_DAT_LINE_ACTIVE0x00000004 | SDHC_DOING_READ0x00000200; | |||
950 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 950, __func__ ))->read_block_from_card(s); | |||
951 | } else { | |||
952 | s->prnsts |= SDHC_DAT_LINE_ACTIVE0x00000004 | SDHC_DOING_WRITE0x00000100; | |||
953 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 953, __func__ ))->write_block_to_card(s); | |||
954 | } | |||
955 | s->stopped_state = sdhc_not_stopped; | |||
956 | } else if (!s->stopped_state && (value & SDHC_STOP_AT_GAP_REQ0x01)) { | |||
957 | if (s->prnsts & SDHC_DOING_READ0x00000200) { | |||
958 | s->stopped_state = sdhc_gap_read; | |||
959 | } else if (s->prnsts & SDHC_DOING_WRITE0x00000100) { | |||
960 | s->stopped_state = sdhc_gap_write; | |||
961 | } | |||
962 | } | |||
963 | } | |||
964 | ||||
965 | static inline void sdhci_reset_write(SDHCIState *s, uint8_t value) | |||
966 | { | |||
967 | switch (value) { | |||
968 | case SDHC_RESET_ALL0x01: | |||
969 | DEVICE_GET_CLASS(s)((DeviceClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("device"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c" , 969, __func__))->reset(DEVICE(s)((DeviceState *)object_dynamic_cast_assert(((Object *)((s))), ("device"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c" , 969, __func__))); | |||
970 | break; | |||
971 | case SDHC_RESET_CMD0x02: | |||
972 | s->prnsts &= ~SDHC_CMD_INHIBIT0x00000001; | |||
973 | s->norintsts &= ~SDHC_NIS_CMDCMP0x0001; | |||
974 | break; | |||
975 | case SDHC_RESET_DATA0x04: | |||
976 | s->data_count = 0; | |||
977 | s->prnsts &= ~(SDHC_SPACE_AVAILABLE0x00000400 | SDHC_DATA_AVAILABLE0x00000800 | | |||
978 | SDHC_DOING_READ0x00000200 | SDHC_DOING_WRITE0x00000100 | | |||
979 | SDHC_DATA_INHIBIT0x00000002 | SDHC_DAT_LINE_ACTIVE0x00000004); | |||
980 | s->blkgap &= ~(SDHC_STOP_AT_GAP_REQ0x01 | SDHC_CONTINUE_REQ0x02); | |||
981 | s->stopped_state = sdhc_not_stopped; | |||
982 | s->norintsts &= ~(SDHC_NIS_WBUFRDY0x0010 | SDHC_NIS_RBUFRDY0x0020 | | |||
983 | SDHC_NIS_DMA0x0008 | SDHC_NIS_TRSCMP0x0002 | SDHC_NIS_BLKGAP0x0004); | |||
984 | break; | |||
985 | } | |||
986 | } | |||
987 | ||||
988 | static void | |||
989 | sdhci_write(SDHCIState *s, unsigned int offset, uint32_t value, unsigned size) | |||
990 | { | |||
991 | unsigned shift = 8 * (offset & 0x3); | |||
992 | uint32_t mask = ~(((1ULL << (size * 8)) - 1) << shift); | |||
993 | value <<= shift; | |||
994 | ||||
995 | switch (offset & ~0x3) { | |||
996 | case SDHC_SYSAD0x00: | |||
997 | s->sdmasysad = (s->sdmasysad & mask) | value; | |||
998 | MASKED_WRITE(s->sdmasysad, mask, value)(s->sdmasysad = (s->sdmasysad & (mask)) | (value)); | |||
999 | /* Writing to last byte of sdmasysad might trigger transfer */ | |||
1000 | if (!(mask & 0xFF000000) && TRANSFERRING_DATA(s->prnsts)((s->prnsts) & (0x00000200 | 0x00000100)) && s->blkcnt && | |||
1001 | s->blksize && SDHC_DMA_TYPE(s->hostctl)((s->hostctl) & 0x18) == SDHC_CTRL_SDMA0x00) { | |||
1002 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 1002, __func__ ))->do_sdma_multi(s); | |||
1003 | } | |||
1004 | break; | |||
1005 | case SDHC_BLKSIZE0x04: | |||
1006 | if (!TRANSFERRING_DATA(s->prnsts)((s->prnsts) & (0x00000200 | 0x00000100))) { | |||
1007 | MASKED_WRITE(s->blksize, mask, value)(s->blksize = (s->blksize & (mask)) | (value)); | |||
1008 | MASKED_WRITE(s->blkcnt, mask >> 16, value >> 16)(s->blkcnt = (s->blkcnt & (mask >> 16)) | (value >> 16)); | |||
1009 | } | |||
1010 | break; | |||
1011 | case SDHC_ARGUMENT0x08: | |||
1012 | MASKED_WRITE(s->argument, mask, value)(s->argument = (s->argument & (mask)) | (value)); | |||
1013 | break; | |||
1014 | case SDHC_TRNMOD0x0C: | |||
1015 | /* DMA can be enabled only if it is supported as indicated by | |||
1016 | * capabilities register */ | |||
1017 | if (!(s->capareg & SDHC_CAN_DO_DMA0x00400000)) { | |||
1018 | value &= ~SDHC_TRNS_DMA0x0001; | |||
1019 | } | |||
1020 | MASKED_WRITE(s->trnmod, mask, value)(s->trnmod = (s->trnmod & (mask)) | (value)); | |||
1021 | MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16)(s->cmdreg = (s->cmdreg & (mask >> 16)) | (value >> 16)); | |||
1022 | ||||
1023 | /* Writing to the upper byte of CMDREG triggers SD command generation */ | |||
1024 | if ((mask & 0xFF000000) || !SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 1024, __func__ ))->can_issue_command(s)) { | |||
1025 | break; | |||
1026 | } | |||
1027 | ||||
1028 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 1028, __func__ ))->send_command(s); | |||
1029 | break; | |||
1030 | case SDHC_BDATA0x20: | |||
1031 | if (sdhci_buff_access_is_sequential(s, offset - SDHC_BDATA0x20)) { | |||
1032 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 1032, __func__ ))->bdata_write(s, value >> shift, size); | |||
1033 | } | |||
1034 | break; | |||
1035 | case SDHC_HOSTCTL0x28: | |||
1036 | if (!(mask & 0xFF0000)) { | |||
1037 | sdhci_blkgap_write(s, value >> 16); | |||
1038 | } | |||
1039 | MASKED_WRITE(s->hostctl, mask, value)(s->hostctl = (s->hostctl & (mask)) | (value)); | |||
1040 | MASKED_WRITE(s->pwrcon, mask >> 8, value >> 8)(s->pwrcon = (s->pwrcon & (mask >> 8)) | (value >> 8)); | |||
1041 | MASKED_WRITE(s->wakcon, mask >> 24, value >> 24)(s->wakcon = (s->wakcon & (mask >> 24)) | (value >> 24)); | |||
1042 | if (!(s->prnsts & SDHC_CARD_PRESENT0x00010000) || ((s->pwrcon >> 1) & 0x7) < 5 || | |||
1043 | !(s->capareg & (1 << (31 - ((s->pwrcon >> 1) & 0x7))))) { | |||
1044 | s->pwrcon &= ~SDHC_POWER_ON(1 << 0); | |||
1045 | } | |||
1046 | break; | |||
1047 | case SDHC_CLKCON0x2C: | |||
1048 | if (!(mask & 0xFF000000)) { | |||
1049 | sdhci_reset_write(s, value >> 24); | |||
1050 | } | |||
1051 | MASKED_WRITE(s->clkcon, mask, value)(s->clkcon = (s->clkcon & (mask)) | (value)); | |||
1052 | MASKED_WRITE(s->timeoutcon, mask >> 16, value >> 16)(s->timeoutcon = (s->timeoutcon & (mask >> 16 )) | (value >> 16)); | |||
1053 | if (s->clkcon & SDHC_CLOCK_INT_EN0x0001) { | |||
1054 | s->clkcon |= SDHC_CLOCK_INT_STABLE0x0002; | |||
1055 | } else { | |||
1056 | s->clkcon &= ~SDHC_CLOCK_INT_STABLE0x0002; | |||
1057 | } | |||
1058 | break; | |||
1059 | case SDHC_NORINTSTS0x30: | |||
1060 | if (s->norintstsen & SDHC_NISEN_CARDINT0x0100) { | |||
1061 | value &= ~SDHC_NIS_CARDINT0x0100; | |||
1062 | } | |||
1063 | s->norintsts &= mask | ~value; | |||
1064 | s->errintsts &= (mask >> 16) | ~(value >> 16); | |||
1065 | if (s->errintsts) { | |||
1066 | s->norintsts |= SDHC_NIS_ERR0x8000; | |||
1067 | } else { | |||
1068 | s->norintsts &= ~SDHC_NIS_ERR0x8000; | |||
1069 | } | |||
1070 | sdhci_update_irq(s); | |||
1071 | break; | |||
1072 | case SDHC_NORINTSTSEN0x34: | |||
1073 | MASKED_WRITE(s->norintstsen, mask, value)(s->norintstsen = (s->norintstsen & (mask)) | (value )); | |||
1074 | MASKED_WRITE(s->errintstsen, mask >> 16, value >> 16)(s->errintstsen = (s->errintstsen & (mask >> 16 )) | (value >> 16)); | |||
1075 | s->norintsts &= s->norintstsen; | |||
1076 | s->errintsts &= s->errintstsen; | |||
1077 | if (s->errintsts) { | |||
1078 | s->norintsts |= SDHC_NIS_ERR0x8000; | |||
1079 | } else { | |||
1080 | s->norintsts &= ~SDHC_NIS_ERR0x8000; | |||
1081 | } | |||
1082 | sdhci_update_irq(s); | |||
1083 | break; | |||
1084 | case SDHC_NORINTSIGEN0x38: | |||
1085 | MASKED_WRITE(s->norintsigen, mask, value)(s->norintsigen = (s->norintsigen & (mask)) | (value )); | |||
1086 | MASKED_WRITE(s->errintsigen, mask >> 16, value >> 16)(s->errintsigen = (s->errintsigen & (mask >> 16 )) | (value >> 16)); | |||
1087 | sdhci_update_irq(s); | |||
1088 | break; | |||
1089 | case SDHC_ADMAERR0x54: | |||
1090 | MASKED_WRITE(s->admaerr, mask, value)(s->admaerr = (s->admaerr & (mask)) | (value)); | |||
1091 | break; | |||
1092 | case SDHC_ADMASYSADDR0x58: | |||
1093 | s->admasysaddr = (s->admasysaddr & (0xFFFFFFFF00000000ULL | | |||
1094 | (uint64_t)mask)) | (uint64_t)value; | |||
1095 | break; | |||
1096 | case SDHC_ADMASYSADDR0x58 + 4: | |||
1097 | s->admasysaddr = (s->admasysaddr & (0x00000000FFFFFFFFULL | | |||
1098 | ((uint64_t)mask << 32))) | ((uint64_t)value << 32); | |||
1099 | break; | |||
1100 | case SDHC_FEAER0x50: | |||
1101 | s->acmd12errsts |= value; | |||
1102 | s->errintsts |= (value >> 16) & s->errintstsen; | |||
1103 | if (s->acmd12errsts) { | |||
1104 | s->errintsts |= SDHC_EIS_CMD12ERR0x0100; | |||
1105 | } | |||
1106 | if (s->errintsts) { | |||
1107 | s->norintsts |= SDHC_NIS_ERR0x8000; | |||
1108 | } | |||
1109 | sdhci_update_irq(s); | |||
1110 | break; | |||
1111 | default: | |||
1112 | ERRPRINT("bad %ub write offset: addr[0x%04x] <- %u(0x%x)\n",do { } while (0) | |||
1113 | size, offset, value >> shift, value >> shift)do { } while (0); | |||
1114 | break; | |||
1115 | } | |||
1116 | DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n",do { } while (0) | |||
1117 | size, offset, value >> shift, value >> shift)do { } while (0); | |||
1118 | } | |||
1119 | ||||
1120 | static uint64_t | |||
1121 | sdhci_readfn(void *opaque, hwaddr offset, unsigned size) | |||
1122 | { | |||
1123 | SDHCIState *s = (SDHCIState *)opaque; | |||
1124 | ||||
1125 | return SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 1125, __func__ ))->mem_read(s, offset, size); | |||
1126 | } | |||
1127 | ||||
1128 | static void | |||
1129 | sdhci_writefn(void *opaque, hwaddr off, uint64_t val, unsigned sz) | |||
1130 | { | |||
1131 | SDHCIState *s = (SDHCIState *)opaque; | |||
1132 | ||||
1133 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 1133, __func__ ))->mem_write(s, off, val, sz); | |||
1134 | } | |||
1135 | ||||
1136 | static const MemoryRegionOps sdhci_mmio_ops = { | |||
1137 | .read = sdhci_readfn, | |||
1138 | .write = sdhci_writefn, | |||
1139 | .valid = { | |||
1140 | .min_access_size = 1, | |||
1141 | .max_access_size = 4, | |||
1142 | .unaligned = false0 | |||
1143 | }, | |||
1144 | .endianness = DEVICE_LITTLE_ENDIAN, | |||
1145 | }; | |||
1146 | ||||
1147 | static inline unsigned int sdhci_get_fifolen(SDHCIState *s) | |||
1148 | { | |||
1149 | switch (SDHC_CAPAB_BLOCKSIZE(s->capareg)(((s->capareg) >> 16) & 0x3)) { | |||
1150 | case 0: | |||
1151 | return 512; | |||
1152 | case 1: | |||
1153 | return 1024; | |||
1154 | case 2: | |||
1155 | return 2048; | |||
1156 | default: | |||
1157 | hw_error("SDHC: unsupported value for maximum block size\n"); | |||
1158 | return 0; | |||
1159 | } | |||
1160 | } | |||
1161 | ||||
1162 | static void sdhci_initfn(Object *obj) | |||
1163 | { | |||
1164 | SDHCIState *s = SDHCI(obj)((SDHCIState *)object_dynamic_cast_assert(((Object *)((obj))) , ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c" , 1164, __func__)); | |||
1165 | DriveInfo *di; | |||
1166 | ||||
1167 | di = drive_get_next(IF_SD); | |||
1168 | s->card = sd_init(di ? di->bdrv : NULL((void*)0), false0); | |||
1169 | if (s->card == NULL((void*)0)) { | |||
1170 | exit(1); | |||
1171 | } | |||
1172 | s->eject_cb = qemu_allocate_irqs(sdhci_insert_eject_cb, s, 1)[0]; | |||
1173 | s->ro_cb = qemu_allocate_irqs(sdhci_card_readonly_cb, s, 1)[0]; | |||
1174 | sd_set_cb(s->card, s->ro_cb, s->eject_cb); | |||
1175 | ||||
1176 | s->insert_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_raise_insertion_irq, s); | |||
1177 | s->transfer_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sdhci_do_data_transfer, s); | |||
1178 | } | |||
1179 | ||||
1180 | static void sdhci_uninitfn(Object *obj) | |||
1181 | { | |||
1182 | SDHCIState *s = SDHCI(obj)((SDHCIState *)object_dynamic_cast_assert(((Object *)((obj))) , ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c" , 1182, __func__)); | |||
1183 | ||||
1184 | timer_del(s->insert_timer); | |||
1185 | timer_free(s->insert_timer); | |||
1186 | timer_del(s->transfer_timer); | |||
1187 | timer_free(s->transfer_timer); | |||
1188 | qemu_free_irqs(&s->eject_cb); | |||
1189 | qemu_free_irqs(&s->ro_cb); | |||
1190 | ||||
1191 | if (s->fifo_buffer) { | |||
1192 | g_free(s->fifo_buffer); | |||
1193 | s->fifo_buffer = NULL((void*)0); | |||
1194 | } | |||
1195 | } | |||
1196 | ||||
1197 | const VMStateDescription sdhci_vmstate = { | |||
1198 | .name = "sdhci", | |||
1199 | .version_id = 1, | |||
1200 | .minimum_version_id = 1, | |||
1201 | .fields = (VMStateField[]) { | |||
1202 | VMSTATE_UINT32(sdmasysad, SDHCIState){ .name = ("sdmasysad"), .version_id = (0), .field_exists = ( ((void*)0)), .size = sizeof(uint32_t), .info = &(vmstate_info_uint32 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , sdmasysad) + ((uint32_t*)0 - (typeof(((SDHCIState *)0)-> sdmasysad)*)0)), }, | |||
1203 | VMSTATE_UINT16(blksize, SDHCIState){ .name = ("blksize"), .version_id = (0), .field_exists = ((( void*)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , blksize) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)->blksize )*)0)), }, | |||
1204 | VMSTATE_UINT16(blkcnt, SDHCIState){ .name = ("blkcnt"), .version_id = (0), .field_exists = (((void *)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , blkcnt) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)->blkcnt )*)0)), }, | |||
1205 | VMSTATE_UINT32(argument, SDHCIState){ .name = ("argument"), .version_id = (0), .field_exists = (( (void*)0)), .size = sizeof(uint32_t), .info = &(vmstate_info_uint32 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , argument) + ((uint32_t*)0 - (typeof(((SDHCIState *)0)->argument )*)0)), }, | |||
1206 | VMSTATE_UINT16(trnmod, SDHCIState){ .name = ("trnmod"), .version_id = (0), .field_exists = (((void *)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , trnmod) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)->trnmod )*)0)), }, | |||
1207 | VMSTATE_UINT16(cmdreg, SDHCIState){ .name = ("cmdreg"), .version_id = (0), .field_exists = (((void *)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , cmdreg) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)->cmdreg )*)0)), }, | |||
1208 | VMSTATE_UINT32_ARRAY(rspreg, SDHCIState, 4){ .name = ("rspreg"), .version_id = (0), .num = (4), .info = & (vmstate_info_uint32), .size = sizeof(uint32_t), .flags = VMS_ARRAY , .offset = (__builtin_offsetof(SDHCIState, rspreg) + ((uint32_t (*)[4])0 - (typeof(((SDHCIState *)0)->rspreg)*)0)), }, | |||
1209 | VMSTATE_UINT32(prnsts, SDHCIState){ .name = ("prnsts"), .version_id = (0), .field_exists = (((void *)0)), .size = sizeof(uint32_t), .info = &(vmstate_info_uint32 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , prnsts) + ((uint32_t*)0 - (typeof(((SDHCIState *)0)->prnsts )*)0)), }, | |||
1210 | VMSTATE_UINT8(hostctl, SDHCIState){ .name = ("hostctl"), .version_id = (0), .field_exists = ((( void*)0)), .size = sizeof(uint8_t), .info = &(vmstate_info_uint8 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , hostctl) + ((uint8_t*)0 - (typeof(((SDHCIState *)0)->hostctl )*)0)), }, | |||
1211 | VMSTATE_UINT8(pwrcon, SDHCIState){ .name = ("pwrcon"), .version_id = (0), .field_exists = (((void *)0)), .size = sizeof(uint8_t), .info = &(vmstate_info_uint8 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , pwrcon) + ((uint8_t*)0 - (typeof(((SDHCIState *)0)->pwrcon )*)0)), }, | |||
1212 | VMSTATE_UINT8(blkgap, SDHCIState){ .name = ("blkgap"), .version_id = (0), .field_exists = (((void *)0)), .size = sizeof(uint8_t), .info = &(vmstate_info_uint8 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , blkgap) + ((uint8_t*)0 - (typeof(((SDHCIState *)0)->blkgap )*)0)), }, | |||
1213 | VMSTATE_UINT8(wakcon, SDHCIState){ .name = ("wakcon"), .version_id = (0), .field_exists = (((void *)0)), .size = sizeof(uint8_t), .info = &(vmstate_info_uint8 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , wakcon) + ((uint8_t*)0 - (typeof(((SDHCIState *)0)->wakcon )*)0)), }, | |||
1214 | VMSTATE_UINT16(clkcon, SDHCIState){ .name = ("clkcon"), .version_id = (0), .field_exists = (((void *)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , clkcon) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)->clkcon )*)0)), }, | |||
1215 | VMSTATE_UINT8(timeoutcon, SDHCIState){ .name = ("timeoutcon"), .version_id = (0), .field_exists = ( ((void*)0)), .size = sizeof(uint8_t), .info = &(vmstate_info_uint8 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , timeoutcon) + ((uint8_t*)0 - (typeof(((SDHCIState *)0)-> timeoutcon)*)0)), }, | |||
1216 | VMSTATE_UINT8(admaerr, SDHCIState){ .name = ("admaerr"), .version_id = (0), .field_exists = ((( void*)0)), .size = sizeof(uint8_t), .info = &(vmstate_info_uint8 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , admaerr) + ((uint8_t*)0 - (typeof(((SDHCIState *)0)->admaerr )*)0)), }, | |||
1217 | VMSTATE_UINT16(norintsts, SDHCIState){ .name = ("norintsts"), .version_id = (0), .field_exists = ( ((void*)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , norintsts) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)-> norintsts)*)0)), }, | |||
1218 | VMSTATE_UINT16(errintsts, SDHCIState){ .name = ("errintsts"), .version_id = (0), .field_exists = ( ((void*)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , errintsts) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)-> errintsts)*)0)), }, | |||
1219 | VMSTATE_UINT16(norintstsen, SDHCIState){ .name = ("norintstsen"), .version_id = (0), .field_exists = (((void*)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , norintstsen) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)-> norintstsen)*)0)), }, | |||
1220 | VMSTATE_UINT16(errintstsen, SDHCIState){ .name = ("errintstsen"), .version_id = (0), .field_exists = (((void*)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , errintstsen) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)-> errintstsen)*)0)), }, | |||
1221 | VMSTATE_UINT16(norintsigen, SDHCIState){ .name = ("norintsigen"), .version_id = (0), .field_exists = (((void*)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , norintsigen) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)-> norintsigen)*)0)), }, | |||
1222 | VMSTATE_UINT16(errintsigen, SDHCIState){ .name = ("errintsigen"), .version_id = (0), .field_exists = (((void*)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , errintsigen) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)-> errintsigen)*)0)), }, | |||
1223 | VMSTATE_UINT16(acmd12errsts, SDHCIState){ .name = ("acmd12errsts"), .version_id = (0), .field_exists = (((void*)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , acmd12errsts) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)-> acmd12errsts)*)0)), }, | |||
1224 | VMSTATE_UINT16(data_count, SDHCIState){ .name = ("data_count"), .version_id = (0), .field_exists = ( ((void*)0)), .size = sizeof(uint16_t), .info = &(vmstate_info_uint16 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , data_count) + ((uint16_t*)0 - (typeof(((SDHCIState *)0)-> data_count)*)0)), }, | |||
1225 | VMSTATE_UINT64(admasysaddr, SDHCIState){ .name = ("admasysaddr"), .version_id = (0), .field_exists = (((void*)0)), .size = sizeof(uint64_t), .info = &(vmstate_info_uint64 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , admasysaddr) + ((uint64_t*)0 - (typeof(((SDHCIState *)0)-> admasysaddr)*)0)), }, | |||
1226 | VMSTATE_UINT8(stopped_state, SDHCIState){ .name = ("stopped_state"), .version_id = (0), .field_exists = (((void*)0)), .size = sizeof(uint8_t), .info = &(vmstate_info_uint8 ), .flags = VMS_SINGLE, .offset = (__builtin_offsetof(SDHCIState , stopped_state) + ((uint8_t*)0 - (typeof(((SDHCIState *)0)-> stopped_state)*)0)), }, | |||
1227 | VMSTATE_VBUFFER_UINT32(fifo_buffer, SDHCIState, 1, NULL, 0, buf_maxsz){ .name = ("fifo_buffer"), .version_id = (1), .field_exists = (((void*)0)), .size_offset = (__builtin_offsetof(SDHCIState, buf_maxsz) + ((uint32_t*)0 - (typeof(((SDHCIState *)0)->buf_maxsz )*)0)), .info = &vmstate_info_buffer, .flags = VMS_VBUFFER |VMS_POINTER, .offset = __builtin_offsetof(SDHCIState, fifo_buffer ), .start = (0), }, | |||
1228 | VMSTATE_TIMER(insert_timer, SDHCIState){ .name = ("insert_timer"), .version_id = (0), .info = &( vmstate_info_timer), .size = sizeof(QEMUTimer *), .flags = VMS_SINGLE |VMS_POINTER, .offset = (__builtin_offsetof(SDHCIState, insert_timer ) + ((QEMUTimer **)0 - (typeof(((SDHCIState *)0)->insert_timer )*)0)), }, | |||
1229 | VMSTATE_TIMER(transfer_timer, SDHCIState){ .name = ("transfer_timer"), .version_id = (0), .info = & (vmstate_info_timer), .size = sizeof(QEMUTimer *), .flags = VMS_SINGLE |VMS_POINTER, .offset = (__builtin_offsetof(SDHCIState, transfer_timer ) + ((QEMUTimer **)0 - (typeof(((SDHCIState *)0)->transfer_timer )*)0)), }, | |||
1230 | VMSTATE_END_OF_LIST(){} | |||
1231 | } | |||
1232 | }; | |||
1233 | ||||
1234 | /* Capabilities registers provide information on supported features of this | |||
1235 | * specific host controller implementation */ | |||
1236 | static Property sdhci_properties[] = { | |||
1237 | DEFINE_PROP_HEX32("capareg", SDHCIState, capareg,{ .name = ("capareg"), .info = &(qdev_prop_hex32), .offset = __builtin_offsetof(SDHCIState, capareg) + ((uint32_t*)0 - ( typeof(((SDHCIState *)0)->capareg)*)0), .qtype = QTYPE_QINT , .defval = (uint32_t)((0ul << 28) | (1ul << 26) | (0ul << 25) | (1ul << 24) | (0ul << 23) | ( 1ul << 22) | (1ul << 21) | (1ul << 20) | (1ul << 19) | (0ul << 16) | (0ul << 8) | (1ul << 7) | (0ul)), } | |||
1238 | SDHC_CAPAB_REG_DEFAULT){ .name = ("capareg"), .info = &(qdev_prop_hex32), .offset = __builtin_offsetof(SDHCIState, capareg) + ((uint32_t*)0 - ( typeof(((SDHCIState *)0)->capareg)*)0), .qtype = QTYPE_QINT , .defval = (uint32_t)((0ul << 28) | (1ul << 26) | (0ul << 25) | (1ul << 24) | (0ul << 23) | ( 1ul << 22) | (1ul << 21) | (1ul << 20) | (1ul << 19) | (0ul << 16) | (0ul << 8) | (1ul << 7) | (0ul)), }, | |||
1239 | DEFINE_PROP_HEX32("maxcurr", SDHCIState, maxcurr, 0){ .name = ("maxcurr"), .info = &(qdev_prop_hex32), .offset = __builtin_offsetof(SDHCIState, maxcurr) + ((uint32_t*)0 - ( typeof(((SDHCIState *)0)->maxcurr)*)0), .qtype = QTYPE_QINT , .defval = (uint32_t)0, }, | |||
1240 | DEFINE_PROP_END_OF_LIST(){}, | |||
1241 | }; | |||
1242 | ||||
1243 | static void sdhci_realize(DeviceState *dev, Error ** errp) | |||
1244 | { | |||
1245 | SDHCIState *s = SDHCI(dev)((SDHCIState *)object_dynamic_cast_assert(((Object *)((dev))) , ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c" , 1245, __func__)); | |||
1246 | SysBusDevice *sbd = SYS_BUS_DEVICE(dev)((SysBusDevice *)object_dynamic_cast_assert(((Object *)((dev) )), ("sys-bus-device"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c" , 1246, __func__)); | |||
1247 | ||||
1248 | s->buf_maxsz = sdhci_get_fifolen(s); | |||
1249 | s->fifo_buffer = g_malloc0(s->buf_maxsz); | |||
1250 | sysbus_init_irq(sbd, &s->irq); | |||
1251 | memory_region_init_io(&s->iomem, OBJECT(s)((Object *)(s)), &sdhci_mmio_ops, s, "sdhci", | |||
1252 | SDHC_REGISTERS_MAP_SIZE0x100); | |||
1253 | sysbus_init_mmio(sbd, &s->iomem); | |||
1254 | } | |||
1255 | ||||
1256 | static void sdhci_generic_reset(DeviceState *ds) | |||
1257 | { | |||
1258 | SDHCIState *s = SDHCI(ds)((SDHCIState *)object_dynamic_cast_assert(((Object *)((ds))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c" , 1258, __func__)); | |||
1259 | SDHCI_GET_CLASS(s)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)(object_get_class(((Object *)((s)))))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c", 1259, __func__ ))->reset(s); | |||
1260 | } | |||
1261 | ||||
1262 | static void sdhci_class_init(ObjectClass *klass, void *data) | |||
1263 | { | |||
1264 | DeviceClass *dc = DEVICE_CLASS(klass)((DeviceClass *)object_class_dynamic_cast_assert(((ObjectClass *)((klass))), ("device"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c" , 1264, __func__)); | |||
1265 | SDHCIClass *k = SDHCI_CLASS(klass)((SDHCIClass *)object_class_dynamic_cast_assert(((ObjectClass *)((klass))), ("generic-sdhci"), "/home/stefan/src/qemu/qemu.org/qemu/hw/sd/sdhci.c" , 1265, __func__)); | |||
1266 | ||||
1267 | dc->vmsd = &sdhci_vmstate; | |||
1268 | dc->props = sdhci_properties; | |||
1269 | dc->reset = sdhci_generic_reset; | |||
1270 | dc->realize = sdhci_realize; | |||
1271 | ||||
1272 | k->reset = sdhci_reset; | |||
1273 | k->mem_read = sdhci_read; | |||
1274 | k->mem_write = sdhci_write; | |||
1275 | k->send_command = sdhci_send_command; | |||
1276 | k->can_issue_command = sdhci_can_issue_command; | |||
1277 | k->data_transfer = sdhci_data_transfer; | |||
1278 | k->end_data_transfer = sdhci_end_transfer; | |||
1279 | k->do_sdma_single = sdhci_sdma_transfer_single_block; | |||
1280 | k->do_sdma_multi = sdhci_sdma_transfer_multi_blocks; | |||
1281 | k->do_adma = sdhci_do_adma; | |||
1282 | k->read_block_from_card = sdhci_read_block_from_card; | |||
1283 | k->write_block_to_card = sdhci_write_block_to_card; | |||
1284 | k->bdata_read = sdhci_read_dataport; | |||
1285 | k->bdata_write = sdhci_write_dataport; | |||
1286 | } | |||
1287 | ||||
1288 | static const TypeInfo sdhci_type_info = { | |||
1289 | .name = TYPE_SDHCI"generic-sdhci", | |||
1290 | .parent = TYPE_SYS_BUS_DEVICE"sys-bus-device", | |||
1291 | .instance_size = sizeof(SDHCIState), | |||
1292 | .instance_init = sdhci_initfn, | |||
1293 | .instance_finalize = sdhci_uninitfn, | |||
1294 | .class_init = sdhci_class_init, | |||
1295 | .class_size = sizeof(SDHCIClass) | |||
1296 | }; | |||
1297 | ||||
1298 | static void sdhci_register_types(void) | |||
1299 | { | |||
1300 | type_register_static(&sdhci_type_info); | |||
1301 | } | |||
1302 | ||||
1303 | type_init(sdhci_register_types)static void __attribute__((constructor)) do_qemu_init_sdhci_register_types (void) { register_module_init(sdhci_register_types, MODULE_INIT_QOM ); } |