arm_pl180_mmci.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * ARM PrimeCell MultiMedia Card Interface - PL180
  3. *
  4. * Copyright (C) ST-Ericsson SA 2010
  5. *
  6. * Author: Ulf Hansson <ulf.hansson@stericsson.com>
  7. * Author: Martin Lundholm <martin.xa.lundholm@stericsson.com>
  8. * Ported to drivers/mmc/ by: Matt Waddel <matt.waddel@linaro.org>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. /* #define DEBUG */
  26. #include <asm/io.h>
  27. #include "common.h"
  28. #include <errno.h>
  29. #include <mmc.h>
  30. #include "arm_pl180_mmci.h"
  31. #include <malloc.h>
  32. static int wait_for_command_end(struct mmc *dev, struct mmc_cmd *cmd)
  33. {
  34. u32 hoststatus, statusmask;
  35. struct pl180_mmc_host *host = dev->priv;
  36. statusmask = SDI_STA_CTIMEOUT | SDI_STA_CCRCFAIL;
  37. if ((cmd->resp_type & MMC_RSP_PRESENT))
  38. statusmask |= SDI_STA_CMDREND;
  39. else
  40. statusmask |= SDI_STA_CMDSENT;
  41. do
  42. hoststatus = readl(&host->base->status) & statusmask;
  43. while (!hoststatus);
  44. writel(statusmask, &host->base->status_clear);
  45. if (hoststatus & SDI_STA_CTIMEOUT) {
  46. debug("CMD%d time out\n", cmd->cmdidx);
  47. return TIMEOUT;
  48. } else if ((hoststatus & SDI_STA_CCRCFAIL) &&
  49. (cmd->resp_type & MMC_RSP_CRC)) {
  50. printf("CMD%d CRC error\n", cmd->cmdidx);
  51. return -EILSEQ;
  52. }
  53. if (cmd->resp_type & MMC_RSP_PRESENT) {
  54. cmd->response[0] = readl(&host->base->response0);
  55. cmd->response[1] = readl(&host->base->response1);
  56. cmd->response[2] = readl(&host->base->response2);
  57. cmd->response[3] = readl(&host->base->response3);
  58. debug("CMD%d response[0]:0x%08X, response[1]:0x%08X, "
  59. "response[2]:0x%08X, response[3]:0x%08X\n",
  60. cmd->cmdidx, cmd->response[0], cmd->response[1],
  61. cmd->response[2], cmd->response[3]);
  62. }
  63. return 0;
  64. }
  65. /* send command to the mmc card and wait for results */
  66. static int do_command(struct mmc *dev, struct mmc_cmd *cmd)
  67. {
  68. int result;
  69. u32 sdi_cmd = 0;
  70. struct pl180_mmc_host *host = dev->priv;
  71. sdi_cmd = ((cmd->cmdidx & SDI_CMD_CMDINDEX_MASK) | SDI_CMD_CPSMEN);
  72. if (cmd->resp_type) {
  73. sdi_cmd |= SDI_CMD_WAITRESP;
  74. if (cmd->resp_type & MMC_RSP_136)
  75. sdi_cmd |= SDI_CMD_LONGRESP;
  76. }
  77. writel((u32)cmd->cmdarg, &host->base->argument);
  78. udelay(COMMAND_REG_DELAY);
  79. writel(sdi_cmd, &host->base->command);
  80. result = wait_for_command_end(dev, cmd);
  81. /* After CMD2 set RCA to a none zero value. */
  82. if ((result == 0) && (cmd->cmdidx == MMC_CMD_ALL_SEND_CID))
  83. dev->rca = 10;
  84. /* After CMD3 open drain is switched off and push pull is used. */
  85. if ((result == 0) && (cmd->cmdidx == MMC_CMD_SET_RELATIVE_ADDR)) {
  86. u32 sdi_pwr = readl(&host->base->power) & ~SDI_PWR_OPD;
  87. writel(sdi_pwr, &host->base->power);
  88. }
  89. return result;
  90. }
  91. static int read_bytes(struct mmc *dev, u32 *dest, u32 blkcount, u32 blksize)
  92. {
  93. u32 *tempbuff = dest;
  94. u64 xfercount = blkcount * blksize;
  95. struct pl180_mmc_host *host = dev->priv;
  96. u32 status, status_err;
  97. debug("read_bytes: blkcount=%u blksize=%u\n", blkcount, blksize);
  98. status = readl(&host->base->status);
  99. status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT |
  100. SDI_STA_RXOVERR);
  101. while ((!status_err) && (xfercount >= sizeof(u32))) {
  102. if (status & SDI_STA_RXDAVL) {
  103. *(tempbuff) = readl(&host->base->fifo);
  104. tempbuff++;
  105. xfercount -= sizeof(u32);
  106. }
  107. status = readl(&host->base->status);
  108. status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT |
  109. SDI_STA_RXOVERR);
  110. }
  111. status_err = status &
  112. (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND |
  113. SDI_STA_RXOVERR);
  114. while (!status_err) {
  115. status = readl(&host->base->status);
  116. status_err = status &
  117. (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND |
  118. SDI_STA_RXOVERR);
  119. }
  120. if (status & SDI_STA_DTIMEOUT) {
  121. printf("Read data timed out, xfercount: %llu, status: 0x%08X\n",
  122. xfercount, status);
  123. return -ETIMEDOUT;
  124. } else if (status & SDI_STA_DCRCFAIL) {
  125. printf("Read data bytes CRC error: 0x%x\n", status);
  126. return -EILSEQ;
  127. } else if (status & SDI_STA_RXOVERR) {
  128. printf("Read data RX overflow error\n");
  129. return -EIO;
  130. }
  131. writel(SDI_ICR_MASK, &host->base->status_clear);
  132. if (xfercount) {
  133. printf("Read data error, xfercount: %llu\n", xfercount);
  134. return -ENOBUFS;
  135. }
  136. return 0;
  137. }
  138. static int write_bytes(struct mmc *dev, u32 *src, u32 blkcount, u32 blksize)
  139. {
  140. u32 *tempbuff = src;
  141. int i;
  142. u64 xfercount = blkcount * blksize;
  143. struct pl180_mmc_host *host = dev->priv;
  144. u32 status, status_err;
  145. debug("write_bytes: blkcount=%u blksize=%u\n", blkcount, blksize);
  146. status = readl(&host->base->status);
  147. status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT);
  148. while (!status_err && xfercount) {
  149. if (status & SDI_STA_TXFIFOBW) {
  150. if (xfercount >= SDI_FIFO_BURST_SIZE * sizeof(u32)) {
  151. for (i = 0; i < SDI_FIFO_BURST_SIZE; i++)
  152. writel(*(tempbuff + i),
  153. &host->base->fifo);
  154. tempbuff += SDI_FIFO_BURST_SIZE;
  155. xfercount -= SDI_FIFO_BURST_SIZE * sizeof(u32);
  156. } else {
  157. while (xfercount >= sizeof(u32)) {
  158. writel(*(tempbuff), &host->base->fifo);
  159. tempbuff++;
  160. xfercount -= sizeof(u32);
  161. }
  162. }
  163. }
  164. status = readl(&host->base->status);
  165. status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT);
  166. }
  167. status_err = status &
  168. (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND);
  169. while (!status_err) {
  170. status = readl(&host->base->status);
  171. status_err = status &
  172. (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND);
  173. }
  174. if (status & SDI_STA_DTIMEOUT) {
  175. printf("Write data timed out, xfercount:%llu,status:0x%08X\n",
  176. xfercount, status);
  177. return -ETIMEDOUT;
  178. } else if (status & SDI_STA_DCRCFAIL) {
  179. printf("Write data CRC error\n");
  180. return -EILSEQ;
  181. }
  182. writel(SDI_ICR_MASK, &host->base->status_clear);
  183. if (xfercount) {
  184. printf("Write data error, xfercount:%llu", xfercount);
  185. return -ENOBUFS;
  186. }
  187. return 0;
  188. }
  189. static int do_data_transfer(struct mmc *dev,
  190. struct mmc_cmd *cmd,
  191. struct mmc_data *data)
  192. {
  193. int error = -ETIMEDOUT;
  194. struct pl180_mmc_host *host = dev->priv;
  195. u32 blksz = 0;
  196. u32 data_ctrl = 0;
  197. u32 data_len = (u32) (data->blocks * data->blocksize);
  198. if (!host->version2) {
  199. blksz = (ffs(data->blocksize) - 1);
  200. data_ctrl |= ((blksz << 4) & SDI_DCTRL_DBLKSIZE_MASK);
  201. } else {
  202. blksz = data->blocksize;
  203. data_ctrl |= (blksz << SDI_DCTRL_DBLOCKSIZE_V2_SHIFT);
  204. }
  205. data_ctrl |= SDI_DCTRL_DTEN | SDI_DCTRL_BUSYMODE;
  206. writel(SDI_DTIMER_DEFAULT, &host->base->datatimer);
  207. writel(data_len, &host->base->datalength);
  208. udelay(DATA_REG_DELAY);
  209. if (data->flags & MMC_DATA_READ) {
  210. data_ctrl |= SDI_DCTRL_DTDIR_IN;
  211. writel(data_ctrl, &host->base->datactrl);
  212. error = do_command(dev, cmd);
  213. if (error)
  214. return error;
  215. error = read_bytes(dev, (u32 *)data->dest, (u32)data->blocks,
  216. (u32)data->blocksize);
  217. } else if (data->flags & MMC_DATA_WRITE) {
  218. error = do_command(dev, cmd);
  219. if (error)
  220. return error;
  221. writel(data_ctrl, &host->base->datactrl);
  222. error = write_bytes(dev, (u32 *)data->src, (u32)data->blocks,
  223. (u32)data->blocksize);
  224. }
  225. return error;
  226. }
  227. static int host_request(struct mmc *dev,
  228. struct mmc_cmd *cmd,
  229. struct mmc_data *data)
  230. {
  231. int result;
  232. if (data)
  233. result = do_data_transfer(dev, cmd, data);
  234. else
  235. result = do_command(dev, cmd);
  236. return result;
  237. }
  238. /* MMC uses open drain drivers in the enumeration phase */
  239. static int mmc_host_reset(struct mmc *dev)
  240. {
  241. struct pl180_mmc_host *host = dev->priv;
  242. writel(host->pwr_init, &host->base->power);
  243. return 0;
  244. }
  245. static void host_set_ios(struct mmc *dev)
  246. {
  247. struct pl180_mmc_host *host = dev->priv;
  248. u32 sdi_clkcr;
  249. sdi_clkcr = readl(&host->base->clock);
  250. /* Ramp up the clock rate */
  251. if (dev->clock) {
  252. u32 clkdiv = 0;
  253. u32 tmp_clock;
  254. if (dev->clock >= dev->f_max) {
  255. clkdiv = 0;
  256. dev->clock = dev->f_max;
  257. } else {
  258. clkdiv = (host->clock_in / dev->clock) - 2;
  259. }
  260. tmp_clock = host->clock_in / (clkdiv + 2);
  261. while (tmp_clock > dev->clock) {
  262. clkdiv++;
  263. tmp_clock = host->clock_in / (clkdiv + 2);
  264. }
  265. if (clkdiv > SDI_CLKCR_CLKDIV_MASK)
  266. clkdiv = SDI_CLKCR_CLKDIV_MASK;
  267. tmp_clock = host->clock_in / (clkdiv + 2);
  268. dev->clock = tmp_clock;
  269. sdi_clkcr &= ~(SDI_CLKCR_CLKDIV_MASK);
  270. sdi_clkcr |= clkdiv;
  271. }
  272. /* Set the bus width */
  273. if (dev->bus_width) {
  274. u32 buswidth = 0;
  275. switch (dev->bus_width) {
  276. case 1:
  277. buswidth |= SDI_CLKCR_WIDBUS_1;
  278. break;
  279. case 4:
  280. buswidth |= SDI_CLKCR_WIDBUS_4;
  281. break;
  282. case 8:
  283. buswidth |= SDI_CLKCR_WIDBUS_8;
  284. break;
  285. default:
  286. printf("Invalid bus width: %d\n", dev->bus_width);
  287. break;
  288. }
  289. sdi_clkcr &= ~(SDI_CLKCR_WIDBUS_MASK);
  290. sdi_clkcr |= buswidth;
  291. }
  292. writel(sdi_clkcr, &host->base->clock);
  293. udelay(CLK_CHANGE_DELAY);
  294. }
  295. /*
  296. * mmc_host_init - initialize the mmc controller.
  297. * Set initial clock and power for mmc slot.
  298. * Initialize mmc struct and register with mmc framework.
  299. */
  300. int arm_pl180_mmci_init(struct pl180_mmc_host *host)
  301. {
  302. struct mmc *dev;
  303. u32 sdi_u32;
  304. dev = malloc(sizeof(struct mmc));
  305. if (!dev)
  306. return -ENOMEM;
  307. memset(dev, 0, sizeof(struct mmc));
  308. dev->priv = host;
  309. writel(host->pwr_init, &host->base->power);
  310. writel(host->clkdiv_init, &host->base->clock);
  311. udelay(CLK_CHANGE_DELAY);
  312. /* Disable mmc interrupts */
  313. sdi_u32 = readl(&host->base->mask0) & ~SDI_MASK0_MASK;
  314. writel(sdi_u32, &host->base->mask0);
  315. strncpy(dev->name, host->name, sizeof(dev->name));
  316. dev->send_cmd = host_request;
  317. dev->set_ios = host_set_ios;
  318. dev->init = mmc_host_reset;
  319. dev->getcd = NULL;
  320. dev->host_caps = host->caps;
  321. dev->voltages = host->voltages;
  322. dev->f_min = host->clock_min;
  323. dev->f_max = host->clock_max;
  324. dev->b_max = host->b_max;
  325. mmc_register(dev);
  326. debug("registered mmc interface number is:%d\n", dev->block_dev.dev);
  327. return 0;
  328. }