tmio_mmc_pio.c 32 KB

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