target-arm: Add S2 translation to 32bit S1 PTWs
[qemu.git] / hw / sd / sdhci.h
blobe2de92d553433bdd0ab271230da60d956063cd89
1 /*
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 #ifndef SDHCI_H
26 #define SDHCI_H
28 #include "qemu-common.h"
29 #include "hw/block/block.h"
30 #include "hw/pci/pci.h"
31 #include "hw/sysbus.h"
32 #include "hw/sd.h"
34 /* R/W SDMA System Address register 0x0 */
35 #define SDHC_SYSAD 0x00
37 /* R/W Host DMA Buffer Boundary and Transfer Block Size Register 0x0 */
38 #define SDHC_BLKSIZE 0x04
40 /* R/W Blocks count for current transfer 0x0 */
41 #define SDHC_BLKCNT 0x06
43 /* R/W Command Argument Register 0x0 */
44 #define SDHC_ARGUMENT 0x08
46 /* R/W Transfer Mode Setting Register 0x0 */
47 #define SDHC_TRNMOD 0x0C
48 #define SDHC_TRNS_DMA 0x0001
49 #define SDHC_TRNS_BLK_CNT_EN 0x0002
50 #define SDHC_TRNS_ACMD12 0x0004
51 #define SDHC_TRNS_READ 0x0010
52 #define SDHC_TRNS_MULTI 0x0020
54 /* R/W Command Register 0x0 */
55 #define SDHC_CMDREG 0x0E
56 #define SDHC_CMD_RSP_WITH_BUSY (3 << 0)
57 #define SDHC_CMD_DATA_PRESENT (1 << 5)
58 #define SDHC_CMD_SUSPEND (1 << 6)
59 #define SDHC_CMD_RESUME (1 << 7)
60 #define SDHC_CMD_ABORT ((1 << 6)|(1 << 7))
61 #define SDHC_CMD_TYPE_MASK ((1 << 6)|(1 << 7))
62 #define SDHC_COMMAND_TYPE(x) ((x) & SDHC_CMD_TYPE_MASK)
64 /* ROC Response Register 0 0x0 */
65 #define SDHC_RSPREG0 0x10
66 /* ROC Response Register 1 0x0 */
67 #define SDHC_RSPREG1 0x14
68 /* ROC Response Register 2 0x0 */
69 #define SDHC_RSPREG2 0x18
70 /* ROC Response Register 3 0x0 */
71 #define SDHC_RSPREG3 0x1C
73 /* R/W Buffer Data Register 0x0 */
74 #define SDHC_BDATA 0x20
76 /* R/ROC Present State Register 0x000A0000 */
77 #define SDHC_PRNSTS 0x24
78 #define SDHC_CMD_INHIBIT 0x00000001
79 #define SDHC_DATA_INHIBIT 0x00000002
80 #define SDHC_DAT_LINE_ACTIVE 0x00000004
81 #define SDHC_DOING_WRITE 0x00000100
82 #define SDHC_DOING_READ 0x00000200
83 #define SDHC_SPACE_AVAILABLE 0x00000400
84 #define SDHC_DATA_AVAILABLE 0x00000800
85 #define SDHC_CARD_PRESENT 0x00010000
86 #define SDHC_CARD_DETECT 0x00040000
87 #define SDHC_WRITE_PROTECT 0x00080000
88 #define TRANSFERRING_DATA(x) \
89 ((x) & (SDHC_DOING_READ | SDHC_DOING_WRITE))
91 /* R/W Host control Register 0x0 */
92 #define SDHC_HOSTCTL 0x28
93 #define SDHC_CTRL_DMA_CHECK_MASK 0x18
94 #define SDHC_CTRL_SDMA 0x00
95 #define SDHC_CTRL_ADMA1_32 0x08
96 #define SDHC_CTRL_ADMA2_32 0x10
97 #define SDHC_CTRL_ADMA2_64 0x18
98 #define SDHC_DMA_TYPE(x) ((x) & SDHC_CTRL_DMA_CHECK_MASK)
100 /* R/W Power Control Register 0x0 */
101 #define SDHC_PWRCON 0x29
102 #define SDHC_POWER_ON (1 << 0)
104 /* R/W Block Gap Control Register 0x0 */
105 #define SDHC_BLKGAP 0x2A
106 #define SDHC_STOP_AT_GAP_REQ 0x01
107 #define SDHC_CONTINUE_REQ 0x02
109 /* R/W WakeUp Control Register 0x0 */
110 #define SDHC_WAKCON 0x2B
111 #define SDHC_WKUP_ON_INS (1 << 1)
112 #define SDHC_WKUP_ON_RMV (1 << 2)
114 /* CLKCON */
115 #define SDHC_CLKCON 0x2C
116 #define SDHC_CLOCK_INT_STABLE 0x0002
117 #define SDHC_CLOCK_INT_EN 0x0001
118 #define SDHC_CLOCK_SDCLK_EN (1 << 2)
119 #define SDHC_CLOCK_CHK_MASK 0x0007
120 #define SDHC_CLOCK_IS_ON(x) \
121 (((x) & SDHC_CLOCK_CHK_MASK) == SDHC_CLOCK_CHK_MASK)
123 /* R/W Timeout Control Register 0x0 */
124 #define SDHC_TIMEOUTCON 0x2E
126 /* R/W Software Reset Register 0x0 */
127 #define SDHC_SWRST 0x2F
128 #define SDHC_RESET_ALL 0x01
129 #define SDHC_RESET_CMD 0x02
130 #define SDHC_RESET_DATA 0x04
132 /* ROC/RW1C Normal Interrupt Status Register 0x0 */
133 #define SDHC_NORINTSTS 0x30
134 #define SDHC_NIS_ERR 0x8000
135 #define SDHC_NIS_CMDCMP 0x0001
136 #define SDHC_NIS_TRSCMP 0x0002
137 #define SDHC_NIS_BLKGAP 0x0004
138 #define SDHC_NIS_DMA 0x0008
139 #define SDHC_NIS_WBUFRDY 0x0010
140 #define SDHC_NIS_RBUFRDY 0x0020
141 #define SDHC_NIS_INSERT 0x0040
142 #define SDHC_NIS_REMOVE 0x0080
143 #define SDHC_NIS_CARDINT 0x0100
145 /* ROC/RW1C Error Interrupt Status Register 0x0 */
146 #define SDHC_ERRINTSTS 0x32
147 #define SDHC_EIS_CMDTIMEOUT 0x0001
148 #define SDHC_EIS_BLKGAP 0x0004
149 #define SDHC_EIS_CMDIDX 0x0008
150 #define SDHC_EIS_CMD12ERR 0x0100
151 #define SDHC_EIS_ADMAERR 0x0200
153 /* R/W Normal Interrupt Status Enable Register 0x0 */
154 #define SDHC_NORINTSTSEN 0x34
155 #define SDHC_NISEN_CMDCMP 0x0001
156 #define SDHC_NISEN_TRSCMP 0x0002
157 #define SDHC_NISEN_DMA 0x0008
158 #define SDHC_NISEN_WBUFRDY 0x0010
159 #define SDHC_NISEN_RBUFRDY 0x0020
160 #define SDHC_NISEN_INSERT 0x0040
161 #define SDHC_NISEN_REMOVE 0x0080
162 #define SDHC_NISEN_CARDINT 0x0100
164 /* R/W Error Interrupt Status Enable Register 0x0 */
165 #define SDHC_ERRINTSTSEN 0x36
166 #define SDHC_EISEN_CMDTIMEOUT 0x0001
167 #define SDHC_EISEN_BLKGAP 0x0004
168 #define SDHC_EISEN_CMDIDX 0x0008
169 #define SDHC_EISEN_ADMAERR 0x0200
171 /* R/W Normal Interrupt Signal Enable Register 0x0 */
172 #define SDHC_NORINTSIGEN 0x38
173 #define SDHC_NORINTSIG_INSERT (1 << 6)
174 #define SDHC_NORINTSIG_REMOVE (1 << 7)
176 /* R/W Error Interrupt Signal Enable Register 0x0 */
177 #define SDHC_ERRINTSIGEN 0x3A
179 /* ROC Auto CMD12 error status register 0x0 */
180 #define SDHC_ACMD12ERRSTS 0x3C
182 /* HWInit Capabilities Register 0x05E80080 */
183 #define SDHC_CAPAREG 0x40
184 #define SDHC_CAN_DO_DMA 0x00400000
185 #define SDHC_CAN_DO_ADMA2 0x00080000
186 #define SDHC_CAN_DO_ADMA1 0x00100000
187 #define SDHC_64_BIT_BUS_SUPPORT (1 << 28)
188 #define SDHC_CAPAB_BLOCKSIZE(x) (((x) >> 16) & 0x3)
190 /* HWInit Maximum Current Capabilities Register 0x0 */
191 #define SDHC_MAXCURR 0x48
193 /* W Force Event Auto CMD12 Error Interrupt Register 0x0000 */
194 #define SDHC_FEAER 0x50
195 /* W Force Event Error Interrupt Register Error Interrupt 0x0000 */
196 #define SDHC_FEERR 0x52
198 /* R/W ADMA Error Status Register 0x00 */
199 #define SDHC_ADMAERR 0x54
200 #define SDHC_ADMAERR_LENGTH_MISMATCH (1 << 2)
201 #define SDHC_ADMAERR_STATE_ST_STOP (0 << 0)
202 #define SDHC_ADMAERR_STATE_ST_FDS (1 << 0)
203 #define SDHC_ADMAERR_STATE_ST_TFR (3 << 0)
204 #define SDHC_ADMAERR_STATE_MASK (3 << 0)
206 /* R/W ADMA System Address Register 0x00 */
207 #define SDHC_ADMASYSADDR 0x58
208 #define SDHC_ADMA_ATTR_SET_LEN (1 << 4)
209 #define SDHC_ADMA_ATTR_ACT_TRAN (1 << 5)
210 #define SDHC_ADMA_ATTR_ACT_LINK (3 << 4)
211 #define SDHC_ADMA_ATTR_INT (1 << 2)
212 #define SDHC_ADMA_ATTR_END (1 << 1)
213 #define SDHC_ADMA_ATTR_VALID (1 << 0)
214 #define SDHC_ADMA_ATTR_ACT_MASK ((1 << 4)|(1 << 5))
216 /* Slot interrupt status */
217 #define SDHC_SLOT_INT_STATUS 0xFC
219 /* HWInit Host Controller Version Register 0x0401 */
220 #define SDHC_HCVER 0xFE
221 #define SD_HOST_SPECv2_VERS 0x2401
223 #define SDHC_REGISTERS_MAP_SIZE 0x100
224 #define SDHC_INSERTION_DELAY (get_ticks_per_sec())
225 #define SDHC_TRANSFER_DELAY 100
226 #define SDHC_ADMA_DESCS_PER_DELAY 5
227 #define SDHC_CMD_RESPONSE (3 << 0)
229 enum {
230 sdhc_not_stopped = 0, /* normal SDHC state */
231 sdhc_gap_read = 1, /* SDHC stopped at block gap during read operation */
232 sdhc_gap_write = 2 /* SDHC stopped at block gap during write operation */
235 /* SD/MMC host controller state */
236 typedef struct SDHCIState {
237 union {
238 PCIDevice pcidev;
239 SysBusDevice busdev;
241 SDState *card;
242 MemoryRegion iomem;
243 BlockConf conf;
245 QEMUTimer *insert_timer; /* timer for 'changing' sd card. */
246 QEMUTimer *transfer_timer;
247 qemu_irq eject_cb;
248 qemu_irq ro_cb;
249 qemu_irq irq;
251 uint32_t sdmasysad; /* SDMA System Address register */
252 uint16_t blksize; /* Host DMA Buff Boundary and Transfer BlkSize Reg */
253 uint16_t blkcnt; /* Blocks count for current transfer */
254 uint32_t argument; /* Command Argument Register */
255 uint16_t trnmod; /* Transfer Mode Setting Register */
256 uint16_t cmdreg; /* Command Register */
257 uint32_t rspreg[4]; /* Response Registers 0-3 */
258 uint32_t prnsts; /* Present State Register */
259 uint8_t hostctl; /* Host Control Register */
260 uint8_t pwrcon; /* Power control Register */
261 uint8_t blkgap; /* Block Gap Control Register */
262 uint8_t wakcon; /* WakeUp Control Register */
263 uint16_t clkcon; /* Clock control Register */
264 uint8_t timeoutcon; /* Timeout Control Register */
265 uint8_t admaerr; /* ADMA Error Status Register */
266 uint16_t norintsts; /* Normal Interrupt Status Register */
267 uint16_t errintsts; /* Error Interrupt Status Register */
268 uint16_t norintstsen; /* Normal Interrupt Status Enable Register */
269 uint16_t errintstsen; /* Error Interrupt Status Enable Register */
270 uint16_t norintsigen; /* Normal Interrupt Signal Enable Register */
271 uint16_t errintsigen; /* Error Interrupt Signal Enable Register */
272 uint16_t acmd12errsts; /* Auto CMD12 error status register */
273 uint64_t admasysaddr; /* ADMA System Address Register */
275 uint32_t capareg; /* Capabilities Register */
276 uint32_t maxcurr; /* Maximum Current Capabilities Register */
277 uint8_t *fifo_buffer; /* SD host i/o FIFO buffer */
278 uint32_t buf_maxsz;
279 uint16_t data_count; /* current element in FIFO buffer */
280 uint8_t stopped_state;/* Current SDHC state */
281 /* Buffer Data Port Register - virtual access point to R and W buffers */
282 /* Software Reset Register - always reads as 0 */
283 /* Force Event Auto CMD12 Error Interrupt Reg - write only */
284 /* Force Event Error Interrupt Register- write only */
285 /* RO Host Controller Version Register always reads as 0x2401 */
286 } SDHCIState;
288 extern const VMStateDescription sdhci_vmstate;
290 #define TYPE_PCI_SDHCI "sdhci-pci"
291 #define PCI_SDHCI(obj) OBJECT_CHECK(SDHCIState, (obj), TYPE_PCI_SDHCI)
293 #define TYPE_SYSBUS_SDHCI "generic-sdhci"
294 #define SYSBUS_SDHCI(obj) \
295 OBJECT_CHECK(SDHCIState, (obj), TYPE_SYSBUS_SDHCI)
297 #endif /* SDHCI_H */