mxcmmc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. /*
  2. * linux/drivers/mmc/host/mxcmmc.c - Freescale i.MX MMCI driver
  3. *
  4. * This is a driver for the SDHC controller found in Freescale MX2/MX3
  5. * SoCs. It is basically the same hardware as found on MX1 (imxmmc.c).
  6. * Unlike the hardware found on MX1, this hardware just works and does
  7. * not need all the quirks found in imxmmc.c, hence the separate driver.
  8. *
  9. * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  10. * Copyright (C) 2006 Pavel Pisa, PiKRON <ppisa@pikron.com>
  11. *
  12. * derived from pxamci.c by Russell King
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/ioport.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/mmc/host.h>
  28. #include <linux/mmc/card.h>
  29. #include <linux/delay.h>
  30. #include <linux/clk.h>
  31. #include <linux/io.h>
  32. #include <linux/gpio.h>
  33. #include <asm/dma.h>
  34. #include <asm/irq.h>
  35. #include <asm/sizes.h>
  36. #include <mach/mmc.h>
  37. #ifdef CONFIG_ARCH_MX2
  38. #include <mach/dma-mx1-mx2.h>
  39. #define HAS_DMA
  40. #endif
  41. #define DRIVER_NAME "mxc-mmc"
  42. #define MMC_REG_STR_STP_CLK 0x00
  43. #define MMC_REG_STATUS 0x04
  44. #define MMC_REG_CLK_RATE 0x08
  45. #define MMC_REG_CMD_DAT_CONT 0x0C
  46. #define MMC_REG_RES_TO 0x10
  47. #define MMC_REG_READ_TO 0x14
  48. #define MMC_REG_BLK_LEN 0x18
  49. #define MMC_REG_NOB 0x1C
  50. #define MMC_REG_REV_NO 0x20
  51. #define MMC_REG_INT_CNTR 0x24
  52. #define MMC_REG_CMD 0x28
  53. #define MMC_REG_ARG 0x2C
  54. #define MMC_REG_RES_FIFO 0x34
  55. #define MMC_REG_BUFFER_ACCESS 0x38
  56. #define STR_STP_CLK_RESET (1 << 3)
  57. #define STR_STP_CLK_START_CLK (1 << 1)
  58. #define STR_STP_CLK_STOP_CLK (1 << 0)
  59. #define STATUS_CARD_INSERTION (1 << 31)
  60. #define STATUS_CARD_REMOVAL (1 << 30)
  61. #define STATUS_YBUF_EMPTY (1 << 29)
  62. #define STATUS_XBUF_EMPTY (1 << 28)
  63. #define STATUS_YBUF_FULL (1 << 27)
  64. #define STATUS_XBUF_FULL (1 << 26)
  65. #define STATUS_BUF_UND_RUN (1 << 25)
  66. #define STATUS_BUF_OVFL (1 << 24)
  67. #define STATUS_SDIO_INT_ACTIVE (1 << 14)
  68. #define STATUS_END_CMD_RESP (1 << 13)
  69. #define STATUS_WRITE_OP_DONE (1 << 12)
  70. #define STATUS_DATA_TRANS_DONE (1 << 11)
  71. #define STATUS_READ_OP_DONE (1 << 11)
  72. #define STATUS_WR_CRC_ERROR_CODE_MASK (3 << 10)
  73. #define STATUS_CARD_BUS_CLK_RUN (1 << 8)
  74. #define STATUS_BUF_READ_RDY (1 << 7)
  75. #define STATUS_BUF_WRITE_RDY (1 << 6)
  76. #define STATUS_RESP_CRC_ERR (1 << 5)
  77. #define STATUS_CRC_READ_ERR (1 << 3)
  78. #define STATUS_CRC_WRITE_ERR (1 << 2)
  79. #define STATUS_TIME_OUT_RESP (1 << 1)
  80. #define STATUS_TIME_OUT_READ (1 << 0)
  81. #define STATUS_ERR_MASK 0x2f
  82. #define CMD_DAT_CONT_CMD_RESP_LONG_OFF (1 << 12)
  83. #define CMD_DAT_CONT_STOP_READWAIT (1 << 11)
  84. #define CMD_DAT_CONT_START_READWAIT (1 << 10)
  85. #define CMD_DAT_CONT_BUS_WIDTH_4 (2 << 8)
  86. #define CMD_DAT_CONT_INIT (1 << 7)
  87. #define CMD_DAT_CONT_WRITE (1 << 4)
  88. #define CMD_DAT_CONT_DATA_ENABLE (1 << 3)
  89. #define CMD_DAT_CONT_RESPONSE_48BIT_CRC (1 << 0)
  90. #define CMD_DAT_CONT_RESPONSE_136BIT (2 << 0)
  91. #define CMD_DAT_CONT_RESPONSE_48BIT (3 << 0)
  92. #define INT_SDIO_INT_WKP_EN (1 << 18)
  93. #define INT_CARD_INSERTION_WKP_EN (1 << 17)
  94. #define INT_CARD_REMOVAL_WKP_EN (1 << 16)
  95. #define INT_CARD_INSERTION_EN (1 << 15)
  96. #define INT_CARD_REMOVAL_EN (1 << 14)
  97. #define INT_SDIO_IRQ_EN (1 << 13)
  98. #define INT_DAT0_EN (1 << 12)
  99. #define INT_BUF_READ_EN (1 << 4)
  100. #define INT_BUF_WRITE_EN (1 << 3)
  101. #define INT_END_CMD_RES_EN (1 << 2)
  102. #define INT_WRITE_OP_DONE_EN (1 << 1)
  103. #define INT_READ_OP_EN (1 << 0)
  104. struct mxcmci_host {
  105. struct mmc_host *mmc;
  106. struct resource *res;
  107. void __iomem *base;
  108. int irq;
  109. int detect_irq;
  110. int dma;
  111. int do_dma;
  112. int use_sdio;
  113. unsigned int power_mode;
  114. struct imxmmc_platform_data *pdata;
  115. struct mmc_request *req;
  116. struct mmc_command *cmd;
  117. struct mmc_data *data;
  118. unsigned int dma_nents;
  119. unsigned int datasize;
  120. unsigned int dma_dir;
  121. u16 rev_no;
  122. unsigned int cmdat;
  123. struct clk *clk;
  124. int clock;
  125. struct work_struct datawork;
  126. spinlock_t lock;
  127. };
  128. static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios);
  129. static inline int mxcmci_use_dma(struct mxcmci_host *host)
  130. {
  131. return host->do_dma;
  132. }
  133. static void mxcmci_softreset(struct mxcmci_host *host)
  134. {
  135. int i;
  136. dev_dbg(mmc_dev(host->mmc), "mxcmci_softreset\n");
  137. /* reset sequence */
  138. writew(STR_STP_CLK_RESET, host->base + MMC_REG_STR_STP_CLK);
  139. writew(STR_STP_CLK_RESET | STR_STP_CLK_START_CLK,
  140. host->base + MMC_REG_STR_STP_CLK);
  141. for (i = 0; i < 8; i++)
  142. writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
  143. writew(0xff, host->base + MMC_REG_RES_TO);
  144. }
  145. static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
  146. {
  147. unsigned int nob = data->blocks;
  148. unsigned int blksz = data->blksz;
  149. unsigned int datasize = nob * blksz;
  150. #ifdef HAS_DMA
  151. struct scatterlist *sg;
  152. int i;
  153. int ret;
  154. #endif
  155. if (data->flags & MMC_DATA_STREAM)
  156. nob = 0xffff;
  157. host->data = data;
  158. data->bytes_xfered = 0;
  159. writew(nob, host->base + MMC_REG_NOB);
  160. writew(blksz, host->base + MMC_REG_BLK_LEN);
  161. host->datasize = datasize;
  162. #ifdef HAS_DMA
  163. for_each_sg(data->sg, sg, data->sg_len, i) {
  164. if (sg->offset & 3 || sg->length & 3) {
  165. host->do_dma = 0;
  166. return 0;
  167. }
  168. }
  169. if (data->flags & MMC_DATA_READ) {
  170. host->dma_dir = DMA_FROM_DEVICE;
  171. host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg,
  172. data->sg_len, host->dma_dir);
  173. ret = imx_dma_setup_sg(host->dma, data->sg, host->dma_nents,
  174. datasize,
  175. host->res->start + MMC_REG_BUFFER_ACCESS,
  176. DMA_MODE_READ);
  177. } else {
  178. host->dma_dir = DMA_TO_DEVICE;
  179. host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg,
  180. data->sg_len, host->dma_dir);
  181. ret = imx_dma_setup_sg(host->dma, data->sg, host->dma_nents,
  182. datasize,
  183. host->res->start + MMC_REG_BUFFER_ACCESS,
  184. DMA_MODE_WRITE);
  185. }
  186. if (ret) {
  187. dev_err(mmc_dev(host->mmc), "failed to setup DMA : %d\n", ret);
  188. return ret;
  189. }
  190. wmb();
  191. imx_dma_enable(host->dma);
  192. #endif /* HAS_DMA */
  193. return 0;
  194. }
  195. static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd,
  196. unsigned int cmdat)
  197. {
  198. u32 int_cntr;
  199. unsigned long flags;
  200. WARN_ON(host->cmd != NULL);
  201. host->cmd = cmd;
  202. switch (mmc_resp_type(cmd)) {
  203. case MMC_RSP_R1: /* short CRC, OPCODE */
  204. case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */
  205. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC;
  206. break;
  207. case MMC_RSP_R2: /* long 136 bit + CRC */
  208. cmdat |= CMD_DAT_CONT_RESPONSE_136BIT;
  209. break;
  210. case MMC_RSP_R3: /* short */
  211. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT;
  212. break;
  213. case MMC_RSP_NONE:
  214. break;
  215. default:
  216. dev_err(mmc_dev(host->mmc), "unhandled response type 0x%x\n",
  217. mmc_resp_type(cmd));
  218. cmd->error = -EINVAL;
  219. return -EINVAL;
  220. }
  221. int_cntr = INT_END_CMD_RES_EN;
  222. if (mxcmci_use_dma(host))
  223. int_cntr |= INT_READ_OP_EN | INT_WRITE_OP_DONE_EN;
  224. spin_lock_irqsave(&host->lock, flags);
  225. if (host->use_sdio)
  226. int_cntr |= INT_SDIO_IRQ_EN;
  227. writel(int_cntr, host->base + MMC_REG_INT_CNTR);
  228. spin_unlock_irqrestore(&host->lock, flags);
  229. writew(cmd->opcode, host->base + MMC_REG_CMD);
  230. writel(cmd->arg, host->base + MMC_REG_ARG);
  231. writew(cmdat, host->base + MMC_REG_CMD_DAT_CONT);
  232. return 0;
  233. }
  234. static void mxcmci_finish_request(struct mxcmci_host *host,
  235. struct mmc_request *req)
  236. {
  237. u32 int_cntr = 0;
  238. unsigned long flags;
  239. spin_lock_irqsave(&host->lock, flags);
  240. if (host->use_sdio)
  241. int_cntr |= INT_SDIO_IRQ_EN;
  242. writel(int_cntr, host->base + MMC_REG_INT_CNTR);
  243. spin_unlock_irqrestore(&host->lock, flags);
  244. host->req = NULL;
  245. host->cmd = NULL;
  246. host->data = NULL;
  247. mmc_request_done(host->mmc, req);
  248. }
  249. static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
  250. {
  251. struct mmc_data *data = host->data;
  252. int data_error;
  253. #ifdef HAS_DMA
  254. if (mxcmci_use_dma(host)) {
  255. imx_dma_disable(host->dma);
  256. dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_nents,
  257. host->dma_dir);
  258. }
  259. #endif
  260. if (stat & STATUS_ERR_MASK) {
  261. dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",
  262. stat);
  263. if (stat & STATUS_CRC_READ_ERR) {
  264. dev_err(mmc_dev(host->mmc), "%s: -EILSEQ\n", __func__);
  265. data->error = -EILSEQ;
  266. } else if (stat & STATUS_CRC_WRITE_ERR) {
  267. u32 err_code = (stat >> 9) & 0x3;
  268. if (err_code == 2) { /* No CRC response */
  269. dev_err(mmc_dev(host->mmc),
  270. "%s: No CRC -ETIMEDOUT\n", __func__);
  271. data->error = -ETIMEDOUT;
  272. } else {
  273. dev_err(mmc_dev(host->mmc),
  274. "%s: -EILSEQ\n", __func__);
  275. data->error = -EILSEQ;
  276. }
  277. } else if (stat & STATUS_TIME_OUT_READ) {
  278. dev_err(mmc_dev(host->mmc),
  279. "%s: read -ETIMEDOUT\n", __func__);
  280. data->error = -ETIMEDOUT;
  281. } else {
  282. dev_err(mmc_dev(host->mmc), "%s: -EIO\n", __func__);
  283. data->error = -EIO;
  284. }
  285. } else {
  286. data->bytes_xfered = host->datasize;
  287. }
  288. data_error = data->error;
  289. host->data = NULL;
  290. return data_error;
  291. }
  292. static void mxcmci_read_response(struct mxcmci_host *host, unsigned int stat)
  293. {
  294. struct mmc_command *cmd = host->cmd;
  295. int i;
  296. u32 a, b, c;
  297. if (!cmd)
  298. return;
  299. if (stat & STATUS_TIME_OUT_RESP) {
  300. dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n");
  301. cmd->error = -ETIMEDOUT;
  302. } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
  303. dev_dbg(mmc_dev(host->mmc), "cmd crc error\n");
  304. cmd->error = -EILSEQ;
  305. }
  306. if (cmd->flags & MMC_RSP_PRESENT) {
  307. if (cmd->flags & MMC_RSP_136) {
  308. for (i = 0; i < 4; i++) {
  309. a = readw(host->base + MMC_REG_RES_FIFO);
  310. b = readw(host->base + MMC_REG_RES_FIFO);
  311. cmd->resp[i] = a << 16 | b;
  312. }
  313. } else {
  314. a = readw(host->base + MMC_REG_RES_FIFO);
  315. b = readw(host->base + MMC_REG_RES_FIFO);
  316. c = readw(host->base + MMC_REG_RES_FIFO);
  317. cmd->resp[0] = a << 24 | b << 8 | c >> 8;
  318. }
  319. }
  320. }
  321. static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask)
  322. {
  323. u32 stat;
  324. unsigned long timeout = jiffies + HZ;
  325. do {
  326. stat = readl(host->base + MMC_REG_STATUS);
  327. if (stat & STATUS_ERR_MASK)
  328. return stat;
  329. if (time_after(jiffies, timeout)) {
  330. mxcmci_softreset(host);
  331. mxcmci_set_clk_rate(host, host->clock);
  332. return STATUS_TIME_OUT_READ;
  333. }
  334. if (stat & mask)
  335. return 0;
  336. cpu_relax();
  337. } while (1);
  338. }
  339. static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes)
  340. {
  341. unsigned int stat;
  342. u32 *buf = _buf;
  343. while (bytes > 3) {
  344. stat = mxcmci_poll_status(host,
  345. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  346. if (stat)
  347. return stat;
  348. *buf++ = readl(host->base + MMC_REG_BUFFER_ACCESS);
  349. bytes -= 4;
  350. }
  351. if (bytes) {
  352. u8 *b = (u8 *)buf;
  353. u32 tmp;
  354. stat = mxcmci_poll_status(host,
  355. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  356. if (stat)
  357. return stat;
  358. tmp = readl(host->base + MMC_REG_BUFFER_ACCESS);
  359. memcpy(b, &tmp, bytes);
  360. }
  361. return 0;
  362. }
  363. static int mxcmci_push(struct mxcmci_host *host, void *_buf, int bytes)
  364. {
  365. unsigned int stat;
  366. u32 *buf = _buf;
  367. while (bytes > 3) {
  368. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  369. if (stat)
  370. return stat;
  371. writel(*buf++, host->base + MMC_REG_BUFFER_ACCESS);
  372. bytes -= 4;
  373. }
  374. if (bytes) {
  375. u8 *b = (u8 *)buf;
  376. u32 tmp;
  377. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  378. if (stat)
  379. return stat;
  380. memcpy(&tmp, b, bytes);
  381. writel(tmp, host->base + MMC_REG_BUFFER_ACCESS);
  382. }
  383. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  384. if (stat)
  385. return stat;
  386. return 0;
  387. }
  388. static int mxcmci_transfer_data(struct mxcmci_host *host)
  389. {
  390. struct mmc_data *data = host->req->data;
  391. struct scatterlist *sg;
  392. int stat, i;
  393. host->data = data;
  394. host->datasize = 0;
  395. if (data->flags & MMC_DATA_READ) {
  396. for_each_sg(data->sg, sg, data->sg_len, i) {
  397. stat = mxcmci_pull(host, sg_virt(sg), sg->length);
  398. if (stat)
  399. return stat;
  400. host->datasize += sg->length;
  401. }
  402. } else {
  403. for_each_sg(data->sg, sg, data->sg_len, i) {
  404. stat = mxcmci_push(host, sg_virt(sg), sg->length);
  405. if (stat)
  406. return stat;
  407. host->datasize += sg->length;
  408. }
  409. stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE);
  410. if (stat)
  411. return stat;
  412. }
  413. return 0;
  414. }
  415. static void mxcmci_datawork(struct work_struct *work)
  416. {
  417. struct mxcmci_host *host = container_of(work, struct mxcmci_host,
  418. datawork);
  419. int datastat = mxcmci_transfer_data(host);
  420. mxcmci_finish_data(host, datastat);
  421. if (host->req->stop) {
  422. if (mxcmci_start_cmd(host, host->req->stop, 0)) {
  423. mxcmci_finish_request(host, host->req);
  424. return;
  425. }
  426. } else {
  427. mxcmci_finish_request(host, host->req);
  428. }
  429. }
  430. #ifdef HAS_DMA
  431. static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat)
  432. {
  433. struct mmc_data *data = host->data;
  434. int data_error;
  435. if (!data)
  436. return;
  437. data_error = mxcmci_finish_data(host, stat);
  438. mxcmci_read_response(host, stat);
  439. host->cmd = NULL;
  440. if (host->req->stop) {
  441. if (mxcmci_start_cmd(host, host->req->stop, 0)) {
  442. mxcmci_finish_request(host, host->req);
  443. return;
  444. }
  445. } else {
  446. mxcmci_finish_request(host, host->req);
  447. }
  448. }
  449. #endif /* HAS_DMA */
  450. static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat)
  451. {
  452. mxcmci_read_response(host, stat);
  453. host->cmd = NULL;
  454. if (!host->data && host->req) {
  455. mxcmci_finish_request(host, host->req);
  456. return;
  457. }
  458. /* For the DMA case the DMA engine handles the data transfer
  459. * automatically. For non DMA we have to do it ourselves.
  460. * Don't do it in interrupt context though.
  461. */
  462. if (!mxcmci_use_dma(host) && host->data)
  463. schedule_work(&host->datawork);
  464. }
  465. static irqreturn_t mxcmci_irq(int irq, void *devid)
  466. {
  467. struct mxcmci_host *host = devid;
  468. unsigned long flags;
  469. bool sdio_irq;
  470. u32 stat;
  471. stat = readl(host->base + MMC_REG_STATUS);
  472. writel(stat & ~STATUS_SDIO_INT_ACTIVE, host->base + MMC_REG_STATUS);
  473. dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat);
  474. spin_lock_irqsave(&host->lock, flags);
  475. sdio_irq = (stat & STATUS_SDIO_INT_ACTIVE) && host->use_sdio;
  476. spin_unlock_irqrestore(&host->lock, flags);
  477. if (sdio_irq) {
  478. writel(STATUS_SDIO_INT_ACTIVE, host->base + MMC_REG_STATUS);
  479. mmc_signal_sdio_irq(host->mmc);
  480. }
  481. if (stat & STATUS_END_CMD_RESP)
  482. mxcmci_cmd_done(host, stat);
  483. #ifdef HAS_DMA
  484. if (mxcmci_use_dma(host) &&
  485. (stat & (STATUS_DATA_TRANS_DONE | STATUS_WRITE_OP_DONE)))
  486. mxcmci_data_done(host, stat);
  487. #endif
  488. return IRQ_HANDLED;
  489. }
  490. static void mxcmci_request(struct mmc_host *mmc, struct mmc_request *req)
  491. {
  492. struct mxcmci_host *host = mmc_priv(mmc);
  493. unsigned int cmdat = host->cmdat;
  494. int error;
  495. WARN_ON(host->req != NULL);
  496. host->req = req;
  497. host->cmdat &= ~CMD_DAT_CONT_INIT;
  498. #ifdef HAS_DMA
  499. host->do_dma = 1;
  500. #endif
  501. if (req->data) {
  502. error = mxcmci_setup_data(host, req->data);
  503. if (error) {
  504. req->cmd->error = error;
  505. goto out;
  506. }
  507. cmdat |= CMD_DAT_CONT_DATA_ENABLE;
  508. if (req->data->flags & MMC_DATA_WRITE)
  509. cmdat |= CMD_DAT_CONT_WRITE;
  510. }
  511. error = mxcmci_start_cmd(host, req->cmd, cmdat);
  512. out:
  513. if (error)
  514. mxcmci_finish_request(host, req);
  515. }
  516. static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
  517. {
  518. unsigned int divider;
  519. int prescaler = 0;
  520. unsigned int clk_in = clk_get_rate(host->clk);
  521. while (prescaler <= 0x800) {
  522. for (divider = 1; divider <= 0xF; divider++) {
  523. int x;
  524. x = (clk_in / (divider + 1));
  525. if (prescaler)
  526. x /= (prescaler * 2);
  527. if (x <= clk_ios)
  528. break;
  529. }
  530. if (divider < 0x10)
  531. break;
  532. if (prescaler == 0)
  533. prescaler = 1;
  534. else
  535. prescaler <<= 1;
  536. }
  537. writew((prescaler << 4) | divider, host->base + MMC_REG_CLK_RATE);
  538. dev_dbg(mmc_dev(host->mmc), "scaler: %d divider: %d in: %d out: %d\n",
  539. prescaler, divider, clk_in, clk_ios);
  540. }
  541. static void mxcmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  542. {
  543. struct mxcmci_host *host = mmc_priv(mmc);
  544. #ifdef HAS_DMA
  545. unsigned int blen;
  546. /*
  547. * use burstlen of 64 in 4 bit mode (--> reg value 0)
  548. * use burstlen of 16 in 1 bit mode (--> reg value 16)
  549. */
  550. if (ios->bus_width == MMC_BUS_WIDTH_4)
  551. blen = 0;
  552. else
  553. blen = 16;
  554. imx_dma_config_burstlen(host->dma, blen);
  555. #endif
  556. if (ios->bus_width == MMC_BUS_WIDTH_4)
  557. host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4;
  558. else
  559. host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4;
  560. if (host->power_mode != ios->power_mode) {
  561. if (host->pdata && host->pdata->setpower)
  562. host->pdata->setpower(mmc_dev(mmc), ios->vdd);
  563. host->power_mode = ios->power_mode;
  564. if (ios->power_mode == MMC_POWER_ON)
  565. host->cmdat |= CMD_DAT_CONT_INIT;
  566. }
  567. if (ios->clock) {
  568. mxcmci_set_clk_rate(host, ios->clock);
  569. writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
  570. } else {
  571. writew(STR_STP_CLK_STOP_CLK, host->base + MMC_REG_STR_STP_CLK);
  572. }
  573. host->clock = ios->clock;
  574. }
  575. static irqreturn_t mxcmci_detect_irq(int irq, void *data)
  576. {
  577. struct mmc_host *mmc = data;
  578. dev_dbg(mmc_dev(mmc), "%s\n", __func__);
  579. mmc_detect_change(mmc, msecs_to_jiffies(250));
  580. return IRQ_HANDLED;
  581. }
  582. static int mxcmci_get_ro(struct mmc_host *mmc)
  583. {
  584. struct mxcmci_host *host = mmc_priv(mmc);
  585. if (host->pdata && host->pdata->get_ro)
  586. return !!host->pdata->get_ro(mmc_dev(mmc));
  587. /*
  588. * Board doesn't support read only detection; let the mmc core
  589. * decide what to do.
  590. */
  591. return -ENOSYS;
  592. }
  593. static void mxcmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  594. {
  595. struct mxcmci_host *host = mmc_priv(mmc);
  596. unsigned long flags;
  597. u32 int_cntr;
  598. spin_lock_irqsave(&host->lock, flags);
  599. host->use_sdio = enable;
  600. int_cntr = readl(host->base + MMC_REG_INT_CNTR);
  601. if (enable)
  602. int_cntr |= INT_SDIO_IRQ_EN;
  603. else
  604. int_cntr &= ~INT_SDIO_IRQ_EN;
  605. writel(int_cntr, host->base + MMC_REG_INT_CNTR);
  606. spin_unlock_irqrestore(&host->lock, flags);
  607. }
  608. static const struct mmc_host_ops mxcmci_ops = {
  609. .request = mxcmci_request,
  610. .set_ios = mxcmci_set_ios,
  611. .get_ro = mxcmci_get_ro,
  612. .enable_sdio_irq = mxcmci_enable_sdio_irq,
  613. };
  614. static int mxcmci_probe(struct platform_device *pdev)
  615. {
  616. struct mmc_host *mmc;
  617. struct mxcmci_host *host = NULL;
  618. struct resource *iores, *r;
  619. int ret = 0, irq;
  620. printk(KERN_INFO "i.MX SDHC driver\n");
  621. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  622. irq = platform_get_irq(pdev, 0);
  623. if (!iores || irq < 0)
  624. return -EINVAL;
  625. r = request_mem_region(iores->start, resource_size(iores), pdev->name);
  626. if (!r)
  627. return -EBUSY;
  628. mmc = mmc_alloc_host(sizeof(struct mxcmci_host), &pdev->dev);
  629. if (!mmc) {
  630. ret = -ENOMEM;
  631. goto out_release_mem;
  632. }
  633. mmc->ops = &mxcmci_ops;
  634. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  635. /* MMC core transfer sizes tunable parameters */
  636. mmc->max_hw_segs = 64;
  637. mmc->max_phys_segs = 64;
  638. mmc->max_blk_size = 2048;
  639. mmc->max_blk_count = 65535;
  640. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  641. mmc->max_seg_size = mmc->max_req_size;
  642. host = mmc_priv(mmc);
  643. host->base = ioremap(r->start, resource_size(r));
  644. if (!host->base) {
  645. ret = -ENOMEM;
  646. goto out_free;
  647. }
  648. host->mmc = mmc;
  649. host->pdata = pdev->dev.platform_data;
  650. spin_lock_init(&host->lock);
  651. if (host->pdata && host->pdata->ocr_avail)
  652. mmc->ocr_avail = host->pdata->ocr_avail;
  653. else
  654. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  655. host->res = r;
  656. host->irq = irq;
  657. host->clk = clk_get(&pdev->dev, NULL);
  658. if (IS_ERR(host->clk)) {
  659. ret = PTR_ERR(host->clk);
  660. goto out_iounmap;
  661. }
  662. clk_enable(host->clk);
  663. mxcmci_softreset(host);
  664. host->rev_no = readw(host->base + MMC_REG_REV_NO);
  665. if (host->rev_no != 0x400) {
  666. ret = -ENODEV;
  667. dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n",
  668. host->rev_no);
  669. goto out_clk_put;
  670. }
  671. mmc->f_min = clk_get_rate(host->clk) >> 16;
  672. mmc->f_max = clk_get_rate(host->clk) >> 1;
  673. /* recommended in data sheet */
  674. writew(0x2db4, host->base + MMC_REG_READ_TO);
  675. writel(0, host->base + MMC_REG_INT_CNTR);
  676. #ifdef HAS_DMA
  677. host->dma = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_LOW);
  678. if (host->dma < 0) {
  679. dev_err(mmc_dev(host->mmc), "imx_dma_request_by_prio failed\n");
  680. ret = -EBUSY;
  681. goto out_clk_put;
  682. }
  683. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  684. if (!r) {
  685. ret = -EINVAL;
  686. goto out_free_dma;
  687. }
  688. ret = imx_dma_config_channel(host->dma,
  689. IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_FIFO,
  690. IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
  691. r->start, 0);
  692. if (ret) {
  693. dev_err(mmc_dev(host->mmc), "failed to config DMA channel\n");
  694. goto out_free_dma;
  695. }
  696. #endif
  697. INIT_WORK(&host->datawork, mxcmci_datawork);
  698. ret = request_irq(host->irq, mxcmci_irq, 0, DRIVER_NAME, host);
  699. if (ret)
  700. goto out_free_dma;
  701. platform_set_drvdata(pdev, mmc);
  702. if (host->pdata && host->pdata->init) {
  703. ret = host->pdata->init(&pdev->dev, mxcmci_detect_irq,
  704. host->mmc);
  705. if (ret)
  706. goto out_free_irq;
  707. }
  708. mmc_add_host(mmc);
  709. return 0;
  710. out_free_irq:
  711. free_irq(host->irq, host);
  712. out_free_dma:
  713. #ifdef HAS_DMA
  714. imx_dma_free(host->dma);
  715. #endif
  716. out_clk_put:
  717. clk_disable(host->clk);
  718. clk_put(host->clk);
  719. out_iounmap:
  720. iounmap(host->base);
  721. out_free:
  722. mmc_free_host(mmc);
  723. out_release_mem:
  724. release_mem_region(iores->start, resource_size(iores));
  725. return ret;
  726. }
  727. static int mxcmci_remove(struct platform_device *pdev)
  728. {
  729. struct mmc_host *mmc = platform_get_drvdata(pdev);
  730. struct mxcmci_host *host = mmc_priv(mmc);
  731. platform_set_drvdata(pdev, NULL);
  732. mmc_remove_host(mmc);
  733. if (host->pdata && host->pdata->exit)
  734. host->pdata->exit(&pdev->dev, mmc);
  735. free_irq(host->irq, host);
  736. iounmap(host->base);
  737. #ifdef HAS_DMA
  738. imx_dma_free(host->dma);
  739. #endif
  740. clk_disable(host->clk);
  741. clk_put(host->clk);
  742. release_mem_region(host->res->start, resource_size(host->res));
  743. release_resource(host->res);
  744. mmc_free_host(mmc);
  745. return 0;
  746. }
  747. #ifdef CONFIG_PM
  748. static int mxcmci_suspend(struct platform_device *dev, pm_message_t state)
  749. {
  750. struct mmc_host *mmc = platform_get_drvdata(dev);
  751. int ret = 0;
  752. if (mmc)
  753. ret = mmc_suspend_host(mmc, state);
  754. return ret;
  755. }
  756. static int mxcmci_resume(struct platform_device *dev)
  757. {
  758. struct mmc_host *mmc = platform_get_drvdata(dev);
  759. struct mxcmci_host *host;
  760. int ret = 0;
  761. if (mmc) {
  762. host = mmc_priv(mmc);
  763. ret = mmc_resume_host(mmc);
  764. }
  765. return ret;
  766. }
  767. #else
  768. #define mxcmci_suspend NULL
  769. #define mxcmci_resume NULL
  770. #endif /* CONFIG_PM */
  771. static struct platform_driver mxcmci_driver = {
  772. .probe = mxcmci_probe,
  773. .remove = mxcmci_remove,
  774. .suspend = mxcmci_suspend,
  775. .resume = mxcmci_resume,
  776. .driver = {
  777. .name = DRIVER_NAME,
  778. .owner = THIS_MODULE,
  779. }
  780. };
  781. static int __init mxcmci_init(void)
  782. {
  783. return platform_driver_register(&mxcmci_driver);
  784. }
  785. static void __exit mxcmci_exit(void)
  786. {
  787. platform_driver_unregister(&mxcmci_driver);
  788. }
  789. module_init(mxcmci_init);
  790. module_exit(mxcmci_exit);
  791. MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver");
  792. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  793. MODULE_LICENSE("GPL");
  794. MODULE_ALIAS("platform:imx-mmc");