pxa_mmc_gen.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
  3. *
  4. * Loosely based on the old code and Linux's PXA MMC driver
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #include <config.h>
  22. #include <common.h>
  23. #include <malloc.h>
  24. #include <mmc.h>
  25. #include <asm/errno.h>
  26. #include <asm/arch/hardware.h>
  27. #include <asm/arch/regs-mmc.h>
  28. #include <asm/io.h>
  29. /* PXAMMC Generic default config for various CPUs */
  30. #if defined(CONFIG_CPU_PXA25X)
  31. #define PXAMMC_FIFO_SIZE 1
  32. #define PXAMMC_MIN_SPEED 312500
  33. #define PXAMMC_MAX_SPEED 20000000
  34. #define PXAMMC_HOST_CAPS (0)
  35. #elif defined(CONFIG_CPU_PXA27X)
  36. #define PXAMMC_CRC_SKIP
  37. #define PXAMMC_FIFO_SIZE 32
  38. #define PXAMMC_MIN_SPEED 304000
  39. #define PXAMMC_MAX_SPEED 19500000
  40. #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT)
  41. #elif defined(CONFIG_CPU_MONAHANS)
  42. #define PXAMMC_FIFO_SIZE 32
  43. #define PXAMMC_MIN_SPEED 304000
  44. #define PXAMMC_MAX_SPEED 26000000
  45. #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT | MMC_MODE_HS)
  46. #else
  47. #error "This CPU isn't supported by PXA MMC!"
  48. #endif
  49. #define MMC_STAT_ERRORS \
  50. (MMC_STAT_RES_CRC_ERROR | MMC_STAT_SPI_READ_ERROR_TOKEN | \
  51. MMC_STAT_CRC_READ_ERROR | MMC_STAT_TIME_OUT_RESPONSE | \
  52. MMC_STAT_READ_TIME_OUT | MMC_STAT_CRC_WRITE_ERROR)
  53. /* 1 millisecond (in wait cycles below it's 100 x 10uS waits) */
  54. #define PXA_MMC_TIMEOUT 100
  55. struct pxa_mmc_priv {
  56. struct pxa_mmc_regs *regs;
  57. };
  58. /* Wait for bit to be set */
  59. static int pxa_mmc_wait(struct mmc *mmc, uint32_t mask)
  60. {
  61. struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
  62. struct pxa_mmc_regs *regs = priv->regs;
  63. unsigned int timeout = PXA_MMC_TIMEOUT;
  64. /* Wait for bit to be set */
  65. while (--timeout) {
  66. if (readl(&regs->stat) & mask)
  67. break;
  68. udelay(10);
  69. }
  70. if (!timeout)
  71. return -ETIMEDOUT;
  72. return 0;
  73. }
  74. static int pxa_mmc_stop_clock(struct mmc *mmc)
  75. {
  76. struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
  77. struct pxa_mmc_regs *regs = priv->regs;
  78. unsigned int timeout = PXA_MMC_TIMEOUT;
  79. /* If the clock aren't running, exit */
  80. if (!(readl(&regs->stat) & MMC_STAT_CLK_EN))
  81. return 0;
  82. /* Tell the controller to turn off the clock */
  83. writel(MMC_STRPCL_STOP_CLK, &regs->strpcl);
  84. /* Wait until the clock are off */
  85. while (--timeout) {
  86. if (!(readl(&regs->stat) & MMC_STAT_CLK_EN))
  87. break;
  88. udelay(10);
  89. }
  90. /* The clock refused to stop, scream and die a painful death */
  91. if (!timeout)
  92. return -ETIMEDOUT;
  93. /* The clock stopped correctly */
  94. return 0;
  95. }
  96. static int pxa_mmc_start_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  97. uint32_t cmdat)
  98. {
  99. struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
  100. struct pxa_mmc_regs *regs = priv->regs;
  101. int ret;
  102. /* The card can send a "busy" response */
  103. if (cmd->resp_type & MMC_RSP_BUSY)
  104. cmdat |= MMC_CMDAT_BUSY;
  105. /* Inform the controller about response type */
  106. switch (cmd->resp_type) {
  107. case MMC_RSP_R1:
  108. case MMC_RSP_R1b:
  109. cmdat |= MMC_CMDAT_R1;
  110. break;
  111. case MMC_RSP_R2:
  112. cmdat |= MMC_CMDAT_R2;
  113. break;
  114. case MMC_RSP_R3:
  115. cmdat |= MMC_CMDAT_R3;
  116. break;
  117. default:
  118. break;
  119. }
  120. /* Load command and it's arguments into the controller */
  121. writel(cmd->cmdidx, &regs->cmd);
  122. writel(cmd->cmdarg >> 16, &regs->argh);
  123. writel(cmd->cmdarg & 0xffff, &regs->argl);
  124. writel(cmdat, &regs->cmdat);
  125. /* Start the controller clock and wait until they are started */
  126. writel(MMC_STRPCL_START_CLK, &regs->strpcl);
  127. ret = pxa_mmc_wait(mmc, MMC_STAT_CLK_EN);
  128. if (ret)
  129. return ret;
  130. /* Correct and happy end */
  131. return 0;
  132. }
  133. static int pxa_mmc_cmd_done(struct mmc *mmc, struct mmc_cmd *cmd)
  134. {
  135. struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
  136. struct pxa_mmc_regs *regs = priv->regs;
  137. uint32_t a, b, c;
  138. int i;
  139. int stat;
  140. /* Read the controller status */
  141. stat = readl(&regs->stat);
  142. /*
  143. * Linux says:
  144. * Did I mention this is Sick. We always need to
  145. * discard the upper 8 bits of the first 16-bit word.
  146. */
  147. a = readl(&regs->res) & 0xffff;
  148. for (i = 0; i < 4; i++) {
  149. b = readl(&regs->res) & 0xffff;
  150. c = readl(&regs->res) & 0xffff;
  151. cmd->response[i] = (a << 24) | (b << 8) | (c >> 8);
  152. a = c;
  153. }
  154. /* The command response didn't arrive */
  155. if (stat & MMC_STAT_TIME_OUT_RESPONSE)
  156. return -ETIMEDOUT;
  157. else if (stat & MMC_STAT_RES_CRC_ERROR
  158. && cmd->resp_type & MMC_RSP_CRC) {
  159. #ifdef PXAMMC_CRC_SKIP
  160. if (cmd->resp_type & MMC_RSP_136
  161. && cmd->response[0] & (1 << 31))
  162. printf("Ignoring CRC, this may be dangerous!\n");
  163. else
  164. #endif
  165. return -EILSEQ;
  166. }
  167. /* The command response was successfully read */
  168. return 0;
  169. }
  170. static int pxa_mmc_do_read_xfer(struct mmc *mmc, struct mmc_data *data)
  171. {
  172. struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
  173. struct pxa_mmc_regs *regs = priv->regs;
  174. uint32_t len;
  175. uint32_t *buf = (uint32_t *)data->dest;
  176. int size;
  177. int ret;
  178. len = data->blocks * data->blocksize;
  179. while (len) {
  180. /* The controller has data ready */
  181. if (readl(&regs->i_reg) & MMC_I_REG_RXFIFO_RD_REQ) {
  182. size = min(len, PXAMMC_FIFO_SIZE);
  183. len -= size;
  184. size /= 4;
  185. /* Read data into the buffer */
  186. while (size--)
  187. *buf++ = readl(&regs->rxfifo);
  188. }
  189. if (readl(&regs->stat) & MMC_STAT_ERRORS)
  190. return -EIO;
  191. }
  192. /* Wait for the transmission-done interrupt */
  193. ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
  194. if (ret)
  195. return ret;
  196. return 0;
  197. }
  198. static int pxa_mmc_do_write_xfer(struct mmc *mmc, struct mmc_data *data)
  199. {
  200. struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
  201. struct pxa_mmc_regs *regs = priv->regs;
  202. uint32_t len;
  203. uint32_t *buf = (uint32_t *)data->src;
  204. int size;
  205. int ret;
  206. len = data->blocks * data->blocksize;
  207. while (len) {
  208. /* The controller is ready to receive data */
  209. if (readl(&regs->i_reg) & MMC_I_REG_TXFIFO_WR_REQ) {
  210. size = min(len, PXAMMC_FIFO_SIZE);
  211. len -= size;
  212. size /= 4;
  213. while (size--)
  214. writel(*buf++, &regs->txfifo);
  215. if (min(len, PXAMMC_FIFO_SIZE) < 32)
  216. writel(MMC_PRTBUF_BUF_PART_FULL, &regs->prtbuf);
  217. }
  218. if (readl(&regs->stat) & MMC_STAT_ERRORS)
  219. return -EIO;
  220. }
  221. /* Wait for the transmission-done interrupt */
  222. ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
  223. if (ret)
  224. return ret;
  225. /* Wait until the data are really written to the card */
  226. ret = pxa_mmc_wait(mmc, MMC_STAT_PRG_DONE);
  227. if (ret)
  228. return ret;
  229. return 0;
  230. }
  231. static int pxa_mmc_request(struct mmc *mmc, struct mmc_cmd *cmd,
  232. struct mmc_data *data)
  233. {
  234. struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
  235. struct pxa_mmc_regs *regs = priv->regs;
  236. uint32_t cmdat = 0;
  237. int ret;
  238. /* Stop the controller */
  239. ret = pxa_mmc_stop_clock(mmc);
  240. if (ret)
  241. return ret;
  242. /* If we're doing data transfer, configure the controller accordingly */
  243. if (data) {
  244. writel(data->blocks, &regs->nob);
  245. writel(data->blocksize, &regs->blklen);
  246. /* This delay can be optimized, but stick with max value */
  247. writel(0xffff, &regs->rdto);
  248. cmdat |= MMC_CMDAT_DATA_EN;
  249. if (data->flags & MMC_DATA_WRITE)
  250. cmdat |= MMC_CMDAT_WRITE;
  251. }
  252. /* Run in 4bit mode if the card can do it */
  253. if (mmc->bus_width == 4)
  254. cmdat |= MMC_CMDAT_SD_4DAT;
  255. /* Execute the command */
  256. ret = pxa_mmc_start_cmd(mmc, cmd, cmdat);
  257. if (ret)
  258. return ret;
  259. /* Wait until the command completes */
  260. ret = pxa_mmc_wait(mmc, MMC_STAT_END_CMD_RES);
  261. if (ret)
  262. return ret;
  263. /* Read back the result */
  264. ret = pxa_mmc_cmd_done(mmc, cmd);
  265. if (ret)
  266. return ret;
  267. /* In case there was a data transfer scheduled, do it */
  268. if (data) {
  269. if (data->flags & MMC_DATA_WRITE)
  270. pxa_mmc_do_write_xfer(mmc, data);
  271. else
  272. pxa_mmc_do_read_xfer(mmc, data);
  273. }
  274. return 0;
  275. }
  276. static void pxa_mmc_set_ios(struct mmc *mmc)
  277. {
  278. struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
  279. struct pxa_mmc_regs *regs = priv->regs;
  280. uint32_t tmp;
  281. uint32_t pxa_mmc_clock;
  282. if (!mmc->clock) {
  283. pxa_mmc_stop_clock(mmc);
  284. return;
  285. }
  286. /* PXA3xx can do 26MHz with special settings. */
  287. if (mmc->clock == 26000000) {
  288. writel(0x7, &regs->clkrt);
  289. return;
  290. }
  291. /* Set clock to the card the usual way. */
  292. pxa_mmc_clock = 0;
  293. tmp = mmc->f_max / mmc->clock;
  294. tmp += tmp % 2;
  295. while (tmp > 1) {
  296. pxa_mmc_clock++;
  297. tmp >>= 1;
  298. }
  299. writel(pxa_mmc_clock, &regs->clkrt);
  300. }
  301. static int pxa_mmc_init(struct mmc *mmc)
  302. {
  303. struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
  304. struct pxa_mmc_regs *regs = priv->regs;
  305. /* Make sure the clock are stopped */
  306. pxa_mmc_stop_clock(mmc);
  307. /* Turn off SPI mode */
  308. writel(0, &regs->spi);
  309. /* Set up maximum timeout to wait for command response */
  310. writel(MMC_RES_TO_MAX_MASK, &regs->resto);
  311. /* Mask all interrupts */
  312. writel(~(MMC_I_MASK_TXFIFO_WR_REQ | MMC_I_MASK_RXFIFO_RD_REQ),
  313. &regs->i_mask);
  314. return 0;
  315. }
  316. int pxa_mmc_register(int card_index)
  317. {
  318. struct mmc *mmc;
  319. struct pxa_mmc_priv *priv;
  320. uint32_t reg;
  321. int ret = -ENOMEM;
  322. mmc = malloc(sizeof(struct mmc));
  323. if (!mmc)
  324. goto err0;
  325. priv = malloc(sizeof(struct pxa_mmc_priv));
  326. if (!priv)
  327. goto err1;
  328. switch (card_index) {
  329. case 0:
  330. priv->regs = (struct pxa_mmc_regs *)MMC0_BASE;
  331. break;
  332. case 1:
  333. priv->regs = (struct pxa_mmc_regs *)MMC1_BASE;
  334. break;
  335. default:
  336. printf("PXA MMC: Invalid MMC controller ID (card_index = %d)\n",
  337. card_index);
  338. goto err2;
  339. }
  340. mmc->priv = priv;
  341. sprintf(mmc->name, "PXA MMC");
  342. mmc->send_cmd = pxa_mmc_request;
  343. mmc->set_ios = pxa_mmc_set_ios;
  344. mmc->init = pxa_mmc_init;
  345. mmc->getcd = NULL;
  346. mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  347. mmc->f_max = PXAMMC_MAX_SPEED;
  348. mmc->f_min = PXAMMC_MIN_SPEED;
  349. mmc->host_caps = PXAMMC_HOST_CAPS;
  350. mmc->b_max = 0;
  351. #ifndef CONFIG_CPU_MONAHANS /* PXA2xx */
  352. reg = readl(CKEN);
  353. reg |= CKEN12_MMC;
  354. writel(reg, CKEN);
  355. #else /* PXA3xx */
  356. reg = readl(CKENA);
  357. reg |= CKENA_12_MMC0 | CKENA_13_MMC1;
  358. writel(reg, CKENA);
  359. #endif
  360. mmc_register(mmc);
  361. return 0;
  362. err2:
  363. free(priv);
  364. err1:
  365. free(mmc);
  366. err0:
  367. return ret;
  368. }