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 #define BLOCK_SIZE_MASK (4 * KiB - 1)
326 static void sdhci_send_command(SDHCIState
*s
)
329 uint8_t response
[16];
331 bool timeout
= false;
335 request
.cmd
= s
->cmdreg
>> 8;
336 request
.arg
= s
->argument
;
338 trace_sdhci_send_command(request
.cmd
, request
.arg
);
339 rlen
= sdbus_do_command(&s
->sdbus
, &request
, response
);
341 if (s
->cmdreg
& SDHC_CMD_RESPONSE
) {
343 s
->rspreg
[0] = ldl_be_p(response
);
344 s
->rspreg
[1] = s
->rspreg
[2] = s
->rspreg
[3] = 0;
345 trace_sdhci_response4(s
->rspreg
[0]);
346 } else if (rlen
== 16) {
347 s
->rspreg
[0] = ldl_be_p(&response
[11]);
348 s
->rspreg
[1] = ldl_be_p(&response
[7]);
349 s
->rspreg
[2] = ldl_be_p(&response
[3]);
350 s
->rspreg
[3] = (response
[0] << 16) | (response
[1] << 8) |
352 trace_sdhci_response16(s
->rspreg
[3], s
->rspreg
[2],
353 s
->rspreg
[1], s
->rspreg
[0]);
356 trace_sdhci_error("timeout waiting for command response");
357 if (s
->errintstsen
& SDHC_EISEN_CMDTIMEOUT
) {
358 s
->errintsts
|= SDHC_EIS_CMDTIMEOUT
;
359 s
->norintsts
|= SDHC_NIS_ERR
;
363 if (!(s
->quirks
& SDHCI_QUIRK_NO_BUSY_IRQ
) &&
364 (s
->norintstsen
& SDHC_NISEN_TRSCMP
) &&
365 (s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
) {
366 s
->norintsts
|= SDHC_NIS_TRSCMP
;
370 if (s
->norintstsen
& SDHC_NISEN_CMDCMP
) {
371 s
->norintsts
|= SDHC_NIS_CMDCMP
;
376 if (!timeout
&& (s
->blksize
& BLOCK_SIZE_MASK
) &&
377 (s
->cmdreg
& SDHC_CMD_DATA_PRESENT
)) {
379 sdhci_data_transfer(s
);
383 static void sdhci_end_transfer(SDHCIState
*s
)
385 /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */
386 if ((s
->trnmod
& SDHC_TRNS_ACMD12
) != 0) {
388 uint8_t response
[16];
392 trace_sdhci_end_transfer(request
.cmd
, request
.arg
);
393 sdbus_do_command(&s
->sdbus
, &request
, response
);
394 /* Auto CMD12 response goes to the upper Response register */
395 s
->rspreg
[3] = ldl_be_p(response
);
398 s
->prnsts
&= ~(SDHC_DOING_READ
| SDHC_DOING_WRITE
|
399 SDHC_DAT_LINE_ACTIVE
| SDHC_DATA_INHIBIT
|
400 SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
);
402 if (s
->norintstsen
& SDHC_NISEN_TRSCMP
) {
403 s
->norintsts
|= SDHC_NIS_TRSCMP
;
410 * Programmed i/o data transfer
413 /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
414 static void sdhci_read_block_from_card(SDHCIState
*s
)
416 const uint16_t blk_size
= s
->blksize
& BLOCK_SIZE_MASK
;
418 if ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
419 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) {
423 if (!FIELD_EX32(s
->hostctl2
, SDHC_HOSTCTL2
, EXECUTE_TUNING
)) {
424 /* Device is not in tuning */
425 sdbus_read_data(&s
->sdbus
, s
->fifo_buffer
, blk_size
);
428 if (FIELD_EX32(s
->hostctl2
, SDHC_HOSTCTL2
, EXECUTE_TUNING
)) {
429 /* Device is in tuning */
430 s
->hostctl2
&= ~R_SDHC_HOSTCTL2_EXECUTE_TUNING_MASK
;
431 s
->hostctl2
|= R_SDHC_HOSTCTL2_SAMPLING_CLKSEL_MASK
;
432 s
->prnsts
&= ~(SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_READ
|
437 /* New data now available for READ through Buffer Port Register */
438 s
->prnsts
|= SDHC_DATA_AVAILABLE
;
439 if (s
->norintstsen
& SDHC_NISEN_RBUFRDY
) {
440 s
->norintsts
|= SDHC_NIS_RBUFRDY
;
443 /* Clear DAT line active status if that was the last block */
444 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
445 ((s
->trnmod
& SDHC_TRNS_MULTI
) && s
->blkcnt
== 1)) {
446 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
449 /* If stop at block gap request was set and it's not the last block of
450 * data - generate Block Event interrupt */
451 if (s
->stopped_state
== sdhc_gap_read
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
453 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
454 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
455 s
->norintsts
|= SDHC_EIS_BLKGAP
;
463 /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
464 static uint32_t sdhci_read_dataport(SDHCIState
*s
, unsigned size
)
469 /* first check that a valid data exists in host controller input buffer */
470 if ((s
->prnsts
& SDHC_DATA_AVAILABLE
) == 0) {
471 trace_sdhci_error("read from empty buffer");
475 for (i
= 0; i
< size
; i
++) {
476 assert(s
->data_count
< s
->buf_maxsz
);
477 value
|= s
->fifo_buffer
[s
->data_count
] << i
* 8;
479 /* check if we've read all valid data (blksize bytes) from buffer */
480 if ((s
->data_count
) >= (s
->blksize
& BLOCK_SIZE_MASK
)) {
481 trace_sdhci_read_dataport(s
->data_count
);
482 s
->prnsts
&= ~SDHC_DATA_AVAILABLE
; /* no more data in a buffer */
483 s
->data_count
= 0; /* next buff read must start at position [0] */
485 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
489 /* if that was the last block of data */
490 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
491 ((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) ||
492 /* stop at gap request */
493 (s
->stopped_state
== sdhc_gap_read
&&
494 !(s
->prnsts
& SDHC_DAT_LINE_ACTIVE
))) {
495 sdhci_end_transfer(s
);
496 } else { /* if there are more data, read next block from card */
497 sdhci_read_block_from_card(s
);
506 /* Write data from host controller FIFO to card */
507 static void sdhci_write_block_to_card(SDHCIState
*s
)
509 if (s
->prnsts
& SDHC_SPACE_AVAILABLE
) {
510 if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
511 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
517 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
518 if (s
->blkcnt
== 0) {
525 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
, s
->blksize
& BLOCK_SIZE_MASK
);
527 /* Next data can be written through BUFFER DATORT register */
528 s
->prnsts
|= SDHC_SPACE_AVAILABLE
;
530 /* Finish transfer if that was the last block of data */
531 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
532 ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
533 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0))) {
534 sdhci_end_transfer(s
);
535 } else if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
536 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
539 /* Generate Block Gap Event if requested and if not the last block */
540 if (s
->stopped_state
== sdhc_gap_write
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
542 s
->prnsts
&= ~SDHC_DOING_WRITE
;
543 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
544 s
->norintsts
|= SDHC_EIS_BLKGAP
;
546 sdhci_end_transfer(s
);
552 /* Write @size bytes of @value data to host controller @s Buffer Data Port
554 static void sdhci_write_dataport(SDHCIState
*s
, uint32_t value
, unsigned size
)
558 /* Check that there is free space left in a buffer */
559 if (!(s
->prnsts
& SDHC_SPACE_AVAILABLE
)) {
560 trace_sdhci_error("Can't write to data buffer: buffer full");
564 for (i
= 0; i
< size
; i
++) {
565 assert(s
->data_count
< s
->buf_maxsz
);
566 s
->fifo_buffer
[s
->data_count
] = value
& 0xFF;
569 if (s
->data_count
>= (s
->blksize
& BLOCK_SIZE_MASK
)) {
570 trace_sdhci_write_dataport(s
->data_count
);
572 s
->prnsts
&= ~SDHC_SPACE_AVAILABLE
;
573 if (s
->prnsts
& SDHC_DOING_WRITE
) {
574 sdhci_write_block_to_card(s
);
581 * Single DMA data transfer
584 /* Multi block SDMA transfer */
585 static void sdhci_sdma_transfer_multi_blocks(SDHCIState
*s
)
587 bool page_aligned
= false;
589 const uint16_t block_size
= s
->blksize
& BLOCK_SIZE_MASK
;
590 uint32_t boundary_chk
= 1 << (((s
->blksize
& ~BLOCK_SIZE_MASK
) >> 12) + 12);
591 uint32_t boundary_count
= boundary_chk
- (s
->sdmasysad
% boundary_chk
);
593 if (!(s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) || !s
->blkcnt
) {
594 qemu_log_mask(LOG_UNIMP
, "infinite transfer is not supported\n");
598 /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
599 * possible stop at page boundary if initial address is not page aligned,
600 * allow them to work properly */
601 if ((s
->sdmasysad
% boundary_chk
) == 0) {
605 s
->prnsts
|= SDHC_DATA_INHIBIT
| SDHC_DAT_LINE_ACTIVE
;
606 if (s
->trnmod
& SDHC_TRNS_READ
) {
607 s
->prnsts
|= SDHC_DOING_READ
;
609 if (s
->data_count
== 0) {
610 sdbus_read_data(&s
->sdbus
, s
->fifo_buffer
, block_size
);
612 begin
= s
->data_count
;
613 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
614 s
->data_count
= boundary_count
+ begin
;
617 s
->data_count
= block_size
;
618 boundary_count
-= block_size
- begin
;
619 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
623 dma_memory_write(s
->dma_as
, s
->sdmasysad
, &s
->fifo_buffer
[begin
],
624 s
->data_count
- begin
, MEMTXATTRS_UNSPECIFIED
);
625 s
->sdmasysad
+= s
->data_count
- begin
;
626 if (s
->data_count
== block_size
) {
629 if (page_aligned
&& boundary_count
== 0) {
634 s
->prnsts
|= SDHC_DOING_WRITE
;
636 begin
= s
->data_count
;
637 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
638 s
->data_count
= boundary_count
+ begin
;
641 s
->data_count
= block_size
;
642 boundary_count
-= block_size
- begin
;
644 dma_memory_read(s
->dma_as
, s
->sdmasysad
, &s
->fifo_buffer
[begin
],
645 s
->data_count
- begin
, MEMTXATTRS_UNSPECIFIED
);
646 s
->sdmasysad
+= s
->data_count
- begin
;
647 if (s
->data_count
== block_size
) {
648 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
, block_size
);
650 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
654 if (page_aligned
&& boundary_count
== 0) {
660 if (s
->blkcnt
== 0) {
661 sdhci_end_transfer(s
);
663 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
664 s
->norintsts
|= SDHC_NIS_DMA
;
670 /* single block SDMA transfer */
671 static void sdhci_sdma_transfer_single_block(SDHCIState
*s
)
673 uint32_t datacnt
= s
->blksize
& BLOCK_SIZE_MASK
;
675 if (s
->trnmod
& SDHC_TRNS_READ
) {
676 sdbus_read_data(&s
->sdbus
, s
->fifo_buffer
, datacnt
);
677 dma_memory_write(s
->dma_as
, s
->sdmasysad
, s
->fifo_buffer
, datacnt
,
678 MEMTXATTRS_UNSPECIFIED
);
680 dma_memory_read(s
->dma_as
, s
->sdmasysad
, s
->fifo_buffer
, datacnt
,
681 MEMTXATTRS_UNSPECIFIED
);
682 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
, datacnt
);
686 sdhci_end_transfer(s
);
689 typedef struct ADMADescr
{
696 static void get_adma_description(SDHCIState
*s
, ADMADescr
*dscr
)
700 hwaddr entry_addr
= (hwaddr
)s
->admasysaddr
;
701 switch (SDHC_DMA_TYPE(s
->hostctl1
)) {
702 case SDHC_CTRL_ADMA2_32
:
703 dma_memory_read(s
->dma_as
, entry_addr
, &adma2
, sizeof(adma2
),
704 MEMTXATTRS_UNSPECIFIED
);
705 adma2
= le64_to_cpu(adma2
);
706 /* The spec does not specify endianness of descriptor table.
707 * We currently assume that it is LE.
709 dscr
->addr
= (hwaddr
)extract64(adma2
, 32, 32) & ~0x3ull
;
710 dscr
->length
= (uint16_t)extract64(adma2
, 16, 16);
711 dscr
->attr
= (uint8_t)extract64(adma2
, 0, 7);
714 case SDHC_CTRL_ADMA1_32
:
715 dma_memory_read(s
->dma_as
, entry_addr
, &adma1
, sizeof(adma1
),
716 MEMTXATTRS_UNSPECIFIED
);
717 adma1
= le32_to_cpu(adma1
);
718 dscr
->addr
= (hwaddr
)(adma1
& 0xFFFFF000);
719 dscr
->attr
= (uint8_t)extract32(adma1
, 0, 7);
721 if ((dscr
->attr
& SDHC_ADMA_ATTR_ACT_MASK
) == SDHC_ADMA_ATTR_SET_LEN
) {
722 dscr
->length
= (uint16_t)extract32(adma1
, 12, 16);
724 dscr
->length
= 4 * KiB
;
727 case SDHC_CTRL_ADMA2_64
:
728 dma_memory_read(s
->dma_as
, entry_addr
, &dscr
->attr
, 1,
729 MEMTXATTRS_UNSPECIFIED
);
730 dma_memory_read(s
->dma_as
, entry_addr
+ 2, &dscr
->length
, 2,
731 MEMTXATTRS_UNSPECIFIED
);
732 dscr
->length
= le16_to_cpu(dscr
->length
);
733 dma_memory_read(s
->dma_as
, entry_addr
+ 4, &dscr
->addr
, 8,
734 MEMTXATTRS_UNSPECIFIED
);
735 dscr
->addr
= le64_to_cpu(dscr
->addr
);
736 dscr
->attr
&= (uint8_t) ~0xC0;
742 /* Advanced DMA data transfer */
744 static void sdhci_do_adma(SDHCIState
*s
)
746 unsigned int begin
, length
;
747 const uint16_t block_size
= s
->blksize
& BLOCK_SIZE_MASK
;
748 const MemTxAttrs attrs
= { .memory
= true };
750 MemTxResult res
= MEMTX_ERROR
;
753 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
&& !s
->blkcnt
) {
754 /* Stop Multiple Transfer */
755 sdhci_end_transfer(s
);
759 for (i
= 0; i
< SDHC_ADMA_DESCS_PER_DELAY
; ++i
) {
760 s
->admaerr
&= ~SDHC_ADMAERR_LENGTH_MISMATCH
;
762 get_adma_description(s
, &dscr
);
763 trace_sdhci_adma_loop(dscr
.addr
, dscr
.length
, dscr
.attr
);
765 if ((dscr
.attr
& SDHC_ADMA_ATTR_VALID
) == 0) {
766 /* Indicate that error occurred in ST_FDS state */
767 s
->admaerr
&= ~SDHC_ADMAERR_STATE_MASK
;
768 s
->admaerr
|= SDHC_ADMAERR_STATE_ST_FDS
;
770 /* Generate ADMA error interrupt */
771 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
772 s
->errintsts
|= SDHC_EIS_ADMAERR
;
773 s
->norintsts
|= SDHC_NIS_ERR
;
780 length
= dscr
.length
? dscr
.length
: 64 * KiB
;
782 switch (dscr
.attr
& SDHC_ADMA_ATTR_ACT_MASK
) {
783 case SDHC_ADMA_ATTR_ACT_TRAN
: /* data transfer */
784 s
->prnsts
|= SDHC_DATA_INHIBIT
| SDHC_DAT_LINE_ACTIVE
;
785 if (s
->trnmod
& SDHC_TRNS_READ
) {
786 s
->prnsts
|= SDHC_DOING_READ
;
788 if (s
->data_count
== 0) {
789 sdbus_read_data(&s
->sdbus
, s
->fifo_buffer
, block_size
);
791 begin
= s
->data_count
;
792 if ((length
+ begin
) < block_size
) {
793 s
->data_count
= length
+ begin
;
796 s
->data_count
= block_size
;
797 length
-= block_size
- begin
;
799 res
= dma_memory_write(s
->dma_as
, dscr
.addr
,
800 &s
->fifo_buffer
[begin
],
801 s
->data_count
- begin
,
803 if (res
!= MEMTX_OK
) {
806 dscr
.addr
+= s
->data_count
- begin
;
807 if (s
->data_count
== block_size
) {
809 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
811 if (s
->blkcnt
== 0) {
818 s
->prnsts
|= SDHC_DOING_WRITE
;
820 begin
= s
->data_count
;
821 if ((length
+ begin
) < block_size
) {
822 s
->data_count
= length
+ begin
;
825 s
->data_count
= block_size
;
826 length
-= block_size
- begin
;
828 res
= dma_memory_read(s
->dma_as
, dscr
.addr
,
829 &s
->fifo_buffer
[begin
],
830 s
->data_count
- begin
,
832 if (res
!= MEMTX_OK
) {
835 dscr
.addr
+= s
->data_count
- begin
;
836 if (s
->data_count
== block_size
) {
837 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
, block_size
);
839 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
841 if (s
->blkcnt
== 0) {
848 if (res
!= MEMTX_OK
) {
850 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
851 trace_sdhci_error("Set ADMA error flag");
852 s
->errintsts
|= SDHC_EIS_ADMAERR
;
853 s
->norintsts
|= SDHC_NIS_ERR
;
857 s
->admasysaddr
+= dscr
.incr
;
860 case SDHC_ADMA_ATTR_ACT_LINK
: /* link to next descriptor table */
861 s
->admasysaddr
= dscr
.addr
;
862 trace_sdhci_adma("link", s
->admasysaddr
);
865 s
->admasysaddr
+= dscr
.incr
;
869 if (dscr
.attr
& SDHC_ADMA_ATTR_INT
) {
870 trace_sdhci_adma("interrupt", s
->admasysaddr
);
871 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
872 s
->norintsts
|= SDHC_NIS_DMA
;
875 if (sdhci_update_irq(s
) && !(dscr
.attr
& SDHC_ADMA_ATTR_END
)) {
876 /* IRQ delivered, reschedule current transfer */
881 /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
882 if (((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
883 (s
->blkcnt
== 0)) || (dscr
.attr
& SDHC_ADMA_ATTR_END
)) {
884 trace_sdhci_adma_transfer_completed();
885 if (length
|| ((dscr
.attr
& SDHC_ADMA_ATTR_END
) &&
886 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
888 trace_sdhci_error("SD/MMC host ADMA length mismatch");
889 s
->admaerr
|= SDHC_ADMAERR_LENGTH_MISMATCH
|
890 SDHC_ADMAERR_STATE_ST_TFR
;
891 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
892 trace_sdhci_error("Set ADMA error flag");
893 s
->errintsts
|= SDHC_EIS_ADMAERR
;
894 s
->norintsts
|= SDHC_NIS_ERR
;
899 sdhci_end_transfer(s
);
905 /* we have unfinished business - reschedule to continue ADMA */
906 timer_mod(s
->transfer_timer
,
907 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + SDHC_TRANSFER_DELAY
);
910 /* Perform data transfer according to controller configuration */
912 static void sdhci_data_transfer(void *opaque
)
914 SDHCIState
*s
= (SDHCIState
*)opaque
;
916 if (s
->trnmod
& SDHC_TRNS_DMA
) {
917 switch (SDHC_DMA_TYPE(s
->hostctl1
)) {
919 if ((s
->blkcnt
== 1) || !(s
->trnmod
& SDHC_TRNS_MULTI
)) {
920 sdhci_sdma_transfer_single_block(s
);
922 sdhci_sdma_transfer_multi_blocks(s
);
926 case SDHC_CTRL_ADMA1_32
:
927 if (!(s
->capareg
& R_SDHC_CAPAB_ADMA1_MASK
)) {
928 trace_sdhci_error("ADMA1 not supported");
934 case SDHC_CTRL_ADMA2_32
:
935 if (!(s
->capareg
& R_SDHC_CAPAB_ADMA2_MASK
)) {
936 trace_sdhci_error("ADMA2 not supported");
942 case SDHC_CTRL_ADMA2_64
:
943 if (!(s
->capareg
& R_SDHC_CAPAB_ADMA2_MASK
) ||
944 !(s
->capareg
& R_SDHC_CAPAB_BUS64BIT_MASK
)) {
945 trace_sdhci_error("64 bit ADMA not supported");
952 trace_sdhci_error("Unsupported DMA type");
956 if ((s
->trnmod
& SDHC_TRNS_READ
) && sdbus_data_ready(&s
->sdbus
)) {
957 s
->prnsts
|= SDHC_DOING_READ
| SDHC_DATA_INHIBIT
|
958 SDHC_DAT_LINE_ACTIVE
;
959 sdhci_read_block_from_card(s
);
961 s
->prnsts
|= SDHC_DOING_WRITE
| SDHC_DAT_LINE_ACTIVE
|
962 SDHC_SPACE_AVAILABLE
| SDHC_DATA_INHIBIT
;
963 sdhci_write_block_to_card(s
);
968 static bool sdhci_can_issue_command(SDHCIState
*s
)
970 if (!SDHC_CLOCK_IS_ON(s
->clkcon
) ||
971 (((s
->prnsts
& SDHC_DATA_INHIBIT
) || s
->stopped_state
) &&
972 ((s
->cmdreg
& SDHC_CMD_DATA_PRESENT
) ||
973 ((s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
&&
974 !(SDHC_COMMAND_TYPE(s
->cmdreg
) == SDHC_CMD_ABORT
))))) {
981 /* The Buffer Data Port register must be accessed in sequential and
982 * continuous manner */
984 sdhci_buff_access_is_sequential(SDHCIState
*s
, unsigned byte_num
)
986 if ((s
->data_count
& 0x3) != byte_num
) {
987 qemu_log_mask(LOG_GUEST_ERROR
,
988 "SDHCI: Non-sequential access to Buffer Data Port"
989 " register is prohibited\n");
995 static void sdhci_resume_pending_transfer(SDHCIState
*s
)
997 timer_del(s
->transfer_timer
);
998 sdhci_data_transfer(s
);
1001 static uint64_t sdhci_read(void *opaque
, hwaddr offset
, unsigned size
)
1003 SDHCIState
*s
= (SDHCIState
*)opaque
;
1006 if (timer_pending(s
->transfer_timer
)) {
1007 sdhci_resume_pending_transfer(s
);
1010 switch (offset
& ~0x3) {
1015 ret
= s
->blksize
| (s
->blkcnt
<< 16);
1021 ret
= s
->trnmod
| (s
->cmdreg
<< 16);
1023 case SDHC_RSPREG0
... SDHC_RSPREG3
:
1024 ret
= s
->rspreg
[((offset
& ~0x3) - SDHC_RSPREG0
) >> 2];
1027 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
1028 ret
= sdhci_read_dataport(s
, size
);
1029 trace_sdhci_access("rd", size
<< 3, offset
, "->", ret
, ret
);
1035 ret
= FIELD_DP32(ret
, SDHC_PRNSTS
, DAT_LVL
,
1036 sdbus_get_dat_lines(&s
->sdbus
));
1037 ret
= FIELD_DP32(ret
, SDHC_PRNSTS
, CMD_LVL
,
1038 sdbus_get_cmd_line(&s
->sdbus
));
1041 ret
= s
->hostctl1
| (s
->pwrcon
<< 8) | (s
->blkgap
<< 16) |
1045 ret
= s
->clkcon
| (s
->timeoutcon
<< 16);
1047 case SDHC_NORINTSTS
:
1048 ret
= s
->norintsts
| (s
->errintsts
<< 16);
1050 case SDHC_NORINTSTSEN
:
1051 ret
= s
->norintstsen
| (s
->errintstsen
<< 16);
1053 case SDHC_NORINTSIGEN
:
1054 ret
= s
->norintsigen
| (s
->errintsigen
<< 16);
1056 case SDHC_ACMD12ERRSTS
:
1057 ret
= s
->acmd12errsts
| (s
->hostctl2
<< 16);
1060 ret
= (uint32_t)s
->capareg
;
1062 case SDHC_CAPAB
+ 4:
1063 ret
= (uint32_t)(s
->capareg
>> 32);
1066 ret
= (uint32_t)s
->maxcurr
;
1068 case SDHC_MAXCURR
+ 4:
1069 ret
= (uint32_t)(s
->maxcurr
>> 32);
1074 case SDHC_ADMASYSADDR
:
1075 ret
= (uint32_t)s
->admasysaddr
;
1077 case SDHC_ADMASYSADDR
+ 4:
1078 ret
= (uint32_t)(s
->admasysaddr
>> 32);
1080 case SDHC_SLOT_INT_STATUS
:
1081 ret
= (s
->version
<< 16) | sdhci_slotint(s
);
1084 qemu_log_mask(LOG_UNIMP
, "SDHC rd_%ub @0x%02" HWADDR_PRIx
" "
1085 "not implemented\n", size
, offset
);
1089 ret
>>= (offset
& 0x3) * 8;
1090 ret
&= (1ULL << (size
* 8)) - 1;
1091 trace_sdhci_access("rd", size
<< 3, offset
, "->", ret
, ret
);
1095 static inline void sdhci_blkgap_write(SDHCIState
*s
, uint8_t value
)
1097 if ((value
& SDHC_STOP_AT_GAP_REQ
) && (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
)) {
1100 s
->blkgap
= value
& SDHC_STOP_AT_GAP_REQ
;
1102 if ((value
& SDHC_CONTINUE_REQ
) && s
->stopped_state
&&
1103 (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
) == 0) {
1104 if (s
->stopped_state
== sdhc_gap_read
) {
1105 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_READ
;
1106 sdhci_read_block_from_card(s
);
1108 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_WRITE
;
1109 sdhci_write_block_to_card(s
);
1111 s
->stopped_state
= sdhc_not_stopped
;
1112 } else if (!s
->stopped_state
&& (value
& SDHC_STOP_AT_GAP_REQ
)) {
1113 if (s
->prnsts
& SDHC_DOING_READ
) {
1114 s
->stopped_state
= sdhc_gap_read
;
1115 } else if (s
->prnsts
& SDHC_DOING_WRITE
) {
1116 s
->stopped_state
= sdhc_gap_write
;
1121 static inline void sdhci_reset_write(SDHCIState
*s
, uint8_t value
)
1124 case SDHC_RESET_ALL
:
1127 case SDHC_RESET_CMD
:
1128 s
->prnsts
&= ~SDHC_CMD_INHIBIT
;
1129 s
->norintsts
&= ~SDHC_NIS_CMDCMP
;
1131 case SDHC_RESET_DATA
:
1133 s
->prnsts
&= ~(SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
|
1134 SDHC_DOING_READ
| SDHC_DOING_WRITE
|
1135 SDHC_DATA_INHIBIT
| SDHC_DAT_LINE_ACTIVE
);
1136 s
->blkgap
&= ~(SDHC_STOP_AT_GAP_REQ
| SDHC_CONTINUE_REQ
);
1137 s
->stopped_state
= sdhc_not_stopped
;
1138 s
->norintsts
&= ~(SDHC_NIS_WBUFRDY
| SDHC_NIS_RBUFRDY
|
1139 SDHC_NIS_DMA
| SDHC_NIS_TRSCMP
| SDHC_NIS_BLKGAP
);
1145 sdhci_write(void *opaque
, hwaddr offset
, uint64_t val
, unsigned size
)
1147 SDHCIState
*s
= (SDHCIState
*)opaque
;
1148 unsigned shift
= 8 * (offset
& 0x3);
1149 uint32_t mask
= ~(((1ULL << (size
* 8)) - 1) << shift
);
1150 uint32_t value
= val
;
1153 if (timer_pending(s
->transfer_timer
)) {
1154 sdhci_resume_pending_transfer(s
);
1157 switch (offset
& ~0x3) {
1159 if (!TRANSFERRING_DATA(s
->prnsts
)) {
1160 s
->sdmasysad
= (s
->sdmasysad
& mask
) | value
;
1161 MASKED_WRITE(s
->sdmasysad
, mask
, value
);
1162 /* Writing to last byte of sdmasysad might trigger transfer */
1163 if (!(mask
& 0xFF000000) && s
->blkcnt
&&
1164 (s
->blksize
& BLOCK_SIZE_MASK
) &&
1165 SDHC_DMA_TYPE(s
->hostctl1
) == SDHC_CTRL_SDMA
) {
1166 if (s
->trnmod
& SDHC_TRNS_MULTI
) {
1167 sdhci_sdma_transfer_multi_blocks(s
);
1169 sdhci_sdma_transfer_single_block(s
);
1175 if (!TRANSFERRING_DATA(s
->prnsts
)) {
1176 uint16_t blksize
= s
->blksize
;
1179 * [14:12] SDMA Buffer Boundary
1180 * [11:00] Transfer Block Size
1182 MASKED_WRITE(s
->blksize
, mask
, extract32(value
, 0, 15));
1183 MASKED_WRITE(s
->blkcnt
, mask
>> 16, value
>> 16);
1185 /* Limit block size to the maximum buffer size */
1186 if (extract32(s
->blksize
, 0, 12) > s
->buf_maxsz
) {
1187 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Size 0x%x is larger than "
1188 "the maximum buffer 0x%x\n", __func__
, s
->blksize
,
1191 s
->blksize
= deposit32(s
->blksize
, 0, 12, s
->buf_maxsz
);
1195 * If the block size is programmed to a different value from
1196 * the previous one, reset the data pointer of s->fifo_buffer[]
1197 * so that s->fifo_buffer[] can be filled in using the new block
1198 * size in the next transfer.
1200 if (blksize
!= s
->blksize
) {
1207 MASKED_WRITE(s
->argument
, mask
, value
);
1210 /* DMA can be enabled only if it is supported as indicated by
1211 * capabilities register */
1212 if (!(s
->capareg
& R_SDHC_CAPAB_SDMA_MASK
)) {
1213 value
&= ~SDHC_TRNS_DMA
;
1216 /* TRNMOD writes are inhibited while Command Inhibit (DAT) is true */
1217 if (s
->prnsts
& SDHC_DATA_INHIBIT
) {
1221 MASKED_WRITE(s
->trnmod
, mask
, value
& SDHC_TRNMOD_MASK
);
1222 MASKED_WRITE(s
->cmdreg
, mask
>> 16, value
>> 16);
1224 /* Writing to the upper byte of CMDREG triggers SD command generation */
1225 if ((mask
& 0xFF000000) || !sdhci_can_issue_command(s
)) {
1229 sdhci_send_command(s
);
1232 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
1233 sdhci_write_dataport(s
, value
>> shift
, size
);
1237 if (!(mask
& 0xFF0000)) {
1238 sdhci_blkgap_write(s
, value
>> 16);
1240 MASKED_WRITE(s
->hostctl1
, mask
, value
);
1241 MASKED_WRITE(s
->pwrcon
, mask
>> 8, value
>> 8);
1242 MASKED_WRITE(s
->wakcon
, mask
>> 24, value
>> 24);
1243 if (!(s
->prnsts
& SDHC_CARD_PRESENT
) || ((s
->pwrcon
>> 1) & 0x7) < 5 ||
1244 !(s
->capareg
& (1 << (31 - ((s
->pwrcon
>> 1) & 0x7))))) {
1245 s
->pwrcon
&= ~SDHC_POWER_ON
;
1249 if (!(mask
& 0xFF000000)) {
1250 sdhci_reset_write(s
, value
>> 24);
1252 MASKED_WRITE(s
->clkcon
, mask
, value
);
1253 MASKED_WRITE(s
->timeoutcon
, mask
>> 16, value
>> 16);
1254 if (s
->clkcon
& SDHC_CLOCK_INT_EN
) {
1255 s
->clkcon
|= SDHC_CLOCK_INT_STABLE
;
1257 s
->clkcon
&= ~SDHC_CLOCK_INT_STABLE
;
1260 case SDHC_NORINTSTS
:
1261 if (s
->norintstsen
& SDHC_NISEN_CARDINT
) {
1262 value
&= ~SDHC_NIS_CARDINT
;
1264 s
->norintsts
&= mask
| ~value
;
1265 s
->errintsts
&= (mask
>> 16) | ~(value
>> 16);
1267 s
->norintsts
|= SDHC_NIS_ERR
;
1269 s
->norintsts
&= ~SDHC_NIS_ERR
;
1271 sdhci_update_irq(s
);
1273 case SDHC_NORINTSTSEN
:
1274 MASKED_WRITE(s
->norintstsen
, mask
, value
);
1275 MASKED_WRITE(s
->errintstsen
, mask
>> 16, value
>> 16);
1276 s
->norintsts
&= s
->norintstsen
;
1277 s
->errintsts
&= s
->errintstsen
;
1279 s
->norintsts
|= SDHC_NIS_ERR
;
1281 s
->norintsts
&= ~SDHC_NIS_ERR
;
1283 /* Quirk for Raspberry Pi: pending card insert interrupt
1284 * appears when first enabled after power on */
1285 if ((s
->norintstsen
& SDHC_NISEN_INSERT
) && s
->pending_insert_state
) {
1286 assert(s
->pending_insert_quirk
);
1287 s
->norintsts
|= SDHC_NIS_INSERT
;
1288 s
->pending_insert_state
= false;
1290 sdhci_update_irq(s
);
1292 case SDHC_NORINTSIGEN
:
1293 MASKED_WRITE(s
->norintsigen
, mask
, value
);
1294 MASKED_WRITE(s
->errintsigen
, mask
>> 16, value
>> 16);
1295 sdhci_update_irq(s
);
1298 MASKED_WRITE(s
->admaerr
, mask
, value
);
1300 case SDHC_ADMASYSADDR
:
1301 s
->admasysaddr
= (s
->admasysaddr
& (0xFFFFFFFF00000000ULL
|
1302 (uint64_t)mask
)) | (uint64_t)value
;
1304 case SDHC_ADMASYSADDR
+ 4:
1305 s
->admasysaddr
= (s
->admasysaddr
& (0x00000000FFFFFFFFULL
|
1306 ((uint64_t)mask
<< 32))) | ((uint64_t)value
<< 32);
1309 s
->acmd12errsts
|= value
;
1310 s
->errintsts
|= (value
>> 16) & s
->errintstsen
;
1311 if (s
->acmd12errsts
) {
1312 s
->errintsts
|= SDHC_EIS_CMD12ERR
;
1315 s
->norintsts
|= SDHC_NIS_ERR
;
1317 sdhci_update_irq(s
);
1319 case SDHC_ACMD12ERRSTS
:
1320 MASKED_WRITE(s
->acmd12errsts
, mask
, value
& UINT16_MAX
);
1321 if (s
->uhs_mode
>= UHS_I
) {
1322 MASKED_WRITE(s
->hostctl2
, mask
>> 16, value
>> 16);
1324 if (FIELD_EX32(s
->hostctl2
, SDHC_HOSTCTL2
, V18_ENA
)) {
1325 sdbus_set_voltage(&s
->sdbus
, SD_VOLTAGE_1_8V
);
1327 sdbus_set_voltage(&s
->sdbus
, SD_VOLTAGE_3_3V
);
1333 case SDHC_CAPAB
+ 4:
1335 case SDHC_MAXCURR
+ 4:
1336 qemu_log_mask(LOG_GUEST_ERROR
, "SDHC wr_%ub @0x%02" HWADDR_PRIx
1337 " <- 0x%08x read-only\n", size
, offset
, value
>> shift
);
1341 qemu_log_mask(LOG_UNIMP
, "SDHC wr_%ub @0x%02" HWADDR_PRIx
" <- 0x%08x "
1342 "not implemented\n", size
, offset
, value
>> shift
);
1345 trace_sdhci_access("wr", size
<< 3, offset
, "<-",
1346 value
>> shift
, value
>> shift
);
1349 static const MemoryRegionOps sdhci_mmio_le_ops
= {
1351 .write
= sdhci_write
,
1353 .min_access_size
= 1,
1354 .max_access_size
= 4,
1357 .endianness
= DEVICE_LITTLE_ENDIAN
,
1360 static const MemoryRegionOps sdhci_mmio_be_ops
= {
1362 .write
= sdhci_write
,
1364 .min_access_size
= 4,
1365 .max_access_size
= 4,
1368 .min_access_size
= 1,
1369 .max_access_size
= 4,
1372 .endianness
= DEVICE_BIG_ENDIAN
,
1375 static void sdhci_init_readonly_registers(SDHCIState
*s
, Error
**errp
)
1379 switch (s
->sd_spec_version
) {
1383 error_setg(errp
, "Only Spec v2/v3 are supported");
1386 s
->version
= (SDHC_HCVER_VENDOR
<< 8) | (s
->sd_spec_version
- 1);
1388 sdhci_check_capareg(s
, errp
);
1394 /* --- qdev common --- */
1396 void sdhci_initfn(SDHCIState
*s
)
1398 qbus_init(&s
->sdbus
, sizeof(s
->sdbus
), TYPE_SDHCI_BUS
, DEVICE(s
), "sd-bus");
1400 s
->insert_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sdhci_raise_insertion_irq
, s
);
1401 s
->transfer_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sdhci_data_transfer
, s
);
1403 s
->io_ops
= &sdhci_mmio_le_ops
;
1406 void sdhci_uninitfn(SDHCIState
*s
)
1408 timer_free(s
->insert_timer
);
1409 timer_free(s
->transfer_timer
);
1411 g_free(s
->fifo_buffer
);
1412 s
->fifo_buffer
= NULL
;
1415 void sdhci_common_realize(SDHCIState
*s
, Error
**errp
)
1419 switch (s
->endianness
) {
1420 case DEVICE_LITTLE_ENDIAN
:
1421 /* s->io_ops is little endian by default */
1423 case DEVICE_BIG_ENDIAN
:
1424 if (s
->io_ops
!= &sdhci_mmio_le_ops
) {
1425 error_setg(errp
, "SD controller doesn't support big endianness");
1428 s
->io_ops
= &sdhci_mmio_be_ops
;
1431 error_setg(errp
, "Incorrect endianness");
1435 sdhci_init_readonly_registers(s
, errp
);
1440 s
->buf_maxsz
= sdhci_get_fifolen(s
);
1441 s
->fifo_buffer
= g_malloc0(s
->buf_maxsz
);
1443 memory_region_init_io(&s
->iomem
, OBJECT(s
), s
->io_ops
, s
, "sdhci",
1444 SDHC_REGISTERS_MAP_SIZE
);
1447 void sdhci_common_unrealize(SDHCIState
*s
)
1449 /* This function is expected to be called only once for each class:
1450 * - SysBus: via DeviceClass->unrealize(),
1451 * - PCI: via PCIDeviceClass->exit().
1452 * However to avoid double-free and/or use-after-free we still nullify
1453 * this variable (better safe than sorry!). */
1454 g_free(s
->fifo_buffer
);
1455 s
->fifo_buffer
= NULL
;
1458 static bool sdhci_pending_insert_vmstate_needed(void *opaque
)
1460 SDHCIState
*s
= opaque
;
1462 return s
->pending_insert_state
;
1465 static const VMStateDescription sdhci_pending_insert_vmstate
= {
1466 .name
= "sdhci/pending-insert",
1468 .minimum_version_id
= 1,
1469 .needed
= sdhci_pending_insert_vmstate_needed
,
1470 .fields
= (const VMStateField
[]) {
1471 VMSTATE_BOOL(pending_insert_state
, SDHCIState
),
1472 VMSTATE_END_OF_LIST()
1476 const VMStateDescription sdhci_vmstate
= {
1479 .minimum_version_id
= 1,
1480 .fields
= (const VMStateField
[]) {
1481 VMSTATE_UINT32(sdmasysad
, SDHCIState
),
1482 VMSTATE_UINT16(blksize
, SDHCIState
),
1483 VMSTATE_UINT16(blkcnt
, SDHCIState
),
1484 VMSTATE_UINT32(argument
, SDHCIState
),
1485 VMSTATE_UINT16(trnmod
, SDHCIState
),
1486 VMSTATE_UINT16(cmdreg
, SDHCIState
),
1487 VMSTATE_UINT32_ARRAY(rspreg
, SDHCIState
, 4),
1488 VMSTATE_UINT32(prnsts
, SDHCIState
),
1489 VMSTATE_UINT8(hostctl1
, SDHCIState
),
1490 VMSTATE_UINT8(pwrcon
, SDHCIState
),
1491 VMSTATE_UINT8(blkgap
, SDHCIState
),
1492 VMSTATE_UINT8(wakcon
, SDHCIState
),
1493 VMSTATE_UINT16(clkcon
, SDHCIState
),
1494 VMSTATE_UINT8(timeoutcon
, SDHCIState
),
1495 VMSTATE_UINT8(admaerr
, SDHCIState
),
1496 VMSTATE_UINT16(norintsts
, SDHCIState
),
1497 VMSTATE_UINT16(errintsts
, SDHCIState
),
1498 VMSTATE_UINT16(norintstsen
, SDHCIState
),
1499 VMSTATE_UINT16(errintstsen
, SDHCIState
),
1500 VMSTATE_UINT16(norintsigen
, SDHCIState
),
1501 VMSTATE_UINT16(errintsigen
, SDHCIState
),
1502 VMSTATE_UINT16(acmd12errsts
, SDHCIState
),
1503 VMSTATE_UINT16(data_count
, SDHCIState
),
1504 VMSTATE_UINT64(admasysaddr
, SDHCIState
),
1505 VMSTATE_UINT8(stopped_state
, SDHCIState
),
1506 VMSTATE_VBUFFER_UINT32(fifo_buffer
, SDHCIState
, 1, NULL
, buf_maxsz
),
1507 VMSTATE_TIMER_PTR(insert_timer
, SDHCIState
),
1508 VMSTATE_TIMER_PTR(transfer_timer
, SDHCIState
),
1509 VMSTATE_END_OF_LIST()
1511 .subsections
= (const VMStateDescription
* const []) {
1512 &sdhci_pending_insert_vmstate
,
1517 void sdhci_common_class_init(ObjectClass
*klass
, void *data
)
1519 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1521 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1522 dc
->vmsd
= &sdhci_vmstate
;
1523 device_class_set_legacy_reset(dc
, sdhci_poweron_reset
);
1526 /* --- qdev SysBus --- */
1528 static Property sdhci_sysbus_properties
[] = {
1529 DEFINE_SDHCI_COMMON_PROPERTIES(SDHCIState
),
1530 DEFINE_PROP_BOOL("pending-insert-quirk", SDHCIState
, pending_insert_quirk
,
1532 DEFINE_PROP_LINK("dma", SDHCIState
,
1533 dma_mr
, TYPE_MEMORY_REGION
, MemoryRegion
*),
1534 DEFINE_PROP_END_OF_LIST(),
1537 static void sdhci_sysbus_init(Object
*obj
)
1539 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1544 static void sdhci_sysbus_finalize(Object
*obj
)
1546 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1549 object_unparent(OBJECT(s
->dma_mr
));
1555 static void sdhci_sysbus_realize(DeviceState
*dev
, Error
**errp
)
1558 SDHCIState
*s
= SYSBUS_SDHCI(dev
);
1559 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1561 sdhci_common_realize(s
, errp
);
1567 s
->dma_as
= &s
->sysbus_dma_as
;
1568 address_space_init(s
->dma_as
, s
->dma_mr
, "sdhci-dma");
1570 /* use system_memory() if property "dma" not set */
1571 s
->dma_as
= &address_space_memory
;
1574 sysbus_init_irq(sbd
, &s
->irq
);
1576 sysbus_init_mmio(sbd
, &s
->iomem
);
1579 static void sdhci_sysbus_unrealize(DeviceState
*dev
)
1581 SDHCIState
*s
= SYSBUS_SDHCI(dev
);
1583 sdhci_common_unrealize(s
);
1586 address_space_destroy(s
->dma_as
);
1590 static void sdhci_sysbus_class_init(ObjectClass
*klass
, void *data
)
1592 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1594 device_class_set_props(dc
, sdhci_sysbus_properties
);
1595 dc
->realize
= sdhci_sysbus_realize
;
1596 dc
->unrealize
= sdhci_sysbus_unrealize
;
1598 sdhci_common_class_init(klass
, data
);
1601 static const TypeInfo sdhci_sysbus_info
= {
1602 .name
= TYPE_SYSBUS_SDHCI
,
1603 .parent
= TYPE_SYS_BUS_DEVICE
,
1604 .instance_size
= sizeof(SDHCIState
),
1605 .instance_init
= sdhci_sysbus_init
,
1606 .instance_finalize
= sdhci_sysbus_finalize
,
1607 .class_init
= sdhci_sysbus_class_init
,
1610 /* --- qdev bus master --- */
1612 static void sdhci_bus_class_init(ObjectClass
*klass
, void *data
)
1614 SDBusClass
*sbc
= SD_BUS_CLASS(klass
);
1616 sbc
->set_inserted
= sdhci_set_inserted
;
1617 sbc
->set_readonly
= sdhci_set_readonly
;
1620 static const TypeInfo sdhci_bus_info
= {
1621 .name
= TYPE_SDHCI_BUS
,
1622 .parent
= TYPE_SD_BUS
,
1623 .instance_size
= sizeof(SDBus
),
1624 .class_init
= sdhci_bus_class_init
,
1627 /* --- qdev i.MX eSDHC --- */
1629 #define USDHC_MIX_CTRL 0x48
1631 #define USDHC_VENDOR_SPEC 0xc0
1632 #define USDHC_IMX_FRC_SDCLK_ON (1 << 8)
1634 #define USDHC_DLL_CTRL 0x60
1636 #define USDHC_TUNING_CTRL 0xcc
1637 #define USDHC_TUNE_CTRL_STATUS 0x68
1638 #define USDHC_WTMK_LVL 0x44
1640 /* Undocumented register used by guests working around erratum ERR004536 */
1641 #define USDHC_UNDOCUMENTED_REG27 0x6c
1643 #define USDHC_CTRL_4BITBUS (0x1 << 1)
1644 #define USDHC_CTRL_8BITBUS (0x2 << 1)
1646 #define USDHC_PRNSTS_SDSTB (1 << 3)
1648 static uint64_t usdhc_read(void *opaque
, hwaddr offset
, unsigned size
)
1650 SDHCIState
*s
= SYSBUS_SDHCI(opaque
);
1656 return sdhci_read(opaque
, offset
, size
);
1660 * For a detailed explanation on the following bit
1661 * manipulation code see comments in a similar part of
1664 hostctl1
= SDHC_DMA_TYPE(s
->hostctl1
) << (8 - 3);
1666 if (s
->hostctl1
& SDHC_CTRL_8BITBUS
) {
1667 hostctl1
|= USDHC_CTRL_8BITBUS
;
1670 if (s
->hostctl1
& SDHC_CTRL_4BITBUS
) {
1671 hostctl1
|= USDHC_CTRL_4BITBUS
;
1675 ret
|= (uint32_t)s
->blkgap
<< 16;
1676 ret
|= (uint32_t)s
->wakcon
<< 24;
1681 /* Add SDSTB (SD Clock Stable) bit to PRNSTS */
1682 ret
= sdhci_read(opaque
, offset
, size
) & ~USDHC_PRNSTS_SDSTB
;
1683 if (s
->clkcon
& SDHC_CLOCK_INT_STABLE
) {
1684 ret
|= USDHC_PRNSTS_SDSTB
;
1688 case USDHC_VENDOR_SPEC
:
1689 ret
= s
->vendor_spec
;
1691 case USDHC_DLL_CTRL
:
1692 case USDHC_TUNE_CTRL_STATUS
:
1693 case USDHC_UNDOCUMENTED_REG27
:
1694 case USDHC_TUNING_CTRL
:
1695 case USDHC_MIX_CTRL
:
1696 case USDHC_WTMK_LVL
:
1705 usdhc_write(void *opaque
, hwaddr offset
, uint64_t val
, unsigned size
)
1707 SDHCIState
*s
= SYSBUS_SDHCI(opaque
);
1709 uint32_t value
= (uint32_t)val
;
1712 case USDHC_DLL_CTRL
:
1713 case USDHC_TUNE_CTRL_STATUS
:
1714 case USDHC_UNDOCUMENTED_REG27
:
1715 case USDHC_TUNING_CTRL
:
1716 case USDHC_WTMK_LVL
:
1719 case USDHC_VENDOR_SPEC
:
1720 s
->vendor_spec
= value
;
1721 switch (s
->vendor
) {
1722 case SDHCI_VENDOR_IMX
:
1723 if (value
& USDHC_IMX_FRC_SDCLK_ON
) {
1724 s
->prnsts
&= ~SDHC_IMX_CLOCK_GATE_OFF
;
1726 s
->prnsts
|= SDHC_IMX_CLOCK_GATE_OFF
;
1736 * Here's What ESDHCI has at offset 0x28 (SDHC_HOSTCTL)
1739 * |-----------+--------+--------+-----------+----------+---------|
1740 * | Card | Card | Endian | DATA3 | Data | Led |
1741 * | Detect | Detect | Mode | as Card | Transfer | Control |
1742 * | Signal | Test | | Detection | Width | |
1743 * | Selection | Level | | Pin | | |
1744 * |-----------+--------+--------+-----------+----------+---------|
1749 * |----------+------|
1750 * | Reserved | DMA |
1753 * |----------+------|
1755 * and here's what SDCHI spec expects those offsets to be:
1757 * 0x28 (Host Control Register)
1760 * |--------+--------+----------+------+--------+----------+---------|
1761 * | Card | Card | Extended | DMA | High | Data | LED |
1762 * | Detect | Detect | Data | Sel. | Speed | Transfer | Control |
1763 * | Signal | Test | Transfer | | Enable | Width | |
1764 * | Sel. | Level | Width | | | | |
1765 * |--------+--------+----------+------+--------+----------+---------|
1767 * and 0x29 (Power Control Register)
1769 * |----------------------------------|
1770 * | Power Control Register |
1772 * | Description omitted, |
1773 * | since it has no analog in ESDHCI |
1775 * |----------------------------------|
1777 * Since offsets 0x2A and 0x2B should be compatible between
1778 * both IP specs we only need to reconcile least 16-bit of the
1779 * word we've been given.
1783 * First, save bits 7 6 and 0 since they are identical
1785 hostctl1
= value
& (SDHC_CTRL_LED
|
1786 SDHC_CTRL_CDTEST_INS
|
1787 SDHC_CTRL_CDTEST_EN
);
1789 * Second, split "Data Transfer Width" from bits 2 and 1 in to
1792 if (value
& USDHC_CTRL_8BITBUS
) {
1793 hostctl1
|= SDHC_CTRL_8BITBUS
;
1796 if (value
& USDHC_CTRL_4BITBUS
) {
1797 hostctl1
|= USDHC_CTRL_4BITBUS
;
1801 * Third, move DMA select from bits 9 and 8 to bits 4 and 3
1803 hostctl1
|= SDHC_DMA_TYPE(value
>> (8 - 3));
1806 * Now place the corrected value into low 16-bit of the value
1807 * we are going to give standard SDHCI write function
1809 * NOTE: This transformation should be the inverse of what can
1810 * be found in drivers/mmc/host/sdhci-esdhc-imx.c in Linux
1813 value
&= ~UINT16_MAX
;
1815 value
|= (uint16_t)s
->pwrcon
<< 8;
1817 sdhci_write(opaque
, offset
, value
, size
);
1820 case USDHC_MIX_CTRL
:
1822 * So, when SD/MMC stack in Linux tries to write to "Transfer
1823 * Mode Register", ESDHC i.MX quirk code will translate it
1824 * into a write to ESDHC_MIX_CTRL, so we do the opposite in
1825 * order to get where we started
1827 * Note that Auto CMD23 Enable bit is located in a wrong place
1828 * on i.MX, but since it is not used by QEMU we do not care.
1830 * We don't want to call sdhci_write(.., SDHC_TRNMOD, ...)
1831 * here because it will result in a call to
1832 * sdhci_send_command(s) which we don't want.
1835 s
->trnmod
= value
& UINT16_MAX
;
1839 * Similar to above, but this time a write to "Command
1840 * Register" will be translated into a 4-byte write to
1841 * "Transfer Mode register" where lower 16-bit of value would
1842 * be set to zero. So what we do is fill those bits with
1843 * cached value from s->trnmod and let the SDHCI
1844 * infrastructure handle the rest
1846 sdhci_write(opaque
, offset
, val
| s
->trnmod
, size
);
1850 * ESDHCI does not implement "Host SDMA Buffer Boundary", and
1851 * Linux driver will try to zero this field out which will
1852 * break the rest of SDHCI emulation.
1854 * Linux defaults to maximum possible setting (512K boundary)
1855 * and it seems to be the only option that i.MX IP implements,
1856 * so we artificially set it to that value.
1861 sdhci_write(opaque
, offset
, val
, size
);
1866 static const MemoryRegionOps usdhc_mmio_ops
= {
1868 .write
= usdhc_write
,
1870 .min_access_size
= 1,
1871 .max_access_size
= 4,
1874 .endianness
= DEVICE_LITTLE_ENDIAN
,
1877 static void imx_usdhc_init(Object
*obj
)
1879 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1881 s
->io_ops
= &usdhc_mmio_ops
;
1882 s
->quirks
= SDHCI_QUIRK_NO_BUSY_IRQ
;
1885 static const TypeInfo imx_usdhc_info
= {
1886 .name
= TYPE_IMX_USDHC
,
1887 .parent
= TYPE_SYSBUS_SDHCI
,
1888 .instance_init
= imx_usdhc_init
,
1891 /* --- qdev Samsung s3c --- */
1893 #define S3C_SDHCI_CONTROL2 0x80
1894 #define S3C_SDHCI_CONTROL3 0x84
1895 #define S3C_SDHCI_CONTROL4 0x8c
1897 static uint64_t sdhci_s3c_read(void *opaque
, hwaddr offset
, unsigned size
)
1902 case S3C_SDHCI_CONTROL2
:
1903 case S3C_SDHCI_CONTROL3
:
1904 case S3C_SDHCI_CONTROL4
:
1909 ret
= sdhci_read(opaque
, offset
, size
);
1916 static void sdhci_s3c_write(void *opaque
, hwaddr offset
, uint64_t val
,
1920 case S3C_SDHCI_CONTROL2
:
1921 case S3C_SDHCI_CONTROL3
:
1922 case S3C_SDHCI_CONTROL4
:
1926 sdhci_write(opaque
, offset
, val
, size
);
1931 static const MemoryRegionOps sdhci_s3c_mmio_ops
= {
1932 .read
= sdhci_s3c_read
,
1933 .write
= sdhci_s3c_write
,
1935 .min_access_size
= 1,
1936 .max_access_size
= 4,
1939 .endianness
= DEVICE_LITTLE_ENDIAN
,
1942 static void sdhci_s3c_init(Object
*obj
)
1944 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1946 s
->io_ops
= &sdhci_s3c_mmio_ops
;
1949 static const TypeInfo sdhci_s3c_info
= {
1950 .name
= TYPE_S3C_SDHCI
,
1951 .parent
= TYPE_SYSBUS_SDHCI
,
1952 .instance_init
= sdhci_s3c_init
,
1955 static void sdhci_register_types(void)
1957 type_register_static(&sdhci_sysbus_info
);
1958 type_register_static(&sdhci_bus_info
);
1959 type_register_static(&imx_usdhc_info
);
1960 type_register_static(&sdhci_s3c_info
);
1963 type_init(sdhci_register_types
)