3 * Rob Emanuele <rob@emanuele.us>
4 * Reinhard Meyer, EMK Elektronik <reinhard.meyer@emk-elektronik.de>
7 * Copyright (C) 2004-2006 Atmel Corporation
9 * SPDX-License-Identifier: GPL-2.0+
19 #include <linux/errno.h>
20 #include <asm/byteorder.h>
21 #include <asm/arch/clk.h>
22 #include <asm/arch/hardware.h>
23 #include "atmel_mci.h"
25 DECLARE_GLOBAL_DATA_PTR
;
27 #ifndef CONFIG_SYS_MMC_CLK_OD
28 # define CONFIG_SYS_MMC_CLK_OD 150000
31 #define MMC_DEFAULT_BLKLEN 512
33 #if defined(CONFIG_ATMEL_MCI_PORTB)
39 struct atmel_mci_priv
{
40 struct mmc_config cfg
;
41 struct atmel_mci
*mci
;
42 unsigned int initialized
:1;
43 unsigned int curr_clk
;
50 /* Read Atmel MCI IP version */
51 static unsigned int atmel_mci_get_version(struct atmel_mci
*mci
)
53 return readl(&mci
->version
) & 0x00000fff;
57 * Print command and status:
59 * - always when DEBUG is defined
62 static void dump_cmd(u32 cmdr
, u32 arg
, u32 status
, const char* msg
)
64 debug("gen_atmel_mci: CMDR %08x (%2u) ARGR %08x (SR: %08x) %s\n",
65 cmdr
, cmdr
& 0x3F, arg
, status
, msg
);
68 /* Setup for MCI Clock and Block Size */
70 static void mci_set_mode(struct atmel_mci_priv
*priv
, u32 hz
, u32 blklen
)
72 struct mmc
*mmc
= &priv
->mmc
;
73 u32 bus_hz
= priv
->bus_clk_rate
;
75 static void mci_set_mode(struct mmc
*mmc
, u32 hz
, u32 blklen
)
77 struct atmel_mci_priv
*priv
= mmc
->priv
;
78 u32 bus_hz
= get_mci_clk_rate();
81 atmel_mci_t
*mci
= priv
->mci
;
83 unsigned int version
= atmel_mci_get_version(mci
);
87 debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n",
90 if (version
>= 0x500) {
91 clkdiv
= DIV_ROUND_UP(bus_hz
, hz
) - 2;
98 debug("mci: setting clock %u Hz, block size %u\n",
99 bus_hz
/ (clkdiv
* 2 + clkodd
+ 2), blklen
);
101 /* find clkdiv yielding a rate <= than requested */
102 for (clkdiv
= 0; clkdiv
< 255; clkdiv
++) {
103 if ((bus_hz
/ (clkdiv
+ 1) / 2) <= hz
)
106 debug("mci: setting clock %u Hz, block size %u\n",
107 (bus_hz
/ (clkdiv
+ 1)) / 2, blklen
);
111 if (version
>= 0x500)
112 priv
->curr_clk
= bus_hz
/ (clkdiv
* 2 + clkodd
+ 2);
114 priv
->curr_clk
= (bus_hz
/ (clkdiv
+ 1)) / 2;
117 mr
= MMCI_BF(CLKDIV
, clkdiv
);
119 /* MCI IP version >= 0x200 has R/WPROOF */
120 if (version
>= 0x200)
121 mr
|= MMCI_BIT(RDPROOF
) | MMCI_BIT(WRPROOF
);
124 * MCI IP version >= 0x500 use bit 16 as clkodd.
125 * MCI IP version < 0x500 use upper 16 bits for blklen.
127 if (version
>= 0x500)
128 mr
|= MMCI_BF(CLKODD
, clkodd
);
130 mr
|= MMCI_BF(BLKLEN
, blklen
);
132 writel(mr
, &mci
->mr
);
134 /* MCI IP version >= 0x200 has blkr */
135 if (version
>= 0x200)
136 writel(MMCI_BF(BLKLEN
, blklen
), &mci
->blkr
);
138 if (mmc
->card_caps
& mmc
->cfg
->host_caps
& MMC_MODE_HS
)
139 writel(MMCI_BIT(HSMODE
), &mci
->cfg
);
141 priv
->initialized
= 1;
144 /* Return the CMDR with flags for a given command and data packet */
145 static u32
mci_encode_cmd(
146 struct mmc_cmd
*cmd
, struct mmc_data
*data
, u32
* error_flags
)
150 /* Default Flags for Errors */
151 *error_flags
|= (MMCI_BIT(DTOE
) | MMCI_BIT(RDIRE
) | MMCI_BIT(RENDE
) |
152 MMCI_BIT(RINDE
) | MMCI_BIT(RTOE
));
154 /* Default Flags for the Command */
155 cmdr
|= MMCI_BIT(MAXLAT
);
158 cmdr
|= MMCI_BF(TRCMD
, 1);
159 if (data
->blocks
> 1)
160 cmdr
|= MMCI_BF(TRTYP
, 1);
161 if (data
->flags
& MMC_DATA_READ
)
162 cmdr
|= MMCI_BIT(TRDIR
);
165 if (cmd
->resp_type
& MMC_RSP_CRC
)
166 *error_flags
|= MMCI_BIT(RCRCE
);
167 if (cmd
->resp_type
& MMC_RSP_136
)
168 cmdr
|= MMCI_BF(RSPTYP
, 2);
169 else if (cmd
->resp_type
& MMC_RSP_BUSY
)
170 cmdr
|= MMCI_BF(RSPTYP
, 3);
171 else if (cmd
->resp_type
& MMC_RSP_PRESENT
)
172 cmdr
|= MMCI_BF(RSPTYP
, 1);
174 return cmdr
| MMCI_BF(CMDNB
, cmd
->cmdidx
);
177 /* Entered into function pointer in mci_send_cmd */
178 static u32
mci_data_read(atmel_mci_t
*mci
, u32
* data
, u32 error_flags
)
183 status
= readl(&mci
->sr
);
184 if (status
& (error_flags
| MMCI_BIT(OVRE
)))
186 } while (!(status
& MMCI_BIT(RXRDY
)));
188 if (status
& MMCI_BIT(RXRDY
)) {
189 *data
= readl(&mci
->rdr
);
196 /* Entered into function pointer in mci_send_cmd */
197 static u32
mci_data_write(atmel_mci_t
*mci
, u32
* data
, u32 error_flags
)
202 status
= readl(&mci
->sr
);
203 if (status
& (error_flags
| MMCI_BIT(UNRE
)))
205 } while (!(status
& MMCI_BIT(TXRDY
)));
207 if (status
& MMCI_BIT(TXRDY
)) {
208 writel(*data
, &mci
->tdr
);
216 * Entered into mmc structure during driver init
218 * Sends a command out on the bus and deals with the block data.
219 * Takes the mmc pointer, a command pointer, and an optional data pointer.
222 static int atmel_mci_send_cmd(struct udevice
*dev
, struct mmc_cmd
*cmd
,
223 struct mmc_data
*data
)
225 struct atmel_mci_priv
*priv
= dev_get_priv(dev
);
226 struct mmc
*mmc
= mmc_get_mmc_dev(dev
);
229 mci_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
, struct mmc_data
*data
)
231 struct atmel_mci_priv
*priv
= mmc
->priv
;
233 atmel_mci_t
*mci
= priv
->mci
;
238 if (!priv
->initialized
) {
239 puts ("MCI not initialized!\n");
243 /* Figure out the transfer arguments */
244 cmdr
= mci_encode_cmd(cmd
, data
, &error_flags
);
246 /* For multi blocks read/write, set the block register */
247 if ((cmd
->cmdidx
== MMC_CMD_READ_MULTIPLE_BLOCK
)
248 || (cmd
->cmdidx
== MMC_CMD_WRITE_MULTIPLE_BLOCK
))
249 writel(data
->blocks
| MMCI_BF(BLKLEN
, mmc
->read_bl_len
),
252 /* Send the command */
253 writel(cmd
->cmdarg
, &mci
->argr
);
254 writel(cmdr
, &mci
->cmdr
);
257 dump_cmd(cmdr
, cmd
->cmdarg
, 0, "DEBUG");
260 /* Wait for the command to complete */
261 while (!((status
= readl(&mci
->sr
)) & MMCI_BIT(CMDRDY
)));
263 if ((status
& error_flags
) & MMCI_BIT(RTOE
)) {
264 dump_cmd(cmdr
, cmd
->cmdarg
, status
, "Command Time Out");
266 } else if (status
& error_flags
) {
267 dump_cmd(cmdr
, cmd
->cmdarg
, status
, "Command Failed");
271 /* Copy the response to the response buffer */
272 if (cmd
->resp_type
& MMC_RSP_136
) {
273 cmd
->response
[0] = readl(&mci
->rspr
);
274 cmd
->response
[1] = readl(&mci
->rspr1
);
275 cmd
->response
[2] = readl(&mci
->rspr2
);
276 cmd
->response
[3] = readl(&mci
->rspr3
);
278 cmd
->response
[0] = readl(&mci
->rspr
);
280 /* transfer all of the blocks */
282 u32 word_count
, block_count
;
284 u32 sys_blocksize
, dummy
, i
;
286 (atmel_mci_t
*mci
, u32
* data
, u32 error_flags
);
288 if (data
->flags
& MMC_DATA_READ
) {
289 mci_data_op
= mci_data_read
;
290 sys_blocksize
= mmc
->read_bl_len
;
291 ioptr
= (u32
*)data
->dest
;
293 mci_data_op
= mci_data_write
;
294 sys_blocksize
= mmc
->write_bl_len
;
295 ioptr
= (u32
*)data
->src
;
299 for (block_count
= 0;
300 block_count
< data
->blocks
&& !status
;
304 status
= mci_data_op(mci
, ioptr
, error_flags
);
307 } while (!status
&& word_count
< (data
->blocksize
/4));
309 if (data
->flags
& MMC_DATA_READ
)
311 u32 cnt
= word_count
* 4;
312 printf("Read Data:\n");
313 print_buffer(0, data
->dest
+ cnt
* block_count
,
318 if (!status
&& word_count
< (sys_blocksize
/ 4))
319 printf("filling rest of block...\n");
321 /* fill the rest of a full block */
322 while (!status
&& word_count
< (sys_blocksize
/ 4)) {
323 status
= mci_data_op(mci
, &dummy
,
328 dump_cmd(cmdr
, cmd
->cmdarg
, status
,
329 "Data Transfer Failed");
334 /* Wait for Transfer End */
337 status
= readl(&mci
->sr
);
339 if (status
& error_flags
) {
340 dump_cmd(cmdr
, cmd
->cmdarg
, status
,
345 } while ((status
& MMCI_BIT(DTIP
)) && i
< 10000);
346 if (status
& MMCI_BIT(DTIP
)) {
347 dump_cmd(cmdr
, cmd
->cmdarg
, status
,
348 "XFER DTIP never unset, ignoring");
353 * After the switch command, wait for 8 clocks before the next
356 if (cmd
->cmdidx
== MMC_CMD_SWITCH
)
357 udelay(8*1000000 / priv
->curr_clk
); /* 8 clk in us */
363 static int atmel_mci_set_ios(struct udevice
*dev
)
365 struct atmel_mci_priv
*priv
= dev_get_priv(dev
);
366 struct mmc
*mmc
= mmc_get_mmc_dev(dev
);
368 /* Entered into mmc structure during driver init */
369 static int mci_set_ios(struct mmc
*mmc
)
371 struct atmel_mci_priv
*priv
= mmc
->priv
;
373 atmel_mci_t
*mci
= priv
->mci
;
374 int bus_width
= mmc
->bus_width
;
375 unsigned int version
= atmel_mci_get_version(mci
);
378 /* Set the clock speed */
380 mci_set_mode(priv
, mmc
->clock
, MMC_DEFAULT_BLKLEN
);
382 mci_set_mode(mmc
, mmc
->clock
, MMC_DEFAULT_BLKLEN
);
386 * set the bus width and select slot for this interface
387 * there is no capability for multiple slots on the same interface yet
389 if ((version
& 0xf00) >= 0x300) {
402 writel(busw
<< 6 | MMCI_BF(SCDSEL
, MCI_BUS
), &mci
->sdcr
);
404 busw
= (bus_width
== 4) ? 1 : 0;
406 writel(busw
<< 7 | MMCI_BF(SCDSEL
, MCI_BUS
), &mci
->sdcr
);
413 static int atmel_mci_hw_init(struct atmel_mci_priv
*priv
)
416 /* Entered into mmc structure during driver init */
417 static int mci_init(struct mmc
*mmc
)
419 struct atmel_mci_priv
*priv
= mmc
->priv
;
421 atmel_mci_t
*mci
= priv
->mci
;
423 /* Initialize controller */
424 writel(MMCI_BIT(SWRST
), &mci
->cr
); /* soft reset */
425 writel(MMCI_BIT(PWSDIS
), &mci
->cr
); /* disable power save */
426 writel(MMCI_BIT(MCIEN
), &mci
->cr
); /* enable mci */
427 writel(MMCI_BF(SCDSEL
, MCI_BUS
), &mci
->sdcr
); /* select port */
429 /* This delay can be optimized, but stick with max value */
430 writel(0x7f, &mci
->dtor
);
431 /* Disable Interrupts */
432 writel(~0UL, &mci
->idr
);
434 /* Set default clocks and blocklen */
436 mci_set_mode(priv
, CONFIG_SYS_MMC_CLK_OD
, MMC_DEFAULT_BLKLEN
);
438 mci_set_mode(mmc
, CONFIG_SYS_MMC_CLK_OD
, MMC_DEFAULT_BLKLEN
);
444 #ifndef CONFIG_DM_MMC
445 static const struct mmc_ops atmel_mci_ops
= {
446 .send_cmd
= mci_send_cmd
,
447 .set_ios
= mci_set_ios
,
452 * This is the only exported function
454 * Call it with the MCI register base address
456 int atmel_mci_init(void *regs
)
459 struct mmc_config
*cfg
;
460 struct atmel_mci_priv
*priv
;
461 unsigned int version
;
463 priv
= calloc(1, sizeof(*priv
));
470 cfg
->ops
= &atmel_mci_ops
;
472 priv
->mci
= (struct atmel_mci
*)regs
;
473 priv
->initialized
= 0;
475 /* need to be able to pass these in on a board by board basis */
476 cfg
->voltages
= MMC_VDD_32_33
| MMC_VDD_33_34
;
477 version
= atmel_mci_get_version(priv
->mci
);
478 if ((version
& 0xf00) >= 0x300) {
479 cfg
->host_caps
= MMC_MODE_8BIT
;
480 cfg
->host_caps
|= MMC_MODE_HS
| MMC_MODE_HS_52MHz
;
483 cfg
->host_caps
|= MMC_MODE_4BIT
;
486 * min and max frequencies determined by
487 * max and min of clock divider
489 cfg
->f_min
= get_mci_clk_rate() / (2*256);
490 cfg
->f_max
= get_mci_clk_rate() / (2*1);
492 cfg
->b_max
= CONFIG_SYS_MMC_MAX_BLK_COUNT
;
494 mmc
= mmc_create(cfg
, priv
);
500 /* NOTE: possibly leaking the priv structure */
507 static const struct dm_mmc_ops atmel_mci_mmc_ops
= {
508 .send_cmd
= atmel_mci_send_cmd
,
509 .set_ios
= atmel_mci_set_ios
,
512 static void atmel_mci_setup_cfg(struct atmel_mci_priv
*priv
)
514 struct mmc_config
*cfg
;
518 cfg
->name
= "Atmel mci";
519 cfg
->voltages
= MMC_VDD_32_33
| MMC_VDD_33_34
;
522 * If the version is above 3.0, the capabilities of the 8-bit
523 * bus width and high speed are supported.
525 version
= atmel_mci_get_version(priv
->mci
);
526 if ((version
& 0xf00) >= 0x300) {
527 cfg
->host_caps
= MMC_MODE_8BIT
|
528 MMC_MODE_HS
| MMC_MODE_HS_52MHz
;
531 cfg
->host_caps
|= MMC_MODE_4BIT
;
532 cfg
->b_max
= CONFIG_SYS_MMC_MAX_BLK_COUNT
;
533 cfg
->f_min
= priv
->bus_clk_rate
/ (2 * 256);
534 cfg
->f_max
= priv
->bus_clk_rate
/ 2;
537 static int atmel_mci_enable_clk(struct udevice
*dev
)
539 struct atmel_mci_priv
*priv
= dev_get_priv(dev
);
544 ret
= clk_get_by_index(dev
, 0, &clk
);
550 ret
= clk_enable(&clk
);
554 clk_rate
= clk_get_rate(&clk
);
560 priv
->bus_clk_rate
= clk_rate
;
568 static int atmel_mci_probe(struct udevice
*dev
)
570 struct mmc_uclass_priv
*upriv
= dev_get_uclass_priv(dev
);
571 struct atmel_mci_priv
*priv
= dev_get_priv(dev
);
575 ret
= atmel_mci_enable_clk(dev
);
579 priv
->mci
= (struct atmel_mci
*)devfdt_get_addr_ptr(dev
);
581 atmel_mci_setup_cfg(priv
);
584 mmc
->cfg
= &priv
->cfg
;
588 atmel_mci_hw_init(priv
);
593 static int atmel_mci_bind(struct udevice
*dev
)
595 struct atmel_mci_priv
*priv
= dev_get_priv(dev
);
597 return mmc_bind(dev
, &priv
->mmc
, &priv
->cfg
);
600 static const struct udevice_id atmel_mci_ids
[] = {
601 { .compatible
= "atmel,hsmci" },
605 U_BOOT_DRIVER(atmel_mci
) = {
608 .of_match
= atmel_mci_ids
,
609 .bind
= atmel_mci_bind
,
610 .probe
= atmel_mci_probe
,
611 .priv_auto_alloc_size
= sizeof(struct atmel_mci_priv
),
612 .ops
= &atmel_mci_mmc_ops
,