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/>.
25 #include "qemu/osdep.h"
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 #define TYPE_SDHCI_BUS "sdhci-bus"
59 #define SDHCI_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_SDHCI_BUS)
61 /* Default SD/MMC host controller features information, which will be
62 * presented in CAPABILITIES register of generic SD host controller at reset.
63 * If not stated otherwise:
64 * 0 - not supported, 1 - supported, other - prohibited.
66 #define SDHC_CAPAB_64BITBUS 0ul /* 64-bit System Bus Support */
67 #define SDHC_CAPAB_18V 1ul /* Voltage support 1.8v */
68 #define SDHC_CAPAB_30V 0ul /* Voltage support 3.0v */
69 #define SDHC_CAPAB_33V 1ul /* Voltage support 3.3v */
70 #define SDHC_CAPAB_SUSPRESUME 0ul /* Suspend/resume support */
71 #define SDHC_CAPAB_SDMA 1ul /* SDMA support */
72 #define SDHC_CAPAB_HIGHSPEED 1ul /* High speed support */
73 #define SDHC_CAPAB_ADMA1 1ul /* ADMA1 support */
74 #define SDHC_CAPAB_ADMA2 1ul /* ADMA2 support */
75 /* Maximum host controller R/W buffers size
76 * Possible values: 512, 1024, 2048 bytes */
77 #define SDHC_CAPAB_MAXBLOCKLENGTH 512ul
78 /* Maximum clock frequency for SDclock in MHz
79 * value in range 10-63 MHz, 0 - not defined */
80 #define SDHC_CAPAB_BASECLKFREQ 52ul
81 #define SDHC_CAPAB_TOUNIT 1ul /* Timeout clock unit 0 - kHz, 1 - MHz */
82 /* Timeout clock frequency 1-63, 0 - not defined */
83 #define SDHC_CAPAB_TOCLKFREQ 52ul
85 /* Now check all parameters and calculate CAPABILITIES REGISTER value */
86 #if SDHC_CAPAB_64BITBUS > 1 || SDHC_CAPAB_18V > 1 || SDHC_CAPAB_30V > 1 || \
87 SDHC_CAPAB_33V > 1 || SDHC_CAPAB_SUSPRESUME > 1 || SDHC_CAPAB_SDMA > 1 || \
88 SDHC_CAPAB_HIGHSPEED > 1 || SDHC_CAPAB_ADMA2 > 1 || SDHC_CAPAB_ADMA1 > 1 ||\
90 #error Capabilities features can have value 0 or 1 only!
93 #if SDHC_CAPAB_MAXBLOCKLENGTH == 512
94 #define MAX_BLOCK_LENGTH 0ul
95 #elif SDHC_CAPAB_MAXBLOCKLENGTH == 1024
96 #define MAX_BLOCK_LENGTH 1ul
97 #elif SDHC_CAPAB_MAXBLOCKLENGTH == 2048
98 #define MAX_BLOCK_LENGTH 2ul
100 #error Max host controller block size can have value 512, 1024 or 2048 only!
103 #if (SDHC_CAPAB_BASECLKFREQ > 0 && SDHC_CAPAB_BASECLKFREQ < 10) || \
104 SDHC_CAPAB_BASECLKFREQ > 63
105 #error SDclock frequency can have value in range 0, 10-63 only!
108 #if SDHC_CAPAB_TOCLKFREQ > 63
109 #error Timeout clock frequency can have value in range 0-63 only!
112 #define SDHC_CAPAB_REG_DEFAULT \
113 ((SDHC_CAPAB_64BITBUS << 28) | (SDHC_CAPAB_18V << 26) | \
114 (SDHC_CAPAB_30V << 25) | (SDHC_CAPAB_33V << 24) | \
115 (SDHC_CAPAB_SUSPRESUME << 23) | (SDHC_CAPAB_SDMA << 22) | \
116 (SDHC_CAPAB_HIGHSPEED << 21) | (SDHC_CAPAB_ADMA1 << 20) | \
117 (SDHC_CAPAB_ADMA2 << 19) | (MAX_BLOCK_LENGTH << 16) | \
118 (SDHC_CAPAB_BASECLKFREQ << 8) | (SDHC_CAPAB_TOUNIT << 7) | \
119 (SDHC_CAPAB_TOCLKFREQ))
121 #define MASKED_WRITE(reg, mask, val) (reg = (reg & (mask)) | (val))
123 static uint8_t sdhci_slotint(SDHCIState
*s
)
125 return (s
->norintsts
& s
->norintsigen
) || (s
->errintsts
& s
->errintsigen
) ||
126 ((s
->norintsts
& SDHC_NIS_INSERT
) && (s
->wakcon
& SDHC_WKUP_ON_INS
)) ||
127 ((s
->norintsts
& SDHC_NIS_REMOVE
) && (s
->wakcon
& SDHC_WKUP_ON_RMV
));
130 static inline void sdhci_update_irq(SDHCIState
*s
)
132 qemu_set_irq(s
->irq
, sdhci_slotint(s
));
135 static void sdhci_raise_insertion_irq(void *opaque
)
137 SDHCIState
*s
= (SDHCIState
*)opaque
;
139 if (s
->norintsts
& SDHC_NIS_REMOVE
) {
140 timer_mod(s
->insert_timer
,
141 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + SDHC_INSERTION_DELAY
);
143 s
->prnsts
= 0x1ff0000;
144 if (s
->norintstsen
& SDHC_NISEN_INSERT
) {
145 s
->norintsts
|= SDHC_NIS_INSERT
;
151 static void sdhci_set_inserted(DeviceState
*dev
, bool level
)
153 SDHCIState
*s
= (SDHCIState
*)dev
;
154 DPRINT_L1("Card state changed: %s!\n", level
? "insert" : "eject");
156 if ((s
->norintsts
& SDHC_NIS_REMOVE
) && level
) {
157 /* Give target some time to notice card ejection */
158 timer_mod(s
->insert_timer
,
159 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + SDHC_INSERTION_DELAY
);
162 s
->prnsts
= 0x1ff0000;
163 if (s
->norintstsen
& SDHC_NISEN_INSERT
) {
164 s
->norintsts
|= SDHC_NIS_INSERT
;
167 s
->prnsts
= 0x1fa0000;
168 s
->pwrcon
&= ~SDHC_POWER_ON
;
169 s
->clkcon
&= ~SDHC_CLOCK_SDCLK_EN
;
170 if (s
->norintstsen
& SDHC_NISEN_REMOVE
) {
171 s
->norintsts
|= SDHC_NIS_REMOVE
;
178 static void sdhci_set_readonly(DeviceState
*dev
, bool level
)
180 SDHCIState
*s
= (SDHCIState
*)dev
;
183 s
->prnsts
&= ~SDHC_WRITE_PROTECT
;
186 s
->prnsts
|= SDHC_WRITE_PROTECT
;
190 static void sdhci_reset(SDHCIState
*s
)
192 DeviceState
*dev
= DEVICE(s
);
194 timer_del(s
->insert_timer
);
195 timer_del(s
->transfer_timer
);
196 /* Set all registers to 0. Capabilities registers are not cleared
197 * and assumed to always preserve their value, given to them during
199 memset(&s
->sdmasysad
, 0, (uintptr_t)&s
->capareg
- (uintptr_t)&s
->sdmasysad
);
201 /* Reset other state based on current card insertion/readonly status */
202 sdhci_set_inserted(dev
, sdbus_get_inserted(&s
->sdbus
));
203 sdhci_set_readonly(dev
, sdbus_get_readonly(&s
->sdbus
));
206 s
->stopped_state
= sdhc_not_stopped
;
207 s
->pending_insert_state
= false;
210 static void sdhci_poweron_reset(DeviceState
*dev
)
212 /* QOM (ie power-on) reset. This is identical to reset
213 * commanded via device register apart from handling of the
214 * 'pending insert on powerup' quirk.
216 SDHCIState
*s
= (SDHCIState
*)dev
;
220 if (s
->pending_insert_quirk
) {
221 s
->pending_insert_state
= true;
225 static void sdhci_data_transfer(void *opaque
);
227 static void sdhci_send_command(SDHCIState
*s
)
230 uint8_t response
[16];
235 request
.cmd
= s
->cmdreg
>> 8;
236 request
.arg
= s
->argument
;
237 DPRINT_L1("sending CMD%u ARG[0x%08x]\n", request
.cmd
, request
.arg
);
238 rlen
= sdbus_do_command(&s
->sdbus
, &request
, response
);
240 if (s
->cmdreg
& SDHC_CMD_RESPONSE
) {
242 s
->rspreg
[0] = (response
[0] << 24) | (response
[1] << 16) |
243 (response
[2] << 8) | response
[3];
244 s
->rspreg
[1] = s
->rspreg
[2] = s
->rspreg
[3] = 0;
245 DPRINT_L1("Response: RSPREG[31..0]=0x%08x\n", s
->rspreg
[0]);
246 } else if (rlen
== 16) {
247 s
->rspreg
[0] = (response
[11] << 24) | (response
[12] << 16) |
248 (response
[13] << 8) | response
[14];
249 s
->rspreg
[1] = (response
[7] << 24) | (response
[8] << 16) |
250 (response
[9] << 8) | response
[10];
251 s
->rspreg
[2] = (response
[3] << 24) | (response
[4] << 16) |
252 (response
[5] << 8) | response
[6];
253 s
->rspreg
[3] = (response
[0] << 16) | (response
[1] << 8) |
255 DPRINT_L1("Response received:\n RSPREG[127..96]=0x%08x, RSPREG[95.."
256 "64]=0x%08x,\n RSPREG[63..32]=0x%08x, RSPREG[31..0]=0x%08x\n",
257 s
->rspreg
[3], s
->rspreg
[2], s
->rspreg
[1], s
->rspreg
[0]);
259 ERRPRINT("Timeout waiting for command response\n");
260 if (s
->errintstsen
& SDHC_EISEN_CMDTIMEOUT
) {
261 s
->errintsts
|= SDHC_EIS_CMDTIMEOUT
;
262 s
->norintsts
|= SDHC_NIS_ERR
;
266 if ((s
->norintstsen
& SDHC_NISEN_TRSCMP
) &&
267 (s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
) {
268 s
->norintsts
|= SDHC_NIS_TRSCMP
;
272 if (s
->norintstsen
& SDHC_NISEN_CMDCMP
) {
273 s
->norintsts
|= SDHC_NIS_CMDCMP
;
278 if (s
->blksize
&& (s
->cmdreg
& SDHC_CMD_DATA_PRESENT
)) {
280 sdhci_data_transfer(s
);
284 static void sdhci_end_transfer(SDHCIState
*s
)
286 /* Automatically send CMD12 to stop transfer if AutoCMD12 enabled */
287 if ((s
->trnmod
& SDHC_TRNS_ACMD12
) != 0) {
289 uint8_t response
[16];
293 DPRINT_L1("Automatically issue CMD%d %08x\n", request
.cmd
, request
.arg
);
294 sdbus_do_command(&s
->sdbus
, &request
, response
);
295 /* Auto CMD12 response goes to the upper Response register */
296 s
->rspreg
[3] = (response
[0] << 24) | (response
[1] << 16) |
297 (response
[2] << 8) | response
[3];
300 s
->prnsts
&= ~(SDHC_DOING_READ
| SDHC_DOING_WRITE
|
301 SDHC_DAT_LINE_ACTIVE
| SDHC_DATA_INHIBIT
|
302 SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
);
304 if (s
->norintstsen
& SDHC_NISEN_TRSCMP
) {
305 s
->norintsts
|= SDHC_NIS_TRSCMP
;
312 * Programmed i/o data transfer
315 /* Fill host controller's read buffer with BLKSIZE bytes of data from card */
316 static void sdhci_read_block_from_card(SDHCIState
*s
)
320 if ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
321 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) {
325 for (index
= 0; index
< (s
->blksize
& 0x0fff); index
++) {
326 s
->fifo_buffer
[index
] = sdbus_read_data(&s
->sdbus
);
329 /* New data now available for READ through Buffer Port Register */
330 s
->prnsts
|= SDHC_DATA_AVAILABLE
;
331 if (s
->norintstsen
& SDHC_NISEN_RBUFRDY
) {
332 s
->norintsts
|= SDHC_NIS_RBUFRDY
;
335 /* Clear DAT line active status if that was the last block */
336 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
337 ((s
->trnmod
& SDHC_TRNS_MULTI
) && s
->blkcnt
== 1)) {
338 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
341 /* If stop at block gap request was set and it's not the last block of
342 * data - generate Block Event interrupt */
343 if (s
->stopped_state
== sdhc_gap_read
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
345 s
->prnsts
&= ~SDHC_DAT_LINE_ACTIVE
;
346 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
347 s
->norintsts
|= SDHC_EIS_BLKGAP
;
354 /* Read @size byte of data from host controller @s BUFFER DATA PORT register */
355 static uint32_t sdhci_read_dataport(SDHCIState
*s
, unsigned size
)
360 /* first check that a valid data exists in host controller input buffer */
361 if ((s
->prnsts
& SDHC_DATA_AVAILABLE
) == 0) {
362 ERRPRINT("Trying to read from empty buffer\n");
366 for (i
= 0; i
< size
; i
++) {
367 value
|= s
->fifo_buffer
[s
->data_count
] << i
* 8;
369 /* check if we've read all valid data (blksize bytes) from buffer */
370 if ((s
->data_count
) >= (s
->blksize
& 0x0fff)) {
371 DPRINT_L2("All %u bytes of data have been read from input buffer\n",
373 s
->prnsts
&= ~SDHC_DATA_AVAILABLE
; /* no more data in a buffer */
374 s
->data_count
= 0; /* next buff read must start at position [0] */
376 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
380 /* if that was the last block of data */
381 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
382 ((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0)) ||
383 /* stop at gap request */
384 (s
->stopped_state
== sdhc_gap_read
&&
385 !(s
->prnsts
& SDHC_DAT_LINE_ACTIVE
))) {
386 sdhci_end_transfer(s
);
387 } else { /* if there are more data, read next block from card */
388 sdhci_read_block_from_card(s
);
397 /* Write data from host controller FIFO to card */
398 static void sdhci_write_block_to_card(SDHCIState
*s
)
402 if (s
->prnsts
& SDHC_SPACE_AVAILABLE
) {
403 if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
404 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
410 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
411 if (s
->blkcnt
== 0) {
418 for (index
= 0; index
< (s
->blksize
& 0x0fff); index
++) {
419 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
[index
]);
422 /* Next data can be written through BUFFER DATORT register */
423 s
->prnsts
|= SDHC_SPACE_AVAILABLE
;
425 /* Finish transfer if that was the last block of data */
426 if ((s
->trnmod
& SDHC_TRNS_MULTI
) == 0 ||
427 ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
428 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) && (s
->blkcnt
== 0))) {
429 sdhci_end_transfer(s
);
430 } else if (s
->norintstsen
& SDHC_NISEN_WBUFRDY
) {
431 s
->norintsts
|= SDHC_NIS_WBUFRDY
;
434 /* Generate Block Gap Event if requested and if not the last block */
435 if (s
->stopped_state
== sdhc_gap_write
&& (s
->trnmod
& SDHC_TRNS_MULTI
) &&
437 s
->prnsts
&= ~SDHC_DOING_WRITE
;
438 if (s
->norintstsen
& SDHC_EISEN_BLKGAP
) {
439 s
->norintsts
|= SDHC_EIS_BLKGAP
;
441 sdhci_end_transfer(s
);
447 /* Write @size bytes of @value data to host controller @s Buffer Data Port
449 static void sdhci_write_dataport(SDHCIState
*s
, uint32_t value
, unsigned size
)
453 /* Check that there is free space left in a buffer */
454 if (!(s
->prnsts
& SDHC_SPACE_AVAILABLE
)) {
455 ERRPRINT("Can't write to data buffer: buffer full\n");
459 for (i
= 0; i
< size
; i
++) {
460 s
->fifo_buffer
[s
->data_count
] = value
& 0xFF;
463 if (s
->data_count
>= (s
->blksize
& 0x0fff)) {
464 DPRINT_L2("write buffer filled with %u bytes of data\n",
467 s
->prnsts
&= ~SDHC_SPACE_AVAILABLE
;
468 if (s
->prnsts
& SDHC_DOING_WRITE
) {
469 sdhci_write_block_to_card(s
);
476 * Single DMA data transfer
479 /* Multi block SDMA transfer */
480 static void sdhci_sdma_transfer_multi_blocks(SDHCIState
*s
)
482 bool page_aligned
= false;
483 unsigned int n
, begin
;
484 const uint16_t block_size
= s
->blksize
& 0x0fff;
485 uint32_t boundary_chk
= 1 << (((s
->blksize
& 0xf000) >> 12) + 12);
486 uint32_t boundary_count
= boundary_chk
- (s
->sdmasysad
% boundary_chk
);
488 /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
489 * possible stop at page boundary if initial address is not page aligned,
490 * allow them to work properly */
491 if ((s
->sdmasysad
% boundary_chk
) == 0) {
495 if (s
->trnmod
& SDHC_TRNS_READ
) {
496 s
->prnsts
|= SDHC_DOING_READ
| SDHC_DATA_INHIBIT
|
497 SDHC_DAT_LINE_ACTIVE
;
499 if (s
->data_count
== 0) {
500 for (n
= 0; n
< block_size
; n
++) {
501 s
->fifo_buffer
[n
] = sdbus_read_data(&s
->sdbus
);
504 begin
= s
->data_count
;
505 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
506 s
->data_count
= boundary_count
+ begin
;
509 s
->data_count
= block_size
;
510 boundary_count
-= block_size
- begin
;
511 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
515 dma_memory_write(&address_space_memory
, s
->sdmasysad
,
516 &s
->fifo_buffer
[begin
], s
->data_count
- begin
);
517 s
->sdmasysad
+= s
->data_count
- begin
;
518 if (s
->data_count
== block_size
) {
521 if (page_aligned
&& boundary_count
== 0) {
526 s
->prnsts
|= SDHC_DOING_WRITE
| SDHC_DATA_INHIBIT
|
527 SDHC_DAT_LINE_ACTIVE
;
529 begin
= s
->data_count
;
530 if (((boundary_count
+ begin
) < block_size
) && page_aligned
) {
531 s
->data_count
= boundary_count
+ begin
;
534 s
->data_count
= block_size
;
535 boundary_count
-= block_size
- begin
;
537 dma_memory_read(&address_space_memory
, s
->sdmasysad
,
538 &s
->fifo_buffer
[begin
], s
->data_count
);
539 s
->sdmasysad
+= s
->data_count
- begin
;
540 if (s
->data_count
== block_size
) {
541 for (n
= 0; n
< block_size
; n
++) {
542 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
[n
]);
545 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
549 if (page_aligned
&& boundary_count
== 0) {
555 if (s
->blkcnt
== 0) {
556 sdhci_end_transfer(s
);
558 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
559 s
->norintsts
|= SDHC_NIS_DMA
;
565 /* single block SDMA transfer */
567 static void sdhci_sdma_transfer_single_block(SDHCIState
*s
)
570 uint32_t datacnt
= s
->blksize
& 0x0fff;
572 if (s
->trnmod
& SDHC_TRNS_READ
) {
573 for (n
= 0; n
< datacnt
; n
++) {
574 s
->fifo_buffer
[n
] = sdbus_read_data(&s
->sdbus
);
576 dma_memory_write(&address_space_memory
, s
->sdmasysad
, s
->fifo_buffer
,
579 dma_memory_read(&address_space_memory
, s
->sdmasysad
, s
->fifo_buffer
,
581 for (n
= 0; n
< datacnt
; n
++) {
582 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
[n
]);
586 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
590 sdhci_end_transfer(s
);
593 typedef struct ADMADescr
{
600 static void get_adma_description(SDHCIState
*s
, ADMADescr
*dscr
)
604 hwaddr entry_addr
= (hwaddr
)s
->admasysaddr
;
605 switch (SDHC_DMA_TYPE(s
->hostctl
)) {
606 case SDHC_CTRL_ADMA2_32
:
607 dma_memory_read(&address_space_memory
, entry_addr
, (uint8_t *)&adma2
,
609 adma2
= le64_to_cpu(adma2
);
610 /* The spec does not specify endianness of descriptor table.
611 * We currently assume that it is LE.
613 dscr
->addr
= (hwaddr
)extract64(adma2
, 32, 32) & ~0x3ull
;
614 dscr
->length
= (uint16_t)extract64(adma2
, 16, 16);
615 dscr
->attr
= (uint8_t)extract64(adma2
, 0, 7);
618 case SDHC_CTRL_ADMA1_32
:
619 dma_memory_read(&address_space_memory
, entry_addr
, (uint8_t *)&adma1
,
621 adma1
= le32_to_cpu(adma1
);
622 dscr
->addr
= (hwaddr
)(adma1
& 0xFFFFF000);
623 dscr
->attr
= (uint8_t)extract32(adma1
, 0, 7);
625 if ((dscr
->attr
& SDHC_ADMA_ATTR_ACT_MASK
) == SDHC_ADMA_ATTR_SET_LEN
) {
626 dscr
->length
= (uint16_t)extract32(adma1
, 12, 16);
631 case SDHC_CTRL_ADMA2_64
:
632 dma_memory_read(&address_space_memory
, entry_addr
,
633 (uint8_t *)(&dscr
->attr
), 1);
634 dma_memory_read(&address_space_memory
, entry_addr
+ 2,
635 (uint8_t *)(&dscr
->length
), 2);
636 dscr
->length
= le16_to_cpu(dscr
->length
);
637 dma_memory_read(&address_space_memory
, entry_addr
+ 4,
638 (uint8_t *)(&dscr
->addr
), 8);
639 dscr
->attr
= le64_to_cpu(dscr
->attr
);
640 dscr
->attr
&= 0xfffffff8;
646 /* Advanced DMA data transfer */
648 static void sdhci_do_adma(SDHCIState
*s
)
650 unsigned int n
, begin
, length
;
651 const uint16_t block_size
= s
->blksize
& 0x0fff;
655 for (i
= 0; i
< SDHC_ADMA_DESCS_PER_DELAY
; ++i
) {
656 s
->admaerr
&= ~SDHC_ADMAERR_LENGTH_MISMATCH
;
658 get_adma_description(s
, &dscr
);
659 DPRINT_L2("ADMA loop: addr=" TARGET_FMT_plx
", len=%d, attr=%x\n",
660 dscr
.addr
, dscr
.length
, dscr
.attr
);
662 if ((dscr
.attr
& SDHC_ADMA_ATTR_VALID
) == 0) {
663 /* Indicate that error occurred in ST_FDS state */
664 s
->admaerr
&= ~SDHC_ADMAERR_STATE_MASK
;
665 s
->admaerr
|= SDHC_ADMAERR_STATE_ST_FDS
;
667 /* Generate ADMA error interrupt */
668 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
669 s
->errintsts
|= SDHC_EIS_ADMAERR
;
670 s
->norintsts
|= SDHC_NIS_ERR
;
677 length
= dscr
.length
? dscr
.length
: 65536;
679 switch (dscr
.attr
& SDHC_ADMA_ATTR_ACT_MASK
) {
680 case SDHC_ADMA_ATTR_ACT_TRAN
: /* data transfer */
682 if (s
->trnmod
& SDHC_TRNS_READ
) {
684 if (s
->data_count
== 0) {
685 for (n
= 0; n
< block_size
; n
++) {
686 s
->fifo_buffer
[n
] = sdbus_read_data(&s
->sdbus
);
689 begin
= s
->data_count
;
690 if ((length
+ begin
) < block_size
) {
691 s
->data_count
= length
+ begin
;
694 s
->data_count
= block_size
;
695 length
-= block_size
- begin
;
697 dma_memory_write(&address_space_memory
, dscr
.addr
,
698 &s
->fifo_buffer
[begin
],
699 s
->data_count
- begin
);
700 dscr
.addr
+= s
->data_count
- begin
;
701 if (s
->data_count
== block_size
) {
703 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
705 if (s
->blkcnt
== 0) {
713 begin
= s
->data_count
;
714 if ((length
+ begin
) < block_size
) {
715 s
->data_count
= length
+ begin
;
718 s
->data_count
= block_size
;
719 length
-= block_size
- begin
;
721 dma_memory_read(&address_space_memory
, dscr
.addr
,
722 &s
->fifo_buffer
[begin
],
723 s
->data_count
- begin
);
724 dscr
.addr
+= s
->data_count
- begin
;
725 if (s
->data_count
== block_size
) {
726 for (n
= 0; n
< block_size
; n
++) {
727 sdbus_write_data(&s
->sdbus
, s
->fifo_buffer
[n
]);
730 if (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) {
732 if (s
->blkcnt
== 0) {
739 s
->admasysaddr
+= dscr
.incr
;
741 case SDHC_ADMA_ATTR_ACT_LINK
: /* link to next descriptor table */
742 s
->admasysaddr
= dscr
.addr
;
743 DPRINT_L1("ADMA link: admasysaddr=0x%" PRIx64
"\n",
747 s
->admasysaddr
+= dscr
.incr
;
751 if (dscr
.attr
& SDHC_ADMA_ATTR_INT
) {
752 DPRINT_L1("ADMA interrupt: admasysaddr=0x%" PRIx64
"\n",
754 if (s
->norintstsen
& SDHC_NISEN_DMA
) {
755 s
->norintsts
|= SDHC_NIS_DMA
;
761 /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
762 if (((s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
763 (s
->blkcnt
== 0)) || (dscr
.attr
& SDHC_ADMA_ATTR_END
)) {
764 DPRINT_L2("ADMA transfer completed\n");
765 if (length
|| ((dscr
.attr
& SDHC_ADMA_ATTR_END
) &&
766 (s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) &&
768 ERRPRINT("SD/MMC host ADMA length mismatch\n");
769 s
->admaerr
|= SDHC_ADMAERR_LENGTH_MISMATCH
|
770 SDHC_ADMAERR_STATE_ST_TFR
;
771 if (s
->errintstsen
& SDHC_EISEN_ADMAERR
) {
772 ERRPRINT("Set ADMA error flag\n");
773 s
->errintsts
|= SDHC_EIS_ADMAERR
;
774 s
->norintsts
|= SDHC_NIS_ERR
;
779 sdhci_end_transfer(s
);
785 /* we have unfinished business - reschedule to continue ADMA */
786 timer_mod(s
->transfer_timer
,
787 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + SDHC_TRANSFER_DELAY
);
790 /* Perform data transfer according to controller configuration */
792 static void sdhci_data_transfer(void *opaque
)
794 SDHCIState
*s
= (SDHCIState
*)opaque
;
796 if (s
->trnmod
& SDHC_TRNS_DMA
) {
797 switch (SDHC_DMA_TYPE(s
->hostctl
)) {
799 if ((s
->trnmod
& SDHC_TRNS_MULTI
) &&
800 (!(s
->trnmod
& SDHC_TRNS_BLK_CNT_EN
) || s
->blkcnt
== 0)) {
804 if ((s
->blkcnt
== 1) || !(s
->trnmod
& SDHC_TRNS_MULTI
)) {
805 sdhci_sdma_transfer_single_block(s
);
807 sdhci_sdma_transfer_multi_blocks(s
);
811 case SDHC_CTRL_ADMA1_32
:
812 if (!(s
->capareg
& SDHC_CAN_DO_ADMA1
)) {
813 ERRPRINT("ADMA1 not supported\n");
819 case SDHC_CTRL_ADMA2_32
:
820 if (!(s
->capareg
& SDHC_CAN_DO_ADMA2
)) {
821 ERRPRINT("ADMA2 not supported\n");
827 case SDHC_CTRL_ADMA2_64
:
828 if (!(s
->capareg
& SDHC_CAN_DO_ADMA2
) ||
829 !(s
->capareg
& SDHC_64_BIT_BUS_SUPPORT
)) {
830 ERRPRINT("64 bit ADMA not supported\n");
837 ERRPRINT("Unsupported DMA type\n");
841 if ((s
->trnmod
& SDHC_TRNS_READ
) && sdbus_data_ready(&s
->sdbus
)) {
842 s
->prnsts
|= SDHC_DOING_READ
| SDHC_DATA_INHIBIT
|
843 SDHC_DAT_LINE_ACTIVE
;
844 sdhci_read_block_from_card(s
);
846 s
->prnsts
|= SDHC_DOING_WRITE
| SDHC_DAT_LINE_ACTIVE
|
847 SDHC_SPACE_AVAILABLE
| SDHC_DATA_INHIBIT
;
848 sdhci_write_block_to_card(s
);
853 static bool sdhci_can_issue_command(SDHCIState
*s
)
855 if (!SDHC_CLOCK_IS_ON(s
->clkcon
) ||
856 (((s
->prnsts
& SDHC_DATA_INHIBIT
) || s
->stopped_state
) &&
857 ((s
->cmdreg
& SDHC_CMD_DATA_PRESENT
) ||
858 ((s
->cmdreg
& SDHC_CMD_RESPONSE
) == SDHC_CMD_RSP_WITH_BUSY
&&
859 !(SDHC_COMMAND_TYPE(s
->cmdreg
) == SDHC_CMD_ABORT
))))) {
866 /* The Buffer Data Port register must be accessed in sequential and
867 * continuous manner */
869 sdhci_buff_access_is_sequential(SDHCIState
*s
, unsigned byte_num
)
871 if ((s
->data_count
& 0x3) != byte_num
) {
872 ERRPRINT("Non-sequential access to Buffer Data Port register"
879 static uint64_t sdhci_read(void *opaque
, hwaddr offset
, unsigned size
)
881 SDHCIState
*s
= (SDHCIState
*)opaque
;
884 switch (offset
& ~0x3) {
889 ret
= s
->blksize
| (s
->blkcnt
<< 16);
895 ret
= s
->trnmod
| (s
->cmdreg
<< 16);
897 case SDHC_RSPREG0
... SDHC_RSPREG3
:
898 ret
= s
->rspreg
[((offset
& ~0x3) - SDHC_RSPREG0
) >> 2];
901 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
902 ret
= sdhci_read_dataport(s
, size
);
903 DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size
, (int)offset
,
912 ret
= s
->hostctl
| (s
->pwrcon
<< 8) | (s
->blkgap
<< 16) |
916 ret
= s
->clkcon
| (s
->timeoutcon
<< 16);
919 ret
= s
->norintsts
| (s
->errintsts
<< 16);
921 case SDHC_NORINTSTSEN
:
922 ret
= s
->norintstsen
| (s
->errintstsen
<< 16);
924 case SDHC_NORINTSIGEN
:
925 ret
= s
->norintsigen
| (s
->errintsigen
<< 16);
927 case SDHC_ACMD12ERRSTS
:
928 ret
= s
->acmd12errsts
;
939 case SDHC_ADMASYSADDR
:
940 ret
= (uint32_t)s
->admasysaddr
;
942 case SDHC_ADMASYSADDR
+ 4:
943 ret
= (uint32_t)(s
->admasysaddr
>> 32);
945 case SDHC_SLOT_INT_STATUS
:
946 ret
= (SD_HOST_SPECv2_VERS
<< 16) | sdhci_slotint(s
);
949 ERRPRINT("bad %ub read: addr[0x%04x]\n", size
, (int)offset
);
953 ret
>>= (offset
& 0x3) * 8;
954 ret
&= (1ULL << (size
* 8)) - 1;
955 DPRINT_L2("read %ub: addr[0x%04x] -> %u(0x%x)\n", size
, (int)offset
, ret
, ret
);
959 static inline void sdhci_blkgap_write(SDHCIState
*s
, uint8_t value
)
961 if ((value
& SDHC_STOP_AT_GAP_REQ
) && (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
)) {
964 s
->blkgap
= value
& SDHC_STOP_AT_GAP_REQ
;
966 if ((value
& SDHC_CONTINUE_REQ
) && s
->stopped_state
&&
967 (s
->blkgap
& SDHC_STOP_AT_GAP_REQ
) == 0) {
968 if (s
->stopped_state
== sdhc_gap_read
) {
969 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_READ
;
970 sdhci_read_block_from_card(s
);
972 s
->prnsts
|= SDHC_DAT_LINE_ACTIVE
| SDHC_DOING_WRITE
;
973 sdhci_write_block_to_card(s
);
975 s
->stopped_state
= sdhc_not_stopped
;
976 } else if (!s
->stopped_state
&& (value
& SDHC_STOP_AT_GAP_REQ
)) {
977 if (s
->prnsts
& SDHC_DOING_READ
) {
978 s
->stopped_state
= sdhc_gap_read
;
979 } else if (s
->prnsts
& SDHC_DOING_WRITE
) {
980 s
->stopped_state
= sdhc_gap_write
;
985 static inline void sdhci_reset_write(SDHCIState
*s
, uint8_t value
)
992 s
->prnsts
&= ~SDHC_CMD_INHIBIT
;
993 s
->norintsts
&= ~SDHC_NIS_CMDCMP
;
995 case SDHC_RESET_DATA
:
997 s
->prnsts
&= ~(SDHC_SPACE_AVAILABLE
| SDHC_DATA_AVAILABLE
|
998 SDHC_DOING_READ
| SDHC_DOING_WRITE
|
999 SDHC_DATA_INHIBIT
| SDHC_DAT_LINE_ACTIVE
);
1000 s
->blkgap
&= ~(SDHC_STOP_AT_GAP_REQ
| SDHC_CONTINUE_REQ
);
1001 s
->stopped_state
= sdhc_not_stopped
;
1002 s
->norintsts
&= ~(SDHC_NIS_WBUFRDY
| SDHC_NIS_RBUFRDY
|
1003 SDHC_NIS_DMA
| SDHC_NIS_TRSCMP
| SDHC_NIS_BLKGAP
);
1009 sdhci_write(void *opaque
, hwaddr offset
, uint64_t val
, unsigned size
)
1011 SDHCIState
*s
= (SDHCIState
*)opaque
;
1012 unsigned shift
= 8 * (offset
& 0x3);
1013 uint32_t mask
= ~(((1ULL << (size
* 8)) - 1) << shift
);
1014 uint32_t value
= val
;
1017 switch (offset
& ~0x3) {
1019 s
->sdmasysad
= (s
->sdmasysad
& mask
) | value
;
1020 MASKED_WRITE(s
->sdmasysad
, mask
, value
);
1021 /* Writing to last byte of sdmasysad might trigger transfer */
1022 if (!(mask
& 0xFF000000) && TRANSFERRING_DATA(s
->prnsts
) && s
->blkcnt
&&
1023 s
->blksize
&& SDHC_DMA_TYPE(s
->hostctl
) == SDHC_CTRL_SDMA
) {
1024 sdhci_sdma_transfer_multi_blocks(s
);
1028 if (!TRANSFERRING_DATA(s
->prnsts
)) {
1029 MASKED_WRITE(s
->blksize
, mask
, value
);
1030 MASKED_WRITE(s
->blkcnt
, mask
>> 16, value
>> 16);
1033 /* Limit block size to the maximum buffer size */
1034 if (extract32(s
->blksize
, 0, 12) > s
->buf_maxsz
) {
1035 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Size 0x%x is larger than " \
1036 "the maximum buffer 0x%x", __func__
, s
->blksize
,
1039 s
->blksize
= deposit32(s
->blksize
, 0, 12, s
->buf_maxsz
);
1044 MASKED_WRITE(s
->argument
, mask
, value
);
1047 /* DMA can be enabled only if it is supported as indicated by
1048 * capabilities register */
1049 if (!(s
->capareg
& SDHC_CAN_DO_DMA
)) {
1050 value
&= ~SDHC_TRNS_DMA
;
1052 MASKED_WRITE(s
->trnmod
, mask
, value
);
1053 MASKED_WRITE(s
->cmdreg
, mask
>> 16, value
>> 16);
1055 /* Writing to the upper byte of CMDREG triggers SD command generation */
1056 if ((mask
& 0xFF000000) || !sdhci_can_issue_command(s
)) {
1060 sdhci_send_command(s
);
1063 if (sdhci_buff_access_is_sequential(s
, offset
- SDHC_BDATA
)) {
1064 sdhci_write_dataport(s
, value
>> shift
, size
);
1068 if (!(mask
& 0xFF0000)) {
1069 sdhci_blkgap_write(s
, value
>> 16);
1071 MASKED_WRITE(s
->hostctl
, mask
, value
);
1072 MASKED_WRITE(s
->pwrcon
, mask
>> 8, value
>> 8);
1073 MASKED_WRITE(s
->wakcon
, mask
>> 24, value
>> 24);
1074 if (!(s
->prnsts
& SDHC_CARD_PRESENT
) || ((s
->pwrcon
>> 1) & 0x7) < 5 ||
1075 !(s
->capareg
& (1 << (31 - ((s
->pwrcon
>> 1) & 0x7))))) {
1076 s
->pwrcon
&= ~SDHC_POWER_ON
;
1080 if (!(mask
& 0xFF000000)) {
1081 sdhci_reset_write(s
, value
>> 24);
1083 MASKED_WRITE(s
->clkcon
, mask
, value
);
1084 MASKED_WRITE(s
->timeoutcon
, mask
>> 16, value
>> 16);
1085 if (s
->clkcon
& SDHC_CLOCK_INT_EN
) {
1086 s
->clkcon
|= SDHC_CLOCK_INT_STABLE
;
1088 s
->clkcon
&= ~SDHC_CLOCK_INT_STABLE
;
1091 case SDHC_NORINTSTS
:
1092 if (s
->norintstsen
& SDHC_NISEN_CARDINT
) {
1093 value
&= ~SDHC_NIS_CARDINT
;
1095 s
->norintsts
&= mask
| ~value
;
1096 s
->errintsts
&= (mask
>> 16) | ~(value
>> 16);
1098 s
->norintsts
|= SDHC_NIS_ERR
;
1100 s
->norintsts
&= ~SDHC_NIS_ERR
;
1102 sdhci_update_irq(s
);
1104 case SDHC_NORINTSTSEN
:
1105 MASKED_WRITE(s
->norintstsen
, mask
, value
);
1106 MASKED_WRITE(s
->errintstsen
, mask
>> 16, value
>> 16);
1107 s
->norintsts
&= s
->norintstsen
;
1108 s
->errintsts
&= s
->errintstsen
;
1110 s
->norintsts
|= SDHC_NIS_ERR
;
1112 s
->norintsts
&= ~SDHC_NIS_ERR
;
1114 /* Quirk for Raspberry Pi: pending card insert interrupt
1115 * appears when first enabled after power on */
1116 if ((s
->norintstsen
& SDHC_NISEN_INSERT
) && s
->pending_insert_state
) {
1117 assert(s
->pending_insert_quirk
);
1118 s
->norintsts
|= SDHC_NIS_INSERT
;
1119 s
->pending_insert_state
= false;
1121 sdhci_update_irq(s
);
1123 case SDHC_NORINTSIGEN
:
1124 MASKED_WRITE(s
->norintsigen
, mask
, value
);
1125 MASKED_WRITE(s
->errintsigen
, mask
>> 16, value
>> 16);
1126 sdhci_update_irq(s
);
1129 MASKED_WRITE(s
->admaerr
, mask
, value
);
1131 case SDHC_ADMASYSADDR
:
1132 s
->admasysaddr
= (s
->admasysaddr
& (0xFFFFFFFF00000000ULL
|
1133 (uint64_t)mask
)) | (uint64_t)value
;
1135 case SDHC_ADMASYSADDR
+ 4:
1136 s
->admasysaddr
= (s
->admasysaddr
& (0x00000000FFFFFFFFULL
|
1137 ((uint64_t)mask
<< 32))) | ((uint64_t)value
<< 32);
1140 s
->acmd12errsts
|= value
;
1141 s
->errintsts
|= (value
>> 16) & s
->errintstsen
;
1142 if (s
->acmd12errsts
) {
1143 s
->errintsts
|= SDHC_EIS_CMD12ERR
;
1146 s
->norintsts
|= SDHC_NIS_ERR
;
1148 sdhci_update_irq(s
);
1151 ERRPRINT("bad %ub write offset: addr[0x%04x] <- %u(0x%x)\n",
1152 size
, (int)offset
, value
>> shift
, value
>> shift
);
1155 DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n",
1156 size
, (int)offset
, value
>> shift
, value
>> shift
);
1159 static const MemoryRegionOps sdhci_mmio_ops
= {
1161 .write
= sdhci_write
,
1163 .min_access_size
= 1,
1164 .max_access_size
= 4,
1167 .endianness
= DEVICE_LITTLE_ENDIAN
,
1170 static inline unsigned int sdhci_get_fifolen(SDHCIState
*s
)
1172 switch (SDHC_CAPAB_BLOCKSIZE(s
->capareg
)) {
1180 hw_error("SDHC: unsupported value for maximum block size\n");
1185 static void sdhci_initfn(SDHCIState
*s
)
1187 qbus_create_inplace(&s
->sdbus
, sizeof(s
->sdbus
),
1188 TYPE_SDHCI_BUS
, DEVICE(s
), "sd-bus");
1190 s
->insert_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sdhci_raise_insertion_irq
, s
);
1191 s
->transfer_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sdhci_data_transfer
, s
);
1194 static void sdhci_uninitfn(SDHCIState
*s
)
1196 timer_del(s
->insert_timer
);
1197 timer_free(s
->insert_timer
);
1198 timer_del(s
->transfer_timer
);
1199 timer_free(s
->transfer_timer
);
1200 qemu_free_irq(s
->eject_cb
);
1201 qemu_free_irq(s
->ro_cb
);
1203 g_free(s
->fifo_buffer
);
1204 s
->fifo_buffer
= NULL
;
1207 static bool sdhci_pending_insert_vmstate_needed(void *opaque
)
1209 SDHCIState
*s
= opaque
;
1211 return s
->pending_insert_state
;
1214 static const VMStateDescription sdhci_pending_insert_vmstate
= {
1215 .name
= "sdhci/pending-insert",
1217 .minimum_version_id
= 1,
1218 .needed
= sdhci_pending_insert_vmstate_needed
,
1219 .fields
= (VMStateField
[]) {
1220 VMSTATE_BOOL(pending_insert_state
, SDHCIState
),
1221 VMSTATE_END_OF_LIST()
1225 const VMStateDescription sdhci_vmstate
= {
1228 .minimum_version_id
= 1,
1229 .fields
= (VMStateField
[]) {
1230 VMSTATE_UINT32(sdmasysad
, SDHCIState
),
1231 VMSTATE_UINT16(blksize
, SDHCIState
),
1232 VMSTATE_UINT16(blkcnt
, SDHCIState
),
1233 VMSTATE_UINT32(argument
, SDHCIState
),
1234 VMSTATE_UINT16(trnmod
, SDHCIState
),
1235 VMSTATE_UINT16(cmdreg
, SDHCIState
),
1236 VMSTATE_UINT32_ARRAY(rspreg
, SDHCIState
, 4),
1237 VMSTATE_UINT32(prnsts
, SDHCIState
),
1238 VMSTATE_UINT8(hostctl
, SDHCIState
),
1239 VMSTATE_UINT8(pwrcon
, SDHCIState
),
1240 VMSTATE_UINT8(blkgap
, SDHCIState
),
1241 VMSTATE_UINT8(wakcon
, SDHCIState
),
1242 VMSTATE_UINT16(clkcon
, SDHCIState
),
1243 VMSTATE_UINT8(timeoutcon
, SDHCIState
),
1244 VMSTATE_UINT8(admaerr
, SDHCIState
),
1245 VMSTATE_UINT16(norintsts
, SDHCIState
),
1246 VMSTATE_UINT16(errintsts
, SDHCIState
),
1247 VMSTATE_UINT16(norintstsen
, SDHCIState
),
1248 VMSTATE_UINT16(errintstsen
, SDHCIState
),
1249 VMSTATE_UINT16(norintsigen
, SDHCIState
),
1250 VMSTATE_UINT16(errintsigen
, SDHCIState
),
1251 VMSTATE_UINT16(acmd12errsts
, SDHCIState
),
1252 VMSTATE_UINT16(data_count
, SDHCIState
),
1253 VMSTATE_UINT64(admasysaddr
, SDHCIState
),
1254 VMSTATE_UINT8(stopped_state
, SDHCIState
),
1255 VMSTATE_VBUFFER_UINT32(fifo_buffer
, SDHCIState
, 1, NULL
, 0, buf_maxsz
),
1256 VMSTATE_TIMER_PTR(insert_timer
, SDHCIState
),
1257 VMSTATE_TIMER_PTR(transfer_timer
, SDHCIState
),
1258 VMSTATE_END_OF_LIST()
1260 .subsections
= (const VMStateDescription
*[]) {
1261 &sdhci_pending_insert_vmstate
,
1266 /* Capabilities registers provide information on supported features of this
1267 * specific host controller implementation */
1268 static Property sdhci_pci_properties
[] = {
1269 DEFINE_PROP_UINT32("capareg", SDHCIState
, capareg
,
1270 SDHC_CAPAB_REG_DEFAULT
),
1271 DEFINE_PROP_UINT32("maxcurr", SDHCIState
, maxcurr
, 0),
1272 DEFINE_PROP_END_OF_LIST(),
1275 static void sdhci_pci_realize(PCIDevice
*dev
, Error
**errp
)
1277 SDHCIState
*s
= PCI_SDHCI(dev
);
1278 dev
->config
[PCI_CLASS_PROG
] = 0x01; /* Standard Host supported DMA */
1279 dev
->config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin A */
1281 s
->buf_maxsz
= sdhci_get_fifolen(s
);
1282 s
->fifo_buffer
= g_malloc0(s
->buf_maxsz
);
1283 s
->irq
= pci_allocate_irq(dev
);
1284 memory_region_init_io(&s
->iomem
, OBJECT(s
), &sdhci_mmio_ops
, s
, "sdhci",
1285 SDHC_REGISTERS_MAP_SIZE
);
1286 pci_register_bar(dev
, 0, 0, &s
->iomem
);
1289 static void sdhci_pci_exit(PCIDevice
*dev
)
1291 SDHCIState
*s
= PCI_SDHCI(dev
);
1295 static void sdhci_pci_class_init(ObjectClass
*klass
, void *data
)
1297 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1298 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1300 k
->realize
= sdhci_pci_realize
;
1301 k
->exit
= sdhci_pci_exit
;
1302 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
1303 k
->device_id
= PCI_DEVICE_ID_REDHAT_SDHCI
;
1304 k
->class_id
= PCI_CLASS_SYSTEM_SDHCI
;
1305 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1306 dc
->vmsd
= &sdhci_vmstate
;
1307 dc
->props
= sdhci_pci_properties
;
1308 dc
->reset
= sdhci_poweron_reset
;
1311 static const TypeInfo sdhci_pci_info
= {
1312 .name
= TYPE_PCI_SDHCI
,
1313 .parent
= TYPE_PCI_DEVICE
,
1314 .instance_size
= sizeof(SDHCIState
),
1315 .class_init
= sdhci_pci_class_init
,
1318 static Property sdhci_sysbus_properties
[] = {
1319 DEFINE_PROP_UINT32("capareg", SDHCIState
, capareg
,
1320 SDHC_CAPAB_REG_DEFAULT
),
1321 DEFINE_PROP_UINT32("maxcurr", SDHCIState
, maxcurr
, 0),
1322 DEFINE_PROP_BOOL("pending-insert-quirk", SDHCIState
, pending_insert_quirk
,
1324 DEFINE_PROP_END_OF_LIST(),
1327 static void sdhci_sysbus_init(Object
*obj
)
1329 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1334 static void sdhci_sysbus_finalize(Object
*obj
)
1336 SDHCIState
*s
= SYSBUS_SDHCI(obj
);
1340 static void sdhci_sysbus_realize(DeviceState
*dev
, Error
** errp
)
1342 SDHCIState
*s
= SYSBUS_SDHCI(dev
);
1343 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1345 s
->buf_maxsz
= sdhci_get_fifolen(s
);
1346 s
->fifo_buffer
= g_malloc0(s
->buf_maxsz
);
1347 sysbus_init_irq(sbd
, &s
->irq
);
1348 memory_region_init_io(&s
->iomem
, OBJECT(s
), &sdhci_mmio_ops
, s
, "sdhci",
1349 SDHC_REGISTERS_MAP_SIZE
);
1350 sysbus_init_mmio(sbd
, &s
->iomem
);
1353 static void sdhci_sysbus_class_init(ObjectClass
*klass
, void *data
)
1355 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1357 dc
->vmsd
= &sdhci_vmstate
;
1358 dc
->props
= sdhci_sysbus_properties
;
1359 dc
->realize
= sdhci_sysbus_realize
;
1360 dc
->reset
= sdhci_poweron_reset
;
1363 static const TypeInfo sdhci_sysbus_info
= {
1364 .name
= TYPE_SYSBUS_SDHCI
,
1365 .parent
= TYPE_SYS_BUS_DEVICE
,
1366 .instance_size
= sizeof(SDHCIState
),
1367 .instance_init
= sdhci_sysbus_init
,
1368 .instance_finalize
= sdhci_sysbus_finalize
,
1369 .class_init
= sdhci_sysbus_class_init
,
1372 static void sdhci_bus_class_init(ObjectClass
*klass
, void *data
)
1374 SDBusClass
*sbc
= SD_BUS_CLASS(klass
);
1376 sbc
->set_inserted
= sdhci_set_inserted
;
1377 sbc
->set_readonly
= sdhci_set_readonly
;
1380 static const TypeInfo sdhci_bus_info
= {
1381 .name
= TYPE_SDHCI_BUS
,
1382 .parent
= TYPE_SD_BUS
,
1383 .instance_size
= sizeof(SDBus
),
1384 .class_init
= sdhci_bus_class_init
,
1387 static void sdhci_register_types(void)
1389 type_register_static(&sdhci_pci_info
);
1390 type_register_static(&sdhci_sysbus_info
);
1391 type_register_static(&sdhci_bus_info
);
1394 type_init(sdhci_register_types
)