blob: 067dd462736280d4c49d67c5c7d7d3c123e126be [file] [log] [blame]
Wei WANGff984e52012-10-29 13:49:38 +08001/* Realtek PCI-Express SD/MMC Card Interface driver
2 *
3 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
4 *
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
8 * later version.
9 *
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.
14 *
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/>.
17 *
18 * Author:
19 * Wei WANG <wei_wang@realsil.com.cn>
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
21 */
22
23#include <linux/module.h>
24#include <linux/highmem.h>
25#include <linux/delay.h>
26#include <linux/platform_device.h>
27#include <linux/mmc/host.h>
28#include <linux/mmc/mmc.h>
29#include <linux/mmc/sd.h>
30#include <linux/mmc/card.h>
31#include <linux/mfd/rtsx_pci.h>
32#include <asm/unaligned.h>
33
34/* SD Tuning Data Structure
35 * Record continuous timing phase path
36 */
37struct timing_phase_path {
38 int start;
39 int end;
40 int mid;
41 int len;
42};
43
44struct realtek_pci_sdmmc {
45 struct platform_device *pdev;
46 struct rtsx_pcr *pcr;
47 struct mmc_host *mmc;
48 struct mmc_request *mrq;
49
50 struct mutex host_mutex;
51
52 u8 ssc_depth;
53 unsigned int clock;
54 bool vpclk;
55 bool double_clk;
56 bool eject;
57 bool initial_mode;
58 bool ddr_mode;
59};
60
61static inline struct device *sdmmc_dev(struct realtek_pci_sdmmc *host)
62{
63 return &(host->pdev->dev);
64}
65
66static inline void sd_clear_error(struct realtek_pci_sdmmc *host)
67{
68 rtsx_pci_write_register(host->pcr, CARD_STOP,
69 SD_STOP | SD_CLR_ERR, SD_STOP | SD_CLR_ERR);
70}
71
72#ifdef DEBUG
73static void sd_print_debug_regs(struct realtek_pci_sdmmc *host)
74{
75 struct rtsx_pcr *pcr = host->pcr;
76 u16 i;
77 u8 *ptr;
78
79 /* Print SD host internal registers */
80 rtsx_pci_init_cmd(pcr);
81 for (i = 0xFDA0; i <= 0xFDAE; i++)
82 rtsx_pci_add_cmd(pcr, READ_REG_CMD, i, 0, 0);
83 for (i = 0xFD52; i <= 0xFD69; i++)
84 rtsx_pci_add_cmd(pcr, READ_REG_CMD, i, 0, 0);
85 rtsx_pci_send_cmd(pcr, 100);
86
87 ptr = rtsx_pci_get_cmd_data(pcr);
88 for (i = 0xFDA0; i <= 0xFDAE; i++)
89 dev_dbg(sdmmc_dev(host), "0x%04X: 0x%02x\n", i, *(ptr++));
90 for (i = 0xFD52; i <= 0xFD69; i++)
91 dev_dbg(sdmmc_dev(host), "0x%04X: 0x%02x\n", i, *(ptr++));
92}
93#else
94#define sd_print_debug_regs(host)
95#endif /* DEBUG */
96
97static int sd_read_data(struct realtek_pci_sdmmc *host, u8 *cmd, u16 byte_cnt,
98 u8 *buf, int buf_len, int timeout)
99{
100 struct rtsx_pcr *pcr = host->pcr;
101 int err, i;
102 u8 trans_mode;
103
104 dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD%d\n", __func__, cmd[0] - 0x40);
105
106 if (!buf)
107 buf_len = 0;
108
109 if ((cmd[0] & 0x3F) == MMC_SEND_TUNING_BLOCK)
110 trans_mode = SD_TM_AUTO_TUNING;
111 else
112 trans_mode = SD_TM_NORMAL_READ;
113
114 rtsx_pci_init_cmd(pcr);
115
116 for (i = 0; i < 5; i++)
117 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD0 + i, 0xFF, cmd[i]);
118
119 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_L, 0xFF, (u8)byte_cnt);
120 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_H,
121 0xFF, (u8)(byte_cnt >> 8));
122 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_L, 0xFF, 1);
123 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_H, 0xFF, 0);
124
125 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG2, 0xFF,
126 SD_CALCULATE_CRC7 | SD_CHECK_CRC16 |
127 SD_NO_WAIT_BUSY_END | SD_CHECK_CRC7 | SD_RSP_LEN_6);
128 if (trans_mode != SD_TM_AUTO_TUNING)
129 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
130 CARD_DATA_SOURCE, 0x01, PINGPONG_BUFFER);
131
132 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER,
133 0xFF, trans_mode | SD_TRANSFER_START);
134 rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, SD_TRANSFER,
135 SD_TRANSFER_END, SD_TRANSFER_END);
136
137 err = rtsx_pci_send_cmd(pcr, timeout);
138 if (err < 0) {
139 sd_print_debug_regs(host);
140 dev_dbg(sdmmc_dev(host),
141 "rtsx_pci_send_cmd fail (err = %d)\n", err);
142 return err;
143 }
144
145 if (buf && buf_len) {
146 err = rtsx_pci_read_ppbuf(pcr, buf, buf_len);
147 if (err < 0) {
148 dev_dbg(sdmmc_dev(host),
149 "rtsx_pci_read_ppbuf fail (err = %d)\n", err);
150 return err;
151 }
152 }
153
154 return 0;
155}
156
157static int sd_write_data(struct realtek_pci_sdmmc *host, u8 *cmd, u16 byte_cnt,
158 u8 *buf, int buf_len, int timeout)
159{
160 struct rtsx_pcr *pcr = host->pcr;
161 int err, i;
162 u8 trans_mode;
163
164 if (!buf)
165 buf_len = 0;
166
167 if (buf && buf_len) {
168 err = rtsx_pci_write_ppbuf(pcr, buf, buf_len);
169 if (err < 0) {
170 dev_dbg(sdmmc_dev(host),
171 "rtsx_pci_write_ppbuf fail (err = %d)\n", err);
172 return err;
173 }
174 }
175
176 trans_mode = cmd ? SD_TM_AUTO_WRITE_2 : SD_TM_AUTO_WRITE_3;
177 rtsx_pci_init_cmd(pcr);
178
179 if (cmd) {
180 dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d\n", __func__,
181 cmd[0] - 0x40);
182
183 for (i = 0; i < 5; i++)
184 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
185 SD_CMD0 + i, 0xFF, cmd[i]);
186 }
187
188 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_L, 0xFF, (u8)byte_cnt);
189 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_H,
190 0xFF, (u8)(byte_cnt >> 8));
191 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_L, 0xFF, 1);
192 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_H, 0xFF, 0);
193
194 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG2, 0xFF,
195 SD_CALCULATE_CRC7 | SD_CHECK_CRC16 |
196 SD_NO_WAIT_BUSY_END | SD_CHECK_CRC7 | SD_RSP_LEN_6);
197
198 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER, 0xFF,
199 trans_mode | SD_TRANSFER_START);
200 rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, SD_TRANSFER,
201 SD_TRANSFER_END, SD_TRANSFER_END);
202
203 err = rtsx_pci_send_cmd(pcr, timeout);
204 if (err < 0) {
205 sd_print_debug_regs(host);
206 dev_dbg(sdmmc_dev(host),
207 "rtsx_pci_send_cmd fail (err = %d)\n", err);
208 return err;
209 }
210
211 return 0;
212}
213
214static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host,
215 struct mmc_command *cmd)
216{
217 struct rtsx_pcr *pcr = host->pcr;
218 u8 cmd_idx = (u8)cmd->opcode;
219 u32 arg = cmd->arg;
220 int err = 0;
221 int timeout = 100;
222 int i;
223 u8 *ptr;
224 int stat_idx = 0;
225 u8 rsp_type;
226 int rsp_len = 5;
227
228 dev_dbg(sdmmc_dev(host), "%s: SD/MMC CMD %d, arg = 0x%08x\n",
229 __func__, cmd_idx, arg);
230
231 /* Response type:
232 * R0
233 * R1, R5, R6, R7
234 * R1b
235 * R2
236 * R3, R4
237 */
238 switch (mmc_resp_type(cmd)) {
239 case MMC_RSP_NONE:
240 rsp_type = SD_RSP_TYPE_R0;
241 rsp_len = 0;
242 break;
243 case MMC_RSP_R1:
244 rsp_type = SD_RSP_TYPE_R1;
245 break;
246 case MMC_RSP_R1B:
247 rsp_type = SD_RSP_TYPE_R1b;
248 break;
249 case MMC_RSP_R2:
250 rsp_type = SD_RSP_TYPE_R2;
251 rsp_len = 16;
252 break;
253 case MMC_RSP_R3:
254 rsp_type = SD_RSP_TYPE_R3;
255 break;
256 default:
257 dev_dbg(sdmmc_dev(host), "cmd->flag is not valid\n");
258 err = -EINVAL;
259 goto out;
260 }
261
262 if (rsp_type == SD_RSP_TYPE_R1b)
263 timeout = 3000;
264
265 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
266 err = rtsx_pci_write_register(pcr, SD_BUS_STAT,
267 0xFF, SD_CLK_TOGGLE_EN);
268 if (err < 0)
269 goto out;
270 }
271
272 rtsx_pci_init_cmd(pcr);
273
274 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD0, 0xFF, 0x40 | cmd_idx);
275 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD1, 0xFF, (u8)(arg >> 24));
276 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD2, 0xFF, (u8)(arg >> 16));
277 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD3, 0xFF, (u8)(arg >> 8));
278 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CMD4, 0xFF, (u8)arg);
279
280 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG2, 0xFF, rsp_type);
281 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
282 0x01, PINGPONG_BUFFER);
283 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER,
284 0xFF, SD_TM_CMD_RSP | SD_TRANSFER_START);
285 rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, SD_TRANSFER,
286 SD_TRANSFER_END | SD_STAT_IDLE,
287 SD_TRANSFER_END | SD_STAT_IDLE);
288
289 if (rsp_type == SD_RSP_TYPE_R2) {
290 /* Read data from ping-pong buffer */
291 for (i = PPBUF_BASE2; i < PPBUF_BASE2 + 16; i++)
292 rtsx_pci_add_cmd(pcr, READ_REG_CMD, (u16)i, 0, 0);
293 stat_idx = 16;
294 } else if (rsp_type != SD_RSP_TYPE_R0) {
295 /* Read data from SD_CMDx registers */
296 for (i = SD_CMD0; i <= SD_CMD4; i++)
297 rtsx_pci_add_cmd(pcr, READ_REG_CMD, (u16)i, 0, 0);
298 stat_idx = 5;
299 }
300
301 rtsx_pci_add_cmd(pcr, READ_REG_CMD, SD_STAT1, 0, 0);
302
303 err = rtsx_pci_send_cmd(pcr, timeout);
304 if (err < 0) {
305 sd_print_debug_regs(host);
306 sd_clear_error(host);
307 dev_dbg(sdmmc_dev(host),
308 "rtsx_pci_send_cmd error (err = %d)\n", err);
309 goto out;
310 }
311
312 if (rsp_type == SD_RSP_TYPE_R0) {
313 err = 0;
314 goto out;
315 }
316
317 /* Eliminate returned value of CHECK_REG_CMD */
318 ptr = rtsx_pci_get_cmd_data(pcr) + 1;
319
320 /* Check (Start,Transmission) bit of Response */
321 if ((ptr[0] & 0xC0) != 0) {
322 err = -EILSEQ;
323 dev_dbg(sdmmc_dev(host), "Invalid response bit\n");
324 goto out;
325 }
326
327 /* Check CRC7 */
328 if (!(rsp_type & SD_NO_CHECK_CRC7)) {
329 if (ptr[stat_idx] & SD_CRC7_ERR) {
330 err = -EILSEQ;
331 dev_dbg(sdmmc_dev(host), "CRC7 error\n");
332 goto out;
333 }
334 }
335
336 if (rsp_type == SD_RSP_TYPE_R2) {
337 for (i = 0; i < 4; i++) {
338 cmd->resp[i] = get_unaligned_be32(ptr + 1 + i * 4);
339 dev_dbg(sdmmc_dev(host), "cmd->resp[%d] = 0x%08x\n",
340 i, cmd->resp[i]);
341 }
342 } else {
343 cmd->resp[0] = get_unaligned_be32(ptr + 1);
344 dev_dbg(sdmmc_dev(host), "cmd->resp[0] = 0x%08x\n",
345 cmd->resp[0]);
346 }
347
348out:
349 cmd->error = err;
350}
351
352static int sd_rw_multi(struct realtek_pci_sdmmc *host, struct mmc_request *mrq)
353{
354 struct rtsx_pcr *pcr = host->pcr;
355 struct mmc_host *mmc = host->mmc;
356 struct mmc_card *card = mmc->card;
357 struct mmc_data *data = mrq->data;
358 int uhs = mmc_sd_card_uhs(card);
359 int read = (data->flags & MMC_DATA_READ) ? 1 : 0;
360 u8 cfg2, trans_mode;
361 int err;
362 size_t data_len = data->blksz * data->blocks;
363
364 if (read) {
365 cfg2 = SD_CALCULATE_CRC7 | SD_CHECK_CRC16 |
366 SD_NO_WAIT_BUSY_END | SD_CHECK_CRC7 | SD_RSP_LEN_0;
367 trans_mode = SD_TM_AUTO_READ_3;
368 } else {
369 cfg2 = SD_NO_CALCULATE_CRC7 | SD_CHECK_CRC16 |
370 SD_NO_WAIT_BUSY_END | SD_NO_CHECK_CRC7 | SD_RSP_LEN_0;
371 trans_mode = SD_TM_AUTO_WRITE_3;
372 }
373
374 if (!uhs)
375 cfg2 |= SD_NO_CHECK_WAIT_CRC_TO;
376
377 rtsx_pci_init_cmd(pcr);
378
379 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_L, 0xFF, 0x00);
380 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BYTE_CNT_H, 0xFF, 0x02);
381 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_L,
382 0xFF, (u8)data->blocks);
383 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_BLOCK_CNT_H,
384 0xFF, (u8)(data->blocks >> 8));
Wei WANGff984e52012-10-29 13:49:38 +0800385
386 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
387 DMA_DONE_INT, DMA_DONE_INT);
388 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC3,
389 0xFF, (u8)(data_len >> 24));
390 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC2,
391 0xFF, (u8)(data_len >> 16));
392 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC1,
393 0xFF, (u8)(data_len >> 8));
394 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMATC0, 0xFF, (u8)data_len);
395 if (read) {
396 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
397 0x03 | DMA_PACK_SIZE_MASK,
398 DMA_DIR_FROM_CARD | DMA_EN | DMA_512);
399 } else {
400 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, DMACTL,
401 0x03 | DMA_PACK_SIZE_MASK,
402 DMA_DIR_TO_CARD | DMA_EN | DMA_512);
403 }
404
405 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DATA_SOURCE,
406 0x01, RING_BUFFER);
407
Wei WANG38d324df2012-11-20 11:24:36 +0800408 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG2, 0xFF, cfg2);
Wei WANGff984e52012-10-29 13:49:38 +0800409 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_TRANSFER, 0xFF,
410 trans_mode | SD_TRANSFER_START);
411 rtsx_pci_add_cmd(pcr, CHECK_REG_CMD, SD_TRANSFER,
412 SD_TRANSFER_END, SD_TRANSFER_END);
413
414 rtsx_pci_send_cmd_no_wait(pcr);
415
416 err = rtsx_pci_transfer_data(pcr, data->sg, data->sg_len, read, 10000);
417 if (err < 0) {
418 sd_clear_error(host);
419 return err;
420 }
421
422 return 0;
423}
424
425static inline void sd_enable_initial_mode(struct realtek_pci_sdmmc *host)
426{
427 rtsx_pci_write_register(host->pcr, SD_CFG1,
428 SD_CLK_DIVIDE_MASK, SD_CLK_DIVIDE_128);
429}
430
431static inline void sd_disable_initial_mode(struct realtek_pci_sdmmc *host)
432{
433 rtsx_pci_write_register(host->pcr, SD_CFG1,
434 SD_CLK_DIVIDE_MASK, SD_CLK_DIVIDE_0);
435}
436
437static void sd_normal_rw(struct realtek_pci_sdmmc *host,
438 struct mmc_request *mrq)
439{
440 struct mmc_command *cmd = mrq->cmd;
441 struct mmc_data *data = mrq->data;
442 u8 _cmd[5], *buf;
443
444 _cmd[0] = 0x40 | (u8)cmd->opcode;
445 put_unaligned_be32(cmd->arg, (u32 *)(&_cmd[1]));
446
447 buf = kzalloc(data->blksz, GFP_NOIO);
448 if (!buf) {
449 cmd->error = -ENOMEM;
450 return;
451 }
452
453 if (data->flags & MMC_DATA_READ) {
454 if (host->initial_mode)
455 sd_disable_initial_mode(host);
456
457 cmd->error = sd_read_data(host, _cmd, (u16)data->blksz, buf,
458 data->blksz, 200);
459
460 if (host->initial_mode)
461 sd_enable_initial_mode(host);
462
463 sg_copy_from_buffer(data->sg, data->sg_len, buf, data->blksz);
464 } else {
465 sg_copy_to_buffer(data->sg, data->sg_len, buf, data->blksz);
466
467 cmd->error = sd_write_data(host, _cmd, (u16)data->blksz, buf,
468 data->blksz, 200);
469 }
470
471 kfree(buf);
472}
473
474static int sd_change_phase(struct realtek_pci_sdmmc *host, u8 sample_point)
475{
476 struct rtsx_pcr *pcr = host->pcr;
477 int err;
478
479 dev_dbg(sdmmc_dev(host), "%s: sample_point = %d\n",
480 __func__, sample_point);
481
482 rtsx_pci_init_cmd(pcr);
483
484 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CHANGE_CLK, CHANGE_CLK);
485 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPRX_CTL, 0x1F, sample_point);
486 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL, PHASE_NOT_RESET, 0);
487 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
488 PHASE_NOT_RESET, PHASE_NOT_RESET);
489 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CHANGE_CLK, 0);
490 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG1, SD_ASYNC_FIFO_NOT_RST, 0);
491
492 err = rtsx_pci_send_cmd(pcr, 100);
493 if (err < 0)
494 return err;
495
496 return 0;
497}
498
499static u8 sd_search_final_phase(struct realtek_pci_sdmmc *host, u32 phase_map)
500{
501 struct timing_phase_path path[MAX_PHASE + 1];
502 int i, j, cont_path_cnt;
503 int new_block, max_len, final_path_idx;
504 u8 final_phase = 0xFF;
505
506 /* Parse phase_map, take it as a bit-ring */
507 cont_path_cnt = 0;
508 new_block = 1;
509 j = 0;
510 for (i = 0; i < MAX_PHASE + 1; i++) {
511 if (phase_map & (1 << i)) {
512 if (new_block) {
513 new_block = 0;
514 j = cont_path_cnt++;
515 path[j].start = i;
516 path[j].end = i;
517 } else {
518 path[j].end = i;
519 }
520 } else {
521 new_block = 1;
522 if (cont_path_cnt) {
523 /* Calculate path length and middle point */
524 int idx = cont_path_cnt - 1;
525 path[idx].len =
526 path[idx].end - path[idx].start + 1;
527 path[idx].mid =
528 path[idx].start + path[idx].len / 2;
529 }
530 }
531 }
532
533 if (cont_path_cnt == 0) {
534 dev_dbg(sdmmc_dev(host), "No continuous phase path\n");
535 goto finish;
536 } else {
537 /* Calculate last continuous path length and middle point */
538 int idx = cont_path_cnt - 1;
539 path[idx].len = path[idx].end - path[idx].start + 1;
540 path[idx].mid = path[idx].start + path[idx].len / 2;
541 }
542
543 /* Connect the first and last continuous paths if they are adjacent */
544 if (!path[0].start && (path[cont_path_cnt - 1].end == MAX_PHASE)) {
545 /* Using negative index */
546 path[0].start = path[cont_path_cnt - 1].start - MAX_PHASE - 1;
547 path[0].len += path[cont_path_cnt - 1].len;
548 path[0].mid = path[0].start + path[0].len / 2;
549 /* Convert negative middle point index to positive one */
550 if (path[0].mid < 0)
551 path[0].mid += MAX_PHASE + 1;
552 cont_path_cnt--;
553 }
554
555 /* Choose the longest continuous phase path */
556 max_len = 0;
557 final_phase = 0;
558 final_path_idx = 0;
559 for (i = 0; i < cont_path_cnt; i++) {
560 if (path[i].len > max_len) {
561 max_len = path[i].len;
562 final_phase = (u8)path[i].mid;
563 final_path_idx = i;
564 }
565
566 dev_dbg(sdmmc_dev(host), "path[%d].start = %d\n",
567 i, path[i].start);
568 dev_dbg(sdmmc_dev(host), "path[%d].end = %d\n",
569 i, path[i].end);
570 dev_dbg(sdmmc_dev(host), "path[%d].len = %d\n",
571 i, path[i].len);
572 dev_dbg(sdmmc_dev(host), "path[%d].mid = %d\n",
573 i, path[i].mid);
574 }
575
576finish:
577 dev_dbg(sdmmc_dev(host), "Final chosen phase: %d\n", final_phase);
578 return final_phase;
579}
580
581static void sd_wait_data_idle(struct realtek_pci_sdmmc *host)
582{
583 int err, i;
584 u8 val = 0;
585
586 for (i = 0; i < 100; i++) {
587 err = rtsx_pci_read_register(host->pcr, SD_DATA_STATE, &val);
588 if (val & SD_DATA_IDLE)
589 return;
590
591 udelay(100);
592 }
593}
594
595static int sd_tuning_rx_cmd(struct realtek_pci_sdmmc *host,
596 u8 opcode, u8 sample_point)
597{
598 int err;
599 u8 cmd[5] = {0};
600
601 err = sd_change_phase(host, sample_point);
602 if (err < 0)
603 return err;
604
605 cmd[0] = 0x40 | opcode;
606 err = sd_read_data(host, cmd, 0x40, NULL, 0, 100);
607 if (err < 0) {
608 /* Wait till SD DATA IDLE */
609 sd_wait_data_idle(host);
610 sd_clear_error(host);
611 return err;
612 }
613
614 return 0;
615}
616
617static int sd_tuning_phase(struct realtek_pci_sdmmc *host,
618 u8 opcode, u32 *phase_map)
619{
620 int err, i;
621 u32 raw_phase_map = 0;
622
623 for (i = MAX_PHASE; i >= 0; i--) {
624 err = sd_tuning_rx_cmd(host, opcode, (u8)i);
625 if (err == 0)
626 raw_phase_map |= 1 << i;
627 }
628
629 if (phase_map)
630 *phase_map = raw_phase_map;
631
632 return 0;
633}
634
635static int sd_tuning_rx(struct realtek_pci_sdmmc *host, u8 opcode)
636{
637 int err, i;
638 u32 raw_phase_map[RX_TUNING_CNT] = {0}, phase_map;
639 u8 final_phase;
640
641 for (i = 0; i < RX_TUNING_CNT; i++) {
642 err = sd_tuning_phase(host, opcode, &(raw_phase_map[i]));
643 if (err < 0)
644 return err;
645
646 if (raw_phase_map[i] == 0)
647 break;
648 }
649
650 phase_map = 0xFFFFFFFF;
651 for (i = 0; i < RX_TUNING_CNT; i++) {
652 dev_dbg(sdmmc_dev(host), "RX raw_phase_map[%d] = 0x%08x\n",
653 i, raw_phase_map[i]);
654 phase_map &= raw_phase_map[i];
655 }
656 dev_dbg(sdmmc_dev(host), "RX phase_map = 0x%08x\n", phase_map);
657
658 if (phase_map) {
659 final_phase = sd_search_final_phase(host, phase_map);
660 if (final_phase == 0xFF)
661 return -EINVAL;
662
663 err = sd_change_phase(host, final_phase);
664 if (err < 0)
665 return err;
666 } else {
667 return -EINVAL;
668 }
669
670 return 0;
671}
672
673static void sdmmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
674{
675 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
676 struct rtsx_pcr *pcr = host->pcr;
677 struct mmc_command *cmd = mrq->cmd;
678 struct mmc_data *data = mrq->data;
679 unsigned int data_size = 0;
680
681 if (host->eject) {
682 cmd->error = -ENOMEDIUM;
683 goto finish;
684 }
685
686 mutex_lock(&pcr->pcr_mutex);
687
688 rtsx_pci_start_run(pcr);
689
690 rtsx_pci_switch_clock(pcr, host->clock, host->ssc_depth,
691 host->initial_mode, host->double_clk, host->vpclk);
692 rtsx_pci_write_register(pcr, CARD_SELECT, 0x07, SD_MOD_SEL);
693 rtsx_pci_write_register(pcr, CARD_SHARE_MODE,
694 CARD_SHARE_MASK, CARD_SHARE_48_SD);
695
696 mutex_lock(&host->host_mutex);
697 host->mrq = mrq;
698 mutex_unlock(&host->host_mutex);
699
700 if (mrq->data)
701 data_size = data->blocks * data->blksz;
702
703 if (!data_size || mmc_op_multi(cmd->opcode) ||
704 (cmd->opcode == MMC_READ_SINGLE_BLOCK) ||
705 (cmd->opcode == MMC_WRITE_BLOCK)) {
706 sd_send_cmd_get_rsp(host, cmd);
707
708 if (!cmd->error && data_size) {
709 sd_rw_multi(host, mrq);
710
711 if (mmc_op_multi(cmd->opcode) && mrq->stop)
712 sd_send_cmd_get_rsp(host, mrq->stop);
713 }
714 } else {
715 sd_normal_rw(host, mrq);
716 }
717
718 if (mrq->data) {
719 if (cmd->error || data->error)
720 data->bytes_xfered = 0;
721 else
722 data->bytes_xfered = data->blocks * data->blksz;
723 }
724
725 mutex_unlock(&pcr->pcr_mutex);
726
727finish:
728 if (cmd->error)
729 dev_dbg(sdmmc_dev(host), "cmd->error = %d\n", cmd->error);
730
731 mutex_lock(&host->host_mutex);
732 host->mrq = NULL;
733 mutex_unlock(&host->host_mutex);
734
735 mmc_request_done(mmc, mrq);
736}
737
738static int sd_set_bus_width(struct realtek_pci_sdmmc *host,
739 unsigned char bus_width)
740{
741 int err = 0;
742 u8 width[] = {
743 [MMC_BUS_WIDTH_1] = SD_BUS_WIDTH_1BIT,
744 [MMC_BUS_WIDTH_4] = SD_BUS_WIDTH_4BIT,
745 [MMC_BUS_WIDTH_8] = SD_BUS_WIDTH_8BIT,
746 };
747
748 if (bus_width <= MMC_BUS_WIDTH_8)
749 err = rtsx_pci_write_register(host->pcr, SD_CFG1,
750 0x03, width[bus_width]);
751
752 return err;
753}
754
755static int sd_power_on(struct realtek_pci_sdmmc *host)
756{
757 struct rtsx_pcr *pcr = host->pcr;
758 int err;
759
760 rtsx_pci_init_cmd(pcr);
761 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_SELECT, 0x07, SD_MOD_SEL);
762 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_SHARE_MODE,
763 CARD_SHARE_MASK, CARD_SHARE_48_SD);
764 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN,
765 SD_CLK_EN, SD_CLK_EN);
766 err = rtsx_pci_send_cmd(pcr, 100);
767 if (err < 0)
768 return err;
769
770 err = rtsx_pci_card_pull_ctl_enable(pcr, RTSX_SD_CARD);
771 if (err < 0)
772 return err;
773
774 err = rtsx_pci_card_power_on(pcr, RTSX_SD_CARD);
775 if (err < 0)
776 return err;
777
778 err = rtsx_pci_write_register(pcr, CARD_OE, SD_OUTPUT_EN, SD_OUTPUT_EN);
779 if (err < 0)
780 return err;
781
782 return 0;
783}
784
785static int sd_power_off(struct realtek_pci_sdmmc *host)
786{
787 struct rtsx_pcr *pcr = host->pcr;
788 int err;
789
790 rtsx_pci_init_cmd(pcr);
791
792 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN, SD_CLK_EN, 0);
793 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_OE, SD_OUTPUT_EN, 0);
794
795 err = rtsx_pci_send_cmd(pcr, 100);
796 if (err < 0)
797 return err;
798
799 err = rtsx_pci_card_power_off(pcr, RTSX_SD_CARD);
800 if (err < 0)
801 return err;
802
803 return rtsx_pci_card_pull_ctl_disable(pcr, RTSX_SD_CARD);
804}
805
806static int sd_set_power_mode(struct realtek_pci_sdmmc *host,
807 unsigned char power_mode)
808{
809 int err;
810
811 if (power_mode == MMC_POWER_OFF)
812 err = sd_power_off(host);
813 else
814 err = sd_power_on(host);
815
816 return err;
817}
818
819static int sd_set_timing(struct realtek_pci_sdmmc *host,
820 unsigned char timing, bool *ddr_mode)
821{
822 struct rtsx_pcr *pcr = host->pcr;
823 int err = 0;
824
825 *ddr_mode = false;
826
827 rtsx_pci_init_cmd(pcr);
828
829 switch (timing) {
830 case MMC_TIMING_UHS_SDR104:
831 case MMC_TIMING_UHS_SDR50:
832 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG1,
833 0x0C | SD_ASYNC_FIFO_NOT_RST,
834 SD_30_MODE | SD_ASYNC_FIFO_NOT_RST);
835 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
836 CLK_LOW_FREQ, CLK_LOW_FREQ);
837 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_SOURCE, 0xFF,
838 CRC_VAR_CLK0 | SD30_FIX_CLK | SAMPLE_VAR_CLK1);
839 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CLK_LOW_FREQ, 0);
840 break;
841
842 case MMC_TIMING_UHS_DDR50:
843 *ddr_mode = true;
844
845 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG1,
846 0x0C | SD_ASYNC_FIFO_NOT_RST,
847 SD_DDR_MODE | SD_ASYNC_FIFO_NOT_RST);
848 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
849 CLK_LOW_FREQ, CLK_LOW_FREQ);
850 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_SOURCE, 0xFF,
851 CRC_VAR_CLK0 | SD30_FIX_CLK | SAMPLE_VAR_CLK1);
852 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CLK_LOW_FREQ, 0);
853 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_PUSH_POINT_CTL,
854 DDR_VAR_TX_CMD_DAT, DDR_VAR_TX_CMD_DAT);
855 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_SAMPLE_POINT_CTL,
856 DDR_VAR_RX_DAT | DDR_VAR_RX_CMD,
857 DDR_VAR_RX_DAT | DDR_VAR_RX_CMD);
858 break;
859
860 case MMC_TIMING_MMC_HS:
861 case MMC_TIMING_SD_HS:
862 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_CFG1,
863 0x0C, SD_20_MODE);
864 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
865 CLK_LOW_FREQ, CLK_LOW_FREQ);
866 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_SOURCE, 0xFF,
867 CRC_FIX_CLK | SD30_VAR_CLK0 | SAMPLE_VAR_CLK1);
868 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CLK_LOW_FREQ, 0);
869 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_PUSH_POINT_CTL,
870 SD20_TX_SEL_MASK, SD20_TX_14_AHEAD);
871 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_SAMPLE_POINT_CTL,
872 SD20_RX_SEL_MASK, SD20_RX_14_DELAY);
873 break;
874
875 default:
876 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
877 SD_CFG1, 0x0C, SD_20_MODE);
878 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
879 CLK_LOW_FREQ, CLK_LOW_FREQ);
880 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_SOURCE, 0xFF,
881 CRC_FIX_CLK | SD30_VAR_CLK0 | SAMPLE_VAR_CLK1);
882 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, CLK_LOW_FREQ, 0);
883 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
884 SD_PUSH_POINT_CTL, 0xFF, 0);
885 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_SAMPLE_POINT_CTL,
886 SD20_RX_SEL_MASK, SD20_RX_POS_EDGE);
887 break;
888 }
889
890 err = rtsx_pci_send_cmd(pcr, 100);
891
892 return err;
893}
894
895static void sdmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
896{
897 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
898 struct rtsx_pcr *pcr = host->pcr;
899
900 if (host->eject)
901 return;
902
903 mutex_lock(&pcr->pcr_mutex);
904
905 rtsx_pci_start_run(pcr);
906
907 sd_set_bus_width(host, ios->bus_width);
908 sd_set_power_mode(host, ios->power_mode);
909 sd_set_timing(host, ios->timing, &host->ddr_mode);
910
911 host->vpclk = false;
912 host->double_clk = true;
913
914 switch (ios->timing) {
915 case MMC_TIMING_UHS_SDR104:
916 case MMC_TIMING_UHS_SDR50:
917 host->ssc_depth = RTSX_SSC_DEPTH_2M;
918 host->vpclk = true;
919 host->double_clk = false;
920 break;
921 case MMC_TIMING_UHS_DDR50:
922 case MMC_TIMING_UHS_SDR25:
923 host->ssc_depth = RTSX_SSC_DEPTH_1M;
924 break;
925 default:
926 host->ssc_depth = RTSX_SSC_DEPTH_500K;
927 break;
928 }
929
930 host->initial_mode = (ios->clock <= 1000000) ? true : false;
931
932 host->clock = ios->clock;
933 rtsx_pci_switch_clock(pcr, ios->clock, host->ssc_depth,
934 host->initial_mode, host->double_clk, host->vpclk);
935
936 mutex_unlock(&pcr->pcr_mutex);
937}
938
939static int sdmmc_get_ro(struct mmc_host *mmc)
940{
941 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
942 struct rtsx_pcr *pcr = host->pcr;
943 int ro = 0;
944 u32 val;
945
946 if (host->eject)
947 return -ENOMEDIUM;
948
949 mutex_lock(&pcr->pcr_mutex);
950
951 rtsx_pci_start_run(pcr);
952
953 /* Check SD mechanical write-protect switch */
954 val = rtsx_pci_readl(pcr, RTSX_BIPR);
955 dev_dbg(sdmmc_dev(host), "%s: RTSX_BIPR = 0x%08x\n", __func__, val);
956 if (val & SD_WRITE_PROTECT)
957 ro = 1;
958
959 mutex_unlock(&pcr->pcr_mutex);
960
961 return ro;
962}
963
964static int sdmmc_get_cd(struct mmc_host *mmc)
965{
966 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
967 struct rtsx_pcr *pcr = host->pcr;
968 int cd = 0;
969 u32 val;
970
971 if (host->eject)
972 return -ENOMEDIUM;
973
974 mutex_lock(&pcr->pcr_mutex);
975
976 rtsx_pci_start_run(pcr);
977
978 /* Check SD card detect */
979 val = rtsx_pci_card_exist(pcr);
980 dev_dbg(sdmmc_dev(host), "%s: RTSX_BIPR = 0x%08x\n", __func__, val);
981 if (val & SD_EXIST)
982 cd = 1;
983
984 mutex_unlock(&pcr->pcr_mutex);
985
986 return cd;
987}
988
989static int sd_wait_voltage_stable_1(struct realtek_pci_sdmmc *host)
990{
991 struct rtsx_pcr *pcr = host->pcr;
992 int err;
993 u8 stat;
994
995 /* Reference to Signal Voltage Switch Sequence in SD spec.
996 * Wait for a period of time so that the card can drive SD_CMD and
997 * SD_DAT[3:0] to low after sending back CMD11 response.
998 */
999 mdelay(1);
1000
1001 /* SD_CMD, SD_DAT[3:0] should be driven to low by card;
1002 * If either one of SD_CMD,SD_DAT[3:0] is not low,
1003 * abort the voltage switch sequence;
1004 */
1005 err = rtsx_pci_read_register(pcr, SD_BUS_STAT, &stat);
1006 if (err < 0)
1007 return err;
1008
1009 if (stat & (SD_CMD_STATUS | SD_DAT3_STATUS | SD_DAT2_STATUS |
1010 SD_DAT1_STATUS | SD_DAT0_STATUS))
1011 return -EINVAL;
1012
1013 /* Stop toggle SD clock */
1014 err = rtsx_pci_write_register(pcr, SD_BUS_STAT,
1015 0xFF, SD_CLK_FORCE_STOP);
1016 if (err < 0)
1017 return err;
1018
1019 return 0;
1020}
1021
1022static int sd_wait_voltage_stable_2(struct realtek_pci_sdmmc *host)
1023{
1024 struct rtsx_pcr *pcr = host->pcr;
1025 int err;
1026 u8 stat, mask, val;
1027
1028 /* Wait 1.8V output of voltage regulator in card stable */
1029 msleep(50);
1030
1031 /* Toggle SD clock again */
1032 err = rtsx_pci_write_register(pcr, SD_BUS_STAT, 0xFF, SD_CLK_TOGGLE_EN);
1033 if (err < 0)
1034 return err;
1035
1036 /* Wait for a period of time so that the card can drive
1037 * SD_DAT[3:0] to high at 1.8V
1038 */
1039 msleep(20);
1040
1041 /* SD_CMD, SD_DAT[3:0] should be pulled high by host */
1042 err = rtsx_pci_read_register(pcr, SD_BUS_STAT, &stat);
1043 if (err < 0)
1044 return err;
1045
1046 mask = SD_CMD_STATUS | SD_DAT3_STATUS | SD_DAT2_STATUS |
1047 SD_DAT1_STATUS | SD_DAT0_STATUS;
1048 val = SD_CMD_STATUS | SD_DAT3_STATUS | SD_DAT2_STATUS |
1049 SD_DAT1_STATUS | SD_DAT0_STATUS;
1050 if ((stat & mask) != val) {
1051 dev_dbg(sdmmc_dev(host),
1052 "%s: SD_BUS_STAT = 0x%x\n", __func__, stat);
1053 rtsx_pci_write_register(pcr, SD_BUS_STAT,
1054 SD_CLK_TOGGLE_EN | SD_CLK_FORCE_STOP, 0);
1055 rtsx_pci_write_register(pcr, CARD_CLK_EN, 0xFF, 0);
1056 return -EINVAL;
1057 }
1058
1059 return 0;
1060}
1061
1062static int sd_change_bank_voltage(struct realtek_pci_sdmmc *host, u8 voltage)
1063{
1064 struct rtsx_pcr *pcr = host->pcr;
1065 int err;
1066
1067 if (voltage == SD_IO_3V3) {
1068 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
1069 if (err < 0)
1070 return err;
1071 } else if (voltage == SD_IO_1V8) {
1072 err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C40 | 0x24);
1073 if (err < 0)
1074 return err;
1075 } else {
1076 return -EINVAL;
1077 }
1078
1079 return 0;
1080}
1081
1082static int sdmmc_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1083{
1084 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
1085 struct rtsx_pcr *pcr = host->pcr;
1086 int err = 0;
1087 u8 voltage;
1088
1089 dev_dbg(sdmmc_dev(host), "%s: signal_voltage = %d\n",
1090 __func__, ios->signal_voltage);
1091
1092 if (host->eject)
1093 return -ENOMEDIUM;
1094
1095 mutex_lock(&pcr->pcr_mutex);
1096
1097 rtsx_pci_start_run(pcr);
1098
1099 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1100 voltage = SD_IO_3V3;
1101 else
1102 voltage = SD_IO_1V8;
1103
1104 if (voltage == SD_IO_1V8) {
1105 err = rtsx_pci_write_register(pcr,
1106 SD30_DRIVE_SEL, 0x07, DRIVER_TYPE_B);
1107 if (err < 0)
1108 goto out;
1109
1110 err = sd_wait_voltage_stable_1(host);
1111 if (err < 0)
1112 goto out;
1113 }
1114
1115 err = sd_change_bank_voltage(host, voltage);
1116 if (err < 0)
1117 goto out;
1118
1119 if (voltage == SD_IO_1V8) {
1120 err = sd_wait_voltage_stable_2(host);
1121 if (err < 0)
1122 goto out;
1123 }
1124
1125 /* Stop toggle SD clock in idle */
1126 err = rtsx_pci_write_register(pcr, SD_BUS_STAT,
1127 SD_CLK_TOGGLE_EN | SD_CLK_FORCE_STOP, 0);
1128
1129out:
1130 mutex_unlock(&pcr->pcr_mutex);
1131
1132 return err;
1133}
1134
1135static int sdmmc_execute_tuning(struct mmc_host *mmc, u32 opcode)
1136{
1137 struct realtek_pci_sdmmc *host = mmc_priv(mmc);
1138 struct rtsx_pcr *pcr = host->pcr;
1139 int err = 0;
1140
1141 if (host->eject)
1142 return -ENOMEDIUM;
1143
1144 mutex_lock(&pcr->pcr_mutex);
1145
1146 rtsx_pci_start_run(pcr);
1147
1148 if (!host->ddr_mode)
1149 err = sd_tuning_rx(host, MMC_SEND_TUNING_BLOCK);
1150
1151 mutex_unlock(&pcr->pcr_mutex);
1152
1153 return err;
1154}
1155
1156static const struct mmc_host_ops realtek_pci_sdmmc_ops = {
1157 .request = sdmmc_request,
1158 .set_ios = sdmmc_set_ios,
1159 .get_ro = sdmmc_get_ro,
1160 .get_cd = sdmmc_get_cd,
1161 .start_signal_voltage_switch = sdmmc_switch_voltage,
1162 .execute_tuning = sdmmc_execute_tuning,
1163};
1164
1165#ifdef CONFIG_PM
1166static int rtsx_pci_sdmmc_suspend(struct platform_device *pdev,
1167 pm_message_t state)
1168{
1169 struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev);
1170 struct mmc_host *mmc = host->mmc;
1171 int err;
1172
1173 dev_dbg(sdmmc_dev(host), "--> %s\n", __func__);
1174
1175 err = mmc_suspend_host(mmc);
1176 if (err)
1177 return err;
1178
1179 return 0;
1180}
1181
1182static int rtsx_pci_sdmmc_resume(struct platform_device *pdev)
1183{
1184 struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev);
1185 struct mmc_host *mmc = host->mmc;
1186
1187 dev_dbg(sdmmc_dev(host), "--> %s\n", __func__);
1188
1189 return mmc_resume_host(mmc);
1190}
1191#else /* CONFIG_PM */
1192#define rtsx_pci_sdmmc_suspend NULL
1193#define rtsx_pci_sdmmc_resume NULL
1194#endif /* CONFIG_PM */
1195
1196static void init_extra_caps(struct realtek_pci_sdmmc *host)
1197{
1198 struct mmc_host *mmc = host->mmc;
1199 struct rtsx_pcr *pcr = host->pcr;
1200
1201 dev_dbg(sdmmc_dev(host), "pcr->extra_caps = 0x%x\n", pcr->extra_caps);
1202
1203 if (pcr->extra_caps & EXTRA_CAPS_SD_SDR50)
1204 mmc->caps |= MMC_CAP_UHS_SDR50;
1205 if (pcr->extra_caps & EXTRA_CAPS_SD_SDR104)
1206 mmc->caps |= MMC_CAP_UHS_SDR104;
1207 if (pcr->extra_caps & EXTRA_CAPS_SD_DDR50)
1208 mmc->caps |= MMC_CAP_UHS_DDR50;
1209 if (pcr->extra_caps & EXTRA_CAPS_MMC_HSDDR)
1210 mmc->caps |= MMC_CAP_1_8V_DDR;
1211 if (pcr->extra_caps & EXTRA_CAPS_MMC_8BIT)
1212 mmc->caps |= MMC_CAP_8_BIT_DATA;
1213}
1214
1215static void realtek_init_host(struct realtek_pci_sdmmc *host)
1216{
1217 struct mmc_host *mmc = host->mmc;
1218
1219 mmc->f_min = 250000;
1220 mmc->f_max = 208000000;
1221 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1222 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SD_HIGHSPEED |
1223 MMC_CAP_MMC_HIGHSPEED | MMC_CAP_BUS_WIDTH_TEST |
1224 MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
1225 mmc->max_current_330 = 400;
1226 mmc->max_current_180 = 800;
1227 mmc->ops = &realtek_pci_sdmmc_ops;
1228
1229 init_extra_caps(host);
1230
1231 mmc->max_segs = 256;
1232 mmc->max_seg_size = 65536;
1233 mmc->max_blk_size = 512;
1234 mmc->max_blk_count = 65535;
1235 mmc->max_req_size = 524288;
1236}
1237
1238static void rtsx_pci_sdmmc_card_event(struct platform_device *pdev)
1239{
1240 struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev);
1241
1242 mmc_detect_change(host->mmc, 0);
1243}
1244
1245static int rtsx_pci_sdmmc_drv_probe(struct platform_device *pdev)
1246{
1247 struct mmc_host *mmc;
1248 struct realtek_pci_sdmmc *host;
1249 struct rtsx_pcr *pcr;
1250 struct pcr_handle *handle = pdev->dev.platform_data;
1251
1252 if (!handle)
1253 return -ENXIO;
1254
1255 pcr = handle->pcr;
1256 if (!pcr)
1257 return -ENXIO;
1258
1259 dev_dbg(&(pdev->dev), ": Realtek PCI-E SDMMC controller found\n");
1260
1261 mmc = mmc_alloc_host(sizeof(*host), &pdev->dev);
1262 if (!mmc)
1263 return -ENOMEM;
1264
1265 host = mmc_priv(mmc);
1266 host->pcr = pcr;
1267 host->mmc = mmc;
1268 host->pdev = pdev;
1269 platform_set_drvdata(pdev, host);
1270 pcr->slots[RTSX_SD_CARD].p_dev = pdev;
1271 pcr->slots[RTSX_SD_CARD].card_event = rtsx_pci_sdmmc_card_event;
1272
1273 mutex_init(&host->host_mutex);
1274
1275 realtek_init_host(host);
1276
1277 mmc_add_host(mmc);
1278
1279 return 0;
1280}
1281
1282static int rtsx_pci_sdmmc_drv_remove(struct platform_device *pdev)
1283{
1284 struct realtek_pci_sdmmc *host = platform_get_drvdata(pdev);
1285 struct rtsx_pcr *pcr;
1286 struct mmc_host *mmc;
1287
1288 if (!host)
1289 return 0;
1290
1291 pcr = host->pcr;
1292 pcr->slots[RTSX_SD_CARD].p_dev = NULL;
1293 pcr->slots[RTSX_SD_CARD].card_event = NULL;
1294 mmc = host->mmc;
1295 host->eject = true;
1296
1297 mutex_lock(&host->host_mutex);
1298 if (host->mrq) {
1299 dev_dbg(&(pdev->dev),
1300 "%s: Controller removed during transfer\n",
1301 mmc_hostname(mmc));
1302
1303 rtsx_pci_complete_unfinished_transfer(pcr);
1304
1305 host->mrq->cmd->error = -ENOMEDIUM;
1306 if (host->mrq->stop)
1307 host->mrq->stop->error = -ENOMEDIUM;
1308 mmc_request_done(mmc, host->mrq);
1309 }
1310 mutex_unlock(&host->host_mutex);
1311
1312 mmc_remove_host(mmc);
1313 mmc_free_host(mmc);
1314
1315 platform_set_drvdata(pdev, NULL);
1316
1317 dev_dbg(&(pdev->dev),
1318 ": Realtek PCI-E SDMMC controller has been removed\n");
1319
1320 return 0;
1321}
1322
1323static struct platform_device_id rtsx_pci_sdmmc_ids[] = {
1324 {
1325 .name = DRV_NAME_RTSX_PCI_SDMMC,
1326 }, {
1327 /* sentinel */
1328 }
1329};
1330MODULE_DEVICE_TABLE(platform, rtsx_pci_sdmmc_ids);
1331
1332static struct platform_driver rtsx_pci_sdmmc_driver = {
1333 .probe = rtsx_pci_sdmmc_drv_probe,
1334 .remove = rtsx_pci_sdmmc_drv_remove,
1335 .id_table = rtsx_pci_sdmmc_ids,
1336 .suspend = rtsx_pci_sdmmc_suspend,
1337 .resume = rtsx_pci_sdmmc_resume,
1338 .driver = {
1339 .owner = THIS_MODULE,
1340 .name = DRV_NAME_RTSX_PCI_SDMMC,
1341 },
1342};
1343module_platform_driver(rtsx_pci_sdmmc_driver);
1344
1345MODULE_LICENSE("GPL");
1346MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1347MODULE_DESCRIPTION("Realtek PCI-E SD/MMC Card Host Driver");