1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Realtek USB Memstick Card Interface driver
4 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
7 * Roger Tseng <rogerable@realtek.com>
10 #include <linux/module.h>
11 #include <linux/highmem.h>
12 #include <linux/delay.h>
13 #include <linux/platform_device.h>
14 #include <linux/workqueue.h>
15 #include <linux/memstick.h>
16 #include <linux/kthread.h>
17 #include <linux/rtsx_usb.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/mutex.h>
20 #include <linux/sched.h>
21 #include <linux/completion.h>
22 #include <asm/unaligned.h>
25 struct platform_device
*pdev
;
27 struct memstick_host
*msh
;
28 struct memstick_request
*req
;
30 struct mutex host_mutex
;
31 struct work_struct handle_req
;
32 struct delayed_work poll_card
;
39 bool system_suspending
;
42 static inline struct device
*ms_dev(struct rtsx_usb_ms
*host
)
44 return &(host
->pdev
->dev
);
47 static inline void ms_clear_error(struct rtsx_usb_ms
*host
)
49 struct rtsx_ucr
*ucr
= host
->ucr
;
50 rtsx_usb_ep0_write_register(ucr
, CARD_STOP
,
52 MS_STOP
| MS_CLR_ERR
);
54 rtsx_usb_clear_dma_err(ucr
);
55 rtsx_usb_clear_fsm_err(ucr
);
60 static void ms_print_debug_regs(struct rtsx_usb_ms
*host
)
62 struct rtsx_ucr
*ucr
= host
->ucr
;
66 /* Print MS host internal registers */
67 rtsx_usb_init_cmd(ucr
);
69 /* MS_CFG to MS_INT_REG */
70 for (i
= 0xFD40; i
<= 0xFD44; i
++)
71 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, i
, 0, 0);
73 /* CARD_SHARE_MODE to CARD_GPIO */
74 for (i
= 0xFD51; i
<= 0xFD56; i
++)
75 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, i
, 0, 0);
78 for (i
= 0xFD60; i
<= 0xFD65; i
++)
79 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, i
, 0, 0);
81 /* CARD_DATA_SOURCE, CARD_SELECT, CARD_CLK_EN, CARD_PWR_CTL */
82 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, CARD_DATA_SOURCE
, 0, 0);
83 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, CARD_SELECT
, 0, 0);
84 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, CARD_CLK_EN
, 0, 0);
85 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, CARD_PWR_CTL
, 0, 0);
87 rtsx_usb_send_cmd(ucr
, MODE_CR
, 100);
88 rtsx_usb_get_rsp(ucr
, 21, 100);
91 for (i
= 0xFD40; i
<= 0xFD44; i
++)
92 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", i
, *(ptr
++));
93 for (i
= 0xFD51; i
<= 0xFD56; i
++)
94 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", i
, *(ptr
++));
95 for (i
= 0xFD60; i
<= 0xFD65; i
++)
96 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", i
, *(ptr
++));
98 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", CARD_DATA_SOURCE
, *(ptr
++));
99 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", CARD_SELECT
, *(ptr
++));
100 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", CARD_CLK_EN
, *(ptr
++));
101 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", CARD_PWR_CTL
, *(ptr
++));
106 static void ms_print_debug_regs(struct rtsx_usb_ms
*host
)
112 static int ms_pull_ctl_disable_lqfp48(struct rtsx_ucr
*ucr
)
114 rtsx_usb_init_cmd(ucr
);
116 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
117 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
118 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
119 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
120 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
121 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
123 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
126 static int ms_pull_ctl_disable_qfn24(struct rtsx_ucr
*ucr
)
128 rtsx_usb_init_cmd(ucr
);
130 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x65);
131 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
132 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
133 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
134 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x56);
135 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
137 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
140 static int ms_pull_ctl_enable_lqfp48(struct rtsx_ucr
*ucr
)
142 rtsx_usb_init_cmd(ucr
);
144 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
145 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
146 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
147 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
148 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
149 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
151 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
154 static int ms_pull_ctl_enable_qfn24(struct rtsx_ucr
*ucr
)
156 rtsx_usb_init_cmd(ucr
);
158 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x65);
159 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
160 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
161 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
162 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
163 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
165 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
168 static int ms_power_on(struct rtsx_usb_ms
*host
)
170 struct rtsx_ucr
*ucr
= host
->ucr
;
173 dev_dbg(ms_dev(host
), "%s\n", __func__
);
175 rtsx_usb_init_cmd(ucr
);
176 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_SELECT
, 0x07, MS_MOD_SEL
);
177 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_SHARE_MODE
,
178 CARD_SHARE_MASK
, CARD_SHARE_MS
);
179 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_CLK_EN
,
180 MS_CLK_EN
, MS_CLK_EN
);
181 err
= rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
185 if (CHECK_PKG(ucr
, LQFP48
))
186 err
= ms_pull_ctl_enable_lqfp48(ucr
);
188 err
= ms_pull_ctl_enable_qfn24(ucr
);
192 err
= rtsx_usb_write_register(ucr
, CARD_PWR_CTL
,
193 POWER_MASK
, PARTIAL_POWER_ON
);
197 usleep_range(800, 1000);
199 rtsx_usb_init_cmd(ucr
);
200 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PWR_CTL
,
201 POWER_MASK
, POWER_ON
);
202 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_OE
,
203 MS_OUTPUT_EN
, MS_OUTPUT_EN
);
205 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
208 static int ms_power_off(struct rtsx_usb_ms
*host
)
210 struct rtsx_ucr
*ucr
= host
->ucr
;
213 dev_dbg(ms_dev(host
), "%s\n", __func__
);
215 rtsx_usb_init_cmd(ucr
);
217 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_CLK_EN
, MS_CLK_EN
, 0);
218 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_OE
, MS_OUTPUT_EN
, 0);
220 err
= rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
224 if (CHECK_PKG(ucr
, LQFP48
))
225 return ms_pull_ctl_disable_lqfp48(ucr
);
227 return ms_pull_ctl_disable_qfn24(ucr
);
230 static int ms_transfer_data(struct rtsx_usb_ms
*host
, unsigned char data_dir
,
231 u8 tpc
, u8 cfg
, struct scatterlist
*sg
)
233 struct rtsx_ucr
*ucr
= host
->ucr
;
235 unsigned int length
= sg
->length
;
236 u16 sec_cnt
= (u16
)(length
/ 512);
237 u8 trans_mode
, dma_dir
, flag
;
239 struct memstick_dev
*card
= host
->msh
->card
;
241 dev_dbg(ms_dev(host
), "%s: tpc = 0x%02x, data_dir = %s, length = %d\n",
242 __func__
, tpc
, (data_dir
== READ
) ? "READ" : "WRITE",
245 if (data_dir
== READ
) {
247 dma_dir
= DMA_DIR_FROM_CARD
;
248 if (card
->id
.type
!= MEMSTICK_TYPE_PRO
)
249 trans_mode
= MS_TM_NORMAL_READ
;
251 trans_mode
= MS_TM_AUTO_READ
;
252 pipe
= usb_rcvbulkpipe(ucr
->pusb_dev
, EP_BULK_IN
);
255 dma_dir
= DMA_DIR_TO_CARD
;
256 if (card
->id
.type
!= MEMSTICK_TYPE_PRO
)
257 trans_mode
= MS_TM_NORMAL_WRITE
;
259 trans_mode
= MS_TM_AUTO_WRITE
;
260 pipe
= usb_sndbulkpipe(ucr
->pusb_dev
, EP_BULK_OUT
);
263 rtsx_usb_init_cmd(ucr
);
265 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
266 if (card
->id
.type
== MEMSTICK_TYPE_PRO
) {
267 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_SECTOR_CNT_H
,
268 0xFF, (u8
)(sec_cnt
>> 8));
269 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
,
272 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
274 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC3
,
275 0xFF, (u8
)(length
>> 24));
276 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC2
,
277 0xFF, (u8
)(length
>> 16));
278 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC1
,
279 0xFF, (u8
)(length
>> 8));
280 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC0
, 0xFF,
282 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_CTL
,
283 0x03 | DMA_PACK_SIZE_MASK
, dma_dir
| DMA_EN
| DMA_512
);
284 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
287 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANSFER
,
288 0xFF, MS_TRANSFER_START
| trans_mode
);
289 rtsx_usb_add_cmd(ucr
, CHECK_REG_CMD
, MS_TRANSFER
,
290 MS_TRANSFER_END
, MS_TRANSFER_END
);
292 err
= rtsx_usb_send_cmd(ucr
, flag
| STAGE_MS_STATUS
, 100);
296 err
= rtsx_usb_transfer_data(ucr
, pipe
, sg
, length
,
301 err
= rtsx_usb_get_rsp(ucr
, 3, 15000);
305 if (ucr
->rsp_buf
[0] & MS_TRANSFER_ERR
||
306 ucr
->rsp_buf
[1] & (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
312 ms_clear_error(host
);
316 static int ms_write_bytes(struct rtsx_usb_ms
*host
, u8 tpc
,
317 u8 cfg
, u8 cnt
, u8
*data
, u8
*int_reg
)
319 struct rtsx_ucr
*ucr
= host
->ucr
;
322 dev_dbg(ms_dev(host
), "%s: tpc = 0x%02x\n", __func__
, tpc
);
324 rtsx_usb_init_cmd(ucr
);
326 for (i
= 0; i
< cnt
; i
++)
327 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
328 PPBUF_BASE2
+ i
, 0xFF, data
[i
]);
331 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
332 PPBUF_BASE2
+ i
, 0xFF, 0xFF);
334 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
335 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
336 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
337 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
338 0x01, PINGPONG_BUFFER
);
340 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANSFER
,
341 0xFF, MS_TRANSFER_START
| MS_TM_WRITE_BYTES
);
342 rtsx_usb_add_cmd(ucr
, CHECK_REG_CMD
, MS_TRANSFER
,
343 MS_TRANSFER_END
, MS_TRANSFER_END
);
344 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, MS_TRANS_CFG
, 0, 0);
346 err
= rtsx_usb_send_cmd(ucr
, MODE_CR
, 100);
350 err
= rtsx_usb_get_rsp(ucr
, 2, 5000);
351 if (err
|| (ucr
->rsp_buf
[0] & MS_TRANSFER_ERR
)) {
354 rtsx_usb_ep0_read_register(ucr
, MS_TRANS_CFG
, &val
);
355 dev_dbg(ms_dev(host
), "MS_TRANS_CFG: 0x%02x\n", val
);
358 *int_reg
= val
& 0x0F;
360 ms_print_debug_regs(host
);
362 ms_clear_error(host
);
365 if (val
& MS_CRC16_ERR
)
369 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
))
378 *int_reg
= ucr
->rsp_buf
[1] & 0x0F;
383 static int ms_read_bytes(struct rtsx_usb_ms
*host
, u8 tpc
,
384 u8 cfg
, u8 cnt
, u8
*data
, u8
*int_reg
)
386 struct rtsx_ucr
*ucr
= host
->ucr
;
390 dev_dbg(ms_dev(host
), "%s: tpc = 0x%02x\n", __func__
, tpc
);
392 rtsx_usb_init_cmd(ucr
);
394 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
395 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
396 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
397 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
398 0x01, PINGPONG_BUFFER
);
400 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANSFER
,
401 0xFF, MS_TRANSFER_START
| MS_TM_READ_BYTES
);
402 rtsx_usb_add_cmd(ucr
, CHECK_REG_CMD
, MS_TRANSFER
,
403 MS_TRANSFER_END
, MS_TRANSFER_END
);
404 for (i
= 0; i
< cnt
- 1; i
++)
405 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, PPBUF_BASE2
+ i
, 0, 0);
407 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, PPBUF_BASE2
+ cnt
, 0, 0);
409 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
,
410 PPBUF_BASE2
+ cnt
- 1, 0, 0);
412 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, MS_TRANS_CFG
, 0, 0);
414 err
= rtsx_usb_send_cmd(ucr
, MODE_CR
, 100);
418 err
= rtsx_usb_get_rsp(ucr
, cnt
+ 2, 5000);
419 if (err
|| (ucr
->rsp_buf
[0] & MS_TRANSFER_ERR
)) {
422 rtsx_usb_ep0_read_register(ucr
, MS_TRANS_CFG
, &val
);
423 dev_dbg(ms_dev(host
), "MS_TRANS_CFG: 0x%02x\n", val
);
425 if (int_reg
&& (host
->ifmode
!= MEMSTICK_SERIAL
))
426 *int_reg
= val
& 0x0F;
428 ms_print_debug_regs(host
);
430 ms_clear_error(host
);
433 if (val
& MS_CRC16_ERR
)
437 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
))
445 ptr
= ucr
->rsp_buf
+ 1;
446 for (i
= 0; i
< cnt
; i
++)
450 if (int_reg
&& (host
->ifmode
!= MEMSTICK_SERIAL
))
451 *int_reg
= *ptr
& 0x0F;
456 static int rtsx_usb_ms_issue_cmd(struct rtsx_usb_ms
*host
)
458 struct memstick_request
*req
= host
->req
;
462 dev_dbg(ms_dev(host
), "%s\n", __func__
);
464 if (req
->need_card_int
) {
465 if (host
->ifmode
!= MEMSTICK_SERIAL
)
469 if (req
->long_data
) {
470 err
= ms_transfer_data(host
, req
->data_dir
,
471 req
->tpc
, cfg
, &(req
->sg
));
473 if (req
->data_dir
== READ
)
474 err
= ms_read_bytes(host
, req
->tpc
, cfg
,
475 req
->data_len
, req
->data
, &int_reg
);
477 err
= ms_write_bytes(host
, req
->tpc
, cfg
,
478 req
->data_len
, req
->data
, &int_reg
);
483 if (req
->need_card_int
) {
484 if (host
->ifmode
== MEMSTICK_SERIAL
) {
485 err
= ms_read_bytes(host
, MS_TPC_GET_INT
,
486 NO_WAIT_INT
, 1, &req
->int_reg
, NULL
);
491 if (int_reg
& MS_INT_CMDNK
)
492 req
->int_reg
|= MEMSTICK_INT_CMDNAK
;
493 if (int_reg
& MS_INT_BREQ
)
494 req
->int_reg
|= MEMSTICK_INT_BREQ
;
495 if (int_reg
& MS_INT_ERR
)
496 req
->int_reg
|= MEMSTICK_INT_ERR
;
497 if (int_reg
& MS_INT_CED
)
498 req
->int_reg
|= MEMSTICK_INT_CED
;
500 dev_dbg(ms_dev(host
), "int_reg: 0x%02x\n", req
->int_reg
);
506 static void rtsx_usb_ms_handle_req(struct work_struct
*work
)
508 struct rtsx_usb_ms
*host
= container_of(work
,
509 struct rtsx_usb_ms
, handle_req
);
510 struct rtsx_ucr
*ucr
= host
->ucr
;
511 struct memstick_host
*msh
= host
->msh
;
515 pm_runtime_get_sync(ms_dev(host
));
517 rc
= memstick_next_req(msh
, &host
->req
);
518 dev_dbg(ms_dev(host
), "next req %d\n", rc
);
521 mutex_lock(&ucr
->dev_mutex
);
523 if (rtsx_usb_card_exclusive_check(ucr
,
525 host
->req
->error
= -EIO
;
528 rtsx_usb_ms_issue_cmd(host
);
530 mutex_unlock(&ucr
->dev_mutex
);
532 dev_dbg(ms_dev(host
), "req result %d\n",
536 pm_runtime_put_sync(ms_dev(host
));
541 static void rtsx_usb_ms_request(struct memstick_host
*msh
)
543 struct rtsx_usb_ms
*host
= memstick_priv(msh
);
545 dev_dbg(ms_dev(host
), "--> %s\n", __func__
);
548 schedule_work(&host
->handle_req
);
551 static int rtsx_usb_ms_set_param(struct memstick_host
*msh
,
552 enum memstick_param param
, int value
)
554 struct rtsx_usb_ms
*host
= memstick_priv(msh
);
555 struct rtsx_ucr
*ucr
= host
->ucr
;
556 unsigned int clock
= 0;
560 dev_dbg(ms_dev(host
), "%s: param = %d, value = %d\n",
561 __func__
, param
, value
);
563 pm_runtime_get_sync(ms_dev(host
));
564 mutex_lock(&ucr
->dev_mutex
);
566 err
= rtsx_usb_card_exclusive_check(ucr
, RTSX_USB_MS_CARD
);
572 if (value
== host
->power_mode
)
575 if (value
== MEMSTICK_POWER_ON
) {
576 pm_runtime_get_noresume(ms_dev(host
));
577 err
= ms_power_on(host
);
579 pm_runtime_put_noidle(ms_dev(host
));
580 } else if (value
== MEMSTICK_POWER_OFF
) {
581 err
= ms_power_off(host
);
583 pm_runtime_put_noidle(ms_dev(host
));
587 host
->power_mode
= value
;
590 case MEMSTICK_INTERFACE
:
591 if (value
== MEMSTICK_SERIAL
) {
593 ssc_depth
= SSC_DEPTH_512K
;
594 err
= rtsx_usb_write_register(ucr
, MS_CFG
, 0x5A,
595 MS_BUS_WIDTH_1
| PUSH_TIME_DEFAULT
);
598 } else if (value
== MEMSTICK_PAR4
) {
600 ssc_depth
= SSC_DEPTH_1M
;
602 err
= rtsx_usb_write_register(ucr
, MS_CFG
, 0x5A,
603 MS_BUS_WIDTH_4
| PUSH_TIME_ODD
|
612 err
= rtsx_usb_switch_clock(ucr
, clock
,
613 ssc_depth
, false, true, false);
615 dev_dbg(ms_dev(host
), "switch clock failed\n");
619 host
->ssc_depth
= ssc_depth
;
621 host
->ifmode
= value
;
628 mutex_unlock(&ucr
->dev_mutex
);
629 pm_runtime_put_sync(ms_dev(host
));
632 if (param
== MEMSTICK_POWER
&& value
== MEMSTICK_POWER_ON
) {
633 usleep_range(10000, 12000);
636 schedule_delayed_work(&host
->poll_card
, 100);
639 dev_dbg(ms_dev(host
), "%s: return = %d\n", __func__
, err
);
643 #ifdef CONFIG_PM_SLEEP
644 static int rtsx_usb_ms_suspend(struct device
*dev
)
646 struct rtsx_usb_ms
*host
= dev_get_drvdata(dev
);
647 struct memstick_host
*msh
= host
->msh
;
649 /* Since we use rtsx_usb's resume callback to runtime resume its
650 * children to implement remote wakeup signaling, this causes
651 * rtsx_usb_ms' runtime resume callback runs after its suspend
653 * rtsx_usb_ms_suspend()
655 * -> rtsx_usb_ms_runtime_resume()
656 * -> memstick_detect_change()
660 * To avoid this, skip runtime resume/suspend if system suspend is
664 host
->system_suspending
= true;
665 memstick_suspend_host(msh
);
670 static int rtsx_usb_ms_resume(struct device
*dev
)
672 struct rtsx_usb_ms
*host
= dev_get_drvdata(dev
);
673 struct memstick_host
*msh
= host
->msh
;
675 memstick_resume_host(msh
);
676 host
->system_suspending
= false;
680 #endif /* CONFIG_PM_SLEEP */
683 static int rtsx_usb_ms_runtime_suspend(struct device
*dev
)
685 struct rtsx_usb_ms
*host
= dev_get_drvdata(dev
);
687 if (host
->system_suspending
)
690 if (host
->msh
->card
|| host
->power_mode
!= MEMSTICK_POWER_OFF
)
696 static int rtsx_usb_ms_runtime_resume(struct device
*dev
)
698 struct rtsx_usb_ms
*host
= dev_get_drvdata(dev
);
701 if (host
->system_suspending
)
704 memstick_detect_change(host
->msh
);
708 #endif /* CONFIG_PM */
710 static const struct dev_pm_ops rtsx_usb_ms_pm_ops
= {
711 SET_SYSTEM_SLEEP_PM_OPS(rtsx_usb_ms_suspend
, rtsx_usb_ms_resume
)
712 SET_RUNTIME_PM_OPS(rtsx_usb_ms_runtime_suspend
, rtsx_usb_ms_runtime_resume
, NULL
)
716 static void rtsx_usb_ms_poll_card(struct work_struct
*work
)
718 struct rtsx_usb_ms
*host
= container_of(work
, struct rtsx_usb_ms
,
720 struct rtsx_ucr
*ucr
= host
->ucr
;
724 if (host
->eject
|| host
->power_mode
!= MEMSTICK_POWER_ON
)
727 pm_runtime_get_sync(ms_dev(host
));
728 mutex_lock(&ucr
->dev_mutex
);
730 /* Check pending MS card changes */
731 err
= rtsx_usb_read_register(ucr
, CARD_INT_PEND
, &val
);
733 mutex_unlock(&ucr
->dev_mutex
);
737 /* Clear the pending */
738 rtsx_usb_write_register(ucr
, CARD_INT_PEND
,
739 XD_INT
| MS_INT
| SD_INT
,
740 XD_INT
| MS_INT
| SD_INT
);
742 mutex_unlock(&ucr
->dev_mutex
);
745 dev_dbg(ms_dev(host
), "MS slot change detected\n");
746 memstick_detect_change(host
->msh
);
750 pm_runtime_put_sync(ms_dev(host
));
752 if (!host
->eject
&& host
->power_mode
== MEMSTICK_POWER_ON
)
753 schedule_delayed_work(&host
->poll_card
, 100);
756 static int rtsx_usb_ms_drv_probe(struct platform_device
*pdev
)
758 struct memstick_host
*msh
;
759 struct rtsx_usb_ms
*host
;
760 struct rtsx_ucr
*ucr
;
763 ucr
= usb_get_intfdata(to_usb_interface(pdev
->dev
.parent
));
767 dev_dbg(&(pdev
->dev
),
768 "Realtek USB Memstick controller found\n");
770 msh
= memstick_alloc_host(sizeof(*host
), &pdev
->dev
);
774 host
= memstick_priv(msh
);
778 host
->power_mode
= MEMSTICK_POWER_OFF
;
779 platform_set_drvdata(pdev
, host
);
781 mutex_init(&host
->host_mutex
);
782 INIT_WORK(&host
->handle_req
, rtsx_usb_ms_handle_req
);
784 INIT_DELAYED_WORK(&host
->poll_card
, rtsx_usb_ms_poll_card
);
786 msh
->request
= rtsx_usb_ms_request
;
787 msh
->set_param
= rtsx_usb_ms_set_param
;
788 msh
->caps
= MEMSTICK_CAP_PAR4
;
790 pm_runtime_get_noresume(ms_dev(host
));
791 pm_runtime_set_active(ms_dev(host
));
792 pm_runtime_enable(ms_dev(host
));
794 err
= memstick_add_host(msh
);
798 pm_runtime_put(ms_dev(host
));
802 memstick_free_host(msh
);
803 pm_runtime_disable(ms_dev(host
));
804 pm_runtime_put_noidle(ms_dev(host
));
808 static int rtsx_usb_ms_drv_remove(struct platform_device
*pdev
)
810 struct rtsx_usb_ms
*host
= platform_get_drvdata(pdev
);
811 struct memstick_host
*msh
= host
->msh
;
815 cancel_work_sync(&host
->handle_req
);
817 mutex_lock(&host
->host_mutex
);
819 dev_dbg(ms_dev(host
),
820 "%s: Controller removed during transfer\n",
821 dev_name(&msh
->dev
));
822 host
->req
->error
= -ENOMEDIUM
;
824 err
= memstick_next_req(msh
, &host
->req
);
826 host
->req
->error
= -ENOMEDIUM
;
829 mutex_unlock(&host
->host_mutex
);
831 memstick_remove_host(msh
);
832 memstick_free_host(msh
);
834 /* Balance possible unbalanced usage count
835 * e.g. unconditional module removal
837 if (pm_runtime_active(ms_dev(host
)))
838 pm_runtime_put(ms_dev(host
));
840 pm_runtime_disable(ms_dev(host
));
841 platform_set_drvdata(pdev
, NULL
);
843 dev_dbg(ms_dev(host
),
844 ": Realtek USB Memstick controller has been removed\n");
849 static struct platform_device_id rtsx_usb_ms_ids
[] = {
851 .name
= "rtsx_usb_ms",
856 MODULE_DEVICE_TABLE(platform
, rtsx_usb_ms_ids
);
858 static struct platform_driver rtsx_usb_ms_driver
= {
859 .probe
= rtsx_usb_ms_drv_probe
,
860 .remove
= rtsx_usb_ms_drv_remove
,
861 .id_table
= rtsx_usb_ms_ids
,
863 .name
= "rtsx_usb_ms",
864 .pm
= &rtsx_usb_ms_pm_ops
,
867 module_platform_driver(rtsx_usb_ms_driver
);
869 MODULE_LICENSE("GPL v2");
870 MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>");
871 MODULE_DESCRIPTION("Realtek USB Memstick Card Host Driver");