wmt-sdmmc.c 24 KB

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