sunxi_mmc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * (C) Copyright 2007-2011
  3. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  4. * Aaron <leafy.myeh@allwinnertech.com>
  5. *
  6. * MMC driver for allwinner sunxi platform.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <errno.h>
  12. #include <malloc.h>
  13. #include <mmc.h>
  14. #include <asm/io.h>
  15. #include <asm/arch/clock.h>
  16. #include <asm/arch/cpu.h>
  17. #include <asm/arch/gpio.h>
  18. #include <asm/arch/mmc.h>
  19. #include <asm-generic/gpio.h>
  20. struct sunxi_mmc_host {
  21. unsigned mmc_no;
  22. uint32_t *mclkreg;
  23. unsigned fatal_err;
  24. struct sunxi_mmc *reg;
  25. struct mmc_config cfg;
  26. };
  27. /* support 4 mmc hosts */
  28. struct sunxi_mmc_host mmc_host[4];
  29. static int sunxi_mmc_getcd_gpio(int sdc_no)
  30. {
  31. switch (sdc_no) {
  32. case 0: return sunxi_name_to_gpio(CONFIG_MMC0_CD_PIN);
  33. case 1: return sunxi_name_to_gpio(CONFIG_MMC1_CD_PIN);
  34. case 2: return sunxi_name_to_gpio(CONFIG_MMC2_CD_PIN);
  35. case 3: return sunxi_name_to_gpio(CONFIG_MMC3_CD_PIN);
  36. }
  37. return -EINVAL;
  38. }
  39. static int mmc_resource_init(int sdc_no)
  40. {
  41. struct sunxi_mmc_host *mmchost = &mmc_host[sdc_no];
  42. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  43. int cd_pin, ret = 0;
  44. debug("init mmc %d resource\n", sdc_no);
  45. switch (sdc_no) {
  46. case 0:
  47. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE;
  48. mmchost->mclkreg = &ccm->sd0_clk_cfg;
  49. break;
  50. case 1:
  51. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE;
  52. mmchost->mclkreg = &ccm->sd1_clk_cfg;
  53. break;
  54. case 2:
  55. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE;
  56. mmchost->mclkreg = &ccm->sd2_clk_cfg;
  57. break;
  58. case 3:
  59. mmchost->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE;
  60. mmchost->mclkreg = &ccm->sd3_clk_cfg;
  61. break;
  62. default:
  63. printf("Wrong mmc number %d\n", sdc_no);
  64. return -1;
  65. }
  66. mmchost->mmc_no = sdc_no;
  67. cd_pin = sunxi_mmc_getcd_gpio(sdc_no);
  68. if (cd_pin >= 0) {
  69. ret = gpio_request(cd_pin, "mmc_cd");
  70. if (!ret) {
  71. sunxi_gpio_set_pull(cd_pin, SUNXI_GPIO_PULL_UP);
  72. ret = gpio_direction_input(cd_pin);
  73. }
  74. }
  75. return ret;
  76. }
  77. static int mmc_set_mod_clk(struct sunxi_mmc_host *mmchost, unsigned int hz)
  78. {
  79. unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly;
  80. if (hz <= 24000000) {
  81. pll = CCM_MMC_CTRL_OSCM24;
  82. pll_hz = 24000000;
  83. } else {
  84. #ifdef CONFIG_MACH_SUN9I
  85. pll = CCM_MMC_CTRL_PLL_PERIPH0;
  86. pll_hz = clock_get_pll4_periph0();
  87. #else
  88. pll = CCM_MMC_CTRL_PLL6;
  89. pll_hz = clock_get_pll6();
  90. #endif
  91. }
  92. div = pll_hz / hz;
  93. if (pll_hz % hz)
  94. div++;
  95. n = 0;
  96. while (div > 16) {
  97. n++;
  98. div = (div + 1) / 2;
  99. }
  100. if (n > 3) {
  101. printf("mmc %u error cannot set clock to %u\n",
  102. mmchost->mmc_no, hz);
  103. return -1;
  104. }
  105. /* determine delays */
  106. if (hz <= 400000) {
  107. oclk_dly = 0;
  108. sclk_dly = 0;
  109. } else if (hz <= 25000000) {
  110. oclk_dly = 0;
  111. sclk_dly = 5;
  112. #ifdef CONFIG_MACH_SUN9I
  113. } else if (hz <= 50000000) {
  114. oclk_dly = 5;
  115. sclk_dly = 4;
  116. } else {
  117. /* hz > 50000000 */
  118. oclk_dly = 2;
  119. sclk_dly = 4;
  120. #else
  121. } else if (hz <= 50000000) {
  122. oclk_dly = 3;
  123. sclk_dly = 4;
  124. } else {
  125. /* hz > 50000000 */
  126. oclk_dly = 1;
  127. sclk_dly = 4;
  128. #endif
  129. }
  130. writel(CCM_MMC_CTRL_ENABLE | pll | CCM_MMC_CTRL_SCLK_DLY(sclk_dly) |
  131. CCM_MMC_CTRL_N(n) | CCM_MMC_CTRL_OCLK_DLY(oclk_dly) |
  132. CCM_MMC_CTRL_M(div), mmchost->mclkreg);
  133. debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n",
  134. mmchost->mmc_no, hz, pll_hz, 1u << n, div,
  135. pll_hz / (1u << n) / div);
  136. return 0;
  137. }
  138. static int mmc_clk_io_on(int sdc_no)
  139. {
  140. struct sunxi_mmc_host *mmchost = &mmc_host[sdc_no];
  141. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  142. debug("init mmc %d clock and io\n", sdc_no);
  143. /* config ahb clock */
  144. setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no));
  145. #ifdef CONFIG_SUNXI_GEN_SUN6I
  146. /* unassert reset */
  147. setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no));
  148. #endif
  149. #if defined(CONFIG_MACH_SUN9I)
  150. /* sun9i has a mmc-common module, also set the gate and reset there */
  151. writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET,
  152. SUNXI_MMC_COMMON_BASE + 4 * sdc_no);
  153. #endif
  154. return mmc_set_mod_clk(mmchost, 24000000);
  155. }
  156. static int mmc_update_clk(struct mmc *mmc)
  157. {
  158. struct sunxi_mmc_host *mmchost = mmc->priv;
  159. unsigned int cmd;
  160. unsigned timeout_msecs = 2000;
  161. cmd = SUNXI_MMC_CMD_START |
  162. SUNXI_MMC_CMD_UPCLK_ONLY |
  163. SUNXI_MMC_CMD_WAIT_PRE_OVER;
  164. writel(cmd, &mmchost->reg->cmd);
  165. while (readl(&mmchost->reg->cmd) & SUNXI_MMC_CMD_START) {
  166. if (!timeout_msecs--)
  167. return -1;
  168. udelay(1000);
  169. }
  170. /* clock update sets various irq status bits, clear these */
  171. writel(readl(&mmchost->reg->rint), &mmchost->reg->rint);
  172. return 0;
  173. }
  174. static int mmc_config_clock(struct mmc *mmc)
  175. {
  176. struct sunxi_mmc_host *mmchost = mmc->priv;
  177. unsigned rval = readl(&mmchost->reg->clkcr);
  178. /* Disable Clock */
  179. rval &= ~SUNXI_MMC_CLK_ENABLE;
  180. writel(rval, &mmchost->reg->clkcr);
  181. if (mmc_update_clk(mmc))
  182. return -1;
  183. /* Set mod_clk to new rate */
  184. if (mmc_set_mod_clk(mmchost, mmc->clock))
  185. return -1;
  186. /* Clear internal divider */
  187. rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK;
  188. writel(rval, &mmchost->reg->clkcr);
  189. /* Re-enable Clock */
  190. rval |= SUNXI_MMC_CLK_ENABLE;
  191. writel(rval, &mmchost->reg->clkcr);
  192. if (mmc_update_clk(mmc))
  193. return -1;
  194. return 0;
  195. }
  196. static void sunxi_mmc_set_ios(struct mmc *mmc)
  197. {
  198. struct sunxi_mmc_host *mmchost = mmc->priv;
  199. debug("set ios: bus_width: %x, clock: %d\n",
  200. mmc->bus_width, mmc->clock);
  201. /* Change clock first */
  202. if (mmc->clock && mmc_config_clock(mmc) != 0) {
  203. mmchost->fatal_err = 1;
  204. return;
  205. }
  206. /* Change bus width */
  207. if (mmc->bus_width == 8)
  208. writel(0x2, &mmchost->reg->width);
  209. else if (mmc->bus_width == 4)
  210. writel(0x1, &mmchost->reg->width);
  211. else
  212. writel(0x0, &mmchost->reg->width);
  213. }
  214. static int sunxi_mmc_core_init(struct mmc *mmc)
  215. {
  216. struct sunxi_mmc_host *mmchost = mmc->priv;
  217. /* Reset controller */
  218. writel(SUNXI_MMC_GCTRL_RESET, &mmchost->reg->gctrl);
  219. udelay(1000);
  220. return 0;
  221. }
  222. static int mmc_trans_data_by_cpu(struct mmc *mmc, struct mmc_data *data)
  223. {
  224. struct sunxi_mmc_host *mmchost = mmc->priv;
  225. const int reading = !!(data->flags & MMC_DATA_READ);
  226. const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY :
  227. SUNXI_MMC_STATUS_FIFO_FULL;
  228. unsigned i;
  229. unsigned *buff = (unsigned int *)(reading ? data->dest : data->src);
  230. unsigned byte_cnt = data->blocksize * data->blocks;
  231. unsigned timeout_msecs = byte_cnt >> 8;
  232. if (timeout_msecs < 2000)
  233. timeout_msecs = 2000;
  234. /* Always read / write data through the CPU */
  235. setbits_le32(&mmchost->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB);
  236. for (i = 0; i < (byte_cnt >> 2); i++) {
  237. while (readl(&mmchost->reg->status) & status_bit) {
  238. if (!timeout_msecs--)
  239. return -1;
  240. udelay(1000);
  241. }
  242. if (reading)
  243. buff[i] = readl(&mmchost->reg->fifo);
  244. else
  245. writel(buff[i], &mmchost->reg->fifo);
  246. }
  247. return 0;
  248. }
  249. static int mmc_rint_wait(struct mmc *mmc, unsigned int timeout_msecs,
  250. unsigned int done_bit, const char *what)
  251. {
  252. struct sunxi_mmc_host *mmchost = mmc->priv;
  253. unsigned int status;
  254. do {
  255. status = readl(&mmchost->reg->rint);
  256. if (!timeout_msecs-- ||
  257. (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) {
  258. debug("%s timeout %x\n", what,
  259. status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT);
  260. return TIMEOUT;
  261. }
  262. udelay(1000);
  263. } while (!(status & done_bit));
  264. return 0;
  265. }
  266. static int sunxi_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  267. struct mmc_data *data)
  268. {
  269. struct sunxi_mmc_host *mmchost = mmc->priv;
  270. unsigned int cmdval = SUNXI_MMC_CMD_START;
  271. unsigned int timeout_msecs;
  272. int error = 0;
  273. unsigned int status = 0;
  274. unsigned int bytecnt = 0;
  275. if (mmchost->fatal_err)
  276. return -1;
  277. if (cmd->resp_type & MMC_RSP_BUSY)
  278. debug("mmc cmd %d check rsp busy\n", cmd->cmdidx);
  279. if (cmd->cmdidx == 12)
  280. return 0;
  281. if (!cmd->cmdidx)
  282. cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
  283. if (cmd->resp_type & MMC_RSP_PRESENT)
  284. cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE;
  285. if (cmd->resp_type & MMC_RSP_136)
  286. cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE;
  287. if (cmd->resp_type & MMC_RSP_CRC)
  288. cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC;
  289. if (data) {
  290. if ((u32) data->dest & 0x3) {
  291. error = -1;
  292. goto out;
  293. }
  294. cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER;
  295. if (data->flags & MMC_DATA_WRITE)
  296. cmdval |= SUNXI_MMC_CMD_WRITE;
  297. if (data->blocks > 1)
  298. cmdval |= SUNXI_MMC_CMD_AUTO_STOP;
  299. writel(data->blocksize, &mmchost->reg->blksz);
  300. writel(data->blocks * data->blocksize, &mmchost->reg->bytecnt);
  301. }
  302. debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", mmchost->mmc_no,
  303. cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg);
  304. writel(cmd->cmdarg, &mmchost->reg->arg);
  305. if (!data)
  306. writel(cmdval | cmd->cmdidx, &mmchost->reg->cmd);
  307. /*
  308. * transfer data and check status
  309. * STATREG[2] : FIFO empty
  310. * STATREG[3] : FIFO full
  311. */
  312. if (data) {
  313. int ret = 0;
  314. bytecnt = data->blocksize * data->blocks;
  315. debug("trans data %d bytes\n", bytecnt);
  316. writel(cmdval | cmd->cmdidx, &mmchost->reg->cmd);
  317. ret = mmc_trans_data_by_cpu(mmc, data);
  318. if (ret) {
  319. error = readl(&mmchost->reg->rint) & \
  320. SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT;
  321. error = TIMEOUT;
  322. goto out;
  323. }
  324. }
  325. error = mmc_rint_wait(mmc, 1000, SUNXI_MMC_RINT_COMMAND_DONE, "cmd");
  326. if (error)
  327. goto out;
  328. if (data) {
  329. timeout_msecs = 120;
  330. debug("cacl timeout %x msec\n", timeout_msecs);
  331. error = mmc_rint_wait(mmc, timeout_msecs,
  332. data->blocks > 1 ?
  333. SUNXI_MMC_RINT_AUTO_COMMAND_DONE :
  334. SUNXI_MMC_RINT_DATA_OVER,
  335. "data");
  336. if (error)
  337. goto out;
  338. }
  339. if (cmd->resp_type & MMC_RSP_BUSY) {
  340. timeout_msecs = 2000;
  341. do {
  342. status = readl(&mmchost->reg->status);
  343. if (!timeout_msecs--) {
  344. debug("busy timeout\n");
  345. error = TIMEOUT;
  346. goto out;
  347. }
  348. udelay(1000);
  349. } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY);
  350. }
  351. if (cmd->resp_type & MMC_RSP_136) {
  352. cmd->response[0] = readl(&mmchost->reg->resp3);
  353. cmd->response[1] = readl(&mmchost->reg->resp2);
  354. cmd->response[2] = readl(&mmchost->reg->resp1);
  355. cmd->response[3] = readl(&mmchost->reg->resp0);
  356. debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
  357. cmd->response[3], cmd->response[2],
  358. cmd->response[1], cmd->response[0]);
  359. } else {
  360. cmd->response[0] = readl(&mmchost->reg->resp0);
  361. debug("mmc resp 0x%08x\n", cmd->response[0]);
  362. }
  363. out:
  364. if (error < 0) {
  365. writel(SUNXI_MMC_GCTRL_RESET, &mmchost->reg->gctrl);
  366. mmc_update_clk(mmc);
  367. }
  368. writel(0xffffffff, &mmchost->reg->rint);
  369. writel(readl(&mmchost->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET,
  370. &mmchost->reg->gctrl);
  371. return error;
  372. }
  373. static int sunxi_mmc_getcd(struct mmc *mmc)
  374. {
  375. struct sunxi_mmc_host *mmchost = mmc->priv;
  376. int cd_pin;
  377. cd_pin = sunxi_mmc_getcd_gpio(mmchost->mmc_no);
  378. if (cd_pin < 0)
  379. return 1;
  380. return !gpio_get_value(cd_pin);
  381. }
  382. int sunxi_mmc_has_egon_boot_signature(struct mmc *mmc)
  383. {
  384. char *buf = malloc(512);
  385. int valid_signature = 0;
  386. if (buf == NULL)
  387. panic("Failed to allocate memory\n");
  388. if (mmc_getcd(mmc) && mmc_init(mmc) == 0 &&
  389. mmc->block_dev.block_read(&mmc->block_dev, 16, 1, buf) == 1 &&
  390. strncmp(&buf[4], "eGON.BT0", 8) == 0)
  391. valid_signature = 1;
  392. free(buf);
  393. return valid_signature;
  394. }
  395. static const struct mmc_ops sunxi_mmc_ops = {
  396. .send_cmd = sunxi_mmc_send_cmd,
  397. .set_ios = sunxi_mmc_set_ios,
  398. .init = sunxi_mmc_core_init,
  399. .getcd = sunxi_mmc_getcd,
  400. };
  401. struct mmc *sunxi_mmc_init(int sdc_no)
  402. {
  403. struct mmc_config *cfg = &mmc_host[sdc_no].cfg;
  404. memset(&mmc_host[sdc_no], 0, sizeof(struct sunxi_mmc_host));
  405. cfg->name = "SUNXI SD/MMC";
  406. cfg->ops = &sunxi_mmc_ops;
  407. cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  408. cfg->host_caps = MMC_MODE_4BIT;
  409. cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  410. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  411. cfg->f_min = 400000;
  412. cfg->f_max = 52000000;
  413. if (mmc_resource_init(sdc_no) != 0)
  414. return NULL;
  415. mmc_clk_io_on(sdc_no);
  416. return mmc_create(cfg, &mmc_host[sdc_no]);
  417. }