tmio_mmc_pio.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /*
  2. * linux/drivers/mmc/host/tmio_mmc_pio.c
  3. *
  4. * Copyright (C) 2016 Sang Engineering, Wolfram Sang
  5. * Copyright (C) 2015-16 Renesas Electronics Corporation
  6. * Copyright (C) 2011 Guennadi Liakhovetski
  7. * Copyright (C) 2007 Ian Molton
  8. * Copyright (C) 2004 Ian Molton
  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 version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * Driver for the MMC / SD / SDIO IP found in:
  15. *
  16. * TC6393XB, TC6391XB, TC6387XB, T7L66XB, ASIC3, SH-Mobile SoCs
  17. *
  18. * This driver draws mainly on scattered spec sheets, Reverse engineering
  19. * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit
  20. * support). (Further 4 bit support from a later datasheet).
  21. *
  22. * TODO:
  23. * Investigate using a workqueue for PIO transfers
  24. * Eliminate FIXMEs
  25. * SDIO support
  26. * Better Power management
  27. * Handle MMC errors better
  28. * double buffer support
  29. *
  30. */
  31. #include <linux/delay.h>
  32. #include <linux/device.h>
  33. #include <linux/highmem.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/io.h>
  36. #include <linux/irq.h>
  37. #include <linux/mfd/tmio.h>
  38. #include <linux/mmc/host.h>
  39. #include <linux/mmc/mmc.h>
  40. #include <linux/mmc/slot-gpio.h>
  41. #include <linux/module.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/pm_qos.h>
  45. #include <linux/pm_runtime.h>
  46. #include <linux/regulator/consumer.h>
  47. #include <linux/mmc/sdio.h>
  48. #include <linux/scatterlist.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/workqueue.h>
  51. #include "tmio_mmc.h"
  52. void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
  53. {
  54. host->sdcard_irq_mask &= ~(i & TMIO_MASK_IRQ);
  55. sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
  56. }
  57. void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
  58. {
  59. host->sdcard_irq_mask |= (i & TMIO_MASK_IRQ);
  60. sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
  61. }
  62. static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i)
  63. {
  64. sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, ~i);
  65. }
  66. static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
  67. {
  68. host->sg_len = data->sg_len;
  69. host->sg_ptr = data->sg;
  70. host->sg_orig = data->sg;
  71. host->sg_off = 0;
  72. }
  73. static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
  74. {
  75. host->sg_ptr = sg_next(host->sg_ptr);
  76. host->sg_off = 0;
  77. return --host->sg_len;
  78. }
  79. #define CMDREQ_TIMEOUT 5000
  80. #ifdef CONFIG_MMC_DEBUG
  81. #define STATUS_TO_TEXT(a, status, i) \
  82. do { \
  83. if (status & TMIO_STAT_##a) { \
  84. if (i++) \
  85. printk(" | "); \
  86. printk(#a); \
  87. } \
  88. } while (0)
  89. static void pr_debug_status(u32 status)
  90. {
  91. int i = 0;
  92. pr_debug("status: %08x = ", status);
  93. STATUS_TO_TEXT(CARD_REMOVE, status, i);
  94. STATUS_TO_TEXT(CARD_INSERT, status, i);
  95. STATUS_TO_TEXT(SIGSTATE, status, i);
  96. STATUS_TO_TEXT(WRPROTECT, status, i);
  97. STATUS_TO_TEXT(CARD_REMOVE_A, status, i);
  98. STATUS_TO_TEXT(CARD_INSERT_A, status, i);
  99. STATUS_TO_TEXT(SIGSTATE_A, status, i);
  100. STATUS_TO_TEXT(CMD_IDX_ERR, status, i);
  101. STATUS_TO_TEXT(STOPBIT_ERR, status, i);
  102. STATUS_TO_TEXT(ILL_FUNC, status, i);
  103. STATUS_TO_TEXT(CMD_BUSY, status, i);
  104. STATUS_TO_TEXT(CMDRESPEND, status, i);
  105. STATUS_TO_TEXT(DATAEND, status, i);
  106. STATUS_TO_TEXT(CRCFAIL, status, i);
  107. STATUS_TO_TEXT(DATATIMEOUT, status, i);
  108. STATUS_TO_TEXT(CMDTIMEOUT, status, i);
  109. STATUS_TO_TEXT(RXOVERFLOW, status, i);
  110. STATUS_TO_TEXT(TXUNDERRUN, status, i);
  111. STATUS_TO_TEXT(RXRDY, status, i);
  112. STATUS_TO_TEXT(TXRQ, status, i);
  113. STATUS_TO_TEXT(ILL_ACCESS, status, i);
  114. printk("\n");
  115. }
  116. #else
  117. #define pr_debug_status(s) do { } while (0)
  118. #endif
  119. static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  120. {
  121. struct tmio_mmc_host *host = mmc_priv(mmc);
  122. if (enable && !host->sdio_irq_enabled) {
  123. /* Keep device active while SDIO irq is enabled */
  124. pm_runtime_get_sync(mmc_dev(mmc));
  125. host->sdio_irq_enabled = true;
  126. host->sdio_irq_mask = TMIO_SDIO_MASK_ALL &
  127. ~TMIO_SDIO_STAT_IOIRQ;
  128. sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
  129. sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
  130. } else if (!enable && host->sdio_irq_enabled) {
  131. host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
  132. sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
  133. sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
  134. host->sdio_irq_enabled = false;
  135. pm_runtime_mark_last_busy(mmc_dev(mmc));
  136. pm_runtime_put_autosuspend(mmc_dev(mmc));
  137. }
  138. }
  139. static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
  140. {
  141. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
  142. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  143. msleep(host->pdata->flags & TMIO_MMC_MIN_RCAR2 ? 1 : 10);
  144. if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG) {
  145. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
  146. msleep(10);
  147. }
  148. }
  149. static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
  150. {
  151. if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG) {
  152. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
  153. msleep(10);
  154. }
  155. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
  156. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  157. msleep(host->pdata->flags & TMIO_MMC_MIN_RCAR2 ? 5 : 10);
  158. }
  159. static void tmio_mmc_set_clock(struct tmio_mmc_host *host,
  160. unsigned int new_clock)
  161. {
  162. u32 clk = 0, clock;
  163. if (new_clock == 0) {
  164. tmio_mmc_clk_stop(host);
  165. return;
  166. }
  167. if (host->clk_update)
  168. clock = host->clk_update(host, new_clock) / 512;
  169. else
  170. clock = host->mmc->f_min;
  171. for (clk = 0x80000080; new_clock >= (clock << 1); clk >>= 1)
  172. clock <<= 1;
  173. /* 1/1 clock is option */
  174. if ((host->pdata->flags & TMIO_MMC_CLK_ACTUAL) && ((clk >> 22) & 0x1))
  175. clk |= 0xff;
  176. if (host->set_clk_div)
  177. host->set_clk_div(host->pdev, (clk >> 22) & 1);
  178. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
  179. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  180. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & CLK_CTL_DIV_MASK);
  181. if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
  182. msleep(10);
  183. tmio_mmc_clk_start(host);
  184. }
  185. static void tmio_mmc_reset(struct tmio_mmc_host *host)
  186. {
  187. /* FIXME - should we set stop clock reg here */
  188. sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
  189. if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG)
  190. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
  191. msleep(10);
  192. sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
  193. if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG)
  194. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
  195. msleep(10);
  196. }
  197. static void tmio_mmc_reset_work(struct work_struct *work)
  198. {
  199. struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
  200. delayed_reset_work.work);
  201. struct mmc_request *mrq;
  202. unsigned long flags;
  203. spin_lock_irqsave(&host->lock, flags);
  204. mrq = host->mrq;
  205. /*
  206. * is request already finished? Since we use a non-blocking
  207. * cancel_delayed_work(), it can happen, that a .set_ios() call preempts
  208. * us, so, have to check for IS_ERR(host->mrq)
  209. */
  210. if (IS_ERR_OR_NULL(mrq)
  211. || time_is_after_jiffies(host->last_req_ts +
  212. msecs_to_jiffies(CMDREQ_TIMEOUT))) {
  213. spin_unlock_irqrestore(&host->lock, flags);
  214. return;
  215. }
  216. dev_warn(&host->pdev->dev,
  217. "timeout waiting for hardware interrupt (CMD%u)\n",
  218. mrq->cmd->opcode);
  219. if (host->data)
  220. host->data->error = -ETIMEDOUT;
  221. else if (host->cmd)
  222. host->cmd->error = -ETIMEDOUT;
  223. else
  224. mrq->cmd->error = -ETIMEDOUT;
  225. host->cmd = NULL;
  226. host->data = NULL;
  227. host->force_pio = false;
  228. spin_unlock_irqrestore(&host->lock, flags);
  229. tmio_mmc_reset(host);
  230. /* Ready for new calls */
  231. host->mrq = NULL;
  232. tmio_mmc_abort_dma(host);
  233. mmc_request_done(host->mmc, mrq);
  234. }
  235. /* called with host->lock held, interrupts disabled */
  236. static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
  237. {
  238. struct mmc_request *mrq;
  239. unsigned long flags;
  240. spin_lock_irqsave(&host->lock, flags);
  241. mrq = host->mrq;
  242. if (IS_ERR_OR_NULL(mrq)) {
  243. spin_unlock_irqrestore(&host->lock, flags);
  244. return;
  245. }
  246. host->cmd = NULL;
  247. host->data = NULL;
  248. host->force_pio = false;
  249. cancel_delayed_work(&host->delayed_reset_work);
  250. host->mrq = NULL;
  251. spin_unlock_irqrestore(&host->lock, flags);
  252. if (mrq->cmd->error || (mrq->data && mrq->data->error))
  253. tmio_mmc_abort_dma(host);
  254. mmc_request_done(host->mmc, mrq);
  255. }
  256. static void tmio_mmc_done_work(struct work_struct *work)
  257. {
  258. struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
  259. done);
  260. tmio_mmc_finish_request(host);
  261. }
  262. /* These are the bitmasks the tmio chip requires to implement the MMC response
  263. * types. Note that R1 and R6 are the same in this scheme. */
  264. #define APP_CMD 0x0040
  265. #define RESP_NONE 0x0300
  266. #define RESP_R1 0x0400
  267. #define RESP_R1B 0x0500
  268. #define RESP_R2 0x0600
  269. #define RESP_R3 0x0700
  270. #define DATA_PRESENT 0x0800
  271. #define TRANSFER_READ 0x1000
  272. #define TRANSFER_MULTI 0x2000
  273. #define SECURITY_CMD 0x4000
  274. #define NO_CMD12_ISSUE 0x4000 /* TMIO_MMC_HAVE_CMD12_CTRL */
  275. static int tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
  276. {
  277. struct mmc_data *data = host->data;
  278. int c = cmd->opcode;
  279. u32 irq_mask = TMIO_MASK_CMD;
  280. /* CMD12 is handled by hardware */
  281. if (cmd->opcode == MMC_STOP_TRANSMISSION && !cmd->arg) {
  282. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
  283. return 0;
  284. }
  285. switch (mmc_resp_type(cmd)) {
  286. case MMC_RSP_NONE: c |= RESP_NONE; break;
  287. case MMC_RSP_R1:
  288. case MMC_RSP_R1_NO_CRC:
  289. c |= RESP_R1; break;
  290. case MMC_RSP_R1B: c |= RESP_R1B; break;
  291. case MMC_RSP_R2: c |= RESP_R2; break;
  292. case MMC_RSP_R3: c |= RESP_R3; break;
  293. default:
  294. pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
  295. return -EINVAL;
  296. }
  297. host->cmd = cmd;
  298. /* FIXME - this seems to be ok commented out but the spec suggest this bit
  299. * should be set when issuing app commands.
  300. * if(cmd->flags & MMC_FLAG_ACMD)
  301. * c |= APP_CMD;
  302. */
  303. if (data) {
  304. c |= DATA_PRESENT;
  305. if (data->blocks > 1) {
  306. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
  307. c |= TRANSFER_MULTI;
  308. /*
  309. * Disable auto CMD12 at IO_RW_EXTENDED when
  310. * multiple block transfer
  311. */
  312. if ((host->pdata->flags & TMIO_MMC_HAVE_CMD12_CTRL) &&
  313. (cmd->opcode == SD_IO_RW_EXTENDED))
  314. c |= NO_CMD12_ISSUE;
  315. }
  316. if (data->flags & MMC_DATA_READ)
  317. c |= TRANSFER_READ;
  318. }
  319. if (!host->native_hotplug)
  320. irq_mask &= ~(TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
  321. tmio_mmc_enable_mmc_irqs(host, irq_mask);
  322. /* Fire off the command */
  323. sd_ctrl_write32_as_16_and_16(host, CTL_ARG_REG, cmd->arg);
  324. sd_ctrl_write16(host, CTL_SD_CMD, c);
  325. return 0;
  326. }
  327. static void tmio_mmc_transfer_data(struct tmio_mmc_host *host,
  328. unsigned short *buf,
  329. unsigned int count)
  330. {
  331. int is_read = host->data->flags & MMC_DATA_READ;
  332. u8 *buf8;
  333. /*
  334. * Transfer the data
  335. */
  336. if (is_read)
  337. sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  338. else
  339. sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  340. /* if count was even number */
  341. if (!(count & 0x1))
  342. return;
  343. /* if count was odd number */
  344. buf8 = (u8 *)(buf + (count >> 1));
  345. /*
  346. * FIXME
  347. *
  348. * driver and this function are assuming that
  349. * it is used as little endian
  350. */
  351. if (is_read)
  352. *buf8 = sd_ctrl_read16(host, CTL_SD_DATA_PORT) & 0xff;
  353. else
  354. sd_ctrl_write16(host, CTL_SD_DATA_PORT, *buf8);
  355. }
  356. /*
  357. * This chip always returns (at least?) as much data as you ask for.
  358. * I'm unsure what happens if you ask for less than a block. This should be
  359. * looked into to ensure that a funny length read doesn't hose the controller.
  360. */
  361. static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
  362. {
  363. struct mmc_data *data = host->data;
  364. void *sg_virt;
  365. unsigned short *buf;
  366. unsigned int count;
  367. unsigned long flags;
  368. if ((host->chan_tx || host->chan_rx) && !host->force_pio) {
  369. pr_err("PIO IRQ in DMA mode!\n");
  370. return;
  371. } else if (!data) {
  372. pr_debug("Spurious PIO IRQ\n");
  373. return;
  374. }
  375. sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
  376. buf = (unsigned short *)(sg_virt + host->sg_off);
  377. count = host->sg_ptr->length - host->sg_off;
  378. if (count > data->blksz)
  379. count = data->blksz;
  380. pr_debug("count: %08x offset: %08x flags %08x\n",
  381. count, host->sg_off, data->flags);
  382. /* Transfer the data */
  383. tmio_mmc_transfer_data(host, buf, count);
  384. host->sg_off += count;
  385. tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
  386. if (host->sg_off == host->sg_ptr->length)
  387. tmio_mmc_next_sg(host);
  388. return;
  389. }
  390. static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
  391. {
  392. if (host->sg_ptr == &host->bounce_sg) {
  393. unsigned long flags;
  394. void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
  395. memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
  396. tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
  397. }
  398. }
  399. /* needs to be called with host->lock held */
  400. void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
  401. {
  402. struct mmc_data *data = host->data;
  403. struct mmc_command *stop;
  404. host->data = NULL;
  405. if (!data) {
  406. dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
  407. return;
  408. }
  409. stop = data->stop;
  410. /* FIXME - return correct transfer count on errors */
  411. if (!data->error)
  412. data->bytes_xfered = data->blocks * data->blksz;
  413. else
  414. data->bytes_xfered = 0;
  415. pr_debug("Completed data request\n");
  416. /*
  417. * FIXME: other drivers allow an optional stop command of any given type
  418. * which we dont do, as the chip can auto generate them.
  419. * Perhaps we can be smarter about when to use auto CMD12 and
  420. * only issue the auto request when we know this is the desired
  421. * stop command, allowing fallback to the stop command the
  422. * upper layers expect. For now, we do what works.
  423. */
  424. if (data->flags & MMC_DATA_READ) {
  425. if (host->chan_rx && !host->force_pio)
  426. tmio_mmc_check_bounce_buffer(host);
  427. dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
  428. host->mrq);
  429. } else {
  430. dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
  431. host->mrq);
  432. }
  433. if (stop) {
  434. if (stop->opcode == MMC_STOP_TRANSMISSION && !stop->arg)
  435. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
  436. else
  437. BUG();
  438. }
  439. schedule_work(&host->done);
  440. }
  441. static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
  442. {
  443. struct mmc_data *data;
  444. spin_lock(&host->lock);
  445. data = host->data;
  446. if (!data)
  447. goto out;
  448. if (host->chan_tx && (data->flags & MMC_DATA_WRITE) && !host->force_pio) {
  449. u32 status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
  450. bool done = false;
  451. /*
  452. * Has all data been written out yet? Testing on SuperH showed,
  453. * that in most cases the first interrupt comes already with the
  454. * BUSY status bit clear, but on some operations, like mount or
  455. * in the beginning of a write / sync / umount, there is one
  456. * DATAEND interrupt with the BUSY bit set, in this cases
  457. * waiting for one more interrupt fixes the problem.
  458. */
  459. if (host->pdata->flags & TMIO_MMC_HAS_IDLE_WAIT) {
  460. if (status & TMIO_STAT_SCLKDIVEN)
  461. done = true;
  462. } else {
  463. if (!(status & TMIO_STAT_CMD_BUSY))
  464. done = true;
  465. }
  466. if (done) {
  467. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  468. tasklet_schedule(&host->dma_complete);
  469. }
  470. } else if (host->chan_rx && (data->flags & MMC_DATA_READ) && !host->force_pio) {
  471. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  472. tasklet_schedule(&host->dma_complete);
  473. } else {
  474. tmio_mmc_do_data_irq(host);
  475. tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
  476. }
  477. out:
  478. spin_unlock(&host->lock);
  479. }
  480. static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
  481. unsigned int stat)
  482. {
  483. struct mmc_command *cmd = host->cmd;
  484. int i, addr;
  485. spin_lock(&host->lock);
  486. if (!host->cmd) {
  487. pr_debug("Spurious CMD irq\n");
  488. goto out;
  489. }
  490. host->cmd = NULL;
  491. /* This controller is sicker than the PXA one. Not only do we need to
  492. * drop the top 8 bits of the first response word, we also need to
  493. * modify the order of the response for short response command types.
  494. */
  495. for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
  496. cmd->resp[i] = sd_ctrl_read16_and_16_as_32(host, addr);
  497. if (cmd->flags & MMC_RSP_136) {
  498. cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
  499. cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
  500. cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
  501. cmd->resp[3] <<= 8;
  502. } else if (cmd->flags & MMC_RSP_R3) {
  503. cmd->resp[0] = cmd->resp[3];
  504. }
  505. if (stat & TMIO_STAT_CMDTIMEOUT)
  506. cmd->error = -ETIMEDOUT;
  507. else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
  508. cmd->error = -EILSEQ;
  509. /* If there is data to handle we enable data IRQs here, and
  510. * we will ultimatley finish the request in the data_end handler.
  511. * If theres no data or we encountered an error, finish now.
  512. */
  513. if (host->data && !cmd->error) {
  514. if (host->data->flags & MMC_DATA_READ) {
  515. if (host->force_pio || !host->chan_rx)
  516. tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
  517. else
  518. tasklet_schedule(&host->dma_issue);
  519. } else {
  520. if (host->force_pio || !host->chan_tx)
  521. tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
  522. else
  523. tasklet_schedule(&host->dma_issue);
  524. }
  525. } else {
  526. schedule_work(&host->done);
  527. }
  528. out:
  529. spin_unlock(&host->lock);
  530. }
  531. static bool __tmio_mmc_card_detect_irq(struct tmio_mmc_host *host,
  532. int ireg, int status)
  533. {
  534. struct mmc_host *mmc = host->mmc;
  535. /* Card insert / remove attempts */
  536. if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
  537. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
  538. TMIO_STAT_CARD_REMOVE);
  539. if ((((ireg & TMIO_STAT_CARD_REMOVE) && mmc->card) ||
  540. ((ireg & TMIO_STAT_CARD_INSERT) && !mmc->card)) &&
  541. !work_pending(&mmc->detect.work))
  542. mmc_detect_change(host->mmc, msecs_to_jiffies(100));
  543. return true;
  544. }
  545. return false;
  546. }
  547. static bool __tmio_mmc_sdcard_irq(struct tmio_mmc_host *host,
  548. int ireg, int status)
  549. {
  550. /* Command completion */
  551. if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
  552. tmio_mmc_ack_mmc_irqs(host,
  553. TMIO_STAT_CMDRESPEND |
  554. TMIO_STAT_CMDTIMEOUT);
  555. tmio_mmc_cmd_irq(host, status);
  556. return true;
  557. }
  558. /* Data transfer */
  559. if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
  560. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
  561. tmio_mmc_pio_irq(host);
  562. return true;
  563. }
  564. /* Data transfer completion */
  565. if (ireg & TMIO_STAT_DATAEND) {
  566. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
  567. tmio_mmc_data_irq(host);
  568. return true;
  569. }
  570. return false;
  571. }
  572. static void tmio_mmc_sdio_irq(int irq, void *devid)
  573. {
  574. struct tmio_mmc_host *host = devid;
  575. struct mmc_host *mmc = host->mmc;
  576. struct tmio_mmc_data *pdata = host->pdata;
  577. unsigned int ireg, status;
  578. unsigned int sdio_status;
  579. if (!(pdata->flags & TMIO_MMC_SDIO_IRQ))
  580. return;
  581. status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
  582. ireg = status & TMIO_SDIO_MASK_ALL & ~host->sdcard_irq_mask;
  583. sdio_status = status & ~TMIO_SDIO_MASK_ALL;
  584. if (pdata->flags & TMIO_MMC_SDIO_STATUS_QUIRK)
  585. sdio_status |= 6;
  586. sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status);
  587. if (mmc->caps & MMC_CAP_SDIO_IRQ && ireg & TMIO_SDIO_STAT_IOIRQ)
  588. mmc_signal_sdio_irq(mmc);
  589. }
  590. irqreturn_t tmio_mmc_irq(int irq, void *devid)
  591. {
  592. struct tmio_mmc_host *host = devid;
  593. unsigned int ireg, status;
  594. status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
  595. ireg = status & TMIO_MASK_IRQ & ~host->sdcard_irq_mask;
  596. pr_debug_status(status);
  597. pr_debug_status(ireg);
  598. /* Clear the status except the interrupt status */
  599. sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, TMIO_MASK_IRQ);
  600. if (__tmio_mmc_card_detect_irq(host, ireg, status))
  601. return IRQ_HANDLED;
  602. if (__tmio_mmc_sdcard_irq(host, ireg, status))
  603. return IRQ_HANDLED;
  604. tmio_mmc_sdio_irq(irq, devid);
  605. return IRQ_HANDLED;
  606. }
  607. EXPORT_SYMBOL(tmio_mmc_irq);
  608. static int tmio_mmc_start_data(struct tmio_mmc_host *host,
  609. struct mmc_data *data)
  610. {
  611. struct tmio_mmc_data *pdata = host->pdata;
  612. pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
  613. data->blksz, data->blocks);
  614. /* Some hardware cannot perform 2 byte requests in 4/8 bit mode */
  615. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4 ||
  616. host->mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
  617. int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
  618. if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
  619. pr_err("%s: %d byte block unsupported in 4/8 bit mode\n",
  620. mmc_hostname(host->mmc), data->blksz);
  621. return -EINVAL;
  622. }
  623. }
  624. tmio_mmc_init_sg(host, data);
  625. host->data = data;
  626. /* Set transfer length / blocksize */
  627. sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
  628. sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
  629. tmio_mmc_start_dma(host, data);
  630. return 0;
  631. }
  632. /* Process requests from the MMC layer */
  633. static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  634. {
  635. struct tmio_mmc_host *host = mmc_priv(mmc);
  636. unsigned long flags;
  637. int ret;
  638. spin_lock_irqsave(&host->lock, flags);
  639. if (host->mrq) {
  640. pr_debug("request not null\n");
  641. if (IS_ERR(host->mrq)) {
  642. spin_unlock_irqrestore(&host->lock, flags);
  643. mrq->cmd->error = -EAGAIN;
  644. mmc_request_done(mmc, mrq);
  645. return;
  646. }
  647. }
  648. host->last_req_ts = jiffies;
  649. wmb();
  650. host->mrq = mrq;
  651. spin_unlock_irqrestore(&host->lock, flags);
  652. if (mrq->data) {
  653. ret = tmio_mmc_start_data(host, mrq->data);
  654. if (ret)
  655. goto fail;
  656. }
  657. ret = tmio_mmc_start_command(host, mrq->cmd);
  658. if (!ret) {
  659. schedule_delayed_work(&host->delayed_reset_work,
  660. msecs_to_jiffies(CMDREQ_TIMEOUT));
  661. return;
  662. }
  663. fail:
  664. host->force_pio = false;
  665. host->mrq = NULL;
  666. mrq->cmd->error = ret;
  667. mmc_request_done(mmc, mrq);
  668. }
  669. static int tmio_mmc_clk_enable(struct tmio_mmc_host *host)
  670. {
  671. if (!host->clk_enable)
  672. return -ENOTSUPP;
  673. return host->clk_enable(host);
  674. }
  675. static void tmio_mmc_power_on(struct tmio_mmc_host *host, unsigned short vdd)
  676. {
  677. struct mmc_host *mmc = host->mmc;
  678. int ret = 0;
  679. /* .set_ios() is returning void, so, no chance to report an error */
  680. if (host->set_pwr)
  681. host->set_pwr(host->pdev, 1);
  682. if (!IS_ERR(mmc->supply.vmmc)) {
  683. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  684. /*
  685. * Attention: empiric value. With a b43 WiFi SDIO card this
  686. * delay proved necessary for reliable card-insertion probing.
  687. * 100us were not enough. Is this the same 140us delay, as in
  688. * tmio_mmc_set_ios()?
  689. */
  690. udelay(200);
  691. }
  692. /*
  693. * It seems, VccQ should be switched on after Vcc, this is also what the
  694. * omap_hsmmc.c driver does.
  695. */
  696. if (!IS_ERR(mmc->supply.vqmmc) && !ret) {
  697. ret = regulator_enable(mmc->supply.vqmmc);
  698. udelay(200);
  699. }
  700. if (ret < 0)
  701. dev_dbg(&host->pdev->dev, "Regulators failed to power up: %d\n",
  702. ret);
  703. }
  704. static void tmio_mmc_power_off(struct tmio_mmc_host *host)
  705. {
  706. struct mmc_host *mmc = host->mmc;
  707. if (!IS_ERR(mmc->supply.vqmmc))
  708. regulator_disable(mmc->supply.vqmmc);
  709. if (!IS_ERR(mmc->supply.vmmc))
  710. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  711. if (host->set_pwr)
  712. host->set_pwr(host->pdev, 0);
  713. }
  714. static void tmio_mmc_set_bus_width(struct tmio_mmc_host *host,
  715. unsigned char bus_width)
  716. {
  717. u16 reg = sd_ctrl_read16(host, CTL_SD_MEM_CARD_OPT)
  718. & ~(CARD_OPT_WIDTH | CARD_OPT_WIDTH8);
  719. /* reg now applies to MMC_BUS_WIDTH_4 */
  720. if (bus_width == MMC_BUS_WIDTH_1)
  721. reg |= CARD_OPT_WIDTH;
  722. else if (bus_width == MMC_BUS_WIDTH_8)
  723. reg |= CARD_OPT_WIDTH8;
  724. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, reg);
  725. }
  726. /* Set MMC clock / power.
  727. * Note: This controller uses a simple divider scheme therefore it cannot
  728. * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
  729. * MMC wont run that fast, it has to be clocked at 12MHz which is the next
  730. * slowest setting.
  731. */
  732. static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  733. {
  734. struct tmio_mmc_host *host = mmc_priv(mmc);
  735. struct device *dev = &host->pdev->dev;
  736. unsigned long flags;
  737. mutex_lock(&host->ios_lock);
  738. spin_lock_irqsave(&host->lock, flags);
  739. if (host->mrq) {
  740. if (IS_ERR(host->mrq)) {
  741. dev_dbg(dev,
  742. "%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
  743. current->comm, task_pid_nr(current),
  744. ios->clock, ios->power_mode);
  745. host->mrq = ERR_PTR(-EINTR);
  746. } else {
  747. dev_dbg(dev,
  748. "%s.%d: CMD%u active since %lu, now %lu!\n",
  749. current->comm, task_pid_nr(current),
  750. host->mrq->cmd->opcode, host->last_req_ts, jiffies);
  751. }
  752. spin_unlock_irqrestore(&host->lock, flags);
  753. mutex_unlock(&host->ios_lock);
  754. return;
  755. }
  756. host->mrq = ERR_PTR(-EBUSY);
  757. spin_unlock_irqrestore(&host->lock, flags);
  758. switch (ios->power_mode) {
  759. case MMC_POWER_OFF:
  760. tmio_mmc_power_off(host);
  761. tmio_mmc_clk_stop(host);
  762. break;
  763. case MMC_POWER_UP:
  764. tmio_mmc_power_on(host, ios->vdd);
  765. tmio_mmc_set_clock(host, ios->clock);
  766. tmio_mmc_set_bus_width(host, ios->bus_width);
  767. break;
  768. case MMC_POWER_ON:
  769. tmio_mmc_set_clock(host, ios->clock);
  770. tmio_mmc_set_bus_width(host, ios->bus_width);
  771. break;
  772. }
  773. /* Let things settle. delay taken from winCE driver */
  774. udelay(140);
  775. if (PTR_ERR(host->mrq) == -EINTR)
  776. dev_dbg(&host->pdev->dev,
  777. "%s.%d: IOS interrupted: clk %u, mode %u",
  778. current->comm, task_pid_nr(current),
  779. ios->clock, ios->power_mode);
  780. host->mrq = NULL;
  781. host->clk_cache = ios->clock;
  782. mutex_unlock(&host->ios_lock);
  783. }
  784. static int tmio_mmc_get_ro(struct mmc_host *mmc)
  785. {
  786. struct tmio_mmc_host *host = mmc_priv(mmc);
  787. struct tmio_mmc_data *pdata = host->pdata;
  788. int ret = mmc_gpio_get_ro(mmc);
  789. if (ret >= 0)
  790. return ret;
  791. ret = !((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
  792. (sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT));
  793. return ret;
  794. }
  795. static int tmio_multi_io_quirk(struct mmc_card *card,
  796. unsigned int direction, int blk_size)
  797. {
  798. struct tmio_mmc_host *host = mmc_priv(card->host);
  799. if (host->multi_io_quirk)
  800. return host->multi_io_quirk(card, direction, blk_size);
  801. return blk_size;
  802. }
  803. static struct mmc_host_ops tmio_mmc_ops = {
  804. .request = tmio_mmc_request,
  805. .set_ios = tmio_mmc_set_ios,
  806. .get_ro = tmio_mmc_get_ro,
  807. .get_cd = mmc_gpio_get_cd,
  808. .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
  809. .multi_io_quirk = tmio_multi_io_quirk,
  810. };
  811. static int tmio_mmc_init_ocr(struct tmio_mmc_host *host)
  812. {
  813. struct tmio_mmc_data *pdata = host->pdata;
  814. struct mmc_host *mmc = host->mmc;
  815. mmc_regulator_get_supply(mmc);
  816. /* use ocr_mask if no regulator */
  817. if (!mmc->ocr_avail)
  818. mmc->ocr_avail = pdata->ocr_mask;
  819. /*
  820. * try again.
  821. * There is possibility that regulator has not been probed
  822. */
  823. if (!mmc->ocr_avail)
  824. return -EPROBE_DEFER;
  825. return 0;
  826. }
  827. static void tmio_mmc_of_parse(struct platform_device *pdev,
  828. struct tmio_mmc_data *pdata)
  829. {
  830. const struct device_node *np = pdev->dev.of_node;
  831. if (!np)
  832. return;
  833. if (of_get_property(np, "toshiba,mmc-wrprotect-disable", NULL))
  834. pdata->flags |= TMIO_MMC_WRPROTECT_DISABLE;
  835. }
  836. struct tmio_mmc_host*
  837. tmio_mmc_host_alloc(struct platform_device *pdev)
  838. {
  839. struct tmio_mmc_host *host;
  840. struct mmc_host *mmc;
  841. mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
  842. if (!mmc)
  843. return NULL;
  844. host = mmc_priv(mmc);
  845. host->mmc = mmc;
  846. host->pdev = pdev;
  847. return host;
  848. }
  849. EXPORT_SYMBOL(tmio_mmc_host_alloc);
  850. void tmio_mmc_host_free(struct tmio_mmc_host *host)
  851. {
  852. mmc_free_host(host->mmc);
  853. }
  854. EXPORT_SYMBOL(tmio_mmc_host_free);
  855. int tmio_mmc_host_probe(struct tmio_mmc_host *_host,
  856. struct tmio_mmc_data *pdata)
  857. {
  858. struct platform_device *pdev = _host->pdev;
  859. struct mmc_host *mmc = _host->mmc;
  860. struct resource *res_ctl;
  861. int ret;
  862. u32 irq_mask = TMIO_MASK_CMD;
  863. tmio_mmc_of_parse(pdev, pdata);
  864. if (!(pdata->flags & TMIO_MMC_HAS_IDLE_WAIT))
  865. _host->write16_hook = NULL;
  866. res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  867. if (!res_ctl)
  868. return -EINVAL;
  869. ret = mmc_of_parse(mmc);
  870. if (ret < 0)
  871. goto host_free;
  872. _host->pdata = pdata;
  873. platform_set_drvdata(pdev, mmc);
  874. _host->set_pwr = pdata->set_pwr;
  875. _host->set_clk_div = pdata->set_clk_div;
  876. ret = tmio_mmc_init_ocr(_host);
  877. if (ret < 0)
  878. goto host_free;
  879. _host->ctl = devm_ioremap(&pdev->dev,
  880. res_ctl->start, resource_size(res_ctl));
  881. if (!_host->ctl) {
  882. ret = -ENOMEM;
  883. goto host_free;
  884. }
  885. tmio_mmc_ops.card_busy = _host->card_busy;
  886. tmio_mmc_ops.start_signal_voltage_switch = _host->start_signal_voltage_switch;
  887. mmc->ops = &tmio_mmc_ops;
  888. mmc->caps |= MMC_CAP_4_BIT_DATA | pdata->capabilities;
  889. mmc->caps2 |= pdata->capabilities2;
  890. mmc->max_segs = 32;
  891. mmc->max_blk_size = 512;
  892. mmc->max_blk_count = (PAGE_SIZE / mmc->max_blk_size) *
  893. mmc->max_segs;
  894. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  895. mmc->max_seg_size = mmc->max_req_size;
  896. _host->native_hotplug = !(pdata->flags & TMIO_MMC_USE_GPIO_CD ||
  897. mmc->caps & MMC_CAP_NEEDS_POLL ||
  898. !mmc_card_is_removable(mmc) ||
  899. mmc->slot.cd_irq >= 0);
  900. /*
  901. * On Gen2+, eMMC with NONREMOVABLE currently fails because native
  902. * hotplug gets disabled. It seems RuntimePM related yet we need further
  903. * research. Since we are planning a PM overhaul anyway, let's enforce
  904. * for now the device being active by enabling native hotplug always.
  905. */
  906. if (pdata->flags & TMIO_MMC_MIN_RCAR2)
  907. _host->native_hotplug = true;
  908. if (tmio_mmc_clk_enable(_host) < 0) {
  909. mmc->f_max = pdata->hclk;
  910. mmc->f_min = mmc->f_max / 512;
  911. }
  912. /*
  913. * Check the sanity of mmc->f_min to prevent tmio_mmc_set_clock() from
  914. * looping forever...
  915. */
  916. if (mmc->f_min == 0) {
  917. ret = -EINVAL;
  918. goto host_free;
  919. }
  920. /*
  921. * While using internal tmio hardware logic for card detection, we need
  922. * to ensure it stays powered for it to work.
  923. */
  924. if (_host->native_hotplug)
  925. pm_runtime_get_noresume(&pdev->dev);
  926. tmio_mmc_clk_stop(_host);
  927. tmio_mmc_reset(_host);
  928. _host->sdcard_irq_mask = sd_ctrl_read16_and_16_as_32(_host, CTL_IRQ_MASK);
  929. tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
  930. /* Unmask the IRQs we want to know about */
  931. if (!_host->chan_rx)
  932. irq_mask |= TMIO_MASK_READOP;
  933. if (!_host->chan_tx)
  934. irq_mask |= TMIO_MASK_WRITEOP;
  935. if (!_host->native_hotplug)
  936. irq_mask &= ~(TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
  937. _host->sdcard_irq_mask &= ~irq_mask;
  938. _host->sdio_irq_enabled = false;
  939. if (pdata->flags & TMIO_MMC_SDIO_IRQ) {
  940. _host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
  941. sd_ctrl_write16(_host, CTL_SDIO_IRQ_MASK, _host->sdio_irq_mask);
  942. sd_ctrl_write16(_host, CTL_TRANSACTION_CTL, 0x0000);
  943. }
  944. spin_lock_init(&_host->lock);
  945. mutex_init(&_host->ios_lock);
  946. /* Init delayed work for request timeouts */
  947. INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
  948. INIT_WORK(&_host->done, tmio_mmc_done_work);
  949. /* See if we also get DMA */
  950. tmio_mmc_request_dma(_host, pdata);
  951. pm_runtime_set_active(&pdev->dev);
  952. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  953. pm_runtime_use_autosuspend(&pdev->dev);
  954. pm_runtime_enable(&pdev->dev);
  955. ret = mmc_add_host(mmc);
  956. if (ret < 0) {
  957. tmio_mmc_host_remove(_host);
  958. return ret;
  959. }
  960. dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
  961. if (pdata->flags & TMIO_MMC_USE_GPIO_CD) {
  962. ret = mmc_gpio_request_cd(mmc, pdata->cd_gpio, 0);
  963. if (ret < 0) {
  964. tmio_mmc_host_remove(_host);
  965. return ret;
  966. }
  967. mmc_gpiod_request_cd_irq(mmc);
  968. }
  969. return 0;
  970. host_free:
  971. return ret;
  972. }
  973. EXPORT_SYMBOL(tmio_mmc_host_probe);
  974. void tmio_mmc_host_remove(struct tmio_mmc_host *host)
  975. {
  976. struct platform_device *pdev = host->pdev;
  977. struct mmc_host *mmc = host->mmc;
  978. if (!host->native_hotplug)
  979. pm_runtime_get_sync(&pdev->dev);
  980. dev_pm_qos_hide_latency_limit(&pdev->dev);
  981. mmc_remove_host(mmc);
  982. cancel_work_sync(&host->done);
  983. cancel_delayed_work_sync(&host->delayed_reset_work);
  984. tmio_mmc_release_dma(host);
  985. pm_runtime_put_sync(&pdev->dev);
  986. pm_runtime_disable(&pdev->dev);
  987. }
  988. EXPORT_SYMBOL(tmio_mmc_host_remove);
  989. #ifdef CONFIG_PM
  990. int tmio_mmc_host_runtime_suspend(struct device *dev)
  991. {
  992. struct mmc_host *mmc = dev_get_drvdata(dev);
  993. struct tmio_mmc_host *host = mmc_priv(mmc);
  994. tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
  995. if (host->clk_cache)
  996. tmio_mmc_clk_stop(host);
  997. if (host->clk_disable)
  998. host->clk_disable(host);
  999. return 0;
  1000. }
  1001. EXPORT_SYMBOL(tmio_mmc_host_runtime_suspend);
  1002. int tmio_mmc_host_runtime_resume(struct device *dev)
  1003. {
  1004. struct mmc_host *mmc = dev_get_drvdata(dev);
  1005. struct tmio_mmc_host *host = mmc_priv(mmc);
  1006. tmio_mmc_reset(host);
  1007. tmio_mmc_clk_enable(host);
  1008. if (host->clk_cache)
  1009. tmio_mmc_set_clock(host, host->clk_cache);
  1010. tmio_mmc_enable_dma(host, true);
  1011. return 0;
  1012. }
  1013. EXPORT_SYMBOL(tmio_mmc_host_runtime_resume);
  1014. #endif
  1015. MODULE_LICENSE("GPL v2");