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