tmio_mmc_pio.c 28 KB

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