mxcmmc.c 24 KB

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