tmio_mmc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. * linux/drivers/mmc/tmio_mmc.c
  3. *
  4. * Copyright (C) 2004 Ian Molton
  5. * Copyright (C) 2007 Ian Molton
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Driver for the MMC / SD / SDIO cell found in:
  12. *
  13. * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
  14. *
  15. * This driver draws mainly on scattered spec sheets, Reverse engineering
  16. * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit
  17. * support). (Further 4 bit support from a later datasheet).
  18. *
  19. * TODO:
  20. * Investigate using a workqueue for PIO transfers
  21. * Eliminate FIXMEs
  22. * SDIO support
  23. * Better Power management
  24. * Handle MMC errors better
  25. * double buffer support
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/irq.h>
  30. #include <linux/device.h>
  31. #include <linux/delay.h>
  32. #include <linux/dmaengine.h>
  33. #include <linux/mmc/host.h>
  34. #include <linux/mfd/core.h>
  35. #include <linux/mfd/tmio.h>
  36. #include "tmio_mmc.h"
  37. static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
  38. {
  39. u32 clk = 0, clock;
  40. if (new_clock) {
  41. for (clock = host->mmc->f_min, clk = 0x80000080;
  42. new_clock >= (clock<<1); clk >>= 1)
  43. clock <<= 1;
  44. clk |= 0x100;
  45. }
  46. if (host->set_clk_div)
  47. host->set_clk_div(host->pdev, (clk>>22) & 1);
  48. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
  49. }
  50. static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
  51. {
  52. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
  53. msleep(10);
  54. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
  55. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  56. msleep(10);
  57. }
  58. static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
  59. {
  60. sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
  61. sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
  62. msleep(10);
  63. sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
  64. msleep(10);
  65. }
  66. static void reset(struct tmio_mmc_host *host)
  67. {
  68. /* FIXME - should we set stop clock reg here */
  69. sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
  70. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
  71. msleep(10);
  72. sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
  73. sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
  74. msleep(10);
  75. }
  76. static void
  77. tmio_mmc_finish_request(struct tmio_mmc_host *host)
  78. {
  79. struct mmc_request *mrq = host->mrq;
  80. host->mrq = NULL;
  81. host->cmd = NULL;
  82. host->data = NULL;
  83. mmc_request_done(host->mmc, mrq);
  84. }
  85. /* These are the bitmasks the tmio chip requires to implement the MMC response
  86. * types. Note that R1 and R6 are the same in this scheme. */
  87. #define APP_CMD 0x0040
  88. #define RESP_NONE 0x0300
  89. #define RESP_R1 0x0400
  90. #define RESP_R1B 0x0500
  91. #define RESP_R2 0x0600
  92. #define RESP_R3 0x0700
  93. #define DATA_PRESENT 0x0800
  94. #define TRANSFER_READ 0x1000
  95. #define TRANSFER_MULTI 0x2000
  96. #define SECURITY_CMD 0x4000
  97. static int
  98. tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
  99. {
  100. struct mmc_data *data = host->data;
  101. int c = cmd->opcode;
  102. /* Command 12 is handled by hardware */
  103. if (cmd->opcode == 12 && !cmd->arg) {
  104. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
  105. return 0;
  106. }
  107. switch (mmc_resp_type(cmd)) {
  108. case MMC_RSP_NONE: c |= RESP_NONE; break;
  109. case MMC_RSP_R1: c |= RESP_R1; break;
  110. case MMC_RSP_R1B: c |= RESP_R1B; break;
  111. case MMC_RSP_R2: c |= RESP_R2; break;
  112. case MMC_RSP_R3: c |= RESP_R3; break;
  113. default:
  114. pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
  115. return -EINVAL;
  116. }
  117. host->cmd = cmd;
  118. /* FIXME - this seems to be ok commented out but the spec suggest this bit
  119. * should be set when issuing app commands.
  120. * if(cmd->flags & MMC_FLAG_ACMD)
  121. * c |= APP_CMD;
  122. */
  123. if (data) {
  124. c |= DATA_PRESENT;
  125. if (data->blocks > 1) {
  126. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
  127. c |= TRANSFER_MULTI;
  128. }
  129. if (data->flags & MMC_DATA_READ)
  130. c |= TRANSFER_READ;
  131. }
  132. enable_mmc_irqs(host, TMIO_MASK_CMD);
  133. /* Fire off the command */
  134. sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
  135. sd_ctrl_write16(host, CTL_SD_CMD, c);
  136. return 0;
  137. }
  138. /*
  139. * This chip always returns (at least?) as much data as you ask for.
  140. * I'm unsure what happens if you ask for less than a block. This should be
  141. * looked into to ensure that a funny length read doesnt hose the controller.
  142. */
  143. static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
  144. {
  145. struct mmc_data *data = host->data;
  146. unsigned short *buf;
  147. unsigned int count;
  148. unsigned long flags;
  149. if (!data) {
  150. pr_debug("Spurious PIO IRQ\n");
  151. return;
  152. }
  153. buf = (unsigned short *)(tmio_mmc_kmap_atomic(host, &flags) +
  154. host->sg_off);
  155. count = host->sg_ptr->length - host->sg_off;
  156. if (count > data->blksz)
  157. count = data->blksz;
  158. pr_debug("count: %08x offset: %08x flags %08x\n",
  159. count, host->sg_off, data->flags);
  160. /* Transfer the data */
  161. if (data->flags & MMC_DATA_READ)
  162. sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  163. else
  164. sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
  165. host->sg_off += count;
  166. tmio_mmc_kunmap_atomic(host, &flags);
  167. if (host->sg_off == host->sg_ptr->length)
  168. tmio_mmc_next_sg(host);
  169. return;
  170. }
  171. static void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
  172. {
  173. struct mmc_data *data = host->data;
  174. struct mmc_command *stop;
  175. host->data = NULL;
  176. if (!data) {
  177. dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
  178. return;
  179. }
  180. stop = data->stop;
  181. /* FIXME - return correct transfer count on errors */
  182. if (!data->error)
  183. data->bytes_xfered = data->blocks * data->blksz;
  184. else
  185. data->bytes_xfered = 0;
  186. pr_debug("Completed data request\n");
  187. /*
  188. * FIXME: other drivers allow an optional stop command of any given type
  189. * which we dont do, as the chip can auto generate them.
  190. * Perhaps we can be smarter about when to use auto CMD12 and
  191. * only issue the auto request when we know this is the desired
  192. * stop command, allowing fallback to the stop command the
  193. * upper layers expect. For now, we do what works.
  194. */
  195. if (data->flags & MMC_DATA_READ) {
  196. if (!host->chan_rx)
  197. disable_mmc_irqs(host, TMIO_MASK_READOP);
  198. dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
  199. host->mrq);
  200. } else {
  201. if (!host->chan_tx)
  202. disable_mmc_irqs(host, TMIO_MASK_WRITEOP);
  203. dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
  204. host->mrq);
  205. }
  206. if (stop) {
  207. if (stop->opcode == 12 && !stop->arg)
  208. sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
  209. else
  210. BUG();
  211. }
  212. tmio_mmc_finish_request(host);
  213. }
  214. static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
  215. {
  216. struct mmc_data *data = host->data;
  217. if (!data)
  218. return;
  219. if (host->chan_tx && (data->flags & MMC_DATA_WRITE)) {
  220. /*
  221. * Has all data been written out yet? Testing on SuperH showed,
  222. * that in most cases the first interrupt comes already with the
  223. * BUSY status bit clear, but on some operations, like mount or
  224. * in the beginning of a write / sync / umount, there is one
  225. * DATAEND interrupt with the BUSY bit set, in this cases
  226. * waiting for one more interrupt fixes the problem.
  227. */
  228. if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
  229. disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  230. tasklet_schedule(&host->dma_complete);
  231. }
  232. } else if (host->chan_rx && (data->flags & MMC_DATA_READ)) {
  233. disable_mmc_irqs(host, TMIO_STAT_DATAEND);
  234. tasklet_schedule(&host->dma_complete);
  235. } else {
  236. tmio_mmc_do_data_irq(host);
  237. }
  238. }
  239. static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
  240. unsigned int stat)
  241. {
  242. struct mmc_command *cmd = host->cmd;
  243. int i, addr;
  244. if (!host->cmd) {
  245. pr_debug("Spurious CMD irq\n");
  246. return;
  247. }
  248. host->cmd = NULL;
  249. /* This controller is sicker than the PXA one. Not only do we need to
  250. * drop the top 8 bits of the first response word, we also need to
  251. * modify the order of the response for short response command types.
  252. */
  253. for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
  254. cmd->resp[i] = sd_ctrl_read32(host, addr);
  255. if (cmd->flags & MMC_RSP_136) {
  256. cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
  257. cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
  258. cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
  259. cmd->resp[3] <<= 8;
  260. } else if (cmd->flags & MMC_RSP_R3) {
  261. cmd->resp[0] = cmd->resp[3];
  262. }
  263. if (stat & TMIO_STAT_CMDTIMEOUT)
  264. cmd->error = -ETIMEDOUT;
  265. else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
  266. cmd->error = -EILSEQ;
  267. /* If there is data to handle we enable data IRQs here, and
  268. * we will ultimatley finish the request in the data_end handler.
  269. * If theres no data or we encountered an error, finish now.
  270. */
  271. if (host->data && !cmd->error) {
  272. if (host->data->flags & MMC_DATA_READ) {
  273. if (!host->chan_rx)
  274. enable_mmc_irqs(host, TMIO_MASK_READOP);
  275. } else {
  276. struct dma_chan *chan = host->chan_tx;
  277. if (!chan)
  278. enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
  279. else
  280. tasklet_schedule(&host->dma_issue);
  281. }
  282. } else {
  283. tmio_mmc_finish_request(host);
  284. }
  285. return;
  286. }
  287. static irqreturn_t tmio_mmc_irq(int irq, void *devid)
  288. {
  289. struct tmio_mmc_host *host = devid;
  290. unsigned int ireg, irq_mask, status;
  291. pr_debug("MMC IRQ begin\n");
  292. status = sd_ctrl_read32(host, CTL_STATUS);
  293. irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
  294. ireg = status & TMIO_MASK_IRQ & ~irq_mask;
  295. pr_debug_status(status);
  296. pr_debug_status(ireg);
  297. if (!ireg) {
  298. disable_mmc_irqs(host, status & ~irq_mask);
  299. pr_warning("tmio_mmc: Spurious irq, disabling! "
  300. "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
  301. pr_debug_status(status);
  302. goto out;
  303. }
  304. while (ireg) {
  305. /* Card insert / remove attempts */
  306. if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
  307. ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
  308. TMIO_STAT_CARD_REMOVE);
  309. mmc_detect_change(host->mmc, msecs_to_jiffies(100));
  310. }
  311. /* CRC and other errors */
  312. /* if (ireg & TMIO_STAT_ERR_IRQ)
  313. * handled |= tmio_error_irq(host, irq, stat);
  314. */
  315. /* Command completion */
  316. if (ireg & TMIO_MASK_CMD) {
  317. ack_mmc_irqs(host, TMIO_MASK_CMD);
  318. tmio_mmc_cmd_irq(host, status);
  319. }
  320. /* Data transfer */
  321. if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
  322. ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
  323. tmio_mmc_pio_irq(host);
  324. }
  325. /* Data transfer completion */
  326. if (ireg & TMIO_STAT_DATAEND) {
  327. ack_mmc_irqs(host, TMIO_STAT_DATAEND);
  328. tmio_mmc_data_irq(host);
  329. }
  330. /* Check status - keep going until we've handled it all */
  331. status = sd_ctrl_read32(host, CTL_STATUS);
  332. irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
  333. ireg = status & TMIO_MASK_IRQ & ~irq_mask;
  334. pr_debug("Status at end of loop: %08x\n", status);
  335. pr_debug_status(status);
  336. }
  337. pr_debug("MMC IRQ end\n");
  338. out:
  339. return IRQ_HANDLED;
  340. }
  341. #ifdef CONFIG_TMIO_MMC_DMA
  342. static void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
  343. {
  344. #if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
  345. /* Switch DMA mode on or off - SuperH specific? */
  346. sd_ctrl_write16(host, 0xd8, enable ? 2 : 0);
  347. #endif
  348. }
  349. static void tmio_dma_complete(void *arg)
  350. {
  351. struct tmio_mmc_host *host = arg;
  352. dev_dbg(&host->pdev->dev, "Command completed\n");
  353. if (!host->data)
  354. dev_warn(&host->pdev->dev, "NULL data in DMA completion!\n");
  355. else
  356. enable_mmc_irqs(host, TMIO_STAT_DATAEND);
  357. }
  358. static int tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
  359. {
  360. struct scatterlist *sg = host->sg_ptr;
  361. struct dma_async_tx_descriptor *desc = NULL;
  362. struct dma_chan *chan = host->chan_rx;
  363. int ret;
  364. ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, DMA_FROM_DEVICE);
  365. if (ret > 0) {
  366. host->dma_sglen = ret;
  367. desc = chan->device->device_prep_slave_sg(chan, sg, ret,
  368. DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  369. }
  370. if (desc) {
  371. host->desc = desc;
  372. desc->callback = tmio_dma_complete;
  373. desc->callback_param = host;
  374. host->cookie = desc->tx_submit(desc);
  375. if (host->cookie < 0) {
  376. host->desc = NULL;
  377. ret = host->cookie;
  378. } else {
  379. chan->device->device_issue_pending(chan);
  380. }
  381. }
  382. dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
  383. __func__, host->sg_len, ret, host->cookie, host->mrq);
  384. if (!host->desc) {
  385. /* DMA failed, fall back to PIO */
  386. if (ret >= 0)
  387. ret = -EIO;
  388. host->chan_rx = NULL;
  389. dma_release_channel(chan);
  390. /* Free the Tx channel too */
  391. chan = host->chan_tx;
  392. if (chan) {
  393. host->chan_tx = NULL;
  394. dma_release_channel(chan);
  395. }
  396. dev_warn(&host->pdev->dev,
  397. "DMA failed: %d, falling back to PIO\n", ret);
  398. tmio_mmc_enable_dma(host, false);
  399. reset(host);
  400. /* Fail this request, let above layers recover */
  401. host->mrq->cmd->error = ret;
  402. tmio_mmc_finish_request(host);
  403. }
  404. dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
  405. desc, host->cookie, host->sg_len);
  406. return ret > 0 ? 0 : ret;
  407. }
  408. static int tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
  409. {
  410. struct scatterlist *sg = host->sg_ptr;
  411. struct dma_async_tx_descriptor *desc = NULL;
  412. struct dma_chan *chan = host->chan_tx;
  413. int ret;
  414. ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, DMA_TO_DEVICE);
  415. if (ret > 0) {
  416. host->dma_sglen = ret;
  417. desc = chan->device->device_prep_slave_sg(chan, sg, ret,
  418. DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  419. }
  420. if (desc) {
  421. host->desc = desc;
  422. desc->callback = tmio_dma_complete;
  423. desc->callback_param = host;
  424. host->cookie = desc->tx_submit(desc);
  425. if (host->cookie < 0) {
  426. host->desc = NULL;
  427. ret = host->cookie;
  428. }
  429. }
  430. dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
  431. __func__, host->sg_len, ret, host->cookie, host->mrq);
  432. if (!host->desc) {
  433. /* DMA failed, fall back to PIO */
  434. if (ret >= 0)
  435. ret = -EIO;
  436. host->chan_tx = NULL;
  437. dma_release_channel(chan);
  438. /* Free the Rx channel too */
  439. chan = host->chan_rx;
  440. if (chan) {
  441. host->chan_rx = NULL;
  442. dma_release_channel(chan);
  443. }
  444. dev_warn(&host->pdev->dev,
  445. "DMA failed: %d, falling back to PIO\n", ret);
  446. tmio_mmc_enable_dma(host, false);
  447. reset(host);
  448. /* Fail this request, let above layers recover */
  449. host->mrq->cmd->error = ret;
  450. tmio_mmc_finish_request(host);
  451. }
  452. dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
  453. desc, host->cookie);
  454. return ret > 0 ? 0 : ret;
  455. }
  456. static int tmio_mmc_start_dma(struct tmio_mmc_host *host,
  457. struct mmc_data *data)
  458. {
  459. if (data->flags & MMC_DATA_READ) {
  460. if (host->chan_rx)
  461. return tmio_mmc_start_dma_rx(host);
  462. } else {
  463. if (host->chan_tx)
  464. return tmio_mmc_start_dma_tx(host);
  465. }
  466. return 0;
  467. }
  468. static void tmio_issue_tasklet_fn(unsigned long priv)
  469. {
  470. struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
  471. struct dma_chan *chan = host->chan_tx;
  472. chan->device->device_issue_pending(chan);
  473. }
  474. static void tmio_tasklet_fn(unsigned long arg)
  475. {
  476. struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
  477. if (host->data->flags & MMC_DATA_READ)
  478. dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->dma_sglen,
  479. DMA_FROM_DEVICE);
  480. else
  481. dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->dma_sglen,
  482. DMA_TO_DEVICE);
  483. tmio_mmc_do_data_irq(host);
  484. }
  485. /* It might be necessary to make filter MFD specific */
  486. static bool tmio_mmc_filter(struct dma_chan *chan, void *arg)
  487. {
  488. dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
  489. chan->private = arg;
  490. return true;
  491. }
  492. static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
  493. struct tmio_mmc_data *pdata)
  494. {
  495. host->cookie = -EINVAL;
  496. host->desc = NULL;
  497. /* We can only either use DMA for both Tx and Rx or not use it at all */
  498. if (pdata->dma) {
  499. dma_cap_mask_t mask;
  500. dma_cap_zero(mask);
  501. dma_cap_set(DMA_SLAVE, mask);
  502. host->chan_tx = dma_request_channel(mask, tmio_mmc_filter,
  503. pdata->dma->chan_priv_tx);
  504. dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
  505. host->chan_tx);
  506. if (!host->chan_tx)
  507. return;
  508. host->chan_rx = dma_request_channel(mask, tmio_mmc_filter,
  509. pdata->dma->chan_priv_rx);
  510. dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
  511. host->chan_rx);
  512. if (!host->chan_rx) {
  513. dma_release_channel(host->chan_tx);
  514. host->chan_tx = NULL;
  515. return;
  516. }
  517. tasklet_init(&host->dma_complete, tmio_tasklet_fn, (unsigned long)host);
  518. tasklet_init(&host->dma_issue, tmio_issue_tasklet_fn, (unsigned long)host);
  519. tmio_mmc_enable_dma(host, true);
  520. }
  521. }
  522. static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
  523. {
  524. if (host->chan_tx) {
  525. struct dma_chan *chan = host->chan_tx;
  526. host->chan_tx = NULL;
  527. dma_release_channel(chan);
  528. }
  529. if (host->chan_rx) {
  530. struct dma_chan *chan = host->chan_rx;
  531. host->chan_rx = NULL;
  532. dma_release_channel(chan);
  533. }
  534. host->cookie = -EINVAL;
  535. host->desc = NULL;
  536. }
  537. #else
  538. static int tmio_mmc_start_dma(struct tmio_mmc_host *host,
  539. struct mmc_data *data)
  540. {
  541. return 0;
  542. }
  543. static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
  544. struct tmio_mmc_data *pdata)
  545. {
  546. host->chan_tx = NULL;
  547. host->chan_rx = NULL;
  548. }
  549. static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
  550. {
  551. }
  552. #endif
  553. static int tmio_mmc_start_data(struct tmio_mmc_host *host,
  554. struct mmc_data *data)
  555. {
  556. pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
  557. data->blksz, data->blocks);
  558. /* Hardware cannot perform 1 and 2 byte requests in 4 bit mode */
  559. if (data->blksz < 4 && host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
  560. pr_err("%s: %d byte block unsupported in 4 bit mode\n",
  561. mmc_hostname(host->mmc), data->blksz);
  562. return -EINVAL;
  563. }
  564. tmio_mmc_init_sg(host, data);
  565. host->data = data;
  566. /* Set transfer length / blocksize */
  567. sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
  568. sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
  569. return tmio_mmc_start_dma(host, data);
  570. }
  571. /* Process requests from the MMC layer */
  572. static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  573. {
  574. struct tmio_mmc_host *host = mmc_priv(mmc);
  575. int ret;
  576. if (host->mrq)
  577. pr_debug("request not null\n");
  578. host->mrq = mrq;
  579. if (mrq->data) {
  580. ret = tmio_mmc_start_data(host, mrq->data);
  581. if (ret)
  582. goto fail;
  583. }
  584. ret = tmio_mmc_start_command(host, mrq->cmd);
  585. if (!ret)
  586. return;
  587. fail:
  588. mrq->cmd->error = ret;
  589. mmc_request_done(mmc, mrq);
  590. }
  591. /* Set MMC clock / power.
  592. * Note: This controller uses a simple divider scheme therefore it cannot
  593. * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
  594. * MMC wont run that fast, it has to be clocked at 12MHz which is the next
  595. * slowest setting.
  596. */
  597. static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  598. {
  599. struct tmio_mmc_host *host = mmc_priv(mmc);
  600. if (ios->clock)
  601. tmio_mmc_set_clock(host, ios->clock);
  602. /* Power sequence - OFF -> ON -> UP */
  603. switch (ios->power_mode) {
  604. case MMC_POWER_OFF: /* power down SD bus */
  605. if (host->set_pwr)
  606. host->set_pwr(host->pdev, 0);
  607. tmio_mmc_clk_stop(host);
  608. break;
  609. case MMC_POWER_ON: /* power up SD bus */
  610. if (host->set_pwr)
  611. host->set_pwr(host->pdev, 1);
  612. break;
  613. case MMC_POWER_UP: /* start bus clock */
  614. tmio_mmc_clk_start(host);
  615. break;
  616. }
  617. switch (ios->bus_width) {
  618. case MMC_BUS_WIDTH_1:
  619. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
  620. break;
  621. case MMC_BUS_WIDTH_4:
  622. sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
  623. break;
  624. }
  625. /* Let things settle. delay taken from winCE driver */
  626. udelay(140);
  627. }
  628. static int tmio_mmc_get_ro(struct mmc_host *mmc)
  629. {
  630. struct tmio_mmc_host *host = mmc_priv(mmc);
  631. struct mfd_cell *cell = host->pdev->dev.platform_data;
  632. struct tmio_mmc_data *pdata = cell->driver_data;
  633. return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
  634. (sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT)) ? 0 : 1;
  635. }
  636. static const struct mmc_host_ops tmio_mmc_ops = {
  637. .request = tmio_mmc_request,
  638. .set_ios = tmio_mmc_set_ios,
  639. .get_ro = tmio_mmc_get_ro,
  640. };
  641. #ifdef CONFIG_PM
  642. static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state)
  643. {
  644. struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
  645. struct mmc_host *mmc = platform_get_drvdata(dev);
  646. int ret;
  647. ret = mmc_suspend_host(mmc);
  648. /* Tell MFD core it can disable us now.*/
  649. if (!ret && cell->disable)
  650. cell->disable(dev);
  651. return ret;
  652. }
  653. static int tmio_mmc_resume(struct platform_device *dev)
  654. {
  655. struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
  656. struct mmc_host *mmc = platform_get_drvdata(dev);
  657. int ret = 0;
  658. /* Tell the MFD core we are ready to be enabled */
  659. if (cell->resume) {
  660. ret = cell->resume(dev);
  661. if (ret)
  662. goto out;
  663. }
  664. mmc_resume_host(mmc);
  665. out:
  666. return ret;
  667. }
  668. #else
  669. #define tmio_mmc_suspend NULL
  670. #define tmio_mmc_resume NULL
  671. #endif
  672. static int __devinit tmio_mmc_probe(struct platform_device *dev)
  673. {
  674. struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
  675. struct tmio_mmc_data *pdata;
  676. struct resource *res_ctl;
  677. struct tmio_mmc_host *host;
  678. struct mmc_host *mmc;
  679. int ret = -EINVAL;
  680. u32 irq_mask = TMIO_MASK_CMD;
  681. if (dev->num_resources != 2)
  682. goto out;
  683. res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0);
  684. if (!res_ctl)
  685. goto out;
  686. pdata = cell->driver_data;
  687. if (!pdata || !pdata->hclk)
  688. goto out;
  689. ret = -ENOMEM;
  690. mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev);
  691. if (!mmc)
  692. goto out;
  693. host = mmc_priv(mmc);
  694. host->mmc = mmc;
  695. host->pdev = dev;
  696. platform_set_drvdata(dev, mmc);
  697. host->set_pwr = pdata->set_pwr;
  698. host->set_clk_div = pdata->set_clk_div;
  699. /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
  700. host->bus_shift = resource_size(res_ctl) >> 10;
  701. host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
  702. if (!host->ctl)
  703. goto host_free;
  704. mmc->ops = &tmio_mmc_ops;
  705. mmc->caps = MMC_CAP_4_BIT_DATA;
  706. mmc->caps |= pdata->capabilities;
  707. mmc->f_max = pdata->hclk;
  708. mmc->f_min = mmc->f_max / 512;
  709. if (pdata->ocr_mask)
  710. mmc->ocr_avail = pdata->ocr_mask;
  711. else
  712. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  713. /* Tell the MFD core we are ready to be enabled */
  714. if (cell->enable) {
  715. ret = cell->enable(dev);
  716. if (ret)
  717. goto unmap_ctl;
  718. }
  719. tmio_mmc_clk_stop(host);
  720. reset(host);
  721. ret = platform_get_irq(dev, 0);
  722. if (ret >= 0)
  723. host->irq = ret;
  724. else
  725. goto cell_disable;
  726. disable_mmc_irqs(host, TMIO_MASK_ALL);
  727. ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED |
  728. IRQF_TRIGGER_FALLING, dev_name(&dev->dev), host);
  729. if (ret)
  730. goto cell_disable;
  731. /* See if we also get DMA */
  732. tmio_mmc_request_dma(host, pdata);
  733. mmc_add_host(mmc);
  734. pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
  735. (unsigned long)host->ctl, host->irq);
  736. /* Unmask the IRQs we want to know about */
  737. if (!host->chan_rx)
  738. irq_mask |= TMIO_MASK_READOP;
  739. if (!host->chan_tx)
  740. irq_mask |= TMIO_MASK_WRITEOP;
  741. enable_mmc_irqs(host, irq_mask);
  742. return 0;
  743. cell_disable:
  744. if (cell->disable)
  745. cell->disable(dev);
  746. unmap_ctl:
  747. iounmap(host->ctl);
  748. host_free:
  749. mmc_free_host(mmc);
  750. out:
  751. return ret;
  752. }
  753. static int __devexit tmio_mmc_remove(struct platform_device *dev)
  754. {
  755. struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
  756. struct mmc_host *mmc = platform_get_drvdata(dev);
  757. platform_set_drvdata(dev, NULL);
  758. if (mmc) {
  759. struct tmio_mmc_host *host = mmc_priv(mmc);
  760. mmc_remove_host(mmc);
  761. tmio_mmc_release_dma(host);
  762. free_irq(host->irq, host);
  763. if (cell->disable)
  764. cell->disable(dev);
  765. iounmap(host->ctl);
  766. mmc_free_host(mmc);
  767. }
  768. return 0;
  769. }
  770. /* ------------------- device registration ----------------------- */
  771. static struct platform_driver tmio_mmc_driver = {
  772. .driver = {
  773. .name = "tmio-mmc",
  774. .owner = THIS_MODULE,
  775. },
  776. .probe = tmio_mmc_probe,
  777. .remove = __devexit_p(tmio_mmc_remove),
  778. .suspend = tmio_mmc_suspend,
  779. .resume = tmio_mmc_resume,
  780. };
  781. static int __init tmio_mmc_init(void)
  782. {
  783. return platform_driver_register(&tmio_mmc_driver);
  784. }
  785. static void __exit tmio_mmc_exit(void)
  786. {
  787. platform_driver_unregister(&tmio_mmc_driver);
  788. }
  789. module_init(tmio_mmc_init);
  790. module_exit(tmio_mmc_exit);
  791. MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
  792. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
  793. MODULE_LICENSE("GPL v2");
  794. MODULE_ALIAS("platform:tmio-mmc");