tmio_mmc_pio.c 26 KB

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