wmt-sdmmc.c 25 KB

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