tmio_mmc_pio.c 26 KB

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