Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 1 | /* Driver for Realtek PCI-Express card reader |
| 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/pci.h> |
| 24 | #include <linux/module.h> |
Samuel Ortiz | aec17ea | 2012-11-09 10:19:54 +0100 | [diff] [blame] | 25 | #include <linux/slab.h> |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 26 | #include <linux/dma-mapping.h> |
| 27 | #include <linux/highmem.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/idr.h> |
| 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/mfd/core.h> |
| 33 | #include <linux/mfd/rtsx_pci.h> |
| 34 | #include <asm/unaligned.h> |
| 35 | |
| 36 | #include "rtsx_pcr.h" |
| 37 | |
| 38 | static bool msi_en = true; |
| 39 | module_param(msi_en, bool, S_IRUGO | S_IWUSR); |
| 40 | MODULE_PARM_DESC(msi_en, "Enable MSI"); |
| 41 | |
| 42 | static DEFINE_IDR(rtsx_pci_idr); |
| 43 | static DEFINE_SPINLOCK(rtsx_pci_lock); |
| 44 | |
| 45 | static 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 | |
| 54 | static DEFINE_PCI_DEVICE_TABLE(rtsx_pci_ids) = { |
| 55 | { 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 Tseng | e123793 | 2013-02-04 15:45:59 +0800 | [diff] [blame^] | 58 | { PCI_DEVICE(0x10EC, 0x5227), PCI_CLASS_OTHERS << 16, 0xFF0000 }, |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 59 | { 0, } |
| 60 | }; |
| 61 | |
| 62 | MODULE_DEVICE_TABLE(pci, rtsx_pci_ids); |
| 63 | |
| 64 | void rtsx_pci_start_run(struct rtsx_pcr *pcr) |
| 65 | { |
| 66 | /* If pci device removed, don't queue idle work any more */ |
| 67 | if (pcr->remove_pci) |
| 68 | return; |
| 69 | |
| 70 | if (pcr->state != PDEV_STAT_RUN) { |
| 71 | pcr->state = PDEV_STAT_RUN; |
| 72 | if (pcr->ops->enable_auto_blink) |
| 73 | pcr->ops->enable_auto_blink(pcr); |
| 74 | } |
| 75 | |
| 76 | mod_delayed_work(system_wq, &pcr->idle_work, msecs_to_jiffies(200)); |
| 77 | } |
| 78 | EXPORT_SYMBOL_GPL(rtsx_pci_start_run); |
| 79 | |
| 80 | int rtsx_pci_write_register(struct rtsx_pcr *pcr, u16 addr, u8 mask, u8 data) |
| 81 | { |
| 82 | int i; |
| 83 | u32 val = HAIMR_WRITE_START; |
| 84 | |
| 85 | val |= (u32)(addr & 0x3FFF) << 16; |
| 86 | val |= (u32)mask << 8; |
| 87 | val |= (u32)data; |
| 88 | |
| 89 | rtsx_pci_writel(pcr, RTSX_HAIMR, val); |
| 90 | |
| 91 | for (i = 0; i < MAX_RW_REG_CNT; i++) { |
| 92 | val = rtsx_pci_readl(pcr, RTSX_HAIMR); |
| 93 | if ((val & HAIMR_TRANS_END) == 0) { |
| 94 | if (data != (u8)val) |
| 95 | return -EIO; |
| 96 | return 0; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return -ETIMEDOUT; |
| 101 | } |
| 102 | EXPORT_SYMBOL_GPL(rtsx_pci_write_register); |
| 103 | |
| 104 | int rtsx_pci_read_register(struct rtsx_pcr *pcr, u16 addr, u8 *data) |
| 105 | { |
| 106 | u32 val = HAIMR_READ_START; |
| 107 | int i; |
| 108 | |
| 109 | val |= (u32)(addr & 0x3FFF) << 16; |
| 110 | rtsx_pci_writel(pcr, RTSX_HAIMR, val); |
| 111 | |
| 112 | for (i = 0; i < MAX_RW_REG_CNT; i++) { |
| 113 | val = rtsx_pci_readl(pcr, RTSX_HAIMR); |
| 114 | if ((val & HAIMR_TRANS_END) == 0) |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | if (i >= MAX_RW_REG_CNT) |
| 119 | return -ETIMEDOUT; |
| 120 | |
| 121 | if (data) |
| 122 | *data = (u8)(val & 0xFF); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(rtsx_pci_read_register); |
| 127 | |
| 128 | int rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val) |
| 129 | { |
| 130 | int err, i, finished = 0; |
| 131 | u8 tmp; |
| 132 | |
| 133 | rtsx_pci_init_cmd(pcr); |
| 134 | |
| 135 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYDATA0, 0xFF, (u8)val); |
| 136 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYDATA1, 0xFF, (u8)(val >> 8)); |
| 137 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYADDR, 0xFF, addr); |
| 138 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYRWCTL, 0xFF, 0x81); |
| 139 | |
| 140 | err = rtsx_pci_send_cmd(pcr, 100); |
| 141 | if (err < 0) |
| 142 | return err; |
| 143 | |
| 144 | for (i = 0; i < 100000; i++) { |
| 145 | err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp); |
| 146 | if (err < 0) |
| 147 | return err; |
| 148 | |
| 149 | if (!(tmp & 0x80)) { |
| 150 | finished = 1; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (!finished) |
| 156 | return -ETIMEDOUT; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(rtsx_pci_write_phy_register); |
| 161 | |
| 162 | int rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val) |
| 163 | { |
| 164 | int err, i, finished = 0; |
| 165 | u16 data; |
| 166 | u8 *ptr, tmp; |
| 167 | |
| 168 | rtsx_pci_init_cmd(pcr); |
| 169 | |
| 170 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYADDR, 0xFF, addr); |
| 171 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PHYRWCTL, 0xFF, 0x80); |
| 172 | |
| 173 | err = rtsx_pci_send_cmd(pcr, 100); |
| 174 | if (err < 0) |
| 175 | return err; |
| 176 | |
| 177 | for (i = 0; i < 100000; i++) { |
| 178 | err = rtsx_pci_read_register(pcr, PHYRWCTL, &tmp); |
| 179 | if (err < 0) |
| 180 | return err; |
| 181 | |
| 182 | if (!(tmp & 0x80)) { |
| 183 | finished = 1; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (!finished) |
| 189 | return -ETIMEDOUT; |
| 190 | |
| 191 | rtsx_pci_init_cmd(pcr); |
| 192 | |
| 193 | rtsx_pci_add_cmd(pcr, READ_REG_CMD, PHYDATA0, 0, 0); |
| 194 | rtsx_pci_add_cmd(pcr, READ_REG_CMD, PHYDATA1, 0, 0); |
| 195 | |
| 196 | err = rtsx_pci_send_cmd(pcr, 100); |
| 197 | if (err < 0) |
| 198 | return err; |
| 199 | |
| 200 | ptr = rtsx_pci_get_cmd_data(pcr); |
| 201 | data = ((u16)ptr[1] << 8) | ptr[0]; |
| 202 | |
| 203 | if (val) |
| 204 | *val = data; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | EXPORT_SYMBOL_GPL(rtsx_pci_read_phy_register); |
| 209 | |
| 210 | void rtsx_pci_stop_cmd(struct rtsx_pcr *pcr) |
| 211 | { |
| 212 | rtsx_pci_writel(pcr, RTSX_HCBCTLR, STOP_CMD); |
| 213 | rtsx_pci_writel(pcr, RTSX_HDBCTLR, STOP_DMA); |
| 214 | |
| 215 | rtsx_pci_write_register(pcr, DMACTL, 0x80, 0x80); |
| 216 | rtsx_pci_write_register(pcr, RBCTL, 0x80, 0x80); |
| 217 | } |
| 218 | EXPORT_SYMBOL_GPL(rtsx_pci_stop_cmd); |
| 219 | |
| 220 | void rtsx_pci_add_cmd(struct rtsx_pcr *pcr, |
| 221 | u8 cmd_type, u16 reg_addr, u8 mask, u8 data) |
| 222 | { |
| 223 | unsigned long flags; |
| 224 | u32 val = 0; |
| 225 | u32 *ptr = (u32 *)(pcr->host_cmds_ptr); |
| 226 | |
| 227 | val |= (u32)(cmd_type & 0x03) << 30; |
| 228 | val |= (u32)(reg_addr & 0x3FFF) << 16; |
| 229 | val |= (u32)mask << 8; |
| 230 | val |= (u32)data; |
| 231 | |
| 232 | spin_lock_irqsave(&pcr->lock, flags); |
| 233 | ptr += pcr->ci; |
| 234 | if (pcr->ci < (HOST_CMDS_BUF_LEN / 4)) { |
| 235 | put_unaligned_le32(val, ptr); |
| 236 | ptr++; |
| 237 | pcr->ci++; |
| 238 | } |
| 239 | spin_unlock_irqrestore(&pcr->lock, flags); |
| 240 | } |
| 241 | EXPORT_SYMBOL_GPL(rtsx_pci_add_cmd); |
| 242 | |
| 243 | void rtsx_pci_send_cmd_no_wait(struct rtsx_pcr *pcr) |
| 244 | { |
| 245 | u32 val = 1 << 31; |
| 246 | |
| 247 | rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr); |
| 248 | |
| 249 | val |= (u32)(pcr->ci * 4) & 0x00FFFFFF; |
| 250 | /* Hardware Auto Response */ |
| 251 | val |= 0x40000000; |
| 252 | rtsx_pci_writel(pcr, RTSX_HCBCTLR, val); |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd_no_wait); |
| 255 | |
| 256 | int rtsx_pci_send_cmd(struct rtsx_pcr *pcr, int timeout) |
| 257 | { |
| 258 | struct completion trans_done; |
| 259 | u32 val = 1 << 31; |
| 260 | long timeleft; |
| 261 | unsigned long flags; |
| 262 | int err = 0; |
| 263 | |
| 264 | spin_lock_irqsave(&pcr->lock, flags); |
| 265 | |
| 266 | /* set up data structures for the wakeup system */ |
| 267 | pcr->done = &trans_done; |
| 268 | pcr->trans_result = TRANS_NOT_READY; |
| 269 | init_completion(&trans_done); |
| 270 | |
| 271 | rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr); |
| 272 | |
| 273 | val |= (u32)(pcr->ci * 4) & 0x00FFFFFF; |
| 274 | /* Hardware Auto Response */ |
| 275 | val |= 0x40000000; |
| 276 | rtsx_pci_writel(pcr, RTSX_HCBCTLR, val); |
| 277 | |
| 278 | spin_unlock_irqrestore(&pcr->lock, flags); |
| 279 | |
| 280 | /* Wait for TRANS_OK_INT */ |
| 281 | timeleft = wait_for_completion_interruptible_timeout( |
| 282 | &trans_done, msecs_to_jiffies(timeout)); |
| 283 | if (timeleft <= 0) { |
| 284 | dev_dbg(&(pcr->pci->dev), "Timeout (%s %d)\n", |
| 285 | __func__, __LINE__); |
| 286 | err = -ETIMEDOUT; |
| 287 | goto finish_send_cmd; |
| 288 | } |
| 289 | |
| 290 | spin_lock_irqsave(&pcr->lock, flags); |
| 291 | if (pcr->trans_result == TRANS_RESULT_FAIL) |
| 292 | err = -EINVAL; |
| 293 | else if (pcr->trans_result == TRANS_RESULT_OK) |
| 294 | err = 0; |
| 295 | else if (pcr->trans_result == TRANS_NO_DEVICE) |
| 296 | err = -ENODEV; |
| 297 | spin_unlock_irqrestore(&pcr->lock, flags); |
| 298 | |
| 299 | finish_send_cmd: |
| 300 | spin_lock_irqsave(&pcr->lock, flags); |
| 301 | pcr->done = NULL; |
| 302 | spin_unlock_irqrestore(&pcr->lock, flags); |
| 303 | |
| 304 | if ((err < 0) && (err != -ENODEV)) |
| 305 | rtsx_pci_stop_cmd(pcr); |
| 306 | |
| 307 | if (pcr->finish_me) |
| 308 | complete(pcr->finish_me); |
| 309 | |
| 310 | return err; |
| 311 | } |
| 312 | EXPORT_SYMBOL_GPL(rtsx_pci_send_cmd); |
| 313 | |
| 314 | static void rtsx_pci_add_sg_tbl(struct rtsx_pcr *pcr, |
| 315 | dma_addr_t addr, unsigned int len, int end) |
| 316 | { |
| 317 | u64 *ptr = (u64 *)(pcr->host_sg_tbl_ptr) + pcr->sgi; |
| 318 | u64 val; |
| 319 | u8 option = SG_VALID | SG_TRANS_DATA; |
| 320 | |
| 321 | dev_dbg(&(pcr->pci->dev), "DMA addr: 0x%x, Len: 0x%x\n", |
| 322 | (unsigned int)addr, len); |
| 323 | |
| 324 | if (end) |
| 325 | option |= SG_END; |
| 326 | val = ((u64)addr << 32) | ((u64)len << 12) | option; |
| 327 | |
| 328 | put_unaligned_le64(val, ptr); |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 329 | pcr->sgi++; |
| 330 | } |
| 331 | |
| 332 | int rtsx_pci_transfer_data(struct rtsx_pcr *pcr, struct scatterlist *sglist, |
| 333 | int num_sg, bool read, int timeout) |
| 334 | { |
| 335 | struct completion trans_done; |
| 336 | u8 dir; |
| 337 | int err = 0, i, count; |
| 338 | long timeleft; |
| 339 | unsigned long flags; |
| 340 | struct scatterlist *sg; |
| 341 | enum dma_data_direction dma_dir; |
| 342 | u32 val; |
| 343 | dma_addr_t addr; |
| 344 | unsigned int len; |
| 345 | |
| 346 | dev_dbg(&(pcr->pci->dev), "--> %s: num_sg = %d\n", __func__, num_sg); |
| 347 | |
| 348 | /* don't transfer data during abort processing */ |
| 349 | if (pcr->remove_pci) |
| 350 | return -EINVAL; |
| 351 | |
| 352 | if ((sglist == NULL) || (num_sg <= 0)) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | if (read) { |
| 356 | dir = DEVICE_TO_HOST; |
| 357 | dma_dir = DMA_FROM_DEVICE; |
| 358 | } else { |
| 359 | dir = HOST_TO_DEVICE; |
| 360 | dma_dir = DMA_TO_DEVICE; |
| 361 | } |
| 362 | |
| 363 | count = dma_map_sg(&(pcr->pci->dev), sglist, num_sg, dma_dir); |
| 364 | if (count < 1) { |
| 365 | dev_err(&(pcr->pci->dev), "scatterlist map failed\n"); |
| 366 | return -EINVAL; |
| 367 | } |
| 368 | dev_dbg(&(pcr->pci->dev), "DMA mapping count: %d\n", count); |
| 369 | |
| 370 | val = ((u32)(dir & 0x01) << 29) | TRIG_DMA | ADMA_MODE; |
| 371 | pcr->sgi = 0; |
| 372 | for_each_sg(sglist, sg, count, i) { |
| 373 | addr = sg_dma_address(sg); |
| 374 | len = sg_dma_len(sg); |
| 375 | rtsx_pci_add_sg_tbl(pcr, addr, len, i == count - 1); |
| 376 | } |
| 377 | |
| 378 | spin_lock_irqsave(&pcr->lock, flags); |
| 379 | |
| 380 | pcr->done = &trans_done; |
| 381 | pcr->trans_result = TRANS_NOT_READY; |
| 382 | init_completion(&trans_done); |
| 383 | rtsx_pci_writel(pcr, RTSX_HDBAR, pcr->host_sg_tbl_addr); |
| 384 | rtsx_pci_writel(pcr, RTSX_HDBCTLR, val); |
| 385 | |
| 386 | spin_unlock_irqrestore(&pcr->lock, flags); |
| 387 | |
| 388 | timeleft = wait_for_completion_interruptible_timeout( |
| 389 | &trans_done, msecs_to_jiffies(timeout)); |
| 390 | if (timeleft <= 0) { |
| 391 | dev_dbg(&(pcr->pci->dev), "Timeout (%s %d)\n", |
| 392 | __func__, __LINE__); |
| 393 | err = -ETIMEDOUT; |
| 394 | goto out; |
| 395 | } |
| 396 | |
| 397 | spin_lock_irqsave(&pcr->lock, flags); |
| 398 | |
| 399 | if (pcr->trans_result == TRANS_RESULT_FAIL) |
| 400 | err = -EINVAL; |
| 401 | else if (pcr->trans_result == TRANS_NO_DEVICE) |
| 402 | err = -ENODEV; |
| 403 | |
| 404 | spin_unlock_irqrestore(&pcr->lock, flags); |
| 405 | |
| 406 | out: |
| 407 | spin_lock_irqsave(&pcr->lock, flags); |
| 408 | pcr->done = NULL; |
| 409 | spin_unlock_irqrestore(&pcr->lock, flags); |
| 410 | |
| 411 | dma_unmap_sg(&(pcr->pci->dev), sglist, num_sg, dma_dir); |
| 412 | |
| 413 | if ((err < 0) && (err != -ENODEV)) |
| 414 | rtsx_pci_stop_cmd(pcr); |
| 415 | |
| 416 | if (pcr->finish_me) |
| 417 | complete(pcr->finish_me); |
| 418 | |
| 419 | return err; |
| 420 | } |
| 421 | EXPORT_SYMBOL_GPL(rtsx_pci_transfer_data); |
| 422 | |
| 423 | int rtsx_pci_read_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len) |
| 424 | { |
| 425 | int err; |
| 426 | int i, j; |
| 427 | u16 reg; |
| 428 | u8 *ptr; |
| 429 | |
| 430 | if (buf_len > 512) |
| 431 | buf_len = 512; |
| 432 | |
| 433 | ptr = buf; |
| 434 | reg = PPBUF_BASE2; |
| 435 | for (i = 0; i < buf_len / 256; i++) { |
| 436 | rtsx_pci_init_cmd(pcr); |
| 437 | |
| 438 | for (j = 0; j < 256; j++) |
| 439 | rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0); |
| 440 | |
| 441 | err = rtsx_pci_send_cmd(pcr, 250); |
| 442 | if (err < 0) |
| 443 | return err; |
| 444 | |
| 445 | memcpy(ptr, rtsx_pci_get_cmd_data(pcr), 256); |
| 446 | ptr += 256; |
| 447 | } |
| 448 | |
| 449 | if (buf_len % 256) { |
| 450 | rtsx_pci_init_cmd(pcr); |
| 451 | |
| 452 | for (j = 0; j < buf_len % 256; j++) |
| 453 | rtsx_pci_add_cmd(pcr, READ_REG_CMD, reg++, 0, 0); |
| 454 | |
| 455 | err = rtsx_pci_send_cmd(pcr, 250); |
| 456 | if (err < 0) |
| 457 | return err; |
| 458 | } |
| 459 | |
| 460 | memcpy(ptr, rtsx_pci_get_cmd_data(pcr), buf_len % 256); |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | EXPORT_SYMBOL_GPL(rtsx_pci_read_ppbuf); |
| 465 | |
| 466 | int rtsx_pci_write_ppbuf(struct rtsx_pcr *pcr, u8 *buf, int buf_len) |
| 467 | { |
| 468 | int err; |
| 469 | int i, j; |
| 470 | u16 reg; |
| 471 | u8 *ptr; |
| 472 | |
| 473 | if (buf_len > 512) |
| 474 | buf_len = 512; |
| 475 | |
| 476 | ptr = buf; |
| 477 | reg = PPBUF_BASE2; |
| 478 | for (i = 0; i < buf_len / 256; i++) { |
| 479 | rtsx_pci_init_cmd(pcr); |
| 480 | |
| 481 | for (j = 0; j < 256; j++) { |
| 482 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, |
| 483 | reg++, 0xFF, *ptr); |
| 484 | ptr++; |
| 485 | } |
| 486 | |
| 487 | err = rtsx_pci_send_cmd(pcr, 250); |
| 488 | if (err < 0) |
| 489 | return err; |
| 490 | } |
| 491 | |
| 492 | if (buf_len % 256) { |
| 493 | rtsx_pci_init_cmd(pcr); |
| 494 | |
| 495 | for (j = 0; j < buf_len % 256; j++) { |
| 496 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, |
| 497 | reg++, 0xFF, *ptr); |
| 498 | ptr++; |
| 499 | } |
| 500 | |
| 501 | err = rtsx_pci_send_cmd(pcr, 250); |
| 502 | if (err < 0) |
| 503 | return err; |
| 504 | } |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | EXPORT_SYMBOL_GPL(rtsx_pci_write_ppbuf); |
| 509 | |
| 510 | static int rtsx_pci_set_pull_ctl(struct rtsx_pcr *pcr, const u32 *tbl) |
| 511 | { |
| 512 | int err; |
| 513 | |
| 514 | rtsx_pci_init_cmd(pcr); |
| 515 | |
| 516 | while (*tbl & 0xFFFF0000) { |
| 517 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, |
| 518 | (u16)(*tbl >> 16), 0xFF, (u8)(*tbl)); |
| 519 | tbl++; |
| 520 | } |
| 521 | |
| 522 | err = rtsx_pci_send_cmd(pcr, 100); |
| 523 | if (err < 0) |
| 524 | return err; |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | int rtsx_pci_card_pull_ctl_enable(struct rtsx_pcr *pcr, int card) |
| 530 | { |
| 531 | const u32 *tbl; |
| 532 | |
| 533 | if (card == RTSX_SD_CARD) |
| 534 | tbl = pcr->sd_pull_ctl_enable_tbl; |
| 535 | else if (card == RTSX_MS_CARD) |
| 536 | tbl = pcr->ms_pull_ctl_enable_tbl; |
| 537 | else |
| 538 | return -EINVAL; |
| 539 | |
| 540 | return rtsx_pci_set_pull_ctl(pcr, tbl); |
| 541 | } |
| 542 | EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_enable); |
| 543 | |
| 544 | int rtsx_pci_card_pull_ctl_disable(struct rtsx_pcr *pcr, int card) |
| 545 | { |
| 546 | const u32 *tbl; |
| 547 | |
| 548 | if (card == RTSX_SD_CARD) |
| 549 | tbl = pcr->sd_pull_ctl_disable_tbl; |
| 550 | else if (card == RTSX_MS_CARD) |
| 551 | tbl = pcr->ms_pull_ctl_disable_tbl; |
| 552 | else |
| 553 | return -EINVAL; |
| 554 | |
| 555 | |
| 556 | return rtsx_pci_set_pull_ctl(pcr, tbl); |
| 557 | } |
| 558 | EXPORT_SYMBOL_GPL(rtsx_pci_card_pull_ctl_disable); |
| 559 | |
| 560 | static void rtsx_pci_enable_bus_int(struct rtsx_pcr *pcr) |
| 561 | { |
| 562 | pcr->bier = TRANS_OK_INT_EN | TRANS_FAIL_INT_EN | SD_INT_EN; |
| 563 | |
| 564 | if (pcr->num_slots > 1) |
| 565 | pcr->bier |= MS_INT_EN; |
| 566 | |
| 567 | /* Enable Bus Interrupt */ |
| 568 | rtsx_pci_writel(pcr, RTSX_BIER, pcr->bier); |
| 569 | |
| 570 | dev_dbg(&(pcr->pci->dev), "RTSX_BIER: 0x%08x\n", pcr->bier); |
| 571 | } |
| 572 | |
| 573 | static inline u8 double_ssc_depth(u8 depth) |
| 574 | { |
| 575 | return ((depth > 1) ? (depth - 1) : depth); |
| 576 | } |
| 577 | |
| 578 | static u8 revise_ssc_depth(u8 ssc_depth, u8 div) |
| 579 | { |
| 580 | if (div > CLK_DIV_1) { |
| 581 | if (ssc_depth > (div - 1)) |
| 582 | ssc_depth -= (div - 1); |
| 583 | else |
| 584 | ssc_depth = SSC_DEPTH_4M; |
| 585 | } |
| 586 | |
| 587 | return ssc_depth; |
| 588 | } |
| 589 | |
| 590 | int rtsx_pci_switch_clock(struct rtsx_pcr *pcr, unsigned int card_clock, |
| 591 | u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk) |
| 592 | { |
| 593 | int err, clk; |
Wei WANG | eebbe25 | 2013-01-29 15:21:36 +0800 | [diff] [blame] | 594 | u8 n, clk_divider, mcu_cnt, div; |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 595 | u8 depth[] = { |
| 596 | [RTSX_SSC_DEPTH_4M] = SSC_DEPTH_4M, |
| 597 | [RTSX_SSC_DEPTH_2M] = SSC_DEPTH_2M, |
| 598 | [RTSX_SSC_DEPTH_1M] = SSC_DEPTH_1M, |
| 599 | [RTSX_SSC_DEPTH_500K] = SSC_DEPTH_500K, |
| 600 | [RTSX_SSC_DEPTH_250K] = SSC_DEPTH_250K, |
| 601 | }; |
| 602 | |
| 603 | if (initial_mode) { |
| 604 | /* We use 250k(around) here, in initial stage */ |
| 605 | clk_divider = SD_CLK_DIVIDE_128; |
| 606 | card_clock = 30000000; |
| 607 | } else { |
| 608 | clk_divider = SD_CLK_DIVIDE_0; |
| 609 | } |
| 610 | err = rtsx_pci_write_register(pcr, SD_CFG1, |
| 611 | SD_CLK_DIVIDE_MASK, clk_divider); |
| 612 | if (err < 0) |
| 613 | return err; |
| 614 | |
| 615 | card_clock /= 1000000; |
| 616 | dev_dbg(&(pcr->pci->dev), "Switch card clock to %dMHz\n", card_clock); |
| 617 | |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 618 | clk = card_clock; |
| 619 | if (!initial_mode && double_clk) |
| 620 | clk = card_clock * 2; |
| 621 | dev_dbg(&(pcr->pci->dev), |
| 622 | "Internal SSC clock: %dMHz (cur_clock = %d)\n", |
| 623 | clk, pcr->cur_clock); |
| 624 | |
| 625 | if (clk == pcr->cur_clock) |
| 626 | return 0; |
| 627 | |
Wei WANG | ab4e8f8 | 2013-01-23 09:51:06 +0800 | [diff] [blame] | 628 | if (pcr->ops->conv_clk_and_div_n) |
Wei WANG | 678cacd | 2013-01-29 15:21:35 +0800 | [diff] [blame] | 629 | n = (u8)pcr->ops->conv_clk_and_div_n(clk, CLK_TO_DIV_N); |
Wei WANG | ab4e8f8 | 2013-01-23 09:51:06 +0800 | [diff] [blame] | 630 | else |
Wei WANG | 678cacd | 2013-01-29 15:21:35 +0800 | [diff] [blame] | 631 | n = (u8)(clk - 2); |
Wei WANG | eebbe25 | 2013-01-29 15:21:36 +0800 | [diff] [blame] | 632 | if ((clk <= 2) || (n > MAX_DIV_N_PCR)) |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 633 | return -EINVAL; |
| 634 | |
| 635 | mcu_cnt = (u8)(125/clk + 3); |
| 636 | if (mcu_cnt > 15) |
| 637 | mcu_cnt = 15; |
| 638 | |
Wei WANG | eebbe25 | 2013-01-29 15:21:36 +0800 | [diff] [blame] | 639 | /* Make sure that the SSC clock div_n is not less than MIN_DIV_N_PCR */ |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 640 | div = CLK_DIV_1; |
Wei WANG | eebbe25 | 2013-01-29 15:21:36 +0800 | [diff] [blame] | 641 | while ((n < MIN_DIV_N_PCR) && (div < CLK_DIV_8)) { |
Wei WANG | ab4e8f8 | 2013-01-23 09:51:06 +0800 | [diff] [blame] | 642 | if (pcr->ops->conv_clk_and_div_n) { |
Wei WANG | 678cacd | 2013-01-29 15:21:35 +0800 | [diff] [blame] | 643 | int dbl_clk = pcr->ops->conv_clk_and_div_n(n, |
Wei WANG | ab4e8f8 | 2013-01-23 09:51:06 +0800 | [diff] [blame] | 644 | DIV_N_TO_CLK) * 2; |
Wei WANG | 678cacd | 2013-01-29 15:21:35 +0800 | [diff] [blame] | 645 | n = (u8)pcr->ops->conv_clk_and_div_n(dbl_clk, |
Wei WANG | ab4e8f8 | 2013-01-23 09:51:06 +0800 | [diff] [blame] | 646 | CLK_TO_DIV_N); |
| 647 | } else { |
Wei WANG | 678cacd | 2013-01-29 15:21:35 +0800 | [diff] [blame] | 648 | n = (n + 2) * 2 - 2; |
Wei WANG | ab4e8f8 | 2013-01-23 09:51:06 +0800 | [diff] [blame] | 649 | } |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 650 | div++; |
| 651 | } |
Wei WANG | 678cacd | 2013-01-29 15:21:35 +0800 | [diff] [blame] | 652 | dev_dbg(&(pcr->pci->dev), "n = %d, div = %d\n", n, div); |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 653 | |
| 654 | ssc_depth = depth[ssc_depth]; |
| 655 | if (double_clk) |
| 656 | ssc_depth = double_ssc_depth(ssc_depth); |
| 657 | |
| 658 | ssc_depth = revise_ssc_depth(ssc_depth, div); |
| 659 | dev_dbg(&(pcr->pci->dev), "ssc_depth = %d\n", ssc_depth); |
| 660 | |
| 661 | rtsx_pci_init_cmd(pcr); |
| 662 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_CTL, |
| 663 | CLK_LOW_FREQ, CLK_LOW_FREQ); |
| 664 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV, |
| 665 | 0xFF, (div << 4) | mcu_cnt); |
| 666 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0); |
| 667 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, |
| 668 | SSC_DEPTH_MASK, ssc_depth); |
Wei WANG | 678cacd | 2013-01-29 15:21:35 +0800 | [diff] [blame] | 669 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, n); |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 670 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB); |
| 671 | if (vpclk) { |
| 672 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL, |
| 673 | PHASE_NOT_RESET, 0); |
| 674 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD_VPCLK0_CTL, |
| 675 | PHASE_NOT_RESET, PHASE_NOT_RESET); |
| 676 | } |
| 677 | |
| 678 | err = rtsx_pci_send_cmd(pcr, 2000); |
| 679 | if (err < 0) |
| 680 | return err; |
| 681 | |
| 682 | /* Wait SSC clock stable */ |
| 683 | udelay(10); |
| 684 | err = rtsx_pci_write_register(pcr, CLK_CTL, CLK_LOW_FREQ, 0); |
| 685 | if (err < 0) |
| 686 | return err; |
| 687 | |
| 688 | pcr->cur_clock = clk; |
| 689 | return 0; |
| 690 | } |
| 691 | EXPORT_SYMBOL_GPL(rtsx_pci_switch_clock); |
| 692 | |
| 693 | int rtsx_pci_card_power_on(struct rtsx_pcr *pcr, int card) |
| 694 | { |
| 695 | if (pcr->ops->card_power_on) |
| 696 | return pcr->ops->card_power_on(pcr, card); |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | EXPORT_SYMBOL_GPL(rtsx_pci_card_power_on); |
| 701 | |
| 702 | int rtsx_pci_card_power_off(struct rtsx_pcr *pcr, int card) |
| 703 | { |
| 704 | if (pcr->ops->card_power_off) |
| 705 | return pcr->ops->card_power_off(pcr, card); |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | EXPORT_SYMBOL_GPL(rtsx_pci_card_power_off); |
| 710 | |
Wei WANG | d817ac4 | 2013-01-23 09:51:04 +0800 | [diff] [blame] | 711 | int rtsx_pci_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage) |
| 712 | { |
| 713 | if (pcr->ops->switch_output_voltage) |
| 714 | return pcr->ops->switch_output_voltage(pcr, voltage); |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | EXPORT_SYMBOL_GPL(rtsx_pci_switch_output_voltage); |
| 719 | |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 720 | unsigned int rtsx_pci_card_exist(struct rtsx_pcr *pcr) |
| 721 | { |
| 722 | unsigned int val; |
| 723 | |
| 724 | val = rtsx_pci_readl(pcr, RTSX_BIPR); |
| 725 | if (pcr->ops->cd_deglitch) |
| 726 | val = pcr->ops->cd_deglitch(pcr); |
| 727 | |
| 728 | return val; |
| 729 | } |
| 730 | EXPORT_SYMBOL_GPL(rtsx_pci_card_exist); |
| 731 | |
| 732 | void rtsx_pci_complete_unfinished_transfer(struct rtsx_pcr *pcr) |
| 733 | { |
| 734 | struct completion finish; |
| 735 | |
| 736 | pcr->finish_me = &finish; |
| 737 | init_completion(&finish); |
| 738 | |
| 739 | if (pcr->done) |
| 740 | complete(pcr->done); |
| 741 | |
| 742 | if (!pcr->remove_pci) |
| 743 | rtsx_pci_stop_cmd(pcr); |
| 744 | |
| 745 | wait_for_completion_interruptible_timeout(&finish, |
| 746 | msecs_to_jiffies(2)); |
| 747 | pcr->finish_me = NULL; |
| 748 | } |
| 749 | EXPORT_SYMBOL_GPL(rtsx_pci_complete_unfinished_transfer); |
| 750 | |
| 751 | static void rtsx_pci_card_detect(struct work_struct *work) |
| 752 | { |
| 753 | struct delayed_work *dwork; |
| 754 | struct rtsx_pcr *pcr; |
| 755 | unsigned long flags; |
Wei WANG | 504decc | 2013-01-29 15:21:37 +0800 | [diff] [blame] | 756 | unsigned int card_detect = 0, card_inserted, card_removed; |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 757 | u32 irq_status; |
| 758 | |
| 759 | dwork = to_delayed_work(work); |
| 760 | pcr = container_of(dwork, struct rtsx_pcr, carddet_work); |
| 761 | |
| 762 | dev_dbg(&(pcr->pci->dev), "--> %s\n", __func__); |
| 763 | |
Wei WANG | 504decc | 2013-01-29 15:21:37 +0800 | [diff] [blame] | 764 | mutex_lock(&pcr->pcr_mutex); |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 765 | spin_lock_irqsave(&pcr->lock, flags); |
| 766 | |
| 767 | irq_status = rtsx_pci_readl(pcr, RTSX_BIPR); |
| 768 | dev_dbg(&(pcr->pci->dev), "irq_status: 0x%08x\n", irq_status); |
| 769 | |
Wei WANG | 504decc | 2013-01-29 15:21:37 +0800 | [diff] [blame] | 770 | irq_status &= CARD_EXIST; |
| 771 | card_inserted = pcr->card_inserted & irq_status; |
| 772 | card_removed = pcr->card_removed; |
| 773 | pcr->card_inserted = 0; |
| 774 | pcr->card_removed = 0; |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 775 | |
| 776 | spin_unlock_irqrestore(&pcr->lock, flags); |
| 777 | |
Wei WANG | 504decc | 2013-01-29 15:21:37 +0800 | [diff] [blame] | 778 | if (card_inserted || card_removed) { |
| 779 | dev_dbg(&(pcr->pci->dev), |
| 780 | "card_inserted: 0x%x, card_removed: 0x%x\n", |
| 781 | card_inserted, card_removed); |
| 782 | |
| 783 | if (pcr->ops->cd_deglitch) |
| 784 | card_inserted = pcr->ops->cd_deglitch(pcr); |
| 785 | |
| 786 | card_detect = card_inserted | card_removed; |
| 787 | } |
| 788 | |
| 789 | mutex_unlock(&pcr->pcr_mutex); |
| 790 | |
Wei WANG | 2d1484f | 2013-01-27 01:55:16 +0100 | [diff] [blame] | 791 | if ((card_detect & SD_EXIST) && pcr->slots[RTSX_SD_CARD].card_event) |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 792 | pcr->slots[RTSX_SD_CARD].card_event( |
| 793 | pcr->slots[RTSX_SD_CARD].p_dev); |
Wei WANG | 2d1484f | 2013-01-27 01:55:16 +0100 | [diff] [blame] | 794 | if ((card_detect & MS_EXIST) && pcr->slots[RTSX_MS_CARD].card_event) |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 795 | pcr->slots[RTSX_MS_CARD].card_event( |
| 796 | pcr->slots[RTSX_MS_CARD].p_dev); |
| 797 | } |
| 798 | |
| 799 | static irqreturn_t rtsx_pci_isr(int irq, void *dev_id) |
| 800 | { |
| 801 | struct rtsx_pcr *pcr = dev_id; |
| 802 | u32 int_reg; |
| 803 | |
| 804 | if (!pcr) |
| 805 | return IRQ_NONE; |
| 806 | |
| 807 | spin_lock(&pcr->lock); |
| 808 | |
| 809 | int_reg = rtsx_pci_readl(pcr, RTSX_BIPR); |
| 810 | /* Clear interrupt flag */ |
| 811 | rtsx_pci_writel(pcr, RTSX_BIPR, int_reg); |
| 812 | if ((int_reg & pcr->bier) == 0) { |
| 813 | spin_unlock(&pcr->lock); |
| 814 | return IRQ_NONE; |
| 815 | } |
| 816 | if (int_reg == 0xFFFFFFFF) { |
| 817 | spin_unlock(&pcr->lock); |
| 818 | return IRQ_HANDLED; |
| 819 | } |
| 820 | |
| 821 | int_reg &= (pcr->bier | 0x7FFFFF); |
| 822 | |
| 823 | if (int_reg & SD_INT) { |
| 824 | if (int_reg & SD_EXIST) { |
| 825 | pcr->card_inserted |= SD_EXIST; |
| 826 | } else { |
| 827 | pcr->card_removed |= SD_EXIST; |
| 828 | pcr->card_inserted &= ~SD_EXIST; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | if (int_reg & MS_INT) { |
| 833 | if (int_reg & MS_EXIST) { |
| 834 | pcr->card_inserted |= MS_EXIST; |
| 835 | } else { |
| 836 | pcr->card_removed |= MS_EXIST; |
| 837 | pcr->card_inserted &= ~MS_EXIST; |
| 838 | } |
| 839 | } |
| 840 | |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 841 | if (int_reg & (NEED_COMPLETE_INT | DELINK_INT)) { |
| 842 | if (int_reg & (TRANS_FAIL_INT | DELINK_INT)) { |
| 843 | pcr->trans_result = TRANS_RESULT_FAIL; |
| 844 | if (pcr->done) |
| 845 | complete(pcr->done); |
| 846 | } else if (int_reg & TRANS_OK_INT) { |
| 847 | pcr->trans_result = TRANS_RESULT_OK; |
| 848 | if (pcr->done) |
| 849 | complete(pcr->done); |
| 850 | } |
| 851 | } |
| 852 | |
Wei WANG | 504decc | 2013-01-29 15:21:37 +0800 | [diff] [blame] | 853 | if (pcr->card_inserted || pcr->card_removed) |
| 854 | schedule_delayed_work(&pcr->carddet_work, |
| 855 | msecs_to_jiffies(200)); |
| 856 | |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 857 | spin_unlock(&pcr->lock); |
| 858 | return IRQ_HANDLED; |
| 859 | } |
| 860 | |
| 861 | static int rtsx_pci_acquire_irq(struct rtsx_pcr *pcr) |
| 862 | { |
| 863 | dev_info(&(pcr->pci->dev), "%s: pcr->msi_en = %d, pci->irq = %d\n", |
| 864 | __func__, pcr->msi_en, pcr->pci->irq); |
| 865 | |
| 866 | if (request_irq(pcr->pci->irq, rtsx_pci_isr, |
| 867 | pcr->msi_en ? 0 : IRQF_SHARED, |
| 868 | DRV_NAME_RTSX_PCI, pcr)) { |
| 869 | dev_err(&(pcr->pci->dev), |
| 870 | "rtsx_sdmmc: unable to grab IRQ %d, disabling device\n", |
| 871 | pcr->pci->irq); |
| 872 | return -1; |
| 873 | } |
| 874 | |
| 875 | pcr->irq = pcr->pci->irq; |
| 876 | pci_intx(pcr->pci, !pcr->msi_en); |
| 877 | |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | static void rtsx_pci_idle_work(struct work_struct *work) |
| 882 | { |
| 883 | struct delayed_work *dwork = to_delayed_work(work); |
| 884 | struct rtsx_pcr *pcr = container_of(dwork, struct rtsx_pcr, idle_work); |
| 885 | |
| 886 | dev_dbg(&(pcr->pci->dev), "--> %s\n", __func__); |
| 887 | |
| 888 | mutex_lock(&pcr->pcr_mutex); |
| 889 | |
| 890 | pcr->state = PDEV_STAT_IDLE; |
| 891 | |
| 892 | if (pcr->ops->disable_auto_blink) |
| 893 | pcr->ops->disable_auto_blink(pcr); |
| 894 | if (pcr->ops->turn_off_led) |
| 895 | pcr->ops->turn_off_led(pcr); |
| 896 | |
| 897 | mutex_unlock(&pcr->pcr_mutex); |
| 898 | } |
| 899 | |
| 900 | static int rtsx_pci_init_hw(struct rtsx_pcr *pcr) |
| 901 | { |
| 902 | int err; |
| 903 | |
| 904 | rtsx_pci_writel(pcr, RTSX_HCBAR, pcr->host_cmds_addr); |
| 905 | |
| 906 | rtsx_pci_enable_bus_int(pcr); |
| 907 | |
| 908 | /* Power on SSC */ |
| 909 | err = rtsx_pci_write_register(pcr, FPDCTL, SSC_POWER_DOWN, 0); |
| 910 | if (err < 0) |
| 911 | return err; |
| 912 | |
| 913 | /* Wait SSC power stable */ |
| 914 | udelay(200); |
| 915 | |
| 916 | if (pcr->ops->optimize_phy) { |
| 917 | err = pcr->ops->optimize_phy(pcr); |
| 918 | if (err < 0) |
| 919 | return err; |
| 920 | } |
| 921 | |
| 922 | rtsx_pci_init_cmd(pcr); |
| 923 | |
| 924 | /* Set mcu_cnt to 7 to ensure data can be sampled properly */ |
| 925 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CLK_DIV, 0x07, 0x07); |
| 926 | |
| 927 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, HOST_SLEEP_STATE, 0x03, 0x00); |
| 928 | /* Disable card clock */ |
| 929 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_CLK_EN, 0x1E, 0); |
| 930 | /* Reset ASPM state to default value */ |
| 931 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, ASPM_FORCE_CTL, 0x3F, 0); |
| 932 | /* Reset delink mode */ |
| 933 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x0A, 0); |
| 934 | /* Card driving select */ |
| 935 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SD30_DRIVE_SEL, |
| 936 | 0x07, DRIVER_TYPE_D); |
| 937 | /* Enable SSC Clock */ |
| 938 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL1, |
| 939 | 0xFF, SSC_8X_EN | SSC_SEL_4M); |
| 940 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, SSC_CTL2, 0xFF, 0x12); |
| 941 | /* Disable cd_pwr_save */ |
| 942 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CHANGE_LINK_STATE, 0x16, 0x10); |
| 943 | /* Clear Link Ready Interrupt */ |
| 944 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, IRQSTAT0, |
| 945 | LINK_RDY_INT, LINK_RDY_INT); |
| 946 | /* Enlarge the estimation window of PERST# glitch |
| 947 | * to reduce the chance of invalid card interrupt |
| 948 | */ |
| 949 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PERST_GLITCH_WIDTH, 0xFF, 0x80); |
| 950 | /* Update RC oscillator to 400k |
| 951 | * bit[0] F_HIGH: for RC oscillator, Rst_value is 1'b1 |
| 952 | * 1: 2M 0: 400k |
| 953 | */ |
| 954 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, RCCTL, 0x01, 0x00); |
| 955 | /* Set interrupt write clear |
| 956 | * bit 1: U_elbi_if_rd_clr_en |
| 957 | * 1: Enable ELBI interrupt[31:22] & [7:0] flag read clear |
| 958 | * 0: ELBI interrupt flag[31:22] & [7:0] only can be write clear |
| 959 | */ |
| 960 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, NFTS_TX_CTRL, 0x02, 0); |
| 961 | /* Force CLKREQ# PIN to drive 0 to request clock */ |
| 962 | rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0x08, 0x08); |
| 963 | |
| 964 | err = rtsx_pci_send_cmd(pcr, 100); |
| 965 | if (err < 0) |
| 966 | return err; |
| 967 | |
| 968 | /* Enable clk_request_n to enable clock power management */ |
| 969 | rtsx_pci_write_config_byte(pcr, 0x81, 1); |
| 970 | /* Enter L1 when host tx idle */ |
| 971 | rtsx_pci_write_config_byte(pcr, 0x70F, 0x5B); |
| 972 | |
| 973 | if (pcr->ops->extra_init_hw) { |
| 974 | err = pcr->ops->extra_init_hw(pcr); |
| 975 | if (err < 0) |
| 976 | return err; |
| 977 | } |
| 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | static int rtsx_pci_init_chip(struct rtsx_pcr *pcr) |
| 983 | { |
| 984 | int err; |
| 985 | |
| 986 | spin_lock_init(&pcr->lock); |
| 987 | mutex_init(&pcr->pcr_mutex); |
| 988 | |
| 989 | switch (PCI_PID(pcr)) { |
| 990 | default: |
| 991 | case 0x5209: |
| 992 | rts5209_init_params(pcr); |
| 993 | break; |
| 994 | |
| 995 | case 0x5229: |
| 996 | rts5229_init_params(pcr); |
| 997 | break; |
| 998 | |
| 999 | case 0x5289: |
| 1000 | rtl8411_init_params(pcr); |
| 1001 | break; |
Roger Tseng | e123793 | 2013-02-04 15:45:59 +0800 | [diff] [blame^] | 1002 | |
| 1003 | case 0x5227: |
| 1004 | rts5227_init_params(pcr); |
| 1005 | break; |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | dev_dbg(&(pcr->pci->dev), "PID: 0x%04x, IC version: 0x%02x\n", |
| 1009 | PCI_PID(pcr), pcr->ic_version); |
| 1010 | |
| 1011 | pcr->slots = kcalloc(pcr->num_slots, sizeof(struct rtsx_slot), |
| 1012 | GFP_KERNEL); |
| 1013 | if (!pcr->slots) |
| 1014 | return -ENOMEM; |
| 1015 | |
| 1016 | pcr->state = PDEV_STAT_IDLE; |
| 1017 | err = rtsx_pci_init_hw(pcr); |
| 1018 | if (err < 0) { |
| 1019 | kfree(pcr->slots); |
| 1020 | return err; |
| 1021 | } |
| 1022 | |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
Greg Kroah-Hartman | 612b95c | 2012-12-21 15:03:15 -0800 | [diff] [blame] | 1026 | static int rtsx_pci_probe(struct pci_dev *pcidev, |
| 1027 | const struct pci_device_id *id) |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 1028 | { |
| 1029 | struct rtsx_pcr *pcr; |
| 1030 | struct pcr_handle *handle; |
| 1031 | u32 base, len; |
| 1032 | int ret, i; |
| 1033 | |
| 1034 | dev_dbg(&(pcidev->dev), |
| 1035 | ": Realtek PCI-E Card Reader found at %s [%04x:%04x] (rev %x)\n", |
| 1036 | pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device, |
| 1037 | (int)pcidev->revision); |
| 1038 | |
Wei WANG | f84ef04 | 2013-01-29 15:21:34 +0800 | [diff] [blame] | 1039 | ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32)); |
| 1040 | if (ret < 0) |
| 1041 | return ret; |
| 1042 | |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 1043 | ret = pci_enable_device(pcidev); |
| 1044 | if (ret) |
| 1045 | return ret; |
| 1046 | |
| 1047 | ret = pci_request_regions(pcidev, DRV_NAME_RTSX_PCI); |
| 1048 | if (ret) |
| 1049 | goto disable; |
| 1050 | |
| 1051 | pcr = kzalloc(sizeof(*pcr), GFP_KERNEL); |
| 1052 | if (!pcr) { |
| 1053 | ret = -ENOMEM; |
| 1054 | goto release_pci; |
| 1055 | } |
| 1056 | |
| 1057 | handle = kzalloc(sizeof(*handle), GFP_KERNEL); |
| 1058 | if (!handle) { |
| 1059 | ret = -ENOMEM; |
| 1060 | goto free_pcr; |
| 1061 | } |
| 1062 | handle->pcr = pcr; |
| 1063 | |
| 1064 | if (!idr_pre_get(&rtsx_pci_idr, GFP_KERNEL)) { |
| 1065 | ret = -ENOMEM; |
| 1066 | goto free_handle; |
| 1067 | } |
| 1068 | |
| 1069 | spin_lock(&rtsx_pci_lock); |
| 1070 | ret = idr_get_new(&rtsx_pci_idr, pcr, &pcr->id); |
| 1071 | spin_unlock(&rtsx_pci_lock); |
| 1072 | if (ret) |
| 1073 | goto free_handle; |
| 1074 | |
| 1075 | pcr->pci = pcidev; |
| 1076 | dev_set_drvdata(&pcidev->dev, handle); |
| 1077 | |
| 1078 | len = pci_resource_len(pcidev, 0); |
| 1079 | base = pci_resource_start(pcidev, 0); |
| 1080 | pcr->remap_addr = ioremap_nocache(base, len); |
| 1081 | if (!pcr->remap_addr) { |
| 1082 | ret = -ENOMEM; |
| 1083 | goto free_host; |
| 1084 | } |
| 1085 | |
| 1086 | pcr->rtsx_resv_buf = dma_alloc_coherent(&(pcidev->dev), |
| 1087 | RTSX_RESV_BUF_LEN, &(pcr->rtsx_resv_buf_addr), |
| 1088 | GFP_KERNEL); |
| 1089 | if (pcr->rtsx_resv_buf == NULL) { |
| 1090 | ret = -ENXIO; |
| 1091 | goto unmap; |
| 1092 | } |
| 1093 | pcr->host_cmds_ptr = pcr->rtsx_resv_buf; |
| 1094 | pcr->host_cmds_addr = pcr->rtsx_resv_buf_addr; |
| 1095 | pcr->host_sg_tbl_ptr = pcr->rtsx_resv_buf + HOST_CMDS_BUF_LEN; |
| 1096 | pcr->host_sg_tbl_addr = pcr->rtsx_resv_buf_addr + HOST_CMDS_BUF_LEN; |
| 1097 | |
| 1098 | pcr->card_inserted = 0; |
| 1099 | pcr->card_removed = 0; |
| 1100 | INIT_DELAYED_WORK(&pcr->carddet_work, rtsx_pci_card_detect); |
| 1101 | INIT_DELAYED_WORK(&pcr->idle_work, rtsx_pci_idle_work); |
| 1102 | |
| 1103 | pcr->msi_en = msi_en; |
| 1104 | if (pcr->msi_en) { |
| 1105 | ret = pci_enable_msi(pcidev); |
| 1106 | if (ret < 0) |
| 1107 | pcr->msi_en = false; |
| 1108 | } |
| 1109 | |
| 1110 | ret = rtsx_pci_acquire_irq(pcr); |
| 1111 | if (ret < 0) |
| 1112 | goto free_dma; |
| 1113 | |
| 1114 | pci_set_master(pcidev); |
| 1115 | synchronize_irq(pcr->irq); |
| 1116 | |
| 1117 | ret = rtsx_pci_init_chip(pcr); |
| 1118 | if (ret < 0) |
| 1119 | goto disable_irq; |
| 1120 | |
| 1121 | for (i = 0; i < ARRAY_SIZE(rtsx_pcr_cells); i++) { |
| 1122 | rtsx_pcr_cells[i].platform_data = handle; |
| 1123 | rtsx_pcr_cells[i].pdata_size = sizeof(*handle); |
| 1124 | } |
| 1125 | ret = mfd_add_devices(&pcidev->dev, pcr->id, rtsx_pcr_cells, |
| 1126 | ARRAY_SIZE(rtsx_pcr_cells), NULL, 0, NULL); |
| 1127 | if (ret < 0) |
| 1128 | goto disable_irq; |
| 1129 | |
| 1130 | schedule_delayed_work(&pcr->idle_work, msecs_to_jiffies(200)); |
| 1131 | |
| 1132 | return 0; |
| 1133 | |
| 1134 | disable_irq: |
| 1135 | free_irq(pcr->irq, (void *)pcr); |
| 1136 | free_dma: |
| 1137 | dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN, |
| 1138 | pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr); |
| 1139 | unmap: |
| 1140 | iounmap(pcr->remap_addr); |
| 1141 | free_host: |
| 1142 | dev_set_drvdata(&pcidev->dev, NULL); |
| 1143 | free_handle: |
| 1144 | kfree(handle); |
| 1145 | free_pcr: |
| 1146 | kfree(pcr); |
| 1147 | release_pci: |
| 1148 | pci_release_regions(pcidev); |
| 1149 | disable: |
| 1150 | pci_disable_device(pcidev); |
| 1151 | |
| 1152 | return ret; |
| 1153 | } |
| 1154 | |
Greg Kroah-Hartman | 612b95c | 2012-12-21 15:03:15 -0800 | [diff] [blame] | 1155 | static void rtsx_pci_remove(struct pci_dev *pcidev) |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 1156 | { |
| 1157 | struct pcr_handle *handle = pci_get_drvdata(pcidev); |
| 1158 | struct rtsx_pcr *pcr = handle->pcr; |
| 1159 | |
| 1160 | pcr->remove_pci = true; |
| 1161 | |
| 1162 | cancel_delayed_work(&pcr->carddet_work); |
| 1163 | cancel_delayed_work(&pcr->idle_work); |
| 1164 | |
| 1165 | mfd_remove_devices(&pcidev->dev); |
| 1166 | |
| 1167 | dma_free_coherent(&(pcr->pci->dev), RTSX_RESV_BUF_LEN, |
| 1168 | pcr->rtsx_resv_buf, pcr->rtsx_resv_buf_addr); |
| 1169 | free_irq(pcr->irq, (void *)pcr); |
| 1170 | if (pcr->msi_en) |
| 1171 | pci_disable_msi(pcr->pci); |
| 1172 | iounmap(pcr->remap_addr); |
| 1173 | |
| 1174 | dev_set_drvdata(&pcidev->dev, NULL); |
| 1175 | pci_release_regions(pcidev); |
| 1176 | pci_disable_device(pcidev); |
| 1177 | |
| 1178 | spin_lock(&rtsx_pci_lock); |
| 1179 | idr_remove(&rtsx_pci_idr, pcr->id); |
| 1180 | spin_unlock(&rtsx_pci_lock); |
| 1181 | |
| 1182 | kfree(pcr->slots); |
| 1183 | kfree(pcr); |
| 1184 | kfree(handle); |
| 1185 | |
| 1186 | dev_dbg(&(pcidev->dev), |
| 1187 | ": Realtek PCI-E Card Reader at %s [%04x:%04x] has been removed\n", |
| 1188 | pci_name(pcidev), (int)pcidev->vendor, (int)pcidev->device); |
| 1189 | } |
| 1190 | |
| 1191 | #ifdef CONFIG_PM |
| 1192 | |
| 1193 | static int rtsx_pci_suspend(struct pci_dev *pcidev, pm_message_t state) |
| 1194 | { |
| 1195 | struct pcr_handle *handle; |
| 1196 | struct rtsx_pcr *pcr; |
| 1197 | int ret = 0; |
| 1198 | |
| 1199 | dev_dbg(&(pcidev->dev), "--> %s\n", __func__); |
| 1200 | |
| 1201 | handle = pci_get_drvdata(pcidev); |
| 1202 | pcr = handle->pcr; |
| 1203 | |
| 1204 | cancel_delayed_work(&pcr->carddet_work); |
| 1205 | cancel_delayed_work(&pcr->idle_work); |
| 1206 | |
| 1207 | mutex_lock(&pcr->pcr_mutex); |
| 1208 | |
| 1209 | if (pcr->ops->turn_off_led) |
| 1210 | pcr->ops->turn_off_led(pcr); |
| 1211 | |
| 1212 | rtsx_pci_writel(pcr, RTSX_BIER, 0); |
| 1213 | pcr->bier = 0; |
| 1214 | |
| 1215 | rtsx_pci_write_register(pcr, PETXCFG, 0x08, 0x08); |
| 1216 | rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, 0x02); |
| 1217 | |
| 1218 | pci_save_state(pcidev); |
| 1219 | pci_enable_wake(pcidev, pci_choose_state(pcidev, state), 0); |
| 1220 | pci_disable_device(pcidev); |
| 1221 | pci_set_power_state(pcidev, pci_choose_state(pcidev, state)); |
| 1222 | |
| 1223 | mutex_unlock(&pcr->pcr_mutex); |
| 1224 | return ret; |
| 1225 | } |
| 1226 | |
| 1227 | static int rtsx_pci_resume(struct pci_dev *pcidev) |
| 1228 | { |
| 1229 | struct pcr_handle *handle; |
| 1230 | struct rtsx_pcr *pcr; |
| 1231 | int ret = 0; |
| 1232 | |
| 1233 | dev_dbg(&(pcidev->dev), "--> %s\n", __func__); |
| 1234 | |
| 1235 | handle = pci_get_drvdata(pcidev); |
| 1236 | pcr = handle->pcr; |
| 1237 | |
| 1238 | mutex_lock(&pcr->pcr_mutex); |
| 1239 | |
| 1240 | pci_set_power_state(pcidev, PCI_D0); |
| 1241 | pci_restore_state(pcidev); |
| 1242 | ret = pci_enable_device(pcidev); |
| 1243 | if (ret) |
| 1244 | goto out; |
| 1245 | pci_set_master(pcidev); |
| 1246 | |
| 1247 | ret = rtsx_pci_write_register(pcr, HOST_SLEEP_STATE, 0x03, 0x00); |
| 1248 | if (ret) |
| 1249 | goto out; |
| 1250 | |
| 1251 | ret = rtsx_pci_init_hw(pcr); |
| 1252 | if (ret) |
| 1253 | goto out; |
| 1254 | |
| 1255 | schedule_delayed_work(&pcr->idle_work, msecs_to_jiffies(200)); |
| 1256 | |
| 1257 | out: |
| 1258 | mutex_unlock(&pcr->pcr_mutex); |
| 1259 | return ret; |
| 1260 | } |
| 1261 | |
| 1262 | #else /* CONFIG_PM */ |
| 1263 | |
| 1264 | #define rtsx_pci_suspend NULL |
| 1265 | #define rtsx_pci_resume NULL |
| 1266 | |
| 1267 | #endif /* CONFIG_PM */ |
| 1268 | |
| 1269 | static struct pci_driver rtsx_pci_driver = { |
| 1270 | .name = DRV_NAME_RTSX_PCI, |
| 1271 | .id_table = rtsx_pci_ids, |
| 1272 | .probe = rtsx_pci_probe, |
Greg Kroah-Hartman | 612b95c | 2012-12-21 15:03:15 -0800 | [diff] [blame] | 1273 | .remove = rtsx_pci_remove, |
Wei WANG | ada8a8a | 2012-10-29 13:49:33 +0800 | [diff] [blame] | 1274 | .suspend = rtsx_pci_suspend, |
| 1275 | .resume = rtsx_pci_resume, |
| 1276 | }; |
| 1277 | module_pci_driver(rtsx_pci_driver); |
| 1278 | |
| 1279 | MODULE_LICENSE("GPL"); |
| 1280 | MODULE_AUTHOR("Wei WANG <wei_wang@realsil.com.cn>"); |
| 1281 | MODULE_DESCRIPTION("Realtek PCI-E Card Reader Driver"); |