rtsx_pci_sdmmc.c 36 KB

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