tmio_mmc_pio.c 23 KB

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