tmio_mmc_pio.c 32 KB

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