tmio_mmc.c 23 KB

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