1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Loosely based on the old code and Linux's PXA MMC driver
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <common.h>
10*4882a593Smuzhiyun #include <asm/arch/hardware.h>
11*4882a593Smuzhiyun #include <asm/arch/regs-mmc.h>
12*4882a593Smuzhiyun #include <linux/errno.h>
13*4882a593Smuzhiyun #include <asm/io.h>
14*4882a593Smuzhiyun #include <malloc.h>
15*4882a593Smuzhiyun #include <mmc.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /* PXAMMC Generic default config for various CPUs */
18*4882a593Smuzhiyun #if defined(CONFIG_CPU_PXA25X)
19*4882a593Smuzhiyun #define PXAMMC_FIFO_SIZE 1
20*4882a593Smuzhiyun #define PXAMMC_MIN_SPEED 312500
21*4882a593Smuzhiyun #define PXAMMC_MAX_SPEED 20000000
22*4882a593Smuzhiyun #define PXAMMC_HOST_CAPS (0)
23*4882a593Smuzhiyun #elif defined(CONFIG_CPU_PXA27X)
24*4882a593Smuzhiyun #define PXAMMC_CRC_SKIP
25*4882a593Smuzhiyun #define PXAMMC_FIFO_SIZE 32
26*4882a593Smuzhiyun #define PXAMMC_MIN_SPEED 304000
27*4882a593Smuzhiyun #define PXAMMC_MAX_SPEED 19500000
28*4882a593Smuzhiyun #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT)
29*4882a593Smuzhiyun #elif defined(CONFIG_CPU_MONAHANS)
30*4882a593Smuzhiyun #define PXAMMC_FIFO_SIZE 32
31*4882a593Smuzhiyun #define PXAMMC_MIN_SPEED 304000
32*4882a593Smuzhiyun #define PXAMMC_MAX_SPEED 26000000
33*4882a593Smuzhiyun #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT | MMC_MODE_HS)
34*4882a593Smuzhiyun #else
35*4882a593Smuzhiyun #error "This CPU isn't supported by PXA MMC!"
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #define MMC_STAT_ERRORS \
39*4882a593Smuzhiyun (MMC_STAT_RES_CRC_ERROR | MMC_STAT_SPI_READ_ERROR_TOKEN | \
40*4882a593Smuzhiyun MMC_STAT_CRC_READ_ERROR | MMC_STAT_TIME_OUT_RESPONSE | \
41*4882a593Smuzhiyun MMC_STAT_READ_TIME_OUT | MMC_STAT_CRC_WRITE_ERROR)
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* 1 millisecond (in wait cycles below it's 100 x 10uS waits) */
44*4882a593Smuzhiyun #define PXA_MMC_TIMEOUT 100
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun struct pxa_mmc_priv {
47*4882a593Smuzhiyun struct pxa_mmc_regs *regs;
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* Wait for bit to be set */
pxa_mmc_wait(struct mmc * mmc,uint32_t mask)51*4882a593Smuzhiyun static int pxa_mmc_wait(struct mmc *mmc, uint32_t mask)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun struct pxa_mmc_priv *priv = mmc->priv;
54*4882a593Smuzhiyun struct pxa_mmc_regs *regs = priv->regs;
55*4882a593Smuzhiyun unsigned int timeout = PXA_MMC_TIMEOUT;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /* Wait for bit to be set */
58*4882a593Smuzhiyun while (--timeout) {
59*4882a593Smuzhiyun if (readl(®s->stat) & mask)
60*4882a593Smuzhiyun break;
61*4882a593Smuzhiyun udelay(10);
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun if (!timeout)
65*4882a593Smuzhiyun return -ETIMEDOUT;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun return 0;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
pxa_mmc_stop_clock(struct mmc * mmc)70*4882a593Smuzhiyun static int pxa_mmc_stop_clock(struct mmc *mmc)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun struct pxa_mmc_priv *priv = mmc->priv;
73*4882a593Smuzhiyun struct pxa_mmc_regs *regs = priv->regs;
74*4882a593Smuzhiyun unsigned int timeout = PXA_MMC_TIMEOUT;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /* If the clock aren't running, exit */
77*4882a593Smuzhiyun if (!(readl(®s->stat) & MMC_STAT_CLK_EN))
78*4882a593Smuzhiyun return 0;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* Tell the controller to turn off the clock */
81*4882a593Smuzhiyun writel(MMC_STRPCL_STOP_CLK, ®s->strpcl);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* Wait until the clock are off */
84*4882a593Smuzhiyun while (--timeout) {
85*4882a593Smuzhiyun if (!(readl(®s->stat) & MMC_STAT_CLK_EN))
86*4882a593Smuzhiyun break;
87*4882a593Smuzhiyun udelay(10);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* The clock refused to stop, scream and die a painful death */
91*4882a593Smuzhiyun if (!timeout)
92*4882a593Smuzhiyun return -ETIMEDOUT;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* The clock stopped correctly */
95*4882a593Smuzhiyun return 0;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
pxa_mmc_start_cmd(struct mmc * mmc,struct mmc_cmd * cmd,uint32_t cmdat)98*4882a593Smuzhiyun static int pxa_mmc_start_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
99*4882a593Smuzhiyun uint32_t cmdat)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun struct pxa_mmc_priv *priv = mmc->priv;
102*4882a593Smuzhiyun struct pxa_mmc_regs *regs = priv->regs;
103*4882a593Smuzhiyun int ret;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /* The card can send a "busy" response */
106*4882a593Smuzhiyun if (cmd->resp_type & MMC_RSP_BUSY)
107*4882a593Smuzhiyun cmdat |= MMC_CMDAT_BUSY;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /* Inform the controller about response type */
110*4882a593Smuzhiyun switch (cmd->resp_type) {
111*4882a593Smuzhiyun case MMC_RSP_R1:
112*4882a593Smuzhiyun case MMC_RSP_R1b:
113*4882a593Smuzhiyun cmdat |= MMC_CMDAT_R1;
114*4882a593Smuzhiyun break;
115*4882a593Smuzhiyun case MMC_RSP_R2:
116*4882a593Smuzhiyun cmdat |= MMC_CMDAT_R2;
117*4882a593Smuzhiyun break;
118*4882a593Smuzhiyun case MMC_RSP_R3:
119*4882a593Smuzhiyun cmdat |= MMC_CMDAT_R3;
120*4882a593Smuzhiyun break;
121*4882a593Smuzhiyun default:
122*4882a593Smuzhiyun break;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* Load command and it's arguments into the controller */
126*4882a593Smuzhiyun writel(cmd->cmdidx, ®s->cmd);
127*4882a593Smuzhiyun writel(cmd->cmdarg >> 16, ®s->argh);
128*4882a593Smuzhiyun writel(cmd->cmdarg & 0xffff, ®s->argl);
129*4882a593Smuzhiyun writel(cmdat, ®s->cmdat);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /* Start the controller clock and wait until they are started */
132*4882a593Smuzhiyun writel(MMC_STRPCL_START_CLK, ®s->strpcl);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun ret = pxa_mmc_wait(mmc, MMC_STAT_CLK_EN);
135*4882a593Smuzhiyun if (ret)
136*4882a593Smuzhiyun return ret;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /* Correct and happy end */
139*4882a593Smuzhiyun return 0;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
pxa_mmc_cmd_done(struct mmc * mmc,struct mmc_cmd * cmd)142*4882a593Smuzhiyun static int pxa_mmc_cmd_done(struct mmc *mmc, struct mmc_cmd *cmd)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun struct pxa_mmc_priv *priv = mmc->priv;
145*4882a593Smuzhiyun struct pxa_mmc_regs *regs = priv->regs;
146*4882a593Smuzhiyun uint32_t a, b, c;
147*4882a593Smuzhiyun int i;
148*4882a593Smuzhiyun int stat;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /* Read the controller status */
151*4882a593Smuzhiyun stat = readl(®s->stat);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /*
154*4882a593Smuzhiyun * Linux says:
155*4882a593Smuzhiyun * Did I mention this is Sick. We always need to
156*4882a593Smuzhiyun * discard the upper 8 bits of the first 16-bit word.
157*4882a593Smuzhiyun */
158*4882a593Smuzhiyun a = readl(®s->res) & 0xffff;
159*4882a593Smuzhiyun for (i = 0; i < 4; i++) {
160*4882a593Smuzhiyun b = readl(®s->res) & 0xffff;
161*4882a593Smuzhiyun c = readl(®s->res) & 0xffff;
162*4882a593Smuzhiyun cmd->response[i] = (a << 24) | (b << 8) | (c >> 8);
163*4882a593Smuzhiyun a = c;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /* The command response didn't arrive */
167*4882a593Smuzhiyun if (stat & MMC_STAT_TIME_OUT_RESPONSE)
168*4882a593Smuzhiyun return -ETIMEDOUT;
169*4882a593Smuzhiyun else if (stat & MMC_STAT_RES_CRC_ERROR
170*4882a593Smuzhiyun && cmd->resp_type & MMC_RSP_CRC) {
171*4882a593Smuzhiyun #ifdef PXAMMC_CRC_SKIP
172*4882a593Smuzhiyun if (cmd->resp_type & MMC_RSP_136
173*4882a593Smuzhiyun && cmd->response[0] & (1 << 31))
174*4882a593Smuzhiyun printf("Ignoring CRC, this may be dangerous!\n");
175*4882a593Smuzhiyun else
176*4882a593Smuzhiyun #endif
177*4882a593Smuzhiyun return -EILSEQ;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /* The command response was successfully read */
181*4882a593Smuzhiyun return 0;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
pxa_mmc_do_read_xfer(struct mmc * mmc,struct mmc_data * data)184*4882a593Smuzhiyun static int pxa_mmc_do_read_xfer(struct mmc *mmc, struct mmc_data *data)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun struct pxa_mmc_priv *priv = mmc->priv;
187*4882a593Smuzhiyun struct pxa_mmc_regs *regs = priv->regs;
188*4882a593Smuzhiyun uint32_t len;
189*4882a593Smuzhiyun uint32_t *buf = (uint32_t *)data->dest;
190*4882a593Smuzhiyun int size;
191*4882a593Smuzhiyun int ret;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun len = data->blocks * data->blocksize;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun while (len) {
196*4882a593Smuzhiyun /* The controller has data ready */
197*4882a593Smuzhiyun if (readl(®s->i_reg) & MMC_I_REG_RXFIFO_RD_REQ) {
198*4882a593Smuzhiyun size = min(len, (uint32_t)PXAMMC_FIFO_SIZE);
199*4882a593Smuzhiyun len -= size;
200*4882a593Smuzhiyun size /= 4;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /* Read data into the buffer */
203*4882a593Smuzhiyun while (size--)
204*4882a593Smuzhiyun *buf++ = readl(®s->rxfifo);
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun if (readl(®s->stat) & MMC_STAT_ERRORS)
209*4882a593Smuzhiyun return -EIO;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /* Wait for the transmission-done interrupt */
213*4882a593Smuzhiyun ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
214*4882a593Smuzhiyun if (ret)
215*4882a593Smuzhiyun return ret;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun return 0;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
pxa_mmc_do_write_xfer(struct mmc * mmc,struct mmc_data * data)220*4882a593Smuzhiyun static int pxa_mmc_do_write_xfer(struct mmc *mmc, struct mmc_data *data)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun struct pxa_mmc_priv *priv = mmc->priv;
223*4882a593Smuzhiyun struct pxa_mmc_regs *regs = priv->regs;
224*4882a593Smuzhiyun uint32_t len;
225*4882a593Smuzhiyun uint32_t *buf = (uint32_t *)data->src;
226*4882a593Smuzhiyun int size;
227*4882a593Smuzhiyun int ret;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun len = data->blocks * data->blocksize;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun while (len) {
232*4882a593Smuzhiyun /* The controller is ready to receive data */
233*4882a593Smuzhiyun if (readl(®s->i_reg) & MMC_I_REG_TXFIFO_WR_REQ) {
234*4882a593Smuzhiyun size = min(len, (uint32_t)PXAMMC_FIFO_SIZE);
235*4882a593Smuzhiyun len -= size;
236*4882a593Smuzhiyun size /= 4;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun while (size--)
239*4882a593Smuzhiyun writel(*buf++, ®s->txfifo);
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun if (min(len, (uint32_t)PXAMMC_FIFO_SIZE) < 32)
242*4882a593Smuzhiyun writel(MMC_PRTBUF_BUF_PART_FULL, ®s->prtbuf);
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun if (readl(®s->stat) & MMC_STAT_ERRORS)
246*4882a593Smuzhiyun return -EIO;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun /* Wait for the transmission-done interrupt */
250*4882a593Smuzhiyun ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
251*4882a593Smuzhiyun if (ret)
252*4882a593Smuzhiyun return ret;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /* Wait until the data are really written to the card */
255*4882a593Smuzhiyun ret = pxa_mmc_wait(mmc, MMC_STAT_PRG_DONE);
256*4882a593Smuzhiyun if (ret)
257*4882a593Smuzhiyun return ret;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun return 0;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun
pxa_mmc_request(struct mmc * mmc,struct mmc_cmd * cmd,struct mmc_data * data)262*4882a593Smuzhiyun static int pxa_mmc_request(struct mmc *mmc, struct mmc_cmd *cmd,
263*4882a593Smuzhiyun struct mmc_data *data)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun struct pxa_mmc_priv *priv = mmc->priv;
266*4882a593Smuzhiyun struct pxa_mmc_regs *regs = priv->regs;
267*4882a593Smuzhiyun uint32_t cmdat = 0;
268*4882a593Smuzhiyun int ret;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /* Stop the controller */
271*4882a593Smuzhiyun ret = pxa_mmc_stop_clock(mmc);
272*4882a593Smuzhiyun if (ret)
273*4882a593Smuzhiyun return ret;
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /* If we're doing data transfer, configure the controller accordingly */
276*4882a593Smuzhiyun if (data) {
277*4882a593Smuzhiyun writel(data->blocks, ®s->nob);
278*4882a593Smuzhiyun writel(data->blocksize, ®s->blklen);
279*4882a593Smuzhiyun /* This delay can be optimized, but stick with max value */
280*4882a593Smuzhiyun writel(0xffff, ®s->rdto);
281*4882a593Smuzhiyun cmdat |= MMC_CMDAT_DATA_EN;
282*4882a593Smuzhiyun if (data->flags & MMC_DATA_WRITE)
283*4882a593Smuzhiyun cmdat |= MMC_CMDAT_WRITE;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun /* Run in 4bit mode if the card can do it */
287*4882a593Smuzhiyun if (mmc->bus_width == 4)
288*4882a593Smuzhiyun cmdat |= MMC_CMDAT_SD_4DAT;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun /* Execute the command */
291*4882a593Smuzhiyun ret = pxa_mmc_start_cmd(mmc, cmd, cmdat);
292*4882a593Smuzhiyun if (ret)
293*4882a593Smuzhiyun return ret;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /* Wait until the command completes */
296*4882a593Smuzhiyun ret = pxa_mmc_wait(mmc, MMC_STAT_END_CMD_RES);
297*4882a593Smuzhiyun if (ret)
298*4882a593Smuzhiyun return ret;
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun /* Read back the result */
301*4882a593Smuzhiyun ret = pxa_mmc_cmd_done(mmc, cmd);
302*4882a593Smuzhiyun if (ret)
303*4882a593Smuzhiyun return ret;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun /* In case there was a data transfer scheduled, do it */
306*4882a593Smuzhiyun if (data) {
307*4882a593Smuzhiyun if (data->flags & MMC_DATA_WRITE)
308*4882a593Smuzhiyun pxa_mmc_do_write_xfer(mmc, data);
309*4882a593Smuzhiyun else
310*4882a593Smuzhiyun pxa_mmc_do_read_xfer(mmc, data);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun return 0;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
pxa_mmc_set_ios(struct mmc * mmc)316*4882a593Smuzhiyun static int pxa_mmc_set_ios(struct mmc *mmc)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun struct pxa_mmc_priv *priv = mmc->priv;
319*4882a593Smuzhiyun struct pxa_mmc_regs *regs = priv->regs;
320*4882a593Smuzhiyun uint32_t tmp;
321*4882a593Smuzhiyun uint32_t pxa_mmc_clock;
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun if (!mmc->clock) {
324*4882a593Smuzhiyun pxa_mmc_stop_clock(mmc);
325*4882a593Smuzhiyun return 0;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun /* PXA3xx can do 26MHz with special settings. */
329*4882a593Smuzhiyun if (mmc->clock == 26000000) {
330*4882a593Smuzhiyun writel(0x7, ®s->clkrt);
331*4882a593Smuzhiyun return 0;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun /* Set clock to the card the usual way. */
335*4882a593Smuzhiyun pxa_mmc_clock = 0;
336*4882a593Smuzhiyun tmp = mmc->cfg->f_max / mmc->clock;
337*4882a593Smuzhiyun tmp += tmp % 2;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun while (tmp > 1) {
340*4882a593Smuzhiyun pxa_mmc_clock++;
341*4882a593Smuzhiyun tmp >>= 1;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun writel(pxa_mmc_clock, ®s->clkrt);
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun return 0;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun
pxa_mmc_init(struct mmc * mmc)349*4882a593Smuzhiyun static int pxa_mmc_init(struct mmc *mmc)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun struct pxa_mmc_priv *priv = mmc->priv;
352*4882a593Smuzhiyun struct pxa_mmc_regs *regs = priv->regs;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun /* Make sure the clock are stopped */
355*4882a593Smuzhiyun pxa_mmc_stop_clock(mmc);
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /* Turn off SPI mode */
358*4882a593Smuzhiyun writel(0, ®s->spi);
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun /* Set up maximum timeout to wait for command response */
361*4882a593Smuzhiyun writel(MMC_RES_TO_MAX_MASK, ®s->resto);
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun /* Mask all interrupts */
364*4882a593Smuzhiyun writel(~(MMC_I_MASK_TXFIFO_WR_REQ | MMC_I_MASK_RXFIFO_RD_REQ),
365*4882a593Smuzhiyun ®s->i_mask);
366*4882a593Smuzhiyun return 0;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun static const struct mmc_ops pxa_mmc_ops = {
370*4882a593Smuzhiyun .send_cmd = pxa_mmc_request,
371*4882a593Smuzhiyun .set_ios = pxa_mmc_set_ios,
372*4882a593Smuzhiyun .init = pxa_mmc_init,
373*4882a593Smuzhiyun };
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun static struct mmc_config pxa_mmc_cfg = {
376*4882a593Smuzhiyun .name = "PXA MMC",
377*4882a593Smuzhiyun .ops = &pxa_mmc_ops,
378*4882a593Smuzhiyun .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
379*4882a593Smuzhiyun .f_max = PXAMMC_MAX_SPEED,
380*4882a593Smuzhiyun .f_min = PXAMMC_MIN_SPEED,
381*4882a593Smuzhiyun .host_caps = PXAMMC_HOST_CAPS,
382*4882a593Smuzhiyun .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
383*4882a593Smuzhiyun };
384*4882a593Smuzhiyun
pxa_mmc_register(int card_index)385*4882a593Smuzhiyun int pxa_mmc_register(int card_index)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun struct mmc *mmc;
388*4882a593Smuzhiyun struct pxa_mmc_priv *priv;
389*4882a593Smuzhiyun uint32_t reg;
390*4882a593Smuzhiyun int ret = -ENOMEM;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun priv = malloc(sizeof(struct pxa_mmc_priv));
393*4882a593Smuzhiyun if (!priv)
394*4882a593Smuzhiyun goto err0;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun memset(priv, 0, sizeof(*priv));
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun switch (card_index) {
399*4882a593Smuzhiyun case 0:
400*4882a593Smuzhiyun priv->regs = (struct pxa_mmc_regs *)MMC0_BASE;
401*4882a593Smuzhiyun break;
402*4882a593Smuzhiyun case 1:
403*4882a593Smuzhiyun priv->regs = (struct pxa_mmc_regs *)MMC1_BASE;
404*4882a593Smuzhiyun break;
405*4882a593Smuzhiyun default:
406*4882a593Smuzhiyun ret = -EINVAL;
407*4882a593Smuzhiyun printf("PXA MMC: Invalid MMC controller ID (card_index = %d)\n",
408*4882a593Smuzhiyun card_index);
409*4882a593Smuzhiyun goto err1;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun #ifndef CONFIG_CPU_MONAHANS /* PXA2xx */
413*4882a593Smuzhiyun reg = readl(CKEN);
414*4882a593Smuzhiyun reg |= CKEN12_MMC;
415*4882a593Smuzhiyun writel(reg, CKEN);
416*4882a593Smuzhiyun #else /* PXA3xx */
417*4882a593Smuzhiyun reg = readl(CKENA);
418*4882a593Smuzhiyun reg |= CKENA_12_MMC0 | CKENA_13_MMC1;
419*4882a593Smuzhiyun writel(reg, CKENA);
420*4882a593Smuzhiyun #endif
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun mmc = mmc_create(&pxa_mmc_cfg, priv);
423*4882a593Smuzhiyun if (mmc == NULL)
424*4882a593Smuzhiyun goto err1;
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun return 0;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun err1:
429*4882a593Smuzhiyun free(priv);
430*4882a593Smuzhiyun err0:
431*4882a593Smuzhiyun return ret;
432*4882a593Smuzhiyun }
433