blob: 834a80f4ac39fcd8ec5488f87c3324d672067546 [file] [log] [blame]
Yann Gautier1e5e85a2018-07-03 18:32:12 +02001/*
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +02002 * Copyright (c) 2021, ARM Limited and Contributors. All rights reserved.
Yann Gautier1e5e85a2018-07-03 18:32:12 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +00007#ifndef MMC_H
8#define MMC_H
Yann Gautier1e5e85a2018-07-03 18:32:12 +02009
10#include <stdint.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
12#include <lib/utils_def.h>
Yann Gautier1e5e85a2018-07-03 18:32:12 +020013
14#define MMC_BLOCK_SIZE U(512)
15#define MMC_BLOCK_MASK (MMC_BLOCK_SIZE - U(1))
16#define MMC_BOOT_CLK_RATE (400 * 1000)
17
18#define MMC_CMD(_x) U(_x)
19
20#define MMC_ACMD(_x) U(_x)
21
22#define OCR_POWERUP BIT(31)
23#define OCR_HCS BIT(30)
24#define OCR_BYTE_MODE (U(0) << 29)
25#define OCR_SECTOR_MODE (U(2) << 29)
26#define OCR_ACCESS_MODE_MASK (U(3) << 29)
27#define OCR_3_5_3_6 BIT(23)
28#define OCR_3_4_3_5 BIT(22)
29#define OCR_3_3_3_4 BIT(21)
30#define OCR_3_2_3_3 BIT(20)
31#define OCR_3_1_3_2 BIT(19)
32#define OCR_3_0_3_1 BIT(18)
33#define OCR_2_9_3_0 BIT(17)
34#define OCR_2_8_2_9 BIT(16)
35#define OCR_2_7_2_8 BIT(15)
36#define OCR_VDD_MIN_2V7 GENMASK(23, 15)
37#define OCR_VDD_MIN_2V0 GENMASK(14, 8)
38#define OCR_VDD_MIN_1V7 BIT(7)
39
Jun Nie028cb122018-06-28 16:38:00 +080040#define MMC_RSP_48 BIT(0)
41#define MMC_RSP_136 BIT(1) /* 136 bit response */
42#define MMC_RSP_CRC BIT(2) /* expect valid crc */
43#define MMC_RSP_CMD_IDX BIT(3) /* response contains cmd idx */
44#define MMC_RSP_BUSY BIT(4) /* device may be busy */
45
46/* JEDEC 4.51 chapter 6.12 */
47#define MMC_RESPONSE_R1 (MMC_RSP_48 | MMC_RSP_CMD_IDX | MMC_RSP_CRC)
48#define MMC_RESPONSE_R1B (MMC_RESPONSE_R1 | MMC_RSP_BUSY)
Yann Gautierf2e8b162018-09-28 16:48:37 +020049#define MMC_RESPONSE_R2 (MMC_RSP_48 | MMC_RSP_136 | MMC_RSP_CRC)
Jun Nie028cb122018-06-28 16:38:00 +080050#define MMC_RESPONSE_R3 (MMC_RSP_48)
51#define MMC_RESPONSE_R4 (MMC_RSP_48)
Yann Gautierf2e8b162018-09-28 16:48:37 +020052#define MMC_RESPONSE_R5 (MMC_RSP_48 | MMC_RSP_CRC | MMC_RSP_CMD_IDX)
53#define MMC_RESPONSE_R6 (MMC_RSP_48 | MMC_RSP_CRC | MMC_RSP_CMD_IDX)
54#define MMC_RESPONSE_R7 (MMC_RSP_48 | MMC_RSP_CRC | MMC_RSP_CMD_IDX)
Yann Gautier1e5e85a2018-07-03 18:32:12 +020055
56/* Value randomly chosen for eMMC RCA, it should be > 1 */
57#define MMC_FIX_RCA 6
58#define RCA_SHIFT_OFFSET 16
59
60#define CMD_EXTCSD_PARTITION_CONFIG 179
61#define CMD_EXTCSD_BUS_WIDTH 183
62#define CMD_EXTCSD_HS_TIMING 185
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +020063#define CMD_EXTCSD_PART_SWITCH_TIME 199
Yann Gautier1e5e85a2018-07-03 18:32:12 +020064#define CMD_EXTCSD_SEC_CNT 212
65
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +020066#define EXT_CSD_PART_CONFIG_ACC_MASK GENMASK(2, 0)
Yann Gautier1e5e85a2018-07-03 18:32:12 +020067#define PART_CFG_BOOT_PARTITION1_ENABLE (U(1) << 3)
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +020068#define PART_CFG_BOOT_PARTITION1_ACCESS (U(1) << 0)
69#define PART_CFG_BOOT_PART_EN_MASK GENMASK(5, 3)
70#define PART_CFG_BOOT_PART_EN_SHIFT 3
71#define PART_CFG_CURRENT_BOOT_PARTITION(x) (((x) & PART_CFG_BOOT_PART_EN_MASK) >> \
72 PART_CFG_BOOT_PART_EN_SHIFT)
Yann Gautier1e5e85a2018-07-03 18:32:12 +020073
74/* Values in EXT CSD register */
75#define MMC_BUS_WIDTH_1 U(0)
76#define MMC_BUS_WIDTH_4 U(1)
77#define MMC_BUS_WIDTH_8 U(2)
78#define MMC_BUS_WIDTH_DDR_4 U(5)
79#define MMC_BUS_WIDTH_DDR_8 U(6)
80#define MMC_BOOT_MODE_BACKWARD (U(0) << 3)
81#define MMC_BOOT_MODE_HS_TIMING (U(1) << 3)
82#define MMC_BOOT_MODE_DDR (U(2) << 3)
83
84#define EXTCSD_SET_CMD (U(0) << 24)
85#define EXTCSD_SET_BITS (U(1) << 24)
86#define EXTCSD_CLR_BITS (U(2) << 24)
87#define EXTCSD_WRITE_BYTES (U(3) << 24)
88#define EXTCSD_CMD(x) (((x) & 0xff) << 16)
89#define EXTCSD_VALUE(x) (((x) & 0xff) << 8)
90#define EXTCSD_CMD_SET_NORMAL U(1)
91
92#define CSD_TRAN_SPEED_UNIT_MASK GENMASK(2, 0)
93#define CSD_TRAN_SPEED_MULT_MASK GENMASK(6, 3)
94#define CSD_TRAN_SPEED_MULT_SHIFT 3
95
96#define STATUS_CURRENT_STATE(x) (((x) & 0xf) << 9)
97#define STATUS_READY_FOR_DATA BIT(8)
98#define STATUS_SWITCH_ERROR BIT(7)
99#define MMC_GET_STATE(x) (((x) >> 9) & 0xf)
100#define MMC_STATE_IDLE 0
101#define MMC_STATE_READY 1
102#define MMC_STATE_IDENT 2
103#define MMC_STATE_STBY 3
104#define MMC_STATE_TRAN 4
105#define MMC_STATE_DATA 5
106#define MMC_STATE_RCV 6
107#define MMC_STATE_PRG 7
108#define MMC_STATE_DIS 8
109#define MMC_STATE_BTST 9
110#define MMC_STATE_SLP 10
111
112#define MMC_FLAG_CMD23 (U(1) << 0)
113
114#define CMD8_CHECK_PATTERN U(0xAA)
115#define VHS_2_7_3_6_V BIT(8)
116
117#define SD_SCR_BUS_WIDTH_1 BIT(8)
118#define SD_SCR_BUS_WIDTH_4 BIT(10)
119
120struct mmc_cmd {
121 unsigned int cmd_idx;
122 unsigned int cmd_arg;
123 unsigned int resp_type;
124 unsigned int resp_data[4];
125};
126
127struct mmc_ops {
128 void (*init)(void);
129 int (*send_cmd)(struct mmc_cmd *cmd);
130 int (*set_ios)(unsigned int clk, unsigned int width);
131 int (*prepare)(int lba, uintptr_t buf, size_t size);
132 int (*read)(int lba, uintptr_t buf, size_t size);
133 int (*write)(int lba, const uintptr_t buf, size_t size);
134};
135
136struct mmc_csd_emmc {
137 unsigned int not_used: 1;
138 unsigned int crc: 7;
139 unsigned int ecc: 2;
140 unsigned int file_format: 2;
141 unsigned int tmp_write_protect: 1;
142 unsigned int perm_write_protect: 1;
143 unsigned int copy: 1;
144 unsigned int file_format_grp: 1;
145
146 unsigned int reserved_1: 5;
147 unsigned int write_bl_partial: 1;
148 unsigned int write_bl_len: 4;
149 unsigned int r2w_factor: 3;
150 unsigned int default_ecc: 2;
151 unsigned int wp_grp_enable: 1;
152
153 unsigned int wp_grp_size: 5;
154 unsigned int erase_grp_mult: 5;
155 unsigned int erase_grp_size: 5;
156 unsigned int c_size_mult: 3;
157 unsigned int vdd_w_curr_max: 3;
158 unsigned int vdd_w_curr_min: 3;
159 unsigned int vdd_r_curr_max: 3;
160 unsigned int vdd_r_curr_min: 3;
161 unsigned int c_size_low: 2;
162
163 unsigned int c_size_high: 10;
164 unsigned int reserved_2: 2;
165 unsigned int dsr_imp: 1;
166 unsigned int read_blk_misalign: 1;
167 unsigned int write_blk_misalign: 1;
168 unsigned int read_bl_partial: 1;
169 unsigned int read_bl_len: 4;
170 unsigned int ccc: 12;
171
172 unsigned int tran_speed: 8;
173 unsigned int nsac: 8;
174 unsigned int taac: 8;
175 unsigned int reserved_3: 2;
176 unsigned int spec_vers: 4;
177 unsigned int csd_structure: 2;
178};
179
180struct mmc_csd_sd_v2 {
181 unsigned int not_used: 1;
182 unsigned int crc: 7;
183 unsigned int reserved_1: 2;
184 unsigned int file_format: 2;
185 unsigned int tmp_write_protect: 1;
186 unsigned int perm_write_protect: 1;
187 unsigned int copy: 1;
188 unsigned int file_format_grp: 1;
189
190 unsigned int reserved_2: 5;
191 unsigned int write_bl_partial: 1;
192 unsigned int write_bl_len: 4;
193 unsigned int r2w_factor: 3;
194 unsigned int reserved_3: 2;
195 unsigned int wp_grp_enable: 1;
196
197 unsigned int wp_grp_size: 7;
198 unsigned int sector_size: 7;
199 unsigned int erase_block_en: 1;
200 unsigned int reserved_4: 1;
201 unsigned int c_size_low: 16;
202
203 unsigned int c_size_high: 6;
204 unsigned int reserved_5: 6;
205 unsigned int dsr_imp: 1;
206 unsigned int read_blk_misalign: 1;
207 unsigned int write_blk_misalign: 1;
208 unsigned int read_bl_partial: 1;
209 unsigned int read_bl_len: 4;
210 unsigned int ccc: 12;
211
212 unsigned int tran_speed: 8;
213 unsigned int nsac: 8;
214 unsigned int taac: 8;
215 unsigned int reserved_6: 6;
216 unsigned int csd_structure: 2;
217};
218
219enum mmc_device_type {
220 MMC_IS_EMMC,
221 MMC_IS_SD,
222 MMC_IS_SD_HC,
223};
224
225struct mmc_device_info {
226 unsigned long long device_size; /* Size of device in bytes */
227 unsigned int block_size; /* Block size in bytes */
228 unsigned int max_bus_freq; /* Max bus freq in Hz */
Tien Hock, Loh313e9002019-03-07 11:34:20 +0800229 unsigned int ocr_voltage; /* OCR voltage */
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200230 enum mmc_device_type mmc_dev_type; /* Type of MMC */
231};
232
Haojian Zhuangd87f0b72018-08-02 14:49:51 +0800233size_t mmc_read_blocks(int lba, uintptr_t buf, size_t size);
234size_t mmc_write_blocks(int lba, const uintptr_t buf, size_t size);
235size_t mmc_erase_blocks(int lba, size_t size);
236size_t mmc_rpmb_read_blocks(int lba, uintptr_t buf, size_t size);
237size_t mmc_rpmb_write_blocks(int lba, const uintptr_t buf, size_t size);
238size_t mmc_rpmb_erase_blocks(int lba, size_t size);
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200239size_t mmc_boot_part_read_blocks(int lba, uintptr_t buf, size_t size);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200240int mmc_init(const struct mmc_ops *ops_ptr, unsigned int clk,
241 unsigned int width, unsigned int flags,
242 struct mmc_device_info *device_info);
243
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000244#endif /* MMC_H */