1 /* Realtek USB Memstick Card Interface driver
3 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, see <http://www.gnu.org/licenses/>.
18 * Roger Tseng <rogerable@realtek.com>
21 #include <linux/module.h>
22 #include <linux/highmem.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/workqueue.h>
26 #include <linux/memstick.h>
27 #include <linux/kthread.h>
28 #include <linux/mfd/rtsx_usb.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/mutex.h>
31 #include <linux/sched.h>
32 #include <linux/completion.h>
33 #include <asm/unaligned.h>
36 struct platform_device
*pdev
;
38 struct memstick_host
*msh
;
39 struct memstick_request
*req
;
41 struct mutex host_mutex
;
42 struct work_struct handle_req
;
44 struct task_struct
*detect_ms
;
45 struct completion detect_ms_exit
;
54 static inline struct device
*ms_dev(struct rtsx_usb_ms
*host
)
56 return &(host
->pdev
->dev
);
59 static inline void ms_clear_error(struct rtsx_usb_ms
*host
)
61 struct rtsx_ucr
*ucr
= host
->ucr
;
62 rtsx_usb_ep0_write_register(ucr
, CARD_STOP
,
64 MS_STOP
| MS_CLR_ERR
);
66 rtsx_usb_clear_dma_err(ucr
);
67 rtsx_usb_clear_fsm_err(ucr
);
72 static void ms_print_debug_regs(struct rtsx_usb_ms
*host
)
74 struct rtsx_ucr
*ucr
= host
->ucr
;
78 /* Print MS host internal registers */
79 rtsx_usb_init_cmd(ucr
);
81 /* MS_CFG to MS_INT_REG */
82 for (i
= 0xFD40; i
<= 0xFD44; i
++)
83 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, i
, 0, 0);
85 /* CARD_SHARE_MODE to CARD_GPIO */
86 for (i
= 0xFD51; i
<= 0xFD56; i
++)
87 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, i
, 0, 0);
90 for (i
= 0xFD60; i
<= 0xFD65; i
++)
91 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, i
, 0, 0);
93 /* CARD_DATA_SOURCE, CARD_SELECT, CARD_CLK_EN, CARD_PWR_CTL */
94 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, CARD_DATA_SOURCE
, 0, 0);
95 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, CARD_SELECT
, 0, 0);
96 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, CARD_CLK_EN
, 0, 0);
97 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, CARD_PWR_CTL
, 0, 0);
99 rtsx_usb_send_cmd(ucr
, MODE_CR
, 100);
100 rtsx_usb_get_rsp(ucr
, 21, 100);
103 for (i
= 0xFD40; i
<= 0xFD44; i
++)
104 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", i
, *(ptr
++));
105 for (i
= 0xFD51; i
<= 0xFD56; i
++)
106 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", i
, *(ptr
++));
107 for (i
= 0xFD60; i
<= 0xFD65; i
++)
108 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", i
, *(ptr
++));
110 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", CARD_DATA_SOURCE
, *(ptr
++));
111 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", CARD_SELECT
, *(ptr
++));
112 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", CARD_CLK_EN
, *(ptr
++));
113 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", CARD_PWR_CTL
, *(ptr
++));
118 static void ms_print_debug_regs(struct rtsx_usb_ms
*host
)
124 static int ms_pull_ctl_disable_lqfp48(struct rtsx_ucr
*ucr
)
126 rtsx_usb_init_cmd(ucr
);
128 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
129 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
130 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
131 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
132 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
133 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
135 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
138 static int ms_pull_ctl_disable_qfn24(struct rtsx_ucr
*ucr
)
140 rtsx_usb_init_cmd(ucr
);
142 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x65);
143 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
144 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
145 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
146 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x56);
147 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
149 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
152 static int ms_pull_ctl_enable_lqfp48(struct rtsx_ucr
*ucr
)
154 rtsx_usb_init_cmd(ucr
);
156 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x55);
157 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
158 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
159 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
160 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
161 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0xA5);
163 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
166 static int ms_pull_ctl_enable_qfn24(struct rtsx_ucr
*ucr
)
168 rtsx_usb_init_cmd(ucr
);
170 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL1
, 0xFF, 0x65);
171 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL2
, 0xFF, 0x55);
172 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL3
, 0xFF, 0x95);
173 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL4
, 0xFF, 0x55);
174 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL5
, 0xFF, 0x55);
175 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PULL_CTL6
, 0xFF, 0x59);
177 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
180 static int ms_power_on(struct rtsx_usb_ms
*host
)
182 struct rtsx_ucr
*ucr
= host
->ucr
;
185 dev_dbg(ms_dev(host
), "%s\n", __func__
);
187 rtsx_usb_init_cmd(ucr
);
188 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_SELECT
, 0x07, MS_MOD_SEL
);
189 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_SHARE_MODE
,
190 CARD_SHARE_MASK
, CARD_SHARE_MS
);
191 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_CLK_EN
,
192 MS_CLK_EN
, MS_CLK_EN
);
193 err
= rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
197 if (CHECK_PKG(ucr
, LQFP48
))
198 err
= ms_pull_ctl_enable_lqfp48(ucr
);
200 err
= ms_pull_ctl_enable_qfn24(ucr
);
204 err
= rtsx_usb_write_register(ucr
, CARD_PWR_CTL
,
205 POWER_MASK
, PARTIAL_POWER_ON
);
209 usleep_range(800, 1000);
211 rtsx_usb_init_cmd(ucr
);
212 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_PWR_CTL
,
213 POWER_MASK
, POWER_ON
);
214 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_OE
,
215 MS_OUTPUT_EN
, MS_OUTPUT_EN
);
217 return rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
220 static int ms_power_off(struct rtsx_usb_ms
*host
)
222 struct rtsx_ucr
*ucr
= host
->ucr
;
225 dev_dbg(ms_dev(host
), "%s\n", __func__
);
227 rtsx_usb_init_cmd(ucr
);
229 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_CLK_EN
, MS_CLK_EN
, 0);
230 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_OE
, MS_OUTPUT_EN
, 0);
232 err
= rtsx_usb_send_cmd(ucr
, MODE_C
, 100);
236 if (CHECK_PKG(ucr
, LQFP48
))
237 return ms_pull_ctl_disable_lqfp48(ucr
);
239 return ms_pull_ctl_disable_qfn24(ucr
);
242 static int ms_transfer_data(struct rtsx_usb_ms
*host
, unsigned char data_dir
,
243 u8 tpc
, u8 cfg
, struct scatterlist
*sg
)
245 struct rtsx_ucr
*ucr
= host
->ucr
;
247 unsigned int length
= sg
->length
;
248 u16 sec_cnt
= (u16
)(length
/ 512);
249 u8 trans_mode
, dma_dir
, flag
;
251 struct memstick_dev
*card
= host
->msh
->card
;
253 dev_dbg(ms_dev(host
), "%s: tpc = 0x%02x, data_dir = %s, length = %d\n",
254 __func__
, tpc
, (data_dir
== READ
) ? "READ" : "WRITE",
257 if (data_dir
== READ
) {
259 dma_dir
= DMA_DIR_FROM_CARD
;
260 if (card
->id
.type
!= MEMSTICK_TYPE_PRO
)
261 trans_mode
= MS_TM_NORMAL_READ
;
263 trans_mode
= MS_TM_AUTO_READ
;
264 pipe
= usb_rcvbulkpipe(ucr
->pusb_dev
, EP_BULK_IN
);
267 dma_dir
= DMA_DIR_TO_CARD
;
268 if (card
->id
.type
!= MEMSTICK_TYPE_PRO
)
269 trans_mode
= MS_TM_NORMAL_WRITE
;
271 trans_mode
= MS_TM_AUTO_WRITE
;
272 pipe
= usb_sndbulkpipe(ucr
->pusb_dev
, EP_BULK_OUT
);
275 rtsx_usb_init_cmd(ucr
);
277 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
278 if (card
->id
.type
== MEMSTICK_TYPE_PRO
) {
279 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_SECTOR_CNT_H
,
280 0xFF, (u8
)(sec_cnt
>> 8));
281 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
,
284 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
286 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC3
,
287 0xFF, (u8
)(length
>> 24));
288 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC2
,
289 0xFF, (u8
)(length
>> 16));
290 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC1
,
291 0xFF, (u8
)(length
>> 8));
292 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_TC0
, 0xFF,
294 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MC_DMA_CTL
,
295 0x03 | DMA_PACK_SIZE_MASK
, dma_dir
| DMA_EN
| DMA_512
);
296 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
299 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANSFER
,
300 0xFF, MS_TRANSFER_START
| trans_mode
);
301 rtsx_usb_add_cmd(ucr
, CHECK_REG_CMD
, MS_TRANSFER
,
302 MS_TRANSFER_END
, MS_TRANSFER_END
);
304 err
= rtsx_usb_send_cmd(ucr
, flag
| STAGE_MS_STATUS
, 100);
308 err
= rtsx_usb_transfer_data(ucr
, pipe
, sg
, length
,
313 err
= rtsx_usb_get_rsp(ucr
, 3, 15000);
317 if (ucr
->rsp_buf
[0] & MS_TRANSFER_ERR
||
318 ucr
->rsp_buf
[1] & (MS_CRC16_ERR
| MS_RDY_TIMEOUT
)) {
324 ms_clear_error(host
);
328 static int ms_write_bytes(struct rtsx_usb_ms
*host
, u8 tpc
,
329 u8 cfg
, u8 cnt
, u8
*data
, u8
*int_reg
)
331 struct rtsx_ucr
*ucr
= host
->ucr
;
334 dev_dbg(ms_dev(host
), "%s: tpc = 0x%02x\n", __func__
, tpc
);
336 rtsx_usb_init_cmd(ucr
);
338 for (i
= 0; i
< cnt
; i
++)
339 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
340 PPBUF_BASE2
+ i
, 0xFF, data
[i
]);
343 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
,
344 PPBUF_BASE2
+ i
, 0xFF, 0xFF);
346 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
347 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
348 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
349 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
350 0x01, PINGPONG_BUFFER
);
352 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANSFER
,
353 0xFF, MS_TRANSFER_START
| MS_TM_WRITE_BYTES
);
354 rtsx_usb_add_cmd(ucr
, CHECK_REG_CMD
, MS_TRANSFER
,
355 MS_TRANSFER_END
, MS_TRANSFER_END
);
356 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, MS_TRANS_CFG
, 0, 0);
358 err
= rtsx_usb_send_cmd(ucr
, MODE_CR
, 100);
362 err
= rtsx_usb_get_rsp(ucr
, 2, 5000);
363 if (err
|| (ucr
->rsp_buf
[0] & MS_TRANSFER_ERR
)) {
366 rtsx_usb_ep0_read_register(ucr
, MS_TRANS_CFG
, &val
);
367 dev_dbg(ms_dev(host
), "MS_TRANS_CFG: 0x%02x\n", val
);
370 *int_reg
= val
& 0x0F;
372 ms_print_debug_regs(host
);
374 ms_clear_error(host
);
377 if (val
& MS_CRC16_ERR
)
381 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
))
390 *int_reg
= ucr
->rsp_buf
[1] & 0x0F;
395 static int ms_read_bytes(struct rtsx_usb_ms
*host
, u8 tpc
,
396 u8 cfg
, u8 cnt
, u8
*data
, u8
*int_reg
)
398 struct rtsx_ucr
*ucr
= host
->ucr
;
402 dev_dbg(ms_dev(host
), "%s: tpc = 0x%02x\n", __func__
, tpc
);
404 rtsx_usb_init_cmd(ucr
);
406 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
407 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
408 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
409 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
410 0x01, PINGPONG_BUFFER
);
412 rtsx_usb_add_cmd(ucr
, WRITE_REG_CMD
, MS_TRANSFER
,
413 0xFF, MS_TRANSFER_START
| MS_TM_READ_BYTES
);
414 rtsx_usb_add_cmd(ucr
, CHECK_REG_CMD
, MS_TRANSFER
,
415 MS_TRANSFER_END
, MS_TRANSFER_END
);
416 for (i
= 0; i
< cnt
- 1; i
++)
417 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, PPBUF_BASE2
+ i
, 0, 0);
419 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, PPBUF_BASE2
+ cnt
, 0, 0);
421 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
,
422 PPBUF_BASE2
+ cnt
- 1, 0, 0);
424 rtsx_usb_add_cmd(ucr
, READ_REG_CMD
, MS_TRANS_CFG
, 0, 0);
426 err
= rtsx_usb_send_cmd(ucr
, MODE_CR
, 100);
430 err
= rtsx_usb_get_rsp(ucr
, cnt
+ 2, 5000);
431 if (err
|| (ucr
->rsp_buf
[0] & MS_TRANSFER_ERR
)) {
434 rtsx_usb_ep0_read_register(ucr
, MS_TRANS_CFG
, &val
);
435 dev_dbg(ms_dev(host
), "MS_TRANS_CFG: 0x%02x\n", val
);
437 if (int_reg
&& (host
->ifmode
!= MEMSTICK_SERIAL
))
438 *int_reg
= val
& 0x0F;
440 ms_print_debug_regs(host
);
442 ms_clear_error(host
);
445 if (val
& MS_CRC16_ERR
)
449 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
))
457 ptr
= ucr
->rsp_buf
+ 1;
458 for (i
= 0; i
< cnt
; i
++)
462 if (int_reg
&& (host
->ifmode
!= MEMSTICK_SERIAL
))
463 *int_reg
= *ptr
& 0x0F;
468 static int rtsx_usb_ms_issue_cmd(struct rtsx_usb_ms
*host
)
470 struct memstick_request
*req
= host
->req
;
474 dev_dbg(ms_dev(host
), "%s\n", __func__
);
476 if (req
->need_card_int
) {
477 if (host
->ifmode
!= MEMSTICK_SERIAL
)
481 if (req
->long_data
) {
482 err
= ms_transfer_data(host
, req
->data_dir
,
483 req
->tpc
, cfg
, &(req
->sg
));
485 if (req
->data_dir
== READ
)
486 err
= ms_read_bytes(host
, req
->tpc
, cfg
,
487 req
->data_len
, req
->data
, &int_reg
);
489 err
= ms_write_bytes(host
, req
->tpc
, cfg
,
490 req
->data_len
, req
->data
, &int_reg
);
495 if (req
->need_card_int
) {
496 if (host
->ifmode
== MEMSTICK_SERIAL
) {
497 err
= ms_read_bytes(host
, MS_TPC_GET_INT
,
498 NO_WAIT_INT
, 1, &req
->int_reg
, NULL
);
503 if (int_reg
& MS_INT_CMDNK
)
504 req
->int_reg
|= MEMSTICK_INT_CMDNAK
;
505 if (int_reg
& MS_INT_BREQ
)
506 req
->int_reg
|= MEMSTICK_INT_BREQ
;
507 if (int_reg
& MS_INT_ERR
)
508 req
->int_reg
|= MEMSTICK_INT_ERR
;
509 if (int_reg
& MS_INT_CED
)
510 req
->int_reg
|= MEMSTICK_INT_CED
;
512 dev_dbg(ms_dev(host
), "int_reg: 0x%02x\n", req
->int_reg
);
518 static void rtsx_usb_ms_handle_req(struct work_struct
*work
)
520 struct rtsx_usb_ms
*host
= container_of(work
,
521 struct rtsx_usb_ms
, handle_req
);
522 struct rtsx_ucr
*ucr
= host
->ucr
;
523 struct memstick_host
*msh
= host
->msh
;
528 rc
= memstick_next_req(msh
, &host
->req
);
529 dev_dbg(ms_dev(host
), "next req %d\n", rc
);
532 mutex_lock(&ucr
->dev_mutex
);
534 if (rtsx_usb_card_exclusive_check(ucr
,
536 host
->req
->error
= -EIO
;
539 rtsx_usb_ms_issue_cmd(host
);
541 mutex_unlock(&ucr
->dev_mutex
);
543 dev_dbg(ms_dev(host
), "req result %d\n",
551 static void rtsx_usb_ms_request(struct memstick_host
*msh
)
553 struct rtsx_usb_ms
*host
= memstick_priv(msh
);
555 dev_dbg(ms_dev(host
), "--> %s\n", __func__
);
558 schedule_work(&host
->handle_req
);
561 static int rtsx_usb_ms_set_param(struct memstick_host
*msh
,
562 enum memstick_param param
, int value
)
564 struct rtsx_usb_ms
*host
= memstick_priv(msh
);
565 struct rtsx_ucr
*ucr
= host
->ucr
;
566 unsigned int clock
= 0;
570 dev_dbg(ms_dev(host
), "%s: param = %d, value = %d\n",
571 __func__
, param
, value
);
573 mutex_lock(&ucr
->dev_mutex
);
575 err
= rtsx_usb_card_exclusive_check(ucr
, RTSX_USB_MS_CARD
);
581 if (value
== host
->power_mode
)
584 if (value
== MEMSTICK_POWER_ON
) {
585 pm_runtime_get_sync(ms_dev(host
));
586 err
= ms_power_on(host
);
587 } else if (value
== MEMSTICK_POWER_OFF
) {
588 err
= ms_power_off(host
);
590 pm_runtime_put_noidle(ms_dev(host
));
592 pm_runtime_put(ms_dev(host
));
596 host
->power_mode
= value
;
599 case MEMSTICK_INTERFACE
:
600 if (value
== MEMSTICK_SERIAL
) {
602 ssc_depth
= SSC_DEPTH_512K
;
603 err
= rtsx_usb_write_register(ucr
, MS_CFG
, 0x5A,
604 MS_BUS_WIDTH_1
| PUSH_TIME_DEFAULT
);
607 } else if (value
== MEMSTICK_PAR4
) {
609 ssc_depth
= SSC_DEPTH_1M
;
611 err
= rtsx_usb_write_register(ucr
, MS_CFG
, 0x5A,
612 MS_BUS_WIDTH_4
| PUSH_TIME_ODD
|
621 err
= rtsx_usb_switch_clock(ucr
, clock
,
622 ssc_depth
, false, true, false);
624 dev_dbg(ms_dev(host
), "switch clock failed\n");
628 host
->ssc_depth
= ssc_depth
;
630 host
->ifmode
= value
;
637 mutex_unlock(&ucr
->dev_mutex
);
640 if (param
== MEMSTICK_POWER
&& value
== MEMSTICK_POWER_ON
)
641 usleep_range(10000, 12000);
643 dev_dbg(ms_dev(host
), "%s: return = %d\n", __func__
, err
);
647 #ifdef CONFIG_PM_SLEEP
648 static int rtsx_usb_ms_suspend(struct device
*dev
)
650 struct rtsx_usb_ms
*host
= dev_get_drvdata(dev
);
651 struct memstick_host
*msh
= host
->msh
;
653 dev_dbg(ms_dev(host
), "--> %s\n", __func__
);
655 memstick_suspend_host(msh
);
659 static int rtsx_usb_ms_resume(struct device
*dev
)
661 struct rtsx_usb_ms
*host
= dev_get_drvdata(dev
);
662 struct memstick_host
*msh
= host
->msh
;
664 dev_dbg(ms_dev(host
), "--> %s\n", __func__
);
666 memstick_resume_host(msh
);
669 #endif /* CONFIG_PM_SLEEP */
672 * Thread function of ms card slot detection. The thread starts right after
673 * successful host addition. It stops while the driver removal function sets
676 static int rtsx_usb_detect_ms_card(void *__host
)
678 struct rtsx_usb_ms
*host
= (struct rtsx_usb_ms
*)__host
;
679 struct rtsx_ucr
*ucr
= host
->ucr
;
684 mutex_lock(&ucr
->dev_mutex
);
686 /* Check pending MS card changes */
687 err
= rtsx_usb_read_register(ucr
, CARD_INT_PEND
, &val
);
689 mutex_unlock(&ucr
->dev_mutex
);
693 /* Clear the pending */
694 rtsx_usb_write_register(ucr
, CARD_INT_PEND
,
695 XD_INT
| MS_INT
| SD_INT
,
696 XD_INT
| MS_INT
| SD_INT
);
698 mutex_unlock(&ucr
->dev_mutex
);
701 dev_dbg(ms_dev(host
), "MS slot change detected\n");
702 memstick_detect_change(host
->msh
);
712 complete(&host
->detect_ms_exit
);
716 static int rtsx_usb_ms_drv_probe(struct platform_device
*pdev
)
718 struct memstick_host
*msh
;
719 struct rtsx_usb_ms
*host
;
720 struct rtsx_ucr
*ucr
;
723 ucr
= usb_get_intfdata(to_usb_interface(pdev
->dev
.parent
));
727 dev_dbg(&(pdev
->dev
),
728 "Realtek USB Memstick controller found\n");
730 msh
= memstick_alloc_host(sizeof(*host
), &pdev
->dev
);
734 host
= memstick_priv(msh
);
738 host
->power_mode
= MEMSTICK_POWER_OFF
;
739 platform_set_drvdata(pdev
, host
);
741 mutex_init(&host
->host_mutex
);
742 INIT_WORK(&host
->handle_req
, rtsx_usb_ms_handle_req
);
744 init_completion(&host
->detect_ms_exit
);
745 host
->detect_ms
= kthread_create(rtsx_usb_detect_ms_card
, host
,
746 "rtsx_usb_ms_%d", pdev
->id
);
747 if (IS_ERR(host
->detect_ms
)) {
748 dev_dbg(&(pdev
->dev
),
749 "Unable to create polling thread.\n");
750 err
= PTR_ERR(host
->detect_ms
);
754 msh
->request
= rtsx_usb_ms_request
;
755 msh
->set_param
= rtsx_usb_ms_set_param
;
756 msh
->caps
= MEMSTICK_CAP_PAR4
;
758 pm_runtime_enable(&pdev
->dev
);
759 err
= memstick_add_host(msh
);
763 wake_up_process(host
->detect_ms
);
766 memstick_free_host(msh
);
770 static int rtsx_usb_ms_drv_remove(struct platform_device
*pdev
)
772 struct rtsx_usb_ms
*host
= platform_get_drvdata(pdev
);
773 struct memstick_host
*msh
;
778 cancel_work_sync(&host
->handle_req
);
780 mutex_lock(&host
->host_mutex
);
782 dev_dbg(&(pdev
->dev
),
783 "%s: Controller removed during transfer\n",
784 dev_name(&msh
->dev
));
785 host
->req
->error
= -ENOMEDIUM
;
787 err
= memstick_next_req(msh
, &host
->req
);
789 host
->req
->error
= -ENOMEDIUM
;
792 mutex_unlock(&host
->host_mutex
);
794 wait_for_completion(&host
->detect_ms_exit
);
795 memstick_remove_host(msh
);
796 memstick_free_host(msh
);
798 /* Balance possible unbalanced usage count
799 * e.g. unconditional module removal
801 if (pm_runtime_active(ms_dev(host
)))
802 pm_runtime_put(ms_dev(host
));
804 pm_runtime_disable(&pdev
->dev
);
805 platform_set_drvdata(pdev
, NULL
);
807 dev_dbg(&(pdev
->dev
),
808 ": Realtek USB Memstick controller has been removed\n");
813 static SIMPLE_DEV_PM_OPS(rtsx_usb_ms_pm_ops
,
814 rtsx_usb_ms_suspend
, rtsx_usb_ms_resume
);
816 static struct platform_device_id rtsx_usb_ms_ids
[] = {
818 .name
= "rtsx_usb_ms",
823 MODULE_DEVICE_TABLE(platform
, rtsx_usb_ms_ids
);
825 static struct platform_driver rtsx_usb_ms_driver
= {
826 .probe
= rtsx_usb_ms_drv_probe
,
827 .remove
= rtsx_usb_ms_drv_remove
,
828 .id_table
= rtsx_usb_ms_ids
,
830 .name
= "rtsx_usb_ms",
831 .pm
= &rtsx_usb_ms_pm_ops
,
834 module_platform_driver(rtsx_usb_ms_driver
);
836 MODULE_LICENSE("GPL v2");
837 MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>");
838 MODULE_DESCRIPTION("Realtek USB Memstick Card Host Driver");