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/>.
27 #include "sysemu/block-backend.h"
28 #include "sysemu/blockdev.h"
29 #include "sysemu/dma.h"
30 #include "qemu/timer.h"
31 #include "qemu/bitops.h"
32 #include "sdhci-internal.h"
34 /* host controller debug messages */
39 #define DPRINT_L1(fmt, args...) \
42 fprintf(stderr, "QEMU SDHC: " fmt, ## args); \
45 #define DPRINT_L2(fmt, args...) \
47 if (SDHC_DEBUG > 1) { \
48 fprintf(stderr, "QEMU SDHC: " fmt, ## args); \
51 #define ERRPRINT(fmt, args...) \
54 fprintf(stderr, "QEMU SDHC ERROR: " fmt, ## args); \
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 52ul
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 52ul
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 timer_mod(s
->insert_timer
,
138 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 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 timer_mod(s
->insert_timer
,
156 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 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 timer_del(s
->insert_timer
);
190 timer_del(s
->transfer_timer
);
191 /* Set all registers to 0. Capabilities registers are not cleared
192 * and assumed to always preserve their value, given to them during
194 memset(&s
->sdmasysad
, 0, (uintptr_t)&s
->capareg
- (uintptr_t)&s
->sdmasysad
);
196 if (!s
->noeject_quirk
) {
197 sd_set_cb(s
->card
, s
->ro_cb
, s
->eject_cb
);
200 s
->stopped_state
= sdhc_not_stopped
;
203 static void sdhci_data_transfer(void *opaque
);
205 static void sdhci_send_command(SDHCIState
*s
)
208 uint8_t response
[16];
213 request
.cmd
= s
->cmdreg
>> 8;
214 request
.arg
= s
->argument
;
215 DPRINT_L1("sending CMD%u ARG[0x%08x]\n", request
.cmd
, request
.arg
);
216 rlen
= sd_do_command(s
->card
, &request
, response
);
218 if (s
->cmdreg
& SDHC_CMD_RESPONSE
) {
220 s
->rspreg
[0] = (response
[0] << 24) | (response
[1] << 16) |
221 (response
[2] << 8) | response
[3];
222 s
->rspreg
[1] = s
->rspreg
[2] = s
->rspreg
[3] = 0;
223 DPRINT_L1("Response: RSPREG[31..0]=0x%08x\n", s
->rspreg
[0]);
224 } else if (rlen
== 16) {
225 s
->rspreg
[0] = (response
[11] << 24) | (response
[12] << 16) |
226 (response
[13] << 8) | response
[14];
227 s
->rspreg
[1] = (response
[7] << 24) | (response
[8] << 16) |
228 (response
[9] << 8) | response
[10];
229 s
->rspreg
[2] = (response
[3] << 24) | (response
[4] << 16) |
230 (response
[5] << 8) | response
[6];
231 s
->rspreg
[3] = (response
[0] << 16) | (response
[1] << 8) |
233 DPRINT_L1("Response received:\n RSPREG[127..96]=0x%08x, RSPREG[95.."
234 "64]=0x%08x,\n RSPREG[63..32]=0x%08x, RSPREG[31..0]=0x%08x\n",
235 s
->rspreg
[3], s
->rspreg
[2], s
->rspreg
[1], s
->rspreg
[0]);
237 ERRPRINT("Timeout waiting for command response\n");
238 if (s
->errintstsen
& SDHC_EISEN_CMDTIMEOUT
) {
239 s
->errintsts
|= SDHC_EIS_CMDTIMEOUT
;
240 s
->norintsts
|= SDHC_NIS_ERR
;
244 if ((s
->norintstsen
& SDHC_NISEN_TRSCMP
) &&
245 (s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
) {
246 s
->norintsts
|= SDHC_NIS_TRSCMP
;
250 if (s
->norintstsen
& SDHC_NISEN_CMDCMP
) {
251 s
->norintsts
|= SDHC_NIS_CMDCMP
;
256 if (s
->blksize
&& (s
->cmdreg
& SDHC_CMD_DATA_PRESENT
)) {
258 sdhci_data_transfer(s
);
262 static void sdhci_end_transfer(SDHCIState
*s
)
264 /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */
265 if ((s
->trnmod
& SDHC_TRNS_ACMD12
) != 0) {
267 uint8_t response
[16];
271 DPRINT_L1("Automatically issue CMD%d %08x\n", request
.cmd
, request
.arg
);
272 sd_do_command(s
->card
, &request
, response
);
273 /* Auto CMD12 response goes to the upper Response register */
274 s
->rspreg
[3] = (response
[0] << 24) | (response
[1] << 16) |
275 (response
[2] << 8) | response
[3];
278 s
->prnsts
&= ~(SDHC_DOING_READ
| SDHC_DOING_WRITE
|
279 SDHC_DAT_LINE_ACTIVE
| SDHC_DATA_INHIBIT
|
280 SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
);
282 if (s
->norintstsen
& SDHC_NISEN_TRSCMP
) {
283 s
->norintsts
|= SDHC_NIS_TRSCMP
;
290 * Programmed i/o data transfer
293 /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
294 static void sdhci_read_block_from_card(SDHCIState
*s
)
298 if ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
299 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) {
303 for (index
= 0; index
< (s
->blksize
& 0x0fff); index
++) {
304 s
->fifo_buffer
[index
] = sd_read_data(s
->card
);
307 /* New data now available for READ through Buffer Port Register */
308 s
->prnsts
|= SDHC_DATA_AVAILABLE
;
309 if (s
->norintstsen
& SDHC_NISEN_RBUFRDY
) {
310 s
->norintsts
|= SDHC_NIS_RBUFRDY
;
313 /* Clear DAT line active status if that was the last block */
314 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
315 ((s
->trnmod
& SDHC_TRNS_MULTI
) && s
->blkcnt
== 1)) {
316 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
319 /* If stop at block gap request was set and it's not the last block of
320 * data - generate Block Event interrupt */
321 if (s
->stopped_state
== sdhc_gap_read
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
323 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
324 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
325 s
->norintsts
|= SDHC_EIS_BLKGAP
;
332 /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
333 static uint32_t sdhci_read_dataport(SDHCIState
*s
, unsigned size
)
338 /* first check that a valid data exists in host controller input buffer */
339 if ((s
->prnsts
& SDHC_DATA_AVAILABLE
) == 0) {
340 ERRPRINT("Trying to read from empty buffer\n");
344 for (i
= 0; i
< size
; i
++) {
345 value
|= s
->fifo_buffer
[s
->data_count
] << i
* 8;
347 /* check if we've read all valid data (blksize bytes) from buffer */
348 if ((s
->data_count
) >= (s
->blksize
& 0x0fff)) {
349 DPRINT_L2("All %u bytes of data have been read from input buffer\n",
351 s
->prnsts
&= ~SDHC_DATA_AVAILABLE
; /* no more data in a buffer */
352 s
->data_count
= 0; /* next buff read must start at position [0] */
354 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
358 /* if that was the last block of data */
359 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
360 ((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) ||
361 /* stop at gap request */
362 (s
->stopped_state
== sdhc_gap_read
&&
363 !(s
->prnsts
& SDHC_DAT_LINE_ACTIVE
))) {
364 sdhci_end_transfer(s
);
365 } else { /* if there are more data, read next block from card */
366 sdhci_read_block_from_card(s
);
375 /* Write data from host controller FIFO to card */
376 static void sdhci_write_block_to_card(SDHCIState
*s
)
380 if (s
->prnsts
& SDHC_SPACE_AVAILABLE
) {
381 if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
382 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
388 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
389 if (s
->blkcnt
== 0) {
396 for (index
= 0; index
< (s
->blksize
& 0x0fff); index
++) {
397 sd_write_data(s
->card
, s
->fifo_buffer
[index
]);
400 /* Next data can be written through BUFFER DATORT register */
401 s
->prnsts
|= SDHC_SPACE_AVAILABLE
;
403 /* Finish transfer if that was the last block of data */
404 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
405 ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
406 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0))) {
407 sdhci_end_transfer(s
);
408 } else if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
409 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
412 /* Generate Block Gap Event if requested and if not the last block */
413 if (s
->stopped_state
== sdhc_gap_write
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
415 s
->prnsts
&= ~SDHC_DOING_WRITE
;
416 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
417 s
->norintsts
|= SDHC_EIS_BLKGAP
;
419 sdhci_end_transfer(s
);
425 /* Write @size bytes of @value data to host controller @s Buffer Data Port
427 static void sdhci_write_dataport(SDHCIState
*s
, uint32_t value
, unsigned size
)
431 /* Check that there is free space left in a buffer */
432 if (!(s
->prnsts
& SDHC_SPACE_AVAILABLE
)) {
433 ERRPRINT("Can't write to data buffer: buffer full\n");
437 for (i
= 0; i
< size
; i
++) {
438 s
->fifo_buffer
[s
->data_count
] = value
& 0xFF;
441 if (s
->data_count
>= (s
->blksize
& 0x0fff)) {
442 DPRINT_L2("write buffer filled with %u bytes of data\n",
445 s
->prnsts
&= ~SDHC_SPACE_AVAILABLE
;
446 if (s
->prnsts
& SDHC_DOING_WRITE
) {
447 sdhci_write_block_to_card(s
);
454 * Single DMA data transfer
457 /* Multi block SDMA transfer */
458 static void sdhci_sdma_transfer_multi_blocks(SDHCIState
*s
)
460 bool page_aligned
= false;
461 unsigned int n
, begin
;
462 const uint16_t block_size
= s
->blksize
& 0x0fff;
463 uint32_t boundary_chk
= 1 << (((s
->blksize
& 0xf000) >> 12) + 12);
464 uint32_t boundary_count
= boundary_chk
- (s
->sdmasysad
% boundary_chk
);
466 /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
467 * possible stop at page boundary if initial address is not page aligned,
468 * allow them to work properly */
469 if ((s
->sdmasysad
% boundary_chk
) == 0) {
473 if (s
->trnmod
& SDHC_TRNS_READ
) {
474 s
->prnsts
|= SDHC_DOING_READ
| SDHC_DATA_INHIBIT
|
475 SDHC_DAT_LINE_ACTIVE
;
477 if (s
->data_count
== 0) {
478 for (n
= 0; n
< block_size
; n
++) {
479 s
->fifo_buffer
[n
] = sd_read_data(s
->card
);
482 begin
= s
->data_count
;
483 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
484 s
->data_count
= boundary_count
+ begin
;
487 s
->data_count
= block_size
;
488 boundary_count
-= block_size
- begin
;
489 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
493 dma_memory_write(&address_space_memory
, s
->sdmasysad
,
494 &s
->fifo_buffer
[begin
], s
->data_count
- begin
);
495 s
->sdmasysad
+= s
->data_count
- begin
;
496 if (s
->data_count
== block_size
) {
499 if (page_aligned
&& boundary_count
== 0) {
504 s
->prnsts
|= SDHC_DOING_WRITE
| SDHC_DATA_INHIBIT
|
505 SDHC_DAT_LINE_ACTIVE
;
507 begin
= s
->data_count
;
508 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
509 s
->data_count
= boundary_count
+ begin
;
512 s
->data_count
= block_size
;
513 boundary_count
-= block_size
- begin
;
515 dma_memory_read(&address_space_memory
, s
->sdmasysad
,
516 &s
->fifo_buffer
[begin
], s
->data_count
);
517 s
->sdmasysad
+= s
->data_count
- begin
;
518 if (s
->data_count
== block_size
) {
519 for (n
= 0; n
< block_size
; n
++) {
520 sd_write_data(s
->card
, s
->fifo_buffer
[n
]);
523 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
527 if (page_aligned
&& boundary_count
== 0) {
533 if (s
->blkcnt
== 0) {
534 sdhci_end_transfer(s
);
536 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
537 s
->norintsts
|= SDHC_NIS_DMA
;
543 /* single block SDMA transfer */
545 static void sdhci_sdma_transfer_single_block(SDHCIState
*s
)
548 uint32_t datacnt
= s
->blksize
& 0x0fff;
550 if (s
->trnmod
& SDHC_TRNS_READ
) {
551 for (n
= 0; n
< datacnt
; n
++) {
552 s
->fifo_buffer
[n
] = sd_read_data(s
->card
);
554 dma_memory_write(&address_space_memory
, s
->sdmasysad
, s
->fifo_buffer
,
557 dma_memory_read(&address_space_memory
, s
->sdmasysad
, s
->fifo_buffer
,
559 for (n
= 0; n
< datacnt
; n
++) {
560 sd_write_data(s
->card
, s
->fifo_buffer
[n
]);
564 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
568 sdhci_end_transfer(s
);
571 typedef struct ADMADescr
{
578 static void get_adma_description(SDHCIState
*s
, ADMADescr
*dscr
)
582 hwaddr entry_addr
= (hwaddr
)s
->admasysaddr
;
583 switch (SDHC_DMA_TYPE(s
->hostctl
)) {
584 case SDHC_CTRL_ADMA2_32
:
585 dma_memory_read(&address_space_memory
, entry_addr
, (uint8_t *)&adma2
,
587 adma2
= le64_to_cpu(adma2
);
588 /* The spec does not specify endianness of descriptor table.
589 * We currently assume that it is LE.
591 dscr
->addr
= (hwaddr
)extract64(adma2
, 32, 32) & ~0x3ull
;
592 dscr
->length
= (uint16_t)extract64(adma2
, 16, 16);
593 dscr
->attr
= (uint8_t)extract64(adma2
, 0, 7);
596 case SDHC_CTRL_ADMA1_32
:
597 dma_memory_read(&address_space_memory
, entry_addr
, (uint8_t *)&adma1
,
599 adma1
= le32_to_cpu(adma1
);
600 dscr
->addr
= (hwaddr
)(adma1
& 0xFFFFF000);
601 dscr
->attr
= (uint8_t)extract32(adma1
, 0, 7);
603 if ((dscr
->attr
& SDHC_ADMA_ATTR_ACT_MASK
) == SDHC_ADMA_ATTR_SET_LEN
) {
604 dscr
->length
= (uint16_t)extract32(adma1
, 12, 16);
609 case SDHC_CTRL_ADMA2_64
:
610 dma_memory_read(&address_space_memory
, entry_addr
,
611 (uint8_t *)(&dscr
->attr
), 1);
612 dma_memory_read(&address_space_memory
, entry_addr
+ 2,
613 (uint8_t *)(&dscr
->length
), 2);
614 dscr
->length
= le16_to_cpu(dscr
->length
);
615 dma_memory_read(&address_space_memory
, entry_addr
+ 4,
616 (uint8_t *)(&dscr
->addr
), 8);
617 dscr
->attr
= le64_to_cpu(dscr
->attr
);
618 dscr
->attr
&= 0xfffffff8;
624 /* Advanced DMA data transfer */
626 static void sdhci_do_adma(SDHCIState
*s
)
628 unsigned int n
, begin
, length
;
629 const uint16_t block_size
= s
->blksize
& 0x0fff;
633 for (i
= 0; i
< SDHC_ADMA_DESCS_PER_DELAY
; ++i
) {
634 s
->admaerr
&= ~SDHC_ADMAERR_LENGTH_MISMATCH
;
636 get_adma_description(s
, &dscr
);
637 DPRINT_L2("ADMA loop: addr=" TARGET_FMT_plx
", len=%d, attr=%x\n",
638 dscr
.addr
, dscr
.length
, dscr
.attr
);
640 if ((dscr
.attr
& SDHC_ADMA_ATTR_VALID
) == 0) {
641 /* Indicate that error occurred in ST_FDS state */
642 s
->admaerr
&= ~SDHC_ADMAERR_STATE_MASK
;
643 s
->admaerr
|= SDHC_ADMAERR_STATE_ST_FDS
;
645 /* Generate ADMA error interrupt */
646 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
647 s
->errintsts
|= SDHC_EIS_ADMAERR
;
648 s
->norintsts
|= SDHC_NIS_ERR
;
655 length
= dscr
.length
? dscr
.length
: 65536;
657 switch (dscr
.attr
& SDHC_ADMA_ATTR_ACT_MASK
) {
658 case SDHC_ADMA_ATTR_ACT_TRAN
: /* data transfer */
660 if (s
->trnmod
& SDHC_TRNS_READ
) {
662 if (s
->data_count
== 0) {
663 for (n
= 0; n
< block_size
; n
++) {
664 s
->fifo_buffer
[n
] = sd_read_data(s
->card
);
667 begin
= s
->data_count
;
668 if ((length
+ begin
) < block_size
) {
669 s
->data_count
= length
+ begin
;
672 s
->data_count
= block_size
;
673 length
-= block_size
- begin
;
675 dma_memory_write(&address_space_memory
, dscr
.addr
,
676 &s
->fifo_buffer
[begin
],
677 s
->data_count
- begin
);
678 dscr
.addr
+= s
->data_count
- begin
;
679 if (s
->data_count
== block_size
) {
681 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
683 if (s
->blkcnt
== 0) {
691 begin
= s
->data_count
;
692 if ((length
+ begin
) < block_size
) {
693 s
->data_count
= length
+ begin
;
696 s
->data_count
= block_size
;
697 length
-= block_size
- begin
;
699 dma_memory_read(&address_space_memory
, dscr
.addr
,
700 &s
->fifo_buffer
[begin
],
701 s
->data_count
- begin
);
702 dscr
.addr
+= s
->data_count
- begin
;
703 if (s
->data_count
== block_size
) {
704 for (n
= 0; n
< block_size
; n
++) {
705 sd_write_data(s
->card
, s
->fifo_buffer
[n
]);
708 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
710 if (s
->blkcnt
== 0) {
717 s
->admasysaddr
+= dscr
.incr
;
719 case SDHC_ADMA_ATTR_ACT_LINK
: /* link to next descriptor table */
720 s
->admasysaddr
= dscr
.addr
;
721 DPRINT_L1("ADMA link: admasysaddr=0x%" PRIx64
"\n",
725 s
->admasysaddr
+= dscr
.incr
;
729 if (dscr
.attr
& SDHC_ADMA_ATTR_INT
) {
730 DPRINT_L1("ADMA interrupt: admasysaddr=0x%" PRIx64
"\n",
732 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
733 s
->norintsts
|= SDHC_NIS_DMA
;
739 /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
740 if (((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
741 (s
->blkcnt
== 0)) || (dscr
.attr
& SDHC_ADMA_ATTR_END
)) {
742 DPRINT_L2("ADMA transfer completed\n");
743 if (length
|| ((dscr
.attr
& SDHC_ADMA_ATTR_END
) &&
744 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
746 ERRPRINT("SD/MMC host ADMA length mismatch\n");
747 s
->admaerr
|= SDHC_ADMAERR_LENGTH_MISMATCH
|
748 SDHC_ADMAERR_STATE_ST_TFR
;
749 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
750 ERRPRINT("Set ADMA error flag\n");
751 s
->errintsts
|= SDHC_EIS_ADMAERR
;
752 s
->norintsts
|= SDHC_NIS_ERR
;
757 sdhci_end_transfer(s
);
763 /* we have unfinished business - reschedule to continue ADMA */
764 timer_mod(s
->transfer_timer
,
765 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + SDHC_TRANSFER_DELAY
);
768 /* Perform data transfer according to controller configuration */
770 static void sdhci_data_transfer(void *opaque
)
772 SDHCIState
*s
= (SDHCIState
*)opaque
;
774 if (s
->trnmod
& SDHC_TRNS_DMA
) {
775 switch (SDHC_DMA_TYPE(s
->hostctl
)) {
777 if ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
778 (!(s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) || s
->blkcnt
== 0)) {
782 if ((s
->blkcnt
== 1) || !(s
->trnmod
& SDHC_TRNS_MULTI
)) {
783 sdhci_sdma_transfer_single_block(s
);
785 sdhci_sdma_transfer_multi_blocks(s
);
789 case SDHC_CTRL_ADMA1_32
:
790 if (!(s
->capareg
& SDHC_CAN_DO_ADMA1
)) {
791 ERRPRINT("ADMA1 not supported\n");
797 case SDHC_CTRL_ADMA2_32
:
798 if (!(s
->capareg
& SDHC_CAN_DO_ADMA2
)) {
799 ERRPRINT("ADMA2 not supported\n");
805 case SDHC_CTRL_ADMA2_64
:
806 if (!(s
->capareg
& SDHC_CAN_DO_ADMA2
) ||
807 !(s
->capareg
& SDHC_64_BIT_BUS_SUPPORT
)) {
808 ERRPRINT("64 bit ADMA not supported\n");
815 ERRPRINT("Unsupported DMA type\n");
819 if ((s
->trnmod
& SDHC_TRNS_READ
) && sd_data_ready(s
->card
)) {
820 s
->prnsts
|= SDHC_DOING_READ
| SDHC_DATA_INHIBIT
|
821 SDHC_DAT_LINE_ACTIVE
;
822 sdhci_read_block_from_card(s
);
824 s
->prnsts
|= SDHC_DOING_WRITE
| SDHC_DAT_LINE_ACTIVE
|
825 SDHC_SPACE_AVAILABLE
| SDHC_DATA_INHIBIT
;
826 sdhci_write_block_to_card(s
);
831 static bool sdhci_can_issue_command(SDHCIState
*s
)
833 if (!SDHC_CLOCK_IS_ON(s
->clkcon
) ||
834 (((s
->prnsts
& SDHC_DATA_INHIBIT
) || s
->stopped_state
) &&
835 ((s
->cmdreg
& SDHC_CMD_DATA_PRESENT
) ||
836 ((s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
&&
837 !(SDHC_COMMAND_TYPE(s
->cmdreg
) == SDHC_CMD_ABORT
))))) {
844 /* The Buffer Data Port register must be accessed in sequential and
845 * continuous manner */
847 sdhci_buff_access_is_sequential(SDHCIState
*s
, unsigned byte_num
)
849 if ((s
->data_count
& 0x3) != byte_num
) {
850 ERRPRINT("Non-sequential access to Buffer Data Port register"
857 static uint64_t sdhci_read(void *opaque
, hwaddr offset
, unsigned size
)
859 SDHCIState
*s
= (SDHCIState
*)opaque
;
862 switch (offset
& ~0x3) {
867 ret
= s
->blksize
| (s
->blkcnt
<< 16);
873 ret
= s
->trnmod
| (s
->cmdreg
<< 16);
875 case SDHC_RSPREG0
... SDHC_RSPREG3
:
876 ret
= s
->rspreg
[((offset
& ~0x3) - SDHC_RSPREG0
) >> 2];
879 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
880 ret
= sdhci_read_dataport(s
, size
);
881 DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size
, (int)offset
,
890 ret
= s
->hostctl
| (s
->pwrcon
<< 8) | (s
->blkgap
<< 16) |
894 ret
= s
->clkcon
| (s
->timeoutcon
<< 16);
897 ret
= s
->norintsts
| (s
->errintsts
<< 16);
899 case SDHC_NORINTSTSEN
:
900 ret
= s
->norintstsen
| (s
->errintstsen
<< 16);
902 case SDHC_NORINTSIGEN
:
903 ret
= s
->norintsigen
| (s
->errintsigen
<< 16);
905 case SDHC_ACMD12ERRSTS
:
906 ret
= s
->acmd12errsts
;
917 case SDHC_ADMASYSADDR
:
918 ret
= (uint32_t)s
->admasysaddr
;
920 case SDHC_ADMASYSADDR
+ 4:
921 ret
= (uint32_t)(s
->admasysaddr
>> 32);
923 case SDHC_SLOT_INT_STATUS
:
924 ret
= (SD_HOST_SPECv2_VERS
<< 16) | sdhci_slotint(s
);
927 ERRPRINT("bad %ub read: addr[0x%04x]\n", size
, (int)offset
);
931 ret
>>= (offset
& 0x3) * 8;
932 ret
&= (1ULL << (size
* 8)) - 1;
933 DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size
, (int)offset
, ret
, ret
);
937 static inline void sdhci_blkgap_write(SDHCIState
*s
, uint8_t value
)
939 if ((value
& SDHC_STOP_AT_GAP_REQ
) && (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
)) {
942 s
->blkgap
= value
& SDHC_STOP_AT_GAP_REQ
;
944 if ((value
& SDHC_CONTINUE_REQ
) && s
->stopped_state
&&
945 (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
) == 0) {
946 if (s
->stopped_state
== sdhc_gap_read
) {
947 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_READ
;
948 sdhci_read_block_from_card(s
);
950 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_WRITE
;
951 sdhci_write_block_to_card(s
);
953 s
->stopped_state
= sdhc_not_stopped
;
954 } else if (!s
->stopped_state
&& (value
& SDHC_STOP_AT_GAP_REQ
)) {
955 if (s
->prnsts
& SDHC_DOING_READ
) {
956 s
->stopped_state
= sdhc_gap_read
;
957 } else if (s
->prnsts
& SDHC_DOING_WRITE
) {
958 s
->stopped_state
= sdhc_gap_write
;
963 static inline void sdhci_reset_write(SDHCIState
*s
, uint8_t value
)
970 s
->prnsts
&= ~SDHC_CMD_INHIBIT
;
971 s
->norintsts
&= ~SDHC_NIS_CMDCMP
;
973 case SDHC_RESET_DATA
:
975 s
->prnsts
&= ~(SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
|
976 SDHC_DOING_READ
| SDHC_DOING_WRITE
|
977 SDHC_DATA_INHIBIT
| SDHC_DAT_LINE_ACTIVE
);
978 s
->blkgap
&= ~(SDHC_STOP_AT_GAP_REQ
| SDHC_CONTINUE_REQ
);
979 s
->stopped_state
= sdhc_not_stopped
;
980 s
->norintsts
&= ~(SDHC_NIS_WBUFRDY
| SDHC_NIS_RBUFRDY
|
981 SDHC_NIS_DMA
| SDHC_NIS_TRSCMP
| SDHC_NIS_BLKGAP
);
987 sdhci_write(void *opaque
, hwaddr offset
, uint64_t val
, unsigned size
)
989 SDHCIState
*s
= (SDHCIState
*)opaque
;
990 unsigned shift
= 8 * (offset
& 0x3);
991 uint32_t mask
= ~(((1ULL << (size
* 8)) - 1) << shift
);
992 uint32_t value
= val
;
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_sdma_transfer_multi_blocks(s
);
1006 if (!TRANSFERRING_DATA(s
->prnsts
)) {
1007 MASKED_WRITE(s
->blksize
, mask
, value
);
1008 MASKED_WRITE(s
->blkcnt
, mask
>> 16, value
>> 16);
1011 /* Limit block size to the maximum buffer size */
1012 if (extract32(s
->blksize
, 0, 12) > s
->buf_maxsz
) {
1013 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Size 0x%x is larger than " \
1014 "the maximum buffer 0x%x", __func__
, s
->blksize
,
1017 s
->blksize
= deposit32(s
->blksize
, 0, 12, s
->buf_maxsz
);
1022 MASKED_WRITE(s
->argument
, mask
, value
);
1025 /* DMA can be enabled only if it is supported as indicated by
1026 * capabilities register */
1027 if (!(s
->capareg
& SDHC_CAN_DO_DMA
)) {
1028 value
&= ~SDHC_TRNS_DMA
;
1030 MASKED_WRITE(s
->trnmod
, mask
, value
);
1031 MASKED_WRITE(s
->cmdreg
, mask
>> 16, value
>> 16);
1033 /* Writing to the upper byte of CMDREG triggers SD command generation */
1034 if ((mask
& 0xFF000000) || !sdhci_can_issue_command(s
)) {
1038 sdhci_send_command(s
);
1041 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
1042 sdhci_write_dataport(s
, value
>> shift
, size
);
1046 if (!(mask
& 0xFF0000)) {
1047 sdhci_blkgap_write(s
, value
>> 16);
1049 MASKED_WRITE(s
->hostctl
, mask
, value
);
1050 MASKED_WRITE(s
->pwrcon
, mask
>> 8, value
>> 8);
1051 MASKED_WRITE(s
->wakcon
, mask
>> 24, value
>> 24);
1052 if (!(s
->prnsts
& SDHC_CARD_PRESENT
) || ((s
->pwrcon
>> 1) & 0x7) < 5 ||
1053 !(s
->capareg
& (1 << (31 - ((s
->pwrcon
>> 1) & 0x7))))) {
1054 s
->pwrcon
&= ~SDHC_POWER_ON
;
1058 if (!(mask
& 0xFF000000)) {
1059 sdhci_reset_write(s
, value
>> 24);
1061 MASKED_WRITE(s
->clkcon
, mask
, value
);
1062 MASKED_WRITE(s
->timeoutcon
, mask
>> 16, value
>> 16);
1063 if (s
->clkcon
& SDHC_CLOCK_INT_EN
) {
1064 s
->clkcon
|= SDHC_CLOCK_INT_STABLE
;
1066 s
->clkcon
&= ~SDHC_CLOCK_INT_STABLE
;
1069 case SDHC_NORINTSTS
:
1070 if (s
->norintstsen
& SDHC_NISEN_CARDINT
) {
1071 value
&= ~SDHC_NIS_CARDINT
;
1073 s
->norintsts
&= mask
| ~value
;
1074 s
->errintsts
&= (mask
>> 16) | ~(value
>> 16);
1076 s
->norintsts
|= SDHC_NIS_ERR
;
1078 s
->norintsts
&= ~SDHC_NIS_ERR
;
1080 sdhci_update_irq(s
);
1082 case SDHC_NORINTSTSEN
:
1083 MASKED_WRITE(s
->norintstsen
, mask
, value
);
1084 MASKED_WRITE(s
->errintstsen
, mask
>> 16, value
>> 16);
1085 s
->norintsts
&= s
->norintstsen
;
1086 s
->errintsts
&= s
->errintstsen
;
1088 s
->norintsts
|= SDHC_NIS_ERR
;
1090 s
->norintsts
&= ~SDHC_NIS_ERR
;
1092 sdhci_update_irq(s
);
1094 case SDHC_NORINTSIGEN
:
1095 MASKED_WRITE(s
->norintsigen
, mask
, value
);
1096 MASKED_WRITE(s
->errintsigen
, mask
>> 16, value
>> 16);
1097 sdhci_update_irq(s
);
1100 MASKED_WRITE(s
->admaerr
, mask
, value
);
1102 case SDHC_ADMASYSADDR
:
1103 s
->admasysaddr
= (s
->admasysaddr
& (0xFFFFFFFF00000000ULL
|
1104 (uint64_t)mask
)) | (uint64_t)value
;
1106 case SDHC_ADMASYSADDR
+ 4:
1107 s
->admasysaddr
= (s
->admasysaddr
& (0x00000000FFFFFFFFULL
|
1108 ((uint64_t)mask
<< 32))) | ((uint64_t)value
<< 32);
1111 s
->acmd12errsts
|= value
;
1112 s
->errintsts
|= (value
>> 16) & s
->errintstsen
;
1113 if (s
->acmd12errsts
) {
1114 s
->errintsts
|= SDHC_EIS_CMD12ERR
;
1117 s
->norintsts
|= SDHC_NIS_ERR
;
1119 sdhci_update_irq(s
);
1122 ERRPRINT("bad %ub write offset: addr[0x%04x] <- %u(0x%x)\n",
1123 size
, (int)offset
, value
>> shift
, value
>> shift
);
1126 DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n",
1127 size
, (int)offset
, value
>> shift
, value
>> shift
);
1130 static const MemoryRegionOps sdhci_mmio_ops
= {
1132 .write
= sdhci_write
,
1134 .min_access_size
= 1,
1135 .max_access_size
= 4,
1138 .endianness
= DEVICE_LITTLE_ENDIAN
,
1141 static inline unsigned int sdhci_get_fifolen(SDHCIState
*s
)
1143 switch (SDHC_CAPAB_BLOCKSIZE(s
->capareg
)) {
1151 hw_error("SDHC: unsupported value for maximum block size\n");
1156 static void sdhci_initfn(SDHCIState
*s
, BlockBackend
*blk
)
1158 s
->card
= sd_init(blk
, false);
1159 if (s
->card
== NULL
) {
1162 s
->eject_cb
= qemu_allocate_irq(sdhci_insert_eject_cb
, s
, 0);
1163 s
->ro_cb
= qemu_allocate_irq(sdhci_card_readonly_cb
, s
, 0);
1164 sd_set_cb(s
->card
, s
->ro_cb
, s
->eject_cb
);
1166 s
->insert_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sdhci_raise_insertion_irq
, s
);
1167 s
->transfer_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sdhci_data_transfer
, s
);
1170 static void sdhci_uninitfn(SDHCIState
*s
)
1172 timer_del(s
->insert_timer
);
1173 timer_free(s
->insert_timer
);
1174 timer_del(s
->transfer_timer
);
1175 timer_free(s
->transfer_timer
);
1176 qemu_free_irq(s
->eject_cb
);
1177 qemu_free_irq(s
->ro_cb
);
1179 g_free(s
->fifo_buffer
);
1180 s
->fifo_buffer
= NULL
;
1183 const VMStateDescription sdhci_vmstate
= {
1186 .minimum_version_id
= 1,
1187 .fields
= (VMStateField
[]) {
1188 VMSTATE_UINT32(sdmasysad
, SDHCIState
),
1189 VMSTATE_UINT16(blksize
, SDHCIState
),
1190 VMSTATE_UINT16(blkcnt
, SDHCIState
),
1191 VMSTATE_UINT32(argument
, SDHCIState
),
1192 VMSTATE_UINT16(trnmod
, SDHCIState
),
1193 VMSTATE_UINT16(cmdreg
, SDHCIState
),
1194 VMSTATE_UINT32_ARRAY(rspreg
, SDHCIState
, 4),
1195 VMSTATE_UINT32(prnsts
, SDHCIState
),
1196 VMSTATE_UINT8(hostctl
, SDHCIState
),
1197 VMSTATE_UINT8(pwrcon
, SDHCIState
),
1198 VMSTATE_UINT8(blkgap
, SDHCIState
),
1199 VMSTATE_UINT8(wakcon
, SDHCIState
),
1200 VMSTATE_UINT16(clkcon
, SDHCIState
),
1201 VMSTATE_UINT8(timeoutcon
, SDHCIState
),
1202 VMSTATE_UINT8(admaerr
, SDHCIState
),
1203 VMSTATE_UINT16(norintsts
, SDHCIState
),
1204 VMSTATE_UINT16(errintsts
, SDHCIState
),
1205 VMSTATE_UINT16(norintstsen
, SDHCIState
),
1206 VMSTATE_UINT16(errintstsen
, SDHCIState
),
1207 VMSTATE_UINT16(norintsigen
, SDHCIState
),
1208 VMSTATE_UINT16(errintsigen
, SDHCIState
),
1209 VMSTATE_UINT16(acmd12errsts
, SDHCIState
),
1210 VMSTATE_UINT16(data_count
, SDHCIState
),
1211 VMSTATE_UINT64(admasysaddr
, SDHCIState
),
1212 VMSTATE_UINT8(stopped_state
, SDHCIState
),
1213 VMSTATE_VBUFFER_UINT32(fifo_buffer
, SDHCIState
, 1, NULL
, 0, buf_maxsz
),
1214 VMSTATE_TIMER_PTR(insert_timer
, SDHCIState
),
1215 VMSTATE_TIMER_PTR(transfer_timer
, SDHCIState
),
1216 VMSTATE_END_OF_LIST()
1220 /* Capabilities registers provide information on supported features of this
1221 * specific host controller implementation */
1222 static Property sdhci_pci_properties
[] = {
1224 * We currently fuse controller and card into a single device
1225 * model, but we intend to separate them. For that purpose, the
1226 * properties that belong to the card are marked as experimental.
1228 DEFINE_PROP_DRIVE("x-drive", SDHCIState
, blk
),
1229 DEFINE_PROP_UINT32("capareg", SDHCIState
, capareg
,
1230 SDHC_CAPAB_REG_DEFAULT
),
1231 DEFINE_PROP_UINT32("maxcurr", SDHCIState
, maxcurr
, 0),
1232 DEFINE_PROP_END_OF_LIST(),
1235 static void sdhci_pci_realize(PCIDevice
*dev
, Error
**errp
)
1237 SDHCIState
*s
= PCI_SDHCI(dev
);
1238 dev
->config
[PCI_CLASS_PROG
] = 0x01; /* Standard Host supported DMA */
1239 dev
->config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin A */
1240 sdhci_initfn(s
, s
->blk
);
1241 s
->buf_maxsz
= sdhci_get_fifolen(s
);
1242 s
->fifo_buffer
= g_malloc0(s
->buf_maxsz
);
1243 s
->irq
= pci_allocate_irq(dev
);
1244 memory_region_init_io(&s
->iomem
, OBJECT(s
), &sdhci_mmio_ops
, s
, "sdhci",
1245 SDHC_REGISTERS_MAP_SIZE
);
1246 pci_register_bar(dev
, 0, 0, &s
->iomem
);
1249 static void sdhci_pci_exit(PCIDevice
*dev
)
1251 SDHCIState
*s
= PCI_SDHCI(dev
);
1255 static void sdhci_pci_class_init(ObjectClass
*klass
, void *data
)
1257 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1258 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1260 k
->realize
= sdhci_pci_realize
;
1261 k
->exit
= sdhci_pci_exit
;
1262 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
1263 k
->device_id
= PCI_DEVICE_ID_REDHAT_SDHCI
;
1264 k
->class_id
= PCI_CLASS_SYSTEM_SDHCI
;
1265 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1266 dc
->vmsd
= &sdhci_vmstate
;
1267 dc
->props
= sdhci_pci_properties
;
1270 static const TypeInfo sdhci_pci_info
= {
1271 .name
= TYPE_PCI_SDHCI
,
1272 .parent
= TYPE_PCI_DEVICE
,
1273 .instance_size
= sizeof(SDHCIState
),
1274 .class_init
= sdhci_pci_class_init
,
1277 static Property sdhci_sysbus_properties
[] = {
1278 DEFINE_PROP_UINT32("capareg", SDHCIState
, capareg
,
1279 SDHC_CAPAB_REG_DEFAULT
),
1280 DEFINE_PROP_UINT32("maxcurr", SDHCIState
, maxcurr
, 0),
1281 DEFINE_PROP_BOOL("noeject-quirk", SDHCIState
, noeject_quirk
, false),
1282 DEFINE_PROP_END_OF_LIST(),
1285 static void sdhci_sysbus_init(Object
*obj
)
1287 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1290 /* FIXME use a qdev drive property instead of drive_get_next() */
1291 di
= drive_get_next(IF_SD
);
1292 sdhci_initfn(s
, di
? blk_by_legacy_dinfo(di
) : NULL
);
1295 static void sdhci_sysbus_finalize(Object
*obj
)
1297 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1301 static void sdhci_sysbus_realize(DeviceState
*dev
, Error
** errp
)
1303 SDHCIState
*s
= SYSBUS_SDHCI(dev
);
1304 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1306 s
->buf_maxsz
= sdhci_get_fifolen(s
);
1307 s
->fifo_buffer
= g_malloc0(s
->buf_maxsz
);
1308 sysbus_init_irq(sbd
, &s
->irq
);
1309 memory_region_init_io(&s
->iomem
, OBJECT(s
), &sdhci_mmio_ops
, s
, "sdhci",
1310 SDHC_REGISTERS_MAP_SIZE
);
1311 sysbus_init_mmio(sbd
, &s
->iomem
);
1314 static void sdhci_sysbus_class_init(ObjectClass
*klass
, void *data
)
1316 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1318 dc
->vmsd
= &sdhci_vmstate
;
1319 dc
->props
= sdhci_sysbus_properties
;
1320 dc
->realize
= sdhci_sysbus_realize
;
1321 /* Reason: instance_init() method uses drive_get_next() */
1322 dc
->cannot_instantiate_with_device_add_yet
= true;
1325 static const TypeInfo sdhci_sysbus_info
= {
1326 .name
= TYPE_SYSBUS_SDHCI
,
1327 .parent
= TYPE_SYS_BUS_DEVICE
,
1328 .instance_size
= sizeof(SDHCIState
),
1329 .instance_init
= sdhci_sysbus_init
,
1330 .instance_finalize
= sdhci_sysbus_finalize
,
1331 .class_init
= sdhci_sysbus_class_init
,
1334 static void sdhci_register_types(void)
1336 type_register_static(&sdhci_pci_info
);
1337 type_register_static(&sdhci_sysbus_info
);
1340 type_init(sdhci_register_types
)