1 /* Realtek PCI-Express Memstick Card Interface driver
3 * Copyright(c) 2009 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>
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
23 #include <linux/module.h>
24 #include <linux/highmem.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
27 #include <linux/memstick.h>
28 #include <linux/mfd/rtsx_pci.h>
29 #include <asm/unaligned.h>
31 struct realtek_pci_ms {
32 struct platform_device *pdev;
34 struct memstick_host *msh;
35 struct memstick_request *req;
37 struct mutex host_mutex;
38 struct work_struct handle_req;
46 static inline struct device *ms_dev(struct realtek_pci_ms *host)
48 return &(host->pdev->dev);
51 static inline void ms_clear_error(struct realtek_pci_ms *host)
53 rtsx_pci_write_register(host->pcr, CARD_STOP,
54 MS_STOP | MS_CLR_ERR, MS_STOP | MS_CLR_ERR);
59 static void ms_print_debug_regs(struct realtek_pci_ms *host)
61 struct rtsx_pcr *pcr = host->pcr;
65 /* Print MS host internal registers */
66 rtsx_pci_init_cmd(pcr);
67 for (i = 0xFD40; i <= 0xFD44; i++)
68 rtsx_pci_add_cmd(pcr, READ_REG_CMD, i, 0, 0);
69 for (i = 0xFD52; i <= 0xFD69; i++)
70 rtsx_pci_add_cmd(pcr, READ_REG_CMD, i, 0, 0);
71 rtsx_pci_send_cmd(pcr, 100);
73 ptr = rtsx_pci_get_cmd_data(pcr);
74 for (i = 0xFD40; i <= 0xFD44; i++)
75 dev_dbg(ms_dev(host), "0x%04X: 0x%02x\n", i, *(ptr++));
76 for (i = 0xFD52; i <= 0xFD69; i++)
77 dev_dbg(ms_dev(host), "0x%04X: 0x%02x\n", i, *(ptr++));
82 #define ms_print_debug_regs(host)
86 static int ms_power_on(struct realtek_pci_ms *host)
88 struct rtsx_pcr *pcr = host->pcr;
91 rtsx_pci_init_cmd(pcr);
92 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_SELECT, 0x07, MS_MOD_SEL);
93 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_SHARE_MODE,
94 CARD_SHARE_MASK, CARD_SHARE_48_MS);
95 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN,
96 MS_CLK_EN, MS_CLK_EN);
97 err = rtsx_pci_send_cmd(pcr, 100);
101 err = rtsx_pci_card_pull_ctl_enable(pcr, RTSX_MS_CARD);
105 err = rtsx_pci_card_power_on(pcr, RTSX_MS_CARD);
109 /* Wait ms power stable */
112 err = rtsx_pci_write_register(pcr, CARD_OE,
113 MS_OUTPUT_EN, MS_OUTPUT_EN);
120 static int ms_power_off(struct realtek_pci_ms *host)
122 struct rtsx_pcr *pcr = host->pcr;
125 rtsx_pci_init_cmd(pcr);
127 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN, MS_CLK_EN, 0);
128 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_OE, MS_OUTPUT_EN, 0);
130 err = rtsx_pci_send_cmd(pcr, 100);
134 err = rtsx_pci_card_power_off(pcr, RTSX_MS_CARD);
138 return rtsx_pci_card_pull_ctl_disable(pcr, RTSX_MS_CARD);
141 static int ms_transfer_data(struct realtek_pci_ms *host, unsigned char data_dir,
142 u8 tpc, u8 cfg, struct scatterlist *sg)
144 struct rtsx_pcr *pcr = host->pcr;
146 unsigned int length = sg->length;
147 u16 sec_cnt = (u16)(length / 512);
148 u8 val, trans_mode, dma_dir;
150 dev_dbg(ms_dev(host), "%s: tpc = 0x%02x, data_dir = %s, length = %d\n",
151 __func__, tpc, (data_dir == READ) ? "READ" : "WRITE",
154 if (data_dir == READ) {
155 dma_dir = DMA_DIR_FROM_CARD;
156 trans_mode = MS_TM_AUTO_READ;
158 dma_dir = DMA_DIR_TO_CARD;
159 trans_mode = MS_TM_AUTO_WRITE;
162 rtsx_pci_init_cmd(pcr);
164 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
165 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_SECTOR_CNT_H,
166 0xFF, (u8)(sec_cnt >> 8));
167 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_SECTOR_CNT_L,
169 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
171 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
172 DMA_DONE_INT, DMA_DONE_INT);
173 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC3, 0xFF, (u8)(length >> 24));
174 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC2, 0xFF, (u8)(length >> 16));
175 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC1, 0xFF, (u8)(length >> 8));
176 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC0, 0xFF, (u8)length);
177 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
178 0x03 | DMA_PACK_SIZE_MASK, dma_dir | DMA_EN | DMA_512);
179 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
182 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANSFER,
183 0xFF, MS_TRANSFER_START | trans_mode);
184 rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, MS_TRANSFER,
185 MS_TRANSFER_END, MS_TRANSFER_END);
187 rtsx_pci_send_cmd_no_wait(pcr);
189 err = rtsx_pci_transfer_data(pcr, sg, 1, data_dir == READ, 10000);
191 ms_clear_error(host);
195 rtsx_pci_read_register(pcr, MS_TRANS_CFG, &val);
196 if (val & (MS_INT_CMDNK | MS_INT_ERR | MS_CRC16_ERR | MS_RDY_TIMEOUT))
202 static int ms_write_bytes(struct realtek_pci_ms *host, u8 tpc,
203 u8 cfg, u8 cnt, u8 *data, u8 *int_reg)
205 struct rtsx_pcr *pcr = host->pcr;
208 dev_dbg(ms_dev(host), "%s: tpc = 0x%02x\n", __func__, tpc);
213 rtsx_pci_init_cmd(pcr);
215 for (i = 0; i < cnt; i++)
216 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
217 PPBUF_BASE2 + i, 0xFF, data[i]);
219 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
220 PPBUF_BASE2 + i, 0xFF, 0xFF);
222 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
223 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_BYTE_CNT, 0xFF, cnt);
224 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
225 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
226 0x01, PINGPONG_BUFFER);
228 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANSFER,
229 0xFF, MS_TRANSFER_START | MS_TM_WRITE_BYTES);
230 rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, MS_TRANSFER,
231 MS_TRANSFER_END, MS_TRANSFER_END);
233 rtsx_pci_add_cmd(pcr, READ_REG_CMD, MS_TRANS_CFG, 0, 0);
235 err = rtsx_pci_send_cmd(pcr, 5000);
239 rtsx_pci_read_register(pcr, MS_TRANS_CFG, &val);
240 dev_dbg(ms_dev(host), "MS_TRANS_CFG: 0x%02x\n", val);
243 *int_reg = val & 0x0F;
245 ms_print_debug_regs(host);
247 ms_clear_error(host);
250 if (val & MS_CRC16_ERR)
254 if (val & (MS_INT_ERR | MS_INT_CMDNK))
263 u8 *ptr = rtsx_pci_get_cmd_data(pcr) + 1;
264 *int_reg = *ptr & 0x0F;
270 static int ms_read_bytes(struct realtek_pci_ms *host, u8 tpc,
271 u8 cfg, u8 cnt, u8 *data, u8 *int_reg)
273 struct rtsx_pcr *pcr = host->pcr;
277 dev_dbg(ms_dev(host), "%s: tpc = 0x%02x\n", __func__, tpc);
282 rtsx_pci_init_cmd(pcr);
284 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc);
285 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_BYTE_CNT, 0xFF, cnt);
286 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg);
287 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
288 0x01, PINGPONG_BUFFER);
290 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, MS_TRANSFER,
291 0xFF, MS_TRANSFER_START | MS_TM_READ_BYTES);
292 rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, MS_TRANSFER,
293 MS_TRANSFER_END, MS_TRANSFER_END);
294 for (i = 0; i < cnt - 1; i++)
295 rtsx_pci_add_cmd(pcr, READ_REG_CMD, PPBUF_BASE2 + i, 0, 0);
297 rtsx_pci_add_cmd(pcr, READ_REG_CMD, PPBUF_BASE2 + cnt, 0, 0);
299 rtsx_pci_add_cmd(pcr, READ_REG_CMD,
300 PPBUF_BASE2 + cnt - 1, 0, 0);
302 rtsx_pci_add_cmd(pcr, READ_REG_CMD, MS_TRANS_CFG, 0, 0);
304 err = rtsx_pci_send_cmd(pcr, 5000);
308 rtsx_pci_read_register(pcr, MS_TRANS_CFG, &val);
309 dev_dbg(ms_dev(host), "MS_TRANS_CFG: 0x%02x\n", val);
312 *int_reg = val & 0x0F;
314 ms_print_debug_regs(host);
316 ms_clear_error(host);
319 if (val & MS_CRC16_ERR)
323 if (val & (MS_INT_ERR | MS_INT_CMDNK))
331 ptr = rtsx_pci_get_cmd_data(pcr) + 1;
332 for (i = 0; i < cnt; i++)
336 *int_reg = *ptr & 0x0F;
341 static int rtsx_pci_ms_issue_cmd(struct realtek_pci_ms *host)
343 struct memstick_request *req = host->req;
347 dev_dbg(ms_dev(host), "%s\n", __func__);
349 if (req->need_card_int) {
350 if (host->ifmode != MEMSTICK_SERIAL)
354 if (req->long_data) {
355 err = ms_transfer_data(host, req->data_dir,
356 req->tpc, cfg, &(req->sg));
358 if (req->data_dir == READ) {
359 err = ms_read_bytes(host, req->tpc, cfg,
360 req->data_len, req->data, &int_reg);
362 err = ms_write_bytes(host, req->tpc, cfg,
363 req->data_len, req->data, &int_reg);
369 if (req->need_card_int && (host->ifmode == MEMSTICK_SERIAL)) {
370 err = ms_read_bytes(host, MS_TPC_GET_INT,
371 NO_WAIT_INT, 1, &int_reg, NULL);
376 if (req->need_card_int) {
377 dev_dbg(ms_dev(host), "int_reg: 0x%02x\n", int_reg);
379 if (int_reg & MS_INT_CMDNK)
380 req->int_reg |= MEMSTICK_INT_CMDNAK;
381 if (int_reg & MS_INT_BREQ)
382 req->int_reg |= MEMSTICK_INT_BREQ;
383 if (int_reg & MS_INT_ERR)
384 req->int_reg |= MEMSTICK_INT_ERR;
385 if (int_reg & MS_INT_CED)
386 req->int_reg |= MEMSTICK_INT_CED;
392 static void rtsx_pci_ms_handle_req(struct work_struct *work)
394 struct realtek_pci_ms *host = container_of(work,
395 struct realtek_pci_ms, handle_req);
396 struct rtsx_pcr *pcr = host->pcr;
397 struct memstick_host *msh = host->msh;
400 mutex_lock(&pcr->pcr_mutex);
402 rtsx_pci_start_run(pcr);
404 rtsx_pci_switch_clock(host->pcr, host->clock, host->ssc_depth,
406 rtsx_pci_write_register(pcr, CARD_SELECT, 0x07, MS_MOD_SEL);
407 rtsx_pci_write_register(pcr, CARD_SHARE_MODE,
408 CARD_SHARE_MASK, CARD_SHARE_48_MS);
412 rc = memstick_next_req(msh, &host->req);
413 dev_dbg(ms_dev(host), "next req %d\n", rc);
416 host->req->error = rtsx_pci_ms_issue_cmd(host);
420 mutex_unlock(&pcr->pcr_mutex);
423 static void rtsx_pci_ms_request(struct memstick_host *msh)
425 struct realtek_pci_ms *host = memstick_priv(msh);
427 dev_dbg(ms_dev(host), "--> %s\n", __func__);
429 if (rtsx_pci_card_exclusive_check(host->pcr, RTSX_MS_CARD))
432 schedule_work(&host->handle_req);
435 static int rtsx_pci_ms_set_param(struct memstick_host *msh,
436 enum memstick_param param, int value)
438 struct realtek_pci_ms *host = memstick_priv(msh);
439 struct rtsx_pcr *pcr = host->pcr;
440 unsigned int clock = 0;
444 dev_dbg(ms_dev(host), "%s: param = %d, value = %d\n",
445 __func__, param, value);
447 err = rtsx_pci_card_exclusive_check(host->pcr, RTSX_MS_CARD);
453 if (value == MEMSTICK_POWER_ON)
454 err = ms_power_on(host);
455 else if (value == MEMSTICK_POWER_OFF)
456 err = ms_power_off(host);
461 case MEMSTICK_INTERFACE:
462 if (value == MEMSTICK_SERIAL) {
464 ssc_depth = RTSX_SSC_DEPTH_500K;
466 err = rtsx_pci_write_register(pcr, MS_CFG,
467 0x18, MS_BUS_WIDTH_1);
470 } else if (value == MEMSTICK_PAR4) {
472 ssc_depth = RTSX_SSC_DEPTH_1M;
474 err = rtsx_pci_write_register(pcr, MS_CFG,
475 0x58, MS_BUS_WIDTH_4 | PUSH_TIME_ODD);
482 err = rtsx_pci_switch_clock(pcr, clock,
483 ssc_depth, false, true, false);
487 host->ssc_depth = ssc_depth;
489 host->ifmode = value;
498 static int rtsx_pci_ms_suspend(struct platform_device *pdev, pm_message_t state)
500 struct realtek_pci_ms *host = platform_get_drvdata(pdev);
501 struct memstick_host *msh = host->msh;
503 dev_dbg(ms_dev(host), "--> %s\n", __func__);
505 memstick_suspend_host(msh);
509 static int rtsx_pci_ms_resume(struct platform_device *pdev)
511 struct realtek_pci_ms *host = platform_get_drvdata(pdev);
512 struct memstick_host *msh = host->msh;
514 dev_dbg(ms_dev(host), "--> %s\n", __func__);
516 memstick_resume_host(msh);
520 #else /* CONFIG_PM */
522 #define rtsx_pci_ms_suspend NULL
523 #define rtsx_pci_ms_resume NULL
525 #endif /* CONFIG_PM */
527 static void rtsx_pci_ms_card_event(struct platform_device *pdev)
529 struct realtek_pci_ms *host = platform_get_drvdata(pdev);
531 memstick_detect_change(host->msh);
534 static int rtsx_pci_ms_drv_probe(struct platform_device *pdev)
536 struct memstick_host *msh;
537 struct realtek_pci_ms *host;
538 struct rtsx_pcr *pcr;
539 struct pcr_handle *handle = pdev->dev.platform_data;
549 dev_dbg(&(pdev->dev),
550 ": Realtek PCI-E Memstick controller found\n");
552 msh = memstick_alloc_host(sizeof(*host), &pdev->dev);
556 host = memstick_priv(msh);
560 platform_set_drvdata(pdev, host);
561 pcr->slots[RTSX_MS_CARD].p_dev = pdev;
562 pcr->slots[RTSX_MS_CARD].card_event = rtsx_pci_ms_card_event;
564 mutex_init(&host->host_mutex);
566 INIT_WORK(&host->handle_req, rtsx_pci_ms_handle_req);
567 msh->request = rtsx_pci_ms_request;
568 msh->set_param = rtsx_pci_ms_set_param;
569 msh->caps = MEMSTICK_CAP_PAR4;
571 rc = memstick_add_host(msh);
573 memstick_free_host(msh);
580 static int rtsx_pci_ms_drv_remove(struct platform_device *pdev)
582 struct realtek_pci_ms *host = platform_get_drvdata(pdev);
583 struct rtsx_pcr *pcr;
584 struct memstick_host *msh;
591 pcr->slots[RTSX_MS_CARD].p_dev = NULL;
592 pcr->slots[RTSX_MS_CARD].card_event = NULL;
596 mutex_lock(&host->host_mutex);
598 dev_dbg(&(pdev->dev),
599 "%s: Controller removed during transfer\n",
600 dev_name(&msh->dev));
602 rtsx_pci_complete_unfinished_transfer(pcr);
604 host->req->error = -ENOMEDIUM;
606 rc = memstick_next_req(msh, &host->req);
608 host->req->error = -ENOMEDIUM;
611 mutex_unlock(&host->host_mutex);
613 memstick_remove_host(msh);
614 memstick_free_host(msh);
616 platform_set_drvdata(pdev, NULL);
618 dev_dbg(&(pdev->dev),
619 ": Realtek PCI-E Memstick controller has been removed\n");
624 static struct platform_device_id rtsx_pci_ms_ids[] = {
626 .name = DRV_NAME_RTSX_PCI_MS,
631 MODULE_DEVICE_TABLE(platform, rtsx_pci_ms_ids);
633 static struct platform_driver rtsx_pci_ms_driver = {
634 .probe = rtsx_pci_ms_drv_probe,
635 .remove = rtsx_pci_ms_drv_remove,
636 .id_table = rtsx_pci_ms_ids,
637 .suspend = rtsx_pci_ms_suspend,
638 .resume = rtsx_pci_ms_resume,
640 .owner = THIS_MODULE,
641 .name = DRV_NAME_RTSX_PCI_MS,
644 module_platform_driver(rtsx_pci_ms_driver);
646 MODULE_LICENSE("GPL");
647 MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
648 MODULE_DESCRIPTION("Realtek PCI-E Memstick Card Host Driver");