2 * Synopsys DesignWare Multimedia Card Interface driver
3 * (Based on NXP driver for lpc 31xx)
5 * Copyright (C) 2009 NXP Semiconductors
6 * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
8 * Copyright (C) 2014 Fuzhou Rockchip Electronics Co.Ltd.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
18 #include "rk_sdmmc_dbg.h"
20 #define DW_MMC_240A 0x240a
21 #define DW_MMC_270A 0x270a
23 #define SDMMC_CTRL 0x000
24 #define SDMMC_PWREN 0x004
25 #define SDMMC_CLKDIV 0x008
26 #define SDMMC_CLKSRC 0x00c
27 #define SDMMC_CLKENA 0x010
28 #define SDMMC_TMOUT 0x014
29 #define SDMMC_CTYPE 0x018
30 #define SDMMC_BLKSIZ 0x01c
31 #define SDMMC_BYTCNT 0x020
32 #define SDMMC_INTMASK 0x024
33 #define SDMMC_CMDARG 0x028
34 #define SDMMC_CMD 0x02c
35 #define SDMMC_RESP0 0x030
36 #define SDMMC_RESP1 0x034
37 #define SDMMC_RESP2 0x038
38 #define SDMMC_RESP3 0x03c
39 #define SDMMC_MINTSTS 0x040
40 #define SDMMC_RINTSTS 0x044
41 #define SDMMC_STATUS 0x048
42 #define SDMMC_FIFOTH 0x04c
43 #define SDMMC_CDETECT 0x050
44 #define SDMMC_WRTPRT 0x054
45 #define SDMMC_GPIO 0x058
46 #define SDMMC_TCBCNT 0x05c
47 #define SDMMC_TBBCNT 0x060
48 #define SDMMC_DEBNCE 0x064
49 #define SDMMC_USRID 0x068
50 #define SDMMC_VERID 0x06c
51 #define SDMMC_HCON 0x070
52 #define SDMMC_UHS_REG 0x074
53 #define SDMMC_RST_N 0x078
54 #define SDMMC_BMOD 0x080
55 #define SDMMC_PLDMND 0x084
56 #define SDMMC_DBADDR 0x088
57 #define SDMMC_IDSTS 0x08c
58 #define SDMMC_IDINTEN 0x090
59 #define SDMMC_DSCADDR 0x094
60 #define SDMMC_BUFADDR 0x098
61 #define SDMMC_CDTHRCTL 0x100
62 #define SDMMC_DATA(x) (x)
65 * Data offset is difference according to Version
66 * Lower than 2.40a : data register offest is 0x100
68 #define DATA_OFFSET 0x100
69 #define DATA_240A_OFFSET 0x200
72 #define _SBF(f, v) ((v) << (f))
81 static struct sdmmc_reg dw_mci_regs[] =
85 { 0x0008, " CLKDIV" },
86 { 0x000C, " CLKSRC" },
87 { 0x0010, " CLKENA" },
90 { 0x001C, " BLKSIZ" },
91 { 0x0020, " BYTCNT" },
92 { 0x0024, " INTMASK" },
93 { 0x0028, " CMDARG" },
99 { 0x0040, " MINSTS" },
100 { 0x0044, " RINTSTS" },
101 { 0x0048, " STATUS" },
102 { 0x004C, " FIFOTH" },
103 { 0x0050, " CDETECT" },
104 { 0x0054, " WRTPRT" },
106 { 0x005C, " TCBCNT" },
107 { 0x0060, " TBBCNT" },
108 { 0x0064, " DEBNCE" },
109 { 0x0068, " USRID" },
110 { 0x006C, " VERID" },
112 { 0x0074, " UHS_REG" },
113 { 0x0078, " RST_n" },
115 { 0x0084, " PLDMND" },
116 { 0x0088, " DBADDR" },
117 { 0x008C, " IDSTS" },
118 { 0x0090, " IDINTEN" },
119 { 0x0094, " DSCADDR" },
120 { 0x0098, " BUFADDR" },
121 { 0x0100, "CARDTHRCTL" },
122 { 0x0104, "BackEndPwr" },
127 /* Control register defines */
128 #define SDMMC_CTRL_USE_IDMAC BIT(25)
129 #define SDMMC_CTRL_CEATA_INT_EN BIT(11)
130 #define SDMMC_CTRL_SEND_AS_CCSD BIT(10)
131 #define SDMMC_CTRL_SEND_CCSD BIT(9)
132 #define SDMMC_CTRL_ABRT_READ_DATA BIT(8)
133 #define SDMMC_CTRL_SEND_IRQ_RESP BIT(7)
134 #define SDMMC_CTRL_READ_WAIT BIT(6)
135 #define SDMMC_CTRL_DMA_ENABLE BIT(5)
136 #define SDMMC_CTRL_INT_ENABLE BIT(4)
137 #define SDMMC_CTRL_DMA_RESET BIT(2)
138 #define SDMMC_CTRL_FIFO_RESET BIT(1)
139 #define SDMMC_CTRL_RESET BIT(0)
140 /* Clock Enable register defines */
141 #define SDMMC_CLKEN_LOW_PWR BIT(16)
142 #define SDMMC_CLKEN_ENABLE BIT(0)
143 /* time-out register defines */
144 #define SDMMC_TMOUT_DATA(n) _SBF(8, (n))
145 #define SDMMC_TMOUT_DATA_MSK 0xFFFFFF00
146 #define SDMMC_TMOUT_RESP(n) ((n) & 0xFF)
147 #define SDMMC_TMOUT_RESP_MSK 0xFF
148 /* card-type register defines */
149 #define SDMMC_CTYPE_8BIT BIT(16)
150 #define SDMMC_CTYPE_4BIT BIT(0)
151 #define SDMMC_CTYPE_1BIT 0
152 /* Interrupt status & mask register defines */
153 #define SDMMC_INT_SDIO(n) BIT(16 + (n))
154 #define SDMMC_INT_EBE BIT(15)
155 #define SDMMC_INT_ACD BIT(14)
156 #define SDMMC_INT_SBE BIT(13)
157 #define SDMMC_INT_HLE BIT(12)
158 #define SDMMC_INT_FRUN BIT(11)
159 #define SDMMC_INT_HTO BIT(10)
160 #define SDMMC_INT_VSI SDMMC_INT_HTO // VSI => Voltage Switch Interrupt,Volt_Switch_int
161 #define SDMMC_INT_DRTO BIT(9)
162 #define SDMMC_INT_RTO BIT(8)
163 #define SDMMC_INT_DCRC BIT(7)
164 #define SDMMC_INT_RCRC BIT(6)
165 #define SDMMC_INT_RXDR BIT(5)
166 #define SDMMC_INT_TXDR BIT(4)
167 #define SDMMC_INT_DATA_OVER BIT(3)
168 #define SDMMC_INT_CMD_DONE BIT(2)
169 #define SDMMC_INT_RESP_ERR BIT(1)
170 #define SDMMC_INT_CD BIT(0)
171 #define SDMMC_INT_ERROR 0xbfc2
172 /* Command register defines */
173 #define SDMMC_CMD_START BIT(31)
174 #define SDMMC_CMD_USE_HOLD_REG BIT(29)
175 #define SDMMC_CMD_VOLT_SWITCH BIT(28) //Voltage switch bit
176 #define SDMMC_CMD_VOLT_SWITCH BIT(28) //Voltage switch bit
177 #define SDMMC_CMD_BOOT_MODE BIT(27) //set boot mode.
178 #define SDMMC_CMD_DISABLE_BOOT BIT(26) //disable boot.
179 #define SDMMC_CMD_EXPECT_BOOT_ACK BIT(25) //Expect Boot Acknowledge.
180 #define SDMMC_CMD_ENABLE_BOOT BIT(24) //be set only for mandatory boot mode.
181 #define SDMMC_CMD_CCS_EXP BIT(23)
182 #define SDMMC_CMD_CEATA_RD BIT(22)
183 #define SDMMC_CMD_UPD_CLK BIT(21)
184 #define SDMMC_CMD_INIT BIT(15)
185 #define SDMMC_CMD_STOP BIT(14)
186 #define SDMMC_CMD_PRV_DAT_WAIT BIT(13)
187 #define SDMMC_CMD_SEND_STOP BIT(12)
188 #define SDMMC_CMD_STRM_MODE BIT(11)
189 #define SDMMC_CMD_DAT_WR BIT(10)
190 #define SDMMC_CMD_DAT_EXP BIT(9)
191 #define SDMMC_CMD_RESP_CRC BIT(8)
192 #define SDMMC_CMD_RESP_LONG BIT(7)
193 #define SDMMC_CMD_RESP_EXP BIT(6)
194 #define SDMMC_CMD_INDX(n) ((n) & 0x1F)
195 /* Status register defines */
196 #define SDMMC_GET_FCNT(x) (((x)>>17) & 0x1FFF)
197 #define SDMMC_STAUTS_MC_BUSY BIT(10)
198 #define SDMMC_STAUTS_DATA_BUSY BIT(9) //Card busy
199 #define SDMMC_CMD_FSM_MASK (0x0F << 4) //Command FSM status mask
200 #define SDMMC_CMD_FSM_IDLE (0x00) //CMD FSM is IDLE
201 #define SDMMC_STAUTS_FIFO_FULL BIT(3) //FIFO is full status
202 #define SDMMC_STAUTS_FIFO_EMPTY BIT(2) //FIFO is empty status
204 /* Control SDMMC_UHS_REG defines (base+ 0x74)*/
205 #define SDMMC_UHS_DDR_MODE BIT(16) // 0--Non DDR Mode; 1--DDR mode
206 #define SDMMC_UHS_VOLT_REG_18 BIT(0) // 0--3.3v; 1--1.8V
208 /* FIFOTH register defines */
209 #define SDMMC_SET_FIFOTH(m, r, t) (((m) & 0x7) << 28 | \
210 ((r) & 0xFFF) << 16 | \
212 /* Internal DMAC interrupt defines */
213 #define SDMMC_IDMAC_INT_AI BIT(9)
214 #define SDMMC_IDMAC_INT_NI BIT(8)
215 #define SDMMC_IDMAC_INT_CES BIT(5)
216 #define SDMMC_IDMAC_INT_DU BIT(4)
217 #define SDMMC_IDMAC_INT_FBE BIT(2)
218 #define SDMMC_IDMAC_INT_RI BIT(1)
219 #define SDMMC_IDMAC_INT_TI BIT(0)
220 /* Internal DMAC bus mode bits */
221 #define SDMMC_IDMAC_ENABLE BIT(7)
222 #define SDMMC_IDMAC_FB BIT(1)
223 #define SDMMC_IDMAC_SWRESET BIT(0)
224 /* Version ID register define */
225 #define SDMMC_GET_VERID(x) ((x) & 0xFFFF)
226 /* Card read threshold */
227 #define SDMMC_SET_RD_THLD(v, x) (((v) & 0x1FFF) << 16 | (x))
229 /* Register access macros */
230 #define mci_readl(dev, reg) \
231 __raw_readl((dev)->regs + SDMMC_##reg)
232 #define mci_writel(dev, reg, value) \
233 __raw_writel((value), (dev)->regs + SDMMC_##reg)
234 #define mci_readreg(dev, addr) \
235 __raw_readl((dev)->regs + addr)
237 /* 16-bit FIFO access macros */
238 #define mci_readw(dev, reg) \
239 __raw_readw((dev)->regs + SDMMC_##reg)
240 #define mci_writew(dev, reg, value) \
241 __raw_writew((value), (dev)->regs + SDMMC_##reg)
243 /* 64-bit FIFO access macros */
245 #define mci_readq(dev, reg) \
246 __raw_readq((dev)->regs + SDMMC_##reg)
247 #define mci_writeq(dev, reg, value) \
248 __raw_writeq((value), (dev)->regs + SDMMC_##reg)
251 * Dummy readq implementation for architectures that don't define it.
253 * We would assume that none of these architectures would configure
254 * the IP block with a 64bit FIFO width, so this code will never be
255 * executed on those machines. Defining these macros here keeps the
256 * rest of the code free from ifdefs.
258 #define mci_readq(dev, reg) \
259 (*(volatile u64 __force *)((dev)->regs + SDMMC_##reg))
260 #define mci_writeq(dev, reg, value) \
261 (*(volatile u64 __force *)((dev)->regs + SDMMC_##reg) = (value))
263 extern void rk_send_wakeup_key(void);
264 extern int dw_mci_probe(struct dw_mci *host);
265 extern void dw_mci_remove(struct dw_mci *host);
267 extern int dw_mci_suspend(struct dw_mci *host);
268 extern int dw_mci_resume(struct dw_mci *host);
272 * struct dw_mci_slot - MMC slot state
273 * @mmc: The mmc_host representing this slot.
274 * @host: The MMC controller this slot is using.
275 * @quirks: Slot-level quirks (DW_MCI_SLOT_QUIRK_XXX)
276 * @wp_gpio: If gpio_is_valid() we'll use this to read write protect.
277 * @ctype: Card type for this slot.
278 * @mrq: mmc_request currently being processed or waiting to be
279 * processed, or NULL when the slot is idle.
280 * @queue_node: List node for placing this node in the @queue list of
282 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
283 * @__clk_old: The last updated clock with reflecting clock divider.
284 * Keeping track of this helps us to avoid spamming the console
285 * with CONFIG_MMC_CLKGATE.
286 * @flags: Random state bits associated with the slot.
287 * @id: Number of this slot.
288 * @last_detect_state: Most recently observed card detect state.
291 struct mmc_host *mmc;
301 struct mmc_request *mrq;
302 struct list_head queue_node;
305 unsigned int __clk_old;
308 #define DW_MMC_CARD_PRESENT 0
309 #define DW_MMC_CARD_NEED_INIT 1
311 int last_detect_state;
314 struct dw_mci_tuning_data {
315 const u8 *blk_pattern;
323 * dw_mci driver data - dw-mshc implementation specific driver data.
324 * @caps: mmc subsystem specified capabilities of the controller(s).
325 * @hold_reg_flag: Fixed the value of HOLG_REG
326 * @init: early implementation specific initialization.
327 * @setup_clock: implementation specific clock configuration.
328 * @prepare_command: handle CMD register extensions.
329 * @set_ios: handle bus specific extensions.
330 * @parse_dt: parse implementation specific device tree properties.
332 * Provide controller implementation specific extensions. The usage of this
333 * data structure is fully optional and usage of each member in this structure
334 * is optional as well.
336 struct dw_mci_drv_data {
338 unsigned int *hold_reg_flag;
339 int (*init)(struct dw_mci *host);
340 int (*setup_clock)(struct dw_mci *host);
341 void (*prepare_command)(struct dw_mci *host, u32 *cmdr);
342 void (*set_ios)(struct dw_mci *host, struct mmc_ios *ios);
343 int (*parse_dt)(struct dw_mci *host);
344 int (*execute_tuning)(struct dw_mci_slot *slot, u32 opcode,
345 struct dw_mci_tuning_data *tuning_data);
347 #endif /* _DW_MMC_H_ */