1 /* Realtek PCI-Express 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 as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 * Wei WANG <wei_wang@realsil.com.cn>
22 #include <linux/module.h>
23 #include <linux/highmem.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
26 #include <linux/memstick.h>
27 #include <linux/mfd/rtsx_pci.h>
28 #include <asm/unaligned.h>
30 struct realtek_pci_ms
{
31 struct platform_device
*pdev
;
33 struct memstick_host
*msh
;
34 struct memstick_request
*req
;
36 struct mutex host_mutex
;
37 struct work_struct handle_req
;
45 static inline struct device
*ms_dev(struct realtek_pci_ms
*host
)
47 return &(host
->pdev
->dev
);
50 static inline void ms_clear_error(struct realtek_pci_ms
*host
)
52 rtsx_pci_write_register(host
->pcr
, CARD_STOP
,
53 MS_STOP
| MS_CLR_ERR
, MS_STOP
| MS_CLR_ERR
);
58 static void ms_print_debug_regs(struct realtek_pci_ms
*host
)
60 struct rtsx_pcr
*pcr
= host
->pcr
;
64 /* Print MS host internal registers */
65 rtsx_pci_init_cmd(pcr
);
66 for (i
= 0xFD40; i
<= 0xFD44; i
++)
67 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, i
, 0, 0);
68 for (i
= 0xFD52; i
<= 0xFD69; i
++)
69 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, i
, 0, 0);
70 rtsx_pci_send_cmd(pcr
, 100);
72 ptr
= rtsx_pci_get_cmd_data(pcr
);
73 for (i
= 0xFD40; i
<= 0xFD44; i
++)
74 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", i
, *(ptr
++));
75 for (i
= 0xFD52; i
<= 0xFD69; i
++)
76 dev_dbg(ms_dev(host
), "0x%04X: 0x%02x\n", i
, *(ptr
++));
81 #define ms_print_debug_regs(host)
85 static int ms_power_on(struct realtek_pci_ms
*host
)
87 struct rtsx_pcr
*pcr
= host
->pcr
;
90 rtsx_pci_init_cmd(pcr
);
91 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SELECT
, 0x07, MS_MOD_SEL
);
92 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_SHARE_MODE
,
93 CARD_SHARE_MASK
, CARD_SHARE_48_MS
);
94 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
,
95 MS_CLK_EN
, MS_CLK_EN
);
96 err
= rtsx_pci_send_cmd(pcr
, 100);
100 err
= rtsx_pci_card_pull_ctl_enable(pcr
, RTSX_MS_CARD
);
104 err
= rtsx_pci_card_power_on(pcr
, RTSX_MS_CARD
);
108 /* Wait ms power stable */
111 err
= rtsx_pci_write_register(pcr
, CARD_OE
,
112 MS_OUTPUT_EN
, MS_OUTPUT_EN
);
119 static int ms_power_off(struct realtek_pci_ms
*host
)
121 struct rtsx_pcr
*pcr
= host
->pcr
;
124 rtsx_pci_init_cmd(pcr
);
126 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_CLK_EN
, MS_CLK_EN
, 0);
127 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_OE
, MS_OUTPUT_EN
, 0);
129 err
= rtsx_pci_send_cmd(pcr
, 100);
133 err
= rtsx_pci_card_power_off(pcr
, RTSX_MS_CARD
);
137 return rtsx_pci_card_pull_ctl_disable(pcr
, RTSX_MS_CARD
);
140 static int ms_transfer_data(struct realtek_pci_ms
*host
, unsigned char data_dir
,
141 u8 tpc
, u8 cfg
, struct scatterlist
*sg
)
143 struct rtsx_pcr
*pcr
= host
->pcr
;
145 unsigned int length
= sg
->length
;
146 u16 sec_cnt
= (u16
)(length
/ 512);
147 u8 val
, trans_mode
, dma_dir
;
149 dev_dbg(ms_dev(host
), "%s: tpc = 0x%02x, data_dir = %s, length = %d\n",
150 __func__
, tpc
, (data_dir
== READ
) ? "READ" : "WRITE",
153 if (data_dir
== READ
) {
154 dma_dir
= DMA_DIR_FROM_CARD
;
155 trans_mode
= MS_TM_AUTO_READ
;
157 dma_dir
= DMA_DIR_TO_CARD
;
158 trans_mode
= MS_TM_AUTO_WRITE
;
161 rtsx_pci_init_cmd(pcr
);
163 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
164 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_SECTOR_CNT_H
,
165 0xFF, (u8
)(sec_cnt
>> 8));
166 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_SECTOR_CNT_L
,
168 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
170 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, IRQSTAT0
,
171 DMA_DONE_INT
, DMA_DONE_INT
);
172 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC3
, 0xFF, (u8
)(length
>> 24));
173 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC2
, 0xFF, (u8
)(length
>> 16));
174 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC1
, 0xFF, (u8
)(length
>> 8));
175 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMATC0
, 0xFF, (u8
)length
);
176 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, DMACTL
,
177 0x03 | DMA_PACK_SIZE_MASK
, dma_dir
| DMA_EN
| DMA_512
);
178 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
181 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_TRANSFER
,
182 0xFF, MS_TRANSFER_START
| trans_mode
);
183 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, MS_TRANSFER
,
184 MS_TRANSFER_END
, MS_TRANSFER_END
);
186 rtsx_pci_send_cmd_no_wait(pcr
);
188 err
= rtsx_pci_transfer_data(pcr
, sg
, 1, data_dir
== READ
, 10000);
190 ms_clear_error(host
);
194 rtsx_pci_read_register(pcr
, MS_TRANS_CFG
, &val
);
195 if (val
& (MS_INT_CMDNK
| MS_INT_ERR
| MS_CRC16_ERR
| MS_RDY_TIMEOUT
))
201 static int ms_write_bytes(struct realtek_pci_ms
*host
, u8 tpc
,
202 u8 cfg
, u8 cnt
, u8
*data
, u8
*int_reg
)
204 struct rtsx_pcr
*pcr
= host
->pcr
;
207 dev_dbg(ms_dev(host
), "%s: tpc = 0x%02x\n", __func__
, tpc
);
212 rtsx_pci_init_cmd(pcr
);
214 for (i
= 0; i
< cnt
; i
++)
215 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
216 PPBUF_BASE2
+ i
, 0xFF, data
[i
]);
218 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
,
219 PPBUF_BASE2
+ i
, 0xFF, 0xFF);
221 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
222 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
223 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
224 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
225 0x01, PINGPONG_BUFFER
);
227 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_TRANSFER
,
228 0xFF, MS_TRANSFER_START
| MS_TM_WRITE_BYTES
);
229 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, MS_TRANSFER
,
230 MS_TRANSFER_END
, MS_TRANSFER_END
);
232 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, MS_TRANS_CFG
, 0, 0);
234 err
= rtsx_pci_send_cmd(pcr
, 5000);
238 rtsx_pci_read_register(pcr
, MS_TRANS_CFG
, &val
);
239 dev_dbg(ms_dev(host
), "MS_TRANS_CFG: 0x%02x\n", val
);
242 *int_reg
= val
& 0x0F;
244 ms_print_debug_regs(host
);
246 ms_clear_error(host
);
249 if (val
& MS_CRC16_ERR
)
253 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
))
262 u8
*ptr
= rtsx_pci_get_cmd_data(pcr
) + 1;
263 *int_reg
= *ptr
& 0x0F;
269 static int ms_read_bytes(struct realtek_pci_ms
*host
, u8 tpc
,
270 u8 cfg
, u8 cnt
, u8
*data
, u8
*int_reg
)
272 struct rtsx_pcr
*pcr
= host
->pcr
;
276 dev_dbg(ms_dev(host
), "%s: tpc = 0x%02x\n", __func__
, tpc
);
281 rtsx_pci_init_cmd(pcr
);
283 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_TPC
, 0xFF, tpc
);
284 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_BYTE_CNT
, 0xFF, cnt
);
285 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_TRANS_CFG
, 0xFF, cfg
);
286 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
287 0x01, PINGPONG_BUFFER
);
289 rtsx_pci_add_cmd(pcr
, WRITE_REG_CMD
, MS_TRANSFER
,
290 0xFF, MS_TRANSFER_START
| MS_TM_READ_BYTES
);
291 rtsx_pci_add_cmd(pcr
, CHECK_REG_CMD
, MS_TRANSFER
,
292 MS_TRANSFER_END
, MS_TRANSFER_END
);
293 for (i
= 0; i
< cnt
- 1; i
++)
294 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, PPBUF_BASE2
+ i
, 0, 0);
296 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, PPBUF_BASE2
+ cnt
, 0, 0);
298 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
,
299 PPBUF_BASE2
+ cnt
- 1, 0, 0);
301 rtsx_pci_add_cmd(pcr
, READ_REG_CMD
, MS_TRANS_CFG
, 0, 0);
303 err
= rtsx_pci_send_cmd(pcr
, 5000);
307 rtsx_pci_read_register(pcr
, MS_TRANS_CFG
, &val
);
308 dev_dbg(ms_dev(host
), "MS_TRANS_CFG: 0x%02x\n", val
);
311 *int_reg
= val
& 0x0F;
313 ms_print_debug_regs(host
);
315 ms_clear_error(host
);
318 if (val
& MS_CRC16_ERR
)
322 if (val
& (MS_INT_ERR
| MS_INT_CMDNK
))
330 ptr
= rtsx_pci_get_cmd_data(pcr
) + 1;
331 for (i
= 0; i
< cnt
; i
++)
335 *int_reg
= *ptr
& 0x0F;
340 static int rtsx_pci_ms_issue_cmd(struct realtek_pci_ms
*host
)
342 struct memstick_request
*req
= host
->req
;
346 dev_dbg(ms_dev(host
), "%s\n", __func__
);
348 if (req
->need_card_int
) {
349 if (host
->ifmode
!= MEMSTICK_SERIAL
)
353 if (req
->long_data
) {
354 err
= ms_transfer_data(host
, req
->data_dir
,
355 req
->tpc
, cfg
, &(req
->sg
));
357 if (req
->data_dir
== READ
) {
358 err
= ms_read_bytes(host
, req
->tpc
, cfg
,
359 req
->data_len
, req
->data
, &int_reg
);
361 err
= ms_write_bytes(host
, req
->tpc
, cfg
,
362 req
->data_len
, req
->data
, &int_reg
);
368 if (req
->need_card_int
&& (host
->ifmode
== MEMSTICK_SERIAL
)) {
369 err
= ms_read_bytes(host
, MS_TPC_GET_INT
,
370 NO_WAIT_INT
, 1, &int_reg
, NULL
);
375 if (req
->need_card_int
) {
376 dev_dbg(ms_dev(host
), "int_reg: 0x%02x\n", int_reg
);
378 if (int_reg
& MS_INT_CMDNK
)
379 req
->int_reg
|= MEMSTICK_INT_CMDNAK
;
380 if (int_reg
& MS_INT_BREQ
)
381 req
->int_reg
|= MEMSTICK_INT_BREQ
;
382 if (int_reg
& MS_INT_ERR
)
383 req
->int_reg
|= MEMSTICK_INT_ERR
;
384 if (int_reg
& MS_INT_CED
)
385 req
->int_reg
|= MEMSTICK_INT_CED
;
391 static void rtsx_pci_ms_handle_req(struct work_struct
*work
)
393 struct realtek_pci_ms
*host
= container_of(work
,
394 struct realtek_pci_ms
, handle_req
);
395 struct rtsx_pcr
*pcr
= host
->pcr
;
396 struct memstick_host
*msh
= host
->msh
;
399 mutex_lock(&pcr
->pcr_mutex
);
401 rtsx_pci_start_run(pcr
);
403 rtsx_pci_switch_clock(host
->pcr
, host
->clock
, host
->ssc_depth
,
405 rtsx_pci_write_register(pcr
, CARD_SELECT
, 0x07, MS_MOD_SEL
);
406 rtsx_pci_write_register(pcr
, CARD_SHARE_MODE
,
407 CARD_SHARE_MASK
, CARD_SHARE_48_MS
);
411 rc
= memstick_next_req(msh
, &host
->req
);
412 dev_dbg(ms_dev(host
), "next req %d\n", rc
);
415 host
->req
->error
= rtsx_pci_ms_issue_cmd(host
);
419 mutex_unlock(&pcr
->pcr_mutex
);
422 static void rtsx_pci_ms_request(struct memstick_host
*msh
)
424 struct realtek_pci_ms
*host
= memstick_priv(msh
);
426 dev_dbg(ms_dev(host
), "--> %s\n", __func__
);
428 if (rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_MS_CARD
))
431 schedule_work(&host
->handle_req
);
434 static int rtsx_pci_ms_set_param(struct memstick_host
*msh
,
435 enum memstick_param param
, int value
)
437 struct realtek_pci_ms
*host
= memstick_priv(msh
);
438 struct rtsx_pcr
*pcr
= host
->pcr
;
439 unsigned int clock
= 0;
443 dev_dbg(ms_dev(host
), "%s: param = %d, value = %d\n",
444 __func__
, param
, value
);
446 err
= rtsx_pci_card_exclusive_check(host
->pcr
, RTSX_MS_CARD
);
452 if (value
== MEMSTICK_POWER_ON
)
453 err
= ms_power_on(host
);
454 else if (value
== MEMSTICK_POWER_OFF
)
455 err
= ms_power_off(host
);
460 case MEMSTICK_INTERFACE
:
461 if (value
== MEMSTICK_SERIAL
) {
463 ssc_depth
= RTSX_SSC_DEPTH_500K
;
465 err
= rtsx_pci_write_register(pcr
, MS_CFG
,
466 0x18, MS_BUS_WIDTH_1
);
469 } else if (value
== MEMSTICK_PAR4
) {
471 ssc_depth
= RTSX_SSC_DEPTH_1M
;
473 err
= rtsx_pci_write_register(pcr
, MS_CFG
,
474 0x58, MS_BUS_WIDTH_4
| PUSH_TIME_ODD
);
481 err
= rtsx_pci_switch_clock(pcr
, clock
,
482 ssc_depth
, false, true, false);
486 host
->ssc_depth
= ssc_depth
;
488 host
->ifmode
= value
;
497 static int rtsx_pci_ms_suspend(struct platform_device
*pdev
, pm_message_t state
)
499 struct realtek_pci_ms
*host
= platform_get_drvdata(pdev
);
500 struct memstick_host
*msh
= host
->msh
;
502 dev_dbg(ms_dev(host
), "--> %s\n", __func__
);
504 memstick_suspend_host(msh
);
508 static int rtsx_pci_ms_resume(struct platform_device
*pdev
)
510 struct realtek_pci_ms
*host
= platform_get_drvdata(pdev
);
511 struct memstick_host
*msh
= host
->msh
;
513 dev_dbg(ms_dev(host
), "--> %s\n", __func__
);
515 memstick_resume_host(msh
);
519 #else /* CONFIG_PM */
521 #define rtsx_pci_ms_suspend NULL
522 #define rtsx_pci_ms_resume NULL
524 #endif /* CONFIG_PM */
526 static void rtsx_pci_ms_card_event(struct platform_device
*pdev
)
528 struct realtek_pci_ms
*host
= platform_get_drvdata(pdev
);
530 memstick_detect_change(host
->msh
);
533 static int rtsx_pci_ms_drv_probe(struct platform_device
*pdev
)
535 struct memstick_host
*msh
;
536 struct realtek_pci_ms
*host
;
537 struct rtsx_pcr
*pcr
;
538 struct pcr_handle
*handle
= pdev
->dev
.platform_data
;
548 dev_dbg(&(pdev
->dev
),
549 ": Realtek PCI-E Memstick controller found\n");
551 msh
= memstick_alloc_host(sizeof(*host
), &pdev
->dev
);
555 host
= memstick_priv(msh
);
559 platform_set_drvdata(pdev
, host
);
560 pcr
->slots
[RTSX_MS_CARD
].p_dev
= pdev
;
561 pcr
->slots
[RTSX_MS_CARD
].card_event
= rtsx_pci_ms_card_event
;
563 mutex_init(&host
->host_mutex
);
565 INIT_WORK(&host
->handle_req
, rtsx_pci_ms_handle_req
);
566 msh
->request
= rtsx_pci_ms_request
;
567 msh
->set_param
= rtsx_pci_ms_set_param
;
568 msh
->caps
= MEMSTICK_CAP_PAR4
;
570 rc
= memstick_add_host(msh
);
572 memstick_free_host(msh
);
579 static int rtsx_pci_ms_drv_remove(struct platform_device
*pdev
)
581 struct realtek_pci_ms
*host
= platform_get_drvdata(pdev
);
582 struct rtsx_pcr
*pcr
;
583 struct memstick_host
*msh
;
590 pcr
->slots
[RTSX_MS_CARD
].p_dev
= NULL
;
591 pcr
->slots
[RTSX_MS_CARD
].card_event
= NULL
;
595 mutex_lock(&host
->host_mutex
);
597 dev_dbg(&(pdev
->dev
),
598 "%s: Controller removed during transfer\n",
599 dev_name(&msh
->dev
));
601 rtsx_pci_complete_unfinished_transfer(pcr
);
603 host
->req
->error
= -ENOMEDIUM
;
605 rc
= memstick_next_req(msh
, &host
->req
);
607 host
->req
->error
= -ENOMEDIUM
;
610 mutex_unlock(&host
->host_mutex
);
612 memstick_remove_host(msh
);
613 memstick_free_host(msh
);
615 platform_set_drvdata(pdev
, NULL
);
617 dev_dbg(&(pdev
->dev
),
618 ": Realtek PCI-E Memstick controller has been removed\n");
623 static struct platform_device_id rtsx_pci_ms_ids
[] = {
625 .name
= DRV_NAME_RTSX_PCI_MS
,
630 MODULE_DEVICE_TABLE(platform
, rtsx_pci_ms_ids
);
632 static struct platform_driver rtsx_pci_ms_driver
= {
633 .probe
= rtsx_pci_ms_drv_probe
,
634 .remove
= rtsx_pci_ms_drv_remove
,
635 .id_table
= rtsx_pci_ms_ids
,
636 .suspend
= rtsx_pci_ms_suspend
,
637 .resume
= rtsx_pci_ms_resume
,
639 .owner
= THIS_MODULE
,
640 .name
= DRV_NAME_RTSX_PCI_MS
,
643 module_platform_driver(rtsx_pci_ms_driver
);
645 MODULE_LICENSE("GPL");
646 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
647 MODULE_DESCRIPTION("Realtek PCI-E Memstick Card Host Driver");