2 * SD Association Host Standard Specification v2.0 controller emulation
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Mitsyanko Igor <i.mitsyanko@samsung.com>
6 * Peter A.G. Crosthwaite <peter.crosthwaite@petalogix.com>
8 * Based on MMC controller for Samsung S5PC1xx-based board emulation
9 * by Alexey Merkulov and Vladimir Monakhov.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 * See the GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include "sysemu/blockdev.h"
27 #include "sysemu/dma.h"
28 #include "qemu/timer.h"
29 #include "block/block_int.h"
30 #include "qemu/bitops.h"
34 /* host controller debug messages */
40 #define DPRINT_L1(fmt, args...) do { } while (0)
41 #define DPRINT_L2(fmt, args...) do { } while (0)
42 #define ERRPRINT(fmt, args...) do { } while (0)
44 #define DPRINT_L1(fmt, args...) \
45 do {fprintf(stderr, "QEMU SDHC: "fmt, ## args); } while (0)
46 #define DPRINT_L2(fmt, args...) do { } while (0)
47 #define ERRPRINT(fmt, args...) \
48 do {fprintf(stderr, "QEMU SDHC ERROR: "fmt, ## args); } while (0)
50 #define DPRINT_L1(fmt, args...) \
51 do {fprintf(stderr, "QEMU SDHC: "fmt, ## args); } while (0)
52 #define DPRINT_L2(fmt, args...) \
53 do {fprintf(stderr, "QEMU SDHC: "fmt, ## args); } while (0)
54 #define ERRPRINT(fmt, args...) \
55 do {fprintf(stderr, "QEMU SDHC ERROR: "fmt, ## args); } while (0)
58 /* Default SD/MMC host controller features information, which will be
59 * presented in CAPABILITIES register of generic SD host controller at reset.
60 * If not stated otherwise:
61 * 0 - not supported, 1 - supported, other - prohibited.
63 #define SDHC_CAPAB_64BITBUS 0ul /* 64-bit System Bus Support */
64 #define SDHC_CAPAB_18V 1ul /* Voltage support 1.8v */
65 #define SDHC_CAPAB_30V 0ul /* Voltage support 3.0v */
66 #define SDHC_CAPAB_33V 1ul /* Voltage support 3.3v */
67 #define SDHC_CAPAB_SUSPRESUME 0ul /* Suspend/resume support */
68 #define SDHC_CAPAB_SDMA 1ul /* SDMA support */
69 #define SDHC_CAPAB_HIGHSPEED 1ul /* High speed support */
70 #define SDHC_CAPAB_ADMA1 1ul /* ADMA1 support */
71 #define SDHC_CAPAB_ADMA2 1ul /* ADMA2 support */
72 /* Maximum host controller R/W buffers size
73 * Possible values: 512, 1024, 2048 bytes */
74 #define SDHC_CAPAB_MAXBLOCKLENGTH 512ul
75 /* Maximum clock frequency for SDclock in MHz
76 * value in range 10-63 MHz, 0 - not defined */
77 #define SDHC_CAPAB_BASECLKFREQ 0ul
78 #define SDHC_CAPAB_TOUNIT 1ul /* Timeout clock unit 0 - kHz, 1 - MHz */
79 /* Timeout clock frequency 1-63, 0 - not defined */
80 #define SDHC_CAPAB_TOCLKFREQ 0ul
82 /* Now check all parameters and calculate CAPABILITIES REGISTER value */
83 #if SDHC_CAPAB_64BITBUS > 1 || SDHC_CAPAB_18V > 1 || SDHC_CAPAB_30V > 1 || \
84 SDHC_CAPAB_33V > 1 || SDHC_CAPAB_SUSPRESUME > 1 || SDHC_CAPAB_SDMA > 1 || \
85 SDHC_CAPAB_HIGHSPEED > 1 || SDHC_CAPAB_ADMA2 > 1 || SDHC_CAPAB_ADMA1 > 1 ||\
87 #error Capabilities features can have value 0 or 1 only!
90 #if SDHC_CAPAB_MAXBLOCKLENGTH == 512
91 #define MAX_BLOCK_LENGTH 0ul
92 #elif SDHC_CAPAB_MAXBLOCKLENGTH == 1024
93 #define MAX_BLOCK_LENGTH 1ul
94 #elif SDHC_CAPAB_MAXBLOCKLENGTH == 2048
95 #define MAX_BLOCK_LENGTH 2ul
97 #error Max host controller block size can have value 512, 1024 or 2048 only!
100 #if (SDHC_CAPAB_BASECLKFREQ > 0 && SDHC_CAPAB_BASECLKFREQ < 10) || \
101 SDHC_CAPAB_BASECLKFREQ > 63
102 #error SDclock frequency can have value in range 0, 10-63 only!
105 #if SDHC_CAPAB_TOCLKFREQ > 63
106 #error Timeout clock frequency can have value in range 0-63 only!
109 #define SDHC_CAPAB_REG_DEFAULT \
110 ((SDHC_CAPAB_64BITBUS << 28) | (SDHC_CAPAB_18V << 26) | \
111 (SDHC_CAPAB_30V << 25) | (SDHC_CAPAB_33V << 24) | \
112 (SDHC_CAPAB_SUSPRESUME << 23) | (SDHC_CAPAB_SDMA << 22) | \
113 (SDHC_CAPAB_HIGHSPEED << 21) | (SDHC_CAPAB_ADMA1 << 20) | \
114 (SDHC_CAPAB_ADMA2 << 19) | (MAX_BLOCK_LENGTH << 16) | \
115 (SDHC_CAPAB_BASECLKFREQ << 8) | (SDHC_CAPAB_TOUNIT << 7) | \
116 (SDHC_CAPAB_TOCLKFREQ))
118 #define MASKED_WRITE(reg, mask, val) (reg = (reg & (mask)) | (val))
120 static uint8_t sdhci_slotint(SDHCIState
*s
)
122 return (s
->norintsts
& s
->norintsigen
) || (s
->errintsts
& s
->errintsigen
) ||
123 ((s
->norintsts
& SDHC_NIS_INSERT
) && (s
->wakcon
& SDHC_WKUP_ON_INS
)) ||
124 ((s
->norintsts
& SDHC_NIS_REMOVE
) && (s
->wakcon
& SDHC_WKUP_ON_RMV
));
127 static inline void sdhci_update_irq(SDHCIState
*s
)
129 qemu_set_irq(s
->irq
, sdhci_slotint(s
));
132 static void sdhci_raise_insertion_irq(void *opaque
)
134 SDHCIState
*s
= (SDHCIState
*)opaque
;
136 if (s
->norintsts
& SDHC_NIS_REMOVE
) {
137 qemu_mod_timer(s
->insert_timer
,
138 qemu_get_clock_ns(vm_clock
) + SDHC_INSERTION_DELAY
);
140 s
->prnsts
= 0x1ff0000;
141 if (s
->norintstsen
& SDHC_NISEN_INSERT
) {
142 s
->norintsts
|= SDHC_NIS_INSERT
;
148 static void sdhci_insert_eject_cb(void *opaque
, int irq
, int level
)
150 SDHCIState
*s
= (SDHCIState
*)opaque
;
151 DPRINT_L1("Card state changed: %s!\n", level
? "insert" : "eject");
153 if ((s
->norintsts
& SDHC_NIS_REMOVE
) && level
) {
154 /* Give target some time to notice card ejection */
155 qemu_mod_timer(s
->insert_timer
,
156 qemu_get_clock_ns(vm_clock
) + SDHC_INSERTION_DELAY
);
159 s
->prnsts
= 0x1ff0000;
160 if (s
->norintstsen
& SDHC_NISEN_INSERT
) {
161 s
->norintsts
|= SDHC_NIS_INSERT
;
164 s
->prnsts
= 0x1fa0000;
165 s
->pwrcon
&= ~SDHC_POWER_ON
;
166 s
->clkcon
&= ~SDHC_CLOCK_SDCLK_EN
;
167 if (s
->norintstsen
& SDHC_NISEN_REMOVE
) {
168 s
->norintsts
|= SDHC_NIS_REMOVE
;
175 static void sdhci_card_readonly_cb(void *opaque
, int irq
, int level
)
177 SDHCIState
*s
= (SDHCIState
*)opaque
;
180 s
->prnsts
&= ~SDHC_WRITE_PROTECT
;
183 s
->prnsts
|= SDHC_WRITE_PROTECT
;
187 static void sdhci_reset(SDHCIState
*s
)
189 qemu_del_timer(s
->insert_timer
);
190 qemu_del_timer(s
->transfer_timer
);
191 /* Set all registers to 0. Capabilities registers are not cleared
192 * and assumed to always preserve their value, given to them during
194 memset(&s
->sdmasysad
, 0, (uintptr_t)&s
->capareg
- (uintptr_t)&s
->sdmasysad
);
196 sd_set_cb(s
->card
, s
->ro_cb
, s
->eject_cb
);
198 s
->stopped_state
= sdhc_not_stopped
;
201 static void sdhci_do_data_transfer(void *opaque
)
203 SDHCIState
*s
= (SDHCIState
*)opaque
;
205 SDHCI_GET_CLASS(s
)->data_transfer(s
);
208 static void sdhci_send_command(SDHCIState
*s
)
211 uint8_t response
[16];
216 request
.cmd
= s
->cmdreg
>> 8;
217 request
.arg
= s
->argument
;
218 DPRINT_L1("sending CMD%u ARG[0x%08x]\n", request
.cmd
, request
.arg
);
219 rlen
= sd_do_command(s
->card
, &request
, response
);
221 if (s
->cmdreg
& SDHC_CMD_RESPONSE
) {
223 s
->rspreg
[0] = (response
[0] << 24) | (response
[1] << 16) |
224 (response
[2] << 8) | response
[3];
225 s
->rspreg
[1] = s
->rspreg
[2] = s
->rspreg
[3] = 0;
226 DPRINT_L1("Response: RSPREG[31..0]=0x%08x\n", s
->rspreg
[0]);
227 } else if (rlen
== 16) {
228 s
->rspreg
[0] = (response
[11] << 24) | (response
[12] << 16) |
229 (response
[13] << 8) | response
[14];
230 s
->rspreg
[1] = (response
[7] << 24) | (response
[8] << 16) |
231 (response
[9] << 8) | response
[10];
232 s
->rspreg
[2] = (response
[3] << 24) | (response
[4] << 16) |
233 (response
[5] << 8) | response
[6];
234 s
->rspreg
[3] = (response
[0] << 16) | (response
[1] << 8) |
236 DPRINT_L1("Response received:\n RSPREG[127..96]=0x%08x, RSPREG[95.."
237 "64]=0x%08x,\n RSPREG[63..32]=0x%08x, RSPREG[31..0]=0x%08x\n",
238 s
->rspreg
[3], s
->rspreg
[2], s
->rspreg
[1], s
->rspreg
[0]);
240 ERRPRINT("Timeout waiting for command response\n");
241 if (s
->errintstsen
& SDHC_EISEN_CMDTIMEOUT
) {
242 s
->errintsts
|= SDHC_EIS_CMDTIMEOUT
;
243 s
->norintsts
|= SDHC_NIS_ERR
;
247 if ((s
->norintstsen
& SDHC_NISEN_TRSCMP
) &&
248 (s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
) {
249 s
->norintsts
|= SDHC_NIS_TRSCMP
;
251 } else if (rlen
!= 0 && (s
->errintstsen
& SDHC_EISEN_CMDIDX
)) {
252 s
->errintsts
|= SDHC_EIS_CMDIDX
;
253 s
->norintsts
|= SDHC_NIS_ERR
;
256 if (s
->norintstsen
& SDHC_NISEN_CMDCMP
) {
257 s
->norintsts
|= SDHC_NIS_CMDCMP
;
262 if (s
->blksize
&& (s
->cmdreg
& SDHC_CMD_DATA_PRESENT
)) {
263 sdhci_do_data_transfer(s
);
267 static void sdhci_end_transfer(SDHCIState
*s
)
269 /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */
270 if ((s
->trnmod
& SDHC_TRNS_ACMD12
) != 0) {
272 uint8_t response
[16];
276 DPRINT_L1("Automatically issue CMD%d %08x\n", request
.cmd
, request
.arg
);
277 sd_do_command(s
->card
, &request
, response
);
278 /* Auto CMD12 response goes to the upper Response register */
279 s
->rspreg
[3] = (response
[0] << 24) | (response
[1] << 16) |
280 (response
[2] << 8) | response
[3];
283 s
->prnsts
&= ~(SDHC_DOING_READ
| SDHC_DOING_WRITE
|
284 SDHC_DAT_LINE_ACTIVE
| SDHC_DATA_INHIBIT
|
285 SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
);
287 if (s
->norintstsen
& SDHC_NISEN_TRSCMP
) {
288 s
->norintsts
|= SDHC_NIS_TRSCMP
;
295 * Programmed i/o data transfer
298 /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
299 static void sdhci_read_block_from_card(SDHCIState
*s
)
303 if ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
304 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) {
308 for (index
= 0; index
< (s
->blksize
& 0x0fff); index
++) {
309 s
->fifo_buffer
[index
] = sd_read_data(s
->card
);
312 /* New data now available for READ through Buffer Port Register */
313 s
->prnsts
|= SDHC_DATA_AVAILABLE
;
314 if (s
->norintstsen
& SDHC_NISEN_RBUFRDY
) {
315 s
->norintsts
|= SDHC_NIS_RBUFRDY
;
318 /* Clear DAT line active status if that was the last block */
319 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
320 ((s
->trnmod
& SDHC_TRNS_MULTI
) && s
->blkcnt
== 1)) {
321 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
324 /* If stop at block gap request was set and it's not the last block of
325 * data - generate Block Event interrupt */
326 if (s
->stopped_state
== sdhc_gap_read
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
328 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
329 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
330 s
->norintsts
|= SDHC_EIS_BLKGAP
;
337 /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
338 static uint32_t sdhci_read_dataport(SDHCIState
*s
, unsigned size
)
343 /* first check that a valid data exists in host controller input buffer */
344 if ((s
->prnsts
& SDHC_DATA_AVAILABLE
) == 0) {
345 ERRPRINT("Trying to read from empty buffer\n");
349 for (i
= 0; i
< size
; i
++) {
350 value
|= s
->fifo_buffer
[s
->data_count
] << i
* 8;
352 /* check if we've read all valid data (blksize bytes) from buffer */
353 if ((s
->data_count
) >= (s
->blksize
& 0x0fff)) {
354 DPRINT_L2("All %u bytes of data have been read from input buffer\n",
356 s
->prnsts
&= ~SDHC_DATA_AVAILABLE
; /* no more data in a buffer */
357 s
->data_count
= 0; /* next buff read must start at position [0] */
359 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
363 /* if that was the last block of data */
364 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
365 ((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) ||
366 /* stop at gap request */
367 (s
->stopped_state
== sdhc_gap_read
&&
368 !(s
->prnsts
& SDHC_DAT_LINE_ACTIVE
))) {
369 SDHCI_GET_CLASS(s
)->end_data_transfer(s
);
370 } else { /* if there are more data, read next block from card */
371 SDHCI_GET_CLASS(s
)->read_block_from_card(s
);
380 /* Write data from host controller FIFO to card */
381 static void sdhci_write_block_to_card(SDHCIState
*s
)
385 if (s
->prnsts
& SDHC_SPACE_AVAILABLE
) {
386 if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
387 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
393 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
394 if (s
->blkcnt
== 0) {
401 for (index
= 0; index
< (s
->blksize
& 0x0fff); index
++) {
402 sd_write_data(s
->card
, s
->fifo_buffer
[index
]);
405 /* Next data can be written through BUFFER DATORT register */
406 s
->prnsts
|= SDHC_SPACE_AVAILABLE
;
407 if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
408 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
411 /* Finish transfer if that was the last block of data */
412 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
413 ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
414 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0))) {
415 SDHCI_GET_CLASS(s
)->end_data_transfer(s
);
418 /* Generate Block Gap Event if requested and if not the last block */
419 if (s
->stopped_state
== sdhc_gap_write
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
421 s
->prnsts
&= ~SDHC_DOING_WRITE
;
422 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
423 s
->norintsts
|= SDHC_EIS_BLKGAP
;
425 SDHCI_GET_CLASS(s
)->end_data_transfer(s
);
431 /* Write @size bytes of @value data to host controller @s Buffer Data Port
433 static void sdhci_write_dataport(SDHCIState
*s
, uint32_t value
, unsigned size
)
437 /* Check that there is free space left in a buffer */
438 if (!(s
->prnsts
& SDHC_SPACE_AVAILABLE
)) {
439 ERRPRINT("Can't write to data buffer: buffer full\n");
443 for (i
= 0; i
< size
; i
++) {
444 s
->fifo_buffer
[s
->data_count
] = value
& 0xFF;
447 if (s
->data_count
>= (s
->blksize
& 0x0fff)) {
448 DPRINT_L2("write buffer filled with %u bytes of data\n",
451 s
->prnsts
&= ~SDHC_SPACE_AVAILABLE
;
452 if (s
->prnsts
& SDHC_DOING_WRITE
) {
453 SDHCI_GET_CLASS(s
)->write_block_to_card(s
);
460 * Single DMA data transfer
463 /* Multi block SDMA transfer */
464 static void sdhci_sdma_transfer_multi_blocks(SDHCIState
*s
)
466 bool page_aligned
= false;
467 unsigned int n
, begin
;
468 const uint16_t block_size
= s
->blksize
& 0x0fff;
469 uint32_t boundary_chk
= 1 << (((s
->blksize
& 0xf000) >> 12) + 12);
470 uint32_t boundary_count
= boundary_chk
- (s
->sdmasysad
% boundary_chk
);
472 /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
473 * possible stop at page boundary if initial address is not page aligned,
474 * allow them to work properly */
475 if ((s
->sdmasysad
% boundary_chk
) == 0) {
479 if (s
->trnmod
& SDHC_TRNS_READ
) {
480 s
->prnsts
|= SDHC_DOING_READ
| SDHC_DATA_INHIBIT
|
481 SDHC_DAT_LINE_ACTIVE
;
483 if (s
->data_count
== 0) {
484 for (n
= 0; n
< block_size
; n
++) {
485 s
->fifo_buffer
[n
] = sd_read_data(s
->card
);
488 begin
= s
->data_count
;
489 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
490 s
->data_count
= boundary_count
+ begin
;
493 s
->data_count
= block_size
;
494 boundary_count
-= block_size
- begin
;
495 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
499 dma_memory_write(&dma_context_memory
, s
->sdmasysad
,
500 &s
->fifo_buffer
[begin
], s
->data_count
- begin
);
501 s
->sdmasysad
+= s
->data_count
- begin
;
502 if (s
->data_count
== block_size
) {
505 if (page_aligned
&& boundary_count
== 0) {
510 s
->prnsts
|= SDHC_DOING_WRITE
| SDHC_DATA_INHIBIT
|
511 SDHC_DAT_LINE_ACTIVE
;
513 begin
= s
->data_count
;
514 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
515 s
->data_count
= boundary_count
+ begin
;
518 s
->data_count
= block_size
;
519 boundary_count
-= block_size
- begin
;
521 dma_memory_read(&dma_context_memory
, s
->sdmasysad
,
522 &s
->fifo_buffer
[begin
], s
->data_count
);
523 s
->sdmasysad
+= s
->data_count
- begin
;
524 if (s
->data_count
== block_size
) {
525 for (n
= 0; n
< block_size
; n
++) {
526 sd_write_data(s
->card
, s
->fifo_buffer
[n
]);
529 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
533 if (page_aligned
&& boundary_count
== 0) {
539 if (s
->blkcnt
== 0) {
540 SDHCI_GET_CLASS(s
)->end_data_transfer(s
);
542 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
543 s
->norintsts
|= SDHC_NIS_DMA
;
549 /* single block SDMA transfer */
551 static void sdhci_sdma_transfer_single_block(SDHCIState
*s
)
554 uint32_t datacnt
= s
->blksize
& 0x0fff;
556 if (s
->trnmod
& SDHC_TRNS_READ
) {
557 for (n
= 0; n
< datacnt
; n
++) {
558 s
->fifo_buffer
[n
] = sd_read_data(s
->card
);
560 dma_memory_write(&dma_context_memory
, s
->sdmasysad
, s
->fifo_buffer
,
563 dma_memory_read(&dma_context_memory
, s
->sdmasysad
, s
->fifo_buffer
,
565 for (n
= 0; n
< datacnt
; n
++) {
566 sd_write_data(s
->card
, s
->fifo_buffer
[n
]);
570 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
574 SDHCI_GET_CLASS(s
)->end_data_transfer(s
);
577 typedef struct ADMADescr
{
584 static void get_adma_description(SDHCIState
*s
, ADMADescr
*dscr
)
588 hwaddr entry_addr
= (hwaddr
)s
->admasysaddr
;
589 switch (SDHC_DMA_TYPE(s
->hostctl
)) {
590 case SDHC_CTRL_ADMA2_32
:
591 dma_memory_read(&dma_context_memory
, entry_addr
, (uint8_t *)&adma2
,
593 adma2
= le64_to_cpu(adma2
);
594 /* The spec does not specify endianness of descriptor table.
595 * We currently assume that it is LE.
597 dscr
->addr
= (hwaddr
)extract64(adma2
, 32, 32) & ~0x3ull
;
598 dscr
->length
= (uint16_t)extract64(adma2
, 16, 16);
599 dscr
->attr
= (uint8_t)extract64(adma2
, 0, 7);
602 case SDHC_CTRL_ADMA1_32
:
603 dma_memory_read(&dma_context_memory
, entry_addr
, (uint8_t *)&adma1
,
605 adma1
= le32_to_cpu(adma1
);
606 dscr
->addr
= (hwaddr
)(adma1
& 0xFFFFF000);
607 dscr
->attr
= (uint8_t)extract32(adma1
, 0, 7);
609 if ((dscr
->attr
& SDHC_ADMA_ATTR_ACT_MASK
) == SDHC_ADMA_ATTR_SET_LEN
) {
610 dscr
->length
= (uint16_t)extract32(adma1
, 12, 16);
615 case SDHC_CTRL_ADMA2_64
:
616 dma_memory_read(&dma_context_memory
, entry_addr
,
617 (uint8_t *)(&dscr
->attr
), 1);
618 dma_memory_read(&dma_context_memory
, entry_addr
+ 2,
619 (uint8_t *)(&dscr
->length
), 2);
620 dscr
->length
= le16_to_cpu(dscr
->length
);
621 dma_memory_read(&dma_context_memory
, entry_addr
+ 4,
622 (uint8_t *)(&dscr
->addr
), 8);
623 dscr
->attr
= le64_to_cpu(dscr
->attr
);
624 dscr
->attr
&= 0xfffffff8;
630 /* Advanced DMA data transfer */
632 static void sdhci_do_adma(SDHCIState
*s
)
634 unsigned int n
, begin
, length
;
635 const uint16_t block_size
= s
->blksize
& 0x0fff;
639 for (i
= 0; i
< SDHC_ADMA_DESCS_PER_DELAY
; ++i
) {
640 s
->admaerr
&= ~SDHC_ADMAERR_LENGTH_MISMATCH
;
642 get_adma_description(s
, &dscr
);
643 DPRINT_L2("ADMA loop: addr=" TARGET_FMT_plx
", len=%d, attr=%x\n",
644 dscr
.addr
, dscr
.length
, dscr
.attr
);
646 if ((dscr
.attr
& SDHC_ADMA_ATTR_VALID
) == 0) {
647 /* Indicate that error occurred in ST_FDS state */
648 s
->admaerr
&= ~SDHC_ADMAERR_STATE_MASK
;
649 s
->admaerr
|= SDHC_ADMAERR_STATE_ST_FDS
;
651 /* Generate ADMA error interrupt */
652 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
653 s
->errintsts
|= SDHC_EIS_ADMAERR
;
654 s
->norintsts
|= SDHC_NIS_ERR
;
661 length
= dscr
.length
? dscr
.length
: 65536;
663 switch (dscr
.attr
& SDHC_ADMA_ATTR_ACT_MASK
) {
664 case SDHC_ADMA_ATTR_ACT_TRAN
: /* data transfer */
666 if (s
->trnmod
& SDHC_TRNS_READ
) {
668 if (s
->data_count
== 0) {
669 for (n
= 0; n
< block_size
; n
++) {
670 s
->fifo_buffer
[n
] = sd_read_data(s
->card
);
673 begin
= s
->data_count
;
674 if ((length
+ begin
) < block_size
) {
675 s
->data_count
= length
+ begin
;
678 s
->data_count
= block_size
;
679 length
-= block_size
- begin
;
681 dma_memory_write(&dma_context_memory
, dscr
.addr
,
682 &s
->fifo_buffer
[begin
],
683 s
->data_count
- begin
);
684 dscr
.addr
+= s
->data_count
- begin
;
685 if (s
->data_count
== block_size
) {
687 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
689 if (s
->blkcnt
== 0) {
697 begin
= s
->data_count
;
698 if ((length
+ begin
) < block_size
) {
699 s
->data_count
= length
+ begin
;
702 s
->data_count
= block_size
;
703 length
-= block_size
- begin
;
705 dma_memory_read(&dma_context_memory
, dscr
.addr
,
706 &s
->fifo_buffer
[begin
], s
->data_count
);
707 dscr
.addr
+= s
->data_count
- begin
;
708 if (s
->data_count
== block_size
) {
709 for (n
= 0; n
< block_size
; n
++) {
710 sd_write_data(s
->card
, s
->fifo_buffer
[n
]);
713 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
715 if (s
->blkcnt
== 0) {
722 s
->admasysaddr
+= dscr
.incr
;
724 case SDHC_ADMA_ATTR_ACT_LINK
: /* link to next descriptor table */
725 s
->admasysaddr
= dscr
.addr
;
726 DPRINT_L1("ADMA link: admasysaddr=0x%lx\n", s
->admasysaddr
);
729 s
->admasysaddr
+= dscr
.incr
;
733 /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
734 if (((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
735 (s
->blkcnt
== 0)) || (dscr
.attr
& SDHC_ADMA_ATTR_END
)) {
736 DPRINT_L2("ADMA transfer completed\n");
737 if (length
|| ((dscr
.attr
& SDHC_ADMA_ATTR_END
) &&
738 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
740 ERRPRINT("SD/MMC host ADMA length mismatch\n");
741 s
->admaerr
|= SDHC_ADMAERR_LENGTH_MISMATCH
|
742 SDHC_ADMAERR_STATE_ST_TFR
;
743 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
744 ERRPRINT("Set ADMA error flag\n");
745 s
->errintsts
|= SDHC_EIS_ADMAERR
;
746 s
->norintsts
|= SDHC_NIS_ERR
;
751 SDHCI_GET_CLASS(s
)->end_data_transfer(s
);
755 if (dscr
.attr
& SDHC_ADMA_ATTR_INT
) {
756 DPRINT_L1("ADMA interrupt: admasysaddr=0x%lx\n", s
->admasysaddr
);
757 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
758 s
->norintsts
|= SDHC_NIS_DMA
;
766 /* we have unfinished bussiness - reschedule to continue ADMA */
767 qemu_mod_timer(s
->transfer_timer
,
768 qemu_get_clock_ns(vm_clock
) + SDHC_TRANSFER_DELAY
);
771 /* Perform data transfer according to controller configuration */
773 static void sdhci_data_transfer(SDHCIState
*s
)
775 SDHCIClass
*k
= SDHCI_GET_CLASS(s
);
778 if (s
->trnmod
& SDHC_TRNS_DMA
) {
779 switch (SDHC_DMA_TYPE(s
->hostctl
)) {
781 if ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
782 (!(s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) || s
->blkcnt
== 0)) {
786 if ((s
->blkcnt
== 1) || !(s
->trnmod
& SDHC_TRNS_MULTI
)) {
787 k
->do_sdma_single(s
);
793 case SDHC_CTRL_ADMA1_32
:
794 if (!(s
->capareg
& SDHC_CAN_DO_ADMA1
)) {
795 ERRPRINT("ADMA1 not supported\n");
801 case SDHC_CTRL_ADMA2_32
:
802 if (!(s
->capareg
& SDHC_CAN_DO_ADMA2
)) {
803 ERRPRINT("ADMA2 not supported\n");
809 case SDHC_CTRL_ADMA2_64
:
810 if (!(s
->capareg
& SDHC_CAN_DO_ADMA2
) ||
811 !(s
->capareg
& SDHC_64_BIT_BUS_SUPPORT
)) {
812 ERRPRINT("64 bit ADMA not supported\n");
819 ERRPRINT("Unsupported DMA type\n");
823 if ((s
->trnmod
& SDHC_TRNS_READ
) && sd_data_ready(s
->card
)) {
824 s
->prnsts
|= SDHC_DOING_READ
| SDHC_DATA_INHIBIT
|
825 SDHC_DAT_LINE_ACTIVE
;
826 SDHCI_GET_CLASS(s
)->read_block_from_card(s
);
828 s
->prnsts
|= SDHC_DOING_WRITE
| SDHC_DAT_LINE_ACTIVE
|
829 SDHC_SPACE_AVAILABLE
| SDHC_DATA_INHIBIT
;
830 SDHCI_GET_CLASS(s
)->write_block_to_card(s
);
835 static bool sdhci_can_issue_command(SDHCIState
*s
)
837 if (!SDHC_CLOCK_IS_ON(s
->clkcon
) || !(s
->pwrcon
& SDHC_POWER_ON
) ||
838 (((s
->prnsts
& SDHC_DATA_INHIBIT
) || s
->stopped_state
) &&
839 ((s
->cmdreg
& SDHC_CMD_DATA_PRESENT
) ||
840 ((s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
&&
841 !(SDHC_COMMAND_TYPE(s
->cmdreg
) == SDHC_CMD_ABORT
))))) {
848 /* The Buffer Data Port register must be accessed in sequential and
849 * continuous manner */
851 sdhci_buff_access_is_sequential(SDHCIState
*s
, unsigned byte_num
)
853 if ((s
->data_count
& 0x3) != byte_num
) {
854 ERRPRINT("Non-sequential access to Buffer Data Port register"
861 static uint32_t sdhci_read(SDHCIState
*s
, unsigned int offset
, unsigned size
)
865 switch (offset
& ~0x3) {
870 ret
= s
->blksize
| (s
->blkcnt
<< 16);
876 ret
= s
->trnmod
| (s
->cmdreg
<< 16);
878 case SDHC_RSPREG0
... SDHC_RSPREG3
:
879 ret
= s
->rspreg
[((offset
& ~0x3) - SDHC_RSPREG0
) >> 2];
882 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
883 ret
= SDHCI_GET_CLASS(s
)->bdata_read(s
, size
);
884 DPRINT_L2("read %ub: addr[0x%04x] -> %u\n", size
, offset
, ret
);
892 ret
= s
->hostctl
| (s
->pwrcon
<< 8) | (s
->blkgap
<< 16) |
896 ret
= s
->clkcon
| (s
->timeoutcon
<< 16);
899 ret
= s
->norintsts
| (s
->errintsts
<< 16);
901 case SDHC_NORINTSTSEN
:
902 ret
= s
->norintstsen
| (s
->errintstsen
<< 16);
904 case SDHC_NORINTSIGEN
:
905 ret
= s
->norintsigen
| (s
->errintsigen
<< 16);
907 case SDHC_ACMD12ERRSTS
:
908 ret
= s
->acmd12errsts
;
919 case SDHC_ADMASYSADDR
:
920 ret
= (uint32_t)s
->admasysaddr
;
922 case SDHC_ADMASYSADDR
+ 4:
923 ret
= (uint32_t)(s
->admasysaddr
>> 32);
925 case SDHC_SLOT_INT_STATUS
:
926 ret
= (SD_HOST_SPECv2_VERS
<< 16) | sdhci_slotint(s
);
929 ERRPRINT("bad %ub read: addr[0x%04x]\n", size
, offset
);
933 ret
>>= (offset
& 0x3) * 8;
934 ret
&= (1ULL << (size
* 8)) - 1;
935 DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size
, offset
, ret
, ret
);
939 static inline void sdhci_blkgap_write(SDHCIState
*s
, uint8_t value
)
941 if ((value
& SDHC_STOP_AT_GAP_REQ
) && (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
)) {
944 s
->blkgap
= value
& SDHC_STOP_AT_GAP_REQ
;
946 if ((value
& SDHC_CONTINUE_REQ
) && s
->stopped_state
&&
947 (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
) == 0) {
948 if (s
->stopped_state
== sdhc_gap_read
) {
949 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_READ
;
950 SDHCI_GET_CLASS(s
)->read_block_from_card(s
);
952 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_WRITE
;
953 SDHCI_GET_CLASS(s
)->write_block_to_card(s
);
955 s
->stopped_state
= sdhc_not_stopped
;
956 } else if (!s
->stopped_state
&& (value
& SDHC_STOP_AT_GAP_REQ
)) {
957 if (s
->prnsts
& SDHC_DOING_READ
) {
958 s
->stopped_state
= sdhc_gap_read
;
959 } else if (s
->prnsts
& SDHC_DOING_WRITE
) {
960 s
->stopped_state
= sdhc_gap_write
;
965 static inline void sdhci_reset_write(SDHCIState
*s
, uint8_t value
)
969 DEVICE_GET_CLASS(s
)->reset(DEVICE(s
));
972 s
->prnsts
&= ~SDHC_CMD_INHIBIT
;
973 s
->norintsts
&= ~SDHC_NIS_CMDCMP
;
975 case SDHC_RESET_DATA
:
977 s
->prnsts
&= ~(SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
|
978 SDHC_DOING_READ
| SDHC_DOING_WRITE
|
979 SDHC_DATA_INHIBIT
| SDHC_DAT_LINE_ACTIVE
);
980 s
->blkgap
&= ~(SDHC_STOP_AT_GAP_REQ
| SDHC_CONTINUE_REQ
);
981 s
->stopped_state
= sdhc_not_stopped
;
982 s
->norintsts
&= ~(SDHC_NIS_WBUFRDY
| SDHC_NIS_RBUFRDY
|
983 SDHC_NIS_DMA
| SDHC_NIS_TRSCMP
| SDHC_NIS_BLKGAP
);
989 sdhci_write(SDHCIState
*s
, unsigned int offset
, uint32_t value
, unsigned size
)
991 unsigned shift
= 8 * (offset
& 0x3);
992 uint32_t mask
= ~(((1ULL << (size
* 8)) - 1) << shift
);
995 switch (offset
& ~0x3) {
997 s
->sdmasysad
= (s
->sdmasysad
& mask
) | value
;
998 MASKED_WRITE(s
->sdmasysad
, mask
, value
);
999 /* Writing to last byte of sdmasysad might trigger transfer */
1000 if (!(mask
& 0xFF000000) && TRANSFERRING_DATA(s
->prnsts
) && s
->blkcnt
&&
1001 s
->blksize
&& SDHC_DMA_TYPE(s
->hostctl
) == SDHC_CTRL_SDMA
) {
1002 SDHCI_GET_CLASS(s
)->do_sdma_multi(s
);
1006 if (!TRANSFERRING_DATA(s
->prnsts
)) {
1007 MASKED_WRITE(s
->blksize
, mask
, value
);
1008 MASKED_WRITE(s
->blkcnt
, mask
>> 16, value
>> 16);
1012 MASKED_WRITE(s
->argument
, mask
, value
);
1015 /* DMA can be enabled only if it is supported as indicated by
1016 * capabilities register */
1017 if (!(s
->capareg
& SDHC_CAN_DO_DMA
)) {
1018 value
&= ~SDHC_TRNS_DMA
;
1020 MASKED_WRITE(s
->trnmod
, mask
, value
);
1021 MASKED_WRITE(s
->cmdreg
, mask
>> 16, value
>> 16);
1023 /* Writing to the upper byte of CMDREG triggers SD command generation */
1024 if ((mask
& 0xFF000000) || !SDHCI_GET_CLASS(s
)->can_issue_command(s
)) {
1028 SDHCI_GET_CLASS(s
)->send_command(s
);
1031 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
1032 SDHCI_GET_CLASS(s
)->bdata_write(s
, value
>> shift
, size
);
1036 if (!(mask
& 0xFF0000)) {
1037 sdhci_blkgap_write(s
, value
>> 16);
1039 MASKED_WRITE(s
->hostctl
, mask
, value
);
1040 MASKED_WRITE(s
->pwrcon
, mask
>> 8, value
>> 8);
1041 MASKED_WRITE(s
->wakcon
, mask
>> 24, value
>> 24);
1042 if (!(s
->prnsts
& SDHC_CARD_PRESENT
) || ((s
->pwrcon
>> 1) & 0x7) < 5 ||
1043 !(s
->capareg
& (1 << (31 - ((s
->pwrcon
>> 1) & 0x7))))) {
1044 s
->pwrcon
&= ~SDHC_POWER_ON
;
1048 if (!(mask
& 0xFF000000)) {
1049 sdhci_reset_write(s
, value
>> 24);
1051 MASKED_WRITE(s
->clkcon
, mask
, value
);
1052 MASKED_WRITE(s
->timeoutcon
, mask
>> 16, value
>> 16);
1053 if (s
->clkcon
& SDHC_CLOCK_INT_EN
) {
1054 s
->clkcon
|= SDHC_CLOCK_INT_STABLE
;
1056 s
->clkcon
&= ~SDHC_CLOCK_INT_STABLE
;
1059 case SDHC_NORINTSTS
:
1060 if (s
->norintstsen
& SDHC_NISEN_CARDINT
) {
1061 value
&= ~SDHC_NIS_CARDINT
;
1063 s
->norintsts
&= mask
| ~value
;
1064 s
->errintsts
&= (mask
>> 16) | ~(value
>> 16);
1066 s
->norintsts
|= SDHC_NIS_ERR
;
1068 s
->norintsts
&= ~SDHC_NIS_ERR
;
1070 sdhci_update_irq(s
);
1072 case SDHC_NORINTSTSEN
:
1073 MASKED_WRITE(s
->norintstsen
, mask
, value
);
1074 MASKED_WRITE(s
->errintstsen
, mask
>> 16, value
>> 16);
1075 s
->norintsts
&= s
->norintstsen
;
1076 s
->errintsts
&= s
->errintstsen
;
1078 s
->norintsts
|= SDHC_NIS_ERR
;
1080 s
->norintsts
&= ~SDHC_NIS_ERR
;
1082 sdhci_update_irq(s
);
1084 case SDHC_NORINTSIGEN
:
1085 MASKED_WRITE(s
->norintsigen
, mask
, value
);
1086 MASKED_WRITE(s
->errintsigen
, mask
>> 16, value
>> 16);
1087 sdhci_update_irq(s
);
1090 MASKED_WRITE(s
->admaerr
, mask
, value
);
1092 case SDHC_ADMASYSADDR
:
1093 s
->admasysaddr
= (s
->admasysaddr
& (0xFFFFFFFF00000000ULL
|
1094 (uint64_t)mask
)) | (uint64_t)value
;
1096 case SDHC_ADMASYSADDR
+ 4:
1097 s
->admasysaddr
= (s
->admasysaddr
& (0x00000000FFFFFFFFULL
|
1098 ((uint64_t)mask
<< 32))) | ((uint64_t)value
<< 32);
1101 s
->acmd12errsts
|= value
;
1102 s
->errintsts
|= (value
>> 16) & s
->errintstsen
;
1103 if (s
->acmd12errsts
) {
1104 s
->errintsts
|= SDHC_EIS_CMD12ERR
;
1107 s
->norintsts
|= SDHC_NIS_ERR
;
1109 sdhci_update_irq(s
);
1112 ERRPRINT("bad %ub write offset: addr[0x%04x] <- %u(0x%x)\n",
1113 size
, offset
, value
>> shift
, value
>> shift
);
1116 DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n",
1117 size
, offset
, value
>> shift
, value
>> shift
);
1121 sdhci_readfn(void *opaque
, hwaddr offset
, unsigned size
)
1123 SDHCIState
*s
= (SDHCIState
*)opaque
;
1125 return SDHCI_GET_CLASS(s
)->mem_read(s
, offset
, size
);
1129 sdhci_writefn(void *opaque
, hwaddr off
, uint64_t val
, unsigned sz
)
1131 SDHCIState
*s
= (SDHCIState
*)opaque
;
1133 SDHCI_GET_CLASS(s
)->mem_write(s
, off
, val
, sz
);
1136 static const MemoryRegionOps sdhci_mmio_ops
= {
1137 .read
= sdhci_readfn
,
1138 .write
= sdhci_writefn
,
1140 .min_access_size
= 1,
1141 .max_access_size
= 4,
1144 .endianness
= DEVICE_LITTLE_ENDIAN
,
1147 static inline unsigned int sdhci_get_fifolen(SDHCIState
*s
)
1149 switch (SDHC_CAPAB_BLOCKSIZE(s
->capareg
)) {
1157 hw_error("SDHC: unsupported value for maximum block size\n");
1162 static void sdhci_initfn(Object
*obj
)
1164 SDHCIState
*s
= SDHCI(obj
);
1167 di
= drive_get_next(IF_SD
);
1168 s
->card
= sd_init(di
? di
->bdrv
: NULL
, 0);
1169 s
->eject_cb
= qemu_allocate_irqs(sdhci_insert_eject_cb
, s
, 1)[0];
1170 s
->ro_cb
= qemu_allocate_irqs(sdhci_card_readonly_cb
, s
, 1)[0];
1171 sd_set_cb(s
->card
, s
->ro_cb
, s
->eject_cb
);
1173 s
->insert_timer
= qemu_new_timer_ns(vm_clock
, sdhci_raise_insertion_irq
, s
);
1174 s
->transfer_timer
= qemu_new_timer_ns(vm_clock
, sdhci_do_data_transfer
, s
);
1177 static void sdhci_uninitfn(Object
*obj
)
1179 SDHCIState
*s
= SDHCI(obj
);
1181 qemu_del_timer(s
->insert_timer
);
1182 qemu_free_timer(s
->insert_timer
);
1183 qemu_del_timer(s
->transfer_timer
);
1184 qemu_free_timer(s
->transfer_timer
);
1185 qemu_free_irqs(&s
->eject_cb
);
1186 qemu_free_irqs(&s
->ro_cb
);
1188 if (s
->fifo_buffer
) {
1189 g_free(s
->fifo_buffer
);
1190 s
->fifo_buffer
= NULL
;
1194 const VMStateDescription sdhci_vmstate
= {
1197 .minimum_version_id
= 1,
1198 .fields
= (VMStateField
[]) {
1199 VMSTATE_UINT32(sdmasysad
, SDHCIState
),
1200 VMSTATE_UINT16(blksize
, SDHCIState
),
1201 VMSTATE_UINT16(blkcnt
, SDHCIState
),
1202 VMSTATE_UINT32(argument
, SDHCIState
),
1203 VMSTATE_UINT16(trnmod
, SDHCIState
),
1204 VMSTATE_UINT16(cmdreg
, SDHCIState
),
1205 VMSTATE_UINT32_ARRAY(rspreg
, SDHCIState
, 4),
1206 VMSTATE_UINT32(prnsts
, SDHCIState
),
1207 VMSTATE_UINT8(hostctl
, SDHCIState
),
1208 VMSTATE_UINT8(pwrcon
, SDHCIState
),
1209 VMSTATE_UINT8(blkgap
, SDHCIState
),
1210 VMSTATE_UINT8(wakcon
, SDHCIState
),
1211 VMSTATE_UINT16(clkcon
, SDHCIState
),
1212 VMSTATE_UINT8(timeoutcon
, SDHCIState
),
1213 VMSTATE_UINT8(admaerr
, SDHCIState
),
1214 VMSTATE_UINT16(norintsts
, SDHCIState
),
1215 VMSTATE_UINT16(errintsts
, SDHCIState
),
1216 VMSTATE_UINT16(norintstsen
, SDHCIState
),
1217 VMSTATE_UINT16(errintstsen
, SDHCIState
),
1218 VMSTATE_UINT16(norintsigen
, SDHCIState
),
1219 VMSTATE_UINT16(errintsigen
, SDHCIState
),
1220 VMSTATE_UINT16(acmd12errsts
, SDHCIState
),
1221 VMSTATE_UINT16(data_count
, SDHCIState
),
1222 VMSTATE_UINT64(admasysaddr
, SDHCIState
),
1223 VMSTATE_UINT8(stopped_state
, SDHCIState
),
1224 VMSTATE_VBUFFER_UINT32(fifo_buffer
, SDHCIState
, 1, NULL
, 0, buf_maxsz
),
1225 VMSTATE_TIMER(insert_timer
, SDHCIState
),
1226 VMSTATE_TIMER(transfer_timer
, SDHCIState
),
1227 VMSTATE_END_OF_LIST()
1231 /* Capabilities registers provide information on supported features of this
1232 * specific host controller implementation */
1233 static Property sdhci_properties
[] = {
1234 DEFINE_PROP_HEX32("capareg", SDHCIState
, capareg
,
1235 SDHC_CAPAB_REG_DEFAULT
),
1236 DEFINE_PROP_HEX32("maxcurr", SDHCIState
, maxcurr
, 0),
1237 DEFINE_PROP_END_OF_LIST(),
1240 static void sdhci_realize(DeviceState
*dev
, Error
** errp
)
1242 SDHCIState
*s
= SDHCI(dev
);
1243 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1245 s
->buf_maxsz
= sdhci_get_fifolen(s
);
1246 s
->fifo_buffer
= g_malloc0(s
->buf_maxsz
);
1247 sysbus_init_irq(sbd
, &s
->irq
);
1248 memory_region_init_io(&s
->iomem
, &sdhci_mmio_ops
, s
, "sdhci",
1249 SDHC_REGISTERS_MAP_SIZE
);
1250 sysbus_init_mmio(sbd
, &s
->iomem
);
1253 static void sdhci_generic_reset(DeviceState
*ds
)
1255 SDHCIState
*s
= SDHCI(ds
);
1256 SDHCI_GET_CLASS(s
)->reset(s
);
1259 static void sdhci_class_init(ObjectClass
*klass
, void *data
)
1261 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1262 SDHCIClass
*k
= SDHCI_CLASS(klass
);
1264 dc
->vmsd
= &sdhci_vmstate
;
1265 dc
->props
= sdhci_properties
;
1266 dc
->reset
= sdhci_generic_reset
;
1267 dc
->realize
= sdhci_realize
;
1269 k
->reset
= sdhci_reset
;
1270 k
->mem_read
= sdhci_read
;
1271 k
->mem_write
= sdhci_write
;
1272 k
->send_command
= sdhci_send_command
;
1273 k
->can_issue_command
= sdhci_can_issue_command
;
1274 k
->data_transfer
= sdhci_data_transfer
;
1275 k
->end_data_transfer
= sdhci_end_transfer
;
1276 k
->do_sdma_single
= sdhci_sdma_transfer_single_block
;
1277 k
->do_sdma_multi
= sdhci_sdma_transfer_multi_blocks
;
1278 k
->do_adma
= sdhci_do_adma
;
1279 k
->read_block_from_card
= sdhci_read_block_from_card
;
1280 k
->write_block_to_card
= sdhci_write_block_to_card
;
1281 k
->bdata_read
= sdhci_read_dataport
;
1282 k
->bdata_write
= sdhci_write_dataport
;
1285 static const TypeInfo sdhci_type_info
= {
1287 .parent
= TYPE_SYS_BUS_DEVICE
,
1288 .instance_size
= sizeof(SDHCIState
),
1289 .instance_init
= sdhci_initfn
,
1290 .instance_finalize
= sdhci_uninitfn
,
1291 .class_init
= sdhci_class_init
,
1292 .class_size
= sizeof(SDHCIClass
)
1295 static void sdhci_register_types(void)
1297 type_register_static(&sdhci_type_info
);
1300 type_init(sdhci_register_types
)