wmt-sdmmc.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * WM8505/WM8650 SD/MMC Host Controller
  3. *
  4. * Copyright (C) 2010 Tony Prisk
  5. * Copyright (C) 2008 WonderMedia Technologies, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/ioport.h>
  15. #include <linux/errno.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/delay.h>
  18. #include <linux/io.h>
  19. #include <linux/irq.h>
  20. #include <linux/clk.h>
  21. #include <linux/gpio.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_device.h>
  26. #include <linux/mmc/host.h>
  27. #include <linux/mmc/mmc.h>
  28. #include <linux/mmc/sd.h>
  29. #include <asm/byteorder.h>
  30. #define DRIVER_NAME "wmt-sdhc"
  31. /* MMC/SD controller registers */
  32. #define SDMMC_CTLR 0x00
  33. #define SDMMC_CMD 0x01
  34. #define SDMMC_RSPTYPE 0x02
  35. #define SDMMC_ARG 0x04
  36. #define SDMMC_BUSMODE 0x08
  37. #define SDMMC_BLKLEN 0x0C
  38. #define SDMMC_BLKCNT 0x0E
  39. #define SDMMC_RSP 0x10
  40. #define SDMMC_CBCR 0x20
  41. #define SDMMC_INTMASK0 0x24
  42. #define SDMMC_INTMASK1 0x25
  43. #define SDMMC_STS0 0x28
  44. #define SDMMC_STS1 0x29
  45. #define SDMMC_STS2 0x2A
  46. #define SDMMC_STS3 0x2B
  47. #define SDMMC_RSPTIMEOUT 0x2C
  48. #define SDMMC_CLK 0x30 /* VT8500 only */
  49. #define SDMMC_EXTCTRL 0x34
  50. #define SDMMC_SBLKLEN 0x38
  51. #define SDMMC_DMATIMEOUT 0x3C
  52. /* SDMMC_CTLR bit fields */
  53. #define CTLR_CMD_START 0x01
  54. #define CTLR_CMD_WRITE 0x04
  55. #define CTLR_FIFO_RESET 0x08
  56. /* SDMMC_BUSMODE bit fields */
  57. #define BM_SPI_MODE 0x01
  58. #define BM_FOURBIT_MODE 0x02
  59. #define BM_EIGHTBIT_MODE 0x04
  60. #define BM_SD_OFF 0x10
  61. #define BM_SPI_CS 0x20
  62. #define BM_SD_POWER 0x40
  63. #define BM_SOFT_RESET 0x80
  64. #define BM_ONEBIT_MASK 0xFD
  65. /* SDMMC_BLKLEN bit fields */
  66. #define BLKL_CRCERR_ABORT 0x0800
  67. #define BLKL_CD_POL_HIGH 0x1000
  68. #define BLKL_GPI_CD 0x2000
  69. #define BLKL_DATA3_CD 0x4000
  70. #define BLKL_INT_ENABLE 0x8000
  71. /* SDMMC_INTMASK0 bit fields */
  72. #define INT0_MBLK_TRAN_DONE_INT_EN 0x10
  73. #define INT0_BLK_TRAN_DONE_INT_EN 0x20
  74. #define INT0_CD_INT_EN 0x40
  75. #define INT0_DI_INT_EN 0x80
  76. /* SDMMC_INTMASK1 bit fields */
  77. #define INT1_CMD_RES_TRAN_DONE_INT_EN 0x02
  78. #define INT1_CMD_RES_TOUT_INT_EN 0x04
  79. #define INT1_MBLK_AUTO_STOP_INT_EN 0x08
  80. #define INT1_DATA_TOUT_INT_EN 0x10
  81. #define INT1_RESCRC_ERR_INT_EN 0x20
  82. #define INT1_RCRC_ERR_INT_EN 0x40
  83. #define INT1_WCRC_ERR_INT_EN 0x80
  84. /* SDMMC_STS0 bit fields */
  85. #define STS0_WRITE_PROTECT 0x02
  86. #define STS0_CD_DATA3 0x04
  87. #define STS0_CD_GPI 0x08
  88. #define STS0_MBLK_DONE 0x10
  89. #define STS0_BLK_DONE 0x20
  90. #define STS0_CARD_DETECT 0x40
  91. #define STS0_DEVICE_INS 0x80
  92. /* SDMMC_STS1 bit fields */
  93. #define STS1_SDIO_INT 0x01
  94. #define STS1_CMDRSP_DONE 0x02
  95. #define STS1_RSP_TIMEOUT 0x04
  96. #define STS1_AUTOSTOP_DONE 0x08
  97. #define STS1_DATA_TIMEOUT 0x10
  98. #define STS1_RSP_CRC_ERR 0x20
  99. #define STS1_RCRC_ERR 0x40
  100. #define STS1_WCRC_ERR 0x80
  101. /* SDMMC_STS2 bit fields */
  102. #define STS2_CMD_RES_BUSY 0x10
  103. #define STS2_DATARSP_BUSY 0x20
  104. #define STS2_DIS_FORCECLK 0x80
  105. /* MMC/SD DMA Controller Registers */
  106. #define SDDMA_GCR 0x100
  107. #define SDDMA_IER 0x104
  108. #define SDDMA_ISR 0x108
  109. #define SDDMA_DESPR 0x10C
  110. #define SDDMA_RBR 0x110
  111. #define SDDMA_DAR 0x114
  112. #define SDDMA_BAR 0x118
  113. #define SDDMA_CPR 0x11C
  114. #define SDDMA_CCR 0x120
  115. /* SDDMA_GCR bit fields */
  116. #define DMA_GCR_DMA_EN 0x00000001
  117. #define DMA_GCR_SOFT_RESET 0x00000100
  118. /* SDDMA_IER bit fields */
  119. #define DMA_IER_INT_EN 0x00000001
  120. /* SDDMA_ISR bit fields */
  121. #define DMA_ISR_INT_STS 0x00000001
  122. /* SDDMA_RBR bit fields */
  123. #define DMA_RBR_FORMAT 0x40000000
  124. #define DMA_RBR_END 0x80000000
  125. /* SDDMA_CCR bit fields */
  126. #define DMA_CCR_RUN 0x00000080
  127. #define DMA_CCR_IF_TO_PERIPHERAL 0x00000000
  128. #define DMA_CCR_PERIPHERAL_TO_IF 0x00400000
  129. /* SDDMA_CCR event status */
  130. #define DMA_CCR_EVT_NO_STATUS 0x00000000
  131. #define DMA_CCR_EVT_UNDERRUN 0x00000001
  132. #define DMA_CCR_EVT_OVERRUN 0x00000002
  133. #define DMA_CCR_EVT_DESP_READ 0x00000003
  134. #define DMA_CCR_EVT_DATA_RW 0x00000004
  135. #define DMA_CCR_EVT_EARLY_END 0x00000005
  136. #define DMA_CCR_EVT_SUCCESS 0x0000000F
  137. #define PDMA_READ 0x00
  138. #define PDMA_WRITE 0x01
  139. #define WMT_SD_POWER_OFF 0
  140. #define WMT_SD_POWER_ON 1
  141. struct wmt_dma_descriptor {
  142. u32 flags;
  143. u32 data_buffer_addr;
  144. u32 branch_addr;
  145. u32 reserved1;
  146. };
  147. struct wmt_mci_caps {
  148. unsigned int f_min;
  149. unsigned int f_max;
  150. u32 ocr_avail;
  151. u32 caps;
  152. u32 max_seg_size;
  153. u32 max_segs;
  154. u32 max_blk_size;
  155. };
  156. struct wmt_mci_priv {
  157. struct mmc_host *mmc;
  158. void __iomem *sdmmc_base;
  159. int irq_regular;
  160. int irq_dma;
  161. void *dma_desc_buffer;
  162. dma_addr_t dma_desc_device_addr;
  163. struct completion cmdcomp;
  164. struct completion datacomp;
  165. struct completion *comp_cmd;
  166. struct completion *comp_dma;
  167. struct mmc_request *req;
  168. struct mmc_command *cmd;
  169. struct clk *clk_sdmmc;
  170. struct device *dev;
  171. u8 power_inverted;
  172. u8 cd_inverted;
  173. };
  174. static void wmt_set_sd_power(struct wmt_mci_priv *priv, int enable)
  175. {
  176. u32 reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  177. if (enable ^ priv->power_inverted)
  178. reg_tmp &= ~BM_SD_OFF;
  179. else
  180. reg_tmp |= BM_SD_OFF;
  181. writeb(reg_tmp, priv->sdmmc_base + SDMMC_BUSMODE);
  182. }
  183. static void wmt_mci_read_response(struct mmc_host *mmc)
  184. {
  185. struct wmt_mci_priv *priv;
  186. int idx1, idx2;
  187. u8 tmp_resp;
  188. u32 response;
  189. priv = mmc_priv(mmc);
  190. for (idx1 = 0; idx1 < 4; idx1++) {
  191. response = 0;
  192. for (idx2 = 0; idx2 < 4; idx2++) {
  193. if ((idx1 == 3) && (idx2 == 3))
  194. tmp_resp = readb(priv->sdmmc_base + SDMMC_RSP);
  195. else
  196. tmp_resp = readb(priv->sdmmc_base + SDMMC_RSP +
  197. (idx1*4) + idx2 + 1);
  198. response |= (tmp_resp << (idx2 * 8));
  199. }
  200. priv->cmd->resp[idx1] = cpu_to_be32(response);
  201. }
  202. }
  203. static void wmt_mci_start_command(struct wmt_mci_priv *priv)
  204. {
  205. u32 reg_tmp;
  206. reg_tmp = readb(priv->sdmmc_base + SDMMC_CTLR);
  207. writeb(reg_tmp | CTLR_CMD_START, priv->sdmmc_base + SDMMC_CTLR);
  208. }
  209. static int wmt_mci_send_command(struct mmc_host *mmc, u8 command, u8 cmdtype,
  210. u32 arg, u8 rsptype)
  211. {
  212. struct wmt_mci_priv *priv;
  213. u32 reg_tmp;
  214. priv = mmc_priv(mmc);
  215. /* write command, arg, resptype registers */
  216. writeb(command, priv->sdmmc_base + SDMMC_CMD);
  217. writel(arg, priv->sdmmc_base + SDMMC_ARG);
  218. writeb(rsptype, priv->sdmmc_base + SDMMC_RSPTYPE);
  219. /* reset response FIFO */
  220. reg_tmp = readb(priv->sdmmc_base + SDMMC_CTLR);
  221. writeb(reg_tmp | CTLR_FIFO_RESET, priv->sdmmc_base + SDMMC_CTLR);
  222. /* ensure clock enabled - VT3465 */
  223. wmt_set_sd_power(priv, WMT_SD_POWER_ON);
  224. /* clear status bits */
  225. writeb(0xFF, priv->sdmmc_base + SDMMC_STS0);
  226. writeb(0xFF, priv->sdmmc_base + SDMMC_STS1);
  227. writeb(0xFF, priv->sdmmc_base + SDMMC_STS2);
  228. writeb(0xFF, priv->sdmmc_base + SDMMC_STS3);
  229. /* set command type */
  230. reg_tmp = readb(priv->sdmmc_base + SDMMC_CTLR);
  231. writeb((reg_tmp & 0x0F) | (cmdtype << 4),
  232. priv->sdmmc_base + SDMMC_CTLR);
  233. return 0;
  234. }
  235. static void wmt_mci_disable_dma(struct wmt_mci_priv *priv)
  236. {
  237. writel(DMA_ISR_INT_STS, priv->sdmmc_base + SDDMA_ISR);
  238. writel(0, priv->sdmmc_base + SDDMA_IER);
  239. }
  240. static void wmt_complete_data_request(struct wmt_mci_priv *priv)
  241. {
  242. struct mmc_request *req;
  243. req = priv->req;
  244. req->data->bytes_xfered = req->data->blksz * req->data->blocks;
  245. /* unmap the DMA pages used for write data */
  246. if (req->data->flags & MMC_DATA_WRITE)
  247. dma_unmap_sg(mmc_dev(priv->mmc), req->data->sg,
  248. req->data->sg_len, DMA_TO_DEVICE);
  249. else
  250. dma_unmap_sg(mmc_dev(priv->mmc), req->data->sg,
  251. req->data->sg_len, DMA_FROM_DEVICE);
  252. /* Check if the DMA ISR returned a data error */
  253. if ((req->cmd->error) || (req->data->error))
  254. mmc_request_done(priv->mmc, req);
  255. else {
  256. wmt_mci_read_response(priv->mmc);
  257. if (!req->data->stop) {
  258. /* single-block read/write requests end here */
  259. mmc_request_done(priv->mmc, req);
  260. } else {
  261. /*
  262. * we change the priv->cmd variable so the response is
  263. * stored in the stop struct rather than the original
  264. * calling command struct
  265. */
  266. priv->comp_cmd = &priv->cmdcomp;
  267. init_completion(priv->comp_cmd);
  268. priv->cmd = req->data->stop;
  269. wmt_mci_send_command(priv->mmc, req->data->stop->opcode,
  270. 7, req->data->stop->arg, 9);
  271. wmt_mci_start_command(priv);
  272. }
  273. }
  274. }
  275. static irqreturn_t wmt_mci_dma_isr(int irq_num, void *data)
  276. {
  277. struct wmt_mci_priv *priv;
  278. int status;
  279. priv = (struct wmt_mci_priv *)data;
  280. status = readl(priv->sdmmc_base + SDDMA_CCR) & 0x0F;
  281. if (status != DMA_CCR_EVT_SUCCESS) {
  282. dev_err(priv->dev, "DMA Error: Status = %d\n", status);
  283. priv->req->data->error = -ETIMEDOUT;
  284. complete(priv->comp_dma);
  285. return IRQ_HANDLED;
  286. }
  287. priv->req->data->error = 0;
  288. wmt_mci_disable_dma(priv);
  289. complete(priv->comp_dma);
  290. if (priv->comp_cmd) {
  291. if (completion_done(priv->comp_cmd)) {
  292. /*
  293. * if the command (regular) interrupt has already
  294. * completed, finish off the request otherwise we wait
  295. * for the command interrupt and finish from there.
  296. */
  297. wmt_complete_data_request(priv);
  298. }
  299. }
  300. return IRQ_HANDLED;
  301. }
  302. static irqreturn_t wmt_mci_regular_isr(int irq_num, void *data)
  303. {
  304. struct wmt_mci_priv *priv;
  305. u32 status0;
  306. u32 status1;
  307. u32 status2;
  308. u32 reg_tmp;
  309. int cmd_done;
  310. priv = (struct wmt_mci_priv *)data;
  311. cmd_done = 0;
  312. status0 = readb(priv->sdmmc_base + SDMMC_STS0);
  313. status1 = readb(priv->sdmmc_base + SDMMC_STS1);
  314. status2 = readb(priv->sdmmc_base + SDMMC_STS2);
  315. /* Check for card insertion */
  316. reg_tmp = readb(priv->sdmmc_base + SDMMC_INTMASK0);
  317. if ((reg_tmp & INT0_DI_INT_EN) && (status0 & STS0_DEVICE_INS)) {
  318. mmc_detect_change(priv->mmc, 0);
  319. if (priv->cmd)
  320. priv->cmd->error = -ETIMEDOUT;
  321. if (priv->comp_cmd)
  322. complete(priv->comp_cmd);
  323. if (priv->comp_dma) {
  324. wmt_mci_disable_dma(priv);
  325. complete(priv->comp_dma);
  326. }
  327. writeb(STS0_DEVICE_INS, priv->sdmmc_base + SDMMC_STS0);
  328. return IRQ_HANDLED;
  329. }
  330. if ((!priv->req->data) ||
  331. ((priv->req->data->stop) && (priv->cmd == priv->req->data->stop))) {
  332. /* handle non-data & stop_transmission requests */
  333. if (status1 & STS1_CMDRSP_DONE) {
  334. priv->cmd->error = 0;
  335. cmd_done = 1;
  336. } else if ((status1 & STS1_RSP_TIMEOUT) ||
  337. (status1 & STS1_DATA_TIMEOUT)) {
  338. priv->cmd->error = -ETIMEDOUT;
  339. cmd_done = 1;
  340. }
  341. if (cmd_done) {
  342. priv->comp_cmd = NULL;
  343. if (!priv->cmd->error)
  344. wmt_mci_read_response(priv->mmc);
  345. priv->cmd = NULL;
  346. mmc_request_done(priv->mmc, priv->req);
  347. }
  348. } else {
  349. /* handle data requests */
  350. if (status1 & STS1_CMDRSP_DONE) {
  351. if (priv->cmd)
  352. priv->cmd->error = 0;
  353. if (priv->comp_cmd)
  354. complete(priv->comp_cmd);
  355. }
  356. if ((status1 & STS1_RSP_TIMEOUT) ||
  357. (status1 & STS1_DATA_TIMEOUT)) {
  358. if (priv->cmd)
  359. priv->cmd->error = -ETIMEDOUT;
  360. if (priv->comp_cmd)
  361. complete(priv->comp_cmd);
  362. if (priv->comp_dma) {
  363. wmt_mci_disable_dma(priv);
  364. complete(priv->comp_dma);
  365. }
  366. }
  367. if (priv->comp_dma) {
  368. /*
  369. * If the dma interrupt has already completed, finish
  370. * off the request; otherwise we wait for the DMA
  371. * interrupt and finish from there.
  372. */
  373. if (completion_done(priv->comp_dma))
  374. wmt_complete_data_request(priv);
  375. }
  376. }
  377. writeb(status0, priv->sdmmc_base + SDMMC_STS0);
  378. writeb(status1, priv->sdmmc_base + SDMMC_STS1);
  379. writeb(status2, priv->sdmmc_base + SDMMC_STS2);
  380. return IRQ_HANDLED;
  381. }
  382. static void wmt_reset_hardware(struct mmc_host *mmc)
  383. {
  384. struct wmt_mci_priv *priv;
  385. u32 reg_tmp;
  386. priv = mmc_priv(mmc);
  387. /* reset controller */
  388. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  389. writeb(reg_tmp | BM_SOFT_RESET, priv->sdmmc_base + SDMMC_BUSMODE);
  390. /* reset response FIFO */
  391. reg_tmp = readb(priv->sdmmc_base + SDMMC_CTLR);
  392. writeb(reg_tmp | CTLR_FIFO_RESET, priv->sdmmc_base + SDMMC_CTLR);
  393. /* enable GPI pin to detect card */
  394. writew(BLKL_INT_ENABLE | BLKL_GPI_CD, priv->sdmmc_base + SDMMC_BLKLEN);
  395. /* clear interrupt status */
  396. writeb(0xFF, priv->sdmmc_base + SDMMC_STS0);
  397. writeb(0xFF, priv->sdmmc_base + SDMMC_STS1);
  398. /* setup interrupts */
  399. writeb(INT0_CD_INT_EN | INT0_DI_INT_EN, priv->sdmmc_base +
  400. SDMMC_INTMASK0);
  401. writeb(INT1_DATA_TOUT_INT_EN | INT1_CMD_RES_TRAN_DONE_INT_EN |
  402. INT1_CMD_RES_TOUT_INT_EN, priv->sdmmc_base + SDMMC_INTMASK1);
  403. /* set the DMA timeout */
  404. writew(8191, priv->sdmmc_base + SDMMC_DMATIMEOUT);
  405. /* auto clock freezing enable */
  406. reg_tmp = readb(priv->sdmmc_base + SDMMC_STS2);
  407. writeb(reg_tmp | STS2_DIS_FORCECLK, priv->sdmmc_base + SDMMC_STS2);
  408. /* set a default clock speed of 400Khz */
  409. clk_set_rate(priv->clk_sdmmc, 400000);
  410. }
  411. static int wmt_dma_init(struct mmc_host *mmc)
  412. {
  413. struct wmt_mci_priv *priv;
  414. priv = mmc_priv(mmc);
  415. writel(DMA_GCR_SOFT_RESET, priv->sdmmc_base + SDDMA_GCR);
  416. writel(DMA_GCR_DMA_EN, priv->sdmmc_base + SDDMA_GCR);
  417. if ((readl(priv->sdmmc_base + SDDMA_GCR) & DMA_GCR_DMA_EN) != 0)
  418. return 0;
  419. else
  420. return 1;
  421. }
  422. static void wmt_dma_init_descriptor(struct wmt_dma_descriptor *desc,
  423. u16 req_count, u32 buffer_addr, u32 branch_addr, int end)
  424. {
  425. desc->flags = 0x40000000 | req_count;
  426. if (end)
  427. desc->flags |= 0x80000000;
  428. desc->data_buffer_addr = buffer_addr;
  429. desc->branch_addr = branch_addr;
  430. }
  431. static void wmt_dma_config(struct mmc_host *mmc, u32 descaddr, u8 dir)
  432. {
  433. struct wmt_mci_priv *priv;
  434. u32 reg_tmp;
  435. priv = mmc_priv(mmc);
  436. /* Enable DMA Interrupts */
  437. writel(DMA_IER_INT_EN, priv->sdmmc_base + SDDMA_IER);
  438. /* Write DMA Descriptor Pointer Register */
  439. writel(descaddr, priv->sdmmc_base + SDDMA_DESPR);
  440. writel(0x00, priv->sdmmc_base + SDDMA_CCR);
  441. if (dir == PDMA_WRITE) {
  442. reg_tmp = readl(priv->sdmmc_base + SDDMA_CCR);
  443. writel(reg_tmp & DMA_CCR_IF_TO_PERIPHERAL, priv->sdmmc_base +
  444. SDDMA_CCR);
  445. } else {
  446. reg_tmp = readl(priv->sdmmc_base + SDDMA_CCR);
  447. writel(reg_tmp | DMA_CCR_PERIPHERAL_TO_IF, priv->sdmmc_base +
  448. SDDMA_CCR);
  449. }
  450. }
  451. static void wmt_dma_start(struct wmt_mci_priv *priv)
  452. {
  453. u32 reg_tmp;
  454. reg_tmp = readl(priv->sdmmc_base + SDDMA_CCR);
  455. writel(reg_tmp | DMA_CCR_RUN, priv->sdmmc_base + SDDMA_CCR);
  456. }
  457. static void wmt_mci_request(struct mmc_host *mmc, struct mmc_request *req)
  458. {
  459. struct wmt_mci_priv *priv;
  460. struct wmt_dma_descriptor *desc;
  461. u8 command;
  462. u8 cmdtype;
  463. u32 arg;
  464. u8 rsptype;
  465. u32 reg_tmp;
  466. struct scatterlist *sg;
  467. int i;
  468. int sg_cnt;
  469. int offset;
  470. u32 dma_address;
  471. int desc_cnt;
  472. priv = mmc_priv(mmc);
  473. priv->req = req;
  474. /*
  475. * Use the cmd variable to pass a pointer to the resp[] structure
  476. * This is required on multi-block requests to pass the pointer to the
  477. * stop command
  478. */
  479. priv->cmd = req->cmd;
  480. command = req->cmd->opcode;
  481. arg = req->cmd->arg;
  482. rsptype = mmc_resp_type(req->cmd);
  483. cmdtype = 0;
  484. /* rsptype=7 only valid for SPI commands - should be =2 for SD */
  485. if (rsptype == 7)
  486. rsptype = 2;
  487. /* rsptype=21 is R1B, convert for controller */
  488. if (rsptype == 21)
  489. rsptype = 9;
  490. if (!req->data) {
  491. wmt_mci_send_command(mmc, command, cmdtype, arg, rsptype);
  492. wmt_mci_start_command(priv);
  493. /* completion is now handled in the regular_isr() */
  494. }
  495. if (req->data) {
  496. priv->comp_cmd = &priv->cmdcomp;
  497. init_completion(priv->comp_cmd);
  498. wmt_dma_init(mmc);
  499. /* set controller data length */
  500. reg_tmp = readw(priv->sdmmc_base + SDMMC_BLKLEN);
  501. writew((reg_tmp & 0xF800) | (req->data->blksz - 1),
  502. priv->sdmmc_base + SDMMC_BLKLEN);
  503. /* set controller block count */
  504. writew(req->data->blocks, priv->sdmmc_base + SDMMC_BLKCNT);
  505. desc = (struct wmt_dma_descriptor *)priv->dma_desc_buffer;
  506. if (req->data->flags & MMC_DATA_WRITE) {
  507. sg_cnt = dma_map_sg(mmc_dev(mmc), req->data->sg,
  508. req->data->sg_len, DMA_TO_DEVICE);
  509. cmdtype = 1;
  510. if (req->data->blocks > 1)
  511. cmdtype = 3;
  512. } else {
  513. sg_cnt = dma_map_sg(mmc_dev(mmc), req->data->sg,
  514. req->data->sg_len, DMA_FROM_DEVICE);
  515. cmdtype = 2;
  516. if (req->data->blocks > 1)
  517. cmdtype = 4;
  518. }
  519. dma_address = priv->dma_desc_device_addr + 16;
  520. desc_cnt = 0;
  521. for_each_sg(req->data->sg, sg, sg_cnt, i) {
  522. offset = 0;
  523. while (offset < sg_dma_len(sg)) {
  524. wmt_dma_init_descriptor(desc, req->data->blksz,
  525. sg_dma_address(sg)+offset,
  526. dma_address, 0);
  527. desc++;
  528. desc_cnt++;
  529. offset += req->data->blksz;
  530. dma_address += 16;
  531. if (desc_cnt == req->data->blocks)
  532. break;
  533. }
  534. }
  535. desc--;
  536. desc->flags |= 0x80000000;
  537. if (req->data->flags & MMC_DATA_WRITE)
  538. wmt_dma_config(mmc, priv->dma_desc_device_addr,
  539. PDMA_WRITE);
  540. else
  541. wmt_dma_config(mmc, priv->dma_desc_device_addr,
  542. PDMA_READ);
  543. wmt_mci_send_command(mmc, command, cmdtype, arg, rsptype);
  544. priv->comp_dma = &priv->datacomp;
  545. init_completion(priv->comp_dma);
  546. wmt_dma_start(priv);
  547. wmt_mci_start_command(priv);
  548. }
  549. }
  550. static void wmt_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  551. {
  552. struct wmt_mci_priv *priv;
  553. u32 reg_tmp;
  554. priv = mmc_priv(mmc);
  555. if (ios->power_mode == MMC_POWER_UP) {
  556. wmt_reset_hardware(mmc);
  557. wmt_set_sd_power(priv, WMT_SD_POWER_ON);
  558. }
  559. if (ios->power_mode == MMC_POWER_OFF)
  560. wmt_set_sd_power(priv, WMT_SD_POWER_OFF);
  561. if (ios->clock != 0)
  562. clk_set_rate(priv->clk_sdmmc, ios->clock);
  563. switch (ios->bus_width) {
  564. case MMC_BUS_WIDTH_8:
  565. reg_tmp = readb(priv->sdmmc_base + SDMMC_EXTCTRL);
  566. writeb(reg_tmp | 0x04, priv->sdmmc_base + SDMMC_EXTCTRL);
  567. break;
  568. case MMC_BUS_WIDTH_4:
  569. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  570. writeb(reg_tmp | BM_FOURBIT_MODE, priv->sdmmc_base +
  571. SDMMC_BUSMODE);
  572. reg_tmp = readb(priv->sdmmc_base + SDMMC_EXTCTRL);
  573. writeb(reg_tmp & 0xFB, priv->sdmmc_base + SDMMC_EXTCTRL);
  574. break;
  575. case MMC_BUS_WIDTH_1:
  576. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  577. writeb(reg_tmp & BM_ONEBIT_MASK, priv->sdmmc_base +
  578. SDMMC_BUSMODE);
  579. reg_tmp = readb(priv->sdmmc_base + SDMMC_EXTCTRL);
  580. writeb(reg_tmp & 0xFB, priv->sdmmc_base + SDMMC_EXTCTRL);
  581. break;
  582. }
  583. }
  584. static int wmt_mci_get_ro(struct mmc_host *mmc)
  585. {
  586. struct wmt_mci_priv *priv = mmc_priv(mmc);
  587. return !(readb(priv->sdmmc_base + SDMMC_STS0) & STS0_WRITE_PROTECT);
  588. }
  589. static int wmt_mci_get_cd(struct mmc_host *mmc)
  590. {
  591. struct wmt_mci_priv *priv = mmc_priv(mmc);
  592. u32 cd = (readb(priv->sdmmc_base + SDMMC_STS0) & STS0_CD_GPI) >> 3;
  593. return !(cd ^ priv->cd_inverted);
  594. }
  595. static struct mmc_host_ops wmt_mci_ops = {
  596. .request = wmt_mci_request,
  597. .set_ios = wmt_mci_set_ios,
  598. .get_ro = wmt_mci_get_ro,
  599. .get_cd = wmt_mci_get_cd,
  600. };
  601. /* Controller capabilities */
  602. static struct wmt_mci_caps wm8505_caps = {
  603. .f_min = 390425,
  604. .f_max = 50000000,
  605. .ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34,
  606. .caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MMC_HIGHSPEED |
  607. MMC_CAP_SD_HIGHSPEED,
  608. .max_seg_size = 65024,
  609. .max_segs = 128,
  610. .max_blk_size = 2048,
  611. };
  612. static struct of_device_id wmt_mci_dt_ids[] = {
  613. { .compatible = "wm,wm8505-sdhc", .data = &wm8505_caps },
  614. { /* Sentinel */ },
  615. };
  616. static int wmt_mci_probe(struct platform_device *pdev)
  617. {
  618. struct mmc_host *mmc;
  619. struct wmt_mci_priv *priv;
  620. struct device_node *np = pdev->dev.of_node;
  621. const struct of_device_id *of_id =
  622. of_match_device(wmt_mci_dt_ids, &pdev->dev);
  623. const struct wmt_mci_caps *wmt_caps;
  624. int ret;
  625. int regular_irq, dma_irq;
  626. if (!of_id || !of_id->data) {
  627. dev_err(&pdev->dev, "Controller capabilities data missing\n");
  628. return -EFAULT;
  629. }
  630. wmt_caps = of_id->data;
  631. if (!np) {
  632. dev_err(&pdev->dev, "Missing SDMMC description in devicetree\n");
  633. return -EFAULT;
  634. }
  635. regular_irq = irq_of_parse_and_map(np, 0);
  636. dma_irq = irq_of_parse_and_map(np, 1);
  637. if (!regular_irq || !dma_irq) {
  638. dev_err(&pdev->dev, "Getting IRQs failed!\n");
  639. ret = -ENXIO;
  640. goto fail1;
  641. }
  642. mmc = mmc_alloc_host(sizeof(struct wmt_mci_priv), &pdev->dev);
  643. if (!mmc) {
  644. dev_err(&pdev->dev, "Failed to allocate mmc_host\n");
  645. ret = -ENOMEM;
  646. goto fail1;
  647. }
  648. mmc->ops = &wmt_mci_ops;
  649. mmc->f_min = wmt_caps->f_min;
  650. mmc->f_max = wmt_caps->f_max;
  651. mmc->ocr_avail = wmt_caps->ocr_avail;
  652. mmc->caps = wmt_caps->caps;
  653. mmc->max_seg_size = wmt_caps->max_seg_size;
  654. mmc->max_segs = wmt_caps->max_segs;
  655. mmc->max_blk_size = wmt_caps->max_blk_size;
  656. mmc->max_req_size = (16*512*mmc->max_segs);
  657. mmc->max_blk_count = mmc->max_req_size / 512;
  658. priv = mmc_priv(mmc);
  659. priv->mmc = mmc;
  660. priv->dev = &pdev->dev;
  661. priv->power_inverted = 0;
  662. priv->cd_inverted = 0;
  663. if (of_get_property(np, "sdon-inverted", NULL))
  664. priv->power_inverted = 1;
  665. if (of_get_property(np, "cd-inverted", NULL))
  666. priv->cd_inverted = 1;
  667. priv->sdmmc_base = of_iomap(np, 0);
  668. if (!priv->sdmmc_base) {
  669. dev_err(&pdev->dev, "Failed to map IO space\n");
  670. ret = -ENOMEM;
  671. goto fail2;
  672. }
  673. priv->irq_regular = regular_irq;
  674. priv->irq_dma = dma_irq;
  675. ret = request_irq(regular_irq, wmt_mci_regular_isr, 0, "sdmmc", priv);
  676. if (ret) {
  677. dev_err(&pdev->dev, "Register regular IRQ fail\n");
  678. goto fail3;
  679. }
  680. ret = request_irq(dma_irq, wmt_mci_dma_isr, 32, "sdmmc", priv);
  681. if (ret) {
  682. dev_err(&pdev->dev, "Register DMA IRQ fail\n");
  683. goto fail4;
  684. }
  685. /* alloc some DMA buffers for descriptors/transfers */
  686. priv->dma_desc_buffer = dma_alloc_coherent(&pdev->dev,
  687. mmc->max_blk_count * 16,
  688. &priv->dma_desc_device_addr,
  689. GFP_KERNEL);
  690. if (!priv->dma_desc_buffer) {
  691. dev_err(&pdev->dev, "DMA alloc fail\n");
  692. ret = -EPERM;
  693. goto fail5;
  694. }
  695. platform_set_drvdata(pdev, mmc);
  696. priv->clk_sdmmc = of_clk_get(np, 0);
  697. if (IS_ERR(priv->clk_sdmmc)) {
  698. dev_err(&pdev->dev, "Error getting clock\n");
  699. ret = PTR_ERR(priv->clk_sdmmc);
  700. goto fail5;
  701. }
  702. clk_prepare_enable(priv->clk_sdmmc);
  703. /* configure the controller to a known 'ready' state */
  704. wmt_reset_hardware(mmc);
  705. mmc_add_host(mmc);
  706. dev_info(&pdev->dev, "WMT SDHC Controller initialized\n");
  707. return 0;
  708. fail5:
  709. free_irq(dma_irq, priv);
  710. fail4:
  711. free_irq(regular_irq, priv);
  712. fail3:
  713. iounmap(priv->sdmmc_base);
  714. fail2:
  715. mmc_free_host(mmc);
  716. fail1:
  717. return ret;
  718. }
  719. static int wmt_mci_remove(struct platform_device *pdev)
  720. {
  721. struct mmc_host *mmc;
  722. struct wmt_mci_priv *priv;
  723. struct resource *res;
  724. u32 reg_tmp;
  725. mmc = platform_get_drvdata(pdev);
  726. priv = mmc_priv(mmc);
  727. /* reset SD controller */
  728. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  729. writel(reg_tmp | BM_SOFT_RESET, priv->sdmmc_base + SDMMC_BUSMODE);
  730. reg_tmp = readw(priv->sdmmc_base + SDMMC_BLKLEN);
  731. writew(reg_tmp & ~(0xA000), priv->sdmmc_base + SDMMC_BLKLEN);
  732. writeb(0xFF, priv->sdmmc_base + SDMMC_STS0);
  733. writeb(0xFF, priv->sdmmc_base + SDMMC_STS1);
  734. /* release the dma buffers */
  735. dma_free_coherent(&pdev->dev, priv->mmc->max_blk_count * 16,
  736. priv->dma_desc_buffer, priv->dma_desc_device_addr);
  737. mmc_remove_host(mmc);
  738. free_irq(priv->irq_regular, priv);
  739. free_irq(priv->irq_dma, priv);
  740. iounmap(priv->sdmmc_base);
  741. clk_disable_unprepare(priv->clk_sdmmc);
  742. clk_put(priv->clk_sdmmc);
  743. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  744. release_mem_region(res->start, resource_size(res));
  745. mmc_free_host(mmc);
  746. dev_info(&pdev->dev, "WMT MCI device removed\n");
  747. return 0;
  748. }
  749. #ifdef CONFIG_PM
  750. static int wmt_mci_suspend(struct device *dev)
  751. {
  752. u32 reg_tmp;
  753. struct platform_device *pdev = to_platform_device(dev);
  754. struct mmc_host *mmc = platform_get_drvdata(pdev);
  755. struct wmt_mci_priv *priv;
  756. if (!mmc)
  757. return 0;
  758. priv = mmc_priv(mmc);
  759. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  760. writeb(reg_tmp | BM_SOFT_RESET, priv->sdmmc_base +
  761. SDMMC_BUSMODE);
  762. reg_tmp = readw(priv->sdmmc_base + SDMMC_BLKLEN);
  763. writew(reg_tmp & 0x5FFF, priv->sdmmc_base + SDMMC_BLKLEN);
  764. writeb(0xFF, priv->sdmmc_base + SDMMC_STS0);
  765. writeb(0xFF, priv->sdmmc_base + SDMMC_STS1);
  766. clk_disable(priv->clk_sdmmc);
  767. return 0;
  768. }
  769. static int wmt_mci_resume(struct device *dev)
  770. {
  771. u32 reg_tmp;
  772. struct platform_device *pdev = to_platform_device(dev);
  773. struct mmc_host *mmc = platform_get_drvdata(pdev);
  774. struct wmt_mci_priv *priv;
  775. if (mmc) {
  776. priv = mmc_priv(mmc);
  777. clk_enable(priv->clk_sdmmc);
  778. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  779. writeb(reg_tmp | BM_SOFT_RESET, priv->sdmmc_base +
  780. SDMMC_BUSMODE);
  781. reg_tmp = readw(priv->sdmmc_base + SDMMC_BLKLEN);
  782. writew(reg_tmp | (BLKL_GPI_CD | BLKL_INT_ENABLE),
  783. priv->sdmmc_base + SDMMC_BLKLEN);
  784. reg_tmp = readb(priv->sdmmc_base + SDMMC_INTMASK0);
  785. writeb(reg_tmp | INT0_DI_INT_EN, priv->sdmmc_base +
  786. SDMMC_INTMASK0);
  787. }
  788. return 0;
  789. }
  790. static const struct dev_pm_ops wmt_mci_pm = {
  791. .suspend = wmt_mci_suspend,
  792. .resume = wmt_mci_resume,
  793. };
  794. #define wmt_mci_pm_ops (&wmt_mci_pm)
  795. #else /* !CONFIG_PM */
  796. #define wmt_mci_pm_ops NULL
  797. #endif
  798. static struct platform_driver wmt_mci_driver = {
  799. .probe = wmt_mci_probe,
  800. .remove = wmt_mci_remove,
  801. .driver = {
  802. .name = DRIVER_NAME,
  803. .owner = THIS_MODULE,
  804. .pm = wmt_mci_pm_ops,
  805. .of_match_table = wmt_mci_dt_ids,
  806. },
  807. };
  808. module_platform_driver(wmt_mci_driver);
  809. MODULE_DESCRIPTION("Wondermedia MMC/SD Driver");
  810. MODULE_AUTHOR("Tony Prisk");
  811. MODULE_LICENSE("GPL v2");
  812. MODULE_DEVICE_TABLE(of, wmt_mci_dt_ids);