386fb1e4ec1159949af170bbb1b6e1195f4d6d68
[project/bcm63xx/atf.git] / drivers / renesas / rcar / emmc / emmc_init.c
1 /*
2 * Copyright (c) 2015-2018, Renesas Electronics Corporation. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <stddef.h>
8 #include <mmio.h>
9 #include "emmc_config.h"
10 #include "emmc_hal.h"
11 #include "emmc_std.h"
12 #include "emmc_registers.h"
13 #include "emmc_def.h"
14 #include "rcar_private.h"
15
16 st_mmc_base mmc_drv_obj;
17
18 EMMC_ERROR_CODE rcar_emmc_memcard_power(uint8_t mode)
19 {
20
21 if (mode == TRUE) {
22 /* power on (Vcc&Vccq is always power on) */
23 mmc_drv_obj.card_power_enable = TRUE;
24 } else {
25 /* power off (Vcc&Vccq is always power on) */
26 mmc_drv_obj.card_power_enable = FALSE;
27 mmc_drv_obj.mount = FALSE;
28 mmc_drv_obj.selected = FALSE;
29 }
30
31 return EMMC_SUCCESS;
32 }
33 static __inline void emmc_set_retry_count(uint32_t retry)
34 {
35 mmc_drv_obj.retries_after_fail = retry;
36 }
37
38 static __inline void emmc_set_data_timeout(uint32_t data_timeout)
39 {
40 mmc_drv_obj.data_timeout = data_timeout;
41 }
42
43 static void emmc_memset(uint8_t *buff, uint8_t data, uint32_t cnt)
44 {
45 if (buff == NULL) {
46 return;
47 }
48
49 while (cnt > 0) {
50 *buff++ = data;
51 cnt--;
52 }
53 }
54
55 static void emmc_driver_config(void)
56 {
57 emmc_set_retry_count(EMMC_RETRY_COUNT);
58 emmc_set_data_timeout(EMMC_RW_DATA_TIMEOUT);
59 }
60
61 static void emmc_drv_init(void)
62 {
63 emmc_memset((uint8_t *) (&mmc_drv_obj), 0, sizeof(st_mmc_base));
64 mmc_drv_obj.card_present = HAL_MEMCARD_CARD_IS_IN;
65 mmc_drv_obj.data_timeout = EMMC_RW_DATA_TIMEOUT;
66 mmc_drv_obj.bus_width = HAL_MEMCARD_DATA_WIDTH_1_BIT;
67 }
68
69 static EMMC_ERROR_CODE emmc_dev_finalize(void)
70 {
71 EMMC_ERROR_CODE result;
72 uint32_t dataL;
73
74 /* MMC power off
75 * the power supply of eMMC device is always turning on.
76 * RST_n : Hi --> Low level.
77 */
78 result = rcar_emmc_memcard_power(FALSE);
79
80 /* host controller reset */
81 SETR_32(SD_INFO1, 0x00000000U); /* all interrupt clear */
82 SETR_32(SD_INFO2, SD_INFO2_CLEAR); /* all interrupt clear */
83 SETR_32(SD_INFO1_MASK, 0x00000000U); /* all interrupt disable */
84 SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR); /* all interrupt disable */
85 SETR_32(SD_CLK_CTRL, 0x00000000U); /* MMC clock stop */
86
87 dataL = mmio_read_32(CPG_SMSTPCR3);
88 if ((dataL & CPG_MSTP_MMC) == 0U) {
89 dataL |= (CPG_MSTP_MMC);
90 mmio_write_32(CPG_CPGWPR, (~dataL));
91 mmio_write_32(CPG_SMSTPCR3, dataL);
92 }
93
94 return result;
95 }
96
97 static EMMC_ERROR_CODE emmc_dev_init(void)
98 {
99 /* Enable clock supply to eMMC. */
100 mstpcr_write(CPG_SMSTPCR3, CPG_MSTPSR3, CPG_MSTP_MMC);
101
102 /* Set SD clock */
103 mmio_write_32(CPG_CPGWPR, ~((uint32_t) (BIT9 | BIT0))); /* SD phy 200MHz */
104
105 /* Stop SDnH clock & SDn=200MHz */
106 mmio_write_32(CPG_SDxCKCR, (BIT9 | BIT0));
107
108 /* MMCIF initialize */
109 SETR_32(SD_INFO1, 0x00000000U); /* all interrupt clear */
110 SETR_32(SD_INFO2, SD_INFO2_CLEAR); /* all interrupt clear */
111 SETR_32(SD_INFO1_MASK, 0x00000000U); /* all interrupt disable */
112 SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR); /* all interrupt disable */
113
114 SETR_32(HOST_MODE, 0x00000000U); /* SD_BUF access width = 64-bit */
115 SETR_32(SD_OPTION, 0x0000C0EEU); /* Bus width = 1bit, timeout=MAX */
116 SETR_32(SD_CLK_CTRL, 0x00000000U); /* Automatic Control=Disable, Clock Output=Disable */
117
118 return EMMC_SUCCESS;
119 }
120
121 static EMMC_ERROR_CODE emmc_reset_controller(void)
122 {
123 EMMC_ERROR_CODE retult;
124
125 /* initialize mmc driver */
126 emmc_drv_init();
127
128 /* initialize H/W */
129 retult = emmc_dev_init();
130 if (EMMC_SUCCESS != retult) {
131 return retult;
132 }
133
134 mmc_drv_obj.initialize = TRUE;
135
136 return retult;
137
138 }
139
140 EMMC_ERROR_CODE emmc_terminate(void)
141 {
142 EMMC_ERROR_CODE result;
143
144 result = emmc_dev_finalize();
145
146 emmc_memset((uint8_t *) (&mmc_drv_obj), 0, sizeof(st_mmc_base));
147
148 return result;
149 }
150
151 EMMC_ERROR_CODE rcar_emmc_init(void)
152 {
153 EMMC_ERROR_CODE retult;
154
155 retult = emmc_reset_controller();
156 if (EMMC_SUCCESS != retult) {
157 return retult;
158 }
159
160 emmc_driver_config();
161
162 return EMMC_SUCCESS;
163 }