tmio_mmc_pio.c 28 KB

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