blob: 590fb9aad77d9a690d6948d61715f04af0a43cd9 [file] [log] [blame]
Wei WANGada8a8a2012-10-29 13:49:33 +08001/* Driver for Realtek PCI-Express card reader
2 *
Wei WANG09fd8672013-08-20 14:18:56 +08003 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
Wei WANGada8a8a2012-10-29 13:49:33 +08004 *
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>
Wei WANGada8a8a2012-10-29 13:49:33 +080020 */
21
22#include <linux/pci.h>
23#include <linux/module.h>
Samuel Ortizaec17ea2012-11-09 10:19:54 +010024#include <linux/slab.h>
Wei WANGada8a8a2012-10-29 13:49:33 +080025#include <linux/dma-mapping.h>
26#include <linux/highmem.h>
27#include <linux/interrupt.h>
28#include <linux/delay.h>
29#include <linux/idr.h>
30#include <linux/platform_device.h>
31#include <linux/mfd/core.h>
32#include <linux/mfd/rtsx_pci.h>
Steven Feng87d28442017-05-23 15:13:24 +080033#include <linux/mmc/card.h>
Wei WANGada8a8a2012-10-29 13:49:33 +080034#include <asm/unaligned.h>
35
36#include "rtsx_pcr.h"
37
38static bool msi_en = true;
39module_param(msi_en, bool, S_IRUGO | S_IWUSR);
40MODULE_PARM_DESC(msi_en, "Enable MSI");
41
42static DEFINE_IDR(rtsx_pci_idr);
43static DEFINE_SPINLOCK(rtsx_pci_lock);
44
45static struct mfd_cell rtsx_pcr_cells[] = {
46 [RTSX_SD_CARD] = {
47 .name = DRV_NAME_RTSX_PCI_SDMMC,
48 },
49 [RTSX_MS_CARD] = {
50 .name = DRV_NAME_RTSX_PCI_MS,
51 },
52};
53
Jingoo Han36fcd062013-12-03 08:15:39 +090054static const struct pci_device_id rtsx_pci_ids[] = {
Wei WANGada8a8a2012-10-29 13:49:33 +080055 { PCI_DEVICE(0x10EC, 0x5209), PCI_CLASS_OTHERS << 16, 0xFF0000 },
56 { PCI_DEVICE(0x10EC, 0x5229), PCI_CLASS_OTHERS << 16, 0xFF0000 },
57 { PCI_DEVICE(0x10EC, 0x5289), PCI_CLASS_OTHERS << 16, 0xFF0000 },
Roger Tsenge1237932013-02-04 15:45:59 +080058 { PCI_DEVICE(0x10EC, 0x5227), PCI_CLASS_OTHERS << 16, 0xFF0000 },
Micky Chingce6a5ac2015-06-29 09:19:39 +080059 { PCI_DEVICE(0x10EC, 0x522A), PCI_CLASS_OTHERS << 16, 0xFF0000 },
Wei WANG4c4b8c12013-04-11 10:43:40 +080060 { PCI_DEVICE(0x10EC, 0x5249), PCI_CLASS_OTHERS << 16, 0xFF0000 },
Roger Tseng9032eab2013-04-19 21:52:42 +080061 { PCI_DEVICE(0x10EC, 0x5287), PCI_CLASS_OTHERS << 16, 0xFF0000 },
Micky Ching56cb3cc2013-12-18 10:03:13 +080062 { PCI_DEVICE(0x10EC, 0x5286), PCI_CLASS_OTHERS << 16, 0xFF0000 },
Micky Ching663c425f2015-02-25 13:50:14 +080063 { PCI_DEVICE(0x10EC, 0x524A), PCI_CLASS_OTHERS << 16, 0xFF0000 },
Micky Ching41bc2332015-02-25 13:50:15 +080064 { PCI_DEVICE(0x10EC, 0x525A), PCI_CLASS_OTHERS << 16, 0xFF0000 },
Wei WANGada8a8a2012-10-29 13:49:33 +080065 { 0, }
66};
67
68MODULE_DEVICE_TABLE(pci, rtsx_pci_ids);
69
Micky Ching19f3bd52015-02-25 13:50:13 +080070static inline void rtsx_pci_enable_aspm(struct rtsx_pcr *pcr)
71{
72 rtsx_pci_update_cfg_byte(pcr, pcr->pcie_cap + PCI_EXP_LNKCTL,
73 0xFC, pcr->aspm_en);
74}
75
76static inline void rtsx_pci_disable_aspm(struct rtsx_pcr *pcr)
77{
78 rtsx_pci_update_cfg_byte(pcr, pcr->pcie_cap + PCI_EXP_LNKCTL,
79 0xFC, 0);
80}
81
Rui Feng8275b772017-09-07 16:26:39 +080082int rtsx_comm_set_ltr_latency(struct rtsx_pcr *pcr, u32 latency)
83{
84 rtsx_pci_write_register(pcr, MSGTXDATA0,
85 MASK_8_BIT_DEF, (u8) (latency & 0xFF));
86 rtsx_pci_write_register(pcr, MSGTXDATA1,
87 MASK_8_BIT_DEF, (u8)((latency >> 8) & 0xFF));
88 rtsx_pci_write_register(pcr, MSGTXDATA2,
89 MASK_8_BIT_DEF, (u8)((latency >> 16) & 0xFF));
90 rtsx_pci_write_register(pcr, MSGTXDATA3,
91 MASK_8_BIT_DEF, (u8)((latency >> 24) & 0xFF));
92 rtsx_pci_write_register(pcr, LTR_CTL, LTR_TX_EN_MASK |
93 LTR_LATENCY_MODE_MASK, LTR_TX_EN_1 | LTR_LATENCY_MODE_SW);
94
95 return 0;
96}
97
98int rtsx_set_ltr_latency(struct rtsx_pcr *pcr, u32 latency)
99{
100 if (pcr->ops->set_ltr_latency)
101 return pcr->ops->set_ltr_latency(pcr, latency);
102 else
103 return rtsx_comm_set_ltr_latency(pcr, latency);
104}
105
106static void rtsx_comm_set_aspm(struct rtsx_pcr *pcr, bool enable)
107{
108 struct rtsx_cr_option *option = &pcr->option;
109
110 if (pcr->aspm_enabled == enable)
111 return;
112
113 if (option->dev_aspm_mode == DEV_ASPM_DYNAMIC) {
114 if (enable)
115 rtsx_pci_enable_aspm(pcr);
116 else
117 rtsx_pci_disable_aspm(pcr);
118 } else if (option->dev_aspm_mode == DEV_ASPM_BACKDOOR) {
119 u8 mask = FORCE_ASPM_VAL_MASK;
120 u8 val = 0;
121
122 if (enable)
123 val = pcr->aspm_en;
124 rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, mask, val);
125 }
126
127 pcr->aspm_enabled = enable;
128}
129
130static void rtsx_disable_aspm(struct rtsx_pcr *pcr)
131{
132 if (pcr->ops->set_aspm)
133 pcr->ops->set_aspm(pcr, false);
134 else
135 rtsx_comm_set_aspm(pcr, false);
136}
137
138int rtsx_set_l1off_sub(struct rtsx_pcr *pcr, u8 val)
139{
140 rtsx_pci_write_register(pcr, L1SUB_CONFIG3, 0xFF, val);
141
142 return 0;
143}
144
145void rtsx_set_l1off_sub_cfg_d0(struct rtsx_pcr *pcr, int active)
146{
147 if (pcr->ops->set_l1off_cfg_sub_d0)
148 pcr->ops->set_l1off_cfg_sub_d0(pcr, active);
149}
150
151static void rtsx_comm_pm_full_on(struct rtsx_pcr *pcr)
152{
153 struct rtsx_cr_option *option = &pcr->option;
154
155 rtsx_disable_aspm(pcr);
156
157 if (option->ltr_enabled)
158 rtsx_set_ltr_latency(pcr, option->ltr_active_latency);
159
160 if (rtsx_check_dev_flag(pcr, LTR_L1SS_PWR_GATE_EN))
161 rtsx_set_l1off_sub_cfg_d0(pcr, 1);
162}
163
164void rtsx_pm_full_on(struct rtsx_pcr *pcr)
165{
166 if (pcr->ops->full_on)
167 pcr->ops->full_on(pcr);
168 else
169 rtsx_comm_pm_full_on(pcr);
170}
171
Wei WANGada8a8a2012-10-29 13:49:33 +0800172void rtsx_pci_start_run(struct rtsx_pcr *pcr)
173{
174 /* If pci device removed, don't queue idle work any more */
175 if (pcr->remove_pci)
176 return;
177
178 if (pcr->state != PDEV_STAT_RUN) {
179 pcr->state = PDEV_STAT_RUN;
180 if (pcr->ops->enable_auto_blink)
181 pcr->ops->enable_auto_blink(pcr);
Rui Feng8275b772017-09-07 16:26:39 +0800182 rtsx_pm_full_on(pcr);
Wei WANGada8a8a2012-10-29 13:49:33 +0800183 }
184
185 mod_delayed_work(system_wq, &pcr->idle_work, msecs_to_jiffies(200));
186}
187EXPORT_SYMBOL_GPL(rtsx_pci_start_run);
188
189int rtsx_pci_write_register(struct rtsx_pcr *pcr, u16 addr, u8 mask, u8 data)
190{
191 int i;
192 u32 val = HAIMR_WRITE_START;
193
194 val |= (u32)(addr & 0x3FFF) << 16;
195 val |= (u32)mask << 8;
196 val |= (u32)data;
197
198 rtsx_pci_writel(pcr, RTSX_HAIMR, val);
199
200 for (i = 0; i < MAX_RW_REG_CNT; i++) {
201 val = rtsx_pci_readl(pcr, RTSX_HAIMR);
202 if ((val & HAIMR_TRANS_END) == 0) {
203 if (data != (u8)val)
204 return -EIO;
205 return 0;
206 }
207 }
208
209 return -ETIMEDOUT;
210}
211EXPORT_SYMBOL_GPL(rtsx_pci_write_register);
212
213int rtsx_pci_read_register(struct rtsx_pcr *pcr, u16 addr, u8 *data)
214{
215 u32 val = HAIMR_READ_START;
216 int i;
217
218 val |= (u32)(addr & 0x3FFF) << 16;
219 rtsx_pci_writel(pcr, RTSX_HAIMR, val);
220
221 for (i = 0; i < MAX_RW_REG_CNT; i++) {
222 val = rtsx_pci_readl(pcr, RTSX_HAIMR);
223 if ((val & HAIMR_TRANS_END) == 0)
224 break;
225 }
226
227 if (i >= MAX_RW_REG_CNT)
228 return -ETIMEDOUT;
229
230 if (data)
231 *data = (u8)(val & 0xFF);
232
233 return 0;
234}
235EXPORT_SYMBOL_GPL(rtsx_pci_read_register);
236
Micky Ching663c425f2015-02-25 13:50:14 +0800237int __rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val)
Wei WANGada8a8a2012-10-29 13:49:33 +0800238{
239 int err, i, finished = 0;
240 u8 tmp;
241
242 rtsx_pci_init_cmd(pcr);
243
244 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYDATA0, 0xFF, (u8)val);
245 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYDATA1, 0xFF, (u8)(val >> 8));
246 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYADDR, 0xFF, addr);
247 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYRWCTL, 0xFF, 0x81);
248
249 err = rtsx_pci_send_cmd(pcr, 100);
250 if (err < 0)
251 return err;
252
253 for (i = 0; i < 100000; i++) {
254 err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
255 if (err < 0)
256 return err;
257
258 if (!(tmp & 0x80)) {
259 finished = 1;
260 break;
261 }
262 }
263
264 if (!finished)
265 return -ETIMEDOUT;
266
267 return 0;
268}
Micky Ching663c425f2015-02-25 13:50:14 +0800269
270int rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val)
271{
272 if (pcr->ops->write_phy)
273 return pcr->ops->write_phy(pcr, addr, val);
274
275 return __rtsx_pci_write_phy_register(pcr, addr, val);
276}
Wei WANGada8a8a2012-10-29 13:49:33 +0800277EXPORT_SYMBOL_GPL(rtsx_pci_write_phy_register);
278
Micky Ching663c425f2015-02-25 13:50:14 +0800279int __rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val)
Wei WANGada8a8a2012-10-29 13:49:33 +0800280{
281 int err, i, finished = 0;
282 u16 data;
283 u8 *ptr, tmp;
284
285 rtsx_pci_init_cmd(pcr);
286
287 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYADDR, 0xFF, addr);
288 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYRWCTL, 0xFF, 0x80);
289
290 err = rtsx_pci_send_cmd(pcr, 100);
291 if (err < 0)
292 return err;
293
294 for (i = 0; i < 100000; i++) {
295 err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp);
296 if (err < 0)
297 return err;
298
299 if (!(tmp & 0x80)) {
300 finished = 1;
301 break;
302 }
303 }
304
305 if (!finished)
306 return -ETIMEDOUT;
307
308 rtsx_pci_init_cmd(pcr);
309
310 rtsx_pci_add_cmd(pcr, READ_REG_CMD, PHYDATA0, 0, 0);
311 rtsx_pci_add_cmd(pcr, READ_REG_CMD, PHYDATA1, 0, 0);
312
313 err = rtsx_pci_send_cmd(pcr, 100);
314 if (err < 0)
315 return err;
316
317 ptr = rtsx_pci_get_cmd_data(pcr);
318 data = ((u16)ptr[1] << 8) | ptr[0];
319
320 if (val)
321 *val = data;
322
323 return 0;
324}
Micky Ching663c425f2015-02-25 13:50:14 +0800325
326int rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val)
327{
328 if (pcr->ops->read_phy)
329 return pcr->ops->read_phy(pcr, addr, val);
330
331 return __rtsx_pci_read_phy_register(pcr, addr, val);
332}
Wei WANGada8a8a2012-10-29 13:49:33 +0800333EXPORT_SYMBOL_GPL(rtsx_pci_read_phy_register);
334
335void rtsx_pci_stop_cmd(struct rtsx_pcr *pcr)
336{
337 rtsx_pci_writel(pcr, RTSX_HCBCTLR, STOP_CMD);
338 rtsx_pci_writel(pcr, RTSX_HDBCTLR, STOP_DMA);
339
340 rtsx_pci_write_register(pcr, DMACTL, 0x80, 0x80);
341 rtsx_pci_write_register(pcr, RBCTL, 0x80, 0x80);
342}
343EXPORT_SYMBOL_GPL(rtsx_pci_stop_cmd);
344
345void rtsx_pci_add_cmd(struct rtsx_pcr *pcr,
346 u8 cmd_type, u16 reg_addr, u8 mask, u8 data)
347{
348 unsigned long flags;
349 u32 val = 0;
350 u32 *ptr = (u32 *)(pcr->host_cmds_ptr);
351
352 val |= (u32)(cmd_type & 0x03) << 30;
353 val |= (u32)(reg_addr & 0x3FFF) << 16;
354 val |= (u32)mask << 8;
355 val |= (u32)data;
356
357 spin_lock_irqsave(&pcr->lock, flags);
358 ptr += pcr->ci;
359 if (pcr->ci < (HOST_CMDS_BUF_LEN / 4)) {
360 put_unaligned_le32(val, ptr);
361 ptr++;
362 pcr->ci++;
363 }
364 spin_unlock_irqrestore(&pcr->lock, flags);
365}
366EXPORT_SYMBOL_GPL(rtsx_pci_add_cmd);
367
368void rtsx_pci_send_cmd_no_wait(struct rtsx_pcr *pcr)
369{
370 u32 val = 1 << 31;
371
372 rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
373
374 val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
375 /* Hardware Auto Response */
376 val |= 0x40000000;
377 rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
378}
379EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd_no_wait);
380
381int rtsx_pci_send_cmd(struct rtsx_pcr *pcr, int timeout)
382{
383 struct completion trans_done;
384 u32 val = 1 << 31;
385 long timeleft;
386 unsigned long flags;
387 int err = 0;
388
389 spin_lock_irqsave(&pcr->lock, flags);
390
391 /* set up data structures for the wakeup system */
392 pcr->done = &trans_done;
393 pcr->trans_result = TRANS_NOT_READY;
394 init_completion(&trans_done);
395
396 rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
397
398 val |= (u32)(pcr->ci * 4) & 0x00FFFFFF;
399 /* Hardware Auto Response */
400 val |= 0x40000000;
401 rtsx_pci_writel(pcr, RTSX_HCBCTLR, val);
402
403 spin_unlock_irqrestore(&pcr->lock, flags);
404
405 /* Wait for TRANS_OK_INT */
406 timeleft = wait_for_completion_interruptible_timeout(
407 &trans_done, msecs_to_jiffies(timeout));
408 if (timeleft <= 0) {
Micky Ching0523b8f2015-02-25 13:50:16 +0800409 pcr_dbg(pcr, "Timeout (%s %d)\n", __func__, __LINE__);
Wei WANGada8a8a2012-10-29 13:49:33 +0800410 err = -ETIMEDOUT;
411 goto finish_send_cmd;
412 }
413
414 spin_lock_irqsave(&pcr->lock, flags);
415 if (pcr->trans_result == TRANS_RESULT_FAIL)
416 err = -EINVAL;
417 else if (pcr->trans_result == TRANS_RESULT_OK)
418 err = 0;
419 else if (pcr->trans_result == TRANS_NO_DEVICE)
420 err = -ENODEV;
421 spin_unlock_irqrestore(&pcr->lock, flags);
422
423finish_send_cmd:
424 spin_lock_irqsave(&pcr->lock, flags);
425 pcr->done = NULL;
426 spin_unlock_irqrestore(&pcr->lock, flags);
427
428 if ((err < 0) && (err != -ENODEV))
429 rtsx_pci_stop_cmd(pcr);
430
431 if (pcr->finish_me)
432 complete(pcr->finish_me);
433
434 return err;
435}
436EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd);
437
438static void rtsx_pci_add_sg_tbl(struct rtsx_pcr *pcr,
439 dma_addr_t addr, unsigned int len, int end)
440{
441 u64 *ptr = (u64 *)(pcr->host_sg_tbl_ptr) + pcr->sgi;
442 u64 val;
443 u8 option = SG_VALID | SG_TRANS_DATA;
444
Micky Ching0523b8f2015-02-25 13:50:16 +0800445 pcr_dbg(pcr, "DMA addr: 0x%x, Len: 0x%x\n", (unsigned int)addr, len);
Wei WANGada8a8a2012-10-29 13:49:33 +0800446
447 if (end)
448 option |= SG_END;
449 val = ((u64)addr << 32) | ((u64)len << 12) | option;
450
451 put_unaligned_le64(val, ptr);
Wei WANGada8a8a2012-10-29 13:49:33 +0800452 pcr->sgi++;
453}
454
455int rtsx_pci_transfer_data(struct rtsx_pcr *pcr, struct scatterlist *sglist,
456 int num_sg, bool read, int timeout)
457{
Micky Ching8cd11832014-06-06 15:05:44 +0800458 int err = 0, count;
Wei WANGada8a8a2012-10-29 13:49:33 +0800459
Micky Ching0523b8f2015-02-25 13:50:16 +0800460 pcr_dbg(pcr, "--> %s: num_sg = %d\n", __func__, num_sg);
Micky Ching8cd11832014-06-06 15:05:44 +0800461 count = rtsx_pci_dma_map_sg(pcr, sglist, num_sg, read);
462 if (count < 1)
463 return -EINVAL;
Micky Ching0523b8f2015-02-25 13:50:16 +0800464 pcr_dbg(pcr, "DMA mapping count: %d\n", count);
Micky Ching98fcc572014-04-29 09:54:54 +0800465
Micky Ching8cd11832014-06-06 15:05:44 +0800466 err = rtsx_pci_dma_transfer(pcr, sglist, count, read, timeout);
467
468 rtsx_pci_dma_unmap_sg(pcr, sglist, num_sg, read);
469
470 return err;
471}
472EXPORT_SYMBOL_GPL(rtsx_pci_transfer_data);
473
474int rtsx_pci_dma_map_sg(struct rtsx_pcr *pcr, struct scatterlist *sglist,
475 int num_sg, bool read)
476{
477 enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
478
Micky Ching98fcc572014-04-29 09:54:54 +0800479 if (pcr->remove_pci)
480 return -EINVAL;
481
482 if ((sglist == NULL) || (num_sg <= 0))
483 return -EINVAL;
484
Micky Ching8cd11832014-06-06 15:05:44 +0800485 return dma_map_sg(&(pcr->pci->dev), sglist, num_sg, dir);
486}
487EXPORT_SYMBOL_GPL(rtsx_pci_dma_map_sg);
Micky Ching98fcc572014-04-29 09:54:54 +0800488
Micky Ching8cd11832014-06-06 15:05:44 +0800489void rtsx_pci_dma_unmap_sg(struct rtsx_pcr *pcr, struct scatterlist *sglist,
490 int num_sg, bool read)
491{
492 enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
493
494 dma_unmap_sg(&(pcr->pci->dev), sglist, num_sg, dir);
495}
496EXPORT_SYMBOL_GPL(rtsx_pci_dma_unmap_sg);
497
498int rtsx_pci_dma_transfer(struct rtsx_pcr *pcr, struct scatterlist *sglist,
499 int count, bool read, int timeout)
500{
501 struct completion trans_done;
502 struct scatterlist *sg;
503 dma_addr_t addr;
504 long timeleft;
505 unsigned long flags;
506 unsigned int len;
507 int i, err = 0;
508 u32 val;
509 u8 dir = read ? DEVICE_TO_HOST : HOST_TO_DEVICE;
510
511 if (pcr->remove_pci)
512 return -ENODEV;
513
514 if ((sglist == NULL) || (count < 1))
Wei WANGada8a8a2012-10-29 13:49:33 +0800515 return -EINVAL;
Wei WANGada8a8a2012-10-29 13:49:33 +0800516
Micky Ching98fcc572014-04-29 09:54:54 +0800517 val = ((u32)(dir & 0x01) << 29) | TRIG_DMA | ADMA_MODE;
518 pcr->sgi = 0;
519 for_each_sg(sglist, sg, count, i) {
520 addr = sg_dma_address(sg);
521 len = sg_dma_len(sg);
522 rtsx_pci_add_sg_tbl(pcr, addr, len, i == count - 1);
523 }
Wei WANGada8a8a2012-10-29 13:49:33 +0800524
525 spin_lock_irqsave(&pcr->lock, flags);
526
527 pcr->done = &trans_done;
528 pcr->trans_result = TRANS_NOT_READY;
529 init_completion(&trans_done);
Micky Ching98fcc572014-04-29 09:54:54 +0800530 rtsx_pci_writel(pcr, RTSX_HDBAR, pcr->host_sg_tbl_addr);
531 rtsx_pci_writel(pcr, RTSX_HDBCTLR, val);
Wei WANGada8a8a2012-10-29 13:49:33 +0800532
533 spin_unlock_irqrestore(&pcr->lock, flags);
534
535 timeleft = wait_for_completion_interruptible_timeout(
536 &trans_done, msecs_to_jiffies(timeout));
537 if (timeleft <= 0) {
Micky Ching0523b8f2015-02-25 13:50:16 +0800538 pcr_dbg(pcr, "Timeout (%s %d)\n", __func__, __LINE__);
Wei WANGada8a8a2012-10-29 13:49:33 +0800539 err = -ETIMEDOUT;
540 goto out;
541 }
542
543 spin_lock_irqsave(&pcr->lock, flags);
Steven Feng87d28442017-05-23 15:13:24 +0800544 if (pcr->trans_result == TRANS_RESULT_FAIL) {
545 err = -EILSEQ;
546 if (pcr->dma_error_count < RTS_MAX_TIMES_FREQ_REDUCTION)
547 pcr->dma_error_count++;
548 }
549
Wei WANGada8a8a2012-10-29 13:49:33 +0800550 else if (pcr->trans_result == TRANS_NO_DEVICE)
551 err = -ENODEV;
Wei WANGada8a8a2012-10-29 13:49:33 +0800552 spin_unlock_irqrestore(&pcr->lock, flags);
553
554out:
555 spin_lock_irqsave(&pcr->lock, flags);
556 pcr->done = NULL;
557 spin_unlock_irqrestore(&pcr->lock, flags);
558
Wei WANGada8a8a2012-10-29 13:49:33 +0800559 if ((err < 0) && (err != -ENODEV))
560 rtsx_pci_stop_cmd(pcr);
561
562 if (pcr->finish_me)
563 complete(pcr->finish_me);
564
565 return err;
566}
Micky Ching8cd11832014-06-06 15:05:44 +0800567EXPORT_SYMBOL_GPL(rtsx_pci_dma_transfer);
Wei WANGada8a8a2012-10-29 13:49:33 +0800568
569int rtsx_pci_read_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
570{
571 int err;
572 int i, j;
573 u16 reg;
574 u8 *ptr;
575
576 if (buf_len > 512)
577 buf_len = 512;
578
579 ptr = buf;
580 reg = PPBUF_BASE2;
581 for (i = 0; i < buf_len / 256; i++) {
582 rtsx_pci_init_cmd(pcr);
583
584 for (j = 0; j < 256; j++)
585 rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
586
587 err = rtsx_pci_send_cmd(pcr, 250);
588 if (err < 0)
589 return err;
590
591 memcpy(ptr, rtsx_pci_get_cmd_data(pcr), 256);
592 ptr += 256;
593 }
594
595 if (buf_len % 256) {
596 rtsx_pci_init_cmd(pcr);
597
598 for (j = 0; j < buf_len % 256; j++)
599 rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0);
600
601 err = rtsx_pci_send_cmd(pcr, 250);
602 if (err < 0)
603 return err;
604 }
605
606 memcpy(ptr, rtsx_pci_get_cmd_data(pcr), buf_len % 256);
607
608 return 0;
609}
610EXPORT_SYMBOL_GPL(rtsx_pci_read_ppbuf);
611
612int rtsx_pci_write_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len)
613{
614 int err;
615 int i, j;
616 u16 reg;
617 u8 *ptr;
618
619 if (buf_len > 512)
620 buf_len = 512;
621
622 ptr = buf;
623 reg = PPBUF_BASE2;
624 for (i = 0; i < buf_len / 256; i++) {
625 rtsx_pci_init_cmd(pcr);
626
627 for (j = 0; j < 256; j++) {
628 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
629 reg++, 0xFF, *ptr);
630 ptr++;
631 }
632
633 err = rtsx_pci_send_cmd(pcr, 250);
634 if (err < 0)
635 return err;
636 }
637
638 if (buf_len % 256) {
639 rtsx_pci_init_cmd(pcr);
640
641 for (j = 0; j < buf_len % 256; j++) {
642 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
643 reg++, 0xFF, *ptr);
644 ptr++;
645 }
646
647 err = rtsx_pci_send_cmd(pcr, 250);
648 if (err < 0)
649 return err;
650 }
651
652 return 0;
653}
654EXPORT_SYMBOL_GPL(rtsx_pci_write_ppbuf);
655
656static int rtsx_pci_set_pull_ctl(struct rtsx_pcr *pcr, const u32 *tbl)
657{
Wei WANGada8a8a2012-10-29 13:49:33 +0800658 rtsx_pci_init_cmd(pcr);
659
660 while (*tbl & 0xFFFF0000) {
661 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD,
662 (u16)(*tbl >> 16), 0xFF, (u8)(*tbl));
663 tbl++;
664 }
665
Javier Martinez Canillasb158b692015-09-29 13:26:05 +0200666 return rtsx_pci_send_cmd(pcr, 100);
Wei WANGada8a8a2012-10-29 13:49:33 +0800667}
668
669int rtsx_pci_card_pull_ctl_enable(struct rtsx_pcr *pcr, int card)
670{
671 const u32 *tbl;
672
673 if (card == RTSX_SD_CARD)
674 tbl = pcr->sd_pull_ctl_enable_tbl;
675 else if (card == RTSX_MS_CARD)
676 tbl = pcr->ms_pull_ctl_enable_tbl;
677 else
678 return -EINVAL;
679
680 return rtsx_pci_set_pull_ctl(pcr, tbl);
681}
682EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_enable);
683
684int rtsx_pci_card_pull_ctl_disable(struct rtsx_pcr *pcr, int card)
685{
686 const u32 *tbl;
687
688 if (card == RTSX_SD_CARD)
689 tbl = pcr->sd_pull_ctl_disable_tbl;
690 else if (card == RTSX_MS_CARD)
691 tbl = pcr->ms_pull_ctl_disable_tbl;
692 else
693 return -EINVAL;
694
695
696 return rtsx_pci_set_pull_ctl(pcr, tbl);
697}
698EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_disable);
699
700static void rtsx_pci_enable_bus_int(struct rtsx_pcr *pcr)
701{
702 pcr->bier = TRANS_OK_INT_EN | TRANS_FAIL_INT_EN | SD_INT_EN;
703
704 if (pcr->num_slots > 1)
705 pcr->bier |= MS_INT_EN;
706
707 /* Enable Bus Interrupt */
708 rtsx_pci_writel(pcr, RTSX_BIER, pcr->bier);
709
Micky Ching0523b8f2015-02-25 13:50:16 +0800710 pcr_dbg(pcr, "RTSX_BIER: 0x%08x\n", pcr->bier);
Wei WANGada8a8a2012-10-29 13:49:33 +0800711}
712
713static inline u8 double_ssc_depth(u8 depth)
714{
715 return ((depth > 1) ? (depth - 1) : depth);
716}
717
718static u8 revise_ssc_depth(u8 ssc_depth, u8 div)
719{
720 if (div > CLK_DIV_1) {
721 if (ssc_depth > (div - 1))
722 ssc_depth -= (div - 1);
723 else
724 ssc_depth = SSC_DEPTH_4M;
725 }
726
727 return ssc_depth;
728}
729
730int rtsx_pci_switch_clock(struct rtsx_pcr *pcr, unsigned int card_clock,
731 u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk)
732{
733 int err, clk;
Wei WANGeebbe252013-01-29 15:21:36 +0800734 u8 n, clk_divider, mcu_cnt, div;
Colin Ian Kinge26ae362017-06-30 10:33:27 +0100735 static const u8 depth[] = {
Wei WANGada8a8a2012-10-29 13:49:33 +0800736 [RTSX_SSC_DEPTH_4M] = SSC_DEPTH_4M,
737 [RTSX_SSC_DEPTH_2M] = SSC_DEPTH_2M,
738 [RTSX_SSC_DEPTH_1M] = SSC_DEPTH_1M,
739 [RTSX_SSC_DEPTH_500K] = SSC_DEPTH_500K,
740 [RTSX_SSC_DEPTH_250K] = SSC_DEPTH_250K,
741 };
742
743 if (initial_mode) {
744 /* We use 250k(around) here, in initial stage */
745 clk_divider = SD_CLK_DIVIDE_128;
746 card_clock = 30000000;
747 } else {
748 clk_divider = SD_CLK_DIVIDE_0;
749 }
750 err = rtsx_pci_write_register(pcr, SD_CFG1,
751 SD_CLK_DIVIDE_MASK, clk_divider);
752 if (err < 0)
753 return err;
754
Steven Feng87d28442017-05-23 15:13:24 +0800755 /* Reduce card clock by 20MHz each time a DMA transfer error occurs */
756 if (card_clock == UHS_SDR104_MAX_DTR &&
757 pcr->dma_error_count &&
758 PCI_PID(pcr) == RTS5227_DEVICE_ID)
759 card_clock = UHS_SDR104_MAX_DTR -
760 (pcr->dma_error_count * 20000000);
761
Wei WANGada8a8a2012-10-29 13:49:33 +0800762 card_clock /= 1000000;
Micky Ching0523b8f2015-02-25 13:50:16 +0800763 pcr_dbg(pcr, "Switch card clock to %dMHz\n", card_clock);
Wei WANGada8a8a2012-10-29 13:49:33 +0800764
Wei WANGada8a8a2012-10-29 13:49:33 +0800765 clk = card_clock;
766 if (!initial_mode && double_clk)
767 clk = card_clock * 2;
Micky Ching0523b8f2015-02-25 13:50:16 +0800768 pcr_dbg(pcr, "Internal SSC clock: %dMHz (cur_clock = %d)\n",
769 clk, pcr->cur_clock);
Wei WANGada8a8a2012-10-29 13:49:33 +0800770
771 if (clk == pcr->cur_clock)
772 return 0;
773
Wei WANGab4e8f82013-01-23 09:51:06 +0800774 if (pcr->ops->conv_clk_and_div_n)
Wei WANG678cacd2013-01-29 15:21:35 +0800775 n = (u8)pcr->ops->conv_clk_and_div_n(clk, CLK_TO_DIV_N);
Wei WANGab4e8f82013-01-23 09:51:06 +0800776 else
Wei WANG678cacd2013-01-29 15:21:35 +0800777 n = (u8)(clk - 2);
Wei WANGeebbe252013-01-29 15:21:36 +0800778 if ((clk <= 2) || (n > MAX_DIV_N_PCR))
Wei WANGada8a8a2012-10-29 13:49:33 +0800779 return -EINVAL;
780
781 mcu_cnt = (u8)(125/clk + 3);
782 if (mcu_cnt > 15)
783 mcu_cnt = 15;
784
Wei WANGeebbe252013-01-29 15:21:36 +0800785 /* Make sure that the SSC clock div_n is not less than MIN_DIV_N_PCR */
Wei WANGada8a8a2012-10-29 13:49:33 +0800786 div = CLK_DIV_1;
Wei WANGeebbe252013-01-29 15:21:36 +0800787 while ((n < MIN_DIV_N_PCR) && (div < CLK_DIV_8)) {
Wei WANGab4e8f82013-01-23 09:51:06 +0800788 if (pcr->ops->conv_clk_and_div_n) {
Wei WANG678cacd2013-01-29 15:21:35 +0800789 int dbl_clk = pcr->ops->conv_clk_and_div_n(n,
Wei WANGab4e8f82013-01-23 09:51:06 +0800790 DIV_N_TO_CLK) * 2;
Wei WANG678cacd2013-01-29 15:21:35 +0800791 n = (u8)pcr->ops->conv_clk_and_div_n(dbl_clk,
Wei WANGab4e8f82013-01-23 09:51:06 +0800792 CLK_TO_DIV_N);
793 } else {
Wei WANG678cacd2013-01-29 15:21:35 +0800794 n = (n + 2) * 2 - 2;
Wei WANGab4e8f82013-01-23 09:51:06 +0800795 }
Wei WANGada8a8a2012-10-29 13:49:33 +0800796 div++;
797 }
Micky Ching0523b8f2015-02-25 13:50:16 +0800798 pcr_dbg(pcr, "n = %d, div = %d\n", n, div);
Wei WANGada8a8a2012-10-29 13:49:33 +0800799
800 ssc_depth = depth[ssc_depth];
801 if (double_clk)
802 ssc_depth = double_ssc_depth(ssc_depth);
803
804 ssc_depth = revise_ssc_depth(ssc_depth, div);
Micky Ching0523b8f2015-02-25 13:50:16 +0800805 pcr_dbg(pcr, "ssc_depth = %d\n", ssc_depth);
Wei WANGada8a8a2012-10-29 13:49:33 +0800806
807 rtsx_pci_init_cmd(pcr);
808 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL,
809 CLK_LOW_FREQ, CLK_LOW_FREQ);
810 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV,
811 0xFF, (div << 4) | mcu_cnt);
812 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0);
813 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2,
814 SSC_DEPTH_MASK, ssc_depth);
Wei WANG678cacd2013-01-29 15:21:35 +0800815 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, n);
Wei WANGada8a8a2012-10-29 13:49:33 +0800816 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB);
817 if (vpclk) {
818 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
819 PHASE_NOT_RESET, 0);
820 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL,
821 PHASE_NOT_RESET, PHASE_NOT_RESET);
822 }
823
824 err = rtsx_pci_send_cmd(pcr, 2000);
825 if (err < 0)
826 return err;
827
828 /* Wait SSC clock stable */
829 udelay(10);
830 err = rtsx_pci_write_register(pcr, CLK_CTL, CLK_LOW_FREQ, 0);
831 if (err < 0)
832 return err;
833
834 pcr->cur_clock = clk;
835 return 0;
836}
837EXPORT_SYMBOL_GPL(rtsx_pci_switch_clock);
838
839int rtsx_pci_card_power_on(struct rtsx_pcr *pcr, int card)
840{
841 if (pcr->ops->card_power_on)
842 return pcr->ops->card_power_on(pcr, card);
843
844 return 0;
845}
846EXPORT_SYMBOL_GPL(rtsx_pci_card_power_on);
847
848int rtsx_pci_card_power_off(struct rtsx_pcr *pcr, int card)
849{
850 if (pcr->ops->card_power_off)
851 return pcr->ops->card_power_off(pcr, card);
852
853 return 0;
854}
855EXPORT_SYMBOL_GPL(rtsx_pci_card_power_off);
856
Wei WANGc3481952013-02-08 15:24:27 +0800857int rtsx_pci_card_exclusive_check(struct rtsx_pcr *pcr, int card)
858{
Colin Ian Kinge26ae362017-06-30 10:33:27 +0100859 static const unsigned int cd_mask[] = {
Wei WANGc3481952013-02-08 15:24:27 +0800860 [RTSX_SD_CARD] = SD_EXIST,
861 [RTSX_MS_CARD] = MS_EXIST
862 };
863
Wei WANG773ccdf2013-08-20 14:18:51 +0800864 if (!(pcr->flags & PCR_MS_PMOS)) {
Wei WANGc3481952013-02-08 15:24:27 +0800865 /* When using single PMOS, accessing card is not permitted
866 * if the existing card is not the designated one.
867 */
868 if (pcr->card_exist & (~cd_mask[card]))
869 return -EIO;
870 }
871
872 return 0;
873}
874EXPORT_SYMBOL_GPL(rtsx_pci_card_exclusive_check);
875
Wei WANGd817ac42013-01-23 09:51:04 +0800876int rtsx_pci_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
877{
878 if (pcr->ops->switch_output_voltage)
879 return pcr->ops->switch_output_voltage(pcr, voltage);
880
881 return 0;
882}
883EXPORT_SYMBOL_GPL(rtsx_pci_switch_output_voltage);
884
Wei WANGada8a8a2012-10-29 13:49:33 +0800885unsigned int rtsx_pci_card_exist(struct rtsx_pcr *pcr)
886{
887 unsigned int val;
888
889 val = rtsx_pci_readl(pcr, RTSX_BIPR);
890 if (pcr->ops->cd_deglitch)
891 val = pcr->ops->cd_deglitch(pcr);
892
893 return val;
894}
895EXPORT_SYMBOL_GPL(rtsx_pci_card_exist);
896
897void rtsx_pci_complete_unfinished_transfer(struct rtsx_pcr *pcr)
898{
899 struct completion finish;
900
901 pcr->finish_me = &finish;
902 init_completion(&finish);
903
904 if (pcr->done)
905 complete(pcr->done);
906
907 if (!pcr->remove_pci)
908 rtsx_pci_stop_cmd(pcr);
909
910 wait_for_completion_interruptible_timeout(&finish,
911 msecs_to_jiffies(2));
912 pcr->finish_me = NULL;
913}
914EXPORT_SYMBOL_GPL(rtsx_pci_complete_unfinished_transfer);
915
916static void rtsx_pci_card_detect(struct work_struct *work)
917{
918 struct delayed_work *dwork;
919 struct rtsx_pcr *pcr;
920 unsigned long flags;
Wei WANG504decc2013-01-29 15:21:37 +0800921 unsigned int card_detect = 0, card_inserted, card_removed;
Wei WANGada8a8a2012-10-29 13:49:33 +0800922 u32 irq_status;
923
924 dwork = to_delayed_work(work);
925 pcr = container_of(dwork, struct rtsx_pcr, carddet_work);
926
Micky Ching0523b8f2015-02-25 13:50:16 +0800927 pcr_dbg(pcr, "--> %s\n", __func__);
Wei WANGada8a8a2012-10-29 13:49:33 +0800928
Wei WANG504decc2013-01-29 15:21:37 +0800929 mutex_lock(&pcr->pcr_mutex);
Wei WANGada8a8a2012-10-29 13:49:33 +0800930 spin_lock_irqsave(&pcr->lock, flags);
931
932 irq_status = rtsx_pci_readl(pcr, RTSX_BIPR);
Micky Ching0523b8f2015-02-25 13:50:16 +0800933 pcr_dbg(pcr, "irq_status: 0x%08x\n", irq_status);
Wei WANGada8a8a2012-10-29 13:49:33 +0800934
Wei WANG504decc2013-01-29 15:21:37 +0800935 irq_status &= CARD_EXIST;
936 card_inserted = pcr->card_inserted & irq_status;
937 card_removed = pcr->card_removed;
938 pcr->card_inserted = 0;
939 pcr->card_removed = 0;
Wei WANGada8a8a2012-10-29 13:49:33 +0800940
941 spin_unlock_irqrestore(&pcr->lock, flags);
942
Wei WANG504decc2013-01-29 15:21:37 +0800943 if (card_inserted || card_removed) {
Micky Ching0523b8f2015-02-25 13:50:16 +0800944 pcr_dbg(pcr, "card_inserted: 0x%x, card_removed: 0x%x\n",
945 card_inserted, card_removed);
Wei WANG504decc2013-01-29 15:21:37 +0800946
947 if (pcr->ops->cd_deglitch)
948 card_inserted = pcr->ops->cd_deglitch(pcr);
949
950 card_detect = card_inserted | card_removed;
Wei WANGc3481952013-02-08 15:24:27 +0800951
952 pcr->card_exist |= card_inserted;
953 pcr->card_exist &= ~card_removed;
Wei WANG504decc2013-01-29 15:21:37 +0800954 }
955
956 mutex_unlock(&pcr->pcr_mutex);
957
Wei WANG2d1484f2013-01-27 01:55:16 +0100958 if ((card_detect & SD_EXIST) && pcr->slots[RTSX_SD_CARD].card_event)
Wei WANGada8a8a2012-10-29 13:49:33 +0800959 pcr->slots[RTSX_SD_CARD].card_event(
960 pcr->slots[RTSX_SD_CARD].p_dev);
Wei WANG2d1484f2013-01-27 01:55:16 +0100961 if ((card_detect & MS_EXIST) && pcr->slots[RTSX_MS_CARD].card_event)
Wei WANGada8a8a2012-10-29 13:49:33 +0800962 pcr->slots[RTSX_MS_CARD].card_event(
963 pcr->slots[RTSX_MS_CARD].p_dev);
964}
965
966static irqreturn_t rtsx_pci_isr(int irq, void *dev_id)
967{
968 struct rtsx_pcr *pcr = dev_id;
969 u32 int_reg;
970
971 if (!pcr)
972 return IRQ_NONE;
973
974 spin_lock(&pcr->lock);
975
976 int_reg = rtsx_pci_readl(pcr, RTSX_BIPR);
977 /* Clear interrupt flag */
978 rtsx_pci_writel(pcr, RTSX_BIPR, int_reg);
979 if ((int_reg & pcr->bier) == 0) {
980 spin_unlock(&pcr->lock);
981 return IRQ_NONE;
982 }
983 if (int_reg == 0xFFFFFFFF) {
984 spin_unlock(&pcr->lock);
985 return IRQ_HANDLED;
986 }
987
988 int_reg &= (pcr->bier | 0x7FFFFF);
989
990 if (int_reg & SD_INT) {
991 if (int_reg & SD_EXIST) {
992 pcr->card_inserted |= SD_EXIST;
993 } else {
994 pcr->card_removed |= SD_EXIST;
995 pcr->card_inserted &= ~SD_EXIST;
996 }
Steven Feng87d28442017-05-23 15:13:24 +0800997 pcr->dma_error_count = 0;
Wei WANGada8a8a2012-10-29 13:49:33 +0800998 }
999
1000 if (int_reg & MS_INT) {
1001 if (int_reg & MS_EXIST) {
1002 pcr->card_inserted |= MS_EXIST;
1003 } else {
1004 pcr->card_removed |= MS_EXIST;
1005 pcr->card_inserted &= ~MS_EXIST;
1006 }
1007 }
1008
Wei WANGada8a8a2012-10-29 13:49:33 +08001009 if (int_reg & (NEED_COMPLETE_INT | DELINK_INT)) {
Micky Ching98fcc572014-04-29 09:54:54 +08001010 if (int_reg & (TRANS_FAIL_INT | DELINK_INT)) {
Wei WANGada8a8a2012-10-29 13:49:33 +08001011 pcr->trans_result = TRANS_RESULT_FAIL;
Micky Ching98fcc572014-04-29 09:54:54 +08001012 if (pcr->done)
1013 complete(pcr->done);
1014 } else if (int_reg & TRANS_OK_INT) {
Wei WANGada8a8a2012-10-29 13:49:33 +08001015 pcr->trans_result = TRANS_RESULT_OK;
Micky Ching98fcc572014-04-29 09:54:54 +08001016 if (pcr->done)
1017 complete(pcr->done);
Wei WANGada8a8a2012-10-29 13:49:33 +08001018 }
1019 }
1020
Wei WANG504decc2013-01-29 15:21:37 +08001021 if (pcr->card_inserted || pcr->card_removed)
1022 schedule_delayed_work(&pcr->carddet_work,
1023 msecs_to_jiffies(200));
1024
Wei WANGada8a8a2012-10-29 13:49:33 +08001025 spin_unlock(&pcr->lock);
1026 return IRQ_HANDLED;
1027}
1028
1029static int rtsx_pci_acquire_irq(struct rtsx_pcr *pcr)
1030{
Borislav Petkov118f6522017-02-02 21:59:35 +01001031 pcr_dbg(pcr, "%s: pcr->msi_en = %d, pci->irq = %d\n",
Wei WANGada8a8a2012-10-29 13:49:33 +08001032 __func__, pcr->msi_en, pcr->pci->irq);
1033
1034 if (request_irq(pcr->pci->irq, rtsx_pci_isr,
1035 pcr->msi_en ? 0 : IRQF_SHARED,
1036 DRV_NAME_RTSX_PCI, pcr)) {
1037 dev_err(&(pcr->pci->dev),
1038 "rtsx_sdmmc: unable to grab IRQ %d, disabling device\n",
1039 pcr->pci->irq);
1040 return -1;
1041 }
1042
1043 pcr->irq = pcr->pci->irq;
1044 pci_intx(pcr->pci, !pcr->msi_en);
1045
1046 return 0;
1047}
1048
Rui Feng8275b772017-09-07 16:26:39 +08001049static void rtsx_enable_aspm(struct rtsx_pcr *pcr)
1050{
1051 if (pcr->ops->set_aspm)
1052 pcr->ops->set_aspm(pcr, true);
1053 else
1054 rtsx_comm_set_aspm(pcr, true);
1055}
1056
1057static void rtsx_comm_pm_power_saving(struct rtsx_pcr *pcr)
1058{
1059 struct rtsx_cr_option *option = &pcr->option;
1060
1061 if (option->ltr_enabled) {
1062 u32 latency = option->ltr_l1off_latency;
1063
1064 if (rtsx_check_dev_flag(pcr, L1_SNOOZE_TEST_EN))
1065 mdelay(option->l1_snooze_delay);
1066
1067 rtsx_set_ltr_latency(pcr, latency);
1068 }
1069
1070 if (rtsx_check_dev_flag(pcr, LTR_L1SS_PWR_GATE_EN))
1071 rtsx_set_l1off_sub_cfg_d0(pcr, 0);
1072
1073 rtsx_enable_aspm(pcr);
1074}
1075
1076void rtsx_pm_power_saving(struct rtsx_pcr *pcr)
1077{
1078 if (pcr->ops->power_saving)
1079 pcr->ops->power_saving(pcr);
1080 else
1081 rtsx_comm_pm_power_saving(pcr);
1082}
1083
Wei WANGada8a8a2012-10-29 13:49:33 +08001084static void rtsx_pci_idle_work(struct work_struct *work)
1085{
1086 struct delayed_work *dwork = to_delayed_work(work);
1087 struct rtsx_pcr *pcr = container_of(dwork, struct rtsx_pcr, idle_work);
1088
Micky Ching0523b8f2015-02-25 13:50:16 +08001089 pcr_dbg(pcr, "--> %s\n", __func__);
Wei WANGada8a8a2012-10-29 13:49:33 +08001090
1091 mutex_lock(&pcr->pcr_mutex);
1092
1093 pcr->state = PDEV_STAT_IDLE;
1094
1095 if (pcr->ops->disable_auto_blink)
1096 pcr->ops->disable_auto_blink(pcr);
1097 if (pcr->ops->turn_off_led)
1098 pcr->ops->turn_off_led(pcr);
1099
Rui Feng8275b772017-09-07 16:26:39 +08001100 rtsx_pm_power_saving(pcr);
Wei WANG773ccdf2013-08-20 14:18:51 +08001101
Wei WANGada8a8a2012-10-29 13:49:33 +08001102 mutex_unlock(&pcr->pcr_mutex);
1103}
1104
Thierry Reding451be642014-10-02 09:25:17 +02001105#ifdef CONFIG_PM
Wei WANG5947c162013-08-20 14:18:52 +08001106static void rtsx_pci_power_off(struct rtsx_pcr *pcr, u8 pm_state)
1107{
1108 if (pcr->ops->turn_off_led)
1109 pcr->ops->turn_off_led(pcr);
1110
1111 rtsx_pci_writel(pcr, RTSX_BIER, 0);
1112 pcr->bier = 0;
1113
1114 rtsx_pci_write_register(pcr, PETXCFG, 0x08, 0x08);
1115 rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, pm_state);
1116
1117 if (pcr->ops->force_power_down)
Wei WANGeb891c62013-08-20 14:18:55 +08001118 pcr->ops->force_power_down(pcr, pm_state);
Wei WANG5947c162013-08-20 14:18:52 +08001119}
Thierry Reding451be642014-10-02 09:25:17 +02001120#endif
Wei WANG5947c162013-08-20 14:18:52 +08001121
Wei WANGada8a8a2012-10-29 13:49:33 +08001122static int rtsx_pci_init_hw(struct rtsx_pcr *pcr)
1123{
1124 int err;
1125
Micky Ching19f3bd52015-02-25 13:50:13 +08001126 pcr->pcie_cap = pci_find_capability(pcr->pci, PCI_CAP_ID_EXP);
Wei WANGada8a8a2012-10-29 13:49:33 +08001127 rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr);
1128
1129 rtsx_pci_enable_bus_int(pcr);
1130
1131 /* Power on SSC */
1132 err = rtsx_pci_write_register(pcr, FPDCTL, SSC_POWER_DOWN, 0);
1133 if (err < 0)
1134 return err;
1135
1136 /* Wait SSC power stable */
1137 udelay(200);
1138
Micky Ching19f3bd52015-02-25 13:50:13 +08001139 rtsx_pci_disable_aspm(pcr);
Wei WANGada8a8a2012-10-29 13:49:33 +08001140 if (pcr->ops->optimize_phy) {
1141 err = pcr->ops->optimize_phy(pcr);
1142 if (err < 0)
1143 return err;
1144 }
1145
1146 rtsx_pci_init_cmd(pcr);
1147
1148 /* Set mcu_cnt to 7 to ensure data can be sampled properly */
1149 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV, 0x07, 0x07);
1150
1151 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, HOST_SLEEP_STATE, 0x03, 0x00);
1152 /* Disable card clock */
1153 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN, 0x1E, 0);
Wei WANGada8a8a2012-10-29 13:49:33 +08001154 /* Reset delink mode */
1155 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x0A, 0);
1156 /* Card driving select */
Wei WANG773ccdf2013-08-20 14:18:51 +08001157 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_DRIVE_SEL,
1158 0xFF, pcr->card_drive_sel);
Wei WANGada8a8a2012-10-29 13:49:33 +08001159 /* Enable SSC Clock */
1160 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1,
1161 0xFF, SSC_8X_EN | SSC_SEL_4M);
1162 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, 0xFF, 0x12);
1163 /* Disable cd_pwr_save */
1164 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x16, 0x10);
1165 /* Clear Link Ready Interrupt */
1166 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0,
1167 LINK_RDY_INT, LINK_RDY_INT);
1168 /* Enlarge the estimation window of PERST# glitch
1169 * to reduce the chance of invalid card interrupt
1170 */
1171 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PERST_GLITCH_WIDTH, 0xFF, 0x80);
1172 /* Update RC oscillator to 400k
1173 * bit[0] F_HIGH: for RC oscillator, Rst_value is 1'b1
1174 * 1: 2M 0: 400k
1175 */
1176 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, RCCTL, 0x01, 0x00);
1177 /* Set interrupt write clear
1178 * bit 1: U_elbi_if_rd_clr_en
1179 * 1: Enable ELBI interrupt[31:22] & [7:0] flag read clear
1180 * 0: ELBI interrupt flag[31:22] & [7:0] only can be write clear
1181 */
1182 rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, NFTS_TX_CTRL, 0x02, 0);
Wei WANGada8a8a2012-10-29 13:49:33 +08001183
1184 err = rtsx_pci_send_cmd(pcr, 100);
1185 if (err < 0)
1186 return err;
1187
Rui Feng8275b772017-09-07 16:26:39 +08001188 switch (PCI_PID(pcr)) {
1189 case PID_5250:
1190 case PID_524A:
1191 case PID_525A:
1192 rtsx_pci_write_register(pcr, PM_CLK_FORCE_CTL, 1, 1);
1193 break;
1194 default:
1195 break;
1196 }
1197
Wei WANGada8a8a2012-10-29 13:49:33 +08001198 /* Enable clk_request_n to enable clock power management */
Micky Ching19f3bd52015-02-25 13:50:13 +08001199 rtsx_pci_write_config_byte(pcr, pcr->pcie_cap + PCI_EXP_LNKCTL + 1, 1);
Wei WANGada8a8a2012-10-29 13:49:33 +08001200 /* Enter L1 when host tx idle */
1201 rtsx_pci_write_config_byte(pcr, 0x70F, 0x5B);
1202
1203 if (pcr->ops->extra_init_hw) {
1204 err = pcr->ops->extra_init_hw(pcr);
1205 if (err < 0)
1206 return err;
1207 }
1208
Wei WANGc3481952013-02-08 15:24:27 +08001209 /* No CD interrupt if probing driver with card inserted.
1210 * So we need to initialize pcr->card_exist here.
1211 */
1212 if (pcr->ops->cd_deglitch)
1213 pcr->card_exist = pcr->ops->cd_deglitch(pcr);
1214 else
1215 pcr->card_exist = rtsx_pci_readl(pcr, RTSX_BIPR) & CARD_EXIST;
1216
Wei WANGada8a8a2012-10-29 13:49:33 +08001217 return 0;
1218}
1219
1220static int rtsx_pci_init_chip(struct rtsx_pcr *pcr)
1221{
1222 int err;
1223
1224 spin_lock_init(&pcr->lock);
1225 mutex_init(&pcr->pcr_mutex);
1226
1227 switch (PCI_PID(pcr)) {
1228 default:
1229 case 0x5209:
1230 rts5209_init_params(pcr);
1231 break;
1232
1233 case 0x5229:
1234 rts5229_init_params(pcr);
1235 break;
1236
1237 case 0x5289:
1238 rtl8411_init_params(pcr);
1239 break;
Roger Tsenge1237932013-02-04 15:45:59 +08001240
1241 case 0x5227:
1242 rts5227_init_params(pcr);
1243 break;
Wei WANG4c4b8c12013-04-11 10:43:40 +08001244
Micky Chingce6a5ac2015-06-29 09:19:39 +08001245 case 0x522A:
1246 rts522a_init_params(pcr);
1247 break;
1248
Wei WANG4c4b8c12013-04-11 10:43:40 +08001249 case 0x5249:
1250 rts5249_init_params(pcr);
1251 break;
Roger Tseng9032eab2013-04-19 21:52:42 +08001252
Micky Ching663c425f2015-02-25 13:50:14 +08001253 case 0x524A:
1254 rts524a_init_params(pcr);
1255 break;
1256
Micky Ching41bc2332015-02-25 13:50:15 +08001257 case 0x525A:
1258 rts525a_init_params(pcr);
1259 break;
1260
Roger Tseng9032eab2013-04-19 21:52:42 +08001261 case 0x5287:
1262 rtl8411b_init_params(pcr);
1263 break;
Micky Ching56cb3cc2013-12-18 10:03:13 +08001264
1265 case 0x5286:
1266 rtl8402_init_params(pcr);
1267 break;
Wei WANGada8a8a2012-10-29 13:49:33 +08001268 }
1269
Micky Ching0523b8f2015-02-25 13:50:16 +08001270 pcr_dbg(pcr, "PID: 0x%04x, IC version: 0x%02x\n",
Wei WANGada8a8a2012-10-29 13:49:33 +08001271 PCI_PID(pcr), pcr->ic_version);
1272
1273 pcr->slots = kcalloc(pcr->num_slots, sizeof(struct rtsx_slot),
1274 GFP_KERNEL);
1275 if (!pcr->slots)
1276 return -ENOMEM;
1277
Wei WANG773ccdf2013-08-20 14:18:51 +08001278 if (pcr->ops->fetch_vendor_settings)
1279 pcr->ops->fetch_vendor_settings(pcr);
1280
Micky Ching0523b8f2015-02-25 13:50:16 +08001281 pcr_dbg(pcr, "pcr->aspm_en = 0x%x\n", pcr->aspm_en);
1282 pcr_dbg(pcr, "pcr->sd30_drive_sel_1v8 = 0x%x\n",
Wei WANG773ccdf2013-08-20 14:18:51 +08001283 pcr->sd30_drive_sel_1v8);
Micky Ching0523b8f2015-02-25 13:50:16 +08001284 pcr_dbg(pcr, "pcr->sd30_drive_sel_3v3 = 0x%x\n",
Wei WANG773ccdf2013-08-20 14:18:51 +08001285 pcr->sd30_drive_sel_3v3);
Micky Ching0523b8f2015-02-25 13:50:16 +08001286 pcr_dbg(pcr, "pcr->card_drive_sel = 0x%x\n",
Wei WANG773ccdf2013-08-20 14:18:51 +08001287 pcr->card_drive_sel);
Micky Ching0523b8f2015-02-25 13:50:16 +08001288 pcr_dbg(pcr, "pcr->flags = 0x%x\n", pcr->flags);
Wei WANG773ccdf2013-08-20 14:18:51 +08001289
Wei WANGada8a8a2012-10-29 13:49:33 +08001290 pcr->state = PDEV_STAT_IDLE;
1291 err = rtsx_pci_init_hw(pcr);
1292 if (err < 0) {
1293 kfree(pcr->slots);
1294 return err;
1295 }
1296
1297 return 0;
1298}
1299
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -08001300static int rtsx_pci_probe(struct pci_dev *pcidev,
1301 const struct pci_device_id *id)
Wei WANGada8a8a2012-10-29 13:49:33 +08001302{
1303 struct rtsx_pcr *pcr;
1304 struct pcr_handle *handle;
1305 u32 base, len;
Micky Ching41bc2332015-02-25 13:50:15 +08001306 int ret, i, bar = 0;
Wei WANGada8a8a2012-10-29 13:49:33 +08001307
1308 dev_dbg(&(pcidev->dev),
1309 ": Realtek PCI-E Card Reader found at %s [%04x:%04x] (rev %x)\n",
1310 pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device,
1311 (int)pcidev->revision);
1312
Wei WANGf84ef042013-01-29 15:21:34 +08001313 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
1314 if (ret < 0)
1315 return ret;
1316
Wei WANGada8a8a2012-10-29 13:49:33 +08001317 ret = pci_enable_device(pcidev);
1318 if (ret)
1319 return ret;
1320
1321 ret = pci_request_regions(pcidev, DRV_NAME_RTSX_PCI);
1322 if (ret)
1323 goto disable;
1324
1325 pcr = kzalloc(sizeof(*pcr), GFP_KERNEL);
1326 if (!pcr) {
1327 ret = -ENOMEM;
1328 goto release_pci;
1329 }
1330
1331 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1332 if (!handle) {
1333 ret = -ENOMEM;
1334 goto free_pcr;
1335 }
1336 handle->pcr = pcr;
1337
Tejun Heo9f125632013-02-27 17:04:29 -08001338 idr_preload(GFP_KERNEL);
Wei WANGada8a8a2012-10-29 13:49:33 +08001339 spin_lock(&rtsx_pci_lock);
Tejun Heo9f125632013-02-27 17:04:29 -08001340 ret = idr_alloc(&rtsx_pci_idr, pcr, 0, 0, GFP_NOWAIT);
1341 if (ret >= 0)
1342 pcr->id = ret;
Wei WANGada8a8a2012-10-29 13:49:33 +08001343 spin_unlock(&rtsx_pci_lock);
Tejun Heo9f125632013-02-27 17:04:29 -08001344 idr_preload_end();
1345 if (ret < 0)
Wei WANGada8a8a2012-10-29 13:49:33 +08001346 goto free_handle;
1347
1348 pcr->pci = pcidev;
1349 dev_set_drvdata(&pcidev->dev, handle);
1350
Micky Ching41bc2332015-02-25 13:50:15 +08001351 if (CHK_PCI_PID(pcr, 0x525A))
1352 bar = 1;
1353 len = pci_resource_len(pcidev, bar);
1354 base = pci_resource_start(pcidev, bar);
Wei WANGada8a8a2012-10-29 13:49:33 +08001355 pcr->remap_addr = ioremap_nocache(base, len);
1356 if (!pcr->remap_addr) {
1357 ret = -ENOMEM;
Sachin Kamataf1192d2013-09-20 14:19:34 +05301358 goto free_handle;
Wei WANGada8a8a2012-10-29 13:49:33 +08001359 }
1360
1361 pcr->rtsx_resv_buf = dma_alloc_coherent(&(pcidev->dev),
1362 RTSX_RESV_BUF_LEN, &(pcr->rtsx_resv_buf_addr),
1363 GFP_KERNEL);
1364 if (pcr->rtsx_resv_buf == NULL) {
1365 ret = -ENXIO;
1366 goto unmap;
1367 }
1368 pcr->host_cmds_ptr = pcr->rtsx_resv_buf;
1369 pcr->host_cmds_addr = pcr->rtsx_resv_buf_addr;
1370 pcr->host_sg_tbl_ptr = pcr->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
1371 pcr->host_sg_tbl_addr = pcr->rtsx_resv_buf_addr + HOST_CMDS_BUF_LEN;
1372
1373 pcr->card_inserted = 0;
1374 pcr->card_removed = 0;
1375 INIT_DELAYED_WORK(&pcr->carddet_work, rtsx_pci_card_detect);
1376 INIT_DELAYED_WORK(&pcr->idle_work, rtsx_pci_idle_work);
1377
1378 pcr->msi_en = msi_en;
1379 if (pcr->msi_en) {
1380 ret = pci_enable_msi(pcidev);
Chris Ball515297052014-09-04 17:11:53 +01001381 if (ret)
Wei WANGada8a8a2012-10-29 13:49:33 +08001382 pcr->msi_en = false;
1383 }
1384
1385 ret = rtsx_pci_acquire_irq(pcr);
1386 if (ret < 0)
Jiri Slaby9d66b562013-04-04 21:34:11 +02001387 goto disable_msi;
Wei WANGada8a8a2012-10-29 13:49:33 +08001388
1389 pci_set_master(pcidev);
1390 synchronize_irq(pcr->irq);
1391
1392 ret = rtsx_pci_init_chip(pcr);
1393 if (ret < 0)
1394 goto disable_irq;
1395
1396 for (i = 0; i < ARRAY_SIZE(rtsx_pcr_cells); i++) {
1397 rtsx_pcr_cells[i].platform_data = handle;
1398 rtsx_pcr_cells[i].pdata_size = sizeof(*handle);
1399 }
1400 ret = mfd_add_devices(&pcidev->dev, pcr->id, rtsx_pcr_cells,
1401 ARRAY_SIZE(rtsx_pcr_cells), NULL, 0, NULL);
1402 if (ret < 0)
1403 goto disable_irq;
1404
1405 schedule_delayed_work(&pcr->idle_work, msecs_to_jiffies(200));
1406
1407 return 0;
1408
1409disable_irq:
1410 free_irq(pcr->irq, (void *)pcr);
Jiri Slaby9d66b562013-04-04 21:34:11 +02001411disable_msi:
1412 if (pcr->msi_en)
1413 pci_disable_msi(pcr->pci);
Wei WANGada8a8a2012-10-29 13:49:33 +08001414 dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN,
1415 pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr);
1416unmap:
1417 iounmap(pcr->remap_addr);
Wei WANGada8a8a2012-10-29 13:49:33 +08001418free_handle:
1419 kfree(handle);
1420free_pcr:
1421 kfree(pcr);
1422release_pci:
1423 pci_release_regions(pcidev);
1424disable:
1425 pci_disable_device(pcidev);
1426
1427 return ret;
1428}
1429
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -08001430static void rtsx_pci_remove(struct pci_dev *pcidev)
Wei WANGada8a8a2012-10-29 13:49:33 +08001431{
1432 struct pcr_handle *handle = pci_get_drvdata(pcidev);
1433 struct rtsx_pcr *pcr = handle->pcr;
1434
1435 pcr->remove_pci = true;
1436
Thomas Gleixner73beb632013-12-02 12:20:36 +01001437 /* Disable interrupts at the pcr level */
1438 spin_lock_irq(&pcr->lock);
1439 rtsx_pci_writel(pcr, RTSX_BIER, 0);
1440 pcr->bier = 0;
1441 spin_unlock_irq(&pcr->lock);
1442
1443 cancel_delayed_work_sync(&pcr->carddet_work);
1444 cancel_delayed_work_sync(&pcr->idle_work);
Wei WANGada8a8a2012-10-29 13:49:33 +08001445
1446 mfd_remove_devices(&pcidev->dev);
1447
1448 dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN,
1449 pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr);
1450 free_irq(pcr->irq, (void *)pcr);
1451 if (pcr->msi_en)
1452 pci_disable_msi(pcr->pci);
1453 iounmap(pcr->remap_addr);
1454
Wei WANGada8a8a2012-10-29 13:49:33 +08001455 pci_release_regions(pcidev);
1456 pci_disable_device(pcidev);
1457
1458 spin_lock(&rtsx_pci_lock);
1459 idr_remove(&rtsx_pci_idr, pcr->id);
1460 spin_unlock(&rtsx_pci_lock);
1461
1462 kfree(pcr->slots);
1463 kfree(pcr);
1464 kfree(handle);
1465
1466 dev_dbg(&(pcidev->dev),
1467 ": Realtek PCI-E Card Reader at %s [%04x:%04x] has been removed\n",
1468 pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device);
1469}
1470
1471#ifdef CONFIG_PM
1472
1473static int rtsx_pci_suspend(struct pci_dev *pcidev, pm_message_t state)
1474{
1475 struct pcr_handle *handle;
1476 struct rtsx_pcr *pcr;
Wei WANGada8a8a2012-10-29 13:49:33 +08001477
1478 dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
1479
1480 handle = pci_get_drvdata(pcidev);
1481 pcr = handle->pcr;
1482
1483 cancel_delayed_work(&pcr->carddet_work);
1484 cancel_delayed_work(&pcr->idle_work);
1485
1486 mutex_lock(&pcr->pcr_mutex);
1487
Wei WANG5947c162013-08-20 14:18:52 +08001488 rtsx_pci_power_off(pcr, HOST_ENTER_S3);
Wei WANGada8a8a2012-10-29 13:49:33 +08001489
1490 pci_save_state(pcidev);
1491 pci_enable_wake(pcidev, pci_choose_state(pcidev, state), 0);
1492 pci_disable_device(pcidev);
1493 pci_set_power_state(pcidev, pci_choose_state(pcidev, state));
1494
1495 mutex_unlock(&pcr->pcr_mutex);
Wei WANG5947c162013-08-20 14:18:52 +08001496 return 0;
Wei WANGada8a8a2012-10-29 13:49:33 +08001497}
1498
1499static int rtsx_pci_resume(struct pci_dev *pcidev)
1500{
1501 struct pcr_handle *handle;
1502 struct rtsx_pcr *pcr;
1503 int ret = 0;
1504
1505 dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
1506
1507 handle = pci_get_drvdata(pcidev);
1508 pcr = handle->pcr;
1509
1510 mutex_lock(&pcr->pcr_mutex);
1511
1512 pci_set_power_state(pcidev, PCI_D0);
1513 pci_restore_state(pcidev);
1514 ret = pci_enable_device(pcidev);
1515 if (ret)
1516 goto out;
1517 pci_set_master(pcidev);
1518
1519 ret = rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, 0x00);
1520 if (ret)
1521 goto out;
1522
1523 ret = rtsx_pci_init_hw(pcr);
1524 if (ret)
1525 goto out;
1526
1527 schedule_delayed_work(&pcr->idle_work, msecs_to_jiffies(200));
1528
1529out:
1530 mutex_unlock(&pcr->pcr_mutex);
1531 return ret;
1532}
1533
Wei WANG5947c162013-08-20 14:18:52 +08001534static void rtsx_pci_shutdown(struct pci_dev *pcidev)
1535{
1536 struct pcr_handle *handle;
1537 struct rtsx_pcr *pcr;
1538
1539 dev_dbg(&(pcidev->dev), "--> %s\n", __func__);
1540
1541 handle = pci_get_drvdata(pcidev);
1542 pcr = handle->pcr;
1543 rtsx_pci_power_off(pcr, HOST_ENTER_S1);
1544
1545 pci_disable_device(pcidev);
1546}
1547
Wei WANGada8a8a2012-10-29 13:49:33 +08001548#else /* CONFIG_PM */
1549
1550#define rtsx_pci_suspend NULL
1551#define rtsx_pci_resume NULL
Wei WANG5947c162013-08-20 14:18:52 +08001552#define rtsx_pci_shutdown NULL
Wei WANGada8a8a2012-10-29 13:49:33 +08001553
1554#endif /* CONFIG_PM */
1555
1556static struct pci_driver rtsx_pci_driver = {
1557 .name = DRV_NAME_RTSX_PCI,
1558 .id_table = rtsx_pci_ids,
1559 .probe = rtsx_pci_probe,
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -08001560 .remove = rtsx_pci_remove,
Wei WANGada8a8a2012-10-29 13:49:33 +08001561 .suspend = rtsx_pci_suspend,
1562 .resume = rtsx_pci_resume,
Wei WANG5947c162013-08-20 14:18:52 +08001563 .shutdown = rtsx_pci_shutdown,
Wei WANGada8a8a2012-10-29 13:49:33 +08001564};
1565module_pci_driver(rtsx_pci_driver);
1566
1567MODULE_LICENSE("GPL");
1568MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>");
1569MODULE_DESCRIPTION("Realtek PCI-E Card Reader Driver");