tmio_mmc_pio.c 24 KB

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