]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/mmc/davinci_mmc.c
2 * Davinci MMC Controller Driver
4 * Copyright (C) 2010 Texas Instruments Incorporated
6 * SPDX-License-Identifier: GPL-2.0+
16 #include <asm/arch/sdmmc_defs.h>
18 #define DAVINCI_MAX_BLOCKS (32)
19 #define WATCHDOG_COUNT (100000)
21 #define get_val(addr) REG(addr)
22 #define set_val(addr, val) REG(addr) = (val)
23 #define set_bit(addr, val) set_val((addr), (get_val(addr) | (val)))
24 #define clear_bit(addr, val) set_val((addr), (get_val(addr) & ~(val)))
26 /* Set davinci clock prescalar value based on the required clock in HZ */
27 static void dmmc_set_clock(struct mmc
*mmc
, uint clock
)
29 struct davinci_mmc
*host
= mmc
->priv
;
30 struct davinci_mmc_regs
*regs
= host
->reg_base
;
31 uint clkrt
, sysclk2
, act_clock
;
33 if (clock
< mmc
->cfg
->f_min
)
34 clock
= mmc
->cfg
->f_min
;
35 if (clock
> mmc
->cfg
->f_max
)
36 clock
= mmc
->cfg
->f_max
;
38 set_val(®s
->mmcclk
, 0);
39 sysclk2
= host
->input_clk
;
40 clkrt
= (sysclk2
/ (2 * clock
)) - 1;
42 /* Calculate the actual clock for the divider used */
43 act_clock
= (sysclk2
/ (2 * (clkrt
+ 1)));
45 /* Adjust divider if actual clock exceeds the required clock */
46 if (act_clock
> clock
)
49 /* check clock divider boundary and correct it */
53 set_val(®s
->mmcclk
, (clkrt
| MMCCLK_CLKEN
));
56 /* Status bit wait loop for MMCST1 */
58 dmmc_wait_fifo_status(volatile struct davinci_mmc_regs
*regs
, uint status
)
60 uint wdog
= WATCHDOG_COUNT
;
62 while (--wdog
&& ((get_val(®s
->mmcst1
) & status
) != status
))
65 if (!(get_val(®s
->mmcctl
) & MMCCTL_WIDTH_4_BIT
))
74 /* Busy bit wait loop for MMCST1 */
75 static int dmmc_busy_wait(volatile struct davinci_mmc_regs
*regs
)
77 uint wdog
= WATCHDOG_COUNT
;
79 while (--wdog
&& (get_val(®s
->mmcst1
) & MMCST1_BUSY
))
88 /* Status bit wait loop for MMCST0 - Checks for error bits as well */
89 static int dmmc_check_status(volatile struct davinci_mmc_regs
*regs
,
90 uint
*cur_st
, uint st_ready
, uint st_error
)
92 uint wdog
= WATCHDOG_COUNT
;
93 uint mmcstatus
= *cur_st
;
96 if (mmcstatus
& st_ready
) {
98 mmcstatus
= get_val(®s
->mmcst1
);
100 } else if (mmcstatus
& st_error
) {
101 if (mmcstatus
& MMCST0_TOUTRS
)
103 printf("[ ST0 ERROR %x]\n", mmcstatus
);
105 * Ignore CRC errors as some MMC cards fail to
106 * initialize on DM365-EVM on the SD1 slot
108 if (mmcstatus
& MMCST0_CRCRS
)
114 mmcstatus
= get_val(®s
->mmcst0
);
117 printf("Status %x Timeout ST0:%x ST1:%x\n", st_ready
, mmcstatus
,
118 get_val(®s
->mmcst1
));
123 * Sends a command out on the bus. Takes the mmc pointer,
124 * a command pointer, and an optional data pointer.
127 dmmc_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
, struct mmc_data
*data
)
129 struct davinci_mmc
*host
= mmc
->priv
;
130 volatile struct davinci_mmc_regs
*regs
= host
->reg_base
;
131 uint mmcstatus
, status_rdy
, status_err
;
132 uint i
, cmddata
, bytes_left
= 0;
133 int fifo_words
, fifo_bytes
, err
;
134 char *data_buf
= NULL
;
136 /* Clear status registers */
137 mmcstatus
= get_val(®s
->mmcst0
);
138 fifo_words
= (host
->version
== MMC_CTLR_VERSION_2
) ? 16 : 8;
139 fifo_bytes
= fifo_words
<< 2;
141 /* Wait for any previous busy signal to be cleared */
142 dmmc_busy_wait(regs
);
144 cmddata
= cmd
->cmdidx
;
145 cmddata
|= MMCCMD_PPLEN
;
147 /* Send init clock for CMD0 */
148 if (cmd
->cmdidx
== MMC_CMD_GO_IDLE_STATE
)
149 cmddata
|= MMCCMD_INITCK
;
151 switch (cmd
->resp_type
) {
153 cmddata
|= MMCCMD_BSYEXP
;
155 case MMC_RSP_R1
: /* R1, R1b, R5, R6, R7 */
156 cmddata
|= MMCCMD_RSPFMT_R1567
;
159 cmddata
|= MMCCMD_RSPFMT_R2
;
161 case MMC_RSP_R3
: /* R3, R4 */
162 cmddata
|= MMCCMD_RSPFMT_R3
;
166 set_val(®s
->mmcim
, 0);
169 /* clear previous data transfer if any and set new one */
170 bytes_left
= (data
->blocksize
* data
->blocks
);
172 /* Reset FIFO - Always use 32 byte fifo threshold */
173 set_val(®s
->mmcfifoctl
,
174 (MMCFIFOCTL_FIFOLEV
| MMCFIFOCTL_FIFORST
));
176 if (host
->version
== MMC_CTLR_VERSION_2
)
177 cmddata
|= MMCCMD_DMATRIG
;
179 cmddata
|= MMCCMD_WDATX
;
180 if (data
->flags
== MMC_DATA_READ
) {
181 set_val(®s
->mmcfifoctl
, MMCFIFOCTL_FIFOLEV
);
182 } else if (data
->flags
== MMC_DATA_WRITE
) {
183 set_val(®s
->mmcfifoctl
,
184 (MMCFIFOCTL_FIFOLEV
|
185 MMCFIFOCTL_FIFODIR
));
186 cmddata
|= MMCCMD_DTRW
;
189 set_val(®s
->mmctod
, 0xFFFF);
190 set_val(®s
->mmcnblk
, (data
->blocks
& MMCNBLK_NBLK_MASK
));
191 set_val(®s
->mmcblen
, (data
->blocksize
& MMCBLEN_BLEN_MASK
));
193 if (data
->flags
== MMC_DATA_WRITE
) {
195 data_buf
= (char *)data
->src
;
196 /* For write, fill FIFO with data before issue of CMD */
197 for (i
= 0; (i
< fifo_words
) && bytes_left
; i
++) {
198 memcpy((char *)&val
, data_buf
, 4);
199 set_val(®s
->mmcdxr
, val
);
205 set_val(®s
->mmcblen
, 0);
206 set_val(®s
->mmcnblk
, 0);
209 set_val(®s
->mmctor
, 0x1FFF);
211 /* Send the command */
212 set_val(®s
->mmcarghl
, cmd
->cmdarg
);
213 set_val(®s
->mmccmd
, cmddata
);
215 status_rdy
= MMCST0_RSPDNE
;
216 status_err
= (MMCST0_TOUTRS
| MMCST0_TOUTRD
|
217 MMCST0_CRCWR
| MMCST0_CRCRD
);
218 if (cmd
->resp_type
& MMC_RSP_CRC
)
219 status_err
|= MMCST0_CRCRS
;
221 mmcstatus
= get_val(®s
->mmcst0
);
222 err
= dmmc_check_status(regs
, &mmcstatus
, status_rdy
, status_err
);
226 /* For R1b wait for busy done */
227 if (cmd
->resp_type
== MMC_RSP_R1b
)
228 dmmc_busy_wait(regs
);
230 /* Collect response from controller for specific commands */
231 if (mmcstatus
& MMCST0_RSPDNE
) {
232 /* Copy the response to the response buffer */
233 if (cmd
->resp_type
& MMC_RSP_136
) {
234 cmd
->response
[0] = get_val(®s
->mmcrsp67
);
235 cmd
->response
[1] = get_val(®s
->mmcrsp45
);
236 cmd
->response
[2] = get_val(®s
->mmcrsp23
);
237 cmd
->response
[3] = get_val(®s
->mmcrsp01
);
238 } else if (cmd
->resp_type
& MMC_RSP_PRESENT
) {
239 cmd
->response
[0] = get_val(®s
->mmcrsp67
);
246 if (data
->flags
== MMC_DATA_READ
) {
247 /* check for DATDNE along with DRRDY as the controller might
248 * set the DATDNE without DRRDY for smaller transfers with
249 * less than FIFO threshold bytes
251 status_rdy
= MMCST0_DRRDY
| MMCST0_DATDNE
;
252 status_err
= MMCST0_TOUTRD
| MMCST0_CRCRD
;
253 data_buf
= data
->dest
;
255 status_rdy
= MMCST0_DXRDY
| MMCST0_DATDNE
;
256 status_err
= MMCST0_CRCWR
;
259 /* Wait until all of the blocks are transferred */
261 err
= dmmc_check_status(regs
, &mmcstatus
, status_rdy
,
266 if (data
->flags
== MMC_DATA_READ
) {
268 * MMC controller sets the Data receive ready bit
269 * (DRRDY) in MMCST0 even before the entire FIFO is
270 * full. This results in erratic behavior if we start
271 * reading the FIFO soon after DRRDY. Wait for the
272 * FIFO full bit in MMCST1 for proper FIFO clearing.
274 if (bytes_left
> fifo_bytes
)
275 dmmc_wait_fifo_status(regs
, 0x4a);
276 else if (bytes_left
== fifo_bytes
) {
277 dmmc_wait_fifo_status(regs
, 0x40);
278 if (cmd
->cmdidx
== MMC_CMD_SEND_EXT_CSD
)
282 for (i
= 0; bytes_left
&& (i
< fifo_words
); i
++) {
283 cmddata
= get_val(®s
->mmcdrr
);
284 memcpy(data_buf
, (char *)&cmddata
, 4);
290 * MMC controller sets the Data transmit ready bit
291 * (DXRDY) in MMCST0 even before the entire FIFO is
292 * empty. This results in erratic behavior if we start
293 * writing the FIFO soon after DXRDY. Wait for the
294 * FIFO empty bit in MMCST1 for proper FIFO clearing.
296 dmmc_wait_fifo_status(regs
, MMCST1_FIFOEMP
);
297 for (i
= 0; bytes_left
&& (i
< fifo_words
); i
++) {
298 memcpy((char *)&cmddata
, data_buf
, 4);
299 set_val(®s
->mmcdxr
, cmddata
);
303 dmmc_busy_wait(regs
);
307 err
= dmmc_check_status(regs
, &mmcstatus
, MMCST0_DATDNE
, status_err
);
314 /* Initialize Davinci MMC controller */
315 static int dmmc_init(struct mmc
*mmc
)
317 struct davinci_mmc
*host
= mmc
->priv
;
318 struct davinci_mmc_regs
*regs
= host
->reg_base
;
320 /* Clear status registers explicitly - soft reset doesn't clear it
321 * If Uboot is invoked from UBL with SDMMC Support, the status
322 * registers can have uncleared bits
324 get_val(®s
->mmcst0
);
325 get_val(®s
->mmcst1
);
327 /* Hold software reset */
328 set_bit(®s
->mmcctl
, MMCCTL_DATRST
);
329 set_bit(®s
->mmcctl
, MMCCTL_CMDRST
);
332 set_val(®s
->mmcclk
, 0x0);
333 set_val(®s
->mmctor
, 0x1FFF);
334 set_val(®s
->mmctod
, 0xFFFF);
336 /* Clear software reset */
337 clear_bit(®s
->mmcctl
, MMCCTL_DATRST
);
338 clear_bit(®s
->mmcctl
, MMCCTL_CMDRST
);
342 /* Reset FIFO - Always use the maximum fifo threshold */
343 set_val(®s
->mmcfifoctl
, (MMCFIFOCTL_FIFOLEV
| MMCFIFOCTL_FIFORST
));
344 set_val(®s
->mmcfifoctl
, MMCFIFOCTL_FIFOLEV
);
349 /* Set buswidth or clock as indicated by the GENERIC_MMC framework */
350 static void dmmc_set_ios(struct mmc
*mmc
)
352 struct davinci_mmc
*host
= mmc
->priv
;
353 struct davinci_mmc_regs
*regs
= host
->reg_base
;
355 /* Set the bus width */
356 if (mmc
->bus_width
== 4)
357 set_bit(®s
->mmcctl
, MMCCTL_WIDTH_4_BIT
);
359 clear_bit(®s
->mmcctl
, MMCCTL_WIDTH_4_BIT
);
361 /* Set clock speed */
363 dmmc_set_clock(mmc
, mmc
->clock
);
366 static const struct mmc_ops dmmc_ops
= {
367 .send_cmd
= dmmc_send_cmd
,
368 .set_ios
= dmmc_set_ios
,
372 /* Called from board_mmc_init during startup. Can be called multiple times
373 * depending on the number of slots available on board and controller
375 int davinci_mmc_init(bd_t
*bis
, struct davinci_mmc
*host
)
377 host
->cfg
.name
= "davinci";
378 host
->cfg
.ops
= &dmmc_ops
;
379 host
->cfg
.f_min
= 200000;
380 host
->cfg
.f_max
= 25000000;
381 host
->cfg
.voltages
= host
->voltages
;
382 host
->cfg
.host_caps
= host
->host_caps
;
384 host
->cfg
.b_max
= DAVINCI_MAX_BLOCKS
;
386 mmc_create(&host
->cfg
, host
);