1 // SPDX-License-Identifier: GPL-2.0-only
3 * WM8505/WM8650 SD/MMC Host Controller
5 * Copyright (C) 2010 Tony Prisk
6 * Copyright (C) 2008 WonderMedia Technologies, Inc.
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/ioport.h>
13 #include <linux/errno.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/delay.h>
17 #include <linux/irq.h>
18 #include <linux/clk.h>
19 #include <linux/interrupt.h>
22 #include <linux/of_address.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_device.h>
26 #include <linux/mmc/host.h>
27 #include <linux/mmc/mmc.h>
28 #include <linux/mmc/sd.h>
30 #include <asm/byteorder.h>
33 #define DRIVER_NAME "wmt-sdhc"
36 /* MMC/SD controller registers */
37 #define SDMMC_CTLR 0x00
38 #define SDMMC_CMD 0x01
39 #define SDMMC_RSPTYPE 0x02
40 #define SDMMC_ARG 0x04
41 #define SDMMC_BUSMODE 0x08
42 #define SDMMC_BLKLEN 0x0C
43 #define SDMMC_BLKCNT 0x0E
44 #define SDMMC_RSP 0x10
45 #define SDMMC_CBCR 0x20
46 #define SDMMC_INTMASK0 0x24
47 #define SDMMC_INTMASK1 0x25
48 #define SDMMC_STS0 0x28
49 #define SDMMC_STS1 0x29
50 #define SDMMC_STS2 0x2A
51 #define SDMMC_STS3 0x2B
52 #define SDMMC_RSPTIMEOUT 0x2C
53 #define SDMMC_CLK 0x30 /* VT8500 only */
54 #define SDMMC_EXTCTRL 0x34
55 #define SDMMC_SBLKLEN 0x38
56 #define SDMMC_DMATIMEOUT 0x3C
59 /* SDMMC_CTLR bit fields */
60 #define CTLR_CMD_START 0x01
61 #define CTLR_CMD_WRITE 0x04
62 #define CTLR_FIFO_RESET 0x08
64 /* SDMMC_BUSMODE bit fields */
65 #define BM_SPI_MODE 0x01
66 #define BM_FOURBIT_MODE 0x02
67 #define BM_EIGHTBIT_MODE 0x04
68 #define BM_SD_OFF 0x10
69 #define BM_SPI_CS 0x20
70 #define BM_SD_POWER 0x40
71 #define BM_SOFT_RESET 0x80
73 /* SDMMC_BLKLEN bit fields */
74 #define BLKL_CRCERR_ABORT 0x0800
75 #define BLKL_CD_POL_HIGH 0x1000
76 #define BLKL_GPI_CD 0x2000
77 #define BLKL_DATA3_CD 0x4000
78 #define BLKL_INT_ENABLE 0x8000
80 /* SDMMC_INTMASK0 bit fields */
81 #define INT0_MBLK_TRAN_DONE_INT_EN 0x10
82 #define INT0_BLK_TRAN_DONE_INT_EN 0x20
83 #define INT0_CD_INT_EN 0x40
84 #define INT0_DI_INT_EN 0x80
86 /* SDMMC_INTMASK1 bit fields */
87 #define INT1_CMD_RES_TRAN_DONE_INT_EN 0x02
88 #define INT1_CMD_RES_TOUT_INT_EN 0x04
89 #define INT1_MBLK_AUTO_STOP_INT_EN 0x08
90 #define INT1_DATA_TOUT_INT_EN 0x10
91 #define INT1_RESCRC_ERR_INT_EN 0x20
92 #define INT1_RCRC_ERR_INT_EN 0x40
93 #define INT1_WCRC_ERR_INT_EN 0x80
95 /* SDMMC_STS0 bit fields */
96 #define STS0_WRITE_PROTECT 0x02
97 #define STS0_CD_DATA3 0x04
98 #define STS0_CD_GPI 0x08
99 #define STS0_MBLK_DONE 0x10
100 #define STS0_BLK_DONE 0x20
101 #define STS0_CARD_DETECT 0x40
102 #define STS0_DEVICE_INS 0x80
104 /* SDMMC_STS1 bit fields */
105 #define STS1_SDIO_INT 0x01
106 #define STS1_CMDRSP_DONE 0x02
107 #define STS1_RSP_TIMEOUT 0x04
108 #define STS1_AUTOSTOP_DONE 0x08
109 #define STS1_DATA_TIMEOUT 0x10
110 #define STS1_RSP_CRC_ERR 0x20
111 #define STS1_RCRC_ERR 0x40
112 #define STS1_WCRC_ERR 0x80
114 /* SDMMC_STS2 bit fields */
115 #define STS2_CMD_RES_BUSY 0x10
116 #define STS2_DATARSP_BUSY 0x20
117 #define STS2_DIS_FORCECLK 0x80
119 /* SDMMC_EXTCTRL bit fields */
120 #define EXT_EIGHTBIT 0x04
122 /* MMC/SD DMA Controller Registers */
123 #define SDDMA_GCR 0x100
124 #define SDDMA_IER 0x104
125 #define SDDMA_ISR 0x108
126 #define SDDMA_DESPR 0x10C
127 #define SDDMA_RBR 0x110
128 #define SDDMA_DAR 0x114
129 #define SDDMA_BAR 0x118
130 #define SDDMA_CPR 0x11C
131 #define SDDMA_CCR 0x120
134 /* SDDMA_GCR bit fields */
135 #define DMA_GCR_DMA_EN 0x00000001
136 #define DMA_GCR_SOFT_RESET 0x00000100
138 /* SDDMA_IER bit fields */
139 #define DMA_IER_INT_EN 0x00000001
141 /* SDDMA_ISR bit fields */
142 #define DMA_ISR_INT_STS 0x00000001
144 /* SDDMA_RBR bit fields */
145 #define DMA_RBR_FORMAT 0x40000000
146 #define DMA_RBR_END 0x80000000
148 /* SDDMA_CCR bit fields */
149 #define DMA_CCR_RUN 0x00000080
150 #define DMA_CCR_IF_TO_PERIPHERAL 0x00000000
151 #define DMA_CCR_PERIPHERAL_TO_IF 0x00400000
153 /* SDDMA_CCR event status */
154 #define DMA_CCR_EVT_NO_STATUS 0x00000000
155 #define DMA_CCR_EVT_UNDERRUN 0x00000001
156 #define DMA_CCR_EVT_OVERRUN 0x00000002
157 #define DMA_CCR_EVT_DESP_READ 0x00000003
158 #define DMA_CCR_EVT_DATA_RW 0x00000004
159 #define DMA_CCR_EVT_EARLY_END 0x00000005
160 #define DMA_CCR_EVT_SUCCESS 0x0000000F
162 #define PDMA_READ 0x00
163 #define PDMA_WRITE 0x01
165 #define WMT_SD_POWER_OFF 0
166 #define WMT_SD_POWER_ON 1
168 struct wmt_dma_descriptor
{
170 u32 data_buffer_addr
;
175 struct wmt_mci_caps
{
185 struct wmt_mci_priv
{
186 struct mmc_host
*mmc
;
187 void __iomem
*sdmmc_base
;
192 void *dma_desc_buffer
;
193 dma_addr_t dma_desc_device_addr
;
195 struct completion cmdcomp
;
196 struct completion datacomp
;
198 struct completion
*comp_cmd
;
199 struct completion
*comp_dma
;
201 struct mmc_request
*req
;
202 struct mmc_command
*cmd
;
204 struct clk
*clk_sdmmc
;
211 static void wmt_set_sd_power(struct wmt_mci_priv
*priv
, int enable
)
213 u32 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_BUSMODE
);
215 if (enable
^ priv
->power_inverted
)
216 reg_tmp
&= ~BM_SD_OFF
;
218 reg_tmp
|= BM_SD_OFF
;
220 writeb(reg_tmp
, priv
->sdmmc_base
+ SDMMC_BUSMODE
);
223 static void wmt_mci_read_response(struct mmc_host
*mmc
)
225 struct wmt_mci_priv
*priv
;
230 priv
= mmc_priv(mmc
);
232 for (idx1
= 0; idx1
< 4; idx1
++) {
234 for (idx2
= 0; idx2
< 4; idx2
++) {
235 if ((idx1
== 3) && (idx2
== 3))
236 tmp_resp
= readb(priv
->sdmmc_base
+ SDMMC_RSP
);
238 tmp_resp
= readb(priv
->sdmmc_base
+ SDMMC_RSP
+
239 (idx1
*4) + idx2
+ 1);
240 response
|= (tmp_resp
<< (idx2
* 8));
242 priv
->cmd
->resp
[idx1
] = cpu_to_be32(response
);
246 static void wmt_mci_start_command(struct wmt_mci_priv
*priv
)
250 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_CTLR
);
251 writeb(reg_tmp
| CTLR_CMD_START
, priv
->sdmmc_base
+ SDMMC_CTLR
);
254 static int wmt_mci_send_command(struct mmc_host
*mmc
, u8 command
, u8 cmdtype
,
257 struct wmt_mci_priv
*priv
;
260 priv
= mmc_priv(mmc
);
262 /* write command, arg, resptype registers */
263 writeb(command
, priv
->sdmmc_base
+ SDMMC_CMD
);
264 writel(arg
, priv
->sdmmc_base
+ SDMMC_ARG
);
265 writeb(rsptype
, priv
->sdmmc_base
+ SDMMC_RSPTYPE
);
267 /* reset response FIFO */
268 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_CTLR
);
269 writeb(reg_tmp
| CTLR_FIFO_RESET
, priv
->sdmmc_base
+ SDMMC_CTLR
);
271 /* ensure clock enabled - VT3465 */
272 wmt_set_sd_power(priv
, WMT_SD_POWER_ON
);
274 /* clear status bits */
275 writeb(0xFF, priv
->sdmmc_base
+ SDMMC_STS0
);
276 writeb(0xFF, priv
->sdmmc_base
+ SDMMC_STS1
);
277 writeb(0xFF, priv
->sdmmc_base
+ SDMMC_STS2
);
278 writeb(0xFF, priv
->sdmmc_base
+ SDMMC_STS3
);
280 /* set command type */
281 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_CTLR
);
282 writeb((reg_tmp
& 0x0F) | (cmdtype
<< 4),
283 priv
->sdmmc_base
+ SDMMC_CTLR
);
288 static void wmt_mci_disable_dma(struct wmt_mci_priv
*priv
)
290 writel(DMA_ISR_INT_STS
, priv
->sdmmc_base
+ SDDMA_ISR
);
291 writel(0, priv
->sdmmc_base
+ SDDMA_IER
);
294 static void wmt_complete_data_request(struct wmt_mci_priv
*priv
)
296 struct mmc_request
*req
;
299 req
->data
->bytes_xfered
= req
->data
->blksz
* req
->data
->blocks
;
301 /* unmap the DMA pages used for write data */
302 if (req
->data
->flags
& MMC_DATA_WRITE
)
303 dma_unmap_sg(mmc_dev(priv
->mmc
), req
->data
->sg
,
304 req
->data
->sg_len
, DMA_TO_DEVICE
);
306 dma_unmap_sg(mmc_dev(priv
->mmc
), req
->data
->sg
,
307 req
->data
->sg_len
, DMA_FROM_DEVICE
);
309 /* Check if the DMA ISR returned a data error */
310 if ((req
->cmd
->error
) || (req
->data
->error
))
311 mmc_request_done(priv
->mmc
, req
);
313 wmt_mci_read_response(priv
->mmc
);
314 if (!req
->data
->stop
) {
315 /* single-block read/write requests end here */
316 mmc_request_done(priv
->mmc
, req
);
319 * we change the priv->cmd variable so the response is
320 * stored in the stop struct rather than the original
321 * calling command struct
323 priv
->comp_cmd
= &priv
->cmdcomp
;
324 init_completion(priv
->comp_cmd
);
325 priv
->cmd
= req
->data
->stop
;
326 wmt_mci_send_command(priv
->mmc
, req
->data
->stop
->opcode
,
327 7, req
->data
->stop
->arg
, 9);
328 wmt_mci_start_command(priv
);
333 static irqreturn_t
wmt_mci_dma_isr(int irq_num
, void *data
)
335 struct wmt_mci_priv
*priv
;
339 priv
= (struct wmt_mci_priv
*)data
;
341 status
= readl(priv
->sdmmc_base
+ SDDMA_CCR
) & 0x0F;
343 if (status
!= DMA_CCR_EVT_SUCCESS
) {
344 dev_err(priv
->dev
, "DMA Error: Status = %d\n", status
);
345 priv
->req
->data
->error
= -ETIMEDOUT
;
346 complete(priv
->comp_dma
);
350 priv
->req
->data
->error
= 0;
352 wmt_mci_disable_dma(priv
);
354 complete(priv
->comp_dma
);
356 if (priv
->comp_cmd
) {
357 if (completion_done(priv
->comp_cmd
)) {
359 * if the command (regular) interrupt has already
360 * completed, finish off the request otherwise we wait
361 * for the command interrupt and finish from there.
363 wmt_complete_data_request(priv
);
370 static irqreturn_t
wmt_mci_regular_isr(int irq_num
, void *data
)
372 struct wmt_mci_priv
*priv
;
379 priv
= (struct wmt_mci_priv
*)data
;
381 status0
= readb(priv
->sdmmc_base
+ SDMMC_STS0
);
382 status1
= readb(priv
->sdmmc_base
+ SDMMC_STS1
);
383 status2
= readb(priv
->sdmmc_base
+ SDMMC_STS2
);
385 /* Check for card insertion */
386 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_INTMASK0
);
387 if ((reg_tmp
& INT0_DI_INT_EN
) && (status0
& STS0_DEVICE_INS
)) {
388 mmc_detect_change(priv
->mmc
, 0);
390 priv
->cmd
->error
= -ETIMEDOUT
;
392 complete(priv
->comp_cmd
);
393 if (priv
->comp_dma
) {
394 wmt_mci_disable_dma(priv
);
395 complete(priv
->comp_dma
);
397 writeb(STS0_DEVICE_INS
, priv
->sdmmc_base
+ SDMMC_STS0
);
401 if ((!priv
->req
->data
) ||
402 ((priv
->req
->data
->stop
) && (priv
->cmd
== priv
->req
->data
->stop
))) {
403 /* handle non-data & stop_transmission requests */
404 if (status1
& STS1_CMDRSP_DONE
) {
405 priv
->cmd
->error
= 0;
407 } else if ((status1
& STS1_RSP_TIMEOUT
) ||
408 (status1
& STS1_DATA_TIMEOUT
)) {
409 priv
->cmd
->error
= -ETIMEDOUT
;
414 priv
->comp_cmd
= NULL
;
416 if (!priv
->cmd
->error
)
417 wmt_mci_read_response(priv
->mmc
);
421 mmc_request_done(priv
->mmc
, priv
->req
);
424 /* handle data requests */
425 if (status1
& STS1_CMDRSP_DONE
) {
427 priv
->cmd
->error
= 0;
429 complete(priv
->comp_cmd
);
432 if ((status1
& STS1_RSP_TIMEOUT
) ||
433 (status1
& STS1_DATA_TIMEOUT
)) {
435 priv
->cmd
->error
= -ETIMEDOUT
;
437 complete(priv
->comp_cmd
);
438 if (priv
->comp_dma
) {
439 wmt_mci_disable_dma(priv
);
440 complete(priv
->comp_dma
);
444 if (priv
->comp_dma
) {
446 * If the dma interrupt has already completed, finish
447 * off the request; otherwise we wait for the DMA
448 * interrupt and finish from there.
450 if (completion_done(priv
->comp_dma
))
451 wmt_complete_data_request(priv
);
455 writeb(status0
, priv
->sdmmc_base
+ SDMMC_STS0
);
456 writeb(status1
, priv
->sdmmc_base
+ SDMMC_STS1
);
457 writeb(status2
, priv
->sdmmc_base
+ SDMMC_STS2
);
462 static void wmt_reset_hardware(struct mmc_host
*mmc
)
464 struct wmt_mci_priv
*priv
;
467 priv
= mmc_priv(mmc
);
469 /* reset controller */
470 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_BUSMODE
);
471 writeb(reg_tmp
| BM_SOFT_RESET
, priv
->sdmmc_base
+ SDMMC_BUSMODE
);
473 /* reset response FIFO */
474 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_CTLR
);
475 writeb(reg_tmp
| CTLR_FIFO_RESET
, priv
->sdmmc_base
+ SDMMC_CTLR
);
477 /* enable GPI pin to detect card */
478 writew(BLKL_INT_ENABLE
| BLKL_GPI_CD
, priv
->sdmmc_base
+ SDMMC_BLKLEN
);
480 /* clear interrupt status */
481 writeb(0xFF, priv
->sdmmc_base
+ SDMMC_STS0
);
482 writeb(0xFF, priv
->sdmmc_base
+ SDMMC_STS1
);
484 /* setup interrupts */
485 writeb(INT0_CD_INT_EN
| INT0_DI_INT_EN
, priv
->sdmmc_base
+
487 writeb(INT1_DATA_TOUT_INT_EN
| INT1_CMD_RES_TRAN_DONE_INT_EN
|
488 INT1_CMD_RES_TOUT_INT_EN
, priv
->sdmmc_base
+ SDMMC_INTMASK1
);
490 /* set the DMA timeout */
491 writew(8191, priv
->sdmmc_base
+ SDMMC_DMATIMEOUT
);
493 /* auto clock freezing enable */
494 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_STS2
);
495 writeb(reg_tmp
| STS2_DIS_FORCECLK
, priv
->sdmmc_base
+ SDMMC_STS2
);
497 /* set a default clock speed of 400Khz */
498 clk_set_rate(priv
->clk_sdmmc
, 400000);
501 static int wmt_dma_init(struct mmc_host
*mmc
)
503 struct wmt_mci_priv
*priv
;
505 priv
= mmc_priv(mmc
);
507 writel(DMA_GCR_SOFT_RESET
, priv
->sdmmc_base
+ SDDMA_GCR
);
508 writel(DMA_GCR_DMA_EN
, priv
->sdmmc_base
+ SDDMA_GCR
);
509 if ((readl(priv
->sdmmc_base
+ SDDMA_GCR
) & DMA_GCR_DMA_EN
) != 0)
515 static void wmt_dma_init_descriptor(struct wmt_dma_descriptor
*desc
,
516 u16 req_count
, u32 buffer_addr
, u32 branch_addr
, int end
)
518 desc
->flags
= 0x40000000 | req_count
;
520 desc
->flags
|= 0x80000000;
521 desc
->data_buffer_addr
= buffer_addr
;
522 desc
->branch_addr
= branch_addr
;
525 static void wmt_dma_config(struct mmc_host
*mmc
, u32 descaddr
, u8 dir
)
527 struct wmt_mci_priv
*priv
;
530 priv
= mmc_priv(mmc
);
532 /* Enable DMA Interrupts */
533 writel(DMA_IER_INT_EN
, priv
->sdmmc_base
+ SDDMA_IER
);
535 /* Write DMA Descriptor Pointer Register */
536 writel(descaddr
, priv
->sdmmc_base
+ SDDMA_DESPR
);
538 writel(0x00, priv
->sdmmc_base
+ SDDMA_CCR
);
540 if (dir
== PDMA_WRITE
) {
541 reg_tmp
= readl(priv
->sdmmc_base
+ SDDMA_CCR
);
542 writel(reg_tmp
& DMA_CCR_IF_TO_PERIPHERAL
, priv
->sdmmc_base
+
545 reg_tmp
= readl(priv
->sdmmc_base
+ SDDMA_CCR
);
546 writel(reg_tmp
| DMA_CCR_PERIPHERAL_TO_IF
, priv
->sdmmc_base
+
551 static void wmt_dma_start(struct wmt_mci_priv
*priv
)
555 reg_tmp
= readl(priv
->sdmmc_base
+ SDDMA_CCR
);
556 writel(reg_tmp
| DMA_CCR_RUN
, priv
->sdmmc_base
+ SDDMA_CCR
);
559 static void wmt_mci_request(struct mmc_host
*mmc
, struct mmc_request
*req
)
561 struct wmt_mci_priv
*priv
;
562 struct wmt_dma_descriptor
*desc
;
569 struct scatterlist
*sg
;
576 priv
= mmc_priv(mmc
);
580 * Use the cmd variable to pass a pointer to the resp[] structure
581 * This is required on multi-block requests to pass the pointer to the
584 priv
->cmd
= req
->cmd
;
586 command
= req
->cmd
->opcode
;
588 rsptype
= mmc_resp_type(req
->cmd
);
591 /* rsptype=7 only valid for SPI commands - should be =2 for SD */
594 /* rsptype=21 is R1B, convert for controller */
599 wmt_mci_send_command(mmc
, command
, cmdtype
, arg
, rsptype
);
600 wmt_mci_start_command(priv
);
601 /* completion is now handled in the regular_isr() */
604 priv
->comp_cmd
= &priv
->cmdcomp
;
605 init_completion(priv
->comp_cmd
);
609 /* set controller data length */
610 reg_tmp
= readw(priv
->sdmmc_base
+ SDMMC_BLKLEN
);
611 writew((reg_tmp
& 0xF800) | (req
->data
->blksz
- 1),
612 priv
->sdmmc_base
+ SDMMC_BLKLEN
);
614 /* set controller block count */
615 writew(req
->data
->blocks
, priv
->sdmmc_base
+ SDMMC_BLKCNT
);
617 desc
= (struct wmt_dma_descriptor
*)priv
->dma_desc_buffer
;
619 if (req
->data
->flags
& MMC_DATA_WRITE
) {
620 sg_cnt
= dma_map_sg(mmc_dev(mmc
), req
->data
->sg
,
621 req
->data
->sg_len
, DMA_TO_DEVICE
);
623 if (req
->data
->blocks
> 1)
626 sg_cnt
= dma_map_sg(mmc_dev(mmc
), req
->data
->sg
,
627 req
->data
->sg_len
, DMA_FROM_DEVICE
);
629 if (req
->data
->blocks
> 1)
633 dma_address
= priv
->dma_desc_device_addr
+ 16;
636 for_each_sg(req
->data
->sg
, sg
, sg_cnt
, i
) {
638 while (offset
< sg_dma_len(sg
)) {
639 wmt_dma_init_descriptor(desc
, req
->data
->blksz
,
640 sg_dma_address(sg
)+offset
,
644 offset
+= req
->data
->blksz
;
646 if (desc_cnt
== req
->data
->blocks
)
651 desc
->flags
|= 0x80000000;
653 if (req
->data
->flags
& MMC_DATA_WRITE
)
654 wmt_dma_config(mmc
, priv
->dma_desc_device_addr
,
657 wmt_dma_config(mmc
, priv
->dma_desc_device_addr
,
660 wmt_mci_send_command(mmc
, command
, cmdtype
, arg
, rsptype
);
662 priv
->comp_dma
= &priv
->datacomp
;
663 init_completion(priv
->comp_dma
);
666 wmt_mci_start_command(priv
);
670 static void wmt_mci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
672 struct wmt_mci_priv
*priv
;
673 u32 busmode
, extctrl
;
675 priv
= mmc_priv(mmc
);
677 if (ios
->power_mode
== MMC_POWER_UP
) {
678 wmt_reset_hardware(mmc
);
680 wmt_set_sd_power(priv
, WMT_SD_POWER_ON
);
682 if (ios
->power_mode
== MMC_POWER_OFF
)
683 wmt_set_sd_power(priv
, WMT_SD_POWER_OFF
);
686 clk_set_rate(priv
->clk_sdmmc
, ios
->clock
);
688 busmode
= readb(priv
->sdmmc_base
+ SDMMC_BUSMODE
);
689 extctrl
= readb(priv
->sdmmc_base
+ SDMMC_EXTCTRL
);
691 busmode
&= ~(BM_EIGHTBIT_MODE
| BM_FOURBIT_MODE
);
692 extctrl
&= ~EXT_EIGHTBIT
;
694 switch (ios
->bus_width
) {
695 case MMC_BUS_WIDTH_8
:
696 busmode
|= BM_EIGHTBIT_MODE
;
697 extctrl
|= EXT_EIGHTBIT
;
699 case MMC_BUS_WIDTH_4
:
700 busmode
|= BM_FOURBIT_MODE
;
702 case MMC_BUS_WIDTH_1
:
706 writeb(busmode
, priv
->sdmmc_base
+ SDMMC_BUSMODE
);
707 writeb(extctrl
, priv
->sdmmc_base
+ SDMMC_EXTCTRL
);
710 static int wmt_mci_get_ro(struct mmc_host
*mmc
)
712 struct wmt_mci_priv
*priv
= mmc_priv(mmc
);
714 return !(readb(priv
->sdmmc_base
+ SDMMC_STS0
) & STS0_WRITE_PROTECT
);
717 static int wmt_mci_get_cd(struct mmc_host
*mmc
)
719 struct wmt_mci_priv
*priv
= mmc_priv(mmc
);
720 u32 cd
= (readb(priv
->sdmmc_base
+ SDMMC_STS0
) & STS0_CD_GPI
) >> 3;
722 return !(cd
^ priv
->cd_inverted
);
725 static const struct mmc_host_ops wmt_mci_ops
= {
726 .request
= wmt_mci_request
,
727 .set_ios
= wmt_mci_set_ios
,
728 .get_ro
= wmt_mci_get_ro
,
729 .get_cd
= wmt_mci_get_cd
,
732 /* Controller capabilities */
733 static struct wmt_mci_caps wm8505_caps
= {
736 .ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
,
737 .caps
= MMC_CAP_4_BIT_DATA
| MMC_CAP_MMC_HIGHSPEED
|
738 MMC_CAP_SD_HIGHSPEED
,
739 .max_seg_size
= 65024,
741 .max_blk_size
= 2048,
744 static const struct of_device_id wmt_mci_dt_ids
[] = {
745 { .compatible
= "wm,wm8505-sdhc", .data
= &wm8505_caps
},
749 static int wmt_mci_probe(struct platform_device
*pdev
)
751 struct mmc_host
*mmc
;
752 struct wmt_mci_priv
*priv
;
753 struct device_node
*np
= pdev
->dev
.of_node
;
754 const struct of_device_id
*of_id
=
755 of_match_device(wmt_mci_dt_ids
, &pdev
->dev
);
756 const struct wmt_mci_caps
*wmt_caps
;
758 int regular_irq
, dma_irq
;
760 if (!of_id
|| !of_id
->data
) {
761 dev_err(&pdev
->dev
, "Controller capabilities data missing\n");
765 wmt_caps
= of_id
->data
;
768 dev_err(&pdev
->dev
, "Missing SDMMC description in devicetree\n");
772 regular_irq
= irq_of_parse_and_map(np
, 0);
773 dma_irq
= irq_of_parse_and_map(np
, 1);
775 if (!regular_irq
|| !dma_irq
) {
776 dev_err(&pdev
->dev
, "Getting IRQs failed!\n");
781 mmc
= mmc_alloc_host(sizeof(struct wmt_mci_priv
), &pdev
->dev
);
783 dev_err(&pdev
->dev
, "Failed to allocate mmc_host\n");
788 mmc
->ops
= &wmt_mci_ops
;
789 mmc
->f_min
= wmt_caps
->f_min
;
790 mmc
->f_max
= wmt_caps
->f_max
;
791 mmc
->ocr_avail
= wmt_caps
->ocr_avail
;
792 mmc
->caps
= wmt_caps
->caps
;
794 mmc
->max_seg_size
= wmt_caps
->max_seg_size
;
795 mmc
->max_segs
= wmt_caps
->max_segs
;
796 mmc
->max_blk_size
= wmt_caps
->max_blk_size
;
798 mmc
->max_req_size
= (16*512*mmc
->max_segs
);
799 mmc
->max_blk_count
= mmc
->max_req_size
/ 512;
801 priv
= mmc_priv(mmc
);
803 priv
->dev
= &pdev
->dev
;
805 priv
->power_inverted
= 0;
806 priv
->cd_inverted
= 0;
808 if (of_get_property(np
, "sdon-inverted", NULL
))
809 priv
->power_inverted
= 1;
810 if (of_get_property(np
, "cd-inverted", NULL
))
811 priv
->cd_inverted
= 1;
813 priv
->sdmmc_base
= of_iomap(np
, 0);
814 if (!priv
->sdmmc_base
) {
815 dev_err(&pdev
->dev
, "Failed to map IO space\n");
820 priv
->irq_regular
= regular_irq
;
821 priv
->irq_dma
= dma_irq
;
823 ret
= request_irq(regular_irq
, wmt_mci_regular_isr
, 0, "sdmmc", priv
);
825 dev_err(&pdev
->dev
, "Register regular IRQ fail\n");
829 ret
= request_irq(dma_irq
, wmt_mci_dma_isr
, 0, "sdmmc", priv
);
831 dev_err(&pdev
->dev
, "Register DMA IRQ fail\n");
835 /* alloc some DMA buffers for descriptors/transfers */
836 priv
->dma_desc_buffer
= dma_alloc_coherent(&pdev
->dev
,
837 mmc
->max_blk_count
* 16,
838 &priv
->dma_desc_device_addr
,
840 if (!priv
->dma_desc_buffer
) {
841 dev_err(&pdev
->dev
, "DMA alloc fail\n");
846 platform_set_drvdata(pdev
, mmc
);
848 priv
->clk_sdmmc
= of_clk_get(np
, 0);
849 if (IS_ERR(priv
->clk_sdmmc
)) {
850 dev_err(&pdev
->dev
, "Error getting clock\n");
851 ret
= PTR_ERR(priv
->clk_sdmmc
);
855 ret
= clk_prepare_enable(priv
->clk_sdmmc
);
859 /* configure the controller to a known 'ready' state */
860 wmt_reset_hardware(mmc
);
864 dev_info(&pdev
->dev
, "WMT SDHC Controller initialized\n");
868 clk_put(priv
->clk_sdmmc
);
870 free_irq(dma_irq
, priv
);
872 free_irq(regular_irq
, priv
);
874 iounmap(priv
->sdmmc_base
);
881 static int wmt_mci_remove(struct platform_device
*pdev
)
883 struct mmc_host
*mmc
;
884 struct wmt_mci_priv
*priv
;
885 struct resource
*res
;
888 mmc
= platform_get_drvdata(pdev
);
889 priv
= mmc_priv(mmc
);
891 /* reset SD controller */
892 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_BUSMODE
);
893 writel(reg_tmp
| BM_SOFT_RESET
, priv
->sdmmc_base
+ SDMMC_BUSMODE
);
894 reg_tmp
= readw(priv
->sdmmc_base
+ SDMMC_BLKLEN
);
895 writew(reg_tmp
& ~(0xA000), priv
->sdmmc_base
+ SDMMC_BLKLEN
);
896 writeb(0xFF, priv
->sdmmc_base
+ SDMMC_STS0
);
897 writeb(0xFF, priv
->sdmmc_base
+ SDMMC_STS1
);
899 /* release the dma buffers */
900 dma_free_coherent(&pdev
->dev
, priv
->mmc
->max_blk_count
* 16,
901 priv
->dma_desc_buffer
, priv
->dma_desc_device_addr
);
903 mmc_remove_host(mmc
);
905 free_irq(priv
->irq_regular
, priv
);
906 free_irq(priv
->irq_dma
, priv
);
908 iounmap(priv
->sdmmc_base
);
910 clk_disable_unprepare(priv
->clk_sdmmc
);
911 clk_put(priv
->clk_sdmmc
);
913 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
914 release_mem_region(res
->start
, resource_size(res
));
918 dev_info(&pdev
->dev
, "WMT MCI device removed\n");
924 static int wmt_mci_suspend(struct device
*dev
)
927 struct mmc_host
*mmc
= dev_get_drvdata(dev
);
928 struct wmt_mci_priv
*priv
;
933 priv
= mmc_priv(mmc
);
934 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_BUSMODE
);
935 writeb(reg_tmp
| BM_SOFT_RESET
, priv
->sdmmc_base
+
938 reg_tmp
= readw(priv
->sdmmc_base
+ SDMMC_BLKLEN
);
939 writew(reg_tmp
& 0x5FFF, priv
->sdmmc_base
+ SDMMC_BLKLEN
);
941 writeb(0xFF, priv
->sdmmc_base
+ SDMMC_STS0
);
942 writeb(0xFF, priv
->sdmmc_base
+ SDMMC_STS1
);
944 clk_disable(priv
->clk_sdmmc
);
948 static int wmt_mci_resume(struct device
*dev
)
951 struct mmc_host
*mmc
= dev_get_drvdata(dev
);
952 struct wmt_mci_priv
*priv
;
955 priv
= mmc_priv(mmc
);
956 clk_enable(priv
->clk_sdmmc
);
958 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_BUSMODE
);
959 writeb(reg_tmp
| BM_SOFT_RESET
, priv
->sdmmc_base
+
962 reg_tmp
= readw(priv
->sdmmc_base
+ SDMMC_BLKLEN
);
963 writew(reg_tmp
| (BLKL_GPI_CD
| BLKL_INT_ENABLE
),
964 priv
->sdmmc_base
+ SDMMC_BLKLEN
);
966 reg_tmp
= readb(priv
->sdmmc_base
+ SDMMC_INTMASK0
);
967 writeb(reg_tmp
| INT0_DI_INT_EN
, priv
->sdmmc_base
+
975 static const struct dev_pm_ops wmt_mci_pm
= {
976 .suspend
= wmt_mci_suspend
,
977 .resume
= wmt_mci_resume
,
980 #define wmt_mci_pm_ops (&wmt_mci_pm)
982 #else /* !CONFIG_PM */
984 #define wmt_mci_pm_ops NULL
988 static struct platform_driver wmt_mci_driver
= {
989 .probe
= wmt_mci_probe
,
990 .remove
= wmt_mci_remove
,
993 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
994 .pm
= wmt_mci_pm_ops
,
995 .of_match_table
= wmt_mci_dt_ids
,
999 module_platform_driver(wmt_mci_driver
);
1001 MODULE_DESCRIPTION("Wondermedia MMC/SD Driver");
1002 MODULE_AUTHOR("Tony Prisk");
1003 MODULE_LICENSE("GPL v2");
1004 MODULE_DEVICE_TABLE(of
, wmt_mci_dt_ids
);