tmio_mmc_pio.c 32 KB

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