2 * Raspberry Pi (BCM2835) SD Host Controller
4 * Copyright (c) 2017 Antfield SAS
7 * Clement Deschamps <clement.deschamps@antfield.fr>
8 * Luc Michel <luc.michel@antfield.fr>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
16 #include "sysemu/blockdev.h"
17 #include "hw/sd/bcm2835_sdhost.h"
20 #define TYPE_BCM2835_SDHOST_BUS "bcm2835-sdhost-bus"
21 #define BCM2835_SDHOST_BUS(obj) \
22 OBJECT_CHECK(SDBus, (obj), TYPE_BCM2835_SDHOST_BUS)
24 #define SDCMD 0x00 /* Command to SD card - 16 R/W */
25 #define SDARG 0x04 /* Argument to SD card - 32 R/W */
26 #define SDTOUT 0x08 /* Start value for timeout counter - 32 R/W */
27 #define SDCDIV 0x0c /* Start value for clock divider - 11 R/W */
28 #define SDRSP0 0x10 /* SD card rsp (31:0) - 32 R */
29 #define SDRSP1 0x14 /* SD card rsp (63:32) - 32 R */
30 #define SDRSP2 0x18 /* SD card rsp (95:64) - 32 R */
31 #define SDRSP3 0x1c /* SD card rsp (127:96) - 32 R */
32 #define SDHSTS 0x20 /* SD host status - 11 R */
33 #define SDVDD 0x30 /* SD card power control - 1 R/W */
34 #define SDEDM 0x34 /* Emergency Debug Mode - 13 R/W */
35 #define SDHCFG 0x38 /* Host configuration - 2 R/W */
36 #define SDHBCT 0x3c /* Host byte count (debug) - 32 R/W */
37 #define SDDATA 0x40 /* Data to/from SD card - 32 R/W */
38 #define SDHBLC 0x50 /* Host block count (SDIO/SDHC) - 9 R/W */
40 #define SDCMD_NEW_FLAG 0x8000
41 #define SDCMD_FAIL_FLAG 0x4000
42 #define SDCMD_BUSYWAIT 0x800
43 #define SDCMD_NO_RESPONSE 0x400
44 #define SDCMD_LONG_RESPONSE 0x200
45 #define SDCMD_WRITE_CMD 0x80
46 #define SDCMD_READ_CMD 0x40
47 #define SDCMD_CMD_MASK 0x3f
49 #define SDCDIV_MAX_CDIV 0x7ff
51 #define SDHSTS_BUSY_IRPT 0x400
52 #define SDHSTS_BLOCK_IRPT 0x200
53 #define SDHSTS_SDIO_IRPT 0x100
54 #define SDHSTS_REW_TIME_OUT 0x80
55 #define SDHSTS_CMD_TIME_OUT 0x40
56 #define SDHSTS_CRC16_ERROR 0x20
57 #define SDHSTS_CRC7_ERROR 0x10
58 #define SDHSTS_FIFO_ERROR 0x08
61 #define SDHSTS_DATA_FLAG 0x01
63 #define SDHCFG_BUSY_IRPT_EN (1 << 10)
64 #define SDHCFG_BLOCK_IRPT_EN (1 << 8)
65 #define SDHCFG_SDIO_IRPT_EN (1 << 5)
66 #define SDHCFG_DATA_IRPT_EN (1 << 4)
67 #define SDHCFG_SLOW_CARD (1 << 3)
68 #define SDHCFG_WIDE_EXT_BUS (1 << 2)
69 #define SDHCFG_WIDE_INT_BUS (1 << 1)
70 #define SDHCFG_REL_CMD_LINE (1 << 0)
72 #define SDEDM_FORCE_DATA_MODE (1 << 19)
73 #define SDEDM_CLOCK_PULSE (1 << 20)
74 #define SDEDM_BYPASS (1 << 21)
76 #define SDEDM_WRITE_THRESHOLD_SHIFT 9
77 #define SDEDM_READ_THRESHOLD_SHIFT 14
78 #define SDEDM_THRESHOLD_MASK 0x1f
80 #define SDEDM_FSM_MASK 0xf
81 #define SDEDM_FSM_IDENTMODE 0x0
82 #define SDEDM_FSM_DATAMODE 0x1
83 #define SDEDM_FSM_READDATA 0x2
84 #define SDEDM_FSM_WRITEDATA 0x3
85 #define SDEDM_FSM_READWAIT 0x4
86 #define SDEDM_FSM_READCRC 0x5
87 #define SDEDM_FSM_WRITECRC 0x6
88 #define SDEDM_FSM_WRITEWAIT1 0x7
89 #define SDEDM_FSM_POWERDOWN 0x8
90 #define SDEDM_FSM_POWERUP 0x9
91 #define SDEDM_FSM_WRITESTART1 0xa
92 #define SDEDM_FSM_WRITESTART2 0xb
93 #define SDEDM_FSM_GENPULSES 0xc
94 #define SDEDM_FSM_WRITEWAIT2 0xd
95 #define SDEDM_FSM_STARTPOWDOWN 0xf
97 #define SDDATA_FIFO_WORDS 16
99 static void bcm2835_sdhost_update_irq(BCM2835SDHostState
*s
)
101 uint32_t irq
= s
->status
&
102 (SDHSTS_BUSY_IRPT
| SDHSTS_BLOCK_IRPT
| SDHSTS_SDIO_IRPT
);
103 trace_bcm2835_sdhost_update_irq(irq
);
104 qemu_set_irq(s
->irq
, !!irq
);
107 static void bcm2835_sdhost_send_command(BCM2835SDHostState
*s
)
113 request
.cmd
= s
->cmd
& SDCMD_CMD_MASK
;
114 request
.arg
= s
->cmdarg
;
116 rlen
= sdbus_do_command(&s
->sdbus
, &request
, rsp
);
120 if (!(s
->cmd
& SDCMD_NO_RESPONSE
)) {
121 if (rlen
== 0 || (rlen
== 4 && (s
->cmd
& SDCMD_LONG_RESPONSE
))) {
124 if (rlen
!= 4 && rlen
!= 16) {
128 s
->rsp
[0] = ldl_be_p(&rsp
[0]);
129 s
->rsp
[1] = s
->rsp
[2] = s
->rsp
[3] = 0;
131 s
->rsp
[0] = ldl_be_p(&rsp
[12]);
132 s
->rsp
[1] = ldl_be_p(&rsp
[8]);
133 s
->rsp
[2] = ldl_be_p(&rsp
[4]);
134 s
->rsp
[3] = ldl_be_p(&rsp
[0]);
137 /* We never really delay commands, so if this was a 'busywait' command
138 * then we've completed it now and can raise the interrupt.
140 if ((s
->cmd
& SDCMD_BUSYWAIT
) && (s
->config
& SDHCFG_BUSY_IRPT_EN
)) {
141 s
->status
|= SDHSTS_BUSY_IRPT
;
146 s
->cmd
|= SDCMD_FAIL_FLAG
;
147 s
->status
|= SDHSTS_CMD_TIME_OUT
;
150 static void bcm2835_sdhost_fifo_push(BCM2835SDHostState
*s
, uint32_t value
)
154 if (s
->fifo_len
== BCM2835_SDHOST_FIFO_LEN
) {
158 n
= (s
->fifo_pos
+ s
->fifo_len
) & (BCM2835_SDHOST_FIFO_LEN
- 1);
163 static uint32_t bcm2835_sdhost_fifo_pop(BCM2835SDHostState
*s
)
167 if (s
->fifo_len
== 0) {
171 value
= s
->fifo
[s
->fifo_pos
];
173 s
->fifo_pos
= (s
->fifo_pos
+ 1) & (BCM2835_SDHOST_FIFO_LEN
- 1);
177 static void bcm2835_sdhost_fifo_run(BCM2835SDHostState
*s
)
184 is_read
= (s
->cmd
& SDCMD_READ_CMD
) != 0;
185 is_write
= (s
->cmd
& SDCMD_WRITE_CMD
) != 0;
186 if (s
->datacnt
!= 0 && (is_write
|| sdbus_data_ready(&s
->sdbus
))) {
189 while (s
->datacnt
&& s
->fifo_len
< BCM2835_SDHOST_FIFO_LEN
) {
190 value
|= (uint32_t)sdbus_read_data(&s
->sdbus
) << (n
* 8);
194 bcm2835_sdhost_fifo_push(s
, value
);
195 s
->status
|= SDHSTS_DATA_FLAG
;
196 if (s
->config
& SDHCFG_DATA_IRPT_EN
) {
197 s
->status
|= SDHSTS_SDIO_IRPT
;
204 bcm2835_sdhost_fifo_push(s
, value
);
205 s
->status
|= SDHSTS_DATA_FLAG
;
206 if (s
->config
& SDHCFG_DATA_IRPT_EN
) {
207 s
->status
|= SDHSTS_SDIO_IRPT
;
210 } else if (is_write
) { /* write */
212 while (s
->datacnt
> 0 && (s
->fifo_len
> 0 || n
> 0)) {
214 value
= bcm2835_sdhost_fifo_pop(s
);
215 s
->status
|= SDHSTS_DATA_FLAG
;
216 if (s
->config
& SDHCFG_DATA_IRPT_EN
) {
217 s
->status
|= SDHSTS_SDIO_IRPT
;
223 sdbus_write_data(&s
->sdbus
, value
& 0xff);
227 if (s
->datacnt
== 0) {
228 s
->edm
&= ~SDEDM_FSM_MASK
;
229 s
->edm
|= SDEDM_FSM_DATAMODE
;
230 trace_bcm2835_sdhost_edm_change("datacnt 0", s
->edm
);
233 /* set block interrupt at end of each block transfer */
234 if (s
->hbct
&& s
->datacnt
% s
->hbct
== 0 &&
235 (s
->config
& SDHCFG_BLOCK_IRPT_EN
)) {
236 s
->status
|= SDHSTS_BLOCK_IRPT
;
238 /* set data interrupt after each transfer */
239 s
->status
|= SDHSTS_DATA_FLAG
;
240 if (s
->config
& SDHCFG_DATA_IRPT_EN
) {
241 s
->status
|= SDHSTS_SDIO_IRPT
;
246 bcm2835_sdhost_update_irq(s
);
248 s
->edm
&= ~(0x1f << 4);
249 s
->edm
|= ((s
->fifo_len
& 0x1f) << 4);
250 trace_bcm2835_sdhost_edm_change("fifo run", s
->edm
);
253 static uint64_t bcm2835_sdhost_read(void *opaque
, hwaddr offset
,
256 BCM2835SDHostState
*s
= (BCM2835SDHostState
*)opaque
;
285 res
= bcm2835_sdhost_fifo_pop(s
);
286 bcm2835_sdhost_fifo_run(s
);
296 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
302 trace_bcm2835_sdhost_read(offset
, res
, size
);
307 static void bcm2835_sdhost_write(void *opaque
, hwaddr offset
,
308 uint64_t value
, unsigned size
)
310 BCM2835SDHostState
*s
= (BCM2835SDHostState
*)opaque
;
312 trace_bcm2835_sdhost_write(offset
, value
, size
);
317 if (value
& SDCMD_NEW_FLAG
) {
318 bcm2835_sdhost_send_command(s
);
319 bcm2835_sdhost_fifo_run(s
);
320 s
->cmd
&= ~SDCMD_NEW_FLAG
;
329 bcm2835_sdhost_update_irq(s
);
335 if ((value
& 0xf) == 0xf) {
340 trace_bcm2835_sdhost_edm_change("guest register write", s
->edm
);
344 bcm2835_sdhost_fifo_run(s
);
350 bcm2835_sdhost_fifo_push(s
, value
);
351 bcm2835_sdhost_fifo_run(s
);
358 s
->datacnt
= s
->hblc
* s
->hbct
;
359 bcm2835_sdhost_fifo_run(s
);
363 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
369 static const MemoryRegionOps bcm2835_sdhost_ops
= {
370 .read
= bcm2835_sdhost_read
,
371 .write
= bcm2835_sdhost_write
,
372 .endianness
= DEVICE_NATIVE_ENDIAN
,
375 static const VMStateDescription vmstate_bcm2835_sdhost
= {
376 .name
= TYPE_BCM2835_SDHOST
,
378 .minimum_version_id
= 1,
379 .fields
= (VMStateField
[]) {
380 VMSTATE_UINT32(cmd
, BCM2835SDHostState
),
381 VMSTATE_UINT32(cmdarg
, BCM2835SDHostState
),
382 VMSTATE_UINT32(status
, BCM2835SDHostState
),
383 VMSTATE_UINT32_ARRAY(rsp
, BCM2835SDHostState
, 4),
384 VMSTATE_UINT32(config
, BCM2835SDHostState
),
385 VMSTATE_UINT32(edm
, BCM2835SDHostState
),
386 VMSTATE_UINT32(vdd
, BCM2835SDHostState
),
387 VMSTATE_UINT32(hbct
, BCM2835SDHostState
),
388 VMSTATE_UINT32(hblc
, BCM2835SDHostState
),
389 VMSTATE_INT32(fifo_pos
, BCM2835SDHostState
),
390 VMSTATE_INT32(fifo_len
, BCM2835SDHostState
),
391 VMSTATE_UINT32_ARRAY(fifo
, BCM2835SDHostState
, BCM2835_SDHOST_FIFO_LEN
),
392 VMSTATE_UINT32(datacnt
, BCM2835SDHostState
),
393 VMSTATE_END_OF_LIST()
397 static void bcm2835_sdhost_init(Object
*obj
)
399 BCM2835SDHostState
*s
= BCM2835_SDHOST(obj
);
401 qbus_create_inplace(&s
->sdbus
, sizeof(s
->sdbus
),
402 TYPE_BCM2835_SDHOST_BUS
, DEVICE(s
), "sd-bus");
404 memory_region_init_io(&s
->iomem
, obj
, &bcm2835_sdhost_ops
, s
,
405 TYPE_BCM2835_SDHOST
, 0x1000);
406 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
407 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->irq
);
410 static void bcm2835_sdhost_reset(DeviceState
*dev
)
412 BCM2835SDHostState
*s
= BCM2835_SDHOST(dev
);
417 trace_bcm2835_sdhost_edm_change("device reset", s
->edm
);
426 static void bcm2835_sdhost_class_init(ObjectClass
*klass
, void *data
)
428 DeviceClass
*dc
= DEVICE_CLASS(klass
);
430 dc
->reset
= bcm2835_sdhost_reset
;
431 dc
->vmsd
= &vmstate_bcm2835_sdhost
;
434 static TypeInfo bcm2835_sdhost_info
= {
435 .name
= TYPE_BCM2835_SDHOST
,
436 .parent
= TYPE_SYS_BUS_DEVICE
,
437 .instance_size
= sizeof(BCM2835SDHostState
),
438 .class_init
= bcm2835_sdhost_class_init
,
439 .instance_init
= bcm2835_sdhost_init
,
442 static const TypeInfo bcm2835_sdhost_bus_info
= {
443 .name
= TYPE_BCM2835_SDHOST_BUS
,
444 .parent
= TYPE_SD_BUS
,
445 .instance_size
= sizeof(SDBus
),
448 static void bcm2835_sdhost_register_types(void)
450 type_register_static(&bcm2835_sdhost_info
);
451 type_register_static(&bcm2835_sdhost_bus_info
);
454 type_init(bcm2835_sdhost_register_types
)