tmio_mmc_pio.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 tmio_mmc_data *pdata = host->pdata;
  464. unsigned int ireg, irq_mask, status;
  465. unsigned int sdio_ireg, sdio_irq_mask, sdio_status;
  466. pr_debug("MMC IRQ begin\n");
  467. status = sd_ctrl_read32(host, CTL_STATUS);
  468. irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
  469. ireg = status & TMIO_MASK_IRQ & ~irq_mask;
  470. sdio_ireg = 0;
  471. if (!ireg && pdata->flags & TMIO_MMC_SDIO_IRQ) {
  472. sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
  473. sdio_irq_mask = sd_ctrl_read16(host, CTL_SDIO_IRQ_MASK);
  474. sdio_ireg = sdio_status & TMIO_SDIO_MASK_ALL & ~sdio_irq_mask;
  475. sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status & ~TMIO_SDIO_MASK_ALL);
  476. if (sdio_ireg && !host->sdio_irq_enabled) {
  477. pr_warning("tmio_mmc: Spurious SDIO IRQ, disabling! 0x%04x 0x%04x 0x%04x\n",
  478. sdio_status, sdio_irq_mask, sdio_ireg);
  479. tmio_mmc_enable_sdio_irq(host->mmc, 0);
  480. goto out;
  481. }
  482. if (host->mmc->caps & MMC_CAP_SDIO_IRQ &&
  483. sdio_ireg & TMIO_SDIO_STAT_IOIRQ)
  484. mmc_signal_sdio_irq(host->mmc);
  485. if (sdio_ireg)
  486. goto out;
  487. }
  488. pr_debug_status(status);
  489. pr_debug_status(ireg);
  490. /* Card insert / remove attempts */
  491. if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
  492. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
  493. TMIO_STAT_CARD_REMOVE);
  494. if (!work_pending(&host->mmc->detect.work))
  495. mmc_detect_change(host->mmc, msecs_to_jiffies(100));
  496. goto out;
  497. }
  498. /* CRC and other errors */
  499. /* if (ireg & TMIO_STAT_ERR_IRQ)
  500. * handled |= tmio_error_irq(host, irq, stat);
  501. */
  502. /* Command completion */
  503. if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
  504. tmio_mmc_ack_mmc_irqs(host,
  505. TMIO_STAT_CMDRESPEND |
  506. TMIO_STAT_CMDTIMEOUT);
  507. tmio_mmc_cmd_irq(host, status);
  508. goto out;
  509. }
  510. /* Data transfer */
  511. if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
  512. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
  513. tmio_mmc_pio_irq(host);
  514. goto out;
  515. }
  516. /* Data transfer completion */
  517. if (ireg & TMIO_STAT_DATAEND) {
  518. tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
  519. tmio_mmc_data_irq(host);
  520. goto out;
  521. }
  522. pr_warning("tmio_mmc: Spurious irq, disabling! "
  523. "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
  524. pr_debug_status(status);
  525. tmio_mmc_disable_mmc_irqs(host, status & ~irq_mask);
  526. out:
  527. return IRQ_HANDLED;
  528. }
  529. EXPORT_SYMBOL(tmio_mmc_irq);
  530. static int tmio_mmc_start_data(struct tmio_mmc_host *host,
  531. struct mmc_data *data)
  532. {
  533. struct tmio_mmc_data *pdata = host->pdata;
  534. pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
  535. data->blksz, data->blocks);
  536. /* Some hardware cannot perform 2 byte requests in 4 bit mode */
  537. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
  538. int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
  539. if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
  540. pr_err("%s: %d byte block unsupported in 4 bit mode\n",
  541. mmc_hostname(host->mmc), data->blksz);
  542. return -EINVAL;
  543. }
  544. }
  545. tmio_mmc_init_sg(host, data);
  546. host->data = data;
  547. /* Set transfer length / blocksize */
  548. sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
  549. sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
  550. tmio_mmc_start_dma(host, data);
  551. return 0;
  552. }
  553. /* Process requests from the MMC layer */
  554. static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  555. {
  556. struct tmio_mmc_host *host = mmc_priv(mmc);
  557. unsigned long flags;
  558. int ret;
  559. spin_lock_irqsave(&host->lock, flags);
  560. if (host->mrq) {
  561. pr_debug("request not null\n");
  562. if (IS_ERR(host->mrq)) {
  563. spin_unlock_irqrestore(&host->lock, flags);
  564. mrq->cmd->error = -EAGAIN;
  565. mmc_request_done(mmc, mrq);
  566. return;
  567. }
  568. }
  569. host->last_req_ts = jiffies;
  570. wmb();
  571. host->mrq = mrq;
  572. spin_unlock_irqrestore(&host->lock, flags);
  573. if (mrq->data) {
  574. ret = tmio_mmc_start_data(host, mrq->data);
  575. if (ret)
  576. goto fail;
  577. }
  578. ret = tmio_mmc_start_command(host, mrq->cmd);
  579. if (!ret) {
  580. schedule_delayed_work(&host->delayed_reset_work,
  581. msecs_to_jiffies(2000));
  582. return;
  583. }
  584. fail:
  585. host->force_pio = false;
  586. host->mrq = NULL;
  587. mrq->cmd->error = ret;
  588. mmc_request_done(mmc, mrq);
  589. }
  590. /* Set MMC clock / power.
  591. * Note: This controller uses a simple divider scheme therefore it cannot
  592. * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
  593. * MMC wont run that fast, it has to be clocked at 12MHz which is the next
  594. * slowest setting.
  595. */
  596. static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  597. {
  598. struct tmio_mmc_host *host = mmc_priv(mmc);
  599. struct tmio_mmc_data *pdata = host->pdata;
  600. unsigned long flags;
  601. mutex_lock(&host->ios_lock);
  602. spin_lock_irqsave(&host->lock, flags);
  603. if (host->mrq) {
  604. if (IS_ERR(host->mrq)) {
  605. dev_dbg(&host->pdev->dev,
  606. "%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
  607. current->comm, task_pid_nr(current),
  608. ios->clock, ios->power_mode);
  609. host->mrq = ERR_PTR(-EINTR);
  610. } else {
  611. dev_dbg(&host->pdev->dev,
  612. "%s.%d: CMD%u active since %lu, now %lu!\n",
  613. current->comm, task_pid_nr(current),
  614. host->mrq->cmd->opcode, host->last_req_ts, jiffies);
  615. }
  616. spin_unlock_irqrestore(&host->lock, flags);
  617. mutex_unlock(&host->ios_lock);
  618. return;
  619. }
  620. host->mrq = ERR_PTR(-EBUSY);
  621. spin_unlock_irqrestore(&host->lock, flags);
  622. if (ios->clock)
  623. tmio_mmc_set_clock(host, ios->clock);
  624. /* Power sequence - OFF -> UP -> ON */
  625. if (ios->power_mode == MMC_POWER_UP) {
  626. if ((pdata->flags & TMIO_MMC_HAS_COLD_CD) && !pdata->power) {
  627. pm_runtime_get_sync(&host->pdev->dev);
  628. pdata->power = true;
  629. }
  630. /* power up SD bus */
  631. if (host->set_pwr)
  632. host->set_pwr(host->pdev, 1);
  633. } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
  634. /* power down SD bus */
  635. if (ios->power_mode == MMC_POWER_OFF) {
  636. if (host->set_pwr)
  637. host->set_pwr(host->pdev, 0);
  638. if ((pdata->flags & TMIO_MMC_HAS_COLD_CD) &&
  639. pdata->power) {
  640. pdata->power = false;
  641. pm_runtime_put(&host->pdev->dev);
  642. }
  643. }
  644. tmio_mmc_clk_stop(host);
  645. } else {
  646. /* start bus clock */
  647. tmio_mmc_clk_start(host);
  648. }
  649. switch (ios->bus_width) {
  650. case MMC_BUS_WIDTH_1:
  651. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
  652. break;
  653. case MMC_BUS_WIDTH_4:
  654. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
  655. break;
  656. }
  657. /* Let things settle. delay taken from winCE driver */
  658. udelay(140);
  659. if (PTR_ERR(host->mrq) == -EINTR)
  660. dev_dbg(&host->pdev->dev,
  661. "%s.%d: IOS interrupted: clk %u, mode %u",
  662. current->comm, task_pid_nr(current),
  663. ios->clock, ios->power_mode);
  664. host->mrq = NULL;
  665. mutex_unlock(&host->ios_lock);
  666. }
  667. static int tmio_mmc_get_ro(struct mmc_host *mmc)
  668. {
  669. struct tmio_mmc_host *host = mmc_priv(mmc);
  670. struct tmio_mmc_data *pdata = host->pdata;
  671. return !((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
  672. (sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT));
  673. }
  674. static int tmio_mmc_get_cd(struct mmc_host *mmc)
  675. {
  676. struct tmio_mmc_host *host = mmc_priv(mmc);
  677. struct tmio_mmc_data *pdata = host->pdata;
  678. if (!pdata->get_cd)
  679. return -ENOSYS;
  680. else
  681. return pdata->get_cd(host->pdev);
  682. }
  683. static const struct mmc_host_ops tmio_mmc_ops = {
  684. .request = tmio_mmc_request,
  685. .set_ios = tmio_mmc_set_ios,
  686. .get_ro = tmio_mmc_get_ro,
  687. .get_cd = tmio_mmc_get_cd,
  688. .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
  689. };
  690. int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host,
  691. struct platform_device *pdev,
  692. struct tmio_mmc_data *pdata)
  693. {
  694. struct tmio_mmc_host *_host;
  695. struct mmc_host *mmc;
  696. struct resource *res_ctl;
  697. int ret;
  698. u32 irq_mask = TMIO_MASK_CMD;
  699. res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  700. if (!res_ctl)
  701. return -EINVAL;
  702. mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
  703. if (!mmc)
  704. return -ENOMEM;
  705. pdata->dev = &pdev->dev;
  706. _host = mmc_priv(mmc);
  707. _host->pdata = pdata;
  708. _host->mmc = mmc;
  709. _host->pdev = pdev;
  710. platform_set_drvdata(pdev, mmc);
  711. _host->set_pwr = pdata->set_pwr;
  712. _host->set_clk_div = pdata->set_clk_div;
  713. /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
  714. _host->bus_shift = resource_size(res_ctl) >> 10;
  715. _host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
  716. if (!_host->ctl) {
  717. ret = -ENOMEM;
  718. goto host_free;
  719. }
  720. mmc->ops = &tmio_mmc_ops;
  721. mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
  722. mmc->f_max = pdata->hclk;
  723. mmc->f_min = mmc->f_max / 512;
  724. mmc->max_segs = 32;
  725. mmc->max_blk_size = 512;
  726. mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
  727. mmc->max_segs;
  728. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  729. mmc->max_seg_size = mmc->max_req_size;
  730. if (pdata->ocr_mask)
  731. mmc->ocr_avail = pdata->ocr_mask;
  732. else
  733. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  734. pdata->power = false;
  735. pm_runtime_enable(&pdev->dev);
  736. ret = pm_runtime_resume(&pdev->dev);
  737. if (ret < 0)
  738. goto pm_disable;
  739. tmio_mmc_clk_stop(_host);
  740. tmio_mmc_reset(_host);
  741. tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
  742. if (pdata->flags & TMIO_MMC_SDIO_IRQ)
  743. tmio_mmc_enable_sdio_irq(mmc, 0);
  744. spin_lock_init(&_host->lock);
  745. mutex_init(&_host->ios_lock);
  746. /* Init delayed work for request timeouts */
  747. INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
  748. INIT_WORK(&_host->done, tmio_mmc_done_work);
  749. /* See if we also get DMA */
  750. tmio_mmc_request_dma(_host, pdata);
  751. /* We have to keep the device powered for its card detection to work */
  752. if (!(pdata->flags & TMIO_MMC_HAS_COLD_CD))
  753. pm_runtime_get_noresume(&pdev->dev);
  754. mmc_add_host(mmc);
  755. /* Unmask the IRQs we want to know about */
  756. if (!_host->chan_rx)
  757. irq_mask |= TMIO_MASK_READOP;
  758. if (!_host->chan_tx)
  759. irq_mask |= TMIO_MASK_WRITEOP;
  760. tmio_mmc_enable_mmc_irqs(_host, irq_mask);
  761. *host = _host;
  762. return 0;
  763. pm_disable:
  764. pm_runtime_disable(&pdev->dev);
  765. iounmap(_host->ctl);
  766. host_free:
  767. mmc_free_host(mmc);
  768. return ret;
  769. }
  770. EXPORT_SYMBOL(tmio_mmc_host_probe);
  771. void tmio_mmc_host_remove(struct tmio_mmc_host *host)
  772. {
  773. struct platform_device *pdev = host->pdev;
  774. /*
  775. * We don't have to manipulate pdata->power here: if there is a card in
  776. * the slot, the runtime PM is active and our .runtime_resume() will not
  777. * be run. If there is no card in the slot and the platform can suspend
  778. * the controller, the runtime PM is suspended and pdata->power == false,
  779. * so, our .runtime_resume() will not try to detect a card in the slot.
  780. */
  781. if (host->pdata->flags & TMIO_MMC_HAS_COLD_CD)
  782. pm_runtime_get_sync(&pdev->dev);
  783. mmc_remove_host(host->mmc);
  784. cancel_work_sync(&host->done);
  785. cancel_delayed_work_sync(&host->delayed_reset_work);
  786. tmio_mmc_release_dma(host);
  787. pm_runtime_put_sync(&pdev->dev);
  788. pm_runtime_disable(&pdev->dev);
  789. iounmap(host->ctl);
  790. mmc_free_host(host->mmc);
  791. }
  792. EXPORT_SYMBOL(tmio_mmc_host_remove);
  793. #ifdef CONFIG_PM
  794. int tmio_mmc_host_suspend(struct device *dev)
  795. {
  796. struct mmc_host *mmc = dev_get_drvdata(dev);
  797. struct tmio_mmc_host *host = mmc_priv(mmc);
  798. int ret = mmc_suspend_host(mmc);
  799. if (!ret)
  800. tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
  801. host->pm_error = pm_runtime_put_sync(dev);
  802. return ret;
  803. }
  804. EXPORT_SYMBOL(tmio_mmc_host_suspend);
  805. int tmio_mmc_host_resume(struct device *dev)
  806. {
  807. struct mmc_host *mmc = dev_get_drvdata(dev);
  808. struct tmio_mmc_host *host = mmc_priv(mmc);
  809. /* The MMC core will perform the complete set up */
  810. host->pdata->power = false;
  811. if (!host->pm_error)
  812. pm_runtime_get_sync(dev);
  813. tmio_mmc_reset(mmc_priv(mmc));
  814. tmio_mmc_request_dma(host, host->pdata);
  815. return mmc_resume_host(mmc);
  816. }
  817. EXPORT_SYMBOL(tmio_mmc_host_resume);
  818. #endif /* CONFIG_PM */
  819. int tmio_mmc_host_runtime_suspend(struct device *dev)
  820. {
  821. return 0;
  822. }
  823. EXPORT_SYMBOL(tmio_mmc_host_runtime_suspend);
  824. int tmio_mmc_host_runtime_resume(struct device *dev)
  825. {
  826. struct mmc_host *mmc = dev_get_drvdata(dev);
  827. struct tmio_mmc_host *host = mmc_priv(mmc);
  828. struct tmio_mmc_data *pdata = host->pdata;
  829. tmio_mmc_reset(host);
  830. if (pdata->power) {
  831. /* Only entered after a card-insert interrupt */
  832. tmio_mmc_set_ios(mmc, &mmc->ios);
  833. mmc_detect_change(mmc, msecs_to_jiffies(100));
  834. }
  835. return 0;
  836. }
  837. EXPORT_SYMBOL(tmio_mmc_host_runtime_resume);
  838. MODULE_LICENSE("GPL v2");