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