wmt-sdmmc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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;
  177. if (enable) {
  178. if (priv->power_inverted) {
  179. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  180. writeb(reg_tmp | BM_SD_OFF,
  181. priv->sdmmc_base + SDMMC_BUSMODE);
  182. } else {
  183. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  184. writeb(reg_tmp & (~BM_SD_OFF),
  185. priv->sdmmc_base + SDMMC_BUSMODE);
  186. }
  187. } else {
  188. if (priv->power_inverted) {
  189. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  190. writeb(reg_tmp & (~BM_SD_OFF),
  191. priv->sdmmc_base + SDMMC_BUSMODE);
  192. } else {
  193. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  194. writeb(reg_tmp | BM_SD_OFF,
  195. priv->sdmmc_base + SDMMC_BUSMODE);
  196. }
  197. }
  198. }
  199. static void wmt_mci_read_response(struct mmc_host *mmc)
  200. {
  201. struct wmt_mci_priv *priv;
  202. int idx1, idx2;
  203. u8 tmp_resp;
  204. u32 response;
  205. priv = mmc_priv(mmc);
  206. for (idx1 = 0; idx1 < 4; idx1++) {
  207. response = 0;
  208. for (idx2 = 0; idx2 < 4; idx2++) {
  209. if ((idx1 == 3) && (idx2 == 3))
  210. tmp_resp = readb(priv->sdmmc_base + SDMMC_RSP);
  211. else
  212. tmp_resp = readb(priv->sdmmc_base + SDMMC_RSP +
  213. (idx1*4) + idx2 + 1);
  214. response |= (tmp_resp << (idx2 * 8));
  215. }
  216. priv->cmd->resp[idx1] = cpu_to_be32(response);
  217. }
  218. }
  219. static void wmt_mci_start_command(struct wmt_mci_priv *priv)
  220. {
  221. u32 reg_tmp;
  222. reg_tmp = readb(priv->sdmmc_base + SDMMC_CTLR);
  223. writeb(reg_tmp | CTLR_CMD_START, priv->sdmmc_base + SDMMC_CTLR);
  224. }
  225. static int wmt_mci_send_command(struct mmc_host *mmc, u8 command, u8 cmdtype,
  226. u32 arg, u8 rsptype)
  227. {
  228. struct wmt_mci_priv *priv;
  229. u32 reg_tmp;
  230. priv = mmc_priv(mmc);
  231. /* write command, arg, resptype registers */
  232. writeb(command, priv->sdmmc_base + SDMMC_CMD);
  233. writel(arg, priv->sdmmc_base + SDMMC_ARG);
  234. writeb(rsptype, priv->sdmmc_base + SDMMC_RSPTYPE);
  235. /* reset response FIFO */
  236. reg_tmp = readb(priv->sdmmc_base + SDMMC_CTLR);
  237. writeb(reg_tmp | CTLR_FIFO_RESET, priv->sdmmc_base + SDMMC_CTLR);
  238. /* ensure clock enabled - VT3465 */
  239. wmt_set_sd_power(priv, WMT_SD_POWER_ON);
  240. /* clear status bits */
  241. writeb(0xFF, priv->sdmmc_base + SDMMC_STS0);
  242. writeb(0xFF, priv->sdmmc_base + SDMMC_STS1);
  243. writeb(0xFF, priv->sdmmc_base + SDMMC_STS2);
  244. writeb(0xFF, priv->sdmmc_base + SDMMC_STS3);
  245. /* set command type */
  246. reg_tmp = readb(priv->sdmmc_base + SDMMC_CTLR);
  247. writeb((reg_tmp & 0x0F) | (cmdtype << 4),
  248. priv->sdmmc_base + SDMMC_CTLR);
  249. return 0;
  250. }
  251. static void wmt_mci_disable_dma(struct wmt_mci_priv *priv)
  252. {
  253. writel(DMA_ISR_INT_STS, priv->sdmmc_base + SDDMA_ISR);
  254. writel(0, priv->sdmmc_base + SDDMA_IER);
  255. }
  256. static void wmt_complete_data_request(struct wmt_mci_priv *priv)
  257. {
  258. struct mmc_request *req;
  259. req = priv->req;
  260. req->data->bytes_xfered = req->data->blksz * req->data->blocks;
  261. /* unmap the DMA pages used for write data */
  262. if (req->data->flags & MMC_DATA_WRITE)
  263. dma_unmap_sg(mmc_dev(priv->mmc), req->data->sg,
  264. req->data->sg_len, DMA_TO_DEVICE);
  265. else
  266. dma_unmap_sg(mmc_dev(priv->mmc), req->data->sg,
  267. req->data->sg_len, DMA_FROM_DEVICE);
  268. /* Check if the DMA ISR returned a data error */
  269. if ((req->cmd->error) || (req->data->error))
  270. mmc_request_done(priv->mmc, req);
  271. else {
  272. wmt_mci_read_response(priv->mmc);
  273. if (!req->data->stop) {
  274. /* single-block read/write requests end here */
  275. mmc_request_done(priv->mmc, req);
  276. } else {
  277. /*
  278. * we change the priv->cmd variable so the response is
  279. * stored in the stop struct rather than the original
  280. * calling command struct
  281. */
  282. priv->comp_cmd = &priv->cmdcomp;
  283. init_completion(priv->comp_cmd);
  284. priv->cmd = req->data->stop;
  285. wmt_mci_send_command(priv->mmc, req->data->stop->opcode,
  286. 7, req->data->stop->arg, 9);
  287. wmt_mci_start_command(priv);
  288. }
  289. }
  290. }
  291. static irqreturn_t wmt_mci_dma_isr(int irq_num, void *data)
  292. {
  293. struct mmc_host *mmc;
  294. struct wmt_mci_priv *priv;
  295. int status;
  296. priv = (struct wmt_mci_priv *)data;
  297. mmc = priv->mmc;
  298. status = readl(priv->sdmmc_base + SDDMA_CCR) & 0x0F;
  299. if (status != DMA_CCR_EVT_SUCCESS) {
  300. dev_err(priv->dev, "DMA Error: Status = %d\n", status);
  301. priv->req->data->error = -ETIMEDOUT;
  302. complete(priv->comp_dma);
  303. return IRQ_HANDLED;
  304. }
  305. priv->req->data->error = 0;
  306. wmt_mci_disable_dma(priv);
  307. complete(priv->comp_dma);
  308. if (priv->comp_cmd) {
  309. if (completion_done(priv->comp_cmd)) {
  310. /*
  311. * if the command (regular) interrupt has already
  312. * completed, finish off the request otherwise we wait
  313. * for the command interrupt and finish from there.
  314. */
  315. wmt_complete_data_request(priv);
  316. }
  317. }
  318. return IRQ_HANDLED;
  319. }
  320. static irqreturn_t wmt_mci_regular_isr(int irq_num, void *data)
  321. {
  322. struct wmt_mci_priv *priv;
  323. u32 status0;
  324. u32 status1;
  325. u32 status2;
  326. u32 reg_tmp;
  327. int cmd_done;
  328. priv = (struct wmt_mci_priv *)data;
  329. cmd_done = 0;
  330. status0 = readb(priv->sdmmc_base + SDMMC_STS0);
  331. status1 = readb(priv->sdmmc_base + SDMMC_STS1);
  332. status2 = readb(priv->sdmmc_base + SDMMC_STS2);
  333. /* Check for card insertion */
  334. reg_tmp = readb(priv->sdmmc_base + SDMMC_INTMASK0);
  335. if ((reg_tmp & INT0_DI_INT_EN) && (status0 & STS0_DEVICE_INS)) {
  336. mmc_detect_change(priv->mmc, 0);
  337. if (priv->cmd)
  338. priv->cmd->error = -ETIMEDOUT;
  339. if (priv->comp_cmd)
  340. complete(priv->comp_cmd);
  341. if (priv->comp_dma) {
  342. wmt_mci_disable_dma(priv);
  343. complete(priv->comp_dma);
  344. }
  345. writeb(STS0_DEVICE_INS, priv->sdmmc_base + SDMMC_STS0);
  346. return IRQ_HANDLED;
  347. }
  348. if ((!priv->req->data) ||
  349. ((priv->req->data->stop) && (priv->cmd == priv->req->data->stop))) {
  350. /* handle non-data & stop_transmission requests */
  351. if (status1 & STS1_CMDRSP_DONE) {
  352. priv->cmd->error = 0;
  353. cmd_done = 1;
  354. } else if ((status1 & STS1_RSP_TIMEOUT) ||
  355. (status1 & STS1_DATA_TIMEOUT)) {
  356. priv->cmd->error = -ETIMEDOUT;
  357. cmd_done = 1;
  358. }
  359. if (cmd_done) {
  360. priv->comp_cmd = NULL;
  361. if (!priv->cmd->error)
  362. wmt_mci_read_response(priv->mmc);
  363. priv->cmd = NULL;
  364. mmc_request_done(priv->mmc, priv->req);
  365. }
  366. } else {
  367. /* handle data requests */
  368. if (status1 & STS1_CMDRSP_DONE) {
  369. if (priv->cmd)
  370. priv->cmd->error = 0;
  371. if (priv->comp_cmd)
  372. complete(priv->comp_cmd);
  373. }
  374. if ((status1 & STS1_RSP_TIMEOUT) ||
  375. (status1 & STS1_DATA_TIMEOUT)) {
  376. if (priv->cmd)
  377. priv->cmd->error = -ETIMEDOUT;
  378. if (priv->comp_cmd)
  379. complete(priv->comp_cmd);
  380. if (priv->comp_dma) {
  381. wmt_mci_disable_dma(priv);
  382. complete(priv->comp_dma);
  383. }
  384. }
  385. if (priv->comp_dma) {
  386. /*
  387. * If the dma interrupt has already completed, finish
  388. * off the request; otherwise we wait for the DMA
  389. * interrupt and finish from there.
  390. */
  391. if (completion_done(priv->comp_dma))
  392. wmt_complete_data_request(priv);
  393. }
  394. }
  395. writeb(status0, priv->sdmmc_base + SDMMC_STS0);
  396. writeb(status1, priv->sdmmc_base + SDMMC_STS1);
  397. writeb(status2, priv->sdmmc_base + SDMMC_STS2);
  398. return IRQ_HANDLED;
  399. }
  400. static void wmt_reset_hardware(struct mmc_host *mmc)
  401. {
  402. struct wmt_mci_priv *priv;
  403. u32 reg_tmp;
  404. priv = mmc_priv(mmc);
  405. /* reset controller */
  406. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  407. writeb(reg_tmp | BM_SOFT_RESET, priv->sdmmc_base + SDMMC_BUSMODE);
  408. /* reset response FIFO */
  409. reg_tmp = readb(priv->sdmmc_base + SDMMC_CTLR);
  410. writeb(reg_tmp | CTLR_FIFO_RESET, priv->sdmmc_base + SDMMC_CTLR);
  411. /* enable GPI pin to detect card */
  412. writew(BLKL_INT_ENABLE | BLKL_GPI_CD, priv->sdmmc_base + SDMMC_BLKLEN);
  413. /* clear interrupt status */
  414. writeb(0xFF, priv->sdmmc_base + SDMMC_STS0);
  415. writeb(0xFF, priv->sdmmc_base + SDMMC_STS1);
  416. /* setup interrupts */
  417. writeb(INT0_CD_INT_EN | INT0_DI_INT_EN, priv->sdmmc_base +
  418. SDMMC_INTMASK0);
  419. writeb(INT1_DATA_TOUT_INT_EN | INT1_CMD_RES_TRAN_DONE_INT_EN |
  420. INT1_CMD_RES_TOUT_INT_EN, priv->sdmmc_base + SDMMC_INTMASK1);
  421. /* set the DMA timeout */
  422. writew(8191, priv->sdmmc_base + SDMMC_DMATIMEOUT);
  423. /* auto clock freezing enable */
  424. reg_tmp = readb(priv->sdmmc_base + SDMMC_STS2);
  425. writeb(reg_tmp | STS2_DIS_FORCECLK, priv->sdmmc_base + SDMMC_STS2);
  426. /* set a default clock speed of 400Khz */
  427. clk_set_rate(priv->clk_sdmmc, 400000);
  428. }
  429. static int wmt_dma_init(struct mmc_host *mmc)
  430. {
  431. struct wmt_mci_priv *priv;
  432. priv = mmc_priv(mmc);
  433. writel(DMA_GCR_SOFT_RESET, priv->sdmmc_base + SDDMA_GCR);
  434. writel(DMA_GCR_DMA_EN, priv->sdmmc_base + SDDMA_GCR);
  435. if ((readl(priv->sdmmc_base + SDDMA_GCR) & DMA_GCR_DMA_EN) != 0)
  436. return 0;
  437. else
  438. return 1;
  439. }
  440. static void wmt_dma_init_descriptor(struct wmt_dma_descriptor *desc,
  441. u16 req_count, u32 buffer_addr, u32 branch_addr, int end)
  442. {
  443. desc->flags = 0x40000000 | req_count;
  444. if (end)
  445. desc->flags |= 0x80000000;
  446. desc->data_buffer_addr = buffer_addr;
  447. desc->branch_addr = branch_addr;
  448. }
  449. static void wmt_dma_config(struct mmc_host *mmc, u32 descaddr, u8 dir)
  450. {
  451. struct wmt_mci_priv *priv;
  452. u32 reg_tmp;
  453. priv = mmc_priv(mmc);
  454. /* Enable DMA Interrupts */
  455. writel(DMA_IER_INT_EN, priv->sdmmc_base + SDDMA_IER);
  456. /* Write DMA Descriptor Pointer Register */
  457. writel(descaddr, priv->sdmmc_base + SDDMA_DESPR);
  458. writel(0x00, priv->sdmmc_base + SDDMA_CCR);
  459. if (dir == PDMA_WRITE) {
  460. reg_tmp = readl(priv->sdmmc_base + SDDMA_CCR);
  461. writel(reg_tmp & DMA_CCR_IF_TO_PERIPHERAL, priv->sdmmc_base +
  462. SDDMA_CCR);
  463. } else {
  464. reg_tmp = readl(priv->sdmmc_base + SDDMA_CCR);
  465. writel(reg_tmp | DMA_CCR_PERIPHERAL_TO_IF, priv->sdmmc_base +
  466. SDDMA_CCR);
  467. }
  468. }
  469. static void wmt_dma_start(struct wmt_mci_priv *priv)
  470. {
  471. u32 reg_tmp;
  472. reg_tmp = readl(priv->sdmmc_base + SDDMA_CCR);
  473. writel(reg_tmp | DMA_CCR_RUN, priv->sdmmc_base + SDDMA_CCR);
  474. }
  475. static void wmt_mci_request(struct mmc_host *mmc, struct mmc_request *req)
  476. {
  477. struct wmt_mci_priv *priv;
  478. struct wmt_dma_descriptor *desc;
  479. u8 command;
  480. u8 cmdtype;
  481. u32 arg;
  482. u8 rsptype;
  483. u32 reg_tmp;
  484. struct scatterlist *sg;
  485. int i;
  486. int sg_cnt;
  487. int offset;
  488. u32 dma_address;
  489. int desc_cnt;
  490. priv = mmc_priv(mmc);
  491. priv->req = req;
  492. /*
  493. * Use the cmd variable to pass a pointer to the resp[] structure
  494. * This is required on multi-block requests to pass the pointer to the
  495. * stop command
  496. */
  497. priv->cmd = req->cmd;
  498. command = req->cmd->opcode;
  499. arg = req->cmd->arg;
  500. rsptype = mmc_resp_type(req->cmd);
  501. cmdtype = 0;
  502. /* rsptype=7 only valid for SPI commands - should be =2 for SD */
  503. if (rsptype == 7)
  504. rsptype = 2;
  505. /* rsptype=21 is R1B, convert for controller */
  506. if (rsptype == 21)
  507. rsptype = 9;
  508. if (!req->data) {
  509. wmt_mci_send_command(mmc, command, cmdtype, arg, rsptype);
  510. wmt_mci_start_command(priv);
  511. /* completion is now handled in the regular_isr() */
  512. }
  513. if (req->data) {
  514. priv->comp_cmd = &priv->cmdcomp;
  515. init_completion(priv->comp_cmd);
  516. wmt_dma_init(mmc);
  517. /* set controller data length */
  518. reg_tmp = readw(priv->sdmmc_base + SDMMC_BLKLEN);
  519. writew((reg_tmp & 0xF800) | (req->data->blksz - 1),
  520. priv->sdmmc_base + SDMMC_BLKLEN);
  521. /* set controller block count */
  522. writew(req->data->blocks, priv->sdmmc_base + SDMMC_BLKCNT);
  523. desc = (struct wmt_dma_descriptor *)priv->dma_desc_buffer;
  524. if (req->data->flags & MMC_DATA_WRITE) {
  525. sg_cnt = dma_map_sg(mmc_dev(mmc), req->data->sg,
  526. req->data->sg_len, DMA_TO_DEVICE);
  527. cmdtype = 1;
  528. if (req->data->blocks > 1)
  529. cmdtype = 3;
  530. } else {
  531. sg_cnt = dma_map_sg(mmc_dev(mmc), req->data->sg,
  532. req->data->sg_len, DMA_FROM_DEVICE);
  533. cmdtype = 2;
  534. if (req->data->blocks > 1)
  535. cmdtype = 4;
  536. }
  537. dma_address = priv->dma_desc_device_addr + 16;
  538. desc_cnt = 0;
  539. for_each_sg(req->data->sg, sg, sg_cnt, i) {
  540. offset = 0;
  541. while (offset < sg_dma_len(sg)) {
  542. wmt_dma_init_descriptor(desc, req->data->blksz,
  543. sg_dma_address(sg)+offset,
  544. dma_address, 0);
  545. desc++;
  546. desc_cnt++;
  547. offset += req->data->blksz;
  548. dma_address += 16;
  549. if (desc_cnt == req->data->blocks)
  550. break;
  551. }
  552. }
  553. desc--;
  554. desc->flags |= 0x80000000;
  555. if (req->data->flags & MMC_DATA_WRITE)
  556. wmt_dma_config(mmc, priv->dma_desc_device_addr,
  557. PDMA_WRITE);
  558. else
  559. wmt_dma_config(mmc, priv->dma_desc_device_addr,
  560. PDMA_READ);
  561. wmt_mci_send_command(mmc, command, cmdtype, arg, rsptype);
  562. priv->comp_dma = &priv->datacomp;
  563. init_completion(priv->comp_dma);
  564. wmt_dma_start(priv);
  565. wmt_mci_start_command(priv);
  566. }
  567. }
  568. static void wmt_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  569. {
  570. struct wmt_mci_priv *priv;
  571. u32 reg_tmp;
  572. priv = mmc_priv(mmc);
  573. if (ios->power_mode == MMC_POWER_UP) {
  574. wmt_reset_hardware(mmc);
  575. wmt_set_sd_power(priv, WMT_SD_POWER_ON);
  576. }
  577. if (ios->power_mode == MMC_POWER_OFF)
  578. wmt_set_sd_power(priv, WMT_SD_POWER_OFF);
  579. if (ios->clock != 0)
  580. clk_set_rate(priv->clk_sdmmc, ios->clock);
  581. switch (ios->bus_width) {
  582. case MMC_BUS_WIDTH_8:
  583. reg_tmp = readb(priv->sdmmc_base + SDMMC_EXTCTRL);
  584. writeb(reg_tmp | 0x04, priv->sdmmc_base + SDMMC_EXTCTRL);
  585. break;
  586. case MMC_BUS_WIDTH_4:
  587. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  588. writeb(reg_tmp | BM_FOURBIT_MODE, priv->sdmmc_base +
  589. SDMMC_BUSMODE);
  590. reg_tmp = readb(priv->sdmmc_base + SDMMC_EXTCTRL);
  591. writeb(reg_tmp & 0xFB, priv->sdmmc_base + SDMMC_EXTCTRL);
  592. break;
  593. case MMC_BUS_WIDTH_1:
  594. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  595. writeb(reg_tmp & BM_ONEBIT_MASK, priv->sdmmc_base +
  596. SDMMC_BUSMODE);
  597. reg_tmp = readb(priv->sdmmc_base + SDMMC_EXTCTRL);
  598. writeb(reg_tmp & 0xFB, priv->sdmmc_base + SDMMC_EXTCTRL);
  599. break;
  600. }
  601. }
  602. static int wmt_mci_get_ro(struct mmc_host *mmc)
  603. {
  604. struct wmt_mci_priv *priv = mmc_priv(mmc);
  605. return !(readb(priv->sdmmc_base + SDMMC_STS0) & STS0_WRITE_PROTECT);
  606. }
  607. static int wmt_mci_get_cd(struct mmc_host *mmc)
  608. {
  609. struct wmt_mci_priv *priv = mmc_priv(mmc);
  610. u32 cd = (readb(priv->sdmmc_base + SDMMC_STS0) & STS0_CD_GPI) >> 3;
  611. return !(cd ^ priv->cd_inverted);
  612. }
  613. static struct mmc_host_ops wmt_mci_ops = {
  614. .request = wmt_mci_request,
  615. .set_ios = wmt_mci_set_ios,
  616. .get_ro = wmt_mci_get_ro,
  617. .get_cd = wmt_mci_get_cd,
  618. };
  619. /* Controller capabilities */
  620. static struct wmt_mci_caps wm8505_caps = {
  621. .f_min = 390425,
  622. .f_max = 50000000,
  623. .ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34,
  624. .caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MMC_HIGHSPEED |
  625. MMC_CAP_SD_HIGHSPEED,
  626. .max_seg_size = 65024,
  627. .max_segs = 128,
  628. .max_blk_size = 2048,
  629. };
  630. static struct of_device_id wmt_mci_dt_ids[] = {
  631. { .compatible = "wm,wm8505-sdhc", .data = &wm8505_caps },
  632. { /* Sentinel */ },
  633. };
  634. static int wmt_mci_probe(struct platform_device *pdev)
  635. {
  636. struct mmc_host *mmc;
  637. struct wmt_mci_priv *priv;
  638. struct device_node *np = pdev->dev.of_node;
  639. const struct of_device_id *of_id =
  640. of_match_device(wmt_mci_dt_ids, &pdev->dev);
  641. const struct wmt_mci_caps *wmt_caps = of_id->data;
  642. int ret;
  643. int regular_irq, dma_irq;
  644. if (!of_id || !of_id->data) {
  645. dev_err(&pdev->dev, "Controller capabilities data missing\n");
  646. return -EFAULT;
  647. }
  648. if (!np) {
  649. dev_err(&pdev->dev, "Missing SDMMC description in devicetree\n");
  650. return -EFAULT;
  651. }
  652. regular_irq = irq_of_parse_and_map(np, 0);
  653. dma_irq = irq_of_parse_and_map(np, 1);
  654. if (!regular_irq || !dma_irq) {
  655. dev_err(&pdev->dev, "Getting IRQs failed!\n");
  656. ret = -ENXIO;
  657. goto fail1;
  658. }
  659. mmc = mmc_alloc_host(sizeof(struct wmt_mci_priv), &pdev->dev);
  660. if (!mmc) {
  661. dev_err(&pdev->dev, "Failed to allocate mmc_host\n");
  662. ret = -ENOMEM;
  663. goto fail1;
  664. }
  665. mmc->ops = &wmt_mci_ops;
  666. mmc->f_min = wmt_caps->f_min;
  667. mmc->f_max = wmt_caps->f_max;
  668. mmc->ocr_avail = wmt_caps->ocr_avail;
  669. mmc->caps = wmt_caps->caps;
  670. mmc->max_seg_size = wmt_caps->max_seg_size;
  671. mmc->max_segs = wmt_caps->max_segs;
  672. mmc->max_blk_size = wmt_caps->max_blk_size;
  673. mmc->max_req_size = (16*512*mmc->max_segs);
  674. mmc->max_blk_count = mmc->max_req_size / 512;
  675. priv = mmc_priv(mmc);
  676. priv->mmc = mmc;
  677. priv->dev = &pdev->dev;
  678. priv->power_inverted = 0;
  679. priv->cd_inverted = 0;
  680. if (of_get_property(np, "sdon-inverted", NULL))
  681. priv->power_inverted = 1;
  682. if (of_get_property(np, "cd-inverted", NULL))
  683. priv->cd_inverted = 1;
  684. priv->sdmmc_base = of_iomap(np, 0);
  685. if (!priv->sdmmc_base) {
  686. dev_err(&pdev->dev, "Failed to map IO space\n");
  687. ret = -ENOMEM;
  688. goto fail2;
  689. }
  690. priv->irq_regular = regular_irq;
  691. priv->irq_dma = dma_irq;
  692. ret = request_irq(regular_irq, wmt_mci_regular_isr, 0, "sdmmc", priv);
  693. if (ret) {
  694. dev_err(&pdev->dev, "Register regular IRQ fail\n");
  695. goto fail3;
  696. }
  697. ret = request_irq(dma_irq, wmt_mci_dma_isr, 32, "sdmmc", priv);
  698. if (ret) {
  699. dev_err(&pdev->dev, "Register DMA IRQ fail\n");
  700. goto fail4;
  701. }
  702. /* alloc some DMA buffers for descriptors/transfers */
  703. priv->dma_desc_buffer = dma_alloc_coherent(&pdev->dev,
  704. mmc->max_blk_count * 16,
  705. &priv->dma_desc_device_addr,
  706. 208);
  707. if (!priv->dma_desc_buffer) {
  708. dev_err(&pdev->dev, "DMA alloc fail\n");
  709. ret = -EPERM;
  710. goto fail5;
  711. }
  712. platform_set_drvdata(pdev, mmc);
  713. priv->clk_sdmmc = of_clk_get(np, 0);
  714. if (IS_ERR(priv->clk_sdmmc)) {
  715. dev_err(&pdev->dev, "Error getting clock\n");
  716. ret = PTR_ERR(priv->clk_sdmmc);
  717. goto fail5;
  718. }
  719. clk_prepare_enable(priv->clk_sdmmc);
  720. /* configure the controller to a known 'ready' state */
  721. wmt_reset_hardware(mmc);
  722. mmc_add_host(mmc);
  723. dev_info(&pdev->dev, "WMT SDHC Controller initialized\n");
  724. return 0;
  725. fail5:
  726. free_irq(dma_irq, priv);
  727. fail4:
  728. free_irq(regular_irq, priv);
  729. fail3:
  730. iounmap(priv->sdmmc_base);
  731. fail2:
  732. mmc_free_host(mmc);
  733. fail1:
  734. return ret;
  735. }
  736. static int wmt_mci_remove(struct platform_device *pdev)
  737. {
  738. struct mmc_host *mmc;
  739. struct wmt_mci_priv *priv;
  740. struct resource *res;
  741. u32 reg_tmp;
  742. mmc = platform_get_drvdata(pdev);
  743. priv = mmc_priv(mmc);
  744. /* reset SD controller */
  745. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  746. writel(reg_tmp | BM_SOFT_RESET, priv->sdmmc_base + SDMMC_BUSMODE);
  747. reg_tmp = readw(priv->sdmmc_base + SDMMC_BLKLEN);
  748. writew(reg_tmp & ~(0xA000), priv->sdmmc_base + SDMMC_BLKLEN);
  749. writeb(0xFF, priv->sdmmc_base + SDMMC_STS0);
  750. writeb(0xFF, priv->sdmmc_base + SDMMC_STS1);
  751. /* release the dma buffers */
  752. dma_free_coherent(&pdev->dev, priv->mmc->max_blk_count * 16,
  753. priv->dma_desc_buffer, priv->dma_desc_device_addr);
  754. mmc_remove_host(mmc);
  755. free_irq(priv->irq_regular, priv);
  756. free_irq(priv->irq_dma, priv);
  757. iounmap(priv->sdmmc_base);
  758. clk_disable_unprepare(priv->clk_sdmmc);
  759. clk_put(priv->clk_sdmmc);
  760. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  761. release_mem_region(res->start, res->end - res->start + 1);
  762. mmc_free_host(mmc);
  763. platform_set_drvdata(pdev, NULL);
  764. dev_info(&pdev->dev, "WMT MCI device removed\n");
  765. return 0;
  766. }
  767. #ifdef CONFIG_PM
  768. static int wmt_mci_suspend(struct device *dev)
  769. {
  770. u32 reg_tmp;
  771. struct platform_device *pdev = to_platform_device(dev);
  772. struct mmc_host *mmc = platform_get_drvdata(pdev);
  773. struct wmt_mci_priv *priv;
  774. int ret;
  775. if (!mmc)
  776. return 0;
  777. priv = mmc_priv(mmc);
  778. ret = mmc_suspend_host(mmc);
  779. if (!ret) {
  780. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  781. writeb(reg_tmp | BM_SOFT_RESET, priv->sdmmc_base +
  782. SDMMC_BUSMODE);
  783. reg_tmp = readw(priv->sdmmc_base + SDMMC_BLKLEN);
  784. writew(reg_tmp & 0x5FFF, priv->sdmmc_base + SDMMC_BLKLEN);
  785. writeb(0xFF, priv->sdmmc_base + SDMMC_STS0);
  786. writeb(0xFF, priv->sdmmc_base + SDMMC_STS1);
  787. clk_disable(priv->clk_sdmmc);
  788. }
  789. return ret;
  790. }
  791. static int wmt_mci_resume(struct device *dev)
  792. {
  793. u32 reg_tmp;
  794. struct platform_device *pdev = to_platform_device(dev);
  795. struct mmc_host *mmc = platform_get_drvdata(pdev);
  796. struct wmt_mci_priv *priv;
  797. int ret = 0;
  798. if (mmc) {
  799. priv = mmc_priv(mmc);
  800. clk_enable(priv->clk_sdmmc);
  801. reg_tmp = readb(priv->sdmmc_base + SDMMC_BUSMODE);
  802. writeb(reg_tmp | BM_SOFT_RESET, priv->sdmmc_base +
  803. SDMMC_BUSMODE);
  804. reg_tmp = readw(priv->sdmmc_base + SDMMC_BLKLEN);
  805. writew(reg_tmp | (BLKL_GPI_CD | BLKL_INT_ENABLE),
  806. priv->sdmmc_base + SDMMC_BLKLEN);
  807. reg_tmp = readb(priv->sdmmc_base + SDMMC_INTMASK0);
  808. writeb(reg_tmp | INT0_DI_INT_EN, priv->sdmmc_base +
  809. SDMMC_INTMASK0);
  810. ret = mmc_resume_host(mmc);
  811. }
  812. return ret;
  813. }
  814. static const struct dev_pm_ops wmt_mci_pm = {
  815. .suspend = wmt_mci_suspend,
  816. .resume = wmt_mci_resume,
  817. };
  818. #define wmt_mci_pm_ops (&wmt_mci_pm)
  819. #else /* !CONFIG_PM */
  820. #define wmt_mci_pm_ops NULL
  821. #endif
  822. static struct platform_driver wmt_mci_driver = {
  823. .probe = wmt_mci_probe,
  824. .remove = wmt_mci_remove,
  825. .driver = {
  826. .name = DRIVER_NAME,
  827. .owner = THIS_MODULE,
  828. .pm = wmt_mci_pm_ops,
  829. .of_match_table = wmt_mci_dt_ids,
  830. },
  831. };
  832. module_platform_driver(wmt_mci_driver);
  833. MODULE_DESCRIPTION("Wondermedia MMC/SD Driver");
  834. MODULE_AUTHOR("Tony Prisk");
  835. MODULE_LICENSE("GPL v2");
  836. MODULE_DEVICE_TABLE(of, wmt_mci_dt_ids);