tmio_mmc_pio.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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: c |= RESP_R1; break;
  288. case MMC_RSP_R1B: c |= RESP_R1B; break;
  289. case MMC_RSP_R2: c |= RESP_R2; break;
  290. case MMC_RSP_R3: c |= RESP_R3; break;
  291. default:
  292. pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
  293. return -EINVAL;
  294. }
  295. host->cmd = cmd;
  296. /* FIXME - this seems to be ok commented out but the spec suggest this bit
  297. * should be set when issuing app commands.
  298. * if(cmd->flags & MMC_FLAG_ACMD)
  299. * c |= APP_CMD;
  300. */
  301. if (data) {
  302. c |= DATA_PRESENT;
  303. if (data->blocks > 1) {
  304. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
  305. c |= TRANSFER_MULTI;
  306. /*
  307. * Disable auto CMD12 at IO_RW_EXTENDED when
  308. * multiple block transfer
  309. */
  310. if ((host->pdata->flags & TMIO_MMC_HAVE_CMD12_CTRL) &&
  311. (cmd->opcode == SD_IO_RW_EXTENDED))
  312. c |= NO_CMD12_ISSUE;
  313. }
  314. if (data->flags & MMC_DATA_READ)
  315. c |= TRANSFER_READ;
  316. }
  317. if (!host->native_hotplug)
  318. irq_mask &= ~(TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
  319. tmio_mmc_enable_mmc_irqs(host, irq_mask);
  320. /* Fire off the command */
  321. sd_ctrl_write32_as_16_and_16(host, CTL_ARG_REG, cmd->arg);
  322. sd_ctrl_write16(host, CTL_SD_CMD, c);
  323. return 0;
  324. }
  325. static void tmio_mmc_transfer_data(struct tmio_mmc_host *host,
  326. unsigned short *buf,
  327. unsigned int count)
  328. {
  329. int is_read = host->data->flags & MMC_DATA_READ;
  330. u8 *buf8;
  331. /*
  332. * Transfer the data
  333. */
  334. if (is_read)
  335. sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  336. else
  337. sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  338. /* if count was even number */
  339. if (!(count & 0x1))
  340. return;
  341. /* if count was odd number */
  342. buf8 = (u8 *)(buf + (count >> 1));
  343. /*
  344. * FIXME
  345. *
  346. * driver and this function are assuming that
  347. * it is used as little endian
  348. */
  349. if (is_read)
  350. *buf8 = sd_ctrl_read16(host, CTL_SD_DATA_PORT) & 0xff;
  351. else
  352. sd_ctrl_write16(host, CTL_SD_DATA_PORT, *buf8);
  353. }
  354. /*
  355. * This chip always returns (at least?) as much data as you ask for.
  356. * I'm unsure what happens if you ask for less than a block. This should be
  357. * looked into to ensure that a funny length read doesn't hose the controller.
  358. */
  359. static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
  360. {
  361. struct mmc_data *data = host->data;
  362. void *sg_virt;
  363. unsigned short *buf;
  364. unsigned int count;
  365. unsigned long flags;
  366. if ((host->chan_tx || host->chan_rx) && !host->force_pio) {
  367. pr_err("PIO IRQ in DMA mode!\n");
  368. return;
  369. } else if (!data) {
  370. pr_debug("Spurious PIO IRQ\n");
  371. return;
  372. }
  373. sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
  374. buf = (unsigned short *)(sg_virt + host->sg_off);
  375. count = host->sg_ptr->length - host->sg_off;
  376. if (count > data->blksz)
  377. count = data->blksz;
  378. pr_debug("count: %08x offset: %08x flags %08x\n",
  379. count, host->sg_off, data->flags);
  380. /* Transfer the data */
  381. tmio_mmc_transfer_data(host, buf, count);
  382. host->sg_off += count;
  383. tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
  384. if (host->sg_off == host->sg_ptr->length)
  385. tmio_mmc_next_sg(host);
  386. return;
  387. }
  388. static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
  389. {
  390. if (host->sg_ptr == &host->bounce_sg) {
  391. unsigned long flags;
  392. void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
  393. memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
  394. tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
  395. }
  396. }
  397. /* needs to be called with host->lock held */
  398. void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
  399. {
  400. struct mmc_data *data = host->data;
  401. struct mmc_command *stop;
  402. host->data = NULL;
  403. if (!data) {
  404. dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
  405. return;
  406. }
  407. stop = data->stop;
  408. /* FIXME - return correct transfer count on errors */
  409. if (!data->error)
  410. data->bytes_xfered = data->blocks * data->blksz;
  411. else
  412. data->bytes_xfered = 0;
  413. pr_debug("Completed data request\n");
  414. /*
  415. * FIXME: other drivers allow an optional stop command of any given type
  416. * which we dont do, as the chip can auto generate them.
  417. * Perhaps we can be smarter about when to use auto CMD12 and
  418. * only issue the auto request when we know this is the desired
  419. * stop command, allowing fallback to the stop command the
  420. * upper layers expect. For now, we do what works.
  421. */
  422. if (data->flags & MMC_DATA_READ) {
  423. if (host->chan_rx && !host->force_pio)
  424. tmio_mmc_check_bounce_buffer(host);
  425. dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
  426. host->mrq);
  427. } else {
  428. dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
  429. host->mrq);
  430. }
  431. if (stop) {
  432. if (stop->opcode == MMC_STOP_TRANSMISSION && !stop->arg)
  433. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
  434. else
  435. BUG();
  436. }
  437. schedule_work(&host->done);
  438. }
  439. static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
  440. {
  441. struct mmc_data *data;
  442. spin_lock(&host->lock);
  443. data = host->data;
  444. if (!data)
  445. goto out;
  446. if (host->chan_tx && (data->flags & MMC_DATA_WRITE) && !host->force_pio) {
  447. u32 status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
  448. bool done = false;
  449. /*
  450. * Has all data been written out yet? Testing on SuperH showed,
  451. * that in most cases the first interrupt comes already with the
  452. * BUSY status bit clear, but on some operations, like mount or
  453. * in the beginning of a write / sync / umount, there is one
  454. * DATAEND interrupt with the BUSY bit set, in this cases
  455. * waiting for one more interrupt fixes the problem.
  456. */
  457. if (host->pdata->flags & TMIO_MMC_HAS_IDLE_WAIT) {
  458. if (status & TMIO_STAT_SCLKDIVEN)
  459. done = true;
  460. } else {
  461. if (!(status & TMIO_STAT_CMD_BUSY))
  462. done = true;
  463. }
  464. if (done) {
  465. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  466. tasklet_schedule(&host->dma_complete);
  467. }
  468. } else if (host->chan_rx && (data->flags & MMC_DATA_READ) && !host->force_pio) {
  469. tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  470. tasklet_schedule(&host->dma_complete);
  471. } else {
  472. tmio_mmc_do_data_irq(host);
  473. tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
  474. }
  475. out:
  476. spin_unlock(&host->lock);
  477. }
  478. static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
  479. unsigned int stat)
  480. {
  481. struct mmc_command *cmd = host->cmd;
  482. int i, addr;
  483. spin_lock(&host->lock);
  484. if (!host->cmd) {
  485. pr_debug("Spurious CMD irq\n");
  486. goto out;
  487. }
  488. host->cmd = NULL;
  489. /* This controller is sicker than the PXA one. Not only do we need to
  490. * drop the top 8 bits of the first response word, we also need to
  491. * modify the order of the response for short response command types.
  492. */
  493. for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
  494. cmd->resp[i] = sd_ctrl_read16_and_16_as_32(host, addr);
  495. if (cmd->flags & MMC_RSP_136) {
  496. cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
  497. cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
  498. cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
  499. cmd->resp[3] <<= 8;
  500. } else if (cmd->flags & MMC_RSP_R3) {
  501. cmd->resp[0] = cmd->resp[3];
  502. }
  503. if (stat & TMIO_STAT_CMDTIMEOUT)
  504. cmd->error = -ETIMEDOUT;
  505. else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
  506. cmd->error = -EILSEQ;
  507. /* If there is data to handle we enable data IRQs here, and
  508. * we will ultimatley finish the request in the data_end handler.
  509. * If theres no data or we encountered an error, finish now.
  510. */
  511. if (host->data && !cmd->error) {
  512. if (host->data->flags & MMC_DATA_READ) {
  513. if (host->force_pio || !host->chan_rx)
  514. tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
  515. else
  516. tasklet_schedule(&host->dma_issue);
  517. } else {
  518. if (host->force_pio || !host->chan_tx)
  519. tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
  520. else
  521. tasklet_schedule(&host->dma_issue);
  522. }
  523. } else {
  524. schedule_work(&host->done);
  525. }
  526. out:
  527. spin_unlock(&host->lock);
  528. }
  529. static bool __tmio_mmc_card_detect_irq(struct tmio_mmc_host *host,
  530. int ireg, int status)
  531. {
  532. struct mmc_host *mmc = host->mmc;
  533. /* Card insert / remove attempts */
  534. if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
  535. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
  536. TMIO_STAT_CARD_REMOVE);
  537. if ((((ireg & TMIO_STAT_CARD_REMOVE) && mmc->card) ||
  538. ((ireg & TMIO_STAT_CARD_INSERT) && !mmc->card)) &&
  539. !work_pending(&mmc->detect.work))
  540. mmc_detect_change(host->mmc, msecs_to_jiffies(100));
  541. return true;
  542. }
  543. return false;
  544. }
  545. static bool __tmio_mmc_sdcard_irq(struct tmio_mmc_host *host,
  546. int ireg, int status)
  547. {
  548. /* Command completion */
  549. if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
  550. tmio_mmc_ack_mmc_irqs(host,
  551. TMIO_STAT_CMDRESPEND |
  552. TMIO_STAT_CMDTIMEOUT);
  553. tmio_mmc_cmd_irq(host, status);
  554. return true;
  555. }
  556. /* Data transfer */
  557. if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
  558. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
  559. tmio_mmc_pio_irq(host);
  560. return true;
  561. }
  562. /* Data transfer completion */
  563. if (ireg & TMIO_STAT_DATAEND) {
  564. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
  565. tmio_mmc_data_irq(host);
  566. return true;
  567. }
  568. return false;
  569. }
  570. static void tmio_mmc_sdio_irq(int irq, void *devid)
  571. {
  572. struct tmio_mmc_host *host = devid;
  573. struct mmc_host *mmc = host->mmc;
  574. struct tmio_mmc_data *pdata = host->pdata;
  575. unsigned int ireg, status;
  576. unsigned int sdio_status;
  577. if (!(pdata->flags & TMIO_MMC_SDIO_IRQ))
  578. return;
  579. status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
  580. ireg = status & TMIO_SDIO_MASK_ALL & ~host->sdcard_irq_mask;
  581. sdio_status = status & ~TMIO_SDIO_MASK_ALL;
  582. if (pdata->flags & TMIO_MMC_SDIO_STATUS_QUIRK)
  583. sdio_status |= 6;
  584. sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status);
  585. if (mmc->caps & MMC_CAP_SDIO_IRQ && ireg & TMIO_SDIO_STAT_IOIRQ)
  586. mmc_signal_sdio_irq(mmc);
  587. }
  588. irqreturn_t tmio_mmc_irq(int irq, void *devid)
  589. {
  590. struct tmio_mmc_host *host = devid;
  591. unsigned int ireg, status;
  592. status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
  593. ireg = status & TMIO_MASK_IRQ & ~host->sdcard_irq_mask;
  594. pr_debug_status(status);
  595. pr_debug_status(ireg);
  596. /* Clear the status except the interrupt status */
  597. sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, TMIO_MASK_IRQ);
  598. if (__tmio_mmc_card_detect_irq(host, ireg, status))
  599. return IRQ_HANDLED;
  600. if (__tmio_mmc_sdcard_irq(host, ireg, status))
  601. return IRQ_HANDLED;
  602. tmio_mmc_sdio_irq(irq, devid);
  603. return IRQ_HANDLED;
  604. }
  605. EXPORT_SYMBOL(tmio_mmc_irq);
  606. static int tmio_mmc_start_data(struct tmio_mmc_host *host,
  607. struct mmc_data *data)
  608. {
  609. struct tmio_mmc_data *pdata = host->pdata;
  610. pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
  611. data->blksz, data->blocks);
  612. /* Some hardware cannot perform 2 byte requests in 4 bit mode */
  613. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
  614. int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
  615. if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
  616. pr_err("%s: %d byte block unsupported in 4 bit mode\n",
  617. mmc_hostname(host->mmc), data->blksz);
  618. return -EINVAL;
  619. }
  620. }
  621. tmio_mmc_init_sg(host, data);
  622. host->data = data;
  623. /* Set transfer length / blocksize */
  624. sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
  625. sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
  626. tmio_mmc_start_dma(host, data);
  627. return 0;
  628. }
  629. /* Process requests from the MMC layer */
  630. static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  631. {
  632. struct tmio_mmc_host *host = mmc_priv(mmc);
  633. unsigned long flags;
  634. int ret;
  635. spin_lock_irqsave(&host->lock, flags);
  636. if (host->mrq) {
  637. pr_debug("request not null\n");
  638. if (IS_ERR(host->mrq)) {
  639. spin_unlock_irqrestore(&host->lock, flags);
  640. mrq->cmd->error = -EAGAIN;
  641. mmc_request_done(mmc, mrq);
  642. return;
  643. }
  644. }
  645. host->last_req_ts = jiffies;
  646. wmb();
  647. host->mrq = mrq;
  648. spin_unlock_irqrestore(&host->lock, flags);
  649. if (mrq->data) {
  650. ret = tmio_mmc_start_data(host, mrq->data);
  651. if (ret)
  652. goto fail;
  653. }
  654. ret = tmio_mmc_start_command(host, mrq->cmd);
  655. if (!ret) {
  656. schedule_delayed_work(&host->delayed_reset_work,
  657. msecs_to_jiffies(CMDREQ_TIMEOUT));
  658. return;
  659. }
  660. fail:
  661. host->force_pio = false;
  662. host->mrq = NULL;
  663. mrq->cmd->error = ret;
  664. mmc_request_done(mmc, mrq);
  665. }
  666. static int tmio_mmc_clk_enable(struct tmio_mmc_host *host)
  667. {
  668. if (!host->clk_enable)
  669. return -ENOTSUPP;
  670. return host->clk_enable(host);
  671. }
  672. static void tmio_mmc_power_on(struct tmio_mmc_host *host, unsigned short vdd)
  673. {
  674. struct mmc_host *mmc = host->mmc;
  675. int ret = 0;
  676. /* .set_ios() is returning void, so, no chance to report an error */
  677. if (host->set_pwr)
  678. host->set_pwr(host->pdev, 1);
  679. if (!IS_ERR(mmc->supply.vmmc)) {
  680. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  681. /*
  682. * Attention: empiric value. With a b43 WiFi SDIO card this
  683. * delay proved necessary for reliable card-insertion probing.
  684. * 100us were not enough. Is this the same 140us delay, as in
  685. * tmio_mmc_set_ios()?
  686. */
  687. udelay(200);
  688. }
  689. /*
  690. * It seems, VccQ should be switched on after Vcc, this is also what the
  691. * omap_hsmmc.c driver does.
  692. */
  693. if (!IS_ERR(mmc->supply.vqmmc) && !ret) {
  694. ret = regulator_enable(mmc->supply.vqmmc);
  695. udelay(200);
  696. }
  697. if (ret < 0)
  698. dev_dbg(&host->pdev->dev, "Regulators failed to power up: %d\n",
  699. ret);
  700. }
  701. static void tmio_mmc_power_off(struct tmio_mmc_host *host)
  702. {
  703. struct mmc_host *mmc = host->mmc;
  704. if (!IS_ERR(mmc->supply.vqmmc))
  705. regulator_disable(mmc->supply.vqmmc);
  706. if (!IS_ERR(mmc->supply.vmmc))
  707. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  708. if (host->set_pwr)
  709. host->set_pwr(host->pdev, 0);
  710. }
  711. static void tmio_mmc_set_bus_width(struct tmio_mmc_host *host,
  712. unsigned char bus_width)
  713. {
  714. switch (bus_width) {
  715. case MMC_BUS_WIDTH_1:
  716. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
  717. break;
  718. case MMC_BUS_WIDTH_4:
  719. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
  720. break;
  721. }
  722. }
  723. /* Set MMC clock / power.
  724. * Note: This controller uses a simple divider scheme therefore it cannot
  725. * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
  726. * MMC wont run that fast, it has to be clocked at 12MHz which is the next
  727. * slowest setting.
  728. */
  729. static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  730. {
  731. struct tmio_mmc_host *host = mmc_priv(mmc);
  732. struct device *dev = &host->pdev->dev;
  733. unsigned long flags;
  734. mutex_lock(&host->ios_lock);
  735. spin_lock_irqsave(&host->lock, flags);
  736. if (host->mrq) {
  737. if (IS_ERR(host->mrq)) {
  738. dev_dbg(dev,
  739. "%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
  740. current->comm, task_pid_nr(current),
  741. ios->clock, ios->power_mode);
  742. host->mrq = ERR_PTR(-EINTR);
  743. } else {
  744. dev_dbg(dev,
  745. "%s.%d: CMD%u active since %lu, now %lu!\n",
  746. current->comm, task_pid_nr(current),
  747. host->mrq->cmd->opcode, host->last_req_ts, jiffies);
  748. }
  749. spin_unlock_irqrestore(&host->lock, flags);
  750. mutex_unlock(&host->ios_lock);
  751. return;
  752. }
  753. host->mrq = ERR_PTR(-EBUSY);
  754. spin_unlock_irqrestore(&host->lock, flags);
  755. switch (ios->power_mode) {
  756. case MMC_POWER_OFF:
  757. tmio_mmc_power_off(host);
  758. tmio_mmc_clk_stop(host);
  759. break;
  760. case MMC_POWER_UP:
  761. tmio_mmc_power_on(host, ios->vdd);
  762. tmio_mmc_set_clock(host, ios->clock);
  763. tmio_mmc_set_bus_width(host, ios->bus_width);
  764. break;
  765. case MMC_POWER_ON:
  766. tmio_mmc_set_clock(host, ios->clock);
  767. tmio_mmc_set_bus_width(host, ios->bus_width);
  768. break;
  769. }
  770. /* Let things settle. delay taken from winCE driver */
  771. udelay(140);
  772. if (PTR_ERR(host->mrq) == -EINTR)
  773. dev_dbg(&host->pdev->dev,
  774. "%s.%d: IOS interrupted: clk %u, mode %u",
  775. current->comm, task_pid_nr(current),
  776. ios->clock, ios->power_mode);
  777. host->mrq = NULL;
  778. host->clk_cache = ios->clock;
  779. mutex_unlock(&host->ios_lock);
  780. }
  781. static int tmio_mmc_get_ro(struct mmc_host *mmc)
  782. {
  783. struct tmio_mmc_host *host = mmc_priv(mmc);
  784. struct tmio_mmc_data *pdata = host->pdata;
  785. int ret = mmc_gpio_get_ro(mmc);
  786. if (ret >= 0)
  787. return ret;
  788. ret = !((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
  789. (sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT));
  790. return ret;
  791. }
  792. static int tmio_multi_io_quirk(struct mmc_card *card,
  793. unsigned int direction, int blk_size)
  794. {
  795. struct tmio_mmc_host *host = mmc_priv(card->host);
  796. if (host->multi_io_quirk)
  797. return host->multi_io_quirk(card, direction, blk_size);
  798. return blk_size;
  799. }
  800. static int tmio_mmc_card_busy(struct mmc_host *mmc)
  801. {
  802. struct tmio_mmc_host *host = mmc_priv(mmc);
  803. return !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) & TMIO_STAT_DAT0);
  804. }
  805. static struct mmc_host_ops tmio_mmc_ops = {
  806. .request = tmio_mmc_request,
  807. .set_ios = tmio_mmc_set_ios,
  808. .get_ro = tmio_mmc_get_ro,
  809. .get_cd = mmc_gpio_get_cd,
  810. .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
  811. .card_busy = tmio_mmc_card_busy,
  812. .multi_io_quirk = tmio_multi_io_quirk,
  813. };
  814. static int tmio_mmc_init_ocr(struct tmio_mmc_host *host)
  815. {
  816. struct tmio_mmc_data *pdata = host->pdata;
  817. struct mmc_host *mmc = host->mmc;
  818. mmc_regulator_get_supply(mmc);
  819. /* use ocr_mask if no regulator */
  820. if (!mmc->ocr_avail)
  821. mmc->ocr_avail = pdata->ocr_mask;
  822. /*
  823. * try again.
  824. * There is possibility that regulator has not been probed
  825. */
  826. if (!mmc->ocr_avail)
  827. return -EPROBE_DEFER;
  828. return 0;
  829. }
  830. static void tmio_mmc_of_parse(struct platform_device *pdev,
  831. struct tmio_mmc_data *pdata)
  832. {
  833. const struct device_node *np = pdev->dev.of_node;
  834. if (!np)
  835. return;
  836. if (of_get_property(np, "toshiba,mmc-wrprotect-disable", NULL))
  837. pdata->flags |= TMIO_MMC_WRPROTECT_DISABLE;
  838. }
  839. struct tmio_mmc_host*
  840. tmio_mmc_host_alloc(struct platform_device *pdev)
  841. {
  842. struct tmio_mmc_host *host;
  843. struct mmc_host *mmc;
  844. mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
  845. if (!mmc)
  846. return NULL;
  847. host = mmc_priv(mmc);
  848. host->mmc = mmc;
  849. host->pdev = pdev;
  850. return host;
  851. }
  852. EXPORT_SYMBOL(tmio_mmc_host_alloc);
  853. void tmio_mmc_host_free(struct tmio_mmc_host *host)
  854. {
  855. mmc_free_host(host->mmc);
  856. }
  857. EXPORT_SYMBOL(tmio_mmc_host_free);
  858. int tmio_mmc_host_probe(struct tmio_mmc_host *_host,
  859. struct tmio_mmc_data *pdata)
  860. {
  861. struct platform_device *pdev = _host->pdev;
  862. struct mmc_host *mmc = _host->mmc;
  863. struct resource *res_ctl;
  864. int ret;
  865. u32 irq_mask = TMIO_MASK_CMD;
  866. tmio_mmc_of_parse(pdev, pdata);
  867. if (!(pdata->flags & TMIO_MMC_HAS_IDLE_WAIT))
  868. _host->write16_hook = NULL;
  869. res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  870. if (!res_ctl)
  871. return -EINVAL;
  872. ret = mmc_of_parse(mmc);
  873. if (ret < 0)
  874. goto host_free;
  875. _host->pdata = pdata;
  876. platform_set_drvdata(pdev, mmc);
  877. _host->set_pwr = pdata->set_pwr;
  878. _host->set_clk_div = pdata->set_clk_div;
  879. ret = tmio_mmc_init_ocr(_host);
  880. if (ret < 0)
  881. goto host_free;
  882. _host->ctl = devm_ioremap(&pdev->dev,
  883. res_ctl->start, resource_size(res_ctl));
  884. if (!_host->ctl) {
  885. ret = -ENOMEM;
  886. goto host_free;
  887. }
  888. tmio_mmc_ops.start_signal_voltage_switch = _host->start_signal_voltage_switch;
  889. mmc->ops = &tmio_mmc_ops;
  890. mmc->caps |= MMC_CAP_4_BIT_DATA | pdata->capabilities;
  891. mmc->caps2 |= pdata->capabilities2;
  892. mmc->max_segs = 32;
  893. mmc->max_blk_size = 512;
  894. mmc->max_blk_count = (PAGE_SIZE / mmc->max_blk_size) *
  895. mmc->max_segs;
  896. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  897. mmc->max_seg_size = mmc->max_req_size;
  898. _host->native_hotplug = !(pdata->flags & TMIO_MMC_USE_GPIO_CD ||
  899. mmc->caps & MMC_CAP_NEEDS_POLL ||
  900. !mmc_card_is_removable(mmc) ||
  901. mmc->slot.cd_irq >= 0);
  902. if (tmio_mmc_clk_enable(_host) < 0) {
  903. mmc->f_max = pdata->hclk;
  904. mmc->f_min = mmc->f_max / 512;
  905. }
  906. /*
  907. * Check the sanity of mmc->f_min to prevent tmio_mmc_set_clock() from
  908. * looping forever...
  909. */
  910. if (mmc->f_min == 0) {
  911. ret = -EINVAL;
  912. goto host_free;
  913. }
  914. /*
  915. * While using internal tmio hardware logic for card detection, we need
  916. * to ensure it stays powered for it to work.
  917. */
  918. if (_host->native_hotplug)
  919. pm_runtime_get_noresume(&pdev->dev);
  920. tmio_mmc_clk_stop(_host);
  921. tmio_mmc_reset(_host);
  922. _host->sdcard_irq_mask = sd_ctrl_read16_and_16_as_32(_host, CTL_IRQ_MASK);
  923. tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
  924. /* Unmask the IRQs we want to know about */
  925. if (!_host->chan_rx)
  926. irq_mask |= TMIO_MASK_READOP;
  927. if (!_host->chan_tx)
  928. irq_mask |= TMIO_MASK_WRITEOP;
  929. if (!_host->native_hotplug)
  930. irq_mask &= ~(TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
  931. _host->sdcard_irq_mask &= ~irq_mask;
  932. _host->sdio_irq_enabled = false;
  933. if (pdata->flags & TMIO_MMC_SDIO_IRQ) {
  934. _host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
  935. sd_ctrl_write16(_host, CTL_SDIO_IRQ_MASK, _host->sdio_irq_mask);
  936. sd_ctrl_write16(_host, CTL_TRANSACTION_CTL, 0x0000);
  937. }
  938. spin_lock_init(&_host->lock);
  939. mutex_init(&_host->ios_lock);
  940. /* Init delayed work for request timeouts */
  941. INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
  942. INIT_WORK(&_host->done, tmio_mmc_done_work);
  943. /* See if we also get DMA */
  944. tmio_mmc_request_dma(_host, pdata);
  945. pm_runtime_set_active(&pdev->dev);
  946. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  947. pm_runtime_use_autosuspend(&pdev->dev);
  948. pm_runtime_enable(&pdev->dev);
  949. ret = mmc_add_host(mmc);
  950. if (ret < 0) {
  951. tmio_mmc_host_remove(_host);
  952. return ret;
  953. }
  954. dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
  955. if (pdata->flags & TMIO_MMC_USE_GPIO_CD) {
  956. ret = mmc_gpio_request_cd(mmc, pdata->cd_gpio, 0);
  957. if (ret < 0) {
  958. tmio_mmc_host_remove(_host);
  959. return ret;
  960. }
  961. mmc_gpiod_request_cd_irq(mmc);
  962. }
  963. return 0;
  964. host_free:
  965. return ret;
  966. }
  967. EXPORT_SYMBOL(tmio_mmc_host_probe);
  968. void tmio_mmc_host_remove(struct tmio_mmc_host *host)
  969. {
  970. struct platform_device *pdev = host->pdev;
  971. struct mmc_host *mmc = host->mmc;
  972. if (!host->native_hotplug)
  973. pm_runtime_get_sync(&pdev->dev);
  974. dev_pm_qos_hide_latency_limit(&pdev->dev);
  975. mmc_remove_host(mmc);
  976. cancel_work_sync(&host->done);
  977. cancel_delayed_work_sync(&host->delayed_reset_work);
  978. tmio_mmc_release_dma(host);
  979. pm_runtime_put_sync(&pdev->dev);
  980. pm_runtime_disable(&pdev->dev);
  981. }
  982. EXPORT_SYMBOL(tmio_mmc_host_remove);
  983. #ifdef CONFIG_PM
  984. int tmio_mmc_host_runtime_suspend(struct device *dev)
  985. {
  986. struct mmc_host *mmc = dev_get_drvdata(dev);
  987. struct tmio_mmc_host *host = mmc_priv(mmc);
  988. tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
  989. if (host->clk_cache)
  990. tmio_mmc_clk_stop(host);
  991. if (host->clk_disable)
  992. host->clk_disable(host);
  993. return 0;
  994. }
  995. EXPORT_SYMBOL(tmio_mmc_host_runtime_suspend);
  996. int tmio_mmc_host_runtime_resume(struct device *dev)
  997. {
  998. struct mmc_host *mmc = dev_get_drvdata(dev);
  999. struct tmio_mmc_host *host = mmc_priv(mmc);
  1000. tmio_mmc_reset(host);
  1001. tmio_mmc_clk_enable(host);
  1002. if (host->clk_cache)
  1003. tmio_mmc_set_clock(host, host->clk_cache);
  1004. tmio_mmc_enable_dma(host, true);
  1005. return 0;
  1006. }
  1007. EXPORT_SYMBOL(tmio_mmc_host_runtime_resume);
  1008. #endif
  1009. MODULE_LICENSE("GPL v2");