moxart-mmc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * MOXA ART MMC host driver.
  3. *
  4. * Copyright (C) 2014 Jonas Jensen
  5. *
  6. * Jonas Jensen <jonas.jensen@gmail.com>
  7. *
  8. * Based on code from
  9. * Moxa Technologies Co., Ltd. <www.moxa.com>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/delay.h>
  19. #include <linux/errno.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/mmc/host.h>
  25. #include <linux/mmc/sd.h>
  26. #include <linux/sched.h>
  27. #include <linux/io.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/clk.h>
  31. #include <linux/bitops.h>
  32. #include <linux/of_dma.h>
  33. #include <linux/spinlock.h>
  34. #define REG_COMMAND 0
  35. #define REG_ARGUMENT 4
  36. #define REG_RESPONSE0 8
  37. #define REG_RESPONSE1 12
  38. #define REG_RESPONSE2 16
  39. #define REG_RESPONSE3 20
  40. #define REG_RESPONSE_COMMAND 24
  41. #define REG_DATA_CONTROL 28
  42. #define REG_DATA_TIMER 32
  43. #define REG_DATA_LENGTH 36
  44. #define REG_STATUS 40
  45. #define REG_CLEAR 44
  46. #define REG_INTERRUPT_MASK 48
  47. #define REG_POWER_CONTROL 52
  48. #define REG_CLOCK_CONTROL 56
  49. #define REG_BUS_WIDTH 60
  50. #define REG_DATA_WINDOW 64
  51. #define REG_FEATURE 68
  52. #define REG_REVISION 72
  53. /* REG_COMMAND */
  54. #define CMD_SDC_RESET BIT(10)
  55. #define CMD_EN BIT(9)
  56. #define CMD_APP_CMD BIT(8)
  57. #define CMD_LONG_RSP BIT(7)
  58. #define CMD_NEED_RSP BIT(6)
  59. #define CMD_IDX_MASK 0x3f
  60. /* REG_RESPONSE_COMMAND */
  61. #define RSP_CMD_APP BIT(6)
  62. #define RSP_CMD_IDX_MASK 0x3f
  63. /* REG_DATA_CONTROL */
  64. #define DCR_DATA_FIFO_RESET BIT(8)
  65. #define DCR_DATA_THRES BIT(7)
  66. #define DCR_DATA_EN BIT(6)
  67. #define DCR_DMA_EN BIT(5)
  68. #define DCR_DATA_WRITE BIT(4)
  69. #define DCR_BLK_SIZE 0x0f
  70. /* REG_DATA_LENGTH */
  71. #define DATA_LEN_MASK 0xffffff
  72. /* REG_STATUS */
  73. #define WRITE_PROT BIT(12)
  74. #define CARD_DETECT BIT(11)
  75. /* 1-10 below can be sent to either registers, interrupt or clear. */
  76. #define CARD_CHANGE BIT(10)
  77. #define FIFO_ORUN BIT(9)
  78. #define FIFO_URUN BIT(8)
  79. #define DATA_END BIT(7)
  80. #define CMD_SENT BIT(6)
  81. #define DATA_CRC_OK BIT(5)
  82. #define RSP_CRC_OK BIT(4)
  83. #define DATA_TIMEOUT BIT(3)
  84. #define RSP_TIMEOUT BIT(2)
  85. #define DATA_CRC_FAIL BIT(1)
  86. #define RSP_CRC_FAIL BIT(0)
  87. #define MASK_RSP (RSP_TIMEOUT | RSP_CRC_FAIL | \
  88. RSP_CRC_OK | CARD_DETECT | CMD_SENT)
  89. #define MASK_DATA (DATA_CRC_OK | DATA_END | \
  90. DATA_CRC_FAIL | DATA_TIMEOUT)
  91. #define MASK_INTR_PIO (FIFO_URUN | FIFO_ORUN | CARD_CHANGE)
  92. /* REG_POWER_CONTROL */
  93. #define SD_POWER_ON BIT(4)
  94. #define SD_POWER_MASK 0x0f
  95. /* REG_CLOCK_CONTROL */
  96. #define CLK_HISPD BIT(9)
  97. #define CLK_OFF BIT(8)
  98. #define CLK_SD BIT(7)
  99. #define CLK_DIV_MASK 0x7f
  100. /* REG_BUS_WIDTH */
  101. #define BUS_WIDTH_8 BIT(2)
  102. #define BUS_WIDTH_4 BIT(1)
  103. #define BUS_WIDTH_1 BIT(0)
  104. #define MMC_VDD_360 23
  105. #define MIN_POWER (MMC_VDD_360 - SD_POWER_MASK)
  106. #define MAX_RETRIES 500000
  107. struct moxart_host {
  108. spinlock_t lock;
  109. void __iomem *base;
  110. phys_addr_t reg_phys;
  111. struct dma_chan *dma_chan_tx;
  112. struct dma_chan *dma_chan_rx;
  113. struct dma_async_tx_descriptor *tx_desc;
  114. struct mmc_host *mmc;
  115. struct mmc_request *mrq;
  116. struct scatterlist *cur_sg;
  117. struct completion dma_complete;
  118. struct completion pio_complete;
  119. u32 num_sg;
  120. u32 data_remain;
  121. u32 data_len;
  122. u32 fifo_width;
  123. u32 timeout;
  124. u32 rate;
  125. long sysclk;
  126. bool have_dma;
  127. bool is_removed;
  128. };
  129. static inline void moxart_init_sg(struct moxart_host *host,
  130. struct mmc_data *data)
  131. {
  132. host->cur_sg = data->sg;
  133. host->num_sg = data->sg_len;
  134. host->data_remain = host->cur_sg->length;
  135. if (host->data_remain > host->data_len)
  136. host->data_remain = host->data_len;
  137. }
  138. static inline int moxart_next_sg(struct moxart_host *host)
  139. {
  140. int remain;
  141. struct mmc_data *data = host->mrq->cmd->data;
  142. host->cur_sg++;
  143. host->num_sg--;
  144. if (host->num_sg > 0) {
  145. host->data_remain = host->cur_sg->length;
  146. remain = host->data_len - data->bytes_xfered;
  147. if (remain > 0 && remain < host->data_remain)
  148. host->data_remain = remain;
  149. }
  150. return host->num_sg;
  151. }
  152. static int moxart_wait_for_status(struct moxart_host *host,
  153. u32 mask, u32 *status)
  154. {
  155. int ret = -ETIMEDOUT;
  156. u32 i;
  157. for (i = 0; i < MAX_RETRIES; i++) {
  158. *status = readl(host->base + REG_STATUS);
  159. if (!(*status & mask)) {
  160. udelay(5);
  161. continue;
  162. }
  163. writel(*status & mask, host->base + REG_CLEAR);
  164. ret = 0;
  165. break;
  166. }
  167. if (ret)
  168. dev_err(mmc_dev(host->mmc), "timed out waiting for status\n");
  169. return ret;
  170. }
  171. static void moxart_send_command(struct moxart_host *host,
  172. struct mmc_command *cmd)
  173. {
  174. u32 status, cmdctrl;
  175. writel(RSP_TIMEOUT | RSP_CRC_OK |
  176. RSP_CRC_FAIL | CMD_SENT, host->base + REG_CLEAR);
  177. writel(cmd->arg, host->base + REG_ARGUMENT);
  178. cmdctrl = cmd->opcode & CMD_IDX_MASK;
  179. if (cmdctrl == SD_APP_SET_BUS_WIDTH || cmdctrl == SD_APP_OP_COND ||
  180. cmdctrl == SD_APP_SEND_SCR || cmdctrl == SD_APP_SD_STATUS ||
  181. cmdctrl == SD_APP_SEND_NUM_WR_BLKS)
  182. cmdctrl |= CMD_APP_CMD;
  183. if (cmd->flags & MMC_RSP_PRESENT)
  184. cmdctrl |= CMD_NEED_RSP;
  185. if (cmd->flags & MMC_RSP_136)
  186. cmdctrl |= CMD_LONG_RSP;
  187. writel(cmdctrl | CMD_EN, host->base + REG_COMMAND);
  188. if (moxart_wait_for_status(host, MASK_RSP, &status) == -ETIMEDOUT)
  189. cmd->error = -ETIMEDOUT;
  190. if (status & RSP_TIMEOUT) {
  191. cmd->error = -ETIMEDOUT;
  192. return;
  193. }
  194. if (status & RSP_CRC_FAIL) {
  195. cmd->error = -EIO;
  196. return;
  197. }
  198. if (status & RSP_CRC_OK) {
  199. if (cmd->flags & MMC_RSP_136) {
  200. cmd->resp[3] = readl(host->base + REG_RESPONSE0);
  201. cmd->resp[2] = readl(host->base + REG_RESPONSE1);
  202. cmd->resp[1] = readl(host->base + REG_RESPONSE2);
  203. cmd->resp[0] = readl(host->base + REG_RESPONSE3);
  204. } else {
  205. cmd->resp[0] = readl(host->base + REG_RESPONSE0);
  206. }
  207. }
  208. }
  209. static void moxart_dma_complete(void *param)
  210. {
  211. struct moxart_host *host = param;
  212. complete(&host->dma_complete);
  213. }
  214. static void moxart_transfer_dma(struct mmc_data *data, struct moxart_host *host)
  215. {
  216. u32 len, dir_data, dir_slave;
  217. long dma_time;
  218. struct dma_async_tx_descriptor *desc = NULL;
  219. struct dma_chan *dma_chan;
  220. if (host->data_len == data->bytes_xfered)
  221. return;
  222. if (data->flags & MMC_DATA_WRITE) {
  223. dma_chan = host->dma_chan_tx;
  224. dir_data = DMA_TO_DEVICE;
  225. dir_slave = DMA_MEM_TO_DEV;
  226. } else {
  227. dma_chan = host->dma_chan_rx;
  228. dir_data = DMA_FROM_DEVICE;
  229. dir_slave = DMA_DEV_TO_MEM;
  230. }
  231. len = dma_map_sg(dma_chan->device->dev, data->sg,
  232. data->sg_len, dir_data);
  233. if (len > 0) {
  234. desc = dmaengine_prep_slave_sg(dma_chan, data->sg,
  235. len, dir_slave,
  236. DMA_PREP_INTERRUPT |
  237. DMA_CTRL_ACK);
  238. } else {
  239. dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
  240. }
  241. if (desc) {
  242. host->tx_desc = desc;
  243. desc->callback = moxart_dma_complete;
  244. desc->callback_param = host;
  245. dmaengine_submit(desc);
  246. dma_async_issue_pending(dma_chan);
  247. }
  248. data->bytes_xfered += host->data_remain;
  249. dma_time = wait_for_completion_interruptible_timeout(
  250. &host->dma_complete, host->timeout);
  251. dma_unmap_sg(dma_chan->device->dev,
  252. data->sg, data->sg_len,
  253. dir_data);
  254. }
  255. static void moxart_transfer_pio(struct moxart_host *host)
  256. {
  257. struct mmc_data *data = host->mrq->cmd->data;
  258. u32 *sgp, len = 0, remain, status;
  259. if (host->data_len == data->bytes_xfered)
  260. return;
  261. sgp = sg_virt(host->cur_sg);
  262. remain = host->data_remain;
  263. if (data->flags & MMC_DATA_WRITE) {
  264. while (remain > 0) {
  265. if (moxart_wait_for_status(host, FIFO_URUN, &status)
  266. == -ETIMEDOUT) {
  267. data->error = -ETIMEDOUT;
  268. complete(&host->pio_complete);
  269. return;
  270. }
  271. for (len = 0; len < remain && len < host->fifo_width;) {
  272. iowrite32(*sgp, host->base + REG_DATA_WINDOW);
  273. sgp++;
  274. len += 4;
  275. }
  276. remain -= len;
  277. }
  278. } else {
  279. while (remain > 0) {
  280. if (moxart_wait_for_status(host, FIFO_ORUN, &status)
  281. == -ETIMEDOUT) {
  282. data->error = -ETIMEDOUT;
  283. complete(&host->pio_complete);
  284. return;
  285. }
  286. for (len = 0; len < remain && len < host->fifo_width;) {
  287. /* SCR data must be read in big endian. */
  288. if (data->mrq->cmd->opcode == SD_APP_SEND_SCR)
  289. *sgp = ioread32be(host->base +
  290. REG_DATA_WINDOW);
  291. else
  292. *sgp = ioread32(host->base +
  293. REG_DATA_WINDOW);
  294. sgp++;
  295. len += 4;
  296. }
  297. remain -= len;
  298. }
  299. }
  300. data->bytes_xfered += host->data_remain - remain;
  301. host->data_remain = remain;
  302. if (host->data_len != data->bytes_xfered)
  303. moxart_next_sg(host);
  304. else
  305. complete(&host->pio_complete);
  306. }
  307. static void moxart_prepare_data(struct moxart_host *host)
  308. {
  309. struct mmc_data *data = host->mrq->cmd->data;
  310. u32 datactrl;
  311. int blksz_bits;
  312. if (!data)
  313. return;
  314. host->data_len = data->blocks * data->blksz;
  315. blksz_bits = ffs(data->blksz) - 1;
  316. BUG_ON(1 << blksz_bits != data->blksz);
  317. moxart_init_sg(host, data);
  318. datactrl = DCR_DATA_EN | (blksz_bits & DCR_BLK_SIZE);
  319. if (data->flags & MMC_DATA_WRITE)
  320. datactrl |= DCR_DATA_WRITE;
  321. if ((host->data_len > host->fifo_width) && host->have_dma)
  322. datactrl |= DCR_DMA_EN;
  323. writel(DCR_DATA_FIFO_RESET, host->base + REG_DATA_CONTROL);
  324. writel(MASK_DATA | FIFO_URUN | FIFO_ORUN, host->base + REG_CLEAR);
  325. writel(host->rate, host->base + REG_DATA_TIMER);
  326. writel(host->data_len, host->base + REG_DATA_LENGTH);
  327. writel(datactrl, host->base + REG_DATA_CONTROL);
  328. }
  329. static void moxart_request(struct mmc_host *mmc, struct mmc_request *mrq)
  330. {
  331. struct moxart_host *host = mmc_priv(mmc);
  332. long pio_time;
  333. unsigned long flags;
  334. u32 status;
  335. spin_lock_irqsave(&host->lock, flags);
  336. init_completion(&host->dma_complete);
  337. init_completion(&host->pio_complete);
  338. host->mrq = mrq;
  339. if (readl(host->base + REG_STATUS) & CARD_DETECT) {
  340. mrq->cmd->error = -ETIMEDOUT;
  341. goto request_done;
  342. }
  343. moxart_prepare_data(host);
  344. moxart_send_command(host, host->mrq->cmd);
  345. if (mrq->cmd->data) {
  346. if ((host->data_len > host->fifo_width) && host->have_dma) {
  347. writel(CARD_CHANGE, host->base + REG_INTERRUPT_MASK);
  348. spin_unlock_irqrestore(&host->lock, flags);
  349. moxart_transfer_dma(mrq->cmd->data, host);
  350. spin_lock_irqsave(&host->lock, flags);
  351. } else {
  352. writel(MASK_INTR_PIO, host->base + REG_INTERRUPT_MASK);
  353. spin_unlock_irqrestore(&host->lock, flags);
  354. /* PIO transfers start from interrupt. */
  355. pio_time = wait_for_completion_interruptible_timeout(
  356. &host->pio_complete, host->timeout);
  357. spin_lock_irqsave(&host->lock, flags);
  358. }
  359. if (host->is_removed) {
  360. dev_err(mmc_dev(host->mmc), "card removed\n");
  361. mrq->cmd->error = -ETIMEDOUT;
  362. goto request_done;
  363. }
  364. if (moxart_wait_for_status(host, MASK_DATA, &status)
  365. == -ETIMEDOUT) {
  366. mrq->cmd->data->error = -ETIMEDOUT;
  367. goto request_done;
  368. }
  369. if (status & DATA_CRC_FAIL)
  370. mrq->cmd->data->error = -ETIMEDOUT;
  371. if (mrq->cmd->data->stop)
  372. moxart_send_command(host, mrq->cmd->data->stop);
  373. }
  374. request_done:
  375. spin_unlock_irqrestore(&host->lock, flags);
  376. mmc_request_done(host->mmc, mrq);
  377. }
  378. static irqreturn_t moxart_irq(int irq, void *devid)
  379. {
  380. struct moxart_host *host = (struct moxart_host *)devid;
  381. u32 status;
  382. unsigned long flags;
  383. spin_lock_irqsave(&host->lock, flags);
  384. status = readl(host->base + REG_STATUS);
  385. if (status & CARD_CHANGE) {
  386. host->is_removed = status & CARD_DETECT;
  387. if (host->is_removed && host->have_dma) {
  388. dmaengine_terminate_all(host->dma_chan_tx);
  389. dmaengine_terminate_all(host->dma_chan_rx);
  390. }
  391. host->mrq = NULL;
  392. writel(MASK_INTR_PIO, host->base + REG_CLEAR);
  393. writel(CARD_CHANGE, host->base + REG_INTERRUPT_MASK);
  394. mmc_detect_change(host->mmc, 0);
  395. }
  396. if (status & (FIFO_ORUN | FIFO_URUN) && host->mrq)
  397. moxart_transfer_pio(host);
  398. spin_unlock_irqrestore(&host->lock, flags);
  399. return IRQ_HANDLED;
  400. }
  401. static void moxart_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  402. {
  403. struct moxart_host *host = mmc_priv(mmc);
  404. unsigned long flags;
  405. u8 power, div;
  406. u32 ctrl;
  407. spin_lock_irqsave(&host->lock, flags);
  408. if (ios->clock) {
  409. for (div = 0; div < CLK_DIV_MASK; ++div) {
  410. if (ios->clock >= host->sysclk / (2 * (div + 1)))
  411. break;
  412. }
  413. ctrl = CLK_SD | div;
  414. host->rate = host->sysclk / (2 * (div + 1));
  415. if (host->rate > host->sysclk)
  416. ctrl |= CLK_HISPD;
  417. writel(ctrl, host->base + REG_CLOCK_CONTROL);
  418. }
  419. if (ios->power_mode == MMC_POWER_OFF) {
  420. writel(readl(host->base + REG_POWER_CONTROL) & ~SD_POWER_ON,
  421. host->base + REG_POWER_CONTROL);
  422. } else {
  423. if (ios->vdd < MIN_POWER)
  424. power = 0;
  425. else
  426. power = ios->vdd - MIN_POWER;
  427. writel(SD_POWER_ON | (u32) power,
  428. host->base + REG_POWER_CONTROL);
  429. }
  430. switch (ios->bus_width) {
  431. case MMC_BUS_WIDTH_4:
  432. writel(BUS_WIDTH_4, host->base + REG_BUS_WIDTH);
  433. break;
  434. case MMC_BUS_WIDTH_8:
  435. writel(BUS_WIDTH_8, host->base + REG_BUS_WIDTH);
  436. break;
  437. default:
  438. writel(BUS_WIDTH_1, host->base + REG_BUS_WIDTH);
  439. break;
  440. }
  441. spin_unlock_irqrestore(&host->lock, flags);
  442. }
  443. static int moxart_get_ro(struct mmc_host *mmc)
  444. {
  445. struct moxart_host *host = mmc_priv(mmc);
  446. return !!(readl(host->base + REG_STATUS) & WRITE_PROT);
  447. }
  448. static struct mmc_host_ops moxart_ops = {
  449. .request = moxart_request,
  450. .set_ios = moxart_set_ios,
  451. .get_ro = moxart_get_ro,
  452. };
  453. static int moxart_probe(struct platform_device *pdev)
  454. {
  455. struct device *dev = &pdev->dev;
  456. struct device_node *node = dev->of_node;
  457. struct resource res_mmc;
  458. struct mmc_host *mmc;
  459. struct moxart_host *host = NULL;
  460. struct dma_slave_config cfg;
  461. struct clk *clk;
  462. void __iomem *reg_mmc;
  463. int irq, ret;
  464. u32 i;
  465. mmc = mmc_alloc_host(sizeof(struct moxart_host), dev);
  466. if (!mmc) {
  467. dev_err(dev, "mmc_alloc_host failed\n");
  468. ret = -ENOMEM;
  469. goto out;
  470. }
  471. ret = of_address_to_resource(node, 0, &res_mmc);
  472. if (ret) {
  473. dev_err(dev, "of_address_to_resource failed\n");
  474. goto out;
  475. }
  476. irq = irq_of_parse_and_map(node, 0);
  477. if (irq <= 0) {
  478. dev_err(dev, "irq_of_parse_and_map failed\n");
  479. ret = -EINVAL;
  480. goto out;
  481. }
  482. clk = devm_clk_get(dev, NULL);
  483. if (IS_ERR(clk)) {
  484. ret = PTR_ERR(clk);
  485. goto out;
  486. }
  487. reg_mmc = devm_ioremap_resource(dev, &res_mmc);
  488. if (IS_ERR(reg_mmc)) {
  489. ret = PTR_ERR(reg_mmc);
  490. goto out;
  491. }
  492. ret = mmc_of_parse(mmc);
  493. if (ret)
  494. goto out;
  495. host = mmc_priv(mmc);
  496. host->mmc = mmc;
  497. host->base = reg_mmc;
  498. host->reg_phys = res_mmc.start;
  499. host->timeout = msecs_to_jiffies(1000);
  500. host->sysclk = clk_get_rate(clk);
  501. host->fifo_width = readl(host->base + REG_FEATURE) << 2;
  502. host->dma_chan_tx = dma_request_slave_channel_reason(dev, "tx");
  503. host->dma_chan_rx = dma_request_slave_channel_reason(dev, "rx");
  504. spin_lock_init(&host->lock);
  505. mmc->ops = &moxart_ops;
  506. mmc->f_max = DIV_ROUND_CLOSEST(host->sysclk, 2);
  507. mmc->f_min = DIV_ROUND_CLOSEST(host->sysclk, CLK_DIV_MASK * 2);
  508. mmc->ocr_avail = 0xffff00; /* Support 2.0v - 3.6v power. */
  509. if (IS_ERR(host->dma_chan_tx) || IS_ERR(host->dma_chan_rx)) {
  510. if (PTR_ERR(host->dma_chan_tx) == -EPROBE_DEFER ||
  511. PTR_ERR(host->dma_chan_rx) == -EPROBE_DEFER) {
  512. ret = -EPROBE_DEFER;
  513. goto out;
  514. }
  515. dev_dbg(dev, "PIO mode transfer enabled\n");
  516. host->have_dma = false;
  517. } else {
  518. dev_dbg(dev, "DMA channels found (%p,%p)\n",
  519. host->dma_chan_tx, host->dma_chan_rx);
  520. host->have_dma = true;
  521. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  522. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  523. cfg.direction = DMA_MEM_TO_DEV;
  524. cfg.src_addr = 0;
  525. cfg.dst_addr = host->reg_phys + REG_DATA_WINDOW;
  526. dmaengine_slave_config(host->dma_chan_tx, &cfg);
  527. cfg.direction = DMA_DEV_TO_MEM;
  528. cfg.src_addr = host->reg_phys + REG_DATA_WINDOW;
  529. cfg.dst_addr = 0;
  530. dmaengine_slave_config(host->dma_chan_rx, &cfg);
  531. }
  532. switch ((readl(host->base + REG_BUS_WIDTH) >> 3) & 3) {
  533. case 1:
  534. mmc->caps |= MMC_CAP_4_BIT_DATA;
  535. break;
  536. case 2:
  537. mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
  538. break;
  539. default:
  540. break;
  541. }
  542. writel(0, host->base + REG_INTERRUPT_MASK);
  543. writel(CMD_SDC_RESET, host->base + REG_COMMAND);
  544. for (i = 0; i < MAX_RETRIES; i++) {
  545. if (!(readl(host->base + REG_COMMAND) & CMD_SDC_RESET))
  546. break;
  547. udelay(5);
  548. }
  549. ret = devm_request_irq(dev, irq, moxart_irq, 0, "moxart-mmc", host);
  550. if (ret)
  551. goto out;
  552. dev_set_drvdata(dev, mmc);
  553. mmc_add_host(mmc);
  554. dev_dbg(dev, "IRQ=%d, FIFO is %d bytes\n", irq, host->fifo_width);
  555. return 0;
  556. out:
  557. if (mmc)
  558. mmc_free_host(mmc);
  559. return ret;
  560. }
  561. static int moxart_remove(struct platform_device *pdev)
  562. {
  563. struct mmc_host *mmc = dev_get_drvdata(&pdev->dev);
  564. struct moxart_host *host = mmc_priv(mmc);
  565. dev_set_drvdata(&pdev->dev, NULL);
  566. if (mmc) {
  567. if (!IS_ERR(host->dma_chan_tx))
  568. dma_release_channel(host->dma_chan_tx);
  569. if (!IS_ERR(host->dma_chan_rx))
  570. dma_release_channel(host->dma_chan_rx);
  571. mmc_remove_host(mmc);
  572. mmc_free_host(mmc);
  573. writel(0, host->base + REG_INTERRUPT_MASK);
  574. writel(0, host->base + REG_POWER_CONTROL);
  575. writel(readl(host->base + REG_CLOCK_CONTROL) | CLK_OFF,
  576. host->base + REG_CLOCK_CONTROL);
  577. }
  578. return 0;
  579. }
  580. static const struct of_device_id moxart_mmc_match[] = {
  581. { .compatible = "moxa,moxart-mmc" },
  582. { .compatible = "faraday,ftsdc010" },
  583. { }
  584. };
  585. MODULE_DEVICE_TABLE(of, moxart_mmc_match);
  586. static struct platform_driver moxart_mmc_driver = {
  587. .probe = moxart_probe,
  588. .remove = moxart_remove,
  589. .driver = {
  590. .name = "mmc-moxart",
  591. .of_match_table = moxart_mmc_match,
  592. },
  593. };
  594. module_platform_driver(moxart_mmc_driver);
  595. MODULE_ALIAS("platform:mmc-moxart");
  596. MODULE_DESCRIPTION("MOXA ART MMC driver");
  597. MODULE_LICENSE("GPL v2");
  598. MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");