1 /* Realtek USB Memstick Card Interface driver 2 * 3 * Copyright(c) 2009-2013 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 version 2 7 * as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License along 15 * with this program; if not, see <http://www.gnu.org/licenses/>. 16 * 17 * Author: 18 * Roger Tseng <rogerable@realtek.com> 19 */ 20 21 #include <linux/module.h> 22 #include <linux/highmem.h> 23 #include <linux/delay.h> 24 #include <linux/platform_device.h> 25 #include <linux/workqueue.h> 26 #include <linux/memstick.h> 27 #include <linux/kthread.h> 28 #include <linux/mfd/rtsx_usb.h> 29 #include <linux/pm_runtime.h> 30 #include <linux/mutex.h> 31 #include <linux/sched.h> 32 #include <linux/completion.h> 33 #include <asm/unaligned.h> 34 35 struct rtsx_usb_ms { 36 struct platform_device *pdev; 37 struct rtsx_ucr *ucr; 38 struct memstick_host *msh; 39 struct memstick_request *req; 40 41 struct mutex host_mutex; 42 struct work_struct handle_req; 43 44 struct task_struct *detect_ms; 45 struct completion detect_ms_exit; 46 47 u8 ssc_depth; 48 unsigned int clock; 49 int power_mode; 50 unsigned char ifmode; 51 bool eject; 52 }; 53 54 static inline struct device *ms_dev(struct rtsx_usb_ms *host) 55 { 56 return &(host->pdev->dev); 57 } 58 59 static inline void ms_clear_error(struct rtsx_usb_ms *host) 60 { 61 struct rtsx_ucr *ucr = host->ucr; 62 rtsx_usb_ep0_write_register(ucr, CARD_STOP, 63 MS_STOP | MS_CLR_ERR, 64 MS_STOP | MS_CLR_ERR); 65 66 rtsx_usb_clear_dma_err(ucr); 67 rtsx_usb_clear_fsm_err(ucr); 68 } 69 70 #ifdef DEBUG 71 72 static void ms_print_debug_regs(struct rtsx_usb_ms *host) 73 { 74 struct rtsx_ucr *ucr = host->ucr; 75 u16 i; 76 u8 *ptr; 77 78 /* Print MS host internal registers */ 79 rtsx_usb_init_cmd(ucr); 80 81 /* MS_CFG to MS_INT_REG */ 82 for (i = 0xFD40; i <= 0xFD44; i++) 83 rtsx_usb_add_cmd(ucr, READ_REG_CMD, i, 0, 0); 84 85 /* CARD_SHARE_MODE to CARD_GPIO */ 86 for (i = 0xFD51; i <= 0xFD56; i++) 87 rtsx_usb_add_cmd(ucr, READ_REG_CMD, i, 0, 0); 88 89 /* CARD_PULL_CTLx */ 90 for (i = 0xFD60; i <= 0xFD65; i++) 91 rtsx_usb_add_cmd(ucr, READ_REG_CMD, i, 0, 0); 92 93 /* CARD_DATA_SOURCE, CARD_SELECT, CARD_CLK_EN, CARD_PWR_CTL */ 94 rtsx_usb_add_cmd(ucr, READ_REG_CMD, CARD_DATA_SOURCE, 0, 0); 95 rtsx_usb_add_cmd(ucr, READ_REG_CMD, CARD_SELECT, 0, 0); 96 rtsx_usb_add_cmd(ucr, READ_REG_CMD, CARD_CLK_EN, 0, 0); 97 rtsx_usb_add_cmd(ucr, READ_REG_CMD, CARD_PWR_CTL, 0, 0); 98 99 rtsx_usb_send_cmd(ucr, MODE_CR, 100); 100 rtsx_usb_get_rsp(ucr, 21, 100); 101 102 ptr = ucr->rsp_buf; 103 for (i = 0xFD40; i <= 0xFD44; i++) 104 dev_dbg(ms_dev(host), "0x%04X: 0x%02x\n", i, *(ptr++)); 105 for (i = 0xFD51; i <= 0xFD56; i++) 106 dev_dbg(ms_dev(host), "0x%04X: 0x%02x\n", i, *(ptr++)); 107 for (i = 0xFD60; i <= 0xFD65; i++) 108 dev_dbg(ms_dev(host), "0x%04X: 0x%02x\n", i, *(ptr++)); 109 110 dev_dbg(ms_dev(host), "0x%04X: 0x%02x\n", CARD_DATA_SOURCE, *(ptr++)); 111 dev_dbg(ms_dev(host), "0x%04X: 0x%02x\n", CARD_SELECT, *(ptr++)); 112 dev_dbg(ms_dev(host), "0x%04X: 0x%02x\n", CARD_CLK_EN, *(ptr++)); 113 dev_dbg(ms_dev(host), "0x%04X: 0x%02x\n", CARD_PWR_CTL, *(ptr++)); 114 } 115 116 #else 117 118 static void ms_print_debug_regs(struct rtsx_usb_ms *host) 119 { 120 } 121 122 #endif 123 124 static int ms_pull_ctl_disable_lqfp48(struct rtsx_ucr *ucr) 125 { 126 rtsx_usb_init_cmd(ucr); 127 128 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL1, 0xFF, 0x55); 129 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL2, 0xFF, 0x55); 130 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL3, 0xFF, 0x95); 131 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL4, 0xFF, 0x55); 132 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL5, 0xFF, 0x55); 133 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL6, 0xFF, 0xA5); 134 135 return rtsx_usb_send_cmd(ucr, MODE_C, 100); 136 } 137 138 static int ms_pull_ctl_disable_qfn24(struct rtsx_ucr *ucr) 139 { 140 rtsx_usb_init_cmd(ucr); 141 142 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL1, 0xFF, 0x65); 143 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL2, 0xFF, 0x55); 144 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL3, 0xFF, 0x95); 145 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL4, 0xFF, 0x55); 146 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL5, 0xFF, 0x56); 147 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL6, 0xFF, 0x59); 148 149 return rtsx_usb_send_cmd(ucr, MODE_C, 100); 150 } 151 152 static int ms_pull_ctl_enable_lqfp48(struct rtsx_ucr *ucr) 153 { 154 rtsx_usb_init_cmd(ucr); 155 156 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL1, 0xFF, 0x55); 157 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL2, 0xFF, 0x55); 158 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL3, 0xFF, 0x95); 159 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL4, 0xFF, 0x55); 160 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL5, 0xFF, 0x55); 161 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL6, 0xFF, 0xA5); 162 163 return rtsx_usb_send_cmd(ucr, MODE_C, 100); 164 } 165 166 static int ms_pull_ctl_enable_qfn24(struct rtsx_ucr *ucr) 167 { 168 rtsx_usb_init_cmd(ucr); 169 170 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL1, 0xFF, 0x65); 171 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL2, 0xFF, 0x55); 172 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL3, 0xFF, 0x95); 173 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL4, 0xFF, 0x55); 174 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL5, 0xFF, 0x55); 175 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL6, 0xFF, 0x59); 176 177 return rtsx_usb_send_cmd(ucr, MODE_C, 100); 178 } 179 180 static int ms_power_on(struct rtsx_usb_ms *host) 181 { 182 struct rtsx_ucr *ucr = host->ucr; 183 int err; 184 185 dev_dbg(ms_dev(host), "%s\n", __func__); 186 187 rtsx_usb_init_cmd(ucr); 188 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_SELECT, 0x07, MS_MOD_SEL); 189 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_SHARE_MODE, 190 CARD_SHARE_MASK, CARD_SHARE_MS); 191 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_CLK_EN, 192 MS_CLK_EN, MS_CLK_EN); 193 err = rtsx_usb_send_cmd(ucr, MODE_C, 100); 194 if (err < 0) 195 return err; 196 197 if (CHECK_PKG(ucr, LQFP48)) 198 err = ms_pull_ctl_enable_lqfp48(ucr); 199 else 200 err = ms_pull_ctl_enable_qfn24(ucr); 201 if (err < 0) 202 return err; 203 204 err = rtsx_usb_write_register(ucr, CARD_PWR_CTL, 205 POWER_MASK, PARTIAL_POWER_ON); 206 if (err) 207 return err; 208 209 usleep_range(800, 1000); 210 211 rtsx_usb_init_cmd(ucr); 212 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PWR_CTL, 213 POWER_MASK, POWER_ON); 214 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_OE, 215 MS_OUTPUT_EN, MS_OUTPUT_EN); 216 217 return rtsx_usb_send_cmd(ucr, MODE_C, 100); 218 } 219 220 static int ms_power_off(struct rtsx_usb_ms *host) 221 { 222 struct rtsx_ucr *ucr = host->ucr; 223 int err; 224 225 dev_dbg(ms_dev(host), "%s\n", __func__); 226 227 rtsx_usb_init_cmd(ucr); 228 229 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_CLK_EN, MS_CLK_EN, 0); 230 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_OE, MS_OUTPUT_EN, 0); 231 232 err = rtsx_usb_send_cmd(ucr, MODE_C, 100); 233 if (err < 0) 234 return err; 235 236 if (CHECK_PKG(ucr, LQFP48)) 237 return ms_pull_ctl_disable_lqfp48(ucr); 238 239 return ms_pull_ctl_disable_qfn24(ucr); 240 } 241 242 static int ms_transfer_data(struct rtsx_usb_ms *host, unsigned char data_dir, 243 u8 tpc, u8 cfg, struct scatterlist *sg) 244 { 245 struct rtsx_ucr *ucr = host->ucr; 246 int err; 247 unsigned int length = sg->length; 248 u16 sec_cnt = (u16)(length / 512); 249 u8 trans_mode, dma_dir, flag; 250 unsigned int pipe; 251 struct memstick_dev *card = host->msh->card; 252 253 dev_dbg(ms_dev(host), "%s: tpc = 0x%02x, data_dir = %s, length = %d\n", 254 __func__, tpc, (data_dir == READ) ? "READ" : "WRITE", 255 length); 256 257 if (data_dir == READ) { 258 flag = MODE_CDIR; 259 dma_dir = DMA_DIR_FROM_CARD; 260 if (card->id.type != MEMSTICK_TYPE_PRO) 261 trans_mode = MS_TM_NORMAL_READ; 262 else 263 trans_mode = MS_TM_AUTO_READ; 264 pipe = usb_rcvbulkpipe(ucr->pusb_dev, EP_BULK_IN); 265 } else { 266 flag = MODE_CDOR; 267 dma_dir = DMA_DIR_TO_CARD; 268 if (card->id.type != MEMSTICK_TYPE_PRO) 269 trans_mode = MS_TM_NORMAL_WRITE; 270 else 271 trans_mode = MS_TM_AUTO_WRITE; 272 pipe = usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT); 273 } 274 275 rtsx_usb_init_cmd(ucr); 276 277 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc); 278 if (card->id.type == MEMSTICK_TYPE_PRO) { 279 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_SECTOR_CNT_H, 280 0xFF, (u8)(sec_cnt >> 8)); 281 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_SECTOR_CNT_L, 282 0xFF, (u8)sec_cnt); 283 } 284 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg); 285 286 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MC_DMA_TC3, 287 0xFF, (u8)(length >> 24)); 288 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MC_DMA_TC2, 289 0xFF, (u8)(length >> 16)); 290 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MC_DMA_TC1, 291 0xFF, (u8)(length >> 8)); 292 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MC_DMA_TC0, 0xFF, 293 (u8)length); 294 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MC_DMA_CTL, 295 0x03 | DMA_PACK_SIZE_MASK, dma_dir | DMA_EN | DMA_512); 296 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_DATA_SOURCE, 297 0x01, RING_BUFFER); 298 299 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_TRANSFER, 300 0xFF, MS_TRANSFER_START | trans_mode); 301 rtsx_usb_add_cmd(ucr, CHECK_REG_CMD, MS_TRANSFER, 302 MS_TRANSFER_END, MS_TRANSFER_END); 303 304 err = rtsx_usb_send_cmd(ucr, flag | STAGE_MS_STATUS, 100); 305 if (err) 306 return err; 307 308 err = rtsx_usb_transfer_data(ucr, pipe, sg, length, 309 1, NULL, 10000); 310 if (err) 311 goto err_out; 312 313 err = rtsx_usb_get_rsp(ucr, 3, 15000); 314 if (err) 315 goto err_out; 316 317 if (ucr->rsp_buf[0] & MS_TRANSFER_ERR || 318 ucr->rsp_buf[1] & (MS_CRC16_ERR | MS_RDY_TIMEOUT)) { 319 err = -EIO; 320 goto err_out; 321 } 322 return 0; 323 err_out: 324 ms_clear_error(host); 325 return err; 326 } 327 328 static int ms_write_bytes(struct rtsx_usb_ms *host, u8 tpc, 329 u8 cfg, u8 cnt, u8 *data, u8 *int_reg) 330 { 331 struct rtsx_ucr *ucr = host->ucr; 332 int err, i; 333 334 dev_dbg(ms_dev(host), "%s: tpc = 0x%02x\n", __func__, tpc); 335 336 rtsx_usb_init_cmd(ucr); 337 338 for (i = 0; i < cnt; i++) 339 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, 340 PPBUF_BASE2 + i, 0xFF, data[i]); 341 342 if (cnt % 2) 343 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, 344 PPBUF_BASE2 + i, 0xFF, 0xFF); 345 346 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc); 347 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_BYTE_CNT, 0xFF, cnt); 348 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg); 349 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_DATA_SOURCE, 350 0x01, PINGPONG_BUFFER); 351 352 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_TRANSFER, 353 0xFF, MS_TRANSFER_START | MS_TM_WRITE_BYTES); 354 rtsx_usb_add_cmd(ucr, CHECK_REG_CMD, MS_TRANSFER, 355 MS_TRANSFER_END, MS_TRANSFER_END); 356 rtsx_usb_add_cmd(ucr, READ_REG_CMD, MS_TRANS_CFG, 0, 0); 357 358 err = rtsx_usb_send_cmd(ucr, MODE_CR, 100); 359 if (err) 360 return err; 361 362 err = rtsx_usb_get_rsp(ucr, 2, 5000); 363 if (err || (ucr->rsp_buf[0] & MS_TRANSFER_ERR)) { 364 u8 val; 365 366 rtsx_usb_ep0_read_register(ucr, MS_TRANS_CFG, &val); 367 dev_dbg(ms_dev(host), "MS_TRANS_CFG: 0x%02x\n", val); 368 369 if (int_reg) 370 *int_reg = val & 0x0F; 371 372 ms_print_debug_regs(host); 373 374 ms_clear_error(host); 375 376 if (!(tpc & 0x08)) { 377 if (val & MS_CRC16_ERR) 378 return -EIO; 379 } else { 380 if (!(val & 0x80)) { 381 if (val & (MS_INT_ERR | MS_INT_CMDNK)) 382 return -EIO; 383 } 384 } 385 386 return -ETIMEDOUT; 387 } 388 389 if (int_reg) 390 *int_reg = ucr->rsp_buf[1] & 0x0F; 391 392 return 0; 393 } 394 395 static int ms_read_bytes(struct rtsx_usb_ms *host, u8 tpc, 396 u8 cfg, u8 cnt, u8 *data, u8 *int_reg) 397 { 398 struct rtsx_ucr *ucr = host->ucr; 399 int err, i; 400 u8 *ptr; 401 402 dev_dbg(ms_dev(host), "%s: tpc = 0x%02x\n", __func__, tpc); 403 404 rtsx_usb_init_cmd(ucr); 405 406 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_TPC, 0xFF, tpc); 407 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_BYTE_CNT, 0xFF, cnt); 408 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_TRANS_CFG, 0xFF, cfg); 409 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_DATA_SOURCE, 410 0x01, PINGPONG_BUFFER); 411 412 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, MS_TRANSFER, 413 0xFF, MS_TRANSFER_START | MS_TM_READ_BYTES); 414 rtsx_usb_add_cmd(ucr, CHECK_REG_CMD, MS_TRANSFER, 415 MS_TRANSFER_END, MS_TRANSFER_END); 416 for (i = 0; i < cnt - 1; i++) 417 rtsx_usb_add_cmd(ucr, READ_REG_CMD, PPBUF_BASE2 + i, 0, 0); 418 if (cnt % 2) 419 rtsx_usb_add_cmd(ucr, READ_REG_CMD, PPBUF_BASE2 + cnt, 0, 0); 420 else 421 rtsx_usb_add_cmd(ucr, READ_REG_CMD, 422 PPBUF_BASE2 + cnt - 1, 0, 0); 423 424 rtsx_usb_add_cmd(ucr, READ_REG_CMD, MS_TRANS_CFG, 0, 0); 425 426 err = rtsx_usb_send_cmd(ucr, MODE_CR, 100); 427 if (err) 428 return err; 429 430 err = rtsx_usb_get_rsp(ucr, cnt + 2, 5000); 431 if (err || (ucr->rsp_buf[0] & MS_TRANSFER_ERR)) { 432 u8 val; 433 434 rtsx_usb_ep0_read_register(ucr, MS_TRANS_CFG, &val); 435 dev_dbg(ms_dev(host), "MS_TRANS_CFG: 0x%02x\n", val); 436 437 if (int_reg && (host->ifmode != MEMSTICK_SERIAL)) 438 *int_reg = val & 0x0F; 439 440 ms_print_debug_regs(host); 441 442 ms_clear_error(host); 443 444 if (!(tpc & 0x08)) { 445 if (val & MS_CRC16_ERR) 446 return -EIO; 447 } else { 448 if (!(val & 0x80)) { 449 if (val & (MS_INT_ERR | MS_INT_CMDNK)) 450 return -EIO; 451 } 452 } 453 454 return -ETIMEDOUT; 455 } 456 457 ptr = ucr->rsp_buf + 1; 458 for (i = 0; i < cnt; i++) 459 data[i] = *ptr++; 460 461 462 if (int_reg && (host->ifmode != MEMSTICK_SERIAL)) 463 *int_reg = *ptr & 0x0F; 464 465 return 0; 466 } 467 468 static int rtsx_usb_ms_issue_cmd(struct rtsx_usb_ms *host) 469 { 470 struct memstick_request *req = host->req; 471 int err = 0; 472 u8 cfg = 0, int_reg; 473 474 dev_dbg(ms_dev(host), "%s\n", __func__); 475 476 if (req->need_card_int) { 477 if (host->ifmode != MEMSTICK_SERIAL) 478 cfg = WAIT_INT; 479 } 480 481 if (req->long_data) { 482 err = ms_transfer_data(host, req->data_dir, 483 req->tpc, cfg, &(req->sg)); 484 } else { 485 if (req->data_dir == READ) 486 err = ms_read_bytes(host, req->tpc, cfg, 487 req->data_len, req->data, &int_reg); 488 else 489 err = ms_write_bytes(host, req->tpc, cfg, 490 req->data_len, req->data, &int_reg); 491 } 492 if (err < 0) 493 return err; 494 495 if (req->need_card_int) { 496 if (host->ifmode == MEMSTICK_SERIAL) { 497 err = ms_read_bytes(host, MS_TPC_GET_INT, 498 NO_WAIT_INT, 1, &req->int_reg, NULL); 499 if (err < 0) 500 return err; 501 } else { 502 503 if (int_reg & MS_INT_CMDNK) 504 req->int_reg |= MEMSTICK_INT_CMDNAK; 505 if (int_reg & MS_INT_BREQ) 506 req->int_reg |= MEMSTICK_INT_BREQ; 507 if (int_reg & MS_INT_ERR) 508 req->int_reg |= MEMSTICK_INT_ERR; 509 if (int_reg & MS_INT_CED) 510 req->int_reg |= MEMSTICK_INT_CED; 511 } 512 dev_dbg(ms_dev(host), "int_reg: 0x%02x\n", req->int_reg); 513 } 514 515 return 0; 516 } 517 518 static void rtsx_usb_ms_handle_req(struct work_struct *work) 519 { 520 struct rtsx_usb_ms *host = container_of(work, 521 struct rtsx_usb_ms, handle_req); 522 struct rtsx_ucr *ucr = host->ucr; 523 struct memstick_host *msh = host->msh; 524 int rc; 525 526 if (!host->req) { 527 pm_runtime_get_sync(ms_dev(host)); 528 do { 529 rc = memstick_next_req(msh, &host->req); 530 dev_dbg(ms_dev(host), "next req %d\n", rc); 531 532 if (!rc) { 533 mutex_lock(&ucr->dev_mutex); 534 535 if (rtsx_usb_card_exclusive_check(ucr, 536 RTSX_USB_MS_CARD)) 537 host->req->error = -EIO; 538 else 539 host->req->error = 540 rtsx_usb_ms_issue_cmd(host); 541 542 mutex_unlock(&ucr->dev_mutex); 543 544 dev_dbg(ms_dev(host), "req result %d\n", 545 host->req->error); 546 } 547 } while (!rc); 548 pm_runtime_put(ms_dev(host)); 549 } 550 551 } 552 553 static void rtsx_usb_ms_request(struct memstick_host *msh) 554 { 555 struct rtsx_usb_ms *host = memstick_priv(msh); 556 557 dev_dbg(ms_dev(host), "--> %s\n", __func__); 558 559 if (!host->eject) 560 schedule_work(&host->handle_req); 561 } 562 563 static int rtsx_usb_ms_set_param(struct memstick_host *msh, 564 enum memstick_param param, int value) 565 { 566 struct rtsx_usb_ms *host = memstick_priv(msh); 567 struct rtsx_ucr *ucr = host->ucr; 568 unsigned int clock = 0; 569 u8 ssc_depth = 0; 570 int err; 571 572 dev_dbg(ms_dev(host), "%s: param = %d, value = %d\n", 573 __func__, param, value); 574 575 pm_runtime_get_sync(ms_dev(host)); 576 mutex_lock(&ucr->dev_mutex); 577 578 err = rtsx_usb_card_exclusive_check(ucr, RTSX_USB_MS_CARD); 579 if (err) 580 goto out; 581 582 switch (param) { 583 case MEMSTICK_POWER: 584 if (value == host->power_mode) 585 break; 586 587 if (value == MEMSTICK_POWER_ON) { 588 pm_runtime_get_sync(ms_dev(host)); 589 err = ms_power_on(host); 590 } else if (value == MEMSTICK_POWER_OFF) { 591 err = ms_power_off(host); 592 if (host->msh->card) 593 pm_runtime_put_noidle(ms_dev(host)); 594 else 595 pm_runtime_put(ms_dev(host)); 596 } else 597 err = -EINVAL; 598 if (!err) 599 host->power_mode = value; 600 break; 601 602 case MEMSTICK_INTERFACE: 603 if (value == MEMSTICK_SERIAL) { 604 clock = 19000000; 605 ssc_depth = SSC_DEPTH_512K; 606 err = rtsx_usb_write_register(ucr, MS_CFG, 0x5A, 607 MS_BUS_WIDTH_1 | PUSH_TIME_DEFAULT); 608 if (err < 0) 609 break; 610 } else if (value == MEMSTICK_PAR4) { 611 clock = 39000000; 612 ssc_depth = SSC_DEPTH_1M; 613 614 err = rtsx_usb_write_register(ucr, MS_CFG, 0x5A, 615 MS_BUS_WIDTH_4 | PUSH_TIME_ODD | 616 MS_NO_CHECK_INT); 617 if (err < 0) 618 break; 619 } else { 620 err = -EINVAL; 621 break; 622 } 623 624 err = rtsx_usb_switch_clock(ucr, clock, 625 ssc_depth, false, true, false); 626 if (err < 0) { 627 dev_dbg(ms_dev(host), "switch clock failed\n"); 628 break; 629 } 630 631 host->ssc_depth = ssc_depth; 632 host->clock = clock; 633 host->ifmode = value; 634 break; 635 default: 636 err = -EINVAL; 637 break; 638 } 639 out: 640 mutex_unlock(&ucr->dev_mutex); 641 pm_runtime_put(ms_dev(host)); 642 643 /* power-on delay */ 644 if (param == MEMSTICK_POWER && value == MEMSTICK_POWER_ON) 645 usleep_range(10000, 12000); 646 647 dev_dbg(ms_dev(host), "%s: return = %d\n", __func__, err); 648 return err; 649 } 650 651 #ifdef CONFIG_PM_SLEEP 652 static int rtsx_usb_ms_suspend(struct device *dev) 653 { 654 struct rtsx_usb_ms *host = dev_get_drvdata(dev); 655 struct memstick_host *msh = host->msh; 656 657 dev_dbg(ms_dev(host), "--> %s\n", __func__); 658 659 memstick_suspend_host(msh); 660 return 0; 661 } 662 663 static int rtsx_usb_ms_resume(struct device *dev) 664 { 665 struct rtsx_usb_ms *host = dev_get_drvdata(dev); 666 struct memstick_host *msh = host->msh; 667 668 dev_dbg(ms_dev(host), "--> %s\n", __func__); 669 670 memstick_resume_host(msh); 671 return 0; 672 } 673 #endif /* CONFIG_PM_SLEEP */ 674 675 /* 676 * Thread function of ms card slot detection. The thread starts right after 677 * successful host addition. It stops while the driver removal function sets 678 * host->eject true. 679 */ 680 static int rtsx_usb_detect_ms_card(void *__host) 681 { 682 struct rtsx_usb_ms *host = (struct rtsx_usb_ms *)__host; 683 struct rtsx_ucr *ucr = host->ucr; 684 u8 val = 0; 685 int err; 686 687 for (;;) { 688 pm_runtime_get_sync(ms_dev(host)); 689 mutex_lock(&ucr->dev_mutex); 690 691 /* Check pending MS card changes */ 692 err = rtsx_usb_read_register(ucr, CARD_INT_PEND, &val); 693 if (err) { 694 mutex_unlock(&ucr->dev_mutex); 695 goto poll_again; 696 } 697 698 /* Clear the pending */ 699 rtsx_usb_write_register(ucr, CARD_INT_PEND, 700 XD_INT | MS_INT | SD_INT, 701 XD_INT | MS_INT | SD_INT); 702 703 mutex_unlock(&ucr->dev_mutex); 704 705 if (val & MS_INT) { 706 dev_dbg(ms_dev(host), "MS slot change detected\n"); 707 memstick_detect_change(host->msh); 708 } 709 710 poll_again: 711 pm_runtime_put(ms_dev(host)); 712 if (host->eject) 713 break; 714 715 schedule_timeout_idle(HZ); 716 } 717 718 complete(&host->detect_ms_exit); 719 return 0; 720 } 721 722 static int rtsx_usb_ms_drv_probe(struct platform_device *pdev) 723 { 724 struct memstick_host *msh; 725 struct rtsx_usb_ms *host; 726 struct rtsx_ucr *ucr; 727 int err; 728 729 ucr = usb_get_intfdata(to_usb_interface(pdev->dev.parent)); 730 if (!ucr) 731 return -ENXIO; 732 733 dev_dbg(&(pdev->dev), 734 "Realtek USB Memstick controller found\n"); 735 736 msh = memstick_alloc_host(sizeof(*host), &pdev->dev); 737 if (!msh) 738 return -ENOMEM; 739 740 host = memstick_priv(msh); 741 host->ucr = ucr; 742 host->msh = msh; 743 host->pdev = pdev; 744 host->power_mode = MEMSTICK_POWER_OFF; 745 platform_set_drvdata(pdev, host); 746 747 mutex_init(&host->host_mutex); 748 INIT_WORK(&host->handle_req, rtsx_usb_ms_handle_req); 749 750 init_completion(&host->detect_ms_exit); 751 host->detect_ms = kthread_create(rtsx_usb_detect_ms_card, host, 752 "rtsx_usb_ms_%d", pdev->id); 753 if (IS_ERR(host->detect_ms)) { 754 dev_dbg(&(pdev->dev), 755 "Unable to create polling thread.\n"); 756 err = PTR_ERR(host->detect_ms); 757 goto err_out; 758 } 759 760 msh->request = rtsx_usb_ms_request; 761 msh->set_param = rtsx_usb_ms_set_param; 762 msh->caps = MEMSTICK_CAP_PAR4; 763 764 pm_runtime_enable(&pdev->dev); 765 err = memstick_add_host(msh); 766 if (err) 767 goto err_out; 768 769 wake_up_process(host->detect_ms); 770 return 0; 771 err_out: 772 memstick_free_host(msh); 773 return err; 774 } 775 776 static int rtsx_usb_ms_drv_remove(struct platform_device *pdev) 777 { 778 struct rtsx_usb_ms *host = platform_get_drvdata(pdev); 779 struct memstick_host *msh; 780 int err; 781 782 msh = host->msh; 783 host->eject = true; 784 cancel_work_sync(&host->handle_req); 785 786 mutex_lock(&host->host_mutex); 787 if (host->req) { 788 dev_dbg(&(pdev->dev), 789 "%s: Controller removed during transfer\n", 790 dev_name(&msh->dev)); 791 host->req->error = -ENOMEDIUM; 792 do { 793 err = memstick_next_req(msh, &host->req); 794 if (!err) 795 host->req->error = -ENOMEDIUM; 796 } while (!err); 797 } 798 mutex_unlock(&host->host_mutex); 799 800 wait_for_completion(&host->detect_ms_exit); 801 memstick_remove_host(msh); 802 memstick_free_host(msh); 803 804 /* Balance possible unbalanced usage count 805 * e.g. unconditional module removal 806 */ 807 if (pm_runtime_active(ms_dev(host))) 808 pm_runtime_put(ms_dev(host)); 809 810 pm_runtime_disable(&pdev->dev); 811 platform_set_drvdata(pdev, NULL); 812 813 dev_dbg(&(pdev->dev), 814 ": Realtek USB Memstick controller has been removed\n"); 815 816 return 0; 817 } 818 819 static SIMPLE_DEV_PM_OPS(rtsx_usb_ms_pm_ops, 820 rtsx_usb_ms_suspend, rtsx_usb_ms_resume); 821 822 static struct platform_device_id rtsx_usb_ms_ids[] = { 823 { 824 .name = "rtsx_usb_ms", 825 }, { 826 /* sentinel */ 827 } 828 }; 829 MODULE_DEVICE_TABLE(platform, rtsx_usb_ms_ids); 830 831 static struct platform_driver rtsx_usb_ms_driver = { 832 .probe = rtsx_usb_ms_drv_probe, 833 .remove = rtsx_usb_ms_drv_remove, 834 .id_table = rtsx_usb_ms_ids, 835 .driver = { 836 .name = "rtsx_usb_ms", 837 .pm = &rtsx_usb_ms_pm_ops, 838 }, 839 }; 840 module_platform_driver(rtsx_usb_ms_driver); 841 842 MODULE_LICENSE("GPL v2"); 843 MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>"); 844 MODULE_DESCRIPTION("Realtek USB Memstick Card Host Driver"); 845