mxcmmc.c 24 KB

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