2 * SD Association Host Standard Specification v2.0 controller emulation
4 * Datasheet: PartA2_SD_Host_Controller_Simplified_Specification_Ver2.00.pdf
6 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
7 * Mitsyanko Igor <i.mitsyanko@samsung.com>
8 * Peter A.G. Crosthwaite <peter.crosthwaite@petalogix.com>
10 * Based on MMC controller for Samsung S5PC1xx-based board emulation
11 * by Alexey Merkulov and Vladimir Monakhov.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 * See the GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include "qemu/osdep.h"
28 #include "qemu/units.h"
29 #include "qemu/error-report.h"
30 #include "qapi/error.h"
32 #include "hw/qdev-properties.h"
33 #include "sysemu/dma.h"
34 #include "qemu/timer.h"
35 #include "qemu/bitops.h"
36 #include "hw/sd/sdhci.h"
37 #include "migration/vmstate.h"
38 #include "sdhci-internal.h"
40 #include "qemu/module.h"
42 #include "qom/object.h"
44 #define TYPE_SDHCI_BUS "sdhci-bus"
45 /* This is reusing the SDBus typedef from SD_BUS */
46 DECLARE_INSTANCE_CHECKER(SDBus
, SDHCI_BUS
,
49 #define MASKED_WRITE(reg, mask, val) (reg = (reg & (mask)) | (val))
51 static inline unsigned int sdhci_get_fifolen(SDHCIState
*s
)
53 return 1 << (9 + FIELD_EX32(s
->capareg
, SDHC_CAPAB
, MAXBLOCKLENGTH
));
56 /* return true on error */
57 static bool sdhci_check_capab_freq_range(SDHCIState
*s
, const char *desc
,
58 uint8_t freq
, Error
**errp
)
60 if (s
->sd_spec_version
>= 3) {
68 error_setg(errp
, "SD %s clock frequency can have value"
69 "in range 0-63 only", desc
);
75 static void sdhci_check_capareg(SDHCIState
*s
, Error
**errp
)
77 uint64_t msk
= s
->capareg
;
81 switch (s
->sd_spec_version
) {
83 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, BUS64BIT_V4
);
84 trace_sdhci_capareg("64-bit system bus (v4)", val
);
85 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, BUS64BIT_V4
, 0);
87 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, UHS_II
);
88 trace_sdhci_capareg("UHS-II", val
);
89 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, UHS_II
, 0);
91 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, ADMA3
);
92 trace_sdhci_capareg("ADMA3", val
);
93 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, ADMA3
, 0);
97 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, ASYNC_INT
);
98 trace_sdhci_capareg("async interrupt", val
);
99 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, ASYNC_INT
, 0);
101 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, SLOT_TYPE
);
103 error_setg(errp
, "slot-type not supported");
106 trace_sdhci_capareg("slot type", val
);
107 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, SLOT_TYPE
, 0);
110 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, EMBEDDED_8BIT
);
111 trace_sdhci_capareg("8-bit bus", val
);
113 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, EMBEDDED_8BIT
, 0);
115 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, BUS_SPEED
);
116 trace_sdhci_capareg("bus speed mask", val
);
117 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, BUS_SPEED
, 0);
119 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, DRIVER_STRENGTH
);
120 trace_sdhci_capareg("driver strength mask", val
);
121 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, DRIVER_STRENGTH
, 0);
123 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, TIMER_RETUNING
);
124 trace_sdhci_capareg("timer re-tuning", val
);
125 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, TIMER_RETUNING
, 0);
127 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, SDR50_TUNING
);
128 trace_sdhci_capareg("use SDR50 tuning", val
);
129 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, SDR50_TUNING
, 0);
131 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, RETUNING_MODE
);
132 trace_sdhci_capareg("re-tuning mode", val
);
133 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, RETUNING_MODE
, 0);
135 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, CLOCK_MULT
);
136 trace_sdhci_capareg("clock multiplier", val
);
137 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, CLOCK_MULT
, 0);
140 case 2: /* default version */
141 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, ADMA2
);
142 trace_sdhci_capareg("ADMA2", val
);
143 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, ADMA2
, 0);
145 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, ADMA1
);
146 trace_sdhci_capareg("ADMA1", val
);
147 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, ADMA1
, 0);
149 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, BUS64BIT
);
150 trace_sdhci_capareg("64-bit system bus (v3)", val
);
151 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, BUS64BIT
, 0);
155 y
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, TOUNIT
);
156 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, TOUNIT
, 0);
158 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, TOCLKFREQ
);
159 trace_sdhci_capareg(y
? "timeout (MHz)" : "Timeout (KHz)", val
);
160 if (sdhci_check_capab_freq_range(s
, "timeout", val
, errp
)) {
163 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, TOCLKFREQ
, 0);
165 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, BASECLKFREQ
);
166 trace_sdhci_capareg(y
? "base (MHz)" : "Base (KHz)", val
);
167 if (sdhci_check_capab_freq_range(s
, "base", val
, errp
)) {
170 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, BASECLKFREQ
, 0);
172 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, MAXBLOCKLENGTH
);
174 error_setg(errp
, "block size can be 512, 1024 or 2048 only");
177 trace_sdhci_capareg("max block length", sdhci_get_fifolen(s
));
178 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, MAXBLOCKLENGTH
, 0);
180 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, HIGHSPEED
);
181 trace_sdhci_capareg("high speed", val
);
182 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, HIGHSPEED
, 0);
184 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, SDMA
);
185 trace_sdhci_capareg("SDMA", val
);
186 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, SDMA
, 0);
188 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, SUSPRESUME
);
189 trace_sdhci_capareg("suspend/resume", val
);
190 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, SUSPRESUME
, 0);
192 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, V33
);
193 trace_sdhci_capareg("3.3v", val
);
194 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, V33
, 0);
196 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, V30
);
197 trace_sdhci_capareg("3.0v", val
);
198 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, V30
, 0);
200 val
= FIELD_EX64(s
->capareg
, SDHC_CAPAB
, V18
);
201 trace_sdhci_capareg("1.8v", val
);
202 msk
= FIELD_DP64(msk
, SDHC_CAPAB
, V18
, 0);
206 error_setg(errp
, "Unsupported spec version: %u", s
->sd_spec_version
);
209 qemu_log_mask(LOG_UNIMP
,
210 "SDHCI: unknown CAPAB mask: 0x%016" PRIx64
"\n", msk
);
214 static uint8_t sdhci_slotint(SDHCIState
*s
)
216 return (s
->norintsts
& s
->norintsigen
) || (s
->errintsts
& s
->errintsigen
) ||
217 ((s
->norintsts
& SDHC_NIS_INSERT
) && (s
->wakcon
& SDHC_WKUP_ON_INS
)) ||
218 ((s
->norintsts
& SDHC_NIS_REMOVE
) && (s
->wakcon
& SDHC_WKUP_ON_RMV
));
221 /* Return true if IRQ was pending and delivered */
222 static bool sdhci_update_irq(SDHCIState
*s
)
224 bool pending
= sdhci_slotint(s
);
226 qemu_set_irq(s
->irq
, pending
);
231 static void sdhci_raise_insertion_irq(void *opaque
)
233 SDHCIState
*s
= (SDHCIState
*)opaque
;
235 if (s
->norintsts
& SDHC_NIS_REMOVE
) {
236 timer_mod(s
->insert_timer
,
237 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + SDHC_INSERTION_DELAY
);
239 s
->prnsts
= 0x1ff0000;
240 if (s
->norintstsen
& SDHC_NISEN_INSERT
) {
241 s
->norintsts
|= SDHC_NIS_INSERT
;
247 static void sdhci_set_inserted(DeviceState
*dev
, bool level
)
249 SDHCIState
*s
= (SDHCIState
*)dev
;
251 trace_sdhci_set_inserted(level
? "insert" : "eject");
252 if ((s
->norintsts
& SDHC_NIS_REMOVE
) && level
) {
253 /* Give target some time to notice card ejection */
254 timer_mod(s
->insert_timer
,
255 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + SDHC_INSERTION_DELAY
);
258 s
->prnsts
= 0x1ff0000;
259 if (s
->norintstsen
& SDHC_NISEN_INSERT
) {
260 s
->norintsts
|= SDHC_NIS_INSERT
;
263 s
->prnsts
= 0x1fa0000;
264 s
->pwrcon
&= ~SDHC_POWER_ON
;
265 s
->clkcon
&= ~SDHC_CLOCK_SDCLK_EN
;
266 if (s
->norintstsen
& SDHC_NISEN_REMOVE
) {
267 s
->norintsts
|= SDHC_NIS_REMOVE
;
274 static void sdhci_set_readonly(DeviceState
*dev
, bool level
)
276 SDHCIState
*s
= (SDHCIState
*)dev
;
279 s
->prnsts
&= ~SDHC_WRITE_PROTECT
;
282 s
->prnsts
|= SDHC_WRITE_PROTECT
;
286 static void sdhci_reset(SDHCIState
*s
)
288 DeviceState
*dev
= DEVICE(s
);
290 timer_del(s
->insert_timer
);
291 timer_del(s
->transfer_timer
);
293 /* Set all registers to 0. Capabilities/Version registers are not cleared
294 * and assumed to always preserve their value, given to them during
296 memset(&s
->sdmasysad
, 0, (uintptr_t)&s
->capareg
- (uintptr_t)&s
->sdmasysad
);
298 /* Reset other state based on current card insertion/readonly status */
299 sdhci_set_inserted(dev
, sdbus_get_inserted(&s
->sdbus
));
300 sdhci_set_readonly(dev
, sdbus_get_readonly(&s
->sdbus
));
303 s
->stopped_state
= sdhc_not_stopped
;
304 s
->pending_insert_state
= false;
307 static void sdhci_poweron_reset(DeviceState
*dev
)
309 /* QOM (ie power-on) reset. This is identical to reset
310 * commanded via device register apart from handling of the
311 * 'pending insert on powerup' quirk.
313 SDHCIState
*s
= (SDHCIState
*)dev
;
317 if (s
->pending_insert_quirk
) {
318 s
->pending_insert_state
= true;
322 static void sdhci_data_transfer(void *opaque
);
324 static void sdhci_send_command(SDHCIState
*s
)
327 uint8_t response
[16];
332 request
.cmd
= s
->cmdreg
>> 8;
333 request
.arg
= s
->argument
;
335 trace_sdhci_send_command(request
.cmd
, request
.arg
);
336 rlen
= sdbus_do_command(&s
->sdbus
, &request
, response
);
338 if (s
->cmdreg
& SDHC_CMD_RESPONSE
) {
340 s
->rspreg
[0] = ldl_be_p(response
);
341 s
->rspreg
[1] = s
->rspreg
[2] = s
->rspreg
[3] = 0;
342 trace_sdhci_response4(s
->rspreg
[0]);
343 } else if (rlen
== 16) {
344 s
->rspreg
[0] = ldl_be_p(&response
[11]);
345 s
->rspreg
[1] = ldl_be_p(&response
[7]);
346 s
->rspreg
[2] = ldl_be_p(&response
[3]);
347 s
->rspreg
[3] = (response
[0] << 16) | (response
[1] << 8) |
349 trace_sdhci_response16(s
->rspreg
[3], s
->rspreg
[2],
350 s
->rspreg
[1], s
->rspreg
[0]);
352 trace_sdhci_error("timeout waiting for command response");
353 if (s
->errintstsen
& SDHC_EISEN_CMDTIMEOUT
) {
354 s
->errintsts
|= SDHC_EIS_CMDTIMEOUT
;
355 s
->norintsts
|= SDHC_NIS_ERR
;
359 if (!(s
->quirks
& SDHCI_QUIRK_NO_BUSY_IRQ
) &&
360 (s
->norintstsen
& SDHC_NISEN_TRSCMP
) &&
361 (s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
) {
362 s
->norintsts
|= SDHC_NIS_TRSCMP
;
366 if (s
->norintstsen
& SDHC_NISEN_CMDCMP
) {
367 s
->norintsts
|= SDHC_NIS_CMDCMP
;
372 if (s
->blksize
&& (s
->cmdreg
& SDHC_CMD_DATA_PRESENT
)) {
374 sdhci_data_transfer(s
);
378 static void sdhci_end_transfer(SDHCIState
*s
)
380 /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */
381 if ((s
->trnmod
& SDHC_TRNS_ACMD12
) != 0) {
383 uint8_t response
[16];
387 trace_sdhci_end_transfer(request
.cmd
, request
.arg
);
388 sdbus_do_command(&s
->sdbus
, &request
, response
);
389 /* Auto CMD12 response goes to the upper Response register */
390 s
->rspreg
[3] = ldl_be_p(response
);
393 s
->prnsts
&= ~(SDHC_DOING_READ
| SDHC_DOING_WRITE
|
394 SDHC_DAT_LINE_ACTIVE
| SDHC_DATA_INHIBIT
|
395 SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
);
397 if (s
->norintstsen
& SDHC_NISEN_TRSCMP
) {
398 s
->norintsts
|= SDHC_NIS_TRSCMP
;
405 * Programmed i/o data transfer
407 #define BLOCK_SIZE_MASK (4 * KiB - 1)
409 /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
410 static void sdhci_read_block_from_card(SDHCIState
*s
)
412 const uint16_t blk_size
= s
->blksize
& BLOCK_SIZE_MASK
;
414 if ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
415 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) {
419 if (!FIELD_EX32(s
->hostctl2
, SDHC_HOSTCTL2
, EXECUTE_TUNING
)) {
420 /* Device is not in tuning */
421 sdbus_read_data(&s
->sdbus
, s
->fifo_buffer
, blk_size
);
424 if (FIELD_EX32(s
->hostctl2
, SDHC_HOSTCTL2
, EXECUTE_TUNING
)) {
425 /* Device is in tuning */
426 s
->hostctl2
&= ~R_SDHC_HOSTCTL2_EXECUTE_TUNING_MASK
;
427 s
->hostctl2
|= R_SDHC_HOSTCTL2_SAMPLING_CLKSEL_MASK
;
428 s
->prnsts
&= ~(SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_READ
|
433 /* New data now available for READ through Buffer Port Register */
434 s
->prnsts
|= SDHC_DATA_AVAILABLE
;
435 if (s
->norintstsen
& SDHC_NISEN_RBUFRDY
) {
436 s
->norintsts
|= SDHC_NIS_RBUFRDY
;
439 /* Clear DAT line active status if that was the last block */
440 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
441 ((s
->trnmod
& SDHC_TRNS_MULTI
) && s
->blkcnt
== 1)) {
442 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
445 /* If stop at block gap request was set and it's not the last block of
446 * data - generate Block Event interrupt */
447 if (s
->stopped_state
== sdhc_gap_read
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
449 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
450 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
451 s
->norintsts
|= SDHC_EIS_BLKGAP
;
459 /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
460 static uint32_t sdhci_read_dataport(SDHCIState
*s
, unsigned size
)
465 /* first check that a valid data exists in host controller input buffer */
466 if ((s
->prnsts
& SDHC_DATA_AVAILABLE
) == 0) {
467 trace_sdhci_error("read from empty buffer");
471 for (i
= 0; i
< size
; i
++) {
472 value
|= s
->fifo_buffer
[s
->data_count
] << i
* 8;
474 /* check if we've read all valid data (blksize bytes) from buffer */
475 if ((s
->data_count
) >= (s
->blksize
& BLOCK_SIZE_MASK
)) {
476 trace_sdhci_read_dataport(s
->data_count
);
477 s
->prnsts
&= ~SDHC_DATA_AVAILABLE
; /* no more data in a buffer */
478 s
->data_count
= 0; /* next buff read must start at position [0] */
480 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
484 /* if that was the last block of data */
485 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
486 ((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) ||
487 /* stop at gap request */
488 (s
->stopped_state
== sdhc_gap_read
&&
489 !(s
->prnsts
& SDHC_DAT_LINE_ACTIVE
))) {
490 sdhci_end_transfer(s
);
491 } else { /* if there are more data, read next block from card */
492 sdhci_read_block_from_card(s
);
501 /* Write data from host controller FIFO to card */
502 static void sdhci_write_block_to_card(SDHCIState
*s
)
504 if (s
->prnsts
& SDHC_SPACE_AVAILABLE
) {
505 if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
506 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
512 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
513 if (s
->blkcnt
== 0) {
520 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
, s
->blksize
& BLOCK_SIZE_MASK
);
522 /* Next data can be written through BUFFER DATORT register */
523 s
->prnsts
|= SDHC_SPACE_AVAILABLE
;
525 /* Finish transfer if that was the last block of data */
526 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
527 ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
528 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0))) {
529 sdhci_end_transfer(s
);
530 } else if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
531 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
534 /* Generate Block Gap Event if requested and if not the last block */
535 if (s
->stopped_state
== sdhc_gap_write
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
537 s
->prnsts
&= ~SDHC_DOING_WRITE
;
538 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
539 s
->norintsts
|= SDHC_EIS_BLKGAP
;
541 sdhci_end_transfer(s
);
547 /* Write @size bytes of @value data to host controller @s Buffer Data Port
549 static void sdhci_write_dataport(SDHCIState
*s
, uint32_t value
, unsigned size
)
553 /* Check that there is free space left in a buffer */
554 if (!(s
->prnsts
& SDHC_SPACE_AVAILABLE
)) {
555 trace_sdhci_error("Can't write to data buffer: buffer full");
559 for (i
= 0; i
< size
; i
++) {
560 s
->fifo_buffer
[s
->data_count
] = value
& 0xFF;
563 if (s
->data_count
>= (s
->blksize
& BLOCK_SIZE_MASK
)) {
564 trace_sdhci_write_dataport(s
->data_count
);
566 s
->prnsts
&= ~SDHC_SPACE_AVAILABLE
;
567 if (s
->prnsts
& SDHC_DOING_WRITE
) {
568 sdhci_write_block_to_card(s
);
575 * Single DMA data transfer
578 /* Multi block SDMA transfer */
579 static void sdhci_sdma_transfer_multi_blocks(SDHCIState
*s
)
581 bool page_aligned
= false;
583 const uint16_t block_size
= s
->blksize
& BLOCK_SIZE_MASK
;
584 uint32_t boundary_chk
= 1 << (((s
->blksize
& ~BLOCK_SIZE_MASK
) >> 12) + 12);
585 uint32_t boundary_count
= boundary_chk
- (s
->sdmasysad
% boundary_chk
);
587 if (!(s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) || !s
->blkcnt
) {
588 qemu_log_mask(LOG_UNIMP
, "infinite transfer is not supported\n");
592 /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
593 * possible stop at page boundary if initial address is not page aligned,
594 * allow them to work properly */
595 if ((s
->sdmasysad
% boundary_chk
) == 0) {
599 if (s
->trnmod
& SDHC_TRNS_READ
) {
600 s
->prnsts
|= SDHC_DOING_READ
| SDHC_DATA_INHIBIT
|
601 SDHC_DAT_LINE_ACTIVE
;
603 if (s
->data_count
== 0) {
604 sdbus_read_data(&s
->sdbus
, s
->fifo_buffer
, block_size
);
606 begin
= s
->data_count
;
607 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
608 s
->data_count
= boundary_count
+ begin
;
611 s
->data_count
= block_size
;
612 boundary_count
-= block_size
- begin
;
613 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
617 dma_memory_write(s
->dma_as
, s
->sdmasysad
,
618 &s
->fifo_buffer
[begin
], s
->data_count
- begin
);
619 s
->sdmasysad
+= s
->data_count
- begin
;
620 if (s
->data_count
== block_size
) {
623 if (page_aligned
&& boundary_count
== 0) {
628 s
->prnsts
|= SDHC_DOING_WRITE
| SDHC_DATA_INHIBIT
|
629 SDHC_DAT_LINE_ACTIVE
;
631 begin
= s
->data_count
;
632 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
633 s
->data_count
= boundary_count
+ begin
;
636 s
->data_count
= block_size
;
637 boundary_count
-= block_size
- begin
;
639 dma_memory_read(s
->dma_as
, s
->sdmasysad
,
640 &s
->fifo_buffer
[begin
], s
->data_count
- begin
);
641 s
->sdmasysad
+= s
->data_count
- begin
;
642 if (s
->data_count
== block_size
) {
643 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
, block_size
);
645 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
649 if (page_aligned
&& boundary_count
== 0) {
655 if (s
->blkcnt
== 0) {
656 sdhci_end_transfer(s
);
658 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
659 s
->norintsts
|= SDHC_NIS_DMA
;
665 /* single block SDMA transfer */
666 static void sdhci_sdma_transfer_single_block(SDHCIState
*s
)
668 uint32_t datacnt
= s
->blksize
& BLOCK_SIZE_MASK
;
670 if (s
->trnmod
& SDHC_TRNS_READ
) {
671 sdbus_read_data(&s
->sdbus
, s
->fifo_buffer
, datacnt
);
672 dma_memory_write(s
->dma_as
, s
->sdmasysad
, s
->fifo_buffer
, datacnt
);
674 dma_memory_read(s
->dma_as
, s
->sdmasysad
, s
->fifo_buffer
, datacnt
);
675 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
, datacnt
);
679 sdhci_end_transfer(s
);
682 typedef struct ADMADescr
{
689 static void get_adma_description(SDHCIState
*s
, ADMADescr
*dscr
)
693 hwaddr entry_addr
= (hwaddr
)s
->admasysaddr
;
694 switch (SDHC_DMA_TYPE(s
->hostctl1
)) {
695 case SDHC_CTRL_ADMA2_32
:
696 dma_memory_read(s
->dma_as
, entry_addr
, &adma2
, sizeof(adma2
));
697 adma2
= le64_to_cpu(adma2
);
698 /* The spec does not specify endianness of descriptor table.
699 * We currently assume that it is LE.
701 dscr
->addr
= (hwaddr
)extract64(adma2
, 32, 32) & ~0x3ull
;
702 dscr
->length
= (uint16_t)extract64(adma2
, 16, 16);
703 dscr
->attr
= (uint8_t)extract64(adma2
, 0, 7);
706 case SDHC_CTRL_ADMA1_32
:
707 dma_memory_read(s
->dma_as
, entry_addr
, &adma1
, sizeof(adma1
));
708 adma1
= le32_to_cpu(adma1
);
709 dscr
->addr
= (hwaddr
)(adma1
& 0xFFFFF000);
710 dscr
->attr
= (uint8_t)extract32(adma1
, 0, 7);
712 if ((dscr
->attr
& SDHC_ADMA_ATTR_ACT_MASK
) == SDHC_ADMA_ATTR_SET_LEN
) {
713 dscr
->length
= (uint16_t)extract32(adma1
, 12, 16);
715 dscr
->length
= 4 * KiB
;
718 case SDHC_CTRL_ADMA2_64
:
719 dma_memory_read(s
->dma_as
, entry_addr
, &dscr
->attr
, 1);
720 dma_memory_read(s
->dma_as
, entry_addr
+ 2, &dscr
->length
, 2);
721 dscr
->length
= le16_to_cpu(dscr
->length
);
722 dma_memory_read(s
->dma_as
, entry_addr
+ 4, &dscr
->addr
, 8);
723 dscr
->addr
= le64_to_cpu(dscr
->addr
);
724 dscr
->attr
&= (uint8_t) ~0xC0;
730 /* Advanced DMA data transfer */
732 static void sdhci_do_adma(SDHCIState
*s
)
734 unsigned int begin
, length
;
735 const uint16_t block_size
= s
->blksize
& BLOCK_SIZE_MASK
;
739 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
&& !s
->blkcnt
) {
740 /* Stop Multiple Transfer */
741 sdhci_end_transfer(s
);
745 for (i
= 0; i
< SDHC_ADMA_DESCS_PER_DELAY
; ++i
) {
746 s
->admaerr
&= ~SDHC_ADMAERR_LENGTH_MISMATCH
;
748 get_adma_description(s
, &dscr
);
749 trace_sdhci_adma_loop(dscr
.addr
, dscr
.length
, dscr
.attr
);
751 if ((dscr
.attr
& SDHC_ADMA_ATTR_VALID
) == 0) {
752 /* Indicate that error occurred in ST_FDS state */
753 s
->admaerr
&= ~SDHC_ADMAERR_STATE_MASK
;
754 s
->admaerr
|= SDHC_ADMAERR_STATE_ST_FDS
;
756 /* Generate ADMA error interrupt */
757 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
758 s
->errintsts
|= SDHC_EIS_ADMAERR
;
759 s
->norintsts
|= SDHC_NIS_ERR
;
766 length
= dscr
.length
? dscr
.length
: 64 * KiB
;
768 switch (dscr
.attr
& SDHC_ADMA_ATTR_ACT_MASK
) {
769 case SDHC_ADMA_ATTR_ACT_TRAN
: /* data transfer */
770 if (s
->trnmod
& SDHC_TRNS_READ
) {
772 if (s
->data_count
== 0) {
773 sdbus_read_data(&s
->sdbus
, s
->fifo_buffer
, block_size
);
775 begin
= s
->data_count
;
776 if ((length
+ begin
) < block_size
) {
777 s
->data_count
= length
+ begin
;
780 s
->data_count
= block_size
;
781 length
-= block_size
- begin
;
783 dma_memory_write(s
->dma_as
, dscr
.addr
,
784 &s
->fifo_buffer
[begin
],
785 s
->data_count
- begin
);
786 dscr
.addr
+= s
->data_count
- begin
;
787 if (s
->data_count
== block_size
) {
789 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
791 if (s
->blkcnt
== 0) {
799 begin
= s
->data_count
;
800 if ((length
+ begin
) < block_size
) {
801 s
->data_count
= length
+ begin
;
804 s
->data_count
= block_size
;
805 length
-= block_size
- begin
;
807 dma_memory_read(s
->dma_as
, dscr
.addr
,
808 &s
->fifo_buffer
[begin
],
809 s
->data_count
- begin
);
810 dscr
.addr
+= s
->data_count
- begin
;
811 if (s
->data_count
== block_size
) {
812 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
, block_size
);
814 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
816 if (s
->blkcnt
== 0) {
823 s
->admasysaddr
+= dscr
.incr
;
825 case SDHC_ADMA_ATTR_ACT_LINK
: /* link to next descriptor table */
826 s
->admasysaddr
= dscr
.addr
;
827 trace_sdhci_adma("link", s
->admasysaddr
);
830 s
->admasysaddr
+= dscr
.incr
;
834 if (dscr
.attr
& SDHC_ADMA_ATTR_INT
) {
835 trace_sdhci_adma("interrupt", s
->admasysaddr
);
836 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
837 s
->norintsts
|= SDHC_NIS_DMA
;
840 if (sdhci_update_irq(s
) && !(dscr
.attr
& SDHC_ADMA_ATTR_END
)) {
841 /* IRQ delivered, reschedule current transfer */
846 /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
847 if (((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
848 (s
->blkcnt
== 0)) || (dscr
.attr
& SDHC_ADMA_ATTR_END
)) {
849 trace_sdhci_adma_transfer_completed();
850 if (length
|| ((dscr
.attr
& SDHC_ADMA_ATTR_END
) &&
851 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
853 trace_sdhci_error("SD/MMC host ADMA length mismatch");
854 s
->admaerr
|= SDHC_ADMAERR_LENGTH_MISMATCH
|
855 SDHC_ADMAERR_STATE_ST_TFR
;
856 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
857 trace_sdhci_error("Set ADMA error flag");
858 s
->errintsts
|= SDHC_EIS_ADMAERR
;
859 s
->norintsts
|= SDHC_NIS_ERR
;
864 sdhci_end_transfer(s
);
870 /* we have unfinished business - reschedule to continue ADMA */
871 timer_mod(s
->transfer_timer
,
872 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + SDHC_TRANSFER_DELAY
);
875 /* Perform data transfer according to controller configuration */
877 static void sdhci_data_transfer(void *opaque
)
879 SDHCIState
*s
= (SDHCIState
*)opaque
;
881 if (s
->trnmod
& SDHC_TRNS_DMA
) {
882 switch (SDHC_DMA_TYPE(s
->hostctl1
)) {
884 if ((s
->blkcnt
== 1) || !(s
->trnmod
& SDHC_TRNS_MULTI
)) {
885 sdhci_sdma_transfer_single_block(s
);
887 sdhci_sdma_transfer_multi_blocks(s
);
891 case SDHC_CTRL_ADMA1_32
:
892 if (!(s
->capareg
& R_SDHC_CAPAB_ADMA1_MASK
)) {
893 trace_sdhci_error("ADMA1 not supported");
899 case SDHC_CTRL_ADMA2_32
:
900 if (!(s
->capareg
& R_SDHC_CAPAB_ADMA2_MASK
)) {
901 trace_sdhci_error("ADMA2 not supported");
907 case SDHC_CTRL_ADMA2_64
:
908 if (!(s
->capareg
& R_SDHC_CAPAB_ADMA2_MASK
) ||
909 !(s
->capareg
& R_SDHC_CAPAB_BUS64BIT_MASK
)) {
910 trace_sdhci_error("64 bit ADMA not supported");
917 trace_sdhci_error("Unsupported DMA type");
921 if ((s
->trnmod
& SDHC_TRNS_READ
) && sdbus_data_ready(&s
->sdbus
)) {
922 s
->prnsts
|= SDHC_DOING_READ
| SDHC_DATA_INHIBIT
|
923 SDHC_DAT_LINE_ACTIVE
;
924 sdhci_read_block_from_card(s
);
926 s
->prnsts
|= SDHC_DOING_WRITE
| SDHC_DAT_LINE_ACTIVE
|
927 SDHC_SPACE_AVAILABLE
| SDHC_DATA_INHIBIT
;
928 sdhci_write_block_to_card(s
);
933 static bool sdhci_can_issue_command(SDHCIState
*s
)
935 if (!SDHC_CLOCK_IS_ON(s
->clkcon
) ||
936 (((s
->prnsts
& SDHC_DATA_INHIBIT
) || s
->stopped_state
) &&
937 ((s
->cmdreg
& SDHC_CMD_DATA_PRESENT
) ||
938 ((s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
&&
939 !(SDHC_COMMAND_TYPE(s
->cmdreg
) == SDHC_CMD_ABORT
))))) {
946 /* The Buffer Data Port register must be accessed in sequential and
947 * continuous manner */
949 sdhci_buff_access_is_sequential(SDHCIState
*s
, unsigned byte_num
)
951 if ((s
->data_count
& 0x3) != byte_num
) {
952 trace_sdhci_error("Non-sequential access to Buffer Data Port register"
959 static void sdhci_resume_pending_transfer(SDHCIState
*s
)
961 timer_del(s
->transfer_timer
);
962 sdhci_data_transfer(s
);
965 static uint64_t sdhci_read(void *opaque
, hwaddr offset
, unsigned size
)
967 SDHCIState
*s
= (SDHCIState
*)opaque
;
970 if (timer_pending(s
->transfer_timer
)) {
971 sdhci_resume_pending_transfer(s
);
974 switch (offset
& ~0x3) {
979 ret
= s
->blksize
| (s
->blkcnt
<< 16);
985 ret
= s
->trnmod
| (s
->cmdreg
<< 16);
987 case SDHC_RSPREG0
... SDHC_RSPREG3
:
988 ret
= s
->rspreg
[((offset
& ~0x3) - SDHC_RSPREG0
) >> 2];
991 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
992 ret
= sdhci_read_dataport(s
, size
);
993 trace_sdhci_access("rd", size
<< 3, offset
, "->", ret
, ret
);
999 ret
= FIELD_DP32(ret
, SDHC_PRNSTS
, DAT_LVL
,
1000 sdbus_get_dat_lines(&s
->sdbus
));
1001 ret
= FIELD_DP32(ret
, SDHC_PRNSTS
, CMD_LVL
,
1002 sdbus_get_cmd_line(&s
->sdbus
));
1005 ret
= s
->hostctl1
| (s
->pwrcon
<< 8) | (s
->blkgap
<< 16) |
1009 ret
= s
->clkcon
| (s
->timeoutcon
<< 16);
1011 case SDHC_NORINTSTS
:
1012 ret
= s
->norintsts
| (s
->errintsts
<< 16);
1014 case SDHC_NORINTSTSEN
:
1015 ret
= s
->norintstsen
| (s
->errintstsen
<< 16);
1017 case SDHC_NORINTSIGEN
:
1018 ret
= s
->norintsigen
| (s
->errintsigen
<< 16);
1020 case SDHC_ACMD12ERRSTS
:
1021 ret
= s
->acmd12errsts
| (s
->hostctl2
<< 16);
1024 ret
= (uint32_t)s
->capareg
;
1026 case SDHC_CAPAB
+ 4:
1027 ret
= (uint32_t)(s
->capareg
>> 32);
1030 ret
= (uint32_t)s
->maxcurr
;
1032 case SDHC_MAXCURR
+ 4:
1033 ret
= (uint32_t)(s
->maxcurr
>> 32);
1038 case SDHC_ADMASYSADDR
:
1039 ret
= (uint32_t)s
->admasysaddr
;
1041 case SDHC_ADMASYSADDR
+ 4:
1042 ret
= (uint32_t)(s
->admasysaddr
>> 32);
1044 case SDHC_SLOT_INT_STATUS
:
1045 ret
= (s
->version
<< 16) | sdhci_slotint(s
);
1048 qemu_log_mask(LOG_UNIMP
, "SDHC rd_%ub @0x%02" HWADDR_PRIx
" "
1049 "not implemented\n", size
, offset
);
1053 ret
>>= (offset
& 0x3) * 8;
1054 ret
&= (1ULL << (size
* 8)) - 1;
1055 trace_sdhci_access("rd", size
<< 3, offset
, "->", ret
, ret
);
1059 static inline void sdhci_blkgap_write(SDHCIState
*s
, uint8_t value
)
1061 if ((value
& SDHC_STOP_AT_GAP_REQ
) && (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
)) {
1064 s
->blkgap
= value
& SDHC_STOP_AT_GAP_REQ
;
1066 if ((value
& SDHC_CONTINUE_REQ
) && s
->stopped_state
&&
1067 (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
) == 0) {
1068 if (s
->stopped_state
== sdhc_gap_read
) {
1069 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_READ
;
1070 sdhci_read_block_from_card(s
);
1072 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_WRITE
;
1073 sdhci_write_block_to_card(s
);
1075 s
->stopped_state
= sdhc_not_stopped
;
1076 } else if (!s
->stopped_state
&& (value
& SDHC_STOP_AT_GAP_REQ
)) {
1077 if (s
->prnsts
& SDHC_DOING_READ
) {
1078 s
->stopped_state
= sdhc_gap_read
;
1079 } else if (s
->prnsts
& SDHC_DOING_WRITE
) {
1080 s
->stopped_state
= sdhc_gap_write
;
1085 static inline void sdhci_reset_write(SDHCIState
*s
, uint8_t value
)
1088 case SDHC_RESET_ALL
:
1091 case SDHC_RESET_CMD
:
1092 s
->prnsts
&= ~SDHC_CMD_INHIBIT
;
1093 s
->norintsts
&= ~SDHC_NIS_CMDCMP
;
1095 case SDHC_RESET_DATA
:
1097 s
->prnsts
&= ~(SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
|
1098 SDHC_DOING_READ
| SDHC_DOING_WRITE
|
1099 SDHC_DATA_INHIBIT
| SDHC_DAT_LINE_ACTIVE
);
1100 s
->blkgap
&= ~(SDHC_STOP_AT_GAP_REQ
| SDHC_CONTINUE_REQ
);
1101 s
->stopped_state
= sdhc_not_stopped
;
1102 s
->norintsts
&= ~(SDHC_NIS_WBUFRDY
| SDHC_NIS_RBUFRDY
|
1103 SDHC_NIS_DMA
| SDHC_NIS_TRSCMP
| SDHC_NIS_BLKGAP
);
1109 sdhci_write(void *opaque
, hwaddr offset
, uint64_t val
, unsigned size
)
1111 SDHCIState
*s
= (SDHCIState
*)opaque
;
1112 unsigned shift
= 8 * (offset
& 0x3);
1113 uint32_t mask
= ~(((1ULL << (size
* 8)) - 1) << shift
);
1114 uint32_t value
= val
;
1117 if (timer_pending(s
->transfer_timer
)) {
1118 sdhci_resume_pending_transfer(s
);
1121 switch (offset
& ~0x3) {
1123 s
->sdmasysad
= (s
->sdmasysad
& mask
) | value
;
1124 MASKED_WRITE(s
->sdmasysad
, mask
, value
);
1125 /* Writing to last byte of sdmasysad might trigger transfer */
1126 if (!(mask
& 0xFF000000) && TRANSFERRING_DATA(s
->prnsts
) && s
->blkcnt
&&
1127 s
->blksize
&& SDHC_DMA_TYPE(s
->hostctl1
) == SDHC_CTRL_SDMA
) {
1128 if (s
->trnmod
& SDHC_TRNS_MULTI
) {
1129 sdhci_sdma_transfer_multi_blocks(s
);
1131 sdhci_sdma_transfer_single_block(s
);
1136 if (!TRANSFERRING_DATA(s
->prnsts
)) {
1137 MASKED_WRITE(s
->blksize
, mask
, extract32(value
, 0, 12));
1138 MASKED_WRITE(s
->blkcnt
, mask
>> 16, value
>> 16);
1141 /* Limit block size to the maximum buffer size */
1142 if (extract32(s
->blksize
, 0, 12) > s
->buf_maxsz
) {
1143 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Size 0x%x is larger than "
1144 "the maximum buffer 0x%x\n", __func__
, s
->blksize
,
1147 s
->blksize
= deposit32(s
->blksize
, 0, 12, s
->buf_maxsz
);
1152 MASKED_WRITE(s
->argument
, mask
, value
);
1155 /* DMA can be enabled only if it is supported as indicated by
1156 * capabilities register */
1157 if (!(s
->capareg
& R_SDHC_CAPAB_SDMA_MASK
)) {
1158 value
&= ~SDHC_TRNS_DMA
;
1160 MASKED_WRITE(s
->trnmod
, mask
, value
& SDHC_TRNMOD_MASK
);
1161 MASKED_WRITE(s
->cmdreg
, mask
>> 16, value
>> 16);
1163 /* Writing to the upper byte of CMDREG triggers SD command generation */
1164 if ((mask
& 0xFF000000) || !sdhci_can_issue_command(s
)) {
1168 sdhci_send_command(s
);
1171 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
1172 sdhci_write_dataport(s
, value
>> shift
, size
);
1176 if (!(mask
& 0xFF0000)) {
1177 sdhci_blkgap_write(s
, value
>> 16);
1179 MASKED_WRITE(s
->hostctl1
, mask
, value
);
1180 MASKED_WRITE(s
->pwrcon
, mask
>> 8, value
>> 8);
1181 MASKED_WRITE(s
->wakcon
, mask
>> 24, value
>> 24);
1182 if (!(s
->prnsts
& SDHC_CARD_PRESENT
) || ((s
->pwrcon
>> 1) & 0x7) < 5 ||
1183 !(s
->capareg
& (1 << (31 - ((s
->pwrcon
>> 1) & 0x7))))) {
1184 s
->pwrcon
&= ~SDHC_POWER_ON
;
1188 if (!(mask
& 0xFF000000)) {
1189 sdhci_reset_write(s
, value
>> 24);
1191 MASKED_WRITE(s
->clkcon
, mask
, value
);
1192 MASKED_WRITE(s
->timeoutcon
, mask
>> 16, value
>> 16);
1193 if (s
->clkcon
& SDHC_CLOCK_INT_EN
) {
1194 s
->clkcon
|= SDHC_CLOCK_INT_STABLE
;
1196 s
->clkcon
&= ~SDHC_CLOCK_INT_STABLE
;
1199 case SDHC_NORINTSTS
:
1200 if (s
->norintstsen
& SDHC_NISEN_CARDINT
) {
1201 value
&= ~SDHC_NIS_CARDINT
;
1203 s
->norintsts
&= mask
| ~value
;
1204 s
->errintsts
&= (mask
>> 16) | ~(value
>> 16);
1206 s
->norintsts
|= SDHC_NIS_ERR
;
1208 s
->norintsts
&= ~SDHC_NIS_ERR
;
1210 sdhci_update_irq(s
);
1212 case SDHC_NORINTSTSEN
:
1213 MASKED_WRITE(s
->norintstsen
, mask
, value
);
1214 MASKED_WRITE(s
->errintstsen
, mask
>> 16, value
>> 16);
1215 s
->norintsts
&= s
->norintstsen
;
1216 s
->errintsts
&= s
->errintstsen
;
1218 s
->norintsts
|= SDHC_NIS_ERR
;
1220 s
->norintsts
&= ~SDHC_NIS_ERR
;
1222 /* Quirk for Raspberry Pi: pending card insert interrupt
1223 * appears when first enabled after power on */
1224 if ((s
->norintstsen
& SDHC_NISEN_INSERT
) && s
->pending_insert_state
) {
1225 assert(s
->pending_insert_quirk
);
1226 s
->norintsts
|= SDHC_NIS_INSERT
;
1227 s
->pending_insert_state
= false;
1229 sdhci_update_irq(s
);
1231 case SDHC_NORINTSIGEN
:
1232 MASKED_WRITE(s
->norintsigen
, mask
, value
);
1233 MASKED_WRITE(s
->errintsigen
, mask
>> 16, value
>> 16);
1234 sdhci_update_irq(s
);
1237 MASKED_WRITE(s
->admaerr
, mask
, value
);
1239 case SDHC_ADMASYSADDR
:
1240 s
->admasysaddr
= (s
->admasysaddr
& (0xFFFFFFFF00000000ULL
|
1241 (uint64_t)mask
)) | (uint64_t)value
;
1243 case SDHC_ADMASYSADDR
+ 4:
1244 s
->admasysaddr
= (s
->admasysaddr
& (0x00000000FFFFFFFFULL
|
1245 ((uint64_t)mask
<< 32))) | ((uint64_t)value
<< 32);
1248 s
->acmd12errsts
|= value
;
1249 s
->errintsts
|= (value
>> 16) & s
->errintstsen
;
1250 if (s
->acmd12errsts
) {
1251 s
->errintsts
|= SDHC_EIS_CMD12ERR
;
1254 s
->norintsts
|= SDHC_NIS_ERR
;
1256 sdhci_update_irq(s
);
1258 case SDHC_ACMD12ERRSTS
:
1259 MASKED_WRITE(s
->acmd12errsts
, mask
, value
& UINT16_MAX
);
1260 if (s
->uhs_mode
>= UHS_I
) {
1261 MASKED_WRITE(s
->hostctl2
, mask
>> 16, value
>> 16);
1263 if (FIELD_EX32(s
->hostctl2
, SDHC_HOSTCTL2
, V18_ENA
)) {
1264 sdbus_set_voltage(&s
->sdbus
, SD_VOLTAGE_1_8V
);
1266 sdbus_set_voltage(&s
->sdbus
, SD_VOLTAGE_3_3V
);
1272 case SDHC_CAPAB
+ 4:
1274 case SDHC_MAXCURR
+ 4:
1275 qemu_log_mask(LOG_GUEST_ERROR
, "SDHC wr_%ub @0x%02" HWADDR_PRIx
1276 " <- 0x%08x read-only\n", size
, offset
, value
>> shift
);
1280 qemu_log_mask(LOG_UNIMP
, "SDHC wr_%ub @0x%02" HWADDR_PRIx
" <- 0x%08x "
1281 "not implemented\n", size
, offset
, value
>> shift
);
1284 trace_sdhci_access("wr", size
<< 3, offset
, "<-",
1285 value
>> shift
, value
>> shift
);
1288 static const MemoryRegionOps sdhci_mmio_ops
= {
1290 .write
= sdhci_write
,
1292 .min_access_size
= 1,
1293 .max_access_size
= 4,
1296 .endianness
= DEVICE_LITTLE_ENDIAN
,
1299 static void sdhci_init_readonly_registers(SDHCIState
*s
, Error
**errp
)
1303 switch (s
->sd_spec_version
) {
1307 error_setg(errp
, "Only Spec v2/v3 are supported");
1310 s
->version
= (SDHC_HCVER_VENDOR
<< 8) | (s
->sd_spec_version
- 1);
1312 sdhci_check_capareg(s
, errp
);
1318 /* --- qdev common --- */
1320 void sdhci_initfn(SDHCIState
*s
)
1322 qbus_create_inplace(&s
->sdbus
, sizeof(s
->sdbus
),
1323 TYPE_SDHCI_BUS
, DEVICE(s
), "sd-bus");
1325 s
->insert_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sdhci_raise_insertion_irq
, s
);
1326 s
->transfer_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sdhci_data_transfer
, s
);
1328 s
->io_ops
= &sdhci_mmio_ops
;
1331 void sdhci_uninitfn(SDHCIState
*s
)
1333 timer_del(s
->insert_timer
);
1334 timer_free(s
->insert_timer
);
1335 timer_del(s
->transfer_timer
);
1336 timer_free(s
->transfer_timer
);
1338 g_free(s
->fifo_buffer
);
1339 s
->fifo_buffer
= NULL
;
1342 void sdhci_common_realize(SDHCIState
*s
, Error
**errp
)
1346 sdhci_init_readonly_registers(s
, errp
);
1350 s
->buf_maxsz
= sdhci_get_fifolen(s
);
1351 s
->fifo_buffer
= g_malloc0(s
->buf_maxsz
);
1353 memory_region_init_io(&s
->iomem
, OBJECT(s
), s
->io_ops
, s
, "sdhci",
1354 SDHC_REGISTERS_MAP_SIZE
);
1357 void sdhci_common_unrealize(SDHCIState
*s
)
1359 /* This function is expected to be called only once for each class:
1360 * - SysBus: via DeviceClass->unrealize(),
1361 * - PCI: via PCIDeviceClass->exit().
1362 * However to avoid double-free and/or use-after-free we still nullify
1363 * this variable (better safe than sorry!). */
1364 g_free(s
->fifo_buffer
);
1365 s
->fifo_buffer
= NULL
;
1368 static bool sdhci_pending_insert_vmstate_needed(void *opaque
)
1370 SDHCIState
*s
= opaque
;
1372 return s
->pending_insert_state
;
1375 static const VMStateDescription sdhci_pending_insert_vmstate
= {
1376 .name
= "sdhci/pending-insert",
1378 .minimum_version_id
= 1,
1379 .needed
= sdhci_pending_insert_vmstate_needed
,
1380 .fields
= (VMStateField
[]) {
1381 VMSTATE_BOOL(pending_insert_state
, SDHCIState
),
1382 VMSTATE_END_OF_LIST()
1386 const VMStateDescription sdhci_vmstate
= {
1389 .minimum_version_id
= 1,
1390 .fields
= (VMStateField
[]) {
1391 VMSTATE_UINT32(sdmasysad
, SDHCIState
),
1392 VMSTATE_UINT16(blksize
, SDHCIState
),
1393 VMSTATE_UINT16(blkcnt
, SDHCIState
),
1394 VMSTATE_UINT32(argument
, SDHCIState
),
1395 VMSTATE_UINT16(trnmod
, SDHCIState
),
1396 VMSTATE_UINT16(cmdreg
, SDHCIState
),
1397 VMSTATE_UINT32_ARRAY(rspreg
, SDHCIState
, 4),
1398 VMSTATE_UINT32(prnsts
, SDHCIState
),
1399 VMSTATE_UINT8(hostctl1
, SDHCIState
),
1400 VMSTATE_UINT8(pwrcon
, SDHCIState
),
1401 VMSTATE_UINT8(blkgap
, SDHCIState
),
1402 VMSTATE_UINT8(wakcon
, SDHCIState
),
1403 VMSTATE_UINT16(clkcon
, SDHCIState
),
1404 VMSTATE_UINT8(timeoutcon
, SDHCIState
),
1405 VMSTATE_UINT8(admaerr
, SDHCIState
),
1406 VMSTATE_UINT16(norintsts
, SDHCIState
),
1407 VMSTATE_UINT16(errintsts
, SDHCIState
),
1408 VMSTATE_UINT16(norintstsen
, SDHCIState
),
1409 VMSTATE_UINT16(errintstsen
, SDHCIState
),
1410 VMSTATE_UINT16(norintsigen
, SDHCIState
),
1411 VMSTATE_UINT16(errintsigen
, SDHCIState
),
1412 VMSTATE_UINT16(acmd12errsts
, SDHCIState
),
1413 VMSTATE_UINT16(data_count
, SDHCIState
),
1414 VMSTATE_UINT64(admasysaddr
, SDHCIState
),
1415 VMSTATE_UINT8(stopped_state
, SDHCIState
),
1416 VMSTATE_VBUFFER_UINT32(fifo_buffer
, SDHCIState
, 1, NULL
, buf_maxsz
),
1417 VMSTATE_TIMER_PTR(insert_timer
, SDHCIState
),
1418 VMSTATE_TIMER_PTR(transfer_timer
, SDHCIState
),
1419 VMSTATE_END_OF_LIST()
1421 .subsections
= (const VMStateDescription
*[]) {
1422 &sdhci_pending_insert_vmstate
,
1427 void sdhci_common_class_init(ObjectClass
*klass
, void *data
)
1429 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1431 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1432 dc
->vmsd
= &sdhci_vmstate
;
1433 dc
->reset
= sdhci_poweron_reset
;
1436 /* --- qdev SysBus --- */
1438 static Property sdhci_sysbus_properties
[] = {
1439 DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState
),
1440 DEFINE_PROP_BOOL("pending-insert-quirk", SDHCIState
, pending_insert_quirk
,
1442 DEFINE_PROP_LINK("dma", SDHCIState
,
1443 dma_mr
, TYPE_MEMORY_REGION
, MemoryRegion
*),
1444 DEFINE_PROP_END_OF_LIST(),
1447 static void sdhci_sysbus_init(Object
*obj
)
1449 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1454 static void sdhci_sysbus_finalize(Object
*obj
)
1456 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1459 object_unparent(OBJECT(s
->dma_mr
));
1465 static void sdhci_sysbus_realize(DeviceState
*dev
, Error
**errp
)
1468 SDHCIState
*s
= SYSBUS_SDHCI(dev
);
1469 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1471 sdhci_common_realize(s
, errp
);
1477 s
->dma_as
= &s
->sysbus_dma_as
;
1478 address_space_init(s
->dma_as
, s
->dma_mr
, "sdhci-dma");
1480 /* use system_memory() if property "dma" not set */
1481 s
->dma_as
= &address_space_memory
;
1484 sysbus_init_irq(sbd
, &s
->irq
);
1486 sysbus_init_mmio(sbd
, &s
->iomem
);
1489 static void sdhci_sysbus_unrealize(DeviceState
*dev
)
1491 SDHCIState
*s
= SYSBUS_SDHCI(dev
);
1493 sdhci_common_unrealize(s
);
1496 address_space_destroy(s
->dma_as
);
1500 static void sdhci_sysbus_class_init(ObjectClass
*klass
, void *data
)
1502 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1504 device_class_set_props(dc
, sdhci_sysbus_properties
);
1505 dc
->realize
= sdhci_sysbus_realize
;
1506 dc
->unrealize
= sdhci_sysbus_unrealize
;
1508 sdhci_common_class_init(klass
, data
);
1511 static const TypeInfo sdhci_sysbus_info
= {
1512 .name
= TYPE_SYSBUS_SDHCI
,
1513 .parent
= TYPE_SYS_BUS_DEVICE
,
1514 .instance_size
= sizeof(SDHCIState
),
1515 .instance_init
= sdhci_sysbus_init
,
1516 .instance_finalize
= sdhci_sysbus_finalize
,
1517 .class_init
= sdhci_sysbus_class_init
,
1520 /* --- qdev bus master --- */
1522 static void sdhci_bus_class_init(ObjectClass
*klass
, void *data
)
1524 SDBusClass
*sbc
= SD_BUS_CLASS(klass
);
1526 sbc
->set_inserted
= sdhci_set_inserted
;
1527 sbc
->set_readonly
= sdhci_set_readonly
;
1530 static const TypeInfo sdhci_bus_info
= {
1531 .name
= TYPE_SDHCI_BUS
,
1532 .parent
= TYPE_SD_BUS
,
1533 .instance_size
= sizeof(SDBus
),
1534 .class_init
= sdhci_bus_class_init
,
1537 /* --- qdev i.MX eSDHC --- */
1539 static uint64_t usdhc_read(void *opaque
, hwaddr offset
, unsigned size
)
1541 SDHCIState
*s
= SYSBUS_SDHCI(opaque
);
1547 return sdhci_read(opaque
, offset
, size
);
1551 * For a detailed explanation on the following bit
1552 * manipulation code see comments in a similar part of
1555 hostctl1
= SDHC_DMA_TYPE(s
->hostctl1
) << (8 - 3);
1557 if (s
->hostctl1
& SDHC_CTRL_8BITBUS
) {
1558 hostctl1
|= ESDHC_CTRL_8BITBUS
;
1561 if (s
->hostctl1
& SDHC_CTRL_4BITBUS
) {
1562 hostctl1
|= ESDHC_CTRL_4BITBUS
;
1566 ret
|= (uint32_t)s
->blkgap
<< 16;
1567 ret
|= (uint32_t)s
->wakcon
<< 24;
1572 /* Add SDSTB (SD Clock Stable) bit to PRNSTS */
1573 ret
= sdhci_read(opaque
, offset
, size
) & ~ESDHC_PRNSTS_SDSTB
;
1574 if (s
->clkcon
& SDHC_CLOCK_INT_STABLE
) {
1575 ret
|= ESDHC_PRNSTS_SDSTB
;
1579 case ESDHC_VENDOR_SPEC
:
1580 ret
= s
->vendor_spec
;
1582 case ESDHC_DLL_CTRL
:
1583 case ESDHC_TUNE_CTRL_STATUS
:
1584 case ESDHC_UNDOCUMENTED_REG27
:
1585 case ESDHC_TUNING_CTRL
:
1586 case ESDHC_MIX_CTRL
:
1587 case ESDHC_WTMK_LVL
:
1596 usdhc_write(void *opaque
, hwaddr offset
, uint64_t val
, unsigned size
)
1598 SDHCIState
*s
= SYSBUS_SDHCI(opaque
);
1600 uint32_t value
= (uint32_t)val
;
1603 case ESDHC_DLL_CTRL
:
1604 case ESDHC_TUNE_CTRL_STATUS
:
1605 case ESDHC_UNDOCUMENTED_REG27
:
1606 case ESDHC_TUNING_CTRL
:
1607 case ESDHC_WTMK_LVL
:
1610 case ESDHC_VENDOR_SPEC
:
1611 s
->vendor_spec
= value
;
1612 switch (s
->vendor
) {
1613 case SDHCI_VENDOR_IMX
:
1614 if (value
& ESDHC_IMX_FRC_SDCLK_ON
) {
1615 s
->prnsts
&= ~SDHC_IMX_CLOCK_GATE_OFF
;
1617 s
->prnsts
|= SDHC_IMX_CLOCK_GATE_OFF
;
1627 * Here's What ESDHCI has at offset 0x28 (SDHC_HOSTCTL)
1630 * |-----------+--------+--------+-----------+----------+---------|
1631 * | Card | Card | Endian | DATA3 | Data | Led |
1632 * | Detect | Detect | Mode | as Card | Transfer | Control |
1633 * | Signal | Test | | Detection | Width | |
1634 * | Selection | Level | | Pin | | |
1635 * |-----------+--------+--------+-----------+----------+---------|
1640 * |----------+------|
1641 * | Reserved | DMA |
1644 * |----------+------|
1646 * and here's what SDCHI spec expects those offsets to be:
1648 * 0x28 (Host Control Register)
1651 * |--------+--------+----------+------+--------+----------+---------|
1652 * | Card | Card | Extended | DMA | High | Data | LED |
1653 * | Detect | Detect | Data | Sel. | Speed | Transfer | Control |
1654 * | Signal | Test | Transfer | | Enable | Width | |
1655 * | Sel. | Level | Width | | | | |
1656 * |--------+--------+----------+------+--------+----------+---------|
1658 * and 0x29 (Power Control Register)
1660 * |----------------------------------|
1661 * | Power Control Register |
1663 * | Description omitted, |
1664 * | since it has no analog in ESDHCI |
1666 * |----------------------------------|
1668 * Since offsets 0x2A and 0x2B should be compatible between
1669 * both IP specs we only need to reconcile least 16-bit of the
1670 * word we've been given.
1674 * First, save bits 7 6 and 0 since they are identical
1676 hostctl1
= value
& (SDHC_CTRL_LED
|
1677 SDHC_CTRL_CDTEST_INS
|
1678 SDHC_CTRL_CDTEST_EN
);
1680 * Second, split "Data Transfer Width" from bits 2 and 1 in to
1683 if (value
& ESDHC_CTRL_8BITBUS
) {
1684 hostctl1
|= SDHC_CTRL_8BITBUS
;
1687 if (value
& ESDHC_CTRL_4BITBUS
) {
1688 hostctl1
|= ESDHC_CTRL_4BITBUS
;
1692 * Third, move DMA select from bits 9 and 8 to bits 4 and 3
1694 hostctl1
|= SDHC_DMA_TYPE(value
>> (8 - 3));
1697 * Now place the corrected value into low 16-bit of the value
1698 * we are going to give standard SDHCI write function
1700 * NOTE: This transformation should be the inverse of what can
1701 * be found in drivers/mmc/host/sdhci-esdhc-imx.c in Linux
1704 value
&= ~UINT16_MAX
;
1706 value
|= (uint16_t)s
->pwrcon
<< 8;
1708 sdhci_write(opaque
, offset
, value
, size
);
1711 case ESDHC_MIX_CTRL
:
1713 * So, when SD/MMC stack in Linux tries to write to "Transfer
1714 * Mode Register", ESDHC i.MX quirk code will translate it
1715 * into a write to ESDHC_MIX_CTRL, so we do the opposite in
1716 * order to get where we started
1718 * Note that Auto CMD23 Enable bit is located in a wrong place
1719 * on i.MX, but since it is not used by QEMU we do not care.
1721 * We don't want to call sdhci_write(.., SDHC_TRNMOD, ...)
1722 * here becuase it will result in a call to
1723 * sdhci_send_command(s) which we don't want.
1726 s
->trnmod
= value
& UINT16_MAX
;
1730 * Similar to above, but this time a write to "Command
1731 * Register" will be translated into a 4-byte write to
1732 * "Transfer Mode register" where lower 16-bit of value would
1733 * be set to zero. So what we do is fill those bits with
1734 * cached value from s->trnmod and let the SDHCI
1735 * infrastructure handle the rest
1737 sdhci_write(opaque
, offset
, val
| s
->trnmod
, size
);
1741 * ESDHCI does not implement "Host SDMA Buffer Boundary", and
1742 * Linux driver will try to zero this field out which will
1743 * break the rest of SDHCI emulation.
1745 * Linux defaults to maximum possible setting (512K boundary)
1746 * and it seems to be the only option that i.MX IP implements,
1747 * so we artificially set it to that value.
1752 sdhci_write(opaque
, offset
, val
, size
);
1757 static const MemoryRegionOps usdhc_mmio_ops
= {
1759 .write
= usdhc_write
,
1761 .min_access_size
= 1,
1762 .max_access_size
= 4,
1765 .endianness
= DEVICE_LITTLE_ENDIAN
,
1768 static void imx_usdhc_init(Object
*obj
)
1770 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1772 s
->io_ops
= &usdhc_mmio_ops
;
1773 s
->quirks
= SDHCI_QUIRK_NO_BUSY_IRQ
;
1776 static const TypeInfo imx_usdhc_info
= {
1777 .name
= TYPE_IMX_USDHC
,
1778 .parent
= TYPE_SYSBUS_SDHCI
,
1779 .instance_init
= imx_usdhc_init
,
1782 /* --- qdev Samsung s3c --- */
1784 #define S3C_SDHCI_CONTROL2 0x80
1785 #define S3C_SDHCI_CONTROL3 0x84
1786 #define S3C_SDHCI_CONTROL4 0x8c
1788 static uint64_t sdhci_s3c_read(void *opaque
, hwaddr offset
, unsigned size
)
1793 case S3C_SDHCI_CONTROL2
:
1794 case S3C_SDHCI_CONTROL3
:
1795 case S3C_SDHCI_CONTROL4
:
1800 ret
= sdhci_read(opaque
, offset
, size
);
1807 static void sdhci_s3c_write(void *opaque
, hwaddr offset
, uint64_t val
,
1811 case S3C_SDHCI_CONTROL2
:
1812 case S3C_SDHCI_CONTROL3
:
1813 case S3C_SDHCI_CONTROL4
:
1817 sdhci_write(opaque
, offset
, val
, size
);
1822 static const MemoryRegionOps sdhci_s3c_mmio_ops
= {
1823 .read
= sdhci_s3c_read
,
1824 .write
= sdhci_s3c_write
,
1826 .min_access_size
= 1,
1827 .max_access_size
= 4,
1830 .endianness
= DEVICE_LITTLE_ENDIAN
,
1833 static void sdhci_s3c_init(Object
*obj
)
1835 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1837 s
->io_ops
= &sdhci_s3c_mmio_ops
;
1840 static const TypeInfo sdhci_s3c_info
= {
1841 .name
= TYPE_S3C_SDHCI
,
1842 .parent
= TYPE_SYSBUS_SDHCI
,
1843 .instance_init
= sdhci_s3c_init
,
1846 static void sdhci_register_types(void)
1848 type_register_static(&sdhci_sysbus_info
);
1849 type_register_static(&sdhci_bus_info
);
1850 type_register_static(&imx_usdhc_info
);
1851 type_register_static(&sdhci_s3c_info
);
1854 type_init(sdhci_register_types
)