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/block-backend.h"
27 #include "sysemu/blockdev.h"
28 #include "sysemu/dma.h"
29 #include "qemu/timer.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 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 sd_set_cb(s
->card
, s
->ro_cb
, s
->eject_cb
);
198 s
->stopped_state
= sdhc_not_stopped
;
201 static void sdhci_data_transfer(void *opaque
);
203 static void sdhci_send_command(SDHCIState
*s
)
206 uint8_t response
[16];
211 request
.cmd
= s
->cmdreg
>> 8;
212 request
.arg
= s
->argument
;
213 DPRINT_L1("sending CMD%u ARG[0x%08x]\n", request
.cmd
, request
.arg
);
214 rlen
= sd_do_command(s
->card
, &request
, response
);
216 if (s
->cmdreg
& SDHC_CMD_RESPONSE
) {
218 s
->rspreg
[0] = (response
[0] << 24) | (response
[1] << 16) |
219 (response
[2] << 8) | response
[3];
220 s
->rspreg
[1] = s
->rspreg
[2] = s
->rspreg
[3] = 0;
221 DPRINT_L1("Response: RSPREG[31..0]=0x%08x\n", s
->rspreg
[0]);
222 } else if (rlen
== 16) {
223 s
->rspreg
[0] = (response
[11] << 24) | (response
[12] << 16) |
224 (response
[13] << 8) | response
[14];
225 s
->rspreg
[1] = (response
[7] << 24) | (response
[8] << 16) |
226 (response
[9] << 8) | response
[10];
227 s
->rspreg
[2] = (response
[3] << 24) | (response
[4] << 16) |
228 (response
[5] << 8) | response
[6];
229 s
->rspreg
[3] = (response
[0] << 16) | (response
[1] << 8) |
231 DPRINT_L1("Response received:\n RSPREG[127..96]=0x%08x, RSPREG[95.."
232 "64]=0x%08x,\n RSPREG[63..32]=0x%08x, RSPREG[31..0]=0x%08x\n",
233 s
->rspreg
[3], s
->rspreg
[2], s
->rspreg
[1], s
->rspreg
[0]);
235 ERRPRINT("Timeout waiting for command response\n");
236 if (s
->errintstsen
& SDHC_EISEN_CMDTIMEOUT
) {
237 s
->errintsts
|= SDHC_EIS_CMDTIMEOUT
;
238 s
->norintsts
|= SDHC_NIS_ERR
;
242 if ((s
->norintstsen
& SDHC_NISEN_TRSCMP
) &&
243 (s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
) {
244 s
->norintsts
|= SDHC_NIS_TRSCMP
;
246 } else if (rlen
!= 0 && (s
->errintstsen
& SDHC_EISEN_CMDIDX
)) {
247 s
->errintsts
|= SDHC_EIS_CMDIDX
;
248 s
->norintsts
|= SDHC_NIS_ERR
;
251 if (s
->norintstsen
& SDHC_NISEN_CMDCMP
) {
252 s
->norintsts
|= SDHC_NIS_CMDCMP
;
257 if (s
->blksize
&& (s
->cmdreg
& SDHC_CMD_DATA_PRESENT
)) {
259 sdhci_data_transfer(s
);
263 static void sdhci_end_transfer(SDHCIState
*s
)
265 /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */
266 if ((s
->trnmod
& SDHC_TRNS_ACMD12
) != 0) {
268 uint8_t response
[16];
272 DPRINT_L1("Automatically issue CMD%d %08x\n", request
.cmd
, request
.arg
);
273 sd_do_command(s
->card
, &request
, response
);
274 /* Auto CMD12 response goes to the upper Response register */
275 s
->rspreg
[3] = (response
[0] << 24) | (response
[1] << 16) |
276 (response
[2] << 8) | response
[3];
279 s
->prnsts
&= ~(SDHC_DOING_READ
| SDHC_DOING_WRITE
|
280 SDHC_DAT_LINE_ACTIVE
| SDHC_DATA_INHIBIT
|
281 SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
);
283 if (s
->norintstsen
& SDHC_NISEN_TRSCMP
) {
284 s
->norintsts
|= SDHC_NIS_TRSCMP
;
291 * Programmed i/o data transfer
294 /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
295 static void sdhci_read_block_from_card(SDHCIState
*s
)
299 if ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
300 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) {
304 for (index
= 0; index
< (s
->blksize
& 0x0fff); index
++) {
305 s
->fifo_buffer
[index
] = sd_read_data(s
->card
);
308 /* New data now available for READ through Buffer Port Register */
309 s
->prnsts
|= SDHC_DATA_AVAILABLE
;
310 if (s
->norintstsen
& SDHC_NISEN_RBUFRDY
) {
311 s
->norintsts
|= SDHC_NIS_RBUFRDY
;
314 /* Clear DAT line active status if that was the last block */
315 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
316 ((s
->trnmod
& SDHC_TRNS_MULTI
) && s
->blkcnt
== 1)) {
317 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
320 /* If stop at block gap request was set and it's not the last block of
321 * data - generate Block Event interrupt */
322 if (s
->stopped_state
== sdhc_gap_read
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
324 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
325 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
326 s
->norintsts
|= SDHC_EIS_BLKGAP
;
333 /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
334 static uint32_t sdhci_read_dataport(SDHCIState
*s
, unsigned size
)
339 /* first check that a valid data exists in host controller input buffer */
340 if ((s
->prnsts
& SDHC_DATA_AVAILABLE
) == 0) {
341 ERRPRINT("Trying to read from empty buffer\n");
345 for (i
= 0; i
< size
; i
++) {
346 value
|= s
->fifo_buffer
[s
->data_count
] << i
* 8;
348 /* check if we've read all valid data (blksize bytes) from buffer */
349 if ((s
->data_count
) >= (s
->blksize
& 0x0fff)) {
350 DPRINT_L2("All %u bytes of data have been read from input buffer\n",
352 s
->prnsts
&= ~SDHC_DATA_AVAILABLE
; /* no more data in a buffer */
353 s
->data_count
= 0; /* next buff read must start at position [0] */
355 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
359 /* if that was the last block of data */
360 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
361 ((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) ||
362 /* stop at gap request */
363 (s
->stopped_state
== sdhc_gap_read
&&
364 !(s
->prnsts
& SDHC_DAT_LINE_ACTIVE
))) {
365 sdhci_end_transfer(s
);
366 } else { /* if there are more data, read next block from card */
367 sdhci_read_block_from_card(s
);
376 /* Write data from host controller FIFO to card */
377 static void sdhci_write_block_to_card(SDHCIState
*s
)
381 if (s
->prnsts
& SDHC_SPACE_AVAILABLE
) {
382 if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
383 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
389 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
390 if (s
->blkcnt
== 0) {
397 for (index
= 0; index
< (s
->blksize
& 0x0fff); index
++) {
398 sd_write_data(s
->card
, s
->fifo_buffer
[index
]);
401 /* Next data can be written through BUFFER DATORT register */
402 s
->prnsts
|= SDHC_SPACE_AVAILABLE
;
404 /* Finish transfer if that was the last block of data */
405 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
406 ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
407 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0))) {
408 sdhci_end_transfer(s
);
409 } else if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
410 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
413 /* Generate Block Gap Event if requested and if not the last block */
414 if (s
->stopped_state
== sdhc_gap_write
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
416 s
->prnsts
&= ~SDHC_DOING_WRITE
;
417 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
418 s
->norintsts
|= SDHC_EIS_BLKGAP
;
420 sdhci_end_transfer(s
);
426 /* Write @size bytes of @value data to host controller @s Buffer Data Port
428 static void sdhci_write_dataport(SDHCIState
*s
, uint32_t value
, unsigned size
)
432 /* Check that there is free space left in a buffer */
433 if (!(s
->prnsts
& SDHC_SPACE_AVAILABLE
)) {
434 ERRPRINT("Can't write to data buffer: buffer full\n");
438 for (i
= 0; i
< size
; i
++) {
439 s
->fifo_buffer
[s
->data_count
] = value
& 0xFF;
442 if (s
->data_count
>= (s
->blksize
& 0x0fff)) {
443 DPRINT_L2("write buffer filled with %u bytes of data\n",
446 s
->prnsts
&= ~SDHC_SPACE_AVAILABLE
;
447 if (s
->prnsts
& SDHC_DOING_WRITE
) {
448 sdhci_write_block_to_card(s
);
455 * Single DMA data transfer
458 /* Multi block SDMA transfer */
459 static void sdhci_sdma_transfer_multi_blocks(SDHCIState
*s
)
461 bool page_aligned
= false;
462 unsigned int n
, begin
;
463 const uint16_t block_size
= s
->blksize
& 0x0fff;
464 uint32_t boundary_chk
= 1 << (((s
->blksize
& 0xf000) >> 12) + 12);
465 uint32_t boundary_count
= boundary_chk
- (s
->sdmasysad
% boundary_chk
);
467 /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
468 * possible stop at page boundary if initial address is not page aligned,
469 * allow them to work properly */
470 if ((s
->sdmasysad
% boundary_chk
) == 0) {
474 if (s
->trnmod
& SDHC_TRNS_READ
) {
475 s
->prnsts
|= SDHC_DOING_READ
| SDHC_DATA_INHIBIT
|
476 SDHC_DAT_LINE_ACTIVE
;
478 if (s
->data_count
== 0) {
479 for (n
= 0; n
< block_size
; n
++) {
480 s
->fifo_buffer
[n
] = sd_read_data(s
->card
);
483 begin
= s
->data_count
;
484 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
485 s
->data_count
= boundary_count
+ begin
;
488 s
->data_count
= block_size
;
489 boundary_count
-= block_size
- begin
;
490 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
494 dma_memory_write(&address_space_memory
, s
->sdmasysad
,
495 &s
->fifo_buffer
[begin
], s
->data_count
- begin
);
496 s
->sdmasysad
+= s
->data_count
- begin
;
497 if (s
->data_count
== block_size
) {
500 if (page_aligned
&& boundary_count
== 0) {
505 s
->prnsts
|= SDHC_DOING_WRITE
| SDHC_DATA_INHIBIT
|
506 SDHC_DAT_LINE_ACTIVE
;
508 begin
= s
->data_count
;
509 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
510 s
->data_count
= boundary_count
+ begin
;
513 s
->data_count
= block_size
;
514 boundary_count
-= block_size
- begin
;
516 dma_memory_read(&address_space_memory
, s
->sdmasysad
,
517 &s
->fifo_buffer
[begin
], s
->data_count
);
518 s
->sdmasysad
+= s
->data_count
- begin
;
519 if (s
->data_count
== block_size
) {
520 for (n
= 0; n
< block_size
; n
++) {
521 sd_write_data(s
->card
, s
->fifo_buffer
[n
]);
524 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
528 if (page_aligned
&& boundary_count
== 0) {
534 if (s
->blkcnt
== 0) {
535 sdhci_end_transfer(s
);
537 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
538 s
->norintsts
|= SDHC_NIS_DMA
;
544 /* single block SDMA transfer */
546 static void sdhci_sdma_transfer_single_block(SDHCIState
*s
)
549 uint32_t datacnt
= s
->blksize
& 0x0fff;
551 if (s
->trnmod
& SDHC_TRNS_READ
) {
552 for (n
= 0; n
< datacnt
; n
++) {
553 s
->fifo_buffer
[n
] = sd_read_data(s
->card
);
555 dma_memory_write(&address_space_memory
, s
->sdmasysad
, s
->fifo_buffer
,
558 dma_memory_read(&address_space_memory
, s
->sdmasysad
, s
->fifo_buffer
,
560 for (n
= 0; n
< datacnt
; n
++) {
561 sd_write_data(s
->card
, s
->fifo_buffer
[n
]);
565 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
569 sdhci_end_transfer(s
);
572 typedef struct ADMADescr
{
579 static void get_adma_description(SDHCIState
*s
, ADMADescr
*dscr
)
583 hwaddr entry_addr
= (hwaddr
)s
->admasysaddr
;
584 switch (SDHC_DMA_TYPE(s
->hostctl
)) {
585 case SDHC_CTRL_ADMA2_32
:
586 dma_memory_read(&address_space_memory
, entry_addr
, (uint8_t *)&adma2
,
588 adma2
= le64_to_cpu(adma2
);
589 /* The spec does not specify endianness of descriptor table.
590 * We currently assume that it is LE.
592 dscr
->addr
= (hwaddr
)extract64(adma2
, 32, 32) & ~0x3ull
;
593 dscr
->length
= (uint16_t)extract64(adma2
, 16, 16);
594 dscr
->attr
= (uint8_t)extract64(adma2
, 0, 7);
597 case SDHC_CTRL_ADMA1_32
:
598 dma_memory_read(&address_space_memory
, entry_addr
, (uint8_t *)&adma1
,
600 adma1
= le32_to_cpu(adma1
);
601 dscr
->addr
= (hwaddr
)(adma1
& 0xFFFFF000);
602 dscr
->attr
= (uint8_t)extract32(adma1
, 0, 7);
604 if ((dscr
->attr
& SDHC_ADMA_ATTR_ACT_MASK
) == SDHC_ADMA_ATTR_SET_LEN
) {
605 dscr
->length
= (uint16_t)extract32(adma1
, 12, 16);
610 case SDHC_CTRL_ADMA2_64
:
611 dma_memory_read(&address_space_memory
, entry_addr
,
612 (uint8_t *)(&dscr
->attr
), 1);
613 dma_memory_read(&address_space_memory
, entry_addr
+ 2,
614 (uint8_t *)(&dscr
->length
), 2);
615 dscr
->length
= le16_to_cpu(dscr
->length
);
616 dma_memory_read(&address_space_memory
, entry_addr
+ 4,
617 (uint8_t *)(&dscr
->addr
), 8);
618 dscr
->attr
= le64_to_cpu(dscr
->attr
);
619 dscr
->attr
&= 0xfffffff8;
625 /* Advanced DMA data transfer */
627 static void sdhci_do_adma(SDHCIState
*s
)
629 unsigned int n
, begin
, length
;
630 const uint16_t block_size
= s
->blksize
& 0x0fff;
634 for (i
= 0; i
< SDHC_ADMA_DESCS_PER_DELAY
; ++i
) {
635 s
->admaerr
&= ~SDHC_ADMAERR_LENGTH_MISMATCH
;
637 get_adma_description(s
, &dscr
);
638 DPRINT_L2("ADMA loop: addr=" TARGET_FMT_plx
", len=%d, attr=%x\n",
639 dscr
.addr
, dscr
.length
, dscr
.attr
);
641 if ((dscr
.attr
& SDHC_ADMA_ATTR_VALID
) == 0) {
642 /* Indicate that error occurred in ST_FDS state */
643 s
->admaerr
&= ~SDHC_ADMAERR_STATE_MASK
;
644 s
->admaerr
|= SDHC_ADMAERR_STATE_ST_FDS
;
646 /* Generate ADMA error interrupt */
647 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
648 s
->errintsts
|= SDHC_EIS_ADMAERR
;
649 s
->norintsts
|= SDHC_NIS_ERR
;
656 length
= dscr
.length
? dscr
.length
: 65536;
658 switch (dscr
.attr
& SDHC_ADMA_ATTR_ACT_MASK
) {
659 case SDHC_ADMA_ATTR_ACT_TRAN
: /* data transfer */
661 if (s
->trnmod
& SDHC_TRNS_READ
) {
663 if (s
->data_count
== 0) {
664 for (n
= 0; n
< block_size
; n
++) {
665 s
->fifo_buffer
[n
] = sd_read_data(s
->card
);
668 begin
= s
->data_count
;
669 if ((length
+ begin
) < block_size
) {
670 s
->data_count
= length
+ begin
;
673 s
->data_count
= block_size
;
674 length
-= block_size
- begin
;
676 dma_memory_write(&address_space_memory
, dscr
.addr
,
677 &s
->fifo_buffer
[begin
],
678 s
->data_count
- begin
);
679 dscr
.addr
+= s
->data_count
- begin
;
680 if (s
->data_count
== block_size
) {
682 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
684 if (s
->blkcnt
== 0) {
692 begin
= s
->data_count
;
693 if ((length
+ begin
) < block_size
) {
694 s
->data_count
= length
+ begin
;
697 s
->data_count
= block_size
;
698 length
-= block_size
- begin
;
700 dma_memory_read(&address_space_memory
, dscr
.addr
,
701 &s
->fifo_buffer
[begin
],
702 s
->data_count
- begin
);
703 dscr
.addr
+= s
->data_count
- begin
;
704 if (s
->data_count
== block_size
) {
705 for (n
= 0; n
< block_size
; n
++) {
706 sd_write_data(s
->card
, s
->fifo_buffer
[n
]);
709 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
711 if (s
->blkcnt
== 0) {
718 s
->admasysaddr
+= dscr
.incr
;
720 case SDHC_ADMA_ATTR_ACT_LINK
: /* link to next descriptor table */
721 s
->admasysaddr
= dscr
.addr
;
722 DPRINT_L1("ADMA link: admasysaddr=0x%lx\n", s
->admasysaddr
);
725 s
->admasysaddr
+= dscr
.incr
;
729 if (dscr
.attr
& SDHC_ADMA_ATTR_INT
) {
730 DPRINT_L1("ADMA interrupt: admasysaddr=0x%lx\n", s
->admasysaddr
);
731 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
732 s
->norintsts
|= SDHC_NIS_DMA
;
738 /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
739 if (((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
740 (s
->blkcnt
== 0)) || (dscr
.attr
& SDHC_ADMA_ATTR_END
)) {
741 DPRINT_L2("ADMA transfer completed\n");
742 if (length
|| ((dscr
.attr
& SDHC_ADMA_ATTR_END
) &&
743 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
745 ERRPRINT("SD/MMC host ADMA length mismatch\n");
746 s
->admaerr
|= SDHC_ADMAERR_LENGTH_MISMATCH
|
747 SDHC_ADMAERR_STATE_ST_TFR
;
748 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
749 ERRPRINT("Set ADMA error flag\n");
750 s
->errintsts
|= SDHC_EIS_ADMAERR
;
751 s
->norintsts
|= SDHC_NIS_ERR
;
756 sdhci_end_transfer(s
);
762 /* we have unfinished business - reschedule to continue ADMA */
763 timer_mod(s
->transfer_timer
,
764 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + SDHC_TRANSFER_DELAY
);
767 /* Perform data transfer according to controller configuration */
769 static void sdhci_data_transfer(void *opaque
)
771 SDHCIState
*s
= (SDHCIState
*)opaque
;
773 if (s
->trnmod
& SDHC_TRNS_DMA
) {
774 switch (SDHC_DMA_TYPE(s
->hostctl
)) {
776 if ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
777 (!(s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) || s
->blkcnt
== 0)) {
781 if ((s
->blkcnt
== 1) || !(s
->trnmod
& SDHC_TRNS_MULTI
)) {
782 sdhci_sdma_transfer_single_block(s
);
784 sdhci_sdma_transfer_multi_blocks(s
);
788 case SDHC_CTRL_ADMA1_32
:
789 if (!(s
->capareg
& SDHC_CAN_DO_ADMA1
)) {
790 ERRPRINT("ADMA1 not supported\n");
796 case SDHC_CTRL_ADMA2_32
:
797 if (!(s
->capareg
& SDHC_CAN_DO_ADMA2
)) {
798 ERRPRINT("ADMA2 not supported\n");
804 case SDHC_CTRL_ADMA2_64
:
805 if (!(s
->capareg
& SDHC_CAN_DO_ADMA2
) ||
806 !(s
->capareg
& SDHC_64_BIT_BUS_SUPPORT
)) {
807 ERRPRINT("64 bit ADMA not supported\n");
814 ERRPRINT("Unsupported DMA type\n");
818 if ((s
->trnmod
& SDHC_TRNS_READ
) && sd_data_ready(s
->card
)) {
819 s
->prnsts
|= SDHC_DOING_READ
| SDHC_DATA_INHIBIT
|
820 SDHC_DAT_LINE_ACTIVE
;
821 sdhci_read_block_from_card(s
);
823 s
->prnsts
|= SDHC_DOING_WRITE
| SDHC_DAT_LINE_ACTIVE
|
824 SDHC_SPACE_AVAILABLE
| SDHC_DATA_INHIBIT
;
825 sdhci_write_block_to_card(s
);
830 static bool sdhci_can_issue_command(SDHCIState
*s
)
832 if (!SDHC_CLOCK_IS_ON(s
->clkcon
) || !(s
->pwrcon
& SDHC_POWER_ON
) ||
833 (((s
->prnsts
& SDHC_DATA_INHIBIT
) || s
->stopped_state
) &&
834 ((s
->cmdreg
& SDHC_CMD_DATA_PRESENT
) ||
835 ((s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
&&
836 !(SDHC_COMMAND_TYPE(s
->cmdreg
) == SDHC_CMD_ABORT
))))) {
843 /* The Buffer Data Port register must be accessed in sequential and
844 * continuous manner */
846 sdhci_buff_access_is_sequential(SDHCIState
*s
, unsigned byte_num
)
848 if ((s
->data_count
& 0x3) != byte_num
) {
849 ERRPRINT("Non-sequential access to Buffer Data Port register"
856 static uint64_t sdhci_read(void *opaque
, hwaddr offset
, unsigned size
)
858 SDHCIState
*s
= (SDHCIState
*)opaque
;
861 switch (offset
& ~0x3) {
866 ret
= s
->blksize
| (s
->blkcnt
<< 16);
872 ret
= s
->trnmod
| (s
->cmdreg
<< 16);
874 case SDHC_RSPREG0
... SDHC_RSPREG3
:
875 ret
= s
->rspreg
[((offset
& ~0x3) - SDHC_RSPREG0
) >> 2];
878 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
879 ret
= sdhci_read_dataport(s
, size
);
880 DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size
, (int)offset
,
889 ret
= s
->hostctl
| (s
->pwrcon
<< 8) | (s
->blkgap
<< 16) |
893 ret
= s
->clkcon
| (s
->timeoutcon
<< 16);
896 ret
= s
->norintsts
| (s
->errintsts
<< 16);
898 case SDHC_NORINTSTSEN
:
899 ret
= s
->norintstsen
| (s
->errintstsen
<< 16);
901 case SDHC_NORINTSIGEN
:
902 ret
= s
->norintsigen
| (s
->errintsigen
<< 16);
904 case SDHC_ACMD12ERRSTS
:
905 ret
= s
->acmd12errsts
;
916 case SDHC_ADMASYSADDR
:
917 ret
= (uint32_t)s
->admasysaddr
;
919 case SDHC_ADMASYSADDR
+ 4:
920 ret
= (uint32_t)(s
->admasysaddr
>> 32);
922 case SDHC_SLOT_INT_STATUS
:
923 ret
= (SD_HOST_SPECv2_VERS
<< 16) | sdhci_slotint(s
);
926 ERRPRINT("bad %ub read: addr[0x%04x]\n", size
, (int)offset
);
930 ret
>>= (offset
& 0x3) * 8;
931 ret
&= (1ULL << (size
* 8)) - 1;
932 DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size
, (int)offset
, ret
, ret
);
936 static inline void sdhci_blkgap_write(SDHCIState
*s
, uint8_t value
)
938 if ((value
& SDHC_STOP_AT_GAP_REQ
) && (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
)) {
941 s
->blkgap
= value
& SDHC_STOP_AT_GAP_REQ
;
943 if ((value
& SDHC_CONTINUE_REQ
) && s
->stopped_state
&&
944 (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
) == 0) {
945 if (s
->stopped_state
== sdhc_gap_read
) {
946 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_READ
;
947 sdhci_read_block_from_card(s
);
949 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_WRITE
;
950 sdhci_write_block_to_card(s
);
952 s
->stopped_state
= sdhc_not_stopped
;
953 } else if (!s
->stopped_state
&& (value
& SDHC_STOP_AT_GAP_REQ
)) {
954 if (s
->prnsts
& SDHC_DOING_READ
) {
955 s
->stopped_state
= sdhc_gap_read
;
956 } else if (s
->prnsts
& SDHC_DOING_WRITE
) {
957 s
->stopped_state
= sdhc_gap_write
;
962 static inline void sdhci_reset_write(SDHCIState
*s
, uint8_t value
)
969 s
->prnsts
&= ~SDHC_CMD_INHIBIT
;
970 s
->norintsts
&= ~SDHC_NIS_CMDCMP
;
972 case SDHC_RESET_DATA
:
974 s
->prnsts
&= ~(SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
|
975 SDHC_DOING_READ
| SDHC_DOING_WRITE
|
976 SDHC_DATA_INHIBIT
| SDHC_DAT_LINE_ACTIVE
);
977 s
->blkgap
&= ~(SDHC_STOP_AT_GAP_REQ
| SDHC_CONTINUE_REQ
);
978 s
->stopped_state
= sdhc_not_stopped
;
979 s
->norintsts
&= ~(SDHC_NIS_WBUFRDY
| SDHC_NIS_RBUFRDY
|
980 SDHC_NIS_DMA
| SDHC_NIS_TRSCMP
| SDHC_NIS_BLKGAP
);
986 sdhci_write(void *opaque
, hwaddr offset
, uint64_t val
, unsigned size
)
988 SDHCIState
*s
= (SDHCIState
*)opaque
;
989 unsigned shift
= 8 * (offset
& 0x3);
990 uint32_t mask
= ~(((1ULL << (size
* 8)) - 1) << shift
);
991 uint32_t value
= val
;
994 switch (offset
& ~0x3) {
996 s
->sdmasysad
= (s
->sdmasysad
& mask
) | value
;
997 MASKED_WRITE(s
->sdmasysad
, mask
, value
);
998 /* Writing to last byte of sdmasysad might trigger transfer */
999 if (!(mask
& 0xFF000000) && TRANSFERRING_DATA(s
->prnsts
) && s
->blkcnt
&&
1000 s
->blksize
&& SDHC_DMA_TYPE(s
->hostctl
) == SDHC_CTRL_SDMA
) {
1001 sdhci_sdma_transfer_multi_blocks(s
);
1005 if (!TRANSFERRING_DATA(s
->prnsts
)) {
1006 MASKED_WRITE(s
->blksize
, mask
, value
);
1007 MASKED_WRITE(s
->blkcnt
, mask
>> 16, value
>> 16);
1011 MASKED_WRITE(s
->argument
, mask
, value
);
1014 /* DMA can be enabled only if it is supported as indicated by
1015 * capabilities register */
1016 if (!(s
->capareg
& SDHC_CAN_DO_DMA
)) {
1017 value
&= ~SDHC_TRNS_DMA
;
1019 MASKED_WRITE(s
->trnmod
, mask
, value
);
1020 MASKED_WRITE(s
->cmdreg
, mask
>> 16, value
>> 16);
1022 /* Writing to the upper byte of CMDREG triggers SD command generation */
1023 if ((mask
& 0xFF000000) || !sdhci_can_issue_command(s
)) {
1027 sdhci_send_command(s
);
1030 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
1031 sdhci_write_dataport(s
, value
>> shift
, size
);
1035 if (!(mask
& 0xFF0000)) {
1036 sdhci_blkgap_write(s
, value
>> 16);
1038 MASKED_WRITE(s
->hostctl
, mask
, value
);
1039 MASKED_WRITE(s
->pwrcon
, mask
>> 8, value
>> 8);
1040 MASKED_WRITE(s
->wakcon
, mask
>> 24, value
>> 24);
1041 if (!(s
->prnsts
& SDHC_CARD_PRESENT
) || ((s
->pwrcon
>> 1) & 0x7) < 5 ||
1042 !(s
->capareg
& (1 << (31 - ((s
->pwrcon
>> 1) & 0x7))))) {
1043 s
->pwrcon
&= ~SDHC_POWER_ON
;
1047 if (!(mask
& 0xFF000000)) {
1048 sdhci_reset_write(s
, value
>> 24);
1050 MASKED_WRITE(s
->clkcon
, mask
, value
);
1051 MASKED_WRITE(s
->timeoutcon
, mask
>> 16, value
>> 16);
1052 if (s
->clkcon
& SDHC_CLOCK_INT_EN
) {
1053 s
->clkcon
|= SDHC_CLOCK_INT_STABLE
;
1055 s
->clkcon
&= ~SDHC_CLOCK_INT_STABLE
;
1058 case SDHC_NORINTSTS
:
1059 if (s
->norintstsen
& SDHC_NISEN_CARDINT
) {
1060 value
&= ~SDHC_NIS_CARDINT
;
1062 s
->norintsts
&= mask
| ~value
;
1063 s
->errintsts
&= (mask
>> 16) | ~(value
>> 16);
1065 s
->norintsts
|= SDHC_NIS_ERR
;
1067 s
->norintsts
&= ~SDHC_NIS_ERR
;
1069 sdhci_update_irq(s
);
1071 case SDHC_NORINTSTSEN
:
1072 MASKED_WRITE(s
->norintstsen
, mask
, value
);
1073 MASKED_WRITE(s
->errintstsen
, mask
>> 16, value
>> 16);
1074 s
->norintsts
&= s
->norintstsen
;
1075 s
->errintsts
&= s
->errintstsen
;
1077 s
->norintsts
|= SDHC_NIS_ERR
;
1079 s
->norintsts
&= ~SDHC_NIS_ERR
;
1081 sdhci_update_irq(s
);
1083 case SDHC_NORINTSIGEN
:
1084 MASKED_WRITE(s
->norintsigen
, mask
, value
);
1085 MASKED_WRITE(s
->errintsigen
, mask
>> 16, value
>> 16);
1086 sdhci_update_irq(s
);
1089 MASKED_WRITE(s
->admaerr
, mask
, value
);
1091 case SDHC_ADMASYSADDR
:
1092 s
->admasysaddr
= (s
->admasysaddr
& (0xFFFFFFFF00000000ULL
|
1093 (uint64_t)mask
)) | (uint64_t)value
;
1095 case SDHC_ADMASYSADDR
+ 4:
1096 s
->admasysaddr
= (s
->admasysaddr
& (0x00000000FFFFFFFFULL
|
1097 ((uint64_t)mask
<< 32))) | ((uint64_t)value
<< 32);
1100 s
->acmd12errsts
|= value
;
1101 s
->errintsts
|= (value
>> 16) & s
->errintstsen
;
1102 if (s
->acmd12errsts
) {
1103 s
->errintsts
|= SDHC_EIS_CMD12ERR
;
1106 s
->norintsts
|= SDHC_NIS_ERR
;
1108 sdhci_update_irq(s
);
1111 ERRPRINT("bad %ub write offset: addr[0x%04x] <- %u(0x%x)\n",
1112 size
, (int)offset
, value
>> shift
, value
>> shift
);
1115 DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n",
1116 size
, (int)offset
, value
>> shift
, value
>> shift
);
1119 static const MemoryRegionOps sdhci_mmio_ops
= {
1121 .write
= sdhci_write
,
1123 .min_access_size
= 1,
1124 .max_access_size
= 4,
1127 .endianness
= DEVICE_LITTLE_ENDIAN
,
1130 static inline unsigned int sdhci_get_fifolen(SDHCIState
*s
)
1132 switch (SDHC_CAPAB_BLOCKSIZE(s
->capareg
)) {
1140 hw_error("SDHC: unsupported value for maximum block size\n");
1145 static void sdhci_initfn(SDHCIState
*s
)
1149 /* FIXME use a qdev drive property instead of drive_get_next() */
1150 di
= drive_get_next(IF_SD
);
1151 s
->card
= sd_init(di
? blk_by_legacy_dinfo(di
) : NULL
, false);
1152 if (s
->card
== NULL
) {
1155 s
->eject_cb
= qemu_allocate_irq(sdhci_insert_eject_cb
, s
, 0);
1156 s
->ro_cb
= qemu_allocate_irq(sdhci_card_readonly_cb
, s
, 0);
1157 sd_set_cb(s
->card
, s
->ro_cb
, s
->eject_cb
);
1159 s
->insert_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sdhci_raise_insertion_irq
, s
);
1160 s
->transfer_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sdhci_data_transfer
, s
);
1163 static void sdhci_uninitfn(SDHCIState
*s
)
1165 timer_del(s
->insert_timer
);
1166 timer_free(s
->insert_timer
);
1167 timer_del(s
->transfer_timer
);
1168 timer_free(s
->transfer_timer
);
1169 qemu_free_irq(s
->eject_cb
);
1170 qemu_free_irq(s
->ro_cb
);
1172 if (s
->fifo_buffer
) {
1173 g_free(s
->fifo_buffer
);
1174 s
->fifo_buffer
= NULL
;
1178 const VMStateDescription sdhci_vmstate
= {
1181 .minimum_version_id
= 1,
1182 .fields
= (VMStateField
[]) {
1183 VMSTATE_UINT32(sdmasysad
, SDHCIState
),
1184 VMSTATE_UINT16(blksize
, SDHCIState
),
1185 VMSTATE_UINT16(blkcnt
, SDHCIState
),
1186 VMSTATE_UINT32(argument
, SDHCIState
),
1187 VMSTATE_UINT16(trnmod
, SDHCIState
),
1188 VMSTATE_UINT16(cmdreg
, SDHCIState
),
1189 VMSTATE_UINT32_ARRAY(rspreg
, SDHCIState
, 4),
1190 VMSTATE_UINT32(prnsts
, SDHCIState
),
1191 VMSTATE_UINT8(hostctl
, SDHCIState
),
1192 VMSTATE_UINT8(pwrcon
, SDHCIState
),
1193 VMSTATE_UINT8(blkgap
, SDHCIState
),
1194 VMSTATE_UINT8(wakcon
, SDHCIState
),
1195 VMSTATE_UINT16(clkcon
, SDHCIState
),
1196 VMSTATE_UINT8(timeoutcon
, SDHCIState
),
1197 VMSTATE_UINT8(admaerr
, SDHCIState
),
1198 VMSTATE_UINT16(norintsts
, SDHCIState
),
1199 VMSTATE_UINT16(errintsts
, SDHCIState
),
1200 VMSTATE_UINT16(norintstsen
, SDHCIState
),
1201 VMSTATE_UINT16(errintstsen
, SDHCIState
),
1202 VMSTATE_UINT16(norintsigen
, SDHCIState
),
1203 VMSTATE_UINT16(errintsigen
, SDHCIState
),
1204 VMSTATE_UINT16(acmd12errsts
, SDHCIState
),
1205 VMSTATE_UINT16(data_count
, SDHCIState
),
1206 VMSTATE_UINT64(admasysaddr
, SDHCIState
),
1207 VMSTATE_UINT8(stopped_state
, SDHCIState
),
1208 VMSTATE_VBUFFER_UINT32(fifo_buffer
, SDHCIState
, 1, NULL
, 0, buf_maxsz
),
1209 VMSTATE_TIMER_PTR(insert_timer
, SDHCIState
),
1210 VMSTATE_TIMER_PTR(transfer_timer
, SDHCIState
),
1211 VMSTATE_END_OF_LIST()
1215 /* Capabilities registers provide information on supported features of this
1216 * specific host controller implementation */
1217 static Property sdhci_properties
[] = {
1218 DEFINE_PROP_UINT32("capareg", SDHCIState
, capareg
,
1219 SDHC_CAPAB_REG_DEFAULT
),
1220 DEFINE_PROP_UINT32("maxcurr", SDHCIState
, maxcurr
, 0),
1221 DEFINE_PROP_END_OF_LIST(),
1224 static void sdhci_pci_realize(PCIDevice
*dev
, Error
**errp
)
1226 SDHCIState
*s
= PCI_SDHCI(dev
);
1227 dev
->config
[PCI_CLASS_PROG
] = 0x01; /* Standard Host supported DMA */
1228 dev
->config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin A */
1230 s
->buf_maxsz
= sdhci_get_fifolen(s
);
1231 s
->fifo_buffer
= g_malloc0(s
->buf_maxsz
);
1232 s
->irq
= pci_allocate_irq(dev
);
1233 memory_region_init_io(&s
->iomem
, OBJECT(s
), &sdhci_mmio_ops
, s
, "sdhci",
1234 SDHC_REGISTERS_MAP_SIZE
);
1235 pci_register_bar(dev
, 0, 0, &s
->iomem
);
1238 static void sdhci_pci_exit(PCIDevice
*dev
)
1240 SDHCIState
*s
= PCI_SDHCI(dev
);
1244 static void sdhci_pci_class_init(ObjectClass
*klass
, void *data
)
1246 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1247 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1249 k
->realize
= sdhci_pci_realize
;
1250 k
->exit
= sdhci_pci_exit
;
1251 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
1252 k
->device_id
= PCI_DEVICE_ID_REDHAT_SDHCI
;
1253 k
->class_id
= PCI_CLASS_SYSTEM_SDHCI
;
1254 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1255 dc
->vmsd
= &sdhci_vmstate
;
1256 dc
->props
= sdhci_properties
;
1257 /* Reason: realize() method uses drive_get_next() */
1258 dc
->cannot_instantiate_with_device_add_yet
= true;
1261 static const TypeInfo sdhci_pci_info
= {
1262 .name
= TYPE_PCI_SDHCI
,
1263 .parent
= TYPE_PCI_DEVICE
,
1264 .instance_size
= sizeof(SDHCIState
),
1265 .class_init
= sdhci_pci_class_init
,
1268 static void sdhci_sysbus_init(Object
*obj
)
1270 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1274 static void sdhci_sysbus_finalize(Object
*obj
)
1276 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1280 static void sdhci_sysbus_realize(DeviceState
*dev
, Error
** errp
)
1282 SDHCIState
*s
= SYSBUS_SDHCI(dev
);
1283 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1285 s
->buf_maxsz
= sdhci_get_fifolen(s
);
1286 s
->fifo_buffer
= g_malloc0(s
->buf_maxsz
);
1287 sysbus_init_irq(sbd
, &s
->irq
);
1288 memory_region_init_io(&s
->iomem
, OBJECT(s
), &sdhci_mmio_ops
, s
, "sdhci",
1289 SDHC_REGISTERS_MAP_SIZE
);
1290 sysbus_init_mmio(sbd
, &s
->iomem
);
1293 static void sdhci_sysbus_class_init(ObjectClass
*klass
, void *data
)
1295 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1297 dc
->vmsd
= &sdhci_vmstate
;
1298 dc
->props
= sdhci_properties
;
1299 dc
->realize
= sdhci_sysbus_realize
;
1300 /* Reason: instance_init() method uses drive_get_next() */
1301 dc
->cannot_instantiate_with_device_add_yet
= true;
1304 static const TypeInfo sdhci_sysbus_info
= {
1305 .name
= TYPE_SYSBUS_SDHCI
,
1306 .parent
= TYPE_SYS_BUS_DEVICE
,
1307 .instance_size
= sizeof(SDHCIState
),
1308 .instance_init
= sdhci_sysbus_init
,
1309 .instance_finalize
= sdhci_sysbus_finalize
,
1310 .class_init
= sdhci_sysbus_class_init
,
1313 static void sdhci_register_types(void)
1315 type_register_static(&sdhci_pci_info
);
1316 type_register_static(&sdhci_sysbus_info
);
1319 type_init(sdhci_register_types
)