blob: 23bef94f2402cad557b30afc8906c03b94aa207e [file] [log] [blame]
Ian Campbelle24ea552014-05-05 14:42:31 +01001/*
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
11#include <common.h>
Hans de Goede90641f82015-04-22 17:03:17 +020012#include <errno.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010013#include <malloc.h>
14#include <mmc.h>
15#include <asm/io.h>
16#include <asm/arch/clock.h>
17#include <asm/arch/cpu.h>
Hans de Goedecd821132014-10-02 20:29:26 +020018#include <asm/arch/gpio.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010019#include <asm/arch/mmc.h>
Hans de Goedecd821132014-10-02 20:29:26 +020020#include <asm-generic/gpio.h>
Ian Campbelle24ea552014-05-05 14:42:31 +010021
Simon Glasse3c794e2017-07-04 13:31:23 -060022struct sunxi_mmc_priv {
Ian Campbelle24ea552014-05-05 14:42:31 +010023 unsigned mmc_no;
24 uint32_t *mclkreg;
Ian Campbelle24ea552014-05-05 14:42:31 +010025 unsigned fatal_err;
Ian Campbelle24ea552014-05-05 14:42:31 +010026 struct sunxi_mmc *reg;
27 struct mmc_config cfg;
28};
29
30/* support 4 mmc hosts */
Simon Glasse3c794e2017-07-04 13:31:23 -060031struct sunxi_mmc_priv mmc_host[4];
Ian Campbelle24ea552014-05-05 14:42:31 +010032
Hans de Goede967325f2014-10-31 16:55:02 +010033static int sunxi_mmc_getcd_gpio(int sdc_no)
34{
35 switch (sdc_no) {
36 case 0: return sunxi_name_to_gpio(CONFIG_MMC0_CD_PIN);
37 case 1: return sunxi_name_to_gpio(CONFIG_MMC1_CD_PIN);
38 case 2: return sunxi_name_to_gpio(CONFIG_MMC2_CD_PIN);
39 case 3: return sunxi_name_to_gpio(CONFIG_MMC3_CD_PIN);
40 }
Hans de Goede90641f82015-04-22 17:03:17 +020041 return -EINVAL;
Hans de Goede967325f2014-10-31 16:55:02 +010042}
43
Ian Campbelle24ea552014-05-05 14:42:31 +010044static int mmc_resource_init(int sdc_no)
45{
Simon Glass3f5af122017-07-04 13:31:24 -060046 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
Ian Campbelle24ea552014-05-05 14:42:31 +010047 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede967325f2014-10-31 16:55:02 +010048 int cd_pin, ret = 0;
Ian Campbelle24ea552014-05-05 14:42:31 +010049
50 debug("init mmc %d resource\n", sdc_no);
51
52 switch (sdc_no) {
53 case 0:
Simon Glass3f5af122017-07-04 13:31:24 -060054 priv->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE;
55 priv->mclkreg = &ccm->sd0_clk_cfg;
Ian Campbelle24ea552014-05-05 14:42:31 +010056 break;
57 case 1:
Simon Glass3f5af122017-07-04 13:31:24 -060058 priv->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE;
59 priv->mclkreg = &ccm->sd1_clk_cfg;
Ian Campbelle24ea552014-05-05 14:42:31 +010060 break;
61 case 2:
Simon Glass3f5af122017-07-04 13:31:24 -060062 priv->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE;
63 priv->mclkreg = &ccm->sd2_clk_cfg;
Ian Campbelle24ea552014-05-05 14:42:31 +010064 break;
65 case 3:
Simon Glass3f5af122017-07-04 13:31:24 -060066 priv->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE;
67 priv->mclkreg = &ccm->sd3_clk_cfg;
Ian Campbelle24ea552014-05-05 14:42:31 +010068 break;
69 default:
70 printf("Wrong mmc number %d\n", sdc_no);
71 return -1;
72 }
Simon Glass3f5af122017-07-04 13:31:24 -060073 priv->mmc_no = sdc_no;
Ian Campbelle24ea552014-05-05 14:42:31 +010074
Hans de Goede967325f2014-10-31 16:55:02 +010075 cd_pin = sunxi_mmc_getcd_gpio(sdc_no);
Hans de Goede90641f82015-04-22 17:03:17 +020076 if (cd_pin >= 0) {
Hans de Goede967325f2014-10-31 16:55:02 +010077 ret = gpio_request(cd_pin, "mmc_cd");
Hans de Goede1c09fa32015-05-30 16:39:10 +020078 if (!ret) {
79 sunxi_gpio_set_pull(cd_pin, SUNXI_GPIO_PULL_UP);
Axel Linb0c4ae12014-12-20 11:41:25 +080080 ret = gpio_direction_input(cd_pin);
Hans de Goede1c09fa32015-05-30 16:39:10 +020081 }
Axel Linb0c4ae12014-12-20 11:41:25 +080082 }
Hans de Goede967325f2014-10-31 16:55:02 +010083
84 return ret;
Ian Campbelle24ea552014-05-05 14:42:31 +010085}
86
Simon Glass3f5af122017-07-04 13:31:24 -060087static int mmc_set_mod_clk(struct sunxi_mmc_priv *priv, unsigned int hz)
Hans de Goedefc3a8322014-12-07 20:55:10 +010088{
89 unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly;
90
91 if (hz <= 24000000) {
92 pll = CCM_MMC_CTRL_OSCM24;
93 pll_hz = 24000000;
94 } else {
Hans de Goededaf22632015-01-14 19:05:03 +010095#ifdef CONFIG_MACH_SUN9I
96 pll = CCM_MMC_CTRL_PLL_PERIPH0;
97 pll_hz = clock_get_pll4_periph0();
98#else
Hans de Goedefc3a8322014-12-07 20:55:10 +010099 pll = CCM_MMC_CTRL_PLL6;
100 pll_hz = clock_get_pll6();
Hans de Goededaf22632015-01-14 19:05:03 +0100101#endif
Hans de Goedefc3a8322014-12-07 20:55:10 +0100102 }
103
104 div = pll_hz / hz;
105 if (pll_hz % hz)
106 div++;
107
108 n = 0;
109 while (div > 16) {
110 n++;
111 div = (div + 1) / 2;
112 }
113
114 if (n > 3) {
Simon Glass3f5af122017-07-04 13:31:24 -0600115 printf("mmc %u error cannot set clock to %u\n", priv->mmc_no,
116 hz);
Hans de Goedefc3a8322014-12-07 20:55:10 +0100117 return -1;
118 }
119
120 /* determine delays */
121 if (hz <= 400000) {
122 oclk_dly = 0;
Hans de Goedebe909742015-09-23 16:13:10 +0200123 sclk_dly = 0;
Hans de Goedefc3a8322014-12-07 20:55:10 +0100124 } else if (hz <= 25000000) {
125 oclk_dly = 0;
126 sclk_dly = 5;
Hans de Goedebe909742015-09-23 16:13:10 +0200127#ifdef CONFIG_MACH_SUN9I
Hans de Goedefc3a8322014-12-07 20:55:10 +0100128 } else if (hz <= 50000000) {
Hans de Goedebe909742015-09-23 16:13:10 +0200129 oclk_dly = 5;
130 sclk_dly = 4;
Hans de Goedefc3a8322014-12-07 20:55:10 +0100131 } else {
132 /* hz > 50000000 */
133 oclk_dly = 2;
134 sclk_dly = 4;
Hans de Goedebe909742015-09-23 16:13:10 +0200135#else
136 } else if (hz <= 50000000) {
137 oclk_dly = 3;
138 sclk_dly = 4;
139 } else {
140 /* hz > 50000000 */
141 oclk_dly = 1;
142 sclk_dly = 4;
143#endif
Hans de Goedefc3a8322014-12-07 20:55:10 +0100144 }
145
146 writel(CCM_MMC_CTRL_ENABLE | pll | CCM_MMC_CTRL_SCLK_DLY(sclk_dly) |
147 CCM_MMC_CTRL_N(n) | CCM_MMC_CTRL_OCLK_DLY(oclk_dly) |
Simon Glass3f5af122017-07-04 13:31:24 -0600148 CCM_MMC_CTRL_M(div), priv->mclkreg);
Hans de Goedefc3a8322014-12-07 20:55:10 +0100149
150 debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n",
Simon Glass3f5af122017-07-04 13:31:24 -0600151 priv->mmc_no, hz, pll_hz, 1u << n, div, pll_hz / (1u << n) / div);
Hans de Goedefc3a8322014-12-07 20:55:10 +0100152
153 return 0;
154}
155
Simon Glass034e2262017-07-04 13:31:25 -0600156static int mmc_update_clk(struct sunxi_mmc_priv *priv)
Ian Campbelle24ea552014-05-05 14:42:31 +0100157{
Ian Campbelle24ea552014-05-05 14:42:31 +0100158 unsigned int cmd;
159 unsigned timeout_msecs = 2000;
160
161 cmd = SUNXI_MMC_CMD_START |
162 SUNXI_MMC_CMD_UPCLK_ONLY |
163 SUNXI_MMC_CMD_WAIT_PRE_OVER;
Simon Glass3f5af122017-07-04 13:31:24 -0600164 writel(cmd, &priv->reg->cmd);
165 while (readl(&priv->reg->cmd) & SUNXI_MMC_CMD_START) {
Ian Campbelle24ea552014-05-05 14:42:31 +0100166 if (!timeout_msecs--)
167 return -1;
168 udelay(1000);
169 }
170
171 /* clock update sets various irq status bits, clear these */
Simon Glass3f5af122017-07-04 13:31:24 -0600172 writel(readl(&priv->reg->rint), &priv->reg->rint);
Ian Campbelle24ea552014-05-05 14:42:31 +0100173
174 return 0;
175}
176
Simon Glass034e2262017-07-04 13:31:25 -0600177static int mmc_config_clock(struct sunxi_mmc_priv *priv, struct mmc *mmc)
Ian Campbelle24ea552014-05-05 14:42:31 +0100178{
Simon Glass3f5af122017-07-04 13:31:24 -0600179 unsigned rval = readl(&priv->reg->clkcr);
Ian Campbelle24ea552014-05-05 14:42:31 +0100180
181 /* Disable Clock */
182 rval &= ~SUNXI_MMC_CLK_ENABLE;
Simon Glass3f5af122017-07-04 13:31:24 -0600183 writel(rval, &priv->reg->clkcr);
Simon Glass034e2262017-07-04 13:31:25 -0600184 if (mmc_update_clk(priv))
Ian Campbelle24ea552014-05-05 14:42:31 +0100185 return -1;
186
Hans de Goedefc3a8322014-12-07 20:55:10 +0100187 /* Set mod_clk to new rate */
Simon Glass3f5af122017-07-04 13:31:24 -0600188 if (mmc_set_mod_clk(priv, mmc->clock))
Ian Campbelle24ea552014-05-05 14:42:31 +0100189 return -1;
Hans de Goedefc3a8322014-12-07 20:55:10 +0100190
191 /* Clear internal divider */
192 rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK;
Simon Glass3f5af122017-07-04 13:31:24 -0600193 writel(rval, &priv->reg->clkcr);
Hans de Goedefc3a8322014-12-07 20:55:10 +0100194
Ian Campbelle24ea552014-05-05 14:42:31 +0100195 /* Re-enable Clock */
196 rval |= SUNXI_MMC_CLK_ENABLE;
Simon Glass3f5af122017-07-04 13:31:24 -0600197 writel(rval, &priv->reg->clkcr);
Simon Glass034e2262017-07-04 13:31:25 -0600198 if (mmc_update_clk(priv))
Ian Campbelle24ea552014-05-05 14:42:31 +0100199 return -1;
200
201 return 0;
202}
203
Simon Glass034e2262017-07-04 13:31:25 -0600204static int sunxi_mmc_set_ios_common(struct sunxi_mmc_priv *priv,
205 struct mmc *mmc)
Ian Campbelle24ea552014-05-05 14:42:31 +0100206{
Hans de Goedefc3a8322014-12-07 20:55:10 +0100207 debug("set ios: bus_width: %x, clock: %d\n",
208 mmc->bus_width, mmc->clock);
Ian Campbelle24ea552014-05-05 14:42:31 +0100209
210 /* Change clock first */
Simon Glass034e2262017-07-04 13:31:25 -0600211 if (mmc->clock && mmc_config_clock(priv, mmc) != 0) {
Simon Glass3f5af122017-07-04 13:31:24 -0600212 priv->fatal_err = 1;
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900213 return -EINVAL;
Ian Campbelle24ea552014-05-05 14:42:31 +0100214 }
215
216 /* Change bus width */
217 if (mmc->bus_width == 8)
Simon Glass3f5af122017-07-04 13:31:24 -0600218 writel(0x2, &priv->reg->width);
Ian Campbelle24ea552014-05-05 14:42:31 +0100219 else if (mmc->bus_width == 4)
Simon Glass3f5af122017-07-04 13:31:24 -0600220 writel(0x1, &priv->reg->width);
Ian Campbelle24ea552014-05-05 14:42:31 +0100221 else
Simon Glass3f5af122017-07-04 13:31:24 -0600222 writel(0x0, &priv->reg->width);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900223
224 return 0;
Ian Campbelle24ea552014-05-05 14:42:31 +0100225}
226
Siarhei Siamashka5abdb152015-02-01 00:42:14 +0200227static int sunxi_mmc_core_init(struct mmc *mmc)
Ian Campbelle24ea552014-05-05 14:42:31 +0100228{
Simon Glass3f5af122017-07-04 13:31:24 -0600229 struct sunxi_mmc_priv *priv = mmc->priv;
Ian Campbelle24ea552014-05-05 14:42:31 +0100230
231 /* Reset controller */
Simon Glass3f5af122017-07-04 13:31:24 -0600232 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
Hans de Goedeb6ae6762014-06-09 11:36:55 +0200233 udelay(1000);
Ian Campbelle24ea552014-05-05 14:42:31 +0100234
235 return 0;
236}
237
Simon Glass034e2262017-07-04 13:31:25 -0600238static int mmc_trans_data_by_cpu(struct sunxi_mmc_priv *priv, struct mmc *mmc,
239 struct mmc_data *data)
Ian Campbelle24ea552014-05-05 14:42:31 +0100240{
Ian Campbelle24ea552014-05-05 14:42:31 +0100241 const int reading = !!(data->flags & MMC_DATA_READ);
242 const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY :
243 SUNXI_MMC_STATUS_FIFO_FULL;
244 unsigned i;
Ian Campbelle24ea552014-05-05 14:42:31 +0100245 unsigned *buff = (unsigned int *)(reading ? data->dest : data->src);
Yousong Zhou28f69b92015-08-29 21:26:11 +0800246 unsigned byte_cnt = data->blocksize * data->blocks;
Tobias Doerffel26c0c152016-07-08 12:40:14 +0200247 unsigned timeout_usecs = (byte_cnt >> 8) * 1000;
248 if (timeout_usecs < 2000000)
249 timeout_usecs = 2000000;
Ian Campbelle24ea552014-05-05 14:42:31 +0100250
Hans de Goedeb6ae6762014-06-09 11:36:55 +0200251 /* Always read / write data through the CPU */
Simon Glass3f5af122017-07-04 13:31:24 -0600252 setbits_le32(&priv->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB);
Hans de Goedeb6ae6762014-06-09 11:36:55 +0200253
Ian Campbelle24ea552014-05-05 14:42:31 +0100254 for (i = 0; i < (byte_cnt >> 2); i++) {
Simon Glass3f5af122017-07-04 13:31:24 -0600255 while (readl(&priv->reg->status) & status_bit) {
Tobias Doerffel26c0c152016-07-08 12:40:14 +0200256 if (!timeout_usecs--)
Ian Campbelle24ea552014-05-05 14:42:31 +0100257 return -1;
Tobias Doerffel26c0c152016-07-08 12:40:14 +0200258 udelay(1);
Ian Campbelle24ea552014-05-05 14:42:31 +0100259 }
260
261 if (reading)
Simon Glass3f5af122017-07-04 13:31:24 -0600262 buff[i] = readl(&priv->reg->fifo);
Ian Campbelle24ea552014-05-05 14:42:31 +0100263 else
Simon Glass3f5af122017-07-04 13:31:24 -0600264 writel(buff[i], &priv->reg->fifo);
Ian Campbelle24ea552014-05-05 14:42:31 +0100265 }
266
267 return 0;
268}
269
Simon Glass034e2262017-07-04 13:31:25 -0600270static int mmc_rint_wait(struct sunxi_mmc_priv *priv, struct mmc *mmc,
271 uint timeout_msecs, uint done_bit, const char *what)
Ian Campbelle24ea552014-05-05 14:42:31 +0100272{
Ian Campbelle24ea552014-05-05 14:42:31 +0100273 unsigned int status;
274
275 do {
Simon Glass3f5af122017-07-04 13:31:24 -0600276 status = readl(&priv->reg->rint);
Ian Campbelle24ea552014-05-05 14:42:31 +0100277 if (!timeout_msecs-- ||
278 (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) {
279 debug("%s timeout %x\n", what,
280 status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900281 return -ETIMEDOUT;
Ian Campbelle24ea552014-05-05 14:42:31 +0100282 }
283 udelay(1000);
284 } while (!(status & done_bit));
285
286 return 0;
287}
288
Simon Glass034e2262017-07-04 13:31:25 -0600289static int sunxi_mmc_send_cmd_common(struct sunxi_mmc_priv *priv,
290 struct mmc *mmc, struct mmc_cmd *cmd,
291 struct mmc_data *data)
Ian Campbelle24ea552014-05-05 14:42:31 +0100292{
Ian Campbelle24ea552014-05-05 14:42:31 +0100293 unsigned int cmdval = SUNXI_MMC_CMD_START;
294 unsigned int timeout_msecs;
295 int error = 0;
296 unsigned int status = 0;
Ian Campbelle24ea552014-05-05 14:42:31 +0100297 unsigned int bytecnt = 0;
298
Simon Glass3f5af122017-07-04 13:31:24 -0600299 if (priv->fatal_err)
Ian Campbelle24ea552014-05-05 14:42:31 +0100300 return -1;
301 if (cmd->resp_type & MMC_RSP_BUSY)
302 debug("mmc cmd %d check rsp busy\n", cmd->cmdidx);
303 if (cmd->cmdidx == 12)
304 return 0;
305
306 if (!cmd->cmdidx)
307 cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
308 if (cmd->resp_type & MMC_RSP_PRESENT)
309 cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE;
310 if (cmd->resp_type & MMC_RSP_136)
311 cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE;
312 if (cmd->resp_type & MMC_RSP_CRC)
313 cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC;
314
315 if (data) {
Alexander Graf0ea5a042016-03-29 17:29:09 +0200316 if ((u32)(long)data->dest & 0x3) {
Ian Campbelle24ea552014-05-05 14:42:31 +0100317 error = -1;
318 goto out;
319 }
320
321 cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER;
322 if (data->flags & MMC_DATA_WRITE)
323 cmdval |= SUNXI_MMC_CMD_WRITE;
324 if (data->blocks > 1)
325 cmdval |= SUNXI_MMC_CMD_AUTO_STOP;
Simon Glass3f5af122017-07-04 13:31:24 -0600326 writel(data->blocksize, &priv->reg->blksz);
327 writel(data->blocks * data->blocksize, &priv->reg->bytecnt);
Ian Campbelle24ea552014-05-05 14:42:31 +0100328 }
329
Simon Glass3f5af122017-07-04 13:31:24 -0600330 debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", priv->mmc_no,
Ian Campbelle24ea552014-05-05 14:42:31 +0100331 cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg);
Simon Glass3f5af122017-07-04 13:31:24 -0600332 writel(cmd->cmdarg, &priv->reg->arg);
Ian Campbelle24ea552014-05-05 14:42:31 +0100333
334 if (!data)
Simon Glass3f5af122017-07-04 13:31:24 -0600335 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
Ian Campbelle24ea552014-05-05 14:42:31 +0100336
337 /*
338 * transfer data and check status
339 * STATREG[2] : FIFO empty
340 * STATREG[3] : FIFO full
341 */
342 if (data) {
343 int ret = 0;
344
345 bytecnt = data->blocksize * data->blocks;
346 debug("trans data %d bytes\n", bytecnt);
Simon Glass3f5af122017-07-04 13:31:24 -0600347 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
Simon Glass034e2262017-07-04 13:31:25 -0600348 ret = mmc_trans_data_by_cpu(priv, mmc, data);
Ian Campbelle24ea552014-05-05 14:42:31 +0100349 if (ret) {
Simon Glass3f5af122017-07-04 13:31:24 -0600350 error = readl(&priv->reg->rint) &
Ian Campbelle24ea552014-05-05 14:42:31 +0100351 SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT;
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900352 error = -ETIMEDOUT;
Ian Campbelle24ea552014-05-05 14:42:31 +0100353 goto out;
354 }
355 }
356
Simon Glass034e2262017-07-04 13:31:25 -0600357 error = mmc_rint_wait(priv, mmc, 1000, SUNXI_MMC_RINT_COMMAND_DONE,
358 "cmd");
Ian Campbelle24ea552014-05-05 14:42:31 +0100359 if (error)
360 goto out;
361
362 if (data) {
Hans de Goedeb6ae6762014-06-09 11:36:55 +0200363 timeout_msecs = 120;
Ian Campbelle24ea552014-05-05 14:42:31 +0100364 debug("cacl timeout %x msec\n", timeout_msecs);
Simon Glass034e2262017-07-04 13:31:25 -0600365 error = mmc_rint_wait(priv, mmc, timeout_msecs,
Ian Campbelle24ea552014-05-05 14:42:31 +0100366 data->blocks > 1 ?
367 SUNXI_MMC_RINT_AUTO_COMMAND_DONE :
368 SUNXI_MMC_RINT_DATA_OVER,
369 "data");
370 if (error)
371 goto out;
372 }
373
374 if (cmd->resp_type & MMC_RSP_BUSY) {
375 timeout_msecs = 2000;
376 do {
Simon Glass3f5af122017-07-04 13:31:24 -0600377 status = readl(&priv->reg->status);
Ian Campbelle24ea552014-05-05 14:42:31 +0100378 if (!timeout_msecs--) {
379 debug("busy timeout\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900380 error = -ETIMEDOUT;
Ian Campbelle24ea552014-05-05 14:42:31 +0100381 goto out;
382 }
383 udelay(1000);
384 } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY);
385 }
386
387 if (cmd->resp_type & MMC_RSP_136) {
Simon Glass3f5af122017-07-04 13:31:24 -0600388 cmd->response[0] = readl(&priv->reg->resp3);
389 cmd->response[1] = readl(&priv->reg->resp2);
390 cmd->response[2] = readl(&priv->reg->resp1);
391 cmd->response[3] = readl(&priv->reg->resp0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100392 debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
393 cmd->response[3], cmd->response[2],
394 cmd->response[1], cmd->response[0]);
395 } else {
Simon Glass3f5af122017-07-04 13:31:24 -0600396 cmd->response[0] = readl(&priv->reg->resp0);
Ian Campbelle24ea552014-05-05 14:42:31 +0100397 debug("mmc resp 0x%08x\n", cmd->response[0]);
398 }
399out:
Ian Campbelle24ea552014-05-05 14:42:31 +0100400 if (error < 0) {
Simon Glass3f5af122017-07-04 13:31:24 -0600401 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
Simon Glass034e2262017-07-04 13:31:25 -0600402 mmc_update_clk(priv);
Ian Campbelle24ea552014-05-05 14:42:31 +0100403 }
Simon Glass3f5af122017-07-04 13:31:24 -0600404 writel(0xffffffff, &priv->reg->rint);
405 writel(readl(&priv->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET,
406 &priv->reg->gctrl);
Ian Campbelle24ea552014-05-05 14:42:31 +0100407
408 return error;
409}
410
Simon Glass034e2262017-07-04 13:31:25 -0600411static int sunxi_mmc_set_ios_legacy(struct mmc *mmc)
412{
413 struct sunxi_mmc_priv *priv = mmc->priv;
414
415 return sunxi_mmc_set_ios_common(priv, mmc);
416}
417
418static int sunxi_mmc_send_cmd_legacy(struct mmc *mmc, struct mmc_cmd *cmd,
419 struct mmc_data *data)
420{
421 struct sunxi_mmc_priv *priv = mmc->priv;
422
423 return sunxi_mmc_send_cmd_common(priv, mmc, cmd, data);
424}
425
426static int sunxi_mmc_getcd_legacy(struct mmc *mmc)
Hans de Goedecd821132014-10-02 20:29:26 +0200427{
Simon Glass3f5af122017-07-04 13:31:24 -0600428 struct sunxi_mmc_priv *priv = mmc->priv;
Hans de Goede967325f2014-10-31 16:55:02 +0100429 int cd_pin;
Hans de Goedecd821132014-10-02 20:29:26 +0200430
Simon Glass3f5af122017-07-04 13:31:24 -0600431 cd_pin = sunxi_mmc_getcd_gpio(priv->mmc_no);
Hans de Goede90641f82015-04-22 17:03:17 +0200432 if (cd_pin < 0)
Hans de Goedecd821132014-10-02 20:29:26 +0200433 return 1;
434
Axel Linb0c4ae12014-12-20 11:41:25 +0800435 return !gpio_get_value(cd_pin);
Hans de Goedecd821132014-10-02 20:29:26 +0200436}
437
Ian Campbelle24ea552014-05-05 14:42:31 +0100438static const struct mmc_ops sunxi_mmc_ops = {
Simon Glass034e2262017-07-04 13:31:25 -0600439 .send_cmd = sunxi_mmc_send_cmd_legacy,
440 .set_ios = sunxi_mmc_set_ios_legacy,
Siarhei Siamashka5abdb152015-02-01 00:42:14 +0200441 .init = sunxi_mmc_core_init,
Simon Glass034e2262017-07-04 13:31:25 -0600442 .getcd = sunxi_mmc_getcd_legacy,
Ian Campbelle24ea552014-05-05 14:42:31 +0100443};
444
Hans de Goedee79c7c82014-10-02 21:13:54 +0200445struct mmc *sunxi_mmc_init(int sdc_no)
Ian Campbelle24ea552014-05-05 14:42:31 +0100446{
Simon Glassec73d962017-07-04 13:31:26 -0600447 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Simon Glass034e2262017-07-04 13:31:25 -0600448 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
449 struct mmc_config *cfg = &priv->cfg;
Simon Glassec73d962017-07-04 13:31:26 -0600450 int ret;
Ian Campbelle24ea552014-05-05 14:42:31 +0100451
Simon Glass034e2262017-07-04 13:31:25 -0600452 memset(priv, '\0', sizeof(struct sunxi_mmc_priv));
Ian Campbelle24ea552014-05-05 14:42:31 +0100453
454 cfg->name = "SUNXI SD/MMC";
455 cfg->ops = &sunxi_mmc_ops;
456
457 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
458 cfg->host_caps = MMC_MODE_4BIT;
Maxime Ripardfb013182016-11-04 16:18:09 +0100459#if defined(CONFIG_MACH_SUN50I) || defined(CONFIG_MACH_SUN8I)
Siarhei Siamashkad96ebc42016-03-29 17:29:10 +0200460 if (sdc_no == 2)
461 cfg->host_caps = MMC_MODE_8BIT;
462#endif
Rob Herring5a203972015-03-23 17:56:59 -0500463 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Ian Campbelle24ea552014-05-05 14:42:31 +0100464 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
465
466 cfg->f_min = 400000;
467 cfg->f_max = 52000000;
468
Hans de Goede967325f2014-10-31 16:55:02 +0100469 if (mmc_resource_init(sdc_no) != 0)
470 return NULL;
471
Simon Glassec73d962017-07-04 13:31:26 -0600472 /* config ahb clock */
473 debug("init mmc %d clock and io\n", sdc_no);
474 setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no));
475
476#ifdef CONFIG_SUNXI_GEN_SUN6I
477 /* unassert reset */
478 setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no));
479#endif
480#if defined(CONFIG_MACH_SUN9I)
481 /* sun9i has a mmc-common module, also set the gate and reset there */
482 writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET,
483 SUNXI_MMC_COMMON_BASE + 4 * sdc_no);
484#endif
485 ret = mmc_set_mod_clk(priv, 24000000);
486 if (ret)
487 return NULL;
Ian Campbelle24ea552014-05-05 14:42:31 +0100488
Simon Glass034e2262017-07-04 13:31:25 -0600489 return mmc_create(cfg, mmc_host);
Ian Campbelle24ea552014-05-05 14:42:31 +0100490}