arm_pl180_mmci.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. struct mmc_host {
  33. struct sdi_registers *base;
  34. };
  35. static int wait_for_command_end(struct mmc *dev, struct mmc_cmd *cmd)
  36. {
  37. u32 hoststatus, statusmask;
  38. struct mmc_host *host = dev->priv;
  39. statusmask = SDI_STA_CTIMEOUT | SDI_STA_CCRCFAIL;
  40. if ((cmd->resp_type & MMC_RSP_PRESENT))
  41. statusmask |= SDI_STA_CMDREND;
  42. else
  43. statusmask |= SDI_STA_CMDSENT;
  44. do
  45. hoststatus = readl(&host->base->status) & statusmask;
  46. while (!hoststatus);
  47. writel(statusmask, &host->base->status_clear);
  48. if (hoststatus & SDI_STA_CTIMEOUT) {
  49. printf("CMD%d time out\n", cmd->cmdidx);
  50. return -ETIMEDOUT;
  51. } else if ((hoststatus & SDI_STA_CCRCFAIL) &&
  52. (cmd->flags & MMC_RSP_CRC)) {
  53. printf("CMD%d CRC error\n", cmd->cmdidx);
  54. return -EILSEQ;
  55. }
  56. if (cmd->resp_type & MMC_RSP_PRESENT) {
  57. cmd->response[0] = readl(&host->base->response0);
  58. cmd->response[1] = readl(&host->base->response1);
  59. cmd->response[2] = readl(&host->base->response2);
  60. cmd->response[3] = readl(&host->base->response3);
  61. debug("CMD%d response[0]:0x%08X, response[1]:0x%08X, "
  62. "response[2]:0x%08X, response[3]:0x%08X\n",
  63. cmd->cmdidx, cmd->response[0], cmd->response[1],
  64. cmd->response[2], cmd->response[3]);
  65. }
  66. return 0;
  67. }
  68. /* send command to the mmc card and wait for results */
  69. static int do_command(struct mmc *dev, struct mmc_cmd *cmd)
  70. {
  71. int result;
  72. u32 sdi_cmd = 0;
  73. struct mmc_host *host = dev->priv;
  74. sdi_cmd = ((cmd->cmdidx & SDI_CMD_CMDINDEX_MASK) | SDI_CMD_CPSMEN);
  75. if (cmd->resp_type) {
  76. sdi_cmd |= SDI_CMD_WAITRESP;
  77. if (cmd->resp_type & MMC_RSP_136)
  78. sdi_cmd |= SDI_CMD_LONGRESP;
  79. }
  80. writel((u32)cmd->cmdarg, &host->base->argument);
  81. udelay(COMMAND_REG_DELAY);
  82. writel(sdi_cmd, &host->base->command);
  83. result = wait_for_command_end(dev, cmd);
  84. /* After CMD2 set RCA to a none zero value. */
  85. if ((result == 0) && (cmd->cmdidx == MMC_CMD_ALL_SEND_CID))
  86. dev->rca = 10;
  87. /* After CMD3 open drain is switched off and push pull is used. */
  88. if ((result == 0) && (cmd->cmdidx == MMC_CMD_SET_RELATIVE_ADDR)) {
  89. u32 sdi_pwr = readl(&host->base->power) & ~SDI_PWR_OPD;
  90. writel(sdi_pwr, &host->base->power);
  91. }
  92. return result;
  93. }
  94. static int read_bytes(struct mmc *dev, u32 *dest, u32 blkcount, u32 blksize)
  95. {
  96. u32 *tempbuff = dest;
  97. u64 xfercount = blkcount * blksize;
  98. struct mmc_host *host = dev->priv;
  99. u32 status, status_err;
  100. debug("read_bytes: blkcount=%u blksize=%u\n", blkcount, blksize);
  101. status = readl(&host->base->status);
  102. status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT |
  103. SDI_STA_RXOVERR);
  104. while ((!status_err) && (xfercount >= sizeof(u32))) {
  105. if (status & SDI_STA_RXDAVL) {
  106. *(tempbuff) = readl(&host->base->fifo);
  107. tempbuff++;
  108. xfercount -= sizeof(u32);
  109. }
  110. status = readl(&host->base->status);
  111. status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT |
  112. SDI_STA_RXOVERR);
  113. }
  114. status_err = status &
  115. (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND |
  116. SDI_STA_RXOVERR);
  117. while (!status_err) {
  118. status = readl(&host->base->status);
  119. status_err = status &
  120. (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND |
  121. SDI_STA_RXOVERR);
  122. }
  123. if (status & SDI_STA_DTIMEOUT) {
  124. printf("Read data timed out, xfercount: %llu, status: 0x%08X\n",
  125. xfercount, status);
  126. return -ETIMEDOUT;
  127. } else if (status & SDI_STA_DCRCFAIL) {
  128. printf("Read data bytes CRC error: 0x%x\n", status);
  129. return -EILSEQ;
  130. } else if (status & SDI_STA_RXOVERR) {
  131. printf("Read data RX overflow error\n");
  132. return -EIO;
  133. }
  134. writel(SDI_ICR_MASK, &host->base->status_clear);
  135. if (xfercount) {
  136. printf("Read data error, xfercount: %llu\n", xfercount);
  137. return -ENOBUFS;
  138. }
  139. return 0;
  140. }
  141. static int write_bytes(struct mmc *dev, u32 *src, u32 blkcount, u32 blksize)
  142. {
  143. u32 *tempbuff = src;
  144. int i;
  145. u64 xfercount = blkcount * blksize;
  146. struct mmc_host *host = dev->priv;
  147. u32 status, status_err;
  148. debug("write_bytes: blkcount=%u blksize=%u\n", blkcount, blksize);
  149. status = readl(&host->base->status);
  150. status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT);
  151. while (!status_err && xfercount) {
  152. if (status & SDI_STA_TXFIFOBW) {
  153. if (xfercount >= SDI_FIFO_BURST_SIZE * sizeof(u32)) {
  154. for (i = 0; i < SDI_FIFO_BURST_SIZE; i++)
  155. writel(*(tempbuff + i),
  156. &host->base->fifo);
  157. tempbuff += SDI_FIFO_BURST_SIZE;
  158. xfercount -= SDI_FIFO_BURST_SIZE * sizeof(u32);
  159. } else {
  160. while (xfercount >= sizeof(u32)) {
  161. writel(*(tempbuff), &host->base->fifo);
  162. tempbuff++;
  163. xfercount -= sizeof(u32);
  164. }
  165. }
  166. }
  167. status = readl(&host->base->status);
  168. status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT);
  169. }
  170. status_err = status &
  171. (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND);
  172. while (!status_err) {
  173. status = readl(&host->base->status);
  174. status_err = status &
  175. (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND);
  176. }
  177. if (status & SDI_STA_DTIMEOUT) {
  178. printf("Write data timed out, xfercount:%llu,status:0x%08X\n",
  179. xfercount, status);
  180. return -ETIMEDOUT;
  181. } else if (status & SDI_STA_DCRCFAIL) {
  182. printf("Write data CRC error\n");
  183. return -EILSEQ;
  184. }
  185. writel(SDI_ICR_MASK, &host->base->status_clear);
  186. if (xfercount) {
  187. printf("Write data error, xfercount:%llu", xfercount);
  188. return -ENOBUFS;
  189. }
  190. return 0;
  191. }
  192. static int do_data_transfer(struct mmc *dev,
  193. struct mmc_cmd *cmd,
  194. struct mmc_data *data)
  195. {
  196. int error = -ETIMEDOUT;
  197. struct mmc_host *host = dev->priv;
  198. u32 blksz = 0;
  199. u32 data_ctrl = 0;
  200. u32 data_len = (u32) (data->blocks * data->blocksize);
  201. blksz = (ffs(data->blocksize) - 1);
  202. data_ctrl |= ((blksz << 4) & SDI_DCTRL_DBLKSIZE_MASK);
  203. data_ctrl |= SDI_DCTRL_DTEN;
  204. writel(SDI_DTIMER_DEFAULT, &host->base->datatimer);
  205. writel(data_len, &host->base->datalength);
  206. udelay(DATA_REG_DELAY);
  207. if (data->flags & MMC_DATA_READ) {
  208. data_ctrl |= SDI_DCTRL_DTDIR_IN;
  209. writel(data_ctrl, &host->base->datactrl);
  210. error = do_command(dev, cmd);
  211. if (error)
  212. return error;
  213. error = read_bytes(dev, (u32 *)data->dest, (u32)data->blocks,
  214. (u32)data->blocksize);
  215. } else if (data->flags & MMC_DATA_WRITE) {
  216. error = do_command(dev, cmd);
  217. if (error)
  218. return error;
  219. writel(data_ctrl, &host->base->datactrl);
  220. error = write_bytes(dev, (u32 *)data->src, (u32)data->blocks,
  221. (u32)data->blocksize);
  222. }
  223. return error;
  224. }
  225. static int host_request(struct mmc *dev,
  226. struct mmc_cmd *cmd,
  227. struct mmc_data *data)
  228. {
  229. int result;
  230. if (data)
  231. result = do_data_transfer(dev, cmd, data);
  232. else
  233. result = do_command(dev, cmd);
  234. return result;
  235. }
  236. /* MMC uses open drain drivers in the enumeration phase */
  237. static int mmc_host_reset(struct mmc *dev)
  238. {
  239. struct mmc_host *host = dev->priv;
  240. u32 sdi_u32 = SDI_PWR_OPD | SDI_PWR_PWRCTRL_ON;
  241. writel(sdi_u32, &host->base->power);
  242. return 0;
  243. }
  244. static void host_set_ios(struct mmc *dev)
  245. {
  246. struct mmc_host *host = dev->priv;
  247. u32 sdi_clkcr;
  248. sdi_clkcr = readl(&host->base->clock);
  249. /* Ramp up the clock rate */
  250. if (dev->clock) {
  251. u32 clkdiv = 0;
  252. if (dev->clock >= dev->f_max)
  253. dev->clock = dev->f_max;
  254. clkdiv = ((ARM_MCLK / dev->clock) / 2) - 1;
  255. if (clkdiv > SDI_CLKCR_CLKDIV_MASK)
  256. clkdiv = SDI_CLKCR_CLKDIV_MASK;
  257. sdi_clkcr &= ~(SDI_CLKCR_CLKDIV_MASK);
  258. sdi_clkcr |= clkdiv;
  259. }
  260. /* Set the bus width */
  261. if (dev->bus_width) {
  262. u32 buswidth = 0;
  263. switch (dev->bus_width) {
  264. case 1:
  265. buswidth |= SDI_CLKCR_WIDBUS_1;
  266. break;
  267. case 4:
  268. buswidth |= SDI_CLKCR_WIDBUS_4;
  269. break;
  270. default:
  271. printf("Invalid bus width\n");
  272. break;
  273. }
  274. sdi_clkcr &= ~(SDI_CLKCR_WIDBUS_MASK);
  275. sdi_clkcr |= buswidth;
  276. }
  277. writel(sdi_clkcr, &host->base->clock);
  278. udelay(CLK_CHANGE_DELAY);
  279. }
  280. struct mmc *alloc_mmc_struct(void)
  281. {
  282. struct mmc_host *host = NULL;
  283. struct mmc *mmc_device = NULL;
  284. host = malloc(sizeof(struct mmc_host));
  285. if (!host)
  286. return NULL;
  287. mmc_device = malloc(sizeof(struct mmc));
  288. if (!mmc_device)
  289. goto err;
  290. mmc_device->priv = host;
  291. return mmc_device;
  292. err:
  293. free(host);
  294. return NULL;
  295. }
  296. /*
  297. * mmc_host_init - initialize the mmc controller.
  298. * Set initial clock and power for mmc slot.
  299. * Initialize mmc struct and register with mmc framework.
  300. */
  301. static int arm_pl180_mmci_host_init(struct mmc *dev)
  302. {
  303. struct mmc_host *host = dev->priv;
  304. u32 sdi_u32;
  305. host->base = (struct sdi_registers *)CONFIG_ARM_PL180_MMCI_BASE;
  306. /* Initially set power-on, full voltage & MMCI read */
  307. sdi_u32 = INIT_PWR;
  308. writel(sdi_u32, &host->base->power);
  309. /* setting clk freq 505KHz */
  310. sdi_u32 = SDI_CLKCR_CLKDIV_INIT | SDI_CLKCR_CLKEN;
  311. writel(sdi_u32, &host->base->clock);
  312. udelay(CLK_CHANGE_DELAY);
  313. /* Disable mmc interrupts */
  314. sdi_u32 = readl(&host->base->mask0) & ~SDI_MASK0_MASK;
  315. writel(sdi_u32, &host->base->mask0);
  316. sprintf(dev->name, "MMC");
  317. dev->clock = ARM_MCLK / (2 * (SDI_CLKCR_CLKDIV_INIT + 1));
  318. dev->send_cmd = host_request;
  319. dev->set_ios = host_set_ios;
  320. dev->init = mmc_host_reset;
  321. dev->host_caps = 0;
  322. dev->voltages = VOLTAGE_WINDOW_MMC;
  323. dev->f_min = dev->clock;
  324. dev->f_max = CONFIG_ARM_PL180_MMCI_CLOCK_FREQ;
  325. return 0;
  326. }
  327. int arm_pl180_mmci_init(void)
  328. {
  329. int error;
  330. struct mmc *dev;
  331. dev = alloc_mmc_struct();
  332. if (!dev)
  333. return -1;
  334. error = arm_pl180_mmci_host_init(dev);
  335. if (error) {
  336. printf("mmci_host_init error - %d\n", error);
  337. return -1;
  338. }
  339. dev->b_max = 0;
  340. mmc_register(dev);
  341. debug("registered mmc interface number is:%d\n", dev->block_dev.dev);
  342. return 0;
  343. }