blob: 5f78dcefa82d964bdabf513848fbcc0865eca164 [file] [log] [blame]
Haojian Zhuangfffe9e72016-03-18 22:08:26 +08001/*
2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef __EMMC_H__
32#define __EMMC_H__
33
34#include <stdint.h>
35
36#define EMMC_BLOCK_SIZE 512
37#define EMMC_BLOCK_MASK (EMMC_BLOCK_SIZE - 1)
38#define EMMC_BOOT_CLK_RATE (400 * 1000)
39
40#define EMMC_CMD0 0
41#define EMMC_CMD1 1
42#define EMMC_CMD2 2
43#define EMMC_CMD3 3
44#define EMMC_CMD6 6
45#define EMMC_CMD7 7
46#define EMMC_CMD8 8
47#define EMMC_CMD9 9
48#define EMMC_CMD12 12
49#define EMMC_CMD13 13
50#define EMMC_CMD17 17
51#define EMMC_CMD18 18
Haojian Zhuangbd5cf9c2016-08-02 20:51:27 +080052#define EMMC_CMD23 23
Haojian Zhuangfffe9e72016-03-18 22:08:26 +080053#define EMMC_CMD24 24
54#define EMMC_CMD25 25
55#define EMMC_CMD35 35
56#define EMMC_CMD36 36
57#define EMMC_CMD38 38
58
59#define OCR_POWERUP (1 << 31)
60#define OCR_BYTE_MODE (0 << 29)
61#define OCR_SECTOR_MODE (2 << 29)
62#define OCR_ACCESS_MODE_MASK (3 << 29)
63#define OCR_VDD_MIN_2V7 (0x1ff << 15)
64#define OCR_VDD_MIN_2V0 (0x7f << 8)
65#define OCR_VDD_MIN_1V7 (1 << 7)
66
67#define EMMC_RESPONSE_R1 1
68#define EMMC_RESPONSE_R1B 1
69#define EMMC_RESPONSE_R2 4
70#define EMMC_RESPONSE_R3 1
71#define EMMC_RESPONSE_R4 1
72#define EMMC_RESPONSE_R5 1
73
74#define EMMC_FIX_RCA 6 /* > 1 */
75#define RCA_SHIFT_OFFSET 16
76
77#define CMD_EXTCSD_PARTITION_CONFIG 179
78#define CMD_EXTCSD_BUS_WIDTH 183
79#define CMD_EXTCSD_HS_TIMING 185
80
81#define PART_CFG_BOOT_PARTITION1_ENABLE (1 << 3)
82#define PART_CFG_PARTITION1_ACCESS (1 << 0)
83
84/* values in EXT CSD register */
85#define EMMC_BUS_WIDTH_1 0
86#define EMMC_BUS_WIDTH_4 1
87#define EMMC_BUS_WIDTH_8 2
88#define EMMC_BOOT_MODE_BACKWARD (0 << 3)
89#define EMMC_BOOT_MODE_HS_TIMING (1 << 3)
90#define EMMC_BOOT_MODE_DDR (2 << 3)
91
92#define EXTCSD_SET_CMD (0 << 24)
93#define EXTCSD_SET_BITS (1 << 24)
94#define EXTCSD_CLR_BITS (2 << 24)
95#define EXTCSD_WRITE_BYTES (3 << 24)
96#define EXTCSD_CMD(x) (((x) & 0xff) << 16)
97#define EXTCSD_VALUE(x) (((x) & 0xff) << 8)
98
99#define STATUS_CURRENT_STATE(x) (((x) & 0xf) << 9)
100#define STATUS_READY_FOR_DATA (1 << 8)
101#define STATUS_SWITCH_ERROR (1 << 7)
102#define EMMC_GET_STATE(x) (((x) >> 9) & 0xf)
103#define EMMC_STATE_IDLE 0
104#define EMMC_STATE_READY 1
105#define EMMC_STATE_IDENT 2
106#define EMMC_STATE_STBY 3
107#define EMMC_STATE_TRAN 4
108#define EMMC_STATE_DATA 5
109#define EMMC_STATE_RCV 6
110#define EMMC_STATE_PRG 7
111#define EMMC_STATE_DIS 8
112#define EMMC_STATE_BTST 9
113#define EMMC_STATE_SLP 10
114
Haojian Zhuangbd5cf9c2016-08-02 20:51:27 +0800115#define EMMC_FLAG_CMD23 (1 << 0)
116
Haojian Zhuangfffe9e72016-03-18 22:08:26 +0800117typedef struct emmc_cmd {
118 unsigned int cmd_idx;
119 unsigned int cmd_arg;
120 unsigned int resp_type;
121 unsigned int resp_data[4];
122} emmc_cmd_t;
123
124typedef struct emmc_ops {
125 void (*init)(void);
126 int (*send_cmd)(emmc_cmd_t *cmd);
127 int (*set_ios)(int clk, int width);
128 int (*prepare)(int lba, uintptr_t buf, size_t size);
129 int (*read)(int lba, uintptr_t buf, size_t size);
130 int (*write)(int lba, const uintptr_t buf, size_t size);
131} emmc_ops_t;
132
133typedef struct emmc_csd {
134 unsigned char not_used: 1;
135 unsigned char crc: 7;
136 unsigned char ecc: 2;
137 unsigned char file_format: 2;
138 unsigned char tmp_write_protect: 1;
139 unsigned char perm_write_protect: 1;
140 unsigned char copy: 1;
141 unsigned char file_format_grp: 1;
142
143 unsigned short reserved_1: 5;
144 unsigned short write_bl_partial: 1;
145 unsigned short write_bl_len: 4;
146 unsigned short r2w_factor: 3;
147 unsigned short default_ecc: 2;
148 unsigned short wp_grp_enable: 1;
149
150 unsigned int wp_grp_size: 5;
151 unsigned int erase_grp_mult: 5;
152 unsigned int erase_grp_size: 5;
153 unsigned int c_size_mult: 3;
154 unsigned int vdd_w_curr_max: 3;
155 unsigned int vdd_w_curr_min: 3;
156 unsigned int vdd_r_curr_max: 3;
157 unsigned int vdd_r_curr_min: 3;
158 unsigned int c_size_low: 2;
159
160 unsigned int c_size_high: 10;
161 unsigned int reserved_2: 2;
162 unsigned int dsr_imp: 1;
163 unsigned int read_blk_misalign: 1;
164 unsigned int write_blk_misalign: 1;
165 unsigned int read_bl_partial: 1;
166 unsigned int read_bl_len: 4;
167 unsigned int ccc: 12;
168
169 unsigned int tran_speed: 8;
170 unsigned int nsac: 8;
171 unsigned int taac: 8;
172 unsigned int reserved_3: 2;
173 unsigned int spec_vers: 4;
174 unsigned int csd_structure: 2;
175} emmc_csd_t;
176
177size_t emmc_read_blocks(int lba, uintptr_t buf, size_t size);
178size_t emmc_write_blocks(int lba, const uintptr_t buf, size_t size);
179size_t emmc_erase_blocks(int lba, size_t size);
180size_t emmc_rpmb_read_blocks(int lba, uintptr_t buf, size_t size);
181size_t emmc_rpmb_write_blocks(int lba, const uintptr_t buf, size_t size);
182size_t emmc_rpmb_erase_blocks(int lba, size_t size);
Haojian Zhuangbd5cf9c2016-08-02 20:51:27 +0800183void emmc_init(const emmc_ops_t *ops, int clk, int bus_width,
184 unsigned int flags);
Haojian Zhuangfffe9e72016-03-18 22:08:26 +0800185
186#endif /* __EMMC_H__ */