blob: 61d449590f6f33f7f638e1728778cde8c660a8dc [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
52#define EMMC_CMD24 24
53#define EMMC_CMD25 25
54#define EMMC_CMD35 35
55#define EMMC_CMD36 36
56#define EMMC_CMD38 38
57
58#define OCR_POWERUP (1 << 31)
59#define OCR_BYTE_MODE (0 << 29)
60#define OCR_SECTOR_MODE (2 << 29)
61#define OCR_ACCESS_MODE_MASK (3 << 29)
62#define OCR_VDD_MIN_2V7 (0x1ff << 15)
63#define OCR_VDD_MIN_2V0 (0x7f << 8)
64#define OCR_VDD_MIN_1V7 (1 << 7)
65
66#define EMMC_RESPONSE_R1 1
67#define EMMC_RESPONSE_R1B 1
68#define EMMC_RESPONSE_R2 4
69#define EMMC_RESPONSE_R3 1
70#define EMMC_RESPONSE_R4 1
71#define EMMC_RESPONSE_R5 1
72
73#define EMMC_FIX_RCA 6 /* > 1 */
74#define RCA_SHIFT_OFFSET 16
75
76#define CMD_EXTCSD_PARTITION_CONFIG 179
77#define CMD_EXTCSD_BUS_WIDTH 183
78#define CMD_EXTCSD_HS_TIMING 185
79
80#define PART_CFG_BOOT_PARTITION1_ENABLE (1 << 3)
81#define PART_CFG_PARTITION1_ACCESS (1 << 0)
82
83/* values in EXT CSD register */
84#define EMMC_BUS_WIDTH_1 0
85#define EMMC_BUS_WIDTH_4 1
86#define EMMC_BUS_WIDTH_8 2
87#define EMMC_BOOT_MODE_BACKWARD (0 << 3)
88#define EMMC_BOOT_MODE_HS_TIMING (1 << 3)
89#define EMMC_BOOT_MODE_DDR (2 << 3)
90
91#define EXTCSD_SET_CMD (0 << 24)
92#define EXTCSD_SET_BITS (1 << 24)
93#define EXTCSD_CLR_BITS (2 << 24)
94#define EXTCSD_WRITE_BYTES (3 << 24)
95#define EXTCSD_CMD(x) (((x) & 0xff) << 16)
96#define EXTCSD_VALUE(x) (((x) & 0xff) << 8)
97
98#define STATUS_CURRENT_STATE(x) (((x) & 0xf) << 9)
99#define STATUS_READY_FOR_DATA (1 << 8)
100#define STATUS_SWITCH_ERROR (1 << 7)
101#define EMMC_GET_STATE(x) (((x) >> 9) & 0xf)
102#define EMMC_STATE_IDLE 0
103#define EMMC_STATE_READY 1
104#define EMMC_STATE_IDENT 2
105#define EMMC_STATE_STBY 3
106#define EMMC_STATE_TRAN 4
107#define EMMC_STATE_DATA 5
108#define EMMC_STATE_RCV 6
109#define EMMC_STATE_PRG 7
110#define EMMC_STATE_DIS 8
111#define EMMC_STATE_BTST 9
112#define EMMC_STATE_SLP 10
113
114typedef struct emmc_cmd {
115 unsigned int cmd_idx;
116 unsigned int cmd_arg;
117 unsigned int resp_type;
118 unsigned int resp_data[4];
119} emmc_cmd_t;
120
121typedef struct emmc_ops {
122 void (*init)(void);
123 int (*send_cmd)(emmc_cmd_t *cmd);
124 int (*set_ios)(int clk, int width);
125 int (*prepare)(int lba, uintptr_t buf, size_t size);
126 int (*read)(int lba, uintptr_t buf, size_t size);
127 int (*write)(int lba, const uintptr_t buf, size_t size);
128} emmc_ops_t;
129
130typedef struct emmc_csd {
131 unsigned char not_used: 1;
132 unsigned char crc: 7;
133 unsigned char ecc: 2;
134 unsigned char file_format: 2;
135 unsigned char tmp_write_protect: 1;
136 unsigned char perm_write_protect: 1;
137 unsigned char copy: 1;
138 unsigned char file_format_grp: 1;
139
140 unsigned short reserved_1: 5;
141 unsigned short write_bl_partial: 1;
142 unsigned short write_bl_len: 4;
143 unsigned short r2w_factor: 3;
144 unsigned short default_ecc: 2;
145 unsigned short wp_grp_enable: 1;
146
147 unsigned int wp_grp_size: 5;
148 unsigned int erase_grp_mult: 5;
149 unsigned int erase_grp_size: 5;
150 unsigned int c_size_mult: 3;
151 unsigned int vdd_w_curr_max: 3;
152 unsigned int vdd_w_curr_min: 3;
153 unsigned int vdd_r_curr_max: 3;
154 unsigned int vdd_r_curr_min: 3;
155 unsigned int c_size_low: 2;
156
157 unsigned int c_size_high: 10;
158 unsigned int reserved_2: 2;
159 unsigned int dsr_imp: 1;
160 unsigned int read_blk_misalign: 1;
161 unsigned int write_blk_misalign: 1;
162 unsigned int read_bl_partial: 1;
163 unsigned int read_bl_len: 4;
164 unsigned int ccc: 12;
165
166 unsigned int tran_speed: 8;
167 unsigned int nsac: 8;
168 unsigned int taac: 8;
169 unsigned int reserved_3: 2;
170 unsigned int spec_vers: 4;
171 unsigned int csd_structure: 2;
172} emmc_csd_t;
173
174size_t emmc_read_blocks(int lba, uintptr_t buf, size_t size);
175size_t emmc_write_blocks(int lba, const uintptr_t buf, size_t size);
176size_t emmc_erase_blocks(int lba, size_t size);
177size_t emmc_rpmb_read_blocks(int lba, uintptr_t buf, size_t size);
178size_t emmc_rpmb_write_blocks(int lba, const uintptr_t buf, size_t size);
179size_t emmc_rpmb_erase_blocks(int lba, size_t size);
180void emmc_init(const emmc_ops_t *ops, int clk, int bus_width);
181
182#endif /* __EMMC_H__ */