toshsd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Toshiba PCI Secure Digital Host Controller Interface driver
  3. *
  4. * Copyright (C) 2014 Ondrej Zary
  5. * Copyright (C) 2007 Richard Betts, All Rights Reserved.
  6. *
  7. * Based on asic3_mmc.c, copyright (c) 2005 SDG Systems, LLC and,
  8. * sdhci.c, copyright (C) 2005-2006 Pierre Ossman
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/pm.h>
  23. #include <linux/mmc/host.h>
  24. #include <linux/mmc/mmc.h>
  25. #include "toshsd.h"
  26. #define DRIVER_NAME "toshsd"
  27. static const struct pci_device_id pci_ids[] = {
  28. { PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA, 0x0805) },
  29. { /* end: all zeroes */ },
  30. };
  31. MODULE_DEVICE_TABLE(pci, pci_ids);
  32. static void toshsd_init(struct toshsd_host *host)
  33. {
  34. /* enable clock */
  35. pci_write_config_byte(host->pdev, SD_PCICFG_CLKSTOP,
  36. SD_PCICFG_CLKSTOP_ENABLE_ALL);
  37. pci_write_config_byte(host->pdev, SD_PCICFG_CARDDETECT, 2);
  38. /* reset */
  39. iowrite16(0, host->ioaddr + SD_SOFTWARERESET); /* assert */
  40. mdelay(2);
  41. iowrite16(1, host->ioaddr + SD_SOFTWARERESET); /* deassert */
  42. mdelay(2);
  43. /* Clear card registers */
  44. iowrite16(0, host->ioaddr + SD_CARDCLOCKCTRL);
  45. iowrite32(0, host->ioaddr + SD_CARDSTATUS);
  46. iowrite32(0, host->ioaddr + SD_ERRORSTATUS0);
  47. iowrite16(0, host->ioaddr + SD_STOPINTERNAL);
  48. /* SDIO clock? */
  49. iowrite16(0x100, host->ioaddr + SDIO_BASE + SDIO_CLOCKNWAITCTRL);
  50. /* enable LED */
  51. pci_write_config_byte(host->pdev, SD_PCICFG_SDLED_ENABLE1,
  52. SD_PCICFG_LED_ENABLE1_START);
  53. pci_write_config_byte(host->pdev, SD_PCICFG_SDLED_ENABLE2,
  54. SD_PCICFG_LED_ENABLE2_START);
  55. /* set interrupt masks */
  56. iowrite32(~(u32)(SD_CARD_RESP_END | SD_CARD_RW_END
  57. | SD_CARD_CARD_REMOVED_0 | SD_CARD_CARD_INSERTED_0
  58. | SD_BUF_READ_ENABLE | SD_BUF_WRITE_ENABLE
  59. | SD_BUF_CMD_TIMEOUT),
  60. host->ioaddr + SD_INTMASKCARD);
  61. iowrite16(0x1000, host->ioaddr + SD_TRANSACTIONCTRL);
  62. }
  63. /* Set MMC clock / power.
  64. * Note: This controller uses a simple divider scheme therefore it cannot run
  65. * SD/MMC cards at full speed (24/20MHz). HCLK (=33MHz PCI clock?) is too high
  66. * and the next slowest is 16MHz (div=2).
  67. */
  68. static void __toshsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  69. {
  70. struct toshsd_host *host = mmc_priv(mmc);
  71. if (ios->clock) {
  72. u16 clk;
  73. int div = 1;
  74. while (ios->clock < HCLK / div)
  75. div *= 2;
  76. clk = div >> 2;
  77. if (div == 1) { /* disable the divider */
  78. pci_write_config_byte(host->pdev, SD_PCICFG_CLKMODE,
  79. SD_PCICFG_CLKMODE_DIV_DISABLE);
  80. clk |= SD_CARDCLK_DIV_DISABLE;
  81. } else
  82. pci_write_config_byte(host->pdev, SD_PCICFG_CLKMODE, 0);
  83. clk |= SD_CARDCLK_ENABLE_CLOCK;
  84. iowrite16(clk, host->ioaddr + SD_CARDCLOCKCTRL);
  85. mdelay(10);
  86. } else
  87. iowrite16(0, host->ioaddr + SD_CARDCLOCKCTRL);
  88. switch (ios->power_mode) {
  89. case MMC_POWER_OFF:
  90. pci_write_config_byte(host->pdev, SD_PCICFG_POWER1,
  91. SD_PCICFG_PWR1_OFF);
  92. mdelay(1);
  93. break;
  94. case MMC_POWER_UP:
  95. break;
  96. case MMC_POWER_ON:
  97. pci_write_config_byte(host->pdev, SD_PCICFG_POWER1,
  98. SD_PCICFG_PWR1_33V);
  99. pci_write_config_byte(host->pdev, SD_PCICFG_POWER2,
  100. SD_PCICFG_PWR2_AUTO);
  101. mdelay(20);
  102. break;
  103. }
  104. switch (ios->bus_width) {
  105. case MMC_BUS_WIDTH_1:
  106. iowrite16(SD_CARDOPT_REQUIRED | SD_CARDOPT_DATA_RESP_TIMEOUT(14)
  107. | SD_CARDOPT_C2_MODULE_ABSENT
  108. | SD_CARDOPT_DATA_XFR_WIDTH_1,
  109. host->ioaddr + SD_CARDOPTIONSETUP);
  110. break;
  111. case MMC_BUS_WIDTH_4:
  112. iowrite16(SD_CARDOPT_REQUIRED | SD_CARDOPT_DATA_RESP_TIMEOUT(14)
  113. | SD_CARDOPT_C2_MODULE_ABSENT
  114. | SD_CARDOPT_DATA_XFR_WIDTH_4,
  115. host->ioaddr + SD_CARDOPTIONSETUP);
  116. break;
  117. }
  118. }
  119. static void toshsd_set_led(struct toshsd_host *host, unsigned char state)
  120. {
  121. iowrite16(state, host->ioaddr + SDIO_BASE + SDIO_LEDCTRL);
  122. }
  123. static void toshsd_finish_request(struct toshsd_host *host)
  124. {
  125. struct mmc_request *mrq = host->mrq;
  126. /* Write something to end the command */
  127. host->mrq = NULL;
  128. host->cmd = NULL;
  129. host->data = NULL;
  130. toshsd_set_led(host, 0);
  131. mmc_request_done(host->mmc, mrq);
  132. }
  133. static irqreturn_t toshsd_thread_irq(int irq, void *dev_id)
  134. {
  135. struct toshsd_host *host = dev_id;
  136. struct mmc_data *data = host->data;
  137. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  138. unsigned short *buf;
  139. int count;
  140. unsigned long flags;
  141. if (!data) {
  142. dev_warn(&host->pdev->dev, "Spurious Data IRQ\n");
  143. if (host->cmd) {
  144. host->cmd->error = -EIO;
  145. toshsd_finish_request(host);
  146. }
  147. return IRQ_NONE;
  148. }
  149. spin_lock_irqsave(&host->lock, flags);
  150. if (!sg_miter_next(sg_miter))
  151. goto done;
  152. buf = sg_miter->addr;
  153. /* Ensure we dont read more than one block. The chip will interrupt us
  154. * When the next block is available.
  155. */
  156. count = sg_miter->length;
  157. if (count > data->blksz)
  158. count = data->blksz;
  159. dev_dbg(&host->pdev->dev, "count: %08x, flags %08x\n", count,
  160. data->flags);
  161. /* Transfer the data */
  162. if (data->flags & MMC_DATA_READ)
  163. ioread32_rep(host->ioaddr + SD_DATAPORT, buf, count >> 2);
  164. else
  165. iowrite32_rep(host->ioaddr + SD_DATAPORT, buf, count >> 2);
  166. sg_miter->consumed = count;
  167. sg_miter_stop(sg_miter);
  168. done:
  169. spin_unlock_irqrestore(&host->lock, flags);
  170. return IRQ_HANDLED;
  171. }
  172. static void toshsd_cmd_irq(struct toshsd_host *host)
  173. {
  174. struct mmc_command *cmd = host->cmd;
  175. u8 *buf;
  176. u16 data;
  177. if (!host->cmd) {
  178. dev_warn(&host->pdev->dev, "Spurious CMD irq\n");
  179. return;
  180. }
  181. buf = (u8 *)cmd->resp;
  182. host->cmd = NULL;
  183. if (cmd->flags & MMC_RSP_PRESENT && cmd->flags & MMC_RSP_136) {
  184. /* R2 */
  185. buf[12] = 0xff;
  186. data = ioread16(host->ioaddr + SD_RESPONSE0);
  187. buf[13] = data & 0xff;
  188. buf[14] = data >> 8;
  189. data = ioread16(host->ioaddr + SD_RESPONSE1);
  190. buf[15] = data & 0xff;
  191. buf[8] = data >> 8;
  192. data = ioread16(host->ioaddr + SD_RESPONSE2);
  193. buf[9] = data & 0xff;
  194. buf[10] = data >> 8;
  195. data = ioread16(host->ioaddr + SD_RESPONSE3);
  196. buf[11] = data & 0xff;
  197. buf[4] = data >> 8;
  198. data = ioread16(host->ioaddr + SD_RESPONSE4);
  199. buf[5] = data & 0xff;
  200. buf[6] = data >> 8;
  201. data = ioread16(host->ioaddr + SD_RESPONSE5);
  202. buf[7] = data & 0xff;
  203. buf[0] = data >> 8;
  204. data = ioread16(host->ioaddr + SD_RESPONSE6);
  205. buf[1] = data & 0xff;
  206. buf[2] = data >> 8;
  207. data = ioread16(host->ioaddr + SD_RESPONSE7);
  208. buf[3] = data & 0xff;
  209. } else if (cmd->flags & MMC_RSP_PRESENT) {
  210. /* R1, R1B, R3, R6, R7 */
  211. data = ioread16(host->ioaddr + SD_RESPONSE0);
  212. buf[0] = data & 0xff;
  213. buf[1] = data >> 8;
  214. data = ioread16(host->ioaddr + SD_RESPONSE1);
  215. buf[2] = data & 0xff;
  216. buf[3] = data >> 8;
  217. }
  218. dev_dbg(&host->pdev->dev, "Command IRQ complete %d %d %x\n",
  219. cmd->opcode, cmd->error, cmd->flags);
  220. /* If there is data to handle we will
  221. * finish the request in the mmc_data_end_irq handler.*/
  222. if (host->data)
  223. return;
  224. toshsd_finish_request(host);
  225. }
  226. static void toshsd_data_end_irq(struct toshsd_host *host)
  227. {
  228. struct mmc_data *data = host->data;
  229. host->data = NULL;
  230. if (!data) {
  231. dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
  232. return;
  233. }
  234. if (data->error == 0)
  235. data->bytes_xfered = data->blocks * data->blksz;
  236. else
  237. data->bytes_xfered = 0;
  238. dev_dbg(&host->pdev->dev, "Completed data request xfr=%d\n",
  239. data->bytes_xfered);
  240. iowrite16(0, host->ioaddr + SD_STOPINTERNAL);
  241. toshsd_finish_request(host);
  242. }
  243. static irqreturn_t toshsd_irq(int irq, void *dev_id)
  244. {
  245. struct toshsd_host *host = dev_id;
  246. u32 int_reg, int_mask, int_status, detail;
  247. int error = 0, ret = IRQ_HANDLED;
  248. spin_lock(&host->lock);
  249. int_status = ioread32(host->ioaddr + SD_CARDSTATUS);
  250. int_mask = ioread32(host->ioaddr + SD_INTMASKCARD);
  251. int_reg = int_status & ~int_mask & ~IRQ_DONT_CARE_BITS;
  252. dev_dbg(&host->pdev->dev, "IRQ status:%x mask:%x\n",
  253. int_status, int_mask);
  254. /* nothing to do: it's not our IRQ */
  255. if (!int_reg) {
  256. ret = IRQ_NONE;
  257. goto irq_end;
  258. }
  259. if (int_reg & SD_BUF_CMD_TIMEOUT) {
  260. error = -ETIMEDOUT;
  261. dev_dbg(&host->pdev->dev, "Timeout\n");
  262. } else if (int_reg & SD_BUF_CRC_ERR) {
  263. error = -EILSEQ;
  264. dev_err(&host->pdev->dev, "BadCRC\n");
  265. } else if (int_reg & (SD_BUF_ILLEGAL_ACCESS
  266. | SD_BUF_CMD_INDEX_ERR
  267. | SD_BUF_STOP_BIT_END_ERR
  268. | SD_BUF_OVERFLOW
  269. | SD_BUF_UNDERFLOW
  270. | SD_BUF_DATA_TIMEOUT)) {
  271. dev_err(&host->pdev->dev, "Buffer status error: { %s%s%s%s%s%s}\n",
  272. int_reg & SD_BUF_ILLEGAL_ACCESS ? "ILLEGAL_ACC " : "",
  273. int_reg & SD_BUF_CMD_INDEX_ERR ? "CMD_INDEX " : "",
  274. int_reg & SD_BUF_STOP_BIT_END_ERR ? "STOPBIT_END " : "",
  275. int_reg & SD_BUF_OVERFLOW ? "OVERFLOW " : "",
  276. int_reg & SD_BUF_UNDERFLOW ? "UNDERFLOW " : "",
  277. int_reg & SD_BUF_DATA_TIMEOUT ? "DATA_TIMEOUT " : "");
  278. detail = ioread32(host->ioaddr + SD_ERRORSTATUS0);
  279. dev_err(&host->pdev->dev, "detail error status { %s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
  280. detail & SD_ERR0_RESP_CMD_ERR ? "RESP_CMD " : "",
  281. detail & SD_ERR0_RESP_NON_CMD12_END_BIT_ERR ? "RESP_END_BIT " : "",
  282. detail & SD_ERR0_RESP_CMD12_END_BIT_ERR ? "RESP_END_BIT " : "",
  283. detail & SD_ERR0_READ_DATA_END_BIT_ERR ? "READ_DATA_END_BIT " : "",
  284. detail & SD_ERR0_WRITE_CRC_STATUS_END_BIT_ERR ? "WRITE_CMD_END_BIT " : "",
  285. detail & SD_ERR0_RESP_NON_CMD12_CRC_ERR ? "RESP_CRC " : "",
  286. detail & SD_ERR0_RESP_CMD12_CRC_ERR ? "RESP_CRC " : "",
  287. detail & SD_ERR0_READ_DATA_CRC_ERR ? "READ_DATA_CRC " : "",
  288. detail & SD_ERR0_WRITE_CMD_CRC_ERR ? "WRITE_CMD_CRC " : "",
  289. detail & SD_ERR1_NO_CMD_RESP ? "NO_CMD_RESP " : "",
  290. detail & SD_ERR1_TIMEOUT_READ_DATA ? "READ_DATA_TIMEOUT " : "",
  291. detail & SD_ERR1_TIMEOUT_CRS_STATUS ? "CRS_STATUS_TIMEOUT " : "",
  292. detail & SD_ERR1_TIMEOUT_CRC_BUSY ? "CRC_BUSY_TIMEOUT " : "");
  293. error = -EIO;
  294. }
  295. if (error) {
  296. if (host->cmd)
  297. host->cmd->error = error;
  298. if (error == -ETIMEDOUT) {
  299. iowrite32(int_status &
  300. ~(SD_BUF_CMD_TIMEOUT | SD_CARD_RESP_END),
  301. host->ioaddr + SD_CARDSTATUS);
  302. } else {
  303. toshsd_init(host);
  304. __toshsd_set_ios(host->mmc, &host->mmc->ios);
  305. goto irq_end;
  306. }
  307. }
  308. /* Card insert/remove. The mmc controlling code is stateless. */
  309. if (int_reg & (SD_CARD_CARD_INSERTED_0 | SD_CARD_CARD_REMOVED_0)) {
  310. iowrite32(int_status &
  311. ~(SD_CARD_CARD_REMOVED_0 | SD_CARD_CARD_INSERTED_0),
  312. host->ioaddr + SD_CARDSTATUS);
  313. if (int_reg & SD_CARD_CARD_INSERTED_0)
  314. toshsd_init(host);
  315. mmc_detect_change(host->mmc, 1);
  316. }
  317. /* Data transfer */
  318. if (int_reg & (SD_BUF_READ_ENABLE | SD_BUF_WRITE_ENABLE)) {
  319. iowrite32(int_status &
  320. ~(SD_BUF_WRITE_ENABLE | SD_BUF_READ_ENABLE),
  321. host->ioaddr + SD_CARDSTATUS);
  322. ret = IRQ_WAKE_THREAD;
  323. goto irq_end;
  324. }
  325. /* Command completion */
  326. if (int_reg & SD_CARD_RESP_END) {
  327. iowrite32(int_status & ~(SD_CARD_RESP_END),
  328. host->ioaddr + SD_CARDSTATUS);
  329. toshsd_cmd_irq(host);
  330. }
  331. /* Data transfer completion */
  332. if (int_reg & SD_CARD_RW_END) {
  333. iowrite32(int_status & ~(SD_CARD_RW_END),
  334. host->ioaddr + SD_CARDSTATUS);
  335. toshsd_data_end_irq(host);
  336. }
  337. irq_end:
  338. spin_unlock(&host->lock);
  339. return ret;
  340. }
  341. static void toshsd_start_cmd(struct toshsd_host *host, struct mmc_command *cmd)
  342. {
  343. struct mmc_data *data = host->data;
  344. int c = cmd->opcode;
  345. dev_dbg(&host->pdev->dev, "Command opcode: %d\n", cmd->opcode);
  346. if (cmd->opcode == MMC_STOP_TRANSMISSION) {
  347. iowrite16(SD_STOPINT_ISSUE_CMD12,
  348. host->ioaddr + SD_STOPINTERNAL);
  349. cmd->resp[0] = cmd->opcode;
  350. cmd->resp[1] = 0;
  351. cmd->resp[2] = 0;
  352. cmd->resp[3] = 0;
  353. toshsd_finish_request(host);
  354. return;
  355. }
  356. switch (mmc_resp_type(cmd)) {
  357. case MMC_RSP_NONE:
  358. c |= SD_CMD_RESP_TYPE_NONE;
  359. break;
  360. case MMC_RSP_R1:
  361. c |= SD_CMD_RESP_TYPE_EXT_R1;
  362. break;
  363. case MMC_RSP_R1B:
  364. c |= SD_CMD_RESP_TYPE_EXT_R1B;
  365. break;
  366. case MMC_RSP_R2:
  367. c |= SD_CMD_RESP_TYPE_EXT_R2;
  368. break;
  369. case MMC_RSP_R3:
  370. c |= SD_CMD_RESP_TYPE_EXT_R3;
  371. break;
  372. default:
  373. dev_err(&host->pdev->dev, "Unknown response type %d\n",
  374. mmc_resp_type(cmd));
  375. break;
  376. }
  377. host->cmd = cmd;
  378. if (cmd->opcode == MMC_APP_CMD)
  379. c |= SD_CMD_TYPE_ACMD;
  380. if (cmd->opcode == MMC_GO_IDLE_STATE)
  381. c |= (3 << 8); /* removed from ipaq-asic3.h for some reason */
  382. if (data) {
  383. c |= SD_CMD_DATA_PRESENT;
  384. if (data->blocks > 1) {
  385. iowrite16(SD_STOPINT_AUTO_ISSUE_CMD12,
  386. host->ioaddr + SD_STOPINTERNAL);
  387. c |= SD_CMD_MULTI_BLOCK;
  388. }
  389. if (data->flags & MMC_DATA_READ)
  390. c |= SD_CMD_TRANSFER_READ;
  391. /* MMC_DATA_WRITE does not require a bit to be set */
  392. }
  393. /* Send the command */
  394. iowrite32(cmd->arg, host->ioaddr + SD_ARG0);
  395. iowrite16(c, host->ioaddr + SD_CMD);
  396. }
  397. static void toshsd_start_data(struct toshsd_host *host, struct mmc_data *data)
  398. {
  399. unsigned int flags = SG_MITER_ATOMIC;
  400. dev_dbg(&host->pdev->dev, "setup data transfer: blocksize %08x nr_blocks %d, offset: %08x\n",
  401. data->blksz, data->blocks, data->sg->offset);
  402. host->data = data;
  403. if (data->flags & MMC_DATA_READ)
  404. flags |= SG_MITER_TO_SG;
  405. else
  406. flags |= SG_MITER_FROM_SG;
  407. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  408. /* Set transfer length and blocksize */
  409. iowrite16(data->blocks, host->ioaddr + SD_BLOCKCOUNT);
  410. iowrite16(data->blksz, host->ioaddr + SD_CARDXFERDATALEN);
  411. }
  412. /* Process requests from the MMC layer */
  413. static void toshsd_request(struct mmc_host *mmc, struct mmc_request *mrq)
  414. {
  415. struct toshsd_host *host = mmc_priv(mmc);
  416. unsigned long flags;
  417. /* abort if card not present */
  418. if (!(ioread16(host->ioaddr + SD_CARDSTATUS) & SD_CARD_PRESENT_0)) {
  419. mrq->cmd->error = -ENOMEDIUM;
  420. mmc_request_done(mmc, mrq);
  421. return;
  422. }
  423. spin_lock_irqsave(&host->lock, flags);
  424. WARN_ON(host->mrq != NULL);
  425. host->mrq = mrq;
  426. if (mrq->data)
  427. toshsd_start_data(host, mrq->data);
  428. toshsd_set_led(host, 1);
  429. toshsd_start_cmd(host, mrq->cmd);
  430. spin_unlock_irqrestore(&host->lock, flags);
  431. }
  432. static void toshsd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  433. {
  434. struct toshsd_host *host = mmc_priv(mmc);
  435. unsigned long flags;
  436. spin_lock_irqsave(&host->lock, flags);
  437. __toshsd_set_ios(mmc, ios);
  438. spin_unlock_irqrestore(&host->lock, flags);
  439. }
  440. static int toshsd_get_ro(struct mmc_host *mmc)
  441. {
  442. struct toshsd_host *host = mmc_priv(mmc);
  443. /* active low */
  444. return !(ioread16(host->ioaddr + SD_CARDSTATUS) & SD_CARD_WRITE_PROTECT);
  445. }
  446. static int toshsd_get_cd(struct mmc_host *mmc)
  447. {
  448. struct toshsd_host *host = mmc_priv(mmc);
  449. return !!(ioread16(host->ioaddr + SD_CARDSTATUS) & SD_CARD_PRESENT_0);
  450. }
  451. static struct mmc_host_ops toshsd_ops = {
  452. .request = toshsd_request,
  453. .set_ios = toshsd_set_ios,
  454. .get_ro = toshsd_get_ro,
  455. .get_cd = toshsd_get_cd,
  456. };
  457. static void toshsd_powerdown(struct toshsd_host *host)
  458. {
  459. /* mask all interrupts */
  460. iowrite32(0xffffffff, host->ioaddr + SD_INTMASKCARD);
  461. /* disable card clock */
  462. iowrite16(0x000, host->ioaddr + SDIO_BASE + SDIO_CLOCKNWAITCTRL);
  463. iowrite16(0, host->ioaddr + SD_CARDCLOCKCTRL);
  464. /* power down card */
  465. pci_write_config_byte(host->pdev, SD_PCICFG_POWER1, SD_PCICFG_PWR1_OFF);
  466. /* disable clock */
  467. pci_write_config_byte(host->pdev, SD_PCICFG_CLKSTOP, 0);
  468. }
  469. #ifdef CONFIG_PM_SLEEP
  470. static int toshsd_pm_suspend(struct device *dev)
  471. {
  472. struct pci_dev *pdev = to_pci_dev(dev);
  473. struct toshsd_host *host = pci_get_drvdata(pdev);
  474. toshsd_powerdown(host);
  475. pci_save_state(pdev);
  476. pci_enable_wake(pdev, PCI_D3hot, 0);
  477. pci_disable_device(pdev);
  478. pci_set_power_state(pdev, PCI_D3hot);
  479. return 0;
  480. }
  481. static int toshsd_pm_resume(struct device *dev)
  482. {
  483. struct pci_dev *pdev = to_pci_dev(dev);
  484. struct toshsd_host *host = pci_get_drvdata(pdev);
  485. int ret;
  486. pci_set_power_state(pdev, PCI_D0);
  487. pci_restore_state(pdev);
  488. ret = pci_enable_device(pdev);
  489. if (ret)
  490. return ret;
  491. toshsd_init(host);
  492. return 0;
  493. }
  494. #endif /* CONFIG_PM_SLEEP */
  495. static int toshsd_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  496. {
  497. int ret;
  498. struct toshsd_host *host;
  499. struct mmc_host *mmc;
  500. resource_size_t base;
  501. ret = pci_enable_device(pdev);
  502. if (ret)
  503. return ret;
  504. mmc = mmc_alloc_host(sizeof(struct toshsd_host), &pdev->dev);
  505. if (!mmc) {
  506. ret = -ENOMEM;
  507. goto err;
  508. }
  509. host = mmc_priv(mmc);
  510. host->mmc = mmc;
  511. host->pdev = pdev;
  512. pci_set_drvdata(pdev, host);
  513. ret = pci_request_regions(pdev, DRIVER_NAME);
  514. if (ret)
  515. goto free;
  516. host->ioaddr = pci_iomap(pdev, 0, 0);
  517. if (!host->ioaddr) {
  518. ret = -ENOMEM;
  519. goto release;
  520. }
  521. /* Set MMC host parameters */
  522. mmc->ops = &toshsd_ops;
  523. mmc->caps = MMC_CAP_4_BIT_DATA;
  524. mmc->ocr_avail = MMC_VDD_32_33;
  525. mmc->f_min = HCLK / 512;
  526. mmc->f_max = HCLK;
  527. spin_lock_init(&host->lock);
  528. toshsd_init(host);
  529. ret = request_threaded_irq(pdev->irq, toshsd_irq, toshsd_thread_irq,
  530. IRQF_SHARED, DRIVER_NAME, host);
  531. if (ret)
  532. goto unmap;
  533. mmc_add_host(mmc);
  534. base = pci_resource_start(pdev, 0);
  535. dev_dbg(&pdev->dev, "MMIO %pa, IRQ %d\n", &base, pdev->irq);
  536. pm_suspend_ignore_children(&pdev->dev, 1);
  537. return 0;
  538. unmap:
  539. pci_iounmap(pdev, host->ioaddr);
  540. release:
  541. pci_release_regions(pdev);
  542. free:
  543. mmc_free_host(mmc);
  544. pci_set_drvdata(pdev, NULL);
  545. err:
  546. pci_disable_device(pdev);
  547. return ret;
  548. }
  549. static void toshsd_remove(struct pci_dev *pdev)
  550. {
  551. struct toshsd_host *host = pci_get_drvdata(pdev);
  552. mmc_remove_host(host->mmc);
  553. toshsd_powerdown(host);
  554. free_irq(pdev->irq, host);
  555. pci_iounmap(pdev, host->ioaddr);
  556. pci_release_regions(pdev);
  557. mmc_free_host(host->mmc);
  558. pci_set_drvdata(pdev, NULL);
  559. pci_disable_device(pdev);
  560. }
  561. static const struct dev_pm_ops toshsd_pm_ops = {
  562. SET_SYSTEM_SLEEP_PM_OPS(toshsd_pm_suspend, toshsd_pm_resume)
  563. };
  564. static struct pci_driver toshsd_driver = {
  565. .name = DRIVER_NAME,
  566. .id_table = pci_ids,
  567. .probe = toshsd_probe,
  568. .remove = toshsd_remove,
  569. .driver.pm = &toshsd_pm_ops,
  570. };
  571. module_pci_driver(toshsd_driver);
  572. MODULE_AUTHOR("Ondrej Zary, Richard Betts");
  573. MODULE_DESCRIPTION("Toshiba PCI Secure Digital Host Controller Interface driver");
  574. MODULE_LICENSE("GPL");