arm_pl180_mmci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ARM PrimeCell MultiMedia Card Interface - PL180
  4. *
  5. * Copyright (C) ST-Ericsson SA 2010
  6. *
  7. * Author: Ulf Hansson <ulf.hansson@stericsson.com>
  8. * Author: Martin Lundholm <martin.xa.lundholm@stericsson.com>
  9. * Ported to drivers/mmc/ by: Matt Waddel <matt.waddel@linaro.org>
  10. */
  11. /* #define DEBUG */
  12. #include "common.h"
  13. #include <clk.h>
  14. #include <errno.h>
  15. #include <log.h>
  16. #include <malloc.h>
  17. #include <mmc.h>
  18. #include <dm/device_compat.h>
  19. #include <asm/io.h>
  20. #include <asm-generic/gpio.h>
  21. #include "arm_pl180_mmci.h"
  22. #include <linux/delay.h>
  23. #ifdef CONFIG_DM_MMC
  24. #include <dm.h>
  25. #define MMC_CLOCK_MAX 48000000
  26. #define MMC_CLOCK_MIN 400000
  27. struct arm_pl180_mmc_plat {
  28. struct mmc_config cfg;
  29. struct mmc mmc;
  30. };
  31. #endif
  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 -ETIMEDOUT;
  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. static int check_peripheral_id(struct pl180_mmc_host *host, u32 periph_id)
  239. {
  240. return readl(&host->base->periph_id0) == (periph_id & 0xFF) &&
  241. readl(&host->base->periph_id1) == ((periph_id >> 8) & 0xFF) &&
  242. readl(&host->base->periph_id2) == ((periph_id >> 16) & 0xFF) &&
  243. readl(&host->base->periph_id3) == ((periph_id >> 24) & 0xFF);
  244. }
  245. static int 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->cfg->f_max) {
  255. clkdiv = 0;
  256. dev->clock = dev->cfg->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. /* For MMCs' with peripheral id 0x02041180 and 0x03041180, H/W flow control
  293. * needs to be enabled for multi block writes (MMC CMD 18).
  294. */
  295. if (check_peripheral_id(host, 0x02041180) ||
  296. check_peripheral_id(host, 0x03041180))
  297. sdi_clkcr |= SDI_CLKCR_HWFCEN;
  298. writel(sdi_clkcr, &host->base->clock);
  299. udelay(CLK_CHANGE_DELAY);
  300. return 0;
  301. }
  302. #ifndef CONFIG_DM_MMC
  303. /* MMC uses open drain drivers in the enumeration phase */
  304. static int mmc_host_reset(struct mmc *dev)
  305. {
  306. struct pl180_mmc_host *host = dev->priv;
  307. writel(host->pwr_init, &host->base->power);
  308. return 0;
  309. }
  310. static const struct mmc_ops arm_pl180_mmci_ops = {
  311. .send_cmd = host_request,
  312. .set_ios = host_set_ios,
  313. .init = mmc_host_reset,
  314. };
  315. /*
  316. * mmc_host_init - initialize the mmc controller.
  317. * Set initial clock and power for mmc slot.
  318. * Initialize mmc struct and register with mmc framework.
  319. */
  320. int arm_pl180_mmci_init(struct pl180_mmc_host *host, struct mmc **mmc)
  321. {
  322. u32 sdi_u32;
  323. writel(host->pwr_init, &host->base->power);
  324. writel(host->clkdiv_init, &host->base->clock);
  325. udelay(CLK_CHANGE_DELAY);
  326. /* Disable mmc interrupts */
  327. sdi_u32 = readl(&host->base->mask0) & ~SDI_MASK0_MASK;
  328. writel(sdi_u32, &host->base->mask0);
  329. host->cfg.name = host->name;
  330. host->cfg.ops = &arm_pl180_mmci_ops;
  331. /* TODO remove the duplicates */
  332. host->cfg.host_caps = host->caps;
  333. host->cfg.voltages = host->voltages;
  334. host->cfg.f_min = host->clock_min;
  335. host->cfg.f_max = host->clock_max;
  336. if (host->b_max != 0)
  337. host->cfg.b_max = host->b_max;
  338. else
  339. host->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  340. *mmc = mmc_create(&host->cfg, host);
  341. if (!*mmc)
  342. return -1;
  343. debug("registered mmc interface number is:%d\n",
  344. (*mmc)->block_dev.devnum);
  345. return 0;
  346. }
  347. #endif
  348. #ifdef CONFIG_DM_MMC
  349. static void arm_pl180_mmc_init(struct pl180_mmc_host *host)
  350. {
  351. u32 sdi_u32;
  352. writel(host->pwr_init, &host->base->power);
  353. writel(host->clkdiv_init, &host->base->clock);
  354. udelay(CLK_CHANGE_DELAY);
  355. /* Disable mmc interrupts */
  356. sdi_u32 = readl(&host->base->mask0) & ~SDI_MASK0_MASK;
  357. writel(sdi_u32, &host->base->mask0);
  358. }
  359. static int arm_pl180_mmc_probe(struct udevice *dev)
  360. {
  361. struct arm_pl180_mmc_plat *pdata = dev_get_plat(dev);
  362. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  363. struct mmc *mmc = &pdata->mmc;
  364. struct pl180_mmc_host *host = dev_get_priv(dev);
  365. struct mmc_config *cfg = &pdata->cfg;
  366. struct clk clk;
  367. u32 periphid;
  368. int ret;
  369. ret = clk_get_by_index(dev, 0, &clk);
  370. if (ret < 0)
  371. return ret;
  372. ret = clk_enable(&clk);
  373. if (ret) {
  374. clk_free(&clk);
  375. dev_err(dev, "failed to enable clock\n");
  376. return ret;
  377. }
  378. host->pwr_init = INIT_PWR;
  379. host->clkdiv_init = SDI_CLKCR_CLKDIV_INIT_V1 | SDI_CLKCR_CLKEN |
  380. SDI_CLKCR_HWFC_EN;
  381. host->clock_in = clk_get_rate(&clk);
  382. cfg->name = dev->name;
  383. cfg->voltages = VOLTAGE_WINDOW_SD;
  384. cfg->host_caps = 0;
  385. cfg->f_min = host->clock_in / (2 * (SDI_CLKCR_CLKDIV_INIT_V1 + 1));
  386. cfg->f_max = MMC_CLOCK_MAX;
  387. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  388. periphid = dev_read_u32_default(dev, "arm,primecell-periphid", 0);
  389. switch (periphid) {
  390. case STM32_MMCI_ID: /* stm32 variant */
  391. host->version2 = false;
  392. break;
  393. case UX500V2_MMCI_ID:
  394. host->pwr_init = SDI_PWR_OPD | SDI_PWR_PWRCTRL_ON;
  395. host->clkdiv_init = SDI_CLKCR_CLKDIV_INIT_V2 | SDI_CLKCR_CLKEN |
  396. SDI_CLKCR_HWFC_EN;
  397. cfg->voltages = VOLTAGE_WINDOW_MMC;
  398. cfg->f_min = host->clock_in / (2 + SDI_CLKCR_CLKDIV_INIT_V2);
  399. host->version2 = true;
  400. break;
  401. default:
  402. host->version2 = true;
  403. }
  404. gpio_request_by_name(dev, "cd-gpios", 0, &host->cd_gpio, GPIOD_IS_IN);
  405. ret = mmc_of_parse(dev, cfg);
  406. if (ret)
  407. return ret;
  408. arm_pl180_mmc_init(host);
  409. mmc->priv = host;
  410. mmc->dev = dev;
  411. upriv->mmc = mmc;
  412. return 0;
  413. }
  414. int arm_pl180_mmc_bind(struct udevice *dev)
  415. {
  416. struct arm_pl180_mmc_plat *plat = dev_get_plat(dev);
  417. return mmc_bind(dev, &plat->mmc, &plat->cfg);
  418. }
  419. static int dm_host_request(struct udevice *dev, struct mmc_cmd *cmd,
  420. struct mmc_data *data)
  421. {
  422. struct mmc *mmc = mmc_get_mmc_dev(dev);
  423. return host_request(mmc, cmd, data);
  424. }
  425. static int dm_host_set_ios(struct udevice *dev)
  426. {
  427. struct mmc *mmc = mmc_get_mmc_dev(dev);
  428. return host_set_ios(mmc);
  429. }
  430. static int dm_mmc_getcd(struct udevice *dev)
  431. {
  432. struct pl180_mmc_host *host = dev_get_priv(dev);
  433. int value = 1;
  434. if (dm_gpio_is_valid(&host->cd_gpio))
  435. value = dm_gpio_get_value(&host->cd_gpio);
  436. return value;
  437. }
  438. static const struct dm_mmc_ops arm_pl180_dm_mmc_ops = {
  439. .send_cmd = dm_host_request,
  440. .set_ios = dm_host_set_ios,
  441. .get_cd = dm_mmc_getcd,
  442. };
  443. static int arm_pl180_mmc_of_to_plat(struct udevice *dev)
  444. {
  445. struct pl180_mmc_host *host = dev_get_priv(dev);
  446. host->base = dev_read_addr_ptr(dev);
  447. if (!host->base)
  448. return -EINVAL;
  449. return 0;
  450. }
  451. static const struct udevice_id arm_pl180_mmc_match[] = {
  452. { .compatible = "arm,pl180" },
  453. { .compatible = "arm,pl18x" },
  454. { /* sentinel */ }
  455. };
  456. U_BOOT_DRIVER(arm_pl180_mmc) = {
  457. .name = "arm_pl180_mmc",
  458. .id = UCLASS_MMC,
  459. .of_match = arm_pl180_mmc_match,
  460. .ops = &arm_pl180_dm_mmc_ops,
  461. .probe = arm_pl180_mmc_probe,
  462. .of_to_plat = arm_pl180_mmc_of_to_plat,
  463. .bind = arm_pl180_mmc_bind,
  464. .priv_auto = sizeof(struct pl180_mmc_host),
  465. .plat_auto = sizeof(struct arm_pl180_mmc_plat),
  466. };
  467. #endif