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/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
;
527 pm_runtime_get_sync(ms_dev(host
));
529 rc
= memstick_next_req(msh
, &host
->req
);
530 dev_dbg(ms_dev(host
), "next req %d\n", rc
);
533 mutex_lock(&ucr
->dev_mutex
);
535 if (rtsx_usb_card_exclusive_check(ucr
,
537 host
->req
->error
= -EIO
;
540 rtsx_usb_ms_issue_cmd(host
);
542 mutex_unlock(&ucr
->dev_mutex
);
544 dev_dbg(ms_dev(host
), "req result %d\n",
548 pm_runtime_put(ms_dev(host
));
553 static void rtsx_usb_ms_request(struct memstick_host
*msh
)
555 struct rtsx_usb_ms
*host
= memstick_priv(msh
);
557 dev_dbg(ms_dev(host
), "--> %s\n", __func__
);
560 schedule_work(&host
->handle_req
);
563 static int rtsx_usb_ms_set_param(struct memstick_host
*msh
,
564 enum memstick_param param
, int value
)
566 struct rtsx_usb_ms
*host
= memstick_priv(msh
);
567 struct rtsx_ucr
*ucr
= host
->ucr
;
568 unsigned int clock
= 0;
572 dev_dbg(ms_dev(host
), "%s: param = %d, value = %d\n",
573 __func__
, param
, value
);
575 pm_runtime_get_sync(ms_dev(host
));
576 mutex_lock(&ucr
->dev_mutex
);
578 err
= rtsx_usb_card_exclusive_check(ucr
, RTSX_USB_MS_CARD
);
584 if (value
== host
->power_mode
)
587 if (value
== MEMSTICK_POWER_ON
) {
588 pm_runtime_get_sync(ms_dev(host
));
589 err
= ms_power_on(host
);
590 } else if (value
== MEMSTICK_POWER_OFF
) {
591 err
= ms_power_off(host
);
593 pm_runtime_put_noidle(ms_dev(host
));
595 pm_runtime_put(ms_dev(host
));
599 host
->power_mode
= value
;
602 case MEMSTICK_INTERFACE
:
603 if (value
== MEMSTICK_SERIAL
) {
605 ssc_depth
= SSC_DEPTH_512K
;
606 err
= rtsx_usb_write_register(ucr
, MS_CFG
, 0x5A,
607 MS_BUS_WIDTH_1
| PUSH_TIME_DEFAULT
);
610 } else if (value
== MEMSTICK_PAR4
) {
612 ssc_depth
= SSC_DEPTH_1M
;
614 err
= rtsx_usb_write_register(ucr
, MS_CFG
, 0x5A,
615 MS_BUS_WIDTH_4
| PUSH_TIME_ODD
|
624 err
= rtsx_usb_switch_clock(ucr
, clock
,
625 ssc_depth
, false, true, false);
627 dev_dbg(ms_dev(host
), "switch clock failed\n");
631 host
->ssc_depth
= ssc_depth
;
633 host
->ifmode
= value
;
640 mutex_unlock(&ucr
->dev_mutex
);
641 pm_runtime_put(ms_dev(host
));
644 if (param
== MEMSTICK_POWER
&& value
== MEMSTICK_POWER_ON
)
645 usleep_range(10000, 12000);
647 dev_dbg(ms_dev(host
), "%s: return = %d\n", __func__
, err
);
651 #ifdef CONFIG_PM_SLEEP
652 static int rtsx_usb_ms_suspend(struct device
*dev
)
654 struct rtsx_usb_ms
*host
= dev_get_drvdata(dev
);
655 struct memstick_host
*msh
= host
->msh
;
657 dev_dbg(ms_dev(host
), "--> %s\n", __func__
);
659 memstick_suspend_host(msh
);
663 static int rtsx_usb_ms_resume(struct device
*dev
)
665 struct rtsx_usb_ms
*host
= dev_get_drvdata(dev
);
666 struct memstick_host
*msh
= host
->msh
;
668 dev_dbg(ms_dev(host
), "--> %s\n", __func__
);
670 memstick_resume_host(msh
);
673 #endif /* CONFIG_PM_SLEEP */
676 * Thread function of ms card slot detection. The thread starts right after
677 * successful host addition. It stops while the driver removal function sets
680 static int rtsx_usb_detect_ms_card(void *__host
)
682 struct rtsx_usb_ms
*host
= (struct rtsx_usb_ms
*)__host
;
683 struct rtsx_ucr
*ucr
= host
->ucr
;
688 pm_runtime_get_sync(ms_dev(host
));
689 mutex_lock(&ucr
->dev_mutex
);
691 /* Check pending MS card changes */
692 err
= rtsx_usb_read_register(ucr
, CARD_INT_PEND
, &val
);
694 mutex_unlock(&ucr
->dev_mutex
);
698 /* Clear the pending */
699 rtsx_usb_write_register(ucr
, CARD_INT_PEND
,
700 XD_INT
| MS_INT
| SD_INT
,
701 XD_INT
| MS_INT
| SD_INT
);
703 mutex_unlock(&ucr
->dev_mutex
);
706 dev_dbg(ms_dev(host
), "MS slot change detected\n");
707 memstick_detect_change(host
->msh
);
711 pm_runtime_put(ms_dev(host
));
715 schedule_timeout_idle(HZ
);
718 complete(&host
->detect_ms_exit
);
722 static int rtsx_usb_ms_drv_probe(struct platform_device
*pdev
)
724 struct memstick_host
*msh
;
725 struct rtsx_usb_ms
*host
;
726 struct rtsx_ucr
*ucr
;
729 ucr
= usb_get_intfdata(to_usb_interface(pdev
->dev
.parent
));
733 dev_dbg(&(pdev
->dev
),
734 "Realtek USB Memstick controller found\n");
736 msh
= memstick_alloc_host(sizeof(*host
), &pdev
->dev
);
740 host
= memstick_priv(msh
);
744 host
->power_mode
= MEMSTICK_POWER_OFF
;
745 platform_set_drvdata(pdev
, host
);
747 mutex_init(&host
->host_mutex
);
748 INIT_WORK(&host
->handle_req
, rtsx_usb_ms_handle_req
);
750 init_completion(&host
->detect_ms_exit
);
751 host
->detect_ms
= kthread_create(rtsx_usb_detect_ms_card
, host
,
752 "rtsx_usb_ms_%d", pdev
->id
);
753 if (IS_ERR(host
->detect_ms
)) {
754 dev_dbg(&(pdev
->dev
),
755 "Unable to create polling thread.\n");
756 err
= PTR_ERR(host
->detect_ms
);
760 msh
->request
= rtsx_usb_ms_request
;
761 msh
->set_param
= rtsx_usb_ms_set_param
;
762 msh
->caps
= MEMSTICK_CAP_PAR4
;
764 pm_runtime_enable(&pdev
->dev
);
765 err
= memstick_add_host(msh
);
769 wake_up_process(host
->detect_ms
);
772 memstick_free_host(msh
);
776 static int rtsx_usb_ms_drv_remove(struct platform_device
*pdev
)
778 struct rtsx_usb_ms
*host
= platform_get_drvdata(pdev
);
779 struct memstick_host
*msh
;
784 cancel_work_sync(&host
->handle_req
);
786 mutex_lock(&host
->host_mutex
);
788 dev_dbg(&(pdev
->dev
),
789 "%s: Controller removed during transfer\n",
790 dev_name(&msh
->dev
));
791 host
->req
->error
= -ENOMEDIUM
;
793 err
= memstick_next_req(msh
, &host
->req
);
795 host
->req
->error
= -ENOMEDIUM
;
798 mutex_unlock(&host
->host_mutex
);
800 wait_for_completion(&host
->detect_ms_exit
);
801 memstick_remove_host(msh
);
802 memstick_free_host(msh
);
804 /* Balance possible unbalanced usage count
805 * e.g. unconditional module removal
807 if (pm_runtime_active(ms_dev(host
)))
808 pm_runtime_put(ms_dev(host
));
810 pm_runtime_disable(&pdev
->dev
);
811 platform_set_drvdata(pdev
, NULL
);
813 dev_dbg(&(pdev
->dev
),
814 ": Realtek USB Memstick controller has been removed\n");
819 static SIMPLE_DEV_PM_OPS(rtsx_usb_ms_pm_ops
,
820 rtsx_usb_ms_suspend
, rtsx_usb_ms_resume
);
822 static struct platform_device_id rtsx_usb_ms_ids
[] = {
824 .name
= "rtsx_usb_ms",
829 MODULE_DEVICE_TABLE(platform
, rtsx_usb_ms_ids
);
831 static struct platform_driver rtsx_usb_ms_driver
= {
832 .probe
= rtsx_usb_ms_drv_probe
,
833 .remove
= rtsx_usb_ms_drv_remove
,
834 .id_table
= rtsx_usb_ms_ids
,
836 .name
= "rtsx_usb_ms",
837 .pm
= &rtsx_usb_ms_pm_ops
,
840 module_platform_driver(rtsx_usb_ms_driver
);
842 MODULE_LICENSE("GPL v2");
843 MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>");
844 MODULE_DESCRIPTION("Realtek USB Memstick Card Host Driver");